isl_ilp.c: fix typos in comments
[isl.git] / isl_schedule_band.c
blobc25d773864b4b3339c1cfcd794aba50b3a75bafd
1 /*
2 * Copyright 2013-2014 Ecole Normale Superieure
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
13 #include <string.h>
14 #include <isl/map.h>
15 #include <isl/schedule_node.h>
16 #include <isl_schedule_band.h>
17 #include <isl_schedule_private.h>
19 isl_ctx *isl_schedule_band_get_ctx(__isl_keep isl_schedule_band *band)
21 return band ? isl_multi_union_pw_aff_get_ctx(band->mupa) : NULL;
24 /* Return a new uninitialized isl_schedule_band.
26 static __isl_give isl_schedule_band *isl_schedule_band_alloc(isl_ctx *ctx)
28 isl_schedule_band *band;
30 band = isl_calloc_type(ctx, isl_schedule_band);
31 if (!band)
32 return NULL;
34 band->ref = 1;
36 return band;
39 /* Return a new isl_schedule_band with partial schedule "mupa".
40 * First replace "mupa" by its greatest integer part to ensure
41 * that the schedule is always integral.
42 * The band is not marked permutable, the dimensions are not
43 * marked coincident and the AST build options are empty.
44 * Since there are no build options, the node is not anchored.
46 __isl_give isl_schedule_band *isl_schedule_band_from_multi_union_pw_aff(
47 __isl_take isl_multi_union_pw_aff *mupa)
49 isl_ctx *ctx;
50 isl_schedule_band *band;
51 isl_space *space;
53 mupa = isl_multi_union_pw_aff_floor(mupa);
54 if (!mupa)
55 return NULL;
56 ctx = isl_multi_union_pw_aff_get_ctx(mupa);
57 band = isl_schedule_band_alloc(ctx);
58 if (!band)
59 goto error;
61 band->n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
62 band->coincident = isl_calloc_array(ctx, int, band->n);
63 band->mupa = mupa;
64 space = isl_space_params_alloc(ctx, 0);
65 band->ast_build_options = isl_union_set_empty(space);
66 band->anchored = 0;
68 if ((band->n && !band->coincident) || !band->ast_build_options)
69 return isl_schedule_band_free(band);
71 return band;
72 error:
73 isl_multi_union_pw_aff_free(mupa);
74 return NULL;
77 /* Create a duplicate of the given isl_schedule_band.
79 __isl_give isl_schedule_band *isl_schedule_band_dup(
80 __isl_keep isl_schedule_band *band)
82 int i;
83 isl_ctx *ctx;
84 isl_schedule_band *dup;
86 if (!band)
87 return NULL;
89 ctx = isl_schedule_band_get_ctx(band);
90 dup = isl_schedule_band_alloc(ctx);
91 if (!dup)
92 return NULL;
94 dup->n = band->n;
95 dup->coincident = isl_alloc_array(ctx, int, band->n);
96 if (band->n && !dup->coincident)
97 return isl_schedule_band_free(dup);
99 for (i = 0; i < band->n; ++i)
100 dup->coincident[i] = band->coincident[i];
101 dup->permutable = band->permutable;
103 dup->mupa = isl_multi_union_pw_aff_copy(band->mupa);
104 dup->ast_build_options = isl_union_set_copy(band->ast_build_options);
105 if (!dup->mupa || !dup->ast_build_options)
106 return isl_schedule_band_free(dup);
108 if (band->loop_type) {
109 dup->loop_type = isl_alloc_array(ctx,
110 enum isl_ast_loop_type, band->n);
111 if (band->n && !dup->loop_type)
112 return isl_schedule_band_free(dup);
113 for (i = 0; i < band->n; ++i)
114 dup->loop_type[i] = band->loop_type[i];
116 if (band->isolate_loop_type) {
117 dup->isolate_loop_type = isl_alloc_array(ctx,
118 enum isl_ast_loop_type, band->n);
119 if (band->n && !dup->isolate_loop_type)
120 return isl_schedule_band_free(dup);
121 for (i = 0; i < band->n; ++i)
122 dup->isolate_loop_type[i] = band->isolate_loop_type[i];
125 return dup;
128 /* Return an isl_schedule_band that is equal to "band" and that has only
129 * a single reference.
131 __isl_give isl_schedule_band *isl_schedule_band_cow(
132 __isl_take isl_schedule_band *band)
134 if (!band)
135 return NULL;
137 if (band->ref == 1)
138 return band;
139 band->ref--;
140 return isl_schedule_band_dup(band);
143 /* Return a new reference to "band".
145 __isl_give isl_schedule_band *isl_schedule_band_copy(
146 __isl_keep isl_schedule_band *band)
148 if (!band)
149 return NULL;
151 band->ref++;
152 return band;
155 /* Free a reference to "band" and return NULL.
157 __isl_null isl_schedule_band *isl_schedule_band_free(
158 __isl_take isl_schedule_band *band)
160 if (!band)
161 return NULL;
163 if (--band->ref > 0)
164 return NULL;
166 isl_multi_union_pw_aff_free(band->mupa);
167 isl_union_set_free(band->ast_build_options);
168 free(band->loop_type);
169 free(band->isolate_loop_type);
170 free(band->coincident);
171 free(band);
173 return NULL;
176 /* Are "band1" and "band2" obviously equal?
178 isl_bool isl_schedule_band_plain_is_equal(__isl_keep isl_schedule_band *band1,
179 __isl_keep isl_schedule_band *band2)
181 int i;
182 isl_bool equal;
184 if (!band1 || !band2)
185 return isl_bool_error;
186 if (band1 == band2)
187 return isl_bool_true;
189 if (band1->n != band2->n)
190 return isl_bool_false;
191 for (i = 0; i < band1->n; ++i)
192 if (band1->coincident[i] != band2->coincident[i])
193 return isl_bool_false;
194 if (band1->permutable != band2->permutable)
195 return isl_bool_false;
197 equal = isl_multi_union_pw_aff_plain_is_equal(band1->mupa, band2->mupa);
198 if (equal < 0 || !equal)
199 return equal;
201 if (!band1->loop_type != !band2->loop_type)
202 return isl_bool_false;
203 if (band1->loop_type)
204 for (i = 0; i < band1->n; ++i)
205 if (band1->loop_type[i] != band2->loop_type[i])
206 return isl_bool_false;
208 if (!band1->isolate_loop_type != !band2->isolate_loop_type)
209 return isl_bool_false;
210 if (band1->isolate_loop_type)
211 for (i = 0; i < band1->n; ++i)
212 if (band1->isolate_loop_type[i] !=
213 band2->isolate_loop_type[i])
214 return isl_bool_false;
216 return isl_union_set_is_equal(band1->ast_build_options,
217 band2->ast_build_options);
220 /* Return the number of scheduling dimensions in the band.
222 int isl_schedule_band_n_member(__isl_keep isl_schedule_band *band)
224 return band ? band->n : 0;
227 /* Is the given scheduling dimension coincident within the band and
228 * with respect to the coincidence constraints?
230 isl_bool isl_schedule_band_member_get_coincident(
231 __isl_keep isl_schedule_band *band, int pos)
233 if (!band)
234 return isl_bool_error;
236 if (pos < 0 || pos >= band->n)
237 isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
238 "invalid member position", return isl_bool_error);
240 return band->coincident[pos];
243 /* Mark the given scheduling dimension as being coincident or not
244 * according to "coincident".
246 __isl_give isl_schedule_band *isl_schedule_band_member_set_coincident(
247 __isl_take isl_schedule_band *band, int pos, int coincident)
249 if (!band)
250 return NULL;
251 if (isl_schedule_band_member_get_coincident(band, pos) == coincident)
252 return band;
253 band = isl_schedule_band_cow(band);
254 if (!band)
255 return NULL;
257 if (pos < 0 || pos >= band->n)
258 isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
259 "invalid member position",
260 isl_schedule_band_free(band));
262 band->coincident[pos] = coincident;
264 return band;
267 /* Is the schedule band mark permutable?
269 isl_bool isl_schedule_band_get_permutable(__isl_keep isl_schedule_band *band)
271 if (!band)
272 return isl_bool_error;
273 return band->permutable;
276 /* Mark the schedule band permutable or not according to "permutable"?
278 __isl_give isl_schedule_band *isl_schedule_band_set_permutable(
279 __isl_take isl_schedule_band *band, int permutable)
281 if (!band)
282 return NULL;
283 if (band->permutable == permutable)
284 return band;
285 band = isl_schedule_band_cow(band);
286 if (!band)
287 return NULL;
289 band->permutable = permutable;
291 return band;
294 /* Is the band node "node" anchored? That is, does it reference
295 * the outer band nodes?
297 int isl_schedule_band_is_anchored(__isl_keep isl_schedule_band *band)
299 return band ? band->anchored : -1;
302 /* Return the schedule space of the band.
304 __isl_give isl_space *isl_schedule_band_get_space(
305 __isl_keep isl_schedule_band *band)
307 if (!band)
308 return NULL;
309 return isl_multi_union_pw_aff_get_space(band->mupa);
312 /* Intersect the domain of the band schedule of "band" with "domain".
314 __isl_give isl_schedule_band *isl_schedule_band_intersect_domain(
315 __isl_take isl_schedule_band *band, __isl_take isl_union_set *domain)
317 band = isl_schedule_band_cow(band);
318 if (!band || !domain)
319 goto error;
321 band->mupa = isl_multi_union_pw_aff_intersect_domain(band->mupa,
322 domain);
323 if (!band->mupa)
324 return isl_schedule_band_free(band);
326 return band;
327 error:
328 isl_schedule_band_free(band);
329 isl_union_set_free(domain);
330 return NULL;
333 /* Return the schedule of the band in isolation.
335 __isl_give isl_multi_union_pw_aff *isl_schedule_band_get_partial_schedule(
336 __isl_keep isl_schedule_band *band)
338 return band ? isl_multi_union_pw_aff_copy(band->mupa) : NULL;
341 /* Replace the schedule of "band" by "schedule".
343 __isl_give isl_schedule_band *isl_schedule_band_set_partial_schedule(
344 __isl_take isl_schedule_band *band,
345 __isl_take isl_multi_union_pw_aff *schedule)
347 band = isl_schedule_band_cow(band);
348 if (!band || !schedule)
349 goto error;
351 isl_multi_union_pw_aff_free(band->mupa);
352 band->mupa = schedule;
354 return band;
355 error:
356 isl_schedule_band_free(band);
357 isl_multi_union_pw_aff_free(schedule);
358 return NULL;
361 /* Return the loop AST generation type for the band member of "band"
362 * at position "pos".
364 enum isl_ast_loop_type isl_schedule_band_member_get_ast_loop_type(
365 __isl_keep isl_schedule_band *band, int pos)
367 if (!band)
368 return isl_ast_loop_error;
370 if (pos < 0 || pos >= band->n)
371 isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
372 "invalid member position", return -1);
374 if (!band->loop_type)
375 return isl_ast_loop_default;
377 return band->loop_type[pos];
380 /* Set the loop AST generation type for the band member of "band"
381 * at position "pos" to "type".
383 __isl_give isl_schedule_band *isl_schedule_band_member_set_ast_loop_type(
384 __isl_take isl_schedule_band *band, int pos,
385 enum isl_ast_loop_type type)
387 if (!band)
388 return NULL;
389 if (isl_schedule_band_member_get_ast_loop_type(band, pos) == type)
390 return band;
392 if (pos < 0 || pos >= band->n)
393 isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
394 "invalid member position",
395 isl_schedule_band_free(band));
397 band = isl_schedule_band_cow(band);
398 if (!band)
399 return isl_schedule_band_free(band);
401 if (!band->loop_type) {
402 isl_ctx *ctx;
404 ctx = isl_schedule_band_get_ctx(band);
405 band->loop_type = isl_calloc_array(ctx,
406 enum isl_ast_loop_type, band->n);
407 if (band->n && !band->loop_type)
408 return isl_schedule_band_free(band);
411 band->loop_type[pos] = type;
413 return band;
416 /* Return the loop AST generation type for the band member of "band"
417 * at position "pos" for the part that has been isolated by the isolate option.
419 enum isl_ast_loop_type isl_schedule_band_member_get_isolate_ast_loop_type(
420 __isl_keep isl_schedule_band *band, int pos)
422 if (!band)
423 return isl_ast_loop_error;
425 if (pos < 0 || pos >= band->n)
426 isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
427 "invalid member position", return -1);
429 if (!band->isolate_loop_type)
430 return isl_ast_loop_default;
432 return band->isolate_loop_type[pos];
435 /* Set the loop AST generation type for the band member of "band"
436 * at position "pos" to "type" for the part that has been isolated
437 * by the isolate option.
439 __isl_give isl_schedule_band *
440 isl_schedule_band_member_set_isolate_ast_loop_type(
441 __isl_take isl_schedule_band *band, int pos,
442 enum isl_ast_loop_type type)
444 if (!band)
445 return NULL;
446 if (isl_schedule_band_member_get_isolate_ast_loop_type(band, pos) ==
447 type)
448 return band;
450 if (pos < 0 || pos >= band->n)
451 isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
452 "invalid member position",
453 isl_schedule_band_free(band));
455 band = isl_schedule_band_cow(band);
456 if (!band)
457 return isl_schedule_band_free(band);
459 if (!band->isolate_loop_type) {
460 isl_ctx *ctx;
462 ctx = isl_schedule_band_get_ctx(band);
463 band->isolate_loop_type = isl_calloc_array(ctx,
464 enum isl_ast_loop_type, band->n);
465 if (band->n && !band->isolate_loop_type)
466 return isl_schedule_band_free(band);
469 band->isolate_loop_type[pos] = type;
471 return band;
474 static const char *option_str[] = {
475 [isl_ast_loop_atomic] = "atomic",
476 [isl_ast_loop_unroll] = "unroll",
477 [isl_ast_loop_separate] = "separate"
480 /* Given a parameter space "space", extend it to a set space
482 * { type[x] }
484 * or
486 * { [isolate[] -> type[x]] }
488 * depending on whether "isolate" is set.
489 * These can be used to encode loop AST generation options of the given type.
491 static __isl_give isl_space *loop_type_space(__isl_take isl_space *space,
492 enum isl_ast_loop_type type, int isolate)
494 const char *name;
496 name = option_str[type];
497 space = isl_space_set_from_params(space);
498 space = isl_space_add_dims(space, isl_dim_set, 1);
499 space = isl_space_set_tuple_name(space, isl_dim_set, name);
500 if (!isolate)
501 return space;
502 space = isl_space_from_range(space);
503 space = isl_space_set_tuple_name(space, isl_dim_in, "isolate");
504 space = isl_space_wrap(space);
506 return space;
509 /* Add encodings of the "n" loop AST generation options "type" to "options".
510 * If "isolate" is set, then these options refer to the isolated part.
512 * In particular, for each sequence of consecutive identical types "t",
513 * different from the default, add an option
515 * { t[x] : first <= x <= last }
517 * or
519 * { [isolate[] -> t[x]] : first <= x <= last }
521 static __isl_give isl_union_set *add_loop_types(
522 __isl_take isl_union_set *options, int n, enum isl_ast_loop_type *type,
523 int isolate)
525 int i;
526 isl_ctx *ctx;
528 if (!type)
529 return options;
530 if (!options)
531 return NULL;
533 ctx = isl_union_set_get_ctx(options);
534 for (i = 0; i < n; ++i) {
535 int first;
536 isl_space *space;
537 isl_set *option;
539 if (type[i] == isl_ast_loop_default)
540 continue;
542 first = i;
543 while (i + 1 < n && type[i + 1] == type[i])
544 ++i;
546 space = isl_union_set_get_space(options);
547 space = loop_type_space(space, type[i], isolate);
548 option = isl_set_universe(space);
549 option = isl_set_lower_bound_si(option, isl_dim_set, 0, first);
550 option = isl_set_upper_bound_si(option, isl_dim_set, 0, i);
551 options = isl_union_set_add_set(options, option);
554 return options;
557 /* Return the AST build options associated to "band".
559 __isl_give isl_union_set *isl_schedule_band_get_ast_build_options(
560 __isl_keep isl_schedule_band *band)
562 isl_union_set *options;
564 if (!band)
565 return NULL;
567 options = isl_union_set_copy(band->ast_build_options);
568 options = add_loop_types(options, band->n, band->loop_type, 0);
569 options = add_loop_types(options, band->n, band->isolate_loop_type, 1);
571 return options;
574 /* Does "uset" contain any set that satisfies "is"?
575 * "is" is assumed to set its integer argument to 1 if it is satisfied.
577 static int has_any(__isl_keep isl_union_set *uset,
578 isl_stat (*is)(__isl_take isl_set *set, void *user))
580 int found = 0;
582 if (isl_union_set_foreach_set(uset, is, &found) < 0 && !found)
583 return -1;
585 return found;
588 /* Does "set" live in a space of the form
590 * isolate[[...] -> [...]]
594 * If so, set *found and abort the search.
596 static isl_stat is_isolate(__isl_take isl_set *set, void *user)
598 int *found = user;
600 if (isl_set_has_tuple_name(set)) {
601 const char *name;
602 name = isl_set_get_tuple_name(set);
603 if (isl_set_is_wrapping(set) && !strcmp(name, "isolate"))
604 *found = 1;
606 isl_set_free(set);
608 return *found ? isl_stat_error : isl_stat_ok;
611 /* Does "options" include an option of the ofrm
613 * isolate[[...] -> [...]]
617 static int has_isolate_option(__isl_keep isl_union_set *options)
619 return has_any(options, &is_isolate);
622 /* Does "set" encode a loop AST generation option?
624 static isl_stat is_loop_type_option(__isl_take isl_set *set, void *user)
626 int *found = user;
628 if (isl_set_dim(set, isl_dim_set) == 1 &&
629 isl_set_has_tuple_name(set)) {
630 const char *name;
631 enum isl_ast_loop_type type;
632 name = isl_set_get_tuple_name(set);
633 for (type = isl_ast_loop_atomic;
634 type <= isl_ast_loop_separate; ++type) {
635 if (strcmp(name, option_str[type]))
636 continue;
637 *found = 1;
638 break;
641 isl_set_free(set);
643 return *found ? isl_stat_error : isl_stat_ok;
646 /* Does "set" encode a loop AST generation option for the isolated part?
647 * That is, is of the form
649 * { [isolate[] -> t[x]] }
651 * with t equal to "atomic", "unroll" or "separate"?
653 static isl_stat is_isolate_loop_type_option(__isl_take isl_set *set, void *user)
655 int *found = user;
656 const char *name;
657 enum isl_ast_loop_type type;
658 isl_map *map;
660 if (!isl_set_is_wrapping(set)) {
661 isl_set_free(set);
662 return isl_stat_ok;
664 map = isl_set_unwrap(set);
665 if (!isl_map_has_tuple_name(map, isl_dim_in) ||
666 !isl_map_has_tuple_name(map, isl_dim_out)) {
667 isl_map_free(map);
668 return isl_stat_ok;
670 name = isl_map_get_tuple_name(map, isl_dim_in);
671 if (!strcmp(name, "isolate")) {
672 name = isl_map_get_tuple_name(map, isl_dim_out);
673 for (type = isl_ast_loop_atomic;
674 type <= isl_ast_loop_separate; ++type) {
675 if (strcmp(name, option_str[type]))
676 continue;
677 *found = 1;
678 break;
681 isl_map_free(map);
683 return *found ? isl_stat_error : isl_stat_ok;
686 /* Does "options" encode any loop AST generation options
687 * for the isolated part?
689 static int has_isolate_loop_type_options(__isl_keep isl_union_set *options)
691 return has_any(options, &is_isolate_loop_type_option);
694 /* Does "options" encode any loop AST generation options?
696 static int has_loop_type_options(__isl_keep isl_union_set *options)
698 return has_any(options, &is_loop_type_option);
701 /* Extract the loop AST generation type for the band member
702 * at position "pos" from "options".
703 * If "isolate" is set, then extract the loop types for the isolated part.
705 static enum isl_ast_loop_type extract_loop_type(
706 __isl_keep isl_union_set *options, int pos, int isolate)
708 isl_ctx *ctx;
709 enum isl_ast_loop_type type, res = isl_ast_loop_default;
711 ctx = isl_union_set_get_ctx(options);
712 for (type = isl_ast_loop_atomic;
713 type <= isl_ast_loop_separate; ++type) {
714 isl_space *space;
715 isl_set *option;
716 int empty;
718 space = isl_union_set_get_space(options);
719 space = loop_type_space(space, type, isolate);
720 option = isl_union_set_extract_set(options, space);
721 option = isl_set_fix_si(option, isl_dim_set, 0, pos);
722 empty = isl_set_is_empty(option);
723 isl_set_free(option);
725 if (empty < 0)
726 return isl_ast_loop_error;
727 if (empty)
728 continue;
729 if (res != isl_ast_loop_default)
730 isl_die(ctx, isl_error_invalid,
731 "conflicting loop type options",
732 return isl_ast_loop_error);
733 res = type;
736 return res;
739 /* Extract the loop AST generation types for the members of "band"
740 * from "options" and store them in band->loop_type.
741 * Return -1 on error.
743 static int extract_loop_types(__isl_keep isl_schedule_band *band,
744 __isl_keep isl_union_set *options)
746 int i;
748 if (!band->loop_type) {
749 isl_ctx *ctx = isl_schedule_band_get_ctx(band);
750 band->loop_type = isl_alloc_array(ctx,
751 enum isl_ast_loop_type, band->n);
752 if (band->n && !band->loop_type)
753 return -1;
755 for (i = 0; i < band->n; ++i) {
756 band->loop_type[i] = extract_loop_type(options, i, 0);
757 if (band->loop_type[i] == isl_ast_loop_error)
758 return -1;
761 return 0;
764 /* Extract the loop AST generation types for the members of "band"
765 * from "options" for the isolated part and
766 * store them in band->isolate_loop_type.
767 * Return -1 on error.
769 static int extract_isolate_loop_types(__isl_keep isl_schedule_band *band,
770 __isl_keep isl_union_set *options)
772 int i;
774 if (!band->isolate_loop_type) {
775 isl_ctx *ctx = isl_schedule_band_get_ctx(band);
776 band->isolate_loop_type = isl_alloc_array(ctx,
777 enum isl_ast_loop_type, band->n);
778 if (band->n && !band->isolate_loop_type)
779 return -1;
781 for (i = 0; i < band->n; ++i) {
782 band->isolate_loop_type[i] = extract_loop_type(options, i, 1);
783 if (band->isolate_loop_type[i] == isl_ast_loop_error)
784 return -1;
787 return 0;
790 /* Construct universe sets of the spaces that encode loop AST generation
791 * types (for the isolated part if "isolate" is set). That is, construct
793 * { atomic[x]; separate[x]; unroll[x] }
795 * or
797 * { [isolate[] -> atomic[x]]; [isolate[] -> separate[x]];
798 * [isolate[] -> unroll[x]] }
800 static __isl_give isl_union_set *loop_types(__isl_take isl_space *space,
801 int isolate)
803 enum isl_ast_loop_type type;
804 isl_union_set *types;
806 types = isl_union_set_empty(space);
807 for (type = isl_ast_loop_atomic;
808 type <= isl_ast_loop_separate; ++type) {
809 isl_set *set;
811 space = isl_union_set_get_space(types);
812 space = loop_type_space(space, type, isolate);
813 set = isl_set_universe(space);
814 types = isl_union_set_add_set(types, set);
817 return types;
820 /* Remove all elements from spaces that encode loop AST generation types
821 * from "options".
823 static __isl_give isl_union_set *clear_loop_types(
824 __isl_take isl_union_set *options)
826 isl_union_set *types;
828 types = loop_types(isl_union_set_get_space(options), 0);
829 options = isl_union_set_subtract(options, types);
831 return options;
834 /* Remove all elements from spaces that encode loop AST generation types
835 * for the isolated part from "options".
837 static __isl_give isl_union_set *clear_isolate_loop_types(
838 __isl_take isl_union_set *options)
840 isl_union_set *types;
842 types = loop_types(isl_union_set_get_space(options), 1);
843 options = isl_union_set_subtract(options, types);
845 return options;
848 /* Replace the AST build options associated to "band" by "options".
849 * If there are any loop AST generation type options, then they
850 * are extracted and stored in band->loop_type. Otherwise,
851 * band->loop_type is removed to indicate that the default applies
852 * to all members. Similarly for the loop AST generation type options
853 * for the isolated part, which are stored in band->isolate_loop_type.
854 * The remaining options are stored in band->ast_build_options.
856 * Set anchored if the options include an isolate option since the
857 * domain of the wrapped map references the outer band node schedules.
859 __isl_give isl_schedule_band *isl_schedule_band_set_ast_build_options(
860 __isl_take isl_schedule_band *band, __isl_take isl_union_set *options)
862 int has_isolate, has_loop_type, has_isolate_loop_type;
864 band = isl_schedule_band_cow(band);
865 if (!band || !options)
866 goto error;
867 has_isolate = has_isolate_option(options);
868 if (has_isolate < 0)
869 goto error;
870 has_loop_type = has_loop_type_options(options);
871 if (has_loop_type < 0)
872 goto error;
873 has_isolate_loop_type = has_isolate_loop_type_options(options);
874 if (has_isolate_loop_type < 0)
875 goto error;
877 if (!has_loop_type) {
878 free(band->loop_type);
879 band->loop_type = NULL;
880 } else {
881 if (extract_loop_types(band, options) < 0)
882 goto error;
883 options = clear_loop_types(options);
884 if (!options)
885 goto error;
888 if (!has_isolate_loop_type) {
889 free(band->isolate_loop_type);
890 band->isolate_loop_type = NULL;
891 } else {
892 if (extract_isolate_loop_types(band, options) < 0)
893 goto error;
894 options = clear_isolate_loop_types(options);
895 if (!options)
896 goto error;
899 isl_union_set_free(band->ast_build_options);
900 band->ast_build_options = options;
901 band->anchored = has_isolate;
903 return band;
904 error:
905 isl_schedule_band_free(band);
906 isl_union_set_free(options);
907 return NULL;
910 /* Multiply the partial schedule of "band" with the factors in "mv".
911 * Replace the result by its greatest integer part to ensure
912 * that the schedule is always integral.
914 __isl_give isl_schedule_band *isl_schedule_band_scale(
915 __isl_take isl_schedule_band *band, __isl_take isl_multi_val *mv)
917 band = isl_schedule_band_cow(band);
918 if (!band || !mv)
919 goto error;
920 band->mupa = isl_multi_union_pw_aff_scale_multi_val(band->mupa, mv);
921 band->mupa = isl_multi_union_pw_aff_floor(band->mupa);
922 if (!band->mupa)
923 return isl_schedule_band_free(band);
924 return band;
925 error:
926 isl_schedule_band_free(band);
927 isl_multi_val_free(mv);
928 return NULL;
931 /* Divide the partial schedule of "band" by the factors in "mv".
932 * Replace the result by its greatest integer part to ensure
933 * that the schedule is always integral.
935 __isl_give isl_schedule_band *isl_schedule_band_scale_down(
936 __isl_take isl_schedule_band *band, __isl_take isl_multi_val *mv)
938 band = isl_schedule_band_cow(band);
939 if (!band || !mv)
940 goto error;
941 band->mupa = isl_multi_union_pw_aff_scale_down_multi_val(band->mupa,
942 mv);
943 band->mupa = isl_multi_union_pw_aff_floor(band->mupa);
944 if (!band->mupa)
945 return isl_schedule_band_free(band);
946 return band;
947 error:
948 isl_schedule_band_free(band);
949 isl_multi_val_free(mv);
950 return NULL;
953 /* Reduce the partial schedule of "band" modulo the factors in "mv".
955 __isl_give isl_schedule_band *isl_schedule_band_mod(
956 __isl_take isl_schedule_band *band, __isl_take isl_multi_val *mv)
958 band = isl_schedule_band_cow(band);
959 if (!band || !mv)
960 goto error;
961 band->mupa = isl_multi_union_pw_aff_mod_multi_val(band->mupa, mv);
962 if (!band->mupa)
963 return isl_schedule_band_free(band);
964 return band;
965 error:
966 isl_schedule_band_free(band);
967 isl_multi_val_free(mv);
968 return NULL;
971 /* Shift the partial schedule of "band" by "shift" after checking
972 * that the domain of the partial schedule would not be affected
973 * by this shift.
975 __isl_give isl_schedule_band *isl_schedule_band_shift(
976 __isl_take isl_schedule_band *band,
977 __isl_take isl_multi_union_pw_aff *shift)
979 isl_union_set *dom1, *dom2;
980 isl_bool subset;
982 band = isl_schedule_band_cow(band);
983 if (!band || !shift)
984 goto error;
985 dom1 = isl_multi_union_pw_aff_domain(
986 isl_multi_union_pw_aff_copy(band->mupa));
987 dom2 = isl_multi_union_pw_aff_domain(
988 isl_multi_union_pw_aff_copy(shift));
989 subset = isl_union_set_is_subset(dom1, dom2);
990 isl_union_set_free(dom1);
991 isl_union_set_free(dom2);
992 if (subset < 0)
993 goto error;
994 if (!subset)
995 isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
996 "domain of shift needs to include domain of "
997 "partial schedule", goto error);
998 band->mupa = isl_multi_union_pw_aff_add(band->mupa, shift);
999 if (!band->mupa)
1000 return isl_schedule_band_free(band);
1001 return band;
1002 error:
1003 isl_schedule_band_free(band);
1004 isl_multi_union_pw_aff_free(shift);
1005 return NULL;
1008 /* Given the schedule of a band, construct the corresponding
1009 * schedule for the tile loops based on the given tile sizes
1010 * and return the result.
1012 * If the scale tile loops options is set, then the tile loops
1013 * are scaled by the tile sizes.
1015 * That is replace each schedule dimension "i" by either
1016 * "floor(i/s)" or "s * floor(i/s)".
1018 static isl_multi_union_pw_aff *isl_multi_union_pw_aff_tile(
1019 __isl_take isl_multi_union_pw_aff *sched,
1020 __isl_take isl_multi_val *sizes)
1022 isl_ctx *ctx;
1023 int i, n;
1024 isl_val *v;
1025 int scale;
1027 ctx = isl_multi_val_get_ctx(sizes);
1028 scale = isl_options_get_tile_scale_tile_loops(ctx);
1030 n = isl_multi_union_pw_aff_dim(sched, isl_dim_set);
1031 for (i = 0; i < n; ++i) {
1032 isl_union_pw_aff *upa;
1034 upa = isl_multi_union_pw_aff_get_union_pw_aff(sched, i);
1035 v = isl_multi_val_get_val(sizes, i);
1037 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(v));
1038 upa = isl_union_pw_aff_floor(upa);
1039 if (scale)
1040 upa = isl_union_pw_aff_scale_val(upa, isl_val_copy(v));
1041 isl_val_free(v);
1043 sched = isl_multi_union_pw_aff_set_union_pw_aff(sched, i, upa);
1046 isl_multi_val_free(sizes);
1047 return sched;
1050 /* Replace "band" by a band corresponding to the tile loops of a tiling
1051 * with the given tile sizes.
1053 __isl_give isl_schedule_band *isl_schedule_band_tile(
1054 __isl_take isl_schedule_band *band, __isl_take isl_multi_val *sizes)
1056 band = isl_schedule_band_cow(band);
1057 if (!band || !sizes)
1058 goto error;
1059 band->mupa = isl_multi_union_pw_aff_tile(band->mupa, sizes);
1060 if (!band->mupa)
1061 return isl_schedule_band_free(band);
1062 return band;
1063 error:
1064 isl_schedule_band_free(band);
1065 isl_multi_val_free(sizes);
1066 return NULL;
1069 /* Replace "band" by a band corresponding to the point loops of a tiling
1070 * with the given tile sizes.
1071 * "tile" is the corresponding tile loop band.
1073 * If the shift point loops option is set, then the point loops
1074 * are shifted to start at zero. That is, each schedule dimension "i"
1075 * is replaced by "i - s * floor(i/s)".
1076 * The expression "floor(i/s)" (or "s * floor(i/s)") is extracted from
1077 * the tile band.
1079 * Otherwise, the band is left untouched.
1081 __isl_give isl_schedule_band *isl_schedule_band_point(
1082 __isl_take isl_schedule_band *band, __isl_keep isl_schedule_band *tile,
1083 __isl_take isl_multi_val *sizes)
1085 isl_ctx *ctx;
1086 isl_multi_union_pw_aff *scaled;
1088 if (!band || !sizes)
1089 goto error;
1091 ctx = isl_schedule_band_get_ctx(band);
1092 if (!isl_options_get_tile_shift_point_loops(ctx)) {
1093 isl_multi_val_free(sizes);
1094 return band;
1096 band = isl_schedule_band_cow(band);
1097 if (!band)
1098 goto error;
1100 scaled = isl_schedule_band_get_partial_schedule(tile);
1101 if (!isl_options_get_tile_scale_tile_loops(ctx))
1102 scaled = isl_multi_union_pw_aff_scale_multi_val(scaled, sizes);
1103 else
1104 isl_multi_val_free(sizes);
1105 band->mupa = isl_multi_union_pw_aff_sub(band->mupa, scaled);
1106 if (!band->mupa)
1107 return isl_schedule_band_free(band);
1108 return band;
1109 error:
1110 isl_schedule_band_free(band);
1111 isl_multi_val_free(sizes);
1112 return NULL;
1115 /* Drop the "n" dimensions starting at "pos" from "band".
1117 * We apply the transformation even if "n" is zero to ensure consistent
1118 * behavior with respect to changes in the schedule space.
1120 * The loop AST generation types for the isolated part become
1121 * meaningless after dropping dimensions, so we remove them.
1123 __isl_give isl_schedule_band *isl_schedule_band_drop(
1124 __isl_take isl_schedule_band *band, int pos, int n)
1126 int i;
1128 if (pos < 0 || n < 0 || pos + n > band->n)
1129 isl_die(isl_schedule_band_get_ctx(band), isl_error_internal,
1130 "range out of bounds",
1131 return isl_schedule_band_free(band));
1133 band = isl_schedule_band_cow(band);
1134 if (!band)
1135 return NULL;
1137 band->mupa = isl_multi_union_pw_aff_drop_dims(band->mupa,
1138 isl_dim_set, pos, n);
1139 if (!band->mupa)
1140 return isl_schedule_band_free(band);
1142 for (i = pos + n; i < band->n; ++i)
1143 band->coincident[i - n] = band->coincident[i];
1144 if (band->loop_type)
1145 for (i = pos + n; i < band->n; ++i)
1146 band->loop_type[i - n] = band->loop_type[i];
1147 free(band->isolate_loop_type);
1148 band->isolate_loop_type = NULL;
1150 band->n -= n;
1152 return band;
1155 /* Reset the user pointer on all identifiers of parameters and tuples
1156 * in "band".
1158 __isl_give isl_schedule_band *isl_schedule_band_reset_user(
1159 __isl_take isl_schedule_band *band)
1161 band = isl_schedule_band_cow(band);
1162 if (!band)
1163 return NULL;
1165 band->mupa = isl_multi_union_pw_aff_reset_user(band->mupa);
1166 band->ast_build_options =
1167 isl_union_set_reset_user(band->ast_build_options);
1168 if (!band->mupa || !band->ast_build_options)
1169 return isl_schedule_band_free(band);
1171 return band;
1174 /* Align the parameters of "band" to those of "space".
1176 __isl_give isl_schedule_band *isl_schedule_band_align_params(
1177 __isl_take isl_schedule_band *band, __isl_take isl_space *space)
1179 band = isl_schedule_band_cow(band);
1180 if (!band || !space)
1181 goto error;
1183 band->mupa = isl_multi_union_pw_aff_align_params(band->mupa,
1184 isl_space_copy(space));
1185 band->ast_build_options =
1186 isl_union_set_align_params(band->ast_build_options, space);
1187 if (!band->mupa || !band->ast_build_options)
1188 return isl_schedule_band_free(band);
1190 return band;
1191 error:
1192 isl_space_free(space);
1193 isl_schedule_band_free(band);
1194 return NULL;
1197 /* Compute the pullback of "band" by the function represented by "upma".
1198 * In other words, plug in "upma" in the iteration domains of "band".
1200 __isl_give isl_schedule_band *isl_schedule_band_pullback_union_pw_multi_aff(
1201 __isl_take isl_schedule_band *band,
1202 __isl_take isl_union_pw_multi_aff *upma)
1204 band = isl_schedule_band_cow(band);
1205 if (!band || !upma)
1206 goto error;
1208 band->mupa =
1209 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(band->mupa,
1210 upma);
1211 if (!band->mupa)
1212 return isl_schedule_band_free(band);
1214 return band;
1215 error:
1216 isl_union_pw_multi_aff_free(upma);
1217 isl_schedule_band_free(band);
1218 return NULL;
1221 /* Compute the gist of "band" with respect to "context".
1222 * In particular, compute the gist of the associated partial schedule.
1224 __isl_give isl_schedule_band *isl_schedule_band_gist(
1225 __isl_take isl_schedule_band *band, __isl_take isl_union_set *context)
1227 if (!band || !context)
1228 goto error;
1229 if (band->n == 0) {
1230 isl_union_set_free(context);
1231 return band;
1233 band = isl_schedule_band_cow(band);
1234 if (!band)
1235 goto error;
1236 band->mupa = isl_multi_union_pw_aff_gist(band->mupa, context);
1237 if (!band->mupa)
1238 return isl_schedule_band_free(band);
1239 return band;
1240 error:
1241 isl_union_set_free(context);
1242 isl_schedule_band_free(band);
1243 return NULL;