clean up isl_basic_set_variable_compression_with_id
[isl.git] / isl_ast_build.c
blob1c4cbd944bbcaa9b489586f2744c7b89e1380fb0
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, n_it;
368 build = isl_ast_build_cow(build);
369 if (!build)
370 goto error;
372 dim = isl_ast_build_dim(build, isl_dim_set);
373 n_it = isl_id_list_n_id(build->iterators);
374 if (dim < 0 || n_it < 0)
375 goto error;
376 if (n_it < dim)
377 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
378 "isl_ast_build in inconsistent state", goto error);
379 if (n_it > dim)
380 build->iterators = isl_id_list_drop(build->iterators,
381 dim, n_it - dim);
382 build->iterators = isl_id_list_concat(build->iterators, iterators);
383 if (!build->iterators)
384 return isl_ast_build_free(build);
386 return build;
387 error:
388 isl_id_list_free(iterators);
389 return isl_ast_build_free(build);
392 /* Set the "at_each_domain" callback of "build" to "fn".
394 __isl_give isl_ast_build *isl_ast_build_set_at_each_domain(
395 __isl_take isl_ast_build *build,
396 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
397 __isl_keep isl_ast_build *build, void *user), void *user)
399 build = isl_ast_build_cow(build);
401 if (!build)
402 return NULL;
404 build->at_each_domain = fn;
405 build->at_each_domain_user = user;
407 return build;
410 /* Set the "before_each_for" callback of "build" to "fn".
412 __isl_give isl_ast_build *isl_ast_build_set_before_each_for(
413 __isl_take isl_ast_build *build,
414 __isl_give isl_id *(*fn)(__isl_keep isl_ast_build *build,
415 void *user), void *user)
417 build = isl_ast_build_cow(build);
419 if (!build)
420 return NULL;
422 build->before_each_for = fn;
423 build->before_each_for_user = user;
425 return build;
428 /* Set the "after_each_for" callback of "build" to "fn".
430 __isl_give isl_ast_build *isl_ast_build_set_after_each_for(
431 __isl_take isl_ast_build *build,
432 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
433 __isl_keep isl_ast_build *build, void *user), void *user)
435 build = isl_ast_build_cow(build);
437 if (!build)
438 return NULL;
440 build->after_each_for = fn;
441 build->after_each_for_user = user;
443 return build;
446 /* Set the "before_each_mark" callback of "build" to "fn".
448 __isl_give isl_ast_build *isl_ast_build_set_before_each_mark(
449 __isl_take isl_ast_build *build,
450 isl_stat (*fn)(__isl_keep isl_id *mark, __isl_keep isl_ast_build *build,
451 void *user), void *user)
453 build = isl_ast_build_cow(build);
455 if (!build)
456 return NULL;
458 build->before_each_mark = fn;
459 build->before_each_mark_user = user;
461 return build;
464 /* Set the "after_each_mark" callback of "build" to "fn".
466 __isl_give isl_ast_build *isl_ast_build_set_after_each_mark(
467 __isl_take isl_ast_build *build,
468 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
469 __isl_keep isl_ast_build *build, void *user), void *user)
471 build = isl_ast_build_cow(build);
473 if (!build)
474 return NULL;
476 build->after_each_mark = fn;
477 build->after_each_mark_user = user;
479 return build;
482 /* Set the "create_leaf" callback of "build" to "fn".
484 __isl_give isl_ast_build *isl_ast_build_set_create_leaf(
485 __isl_take isl_ast_build *build,
486 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_build *build,
487 void *user), void *user)
489 build = isl_ast_build_cow(build);
491 if (!build)
492 return NULL;
494 build->create_leaf = fn;
495 build->create_leaf_user = user;
497 return build;
500 /* Clear all information that is specific to this code generation
501 * and that is (probably) not meaningful to any nested code generation.
503 __isl_give isl_ast_build *isl_ast_build_clear_local_info(
504 __isl_take isl_ast_build *build)
506 isl_space *space;
508 build = isl_ast_build_cow(build);
509 if (!build)
510 return NULL;
512 space = isl_union_map_get_space(build->options);
513 isl_union_map_free(build->options);
514 build->options = isl_union_map_empty(space);
516 build->at_each_domain = NULL;
517 build->at_each_domain_user = NULL;
518 build->before_each_for = NULL;
519 build->before_each_for_user = NULL;
520 build->after_each_for = NULL;
521 build->after_each_for_user = NULL;
522 build->before_each_mark = NULL;
523 build->before_each_mark_user = NULL;
524 build->after_each_mark = NULL;
525 build->after_each_mark_user = NULL;
526 build->create_leaf = NULL;
527 build->create_leaf_user = NULL;
529 if (!build->options)
530 return isl_ast_build_free(build);
532 return build;
535 /* Have any loops been eliminated?
536 * That is, do any of the original schedule dimensions have a fixed
537 * value that has been substituted?
539 static int any_eliminated(isl_ast_build *build)
541 int i;
543 for (i = 0; i < build->depth; ++i)
544 if (isl_ast_build_has_affine_value(build, i))
545 return 1;
547 return 0;
550 /* Clear build->schedule_map.
551 * This function should be called whenever anything that might affect
552 * the result of isl_ast_build_get_schedule_map_multi_aff changes.
553 * In particular, it should be called when the depth is changed or
554 * when an iterator is determined to have a fixed value.
556 static void isl_ast_build_reset_schedule_map(__isl_keep isl_ast_build *build)
558 if (!build)
559 return;
560 isl_multi_aff_free(build->schedule_map);
561 build->schedule_map = NULL;
564 /* Do we need a (non-trivial) schedule map?
565 * That is, is the internal schedule space different from
566 * the external schedule space?
568 * The internal and external schedule spaces are only the same
569 * if code has been generated for the entire schedule and if none
570 * of the loops have been eliminated.
572 isl_bool isl_ast_build_need_schedule_map(__isl_keep isl_ast_build *build)
574 isl_size dim;
576 dim = isl_ast_build_dim(build, isl_dim_set);
577 if (dim < 0)
578 return isl_bool_error;
579 return isl_bool_ok(build->depth != dim || any_eliminated(build));
582 /* Return a mapping from the internal schedule space to the external
583 * schedule space in the form of an isl_multi_aff.
584 * The internal schedule space originally corresponds to that of the
585 * input schedule. This may change during the code generation if
586 * if isl_ast_build_insert_dim is ever called.
587 * The external schedule space corresponds to the
588 * loops that have been generated.
590 * Currently, the only difference between the internal schedule domain
591 * and the external schedule domain is that some dimensions are projected
592 * out in the external schedule domain. In particular, the dimensions
593 * for which no code has been generated yet and the dimensions that correspond
594 * to eliminated loops.
596 * We cache a copy of the schedule_map in build->schedule_map.
597 * The cache is cleared through isl_ast_build_reset_schedule_map
598 * whenever anything changes that might affect the result of this function.
600 __isl_give isl_multi_aff *isl_ast_build_get_schedule_map_multi_aff(
601 __isl_keep isl_ast_build *build)
603 isl_bool needs_map;
604 isl_space *space;
605 isl_multi_aff *ma;
607 if (!build)
608 return NULL;
609 if (build->schedule_map)
610 return isl_multi_aff_copy(build->schedule_map);
611 needs_map = isl_ast_build_need_schedule_map(build);
612 if (needs_map < 0)
613 return NULL;
615 space = isl_ast_build_get_space(build, 1);
616 space = isl_space_map_from_set(space);
617 ma = isl_multi_aff_identity(space);
618 if (needs_map) {
619 int i;
620 isl_size dim = isl_ast_build_dim(build, isl_dim_set);
622 if (dim < 0)
623 ma = isl_multi_aff_free(ma);
624 ma = isl_multi_aff_drop_dims(ma, isl_dim_out,
625 build->depth, dim - build->depth);
626 for (i = build->depth - 1; i >= 0; --i)
627 if (isl_ast_build_has_affine_value(build, i))
628 ma = isl_multi_aff_drop_dims(ma,
629 isl_dim_out, i, 1);
632 build->schedule_map = ma;
633 return isl_multi_aff_copy(build->schedule_map);
636 /* Return a mapping from the internal schedule space to the external
637 * schedule space in the form of an isl_map.
639 __isl_give isl_map *isl_ast_build_get_schedule_map(
640 __isl_keep isl_ast_build *build)
642 isl_multi_aff *ma;
644 ma = isl_ast_build_get_schedule_map_multi_aff(build);
645 return isl_map_from_multi_aff(ma);
648 /* Return the position of the dimension in build->domain for which
649 * an AST node is currently being generated.
651 int isl_ast_build_get_depth(__isl_keep isl_ast_build *build)
653 return build ? build->depth : -1;
656 /* Prepare for generating code for the next level.
657 * In particular, increase the depth and reset any information
658 * that is local to the current depth.
660 __isl_give isl_ast_build *isl_ast_build_increase_depth(
661 __isl_take isl_ast_build *build)
663 build = isl_ast_build_cow(build);
664 if (!build)
665 return NULL;
666 build->depth++;
667 isl_ast_build_reset_schedule_map(build);
668 build->value = isl_pw_aff_free(build->value);
669 return build;
672 void isl_ast_build_dump(__isl_keep isl_ast_build *build)
674 if (!build)
675 return;
677 fprintf(stderr, "domain: ");
678 isl_set_dump(build->domain);
679 fprintf(stderr, "generated: ");
680 isl_set_dump(build->generated);
681 fprintf(stderr, "pending: ");
682 isl_set_dump(build->pending);
683 fprintf(stderr, "iterators: ");
684 isl_id_list_dump(build->iterators);
685 fprintf(stderr, "values: ");
686 isl_multi_aff_dump(build->values);
687 if (build->value) {
688 fprintf(stderr, "value: ");
689 isl_pw_aff_dump(build->value);
691 fprintf(stderr, "strides: ");
692 isl_vec_dump(build->strides);
693 fprintf(stderr, "offsets: ");
694 isl_multi_aff_dump(build->offsets);
695 fprintf(stderr, "internal2input: ");
696 isl_multi_aff_dump(build->internal2input);
699 /* Initialize "build" for AST construction in schedule space "space"
700 * in the case that build->domain is a parameter set.
702 * build->iterators is assumed to have been updated already.
704 static __isl_give isl_ast_build *isl_ast_build_init(
705 __isl_take isl_ast_build *build, __isl_take isl_space *space)
707 isl_set *set;
709 build = isl_ast_build_cow(build);
710 if (!build)
711 goto error;
713 set = isl_set_universe(isl_space_copy(space));
714 build->domain = isl_set_intersect_params(isl_set_copy(set),
715 build->domain);
716 build->pending = isl_set_intersect_params(isl_set_copy(set),
717 build->pending);
718 build->generated = isl_set_intersect_params(set, build->generated);
720 return isl_ast_build_init_derived(build, space);
721 error:
722 isl_ast_build_free(build);
723 isl_space_free(space);
724 return NULL;
727 /* Assign "aff" to *user and return -1, effectively extracting
728 * the first (and presumably only) affine expression in the isl_pw_aff
729 * on which this function is used.
731 static isl_stat extract_single_piece(__isl_take isl_set *set,
732 __isl_take isl_aff *aff, void *user)
734 isl_aff **p = user;
736 *p = aff;
737 isl_set_free(set);
739 return isl_stat_error;
742 /* Intersect "set" with the stride constraint of "build", if any.
744 static __isl_give isl_set *intersect_stride_constraint(__isl_take isl_set *set,
745 __isl_keep isl_ast_build *build)
747 isl_set *stride;
749 if (!build)
750 return isl_set_free(set);
751 if (!isl_ast_build_has_stride(build, build->depth))
752 return set;
754 stride = isl_ast_build_get_stride_constraint(build);
755 return isl_set_intersect(set, stride);
758 /* Check if the given bounds on the current dimension (together with
759 * the stride constraint, if any) imply that
760 * this current dimension attains only a single value (in terms of
761 * parameters and outer dimensions).
762 * If so, we record it in build->value.
763 * If, moreover, this value can be represented as a single affine expression,
764 * then we also update build->values, effectively marking the current
765 * dimension as "eliminated".
767 * When computing the gist of the fixed value that can be represented
768 * as a single affine expression, it is important to only take into
769 * account the domain constraints in the original AST build and
770 * not the domain of the affine expression itself.
771 * Otherwise, a [i/3] is changed into a i/3 because we know that i
772 * is a multiple of 3, but then we end up not expressing anywhere
773 * in the context that i is a multiple of 3.
775 static __isl_give isl_ast_build *update_values(
776 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
778 isl_bool sv;
779 isl_size n;
780 isl_pw_multi_aff *pma;
781 isl_aff *aff = NULL;
782 isl_map *it_map;
783 isl_set *set;
785 set = isl_set_from_basic_set(bounds);
786 set = isl_set_intersect(set, isl_set_copy(build->domain));
787 set = intersect_stride_constraint(set, build);
788 it_map = isl_ast_build_map_to_iterator(build, set);
790 sv = isl_map_is_single_valued(it_map);
791 if (sv < 0)
792 build = isl_ast_build_free(build);
793 if (!build || !sv) {
794 isl_map_free(it_map);
795 return build;
798 pma = isl_pw_multi_aff_from_map(it_map);
799 build->value = isl_pw_multi_aff_get_pw_aff(pma, 0);
800 build->value = isl_ast_build_compute_gist_pw_aff(build, build->value);
801 build->value = isl_pw_aff_coalesce(build->value);
802 isl_pw_multi_aff_free(pma);
804 n = isl_pw_aff_n_piece(build->value);
805 if (n < 0)
806 return isl_ast_build_free(build);
807 if (n != 1)
808 return build;
810 isl_pw_aff_foreach_piece(build->value, &extract_single_piece, &aff);
812 build->values = isl_multi_aff_set_aff(build->values, build->depth, aff);
813 if (!build->values)
814 return isl_ast_build_free(build);
815 isl_ast_build_reset_schedule_map(build);
816 return build;
819 /* Update the AST build based on the given loop bounds for
820 * the current dimension and the stride information available in the build.
822 * We first make sure that the bounds do not refer to any iterators
823 * that have already been eliminated.
824 * Then, we check if the bounds imply that the current iterator
825 * has a fixed value.
826 * If they do and if this fixed value can be expressed as a single
827 * affine expression, we eliminate the iterators from the bounds.
828 * Note that we cannot simply plug in this single value using
829 * isl_basic_set_preimage_multi_aff as the single value may only
830 * be defined on a subset of the domain. Plugging in the value
831 * would restrict the build domain to this subset, while this
832 * restriction may not be reflected in the generated code.
833 * Finally, we intersect build->domain with the updated bounds.
834 * We also add the stride constraint unless we have been able
835 * to find a fixed value expressed as a single affine expression.
837 * Note that the check for a fixed value in update_values requires
838 * us to intersect the bounds with the current build domain.
839 * When we intersect build->domain with the updated bounds in
840 * the final step, we make sure that these updated bounds have
841 * not been intersected with the old build->domain.
842 * Otherwise, we would indirectly intersect the build domain with itself,
843 * which can lead to inefficiencies, in particular if the build domain
844 * contains any unknown divs.
846 * The pending and generated sets are not updated by this function to
847 * match the updated domain.
848 * The caller still needs to call isl_ast_build_set_pending_generated.
850 __isl_give isl_ast_build *isl_ast_build_set_loop_bounds(
851 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
853 isl_set *set;
855 build = isl_ast_build_cow(build);
856 if (!build)
857 goto error;
859 build = update_values(build, isl_basic_set_copy(bounds));
860 if (!build)
861 goto error;
862 set = isl_set_from_basic_set(isl_basic_set_copy(bounds));
863 if (isl_ast_build_has_affine_value(build, build->depth)) {
864 set = isl_set_eliminate(set, isl_dim_set, build->depth, 1);
865 set = isl_set_compute_divs(set);
866 build->pending = isl_set_intersect(build->pending,
867 isl_set_copy(set));
868 build->domain = isl_set_intersect(build->domain, set);
869 } else {
870 build->domain = isl_set_intersect(build->domain, set);
871 build = isl_ast_build_include_stride(build);
872 if (!build)
873 goto error;
875 isl_basic_set_free(bounds);
877 if (!build->domain || !build->pending || !build->generated)
878 return isl_ast_build_free(build);
880 return build;
881 error:
882 isl_ast_build_free(build);
883 isl_basic_set_free(bounds);
884 return NULL;
887 /* Update the pending and generated sets of "build" according to "bounds".
888 * If the build has an affine value at the current depth,
889 * then isl_ast_build_set_loop_bounds has already set the pending set.
890 * Otherwise, do it here.
892 __isl_give isl_ast_build *isl_ast_build_set_pending_generated(
893 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
895 isl_basic_set *generated, *pending;
897 if (!build)
898 goto error;
900 if (isl_ast_build_has_affine_value(build, build->depth)) {
901 isl_basic_set_free(bounds);
902 return build;
905 build = isl_ast_build_cow(build);
906 if (!build)
907 goto error;
909 pending = isl_basic_set_copy(bounds);
910 pending = isl_basic_set_drop_constraints_involving_dims(pending,
911 isl_dim_set, build->depth, 1);
912 build->pending = isl_set_intersect(build->pending,
913 isl_set_from_basic_set(pending));
914 generated = bounds;
915 generated = isl_basic_set_drop_constraints_not_involving_dims(
916 generated, isl_dim_set, build->depth, 1);
917 build->generated = isl_set_intersect(build->generated,
918 isl_set_from_basic_set(generated));
920 if (!build->pending || !build->generated)
921 return isl_ast_build_free(build);
923 return build;
924 error:
925 isl_ast_build_free(build);
926 isl_basic_set_free(bounds);
927 return NULL;
930 /* Intersect build->domain with "set", where "set" is specified
931 * in terms of the internal schedule domain.
933 static __isl_give isl_ast_build *isl_ast_build_restrict_internal(
934 __isl_take isl_ast_build *build, __isl_take isl_set *set)
936 build = isl_ast_build_cow(build);
937 if (!build)
938 goto error;
940 set = isl_set_compute_divs(set);
941 build->domain = isl_set_intersect(build->domain, set);
942 build->domain = isl_set_coalesce(build->domain);
944 if (!build->domain)
945 return isl_ast_build_free(build);
947 return build;
948 error:
949 isl_ast_build_free(build);
950 isl_set_free(set);
951 return NULL;
954 /* Intersect build->generated and build->domain with "set",
955 * where "set" is specified in terms of the internal schedule domain.
957 __isl_give isl_ast_build *isl_ast_build_restrict_generated(
958 __isl_take isl_ast_build *build, __isl_take isl_set *set)
960 set = isl_set_compute_divs(set);
961 build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
962 build = isl_ast_build_cow(build);
963 if (!build)
964 goto error;
966 build->generated = isl_set_intersect(build->generated, set);
967 build->generated = isl_set_coalesce(build->generated);
969 if (!build->generated)
970 return isl_ast_build_free(build);
972 return build;
973 error:
974 isl_ast_build_free(build);
975 isl_set_free(set);
976 return NULL;
979 /* Replace the set of pending constraints by "guard", which is then
980 * no longer considered as pending.
981 * That is, add "guard" to the generated constraints and clear all pending
982 * constraints, making the domain equal to the generated constraints.
984 __isl_give isl_ast_build *isl_ast_build_replace_pending_by_guard(
985 __isl_take isl_ast_build *build, __isl_take isl_set *guard)
987 build = isl_ast_build_restrict_generated(build, guard);
988 build = isl_ast_build_cow(build);
989 if (!build)
990 return NULL;
992 isl_set_free(build->domain);
993 build->domain = isl_set_copy(build->generated);
994 isl_set_free(build->pending);
995 build->pending = isl_set_universe(isl_set_get_space(build->domain));
997 if (!build->pending)
998 return isl_ast_build_free(build);
1000 return build;
1003 /* Intersect build->domain with "set", where "set" is specified
1004 * in terms of the external schedule domain.
1006 __isl_give isl_ast_build *isl_ast_build_restrict(
1007 __isl_take isl_ast_build *build, __isl_take isl_set *set)
1009 isl_bool needs_map;
1011 if (isl_set_is_params(set))
1012 return isl_ast_build_restrict_generated(build, set);
1014 needs_map = isl_ast_build_need_schedule_map(build);
1015 if (needs_map < 0)
1016 goto error;
1017 if (needs_map) {
1018 isl_multi_aff *ma;
1019 ma = isl_ast_build_get_schedule_map_multi_aff(build);
1020 set = isl_set_preimage_multi_aff(set, ma);
1022 return isl_ast_build_restrict_generated(build, set);
1023 error:
1024 isl_ast_build_free(build);
1025 isl_set_free(set);
1026 return NULL;
1029 /* Replace build->executed by "executed".
1031 __isl_give isl_ast_build *isl_ast_build_set_executed(
1032 __isl_take isl_ast_build *build, __isl_take isl_union_map *executed)
1034 build = isl_ast_build_cow(build);
1035 if (!build)
1036 goto error;
1038 isl_union_map_free(build->executed);
1039 build->executed = executed;
1041 return build;
1042 error:
1043 isl_ast_build_free(build);
1044 isl_union_map_free(executed);
1045 return NULL;
1048 /* Does "build" point to a band node?
1049 * That is, are we currently handling a band node inside a schedule tree?
1051 int isl_ast_build_has_schedule_node(__isl_keep isl_ast_build *build)
1053 if (!build)
1054 return -1;
1055 return build->node != NULL;
1058 /* Return a copy of the band node that "build" refers to.
1060 __isl_give isl_schedule_node *isl_ast_build_get_schedule_node(
1061 __isl_keep isl_ast_build *build)
1063 if (!build)
1064 return NULL;
1065 return isl_schedule_node_copy(build->node);
1068 /* Extract the loop AST generation types for the members of build->node
1069 * and store them in build->loop_type.
1071 static __isl_give isl_ast_build *extract_loop_types(
1072 __isl_take isl_ast_build *build)
1074 int i;
1075 isl_size n;
1076 isl_ctx *ctx;
1077 isl_schedule_node *node;
1079 if (!build)
1080 return NULL;
1081 n = isl_schedule_node_band_n_member(build->node);
1082 if (n < 0)
1083 return isl_ast_build_free(build);
1084 ctx = isl_ast_build_get_ctx(build);
1085 if (!build->node)
1086 isl_die(ctx, isl_error_internal, "missing AST node",
1087 return isl_ast_build_free(build));
1089 free(build->loop_type);
1090 build->n = n;
1091 build->loop_type = isl_alloc_array(ctx,
1092 enum isl_ast_loop_type, build->n);
1093 if (build->n && !build->loop_type)
1094 return isl_ast_build_free(build);
1095 node = build->node;
1096 for (i = 0; i < build->n; ++i)
1097 build->loop_type[i] =
1098 isl_schedule_node_band_member_get_ast_loop_type(node, i);
1100 return build;
1103 /* Replace the band node that "build" refers to by "node" and
1104 * extract the corresponding loop AST generation types.
1106 __isl_give isl_ast_build *isl_ast_build_set_schedule_node(
1107 __isl_take isl_ast_build *build,
1108 __isl_take isl_schedule_node *node)
1110 build = isl_ast_build_cow(build);
1111 if (!build || !node)
1112 goto error;
1114 isl_schedule_node_free(build->node);
1115 build->node = node;
1117 build = extract_loop_types(build);
1119 return build;
1120 error:
1121 isl_ast_build_free(build);
1122 isl_schedule_node_free(node);
1123 return NULL;
1126 /* Remove any reference to a band node from "build".
1128 __isl_give isl_ast_build *isl_ast_build_reset_schedule_node(
1129 __isl_take isl_ast_build *build)
1131 build = isl_ast_build_cow(build);
1132 if (!build)
1133 return NULL;
1135 isl_schedule_node_free(build->node);
1136 build->node = NULL;
1138 return build;
1141 /* Return a copy of the current schedule domain.
1143 __isl_give isl_set *isl_ast_build_get_domain(__isl_keep isl_ast_build *build)
1145 return build ? isl_set_copy(build->domain) : NULL;
1148 /* Return a copy of the set of pending constraints.
1150 __isl_give isl_set *isl_ast_build_get_pending(
1151 __isl_keep isl_ast_build *build)
1153 return build ? isl_set_copy(build->pending) : NULL;
1156 /* Return a copy of the set of generated constraints.
1158 __isl_give isl_set *isl_ast_build_get_generated(
1159 __isl_keep isl_ast_build *build)
1161 return build ? isl_set_copy(build->generated) : NULL;
1164 /* Return a copy of the map from the internal schedule domain
1165 * to the original input schedule domain.
1167 __isl_give isl_multi_aff *isl_ast_build_get_internal2input(
1168 __isl_keep isl_ast_build *build)
1170 return build ? isl_multi_aff_copy(build->internal2input) : NULL;
1173 /* Return the number of variables of the given type
1174 * in the (internal) schedule space.
1176 isl_size isl_ast_build_dim(__isl_keep isl_ast_build *build,
1177 enum isl_dim_type type)
1179 if (!build)
1180 return isl_size_error;
1181 return isl_set_dim(build->domain, type);
1184 /* Return the (schedule) space of "build".
1186 * If "internal" is set, then this space is the space of the internal
1187 * representation of the entire schedule, including those parts for
1188 * which no code has been generated yet.
1190 * If "internal" is not set, then this space is the external representation
1191 * of the loops generated so far.
1193 __isl_give isl_space *isl_ast_build_get_space(__isl_keep isl_ast_build *build,
1194 int internal)
1196 int i;
1197 isl_size dim;
1198 isl_bool needs_map;
1199 isl_space *space;
1201 if (!build)
1202 return NULL;
1204 space = isl_set_get_space(build->domain);
1205 if (internal)
1206 return space;
1208 needs_map = isl_ast_build_need_schedule_map(build);
1209 if (needs_map < 0)
1210 return isl_space_free(space);
1211 if (!needs_map)
1212 return space;
1214 dim = isl_ast_build_dim(build, isl_dim_set);
1215 if (dim < 0)
1216 return isl_space_free(space);
1217 space = isl_space_drop_dims(space, isl_dim_set,
1218 build->depth, dim - build->depth);
1219 for (i = build->depth - 1; i >= 0; --i) {
1220 isl_bool affine = isl_ast_build_has_affine_value(build, i);
1222 if (affine < 0)
1223 return isl_space_free(space);
1224 if (affine)
1225 space = isl_space_drop_dims(space, isl_dim_set, i, 1);
1228 return space;
1231 /* Return the external representation of the schedule space of "build",
1232 * i.e., a space with a dimension for each loop generated so far,
1233 * with the names of the dimensions set to the loop iterators.
1235 __isl_give isl_space *isl_ast_build_get_schedule_space(
1236 __isl_keep isl_ast_build *build)
1238 isl_space *space;
1239 int i, skip;
1241 if (!build)
1242 return NULL;
1244 space = isl_ast_build_get_space(build, 0);
1246 skip = 0;
1247 for (i = 0; i < build->depth; ++i) {
1248 isl_id *id;
1250 if (isl_ast_build_has_affine_value(build, i)) {
1251 skip++;
1252 continue;
1255 id = isl_ast_build_get_iterator_id(build, i);
1256 space = isl_space_set_dim_id(space, isl_dim_set, i - skip, id);
1259 return space;
1262 /* Return the current schedule, as stored in build->executed, in terms
1263 * of the external schedule domain.
1265 __isl_give isl_union_map *isl_ast_build_get_schedule(
1266 __isl_keep isl_ast_build *build)
1268 isl_bool needs_map;
1269 isl_union_map *executed;
1270 isl_union_map *schedule;
1272 needs_map = isl_ast_build_need_schedule_map(build);
1273 if (needs_map < 0)
1274 return NULL;
1276 executed = isl_union_map_copy(build->executed);
1277 if (needs_map) {
1278 isl_map *proj = isl_ast_build_get_schedule_map(build);
1279 executed = isl_union_map_apply_domain(executed,
1280 isl_union_map_from_map(proj));
1282 schedule = isl_union_map_reverse(executed);
1284 return schedule;
1287 /* Return the iterator attached to the internal schedule dimension "pos".
1289 __isl_give isl_id *isl_ast_build_get_iterator_id(
1290 __isl_keep isl_ast_build *build, int pos)
1292 if (!build)
1293 return NULL;
1295 return isl_id_list_get_id(build->iterators, pos);
1298 /* Set the stride and offset of the current dimension to the given
1299 * value and expression.
1301 static __isl_give isl_ast_build *set_stride(__isl_take isl_ast_build *build,
1302 __isl_take isl_val *stride, __isl_take isl_aff *offset)
1304 int pos;
1306 build = isl_ast_build_cow(build);
1307 if (!build || !stride || !offset)
1308 goto error;
1310 pos = build->depth;
1312 build->strides = isl_vec_set_element_val(build->strides, pos, stride);
1313 build->offsets = isl_multi_aff_set_aff(build->offsets, pos, offset);
1314 if (!build->strides || !build->offsets)
1315 return isl_ast_build_free(build);
1317 return build;
1318 error:
1319 isl_val_free(stride);
1320 isl_aff_free(offset);
1321 return isl_ast_build_free(build);
1324 /* Return a set expressing the stride constraint at the current depth.
1326 * In particular, if the current iterator (i) is known to attain values
1328 * f + s a
1330 * where f is the offset and s is the stride, then the returned set
1331 * expresses the constraint
1333 * (f - i) mod s = 0
1335 __isl_give isl_set *isl_ast_build_get_stride_constraint(
1336 __isl_keep isl_ast_build *build)
1338 isl_aff *aff;
1339 isl_set *set;
1340 isl_val *stride;
1341 int pos;
1343 if (!build)
1344 return NULL;
1346 pos = build->depth;
1348 if (!isl_ast_build_has_stride(build, pos))
1349 return isl_set_universe(isl_ast_build_get_space(build, 1));
1351 stride = isl_ast_build_get_stride(build, pos);
1352 aff = isl_ast_build_get_offset(build, pos);
1353 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, -1);
1354 aff = isl_aff_mod_val(aff, stride);
1355 set = isl_set_from_basic_set(isl_aff_zero_basic_set(aff));
1357 return set;
1360 /* Return the expansion implied by the stride and offset at the current
1361 * depth.
1363 * That is, return the mapping
1365 * [i_0, ..., i_{d-1}, i_d, i_{d+1}, ...]
1366 * -> [i_0, ..., i_{d-1}, s * i_d + offset(i), i_{d+1}, ...]
1368 * where s is the stride at the current depth d and offset(i) is
1369 * the corresponding offset.
1371 __isl_give isl_multi_aff *isl_ast_build_get_stride_expansion(
1372 __isl_keep isl_ast_build *build)
1374 isl_space *space;
1375 isl_multi_aff *ma;
1376 int pos;
1377 isl_aff *aff, *offset;
1378 isl_val *stride;
1380 if (!build)
1381 return NULL;
1383 pos = isl_ast_build_get_depth(build);
1384 space = isl_ast_build_get_space(build, 1);
1385 space = isl_space_map_from_set(space);
1386 ma = isl_multi_aff_identity(space);
1388 if (!isl_ast_build_has_stride(build, pos))
1389 return ma;
1391 offset = isl_ast_build_get_offset(build, pos);
1392 stride = isl_ast_build_get_stride(build, pos);
1393 aff = isl_multi_aff_get_aff(ma, pos);
1394 aff = isl_aff_scale_val(aff, stride);
1395 aff = isl_aff_add(aff, offset);
1396 ma = isl_multi_aff_set_aff(ma, pos, aff);
1398 return ma;
1401 /* Add constraints corresponding to any previously detected
1402 * stride on the current dimension to build->domain.
1404 __isl_give isl_ast_build *isl_ast_build_include_stride(
1405 __isl_take isl_ast_build *build)
1407 isl_set *set;
1409 if (!build)
1410 return NULL;
1411 if (!isl_ast_build_has_stride(build, build->depth))
1412 return build;
1413 build = isl_ast_build_cow(build);
1414 if (!build)
1415 return NULL;
1417 set = isl_ast_build_get_stride_constraint(build);
1419 build->domain = isl_set_intersect(build->domain, isl_set_copy(set));
1420 build->generated = isl_set_intersect(build->generated, set);
1421 if (!build->domain || !build->generated)
1422 return isl_ast_build_free(build);
1424 return build;
1427 /* Check if the constraints in "set" imply any stride on the current
1428 * dimension and, if so, record the stride information in "build"
1429 * and return the updated "build".
1431 * We assume that inner dimensions have been eliminated from "set"
1432 * by the caller. This is needed because the common stride
1433 * may be imposed by different inner dimensions on different parts of
1434 * the domain.
1435 * The assumption ensures that the lower bound does not depend
1436 * on inner dimensions.
1438 __isl_give isl_ast_build *isl_ast_build_detect_strides(
1439 __isl_take isl_ast_build *build, __isl_take isl_set *set)
1441 int pos;
1442 isl_bool no_stride;
1443 isl_val *stride;
1444 isl_aff *offset;
1445 isl_stride_info *si;
1447 if (!build)
1448 goto error;
1450 pos = isl_ast_build_get_depth(build);
1451 si = isl_set_get_stride_info(set, pos);
1452 stride = isl_stride_info_get_stride(si);
1453 offset = isl_stride_info_get_offset(si);
1454 isl_stride_info_free(si);
1455 isl_set_free(set);
1457 no_stride = isl_val_is_one(stride);
1458 if (no_stride >= 0 && !no_stride)
1459 return set_stride(build, stride, offset);
1460 isl_val_free(stride);
1461 isl_aff_free(offset);
1462 if (no_stride < 0)
1463 return isl_ast_build_free(build);
1464 return build;
1465 error:
1466 isl_set_free(set);
1467 return NULL;
1470 struct isl_ast_build_involves_data {
1471 int depth;
1472 int involves;
1475 /* Check if "map" involves the input dimension data->depth.
1477 static isl_stat involves_depth(__isl_take isl_map *map, void *user)
1479 struct isl_ast_build_involves_data *data = user;
1481 data->involves = isl_map_involves_dims(map, isl_dim_in, data->depth, 1);
1482 isl_map_free(map);
1484 if (data->involves < 0 || data->involves)
1485 return isl_stat_error;
1486 return isl_stat_ok;
1489 /* Do any options depend on the value of the dimension at the current depth?
1491 int isl_ast_build_options_involve_depth(__isl_keep isl_ast_build *build)
1493 struct isl_ast_build_involves_data data;
1495 if (!build)
1496 return -1;
1498 data.depth = build->depth;
1499 data.involves = 0;
1501 if (isl_union_map_foreach_map(build->options,
1502 &involves_depth, &data) < 0) {
1503 if (data.involves < 0 || !data.involves)
1504 return -1;
1507 return data.involves;
1510 /* Construct the map
1512 * { [i] -> [i] : i < pos; [i] -> [i + 1] : i >= pos }
1514 * with "space" the parameter space of the constructed map.
1516 static __isl_give isl_map *construct_insertion_map(__isl_take isl_space *space,
1517 int pos)
1519 isl_constraint *c;
1520 isl_basic_map *bmap1, *bmap2;
1522 space = isl_space_set_from_params(space);
1523 space = isl_space_add_dims(space, isl_dim_set, 1);
1524 space = isl_space_map_from_set(space);
1525 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
1526 c = isl_constraint_set_coefficient_si(c, isl_dim_in, 0, 1);
1527 c = isl_constraint_set_coefficient_si(c, isl_dim_out, 0, -1);
1528 bmap1 = isl_basic_map_from_constraint(isl_constraint_copy(c));
1529 c = isl_constraint_set_constant_si(c, 1);
1530 bmap2 = isl_basic_map_from_constraint(c);
1532 bmap1 = isl_basic_map_upper_bound_si(bmap1, isl_dim_in, 0, pos - 1);
1533 bmap2 = isl_basic_map_lower_bound_si(bmap2, isl_dim_in, 0, pos);
1535 return isl_basic_map_union(bmap1, bmap2);
1538 static const char *option_str[] = {
1539 [isl_ast_loop_atomic] = "atomic",
1540 [isl_ast_loop_unroll] = "unroll",
1541 [isl_ast_loop_separate] = "separate"
1544 /* Update the "options" to reflect the insertion of a dimension
1545 * at position "pos" in the schedule domain space.
1546 * "space" is the original domain space before the insertion and
1547 * may be named and/or structured.
1549 * The (relevant) input options all have "space" as domain, which
1550 * has to be mapped to the extended space.
1551 * The values of the ranges also refer to the schedule domain positions
1552 * and they therefore also need to be adjusted. In particular, values
1553 * smaller than pos do not need to change, while values greater than or
1554 * equal to pos need to be incremented.
1555 * That is, we need to apply the following map.
1557 * { atomic[i] -> atomic[i] : i < pos; [i] -> [i + 1] : i >= pos;
1558 * unroll[i] -> unroll[i] : i < pos; [i] -> [i + 1] : i >= pos;
1559 * separate[i] -> separate[i] : i < pos; [i] -> [i + 1] : i >= pos;
1560 * separation_class[[i] -> [c]]
1561 * -> separation_class[[i] -> [c]] : i < pos;
1562 * separation_class[[i] -> [c]]
1563 * -> separation_class[[i + 1] -> [c]] : i >= pos }
1565 static __isl_give isl_union_map *options_insert_dim(
1566 __isl_take isl_union_map *options, __isl_take isl_space *space, int pos)
1568 isl_map *map;
1569 isl_union_map *insertion;
1570 enum isl_ast_loop_type type;
1571 const char *name = "separation_class";
1573 space = isl_space_map_from_set(space);
1574 map = isl_map_identity(space);
1575 map = isl_map_insert_dims(map, isl_dim_out, pos, 1);
1576 options = isl_union_map_apply_domain(options,
1577 isl_union_map_from_map(map));
1579 if (!options)
1580 return NULL;
1582 map = construct_insertion_map(isl_union_map_get_space(options), pos);
1584 insertion = isl_union_map_empty(isl_union_map_get_space(options));
1586 for (type = isl_ast_loop_atomic;
1587 type <= isl_ast_loop_separate; ++type) {
1588 isl_map *map_type = isl_map_copy(map);
1589 const char *name = option_str[type];
1590 map_type = isl_map_set_tuple_name(map_type, isl_dim_in, name);
1591 map_type = isl_map_set_tuple_name(map_type, isl_dim_out, name);
1592 insertion = isl_union_map_add_map(insertion, map_type);
1595 map = isl_map_product(map, isl_map_identity(isl_map_get_space(map)));
1596 map = isl_map_set_tuple_name(map, isl_dim_in, name);
1597 map = isl_map_set_tuple_name(map, isl_dim_out, name);
1598 insertion = isl_union_map_add_map(insertion, map);
1600 options = isl_union_map_apply_range(options, insertion);
1602 return options;
1605 /* If we are generating an AST from a schedule tree (build->node is set),
1606 * then update the loop AST generation types
1607 * to reflect the insertion of a dimension at (global) position "pos"
1608 * in the schedule domain space.
1609 * We do not need to adjust any isolate option since we would not be inserting
1610 * any dimensions if there were any isolate option.
1612 static __isl_give isl_ast_build *node_insert_dim(
1613 __isl_take isl_ast_build *build, int pos)
1615 int i;
1616 int local_pos;
1617 enum isl_ast_loop_type *loop_type;
1618 isl_ctx *ctx;
1620 build = isl_ast_build_cow(build);
1621 if (!build)
1622 return NULL;
1623 if (!build->node)
1624 return build;
1626 ctx = isl_ast_build_get_ctx(build);
1627 local_pos = pos - build->outer_pos;
1628 loop_type = isl_realloc_array(ctx, build->loop_type,
1629 enum isl_ast_loop_type, build->n + 1);
1630 if (!loop_type)
1631 return isl_ast_build_free(build);
1632 build->loop_type = loop_type;
1633 for (i = build->n - 1; i >= local_pos; --i)
1634 loop_type[i + 1] = loop_type[i];
1635 loop_type[local_pos] = isl_ast_loop_default;
1636 build->n++;
1638 return build;
1641 /* Insert a single dimension in the schedule domain at position "pos".
1642 * The new dimension is given an isl_id with the empty string as name.
1644 * The main difficulty is updating build->options to reflect the
1645 * extra dimension. This is handled in options_insert_dim.
1647 * Note that because of the dimension manipulations, the resulting
1648 * schedule domain space will always be unnamed and unstructured.
1649 * However, the original schedule domain space may be named and/or
1650 * structured, so we have to take this possibility into account
1651 * while performing the transformations.
1653 * Since the inserted schedule dimension is used by the caller
1654 * to differentiate between different domain spaces, there is
1655 * no longer a uniform mapping from the internal schedule space
1656 * to the input schedule space. The internal2input mapping is
1657 * therefore removed.
1659 __isl_give isl_ast_build *isl_ast_build_insert_dim(
1660 __isl_take isl_ast_build *build, int pos)
1662 isl_ctx *ctx;
1663 isl_space *space, *ma_space;
1664 isl_id *id;
1665 isl_multi_aff *ma;
1667 build = isl_ast_build_cow(build);
1668 if (!build)
1669 return NULL;
1671 ctx = isl_ast_build_get_ctx(build);
1672 id = isl_id_alloc(ctx, "", NULL);
1673 if (!build->node)
1674 space = isl_ast_build_get_space(build, 1);
1675 build->iterators = isl_id_list_insert(build->iterators, pos, id);
1676 build->domain = isl_set_insert_dims(build->domain,
1677 isl_dim_set, pos, 1);
1678 build->generated = isl_set_insert_dims(build->generated,
1679 isl_dim_set, pos, 1);
1680 build->pending = isl_set_insert_dims(build->pending,
1681 isl_dim_set, pos, 1);
1682 build->strides = isl_vec_insert_els(build->strides, pos, 1);
1683 build->strides = isl_vec_set_element_si(build->strides, pos, 1);
1684 ma_space = isl_space_params(isl_multi_aff_get_space(build->offsets));
1685 ma_space = isl_space_set_from_params(ma_space);
1686 ma_space = isl_space_add_dims(ma_space, isl_dim_set, 1);
1687 ma_space = isl_space_map_from_set(ma_space);
1688 ma = isl_multi_aff_zero(isl_space_copy(ma_space));
1689 build->offsets = isl_multi_aff_splice(build->offsets, pos, pos, ma);
1690 ma = isl_multi_aff_identity(ma_space);
1691 build->values = isl_multi_aff_splice(build->values, pos, pos, ma);
1692 if (!build->node)
1693 build->options = options_insert_dim(build->options, space, pos);
1694 build->internal2input = isl_multi_aff_free(build->internal2input);
1696 if (!build->iterators || !build->domain || !build->generated ||
1697 !build->pending || !build->values ||
1698 !build->strides || !build->offsets || !build->options)
1699 return isl_ast_build_free(build);
1701 build = node_insert_dim(build, pos);
1703 return build;
1706 /* Scale down the current dimension by a factor of "m".
1707 * "umap" is an isl_union_map that implements the scaling down.
1708 * That is, it is of the form
1710 * { [.... i ....] -> [.... i' ....] : i = m i' }
1712 * This function is called right after the strides have been
1713 * detected, but before any constraints on the current dimension
1714 * have been included in build->domain.
1715 * We therefore only need to update stride, offset, the options and
1716 * the mapping from internal schedule space to the original schedule
1717 * space, if we are still keeping track of such a mapping.
1718 * The latter mapping is updated by plugging in
1719 * { [... i ...] -> [... m i ... ] }.
1721 __isl_give isl_ast_build *isl_ast_build_scale_down(
1722 __isl_take isl_ast_build *build, __isl_take isl_val *m,
1723 __isl_take isl_union_map *umap)
1725 isl_aff *aff;
1726 isl_val *v;
1727 int depth;
1729 build = isl_ast_build_cow(build);
1730 if (!build || !umap || !m)
1731 goto error;
1733 depth = build->depth;
1735 if (build->internal2input) {
1736 isl_space *space;
1737 isl_multi_aff *ma;
1738 isl_aff *aff;
1740 space = isl_multi_aff_get_space(build->internal2input);
1741 space = isl_space_map_from_set(isl_space_domain(space));
1742 ma = isl_multi_aff_identity(space);
1743 aff = isl_multi_aff_get_aff(ma, depth);
1744 aff = isl_aff_scale_val(aff, isl_val_copy(m));
1745 ma = isl_multi_aff_set_aff(ma, depth, aff);
1746 build->internal2input =
1747 isl_multi_aff_pullback_multi_aff(build->internal2input, ma);
1748 if (!build->internal2input)
1749 goto error;
1752 v = isl_vec_get_element_val(build->strides, depth);
1753 v = isl_val_div(v, isl_val_copy(m));
1754 build->strides = isl_vec_set_element_val(build->strides, depth, v);
1756 aff = isl_multi_aff_get_aff(build->offsets, depth);
1757 aff = isl_aff_scale_down_val(aff, m);
1758 build->offsets = isl_multi_aff_set_aff(build->offsets, depth, aff);
1759 build->options = isl_union_map_apply_domain(build->options, umap);
1760 if (!build->strides || !build->offsets || !build->options)
1761 return isl_ast_build_free(build);
1763 return build;
1764 error:
1765 isl_val_free(m);
1766 isl_union_map_free(umap);
1767 return isl_ast_build_free(build);
1770 /* Return a list of "n" isl_ids called "c%d", with "%d" starting at "first".
1771 * If an isl_id with such a name already appears among the parameters
1772 * in build->domain, then adjust the name to "c%d_%d".
1774 static __isl_give isl_id_list *generate_names(isl_ctx *ctx, int n, int first,
1775 __isl_keep isl_ast_build *build)
1777 int i;
1778 isl_id_list *names;
1780 names = isl_id_list_alloc(ctx, n);
1781 for (i = 0; i < n; ++i) {
1782 isl_id *id;
1784 id = generate_name(ctx, first + i, build);
1785 names = isl_id_list_add(names, id);
1788 return names;
1791 /* Embed "options" into the given isl_ast_build space.
1793 * This function is called from within a nested call to
1794 * isl_ast_build_node_from_schedule_map.
1795 * "options" refers to the additional schedule,
1796 * while space refers to both the space of the outer isl_ast_build and
1797 * that of the additional schedule.
1798 * Specifically, space is of the form
1800 * [I -> S]
1802 * while options lives in the space(s)
1804 * S -> *
1806 * We compute
1808 * [I -> S] -> S
1810 * and compose this with options, to obtain the new options
1811 * living in the space(s)
1813 * [I -> S] -> *
1815 static __isl_give isl_union_map *embed_options(
1816 __isl_take isl_union_map *options, __isl_take isl_space *space)
1818 isl_map *map;
1820 map = isl_map_universe(isl_space_unwrap(space));
1821 map = isl_map_range_map(map);
1823 options = isl_union_map_apply_range(
1824 isl_union_map_from_map(map), options);
1826 return options;
1829 /* Update "build" for use in a (possibly nested) code generation. That is,
1830 * extend "build" from an AST build on some domain O to an AST build
1831 * on domain [O -> S], with S corresponding to "space".
1832 * If the original domain is a parameter domain, then the new domain is
1833 * simply S.
1834 * "iterators" is a list of iterators for S, but the number of elements
1835 * may be smaller or greater than the number of set dimensions of S.
1836 * If "keep_iterators" is set, then any extra ids in build->iterators
1837 * are reused for S. Otherwise, these extra ids are dropped.
1839 * We first update build->outer_pos to the current depth.
1840 * This depth is zero in case this is the outermost code generation.
1842 * We then add additional ids such that the number of iterators is at least
1843 * equal to the dimension of the new build domain.
1845 * If the original domain is parametric, then we are constructing
1846 * an isl_ast_build for the outer code generation and we pass control
1847 * to isl_ast_build_init.
1849 * Otherwise, we adjust the fields of "build" to include "space".
1851 __isl_give isl_ast_build *isl_ast_build_product(
1852 __isl_take isl_ast_build *build, __isl_take isl_space *space)
1854 isl_ctx *ctx;
1855 isl_vec *strides;
1856 isl_set *set;
1857 isl_multi_aff *embedding;
1858 isl_size dim, space_dim, n_it;
1860 build = isl_ast_build_cow(build);
1861 if (!build)
1862 goto error;
1864 build->outer_pos = build->depth;
1866 ctx = isl_ast_build_get_ctx(build);
1867 dim = isl_ast_build_dim(build, isl_dim_set);
1868 space_dim = isl_space_dim(space, isl_dim_set);
1869 n_it = isl_id_list_n_id(build->iterators);
1870 if (dim < 0 || space_dim < 0 || n_it < 0)
1871 goto error;
1872 dim += space_dim;
1873 if (n_it < dim) {
1874 isl_id_list *l;
1875 l = generate_names(ctx, dim - n_it, n_it, build);
1876 build->iterators = isl_id_list_concat(build->iterators, l);
1879 if (isl_set_is_params(build->domain))
1880 return isl_ast_build_init(build, space);
1882 set = isl_set_universe(isl_space_copy(space));
1883 build->domain = isl_set_product(build->domain, isl_set_copy(set));
1884 build->pending = isl_set_product(build->pending, isl_set_copy(set));
1885 build->generated = isl_set_product(build->generated, set);
1887 strides = isl_vec_alloc(ctx, space_dim);
1888 strides = isl_vec_set_si(strides, 1);
1889 build->strides = isl_vec_concat(build->strides, strides);
1891 space = isl_space_map_from_set(space);
1892 build->offsets = isl_multi_aff_align_params(build->offsets,
1893 isl_space_copy(space));
1894 build->offsets = isl_multi_aff_product(build->offsets,
1895 isl_multi_aff_zero(isl_space_copy(space)));
1896 build->values = isl_multi_aff_align_params(build->values,
1897 isl_space_copy(space));
1898 embedding = isl_multi_aff_identity(space);
1899 build->values = isl_multi_aff_product(build->values,
1900 isl_multi_aff_copy(embedding));
1901 if (build->internal2input) {
1902 build->internal2input =
1903 isl_multi_aff_product(build->internal2input, embedding);
1904 build->internal2input =
1905 isl_multi_aff_flatten_range(build->internal2input);
1906 if (!build->internal2input)
1907 return isl_ast_build_free(build);
1908 } else {
1909 isl_multi_aff_free(embedding);
1912 space = isl_ast_build_get_space(build, 1);
1913 build->options = embed_options(build->options, space);
1915 if (!build->iterators || !build->domain || !build->generated ||
1916 !build->pending || !build->values ||
1917 !build->strides || !build->offsets || !build->options)
1918 return isl_ast_build_free(build);
1920 return build;
1921 error:
1922 isl_ast_build_free(build);
1923 isl_space_free(space);
1924 return NULL;
1927 /* Does "aff" only attain non-negative values over build->domain?
1928 * That is, does it not attain any negative values?
1930 int isl_ast_build_aff_is_nonneg(__isl_keep isl_ast_build *build,
1931 __isl_keep isl_aff *aff)
1933 isl_set *test;
1934 int empty;
1936 if (!build)
1937 return -1;
1939 aff = isl_aff_copy(aff);
1940 test = isl_set_from_basic_set(isl_aff_neg_basic_set(aff));
1941 test = isl_set_intersect(test, isl_set_copy(build->domain));
1942 empty = isl_set_is_empty(test);
1943 isl_set_free(test);
1945 return empty;
1948 /* Does the dimension at (internal) position "pos" have a non-trivial stride?
1950 isl_bool isl_ast_build_has_stride(__isl_keep isl_ast_build *build, int pos)
1952 isl_val *v;
1953 isl_bool has_stride;
1955 if (!build)
1956 return isl_bool_error;
1958 v = isl_vec_get_element_val(build->strides, pos);
1959 has_stride = isl_bool_not(isl_val_is_one(v));
1960 isl_val_free(v);
1962 return has_stride;
1965 /* Given that the dimension at position "pos" takes on values
1967 * f + s a
1969 * with a an integer, return s through *stride.
1971 __isl_give isl_val *isl_ast_build_get_stride(__isl_keep isl_ast_build *build,
1972 int pos)
1974 if (!build)
1975 return NULL;
1977 return isl_vec_get_element_val(build->strides, pos);
1980 /* Given that the dimension at position "pos" takes on values
1982 * f + s a
1984 * with a an integer, return f.
1986 __isl_give isl_aff *isl_ast_build_get_offset(
1987 __isl_keep isl_ast_build *build, int pos)
1989 if (!build)
1990 return NULL;
1992 return isl_multi_aff_get_aff(build->offsets, pos);
1995 /* Is the dimension at position "pos" known to attain only a single
1996 * value that, moreover, can be described by a single affine expression
1997 * in terms of the outer dimensions and parameters?
1999 * If not, then the corresponding affine expression in build->values
2000 * is set to be equal to the same input dimension.
2001 * Otherwise, it is set to the requested expression in terms of
2002 * outer dimensions and parameters.
2004 isl_bool isl_ast_build_has_affine_value(__isl_keep isl_ast_build *build,
2005 int pos)
2007 isl_aff *aff;
2008 isl_bool involves;
2010 if (!build)
2011 return isl_bool_error;
2013 aff = isl_multi_aff_get_aff(build->values, pos);
2014 involves = isl_aff_involves_dims(aff, isl_dim_in, pos, 1);
2015 isl_aff_free(aff);
2017 return isl_bool_not(involves);
2020 /* Plug in the known values (fixed affine expressions in terms of
2021 * parameters and outer loop iterators) of all loop iterators
2022 * in the domain of "umap".
2024 * We simply precompose "umap" with build->values.
2026 __isl_give isl_union_map *isl_ast_build_substitute_values_union_map_domain(
2027 __isl_keep isl_ast_build *build, __isl_take isl_union_map *umap)
2029 isl_multi_aff *values;
2031 if (!build)
2032 return isl_union_map_free(umap);
2034 values = isl_multi_aff_copy(build->values);
2035 umap = isl_union_map_preimage_domain_multi_aff(umap, values);
2037 return umap;
2040 /* Is the current dimension known to attain only a single value?
2042 int isl_ast_build_has_value(__isl_keep isl_ast_build *build)
2044 if (!build)
2045 return -1;
2047 return build->value != NULL;
2050 /* Simplify the basic set "bset" based on what we know about
2051 * the iterators of already generated loops.
2053 * "bset" is assumed to live in the (internal) schedule domain.
2055 __isl_give isl_basic_set *isl_ast_build_compute_gist_basic_set(
2056 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
2058 if (!build)
2059 goto error;
2061 bset = isl_basic_set_preimage_multi_aff(bset,
2062 isl_multi_aff_copy(build->values));
2063 bset = isl_basic_set_gist(bset,
2064 isl_set_simple_hull(isl_set_copy(build->domain)));
2066 return bset;
2067 error:
2068 isl_basic_set_free(bset);
2069 return NULL;
2072 /* Simplify the set "set" based on what we know about
2073 * the iterators of already generated loops.
2075 * "set" is assumed to live in the (internal) schedule domain.
2077 __isl_give isl_set *isl_ast_build_compute_gist(
2078 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2080 if (!build)
2081 goto error;
2083 if (!isl_set_is_params(set))
2084 set = isl_set_preimage_multi_aff(set,
2085 isl_multi_aff_copy(build->values));
2086 set = isl_set_gist(set, isl_set_copy(build->domain));
2088 return set;
2089 error:
2090 isl_set_free(set);
2091 return NULL;
2094 /* Include information about what we know about the iterators of
2095 * already generated loops to "set".
2097 * We currently only plug in the known affine values of outer loop
2098 * iterators.
2099 * In principle we could also introduce equalities or even other
2100 * constraints implied by the intersection of "set" and build->domain.
2102 __isl_give isl_set *isl_ast_build_specialize(__isl_keep isl_ast_build *build,
2103 __isl_take isl_set *set)
2105 if (!build)
2106 return isl_set_free(set);
2108 return isl_set_preimage_multi_aff(set,
2109 isl_multi_aff_copy(build->values));
2112 /* Plug in the known affine values of outer loop iterators in "bset".
2114 __isl_give isl_basic_set *isl_ast_build_specialize_basic_set(
2115 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
2117 if (!build)
2118 return isl_basic_set_free(bset);
2120 return isl_basic_set_preimage_multi_aff(bset,
2121 isl_multi_aff_copy(build->values));
2124 /* Simplify the map "map" based on what we know about
2125 * the iterators of already generated loops.
2127 * The domain of "map" is assumed to live in the (internal) schedule domain.
2129 __isl_give isl_map *isl_ast_build_compute_gist_map_domain(
2130 __isl_keep isl_ast_build *build, __isl_take isl_map *map)
2132 if (!build)
2133 goto error;
2135 map = isl_map_gist_domain(map, isl_set_copy(build->domain));
2137 return map;
2138 error:
2139 isl_map_free(map);
2140 return NULL;
2143 /* Simplify the affine expression "aff" based on what we know about
2144 * the iterators of already generated loops.
2146 * The domain of "aff" is assumed to live in the (internal) schedule domain.
2148 __isl_give isl_aff *isl_ast_build_compute_gist_aff(
2149 __isl_keep isl_ast_build *build, __isl_take isl_aff *aff)
2151 if (!build)
2152 goto error;
2154 aff = isl_aff_gist(aff, isl_set_copy(build->domain));
2156 return aff;
2157 error:
2158 isl_aff_free(aff);
2159 return NULL;
2162 /* Simplify the piecewise affine expression "aff" based on what we know about
2163 * the iterators of already generated loops.
2165 * The domain of "pa" is assumed to live in the (internal) schedule domain.
2167 __isl_give isl_pw_aff *isl_ast_build_compute_gist_pw_aff(
2168 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2170 if (!build)
2171 goto error;
2173 if (!isl_set_is_params(build->domain))
2174 pa = isl_pw_aff_pullback_multi_aff(pa,
2175 isl_multi_aff_copy(build->values));
2176 pa = isl_pw_aff_gist(pa, isl_set_copy(build->domain));
2178 return pa;
2179 error:
2180 isl_pw_aff_free(pa);
2181 return NULL;
2184 /* Simplify the piecewise multi-affine expression "aff" based on what
2185 * we know about the iterators of already generated loops.
2187 * The domain of "pma" is assumed to live in the (internal) schedule domain.
2189 __isl_give isl_pw_multi_aff *isl_ast_build_compute_gist_pw_multi_aff(
2190 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
2192 if (!build)
2193 goto error;
2195 pma = isl_pw_multi_aff_pullback_multi_aff(pma,
2196 isl_multi_aff_copy(build->values));
2197 pma = isl_pw_multi_aff_gist(pma, isl_set_copy(build->domain));
2199 return pma;
2200 error:
2201 isl_pw_multi_aff_free(pma);
2202 return NULL;
2205 /* Extract the schedule domain of the given type from build->options
2206 * at the current depth.
2208 * In particular, find the subset of build->options that is of
2209 * the following form
2211 * schedule_domain -> type[depth]
2213 * and return the corresponding domain, after eliminating inner dimensions
2214 * and divs that depend on the current dimension.
2216 * Note that the domain of build->options has been reformulated
2217 * in terms of the internal build space in embed_options,
2218 * but the position is still that within the current code generation.
2220 __isl_give isl_set *isl_ast_build_get_option_domain(
2221 __isl_keep isl_ast_build *build, enum isl_ast_loop_type type)
2223 const char *name;
2224 isl_space *space;
2225 isl_map *option;
2226 isl_set *domain;
2227 int local_pos;
2229 if (!build)
2230 return NULL;
2232 name = option_str[type];
2233 local_pos = build->depth - build->outer_pos;
2235 space = isl_ast_build_get_space(build, 1);
2236 space = isl_space_from_domain(space);
2237 space = isl_space_add_dims(space, isl_dim_out, 1);
2238 space = isl_space_set_tuple_name(space, isl_dim_out, name);
2240 option = isl_union_map_extract_map(build->options, space);
2241 option = isl_map_fix_si(option, isl_dim_out, 0, local_pos);
2243 domain = isl_map_domain(option);
2244 domain = isl_ast_build_eliminate(build, domain);
2246 return domain;
2249 /* How does the user want the current schedule dimension to be generated?
2250 * These choices have been extracted from the schedule node
2251 * in extract_loop_types and stored in build->loop_type.
2252 * They have been updated to reflect any dimension insertion in
2253 * node_insert_dim.
2254 * Return isl_ast_domain_error on error.
2256 * If "isolated" is set, then we get the loop AST generation type
2257 * directly from the band node since node_insert_dim cannot have been
2258 * called on a band with the isolate option.
2260 enum isl_ast_loop_type isl_ast_build_get_loop_type(
2261 __isl_keep isl_ast_build *build, int isolated)
2263 int local_pos;
2264 isl_ctx *ctx;
2266 if (!build)
2267 return isl_ast_loop_error;
2268 ctx = isl_ast_build_get_ctx(build);
2269 if (!build->node)
2270 isl_die(ctx, isl_error_internal,
2271 "only works for schedule tree based AST generation",
2272 return isl_ast_loop_error);
2274 local_pos = build->depth - build->outer_pos;
2275 if (!isolated)
2276 return build->loop_type[local_pos];
2277 return isl_schedule_node_band_member_get_isolate_ast_loop_type(
2278 build->node, local_pos);
2281 /* Extract the isolated set from the isolate option, if any,
2282 * and store in the build.
2283 * If there is no isolate option, then the isolated set is
2284 * set to the empty set.
2286 * The isolate option is of the form
2288 * isolate[[outer bands] -> current_band]
2290 * We flatten this set and then map it back to the internal
2291 * schedule space.
2293 * If we have already extracted the isolated set
2294 * or if internal2input is no longer set, then we do not
2295 * need to do anything. In the latter case, we know
2296 * that the current band cannot have any isolate option.
2298 __isl_give isl_ast_build *isl_ast_build_extract_isolated(
2299 __isl_take isl_ast_build *build)
2301 isl_set *isolated;
2303 if (!build)
2304 return NULL;
2305 if (!build->internal2input)
2306 return build;
2307 if (build->isolated)
2308 return build;
2310 build = isl_ast_build_cow(build);
2311 if (!build)
2312 return NULL;
2314 isolated = isl_schedule_node_band_get_ast_isolate_option(build->node);
2315 isolated = isl_set_flatten(isolated);
2316 isolated = isl_set_preimage_multi_aff(isolated,
2317 isl_multi_aff_copy(build->internal2input));
2319 build->isolated = isolated;
2320 if (!build->isolated)
2321 return isl_ast_build_free(build);
2323 return build;
2326 /* Does "build" have a non-empty isolated set?
2328 * The caller is assumed to have called isl_ast_build_extract_isolated first.
2330 int isl_ast_build_has_isolated(__isl_keep isl_ast_build *build)
2332 int empty;
2334 if (!build)
2335 return -1;
2336 if (!build->internal2input)
2337 return 0;
2338 if (!build->isolated)
2339 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
2340 "isolated set not extracted yet", return -1);
2342 empty = isl_set_plain_is_empty(build->isolated);
2343 return empty < 0 ? -1 : !empty;
2346 /* Return a copy of the isolated set of "build".
2348 * The caller is assume to have called isl_ast_build_has_isolated first,
2349 * with this function returning true.
2350 * In particular, this function should not be called if we are no
2351 * longer keeping track of internal2input (and there therefore could
2352 * not possibly be any isolated set).
2354 __isl_give isl_set *isl_ast_build_get_isolated(__isl_keep isl_ast_build *build)
2356 if (!build)
2357 return NULL;
2358 if (!build->internal2input)
2359 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
2360 "build cannot have isolated set", return NULL);
2362 return isl_set_copy(build->isolated);
2365 /* Extract the separation class mapping at the current depth.
2367 * In particular, find and return the subset of build->options that is of
2368 * the following form
2370 * schedule_domain -> separation_class[[depth] -> [class]]
2372 * The caller is expected to eliminate inner dimensions from the domain.
2374 * Note that the domain of build->options has been reformulated
2375 * in terms of the internal build space in embed_options,
2376 * but the position is still that within the current code generation.
2378 __isl_give isl_map *isl_ast_build_get_separation_class(
2379 __isl_keep isl_ast_build *build)
2381 isl_ctx *ctx;
2382 isl_space *space_sep, *space;
2383 isl_map *res;
2384 int local_pos;
2386 if (!build)
2387 return NULL;
2389 local_pos = build->depth - build->outer_pos;
2390 ctx = isl_ast_build_get_ctx(build);
2391 space_sep = isl_space_alloc(ctx, 0, 1, 1);
2392 space_sep = isl_space_wrap(space_sep);
2393 space_sep = isl_space_set_tuple_name(space_sep, isl_dim_set,
2394 "separation_class");
2395 space = isl_ast_build_get_space(build, 1);
2396 space_sep = isl_space_align_params(space_sep, isl_space_copy(space));
2397 space = isl_space_map_from_domain_and_range(space, space_sep);
2399 res = isl_union_map_extract_map(build->options, space);
2400 res = isl_map_fix_si(res, isl_dim_out, 0, local_pos);
2401 res = isl_map_coalesce(res);
2403 return res;
2406 /* Eliminate dimensions inner to the current dimension.
2408 __isl_give isl_set *isl_ast_build_eliminate_inner(
2409 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2411 int dim;
2412 int depth;
2414 if (!build)
2415 return isl_set_free(set);
2417 dim = isl_set_dim(set, isl_dim_set);
2418 depth = build->depth;
2419 set = isl_set_detect_equalities(set);
2420 set = isl_set_eliminate(set, isl_dim_set, depth + 1, dim - (depth + 1));
2422 return set;
2425 /* Eliminate unknown divs and divs that depend on the current dimension.
2427 * Note that during the elimination of unknown divs, we may discover
2428 * an explicit representation of some other unknown divs, which may
2429 * depend on the current dimension. We therefore need to eliminate
2430 * unknown divs first.
2432 __isl_give isl_set *isl_ast_build_eliminate_divs(
2433 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2435 int depth;
2437 if (!build)
2438 return isl_set_free(set);
2440 set = isl_set_remove_unknown_divs(set);
2441 depth = build->depth;
2442 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, depth, 1);
2444 return set;
2447 /* Eliminate dimensions inner to the current dimension as well as
2448 * unknown divs and divs that depend on the current dimension.
2449 * The result then consists only of constraints that are independent
2450 * of the current dimension and upper and lower bounds on the current
2451 * dimension.
2453 __isl_give isl_set *isl_ast_build_eliminate(
2454 __isl_keep isl_ast_build *build, __isl_take isl_set *domain)
2456 domain = isl_ast_build_eliminate_inner(build, domain);
2457 domain = isl_ast_build_eliminate_divs(build, domain);
2458 return domain;
2461 /* Replace build->single_valued by "sv".
2463 __isl_give isl_ast_build *isl_ast_build_set_single_valued(
2464 __isl_take isl_ast_build *build, int sv)
2466 if (!build)
2467 return build;
2468 if (build->single_valued == sv)
2469 return build;
2470 build = isl_ast_build_cow(build);
2471 if (!build)
2472 return build;
2473 build->single_valued = sv;
2475 return build;