add pet_union_map_move_dims
[pet.git] / nest.c
blob9459a404538fbf406c7492f790a74134b1ecc2bb
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
35 #include <string.h>
37 #include "aff.h"
38 #include "expr.h"
39 #include "expr_arg.h"
40 #include "nest.h"
41 #include "scop.h"
42 #include "tree.h"
44 /* A wrapper around pet_expr_free to be used as an isl_id free user function.
46 static void pet_expr_free_wrap(void *user)
48 pet_expr_free((pet_expr *) user);
51 /* Create an isl_id that refers to the nested access "expr".
53 __isl_give isl_id *pet_nested_pet_expr(__isl_take pet_expr *expr)
55 isl_id *id;
57 id = isl_id_alloc(pet_expr_get_ctx(expr), "__pet_expr", expr);
58 id = isl_id_set_free_user(id, &pet_expr_free_wrap);
60 return id;
63 /* Extract a pet_expr from an isl_id created by pet_nested_pet_expr.
64 * Such an isl_id has name "__pet_expr" and
65 * the user pointer points to a pet_expr object.
67 __isl_give pet_expr *pet_nested_extract_expr(__isl_keep isl_id *id)
69 return pet_expr_copy((pet_expr *) isl_id_get_user(id));
72 /* Does "id" refer to a nested access created by pet_nested_pet_expr?
74 int pet_nested_in_id(__isl_keep isl_id *id)
76 const char *name;
78 if (!id)
79 return 0;
80 if (!isl_id_get_user(id))
81 return 0;
83 name = isl_id_get_name(id);
84 return !strcmp(name, "__pet_expr");
87 /* Does parameter "pos" of "space" refer to a nested access?
89 static int pet_nested_in_space(__isl_keep isl_space *space, int pos)
91 int nested;
92 isl_id *id;
94 id = isl_space_get_dim_id(space, isl_dim_param, pos);
95 nested = pet_nested_in_id(id);
96 isl_id_free(id);
98 return nested;
101 /* Does parameter "pos" of "set" refer to a nested access?
103 int pet_nested_in_set(__isl_keep isl_set *set, int pos)
105 int nested;
106 isl_id *id;
108 id = isl_set_get_dim_id(set, isl_dim_param, pos);
109 nested = pet_nested_in_id(id);
110 isl_id_free(id);
112 return nested;
115 /* Does parameter "pos" of "map" refer to a nested access?
117 int pet_nested_in_map(__isl_keep isl_map *map, int pos)
119 int nested;
120 isl_id *id;
122 id = isl_map_get_dim_id(map, isl_dim_param, pos);
123 nested = pet_nested_in_id(id);
124 isl_id_free(id);
126 return nested;
129 /* Does "space" involve any parameters that refer to nested accesses?
131 int pet_nested_any_in_space(__isl_keep isl_space *space)
133 int i;
134 int nparam;
136 nparam = isl_space_dim(space, isl_dim_param);
137 for (i = 0; i < nparam; ++i)
138 if (pet_nested_in_space(space, i))
139 return 1;
141 return 0;
144 /* Does "pa" involve any parameters that refer to nested accesses?
146 int pet_nested_any_in_pw_aff(__isl_keep isl_pw_aff *pa)
148 isl_space *space;
149 int nested;
151 space = isl_pw_aff_get_space(pa);
152 nested = pet_nested_any_in_space(space);
153 isl_space_free(space);
155 return nested;
158 /* How many parameters of "space" refer to nested accesses?
160 int pet_nested_n_in_space(__isl_keep isl_space *space)
162 int i, n = 0;
163 int nparam;
165 nparam = isl_space_dim(space, isl_dim_param);
166 for (i = 0; i < nparam; ++i)
167 if (pet_nested_in_space(space, i))
168 ++n;
170 return n;
173 /* How many parameters of "map" refer to nested accesses?
175 int pet_nested_n_in_map(__isl_keep isl_map *map)
177 isl_space *space;
178 int n;
180 space = isl_map_get_space(map);
181 n = pet_nested_n_in_space(space);
182 isl_space_free(space);
184 return n;
187 /* How many parameters of "set" refer to nested accesses?
189 int pet_nested_n_in_set(__isl_keep isl_set *set)
191 isl_space *space;
192 int n;
194 space = isl_set_get_space(set);
195 n = pet_nested_n_in_space(space);
196 isl_space_free(space);
198 return n;
201 /* Remove all parameters from "space" that refer to nested accesses.
203 __isl_give isl_space *pet_nested_remove_from_space(__isl_take isl_space *space)
205 int i;
206 int nparam;
208 nparam = isl_space_dim(space, isl_dim_param);
209 for (i = nparam - 1; i >= 0; --i)
210 if (pet_nested_in_space(space, i))
211 space = isl_space_drop_dims(space, isl_dim_param, i, 1);
213 return space;
216 /* Remove all parameters from "set" that refer to nested accesses.
218 __isl_give isl_set *pet_nested_remove_from_set(__isl_take isl_set *set)
220 int i;
221 int nparam;
223 nparam = isl_set_dim(set, isl_dim_param);
224 for (i = nparam - 1; i >= 0; --i)
225 if (pet_nested_in_set(set, i))
226 set = isl_set_project_out(set, isl_dim_param, i, 1);
228 return set;
231 /* Remove all parameters from "map" that refer to nested accesses.
233 static __isl_give isl_map *pet_nested_remove_from_map(__isl_take isl_map *map)
235 int i;
236 int nparam;
238 nparam = isl_map_dim(map, isl_dim_param);
239 for (i = nparam - 1; i >= 0; --i)
240 if (pet_nested_in_map(map, i))
241 map = isl_map_project_out(map, isl_dim_param, i, 1);
243 return map;
246 /* Remove all parameters from "mpa" that refer to nested accesses.
248 static __isl_give isl_multi_pw_aff *pet_nested_remove_from_multi_pw_aff(
249 __isl_take isl_multi_pw_aff *mpa)
251 int i;
252 int nparam;
253 isl_space *space;
255 space = isl_multi_pw_aff_get_space(mpa);
256 nparam = isl_space_dim(space, isl_dim_param);
257 for (i = nparam - 1; i >= 0; --i) {
258 if (!pet_nested_in_space(space, i))
259 continue;
260 mpa = isl_multi_pw_aff_drop_dims(mpa, isl_dim_param, i, 1);
262 isl_space_free(space);
264 return mpa;
267 /* Remove all parameters from the index expression and access relation of "expr"
268 * that refer to nested accesses.
270 static __isl_give pet_expr *expr_remove_nested_parameters(
271 __isl_take pet_expr *expr, void *user)
273 expr = pet_expr_cow(expr);
274 if (!expr)
275 return NULL;
277 if (expr->acc.access) {
278 expr->acc.access = pet_nested_remove_from_map(expr->acc.access);
279 if (!expr->acc.access)
280 expr->acc.index =
281 isl_multi_pw_aff_free(expr->acc.index);
283 expr->acc.index = pet_nested_remove_from_multi_pw_aff(expr->acc.index);
284 if (!expr->acc.index)
285 return pet_expr_free(expr);
287 return expr;
290 /* Remove all nested access parameters from the schedule and all
291 * accesses of "stmt".
292 * There is no need to remove them from the domain as these parameters
293 * have already been removed from the domain when this function is called.
295 struct pet_stmt *pet_stmt_remove_nested_parameters(struct pet_stmt *stmt)
297 int i;
299 if (!stmt)
300 return NULL;
301 stmt->schedule = pet_nested_remove_from_map(stmt->schedule);
302 stmt->body = pet_tree_map_access_expr(stmt->body,
303 &expr_remove_nested_parameters, NULL);
304 if (!stmt->schedule || !stmt->body)
305 goto error;
306 for (i = 0; i < stmt->n_arg; ++i) {
307 stmt->args[i] = pet_expr_map_access(stmt->args[i],
308 &expr_remove_nested_parameters, NULL);
309 if (!stmt->args[i])
310 goto error;
313 return stmt;
314 error:
315 pet_stmt_free(stmt);
316 return NULL;
319 /* Set *dim to the dimension of the domain of the access expression "expr" and
320 * abort the search.
322 static int set_dim(__isl_keep pet_expr *expr, void *user)
324 int *dim = user;
325 isl_space *space;
327 space = pet_expr_access_get_domain_space(expr);
328 *dim = isl_space_dim(space, isl_dim_set);
329 isl_space_free(space);
331 return -1;
334 /* Determine the dimension of the domain of the access expressions in "expr".
336 * In particular, return the dimension of the domain of the first access
337 * expression in "expr" as all access expressions should have the same
338 * domain.
340 * If "expr" does not contain any access expressions, then we return 0.
342 static int pet_expr_domain_dim(__isl_keep pet_expr *expr)
344 int dim = -1;
346 if (pet_expr_foreach_access_expr(expr, &set_dim, &dim) >= 0)
347 return 0;
349 return dim;
352 /* Embed all access expressions in "expr" in the domain "space".
353 * The initial domain of the access expressions
354 * is an anonymous domain of a dimension that may be lower
355 * than the dimension of "space".
356 * We may therefore need to introduce extra dimensions as well as
357 * (potentially) the name of "space".
359 static __isl_give pet_expr *embed(__isl_take pet_expr *expr,
360 __isl_keep isl_space *space)
362 int n;
363 isl_multi_pw_aff *mpa;
365 n = pet_expr_domain_dim(expr);
366 if (n < 0)
367 return pet_expr_free(expr);
369 space = isl_space_copy(space);
370 mpa = isl_multi_pw_aff_from_multi_aff(pet_prefix_projection(space, n));
371 expr = pet_expr_update_domain(expr, mpa);
373 return expr;
376 /* For each nested access parameter in "space",
377 * construct a corresponding pet_expr, place it in args and
378 * record its position in "param2pos".
379 * The constructed pet_expr objects are embedded in "space"
380 * (with the nested access parameters removed).
381 * "n_arg" is the number of elements that are already in args.
382 * The position recorded in "param2pos" takes this number into account.
383 * If the pet_expr corresponding to a parameter is identical to
384 * the pet_expr corresponding to an earlier parameter, then these two
385 * parameters are made to refer to the same element in args.
387 * Return the final number of elements in args or -1 if an error has occurred.
389 int pet_extract_nested_from_space(__isl_keep isl_space *space,
390 int n_arg, __isl_give pet_expr **args, int *param2pos)
392 int i, nparam;
393 isl_space *domain;
395 domain = isl_space_copy(space);
396 domain = pet_nested_remove_from_space(domain);
397 nparam = isl_space_dim(space, isl_dim_param);
398 for (i = 0; i < nparam; ++i) {
399 int j;
400 isl_id *id = isl_space_get_dim_id(space, isl_dim_param, i);
402 if (!pet_nested_in_id(id)) {
403 isl_id_free(id);
404 continue;
407 args[n_arg] = embed(pet_nested_extract_expr(id), domain);
408 isl_id_free(id);
409 if (!args[n_arg])
410 return -1;
412 for (j = 0; j < n_arg; ++j)
413 if (pet_expr_is_equal(args[j], args[n_arg]))
414 break;
416 if (j < n_arg) {
417 pet_expr_free(args[n_arg]);
418 args[n_arg] = NULL;
419 param2pos[i] = j;
420 } else
421 param2pos[i] = n_arg++;
423 isl_space_free(domain);
425 return n_arg;
428 /* For each nested access parameter in the access relations in "expr",
429 * construct a corresponding pet_expr, append it to the arguments of "expr"
430 * and record its position in "param2pos" (relative to the initial
431 * number of arguments).
432 * n is the number of nested access parameters.
434 __isl_give pet_expr *pet_expr_extract_nested(__isl_take pet_expr *expr, int n,
435 int *param2pos)
437 isl_ctx *ctx;
438 isl_space *space;
439 int i, n_arg;
440 pet_expr **args;
442 ctx = pet_expr_get_ctx(expr);
443 args = isl_calloc_array(ctx, pet_expr *, n);
444 if (!args)
445 return pet_expr_free(expr);
447 n_arg = pet_expr_get_n_arg(expr);
448 space = pet_expr_access_get_domain_space(expr);
449 n = pet_extract_nested_from_space(space, 0, args, param2pos);
450 isl_space_free(space);
452 if (n < 0)
453 expr = pet_expr_free(expr);
454 else
455 expr = pet_expr_set_n_arg(expr, n_arg + n);
457 for (i = 0; i < n; ++i)
458 expr = pet_expr_set_arg(expr, n_arg + i, args[i]);
459 free(args);
461 return expr;
464 /* Mark self dependences among the arguments of "expr" starting at "first".
465 * These arguments have already been added to the list of arguments
466 * but are not yet referenced directly from the index expression.
467 * Instead, they are still referenced through parameters encoding
468 * nested accesses.
470 * In particular, if "expr" is a read access, then check the arguments
471 * starting at "first" to see if "expr" accesses a subset of
472 * the elements accessed by the argument, or under more restrictive conditions.
473 * If so, then this nested access can be removed from the constraints
474 * governing the outer access. There is no point in restricting
475 * accesses to an array if in order to evaluate the restriction,
476 * we have to access the same elements (or more).
478 * Rather than removing the argument at this point (which would
479 * complicate the resolution of the other nested accesses), we simply
480 * mark it here by replacing it by a NaN pet_expr.
481 * These NaNs are then later removed in remove_marked_self_dependences.
483 static __isl_give pet_expr *mark_self_dependences(__isl_take pet_expr *expr,
484 int first)
486 int i, n;
488 if (pet_expr_access_is_write(expr))
489 return expr;
491 n = pet_expr_get_n_arg(expr);
492 for (i = first; i < n; ++i) {
493 int mark;
494 pet_expr *arg;
496 arg = pet_expr_get_arg(expr, i);
497 mark = pet_expr_is_sub_access(expr, arg, first);
498 pet_expr_free(arg);
499 if (mark < 0)
500 return pet_expr_free(expr);
501 if (!mark)
502 continue;
504 arg = pet_expr_new_int(isl_val_nan(pet_expr_get_ctx(expr)));
505 expr = pet_expr_set_arg(expr, i, arg);
508 return expr;
511 /* Is "expr" a NaN integer expression?
513 static int expr_is_nan(__isl_keep pet_expr *expr)
515 isl_val *v;
516 int is_nan;
518 if (pet_expr_get_type(expr) != pet_expr_int)
519 return 0;
521 v = pet_expr_int_get_val(expr);
522 is_nan = isl_val_is_nan(v);
523 isl_val_free(v);
525 return is_nan;
528 /* Check if we have marked any self dependences (as NaNs)
529 * in mark_self_dependences and remove them here.
530 * It is safe to project them out since these arguments
531 * can at most be referenced from the condition of the access relation,
532 * but do not appear in the index expression.
533 * "dim" is the dimension of the iteration domain.
535 static __isl_give pet_expr *remove_marked_self_dependences(
536 __isl_take pet_expr *expr, int dim, int first)
538 int i, n;
540 n = pet_expr_get_n_arg(expr);
541 for (i = n - 1; i >= first; --i) {
542 int is_nan;
543 pet_expr *arg;
545 arg = pet_expr_get_arg(expr, i);
546 is_nan = expr_is_nan(arg);
547 pet_expr_free(arg);
548 if (!is_nan)
549 continue;
550 expr = pet_expr_access_project_out_arg(expr, dim, i);
553 return expr;
556 /* Look for parameters in any access relation in "expr" that
557 * refer to nested accesses. In particular, these are
558 * parameters with name "__pet_expr".
560 * If there are any such parameters, then the domain of the index
561 * expression and the access relation, which is either "domain" or
562 * [domain -> [a_1,...,a_m]] at this point, is replaced by
563 * [domain -> [t_1,...,t_n]] or [domain -> [a_1,...,a_m,t_1,...,t_n]],
564 * with m the original number of arguments (n_arg) and
565 * n the number of these parameters
566 * (after identifying identical nested accesses).
568 * This transformation is performed in several steps.
569 * We first extract the arguments in pet_expr_extract_nested.
570 * param2pos maps the original parameter position to the position
571 * of the argument beyond the initial (n_arg) number of arguments.
572 * Then we move these parameters to input dimensions.
573 * t2pos maps the positions of these temporary input dimensions
574 * to the positions of the corresponding arguments inside the space
575 * [domain -> [t_1,...,t_n]].
576 * Finally, we express these temporary dimensions in terms of the domain
577 * [domain -> [a_1,...,a_m,t_1,...,t_n]] and precompose index expression and
578 * access relations with this function.
580 __isl_give pet_expr *pet_expr_resolve_nested(__isl_take pet_expr *expr,
581 __isl_keep isl_space *domain)
583 int i, n, n_arg, dim, n_in;
584 int nparam;
585 isl_ctx *ctx;
586 isl_space *space;
587 isl_local_space *ls;
588 isl_aff *aff;
589 isl_multi_aff *ma;
590 int *param2pos;
591 int *t2pos;
593 if (!expr)
594 return expr;
596 n_arg = pet_expr_get_n_arg(expr);
597 for (i = 0; i < n_arg; ++i) {
598 pet_expr *arg;
599 arg = pet_expr_get_arg(expr, i);
600 arg = pet_expr_resolve_nested(arg, domain);
601 expr = pet_expr_set_arg(expr, i, arg);
604 if (pet_expr_get_type(expr) != pet_expr_access)
605 return expr;
607 dim = isl_space_dim(domain, isl_dim_set);
608 n_in = dim + n_arg;
610 space = pet_expr_access_get_parameter_space(expr);
611 n = pet_nested_n_in_space(space);
612 isl_space_free(space);
613 if (n == 0)
614 return expr;
616 expr = pet_expr_access_align_params(expr);
617 if (!expr)
618 return NULL;
620 space = pet_expr_access_get_parameter_space(expr);
621 nparam = isl_space_dim(space, isl_dim_param);
622 isl_space_free(space);
624 ctx = pet_expr_get_ctx(expr);
626 param2pos = isl_alloc_array(ctx, int, nparam);
627 t2pos = isl_alloc_array(ctx, int, n);
628 if (!param2pos)
629 goto error;
630 expr = pet_expr_extract_nested(expr, n, param2pos);
631 expr = mark_self_dependences(expr, n_arg);
632 if (!expr)
633 goto error;
635 n = 0;
636 space = pet_expr_access_get_parameter_space(expr);
637 nparam = isl_space_dim(space, isl_dim_param);
638 for (i = nparam - 1; i >= 0; --i) {
639 isl_id *id = isl_space_get_dim_id(space, isl_dim_param, i);
640 if (!pet_nested_in_id(id)) {
641 isl_id_free(id);
642 continue;
645 expr = pet_expr_access_move_dims(expr,
646 isl_dim_in, n_in + n, isl_dim_param, i, 1);
647 t2pos[n] = n_in + param2pos[i];
648 n++;
650 isl_id_free(id);
652 isl_space_free(space);
654 space = isl_space_copy(domain);
655 space = isl_space_from_domain(space);
656 space = isl_space_add_dims(space, isl_dim_out,
657 pet_expr_get_n_arg(expr));
658 space = isl_space_wrap(space);
659 ls = isl_local_space_from_space(isl_space_copy(space));
660 space = isl_space_from_domain(space);
661 space = isl_space_add_dims(space, isl_dim_out, n_in + n);
662 ma = isl_multi_aff_zero(space);
664 for (i = 0; i < n_in; ++i) {
665 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
666 isl_dim_set, i);
667 ma = isl_multi_aff_set_aff(ma, i, aff);
669 for (i = 0; i < n; ++i) {
670 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
671 isl_dim_set, t2pos[i]);
672 ma = isl_multi_aff_set_aff(ma, n_in + i, aff);
674 isl_local_space_free(ls);
676 expr = pet_expr_access_pullback_multi_aff(expr, ma);
678 expr = remove_marked_self_dependences(expr, dim, n_arg);
680 free(t2pos);
681 free(param2pos);
682 return expr;
683 error:
684 free(t2pos);
685 free(param2pos);
686 return pet_expr_free(expr);
689 /* Wrapper around pet_expr_resolve_nested
690 * for use as a callback to pet_tree_map_expr.
692 static __isl_give pet_expr *resolve_nested(__isl_take pet_expr *expr,
693 void *user)
695 isl_space *space = user;
697 return pet_expr_resolve_nested(expr, space);
700 /* Call pet_expr_resolve_nested on each of the expressions in "tree".
702 __isl_give pet_tree *pet_tree_resolve_nested(__isl_take pet_tree *tree,
703 __isl_keep isl_space *space)
705 return pet_tree_map_expr(tree, &resolve_nested, space);
708 /* For each nested access parameter in the domain of "stmt",
709 * construct a corresponding pet_expr, place it before the original
710 * elements in stmt->args and record its position in "param2pos".
711 * n is the number of nested access parameters.
713 struct pet_stmt *pet_stmt_extract_nested(struct pet_stmt *stmt, int n,
714 int *param2pos)
716 int i;
717 isl_ctx *ctx;
718 isl_space *space;
719 int n_arg;
720 pet_expr **args;
722 ctx = isl_set_get_ctx(stmt->domain);
724 n_arg = stmt->n_arg;
725 args = isl_calloc_array(ctx, pet_expr *, n + n_arg);
726 if (!args)
727 goto error;
729 space = isl_set_get_space(stmt->domain);
730 if (isl_space_is_wrapping(space))
731 space = isl_space_domain(isl_space_unwrap(space));
732 n_arg = pet_extract_nested_from_space(space, 0, args, param2pos);
733 isl_space_free(space);
735 if (n_arg < 0)
736 goto error;
738 for (i = 0; i < stmt->n_arg; ++i)
739 args[n_arg + i] = stmt->args[i];
740 free(stmt->args);
741 stmt->args = args;
742 stmt->n_arg += n_arg;
744 return stmt;
745 error:
746 if (args) {
747 for (i = 0; i < n; ++i)
748 pet_expr_free(args[i]);
749 free(args);
751 pet_stmt_free(stmt);
752 return NULL;
755 /* Check whether any of the arguments i of "stmt" starting at position "n"
756 * is equal to one of the first "n" arguments j.
757 * If so, combine the constraints on arguments i and j and remove
758 * argument i.
760 static struct pet_stmt *remove_duplicate_arguments(struct pet_stmt *stmt, int n)
762 int i, j;
763 isl_map *map;
765 if (!stmt)
766 return NULL;
767 if (n == 0)
768 return stmt;
769 if (n == stmt->n_arg)
770 return stmt;
772 map = isl_set_unwrap(stmt->domain);
774 for (i = stmt->n_arg - 1; i >= n; --i) {
775 for (j = 0; j < n; ++j)
776 if (pet_expr_is_equal(stmt->args[i], stmt->args[j]))
777 break;
778 if (j >= n)
779 continue;
781 map = isl_map_equate(map, isl_dim_out, i, isl_dim_out, j);
782 map = isl_map_project_out(map, isl_dim_out, i, 1);
784 pet_expr_free(stmt->args[i]);
785 for (j = i; j + 1 < stmt->n_arg; ++j)
786 stmt->args[j] = stmt->args[j + 1];
787 stmt->n_arg--;
790 stmt->domain = isl_map_wrap(map);
791 if (!stmt->domain)
792 goto error;
793 return stmt;
794 error:
795 pet_stmt_free(stmt);
796 return NULL;
799 /* Look for parameters in the iteration domain of "stmt" that
800 * refer to nested accesses. In particular, these are
801 * parameters with name "__pet_expr".
803 * If there are any such parameters, then as many extra variables
804 * (after identifying identical nested accesses) are inserted in the
805 * range of the map wrapped inside the domain, before the original variables.
806 * If the original domain is not a wrapped map, then a new wrapped
807 * map is created with zero output dimensions.
808 * The parameters are then equated to the corresponding output dimensions
809 * and subsequently projected out, from the iteration domain,
810 * the schedule and the access relations.
811 * For each of the output dimensions, a corresponding argument
812 * expression is inserted, embedded in the current iteration domain.
813 * param2pos maps the position of the parameter to the position
814 * of the corresponding output dimension in the wrapped map.
816 struct pet_stmt *pet_stmt_resolve_nested(struct pet_stmt *stmt)
818 int i, n;
819 int pos;
820 int nparam;
821 unsigned n_arg;
822 isl_ctx *ctx;
823 isl_map *map;
824 isl_space *space;
825 isl_multi_aff *ma;
826 int *param2pos;
828 if (!stmt)
829 return NULL;
831 n = pet_nested_n_in_set(stmt->domain);
832 if (n == 0)
833 return stmt;
835 ctx = isl_set_get_ctx(stmt->domain);
837 n_arg = stmt->n_arg;
838 nparam = isl_set_dim(stmt->domain, isl_dim_param);
839 param2pos = isl_alloc_array(ctx, int, nparam);
840 stmt = pet_stmt_extract_nested(stmt, n, param2pos);
841 if (!stmt) {
842 free(param2pos);
843 return NULL;
846 n = stmt->n_arg - n_arg;
847 if (isl_set_is_wrapping(stmt->domain))
848 map = isl_set_unwrap(stmt->domain);
849 else
850 map = isl_map_from_domain(stmt->domain);
851 map = isl_map_insert_dims(map, isl_dim_out, 0, n);
853 for (i = nparam - 1; i >= 0; --i) {
854 isl_id *id;
856 if (!pet_nested_in_map(map, i))
857 continue;
859 id = pet_expr_access_get_id(stmt->args[param2pos[i]]);
860 map = isl_map_set_dim_id(map, isl_dim_out, param2pos[i], id);
861 map = isl_map_equate(map, isl_dim_param, i, isl_dim_out,
862 param2pos[i]);
863 map = isl_map_project_out(map, isl_dim_param, i, 1);
866 stmt->domain = isl_map_wrap(map);
868 stmt = pet_stmt_remove_nested_parameters(stmt);
869 stmt = remove_duplicate_arguments(stmt, n);
871 free(param2pos);
872 return stmt;
875 /* For each statement in "scop", move the parameters that correspond
876 * to nested access into the ranges of the domains and create
877 * corresponding argument expressions.
879 struct pet_scop *pet_scop_resolve_nested(struct pet_scop *scop)
881 int i;
883 if (!scop)
884 return NULL;
886 for (i = 0; i < scop->n_stmt; ++i) {
887 scop->stmts[i] = pet_stmt_resolve_nested(scop->stmts[i]);
888 if (!scop->stmts[i])
889 return pet_scop_free(scop);
892 return scop;