make detection of conditional assignment optional
[pet.git] / scop.c
blobda34e468a162440730ba0f4130fa607afdd70213
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;
895 return 1;
898 /* Return 1 if the two pet_stmts are equivalent.
900 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
902 int i;
904 if (!stmt1 || !stmt2)
905 return 0;
907 if (stmt1->line != stmt2->line)
908 return 0;
909 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
910 return 0;
911 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
912 return 0;
913 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
914 return 0;
915 if (stmt1->n_arg != stmt2->n_arg)
916 return 0;
917 for (i = 0; i < stmt1->n_arg; ++i) {
918 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
919 return 0;
922 return 1;
925 /* Return 1 if the two pet_scops are equivalent.
927 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
929 int i;
931 if (!scop1 || !scop2)
932 return 0;
934 if (!isl_set_is_equal(scop1->context, scop2->context))
935 return 0;
936 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
937 return 0;
939 if (scop1->n_array != scop2->n_array)
940 return 0;
941 for (i = 0; i < scop1->n_array; ++i)
942 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
943 return 0;
945 if (scop1->n_stmt != scop2->n_stmt)
946 return 0;
947 for (i = 0; i < scop1->n_stmt; ++i)
948 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
949 return 0;
951 return 1;
954 /* Prefix the schedule of "stmt" with an extra dimension with constant
955 * value "pos".
957 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
959 if (!stmt)
960 return NULL;
962 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
963 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
964 if (!stmt->schedule)
965 return pet_stmt_free(stmt);
967 return stmt;
970 /* Prefix the schedules of all statements in "scop" with an extra
971 * dimension with constant value "pos".
973 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
975 int i;
977 if (!scop)
978 return NULL;
980 for (i = 0; i < scop->n_stmt; ++i) {
981 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
982 if (!scop->stmts[i])
983 return pet_scop_free(scop);
986 return scop;
989 /* Data used in embed_access.
990 * extend adds an iterator to the iteration domain
991 * var_id represents the induction variable of the corresponding loop
993 struct pet_embed_access {
994 isl_map *extend;
995 isl_id *var_id;
998 /* Embed the access relation in an extra outer loop.
1000 * We first update the iteration domain to insert the extra dimension.
1002 * If the access refers to the induction variable, then it is
1003 * turned into an access to the set of integers with index (and value)
1004 * equal to the induction variable.
1006 * If the induction variable appears in the constraints (as a parameter),
1007 * then the parameter is equated to the newly introduced iteration
1008 * domain dimension and subsequently projected out.
1010 * Similarly, if the accessed array is a virtual array (with user
1011 * pointer equal to NULL), as created by create_test_access,
1012 * then it is extended along with the domain of the access.
1014 static __isl_give isl_map *embed_access(__isl_take isl_map *access,
1015 void *user)
1017 struct pet_embed_access *data = user;
1018 isl_id *array_id = NULL;
1019 int pos;
1021 access = update_domain(access, data->extend);
1023 if (isl_map_has_tuple_id(access, isl_dim_out))
1024 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1025 if (array_id == data->var_id ||
1026 (array_id && !isl_id_get_user(array_id))) {
1027 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1028 access = isl_map_equate(access,
1029 isl_dim_in, 0, isl_dim_out, 0);
1030 if (array_id != data->var_id)
1031 access = isl_map_set_tuple_id(access, isl_dim_out,
1032 isl_id_copy(array_id));
1034 isl_id_free(array_id);
1036 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1037 if (pos >= 0) {
1038 access = isl_map_equate(access,
1039 isl_dim_param, pos, isl_dim_in, 0);
1040 access = isl_map_project_out(access, isl_dim_param, pos, 1);
1042 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1043 isl_id_copy(data->var_id));
1045 return access;
1048 /* Embed all access relations in "expr" in an extra loop.
1049 * "extend" inserts an outer loop iterator in the iteration domains.
1050 * "var_id" represents the induction variable.
1052 static struct pet_expr *expr_embed(struct pet_expr *expr,
1053 __isl_take isl_map *extend, __isl_keep isl_id *var_id)
1055 struct pet_embed_access data = { .extend = extend, .var_id = var_id };
1057 expr = pet_expr_foreach_access(expr, &embed_access, &data);
1058 isl_map_free(extend);
1059 return expr;
1062 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1063 * "dom" and schedule "sched". "var_id" represents the induction variable
1064 * of the loop.
1066 * The iteration domain and schedule of the statement are updated
1067 * according to the iteration domain and schedule of the new loop.
1068 * If stmt->domain is a wrapped map, then the iteration domain
1069 * is the domain of this map, so we need to be careful to adjust
1070 * this domain.
1072 * If the induction variable appears in the constraints (as a parameter)
1073 * of the current iteration domain or the schedule of the statement,
1074 * then the parameter is equated to the newly introduced iteration
1075 * domain dimension and subsequently projected out.
1077 * Finally, all access relations are updated based on the extra loop.
1079 struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt, __isl_take isl_set *dom,
1080 __isl_take isl_map *sched, __isl_take isl_id *var_id)
1082 int i;
1083 int pos;
1084 isl_id *stmt_id;
1085 isl_space *dim;
1086 isl_map *extend;
1088 if (!stmt)
1089 goto error;
1091 if (isl_set_is_wrapping(stmt->domain)) {
1092 isl_map *map;
1093 isl_map *ext;
1094 isl_space *ran_dim;
1096 map = isl_set_unwrap(stmt->domain);
1097 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1098 ran_dim = isl_space_range(isl_map_get_space(map));
1099 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1100 isl_set_universe(ran_dim));
1101 map = isl_map_flat_domain_product(ext, map);
1102 map = isl_map_set_tuple_id(map, isl_dim_in,
1103 isl_id_copy(stmt_id));
1104 dim = isl_space_domain(isl_map_get_space(map));
1105 stmt->domain = isl_map_wrap(map);
1106 } else {
1107 stmt_id = isl_set_get_tuple_id(stmt->domain);
1108 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1109 stmt->domain);
1110 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1111 isl_id_copy(stmt_id));
1112 dim = isl_set_get_space(stmt->domain);
1115 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1116 if (pos >= 0) {
1117 stmt->domain = isl_set_equate(stmt->domain,
1118 isl_dim_param, pos, isl_dim_set, 0);
1119 stmt->domain = isl_set_project_out(stmt->domain,
1120 isl_dim_param, pos, 1);
1123 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1124 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1125 isl_dim_in, stmt_id);
1127 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1128 if (pos >= 0) {
1129 stmt->schedule = isl_map_equate(stmt->schedule,
1130 isl_dim_param, pos, isl_dim_in, 0);
1131 stmt->schedule = isl_map_project_out(stmt->schedule,
1132 isl_dim_param, pos, 1);
1135 dim = isl_space_map_from_set(dim);
1136 extend = isl_map_identity(dim);
1137 extend = isl_map_remove_dims(extend, isl_dim_in, 0, 1);
1138 extend = isl_map_set_tuple_id(extend, isl_dim_in,
1139 isl_map_get_tuple_id(extend, isl_dim_out));
1140 for (i = 0; i < stmt->n_arg; ++i)
1141 stmt->args[i] = expr_embed(stmt->args[i],
1142 isl_map_copy(extend), var_id);
1143 stmt->body = expr_embed(stmt->body, extend, var_id);
1145 isl_set_free(dom);
1146 isl_id_free(var_id);
1148 for (i = 0; i < stmt->n_arg; ++i)
1149 if (!stmt->args[i])
1150 return pet_stmt_free(stmt);
1151 if (!stmt->domain || !stmt->schedule || !stmt->body)
1152 return pet_stmt_free(stmt);
1153 return stmt;
1154 error:
1155 isl_set_free(dom);
1156 isl_map_free(sched);
1157 isl_id_free(var_id);
1158 return NULL;
1161 /* Embed the given pet_array in an extra outer loop with iteration domain
1162 * "dom".
1163 * This embedding only has an effect on virtual arrays (those with
1164 * user pointer equal to NULL), which need to be extended along with
1165 * the iteration domain.
1167 static struct pet_array *pet_array_embed(struct pet_array *array,
1168 __isl_take isl_set *dom)
1170 isl_id *array_id = NULL;
1172 if (!array)
1173 goto error;
1175 if (isl_set_has_tuple_id(array->extent))
1176 array_id = isl_set_get_tuple_id(array->extent);
1178 if (array_id && !isl_id_get_user(array_id)) {
1179 array->extent = isl_set_flat_product(dom, array->extent);
1180 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1181 } else {
1182 isl_set_free(dom);
1183 isl_id_free(array_id);
1186 return array;
1187 error:
1188 isl_set_free(dom);
1189 return NULL;
1192 /* Project out all unnamed parameters from "set" and return the result.
1194 static __isl_give isl_set *set_project_out_unnamed_params(
1195 __isl_take isl_set *set)
1197 int i, n;
1199 n = isl_set_dim(set, isl_dim_param);
1200 for (i = n - 1; i >= 0; --i) {
1201 if (isl_set_has_dim_name(set, isl_dim_param, i))
1202 continue;
1203 set = isl_set_project_out(set, isl_dim_param, i, 1);
1206 return set;
1209 /* Update the context with respect to an embedding into a loop
1210 * with iteration domain "dom" and induction variable "id".
1212 * If the current context is independent of "id", we don't need
1213 * to do anything.
1214 * Otherwise, a parameter value is invalid for the embedding if
1215 * any of the corresponding iterator values is invalid.
1216 * That is, a parameter value is valid only if all the corresponding
1217 * iterator values are valid.
1218 * We therefore compute the set of parameters
1220 * forall i in dom : valid (i)
1222 * or
1224 * not exists i in dom : not valid(i)
1226 * i.e.,
1228 * not exists i in dom \ valid(i)
1230 * If there are any unnamed parameters in "dom", then we consider
1231 * a parameter value to be valid if it is valid for any value of those
1232 * unnamed parameters. They are therefore projected out at the end.
1234 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1235 __isl_keep isl_set *dom, __isl_keep isl_id *id)
1237 int pos;
1239 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1240 if (pos < 0)
1241 return context;
1243 context = isl_set_from_params(context);
1244 context = isl_set_add_dims(context, isl_dim_set, 1);
1245 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1246 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1247 context = isl_set_subtract(isl_set_copy(dom), context);
1248 context = isl_set_params(context);
1249 context = isl_set_complement(context);
1250 context = set_project_out_unnamed_params(context);
1251 return context;
1254 /* Embed all statements and arrays in "scop" in an extra outer loop
1255 * with iteration domain "dom" and schedule "sched".
1256 * "id" represents the induction variable of the loop.
1258 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1259 __isl_take isl_map *sched, __isl_take isl_id *id)
1261 int i;
1263 if (!scop)
1264 goto error;
1266 scop->context = context_embed(scop->context, dom, id);
1267 if (!scop->context)
1268 goto error;
1270 for (i = 0; i < scop->n_stmt; ++i) {
1271 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1272 isl_set_copy(dom),
1273 isl_map_copy(sched), isl_id_copy(id));
1274 if (!scop->stmts[i])
1275 goto error;
1278 for (i = 0; i < scop->n_array; ++i) {
1279 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1280 isl_set_copy(dom));
1281 if (!scop->arrays[i])
1282 goto error;
1285 isl_set_free(dom);
1286 isl_map_free(sched);
1287 isl_id_free(id);
1288 return scop;
1289 error:
1290 isl_set_free(dom);
1291 isl_map_free(sched);
1292 isl_id_free(id);
1293 return pet_scop_free(scop);
1296 /* Add extra conditions on the parameters to iteration domain of "stmt".
1298 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1299 __isl_take isl_set *cond)
1301 if (!stmt)
1302 goto error;
1304 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1306 return stmt;
1307 error:
1308 isl_set_free(cond);
1309 return pet_stmt_free(stmt);
1312 /* Add extra conditions on the parameters to all iteration domains.
1314 * A parameter value is valid for the result if it was valid
1315 * for the original scop and satisfies "cond" or if it does
1316 * not satisfy "cond" as in this case the scop is not executed
1317 * and the original constraints on the parameters are irrelevant.
1319 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1320 __isl_take isl_set *cond)
1322 int i;
1324 if (!scop)
1325 goto error;
1327 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1328 scop->context = isl_set_union(scop->context,
1329 isl_set_complement(isl_set_copy(cond)));
1330 scop->context = isl_set_coalesce(scop->context);
1331 if (!scop->context)
1332 goto error;
1334 for (i = 0; i < scop->n_stmt; ++i) {
1335 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1336 isl_set_copy(cond));
1337 if (!scop->stmts[i])
1338 goto error;
1341 isl_set_free(cond);
1342 return scop;
1343 error:
1344 isl_set_free(cond);
1345 return pet_scop_free(scop);
1348 /* Make the statements "stmt" depend on the value of "test"
1349 * being equal to "satisfied" by adjusting stmt->domain.
1351 * We insert an argument corresponding to a read to "test"
1352 * from the iteration domain of "stmt" in front of the list of arguments.
1353 * We also insert a corresponding output dimension in the wrapped
1354 * map contained in stmt->domain, with value set to "satisfied".
1356 static struct pet_stmt *stmt_filter(struct pet_stmt *stmt,
1357 __isl_take isl_map *test, int satisfied)
1359 int i;
1360 isl_id *id;
1361 isl_ctx *ctx;
1362 isl_map *map;
1363 isl_set *dom;
1365 if (!stmt || !test)
1366 goto error;
1368 if (isl_set_is_wrapping(stmt->domain))
1369 map = isl_set_unwrap(stmt->domain);
1370 else
1371 map = isl_map_from_domain(stmt->domain);
1372 map = isl_map_insert_dims(map, isl_dim_out, 0, 1);
1373 id = isl_map_get_tuple_id(test, isl_dim_out);
1374 map = isl_map_set_dim_id(map, isl_dim_out, 0, id);
1375 map = isl_map_fix_si(map, isl_dim_out, 0, satisfied);
1376 dom = isl_set_universe(isl_space_domain(isl_map_get_space(map)));
1377 test = isl_map_apply_domain(test, isl_map_from_range(dom));
1379 stmt->domain = isl_map_wrap(map);
1381 ctx = isl_map_get_ctx(test);
1382 if (!stmt->args) {
1383 stmt->args = isl_calloc_array(ctx, struct pet_expr *, 1);
1384 if (!stmt->args)
1385 goto error;
1386 } else {
1387 struct pet_expr **args;
1388 args = isl_calloc_array(ctx, struct pet_expr *, 1 + stmt->n_arg);
1389 if (!args)
1390 goto error;
1391 for (i = 0; i < stmt->n_arg; ++i)
1392 args[1 + i] = stmt->args[i];
1393 free(stmt->args);
1394 stmt->args = args;
1396 stmt->n_arg++;
1397 stmt->args[0] = pet_expr_from_access(isl_map_copy(test));
1398 if (!stmt->args[0])
1399 goto error;
1401 isl_map_free(test);
1402 return stmt;
1403 error:
1404 isl_map_free(test);
1405 return pet_stmt_free(stmt);
1408 /* Make all statements in "scop" depend on the value of "test"
1409 * being equal to "satisfied" by adjusting their domains.
1411 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
1412 __isl_take isl_map *test, int satisfied)
1414 int i;
1416 if (!scop)
1417 goto error;
1419 for (i = 0; i < scop->n_stmt; ++i) {
1420 scop->stmts[i] = stmt_filter(scop->stmts[i],
1421 isl_map_copy(test), satisfied);
1422 if (!scop->stmts[i])
1423 goto error;
1426 isl_map_free(test);
1427 return scop;
1428 error:
1429 isl_map_free(test);
1430 return pet_scop_free(scop);
1433 /* Add all parameters in "expr" to "dim" and return the result.
1435 static __isl_give isl_space *expr_collect_params(struct pet_expr *expr,
1436 __isl_take isl_space *dim)
1438 int i;
1440 if (!expr)
1441 goto error;
1442 for (i = 0; i < expr->n_arg; ++i)
1444 dim = expr_collect_params(expr->args[i], dim);
1446 if (expr->type == pet_expr_access)
1447 dim = isl_space_align_params(dim,
1448 isl_map_get_space(expr->acc.access));
1450 return dim;
1451 error:
1452 isl_space_free(dim);
1453 return pet_expr_free(expr);
1456 /* Add all parameters in "stmt" to "dim" and return the result.
1458 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
1459 __isl_take isl_space *dim)
1461 if (!stmt)
1462 goto error;
1464 dim = isl_space_align_params(dim, isl_set_get_space(stmt->domain));
1465 dim = isl_space_align_params(dim, isl_map_get_space(stmt->schedule));
1466 dim = expr_collect_params(stmt->body, dim);
1468 return dim;
1469 error:
1470 isl_space_free(dim);
1471 return pet_stmt_free(stmt);
1474 /* Add all parameters in "array" to "dim" and return the result.
1476 static __isl_give isl_space *array_collect_params(struct pet_array *array,
1477 __isl_take isl_space *dim)
1479 if (!array)
1480 goto error;
1482 dim = isl_space_align_params(dim, isl_set_get_space(array->context));
1483 dim = isl_space_align_params(dim, isl_set_get_space(array->extent));
1485 return dim;
1486 error:
1487 isl_space_free(dim);
1488 return pet_array_free(array);
1491 /* Add all parameters in "scop" to "dim" and return the result.
1493 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
1494 __isl_take isl_space *dim)
1496 int i;
1498 if (!scop)
1499 goto error;
1501 for (i = 0; i < scop->n_array; ++i)
1502 dim = array_collect_params(scop->arrays[i], dim);
1504 for (i = 0; i < scop->n_stmt; ++i)
1505 dim = stmt_collect_params(scop->stmts[i], dim);
1507 return dim;
1508 error:
1509 isl_space_free(dim);
1510 return pet_scop_free(scop);
1513 /* Add all parameters in "dim" to all access relations in "expr".
1515 static struct pet_expr *expr_propagate_params(struct pet_expr *expr,
1516 __isl_take isl_space *dim)
1518 int i;
1520 if (!expr)
1521 goto error;
1523 for (i = 0; i < expr->n_arg; ++i) {
1524 expr->args[i] =
1525 expr_propagate_params(expr->args[i],
1526 isl_space_copy(dim));
1527 if (!expr->args[i])
1528 goto error;
1531 if (expr->type == pet_expr_access) {
1532 expr->acc.access = isl_map_align_params(expr->acc.access,
1533 isl_space_copy(dim));
1534 if (!expr->acc.access)
1535 goto error;
1538 isl_space_free(dim);
1539 return expr;
1540 error:
1541 isl_space_free(dim);
1542 return pet_expr_free(expr);
1545 /* Add all parameters in "dim" to the domain, schedule and
1546 * all access relations in "stmt".
1548 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
1549 __isl_take isl_space *dim)
1551 if (!stmt)
1552 goto error;
1554 stmt->domain = isl_set_align_params(stmt->domain, isl_space_copy(dim));
1555 stmt->schedule = isl_map_align_params(stmt->schedule,
1556 isl_space_copy(dim));
1557 stmt->body = expr_propagate_params(stmt->body, isl_space_copy(dim));
1559 if (!stmt->domain || !stmt->schedule || !stmt->body)
1560 goto error;
1562 isl_space_free(dim);
1563 return stmt;
1564 error:
1565 isl_space_free(dim);
1566 return pet_stmt_free(stmt);
1569 /* Add all parameters in "dim" to "array".
1571 static struct pet_array *array_propagate_params(struct pet_array *array,
1572 __isl_take isl_space *dim)
1574 if (!array)
1575 goto error;
1577 array->context = isl_set_align_params(array->context,
1578 isl_space_copy(dim));
1579 array->extent = isl_set_align_params(array->extent,
1580 isl_space_copy(dim));
1581 if (array->value_bounds) {
1582 array->value_bounds = isl_set_align_params(array->value_bounds,
1583 isl_space_copy(dim));
1584 if (!array->value_bounds)
1585 goto error;
1588 if (!array->context || !array->extent)
1589 goto error;
1591 isl_space_free(dim);
1592 return array;
1593 error:
1594 isl_space_free(dim);
1595 return pet_array_free(array);
1598 /* Add all parameters in "dim" to "scop".
1600 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
1601 __isl_take isl_space *dim)
1603 int i;
1605 if (!scop)
1606 goto error;
1608 for (i = 0; i < scop->n_array; ++i) {
1609 scop->arrays[i] = array_propagate_params(scop->arrays[i],
1610 isl_space_copy(dim));
1611 if (!scop->arrays[i])
1612 goto error;
1615 for (i = 0; i < scop->n_stmt; ++i) {
1616 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
1617 isl_space_copy(dim));
1618 if (!scop->stmts[i])
1619 goto error;
1622 isl_space_free(dim);
1623 return scop;
1624 error:
1625 isl_space_free(dim);
1626 return pet_scop_free(scop);
1629 /* Update all isl_sets and isl_maps in "scop" such that they all
1630 * have the same parameters.
1632 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
1634 isl_space *dim;
1636 if (!scop)
1637 return NULL;
1639 dim = isl_set_get_space(scop->context);
1640 dim = scop_collect_params(scop, dim);
1642 scop->context = isl_set_align_params(scop->context, isl_space_copy(dim));
1643 scop = scop_propagate_params(scop, dim);
1645 return scop;
1648 /* Check if the given access relation accesses a (0D) array that corresponds
1649 * to one of the parameters in "dim". If so, replace the array access
1650 * by an access to the set of integers with as index (and value)
1651 * that parameter.
1653 static __isl_give isl_map *access_detect_parameter(__isl_take isl_map *access,
1654 __isl_take isl_space *dim)
1656 isl_id *array_id = NULL;
1657 int pos = -1;
1659 if (isl_map_has_tuple_id(access, isl_dim_out)) {
1660 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1661 pos = isl_space_find_dim_by_id(dim, isl_dim_param, array_id);
1663 isl_space_free(dim);
1665 if (pos < 0) {
1666 isl_id_free(array_id);
1667 return access;
1670 pos = isl_map_find_dim_by_id(access, isl_dim_param, array_id);
1671 if (pos < 0) {
1672 access = isl_map_insert_dims(access, isl_dim_param, 0, 1);
1673 access = isl_map_set_dim_id(access, isl_dim_param, 0, array_id);
1674 pos = 0;
1675 } else
1676 isl_id_free(array_id);
1678 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1679 access = isl_map_equate(access, isl_dim_param, pos, isl_dim_out, 0);
1681 return access;
1684 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1685 * in "dim" by a value equal to the corresponding parameter.
1687 static struct pet_expr *expr_detect_parameter_accesses(struct pet_expr *expr,
1688 __isl_take isl_space *dim)
1690 int i;
1692 if (!expr)
1693 goto error;
1695 for (i = 0; i < expr->n_arg; ++i) {
1696 expr->args[i] =
1697 expr_detect_parameter_accesses(expr->args[i],
1698 isl_space_copy(dim));
1699 if (!expr->args[i])
1700 goto error;
1703 if (expr->type == pet_expr_access) {
1704 expr->acc.access = access_detect_parameter(expr->acc.access,
1705 isl_space_copy(dim));
1706 if (!expr->acc.access)
1707 goto error;
1710 isl_space_free(dim);
1711 return expr;
1712 error:
1713 isl_space_free(dim);
1714 return pet_expr_free(expr);
1717 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1718 * in "dim" by a value equal to the corresponding parameter.
1720 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
1721 __isl_take isl_space *dim)
1723 if (!stmt)
1724 goto error;
1726 stmt->body = expr_detect_parameter_accesses(stmt->body,
1727 isl_space_copy(dim));
1729 if (!stmt->domain || !stmt->schedule || !stmt->body)
1730 goto error;
1732 isl_space_free(dim);
1733 return stmt;
1734 error:
1735 isl_space_free(dim);
1736 return pet_stmt_free(stmt);
1739 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1740 * in "dim" by a value equal to the corresponding parameter.
1742 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
1743 __isl_take isl_space *dim)
1745 int i;
1747 if (!scop)
1748 goto error;
1750 for (i = 0; i < scop->n_stmt; ++i) {
1751 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
1752 isl_space_copy(dim));
1753 if (!scop->stmts[i])
1754 goto error;
1757 isl_space_free(dim);
1758 return scop;
1759 error:
1760 isl_space_free(dim);
1761 return pet_scop_free(scop);
1764 /* Replace all accesses to (0D) arrays that correspond to any of
1765 * the parameters used in "scop" by a value equal
1766 * to the corresponding parameter.
1768 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
1770 isl_space *dim;
1772 if (!scop)
1773 return NULL;
1775 dim = isl_set_get_space(scop->context);
1776 dim = scop_collect_params(scop, dim);
1778 scop = scop_detect_parameter_accesses(scop, dim);
1780 return scop;
1783 /* Add all read access relations (if "read" is set) and/or all write
1784 * access relations (if "write" is set) to "accesses" and return the result.
1786 static __isl_give isl_union_map *expr_collect_accesses(struct pet_expr *expr,
1787 int read, int write, __isl_take isl_union_map *accesses)
1789 int i;
1790 isl_id *id;
1791 isl_space *dim;
1793 if (!expr)
1794 return NULL;
1796 for (i = 0; i < expr->n_arg; ++i)
1797 accesses = expr_collect_accesses(expr->args[i],
1798 read, write, accesses);
1800 if (expr->type == pet_expr_access &&
1801 isl_map_has_tuple_id(expr->acc.access, isl_dim_out) &&
1802 ((read && expr->acc.read) || (write && expr->acc.write)))
1803 accesses = isl_union_map_add_map(accesses,
1804 isl_map_copy(expr->acc.access));
1806 return accesses;
1809 /* Collect and return all read access relations (if "read" is set)
1810 * and/or all write * access relations (if "write" is set) in "stmt".
1812 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
1813 int read, int write, __isl_take isl_space *dim)
1815 isl_union_map *accesses;
1817 if (!stmt)
1818 return NULL;
1820 accesses = isl_union_map_empty(dim);
1821 accesses = expr_collect_accesses(stmt->body, read, write, accesses);
1822 accesses = isl_union_map_intersect_domain(accesses,
1823 isl_union_set_from_set(isl_set_copy(stmt->domain)));
1825 return accesses;
1828 /* Collect and return all read access relations (if "read" is set)
1829 * and/or all write * access relations (if "write" is set) in "scop".
1831 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
1832 int read, int write)
1834 int i;
1835 isl_union_map *accesses;
1837 if (!scop)
1838 return NULL;
1840 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
1842 for (i = 0; i < scop->n_stmt; ++i) {
1843 isl_union_map *accesses_i;
1844 isl_space *dim = isl_set_get_space(scop->context);
1845 accesses_i = stmt_collect_accesses(scop->stmts[i],
1846 read, write, dim);
1847 accesses = isl_union_map_union(accesses, accesses_i);
1850 return accesses;
1853 __isl_give isl_union_map *pet_scop_collect_reads(struct pet_scop *scop)
1855 return scop_collect_accesses(scop, 1, 0);
1858 __isl_give isl_union_map *pet_scop_collect_writes(struct pet_scop *scop)
1860 return scop_collect_accesses(scop, 0, 1);
1863 /* Collect and return the union of iteration domains in "scop".
1865 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
1867 int i;
1868 isl_set *domain_i;
1869 isl_union_set *domain;
1871 if (!scop)
1872 return NULL;
1874 domain = isl_union_set_empty(isl_set_get_space(scop->context));
1876 for (i = 0; i < scop->n_stmt; ++i) {
1877 domain_i = isl_set_copy(scop->stmts[i]->domain);
1878 domain = isl_union_set_add_set(domain, domain_i);
1881 return domain;
1884 /* Collect and return the schedules of the statements in "scop".
1885 * The range is normalized to the maximal number of scheduling
1886 * dimensions.
1888 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
1890 int i, j;
1891 isl_map *schedule_i;
1892 isl_union_map *schedule;
1893 int depth, max_depth = 0;
1895 if (!scop)
1896 return NULL;
1898 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
1900 for (i = 0; i < scop->n_stmt; ++i) {
1901 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
1902 if (depth > max_depth)
1903 max_depth = depth;
1906 for (i = 0; i < scop->n_stmt; ++i) {
1907 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
1908 depth = isl_map_dim(schedule_i, isl_dim_out);
1909 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
1910 max_depth - depth);
1911 for (j = depth; j < max_depth; ++j)
1912 schedule_i = isl_map_fix_si(schedule_i,
1913 isl_dim_out, j, 0);
1914 schedule = isl_union_map_add_map(schedule, schedule_i);
1917 return schedule;
1920 /* Does expression "expr" write to "id"?
1922 static int expr_writes(struct pet_expr *expr, __isl_keep isl_id *id)
1924 int i;
1925 isl_id *write_id;
1927 for (i = 0; i < expr->n_arg; ++i) {
1928 int writes = expr_writes(expr->args[i], id);
1929 if (writes < 0 || writes)
1930 return writes;
1933 if (expr->type != pet_expr_access)
1934 return 0;
1935 if (!expr->acc.write)
1936 return 0;
1937 if (!isl_map_has_tuple_id(expr->acc.access, isl_dim_out))
1938 return 0;
1940 write_id = isl_map_get_tuple_id(expr->acc.access, isl_dim_out);
1941 isl_id_free(write_id);
1943 if (!write_id)
1944 return -1;
1946 return write_id == id;
1949 /* Does statement "stmt" write to "id"?
1951 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
1953 return expr_writes(stmt->body, id);
1956 /* Is there any write access in "scop" that accesses "id"?
1958 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
1960 int i;
1962 if (!scop)
1963 return -1;
1965 for (i = 0; i < scop->n_stmt; ++i) {
1966 int writes = stmt_writes(scop->stmts[i], id);
1967 if (writes < 0 || writes)
1968 return writes;
1971 return 0;
1974 /* Reset the user pointer on all parameter ids in "set".
1976 static __isl_give isl_set *set_anonymize(__isl_take isl_set *set)
1978 int i, n;
1980 n = isl_set_dim(set, isl_dim_param);
1981 for (i = 0; i < n; ++i) {
1982 isl_id *id = isl_set_get_dim_id(set, isl_dim_param, i);
1983 const char *name = isl_id_get_name(id);
1984 set = isl_set_set_dim_name(set, isl_dim_param, i, name);
1985 isl_id_free(id);
1988 return set;
1991 /* Reset the user pointer on all parameter ids in "map".
1993 static __isl_give isl_map *map_anonymize(__isl_take isl_map *map)
1995 int i, n;
1997 n = isl_map_dim(map, isl_dim_param);
1998 for (i = 0; i < n; ++i) {
1999 isl_id *id = isl_map_get_dim_id(map, isl_dim_param, i);
2000 const char *name = isl_id_get_name(id);
2001 map = isl_map_set_dim_name(map, isl_dim_param, i, name);
2002 isl_id_free(id);
2005 return map;
2008 /* Reset the user pointer on all parameter ids in "array".
2010 static struct pet_array *array_anonymize(struct pet_array *array)
2012 if (!array)
2013 return NULL;
2015 array->context = set_anonymize(array->context);
2016 array->extent = set_anonymize(array->extent);
2017 if (!array->context || !array->extent)
2018 return pet_array_free(array);
2020 return array;
2023 /* Reset the user pointer on all parameter ids in "access".
2025 static __isl_give isl_map *access_anonymize(__isl_take isl_map *access,
2026 void *user)
2028 access = map_anonymize(access);
2030 return access;
2033 /* Reset the user pointer on all parameter ids in "stmt".
2035 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2037 int i;
2038 isl_space *space;
2039 isl_set *domain;
2041 if (!stmt)
2042 return NULL;
2044 stmt->domain = set_anonymize(stmt->domain);
2045 stmt->schedule = map_anonymize(stmt->schedule);
2046 if (!stmt->domain || !stmt->schedule)
2047 return pet_stmt_free(stmt);
2049 for (i = 0; i < stmt->n_arg; ++i) {
2050 stmt->args[i] = pet_expr_foreach_access(stmt->args[i],
2051 &access_anonymize, NULL);
2052 if (!stmt->args[i])
2053 return pet_stmt_free(stmt);
2056 stmt->body = pet_expr_foreach_access(stmt->body,
2057 &access_anonymize, NULL);
2058 if (!stmt->body)
2059 return pet_stmt_free(stmt);
2061 return stmt;
2064 /* Reset the user pointer on all parameter ids in "scop".
2066 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2068 int i;
2070 if (!scop)
2071 return NULL;
2073 scop->context = set_anonymize(scop->context);
2074 scop->context_value = set_anonymize(scop->context_value);
2075 if (!scop->context || !scop->context_value)
2076 return pet_scop_free(scop);
2078 for (i = 0; i < scop->n_array; ++i) {
2079 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2080 if (!scop->arrays[i])
2081 return pet_scop_free(scop);
2084 for (i = 0; i < scop->n_stmt; ++i) {
2085 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2086 if (!scop->stmts[i])
2087 return pet_scop_free(scop);
2090 return scop;
2093 /* Given a set "domain", return a wrapped relation with the given set
2094 * as domain and a range of dimension "n_arg", where each coordinate
2095 * is either unbounded or, if the corresponding element of args is of
2096 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
2098 static __isl_give isl_set *apply_value_bounds(__isl_take isl_set *domain,
2099 unsigned n_arg, struct pet_expr **args,
2100 __isl_keep isl_union_map *value_bounds)
2102 int i;
2103 isl_map *map;
2104 isl_space *space;
2105 isl_ctx *ctx = isl_set_get_ctx(domain);
2107 map = isl_map_from_domain(domain);
2108 space = isl_map_get_space(map);
2109 space = isl_space_add_dims(space, isl_dim_out, 1);
2111 for (i = 0; i < n_arg; ++i) {
2112 isl_map *map_i;
2113 struct pet_expr *arg = args[i];
2114 isl_id *id;
2115 isl_space *space2;
2117 map_i = isl_map_universe(isl_space_copy(space));
2118 if (arg->type == pet_expr_access) {
2119 isl_map *vb;
2120 id = isl_map_get_tuple_id(arg->acc.access, isl_dim_out);
2121 space2 = isl_space_alloc(ctx, 0, 0, 1);
2122 space2 = isl_space_set_tuple_id(space2, isl_dim_in, id);
2123 vb = isl_union_map_extract_map(value_bounds, space2);
2124 if (!isl_map_plain_is_empty(vb))
2125 map_i = isl_map_intersect_range(map_i,
2126 isl_map_range(vb));
2127 else
2128 isl_map_free(vb);
2130 map = isl_map_flat_range_product(map, map_i);
2132 isl_space_free(space);
2134 return isl_map_wrap(map);
2137 /* Data used in access_gist() callback.
2139 struct pet_access_gist_data {
2140 isl_set *domain;
2141 isl_union_map *value_bounds;
2144 /* Given an expression "expr" of type pet_expr_access, compute
2145 * the gist of the associated access relation with respect to
2146 * data->domain and the bounds on the values of the arguments
2147 * of the expression.
2149 static struct pet_expr *access_gist(struct pet_expr *expr, void *user)
2151 struct pet_access_gist_data *data = user;
2152 isl_set *domain;
2154 domain = isl_set_copy(data->domain);
2155 if (expr->n_arg > 0)
2156 domain = apply_value_bounds(domain, expr->n_arg, expr->args,
2157 data->value_bounds);
2159 expr->acc.access = isl_map_gist_domain(expr->acc.access, domain);
2160 if (!expr->acc.access)
2161 return pet_expr_free(expr);
2163 return expr;
2166 /* Compute the gist of the iteration domain and all access relations
2167 * of "stmt" based on the constraints on the parameters specified by "context"
2168 * and the constraints on the values of nested accesses specified
2169 * by "value_bounds".
2171 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
2172 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2174 int i;
2175 isl_space *space;
2176 isl_set *domain;
2177 struct pet_access_gist_data data;
2179 if (!stmt)
2180 return NULL;
2182 data.domain = isl_set_copy(stmt->domain);
2183 data.value_bounds = value_bounds;
2184 if (stmt->n_arg > 0)
2185 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
2187 data.domain = isl_set_intersect_params(data.domain,
2188 isl_set_copy(context));
2190 for (i = 0; i < stmt->n_arg; ++i) {
2191 stmt->args[i] = pet_expr_foreach_access_expr(stmt->args[i],
2192 &access_gist, &data);
2193 if (!stmt->args[i])
2194 goto error;
2197 stmt->body = pet_expr_foreach_access_expr(stmt->body,
2198 &access_gist, &data);
2199 if (!stmt->body)
2200 goto error;
2202 isl_set_free(data.domain);
2204 space = isl_set_get_space(stmt->domain);
2205 if (isl_space_is_wrapping(space))
2206 space = isl_space_domain(isl_space_unwrap(space));
2207 domain = isl_set_universe(space);
2208 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2209 if (stmt->n_arg > 0)
2210 domain = apply_value_bounds(domain, stmt->n_arg, stmt->args,
2211 value_bounds);
2212 stmt->domain = isl_set_gist(stmt->domain, domain);
2213 if (!stmt->domain)
2214 return pet_stmt_free(stmt);
2216 return stmt;
2217 error:
2218 isl_set_free(data.domain);
2219 return pet_stmt_free(stmt);
2222 /* Compute the gist of the extent of the array
2223 * based on the constraints on the parameters specified by "context".
2225 static struct pet_array *array_gist(struct pet_array *array,
2226 __isl_keep isl_set *context)
2228 if (!array)
2229 return NULL;
2231 array->extent = isl_set_gist_params(array->extent,
2232 isl_set_copy(context));
2233 if (!array->extent)
2234 return pet_array_free(array);
2236 return array;
2239 /* Compute the gist of all sets and relations in "scop"
2240 * based on the constraints on the parameters specified by "scop->context"
2241 * and the constraints on the values of nested accesses specified
2242 * by "value_bounds".
2244 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
2245 __isl_keep isl_union_map *value_bounds)
2247 int i;
2249 if (!scop)
2250 return NULL;
2252 scop->context = isl_set_coalesce(scop->context);
2253 if (!scop->context)
2254 return pet_scop_free(scop);
2256 for (i = 0; i < scop->n_array; ++i) {
2257 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
2258 if (!scop->arrays[i])
2259 return pet_scop_free(scop);
2262 for (i = 0; i < scop->n_stmt; ++i) {
2263 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
2264 value_bounds);
2265 if (!scop->stmts[i])
2266 return pet_scop_free(scop);
2269 return scop;
2272 /* Intersect the context of "scop" with "context".
2273 * To ensure that we don't introduce any unnamed parameters in
2274 * the context of "scop", we first remove the unnamed parameters
2275 * from "context".
2277 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
2278 __isl_take isl_set *context)
2280 if (!scop)
2281 goto error;
2283 context = set_project_out_unnamed_params(context);
2284 scop->context = isl_set_intersect(scop->context, context);
2285 if (!scop->context)
2286 return pet_scop_free(scop);
2288 return scop;
2289 error:
2290 isl_set_free(context);
2291 return pet_scop_free(scop);