isl_ast_build_node_from_schedule: handle basic AST build options
[isl.git] / isl_ast_build.c
blobd86ceedb418c8bd572d31a8c4dd9bf5f5970316e
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/map.h>
14 #include <isl/aff.h>
15 #include <isl/map.h>
16 #include <isl_ast_build_private.h>
17 #include <isl_ast_private.h>
19 /* Construct a map that isolates the current dimension.
21 * Essentially, the current dimension of "set" is moved to the single output
22 * dimension in the result, with the current dimension in the domain replaced
23 * by an unconstrained variable.
25 __isl_give isl_map *isl_ast_build_map_to_iterator(
26 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
28 isl_map *map;
30 map = isl_map_from_domain(set);
31 map = isl_map_add_dims(map, isl_dim_out, 1);
33 if (!build)
34 return isl_map_free(map);
36 map = isl_map_equate(map, isl_dim_in, build->depth, isl_dim_out, 0);
37 map = isl_map_eliminate(map, isl_dim_in, build->depth, 1);
39 return map;
42 /* Initialize the information derived during the AST generation to default
43 * values for a schedule domain in "space".
45 * We also check that the remaining fields are not NULL so that
46 * the calling functions don't have to perform this test.
48 static __isl_give isl_ast_build *isl_ast_build_init_derived(
49 __isl_take isl_ast_build *build, __isl_take isl_space *space)
51 isl_ctx *ctx;
52 isl_vec *strides;
54 build = isl_ast_build_cow(build);
55 if (!build || !build->domain)
56 goto error;
58 ctx = isl_ast_build_get_ctx(build);
59 strides = isl_vec_alloc(ctx, isl_space_dim(space, isl_dim_set));
60 strides = isl_vec_set_si(strides, 1);
62 isl_vec_free(build->strides);
63 build->strides = strides;
65 space = isl_space_map_from_set(space);
66 isl_multi_aff_free(build->offsets);
67 build->offsets = isl_multi_aff_zero(isl_space_copy(space));
68 isl_multi_aff_free(build->values);
69 build->values = isl_multi_aff_identity(space);
71 if (!build->iterators || !build->domain || !build->generated ||
72 !build->pending || !build->values ||
73 !build->strides || !build->offsets || !build->options)
74 return isl_ast_build_free(build);
76 return build;
77 error:
78 isl_space_free(space);
79 return isl_ast_build_free(build);
82 /* Return an isl_id called "c%d", with "%d" set to "i".
83 * If an isl_id with such a name already appears among the parameters
84 * in build->domain, then adjust the name to "c%d_%d".
86 static __isl_give isl_id *generate_name(isl_ctx *ctx, int i,
87 __isl_keep isl_ast_build *build)
89 int j;
90 char name[16];
91 isl_set *dom = build->domain;
93 snprintf(name, sizeof(name), "c%d", i);
94 j = 0;
95 while (isl_set_find_dim_by_name(dom, isl_dim_param, name) >= 0)
96 snprintf(name, sizeof(name), "c%d_%d", i, j++);
97 return isl_id_alloc(ctx, name, NULL);
100 /* Create an isl_ast_build with "set" as domain.
102 * The input set is usually a parameter domain, but we currently allow it to
103 * be any kind of set. We set the domain of the returned isl_ast_build
104 * to "set" and initialize all the other fields to default values.
106 __isl_give isl_ast_build *isl_ast_build_from_context(__isl_take isl_set *set)
108 int i, n;
109 isl_ctx *ctx;
110 isl_space *space;
111 isl_ast_build *build;
113 set = isl_set_compute_divs(set);
114 if (!set)
115 return NULL;
117 ctx = isl_set_get_ctx(set);
119 build = isl_calloc_type(ctx, isl_ast_build);
120 if (!build)
121 goto error;
123 build->ref = 1;
124 build->domain = set;
125 build->generated = isl_set_copy(build->domain);
126 build->pending = isl_set_universe(isl_set_get_space(build->domain));
127 build->options = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
128 n = isl_set_dim(set, isl_dim_set);
129 build->depth = n;
130 build->iterators = isl_id_list_alloc(ctx, n);
131 for (i = 0; i < n; ++i) {
132 isl_id *id;
133 if (isl_set_has_dim_id(set, isl_dim_set, i))
134 id = isl_set_get_dim_id(set, isl_dim_set, i);
135 else
136 id = generate_name(ctx, i, build);
137 build->iterators = isl_id_list_add(build->iterators, id);
139 space = isl_set_get_space(set);
140 if (isl_space_is_params(space))
141 space = isl_space_set_from_params(space);
143 return isl_ast_build_init_derived(build, space);
144 error:
145 isl_set_free(set);
146 return NULL;
149 /* Create an isl_ast_build with a universe (parametric) context.
151 __isl_give isl_ast_build *isl_ast_build_alloc(isl_ctx *ctx)
153 isl_space *space;
154 isl_set *context;
156 space = isl_space_params_alloc(ctx, 0);
157 context = isl_set_universe(space);
159 return isl_ast_build_from_context(context);
162 __isl_give isl_ast_build *isl_ast_build_copy(__isl_keep isl_ast_build *build)
164 if (!build)
165 return NULL;
167 build->ref++;
168 return build;
171 __isl_give isl_ast_build *isl_ast_build_dup(__isl_keep isl_ast_build *build)
173 isl_ctx *ctx;
174 isl_ast_build *dup;
176 if (!build)
177 return NULL;
179 ctx = isl_ast_build_get_ctx(build);
180 dup = isl_calloc_type(ctx, isl_ast_build);
181 if (!dup)
182 return NULL;
184 dup->ref = 1;
185 dup->outer_pos = build->outer_pos;
186 dup->depth = build->depth;
187 dup->iterators = isl_id_list_copy(build->iterators);
188 dup->domain = isl_set_copy(build->domain);
189 dup->generated = isl_set_copy(build->generated);
190 dup->pending = isl_set_copy(build->pending);
191 dup->values = isl_multi_aff_copy(build->values);
192 dup->value = isl_pw_aff_copy(build->value);
193 dup->strides = isl_vec_copy(build->strides);
194 dup->offsets = isl_multi_aff_copy(build->offsets);
195 dup->executed = isl_union_map_copy(build->executed);
196 dup->single_valued = build->single_valued;
197 dup->options = isl_union_map_copy(build->options);
198 dup->at_each_domain = build->at_each_domain;
199 dup->at_each_domain_user = build->at_each_domain_user;
200 dup->before_each_for = build->before_each_for;
201 dup->before_each_for_user = build->before_each_for_user;
202 dup->after_each_for = build->after_each_for;
203 dup->after_each_for_user = build->after_each_for_user;
204 dup->create_leaf = build->create_leaf;
205 dup->create_leaf_user = build->create_leaf_user;
206 dup->node = isl_schedule_node_copy(build->node);
207 if (build->loop_type) {
208 int i;
210 dup->n = build->n;
211 dup->loop_type = isl_alloc_array(ctx,
212 enum isl_ast_loop_type, dup->n);
213 if (dup->n && !dup->loop_type)
214 return isl_ast_build_free(dup);
215 for (i = 0; i < dup->n; ++i)
216 dup->loop_type[i] = build->loop_type[i];
219 if (!dup->iterators || !dup->domain || !dup->generated ||
220 !dup->pending || !dup->values ||
221 !dup->strides || !dup->offsets || !dup->options ||
222 (build->executed && !dup->executed) ||
223 (build->value && !dup->value) ||
224 (build->node && !dup->node))
225 return isl_ast_build_free(dup);
227 return dup;
230 /* Align the parameters of "build" to those of "model", introducing
231 * additional parameters if needed.
233 __isl_give isl_ast_build *isl_ast_build_align_params(
234 __isl_take isl_ast_build *build, __isl_take isl_space *model)
236 build = isl_ast_build_cow(build);
237 if (!build)
238 goto error;
240 build->domain = isl_set_align_params(build->domain,
241 isl_space_copy(model));
242 build->generated = isl_set_align_params(build->generated,
243 isl_space_copy(model));
244 build->pending = isl_set_align_params(build->pending,
245 isl_space_copy(model));
246 build->values = isl_multi_aff_align_params(build->values,
247 isl_space_copy(model));
248 build->offsets = isl_multi_aff_align_params(build->offsets,
249 isl_space_copy(model));
250 build->options = isl_union_map_align_params(build->options,
251 isl_space_copy(model));
252 isl_space_free(model);
254 if (!build->domain || !build->values || !build->offsets ||
255 !build->options)
256 return isl_ast_build_free(build);
258 return build;
259 error:
260 isl_space_free(model);
261 return NULL;
264 __isl_give isl_ast_build *isl_ast_build_cow(__isl_take isl_ast_build *build)
266 if (!build)
267 return NULL;
269 if (build->ref == 1)
270 return build;
271 build->ref--;
272 return isl_ast_build_dup(build);
275 __isl_null isl_ast_build *isl_ast_build_free(
276 __isl_take isl_ast_build *build)
278 if (!build)
279 return NULL;
281 if (--build->ref > 0)
282 return NULL;
284 isl_id_list_free(build->iterators);
285 isl_set_free(build->domain);
286 isl_set_free(build->generated);
287 isl_set_free(build->pending);
288 isl_multi_aff_free(build->values);
289 isl_pw_aff_free(build->value);
290 isl_vec_free(build->strides);
291 isl_multi_aff_free(build->offsets);
292 isl_multi_aff_free(build->schedule_map);
293 isl_union_map_free(build->executed);
294 isl_union_map_free(build->options);
295 isl_schedule_node_free(build->node);
296 free(build->loop_type);
298 free(build);
300 return NULL;
303 isl_ctx *isl_ast_build_get_ctx(__isl_keep isl_ast_build *build)
305 return build ? isl_set_get_ctx(build->domain) : NULL;
308 /* Replace build->options by "options".
310 __isl_give isl_ast_build *isl_ast_build_set_options(
311 __isl_take isl_ast_build *build, __isl_take isl_union_map *options)
313 build = isl_ast_build_cow(build);
315 if (!build || !options)
316 goto error;
318 isl_union_map_free(build->options);
319 build->options = options;
321 return build;
322 error:
323 isl_union_map_free(options);
324 return isl_ast_build_free(build);
327 /* Set the iterators for the next code generation.
329 * If we still have some iterators left from the previous code generation
330 * (if any) or if iterators have already been set by a previous
331 * call to this function, then we remove them first.
333 __isl_give isl_ast_build *isl_ast_build_set_iterators(
334 __isl_take isl_ast_build *build, __isl_take isl_id_list *iterators)
336 int dim, n_it;
338 build = isl_ast_build_cow(build);
339 if (!build)
340 goto error;
342 dim = isl_set_dim(build->domain, isl_dim_set);
343 n_it = isl_id_list_n_id(build->iterators);
344 if (n_it < dim)
345 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
346 "isl_ast_build in inconsistent state", goto error);
347 if (n_it > dim)
348 build->iterators = isl_id_list_drop(build->iterators,
349 dim, n_it - dim);
350 build->iterators = isl_id_list_concat(build->iterators, iterators);
351 if (!build->iterators)
352 return isl_ast_build_free(build);
354 return build;
355 error:
356 isl_id_list_free(iterators);
357 return isl_ast_build_free(build);
360 /* Set the "at_each_domain" callback of "build" to "fn".
362 __isl_give isl_ast_build *isl_ast_build_set_at_each_domain(
363 __isl_take isl_ast_build *build,
364 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
365 __isl_keep isl_ast_build *build, void *user), void *user)
367 build = isl_ast_build_cow(build);
369 if (!build)
370 return NULL;
372 build->at_each_domain = fn;
373 build->at_each_domain_user = user;
375 return build;
378 /* Set the "before_each_for" callback of "build" to "fn".
380 __isl_give isl_ast_build *isl_ast_build_set_before_each_for(
381 __isl_take isl_ast_build *build,
382 __isl_give isl_id *(*fn)(__isl_keep isl_ast_build *build,
383 void *user), void *user)
385 build = isl_ast_build_cow(build);
387 if (!build)
388 return NULL;
390 build->before_each_for = fn;
391 build->before_each_for_user = user;
393 return build;
396 /* Set the "after_each_for" callback of "build" to "fn".
398 __isl_give isl_ast_build *isl_ast_build_set_after_each_for(
399 __isl_take isl_ast_build *build,
400 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
401 __isl_keep isl_ast_build *build, void *user), void *user)
403 build = isl_ast_build_cow(build);
405 if (!build)
406 return NULL;
408 build->after_each_for = fn;
409 build->after_each_for_user = user;
411 return build;
414 /* Set the "create_leaf" callback of "build" to "fn".
416 __isl_give isl_ast_build *isl_ast_build_set_create_leaf(
417 __isl_take isl_ast_build *build,
418 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_build *build,
419 void *user), void *user)
421 build = isl_ast_build_cow(build);
423 if (!build)
424 return NULL;
426 build->create_leaf = fn;
427 build->create_leaf_user = user;
429 return build;
432 /* Clear all information that is specific to this code generation
433 * and that is (probably) not meaningful to any nested code generation.
435 __isl_give isl_ast_build *isl_ast_build_clear_local_info(
436 __isl_take isl_ast_build *build)
438 isl_space *space;
440 build = isl_ast_build_cow(build);
441 if (!build)
442 return NULL;
444 space = isl_union_map_get_space(build->options);
445 isl_union_map_free(build->options);
446 build->options = isl_union_map_empty(space);
448 build->at_each_domain = NULL;
449 build->at_each_domain_user = NULL;
450 build->before_each_for = NULL;
451 build->before_each_for_user = NULL;
452 build->after_each_for = NULL;
453 build->after_each_for_user = NULL;
454 build->create_leaf = NULL;
455 build->create_leaf_user = NULL;
457 if (!build->options)
458 return isl_ast_build_free(build);
460 return build;
463 /* Have any loops been eliminated?
464 * That is, do any of the original schedule dimensions have a fixed
465 * value that has been substituted?
467 static int any_eliminated(isl_ast_build *build)
469 int i;
471 for (i = 0; i < build->depth; ++i)
472 if (isl_ast_build_has_affine_value(build, i))
473 return 1;
475 return 0;
478 /* Clear build->schedule_map.
479 * This function should be called whenever anything that might affect
480 * the result of isl_ast_build_get_schedule_map_multi_aff changes.
481 * In particular, it should be called when the depth is changed or
482 * when an iterator is determined to have a fixed value.
484 static void isl_ast_build_reset_schedule_map(__isl_keep isl_ast_build *build)
486 if (!build)
487 return;
488 isl_multi_aff_free(build->schedule_map);
489 build->schedule_map = NULL;
492 /* Do we need a (non-trivial) schedule map?
493 * That is, is the internal schedule space different from
494 * the external schedule space?
496 * The internal and external schedule spaces are only the same
497 * if code has been generated for the entire schedule and if none
498 * of the loops have been eliminated.
500 __isl_give int isl_ast_build_need_schedule_map(__isl_keep isl_ast_build *build)
502 int dim;
504 if (!build)
505 return -1;
507 dim = isl_set_dim(build->domain, isl_dim_set);
508 return build->depth != dim || any_eliminated(build);
511 /* Return a mapping from the internal schedule space to the external
512 * schedule space in the form of an isl_multi_aff.
513 * The internal schedule space originally corresponds to that of the
514 * input schedule. This may change during the code generation if
515 * if isl_ast_build_insert_dim is ever called.
516 * The external schedule space corresponds to the
517 * loops that have been generated.
519 * Currently, the only difference between the internal schedule domain
520 * and the external schedule domain is that some dimensions are projected
521 * out in the external schedule domain. In particular, the dimensions
522 * for which no code has been generated yet and the dimensions that correspond
523 * to eliminated loops.
525 * We cache a copy of the schedule_map in build->schedule_map.
526 * The cache is cleared through isl_ast_build_reset_schedule_map
527 * whenever anything changes that might affect the result of this function.
529 __isl_give isl_multi_aff *isl_ast_build_get_schedule_map_multi_aff(
530 __isl_keep isl_ast_build *build)
532 isl_space *space;
533 isl_multi_aff *ma;
535 if (!build)
536 return NULL;
537 if (build->schedule_map)
538 return isl_multi_aff_copy(build->schedule_map);
540 space = isl_ast_build_get_space(build, 1);
541 space = isl_space_map_from_set(space);
542 ma = isl_multi_aff_identity(space);
543 if (isl_ast_build_need_schedule_map(build)) {
544 int i;
545 int dim = isl_set_dim(build->domain, isl_dim_set);
546 ma = isl_multi_aff_drop_dims(ma, isl_dim_out,
547 build->depth, dim - build->depth);
548 for (i = build->depth - 1; i >= 0; --i)
549 if (isl_ast_build_has_affine_value(build, i))
550 ma = isl_multi_aff_drop_dims(ma,
551 isl_dim_out, i, 1);
554 build->schedule_map = ma;
555 return isl_multi_aff_copy(build->schedule_map);
558 /* Return a mapping from the internal schedule space to the external
559 * schedule space in the form of an isl_map.
561 __isl_give isl_map *isl_ast_build_get_schedule_map(
562 __isl_keep isl_ast_build *build)
564 isl_multi_aff *ma;
566 ma = isl_ast_build_get_schedule_map_multi_aff(build);
567 return isl_map_from_multi_aff(ma);
570 /* Return the position of the dimension in build->domain for which
571 * an AST node is currently being generated.
573 int isl_ast_build_get_depth(__isl_keep isl_ast_build *build)
575 return build ? build->depth : -1;
578 /* Prepare for generating code for the next level.
579 * In particular, increase the depth and reset any information
580 * that is local to the current depth.
582 __isl_give isl_ast_build *isl_ast_build_increase_depth(
583 __isl_take isl_ast_build *build)
585 build = isl_ast_build_cow(build);
586 if (!build)
587 return NULL;
588 build->depth++;
589 isl_ast_build_reset_schedule_map(build);
590 build->value = isl_pw_aff_free(build->value);
591 return build;
594 void isl_ast_build_dump(__isl_keep isl_ast_build *build)
596 if (!build)
597 return;
599 fprintf(stderr, "domain: ");
600 isl_set_dump(build->domain);
601 fprintf(stderr, "generated: ");
602 isl_set_dump(build->generated);
603 fprintf(stderr, "pending: ");
604 isl_set_dump(build->pending);
605 fprintf(stderr, "iterators: ");
606 isl_id_list_dump(build->iterators);
607 fprintf(stderr, "values: ");
608 isl_multi_aff_dump(build->values);
609 if (build->value) {
610 fprintf(stderr, "value: ");
611 isl_pw_aff_dump(build->value);
613 fprintf(stderr, "strides: ");
614 isl_vec_dump(build->strides);
615 fprintf(stderr, "offsets: ");
616 isl_multi_aff_dump(build->offsets);
619 /* Initialize "build" for AST construction in schedule space "space"
620 * in the case that build->domain is a parameter set.
622 * build->iterators is assumed to have been updated already.
624 static __isl_give isl_ast_build *isl_ast_build_init(
625 __isl_take isl_ast_build *build, __isl_take isl_space *space)
627 isl_set *set;
629 build = isl_ast_build_cow(build);
630 if (!build)
631 goto error;
633 set = isl_set_universe(isl_space_copy(space));
634 build->domain = isl_set_intersect_params(isl_set_copy(set),
635 build->domain);
636 build->pending = isl_set_intersect_params(isl_set_copy(set),
637 build->pending);
638 build->generated = isl_set_intersect_params(set, build->generated);
640 return isl_ast_build_init_derived(build, space);
641 error:
642 isl_ast_build_free(build);
643 isl_space_free(space);
644 return NULL;
647 /* Assign "aff" to *user and return -1, effectively extracting
648 * the first (and presumably only) affine expression in the isl_pw_aff
649 * on which this function is used.
651 static int extract_single_piece(__isl_take isl_set *set,
652 __isl_take isl_aff *aff, void *user)
654 isl_aff **p = user;
656 *p = aff;
657 isl_set_free(set);
659 return -1;
662 /* Intersect "set" with the stride constraint of "build", if any.
664 static __isl_give isl_set *intersect_stride_constraint(__isl_take isl_set *set,
665 __isl_keep isl_ast_build *build)
667 isl_set *stride;
669 if (!build)
670 return isl_set_free(set);
671 if (!isl_ast_build_has_stride(build, build->depth))
672 return set;
674 stride = isl_ast_build_get_stride_constraint(build);
675 return isl_set_intersect(set, stride);
678 /* Check if the given bounds on the current dimension (together with
679 * the stride constraint, if any) imply that
680 * this current dimension attains only a single value (in terms of
681 * parameters and outer dimensions).
682 * If so, we record it in build->value.
683 * If, moreover, this value can be represented as a single affine expression,
684 * then we also update build->values, effectively marking the current
685 * dimension as "eliminated".
687 * When computing the gist of the fixed value that can be represented
688 * as a single affine expression, it is important to only take into
689 * account the domain constraints in the original AST build and
690 * not the domain of the affine expression itself.
691 * Otherwise, a [i/3] is changed into a i/3 because we know that i
692 * is a multiple of 3, but then we end up not expressing anywhere
693 * in the context that i is a multiple of 3.
695 static __isl_give isl_ast_build *update_values(
696 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
698 int sv;
699 isl_pw_multi_aff *pma;
700 isl_aff *aff = NULL;
701 isl_map *it_map;
702 isl_set *set;
704 set = isl_set_from_basic_set(bounds);
705 set = isl_set_intersect(set, isl_set_copy(build->domain));
706 set = intersect_stride_constraint(set, build);
707 it_map = isl_ast_build_map_to_iterator(build, set);
709 sv = isl_map_is_single_valued(it_map);
710 if (sv < 0)
711 build = isl_ast_build_free(build);
712 if (!build || !sv) {
713 isl_map_free(it_map);
714 return build;
717 pma = isl_pw_multi_aff_from_map(it_map);
718 build->value = isl_pw_multi_aff_get_pw_aff(pma, 0);
719 build->value = isl_ast_build_compute_gist_pw_aff(build, build->value);
720 build->value = isl_pw_aff_coalesce(build->value);
721 isl_pw_multi_aff_free(pma);
723 if (!build->value)
724 return isl_ast_build_free(build);
726 if (isl_pw_aff_n_piece(build->value) != 1)
727 return build;
729 isl_pw_aff_foreach_piece(build->value, &extract_single_piece, &aff);
731 build->values = isl_multi_aff_set_aff(build->values, build->depth, aff);
732 if (!build->values)
733 return isl_ast_build_free(build);
734 isl_ast_build_reset_schedule_map(build);
735 return build;
738 /* Update the AST build based on the given loop bounds for
739 * the current dimension and the stride information available in the build.
741 * We first make sure that the bounds do not refer to any iterators
742 * that have already been eliminated.
743 * Then, we check if the bounds imply that the current iterator
744 * has a fixed value.
745 * If they do and if this fixed value can be expressed as a single
746 * affine expression, we eliminate the iterators from the bounds.
747 * Note that we cannot simply plug in this single value using
748 * isl_basic_set_preimage_multi_aff as the single value may only
749 * be defined on a subset of the domain. Plugging in the value
750 * would restrict the build domain to this subset, while this
751 * restriction may not be reflected in the generated code.
752 * Finally, we intersect build->domain with the updated bounds.
753 * We also add the stride constraint unless we have been able
754 * to find a fixed value expressed as a single affine expression.
756 * Note that the check for a fixed value in update_values requires
757 * us to intersect the bounds with the current build domain.
758 * When we intersect build->domain with the updated bounds in
759 * the final step, we make sure that these updated bounds have
760 * not been intersected with the old build->domain.
761 * Otherwise, we would indirectly intersect the build domain with itself,
762 * which can lead to inefficiencies, in particular if the build domain
763 * contains any unknown divs.
765 __isl_give isl_ast_build *isl_ast_build_set_loop_bounds(
766 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
768 isl_set *set;
770 build = isl_ast_build_cow(build);
771 if (!build)
772 goto error;
774 bounds = isl_basic_set_preimage_multi_aff(bounds,
775 isl_multi_aff_copy(build->values));
776 build = update_values(build, isl_basic_set_copy(bounds));
777 if (!build)
778 goto error;
779 set = isl_set_from_basic_set(isl_basic_set_copy(bounds));
780 if (isl_ast_build_has_affine_value(build, build->depth)) {
781 set = isl_set_eliminate(set, isl_dim_set, build->depth, 1);
782 set = isl_set_compute_divs(set);
783 build->pending = isl_set_intersect(build->pending,
784 isl_set_copy(set));
785 build->domain = isl_set_intersect(build->domain, set);
786 } else {
787 isl_basic_set *generated, *pending;
789 pending = isl_basic_set_copy(bounds);
790 pending = isl_basic_set_drop_constraints_involving_dims(pending,
791 isl_dim_set, build->depth, 1);
792 build->pending = isl_set_intersect(build->pending,
793 isl_set_from_basic_set(pending));
794 generated = isl_basic_set_copy(bounds);
795 generated = isl_basic_set_drop_constraints_not_involving_dims(
796 generated, isl_dim_set, build->depth, 1);
797 build->generated = isl_set_intersect(build->generated,
798 isl_set_from_basic_set(generated));
799 build->domain = isl_set_intersect(build->domain, set);
800 build = isl_ast_build_include_stride(build);
801 if (!build)
802 goto error;
804 isl_basic_set_free(bounds);
806 if (!build->domain || !build->pending || !build->generated)
807 return isl_ast_build_free(build);
809 return build;
810 error:
811 isl_ast_build_free(build);
812 isl_basic_set_free(bounds);
813 return NULL;
816 /* Intersect build->domain with "set", where "set" is specified
817 * in terms of the internal schedule domain.
819 static __isl_give isl_ast_build *isl_ast_build_restrict_internal(
820 __isl_take isl_ast_build *build, __isl_take isl_set *set)
822 build = isl_ast_build_cow(build);
823 if (!build)
824 goto error;
826 set = isl_set_compute_divs(set);
827 build->domain = isl_set_intersect(build->domain, set);
828 build->domain = isl_set_coalesce(build->domain);
830 if (!build->domain)
831 return isl_ast_build_free(build);
833 return build;
834 error:
835 isl_ast_build_free(build);
836 isl_set_free(set);
837 return NULL;
840 /* Intersect build->generated and build->domain with "set",
841 * where "set" is specified in terms of the internal schedule domain.
843 __isl_give isl_ast_build *isl_ast_build_restrict_generated(
844 __isl_take isl_ast_build *build, __isl_take isl_set *set)
846 set = isl_set_compute_divs(set);
847 build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
848 build = isl_ast_build_cow(build);
849 if (!build)
850 goto error;
852 build->generated = isl_set_intersect(build->generated, set);
853 build->generated = isl_set_coalesce(build->generated);
855 if (!build->generated)
856 return isl_ast_build_free(build);
858 return build;
859 error:
860 isl_ast_build_free(build);
861 isl_set_free(set);
862 return NULL;
865 /* Replace the set of pending constraints by "guard", which is then
866 * no longer considered as pending.
867 * That is, add "guard" to the generated constraints and clear all pending
868 * constraints, making the domain equal to the generated constraints.
870 __isl_give isl_ast_build *isl_ast_build_replace_pending_by_guard(
871 __isl_take isl_ast_build *build, __isl_take isl_set *guard)
873 build = isl_ast_build_restrict_generated(build, guard);
874 build = isl_ast_build_cow(build);
875 if (!build)
876 return NULL;
878 isl_set_free(build->domain);
879 build->domain = isl_set_copy(build->generated);
880 isl_set_free(build->pending);
881 build->pending = isl_set_universe(isl_set_get_space(build->domain));
883 if (!build->pending)
884 return isl_ast_build_free(build);
886 return build;
889 /* Intersect build->pending and build->domain with "set",
890 * where "set" is specified in terms of the internal schedule domain.
892 __isl_give isl_ast_build *isl_ast_build_restrict_pending(
893 __isl_take isl_ast_build *build, __isl_take isl_set *set)
895 set = isl_set_compute_divs(set);
896 build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
897 build = isl_ast_build_cow(build);
898 if (!build)
899 goto error;
901 build->pending = isl_set_intersect(build->pending, set);
902 build->pending = isl_set_coalesce(build->pending);
904 if (!build->pending)
905 return isl_ast_build_free(build);
907 return build;
908 error:
909 isl_ast_build_free(build);
910 isl_set_free(set);
911 return NULL;
914 /* Intersect build->domain with "set", where "set" is specified
915 * in terms of the external schedule domain.
917 __isl_give isl_ast_build *isl_ast_build_restrict(
918 __isl_take isl_ast_build *build, __isl_take isl_set *set)
920 if (isl_set_is_params(set))
921 return isl_ast_build_restrict_generated(build, set);
923 if (isl_ast_build_need_schedule_map(build)) {
924 isl_multi_aff *ma;
925 ma = isl_ast_build_get_schedule_map_multi_aff(build);
926 set = isl_set_preimage_multi_aff(set, ma);
928 return isl_ast_build_restrict_generated(build, set);
931 /* Replace build->executed by "executed".
933 __isl_give isl_ast_build *isl_ast_build_set_executed(
934 __isl_take isl_ast_build *build, __isl_take isl_union_map *executed)
936 build = isl_ast_build_cow(build);
937 if (!build)
938 goto error;
940 isl_union_map_free(build->executed);
941 build->executed = executed;
943 return build;
944 error:
945 isl_ast_build_free(build);
946 isl_union_map_free(executed);
947 return NULL;
950 /* Does "build" point to a band node?
951 * That is, are we currently handling a band node inside a schedule tree?
953 int isl_ast_build_has_schedule_node(__isl_keep isl_ast_build *build)
955 if (!build)
956 return -1;
957 return build->node != NULL;
960 /* Return a copy of the band node that "build" refers to.
962 __isl_give isl_schedule_node *isl_ast_build_get_schedule_node(
963 __isl_keep isl_ast_build *build)
965 if (!build)
966 return NULL;
967 return isl_schedule_node_copy(build->node);
970 /* Extract the loop AST generation types for the members of build->node
971 * and store them in build->loop_type.
973 static __isl_give isl_ast_build *extract_loop_types(
974 __isl_take isl_ast_build *build)
976 int i;
977 isl_ctx *ctx;
978 isl_schedule_node *node;
980 if (!build)
981 return NULL;
982 ctx = isl_ast_build_get_ctx(build);
983 if (!build->node)
984 isl_die(ctx, isl_error_internal, "missing AST node",
985 return isl_ast_build_free(build));
987 free(build->loop_type);
988 build->n = isl_schedule_node_band_n_member(build->node);
989 build->loop_type = isl_alloc_array(ctx,
990 enum isl_ast_loop_type, build->n);
991 if (build->n && !build->loop_type)
992 return isl_ast_build_free(build);
993 node = build->node;
994 for (i = 0; i < build->n; ++i)
995 build->loop_type[i] =
996 isl_schedule_node_band_member_get_ast_loop_type(node, i);
998 return build;
1001 /* Replace the band node that "build" refers to by "node" and
1002 * extract the corresponding loop AST generation types.
1004 __isl_give isl_ast_build *isl_ast_build_set_schedule_node(
1005 __isl_take isl_ast_build *build,
1006 __isl_take isl_schedule_node *node)
1008 build = isl_ast_build_cow(build);
1009 if (!build || !node)
1010 goto error;
1012 isl_schedule_node_free(build->node);
1013 build->node = node;
1015 build = extract_loop_types(build);
1017 return build;
1018 error:
1019 isl_ast_build_free(build);
1020 isl_schedule_node_free(node);
1021 return NULL;
1024 /* Remove any reference to a band node from "build".
1026 __isl_give isl_ast_build *isl_ast_build_reset_schedule_node(
1027 __isl_take isl_ast_build *build)
1029 build = isl_ast_build_cow(build);
1030 if (!build)
1031 return NULL;
1033 isl_schedule_node_free(build->node);
1034 build->node = NULL;
1036 return build;
1039 /* Return a copy of the current schedule domain.
1041 __isl_give isl_set *isl_ast_build_get_domain(__isl_keep isl_ast_build *build)
1043 return build ? isl_set_copy(build->domain) : NULL;
1046 /* Return a copy of the set of pending constraints.
1048 __isl_give isl_set *isl_ast_build_get_pending(
1049 __isl_keep isl_ast_build *build)
1051 return build ? isl_set_copy(build->pending) : NULL;
1054 /* Return a copy of the set of generated constraints.
1056 __isl_give isl_set *isl_ast_build_get_generated(
1057 __isl_keep isl_ast_build *build)
1059 return build ? isl_set_copy(build->generated) : NULL;
1062 /* Return the number of variables of the given type
1063 * in the (internal) schedule space.
1065 unsigned isl_ast_build_dim(__isl_keep isl_ast_build *build,
1066 enum isl_dim_type type)
1068 if (!build)
1069 return 0;
1070 return isl_set_dim(build->domain, type);
1073 /* Return the (schedule) space of "build".
1075 * If "internal" is set, then this space is the space of the internal
1076 * representation of the entire schedule, including those parts for
1077 * which no code has been generated yet.
1079 * If "internal" is not set, then this space is the external representation
1080 * of the loops generated so far.
1082 __isl_give isl_space *isl_ast_build_get_space(__isl_keep isl_ast_build *build,
1083 int internal)
1085 int i;
1086 int dim;
1087 isl_space *space;
1089 if (!build)
1090 return NULL;
1092 space = isl_set_get_space(build->domain);
1093 if (internal)
1094 return space;
1096 if (!isl_ast_build_need_schedule_map(build))
1097 return space;
1099 dim = isl_set_dim(build->domain, isl_dim_set);
1100 space = isl_space_drop_dims(space, isl_dim_set,
1101 build->depth, dim - build->depth);
1102 for (i = build->depth - 1; i >= 0; --i)
1103 if (isl_ast_build_has_affine_value(build, i))
1104 space = isl_space_drop_dims(space, isl_dim_set, i, 1);
1106 return space;
1109 /* Return the external representation of the schedule space of "build",
1110 * i.e., a space with a dimension for each loop generated so far,
1111 * with the names of the dimensions set to the loop iterators.
1113 __isl_give isl_space *isl_ast_build_get_schedule_space(
1114 __isl_keep isl_ast_build *build)
1116 isl_space *space;
1117 int i, skip;
1119 if (!build)
1120 return NULL;
1122 space = isl_ast_build_get_space(build, 0);
1124 skip = 0;
1125 for (i = 0; i < build->depth; ++i) {
1126 isl_id *id;
1128 if (isl_ast_build_has_affine_value(build, i)) {
1129 skip++;
1130 continue;
1133 id = isl_ast_build_get_iterator_id(build, i);
1134 space = isl_space_set_dim_id(space, isl_dim_set, i - skip, id);
1137 return space;
1140 /* Return the current schedule, as stored in build->executed, in terms
1141 * of the external schedule domain.
1143 __isl_give isl_union_map *isl_ast_build_get_schedule(
1144 __isl_keep isl_ast_build *build)
1146 isl_union_map *executed;
1147 isl_union_map *schedule;
1149 if (!build)
1150 return NULL;
1152 executed = isl_union_map_copy(build->executed);
1153 if (isl_ast_build_need_schedule_map(build)) {
1154 isl_map *proj = isl_ast_build_get_schedule_map(build);
1155 executed = isl_union_map_apply_domain(executed,
1156 isl_union_map_from_map(proj));
1158 schedule = isl_union_map_reverse(executed);
1160 return schedule;
1163 /* Return the iterator attached to the internal schedule dimension "pos".
1165 __isl_give isl_id *isl_ast_build_get_iterator_id(
1166 __isl_keep isl_ast_build *build, int pos)
1168 if (!build)
1169 return NULL;
1171 return isl_id_list_get_id(build->iterators, pos);
1174 /* Set the stride and offset of the current dimension to the given
1175 * value and expression.
1177 * If we had already found a stride before, then the two strides
1178 * are combined into a single stride.
1180 * In particular, if the new stride information is of the form
1182 * i = f + s (...)
1184 * and the old stride information is of the form
1186 * i = f2 + s2 (...)
1188 * then we compute the extended gcd of s and s2
1190 * a s + b s2 = g,
1192 * with g = gcd(s,s2), multiply the first equation with t1 = b s2/g
1193 * and the second with t2 = a s1/g.
1194 * This results in
1196 * i = (b s2 + a s1)/g i = t1 f + t2 f2 + (s s2)/g (...)
1198 * so that t1 f + t2 f2 is the combined offset and (s s2)/g = lcm(s,s2)
1199 * is the combined stride.
1201 static __isl_give isl_ast_build *set_stride(__isl_take isl_ast_build *build,
1202 __isl_take isl_val *stride, __isl_take isl_aff *offset)
1204 int pos;
1206 build = isl_ast_build_cow(build);
1207 if (!build || !stride || !offset)
1208 goto error;
1210 pos = build->depth;
1212 if (isl_ast_build_has_stride(build, pos)) {
1213 isl_val *stride2, *a, *b, *g;
1214 isl_aff *offset2;
1216 stride2 = isl_vec_get_element_val(build->strides, pos);
1217 g = isl_val_gcdext(isl_val_copy(stride), isl_val_copy(stride2),
1218 &a, &b);
1219 a = isl_val_mul(a, isl_val_copy(stride));
1220 a = isl_val_div(a, isl_val_copy(g));
1221 stride2 = isl_val_div(stride2, g);
1222 b = isl_val_mul(b, isl_val_copy(stride2));
1223 stride = isl_val_mul(stride, stride2);
1225 offset2 = isl_multi_aff_get_aff(build->offsets, pos);
1226 offset2 = isl_aff_scale_val(offset2, a);
1227 offset = isl_aff_scale_val(offset, b);
1228 offset = isl_aff_add(offset, offset2);
1231 build->strides = isl_vec_set_element_val(build->strides, pos, stride);
1232 build->offsets = isl_multi_aff_set_aff(build->offsets, pos, offset);
1233 if (!build->strides || !build->offsets)
1234 return isl_ast_build_free(build);
1236 return build;
1237 error:
1238 isl_val_free(stride);
1239 isl_aff_free(offset);
1240 return isl_ast_build_free(build);
1243 /* Return a set expressing the stride constraint at the current depth.
1245 * In particular, if the current iterator (i) is known to attain values
1247 * f + s a
1249 * where f is the offset and s is the stride, then the returned set
1250 * expresses the constraint
1252 * (f - i) mod s = 0
1254 __isl_give isl_set *isl_ast_build_get_stride_constraint(
1255 __isl_keep isl_ast_build *build)
1257 isl_aff *aff;
1258 isl_set *set;
1259 isl_val *stride;
1260 int pos;
1262 if (!build)
1263 return NULL;
1265 pos = build->depth;
1267 if (!isl_ast_build_has_stride(build, pos))
1268 return isl_set_universe(isl_ast_build_get_space(build, 1));
1270 stride = isl_ast_build_get_stride(build, pos);
1271 aff = isl_ast_build_get_offset(build, pos);
1272 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, -1);
1273 aff = isl_aff_mod_val(aff, stride);
1274 set = isl_set_from_basic_set(isl_aff_zero_basic_set(aff));
1276 return set;
1279 /* Return the expansion implied by the stride and offset at the current
1280 * depth.
1282 * That is, return the mapping
1284 * [i_0, ..., i_{d-1}, i_d, i_{d+1}, ...]
1285 * -> [i_0, ..., i_{d-1}, s * i_d + offset(i), i_{d+1}, ...]
1287 * where s is the stride at the current depth d and offset(i) is
1288 * the corresponding offset.
1290 __isl_give isl_multi_aff *isl_ast_build_get_stride_expansion(
1291 __isl_keep isl_ast_build *build)
1293 isl_space *space;
1294 isl_multi_aff *ma;
1295 int pos;
1296 isl_aff *aff, *offset;
1297 isl_val *stride;
1299 if (!build)
1300 return NULL;
1302 pos = isl_ast_build_get_depth(build);
1303 space = isl_ast_build_get_space(build, 1);
1304 space = isl_space_map_from_set(space);
1305 ma = isl_multi_aff_identity(space);
1307 if (!isl_ast_build_has_stride(build, pos))
1308 return ma;
1310 offset = isl_ast_build_get_offset(build, pos);
1311 stride = isl_ast_build_get_stride(build, pos);
1312 aff = isl_multi_aff_get_aff(ma, pos);
1313 aff = isl_aff_scale_val(aff, stride);
1314 aff = isl_aff_add(aff, offset);
1315 ma = isl_multi_aff_set_aff(ma, pos, aff);
1317 return ma;
1320 /* Add constraints corresponding to any previously detected
1321 * stride on the current dimension to build->domain.
1323 __isl_give isl_ast_build *isl_ast_build_include_stride(
1324 __isl_take isl_ast_build *build)
1326 isl_set *set;
1328 if (!build)
1329 return NULL;
1330 if (!isl_ast_build_has_stride(build, build->depth))
1331 return build;
1332 build = isl_ast_build_cow(build);
1333 if (!build)
1334 return NULL;
1336 set = isl_ast_build_get_stride_constraint(build);
1338 build->domain = isl_set_intersect(build->domain, isl_set_copy(set));
1339 build->generated = isl_set_intersect(build->generated, set);
1340 if (!build->domain || !build->generated)
1341 return isl_ast_build_free(build);
1343 return build;
1346 /* Information used inside detect_stride.
1348 * "build" may be updated by detect_stride to include stride information.
1349 * "pos" is equal to build->depth.
1351 struct isl_detect_stride_data {
1352 isl_ast_build *build;
1353 int pos;
1356 /* Check if constraint "c" imposes any stride on dimension data->pos
1357 * and, if so, update the stride information in data->build.
1359 * In order to impose a stride on the dimension, "c" needs to be an equality
1360 * and it needs to involve the dimension. Note that "c" may also be
1361 * a div constraint and thus an inequality that we cannot use.
1363 * Let c be of the form
1365 * h(p) + g * v * i + g * stride * f(alpha) = 0
1367 * with h(p) an expression in terms of the parameters and outer dimensions
1368 * and f(alpha) an expression in terms of the existentially quantified
1369 * variables. Note that the inner dimensions have been eliminated so
1370 * they do not appear in "c".
1372 * If "stride" is not zero and not one, then it represents a non-trivial stride
1373 * on "i". We compute a and b such that
1375 * a v + b stride = 1
1377 * We have
1379 * g v i = -h(p) + g stride f(alpha)
1381 * a g v i = -a h(p) + g stride f(alpha)
1383 * a g v i + b g stride i = -a h(p) + g stride * (...)
1385 * g i = -a h(p) + g stride * (...)
1387 * i = -a h(p)/g + stride * (...)
1389 * The expression "-a h(p)/g" can therefore be used as offset.
1391 static int detect_stride(__isl_take isl_constraint *c, void *user)
1393 struct isl_detect_stride_data *data = user;
1394 int i, n_div;
1395 isl_ctx *ctx;
1396 isl_val *v, *stride, *m;
1398 if (!isl_constraint_is_equality(c) ||
1399 !isl_constraint_involves_dims(c, isl_dim_set, data->pos, 1)) {
1400 isl_constraint_free(c);
1401 return 0;
1404 ctx = isl_constraint_get_ctx(c);
1405 stride = isl_val_zero(ctx);
1406 n_div = isl_constraint_dim(c, isl_dim_div);
1407 for (i = 0; i < n_div; ++i) {
1408 v = isl_constraint_get_coefficient_val(c, isl_dim_div, i);
1409 stride = isl_val_gcd(stride, v);
1412 v = isl_constraint_get_coefficient_val(c, isl_dim_set, data->pos);
1413 m = isl_val_gcd(isl_val_copy(stride), isl_val_copy(v));
1414 stride = isl_val_div(stride, isl_val_copy(m));
1415 v = isl_val_div(v, isl_val_copy(m));
1417 if (!isl_val_is_zero(stride) && !isl_val_is_one(stride)) {
1418 isl_aff *aff;
1419 isl_val *gcd, *a, *b;
1421 gcd = isl_val_gcdext(v, isl_val_copy(stride), &a, &b);
1422 isl_val_free(gcd);
1423 isl_val_free(b);
1425 aff = isl_constraint_get_aff(c);
1426 for (i = 0; i < n_div; ++i)
1427 aff = isl_aff_set_coefficient_si(aff,
1428 isl_dim_div, i, 0);
1429 aff = isl_aff_set_coefficient_si(aff, isl_dim_in, data->pos, 0);
1430 a = isl_val_neg(a);
1431 aff = isl_aff_scale_val(aff, a);
1432 aff = isl_aff_scale_down_val(aff, m);
1433 data->build = set_stride(data->build, stride, aff);
1434 } else {
1435 isl_val_free(stride);
1436 isl_val_free(m);
1437 isl_val_free(v);
1440 isl_constraint_free(c);
1441 return 0;
1444 /* Check if the constraints in "set" imply any stride on the current
1445 * dimension and, if so, record the stride information in "build"
1446 * and return the updated "build".
1448 * We compute the affine hull and then check if any of the constraints
1449 * in the hull imposes any stride on the current dimension.
1451 * We assume that inner dimensions have been eliminated from "set"
1452 * by the caller. This is needed because the common stride
1453 * may be imposed by different inner dimensions on different parts of
1454 * the domain.
1456 __isl_give isl_ast_build *isl_ast_build_detect_strides(
1457 __isl_take isl_ast_build *build, __isl_take isl_set *set)
1459 isl_basic_set *hull;
1460 struct isl_detect_stride_data data;
1462 if (!build)
1463 goto error;
1465 data.build = build;
1466 data.pos = isl_ast_build_get_depth(build);
1467 hull = isl_set_affine_hull(set);
1469 if (isl_basic_set_foreach_constraint(hull, &detect_stride, &data) < 0)
1470 data.build = isl_ast_build_free(data.build);
1472 isl_basic_set_free(hull);
1473 return data.build;
1474 error:
1475 isl_set_free(set);
1476 return NULL;
1479 struct isl_ast_build_involves_data {
1480 int depth;
1481 int involves;
1484 /* Check if "map" involves the input dimension data->depth.
1486 static int involves_depth(__isl_take isl_map *map, void *user)
1488 struct isl_ast_build_involves_data *data = user;
1490 data->involves = isl_map_involves_dims(map, isl_dim_in, data->depth, 1);
1491 isl_map_free(map);
1493 if (data->involves < 0 || data->involves)
1494 return -1;
1495 return 0;
1498 /* Do any options depend on the value of the dimension at the current depth?
1500 int isl_ast_build_options_involve_depth(__isl_keep isl_ast_build *build)
1502 struct isl_ast_build_involves_data data;
1504 if (!build)
1505 return -1;
1507 data.depth = build->depth;
1508 data.involves = 0;
1510 if (isl_union_map_foreach_map(build->options,
1511 &involves_depth, &data) < 0) {
1512 if (data.involves < 0 || !data.involves)
1513 return -1;
1516 return data.involves;
1519 /* Construct the map
1521 * { [i] -> [i] : i < pos; [i] -> [i + 1] : i >= pos }
1523 * with "space" the parameter space of the constructed map.
1525 static __isl_give isl_map *construct_insertion_map(__isl_take isl_space *space,
1526 int pos)
1528 isl_constraint *c;
1529 isl_basic_map *bmap1, *bmap2;
1531 space = isl_space_set_from_params(space);
1532 space = isl_space_add_dims(space, isl_dim_set, 1);
1533 space = isl_space_map_from_set(space);
1534 c = isl_equality_alloc(isl_local_space_from_space(space));
1535 c = isl_constraint_set_coefficient_si(c, isl_dim_in, 0, 1);
1536 c = isl_constraint_set_coefficient_si(c, isl_dim_out, 0, -1);
1537 bmap1 = isl_basic_map_from_constraint(isl_constraint_copy(c));
1538 c = isl_constraint_set_constant_si(c, 1);
1539 bmap2 = isl_basic_map_from_constraint(c);
1541 bmap1 = isl_basic_map_upper_bound_si(bmap1, isl_dim_in, 0, pos - 1);
1542 bmap2 = isl_basic_map_lower_bound_si(bmap2, isl_dim_in, 0, pos);
1544 return isl_basic_map_union(bmap1, bmap2);
1547 static const char *option_str[] = {
1548 [isl_ast_loop_atomic] = "atomic",
1549 [isl_ast_loop_unroll] = "unroll",
1550 [isl_ast_loop_separate] = "separate"
1553 /* Update the "options" to reflect the insertion of a dimension
1554 * at position "pos" in the schedule domain space.
1555 * "space" is the original domain space before the insertion and
1556 * may be named and/or structured.
1558 * The (relevant) input options all have "space" as domain, which
1559 * has to be mapped to the extended space.
1560 * The values of the ranges also refer to the schedule domain positions
1561 * and they therefore also need to be adjusted. In particular, values
1562 * smaller than pos do not need to change, while values greater than or
1563 * equal to pos need to be incremented.
1564 * That is, we need to apply the following map.
1566 * { atomic[i] -> atomic[i] : i < pos; [i] -> [i + 1] : i >= pos;
1567 * unroll[i] -> unroll[i] : i < pos; [i] -> [i + 1] : i >= pos;
1568 * separate[i] -> separate[i] : i < pos; [i] -> [i + 1] : i >= pos;
1569 * separation_class[[i] -> [c]]
1570 * -> separation_class[[i] -> [c]] : i < pos;
1571 * separation_class[[i] -> [c]]
1572 * -> separation_class[[i + 1] -> [c]] : i >= pos }
1574 static __isl_give isl_union_map *options_insert_dim(
1575 __isl_take isl_union_map *options, __isl_take isl_space *space, int pos)
1577 isl_map *map;
1578 isl_union_map *insertion;
1579 enum isl_ast_loop_type type;
1580 const char *name = "separation_class";
1582 space = isl_space_map_from_set(space);
1583 map = isl_map_identity(space);
1584 map = isl_map_insert_dims(map, isl_dim_out, pos, 1);
1585 options = isl_union_map_apply_domain(options,
1586 isl_union_map_from_map(map));
1588 if (!options)
1589 return NULL;
1591 map = construct_insertion_map(isl_union_map_get_space(options), pos);
1593 insertion = isl_union_map_empty(isl_union_map_get_space(options));
1595 for (type = isl_ast_loop_atomic;
1596 type <= isl_ast_loop_separate; ++type) {
1597 isl_map *map_type = isl_map_copy(map);
1598 const char *name = option_str[type];
1599 map_type = isl_map_set_tuple_name(map_type, isl_dim_in, name);
1600 map_type = isl_map_set_tuple_name(map_type, isl_dim_out, name);
1601 insertion = isl_union_map_add_map(insertion, map_type);
1604 map = isl_map_product(map, isl_map_identity(isl_map_get_space(map)));
1605 map = isl_map_set_tuple_name(map, isl_dim_in, name);
1606 map = isl_map_set_tuple_name(map, isl_dim_out, name);
1607 insertion = isl_union_map_add_map(insertion, map);
1609 options = isl_union_map_apply_range(options, insertion);
1611 return options;
1614 /* If we are generating an AST from a schedule tree (build->node is set),
1615 * then update the loop AST generation types
1616 * to reflect the insertion of a dimension at (global) position "pos"
1617 * in the schedule domain space.
1619 static __isl_give isl_ast_build *node_insert_dim(
1620 __isl_take isl_ast_build *build, int pos)
1622 int i;
1623 int local_pos;
1624 enum isl_ast_loop_type *loop_type;
1625 isl_ctx *ctx;
1627 build = isl_ast_build_cow(build);
1628 if (!build)
1629 return NULL;
1630 if (!build->node)
1631 return build;
1633 ctx = isl_ast_build_get_ctx(build);
1634 local_pos = pos - build->outer_pos;
1635 loop_type = isl_realloc_array(ctx, build->loop_type,
1636 enum isl_ast_loop_type, build->n + 1);
1637 if (!loop_type)
1638 return isl_ast_build_free(build);
1639 build->loop_type = loop_type;
1640 for (i = build->n - 1; i >= local_pos; --i)
1641 loop_type[i + 1] = loop_type[i];
1642 loop_type[local_pos] = isl_ast_loop_default;
1643 build->n++;
1645 return build;
1648 /* Insert a single dimension in the schedule domain at position "pos".
1649 * The new dimension is given an isl_id with the empty string as name.
1651 * The main difficulty is updating build->options to reflect the
1652 * extra dimension. This is handled in options_insert_dim.
1654 * Note that because of the dimension manipulations, the resulting
1655 * schedule domain space will always be unnamed and unstructured.
1656 * However, the original schedule domain space may be named and/or
1657 * structured, so we have to take this possibility into account
1658 * while performing the transformations.
1660 __isl_give isl_ast_build *isl_ast_build_insert_dim(
1661 __isl_take isl_ast_build *build, int pos)
1663 isl_ctx *ctx;
1664 isl_space *space, *ma_space;
1665 isl_id *id;
1666 isl_multi_aff *ma;
1668 build = isl_ast_build_cow(build);
1669 if (!build)
1670 return NULL;
1672 ctx = isl_ast_build_get_ctx(build);
1673 id = isl_id_alloc(ctx, "", NULL);
1674 if (!build->node)
1675 space = isl_ast_build_get_space(build, 1);
1676 build->iterators = isl_id_list_insert(build->iterators, pos, id);
1677 build->domain = isl_set_insert_dims(build->domain,
1678 isl_dim_set, pos, 1);
1679 build->generated = isl_set_insert_dims(build->generated,
1680 isl_dim_set, pos, 1);
1681 build->pending = isl_set_insert_dims(build->pending,
1682 isl_dim_set, pos, 1);
1683 build->strides = isl_vec_insert_els(build->strides, pos, 1);
1684 build->strides = isl_vec_set_element_si(build->strides, pos, 1);
1685 ma_space = isl_space_params(isl_multi_aff_get_space(build->offsets));
1686 ma_space = isl_space_set_from_params(ma_space);
1687 ma_space = isl_space_add_dims(ma_space, isl_dim_set, 1);
1688 ma_space = isl_space_map_from_set(ma_space);
1689 ma = isl_multi_aff_zero(isl_space_copy(ma_space));
1690 build->offsets = isl_multi_aff_splice(build->offsets, pos, pos, ma);
1691 ma = isl_multi_aff_identity(ma_space);
1692 build->values = isl_multi_aff_splice(build->values, pos, pos, ma);
1693 if (!build->node)
1694 build->options = options_insert_dim(build->options, space, pos);
1696 if (!build->iterators || !build->domain || !build->generated ||
1697 !build->pending || !build->values ||
1698 !build->strides || !build->offsets || !build->options)
1699 return isl_ast_build_free(build);
1701 build = node_insert_dim(build, pos);
1703 return build;
1706 /* Scale down the current dimension by a factor of "m".
1707 * "umap" is an isl_union_map that implements the scaling down.
1708 * That is, it is of the form
1710 * { [.... i ....] -> [.... i' ....] : i = m i' }
1712 * This function is called right after the strides have been
1713 * detected, but before any constraints on the current dimension
1714 * have been included in build->domain.
1715 * We therefore only need to update stride, offset and the options.
1717 __isl_give isl_ast_build *isl_ast_build_scale_down(
1718 __isl_take isl_ast_build *build, __isl_take isl_val *m,
1719 __isl_take isl_union_map *umap)
1721 isl_aff *aff;
1722 isl_val *v;
1723 int depth;
1725 build = isl_ast_build_cow(build);
1726 if (!build || !umap || !m)
1727 goto error;
1729 depth = build->depth;
1731 v = isl_vec_get_element_val(build->strides, depth);
1732 v = isl_val_div(v, isl_val_copy(m));
1733 build->strides = isl_vec_set_element_val(build->strides, depth, v);
1735 aff = isl_multi_aff_get_aff(build->offsets, depth);
1736 aff = isl_aff_scale_down_val(aff, m);
1737 build->offsets = isl_multi_aff_set_aff(build->offsets, depth, aff);
1738 build->options = isl_union_map_apply_domain(build->options, umap);
1739 if (!build->strides || !build->offsets || !build->options)
1740 return isl_ast_build_free(build);
1742 return build;
1743 error:
1744 isl_val_free(m);
1745 isl_union_map_free(umap);
1746 return isl_ast_build_free(build);
1749 /* Return a list of "n" isl_ids called "c%d", with "%d" starting at "first".
1750 * If an isl_id with such a name already appears among the parameters
1751 * in build->domain, then adjust the name to "c%d_%d".
1753 static __isl_give isl_id_list *generate_names(isl_ctx *ctx, int n, int first,
1754 __isl_keep isl_ast_build *build)
1756 int i;
1757 isl_id_list *names;
1759 names = isl_id_list_alloc(ctx, n);
1760 for (i = 0; i < n; ++i) {
1761 isl_id *id;
1763 id = generate_name(ctx, first + i, build);
1764 names = isl_id_list_add(names, id);
1767 return names;
1770 /* Embed "options" into the given isl_ast_build space.
1772 * This function is called from within a nested call to
1773 * isl_ast_build_node_from_schedule_map.
1774 * "options" refers to the additional schedule,
1775 * while space refers to both the space of the outer isl_ast_build and
1776 * that of the additional schedule.
1777 * Specifically, space is of the form
1779 * [I -> S]
1781 * while options lives in the space(s)
1783 * S -> *
1785 * We compute
1787 * [I -> S] -> S
1789 * and compose this with options, to obtain the new options
1790 * living in the space(s)
1792 * [I -> S] -> *
1794 static __isl_give isl_union_map *embed_options(
1795 __isl_take isl_union_map *options, __isl_take isl_space *space)
1797 isl_map *map;
1799 map = isl_map_universe(isl_space_unwrap(space));
1800 map = isl_map_range_map(map);
1802 options = isl_union_map_apply_range(
1803 isl_union_map_from_map(map), options);
1805 return options;
1808 /* Update "build" for use in a (possibly nested) code generation. That is,
1809 * extend "build" from an AST build on some domain O to an AST build
1810 * on domain [O -> S], with S corresponding to "space".
1811 * If the original domain is a parameter domain, then the new domain is
1812 * simply S.
1813 * "iterators" is a list of iterators for S, but the number of elements
1814 * may be smaller or greater than the number of set dimensions of S.
1815 * If "keep_iterators" is set, then any extra ids in build->iterators
1816 * are reused for S. Otherwise, these extra ids are dropped.
1818 * We first update build->outer_pos to the current depth.
1819 * This depth is zero in case this is the outermost code generation.
1821 * We then add additional ids such that the number of iterators is at least
1822 * equal to the dimension of the new build domain.
1824 * If the original domain is parametric, then we are constructing
1825 * an isl_ast_build for the outer code generation and we pass control
1826 * to isl_ast_build_init.
1828 * Otherwise, we adjust the fields of "build" to include "space".
1830 __isl_give isl_ast_build *isl_ast_build_product(
1831 __isl_take isl_ast_build *build, __isl_take isl_space *space)
1833 isl_ctx *ctx;
1834 isl_vec *strides;
1835 isl_set *set;
1836 isl_multi_aff *embedding;
1837 int dim, n_it;
1839 build = isl_ast_build_cow(build);
1840 if (!build)
1841 goto error;
1843 build->outer_pos = build->depth;
1845 ctx = isl_ast_build_get_ctx(build);
1846 dim = isl_set_dim(build->domain, isl_dim_set);
1847 dim += isl_space_dim(space, isl_dim_set);
1848 n_it = isl_id_list_n_id(build->iterators);
1849 if (n_it < dim) {
1850 isl_id_list *l;
1851 l = generate_names(ctx, dim - n_it, n_it, build);
1852 build->iterators = isl_id_list_concat(build->iterators, l);
1855 if (isl_set_is_params(build->domain))
1856 return isl_ast_build_init(build, space);
1858 set = isl_set_universe(isl_space_copy(space));
1859 build->domain = isl_set_product(build->domain, isl_set_copy(set));
1860 build->pending = isl_set_product(build->pending, isl_set_copy(set));
1861 build->generated = isl_set_product(build->generated, set);
1863 strides = isl_vec_alloc(ctx, isl_space_dim(space, isl_dim_set));
1864 strides = isl_vec_set_si(strides, 1);
1865 build->strides = isl_vec_concat(build->strides, strides);
1867 space = isl_space_map_from_set(space);
1868 build->offsets = isl_multi_aff_align_params(build->offsets,
1869 isl_space_copy(space));
1870 build->offsets = isl_multi_aff_product(build->offsets,
1871 isl_multi_aff_zero(isl_space_copy(space)));
1872 build->values = isl_multi_aff_align_params(build->values,
1873 isl_space_copy(space));
1874 embedding = isl_multi_aff_identity(space);
1875 build->values = isl_multi_aff_product(build->values, embedding);
1877 space = isl_ast_build_get_space(build, 1);
1878 build->options = embed_options(build->options, space);
1880 if (!build->iterators || !build->domain || !build->generated ||
1881 !build->pending || !build->values ||
1882 !build->strides || !build->offsets || !build->options)
1883 return isl_ast_build_free(build);
1885 return build;
1886 error:
1887 isl_ast_build_free(build);
1888 isl_space_free(space);
1889 return NULL;
1892 /* Does "aff" only attain non-negative values over build->domain?
1893 * That is, does it not attain any negative values?
1895 int isl_ast_build_aff_is_nonneg(__isl_keep isl_ast_build *build,
1896 __isl_keep isl_aff *aff)
1898 isl_set *test;
1899 int empty;
1901 if (!build)
1902 return -1;
1904 aff = isl_aff_copy(aff);
1905 test = isl_set_from_basic_set(isl_aff_neg_basic_set(aff));
1906 test = isl_set_intersect(test, isl_set_copy(build->domain));
1907 empty = isl_set_is_empty(test);
1908 isl_set_free(test);
1910 return empty;
1913 /* Does the dimension at (internal) position "pos" have a non-trivial stride?
1915 int isl_ast_build_has_stride(__isl_keep isl_ast_build *build, int pos)
1917 isl_val *v;
1918 int has_stride;
1920 if (!build)
1921 return -1;
1923 v = isl_vec_get_element_val(build->strides, pos);
1924 if (!v)
1925 return -1;
1926 has_stride = !isl_val_is_one(v);
1927 isl_val_free(v);
1929 return has_stride;
1932 /* Given that the dimension at position "pos" takes on values
1934 * f + s a
1936 * with a an integer, return s through *stride.
1938 __isl_give isl_val *isl_ast_build_get_stride(__isl_keep isl_ast_build *build,
1939 int pos)
1941 if (!build)
1942 return NULL;
1944 return isl_vec_get_element_val(build->strides, pos);
1947 /* Given that the dimension at position "pos" takes on values
1949 * f + s a
1951 * with a an integer, return f.
1953 __isl_give isl_aff *isl_ast_build_get_offset(
1954 __isl_keep isl_ast_build *build, int pos)
1956 if (!build)
1957 return NULL;
1959 return isl_multi_aff_get_aff(build->offsets, pos);
1962 /* Is the dimension at position "pos" known to attain only a single
1963 * value that, moreover, can be described by a single affine expression
1964 * in terms of the outer dimensions and parameters?
1966 * If not, then the corresponding affine expression in build->values
1967 * is set to be equal to the same input dimension.
1968 * Otherwise, it is set to the requested expression in terms of
1969 * outer dimensions and parameters.
1971 int isl_ast_build_has_affine_value(__isl_keep isl_ast_build *build,
1972 int pos)
1974 isl_aff *aff;
1975 int involves;
1977 if (!build)
1978 return -1;
1980 aff = isl_multi_aff_get_aff(build->values, pos);
1981 involves = isl_aff_involves_dims(aff, isl_dim_in, pos, 1);
1982 isl_aff_free(aff);
1984 if (involves < 0)
1985 return -1;
1987 return !involves;
1990 /* Plug in the known values (fixed affine expressions in terms of
1991 * parameters and outer loop iterators) of all loop iterators
1992 * in the domain of "umap".
1994 * We simply precompose "umap" with build->values.
1996 __isl_give isl_union_map *isl_ast_build_substitute_values_union_map_domain(
1997 __isl_keep isl_ast_build *build, __isl_take isl_union_map *umap)
1999 isl_multi_aff *values;
2001 if (!build)
2002 return isl_union_map_free(umap);
2004 values = isl_multi_aff_copy(build->values);
2005 umap = isl_union_map_preimage_domain_multi_aff(umap, values);
2007 return umap;
2010 /* Is the current dimension known to attain only a single value?
2012 int isl_ast_build_has_value(__isl_keep isl_ast_build *build)
2014 if (!build)
2015 return -1;
2017 return build->value != NULL;
2020 /* Simplify the basic set "bset" based on what we know about
2021 * the iterators of already generated loops.
2023 * "bset" is assumed to live in the (internal) schedule domain.
2025 __isl_give isl_basic_set *isl_ast_build_compute_gist_basic_set(
2026 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
2028 if (!build)
2029 goto error;
2031 bset = isl_basic_set_preimage_multi_aff(bset,
2032 isl_multi_aff_copy(build->values));
2033 bset = isl_basic_set_gist(bset,
2034 isl_set_simple_hull(isl_set_copy(build->domain)));
2036 return bset;
2037 error:
2038 isl_basic_set_free(bset);
2039 return NULL;
2042 /* Simplify the set "set" based on what we know about
2043 * the iterators of already generated loops.
2045 * "set" is assumed to live in the (internal) schedule domain.
2047 __isl_give isl_set *isl_ast_build_compute_gist(
2048 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2050 if (!build)
2051 goto error;
2053 if (!isl_set_is_params(set))
2054 set = isl_set_preimage_multi_aff(set,
2055 isl_multi_aff_copy(build->values));
2056 set = isl_set_gist(set, isl_set_copy(build->domain));
2058 return set;
2059 error:
2060 isl_set_free(set);
2061 return NULL;
2064 /* Include information about what we know about the iterators of
2065 * already generated loops to "set".
2067 * We currently only plug in the known affine values of outer loop
2068 * iterators.
2069 * In principle we could also introduce equalities or even other
2070 * constraints implied by the intersection of "set" and build->domain.
2072 __isl_give isl_set *isl_ast_build_specialize(__isl_keep isl_ast_build *build,
2073 __isl_take isl_set *set)
2075 if (!build)
2076 return isl_set_free(set);
2078 return isl_set_preimage_multi_aff(set,
2079 isl_multi_aff_copy(build->values));
2082 /* Simplify the map "map" based on what we know about
2083 * the iterators of already generated loops.
2085 * The domain of "map" is assumed to live in the (internal) schedule domain.
2087 __isl_give isl_map *isl_ast_build_compute_gist_map_domain(
2088 __isl_keep isl_ast_build *build, __isl_take isl_map *map)
2090 if (!build)
2091 goto error;
2093 map = isl_map_gist_domain(map, isl_set_copy(build->domain));
2095 return map;
2096 error:
2097 isl_map_free(map);
2098 return NULL;
2101 /* Simplify the affine expression "aff" based on what we know about
2102 * the iterators of already generated loops.
2104 * The domain of "aff" is assumed to live in the (internal) schedule domain.
2106 __isl_give isl_aff *isl_ast_build_compute_gist_aff(
2107 __isl_keep isl_ast_build *build, __isl_take isl_aff *aff)
2109 if (!build)
2110 goto error;
2112 aff = isl_aff_gist(aff, isl_set_copy(build->domain));
2114 return aff;
2115 error:
2116 isl_aff_free(aff);
2117 return NULL;
2120 /* Simplify the piecewise affine expression "aff" based on what we know about
2121 * the iterators of already generated loops.
2123 * The domain of "pa" is assumed to live in the (internal) schedule domain.
2125 __isl_give isl_pw_aff *isl_ast_build_compute_gist_pw_aff(
2126 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2128 if (!build)
2129 goto error;
2131 if (!isl_set_is_params(build->domain))
2132 pa = isl_pw_aff_pullback_multi_aff(pa,
2133 isl_multi_aff_copy(build->values));
2134 pa = isl_pw_aff_gist(pa, isl_set_copy(build->domain));
2136 return pa;
2137 error:
2138 isl_pw_aff_free(pa);
2139 return NULL;
2142 /* Simplify the piecewise multi-affine expression "aff" based on what
2143 * we know about the iterators of already generated loops.
2145 * The domain of "pma" is assumed to live in the (internal) schedule domain.
2147 __isl_give isl_pw_multi_aff *isl_ast_build_compute_gist_pw_multi_aff(
2148 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
2150 if (!build)
2151 goto error;
2153 pma = isl_pw_multi_aff_pullback_multi_aff(pma,
2154 isl_multi_aff_copy(build->values));
2155 pma = isl_pw_multi_aff_gist(pma, isl_set_copy(build->domain));
2157 return pma;
2158 error:
2159 isl_pw_multi_aff_free(pma);
2160 return NULL;
2163 /* Extract the schedule domain of the given type from build->options
2164 * at the current depth.
2166 * In particular, find the subset of build->options that is of
2167 * the following form
2169 * schedule_domain -> type[depth]
2171 * and return the corresponding domain, after eliminating inner dimensions
2172 * and divs that depend on the current dimension.
2174 * Note that the domain of build->options has been reformulated
2175 * in terms of the internal build space in embed_options,
2176 * but the position is still that within the current code generation.
2178 __isl_give isl_set *isl_ast_build_get_option_domain(
2179 __isl_keep isl_ast_build *build, enum isl_ast_loop_type type)
2181 const char *name;
2182 isl_space *space;
2183 isl_map *option;
2184 isl_set *domain;
2185 int local_pos;
2187 if (!build)
2188 return NULL;
2190 name = option_str[type];
2191 local_pos = build->depth - build->outer_pos;
2193 space = isl_ast_build_get_space(build, 1);
2194 space = isl_space_from_domain(space);
2195 space = isl_space_add_dims(space, isl_dim_out, 1);
2196 space = isl_space_set_tuple_name(space, isl_dim_out, name);
2198 option = isl_union_map_extract_map(build->options, space);
2199 option = isl_map_fix_si(option, isl_dim_out, 0, local_pos);
2201 domain = isl_map_domain(option);
2202 domain = isl_ast_build_eliminate(build, domain);
2204 return domain;
2207 /* How does the user want the current schedule dimension to be generated?
2208 * These choices have been extracted from the schedule node
2209 * in extract_loop_types and stored in build->loop_type.
2210 * They have been updated to reflect any dimension insertion in
2211 * node_insert_dim.
2212 * Return isl_ast_domain_error on error.
2214 enum isl_ast_loop_type isl_ast_build_get_loop_type(
2215 __isl_keep isl_ast_build *build)
2217 int local_pos;
2218 isl_ctx *ctx;
2220 if (!build)
2221 return isl_ast_loop_error;
2222 ctx = isl_ast_build_get_ctx(build);
2223 if (!build->node)
2224 isl_die(ctx, isl_error_internal,
2225 "only works for schedule tree based AST generation",
2226 return isl_ast_loop_error);
2228 local_pos = build->depth - build->outer_pos;
2229 return build->loop_type[local_pos];
2232 /* Extract the separation class mapping at the current depth.
2234 * In particular, find and return the subset of build->options that is of
2235 * the following form
2237 * schedule_domain -> separation_class[[depth] -> [class]]
2239 * The caller is expected to eliminate inner dimensions from the domain.
2241 * Note that the domain of build->options has been reformulated
2242 * in terms of the internal build space in embed_options,
2243 * but the position is still that within the current code generation.
2245 __isl_give isl_map *isl_ast_build_get_separation_class(
2246 __isl_keep isl_ast_build *build)
2248 isl_ctx *ctx;
2249 isl_space *space_sep, *space;
2250 isl_map *res;
2251 int local_pos;
2253 if (!build)
2254 return NULL;
2256 local_pos = build->depth - build->outer_pos;
2257 ctx = isl_ast_build_get_ctx(build);
2258 space_sep = isl_space_alloc(ctx, 0, 1, 1);
2259 space_sep = isl_space_wrap(space_sep);
2260 space_sep = isl_space_set_tuple_name(space_sep, isl_dim_set,
2261 "separation_class");
2262 space = isl_ast_build_get_space(build, 1);
2263 space_sep = isl_space_align_params(space_sep, isl_space_copy(space));
2264 space = isl_space_map_from_domain_and_range(space, space_sep);
2266 res = isl_union_map_extract_map(build->options, space);
2267 res = isl_map_fix_si(res, isl_dim_out, 0, local_pos);
2268 res = isl_map_coalesce(res);
2270 return res;
2273 /* Eliminate dimensions inner to the current dimension.
2275 __isl_give isl_set *isl_ast_build_eliminate_inner(
2276 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2278 int dim;
2279 int depth;
2281 if (!build)
2282 return isl_set_free(set);
2284 dim = isl_set_dim(set, isl_dim_set);
2285 depth = build->depth;
2286 set = isl_set_detect_equalities(set);
2287 set = isl_set_eliminate(set, isl_dim_set, depth + 1, dim - (depth + 1));
2289 return set;
2292 /* Eliminate unknown divs and divs that depend on the current dimension.
2294 * Note that during the elimination of unknown divs, we may discover
2295 * an explicit representation of some other unknown divs, which may
2296 * depend on the current dimension. We therefore need to eliminate
2297 * unknown divs first.
2299 __isl_give isl_set *isl_ast_build_eliminate_divs(
2300 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2302 int depth;
2304 if (!build)
2305 return isl_set_free(set);
2307 set = isl_set_remove_unknown_divs(set);
2308 depth = build->depth;
2309 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, depth, 1);
2311 return set;
2314 /* Eliminate dimensions inner to the current dimension as well as
2315 * unknown divs and divs that depend on the current dimension.
2316 * The result then consists only of constraints that are independent
2317 * of the current dimension and upper and lower bounds on the current
2318 * dimension.
2320 __isl_give isl_set *isl_ast_build_eliminate(
2321 __isl_keep isl_ast_build *build, __isl_take isl_set *domain)
2323 domain = isl_ast_build_eliminate_inner(build, domain);
2324 domain = isl_ast_build_eliminate_divs(build, domain);
2325 return domain;
2328 /* Replace build->single_valued by "sv".
2330 __isl_give isl_ast_build *isl_ast_build_set_single_valued(
2331 __isl_take isl_ast_build *build, int sv)
2333 if (!build)
2334 return build;
2335 if (build->single_valued == sv)
2336 return build;
2337 build = isl_ast_build_cow(build);
2338 if (!build)
2339 return build;
2340 build->single_valued = sv;
2342 return build;