isl_inequality_negate: return modified result
[isl.git] / isl_ast_build.c
blob6670a5893a5a5b18e48d539a286bf4a379023417
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;
61 build = isl_ast_build_cow(build);
62 if (!build || !build->domain)
63 goto error;
65 ctx = isl_ast_build_get_ctx(build);
66 strides = isl_vec_alloc(ctx, isl_space_dim(space, isl_dim_set));
67 strides = isl_vec_set_si(strides, 1);
69 isl_vec_free(build->strides);
70 build->strides = strides;
72 space = isl_space_map_from_set(space);
73 isl_multi_aff_free(build->offsets);
74 build->offsets = isl_multi_aff_zero(isl_space_copy(space));
75 isl_multi_aff_free(build->values);
76 build->values = isl_multi_aff_identity(isl_space_copy(space));
77 isl_multi_aff_free(build->internal2input);
78 build->internal2input = isl_multi_aff_identity(space);
80 if (!build->iterators || !build->domain || !build->generated ||
81 !build->pending || !build->values || !build->internal2input ||
82 !build->strides || !build->offsets || !build->options)
83 return isl_ast_build_free(build);
85 return build;
86 error:
87 isl_space_free(space);
88 return isl_ast_build_free(build);
91 /* Return an isl_id called "c%d", with "%d" set to "i".
92 * If an isl_id with such a name already appears among the parameters
93 * in build->domain, then adjust the name to "c%d_%d".
95 static __isl_give isl_id *generate_name(isl_ctx *ctx, int i,
96 __isl_keep isl_ast_build *build)
98 int j;
99 char name[23];
100 isl_set *dom = build->domain;
102 snprintf(name, sizeof(name), "c%d", i);
103 j = 0;
104 while (isl_set_find_dim_by_name(dom, isl_dim_param, name) >= 0)
105 snprintf(name, sizeof(name), "c%d_%d", i, j++);
106 return isl_id_alloc(ctx, name, NULL);
109 /* Create an isl_ast_build with "set" as domain.
111 * The input set is usually a parameter domain, but we currently allow it to
112 * be any kind of set. We set the domain of the returned isl_ast_build
113 * to "set" and initialize all the other fields to default values.
115 __isl_give isl_ast_build *isl_ast_build_from_context(__isl_take isl_set *set)
117 int i, n;
118 isl_ctx *ctx;
119 isl_space *space;
120 isl_ast_build *build;
122 set = isl_set_compute_divs(set);
123 if (!set)
124 return NULL;
126 ctx = isl_set_get_ctx(set);
128 build = isl_calloc_type(ctx, isl_ast_build);
129 if (!build)
130 goto error;
132 build->ref = 1;
133 build->domain = set;
134 build->generated = isl_set_copy(build->domain);
135 build->pending = isl_set_universe(isl_set_get_space(build->domain));
136 build->options = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
137 n = isl_set_dim(set, isl_dim_set);
138 build->depth = n;
139 build->iterators = isl_id_list_alloc(ctx, n);
140 for (i = 0; i < n; ++i) {
141 isl_id *id;
142 if (isl_set_has_dim_id(set, isl_dim_set, i))
143 id = isl_set_get_dim_id(set, isl_dim_set, i);
144 else
145 id = generate_name(ctx, i, build);
146 build->iterators = isl_id_list_add(build->iterators, id);
148 space = isl_set_get_space(set);
149 if (isl_space_is_params(space))
150 space = isl_space_set_from_params(space);
152 return isl_ast_build_init_derived(build, space);
153 error:
154 isl_set_free(set);
155 return NULL;
158 /* Create an isl_ast_build with a universe (parametric) context.
160 __isl_give isl_ast_build *isl_ast_build_alloc(isl_ctx *ctx)
162 isl_space *space;
163 isl_set *context;
165 space = isl_space_params_alloc(ctx, 0);
166 context = isl_set_universe(space);
168 return isl_ast_build_from_context(context);
171 __isl_give isl_ast_build *isl_ast_build_copy(__isl_keep isl_ast_build *build)
173 if (!build)
174 return NULL;
176 build->ref++;
177 return build;
180 __isl_give isl_ast_build *isl_ast_build_dup(__isl_keep isl_ast_build *build)
182 isl_ctx *ctx;
183 isl_ast_build *dup;
185 if (!build)
186 return NULL;
188 ctx = isl_ast_build_get_ctx(build);
189 dup = isl_calloc_type(ctx, isl_ast_build);
190 if (!dup)
191 return NULL;
193 dup->ref = 1;
194 dup->outer_pos = build->outer_pos;
195 dup->depth = build->depth;
196 dup->iterators = isl_id_list_copy(build->iterators);
197 dup->domain = isl_set_copy(build->domain);
198 dup->generated = isl_set_copy(build->generated);
199 dup->pending = isl_set_copy(build->pending);
200 dup->values = isl_multi_aff_copy(build->values);
201 dup->internal2input = isl_multi_aff_copy(build->internal2input);
202 dup->value = isl_pw_aff_copy(build->value);
203 dup->strides = isl_vec_copy(build->strides);
204 dup->offsets = isl_multi_aff_copy(build->offsets);
205 dup->executed = isl_union_map_copy(build->executed);
206 dup->single_valued = build->single_valued;
207 dup->options = isl_union_map_copy(build->options);
208 dup->at_each_domain = build->at_each_domain;
209 dup->at_each_domain_user = build->at_each_domain_user;
210 dup->before_each_for = build->before_each_for;
211 dup->before_each_for_user = build->before_each_for_user;
212 dup->after_each_for = build->after_each_for;
213 dup->after_each_for_user = build->after_each_for_user;
214 dup->before_each_mark = build->before_each_mark;
215 dup->before_each_mark_user = build->before_each_mark_user;
216 dup->after_each_mark = build->after_each_mark;
217 dup->after_each_mark_user = build->after_each_mark_user;
218 dup->create_leaf = build->create_leaf;
219 dup->create_leaf_user = build->create_leaf_user;
220 dup->node = isl_schedule_node_copy(build->node);
221 if (build->loop_type) {
222 int i;
224 dup->n = build->n;
225 dup->loop_type = isl_alloc_array(ctx,
226 enum isl_ast_loop_type, dup->n);
227 if (dup->n && !dup->loop_type)
228 return isl_ast_build_free(dup);
229 for (i = 0; i < dup->n; ++i)
230 dup->loop_type[i] = build->loop_type[i];
233 if (!dup->iterators || !dup->domain || !dup->generated ||
234 !dup->pending || !dup->values ||
235 !dup->strides || !dup->offsets || !dup->options ||
236 (build->internal2input && !dup->internal2input) ||
237 (build->executed && !dup->executed) ||
238 (build->value && !dup->value) ||
239 (build->node && !dup->node))
240 return isl_ast_build_free(dup);
242 return dup;
245 /* Align the parameters of "build" to those of "model", introducing
246 * additional parameters if needed.
248 __isl_give isl_ast_build *isl_ast_build_align_params(
249 __isl_take isl_ast_build *build, __isl_take isl_space *model)
251 build = isl_ast_build_cow(build);
252 if (!build)
253 goto error;
255 build->domain = isl_set_align_params(build->domain,
256 isl_space_copy(model));
257 build->generated = isl_set_align_params(build->generated,
258 isl_space_copy(model));
259 build->pending = isl_set_align_params(build->pending,
260 isl_space_copy(model));
261 build->values = isl_multi_aff_align_params(build->values,
262 isl_space_copy(model));
263 build->offsets = isl_multi_aff_align_params(build->offsets,
264 isl_space_copy(model));
265 build->options = isl_union_map_align_params(build->options,
266 isl_space_copy(model));
267 if (build->internal2input) {
268 build->internal2input =
269 isl_multi_aff_align_params(build->internal2input,
270 model);
271 if (!build->internal2input)
272 return isl_ast_build_free(build);
273 } else {
274 isl_space_free(model);
277 if (!build->domain || !build->values || !build->offsets ||
278 !build->options)
279 return isl_ast_build_free(build);
281 return build;
282 error:
283 isl_space_free(model);
284 return NULL;
287 __isl_give isl_ast_build *isl_ast_build_cow(__isl_take isl_ast_build *build)
289 if (!build)
290 return NULL;
292 if (build->ref == 1)
293 return build;
294 build->ref--;
295 return isl_ast_build_dup(build);
298 __isl_null isl_ast_build *isl_ast_build_free(
299 __isl_take isl_ast_build *build)
301 if (!build)
302 return NULL;
304 if (--build->ref > 0)
305 return NULL;
307 isl_id_list_free(build->iterators);
308 isl_set_free(build->domain);
309 isl_set_free(build->generated);
310 isl_set_free(build->pending);
311 isl_multi_aff_free(build->values);
312 isl_multi_aff_free(build->internal2input);
313 isl_pw_aff_free(build->value);
314 isl_vec_free(build->strides);
315 isl_multi_aff_free(build->offsets);
316 isl_multi_aff_free(build->schedule_map);
317 isl_union_map_free(build->executed);
318 isl_union_map_free(build->options);
319 isl_schedule_node_free(build->node);
320 free(build->loop_type);
321 isl_set_free(build->isolated);
323 free(build);
325 return NULL;
328 isl_ctx *isl_ast_build_get_ctx(__isl_keep isl_ast_build *build)
330 return build ? isl_set_get_ctx(build->domain) : NULL;
333 /* Replace build->options by "options".
335 __isl_give isl_ast_build *isl_ast_build_set_options(
336 __isl_take isl_ast_build *build, __isl_take isl_union_map *options)
338 build = isl_ast_build_cow(build);
340 if (!build || !options)
341 goto error;
343 isl_union_map_free(build->options);
344 build->options = options;
346 return build;
347 error:
348 isl_union_map_free(options);
349 return isl_ast_build_free(build);
352 /* Set the iterators for the next code generation.
354 * If we still have some iterators left from the previous code generation
355 * (if any) or if iterators have already been set by a previous
356 * call to this function, then we remove them first.
358 __isl_give isl_ast_build *isl_ast_build_set_iterators(
359 __isl_take isl_ast_build *build, __isl_take isl_id_list *iterators)
361 int dim, n_it;
363 build = isl_ast_build_cow(build);
364 if (!build)
365 goto error;
367 dim = isl_ast_build_dim(build, isl_dim_set);
368 n_it = isl_id_list_n_id(build->iterators);
369 if (n_it < dim)
370 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
371 "isl_ast_build in inconsistent state", goto error);
372 if (n_it > dim)
373 build->iterators = isl_id_list_drop(build->iterators,
374 dim, n_it - dim);
375 build->iterators = isl_id_list_concat(build->iterators, iterators);
376 if (!build->iterators)
377 return isl_ast_build_free(build);
379 return build;
380 error:
381 isl_id_list_free(iterators);
382 return isl_ast_build_free(build);
385 /* Set the "at_each_domain" callback of "build" to "fn".
387 __isl_give isl_ast_build *isl_ast_build_set_at_each_domain(
388 __isl_take isl_ast_build *build,
389 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
390 __isl_keep isl_ast_build *build, void *user), void *user)
392 build = isl_ast_build_cow(build);
394 if (!build)
395 return NULL;
397 build->at_each_domain = fn;
398 build->at_each_domain_user = user;
400 return build;
403 /* Set the "before_each_for" callback of "build" to "fn".
405 __isl_give isl_ast_build *isl_ast_build_set_before_each_for(
406 __isl_take isl_ast_build *build,
407 __isl_give isl_id *(*fn)(__isl_keep isl_ast_build *build,
408 void *user), void *user)
410 build = isl_ast_build_cow(build);
412 if (!build)
413 return NULL;
415 build->before_each_for = fn;
416 build->before_each_for_user = user;
418 return build;
421 /* Set the "after_each_for" callback of "build" to "fn".
423 __isl_give isl_ast_build *isl_ast_build_set_after_each_for(
424 __isl_take isl_ast_build *build,
425 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
426 __isl_keep isl_ast_build *build, void *user), void *user)
428 build = isl_ast_build_cow(build);
430 if (!build)
431 return NULL;
433 build->after_each_for = fn;
434 build->after_each_for_user = user;
436 return build;
439 /* Set the "before_each_mark" callback of "build" to "fn".
441 __isl_give isl_ast_build *isl_ast_build_set_before_each_mark(
442 __isl_take isl_ast_build *build,
443 isl_stat (*fn)(__isl_keep isl_id *mark, __isl_keep isl_ast_build *build,
444 void *user), void *user)
446 build = isl_ast_build_cow(build);
448 if (!build)
449 return NULL;
451 build->before_each_mark = fn;
452 build->before_each_mark_user = user;
454 return build;
457 /* Set the "after_each_mark" callback of "build" to "fn".
459 __isl_give isl_ast_build *isl_ast_build_set_after_each_mark(
460 __isl_take isl_ast_build *build,
461 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
462 __isl_keep isl_ast_build *build, void *user), void *user)
464 build = isl_ast_build_cow(build);
466 if (!build)
467 return NULL;
469 build->after_each_mark = fn;
470 build->after_each_mark_user = user;
472 return build;
475 /* Set the "create_leaf" callback of "build" to "fn".
477 __isl_give isl_ast_build *isl_ast_build_set_create_leaf(
478 __isl_take isl_ast_build *build,
479 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_build *build,
480 void *user), void *user)
482 build = isl_ast_build_cow(build);
484 if (!build)
485 return NULL;
487 build->create_leaf = fn;
488 build->create_leaf_user = user;
490 return build;
493 /* Clear all information that is specific to this code generation
494 * and that is (probably) not meaningful to any nested code generation.
496 __isl_give isl_ast_build *isl_ast_build_clear_local_info(
497 __isl_take isl_ast_build *build)
499 isl_space *space;
501 build = isl_ast_build_cow(build);
502 if (!build)
503 return NULL;
505 space = isl_union_map_get_space(build->options);
506 isl_union_map_free(build->options);
507 build->options = isl_union_map_empty(space);
509 build->at_each_domain = NULL;
510 build->at_each_domain_user = NULL;
511 build->before_each_for = NULL;
512 build->before_each_for_user = NULL;
513 build->after_each_for = NULL;
514 build->after_each_for_user = NULL;
515 build->before_each_mark = NULL;
516 build->before_each_mark_user = NULL;
517 build->after_each_mark = NULL;
518 build->after_each_mark_user = NULL;
519 build->create_leaf = NULL;
520 build->create_leaf_user = NULL;
522 if (!build->options)
523 return isl_ast_build_free(build);
525 return build;
528 /* Have any loops been eliminated?
529 * That is, do any of the original schedule dimensions have a fixed
530 * value that has been substituted?
532 static int any_eliminated(isl_ast_build *build)
534 int i;
536 for (i = 0; i < build->depth; ++i)
537 if (isl_ast_build_has_affine_value(build, i))
538 return 1;
540 return 0;
543 /* Clear build->schedule_map.
544 * This function should be called whenever anything that might affect
545 * the result of isl_ast_build_get_schedule_map_multi_aff changes.
546 * In particular, it should be called when the depth is changed or
547 * when an iterator is determined to have a fixed value.
549 static void isl_ast_build_reset_schedule_map(__isl_keep isl_ast_build *build)
551 if (!build)
552 return;
553 isl_multi_aff_free(build->schedule_map);
554 build->schedule_map = NULL;
557 /* Do we need a (non-trivial) schedule map?
558 * That is, is the internal schedule space different from
559 * the external schedule space?
561 * The internal and external schedule spaces are only the same
562 * if code has been generated for the entire schedule and if none
563 * of the loops have been eliminated.
565 isl_bool isl_ast_build_need_schedule_map(__isl_keep isl_ast_build *build)
567 int dim;
569 if (!build)
570 return isl_bool_error;
572 dim = isl_ast_build_dim(build, isl_dim_set);
573 return build->depth != dim || any_eliminated(build);
576 /* Return a mapping from the internal schedule space to the external
577 * schedule space in the form of an isl_multi_aff.
578 * The internal schedule space originally corresponds to that of the
579 * input schedule. This may change during the code generation if
580 * if isl_ast_build_insert_dim is ever called.
581 * The external schedule space corresponds to the
582 * loops that have been generated.
584 * Currently, the only difference between the internal schedule domain
585 * and the external schedule domain is that some dimensions are projected
586 * out in the external schedule domain. In particular, the dimensions
587 * for which no code has been generated yet and the dimensions that correspond
588 * to eliminated loops.
590 * We cache a copy of the schedule_map in build->schedule_map.
591 * The cache is cleared through isl_ast_build_reset_schedule_map
592 * whenever anything changes that might affect the result of this function.
594 __isl_give isl_multi_aff *isl_ast_build_get_schedule_map_multi_aff(
595 __isl_keep isl_ast_build *build)
597 isl_bool needs_map;
598 isl_space *space;
599 isl_multi_aff *ma;
601 if (!build)
602 return NULL;
603 if (build->schedule_map)
604 return isl_multi_aff_copy(build->schedule_map);
605 needs_map = isl_ast_build_need_schedule_map(build);
606 if (needs_map < 0)
607 return NULL;
609 space = isl_ast_build_get_space(build, 1);
610 space = isl_space_map_from_set(space);
611 ma = isl_multi_aff_identity(space);
612 if (needs_map) {
613 int i;
614 int dim = isl_ast_build_dim(build, isl_dim_set);
615 ma = isl_multi_aff_drop_dims(ma, isl_dim_out,
616 build->depth, dim - build->depth);
617 for (i = build->depth - 1; i >= 0; --i)
618 if (isl_ast_build_has_affine_value(build, i))
619 ma = isl_multi_aff_drop_dims(ma,
620 isl_dim_out, i, 1);
623 build->schedule_map = ma;
624 return isl_multi_aff_copy(build->schedule_map);
627 /* Return a mapping from the internal schedule space to the external
628 * schedule space in the form of an isl_map.
630 __isl_give isl_map *isl_ast_build_get_schedule_map(
631 __isl_keep isl_ast_build *build)
633 isl_multi_aff *ma;
635 ma = isl_ast_build_get_schedule_map_multi_aff(build);
636 return isl_map_from_multi_aff(ma);
639 /* Return the position of the dimension in build->domain for which
640 * an AST node is currently being generated.
642 int isl_ast_build_get_depth(__isl_keep isl_ast_build *build)
644 return build ? build->depth : -1;
647 /* Prepare for generating code for the next level.
648 * In particular, increase the depth and reset any information
649 * that is local to the current depth.
651 __isl_give isl_ast_build *isl_ast_build_increase_depth(
652 __isl_take isl_ast_build *build)
654 build = isl_ast_build_cow(build);
655 if (!build)
656 return NULL;
657 build->depth++;
658 isl_ast_build_reset_schedule_map(build);
659 build->value = isl_pw_aff_free(build->value);
660 return build;
663 void isl_ast_build_dump(__isl_keep isl_ast_build *build)
665 if (!build)
666 return;
668 fprintf(stderr, "domain: ");
669 isl_set_dump(build->domain);
670 fprintf(stderr, "generated: ");
671 isl_set_dump(build->generated);
672 fprintf(stderr, "pending: ");
673 isl_set_dump(build->pending);
674 fprintf(stderr, "iterators: ");
675 isl_id_list_dump(build->iterators);
676 fprintf(stderr, "values: ");
677 isl_multi_aff_dump(build->values);
678 if (build->value) {
679 fprintf(stderr, "value: ");
680 isl_pw_aff_dump(build->value);
682 fprintf(stderr, "strides: ");
683 isl_vec_dump(build->strides);
684 fprintf(stderr, "offsets: ");
685 isl_multi_aff_dump(build->offsets);
686 fprintf(stderr, "internal2input: ");
687 isl_multi_aff_dump(build->internal2input);
690 /* Initialize "build" for AST construction in schedule space "space"
691 * in the case that build->domain is a parameter set.
693 * build->iterators is assumed to have been updated already.
695 static __isl_give isl_ast_build *isl_ast_build_init(
696 __isl_take isl_ast_build *build, __isl_take isl_space *space)
698 isl_set *set;
700 build = isl_ast_build_cow(build);
701 if (!build)
702 goto error;
704 set = isl_set_universe(isl_space_copy(space));
705 build->domain = isl_set_intersect_params(isl_set_copy(set),
706 build->domain);
707 build->pending = isl_set_intersect_params(isl_set_copy(set),
708 build->pending);
709 build->generated = isl_set_intersect_params(set, build->generated);
711 return isl_ast_build_init_derived(build, space);
712 error:
713 isl_ast_build_free(build);
714 isl_space_free(space);
715 return NULL;
718 /* Assign "aff" to *user and return -1, effectively extracting
719 * the first (and presumably only) affine expression in the isl_pw_aff
720 * on which this function is used.
722 static isl_stat extract_single_piece(__isl_take isl_set *set,
723 __isl_take isl_aff *aff, void *user)
725 isl_aff **p = user;
727 *p = aff;
728 isl_set_free(set);
730 return isl_stat_error;
733 /* Intersect "set" with the stride constraint of "build", if any.
735 static __isl_give isl_set *intersect_stride_constraint(__isl_take isl_set *set,
736 __isl_keep isl_ast_build *build)
738 isl_set *stride;
740 if (!build)
741 return isl_set_free(set);
742 if (!isl_ast_build_has_stride(build, build->depth))
743 return set;
745 stride = isl_ast_build_get_stride_constraint(build);
746 return isl_set_intersect(set, stride);
749 /* Check if the given bounds on the current dimension (together with
750 * the stride constraint, if any) imply that
751 * this current dimension attains only a single value (in terms of
752 * parameters and outer dimensions).
753 * If so, we record it in build->value.
754 * If, moreover, this value can be represented as a single affine expression,
755 * then we also update build->values, effectively marking the current
756 * dimension as "eliminated".
758 * When computing the gist of the fixed value that can be represented
759 * as a single affine expression, it is important to only take into
760 * account the domain constraints in the original AST build and
761 * not the domain of the affine expression itself.
762 * Otherwise, a [i/3] is changed into a i/3 because we know that i
763 * is a multiple of 3, but then we end up not expressing anywhere
764 * in the context that i is a multiple of 3.
766 static __isl_give isl_ast_build *update_values(
767 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
769 isl_bool sv;
770 isl_pw_multi_aff *pma;
771 isl_aff *aff = NULL;
772 isl_map *it_map;
773 isl_set *set;
775 set = isl_set_from_basic_set(bounds);
776 set = isl_set_intersect(set, isl_set_copy(build->domain));
777 set = intersect_stride_constraint(set, build);
778 it_map = isl_ast_build_map_to_iterator(build, set);
780 sv = isl_map_is_single_valued(it_map);
781 if (sv < 0)
782 build = isl_ast_build_free(build);
783 if (!build || !sv) {
784 isl_map_free(it_map);
785 return build;
788 pma = isl_pw_multi_aff_from_map(it_map);
789 build->value = isl_pw_multi_aff_get_pw_aff(pma, 0);
790 build->value = isl_ast_build_compute_gist_pw_aff(build, build->value);
791 build->value = isl_pw_aff_coalesce(build->value);
792 isl_pw_multi_aff_free(pma);
794 if (!build->value)
795 return isl_ast_build_free(build);
797 if (isl_pw_aff_n_piece(build->value) != 1)
798 return build;
800 isl_pw_aff_foreach_piece(build->value, &extract_single_piece, &aff);
802 build->values = isl_multi_aff_set_aff(build->values, build->depth, aff);
803 if (!build->values)
804 return isl_ast_build_free(build);
805 isl_ast_build_reset_schedule_map(build);
806 return build;
809 /* Update the AST build based on the given loop bounds for
810 * the current dimension and the stride information available in the build.
812 * We first make sure that the bounds do not refer to any iterators
813 * that have already been eliminated.
814 * Then, we check if the bounds imply that the current iterator
815 * has a fixed value.
816 * If they do and if this fixed value can be expressed as a single
817 * affine expression, we eliminate the iterators from the bounds.
818 * Note that we cannot simply plug in this single value using
819 * isl_basic_set_preimage_multi_aff as the single value may only
820 * be defined on a subset of the domain. Plugging in the value
821 * would restrict the build domain to this subset, while this
822 * restriction may not be reflected in the generated code.
823 * Finally, we intersect build->domain with the updated bounds.
824 * We also add the stride constraint unless we have been able
825 * to find a fixed value expressed as a single affine expression.
827 * Note that the check for a fixed value in update_values requires
828 * us to intersect the bounds with the current build domain.
829 * When we intersect build->domain with the updated bounds in
830 * the final step, we make sure that these updated bounds have
831 * not been intersected with the old build->domain.
832 * Otherwise, we would indirectly intersect the build domain with itself,
833 * which can lead to inefficiencies, in particular if the build domain
834 * contains any unknown divs.
836 * The pending and generated sets are not updated by this function to
837 * match the updated domain.
838 * The caller still needs to call isl_ast_build_set_pending_generated.
840 __isl_give isl_ast_build *isl_ast_build_set_loop_bounds(
841 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
843 isl_set *set;
845 build = isl_ast_build_cow(build);
846 if (!build)
847 goto error;
849 build = update_values(build, isl_basic_set_copy(bounds));
850 if (!build)
851 goto error;
852 set = isl_set_from_basic_set(isl_basic_set_copy(bounds));
853 if (isl_ast_build_has_affine_value(build, build->depth)) {
854 set = isl_set_eliminate(set, isl_dim_set, build->depth, 1);
855 set = isl_set_compute_divs(set);
856 build->pending = isl_set_intersect(build->pending,
857 isl_set_copy(set));
858 build->domain = isl_set_intersect(build->domain, set);
859 } else {
860 build->domain = isl_set_intersect(build->domain, set);
861 build = isl_ast_build_include_stride(build);
862 if (!build)
863 goto error;
865 isl_basic_set_free(bounds);
867 if (!build->domain || !build->pending || !build->generated)
868 return isl_ast_build_free(build);
870 return build;
871 error:
872 isl_ast_build_free(build);
873 isl_basic_set_free(bounds);
874 return NULL;
877 /* Update the pending and generated sets of "build" according to "bounds".
878 * If the build has an affine value at the current depth,
879 * then isl_ast_build_set_loop_bounds has already set the pending set.
880 * Otherwise, do it here.
882 __isl_give isl_ast_build *isl_ast_build_set_pending_generated(
883 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
885 isl_basic_set *generated, *pending;
887 if (!build)
888 goto error;
890 if (isl_ast_build_has_affine_value(build, build->depth)) {
891 isl_basic_set_free(bounds);
892 return build;
895 build = isl_ast_build_cow(build);
896 if (!build)
897 goto error;
899 pending = isl_basic_set_copy(bounds);
900 pending = isl_basic_set_drop_constraints_involving_dims(pending,
901 isl_dim_set, build->depth, 1);
902 build->pending = isl_set_intersect(build->pending,
903 isl_set_from_basic_set(pending));
904 generated = bounds;
905 generated = isl_basic_set_drop_constraints_not_involving_dims(
906 generated, isl_dim_set, build->depth, 1);
907 build->generated = isl_set_intersect(build->generated,
908 isl_set_from_basic_set(generated));
910 if (!build->pending || !build->generated)
911 return isl_ast_build_free(build);
913 return build;
914 error:
915 isl_ast_build_free(build);
916 isl_basic_set_free(bounds);
917 return NULL;
920 /* Intersect build->domain with "set", where "set" is specified
921 * in terms of the internal schedule domain.
923 static __isl_give isl_ast_build *isl_ast_build_restrict_internal(
924 __isl_take isl_ast_build *build, __isl_take isl_set *set)
926 build = isl_ast_build_cow(build);
927 if (!build)
928 goto error;
930 set = isl_set_compute_divs(set);
931 build->domain = isl_set_intersect(build->domain, set);
932 build->domain = isl_set_coalesce(build->domain);
934 if (!build->domain)
935 return isl_ast_build_free(build);
937 return build;
938 error:
939 isl_ast_build_free(build);
940 isl_set_free(set);
941 return NULL;
944 /* Intersect build->generated and build->domain with "set",
945 * where "set" is specified in terms of the internal schedule domain.
947 __isl_give isl_ast_build *isl_ast_build_restrict_generated(
948 __isl_take isl_ast_build *build, __isl_take isl_set *set)
950 set = isl_set_compute_divs(set);
951 build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
952 build = isl_ast_build_cow(build);
953 if (!build)
954 goto error;
956 build->generated = isl_set_intersect(build->generated, set);
957 build->generated = isl_set_coalesce(build->generated);
959 if (!build->generated)
960 return isl_ast_build_free(build);
962 return build;
963 error:
964 isl_ast_build_free(build);
965 isl_set_free(set);
966 return NULL;
969 /* Replace the set of pending constraints by "guard", which is then
970 * no longer considered as pending.
971 * That is, add "guard" to the generated constraints and clear all pending
972 * constraints, making the domain equal to the generated constraints.
974 __isl_give isl_ast_build *isl_ast_build_replace_pending_by_guard(
975 __isl_take isl_ast_build *build, __isl_take isl_set *guard)
977 build = isl_ast_build_restrict_generated(build, guard);
978 build = isl_ast_build_cow(build);
979 if (!build)
980 return NULL;
982 isl_set_free(build->domain);
983 build->domain = isl_set_copy(build->generated);
984 isl_set_free(build->pending);
985 build->pending = isl_set_universe(isl_set_get_space(build->domain));
987 if (!build->pending)
988 return isl_ast_build_free(build);
990 return build;
993 /* Intersect build->domain with "set", where "set" is specified
994 * in terms of the external schedule domain.
996 __isl_give isl_ast_build *isl_ast_build_restrict(
997 __isl_take isl_ast_build *build, __isl_take isl_set *set)
999 isl_bool needs_map;
1001 if (isl_set_is_params(set))
1002 return isl_ast_build_restrict_generated(build, set);
1004 needs_map = isl_ast_build_need_schedule_map(build);
1005 if (needs_map < 0)
1006 goto error;
1007 if (needs_map) {
1008 isl_multi_aff *ma;
1009 ma = isl_ast_build_get_schedule_map_multi_aff(build);
1010 set = isl_set_preimage_multi_aff(set, ma);
1012 return isl_ast_build_restrict_generated(build, set);
1013 error:
1014 isl_ast_build_free(build);
1015 isl_set_free(set);
1016 return NULL;
1019 /* Replace build->executed by "executed".
1021 __isl_give isl_ast_build *isl_ast_build_set_executed(
1022 __isl_take isl_ast_build *build, __isl_take isl_union_map *executed)
1024 build = isl_ast_build_cow(build);
1025 if (!build)
1026 goto error;
1028 isl_union_map_free(build->executed);
1029 build->executed = executed;
1031 return build;
1032 error:
1033 isl_ast_build_free(build);
1034 isl_union_map_free(executed);
1035 return NULL;
1038 /* Does "build" point to a band node?
1039 * That is, are we currently handling a band node inside a schedule tree?
1041 int isl_ast_build_has_schedule_node(__isl_keep isl_ast_build *build)
1043 if (!build)
1044 return -1;
1045 return build->node != NULL;
1048 /* Return a copy of the band node that "build" refers to.
1050 __isl_give isl_schedule_node *isl_ast_build_get_schedule_node(
1051 __isl_keep isl_ast_build *build)
1053 if (!build)
1054 return NULL;
1055 return isl_schedule_node_copy(build->node);
1058 /* Extract the loop AST generation types for the members of build->node
1059 * and store them in build->loop_type.
1061 static __isl_give isl_ast_build *extract_loop_types(
1062 __isl_take isl_ast_build *build)
1064 int i;
1065 isl_ctx *ctx;
1066 isl_schedule_node *node;
1068 if (!build)
1069 return NULL;
1070 ctx = isl_ast_build_get_ctx(build);
1071 if (!build->node)
1072 isl_die(ctx, isl_error_internal, "missing AST node",
1073 return isl_ast_build_free(build));
1075 free(build->loop_type);
1076 build->n = isl_schedule_node_band_n_member(build->node);
1077 build->loop_type = isl_alloc_array(ctx,
1078 enum isl_ast_loop_type, build->n);
1079 if (build->n && !build->loop_type)
1080 return isl_ast_build_free(build);
1081 node = build->node;
1082 for (i = 0; i < build->n; ++i)
1083 build->loop_type[i] =
1084 isl_schedule_node_band_member_get_ast_loop_type(node, i);
1086 return build;
1089 /* Replace the band node that "build" refers to by "node" and
1090 * extract the corresponding loop AST generation types.
1092 __isl_give isl_ast_build *isl_ast_build_set_schedule_node(
1093 __isl_take isl_ast_build *build,
1094 __isl_take isl_schedule_node *node)
1096 build = isl_ast_build_cow(build);
1097 if (!build || !node)
1098 goto error;
1100 isl_schedule_node_free(build->node);
1101 build->node = node;
1103 build = extract_loop_types(build);
1105 return build;
1106 error:
1107 isl_ast_build_free(build);
1108 isl_schedule_node_free(node);
1109 return NULL;
1112 /* Remove any reference to a band node from "build".
1114 __isl_give isl_ast_build *isl_ast_build_reset_schedule_node(
1115 __isl_take isl_ast_build *build)
1117 build = isl_ast_build_cow(build);
1118 if (!build)
1119 return NULL;
1121 isl_schedule_node_free(build->node);
1122 build->node = NULL;
1124 return build;
1127 /* Return a copy of the current schedule domain.
1129 __isl_give isl_set *isl_ast_build_get_domain(__isl_keep isl_ast_build *build)
1131 return build ? isl_set_copy(build->domain) : NULL;
1134 /* Return a copy of the set of pending constraints.
1136 __isl_give isl_set *isl_ast_build_get_pending(
1137 __isl_keep isl_ast_build *build)
1139 return build ? isl_set_copy(build->pending) : NULL;
1142 /* Return a copy of the set of generated constraints.
1144 __isl_give isl_set *isl_ast_build_get_generated(
1145 __isl_keep isl_ast_build *build)
1147 return build ? isl_set_copy(build->generated) : NULL;
1150 /* Return a copy of the map from the internal schedule domain
1151 * to the original input schedule domain.
1153 __isl_give isl_multi_aff *isl_ast_build_get_internal2input(
1154 __isl_keep isl_ast_build *build)
1156 return build ? isl_multi_aff_copy(build->internal2input) : NULL;
1159 /* Return the number of variables of the given type
1160 * in the (internal) schedule space.
1162 unsigned isl_ast_build_dim(__isl_keep isl_ast_build *build,
1163 enum isl_dim_type type)
1165 if (!build)
1166 return 0;
1167 return isl_set_dim(build->domain, type);
1170 /* Return the (schedule) space of "build".
1172 * If "internal" is set, then this space is the space of the internal
1173 * representation of the entire schedule, including those parts for
1174 * which no code has been generated yet.
1176 * If "internal" is not set, then this space is the external representation
1177 * of the loops generated so far.
1179 __isl_give isl_space *isl_ast_build_get_space(__isl_keep isl_ast_build *build,
1180 int internal)
1182 int i;
1183 int dim;
1184 isl_bool needs_map;
1185 isl_space *space;
1187 if (!build)
1188 return NULL;
1190 space = isl_set_get_space(build->domain);
1191 if (internal)
1192 return space;
1194 needs_map = isl_ast_build_need_schedule_map(build);
1195 if (needs_map < 0)
1196 return isl_space_free(space);
1197 if (!needs_map)
1198 return space;
1200 dim = isl_ast_build_dim(build, isl_dim_set);
1201 space = isl_space_drop_dims(space, isl_dim_set,
1202 build->depth, dim - build->depth);
1203 for (i = build->depth - 1; i >= 0; --i) {
1204 isl_bool affine = isl_ast_build_has_affine_value(build, i);
1206 if (affine < 0)
1207 return isl_space_free(space);
1208 if (affine)
1209 space = isl_space_drop_dims(space, isl_dim_set, i, 1);
1212 return space;
1215 /* Return the external representation of the schedule space of "build",
1216 * i.e., a space with a dimension for each loop generated so far,
1217 * with the names of the dimensions set to the loop iterators.
1219 __isl_give isl_space *isl_ast_build_get_schedule_space(
1220 __isl_keep isl_ast_build *build)
1222 isl_space *space;
1223 int i, skip;
1225 if (!build)
1226 return NULL;
1228 space = isl_ast_build_get_space(build, 0);
1230 skip = 0;
1231 for (i = 0; i < build->depth; ++i) {
1232 isl_id *id;
1234 if (isl_ast_build_has_affine_value(build, i)) {
1235 skip++;
1236 continue;
1239 id = isl_ast_build_get_iterator_id(build, i);
1240 space = isl_space_set_dim_id(space, isl_dim_set, i - skip, id);
1243 return space;
1246 /* Return the current schedule, as stored in build->executed, in terms
1247 * of the external schedule domain.
1249 __isl_give isl_union_map *isl_ast_build_get_schedule(
1250 __isl_keep isl_ast_build *build)
1252 isl_bool needs_map;
1253 isl_union_map *executed;
1254 isl_union_map *schedule;
1256 needs_map = isl_ast_build_need_schedule_map(build);
1257 if (needs_map < 0)
1258 return NULL;
1260 executed = isl_union_map_copy(build->executed);
1261 if (needs_map) {
1262 isl_map *proj = isl_ast_build_get_schedule_map(build);
1263 executed = isl_union_map_apply_domain(executed,
1264 isl_union_map_from_map(proj));
1266 schedule = isl_union_map_reverse(executed);
1268 return schedule;
1271 /* Return the iterator attached to the internal schedule dimension "pos".
1273 __isl_give isl_id *isl_ast_build_get_iterator_id(
1274 __isl_keep isl_ast_build *build, int pos)
1276 if (!build)
1277 return NULL;
1279 return isl_id_list_get_id(build->iterators, pos);
1282 /* Set the stride and offset of the current dimension to the given
1283 * value and expression.
1285 static __isl_give isl_ast_build *set_stride(__isl_take isl_ast_build *build,
1286 __isl_take isl_val *stride, __isl_take isl_aff *offset)
1288 int pos;
1290 build = isl_ast_build_cow(build);
1291 if (!build || !stride || !offset)
1292 goto error;
1294 pos = build->depth;
1296 build->strides = isl_vec_set_element_val(build->strides, pos, stride);
1297 build->offsets = isl_multi_aff_set_aff(build->offsets, pos, offset);
1298 if (!build->strides || !build->offsets)
1299 return isl_ast_build_free(build);
1301 return build;
1302 error:
1303 isl_val_free(stride);
1304 isl_aff_free(offset);
1305 return isl_ast_build_free(build);
1308 /* Return a set expressing the stride constraint at the current depth.
1310 * In particular, if the current iterator (i) is known to attain values
1312 * f + s a
1314 * where f is the offset and s is the stride, then the returned set
1315 * expresses the constraint
1317 * (f - i) mod s = 0
1319 __isl_give isl_set *isl_ast_build_get_stride_constraint(
1320 __isl_keep isl_ast_build *build)
1322 isl_aff *aff;
1323 isl_set *set;
1324 isl_val *stride;
1325 int pos;
1327 if (!build)
1328 return NULL;
1330 pos = build->depth;
1332 if (!isl_ast_build_has_stride(build, pos))
1333 return isl_set_universe(isl_ast_build_get_space(build, 1));
1335 stride = isl_ast_build_get_stride(build, pos);
1336 aff = isl_ast_build_get_offset(build, pos);
1337 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, -1);
1338 aff = isl_aff_mod_val(aff, stride);
1339 set = isl_set_from_basic_set(isl_aff_zero_basic_set(aff));
1341 return set;
1344 /* Return the expansion implied by the stride and offset at the current
1345 * depth.
1347 * That is, return the mapping
1349 * [i_0, ..., i_{d-1}, i_d, i_{d+1}, ...]
1350 * -> [i_0, ..., i_{d-1}, s * i_d + offset(i), i_{d+1}, ...]
1352 * where s is the stride at the current depth d and offset(i) is
1353 * the corresponding offset.
1355 __isl_give isl_multi_aff *isl_ast_build_get_stride_expansion(
1356 __isl_keep isl_ast_build *build)
1358 isl_space *space;
1359 isl_multi_aff *ma;
1360 int pos;
1361 isl_aff *aff, *offset;
1362 isl_val *stride;
1364 if (!build)
1365 return NULL;
1367 pos = isl_ast_build_get_depth(build);
1368 space = isl_ast_build_get_space(build, 1);
1369 space = isl_space_map_from_set(space);
1370 ma = isl_multi_aff_identity(space);
1372 if (!isl_ast_build_has_stride(build, pos))
1373 return ma;
1375 offset = isl_ast_build_get_offset(build, pos);
1376 stride = isl_ast_build_get_stride(build, pos);
1377 aff = isl_multi_aff_get_aff(ma, pos);
1378 aff = isl_aff_scale_val(aff, stride);
1379 aff = isl_aff_add(aff, offset);
1380 ma = isl_multi_aff_set_aff(ma, pos, aff);
1382 return ma;
1385 /* Add constraints corresponding to any previously detected
1386 * stride on the current dimension to build->domain.
1388 __isl_give isl_ast_build *isl_ast_build_include_stride(
1389 __isl_take isl_ast_build *build)
1391 isl_set *set;
1393 if (!build)
1394 return NULL;
1395 if (!isl_ast_build_has_stride(build, build->depth))
1396 return build;
1397 build = isl_ast_build_cow(build);
1398 if (!build)
1399 return NULL;
1401 set = isl_ast_build_get_stride_constraint(build);
1403 build->domain = isl_set_intersect(build->domain, isl_set_copy(set));
1404 build->generated = isl_set_intersect(build->generated, set);
1405 if (!build->domain || !build->generated)
1406 return isl_ast_build_free(build);
1408 return build;
1411 /* Check if the constraints in "set" imply any stride on the current
1412 * dimension and, if so, record the stride information in "build"
1413 * and return the updated "build".
1415 * We assume that inner dimensions have been eliminated from "set"
1416 * by the caller. This is needed because the common stride
1417 * may be imposed by different inner dimensions on different parts of
1418 * the domain.
1419 * The assumption ensures that the lower bound does not depend
1420 * on inner dimensions.
1422 __isl_give isl_ast_build *isl_ast_build_detect_strides(
1423 __isl_take isl_ast_build *build, __isl_take isl_set *set)
1425 int pos;
1426 isl_bool no_stride;
1427 isl_val *stride;
1428 isl_aff *offset;
1429 isl_stride_info *si;
1431 if (!build)
1432 goto error;
1434 pos = isl_ast_build_get_depth(build);
1435 si = isl_set_get_stride_info(set, pos);
1436 stride = isl_stride_info_get_stride(si);
1437 offset = isl_stride_info_get_offset(si);
1438 isl_stride_info_free(si);
1439 isl_set_free(set);
1441 no_stride = isl_val_is_one(stride);
1442 if (no_stride >= 0 && !no_stride)
1443 return set_stride(build, stride, offset);
1444 isl_val_free(stride);
1445 isl_aff_free(offset);
1446 if (no_stride < 0)
1447 return isl_ast_build_free(build);
1448 return build;
1449 error:
1450 isl_set_free(set);
1451 return NULL;
1454 struct isl_ast_build_involves_data {
1455 int depth;
1456 int involves;
1459 /* Check if "map" involves the input dimension data->depth.
1461 static isl_stat involves_depth(__isl_take isl_map *map, void *user)
1463 struct isl_ast_build_involves_data *data = user;
1465 data->involves = isl_map_involves_dims(map, isl_dim_in, data->depth, 1);
1466 isl_map_free(map);
1468 if (data->involves < 0 || data->involves)
1469 return isl_stat_error;
1470 return isl_stat_ok;
1473 /* Do any options depend on the value of the dimension at the current depth?
1475 int isl_ast_build_options_involve_depth(__isl_keep isl_ast_build *build)
1477 struct isl_ast_build_involves_data data;
1479 if (!build)
1480 return -1;
1482 data.depth = build->depth;
1483 data.involves = 0;
1485 if (isl_union_map_foreach_map(build->options,
1486 &involves_depth, &data) < 0) {
1487 if (data.involves < 0 || !data.involves)
1488 return -1;
1491 return data.involves;
1494 /* Construct the map
1496 * { [i] -> [i] : i < pos; [i] -> [i + 1] : i >= pos }
1498 * with "space" the parameter space of the constructed map.
1500 static __isl_give isl_map *construct_insertion_map(__isl_take isl_space *space,
1501 int pos)
1503 isl_constraint *c;
1504 isl_basic_map *bmap1, *bmap2;
1506 space = isl_space_set_from_params(space);
1507 space = isl_space_add_dims(space, isl_dim_set, 1);
1508 space = isl_space_map_from_set(space);
1509 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
1510 c = isl_constraint_set_coefficient_si(c, isl_dim_in, 0, 1);
1511 c = isl_constraint_set_coefficient_si(c, isl_dim_out, 0, -1);
1512 bmap1 = isl_basic_map_from_constraint(isl_constraint_copy(c));
1513 c = isl_constraint_set_constant_si(c, 1);
1514 bmap2 = isl_basic_map_from_constraint(c);
1516 bmap1 = isl_basic_map_upper_bound_si(bmap1, isl_dim_in, 0, pos - 1);
1517 bmap2 = isl_basic_map_lower_bound_si(bmap2, isl_dim_in, 0, pos);
1519 return isl_basic_map_union(bmap1, bmap2);
1522 static const char *option_str[] = {
1523 [isl_ast_loop_atomic] = "atomic",
1524 [isl_ast_loop_unroll] = "unroll",
1525 [isl_ast_loop_separate] = "separate"
1528 /* Update the "options" to reflect the insertion of a dimension
1529 * at position "pos" in the schedule domain space.
1530 * "space" is the original domain space before the insertion and
1531 * may be named and/or structured.
1533 * The (relevant) input options all have "space" as domain, which
1534 * has to be mapped to the extended space.
1535 * The values of the ranges also refer to the schedule domain positions
1536 * and they therefore also need to be adjusted. In particular, values
1537 * smaller than pos do not need to change, while values greater than or
1538 * equal to pos need to be incremented.
1539 * That is, we need to apply the following map.
1541 * { atomic[i] -> atomic[i] : i < pos; [i] -> [i + 1] : i >= pos;
1542 * unroll[i] -> unroll[i] : i < pos; [i] -> [i + 1] : i >= pos;
1543 * separate[i] -> separate[i] : i < pos; [i] -> [i + 1] : i >= pos;
1544 * separation_class[[i] -> [c]]
1545 * -> separation_class[[i] -> [c]] : i < pos;
1546 * separation_class[[i] -> [c]]
1547 * -> separation_class[[i + 1] -> [c]] : i >= pos }
1549 static __isl_give isl_union_map *options_insert_dim(
1550 __isl_take isl_union_map *options, __isl_take isl_space *space, int pos)
1552 isl_map *map;
1553 isl_union_map *insertion;
1554 enum isl_ast_loop_type type;
1555 const char *name = "separation_class";
1557 space = isl_space_map_from_set(space);
1558 map = isl_map_identity(space);
1559 map = isl_map_insert_dims(map, isl_dim_out, pos, 1);
1560 options = isl_union_map_apply_domain(options,
1561 isl_union_map_from_map(map));
1563 if (!options)
1564 return NULL;
1566 map = construct_insertion_map(isl_union_map_get_space(options), pos);
1568 insertion = isl_union_map_empty(isl_union_map_get_space(options));
1570 for (type = isl_ast_loop_atomic;
1571 type <= isl_ast_loop_separate; ++type) {
1572 isl_map *map_type = isl_map_copy(map);
1573 const char *name = option_str[type];
1574 map_type = isl_map_set_tuple_name(map_type, isl_dim_in, name);
1575 map_type = isl_map_set_tuple_name(map_type, isl_dim_out, name);
1576 insertion = isl_union_map_add_map(insertion, map_type);
1579 map = isl_map_product(map, isl_map_identity(isl_map_get_space(map)));
1580 map = isl_map_set_tuple_name(map, isl_dim_in, name);
1581 map = isl_map_set_tuple_name(map, isl_dim_out, name);
1582 insertion = isl_union_map_add_map(insertion, map);
1584 options = isl_union_map_apply_range(options, insertion);
1586 return options;
1589 /* If we are generating an AST from a schedule tree (build->node is set),
1590 * then update the loop AST generation types
1591 * to reflect the insertion of a dimension at (global) position "pos"
1592 * in the schedule domain space.
1593 * We do not need to adjust any isolate option since we would not be inserting
1594 * any dimensions if there were any isolate option.
1596 static __isl_give isl_ast_build *node_insert_dim(
1597 __isl_take isl_ast_build *build, int pos)
1599 int i;
1600 int local_pos;
1601 enum isl_ast_loop_type *loop_type;
1602 isl_ctx *ctx;
1604 build = isl_ast_build_cow(build);
1605 if (!build)
1606 return NULL;
1607 if (!build->node)
1608 return build;
1610 ctx = isl_ast_build_get_ctx(build);
1611 local_pos = pos - build->outer_pos;
1612 loop_type = isl_realloc_array(ctx, build->loop_type,
1613 enum isl_ast_loop_type, build->n + 1);
1614 if (!loop_type)
1615 return isl_ast_build_free(build);
1616 build->loop_type = loop_type;
1617 for (i = build->n - 1; i >= local_pos; --i)
1618 loop_type[i + 1] = loop_type[i];
1619 loop_type[local_pos] = isl_ast_loop_default;
1620 build->n++;
1622 return build;
1625 /* Insert a single dimension in the schedule domain at position "pos".
1626 * The new dimension is given an isl_id with the empty string as name.
1628 * The main difficulty is updating build->options to reflect the
1629 * extra dimension. This is handled in options_insert_dim.
1631 * Note that because of the dimension manipulations, the resulting
1632 * schedule domain space will always be unnamed and unstructured.
1633 * However, the original schedule domain space may be named and/or
1634 * structured, so we have to take this possibility into account
1635 * while performing the transformations.
1637 * Since the inserted schedule dimension is used by the caller
1638 * to differentiate between different domain spaces, there is
1639 * no longer a uniform mapping from the internal schedule space
1640 * to the input schedule space. The internal2input mapping is
1641 * therefore removed.
1643 __isl_give isl_ast_build *isl_ast_build_insert_dim(
1644 __isl_take isl_ast_build *build, int pos)
1646 isl_ctx *ctx;
1647 isl_space *space, *ma_space;
1648 isl_id *id;
1649 isl_multi_aff *ma;
1651 build = isl_ast_build_cow(build);
1652 if (!build)
1653 return NULL;
1655 ctx = isl_ast_build_get_ctx(build);
1656 id = isl_id_alloc(ctx, "", NULL);
1657 if (!build->node)
1658 space = isl_ast_build_get_space(build, 1);
1659 build->iterators = isl_id_list_insert(build->iterators, pos, id);
1660 build->domain = isl_set_insert_dims(build->domain,
1661 isl_dim_set, pos, 1);
1662 build->generated = isl_set_insert_dims(build->generated,
1663 isl_dim_set, pos, 1);
1664 build->pending = isl_set_insert_dims(build->pending,
1665 isl_dim_set, pos, 1);
1666 build->strides = isl_vec_insert_els(build->strides, pos, 1);
1667 build->strides = isl_vec_set_element_si(build->strides, pos, 1);
1668 ma_space = isl_space_params(isl_multi_aff_get_space(build->offsets));
1669 ma_space = isl_space_set_from_params(ma_space);
1670 ma_space = isl_space_add_dims(ma_space, isl_dim_set, 1);
1671 ma_space = isl_space_map_from_set(ma_space);
1672 ma = isl_multi_aff_zero(isl_space_copy(ma_space));
1673 build->offsets = isl_multi_aff_splice(build->offsets, pos, pos, ma);
1674 ma = isl_multi_aff_identity(ma_space);
1675 build->values = isl_multi_aff_splice(build->values, pos, pos, ma);
1676 if (!build->node)
1677 build->options = options_insert_dim(build->options, space, pos);
1678 build->internal2input = isl_multi_aff_free(build->internal2input);
1680 if (!build->iterators || !build->domain || !build->generated ||
1681 !build->pending || !build->values ||
1682 !build->strides || !build->offsets || !build->options)
1683 return isl_ast_build_free(build);
1685 build = node_insert_dim(build, pos);
1687 return build;
1690 /* Scale down the current dimension by a factor of "m".
1691 * "umap" is an isl_union_map that implements the scaling down.
1692 * That is, it is of the form
1694 * { [.... i ....] -> [.... i' ....] : i = m i' }
1696 * This function is called right after the strides have been
1697 * detected, but before any constraints on the current dimension
1698 * have been included in build->domain.
1699 * We therefore only need to update stride, offset, the options and
1700 * the mapping from internal schedule space to the original schedule
1701 * space, if we are still keeping track of such a mapping.
1702 * The latter mapping is updated by plugging in
1703 * { [... i ...] -> [... m i ... ] }.
1705 __isl_give isl_ast_build *isl_ast_build_scale_down(
1706 __isl_take isl_ast_build *build, __isl_take isl_val *m,
1707 __isl_take isl_union_map *umap)
1709 isl_aff *aff;
1710 isl_val *v;
1711 int depth;
1713 build = isl_ast_build_cow(build);
1714 if (!build || !umap || !m)
1715 goto error;
1717 depth = build->depth;
1719 if (build->internal2input) {
1720 isl_space *space;
1721 isl_multi_aff *ma;
1722 isl_aff *aff;
1724 space = isl_multi_aff_get_space(build->internal2input);
1725 space = isl_space_map_from_set(isl_space_domain(space));
1726 ma = isl_multi_aff_identity(space);
1727 aff = isl_multi_aff_get_aff(ma, depth);
1728 aff = isl_aff_scale_val(aff, isl_val_copy(m));
1729 ma = isl_multi_aff_set_aff(ma, depth, aff);
1730 build->internal2input =
1731 isl_multi_aff_pullback_multi_aff(build->internal2input, ma);
1732 if (!build->internal2input)
1733 goto error;
1736 v = isl_vec_get_element_val(build->strides, depth);
1737 v = isl_val_div(v, isl_val_copy(m));
1738 build->strides = isl_vec_set_element_val(build->strides, depth, v);
1740 aff = isl_multi_aff_get_aff(build->offsets, depth);
1741 aff = isl_aff_scale_down_val(aff, m);
1742 build->offsets = isl_multi_aff_set_aff(build->offsets, depth, aff);
1743 build->options = isl_union_map_apply_domain(build->options, umap);
1744 if (!build->strides || !build->offsets || !build->options)
1745 return isl_ast_build_free(build);
1747 return build;
1748 error:
1749 isl_val_free(m);
1750 isl_union_map_free(umap);
1751 return isl_ast_build_free(build);
1754 /* Return a list of "n" isl_ids called "c%d", with "%d" starting at "first".
1755 * If an isl_id with such a name already appears among the parameters
1756 * in build->domain, then adjust the name to "c%d_%d".
1758 static __isl_give isl_id_list *generate_names(isl_ctx *ctx, int n, int first,
1759 __isl_keep isl_ast_build *build)
1761 int i;
1762 isl_id_list *names;
1764 names = isl_id_list_alloc(ctx, n);
1765 for (i = 0; i < n; ++i) {
1766 isl_id *id;
1768 id = generate_name(ctx, first + i, build);
1769 names = isl_id_list_add(names, id);
1772 return names;
1775 /* Embed "options" into the given isl_ast_build space.
1777 * This function is called from within a nested call to
1778 * isl_ast_build_node_from_schedule_map.
1779 * "options" refers to the additional schedule,
1780 * while space refers to both the space of the outer isl_ast_build and
1781 * that of the additional schedule.
1782 * Specifically, space is of the form
1784 * [I -> S]
1786 * while options lives in the space(s)
1788 * S -> *
1790 * We compute
1792 * [I -> S] -> S
1794 * and compose this with options, to obtain the new options
1795 * living in the space(s)
1797 * [I -> S] -> *
1799 static __isl_give isl_union_map *embed_options(
1800 __isl_take isl_union_map *options, __isl_take isl_space *space)
1802 isl_map *map;
1804 map = isl_map_universe(isl_space_unwrap(space));
1805 map = isl_map_range_map(map);
1807 options = isl_union_map_apply_range(
1808 isl_union_map_from_map(map), options);
1810 return options;
1813 /* Update "build" for use in a (possibly nested) code generation. That is,
1814 * extend "build" from an AST build on some domain O to an AST build
1815 * on domain [O -> S], with S corresponding to "space".
1816 * If the original domain is a parameter domain, then the new domain is
1817 * simply S.
1818 * "iterators" is a list of iterators for S, but the number of elements
1819 * may be smaller or greater than the number of set dimensions of S.
1820 * If "keep_iterators" is set, then any extra ids in build->iterators
1821 * are reused for S. Otherwise, these extra ids are dropped.
1823 * We first update build->outer_pos to the current depth.
1824 * This depth is zero in case this is the outermost code generation.
1826 * We then add additional ids such that the number of iterators is at least
1827 * equal to the dimension of the new build domain.
1829 * If the original domain is parametric, then we are constructing
1830 * an isl_ast_build for the outer code generation and we pass control
1831 * to isl_ast_build_init.
1833 * Otherwise, we adjust the fields of "build" to include "space".
1835 __isl_give isl_ast_build *isl_ast_build_product(
1836 __isl_take isl_ast_build *build, __isl_take isl_space *space)
1838 isl_ctx *ctx;
1839 isl_vec *strides;
1840 isl_set *set;
1841 isl_multi_aff *embedding;
1842 int dim, n_it;
1844 build = isl_ast_build_cow(build);
1845 if (!build)
1846 goto error;
1848 build->outer_pos = build->depth;
1850 ctx = isl_ast_build_get_ctx(build);
1851 dim = isl_ast_build_dim(build, isl_dim_set);
1852 dim += isl_space_dim(space, isl_dim_set);
1853 n_it = isl_id_list_n_id(build->iterators);
1854 if (n_it < dim) {
1855 isl_id_list *l;
1856 l = generate_names(ctx, dim - n_it, n_it, build);
1857 build->iterators = isl_id_list_concat(build->iterators, l);
1860 if (isl_set_is_params(build->domain))
1861 return isl_ast_build_init(build, space);
1863 set = isl_set_universe(isl_space_copy(space));
1864 build->domain = isl_set_product(build->domain, isl_set_copy(set));
1865 build->pending = isl_set_product(build->pending, isl_set_copy(set));
1866 build->generated = isl_set_product(build->generated, set);
1868 strides = isl_vec_alloc(ctx, isl_space_dim(space, isl_dim_set));
1869 strides = isl_vec_set_si(strides, 1);
1870 build->strides = isl_vec_concat(build->strides, strides);
1872 space = isl_space_map_from_set(space);
1873 build->offsets = isl_multi_aff_align_params(build->offsets,
1874 isl_space_copy(space));
1875 build->offsets = isl_multi_aff_product(build->offsets,
1876 isl_multi_aff_zero(isl_space_copy(space)));
1877 build->values = isl_multi_aff_align_params(build->values,
1878 isl_space_copy(space));
1879 embedding = isl_multi_aff_identity(space);
1880 build->values = isl_multi_aff_product(build->values,
1881 isl_multi_aff_copy(embedding));
1882 if (build->internal2input) {
1883 build->internal2input =
1884 isl_multi_aff_product(build->internal2input, embedding);
1885 build->internal2input =
1886 isl_multi_aff_flatten_range(build->internal2input);
1887 if (!build->internal2input)
1888 return isl_ast_build_free(build);
1889 } else {
1890 isl_multi_aff_free(embedding);
1893 space = isl_ast_build_get_space(build, 1);
1894 build->options = embed_options(build->options, space);
1896 if (!build->iterators || !build->domain || !build->generated ||
1897 !build->pending || !build->values ||
1898 !build->strides || !build->offsets || !build->options)
1899 return isl_ast_build_free(build);
1901 return build;
1902 error:
1903 isl_ast_build_free(build);
1904 isl_space_free(space);
1905 return NULL;
1908 /* Does "aff" only attain non-negative values over build->domain?
1909 * That is, does it not attain any negative values?
1911 int isl_ast_build_aff_is_nonneg(__isl_keep isl_ast_build *build,
1912 __isl_keep isl_aff *aff)
1914 isl_set *test;
1915 int empty;
1917 if (!build)
1918 return -1;
1920 aff = isl_aff_copy(aff);
1921 test = isl_set_from_basic_set(isl_aff_neg_basic_set(aff));
1922 test = isl_set_intersect(test, isl_set_copy(build->domain));
1923 empty = isl_set_is_empty(test);
1924 isl_set_free(test);
1926 return empty;
1929 /* Does the dimension at (internal) position "pos" have a non-trivial stride?
1931 isl_bool isl_ast_build_has_stride(__isl_keep isl_ast_build *build, int pos)
1933 isl_val *v;
1934 isl_bool has_stride;
1936 if (!build)
1937 return isl_bool_error;
1939 v = isl_vec_get_element_val(build->strides, pos);
1940 has_stride = isl_bool_not(isl_val_is_one(v));
1941 isl_val_free(v);
1943 return has_stride;
1946 /* Given that the dimension at position "pos" takes on values
1948 * f + s a
1950 * with a an integer, return s through *stride.
1952 __isl_give isl_val *isl_ast_build_get_stride(__isl_keep isl_ast_build *build,
1953 int pos)
1955 if (!build)
1956 return NULL;
1958 return isl_vec_get_element_val(build->strides, pos);
1961 /* Given that the dimension at position "pos" takes on values
1963 * f + s a
1965 * with a an integer, return f.
1967 __isl_give isl_aff *isl_ast_build_get_offset(
1968 __isl_keep isl_ast_build *build, int pos)
1970 if (!build)
1971 return NULL;
1973 return isl_multi_aff_get_aff(build->offsets, pos);
1976 /* Is the dimension at position "pos" known to attain only a single
1977 * value that, moreover, can be described by a single affine expression
1978 * in terms of the outer dimensions and parameters?
1980 * If not, then the corresponding affine expression in build->values
1981 * is set to be equal to the same input dimension.
1982 * Otherwise, it is set to the requested expression in terms of
1983 * outer dimensions and parameters.
1985 isl_bool isl_ast_build_has_affine_value(__isl_keep isl_ast_build *build,
1986 int pos)
1988 isl_aff *aff;
1989 isl_bool involves;
1991 if (!build)
1992 return isl_bool_error;
1994 aff = isl_multi_aff_get_aff(build->values, pos);
1995 involves = isl_aff_involves_dims(aff, isl_dim_in, pos, 1);
1996 isl_aff_free(aff);
1998 return isl_bool_not(involves);
2001 /* Plug in the known values (fixed affine expressions in terms of
2002 * parameters and outer loop iterators) of all loop iterators
2003 * in the domain of "umap".
2005 * We simply precompose "umap" with build->values.
2007 __isl_give isl_union_map *isl_ast_build_substitute_values_union_map_domain(
2008 __isl_keep isl_ast_build *build, __isl_take isl_union_map *umap)
2010 isl_multi_aff *values;
2012 if (!build)
2013 return isl_union_map_free(umap);
2015 values = isl_multi_aff_copy(build->values);
2016 umap = isl_union_map_preimage_domain_multi_aff(umap, values);
2018 return umap;
2021 /* Is the current dimension known to attain only a single value?
2023 int isl_ast_build_has_value(__isl_keep isl_ast_build *build)
2025 if (!build)
2026 return -1;
2028 return build->value != NULL;
2031 /* Simplify the basic set "bset" based on what we know about
2032 * the iterators of already generated loops.
2034 * "bset" is assumed to live in the (internal) schedule domain.
2036 __isl_give isl_basic_set *isl_ast_build_compute_gist_basic_set(
2037 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
2039 if (!build)
2040 goto error;
2042 bset = isl_basic_set_preimage_multi_aff(bset,
2043 isl_multi_aff_copy(build->values));
2044 bset = isl_basic_set_gist(bset,
2045 isl_set_simple_hull(isl_set_copy(build->domain)));
2047 return bset;
2048 error:
2049 isl_basic_set_free(bset);
2050 return NULL;
2053 /* Simplify the set "set" based on what we know about
2054 * the iterators of already generated loops.
2056 * "set" is assumed to live in the (internal) schedule domain.
2058 __isl_give isl_set *isl_ast_build_compute_gist(
2059 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2061 if (!build)
2062 goto error;
2064 if (!isl_set_is_params(set))
2065 set = isl_set_preimage_multi_aff(set,
2066 isl_multi_aff_copy(build->values));
2067 set = isl_set_gist(set, isl_set_copy(build->domain));
2069 return set;
2070 error:
2071 isl_set_free(set);
2072 return NULL;
2075 /* Include information about what we know about the iterators of
2076 * already generated loops to "set".
2078 * We currently only plug in the known affine values of outer loop
2079 * iterators.
2080 * In principle we could also introduce equalities or even other
2081 * constraints implied by the intersection of "set" and build->domain.
2083 __isl_give isl_set *isl_ast_build_specialize(__isl_keep isl_ast_build *build,
2084 __isl_take isl_set *set)
2086 if (!build)
2087 return isl_set_free(set);
2089 return isl_set_preimage_multi_aff(set,
2090 isl_multi_aff_copy(build->values));
2093 /* Plug in the known affine values of outer loop iterators in "bset".
2095 __isl_give isl_basic_set *isl_ast_build_specialize_basic_set(
2096 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
2098 if (!build)
2099 return isl_basic_set_free(bset);
2101 return isl_basic_set_preimage_multi_aff(bset,
2102 isl_multi_aff_copy(build->values));
2105 /* Simplify the map "map" based on what we know about
2106 * the iterators of already generated loops.
2108 * The domain of "map" is assumed to live in the (internal) schedule domain.
2110 __isl_give isl_map *isl_ast_build_compute_gist_map_domain(
2111 __isl_keep isl_ast_build *build, __isl_take isl_map *map)
2113 if (!build)
2114 goto error;
2116 map = isl_map_gist_domain(map, isl_set_copy(build->domain));
2118 return map;
2119 error:
2120 isl_map_free(map);
2121 return NULL;
2124 /* Simplify the affine expression "aff" based on what we know about
2125 * the iterators of already generated loops.
2127 * The domain of "aff" is assumed to live in the (internal) schedule domain.
2129 __isl_give isl_aff *isl_ast_build_compute_gist_aff(
2130 __isl_keep isl_ast_build *build, __isl_take isl_aff *aff)
2132 if (!build)
2133 goto error;
2135 aff = isl_aff_gist(aff, isl_set_copy(build->domain));
2137 return aff;
2138 error:
2139 isl_aff_free(aff);
2140 return NULL;
2143 /* Simplify the piecewise affine expression "aff" based on what we know about
2144 * the iterators of already generated loops.
2146 * The domain of "pa" is assumed to live in the (internal) schedule domain.
2148 __isl_give isl_pw_aff *isl_ast_build_compute_gist_pw_aff(
2149 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2151 if (!build)
2152 goto error;
2154 if (!isl_set_is_params(build->domain))
2155 pa = isl_pw_aff_pullback_multi_aff(pa,
2156 isl_multi_aff_copy(build->values));
2157 pa = isl_pw_aff_gist(pa, isl_set_copy(build->domain));
2159 return pa;
2160 error:
2161 isl_pw_aff_free(pa);
2162 return NULL;
2165 /* Simplify the piecewise multi-affine expression "aff" based on what
2166 * we know about the iterators of already generated loops.
2168 * The domain of "pma" is assumed to live in the (internal) schedule domain.
2170 __isl_give isl_pw_multi_aff *isl_ast_build_compute_gist_pw_multi_aff(
2171 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
2173 if (!build)
2174 goto error;
2176 pma = isl_pw_multi_aff_pullback_multi_aff(pma,
2177 isl_multi_aff_copy(build->values));
2178 pma = isl_pw_multi_aff_gist(pma, isl_set_copy(build->domain));
2180 return pma;
2181 error:
2182 isl_pw_multi_aff_free(pma);
2183 return NULL;
2186 /* Extract the schedule domain of the given type from build->options
2187 * at the current depth.
2189 * In particular, find the subset of build->options that is of
2190 * the following form
2192 * schedule_domain -> type[depth]
2194 * and return the corresponding domain, after eliminating inner dimensions
2195 * and divs that depend on the current dimension.
2197 * Note that the domain of build->options has been reformulated
2198 * in terms of the internal build space in embed_options,
2199 * but the position is still that within the current code generation.
2201 __isl_give isl_set *isl_ast_build_get_option_domain(
2202 __isl_keep isl_ast_build *build, enum isl_ast_loop_type type)
2204 const char *name;
2205 isl_space *space;
2206 isl_map *option;
2207 isl_set *domain;
2208 int local_pos;
2210 if (!build)
2211 return NULL;
2213 name = option_str[type];
2214 local_pos = build->depth - build->outer_pos;
2216 space = isl_ast_build_get_space(build, 1);
2217 space = isl_space_from_domain(space);
2218 space = isl_space_add_dims(space, isl_dim_out, 1);
2219 space = isl_space_set_tuple_name(space, isl_dim_out, name);
2221 option = isl_union_map_extract_map(build->options, space);
2222 option = isl_map_fix_si(option, isl_dim_out, 0, local_pos);
2224 domain = isl_map_domain(option);
2225 domain = isl_ast_build_eliminate(build, domain);
2227 return domain;
2230 /* How does the user want the current schedule dimension to be generated?
2231 * These choices have been extracted from the schedule node
2232 * in extract_loop_types and stored in build->loop_type.
2233 * They have been updated to reflect any dimension insertion in
2234 * node_insert_dim.
2235 * Return isl_ast_domain_error on error.
2237 * If "isolated" is set, then we get the loop AST generation type
2238 * directly from the band node since node_insert_dim cannot have been
2239 * called on a band with the isolate option.
2241 enum isl_ast_loop_type isl_ast_build_get_loop_type(
2242 __isl_keep isl_ast_build *build, int isolated)
2244 int local_pos;
2245 isl_ctx *ctx;
2247 if (!build)
2248 return isl_ast_loop_error;
2249 ctx = isl_ast_build_get_ctx(build);
2250 if (!build->node)
2251 isl_die(ctx, isl_error_internal,
2252 "only works for schedule tree based AST generation",
2253 return isl_ast_loop_error);
2255 local_pos = build->depth - build->outer_pos;
2256 if (!isolated)
2257 return build->loop_type[local_pos];
2258 return isl_schedule_node_band_member_get_isolate_ast_loop_type(
2259 build->node, local_pos);
2262 /* Extract the isolated set from the isolate option, if any,
2263 * and store in the build.
2264 * If there is no isolate option, then the isolated set is
2265 * set to the empty set.
2267 * The isolate option is of the form
2269 * isolate[[outer bands] -> current_band]
2271 * We flatten this set and then map it back to the internal
2272 * schedule space.
2274 * If we have already extracted the isolated set
2275 * or if internal2input is no longer set, then we do not
2276 * need to do anything. In the latter case, we know
2277 * that the current band cannot have any isolate option.
2279 __isl_give isl_ast_build *isl_ast_build_extract_isolated(
2280 __isl_take isl_ast_build *build)
2282 isl_set *isolated;
2284 if (!build)
2285 return NULL;
2286 if (!build->internal2input)
2287 return build;
2288 if (build->isolated)
2289 return build;
2291 build = isl_ast_build_cow(build);
2292 if (!build)
2293 return NULL;
2295 isolated = isl_schedule_node_band_get_ast_isolate_option(build->node);
2296 isolated = isl_set_flatten(isolated);
2297 isolated = isl_set_preimage_multi_aff(isolated,
2298 isl_multi_aff_copy(build->internal2input));
2300 build->isolated = isolated;
2301 if (!build->isolated)
2302 return isl_ast_build_free(build);
2304 return build;
2307 /* Does "build" have a non-empty isolated set?
2309 * The caller is assumed to have called isl_ast_build_extract_isolated first.
2311 int isl_ast_build_has_isolated(__isl_keep isl_ast_build *build)
2313 int empty;
2315 if (!build)
2316 return -1;
2317 if (!build->internal2input)
2318 return 0;
2319 if (!build->isolated)
2320 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
2321 "isolated set not extracted yet", return -1);
2323 empty = isl_set_plain_is_empty(build->isolated);
2324 return empty < 0 ? -1 : !empty;
2327 /* Return a copy of the isolated set of "build".
2329 * The caller is assume to have called isl_ast_build_has_isolated first,
2330 * with this function returning true.
2331 * In particular, this function should not be called if we are no
2332 * longer keeping track of internal2input (and there therefore could
2333 * not possibly be any isolated set).
2335 __isl_give isl_set *isl_ast_build_get_isolated(__isl_keep isl_ast_build *build)
2337 if (!build)
2338 return NULL;
2339 if (!build->internal2input)
2340 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
2341 "build cannot have isolated set", return NULL);
2343 return isl_set_copy(build->isolated);
2346 /* Extract the separation class mapping at the current depth.
2348 * In particular, find and return the subset of build->options that is of
2349 * the following form
2351 * schedule_domain -> separation_class[[depth] -> [class]]
2353 * The caller is expected to eliminate inner dimensions from the domain.
2355 * Note that the domain of build->options has been reformulated
2356 * in terms of the internal build space in embed_options,
2357 * but the position is still that within the current code generation.
2359 __isl_give isl_map *isl_ast_build_get_separation_class(
2360 __isl_keep isl_ast_build *build)
2362 isl_ctx *ctx;
2363 isl_space *space_sep, *space;
2364 isl_map *res;
2365 int local_pos;
2367 if (!build)
2368 return NULL;
2370 local_pos = build->depth - build->outer_pos;
2371 ctx = isl_ast_build_get_ctx(build);
2372 space_sep = isl_space_alloc(ctx, 0, 1, 1);
2373 space_sep = isl_space_wrap(space_sep);
2374 space_sep = isl_space_set_tuple_name(space_sep, isl_dim_set,
2375 "separation_class");
2376 space = isl_ast_build_get_space(build, 1);
2377 space_sep = isl_space_align_params(space_sep, isl_space_copy(space));
2378 space = isl_space_map_from_domain_and_range(space, space_sep);
2380 res = isl_union_map_extract_map(build->options, space);
2381 res = isl_map_fix_si(res, isl_dim_out, 0, local_pos);
2382 res = isl_map_coalesce(res);
2384 return res;
2387 /* Eliminate dimensions inner to the current dimension.
2389 __isl_give isl_set *isl_ast_build_eliminate_inner(
2390 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2392 int dim;
2393 int depth;
2395 if (!build)
2396 return isl_set_free(set);
2398 dim = isl_set_dim(set, isl_dim_set);
2399 depth = build->depth;
2400 set = isl_set_detect_equalities(set);
2401 set = isl_set_eliminate(set, isl_dim_set, depth + 1, dim - (depth + 1));
2403 return set;
2406 /* Eliminate unknown divs and divs that depend on the current dimension.
2408 * Note that during the elimination of unknown divs, we may discover
2409 * an explicit representation of some other unknown divs, which may
2410 * depend on the current dimension. We therefore need to eliminate
2411 * unknown divs first.
2413 __isl_give isl_set *isl_ast_build_eliminate_divs(
2414 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2416 int depth;
2418 if (!build)
2419 return isl_set_free(set);
2421 set = isl_set_remove_unknown_divs(set);
2422 depth = build->depth;
2423 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, depth, 1);
2425 return set;
2428 /* Eliminate dimensions inner to the current dimension as well as
2429 * unknown divs and divs that depend on the current dimension.
2430 * The result then consists only of constraints that are independent
2431 * of the current dimension and upper and lower bounds on the current
2432 * dimension.
2434 __isl_give isl_set *isl_ast_build_eliminate(
2435 __isl_keep isl_ast_build *build, __isl_take isl_set *domain)
2437 domain = isl_ast_build_eliminate_inner(build, domain);
2438 domain = isl_ast_build_eliminate_divs(build, domain);
2439 return domain;
2442 /* Replace build->single_valued by "sv".
2444 __isl_give isl_ast_build *isl_ast_build_set_single_valued(
2445 __isl_take isl_ast_build *build, int sv)
2447 if (!build)
2448 return build;
2449 if (build->single_valued == sv)
2450 return build;
2451 build = isl_ast_build_cow(build);
2452 if (!build)
2453 return build;
2454 build->single_valued = sv;
2456 return build;