isl_multi_templ.c: extract out isl_multi_product_templ.c
[isl.git] / isl_ast_build.c
blob4cd94c4496630cff663441b1b9ac11da9c3f186c
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_set_dim(build->domain, 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_give int isl_ast_build_need_schedule_map(__isl_keep isl_ast_build *build)
567 int dim;
569 if (!build)
570 return -1;
572 dim = isl_set_dim(build->domain, 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_space *space;
598 isl_multi_aff *ma;
600 if (!build)
601 return NULL;
602 if (build->schedule_map)
603 return isl_multi_aff_copy(build->schedule_map);
605 space = isl_ast_build_get_space(build, 1);
606 space = isl_space_map_from_set(space);
607 ma = isl_multi_aff_identity(space);
608 if (isl_ast_build_need_schedule_map(build)) {
609 int i;
610 int dim = isl_set_dim(build->domain, isl_dim_set);
611 ma = isl_multi_aff_drop_dims(ma, isl_dim_out,
612 build->depth, dim - build->depth);
613 for (i = build->depth - 1; i >= 0; --i)
614 if (isl_ast_build_has_affine_value(build, i))
615 ma = isl_multi_aff_drop_dims(ma,
616 isl_dim_out, i, 1);
619 build->schedule_map = ma;
620 return isl_multi_aff_copy(build->schedule_map);
623 /* Return a mapping from the internal schedule space to the external
624 * schedule space in the form of an isl_map.
626 __isl_give isl_map *isl_ast_build_get_schedule_map(
627 __isl_keep isl_ast_build *build)
629 isl_multi_aff *ma;
631 ma = isl_ast_build_get_schedule_map_multi_aff(build);
632 return isl_map_from_multi_aff(ma);
635 /* Return the position of the dimension in build->domain for which
636 * an AST node is currently being generated.
638 int isl_ast_build_get_depth(__isl_keep isl_ast_build *build)
640 return build ? build->depth : -1;
643 /* Prepare for generating code for the next level.
644 * In particular, increase the depth and reset any information
645 * that is local to the current depth.
647 __isl_give isl_ast_build *isl_ast_build_increase_depth(
648 __isl_take isl_ast_build *build)
650 build = isl_ast_build_cow(build);
651 if (!build)
652 return NULL;
653 build->depth++;
654 isl_ast_build_reset_schedule_map(build);
655 build->value = isl_pw_aff_free(build->value);
656 return build;
659 void isl_ast_build_dump(__isl_keep isl_ast_build *build)
661 if (!build)
662 return;
664 fprintf(stderr, "domain: ");
665 isl_set_dump(build->domain);
666 fprintf(stderr, "generated: ");
667 isl_set_dump(build->generated);
668 fprintf(stderr, "pending: ");
669 isl_set_dump(build->pending);
670 fprintf(stderr, "iterators: ");
671 isl_id_list_dump(build->iterators);
672 fprintf(stderr, "values: ");
673 isl_multi_aff_dump(build->values);
674 if (build->value) {
675 fprintf(stderr, "value: ");
676 isl_pw_aff_dump(build->value);
678 fprintf(stderr, "strides: ");
679 isl_vec_dump(build->strides);
680 fprintf(stderr, "offsets: ");
681 isl_multi_aff_dump(build->offsets);
682 fprintf(stderr, "internal2input: ");
683 isl_multi_aff_dump(build->internal2input);
686 /* Initialize "build" for AST construction in schedule space "space"
687 * in the case that build->domain is a parameter set.
689 * build->iterators is assumed to have been updated already.
691 static __isl_give isl_ast_build *isl_ast_build_init(
692 __isl_take isl_ast_build *build, __isl_take isl_space *space)
694 isl_set *set;
696 build = isl_ast_build_cow(build);
697 if (!build)
698 goto error;
700 set = isl_set_universe(isl_space_copy(space));
701 build->domain = isl_set_intersect_params(isl_set_copy(set),
702 build->domain);
703 build->pending = isl_set_intersect_params(isl_set_copy(set),
704 build->pending);
705 build->generated = isl_set_intersect_params(set, build->generated);
707 return isl_ast_build_init_derived(build, space);
708 error:
709 isl_ast_build_free(build);
710 isl_space_free(space);
711 return NULL;
714 /* Assign "aff" to *user and return -1, effectively extracting
715 * the first (and presumably only) affine expression in the isl_pw_aff
716 * on which this function is used.
718 static isl_stat extract_single_piece(__isl_take isl_set *set,
719 __isl_take isl_aff *aff, void *user)
721 isl_aff **p = user;
723 *p = aff;
724 isl_set_free(set);
726 return isl_stat_error;
729 /* Intersect "set" with the stride constraint of "build", if any.
731 static __isl_give isl_set *intersect_stride_constraint(__isl_take isl_set *set,
732 __isl_keep isl_ast_build *build)
734 isl_set *stride;
736 if (!build)
737 return isl_set_free(set);
738 if (!isl_ast_build_has_stride(build, build->depth))
739 return set;
741 stride = isl_ast_build_get_stride_constraint(build);
742 return isl_set_intersect(set, stride);
745 /* Check if the given bounds on the current dimension (together with
746 * the stride constraint, if any) imply that
747 * this current dimension attains only a single value (in terms of
748 * parameters and outer dimensions).
749 * If so, we record it in build->value.
750 * If, moreover, this value can be represented as a single affine expression,
751 * then we also update build->values, effectively marking the current
752 * dimension as "eliminated".
754 * When computing the gist of the fixed value that can be represented
755 * as a single affine expression, it is important to only take into
756 * account the domain constraints in the original AST build and
757 * not the domain of the affine expression itself.
758 * Otherwise, a [i/3] is changed into a i/3 because we know that i
759 * is a multiple of 3, but then we end up not expressing anywhere
760 * in the context that i is a multiple of 3.
762 static __isl_give isl_ast_build *update_values(
763 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
765 int sv;
766 isl_pw_multi_aff *pma;
767 isl_aff *aff = NULL;
768 isl_map *it_map;
769 isl_set *set;
771 set = isl_set_from_basic_set(bounds);
772 set = isl_set_intersect(set, isl_set_copy(build->domain));
773 set = intersect_stride_constraint(set, build);
774 it_map = isl_ast_build_map_to_iterator(build, set);
776 sv = isl_map_is_single_valued(it_map);
777 if (sv < 0)
778 build = isl_ast_build_free(build);
779 if (!build || !sv) {
780 isl_map_free(it_map);
781 return build;
784 pma = isl_pw_multi_aff_from_map(it_map);
785 build->value = isl_pw_multi_aff_get_pw_aff(pma, 0);
786 build->value = isl_ast_build_compute_gist_pw_aff(build, build->value);
787 build->value = isl_pw_aff_coalesce(build->value);
788 isl_pw_multi_aff_free(pma);
790 if (!build->value)
791 return isl_ast_build_free(build);
793 if (isl_pw_aff_n_piece(build->value) != 1)
794 return build;
796 isl_pw_aff_foreach_piece(build->value, &extract_single_piece, &aff);
798 build->values = isl_multi_aff_set_aff(build->values, build->depth, aff);
799 if (!build->values)
800 return isl_ast_build_free(build);
801 isl_ast_build_reset_schedule_map(build);
802 return build;
805 /* Update the AST build based on the given loop bounds for
806 * the current dimension and the stride information available in the build.
808 * We first make sure that the bounds do not refer to any iterators
809 * that have already been eliminated.
810 * Then, we check if the bounds imply that the current iterator
811 * has a fixed value.
812 * If they do and if this fixed value can be expressed as a single
813 * affine expression, we eliminate the iterators from the bounds.
814 * Note that we cannot simply plug in this single value using
815 * isl_basic_set_preimage_multi_aff as the single value may only
816 * be defined on a subset of the domain. Plugging in the value
817 * would restrict the build domain to this subset, while this
818 * restriction may not be reflected in the generated code.
819 * Finally, we intersect build->domain with the updated bounds.
820 * We also add the stride constraint unless we have been able
821 * to find a fixed value expressed as a single affine expression.
823 * Note that the check for a fixed value in update_values requires
824 * us to intersect the bounds with the current build domain.
825 * When we intersect build->domain with the updated bounds in
826 * the final step, we make sure that these updated bounds have
827 * not been intersected with the old build->domain.
828 * Otherwise, we would indirectly intersect the build domain with itself,
829 * which can lead to inefficiencies, in particular if the build domain
830 * contains any unknown divs.
832 * The pending and generated sets are not updated by this function to
833 * match the updated domain.
834 * The caller still needs to call isl_ast_build_set_pending_generated.
836 __isl_give isl_ast_build *isl_ast_build_set_loop_bounds(
837 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
839 isl_set *set;
841 build = isl_ast_build_cow(build);
842 if (!build)
843 goto error;
845 build = update_values(build, isl_basic_set_copy(bounds));
846 if (!build)
847 goto error;
848 set = isl_set_from_basic_set(isl_basic_set_copy(bounds));
849 if (isl_ast_build_has_affine_value(build, build->depth)) {
850 set = isl_set_eliminate(set, isl_dim_set, build->depth, 1);
851 set = isl_set_compute_divs(set);
852 build->pending = isl_set_intersect(build->pending,
853 isl_set_copy(set));
854 build->domain = isl_set_intersect(build->domain, set);
855 } else {
856 build->domain = isl_set_intersect(build->domain, set);
857 build = isl_ast_build_include_stride(build);
858 if (!build)
859 goto error;
861 isl_basic_set_free(bounds);
863 if (!build->domain || !build->pending || !build->generated)
864 return isl_ast_build_free(build);
866 return build;
867 error:
868 isl_ast_build_free(build);
869 isl_basic_set_free(bounds);
870 return NULL;
873 /* Update the pending and generated sets of "build" according to "bounds".
874 * If the build has an affine value at the current depth,
875 * then isl_ast_build_set_loop_bounds has already set the pending set.
876 * Otherwise, do it here.
878 __isl_give isl_ast_build *isl_ast_build_set_pending_generated(
879 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
881 isl_basic_set *generated, *pending;
883 if (!build)
884 goto error;
886 if (isl_ast_build_has_affine_value(build, build->depth)) {
887 isl_basic_set_free(bounds);
888 return build;
891 build = isl_ast_build_cow(build);
892 if (!build)
893 goto error;
895 pending = isl_basic_set_copy(bounds);
896 pending = isl_basic_set_drop_constraints_involving_dims(pending,
897 isl_dim_set, build->depth, 1);
898 build->pending = isl_set_intersect(build->pending,
899 isl_set_from_basic_set(pending));
900 generated = bounds;
901 generated = isl_basic_set_drop_constraints_not_involving_dims(
902 generated, isl_dim_set, build->depth, 1);
903 build->generated = isl_set_intersect(build->generated,
904 isl_set_from_basic_set(generated));
906 if (!build->pending || !build->generated)
907 return isl_ast_build_free(build);
909 return build;
910 error:
911 isl_ast_build_free(build);
912 isl_basic_set_free(bounds);
913 return NULL;
916 /* Intersect build->domain with "set", where "set" is specified
917 * in terms of the internal schedule domain.
919 static __isl_give isl_ast_build *isl_ast_build_restrict_internal(
920 __isl_take isl_ast_build *build, __isl_take isl_set *set)
922 build = isl_ast_build_cow(build);
923 if (!build)
924 goto error;
926 set = isl_set_compute_divs(set);
927 build->domain = isl_set_intersect(build->domain, set);
928 build->domain = isl_set_coalesce(build->domain);
930 if (!build->domain)
931 return isl_ast_build_free(build);
933 return build;
934 error:
935 isl_ast_build_free(build);
936 isl_set_free(set);
937 return NULL;
940 /* Intersect build->generated and build->domain with "set",
941 * where "set" is specified in terms of the internal schedule domain.
943 __isl_give isl_ast_build *isl_ast_build_restrict_generated(
944 __isl_take isl_ast_build *build, __isl_take isl_set *set)
946 set = isl_set_compute_divs(set);
947 build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
948 build = isl_ast_build_cow(build);
949 if (!build)
950 goto error;
952 build->generated = isl_set_intersect(build->generated, set);
953 build->generated = isl_set_coalesce(build->generated);
955 if (!build->generated)
956 return isl_ast_build_free(build);
958 return build;
959 error:
960 isl_ast_build_free(build);
961 isl_set_free(set);
962 return NULL;
965 /* Replace the set of pending constraints by "guard", which is then
966 * no longer considered as pending.
967 * That is, add "guard" to the generated constraints and clear all pending
968 * constraints, making the domain equal to the generated constraints.
970 __isl_give isl_ast_build *isl_ast_build_replace_pending_by_guard(
971 __isl_take isl_ast_build *build, __isl_take isl_set *guard)
973 build = isl_ast_build_restrict_generated(build, guard);
974 build = isl_ast_build_cow(build);
975 if (!build)
976 return NULL;
978 isl_set_free(build->domain);
979 build->domain = isl_set_copy(build->generated);
980 isl_set_free(build->pending);
981 build->pending = isl_set_universe(isl_set_get_space(build->domain));
983 if (!build->pending)
984 return isl_ast_build_free(build);
986 return build;
989 /* Intersect build->domain with "set", where "set" is specified
990 * in terms of the external schedule domain.
992 __isl_give isl_ast_build *isl_ast_build_restrict(
993 __isl_take isl_ast_build *build, __isl_take isl_set *set)
995 if (isl_set_is_params(set))
996 return isl_ast_build_restrict_generated(build, set);
998 if (isl_ast_build_need_schedule_map(build)) {
999 isl_multi_aff *ma;
1000 ma = isl_ast_build_get_schedule_map_multi_aff(build);
1001 set = isl_set_preimage_multi_aff(set, ma);
1003 return isl_ast_build_restrict_generated(build, set);
1006 /* Replace build->executed by "executed".
1008 __isl_give isl_ast_build *isl_ast_build_set_executed(
1009 __isl_take isl_ast_build *build, __isl_take isl_union_map *executed)
1011 build = isl_ast_build_cow(build);
1012 if (!build)
1013 goto error;
1015 isl_union_map_free(build->executed);
1016 build->executed = executed;
1018 return build;
1019 error:
1020 isl_ast_build_free(build);
1021 isl_union_map_free(executed);
1022 return NULL;
1025 /* Does "build" point to a band node?
1026 * That is, are we currently handling a band node inside a schedule tree?
1028 int isl_ast_build_has_schedule_node(__isl_keep isl_ast_build *build)
1030 if (!build)
1031 return -1;
1032 return build->node != NULL;
1035 /* Return a copy of the band node that "build" refers to.
1037 __isl_give isl_schedule_node *isl_ast_build_get_schedule_node(
1038 __isl_keep isl_ast_build *build)
1040 if (!build)
1041 return NULL;
1042 return isl_schedule_node_copy(build->node);
1045 /* Extract the loop AST generation types for the members of build->node
1046 * and store them in build->loop_type.
1048 static __isl_give isl_ast_build *extract_loop_types(
1049 __isl_take isl_ast_build *build)
1051 int i;
1052 isl_ctx *ctx;
1053 isl_schedule_node *node;
1055 if (!build)
1056 return NULL;
1057 ctx = isl_ast_build_get_ctx(build);
1058 if (!build->node)
1059 isl_die(ctx, isl_error_internal, "missing AST node",
1060 return isl_ast_build_free(build));
1062 free(build->loop_type);
1063 build->n = isl_schedule_node_band_n_member(build->node);
1064 build->loop_type = isl_alloc_array(ctx,
1065 enum isl_ast_loop_type, build->n);
1066 if (build->n && !build->loop_type)
1067 return isl_ast_build_free(build);
1068 node = build->node;
1069 for (i = 0; i < build->n; ++i)
1070 build->loop_type[i] =
1071 isl_schedule_node_band_member_get_ast_loop_type(node, i);
1073 return build;
1076 /* Replace the band node that "build" refers to by "node" and
1077 * extract the corresponding loop AST generation types.
1079 __isl_give isl_ast_build *isl_ast_build_set_schedule_node(
1080 __isl_take isl_ast_build *build,
1081 __isl_take isl_schedule_node *node)
1083 build = isl_ast_build_cow(build);
1084 if (!build || !node)
1085 goto error;
1087 isl_schedule_node_free(build->node);
1088 build->node = node;
1090 build = extract_loop_types(build);
1092 return build;
1093 error:
1094 isl_ast_build_free(build);
1095 isl_schedule_node_free(node);
1096 return NULL;
1099 /* Remove any reference to a band node from "build".
1101 __isl_give isl_ast_build *isl_ast_build_reset_schedule_node(
1102 __isl_take isl_ast_build *build)
1104 build = isl_ast_build_cow(build);
1105 if (!build)
1106 return NULL;
1108 isl_schedule_node_free(build->node);
1109 build->node = NULL;
1111 return build;
1114 /* Return a copy of the current schedule domain.
1116 __isl_give isl_set *isl_ast_build_get_domain(__isl_keep isl_ast_build *build)
1118 return build ? isl_set_copy(build->domain) : NULL;
1121 /* Return a copy of the set of pending constraints.
1123 __isl_give isl_set *isl_ast_build_get_pending(
1124 __isl_keep isl_ast_build *build)
1126 return build ? isl_set_copy(build->pending) : NULL;
1129 /* Return a copy of the set of generated constraints.
1131 __isl_give isl_set *isl_ast_build_get_generated(
1132 __isl_keep isl_ast_build *build)
1134 return build ? isl_set_copy(build->generated) : NULL;
1137 /* Return a copy of the map from the internal schedule domain
1138 * to the original input schedule domain.
1140 __isl_give isl_multi_aff *isl_ast_build_get_internal2input(
1141 __isl_keep isl_ast_build *build)
1143 return build ? isl_multi_aff_copy(build->internal2input) : NULL;
1146 /* Return the number of variables of the given type
1147 * in the (internal) schedule space.
1149 unsigned isl_ast_build_dim(__isl_keep isl_ast_build *build,
1150 enum isl_dim_type type)
1152 if (!build)
1153 return 0;
1154 return isl_set_dim(build->domain, type);
1157 /* Return the (schedule) space of "build".
1159 * If "internal" is set, then this space is the space of the internal
1160 * representation of the entire schedule, including those parts for
1161 * which no code has been generated yet.
1163 * If "internal" is not set, then this space is the external representation
1164 * of the loops generated so far.
1166 __isl_give isl_space *isl_ast_build_get_space(__isl_keep isl_ast_build *build,
1167 int internal)
1169 int i;
1170 int dim;
1171 isl_space *space;
1173 if (!build)
1174 return NULL;
1176 space = isl_set_get_space(build->domain);
1177 if (internal)
1178 return space;
1180 if (!isl_ast_build_need_schedule_map(build))
1181 return space;
1183 dim = isl_set_dim(build->domain, isl_dim_set);
1184 space = isl_space_drop_dims(space, isl_dim_set,
1185 build->depth, dim - build->depth);
1186 for (i = build->depth - 1; i >= 0; --i) {
1187 isl_bool affine = isl_ast_build_has_affine_value(build, i);
1189 if (affine < 0)
1190 return isl_space_free(space);
1191 if (affine)
1192 space = isl_space_drop_dims(space, isl_dim_set, i, 1);
1195 return space;
1198 /* Return the external representation of the schedule space of "build",
1199 * i.e., a space with a dimension for each loop generated so far,
1200 * with the names of the dimensions set to the loop iterators.
1202 __isl_give isl_space *isl_ast_build_get_schedule_space(
1203 __isl_keep isl_ast_build *build)
1205 isl_space *space;
1206 int i, skip;
1208 if (!build)
1209 return NULL;
1211 space = isl_ast_build_get_space(build, 0);
1213 skip = 0;
1214 for (i = 0; i < build->depth; ++i) {
1215 isl_id *id;
1217 if (isl_ast_build_has_affine_value(build, i)) {
1218 skip++;
1219 continue;
1222 id = isl_ast_build_get_iterator_id(build, i);
1223 space = isl_space_set_dim_id(space, isl_dim_set, i - skip, id);
1226 return space;
1229 /* Return the current schedule, as stored in build->executed, in terms
1230 * of the external schedule domain.
1232 __isl_give isl_union_map *isl_ast_build_get_schedule(
1233 __isl_keep isl_ast_build *build)
1235 isl_union_map *executed;
1236 isl_union_map *schedule;
1238 if (!build)
1239 return NULL;
1241 executed = isl_union_map_copy(build->executed);
1242 if (isl_ast_build_need_schedule_map(build)) {
1243 isl_map *proj = isl_ast_build_get_schedule_map(build);
1244 executed = isl_union_map_apply_domain(executed,
1245 isl_union_map_from_map(proj));
1247 schedule = isl_union_map_reverse(executed);
1249 return schedule;
1252 /* Return the iterator attached to the internal schedule dimension "pos".
1254 __isl_give isl_id *isl_ast_build_get_iterator_id(
1255 __isl_keep isl_ast_build *build, int pos)
1257 if (!build)
1258 return NULL;
1260 return isl_id_list_get_id(build->iterators, pos);
1263 /* Set the stride and offset of the current dimension to the given
1264 * value and expression.
1266 static __isl_give isl_ast_build *set_stride(__isl_take isl_ast_build *build,
1267 __isl_take isl_val *stride, __isl_take isl_aff *offset)
1269 int pos;
1271 build = isl_ast_build_cow(build);
1272 if (!build || !stride || !offset)
1273 goto error;
1275 pos = build->depth;
1277 build->strides = isl_vec_set_element_val(build->strides, pos, stride);
1278 build->offsets = isl_multi_aff_set_aff(build->offsets, pos, offset);
1279 if (!build->strides || !build->offsets)
1280 return isl_ast_build_free(build);
1282 return build;
1283 error:
1284 isl_val_free(stride);
1285 isl_aff_free(offset);
1286 return isl_ast_build_free(build);
1289 /* Return a set expressing the stride constraint at the current depth.
1291 * In particular, if the current iterator (i) is known to attain values
1293 * f + s a
1295 * where f is the offset and s is the stride, then the returned set
1296 * expresses the constraint
1298 * (f - i) mod s = 0
1300 __isl_give isl_set *isl_ast_build_get_stride_constraint(
1301 __isl_keep isl_ast_build *build)
1303 isl_aff *aff;
1304 isl_set *set;
1305 isl_val *stride;
1306 int pos;
1308 if (!build)
1309 return NULL;
1311 pos = build->depth;
1313 if (!isl_ast_build_has_stride(build, pos))
1314 return isl_set_universe(isl_ast_build_get_space(build, 1));
1316 stride = isl_ast_build_get_stride(build, pos);
1317 aff = isl_ast_build_get_offset(build, pos);
1318 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, -1);
1319 aff = isl_aff_mod_val(aff, stride);
1320 set = isl_set_from_basic_set(isl_aff_zero_basic_set(aff));
1322 return set;
1325 /* Return the expansion implied by the stride and offset at the current
1326 * depth.
1328 * That is, return the mapping
1330 * [i_0, ..., i_{d-1}, i_d, i_{d+1}, ...]
1331 * -> [i_0, ..., i_{d-1}, s * i_d + offset(i), i_{d+1}, ...]
1333 * where s is the stride at the current depth d and offset(i) is
1334 * the corresponding offset.
1336 __isl_give isl_multi_aff *isl_ast_build_get_stride_expansion(
1337 __isl_keep isl_ast_build *build)
1339 isl_space *space;
1340 isl_multi_aff *ma;
1341 int pos;
1342 isl_aff *aff, *offset;
1343 isl_val *stride;
1345 if (!build)
1346 return NULL;
1348 pos = isl_ast_build_get_depth(build);
1349 space = isl_ast_build_get_space(build, 1);
1350 space = isl_space_map_from_set(space);
1351 ma = isl_multi_aff_identity(space);
1353 if (!isl_ast_build_has_stride(build, pos))
1354 return ma;
1356 offset = isl_ast_build_get_offset(build, pos);
1357 stride = isl_ast_build_get_stride(build, pos);
1358 aff = isl_multi_aff_get_aff(ma, pos);
1359 aff = isl_aff_scale_val(aff, stride);
1360 aff = isl_aff_add(aff, offset);
1361 ma = isl_multi_aff_set_aff(ma, pos, aff);
1363 return ma;
1366 /* Add constraints corresponding to any previously detected
1367 * stride on the current dimension to build->domain.
1369 __isl_give isl_ast_build *isl_ast_build_include_stride(
1370 __isl_take isl_ast_build *build)
1372 isl_set *set;
1374 if (!build)
1375 return NULL;
1376 if (!isl_ast_build_has_stride(build, build->depth))
1377 return build;
1378 build = isl_ast_build_cow(build);
1379 if (!build)
1380 return NULL;
1382 set = isl_ast_build_get_stride_constraint(build);
1384 build->domain = isl_set_intersect(build->domain, isl_set_copy(set));
1385 build->generated = isl_set_intersect(build->generated, set);
1386 if (!build->domain || !build->generated)
1387 return isl_ast_build_free(build);
1389 return build;
1392 /* Check if the constraints in "set" imply any stride on the current
1393 * dimension and, if so, record the stride information in "build"
1394 * and return the updated "build".
1396 * We assume that inner dimensions have been eliminated from "set"
1397 * by the caller. This is needed because the common stride
1398 * may be imposed by different inner dimensions on different parts of
1399 * the domain.
1400 * The assumption ensures that the lower bound does not depend
1401 * on inner dimensions.
1403 __isl_give isl_ast_build *isl_ast_build_detect_strides(
1404 __isl_take isl_ast_build *build, __isl_take isl_set *set)
1406 int pos;
1407 isl_bool no_stride;
1408 isl_val *stride;
1409 isl_aff *offset;
1410 isl_stride_info *si;
1412 if (!build)
1413 goto error;
1415 pos = isl_ast_build_get_depth(build);
1416 si = isl_set_get_stride_info(set, pos);
1417 stride = isl_stride_info_get_stride(si);
1418 offset = isl_stride_info_get_offset(si);
1419 isl_stride_info_free(si);
1420 isl_set_free(set);
1422 no_stride = isl_val_is_one(stride);
1423 if (no_stride >= 0 && !no_stride)
1424 return set_stride(build, stride, offset);
1425 isl_val_free(stride);
1426 isl_aff_free(offset);
1427 if (no_stride < 0)
1428 return isl_ast_build_free(build);
1429 return build;
1430 error:
1431 isl_set_free(set);
1432 return NULL;
1435 struct isl_ast_build_involves_data {
1436 int depth;
1437 int involves;
1440 /* Check if "map" involves the input dimension data->depth.
1442 static isl_stat involves_depth(__isl_take isl_map *map, void *user)
1444 struct isl_ast_build_involves_data *data = user;
1446 data->involves = isl_map_involves_dims(map, isl_dim_in, data->depth, 1);
1447 isl_map_free(map);
1449 if (data->involves < 0 || data->involves)
1450 return isl_stat_error;
1451 return isl_stat_ok;
1454 /* Do any options depend on the value of the dimension at the current depth?
1456 int isl_ast_build_options_involve_depth(__isl_keep isl_ast_build *build)
1458 struct isl_ast_build_involves_data data;
1460 if (!build)
1461 return -1;
1463 data.depth = build->depth;
1464 data.involves = 0;
1466 if (isl_union_map_foreach_map(build->options,
1467 &involves_depth, &data) < 0) {
1468 if (data.involves < 0 || !data.involves)
1469 return -1;
1472 return data.involves;
1475 /* Construct the map
1477 * { [i] -> [i] : i < pos; [i] -> [i + 1] : i >= pos }
1479 * with "space" the parameter space of the constructed map.
1481 static __isl_give isl_map *construct_insertion_map(__isl_take isl_space *space,
1482 int pos)
1484 isl_constraint *c;
1485 isl_basic_map *bmap1, *bmap2;
1487 space = isl_space_set_from_params(space);
1488 space = isl_space_add_dims(space, isl_dim_set, 1);
1489 space = isl_space_map_from_set(space);
1490 c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
1491 c = isl_constraint_set_coefficient_si(c, isl_dim_in, 0, 1);
1492 c = isl_constraint_set_coefficient_si(c, isl_dim_out, 0, -1);
1493 bmap1 = isl_basic_map_from_constraint(isl_constraint_copy(c));
1494 c = isl_constraint_set_constant_si(c, 1);
1495 bmap2 = isl_basic_map_from_constraint(c);
1497 bmap1 = isl_basic_map_upper_bound_si(bmap1, isl_dim_in, 0, pos - 1);
1498 bmap2 = isl_basic_map_lower_bound_si(bmap2, isl_dim_in, 0, pos);
1500 return isl_basic_map_union(bmap1, bmap2);
1503 static const char *option_str[] = {
1504 [isl_ast_loop_atomic] = "atomic",
1505 [isl_ast_loop_unroll] = "unroll",
1506 [isl_ast_loop_separate] = "separate"
1509 /* Update the "options" to reflect the insertion of a dimension
1510 * at position "pos" in the schedule domain space.
1511 * "space" is the original domain space before the insertion and
1512 * may be named and/or structured.
1514 * The (relevant) input options all have "space" as domain, which
1515 * has to be mapped to the extended space.
1516 * The values of the ranges also refer to the schedule domain positions
1517 * and they therefore also need to be adjusted. In particular, values
1518 * smaller than pos do not need to change, while values greater than or
1519 * equal to pos need to be incremented.
1520 * That is, we need to apply the following map.
1522 * { atomic[i] -> atomic[i] : i < pos; [i] -> [i + 1] : i >= pos;
1523 * unroll[i] -> unroll[i] : i < pos; [i] -> [i + 1] : i >= pos;
1524 * separate[i] -> separate[i] : i < pos; [i] -> [i + 1] : i >= pos;
1525 * separation_class[[i] -> [c]]
1526 * -> separation_class[[i] -> [c]] : i < pos;
1527 * separation_class[[i] -> [c]]
1528 * -> separation_class[[i + 1] -> [c]] : i >= pos }
1530 static __isl_give isl_union_map *options_insert_dim(
1531 __isl_take isl_union_map *options, __isl_take isl_space *space, int pos)
1533 isl_map *map;
1534 isl_union_map *insertion;
1535 enum isl_ast_loop_type type;
1536 const char *name = "separation_class";
1538 space = isl_space_map_from_set(space);
1539 map = isl_map_identity(space);
1540 map = isl_map_insert_dims(map, isl_dim_out, pos, 1);
1541 options = isl_union_map_apply_domain(options,
1542 isl_union_map_from_map(map));
1544 if (!options)
1545 return NULL;
1547 map = construct_insertion_map(isl_union_map_get_space(options), pos);
1549 insertion = isl_union_map_empty(isl_union_map_get_space(options));
1551 for (type = isl_ast_loop_atomic;
1552 type <= isl_ast_loop_separate; ++type) {
1553 isl_map *map_type = isl_map_copy(map);
1554 const char *name = option_str[type];
1555 map_type = isl_map_set_tuple_name(map_type, isl_dim_in, name);
1556 map_type = isl_map_set_tuple_name(map_type, isl_dim_out, name);
1557 insertion = isl_union_map_add_map(insertion, map_type);
1560 map = isl_map_product(map, isl_map_identity(isl_map_get_space(map)));
1561 map = isl_map_set_tuple_name(map, isl_dim_in, name);
1562 map = isl_map_set_tuple_name(map, isl_dim_out, name);
1563 insertion = isl_union_map_add_map(insertion, map);
1565 options = isl_union_map_apply_range(options, insertion);
1567 return options;
1570 /* If we are generating an AST from a schedule tree (build->node is set),
1571 * then update the loop AST generation types
1572 * to reflect the insertion of a dimension at (global) position "pos"
1573 * in the schedule domain space.
1574 * We do not need to adjust any isolate option since we would not be inserting
1575 * any dimensions if there were any isolate option.
1577 static __isl_give isl_ast_build *node_insert_dim(
1578 __isl_take isl_ast_build *build, int pos)
1580 int i;
1581 int local_pos;
1582 enum isl_ast_loop_type *loop_type;
1583 isl_ctx *ctx;
1585 build = isl_ast_build_cow(build);
1586 if (!build)
1587 return NULL;
1588 if (!build->node)
1589 return build;
1591 ctx = isl_ast_build_get_ctx(build);
1592 local_pos = pos - build->outer_pos;
1593 loop_type = isl_realloc_array(ctx, build->loop_type,
1594 enum isl_ast_loop_type, build->n + 1);
1595 if (!loop_type)
1596 return isl_ast_build_free(build);
1597 build->loop_type = loop_type;
1598 for (i = build->n - 1; i >= local_pos; --i)
1599 loop_type[i + 1] = loop_type[i];
1600 loop_type[local_pos] = isl_ast_loop_default;
1601 build->n++;
1603 return build;
1606 /* Insert a single dimension in the schedule domain at position "pos".
1607 * The new dimension is given an isl_id with the empty string as name.
1609 * The main difficulty is updating build->options to reflect the
1610 * extra dimension. This is handled in options_insert_dim.
1612 * Note that because of the dimension manipulations, the resulting
1613 * schedule domain space will always be unnamed and unstructured.
1614 * However, the original schedule domain space may be named and/or
1615 * structured, so we have to take this possibility into account
1616 * while performing the transformations.
1618 * Since the inserted schedule dimension is used by the caller
1619 * to differentiate between different domain spaces, there is
1620 * no longer a uniform mapping from the internal schedule space
1621 * to the input schedule space. The internal2input mapping is
1622 * therefore removed.
1624 __isl_give isl_ast_build *isl_ast_build_insert_dim(
1625 __isl_take isl_ast_build *build, int pos)
1627 isl_ctx *ctx;
1628 isl_space *space, *ma_space;
1629 isl_id *id;
1630 isl_multi_aff *ma;
1632 build = isl_ast_build_cow(build);
1633 if (!build)
1634 return NULL;
1636 ctx = isl_ast_build_get_ctx(build);
1637 id = isl_id_alloc(ctx, "", NULL);
1638 if (!build->node)
1639 space = isl_ast_build_get_space(build, 1);
1640 build->iterators = isl_id_list_insert(build->iterators, pos, id);
1641 build->domain = isl_set_insert_dims(build->domain,
1642 isl_dim_set, pos, 1);
1643 build->generated = isl_set_insert_dims(build->generated,
1644 isl_dim_set, pos, 1);
1645 build->pending = isl_set_insert_dims(build->pending,
1646 isl_dim_set, pos, 1);
1647 build->strides = isl_vec_insert_els(build->strides, pos, 1);
1648 build->strides = isl_vec_set_element_si(build->strides, pos, 1);
1649 ma_space = isl_space_params(isl_multi_aff_get_space(build->offsets));
1650 ma_space = isl_space_set_from_params(ma_space);
1651 ma_space = isl_space_add_dims(ma_space, isl_dim_set, 1);
1652 ma_space = isl_space_map_from_set(ma_space);
1653 ma = isl_multi_aff_zero(isl_space_copy(ma_space));
1654 build->offsets = isl_multi_aff_splice(build->offsets, pos, pos, ma);
1655 ma = isl_multi_aff_identity(ma_space);
1656 build->values = isl_multi_aff_splice(build->values, pos, pos, ma);
1657 if (!build->node)
1658 build->options = options_insert_dim(build->options, space, pos);
1659 build->internal2input = isl_multi_aff_free(build->internal2input);
1661 if (!build->iterators || !build->domain || !build->generated ||
1662 !build->pending || !build->values ||
1663 !build->strides || !build->offsets || !build->options)
1664 return isl_ast_build_free(build);
1666 build = node_insert_dim(build, pos);
1668 return build;
1671 /* Scale down the current dimension by a factor of "m".
1672 * "umap" is an isl_union_map that implements the scaling down.
1673 * That is, it is of the form
1675 * { [.... i ....] -> [.... i' ....] : i = m i' }
1677 * This function is called right after the strides have been
1678 * detected, but before any constraints on the current dimension
1679 * have been included in build->domain.
1680 * We therefore only need to update stride, offset, the options and
1681 * the mapping from internal schedule space to the original schedule
1682 * space, if we are still keeping track of such a mapping.
1683 * The latter mapping is updated by plugging in
1684 * { [... i ...] -> [... m i ... ] }.
1686 __isl_give isl_ast_build *isl_ast_build_scale_down(
1687 __isl_take isl_ast_build *build, __isl_take isl_val *m,
1688 __isl_take isl_union_map *umap)
1690 isl_aff *aff;
1691 isl_val *v;
1692 int depth;
1694 build = isl_ast_build_cow(build);
1695 if (!build || !umap || !m)
1696 goto error;
1698 depth = build->depth;
1700 if (build->internal2input) {
1701 isl_space *space;
1702 isl_multi_aff *ma;
1703 isl_aff *aff;
1705 space = isl_multi_aff_get_space(build->internal2input);
1706 space = isl_space_map_from_set(isl_space_domain(space));
1707 ma = isl_multi_aff_identity(space);
1708 aff = isl_multi_aff_get_aff(ma, depth);
1709 aff = isl_aff_scale_val(aff, isl_val_copy(m));
1710 ma = isl_multi_aff_set_aff(ma, depth, aff);
1711 build->internal2input =
1712 isl_multi_aff_pullback_multi_aff(build->internal2input, ma);
1713 if (!build->internal2input)
1714 goto error;
1717 v = isl_vec_get_element_val(build->strides, depth);
1718 v = isl_val_div(v, isl_val_copy(m));
1719 build->strides = isl_vec_set_element_val(build->strides, depth, v);
1721 aff = isl_multi_aff_get_aff(build->offsets, depth);
1722 aff = isl_aff_scale_down_val(aff, m);
1723 build->offsets = isl_multi_aff_set_aff(build->offsets, depth, aff);
1724 build->options = isl_union_map_apply_domain(build->options, umap);
1725 if (!build->strides || !build->offsets || !build->options)
1726 return isl_ast_build_free(build);
1728 return build;
1729 error:
1730 isl_val_free(m);
1731 isl_union_map_free(umap);
1732 return isl_ast_build_free(build);
1735 /* Return a list of "n" isl_ids called "c%d", with "%d" starting at "first".
1736 * If an isl_id with such a name already appears among the parameters
1737 * in build->domain, then adjust the name to "c%d_%d".
1739 static __isl_give isl_id_list *generate_names(isl_ctx *ctx, int n, int first,
1740 __isl_keep isl_ast_build *build)
1742 int i;
1743 isl_id_list *names;
1745 names = isl_id_list_alloc(ctx, n);
1746 for (i = 0; i < n; ++i) {
1747 isl_id *id;
1749 id = generate_name(ctx, first + i, build);
1750 names = isl_id_list_add(names, id);
1753 return names;
1756 /* Embed "options" into the given isl_ast_build space.
1758 * This function is called from within a nested call to
1759 * isl_ast_build_node_from_schedule_map.
1760 * "options" refers to the additional schedule,
1761 * while space refers to both the space of the outer isl_ast_build and
1762 * that of the additional schedule.
1763 * Specifically, space is of the form
1765 * [I -> S]
1767 * while options lives in the space(s)
1769 * S -> *
1771 * We compute
1773 * [I -> S] -> S
1775 * and compose this with options, to obtain the new options
1776 * living in the space(s)
1778 * [I -> S] -> *
1780 static __isl_give isl_union_map *embed_options(
1781 __isl_take isl_union_map *options, __isl_take isl_space *space)
1783 isl_map *map;
1785 map = isl_map_universe(isl_space_unwrap(space));
1786 map = isl_map_range_map(map);
1788 options = isl_union_map_apply_range(
1789 isl_union_map_from_map(map), options);
1791 return options;
1794 /* Update "build" for use in a (possibly nested) code generation. That is,
1795 * extend "build" from an AST build on some domain O to an AST build
1796 * on domain [O -> S], with S corresponding to "space".
1797 * If the original domain is a parameter domain, then the new domain is
1798 * simply S.
1799 * "iterators" is a list of iterators for S, but the number of elements
1800 * may be smaller or greater than the number of set dimensions of S.
1801 * If "keep_iterators" is set, then any extra ids in build->iterators
1802 * are reused for S. Otherwise, these extra ids are dropped.
1804 * We first update build->outer_pos to the current depth.
1805 * This depth is zero in case this is the outermost code generation.
1807 * We then add additional ids such that the number of iterators is at least
1808 * equal to the dimension of the new build domain.
1810 * If the original domain is parametric, then we are constructing
1811 * an isl_ast_build for the outer code generation and we pass control
1812 * to isl_ast_build_init.
1814 * Otherwise, we adjust the fields of "build" to include "space".
1816 __isl_give isl_ast_build *isl_ast_build_product(
1817 __isl_take isl_ast_build *build, __isl_take isl_space *space)
1819 isl_ctx *ctx;
1820 isl_vec *strides;
1821 isl_set *set;
1822 isl_multi_aff *embedding;
1823 int dim, n_it;
1825 build = isl_ast_build_cow(build);
1826 if (!build)
1827 goto error;
1829 build->outer_pos = build->depth;
1831 ctx = isl_ast_build_get_ctx(build);
1832 dim = isl_set_dim(build->domain, isl_dim_set);
1833 dim += isl_space_dim(space, isl_dim_set);
1834 n_it = isl_id_list_n_id(build->iterators);
1835 if (n_it < dim) {
1836 isl_id_list *l;
1837 l = generate_names(ctx, dim - n_it, n_it, build);
1838 build->iterators = isl_id_list_concat(build->iterators, l);
1841 if (isl_set_is_params(build->domain))
1842 return isl_ast_build_init(build, space);
1844 set = isl_set_universe(isl_space_copy(space));
1845 build->domain = isl_set_product(build->domain, isl_set_copy(set));
1846 build->pending = isl_set_product(build->pending, isl_set_copy(set));
1847 build->generated = isl_set_product(build->generated, set);
1849 strides = isl_vec_alloc(ctx, isl_space_dim(space, isl_dim_set));
1850 strides = isl_vec_set_si(strides, 1);
1851 build->strides = isl_vec_concat(build->strides, strides);
1853 space = isl_space_map_from_set(space);
1854 build->offsets = isl_multi_aff_align_params(build->offsets,
1855 isl_space_copy(space));
1856 build->offsets = isl_multi_aff_product(build->offsets,
1857 isl_multi_aff_zero(isl_space_copy(space)));
1858 build->values = isl_multi_aff_align_params(build->values,
1859 isl_space_copy(space));
1860 embedding = isl_multi_aff_identity(space);
1861 build->values = isl_multi_aff_product(build->values,
1862 isl_multi_aff_copy(embedding));
1863 if (build->internal2input) {
1864 build->internal2input =
1865 isl_multi_aff_product(build->internal2input, embedding);
1866 build->internal2input =
1867 isl_multi_aff_flatten_range(build->internal2input);
1868 if (!build->internal2input)
1869 return isl_ast_build_free(build);
1870 } else {
1871 isl_multi_aff_free(embedding);
1874 space = isl_ast_build_get_space(build, 1);
1875 build->options = embed_options(build->options, space);
1877 if (!build->iterators || !build->domain || !build->generated ||
1878 !build->pending || !build->values ||
1879 !build->strides || !build->offsets || !build->options)
1880 return isl_ast_build_free(build);
1882 return build;
1883 error:
1884 isl_ast_build_free(build);
1885 isl_space_free(space);
1886 return NULL;
1889 /* Does "aff" only attain non-negative values over build->domain?
1890 * That is, does it not attain any negative values?
1892 int isl_ast_build_aff_is_nonneg(__isl_keep isl_ast_build *build,
1893 __isl_keep isl_aff *aff)
1895 isl_set *test;
1896 int empty;
1898 if (!build)
1899 return -1;
1901 aff = isl_aff_copy(aff);
1902 test = isl_set_from_basic_set(isl_aff_neg_basic_set(aff));
1903 test = isl_set_intersect(test, isl_set_copy(build->domain));
1904 empty = isl_set_is_empty(test);
1905 isl_set_free(test);
1907 return empty;
1910 /* Does the dimension at (internal) position "pos" have a non-trivial stride?
1912 isl_bool isl_ast_build_has_stride(__isl_keep isl_ast_build *build, int pos)
1914 isl_val *v;
1915 isl_bool has_stride;
1917 if (!build)
1918 return isl_bool_error;
1920 v = isl_vec_get_element_val(build->strides, pos);
1921 has_stride = isl_bool_not(isl_val_is_one(v));
1922 isl_val_free(v);
1924 return has_stride;
1927 /* Given that the dimension at position "pos" takes on values
1929 * f + s a
1931 * with a an integer, return s through *stride.
1933 __isl_give isl_val *isl_ast_build_get_stride(__isl_keep isl_ast_build *build,
1934 int pos)
1936 if (!build)
1937 return NULL;
1939 return isl_vec_get_element_val(build->strides, pos);
1942 /* Given that the dimension at position "pos" takes on values
1944 * f + s a
1946 * with a an integer, return f.
1948 __isl_give isl_aff *isl_ast_build_get_offset(
1949 __isl_keep isl_ast_build *build, int pos)
1951 if (!build)
1952 return NULL;
1954 return isl_multi_aff_get_aff(build->offsets, pos);
1957 /* Is the dimension at position "pos" known to attain only a single
1958 * value that, moreover, can be described by a single affine expression
1959 * in terms of the outer dimensions and parameters?
1961 * If not, then the corresponding affine expression in build->values
1962 * is set to be equal to the same input dimension.
1963 * Otherwise, it is set to the requested expression in terms of
1964 * outer dimensions and parameters.
1966 isl_bool isl_ast_build_has_affine_value(__isl_keep isl_ast_build *build,
1967 int pos)
1969 isl_aff *aff;
1970 isl_bool involves;
1972 if (!build)
1973 return isl_bool_error;
1975 aff = isl_multi_aff_get_aff(build->values, pos);
1976 involves = isl_aff_involves_dims(aff, isl_dim_in, pos, 1);
1977 isl_aff_free(aff);
1979 return isl_bool_not(involves);
1982 /* Plug in the known values (fixed affine expressions in terms of
1983 * parameters and outer loop iterators) of all loop iterators
1984 * in the domain of "umap".
1986 * We simply precompose "umap" with build->values.
1988 __isl_give isl_union_map *isl_ast_build_substitute_values_union_map_domain(
1989 __isl_keep isl_ast_build *build, __isl_take isl_union_map *umap)
1991 isl_multi_aff *values;
1993 if (!build)
1994 return isl_union_map_free(umap);
1996 values = isl_multi_aff_copy(build->values);
1997 umap = isl_union_map_preimage_domain_multi_aff(umap, values);
1999 return umap;
2002 /* Is the current dimension known to attain only a single value?
2004 int isl_ast_build_has_value(__isl_keep isl_ast_build *build)
2006 if (!build)
2007 return -1;
2009 return build->value != NULL;
2012 /* Simplify the basic set "bset" based on what we know about
2013 * the iterators of already generated loops.
2015 * "bset" is assumed to live in the (internal) schedule domain.
2017 __isl_give isl_basic_set *isl_ast_build_compute_gist_basic_set(
2018 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
2020 if (!build)
2021 goto error;
2023 bset = isl_basic_set_preimage_multi_aff(bset,
2024 isl_multi_aff_copy(build->values));
2025 bset = isl_basic_set_gist(bset,
2026 isl_set_simple_hull(isl_set_copy(build->domain)));
2028 return bset;
2029 error:
2030 isl_basic_set_free(bset);
2031 return NULL;
2034 /* Simplify the set "set" based on what we know about
2035 * the iterators of already generated loops.
2037 * "set" is assumed to live in the (internal) schedule domain.
2039 __isl_give isl_set *isl_ast_build_compute_gist(
2040 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2042 if (!build)
2043 goto error;
2045 if (!isl_set_is_params(set))
2046 set = isl_set_preimage_multi_aff(set,
2047 isl_multi_aff_copy(build->values));
2048 set = isl_set_gist(set, isl_set_copy(build->domain));
2050 return set;
2051 error:
2052 isl_set_free(set);
2053 return NULL;
2056 /* Include information about what we know about the iterators of
2057 * already generated loops to "set".
2059 * We currently only plug in the known affine values of outer loop
2060 * iterators.
2061 * In principle we could also introduce equalities or even other
2062 * constraints implied by the intersection of "set" and build->domain.
2064 __isl_give isl_set *isl_ast_build_specialize(__isl_keep isl_ast_build *build,
2065 __isl_take isl_set *set)
2067 if (!build)
2068 return isl_set_free(set);
2070 return isl_set_preimage_multi_aff(set,
2071 isl_multi_aff_copy(build->values));
2074 /* Plug in the known affine values of outer loop iterators in "bset".
2076 __isl_give isl_basic_set *isl_ast_build_specialize_basic_set(
2077 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
2079 if (!build)
2080 return isl_basic_set_free(bset);
2082 return isl_basic_set_preimage_multi_aff(bset,
2083 isl_multi_aff_copy(build->values));
2086 /* Simplify the map "map" based on what we know about
2087 * the iterators of already generated loops.
2089 * The domain of "map" is assumed to live in the (internal) schedule domain.
2091 __isl_give isl_map *isl_ast_build_compute_gist_map_domain(
2092 __isl_keep isl_ast_build *build, __isl_take isl_map *map)
2094 if (!build)
2095 goto error;
2097 map = isl_map_gist_domain(map, isl_set_copy(build->domain));
2099 return map;
2100 error:
2101 isl_map_free(map);
2102 return NULL;
2105 /* Simplify the affine expression "aff" based on what we know about
2106 * the iterators of already generated loops.
2108 * The domain of "aff" is assumed to live in the (internal) schedule domain.
2110 __isl_give isl_aff *isl_ast_build_compute_gist_aff(
2111 __isl_keep isl_ast_build *build, __isl_take isl_aff *aff)
2113 if (!build)
2114 goto error;
2116 aff = isl_aff_gist(aff, isl_set_copy(build->domain));
2118 return aff;
2119 error:
2120 isl_aff_free(aff);
2121 return NULL;
2124 /* Simplify the piecewise affine expression "aff" based on what we know about
2125 * the iterators of already generated loops.
2127 * The domain of "pa" is assumed to live in the (internal) schedule domain.
2129 __isl_give isl_pw_aff *isl_ast_build_compute_gist_pw_aff(
2130 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2132 if (!build)
2133 goto error;
2135 if (!isl_set_is_params(build->domain))
2136 pa = isl_pw_aff_pullback_multi_aff(pa,
2137 isl_multi_aff_copy(build->values));
2138 pa = isl_pw_aff_gist(pa, isl_set_copy(build->domain));
2140 return pa;
2141 error:
2142 isl_pw_aff_free(pa);
2143 return NULL;
2146 /* Simplify the piecewise multi-affine expression "aff" based on what
2147 * we know about the iterators of already generated loops.
2149 * The domain of "pma" is assumed to live in the (internal) schedule domain.
2151 __isl_give isl_pw_multi_aff *isl_ast_build_compute_gist_pw_multi_aff(
2152 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
2154 if (!build)
2155 goto error;
2157 pma = isl_pw_multi_aff_pullback_multi_aff(pma,
2158 isl_multi_aff_copy(build->values));
2159 pma = isl_pw_multi_aff_gist(pma, isl_set_copy(build->domain));
2161 return pma;
2162 error:
2163 isl_pw_multi_aff_free(pma);
2164 return NULL;
2167 /* Extract the schedule domain of the given type from build->options
2168 * at the current depth.
2170 * In particular, find the subset of build->options that is of
2171 * the following form
2173 * schedule_domain -> type[depth]
2175 * and return the corresponding domain, after eliminating inner dimensions
2176 * and divs that depend on the current dimension.
2178 * Note that the domain of build->options has been reformulated
2179 * in terms of the internal build space in embed_options,
2180 * but the position is still that within the current code generation.
2182 __isl_give isl_set *isl_ast_build_get_option_domain(
2183 __isl_keep isl_ast_build *build, enum isl_ast_loop_type type)
2185 const char *name;
2186 isl_space *space;
2187 isl_map *option;
2188 isl_set *domain;
2189 int local_pos;
2191 if (!build)
2192 return NULL;
2194 name = option_str[type];
2195 local_pos = build->depth - build->outer_pos;
2197 space = isl_ast_build_get_space(build, 1);
2198 space = isl_space_from_domain(space);
2199 space = isl_space_add_dims(space, isl_dim_out, 1);
2200 space = isl_space_set_tuple_name(space, isl_dim_out, name);
2202 option = isl_union_map_extract_map(build->options, space);
2203 option = isl_map_fix_si(option, isl_dim_out, 0, local_pos);
2205 domain = isl_map_domain(option);
2206 domain = isl_ast_build_eliminate(build, domain);
2208 return domain;
2211 /* How does the user want the current schedule dimension to be generated?
2212 * These choices have been extracted from the schedule node
2213 * in extract_loop_types and stored in build->loop_type.
2214 * They have been updated to reflect any dimension insertion in
2215 * node_insert_dim.
2216 * Return isl_ast_domain_error on error.
2218 * If "isolated" is set, then we get the loop AST generation type
2219 * directly from the band node since node_insert_dim cannot have been
2220 * called on a band with the isolate option.
2222 enum isl_ast_loop_type isl_ast_build_get_loop_type(
2223 __isl_keep isl_ast_build *build, int isolated)
2225 int local_pos;
2226 isl_ctx *ctx;
2228 if (!build)
2229 return isl_ast_loop_error;
2230 ctx = isl_ast_build_get_ctx(build);
2231 if (!build->node)
2232 isl_die(ctx, isl_error_internal,
2233 "only works for schedule tree based AST generation",
2234 return isl_ast_loop_error);
2236 local_pos = build->depth - build->outer_pos;
2237 if (!isolated)
2238 return build->loop_type[local_pos];
2239 return isl_schedule_node_band_member_get_isolate_ast_loop_type(
2240 build->node, local_pos);
2243 /* Extract the isolated set from the isolate option, if any,
2244 * and store in the build.
2245 * If there is no isolate option, then the isolated set is
2246 * set to the empty set.
2248 * The isolate option is of the form
2250 * isolate[[outer bands] -> current_band]
2252 * We flatten this set and then map it back to the internal
2253 * schedule space.
2255 * If we have already extracted the isolated set
2256 * or if internal2input is no longer set, then we do not
2257 * need to do anything. In the latter case, we know
2258 * that the current band cannot have any isolate option.
2260 __isl_give isl_ast_build *isl_ast_build_extract_isolated(
2261 __isl_take isl_ast_build *build)
2263 isl_set *isolated;
2265 if (!build)
2266 return NULL;
2267 if (!build->internal2input)
2268 return build;
2269 if (build->isolated)
2270 return build;
2272 build = isl_ast_build_cow(build);
2273 if (!build)
2274 return NULL;
2276 isolated = isl_schedule_node_band_get_ast_isolate_option(build->node);
2277 isolated = isl_set_flatten(isolated);
2278 isolated = isl_set_preimage_multi_aff(isolated,
2279 isl_multi_aff_copy(build->internal2input));
2281 build->isolated = isolated;
2282 if (!build->isolated)
2283 return isl_ast_build_free(build);
2285 return build;
2288 /* Does "build" have a non-empty isolated set?
2290 * The caller is assumed to have called isl_ast_build_extract_isolated first.
2292 int isl_ast_build_has_isolated(__isl_keep isl_ast_build *build)
2294 int empty;
2296 if (!build)
2297 return -1;
2298 if (!build->internal2input)
2299 return 0;
2300 if (!build->isolated)
2301 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
2302 "isolated set not extracted yet", return -1);
2304 empty = isl_set_plain_is_empty(build->isolated);
2305 return empty < 0 ? -1 : !empty;
2308 /* Return a copy of the isolated set of "build".
2310 * The caller is assume to have called isl_ast_build_has_isolated first,
2311 * with this function returning true.
2312 * In particular, this function should not be called if we are no
2313 * longer keeping track of internal2input (and there therefore could
2314 * not possibly be any isolated set).
2316 __isl_give isl_set *isl_ast_build_get_isolated(__isl_keep isl_ast_build *build)
2318 if (!build)
2319 return NULL;
2320 if (!build->internal2input)
2321 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
2322 "build cannot have isolated set", return NULL);
2324 return isl_set_copy(build->isolated);
2327 /* Extract the separation class mapping at the current depth.
2329 * In particular, find and return the subset of build->options that is of
2330 * the following form
2332 * schedule_domain -> separation_class[[depth] -> [class]]
2334 * The caller is expected to eliminate inner dimensions from the domain.
2336 * Note that the domain of build->options has been reformulated
2337 * in terms of the internal build space in embed_options,
2338 * but the position is still that within the current code generation.
2340 __isl_give isl_map *isl_ast_build_get_separation_class(
2341 __isl_keep isl_ast_build *build)
2343 isl_ctx *ctx;
2344 isl_space *space_sep, *space;
2345 isl_map *res;
2346 int local_pos;
2348 if (!build)
2349 return NULL;
2351 local_pos = build->depth - build->outer_pos;
2352 ctx = isl_ast_build_get_ctx(build);
2353 space_sep = isl_space_alloc(ctx, 0, 1, 1);
2354 space_sep = isl_space_wrap(space_sep);
2355 space_sep = isl_space_set_tuple_name(space_sep, isl_dim_set,
2356 "separation_class");
2357 space = isl_ast_build_get_space(build, 1);
2358 space_sep = isl_space_align_params(space_sep, isl_space_copy(space));
2359 space = isl_space_map_from_domain_and_range(space, space_sep);
2361 res = isl_union_map_extract_map(build->options, space);
2362 res = isl_map_fix_si(res, isl_dim_out, 0, local_pos);
2363 res = isl_map_coalesce(res);
2365 return res;
2368 /* Eliminate dimensions inner to the current dimension.
2370 __isl_give isl_set *isl_ast_build_eliminate_inner(
2371 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2373 int dim;
2374 int depth;
2376 if (!build)
2377 return isl_set_free(set);
2379 dim = isl_set_dim(set, isl_dim_set);
2380 depth = build->depth;
2381 set = isl_set_detect_equalities(set);
2382 set = isl_set_eliminate(set, isl_dim_set, depth + 1, dim - (depth + 1));
2384 return set;
2387 /* Eliminate unknown divs and divs that depend on the current dimension.
2389 * Note that during the elimination of unknown divs, we may discover
2390 * an explicit representation of some other unknown divs, which may
2391 * depend on the current dimension. We therefore need to eliminate
2392 * unknown divs first.
2394 __isl_give isl_set *isl_ast_build_eliminate_divs(
2395 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2397 int depth;
2399 if (!build)
2400 return isl_set_free(set);
2402 set = isl_set_remove_unknown_divs(set);
2403 depth = build->depth;
2404 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, depth, 1);
2406 return set;
2409 /* Eliminate dimensions inner to the current dimension as well as
2410 * unknown divs and divs that depend on the current dimension.
2411 * The result then consists only of constraints that are independent
2412 * of the current dimension and upper and lower bounds on the current
2413 * dimension.
2415 __isl_give isl_set *isl_ast_build_eliminate(
2416 __isl_keep isl_ast_build *build, __isl_take isl_set *domain)
2418 domain = isl_ast_build_eliminate_inner(build, domain);
2419 domain = isl_ast_build_eliminate_divs(build, domain);
2420 return domain;
2423 /* Replace build->single_valued by "sv".
2425 __isl_give isl_ast_build *isl_ast_build_set_single_valued(
2426 __isl_take isl_ast_build *build, int sv)
2428 if (!build)
2429 return build;
2430 if (build->single_valued == sv)
2431 return build;
2432 build = isl_ast_build_cow(build);
2433 if (!build)
2434 return build;
2435 build->single_valued = sv;
2437 return build;