isl_term_get_exp: return isl_size
[isl.git] / isl_ast_build.c
blob8c9edaa08d585b9451fc156558eb193da2a3a322
1 /*
2 * Copyright 2012-2013 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 <isl/id.h>
14 #include <isl/val.h>
15 #include <isl/space.h>
16 #include <isl/map.h>
17 #include <isl/aff.h>
18 #include <isl/constraint.h>
19 #include <isl/map.h>
20 #include <isl/union_set.h>
21 #include <isl/union_map.h>
22 #include <isl_ast_build_private.h>
23 #include <isl_ast_private.h>
24 #include <isl_config.h>
26 /* Construct a map that isolates the current dimension.
28 * Essentially, the current dimension of "set" is moved to the single output
29 * dimension in the result, with the current dimension in the domain replaced
30 * by an unconstrained variable.
32 __isl_give isl_map *isl_ast_build_map_to_iterator(
33 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
35 isl_map *map;
37 map = isl_map_from_domain(set);
38 map = isl_map_add_dims(map, isl_dim_out, 1);
40 if (!build)
41 return isl_map_free(map);
43 map = isl_map_equate(map, isl_dim_in, build->depth, isl_dim_out, 0);
44 map = isl_map_eliminate(map, isl_dim_in, build->depth, 1);
46 return map;
49 /* Initialize the information derived during the AST generation to default
50 * values for a schedule domain in "space".
52 * We also check that the remaining fields are not NULL so that
53 * the calling functions don't have to perform this test.
55 static __isl_give isl_ast_build *isl_ast_build_init_derived(
56 __isl_take isl_ast_build *build, __isl_take isl_space *space)
58 isl_ctx *ctx;
59 isl_vec *strides;
60 isl_size dim;
62 build = isl_ast_build_cow(build);
63 if (!build || !build->domain)
64 goto error;
66 ctx = isl_ast_build_get_ctx(build);
67 dim = isl_space_dim(space, isl_dim_set);
68 if (dim < 0)
69 goto error;
70 strides = isl_vec_alloc(ctx, dim);
71 strides = isl_vec_set_si(strides, 1);
73 isl_vec_free(build->strides);
74 build->strides = strides;
76 space = isl_space_map_from_set(space);
77 isl_multi_aff_free(build->offsets);
78 build->offsets = isl_multi_aff_zero(isl_space_copy(space));
79 isl_multi_aff_free(build->values);
80 build->values = isl_multi_aff_identity(isl_space_copy(space));
81 isl_multi_aff_free(build->internal2input);
82 build->internal2input = isl_multi_aff_identity(space);
84 if (!build->iterators || !build->domain || !build->generated ||
85 !build->pending || !build->values || !build->internal2input ||
86 !build->strides || !build->offsets || !build->options)
87 return isl_ast_build_free(build);
89 return build;
90 error:
91 isl_space_free(space);
92 return isl_ast_build_free(build);
95 /* Return an isl_id called "c%d", with "%d" set to "i".
96 * If an isl_id with such a name already appears among the parameters
97 * in build->domain, then adjust the name to "c%d_%d".
99 static __isl_give isl_id *generate_name(isl_ctx *ctx, int i,
100 __isl_keep isl_ast_build *build)
102 int j;
103 char name[23];
104 isl_set *dom = build->domain;
106 snprintf(name, sizeof(name), "c%d", i);
107 j = 0;
108 while (isl_set_find_dim_by_name(dom, isl_dim_param, name) >= 0)
109 snprintf(name, sizeof(name), "c%d_%d", i, j++);
110 return isl_id_alloc(ctx, name, NULL);
113 /* Create an isl_ast_build with "set" as domain.
115 * The input set is usually a parameter domain, but we currently allow it to
116 * be any kind of set. We set the domain of the returned isl_ast_build
117 * to "set" and initialize all the other fields to default values.
119 __isl_give isl_ast_build *isl_ast_build_from_context(__isl_take isl_set *set)
121 int i;
122 isl_size n;
123 isl_ctx *ctx;
124 isl_space *space;
125 isl_ast_build *build;
127 set = isl_set_compute_divs(set);
128 n = isl_set_dim(set, isl_dim_set);
129 if (n < 0)
130 goto error;
132 ctx = isl_set_get_ctx(set);
134 build = isl_calloc_type(ctx, isl_ast_build);
135 if (!build)
136 goto error;
138 build->ref = 1;
139 build->domain = set;
140 build->generated = isl_set_copy(build->domain);
141 build->pending = isl_set_universe(isl_set_get_space(build->domain));
142 build->options = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
143 build->depth = n;
144 build->iterators = isl_id_list_alloc(ctx, n);
145 for (i = 0; i < n; ++i) {
146 isl_id *id;
147 if (isl_set_has_dim_id(set, isl_dim_set, i))
148 id = isl_set_get_dim_id(set, isl_dim_set, i);
149 else
150 id = generate_name(ctx, i, build);
151 build->iterators = isl_id_list_add(build->iterators, id);
153 space = isl_set_get_space(set);
154 if (isl_space_is_params(space))
155 space = isl_space_set_from_params(space);
157 return isl_ast_build_init_derived(build, space);
158 error:
159 isl_set_free(set);
160 return NULL;
163 /* Create an isl_ast_build with a universe (parametric) context.
165 __isl_give isl_ast_build *isl_ast_build_alloc(isl_ctx *ctx)
167 isl_space *space;
168 isl_set *context;
170 space = isl_space_params_alloc(ctx, 0);
171 context = isl_set_universe(space);
173 return isl_ast_build_from_context(context);
176 __isl_give isl_ast_build *isl_ast_build_copy(__isl_keep isl_ast_build *build)
178 if (!build)
179 return NULL;
181 build->ref++;
182 return build;
185 __isl_give isl_ast_build *isl_ast_build_dup(__isl_keep isl_ast_build *build)
187 isl_ctx *ctx;
188 isl_ast_build *dup;
190 if (!build)
191 return NULL;
193 ctx = isl_ast_build_get_ctx(build);
194 dup = isl_calloc_type(ctx, isl_ast_build);
195 if (!dup)
196 return NULL;
198 dup->ref = 1;
199 dup->outer_pos = build->outer_pos;
200 dup->depth = build->depth;
201 dup->iterators = isl_id_list_copy(build->iterators);
202 dup->domain = isl_set_copy(build->domain);
203 dup->generated = isl_set_copy(build->generated);
204 dup->pending = isl_set_copy(build->pending);
205 dup->values = isl_multi_aff_copy(build->values);
206 dup->internal2input = isl_multi_aff_copy(build->internal2input);
207 dup->value = isl_pw_aff_copy(build->value);
208 dup->strides = isl_vec_copy(build->strides);
209 dup->offsets = isl_multi_aff_copy(build->offsets);
210 dup->executed = isl_union_map_copy(build->executed);
211 dup->single_valued = build->single_valued;
212 dup->options = isl_union_map_copy(build->options);
213 dup->at_each_domain = build->at_each_domain;
214 dup->at_each_domain_user = build->at_each_domain_user;
215 dup->before_each_for = build->before_each_for;
216 dup->before_each_for_user = build->before_each_for_user;
217 dup->after_each_for = build->after_each_for;
218 dup->after_each_for_user = build->after_each_for_user;
219 dup->before_each_mark = build->before_each_mark;
220 dup->before_each_mark_user = build->before_each_mark_user;
221 dup->after_each_mark = build->after_each_mark;
222 dup->after_each_mark_user = build->after_each_mark_user;
223 dup->create_leaf = build->create_leaf;
224 dup->create_leaf_user = build->create_leaf_user;
225 dup->node = isl_schedule_node_copy(build->node);
226 if (build->loop_type) {
227 int i;
229 dup->n = build->n;
230 dup->loop_type = isl_alloc_array(ctx,
231 enum isl_ast_loop_type, dup->n);
232 if (dup->n && !dup->loop_type)
233 return isl_ast_build_free(dup);
234 for (i = 0; i < dup->n; ++i)
235 dup->loop_type[i] = build->loop_type[i];
238 if (!dup->iterators || !dup->domain || !dup->generated ||
239 !dup->pending || !dup->values ||
240 !dup->strides || !dup->offsets || !dup->options ||
241 (build->internal2input && !dup->internal2input) ||
242 (build->executed && !dup->executed) ||
243 (build->value && !dup->value) ||
244 (build->node && !dup->node))
245 return isl_ast_build_free(dup);
247 return dup;
250 /* Align the parameters of "build" to those of "model", introducing
251 * additional parameters if needed.
253 __isl_give isl_ast_build *isl_ast_build_align_params(
254 __isl_take isl_ast_build *build, __isl_take isl_space *model)
256 build = isl_ast_build_cow(build);
257 if (!build)
258 goto error;
260 build->domain = isl_set_align_params(build->domain,
261 isl_space_copy(model));
262 build->generated = isl_set_align_params(build->generated,
263 isl_space_copy(model));
264 build->pending = isl_set_align_params(build->pending,
265 isl_space_copy(model));
266 build->values = isl_multi_aff_align_params(build->values,
267 isl_space_copy(model));
268 build->offsets = isl_multi_aff_align_params(build->offsets,
269 isl_space_copy(model));
270 build->options = isl_union_map_align_params(build->options,
271 isl_space_copy(model));
272 if (build->internal2input) {
273 build->internal2input =
274 isl_multi_aff_align_params(build->internal2input,
275 model);
276 if (!build->internal2input)
277 return isl_ast_build_free(build);
278 } else {
279 isl_space_free(model);
282 if (!build->domain || !build->values || !build->offsets ||
283 !build->options)
284 return isl_ast_build_free(build);
286 return build;
287 error:
288 isl_space_free(model);
289 return NULL;
292 __isl_give isl_ast_build *isl_ast_build_cow(__isl_take isl_ast_build *build)
294 if (!build)
295 return NULL;
297 if (build->ref == 1)
298 return build;
299 build->ref--;
300 return isl_ast_build_dup(build);
303 __isl_null isl_ast_build *isl_ast_build_free(
304 __isl_take isl_ast_build *build)
306 if (!build)
307 return NULL;
309 if (--build->ref > 0)
310 return NULL;
312 isl_id_list_free(build->iterators);
313 isl_set_free(build->domain);
314 isl_set_free(build->generated);
315 isl_set_free(build->pending);
316 isl_multi_aff_free(build->values);
317 isl_multi_aff_free(build->internal2input);
318 isl_pw_aff_free(build->value);
319 isl_vec_free(build->strides);
320 isl_multi_aff_free(build->offsets);
321 isl_multi_aff_free(build->schedule_map);
322 isl_union_map_free(build->executed);
323 isl_union_map_free(build->options);
324 isl_schedule_node_free(build->node);
325 free(build->loop_type);
326 isl_set_free(build->isolated);
328 free(build);
330 return NULL;
333 isl_ctx *isl_ast_build_get_ctx(__isl_keep isl_ast_build *build)
335 return build ? isl_set_get_ctx(build->domain) : NULL;
338 /* Replace build->options by "options".
340 __isl_give isl_ast_build *isl_ast_build_set_options(
341 __isl_take isl_ast_build *build, __isl_take isl_union_map *options)
343 build = isl_ast_build_cow(build);
345 if (!build || !options)
346 goto error;
348 isl_union_map_free(build->options);
349 build->options = options;
351 return build;
352 error:
353 isl_union_map_free(options);
354 return isl_ast_build_free(build);
357 /* Set the iterators for the next code generation.
359 * If we still have some iterators left from the previous code generation
360 * (if any) or if iterators have already been set by a previous
361 * call to this function, then we remove them first.
363 __isl_give isl_ast_build *isl_ast_build_set_iterators(
364 __isl_take isl_ast_build *build, __isl_take isl_id_list *iterators)
366 isl_size dim;
367 int n_it;
369 build = isl_ast_build_cow(build);
370 if (!build)
371 goto error;
373 dim = isl_ast_build_dim(build, isl_dim_set);
374 n_it = isl_id_list_n_id(build->iterators);
375 if (dim < 0)
376 goto error;
377 if (n_it < dim)
378 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
379 "isl_ast_build in inconsistent state", goto error);
380 if (n_it > dim)
381 build->iterators = isl_id_list_drop(build->iterators,
382 dim, n_it - dim);
383 build->iterators = isl_id_list_concat(build->iterators, iterators);
384 if (!build->iterators)
385 return isl_ast_build_free(build);
387 return build;
388 error:
389 isl_id_list_free(iterators);
390 return isl_ast_build_free(build);
393 /* Set the "at_each_domain" callback of "build" to "fn".
395 __isl_give isl_ast_build *isl_ast_build_set_at_each_domain(
396 __isl_take isl_ast_build *build,
397 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
398 __isl_keep isl_ast_build *build, void *user), void *user)
400 build = isl_ast_build_cow(build);
402 if (!build)
403 return NULL;
405 build->at_each_domain = fn;
406 build->at_each_domain_user = user;
408 return build;
411 /* Set the "before_each_for" callback of "build" to "fn".
413 __isl_give isl_ast_build *isl_ast_build_set_before_each_for(
414 __isl_take isl_ast_build *build,
415 __isl_give isl_id *(*fn)(__isl_keep isl_ast_build *build,
416 void *user), void *user)
418 build = isl_ast_build_cow(build);
420 if (!build)
421 return NULL;
423 build->before_each_for = fn;
424 build->before_each_for_user = user;
426 return build;
429 /* Set the "after_each_for" callback of "build" to "fn".
431 __isl_give isl_ast_build *isl_ast_build_set_after_each_for(
432 __isl_take isl_ast_build *build,
433 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
434 __isl_keep isl_ast_build *build, void *user), void *user)
436 build = isl_ast_build_cow(build);
438 if (!build)
439 return NULL;
441 build->after_each_for = fn;
442 build->after_each_for_user = user;
444 return build;
447 /* Set the "before_each_mark" callback of "build" to "fn".
449 __isl_give isl_ast_build *isl_ast_build_set_before_each_mark(
450 __isl_take isl_ast_build *build,
451 isl_stat (*fn)(__isl_keep isl_id *mark, __isl_keep isl_ast_build *build,
452 void *user), void *user)
454 build = isl_ast_build_cow(build);
456 if (!build)
457 return NULL;
459 build->before_each_mark = fn;
460 build->before_each_mark_user = user;
462 return build;
465 /* Set the "after_each_mark" callback of "build" to "fn".
467 __isl_give isl_ast_build *isl_ast_build_set_after_each_mark(
468 __isl_take isl_ast_build *build,
469 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
470 __isl_keep isl_ast_build *build, void *user), void *user)
472 build = isl_ast_build_cow(build);
474 if (!build)
475 return NULL;
477 build->after_each_mark = fn;
478 build->after_each_mark_user = user;
480 return build;
483 /* Set the "create_leaf" callback of "build" to "fn".
485 __isl_give isl_ast_build *isl_ast_build_set_create_leaf(
486 __isl_take isl_ast_build *build,
487 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_build *build,
488 void *user), void *user)
490 build = isl_ast_build_cow(build);
492 if (!build)
493 return NULL;
495 build->create_leaf = fn;
496 build->create_leaf_user = user;
498 return build;
501 /* Clear all information that is specific to this code generation
502 * and that is (probably) not meaningful to any nested code generation.
504 __isl_give isl_ast_build *isl_ast_build_clear_local_info(
505 __isl_take isl_ast_build *build)
507 isl_space *space;
509 build = isl_ast_build_cow(build);
510 if (!build)
511 return NULL;
513 space = isl_union_map_get_space(build->options);
514 isl_union_map_free(build->options);
515 build->options = isl_union_map_empty(space);
517 build->at_each_domain = NULL;
518 build->at_each_domain_user = NULL;
519 build->before_each_for = NULL;
520 build->before_each_for_user = NULL;
521 build->after_each_for = NULL;
522 build->after_each_for_user = NULL;
523 build->before_each_mark = NULL;
524 build->before_each_mark_user = NULL;
525 build->after_each_mark = NULL;
526 build->after_each_mark_user = NULL;
527 build->create_leaf = NULL;
528 build->create_leaf_user = NULL;
530 if (!build->options)
531 return isl_ast_build_free(build);
533 return build;
536 /* Have any loops been eliminated?
537 * That is, do any of the original schedule dimensions have a fixed
538 * value that has been substituted?
540 static int any_eliminated(isl_ast_build *build)
542 int i;
544 for (i = 0; i < build->depth; ++i)
545 if (isl_ast_build_has_affine_value(build, i))
546 return 1;
548 return 0;
551 /* Clear build->schedule_map.
552 * This function should be called whenever anything that might affect
553 * the result of isl_ast_build_get_schedule_map_multi_aff changes.
554 * In particular, it should be called when the depth is changed or
555 * when an iterator is determined to have a fixed value.
557 static void isl_ast_build_reset_schedule_map(__isl_keep isl_ast_build *build)
559 if (!build)
560 return;
561 isl_multi_aff_free(build->schedule_map);
562 build->schedule_map = NULL;
565 /* Do we need a (non-trivial) schedule map?
566 * That is, is the internal schedule space different from
567 * the external schedule space?
569 * The internal and external schedule spaces are only the same
570 * if code has been generated for the entire schedule and if none
571 * of the loops have been eliminated.
573 isl_bool isl_ast_build_need_schedule_map(__isl_keep isl_ast_build *build)
575 isl_size dim;
577 dim = isl_ast_build_dim(build, isl_dim_set);
578 if (dim < 0)
579 return isl_bool_error;
580 return build->depth != dim || any_eliminated(build);
583 /* Return a mapping from the internal schedule space to the external
584 * schedule space in the form of an isl_multi_aff.
585 * The internal schedule space originally corresponds to that of the
586 * input schedule. This may change during the code generation if
587 * if isl_ast_build_insert_dim is ever called.
588 * The external schedule space corresponds to the
589 * loops that have been generated.
591 * Currently, the only difference between the internal schedule domain
592 * and the external schedule domain is that some dimensions are projected
593 * out in the external schedule domain. In particular, the dimensions
594 * for which no code has been generated yet and the dimensions that correspond
595 * to eliminated loops.
597 * We cache a copy of the schedule_map in build->schedule_map.
598 * The cache is cleared through isl_ast_build_reset_schedule_map
599 * whenever anything changes that might affect the result of this function.
601 __isl_give isl_multi_aff *isl_ast_build_get_schedule_map_multi_aff(
602 __isl_keep isl_ast_build *build)
604 isl_bool needs_map;
605 isl_space *space;
606 isl_multi_aff *ma;
608 if (!build)
609 return NULL;
610 if (build->schedule_map)
611 return isl_multi_aff_copy(build->schedule_map);
612 needs_map = isl_ast_build_need_schedule_map(build);
613 if (needs_map < 0)
614 return NULL;
616 space = isl_ast_build_get_space(build, 1);
617 space = isl_space_map_from_set(space);
618 ma = isl_multi_aff_identity(space);
619 if (needs_map) {
620 int i;
621 isl_size dim = isl_ast_build_dim(build, isl_dim_set);
623 if (dim < 0)
624 ma = isl_multi_aff_free(ma);
625 ma = isl_multi_aff_drop_dims(ma, isl_dim_out,
626 build->depth, dim - build->depth);
627 for (i = build->depth - 1; i >= 0; --i)
628 if (isl_ast_build_has_affine_value(build, i))
629 ma = isl_multi_aff_drop_dims(ma,
630 isl_dim_out, i, 1);
633 build->schedule_map = ma;
634 return isl_multi_aff_copy(build->schedule_map);
637 /* Return a mapping from the internal schedule space to the external
638 * schedule space in the form of an isl_map.
640 __isl_give isl_map *isl_ast_build_get_schedule_map(
641 __isl_keep isl_ast_build *build)
643 isl_multi_aff *ma;
645 ma = isl_ast_build_get_schedule_map_multi_aff(build);
646 return isl_map_from_multi_aff(ma);
649 /* Return the position of the dimension in build->domain for which
650 * an AST node is currently being generated.
652 int isl_ast_build_get_depth(__isl_keep isl_ast_build *build)
654 return build ? build->depth : -1;
657 /* Prepare for generating code for the next level.
658 * In particular, increase the depth and reset any information
659 * that is local to the current depth.
661 __isl_give isl_ast_build *isl_ast_build_increase_depth(
662 __isl_take isl_ast_build *build)
664 build = isl_ast_build_cow(build);
665 if (!build)
666 return NULL;
667 build->depth++;
668 isl_ast_build_reset_schedule_map(build);
669 build->value = isl_pw_aff_free(build->value);
670 return build;
673 void isl_ast_build_dump(__isl_keep isl_ast_build *build)
675 if (!build)
676 return;
678 fprintf(stderr, "domain: ");
679 isl_set_dump(build->domain);
680 fprintf(stderr, "generated: ");
681 isl_set_dump(build->generated);
682 fprintf(stderr, "pending: ");
683 isl_set_dump(build->pending);
684 fprintf(stderr, "iterators: ");
685 isl_id_list_dump(build->iterators);
686 fprintf(stderr, "values: ");
687 isl_multi_aff_dump(build->values);
688 if (build->value) {
689 fprintf(stderr, "value: ");
690 isl_pw_aff_dump(build->value);
692 fprintf(stderr, "strides: ");
693 isl_vec_dump(build->strides);
694 fprintf(stderr, "offsets: ");
695 isl_multi_aff_dump(build->offsets);
696 fprintf(stderr, "internal2input: ");
697 isl_multi_aff_dump(build->internal2input);
700 /* Initialize "build" for AST construction in schedule space "space"
701 * in the case that build->domain is a parameter set.
703 * build->iterators is assumed to have been updated already.
705 static __isl_give isl_ast_build *isl_ast_build_init(
706 __isl_take isl_ast_build *build, __isl_take isl_space *space)
708 isl_set *set;
710 build = isl_ast_build_cow(build);
711 if (!build)
712 goto error;
714 set = isl_set_universe(isl_space_copy(space));
715 build->domain = isl_set_intersect_params(isl_set_copy(set),
716 build->domain);
717 build->pending = isl_set_intersect_params(isl_set_copy(set),
718 build->pending);
719 build->generated = isl_set_intersect_params(set, build->generated);
721 return isl_ast_build_init_derived(build, space);
722 error:
723 isl_ast_build_free(build);
724 isl_space_free(space);
725 return NULL;
728 /* Assign "aff" to *user and return -1, effectively extracting
729 * the first (and presumably only) affine expression in the isl_pw_aff
730 * on which this function is used.
732 static isl_stat extract_single_piece(__isl_take isl_set *set,
733 __isl_take isl_aff *aff, void *user)
735 isl_aff **p = user;
737 *p = aff;
738 isl_set_free(set);
740 return isl_stat_error;
743 /* Intersect "set" with the stride constraint of "build", if any.
745 static __isl_give isl_set *intersect_stride_constraint(__isl_take isl_set *set,
746 __isl_keep isl_ast_build *build)
748 isl_set *stride;
750 if (!build)
751 return isl_set_free(set);
752 if (!isl_ast_build_has_stride(build, build->depth))
753 return set;
755 stride = isl_ast_build_get_stride_constraint(build);
756 return isl_set_intersect(set, stride);
759 /* Check if the given bounds on the current dimension (together with
760 * the stride constraint, if any) imply that
761 * this current dimension attains only a single value (in terms of
762 * parameters and outer dimensions).
763 * If so, we record it in build->value.
764 * If, moreover, this value can be represented as a single affine expression,
765 * then we also update build->values, effectively marking the current
766 * dimension as "eliminated".
768 * When computing the gist of the fixed value that can be represented
769 * as a single affine expression, it is important to only take into
770 * account the domain constraints in the original AST build and
771 * not the domain of the affine expression itself.
772 * Otherwise, a [i/3] is changed into a i/3 because we know that i
773 * is a multiple of 3, but then we end up not expressing anywhere
774 * in the context that i is a multiple of 3.
776 static __isl_give isl_ast_build *update_values(
777 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
779 isl_bool sv;
780 isl_size n;
781 isl_pw_multi_aff *pma;
782 isl_aff *aff = NULL;
783 isl_map *it_map;
784 isl_set *set;
786 set = isl_set_from_basic_set(bounds);
787 set = isl_set_intersect(set, isl_set_copy(build->domain));
788 set = intersect_stride_constraint(set, build);
789 it_map = isl_ast_build_map_to_iterator(build, set);
791 sv = isl_map_is_single_valued(it_map);
792 if (sv < 0)
793 build = isl_ast_build_free(build);
794 if (!build || !sv) {
795 isl_map_free(it_map);
796 return build;
799 pma = isl_pw_multi_aff_from_map(it_map);
800 build->value = isl_pw_multi_aff_get_pw_aff(pma, 0);
801 build->value = isl_ast_build_compute_gist_pw_aff(build, build->value);
802 build->value = isl_pw_aff_coalesce(build->value);
803 isl_pw_multi_aff_free(pma);
805 n = isl_pw_aff_n_piece(build->value);
806 if (n < 0)
807 return isl_ast_build_free(build);
808 if (n != 1)
809 return build;
811 isl_pw_aff_foreach_piece(build->value, &extract_single_piece, &aff);
813 build->values = isl_multi_aff_set_aff(build->values, build->depth, aff);
814 if (!build->values)
815 return isl_ast_build_free(build);
816 isl_ast_build_reset_schedule_map(build);
817 return build;
820 /* Update the AST build based on the given loop bounds for
821 * the current dimension and the stride information available in the build.
823 * We first make sure that the bounds do not refer to any iterators
824 * that have already been eliminated.
825 * Then, we check if the bounds imply that the current iterator
826 * has a fixed value.
827 * If they do and if this fixed value can be expressed as a single
828 * affine expression, we eliminate the iterators from the bounds.
829 * Note that we cannot simply plug in this single value using
830 * isl_basic_set_preimage_multi_aff as the single value may only
831 * be defined on a subset of the domain. Plugging in the value
832 * would restrict the build domain to this subset, while this
833 * restriction may not be reflected in the generated code.
834 * Finally, we intersect build->domain with the updated bounds.
835 * We also add the stride constraint unless we have been able
836 * to find a fixed value expressed as a single affine expression.
838 * Note that the check for a fixed value in update_values requires
839 * us to intersect the bounds with the current build domain.
840 * When we intersect build->domain with the updated bounds in
841 * the final step, we make sure that these updated bounds have
842 * not been intersected with the old build->domain.
843 * Otherwise, we would indirectly intersect the build domain with itself,
844 * which can lead to inefficiencies, in particular if the build domain
845 * contains any unknown divs.
847 * The pending and generated sets are not updated by this function to
848 * match the updated domain.
849 * The caller still needs to call isl_ast_build_set_pending_generated.
851 __isl_give isl_ast_build *isl_ast_build_set_loop_bounds(
852 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
854 isl_set *set;
856 build = isl_ast_build_cow(build);
857 if (!build)
858 goto error;
860 build = update_values(build, isl_basic_set_copy(bounds));
861 if (!build)
862 goto error;
863 set = isl_set_from_basic_set(isl_basic_set_copy(bounds));
864 if (isl_ast_build_has_affine_value(build, build->depth)) {
865 set = isl_set_eliminate(set, isl_dim_set, build->depth, 1);
866 set = isl_set_compute_divs(set);
867 build->pending = isl_set_intersect(build->pending,
868 isl_set_copy(set));
869 build->domain = isl_set_intersect(build->domain, set);
870 } else {
871 build->domain = isl_set_intersect(build->domain, set);
872 build = isl_ast_build_include_stride(build);
873 if (!build)
874 goto error;
876 isl_basic_set_free(bounds);
878 if (!build->domain || !build->pending || !build->generated)
879 return isl_ast_build_free(build);
881 return build;
882 error:
883 isl_ast_build_free(build);
884 isl_basic_set_free(bounds);
885 return NULL;
888 /* Update the pending and generated sets of "build" according to "bounds".
889 * If the build has an affine value at the current depth,
890 * then isl_ast_build_set_loop_bounds has already set the pending set.
891 * Otherwise, do it here.
893 __isl_give isl_ast_build *isl_ast_build_set_pending_generated(
894 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
896 isl_basic_set *generated, *pending;
898 if (!build)
899 goto error;
901 if (isl_ast_build_has_affine_value(build, build->depth)) {
902 isl_basic_set_free(bounds);
903 return build;
906 build = isl_ast_build_cow(build);
907 if (!build)
908 goto error;
910 pending = isl_basic_set_copy(bounds);
911 pending = isl_basic_set_drop_constraints_involving_dims(pending,
912 isl_dim_set, build->depth, 1);
913 build->pending = isl_set_intersect(build->pending,
914 isl_set_from_basic_set(pending));
915 generated = bounds;
916 generated = isl_basic_set_drop_constraints_not_involving_dims(
917 generated, isl_dim_set, build->depth, 1);
918 build->generated = isl_set_intersect(build->generated,
919 isl_set_from_basic_set(generated));
921 if (!build->pending || !build->generated)
922 return isl_ast_build_free(build);
924 return build;
925 error:
926 isl_ast_build_free(build);
927 isl_basic_set_free(bounds);
928 return NULL;
931 /* Intersect build->domain with "set", where "set" is specified
932 * in terms of the internal schedule domain.
934 static __isl_give isl_ast_build *isl_ast_build_restrict_internal(
935 __isl_take isl_ast_build *build, __isl_take isl_set *set)
937 build = isl_ast_build_cow(build);
938 if (!build)
939 goto error;
941 set = isl_set_compute_divs(set);
942 build->domain = isl_set_intersect(build->domain, set);
943 build->domain = isl_set_coalesce(build->domain);
945 if (!build->domain)
946 return isl_ast_build_free(build);
948 return build;
949 error:
950 isl_ast_build_free(build);
951 isl_set_free(set);
952 return NULL;
955 /* Intersect build->generated and build->domain with "set",
956 * where "set" is specified in terms of the internal schedule domain.
958 __isl_give isl_ast_build *isl_ast_build_restrict_generated(
959 __isl_take isl_ast_build *build, __isl_take isl_set *set)
961 set = isl_set_compute_divs(set);
962 build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
963 build = isl_ast_build_cow(build);
964 if (!build)
965 goto error;
967 build->generated = isl_set_intersect(build->generated, set);
968 build->generated = isl_set_coalesce(build->generated);
970 if (!build->generated)
971 return isl_ast_build_free(build);
973 return build;
974 error:
975 isl_ast_build_free(build);
976 isl_set_free(set);
977 return NULL;
980 /* Replace the set of pending constraints by "guard", which is then
981 * no longer considered as pending.
982 * That is, add "guard" to the generated constraints and clear all pending
983 * constraints, making the domain equal to the generated constraints.
985 __isl_give isl_ast_build *isl_ast_build_replace_pending_by_guard(
986 __isl_take isl_ast_build *build, __isl_take isl_set *guard)
988 build = isl_ast_build_restrict_generated(build, guard);
989 build = isl_ast_build_cow(build);
990 if (!build)
991 return NULL;
993 isl_set_free(build->domain);
994 build->domain = isl_set_copy(build->generated);
995 isl_set_free(build->pending);
996 build->pending = isl_set_universe(isl_set_get_space(build->domain));
998 if (!build->pending)
999 return isl_ast_build_free(build);
1001 return build;
1004 /* Intersect build->domain with "set", where "set" is specified
1005 * in terms of the external schedule domain.
1007 __isl_give isl_ast_build *isl_ast_build_restrict(
1008 __isl_take isl_ast_build *build, __isl_take isl_set *set)
1010 isl_bool needs_map;
1012 if (isl_set_is_params(set))
1013 return isl_ast_build_restrict_generated(build, set);
1015 needs_map = isl_ast_build_need_schedule_map(build);
1016 if (needs_map < 0)
1017 goto error;
1018 if (needs_map) {
1019 isl_multi_aff *ma;
1020 ma = isl_ast_build_get_schedule_map_multi_aff(build);
1021 set = isl_set_preimage_multi_aff(set, ma);
1023 return isl_ast_build_restrict_generated(build, set);
1024 error:
1025 isl_ast_build_free(build);
1026 isl_set_free(set);
1027 return NULL;
1030 /* Replace build->executed by "executed".
1032 __isl_give isl_ast_build *isl_ast_build_set_executed(
1033 __isl_take isl_ast_build *build, __isl_take isl_union_map *executed)
1035 build = isl_ast_build_cow(build);
1036 if (!build)
1037 goto error;
1039 isl_union_map_free(build->executed);
1040 build->executed = executed;
1042 return build;
1043 error:
1044 isl_ast_build_free(build);
1045 isl_union_map_free(executed);
1046 return NULL;
1049 /* Does "build" point to a band node?
1050 * That is, are we currently handling a band node inside a schedule tree?
1052 int isl_ast_build_has_schedule_node(__isl_keep isl_ast_build *build)
1054 if (!build)
1055 return -1;
1056 return build->node != NULL;
1059 /* Return a copy of the band node that "build" refers to.
1061 __isl_give isl_schedule_node *isl_ast_build_get_schedule_node(
1062 __isl_keep isl_ast_build *build)
1064 if (!build)
1065 return NULL;
1066 return isl_schedule_node_copy(build->node);
1069 /* Extract the loop AST generation types for the members of build->node
1070 * and store them in build->loop_type.
1072 static __isl_give isl_ast_build *extract_loop_types(
1073 __isl_take isl_ast_build *build)
1075 int i;
1076 isl_ctx *ctx;
1077 isl_schedule_node *node;
1079 if (!build)
1080 return NULL;
1081 ctx = isl_ast_build_get_ctx(build);
1082 if (!build->node)
1083 isl_die(ctx, isl_error_internal, "missing AST node",
1084 return isl_ast_build_free(build));
1086 free(build->loop_type);
1087 build->n = isl_schedule_node_band_n_member(build->node);
1088 build->loop_type = isl_alloc_array(ctx,
1089 enum isl_ast_loop_type, build->n);
1090 if (build->n && !build->loop_type)
1091 return isl_ast_build_free(build);
1092 node = build->node;
1093 for (i = 0; i < build->n; ++i)
1094 build->loop_type[i] =
1095 isl_schedule_node_band_member_get_ast_loop_type(node, i);
1097 return build;
1100 /* Replace the band node that "build" refers to by "node" and
1101 * extract the corresponding loop AST generation types.
1103 __isl_give isl_ast_build *isl_ast_build_set_schedule_node(
1104 __isl_take isl_ast_build *build,
1105 __isl_take isl_schedule_node *node)
1107 build = isl_ast_build_cow(build);
1108 if (!build || !node)
1109 goto error;
1111 isl_schedule_node_free(build->node);
1112 build->node = node;
1114 build = extract_loop_types(build);
1116 return build;
1117 error:
1118 isl_ast_build_free(build);
1119 isl_schedule_node_free(node);
1120 return NULL;
1123 /* Remove any reference to a band node from "build".
1125 __isl_give isl_ast_build *isl_ast_build_reset_schedule_node(
1126 __isl_take isl_ast_build *build)
1128 build = isl_ast_build_cow(build);
1129 if (!build)
1130 return NULL;
1132 isl_schedule_node_free(build->node);
1133 build->node = NULL;
1135 return build;
1138 /* Return a copy of the current schedule domain.
1140 __isl_give isl_set *isl_ast_build_get_domain(__isl_keep isl_ast_build *build)
1142 return build ? isl_set_copy(build->domain) : NULL;
1145 /* Return a copy of the set of pending constraints.
1147 __isl_give isl_set *isl_ast_build_get_pending(
1148 __isl_keep isl_ast_build *build)
1150 return build ? isl_set_copy(build->pending) : NULL;
1153 /* Return a copy of the set of generated constraints.
1155 __isl_give isl_set *isl_ast_build_get_generated(
1156 __isl_keep isl_ast_build *build)
1158 return build ? isl_set_copy(build->generated) : NULL;
1161 /* Return a copy of the map from the internal schedule domain
1162 * to the original input schedule domain.
1164 __isl_give isl_multi_aff *isl_ast_build_get_internal2input(
1165 __isl_keep isl_ast_build *build)
1167 return build ? isl_multi_aff_copy(build->internal2input) : NULL;
1170 /* Return the number of variables of the given type
1171 * in the (internal) schedule space.
1173 isl_size isl_ast_build_dim(__isl_keep isl_ast_build *build,
1174 enum isl_dim_type type)
1176 if (!build)
1177 return isl_size_error;
1178 return isl_set_dim(build->domain, type);
1181 /* Return the (schedule) space of "build".
1183 * If "internal" is set, then this space is the space of the internal
1184 * representation of the entire schedule, including those parts for
1185 * which no code has been generated yet.
1187 * If "internal" is not set, then this space is the external representation
1188 * of the loops generated so far.
1190 __isl_give isl_space *isl_ast_build_get_space(__isl_keep isl_ast_build *build,
1191 int internal)
1193 int i;
1194 isl_size dim;
1195 isl_bool needs_map;
1196 isl_space *space;
1198 if (!build)
1199 return NULL;
1201 space = isl_set_get_space(build->domain);
1202 if (internal)
1203 return space;
1205 needs_map = isl_ast_build_need_schedule_map(build);
1206 if (needs_map < 0)
1207 return isl_space_free(space);
1208 if (!needs_map)
1209 return space;
1211 dim = isl_ast_build_dim(build, isl_dim_set);
1212 if (dim < 0)
1213 return isl_space_free(space);
1214 space = isl_space_drop_dims(space, isl_dim_set,
1215 build->depth, dim - build->depth);
1216 for (i = build->depth - 1; i >= 0; --i) {
1217 isl_bool affine = isl_ast_build_has_affine_value(build, i);
1219 if (affine < 0)
1220 return isl_space_free(space);
1221 if (affine)
1222 space = isl_space_drop_dims(space, isl_dim_set, i, 1);
1225 return space;
1228 /* Return the external representation of the schedule space of "build",
1229 * i.e., a space with a dimension for each loop generated so far,
1230 * with the names of the dimensions set to the loop iterators.
1232 __isl_give isl_space *isl_ast_build_get_schedule_space(
1233 __isl_keep isl_ast_build *build)
1235 isl_space *space;
1236 int i, skip;
1238 if (!build)
1239 return NULL;
1241 space = isl_ast_build_get_space(build, 0);
1243 skip = 0;
1244 for (i = 0; i < build->depth; ++i) {
1245 isl_id *id;
1247 if (isl_ast_build_has_affine_value(build, i)) {
1248 skip++;
1249 continue;
1252 id = isl_ast_build_get_iterator_id(build, i);
1253 space = isl_space_set_dim_id(space, isl_dim_set, i - skip, id);
1256 return space;
1259 /* Return the current schedule, as stored in build->executed, in terms
1260 * of the external schedule domain.
1262 __isl_give isl_union_map *isl_ast_build_get_schedule(
1263 __isl_keep isl_ast_build *build)
1265 isl_bool needs_map;
1266 isl_union_map *executed;
1267 isl_union_map *schedule;
1269 needs_map = isl_ast_build_need_schedule_map(build);
1270 if (needs_map < 0)
1271 return NULL;
1273 executed = isl_union_map_copy(build->executed);
1274 if (needs_map) {
1275 isl_map *proj = isl_ast_build_get_schedule_map(build);
1276 executed = isl_union_map_apply_domain(executed,
1277 isl_union_map_from_map(proj));
1279 schedule = isl_union_map_reverse(executed);
1281 return schedule;
1284 /* Return the iterator attached to the internal schedule dimension "pos".
1286 __isl_give isl_id *isl_ast_build_get_iterator_id(
1287 __isl_keep isl_ast_build *build, int pos)
1289 if (!build)
1290 return NULL;
1292 return isl_id_list_get_id(build->iterators, pos);
1295 /* Set the stride and offset of the current dimension to the given
1296 * value and expression.
1298 static __isl_give isl_ast_build *set_stride(__isl_take isl_ast_build *build,
1299 __isl_take isl_val *stride, __isl_take isl_aff *offset)
1301 int pos;
1303 build = isl_ast_build_cow(build);
1304 if (!build || !stride || !offset)
1305 goto error;
1307 pos = build->depth;
1309 build->strides = isl_vec_set_element_val(build->strides, pos, stride);
1310 build->offsets = isl_multi_aff_set_aff(build->offsets, pos, offset);
1311 if (!build->strides || !build->offsets)
1312 return isl_ast_build_free(build);
1314 return build;
1315 error:
1316 isl_val_free(stride);
1317 isl_aff_free(offset);
1318 return isl_ast_build_free(build);
1321 /* Return a set expressing the stride constraint at the current depth.
1323 * In particular, if the current iterator (i) is known to attain values
1325 * f + s a
1327 * where f is the offset and s is the stride, then the returned set
1328 * expresses the constraint
1330 * (f - i) mod s = 0
1332 __isl_give isl_set *isl_ast_build_get_stride_constraint(
1333 __isl_keep isl_ast_build *build)
1335 isl_aff *aff;
1336 isl_set *set;
1337 isl_val *stride;
1338 int pos;
1340 if (!build)
1341 return NULL;
1343 pos = build->depth;
1345 if (!isl_ast_build_has_stride(build, pos))
1346 return isl_set_universe(isl_ast_build_get_space(build, 1));
1348 stride = isl_ast_build_get_stride(build, pos);
1349 aff = isl_ast_build_get_offset(build, pos);
1350 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, -1);
1351 aff = isl_aff_mod_val(aff, stride);
1352 set = isl_set_from_basic_set(isl_aff_zero_basic_set(aff));
1354 return set;
1357 /* Return the expansion implied by the stride and offset at the current
1358 * depth.
1360 * That is, return the mapping
1362 * [i_0, ..., i_{d-1}, i_d, i_{d+1}, ...]
1363 * -> [i_0, ..., i_{d-1}, s * i_d + offset(i), i_{d+1}, ...]
1365 * where s is the stride at the current depth d and offset(i) is
1366 * the corresponding offset.
1368 __isl_give isl_multi_aff *isl_ast_build_get_stride_expansion(
1369 __isl_keep isl_ast_build *build)
1371 isl_space *space;
1372 isl_multi_aff *ma;
1373 int pos;
1374 isl_aff *aff, *offset;
1375 isl_val *stride;
1377 if (!build)
1378 return NULL;
1380 pos = isl_ast_build_get_depth(build);
1381 space = isl_ast_build_get_space(build, 1);
1382 space = isl_space_map_from_set(space);
1383 ma = isl_multi_aff_identity(space);
1385 if (!isl_ast_build_has_stride(build, pos))
1386 return ma;
1388 offset = isl_ast_build_get_offset(build, pos);
1389 stride = isl_ast_build_get_stride(build, pos);
1390 aff = isl_multi_aff_get_aff(ma, pos);
1391 aff = isl_aff_scale_val(aff, stride);
1392 aff = isl_aff_add(aff, offset);
1393 ma = isl_multi_aff_set_aff(ma, pos, aff);
1395 return ma;
1398 /* Add constraints corresponding to any previously detected
1399 * stride on the current dimension to build->domain.
1401 __isl_give isl_ast_build *isl_ast_build_include_stride(
1402 __isl_take isl_ast_build *build)
1404 isl_set *set;
1406 if (!build)
1407 return NULL;
1408 if (!isl_ast_build_has_stride(build, build->depth))
1409 return build;
1410 build = isl_ast_build_cow(build);
1411 if (!build)
1412 return NULL;
1414 set = isl_ast_build_get_stride_constraint(build);
1416 build->domain = isl_set_intersect(build->domain, isl_set_copy(set));
1417 build->generated = isl_set_intersect(build->generated, set);
1418 if (!build->domain || !build->generated)
1419 return isl_ast_build_free(build);
1421 return build;
1424 /* Check if the constraints in "set" imply any stride on the current
1425 * dimension and, if so, record the stride information in "build"
1426 * and return the updated "build".
1428 * We assume that inner dimensions have been eliminated from "set"
1429 * by the caller. This is needed because the common stride
1430 * may be imposed by different inner dimensions on different parts of
1431 * the domain.
1432 * The assumption ensures that the lower bound does not depend
1433 * on inner dimensions.
1435 __isl_give isl_ast_build *isl_ast_build_detect_strides(
1436 __isl_take isl_ast_build *build, __isl_take isl_set *set)
1438 int pos;
1439 isl_bool no_stride;
1440 isl_val *stride;
1441 isl_aff *offset;
1442 isl_stride_info *si;
1444 if (!build)
1445 goto error;
1447 pos = isl_ast_build_get_depth(build);
1448 si = isl_set_get_stride_info(set, pos);
1449 stride = isl_stride_info_get_stride(si);
1450 offset = isl_stride_info_get_offset(si);
1451 isl_stride_info_free(si);
1452 isl_set_free(set);
1454 no_stride = isl_val_is_one(stride);
1455 if (no_stride >= 0 && !no_stride)
1456 return set_stride(build, stride, offset);
1457 isl_val_free(stride);
1458 isl_aff_free(offset);
1459 if (no_stride < 0)
1460 return isl_ast_build_free(build);
1461 return build;
1462 error:
1463 isl_set_free(set);
1464 return NULL;
1467 struct isl_ast_build_involves_data {
1468 int depth;
1469 int involves;
1472 /* Check if "map" involves the input dimension data->depth.
1474 static isl_stat involves_depth(__isl_take isl_map *map, void *user)
1476 struct isl_ast_build_involves_data *data = user;
1478 data->involves = isl_map_involves_dims(map, isl_dim_in, data->depth, 1);
1479 isl_map_free(map);
1481 if (data->involves < 0 || data->involves)
1482 return isl_stat_error;
1483 return isl_stat_ok;
1486 /* Do any options depend on the value of the dimension at the current depth?
1488 int isl_ast_build_options_involve_depth(__isl_keep isl_ast_build *build)
1490 struct isl_ast_build_involves_data data;
1492 if (!build)
1493 return -1;
1495 data.depth = build->depth;
1496 data.involves = 0;
1498 if (isl_union_map_foreach_map(build->options,
1499 &involves_depth, &data) < 0) {
1500 if (data.involves < 0 || !data.involves)
1501 return -1;
1504 return data.involves;
1507 /* Construct the map
1509 * { [i] -> [i] : i < pos; [i] -> [i + 1] : i >= pos }
1511 * with "space" the parameter space of the constructed map.
1513 static __isl_give isl_map *construct_insertion_map(__isl_take isl_space *space,
1514 int pos)
1516 isl_constraint *c;
1517 isl_basic_map *bmap1, *bmap2;
1519 space = isl_space_set_from_params(space);
1520 space = isl_space_add_dims(space, isl_dim_set, 1);
1521 space = isl_space_map_from_set(space);
1522 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
1523 c = isl_constraint_set_coefficient_si(c, isl_dim_in, 0, 1);
1524 c = isl_constraint_set_coefficient_si(c, isl_dim_out, 0, -1);
1525 bmap1 = isl_basic_map_from_constraint(isl_constraint_copy(c));
1526 c = isl_constraint_set_constant_si(c, 1);
1527 bmap2 = isl_basic_map_from_constraint(c);
1529 bmap1 = isl_basic_map_upper_bound_si(bmap1, isl_dim_in, 0, pos - 1);
1530 bmap2 = isl_basic_map_lower_bound_si(bmap2, isl_dim_in, 0, pos);
1532 return isl_basic_map_union(bmap1, bmap2);
1535 static const char *option_str[] = {
1536 [isl_ast_loop_atomic] = "atomic",
1537 [isl_ast_loop_unroll] = "unroll",
1538 [isl_ast_loop_separate] = "separate"
1541 /* Update the "options" to reflect the insertion of a dimension
1542 * at position "pos" in the schedule domain space.
1543 * "space" is the original domain space before the insertion and
1544 * may be named and/or structured.
1546 * The (relevant) input options all have "space" as domain, which
1547 * has to be mapped to the extended space.
1548 * The values of the ranges also refer to the schedule domain positions
1549 * and they therefore also need to be adjusted. In particular, values
1550 * smaller than pos do not need to change, while values greater than or
1551 * equal to pos need to be incremented.
1552 * That is, we need to apply the following map.
1554 * { atomic[i] -> atomic[i] : i < pos; [i] -> [i + 1] : i >= pos;
1555 * unroll[i] -> unroll[i] : i < pos; [i] -> [i + 1] : i >= pos;
1556 * separate[i] -> separate[i] : i < pos; [i] -> [i + 1] : i >= pos;
1557 * separation_class[[i] -> [c]]
1558 * -> separation_class[[i] -> [c]] : i < pos;
1559 * separation_class[[i] -> [c]]
1560 * -> separation_class[[i + 1] -> [c]] : i >= pos }
1562 static __isl_give isl_union_map *options_insert_dim(
1563 __isl_take isl_union_map *options, __isl_take isl_space *space, int pos)
1565 isl_map *map;
1566 isl_union_map *insertion;
1567 enum isl_ast_loop_type type;
1568 const char *name = "separation_class";
1570 space = isl_space_map_from_set(space);
1571 map = isl_map_identity(space);
1572 map = isl_map_insert_dims(map, isl_dim_out, pos, 1);
1573 options = isl_union_map_apply_domain(options,
1574 isl_union_map_from_map(map));
1576 if (!options)
1577 return NULL;
1579 map = construct_insertion_map(isl_union_map_get_space(options), pos);
1581 insertion = isl_union_map_empty(isl_union_map_get_space(options));
1583 for (type = isl_ast_loop_atomic;
1584 type <= isl_ast_loop_separate; ++type) {
1585 isl_map *map_type = isl_map_copy(map);
1586 const char *name = option_str[type];
1587 map_type = isl_map_set_tuple_name(map_type, isl_dim_in, name);
1588 map_type = isl_map_set_tuple_name(map_type, isl_dim_out, name);
1589 insertion = isl_union_map_add_map(insertion, map_type);
1592 map = isl_map_product(map, isl_map_identity(isl_map_get_space(map)));
1593 map = isl_map_set_tuple_name(map, isl_dim_in, name);
1594 map = isl_map_set_tuple_name(map, isl_dim_out, name);
1595 insertion = isl_union_map_add_map(insertion, map);
1597 options = isl_union_map_apply_range(options, insertion);
1599 return options;
1602 /* If we are generating an AST from a schedule tree (build->node is set),
1603 * then update the loop AST generation types
1604 * to reflect the insertion of a dimension at (global) position "pos"
1605 * in the schedule domain space.
1606 * We do not need to adjust any isolate option since we would not be inserting
1607 * any dimensions if there were any isolate option.
1609 static __isl_give isl_ast_build *node_insert_dim(
1610 __isl_take isl_ast_build *build, int pos)
1612 int i;
1613 int local_pos;
1614 enum isl_ast_loop_type *loop_type;
1615 isl_ctx *ctx;
1617 build = isl_ast_build_cow(build);
1618 if (!build)
1619 return NULL;
1620 if (!build->node)
1621 return build;
1623 ctx = isl_ast_build_get_ctx(build);
1624 local_pos = pos - build->outer_pos;
1625 loop_type = isl_realloc_array(ctx, build->loop_type,
1626 enum isl_ast_loop_type, build->n + 1);
1627 if (!loop_type)
1628 return isl_ast_build_free(build);
1629 build->loop_type = loop_type;
1630 for (i = build->n - 1; i >= local_pos; --i)
1631 loop_type[i + 1] = loop_type[i];
1632 loop_type[local_pos] = isl_ast_loop_default;
1633 build->n++;
1635 return build;
1638 /* Insert a single dimension in the schedule domain at position "pos".
1639 * The new dimension is given an isl_id with the empty string as name.
1641 * The main difficulty is updating build->options to reflect the
1642 * extra dimension. This is handled in options_insert_dim.
1644 * Note that because of the dimension manipulations, the resulting
1645 * schedule domain space will always be unnamed and unstructured.
1646 * However, the original schedule domain space may be named and/or
1647 * structured, so we have to take this possibility into account
1648 * while performing the transformations.
1650 * Since the inserted schedule dimension is used by the caller
1651 * to differentiate between different domain spaces, there is
1652 * no longer a uniform mapping from the internal schedule space
1653 * to the input schedule space. The internal2input mapping is
1654 * therefore removed.
1656 __isl_give isl_ast_build *isl_ast_build_insert_dim(
1657 __isl_take isl_ast_build *build, int pos)
1659 isl_ctx *ctx;
1660 isl_space *space, *ma_space;
1661 isl_id *id;
1662 isl_multi_aff *ma;
1664 build = isl_ast_build_cow(build);
1665 if (!build)
1666 return NULL;
1668 ctx = isl_ast_build_get_ctx(build);
1669 id = isl_id_alloc(ctx, "", NULL);
1670 if (!build->node)
1671 space = isl_ast_build_get_space(build, 1);
1672 build->iterators = isl_id_list_insert(build->iterators, pos, id);
1673 build->domain = isl_set_insert_dims(build->domain,
1674 isl_dim_set, pos, 1);
1675 build->generated = isl_set_insert_dims(build->generated,
1676 isl_dim_set, pos, 1);
1677 build->pending = isl_set_insert_dims(build->pending,
1678 isl_dim_set, pos, 1);
1679 build->strides = isl_vec_insert_els(build->strides, pos, 1);
1680 build->strides = isl_vec_set_element_si(build->strides, pos, 1);
1681 ma_space = isl_space_params(isl_multi_aff_get_space(build->offsets));
1682 ma_space = isl_space_set_from_params(ma_space);
1683 ma_space = isl_space_add_dims(ma_space, isl_dim_set, 1);
1684 ma_space = isl_space_map_from_set(ma_space);
1685 ma = isl_multi_aff_zero(isl_space_copy(ma_space));
1686 build->offsets = isl_multi_aff_splice(build->offsets, pos, pos, ma);
1687 ma = isl_multi_aff_identity(ma_space);
1688 build->values = isl_multi_aff_splice(build->values, pos, pos, ma);
1689 if (!build->node)
1690 build->options = options_insert_dim(build->options, space, pos);
1691 build->internal2input = isl_multi_aff_free(build->internal2input);
1693 if (!build->iterators || !build->domain || !build->generated ||
1694 !build->pending || !build->values ||
1695 !build->strides || !build->offsets || !build->options)
1696 return isl_ast_build_free(build);
1698 build = node_insert_dim(build, pos);
1700 return build;
1703 /* Scale down the current dimension by a factor of "m".
1704 * "umap" is an isl_union_map that implements the scaling down.
1705 * That is, it is of the form
1707 * { [.... i ....] -> [.... i' ....] : i = m i' }
1709 * This function is called right after the strides have been
1710 * detected, but before any constraints on the current dimension
1711 * have been included in build->domain.
1712 * We therefore only need to update stride, offset, the options and
1713 * the mapping from internal schedule space to the original schedule
1714 * space, if we are still keeping track of such a mapping.
1715 * The latter mapping is updated by plugging in
1716 * { [... i ...] -> [... m i ... ] }.
1718 __isl_give isl_ast_build *isl_ast_build_scale_down(
1719 __isl_take isl_ast_build *build, __isl_take isl_val *m,
1720 __isl_take isl_union_map *umap)
1722 isl_aff *aff;
1723 isl_val *v;
1724 int depth;
1726 build = isl_ast_build_cow(build);
1727 if (!build || !umap || !m)
1728 goto error;
1730 depth = build->depth;
1732 if (build->internal2input) {
1733 isl_space *space;
1734 isl_multi_aff *ma;
1735 isl_aff *aff;
1737 space = isl_multi_aff_get_space(build->internal2input);
1738 space = isl_space_map_from_set(isl_space_domain(space));
1739 ma = isl_multi_aff_identity(space);
1740 aff = isl_multi_aff_get_aff(ma, depth);
1741 aff = isl_aff_scale_val(aff, isl_val_copy(m));
1742 ma = isl_multi_aff_set_aff(ma, depth, aff);
1743 build->internal2input =
1744 isl_multi_aff_pullback_multi_aff(build->internal2input, ma);
1745 if (!build->internal2input)
1746 goto error;
1749 v = isl_vec_get_element_val(build->strides, depth);
1750 v = isl_val_div(v, isl_val_copy(m));
1751 build->strides = isl_vec_set_element_val(build->strides, depth, v);
1753 aff = isl_multi_aff_get_aff(build->offsets, depth);
1754 aff = isl_aff_scale_down_val(aff, m);
1755 build->offsets = isl_multi_aff_set_aff(build->offsets, depth, aff);
1756 build->options = isl_union_map_apply_domain(build->options, umap);
1757 if (!build->strides || !build->offsets || !build->options)
1758 return isl_ast_build_free(build);
1760 return build;
1761 error:
1762 isl_val_free(m);
1763 isl_union_map_free(umap);
1764 return isl_ast_build_free(build);
1767 /* Return a list of "n" isl_ids called "c%d", with "%d" starting at "first".
1768 * If an isl_id with such a name already appears among the parameters
1769 * in build->domain, then adjust the name to "c%d_%d".
1771 static __isl_give isl_id_list *generate_names(isl_ctx *ctx, int n, int first,
1772 __isl_keep isl_ast_build *build)
1774 int i;
1775 isl_id_list *names;
1777 names = isl_id_list_alloc(ctx, n);
1778 for (i = 0; i < n; ++i) {
1779 isl_id *id;
1781 id = generate_name(ctx, first + i, build);
1782 names = isl_id_list_add(names, id);
1785 return names;
1788 /* Embed "options" into the given isl_ast_build space.
1790 * This function is called from within a nested call to
1791 * isl_ast_build_node_from_schedule_map.
1792 * "options" refers to the additional schedule,
1793 * while space refers to both the space of the outer isl_ast_build and
1794 * that of the additional schedule.
1795 * Specifically, space is of the form
1797 * [I -> S]
1799 * while options lives in the space(s)
1801 * S -> *
1803 * We compute
1805 * [I -> S] -> S
1807 * and compose this with options, to obtain the new options
1808 * living in the space(s)
1810 * [I -> S] -> *
1812 static __isl_give isl_union_map *embed_options(
1813 __isl_take isl_union_map *options, __isl_take isl_space *space)
1815 isl_map *map;
1817 map = isl_map_universe(isl_space_unwrap(space));
1818 map = isl_map_range_map(map);
1820 options = isl_union_map_apply_range(
1821 isl_union_map_from_map(map), options);
1823 return options;
1826 /* Update "build" for use in a (possibly nested) code generation. That is,
1827 * extend "build" from an AST build on some domain O to an AST build
1828 * on domain [O -> S], with S corresponding to "space".
1829 * If the original domain is a parameter domain, then the new domain is
1830 * simply S.
1831 * "iterators" is a list of iterators for S, but the number of elements
1832 * may be smaller or greater than the number of set dimensions of S.
1833 * If "keep_iterators" is set, then any extra ids in build->iterators
1834 * are reused for S. Otherwise, these extra ids are dropped.
1836 * We first update build->outer_pos to the current depth.
1837 * This depth is zero in case this is the outermost code generation.
1839 * We then add additional ids such that the number of iterators is at least
1840 * equal to the dimension of the new build domain.
1842 * If the original domain is parametric, then we are constructing
1843 * an isl_ast_build for the outer code generation and we pass control
1844 * to isl_ast_build_init.
1846 * Otherwise, we adjust the fields of "build" to include "space".
1848 __isl_give isl_ast_build *isl_ast_build_product(
1849 __isl_take isl_ast_build *build, __isl_take isl_space *space)
1851 isl_ctx *ctx;
1852 isl_vec *strides;
1853 isl_set *set;
1854 isl_multi_aff *embedding;
1855 isl_size dim, space_dim;
1856 int n_it;
1858 build = isl_ast_build_cow(build);
1859 if (!build)
1860 goto error;
1862 build->outer_pos = build->depth;
1864 ctx = isl_ast_build_get_ctx(build);
1865 dim = isl_ast_build_dim(build, isl_dim_set);
1866 space_dim = isl_space_dim(space, isl_dim_set);
1867 n_it = isl_id_list_n_id(build->iterators);
1868 if (dim < 0 || space_dim < 0)
1869 goto error;
1870 dim += space_dim;
1871 if (n_it < dim) {
1872 isl_id_list *l;
1873 l = generate_names(ctx, dim - n_it, n_it, build);
1874 build->iterators = isl_id_list_concat(build->iterators, l);
1877 if (isl_set_is_params(build->domain))
1878 return isl_ast_build_init(build, space);
1880 set = isl_set_universe(isl_space_copy(space));
1881 build->domain = isl_set_product(build->domain, isl_set_copy(set));
1882 build->pending = isl_set_product(build->pending, isl_set_copy(set));
1883 build->generated = isl_set_product(build->generated, set);
1885 strides = isl_vec_alloc(ctx, space_dim);
1886 strides = isl_vec_set_si(strides, 1);
1887 build->strides = isl_vec_concat(build->strides, strides);
1889 space = isl_space_map_from_set(space);
1890 build->offsets = isl_multi_aff_align_params(build->offsets,
1891 isl_space_copy(space));
1892 build->offsets = isl_multi_aff_product(build->offsets,
1893 isl_multi_aff_zero(isl_space_copy(space)));
1894 build->values = isl_multi_aff_align_params(build->values,
1895 isl_space_copy(space));
1896 embedding = isl_multi_aff_identity(space);
1897 build->values = isl_multi_aff_product(build->values,
1898 isl_multi_aff_copy(embedding));
1899 if (build->internal2input) {
1900 build->internal2input =
1901 isl_multi_aff_product(build->internal2input, embedding);
1902 build->internal2input =
1903 isl_multi_aff_flatten_range(build->internal2input);
1904 if (!build->internal2input)
1905 return isl_ast_build_free(build);
1906 } else {
1907 isl_multi_aff_free(embedding);
1910 space = isl_ast_build_get_space(build, 1);
1911 build->options = embed_options(build->options, space);
1913 if (!build->iterators || !build->domain || !build->generated ||
1914 !build->pending || !build->values ||
1915 !build->strides || !build->offsets || !build->options)
1916 return isl_ast_build_free(build);
1918 return build;
1919 error:
1920 isl_ast_build_free(build);
1921 isl_space_free(space);
1922 return NULL;
1925 /* Does "aff" only attain non-negative values over build->domain?
1926 * That is, does it not attain any negative values?
1928 int isl_ast_build_aff_is_nonneg(__isl_keep isl_ast_build *build,
1929 __isl_keep isl_aff *aff)
1931 isl_set *test;
1932 int empty;
1934 if (!build)
1935 return -1;
1937 aff = isl_aff_copy(aff);
1938 test = isl_set_from_basic_set(isl_aff_neg_basic_set(aff));
1939 test = isl_set_intersect(test, isl_set_copy(build->domain));
1940 empty = isl_set_is_empty(test);
1941 isl_set_free(test);
1943 return empty;
1946 /* Does the dimension at (internal) position "pos" have a non-trivial stride?
1948 isl_bool isl_ast_build_has_stride(__isl_keep isl_ast_build *build, int pos)
1950 isl_val *v;
1951 isl_bool has_stride;
1953 if (!build)
1954 return isl_bool_error;
1956 v = isl_vec_get_element_val(build->strides, pos);
1957 has_stride = isl_bool_not(isl_val_is_one(v));
1958 isl_val_free(v);
1960 return has_stride;
1963 /* Given that the dimension at position "pos" takes on values
1965 * f + s a
1967 * with a an integer, return s through *stride.
1969 __isl_give isl_val *isl_ast_build_get_stride(__isl_keep isl_ast_build *build,
1970 int pos)
1972 if (!build)
1973 return NULL;
1975 return isl_vec_get_element_val(build->strides, pos);
1978 /* Given that the dimension at position "pos" takes on values
1980 * f + s a
1982 * with a an integer, return f.
1984 __isl_give isl_aff *isl_ast_build_get_offset(
1985 __isl_keep isl_ast_build *build, int pos)
1987 if (!build)
1988 return NULL;
1990 return isl_multi_aff_get_aff(build->offsets, pos);
1993 /* Is the dimension at position "pos" known to attain only a single
1994 * value that, moreover, can be described by a single affine expression
1995 * in terms of the outer dimensions and parameters?
1997 * If not, then the corresponding affine expression in build->values
1998 * is set to be equal to the same input dimension.
1999 * Otherwise, it is set to the requested expression in terms of
2000 * outer dimensions and parameters.
2002 isl_bool isl_ast_build_has_affine_value(__isl_keep isl_ast_build *build,
2003 int pos)
2005 isl_aff *aff;
2006 isl_bool involves;
2008 if (!build)
2009 return isl_bool_error;
2011 aff = isl_multi_aff_get_aff(build->values, pos);
2012 involves = isl_aff_involves_dims(aff, isl_dim_in, pos, 1);
2013 isl_aff_free(aff);
2015 return isl_bool_not(involves);
2018 /* Plug in the known values (fixed affine expressions in terms of
2019 * parameters and outer loop iterators) of all loop iterators
2020 * in the domain of "umap".
2022 * We simply precompose "umap" with build->values.
2024 __isl_give isl_union_map *isl_ast_build_substitute_values_union_map_domain(
2025 __isl_keep isl_ast_build *build, __isl_take isl_union_map *umap)
2027 isl_multi_aff *values;
2029 if (!build)
2030 return isl_union_map_free(umap);
2032 values = isl_multi_aff_copy(build->values);
2033 umap = isl_union_map_preimage_domain_multi_aff(umap, values);
2035 return umap;
2038 /* Is the current dimension known to attain only a single value?
2040 int isl_ast_build_has_value(__isl_keep isl_ast_build *build)
2042 if (!build)
2043 return -1;
2045 return build->value != NULL;
2048 /* Simplify the basic set "bset" based on what we know about
2049 * the iterators of already generated loops.
2051 * "bset" is assumed to live in the (internal) schedule domain.
2053 __isl_give isl_basic_set *isl_ast_build_compute_gist_basic_set(
2054 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
2056 if (!build)
2057 goto error;
2059 bset = isl_basic_set_preimage_multi_aff(bset,
2060 isl_multi_aff_copy(build->values));
2061 bset = isl_basic_set_gist(bset,
2062 isl_set_simple_hull(isl_set_copy(build->domain)));
2064 return bset;
2065 error:
2066 isl_basic_set_free(bset);
2067 return NULL;
2070 /* Simplify the set "set" based on what we know about
2071 * the iterators of already generated loops.
2073 * "set" is assumed to live in the (internal) schedule domain.
2075 __isl_give isl_set *isl_ast_build_compute_gist(
2076 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2078 if (!build)
2079 goto error;
2081 if (!isl_set_is_params(set))
2082 set = isl_set_preimage_multi_aff(set,
2083 isl_multi_aff_copy(build->values));
2084 set = isl_set_gist(set, isl_set_copy(build->domain));
2086 return set;
2087 error:
2088 isl_set_free(set);
2089 return NULL;
2092 /* Include information about what we know about the iterators of
2093 * already generated loops to "set".
2095 * We currently only plug in the known affine values of outer loop
2096 * iterators.
2097 * In principle we could also introduce equalities or even other
2098 * constraints implied by the intersection of "set" and build->domain.
2100 __isl_give isl_set *isl_ast_build_specialize(__isl_keep isl_ast_build *build,
2101 __isl_take isl_set *set)
2103 if (!build)
2104 return isl_set_free(set);
2106 return isl_set_preimage_multi_aff(set,
2107 isl_multi_aff_copy(build->values));
2110 /* Plug in the known affine values of outer loop iterators in "bset".
2112 __isl_give isl_basic_set *isl_ast_build_specialize_basic_set(
2113 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
2115 if (!build)
2116 return isl_basic_set_free(bset);
2118 return isl_basic_set_preimage_multi_aff(bset,
2119 isl_multi_aff_copy(build->values));
2122 /* Simplify the map "map" based on what we know about
2123 * the iterators of already generated loops.
2125 * The domain of "map" is assumed to live in the (internal) schedule domain.
2127 __isl_give isl_map *isl_ast_build_compute_gist_map_domain(
2128 __isl_keep isl_ast_build *build, __isl_take isl_map *map)
2130 if (!build)
2131 goto error;
2133 map = isl_map_gist_domain(map, isl_set_copy(build->domain));
2135 return map;
2136 error:
2137 isl_map_free(map);
2138 return NULL;
2141 /* Simplify the affine expression "aff" based on what we know about
2142 * the iterators of already generated loops.
2144 * The domain of "aff" is assumed to live in the (internal) schedule domain.
2146 __isl_give isl_aff *isl_ast_build_compute_gist_aff(
2147 __isl_keep isl_ast_build *build, __isl_take isl_aff *aff)
2149 if (!build)
2150 goto error;
2152 aff = isl_aff_gist(aff, isl_set_copy(build->domain));
2154 return aff;
2155 error:
2156 isl_aff_free(aff);
2157 return NULL;
2160 /* Simplify the piecewise affine expression "aff" based on what we know about
2161 * the iterators of already generated loops.
2163 * The domain of "pa" is assumed to live in the (internal) schedule domain.
2165 __isl_give isl_pw_aff *isl_ast_build_compute_gist_pw_aff(
2166 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2168 if (!build)
2169 goto error;
2171 if (!isl_set_is_params(build->domain))
2172 pa = isl_pw_aff_pullback_multi_aff(pa,
2173 isl_multi_aff_copy(build->values));
2174 pa = isl_pw_aff_gist(pa, isl_set_copy(build->domain));
2176 return pa;
2177 error:
2178 isl_pw_aff_free(pa);
2179 return NULL;
2182 /* Simplify the piecewise multi-affine expression "aff" based on what
2183 * we know about the iterators of already generated loops.
2185 * The domain of "pma" is assumed to live in the (internal) schedule domain.
2187 __isl_give isl_pw_multi_aff *isl_ast_build_compute_gist_pw_multi_aff(
2188 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
2190 if (!build)
2191 goto error;
2193 pma = isl_pw_multi_aff_pullback_multi_aff(pma,
2194 isl_multi_aff_copy(build->values));
2195 pma = isl_pw_multi_aff_gist(pma, isl_set_copy(build->domain));
2197 return pma;
2198 error:
2199 isl_pw_multi_aff_free(pma);
2200 return NULL;
2203 /* Extract the schedule domain of the given type from build->options
2204 * at the current depth.
2206 * In particular, find the subset of build->options that is of
2207 * the following form
2209 * schedule_domain -> type[depth]
2211 * and return the corresponding domain, after eliminating inner dimensions
2212 * and divs that depend on the current dimension.
2214 * Note that the domain of build->options has been reformulated
2215 * in terms of the internal build space in embed_options,
2216 * but the position is still that within the current code generation.
2218 __isl_give isl_set *isl_ast_build_get_option_domain(
2219 __isl_keep isl_ast_build *build, enum isl_ast_loop_type type)
2221 const char *name;
2222 isl_space *space;
2223 isl_map *option;
2224 isl_set *domain;
2225 int local_pos;
2227 if (!build)
2228 return NULL;
2230 name = option_str[type];
2231 local_pos = build->depth - build->outer_pos;
2233 space = isl_ast_build_get_space(build, 1);
2234 space = isl_space_from_domain(space);
2235 space = isl_space_add_dims(space, isl_dim_out, 1);
2236 space = isl_space_set_tuple_name(space, isl_dim_out, name);
2238 option = isl_union_map_extract_map(build->options, space);
2239 option = isl_map_fix_si(option, isl_dim_out, 0, local_pos);
2241 domain = isl_map_domain(option);
2242 domain = isl_ast_build_eliminate(build, domain);
2244 return domain;
2247 /* How does the user want the current schedule dimension to be generated?
2248 * These choices have been extracted from the schedule node
2249 * in extract_loop_types and stored in build->loop_type.
2250 * They have been updated to reflect any dimension insertion in
2251 * node_insert_dim.
2252 * Return isl_ast_domain_error on error.
2254 * If "isolated" is set, then we get the loop AST generation type
2255 * directly from the band node since node_insert_dim cannot have been
2256 * called on a band with the isolate option.
2258 enum isl_ast_loop_type isl_ast_build_get_loop_type(
2259 __isl_keep isl_ast_build *build, int isolated)
2261 int local_pos;
2262 isl_ctx *ctx;
2264 if (!build)
2265 return isl_ast_loop_error;
2266 ctx = isl_ast_build_get_ctx(build);
2267 if (!build->node)
2268 isl_die(ctx, isl_error_internal,
2269 "only works for schedule tree based AST generation",
2270 return isl_ast_loop_error);
2272 local_pos = build->depth - build->outer_pos;
2273 if (!isolated)
2274 return build->loop_type[local_pos];
2275 return isl_schedule_node_band_member_get_isolate_ast_loop_type(
2276 build->node, local_pos);
2279 /* Extract the isolated set from the isolate option, if any,
2280 * and store in the build.
2281 * If there is no isolate option, then the isolated set is
2282 * set to the empty set.
2284 * The isolate option is of the form
2286 * isolate[[outer bands] -> current_band]
2288 * We flatten this set and then map it back to the internal
2289 * schedule space.
2291 * If we have already extracted the isolated set
2292 * or if internal2input is no longer set, then we do not
2293 * need to do anything. In the latter case, we know
2294 * that the current band cannot have any isolate option.
2296 __isl_give isl_ast_build *isl_ast_build_extract_isolated(
2297 __isl_take isl_ast_build *build)
2299 isl_set *isolated;
2301 if (!build)
2302 return NULL;
2303 if (!build->internal2input)
2304 return build;
2305 if (build->isolated)
2306 return build;
2308 build = isl_ast_build_cow(build);
2309 if (!build)
2310 return NULL;
2312 isolated = isl_schedule_node_band_get_ast_isolate_option(build->node);
2313 isolated = isl_set_flatten(isolated);
2314 isolated = isl_set_preimage_multi_aff(isolated,
2315 isl_multi_aff_copy(build->internal2input));
2317 build->isolated = isolated;
2318 if (!build->isolated)
2319 return isl_ast_build_free(build);
2321 return build;
2324 /* Does "build" have a non-empty isolated set?
2326 * The caller is assumed to have called isl_ast_build_extract_isolated first.
2328 int isl_ast_build_has_isolated(__isl_keep isl_ast_build *build)
2330 int empty;
2332 if (!build)
2333 return -1;
2334 if (!build->internal2input)
2335 return 0;
2336 if (!build->isolated)
2337 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
2338 "isolated set not extracted yet", return -1);
2340 empty = isl_set_plain_is_empty(build->isolated);
2341 return empty < 0 ? -1 : !empty;
2344 /* Return a copy of the isolated set of "build".
2346 * The caller is assume to have called isl_ast_build_has_isolated first,
2347 * with this function returning true.
2348 * In particular, this function should not be called if we are no
2349 * longer keeping track of internal2input (and there therefore could
2350 * not possibly be any isolated set).
2352 __isl_give isl_set *isl_ast_build_get_isolated(__isl_keep isl_ast_build *build)
2354 if (!build)
2355 return NULL;
2356 if (!build->internal2input)
2357 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
2358 "build cannot have isolated set", return NULL);
2360 return isl_set_copy(build->isolated);
2363 /* Extract the separation class mapping at the current depth.
2365 * In particular, find and return the subset of build->options that is of
2366 * the following form
2368 * schedule_domain -> separation_class[[depth] -> [class]]
2370 * The caller is expected to eliminate inner dimensions from the domain.
2372 * Note that the domain of build->options has been reformulated
2373 * in terms of the internal build space in embed_options,
2374 * but the position is still that within the current code generation.
2376 __isl_give isl_map *isl_ast_build_get_separation_class(
2377 __isl_keep isl_ast_build *build)
2379 isl_ctx *ctx;
2380 isl_space *space_sep, *space;
2381 isl_map *res;
2382 int local_pos;
2384 if (!build)
2385 return NULL;
2387 local_pos = build->depth - build->outer_pos;
2388 ctx = isl_ast_build_get_ctx(build);
2389 space_sep = isl_space_alloc(ctx, 0, 1, 1);
2390 space_sep = isl_space_wrap(space_sep);
2391 space_sep = isl_space_set_tuple_name(space_sep, isl_dim_set,
2392 "separation_class");
2393 space = isl_ast_build_get_space(build, 1);
2394 space_sep = isl_space_align_params(space_sep, isl_space_copy(space));
2395 space = isl_space_map_from_domain_and_range(space, space_sep);
2397 res = isl_union_map_extract_map(build->options, space);
2398 res = isl_map_fix_si(res, isl_dim_out, 0, local_pos);
2399 res = isl_map_coalesce(res);
2401 return res;
2404 /* Eliminate dimensions inner to the current dimension.
2406 __isl_give isl_set *isl_ast_build_eliminate_inner(
2407 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2409 int dim;
2410 int depth;
2412 if (!build)
2413 return isl_set_free(set);
2415 dim = isl_set_dim(set, isl_dim_set);
2416 depth = build->depth;
2417 set = isl_set_detect_equalities(set);
2418 set = isl_set_eliminate(set, isl_dim_set, depth + 1, dim - (depth + 1));
2420 return set;
2423 /* Eliminate unknown divs and divs that depend on the current dimension.
2425 * Note that during the elimination of unknown divs, we may discover
2426 * an explicit representation of some other unknown divs, which may
2427 * depend on the current dimension. We therefore need to eliminate
2428 * unknown divs first.
2430 __isl_give isl_set *isl_ast_build_eliminate_divs(
2431 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2433 int depth;
2435 if (!build)
2436 return isl_set_free(set);
2438 set = isl_set_remove_unknown_divs(set);
2439 depth = build->depth;
2440 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, depth, 1);
2442 return set;
2445 /* Eliminate dimensions inner to the current dimension as well as
2446 * unknown divs and divs that depend on the current dimension.
2447 * The result then consists only of constraints that are independent
2448 * of the current dimension and upper and lower bounds on the current
2449 * dimension.
2451 __isl_give isl_set *isl_ast_build_eliminate(
2452 __isl_keep isl_ast_build *build, __isl_take isl_set *domain)
2454 domain = isl_ast_build_eliminate_inner(build, domain);
2455 domain = isl_ast_build_eliminate_divs(build, domain);
2456 return domain;
2459 /* Replace build->single_valued by "sv".
2461 __isl_give isl_ast_build *isl_ast_build_set_single_valued(
2462 __isl_take isl_ast_build *build, int sv)
2464 if (!build)
2465 return build;
2466 if (build->single_valued == sv)
2467 return build;
2468 build = isl_ast_build_cow(build);
2469 if (!build)
2470 return build;
2471 build->single_valued = sv;
2473 return build;