generalize isl_multi_aff_sub
[isl.git] / isl_ast_build.c
blob61576bb0a5124468646a127f4cc268af8afc5e36
1 /*
2 * Copyright 2012 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 __isl_give isl_ast_build *isl_ast_build_copy(__isl_keep isl_ast_build *build)
151 if (!build)
152 return NULL;
154 build->ref++;
155 return build;
158 __isl_give isl_ast_build *isl_ast_build_dup(__isl_keep isl_ast_build *build)
160 isl_ctx *ctx;
161 isl_ast_build *dup;
163 if (!build)
164 return NULL;
166 ctx = isl_ast_build_get_ctx(build);
167 dup = isl_calloc_type(ctx, isl_ast_build);
168 if (!dup)
169 return NULL;
171 dup->ref = 1;
172 dup->outer_pos = build->outer_pos;
173 dup->depth = build->depth;
174 dup->iterators = isl_id_list_copy(build->iterators);
175 dup->domain = isl_set_copy(build->domain);
176 dup->generated = isl_set_copy(build->generated);
177 dup->pending = isl_set_copy(build->pending);
178 dup->values = isl_multi_aff_copy(build->values);
179 dup->value = isl_pw_aff_copy(build->value);
180 dup->strides = isl_vec_copy(build->strides);
181 dup->offsets = isl_multi_aff_copy(build->offsets);
182 dup->executed = isl_union_map_copy(build->executed);
183 dup->single_valued = build->single_valued;
184 dup->options = isl_union_map_copy(build->options);
185 dup->at_each_domain = build->at_each_domain;
186 dup->at_each_domain_user = build->at_each_domain_user;
187 dup->before_each_for = build->before_each_for;
188 dup->before_each_for_user = build->before_each_for_user;
189 dup->after_each_for = build->after_each_for;
190 dup->after_each_for_user = build->after_each_for_user;
191 dup->create_leaf = build->create_leaf;
192 dup->create_leaf_user = build->create_leaf_user;
194 if (!dup->iterators || !dup->domain || !dup->generated ||
195 !dup->pending || !dup->values ||
196 !dup->strides || !dup->offsets || !dup->options ||
197 (build->executed && !dup->executed) ||
198 (build->value && !dup->value))
199 return isl_ast_build_free(dup);
201 return dup;
204 /* Align the parameters of "build" to those of "model", introducing
205 * additional parameters if needed.
207 __isl_give isl_ast_build *isl_ast_build_align_params(
208 __isl_take isl_ast_build *build, __isl_take isl_space *model)
210 build = isl_ast_build_cow(build);
211 if (!build)
212 goto error;
214 build->domain = isl_set_align_params(build->domain,
215 isl_space_copy(model));
216 build->generated = isl_set_align_params(build->generated,
217 isl_space_copy(model));
218 build->pending = isl_set_align_params(build->pending,
219 isl_space_copy(model));
220 build->values = isl_multi_aff_align_params(build->values,
221 isl_space_copy(model));
222 build->offsets = isl_multi_aff_align_params(build->offsets,
223 isl_space_copy(model));
224 build->options = isl_union_map_align_params(build->options,
225 isl_space_copy(model));
226 isl_space_free(model);
228 if (!build->domain || !build->values || !build->offsets ||
229 !build->options)
230 return isl_ast_build_free(build);
232 return build;
233 error:
234 isl_space_free(model);
235 return NULL;
238 __isl_give isl_ast_build *isl_ast_build_cow(__isl_take isl_ast_build *build)
240 if (!build)
241 return NULL;
243 if (build->ref == 1)
244 return build;
245 build->ref--;
246 return isl_ast_build_dup(build);
249 __isl_null isl_ast_build *isl_ast_build_free(
250 __isl_take isl_ast_build *build)
252 if (!build)
253 return NULL;
255 if (--build->ref > 0)
256 return NULL;
258 isl_id_list_free(build->iterators);
259 isl_set_free(build->domain);
260 isl_set_free(build->generated);
261 isl_set_free(build->pending);
262 isl_multi_aff_free(build->values);
263 isl_pw_aff_free(build->value);
264 isl_vec_free(build->strides);
265 isl_multi_aff_free(build->offsets);
266 isl_multi_aff_free(build->schedule_map);
267 isl_union_map_free(build->executed);
268 isl_union_map_free(build->options);
270 free(build);
272 return NULL;
275 isl_ctx *isl_ast_build_get_ctx(__isl_keep isl_ast_build *build)
277 return build ? isl_set_get_ctx(build->domain) : NULL;
280 /* Replace build->options by "options".
282 __isl_give isl_ast_build *isl_ast_build_set_options(
283 __isl_take isl_ast_build *build, __isl_take isl_union_map *options)
285 build = isl_ast_build_cow(build);
287 if (!build || !options)
288 goto error;
290 isl_union_map_free(build->options);
291 build->options = options;
293 return build;
294 error:
295 isl_union_map_free(options);
296 return isl_ast_build_free(build);
299 /* Set the iterators for the next code generation.
301 * If we still have some iterators left from the previous code generation
302 * (if any) or if iterators have already been set by a previous
303 * call to this function, then we remove them first.
305 __isl_give isl_ast_build *isl_ast_build_set_iterators(
306 __isl_take isl_ast_build *build, __isl_take isl_id_list *iterators)
308 int dim, n_it;
310 build = isl_ast_build_cow(build);
311 if (!build)
312 goto error;
314 dim = isl_set_dim(build->domain, isl_dim_set);
315 n_it = isl_id_list_n_id(build->iterators);
316 if (n_it < dim)
317 isl_die(isl_ast_build_get_ctx(build), isl_error_internal,
318 "isl_ast_build in inconsistent state", goto error);
319 if (n_it > dim)
320 build->iterators = isl_id_list_drop(build->iterators,
321 dim, n_it - dim);
322 build->iterators = isl_id_list_concat(build->iterators, iterators);
323 if (!build->iterators)
324 return isl_ast_build_free(build);
326 return build;
327 error:
328 isl_id_list_free(iterators);
329 return isl_ast_build_free(build);
332 /* Set the "at_each_domain" callback of "build" to "fn".
334 __isl_give isl_ast_build *isl_ast_build_set_at_each_domain(
335 __isl_take isl_ast_build *build,
336 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
337 __isl_keep isl_ast_build *build, void *user), void *user)
339 build = isl_ast_build_cow(build);
341 if (!build)
342 return NULL;
344 build->at_each_domain = fn;
345 build->at_each_domain_user = user;
347 return build;
350 /* Set the "before_each_for" callback of "build" to "fn".
352 __isl_give isl_ast_build *isl_ast_build_set_before_each_for(
353 __isl_take isl_ast_build *build,
354 __isl_give isl_id *(*fn)(__isl_keep isl_ast_build *build,
355 void *user), void *user)
357 build = isl_ast_build_cow(build);
359 if (!build)
360 return NULL;
362 build->before_each_for = fn;
363 build->before_each_for_user = user;
365 return build;
368 /* Set the "after_each_for" callback of "build" to "fn".
370 __isl_give isl_ast_build *isl_ast_build_set_after_each_for(
371 __isl_take isl_ast_build *build,
372 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_node *node,
373 __isl_keep isl_ast_build *build, void *user), void *user)
375 build = isl_ast_build_cow(build);
377 if (!build)
378 return NULL;
380 build->after_each_for = fn;
381 build->after_each_for_user = user;
383 return build;
386 /* Set the "create_leaf" callback of "build" to "fn".
388 __isl_give isl_ast_build *isl_ast_build_set_create_leaf(
389 __isl_take isl_ast_build *build,
390 __isl_give isl_ast_node *(*fn)(__isl_take isl_ast_build *build,
391 void *user), void *user)
393 build = isl_ast_build_cow(build);
395 if (!build)
396 return NULL;
398 build->create_leaf = fn;
399 build->create_leaf_user = user;
401 return build;
404 /* Clear all information that is specific to this code generation
405 * and that is (probably) not meaningful to any nested code generation.
407 __isl_give isl_ast_build *isl_ast_build_clear_local_info(
408 __isl_take isl_ast_build *build)
410 isl_space *space;
412 build = isl_ast_build_cow(build);
413 if (!build)
414 return NULL;
416 space = isl_union_map_get_space(build->options);
417 isl_union_map_free(build->options);
418 build->options = isl_union_map_empty(space);
420 build->at_each_domain = NULL;
421 build->at_each_domain_user = NULL;
422 build->before_each_for = NULL;
423 build->before_each_for_user = NULL;
424 build->after_each_for = NULL;
425 build->after_each_for_user = NULL;
426 build->create_leaf = NULL;
427 build->create_leaf_user = NULL;
429 if (!build->options)
430 return isl_ast_build_free(build);
432 return build;
435 /* Have any loops been eliminated?
436 * That is, do any of the original schedule dimensions have a fixed
437 * value that has been substituted?
439 static int any_eliminated(isl_ast_build *build)
441 int i;
443 for (i = 0; i < build->depth; ++i)
444 if (isl_ast_build_has_affine_value(build, i))
445 return 1;
447 return 0;
450 /* Clear build->schedule_map.
451 * This function should be called whenever anything that might affect
452 * the result of isl_ast_build_get_schedule_map_multi_aff changes.
453 * In particular, it should be called when the depth is changed or
454 * when an iterator is determined to have a fixed value.
456 static void isl_ast_build_reset_schedule_map(__isl_keep isl_ast_build *build)
458 if (!build)
459 return;
460 isl_multi_aff_free(build->schedule_map);
461 build->schedule_map = NULL;
464 /* Do we need a (non-trivial) schedule map?
465 * That is, is the internal schedule space different from
466 * the external schedule space?
468 * The internal and external schedule spaces are only the same
469 * if code has been generated for the entire schedule and if none
470 * of the loops have been eliminated.
472 __isl_give int isl_ast_build_need_schedule_map(__isl_keep isl_ast_build *build)
474 int dim;
476 if (!build)
477 return -1;
479 dim = isl_set_dim(build->domain, isl_dim_set);
480 return build->depth != dim || any_eliminated(build);
483 /* Return a mapping from the internal schedule space to the external
484 * schedule space in the form of an isl_multi_aff.
485 * The internal schedule space originally corresponds to that of the
486 * input schedule. This may change during the code generation if
487 * if isl_ast_build_insert_dim is ever called.
488 * The external schedule space corresponds to the
489 * loops that have been generated.
491 * Currently, the only difference between the internal schedule domain
492 * and the external schedule domain is that some dimensions are projected
493 * out in the external schedule domain. In particular, the dimensions
494 * for which no code has been generated yet and the dimensions that correspond
495 * to eliminated loops.
497 * We cache a copy of the schedule_map in build->schedule_map.
498 * The cache is cleared through isl_ast_build_reset_schedule_map
499 * whenever anything changes that might affect the result of this function.
501 __isl_give isl_multi_aff *isl_ast_build_get_schedule_map_multi_aff(
502 __isl_keep isl_ast_build *build)
504 isl_space *space;
505 isl_multi_aff *ma;
507 if (!build)
508 return NULL;
509 if (build->schedule_map)
510 return isl_multi_aff_copy(build->schedule_map);
512 space = isl_ast_build_get_space(build, 1);
513 space = isl_space_map_from_set(space);
514 ma = isl_multi_aff_identity(space);
515 if (isl_ast_build_need_schedule_map(build)) {
516 int i;
517 int dim = isl_set_dim(build->domain, isl_dim_set);
518 ma = isl_multi_aff_drop_dims(ma, isl_dim_out,
519 build->depth, dim - build->depth);
520 for (i = build->depth - 1; i >= 0; --i)
521 if (isl_ast_build_has_affine_value(build, i))
522 ma = isl_multi_aff_drop_dims(ma,
523 isl_dim_out, i, 1);
526 build->schedule_map = ma;
527 return isl_multi_aff_copy(build->schedule_map);
530 /* Return a mapping from the internal schedule space to the external
531 * schedule space in the form of an isl_map.
533 __isl_give isl_map *isl_ast_build_get_schedule_map(
534 __isl_keep isl_ast_build *build)
536 isl_multi_aff *ma;
538 ma = isl_ast_build_get_schedule_map_multi_aff(build);
539 return isl_map_from_multi_aff(ma);
542 /* Return the position of the dimension in build->domain for which
543 * an AST node is currently being generated.
545 int isl_ast_build_get_depth(__isl_keep isl_ast_build *build)
547 return build ? build->depth : -1;
550 /* Prepare for generating code for the next level.
551 * In particular, increase the depth and reset any information
552 * that is local to the current depth.
554 __isl_give isl_ast_build *isl_ast_build_increase_depth(
555 __isl_take isl_ast_build *build)
557 build = isl_ast_build_cow(build);
558 if (!build)
559 return NULL;
560 build->depth++;
561 isl_ast_build_reset_schedule_map(build);
562 build->value = isl_pw_aff_free(build->value);
563 return build;
566 void isl_ast_build_dump(__isl_keep isl_ast_build *build)
568 if (!build)
569 return;
571 fprintf(stderr, "domain: ");
572 isl_set_dump(build->domain);
573 fprintf(stderr, "generated: ");
574 isl_set_dump(build->generated);
575 fprintf(stderr, "pending: ");
576 isl_set_dump(build->pending);
577 fprintf(stderr, "iterators: ");
578 isl_id_list_dump(build->iterators);
579 fprintf(stderr, "values: ");
580 isl_multi_aff_dump(build->values);
581 if (build->value) {
582 fprintf(stderr, "value: ");
583 isl_pw_aff_dump(build->value);
585 fprintf(stderr, "strides: ");
586 isl_vec_dump(build->strides);
587 fprintf(stderr, "offsets: ");
588 isl_multi_aff_dump(build->offsets);
591 /* Initialize "build" for AST construction in schedule space "space"
592 * in the case that build->domain is a parameter set.
594 * build->iterators is assumed to have been updated already.
596 static __isl_give isl_ast_build *isl_ast_build_init(
597 __isl_take isl_ast_build *build, __isl_take isl_space *space)
599 isl_set *set;
601 build = isl_ast_build_cow(build);
602 if (!build)
603 goto error;
605 set = isl_set_universe(isl_space_copy(space));
606 build->domain = isl_set_intersect_params(isl_set_copy(set),
607 build->domain);
608 build->pending = isl_set_intersect_params(isl_set_copy(set),
609 build->pending);
610 build->generated = isl_set_intersect_params(set, build->generated);
612 return isl_ast_build_init_derived(build, space);
613 error:
614 isl_ast_build_free(build);
615 isl_space_free(space);
616 return NULL;
619 /* Assign "aff" to *user and return -1, effectively extracting
620 * the first (and presumably only) affine expression in the isl_pw_aff
621 * on which this function is used.
623 static int extract_single_piece(__isl_take isl_set *set,
624 __isl_take isl_aff *aff, void *user)
626 isl_aff **p = user;
628 *p = aff;
629 isl_set_free(set);
631 return -1;
634 /* Intersect "set" with the stride constraint of "build", if any.
636 static __isl_give isl_set *intersect_stride_constraint(__isl_take isl_set *set,
637 __isl_keep isl_ast_build *build)
639 isl_set *stride;
641 if (!build)
642 return isl_set_free(set);
643 if (!isl_ast_build_has_stride(build, build->depth))
644 return set;
646 stride = isl_ast_build_get_stride_constraint(build);
647 return isl_set_intersect(set, stride);
650 /* Check if the given bounds on the current dimension (together with
651 * the stride constraint, if any) imply that
652 * this current dimension attains only a single value (in terms of
653 * parameters and outer dimensions).
654 * If so, we record it in build->value.
655 * If, moreover, this value can be represented as a single affine expression,
656 * then we also update build->values, effectively marking the current
657 * dimension as "eliminated".
659 * When computing the gist of the fixed value that can be represented
660 * as a single affine expression, it is important to only take into
661 * account the domain constraints in the original AST build and
662 * not the domain of the affine expression itself.
663 * Otherwise, a [i/3] is changed into a i/3 because we know that i
664 * is a multiple of 3, but then we end up not expressing anywhere
665 * in the context that i is a multiple of 3.
667 static __isl_give isl_ast_build *update_values(
668 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
670 int sv;
671 isl_pw_multi_aff *pma;
672 isl_aff *aff = NULL;
673 isl_map *it_map;
674 isl_set *set;
676 set = isl_set_from_basic_set(bounds);
677 set = isl_set_intersect(set, isl_set_copy(build->domain));
678 set = intersect_stride_constraint(set, build);
679 it_map = isl_ast_build_map_to_iterator(build, set);
681 sv = isl_map_is_single_valued(it_map);
682 if (sv < 0)
683 build = isl_ast_build_free(build);
684 if (!build || !sv) {
685 isl_map_free(it_map);
686 return build;
689 pma = isl_pw_multi_aff_from_map(it_map);
690 build->value = isl_pw_multi_aff_get_pw_aff(pma, 0);
691 build->value = isl_ast_build_compute_gist_pw_aff(build, build->value);
692 build->value = isl_pw_aff_coalesce(build->value);
693 isl_pw_multi_aff_free(pma);
695 if (!build->value)
696 return isl_ast_build_free(build);
698 if (isl_pw_aff_n_piece(build->value) != 1)
699 return build;
701 isl_pw_aff_foreach_piece(build->value, &extract_single_piece, &aff);
703 build->values = isl_multi_aff_set_aff(build->values, build->depth, aff);
704 if (!build->values)
705 return isl_ast_build_free(build);
706 isl_ast_build_reset_schedule_map(build);
707 return build;
710 /* Update the AST build based on the given loop bounds for
711 * the current dimension and the stride information available in the build.
713 * We first make sure that the bounds do not refer to any iterators
714 * that have already been eliminated.
715 * Then, we check if the bounds imply that the current iterator
716 * has a fixed value.
717 * If they do and if this fixed value can be expressed as a single
718 * affine expression, we eliminate the iterators from the bounds.
719 * Note that we cannot simply plug in this single value using
720 * isl_basic_set_preimage_multi_aff as the single value may only
721 * be defined on a subset of the domain. Plugging in the value
722 * would restrict the build domain to this subset, while this
723 * restriction may not be reflected in the generated code.
724 * Finally, we intersect build->domain with the updated bounds.
725 * We also add the stride constraint unless we have been able
726 * to find a fixed value expressed as a single affine expression.
728 * Note that the check for a fixed value in update_values requires
729 * us to intersect the bounds with the current build domain.
730 * When we intersect build->domain with the updated bounds in
731 * the final step, we make sure that these updated bounds have
732 * not been intersected with the old build->domain.
733 * Otherwise, we would indirectly intersect the build domain with itself,
734 * which can lead to inefficiencies, in particular if the build domain
735 * contains any unknown divs.
737 __isl_give isl_ast_build *isl_ast_build_set_loop_bounds(
738 __isl_take isl_ast_build *build, __isl_take isl_basic_set *bounds)
740 isl_set *set;
742 build = isl_ast_build_cow(build);
743 if (!build)
744 goto error;
746 bounds = isl_basic_set_preimage_multi_aff(bounds,
747 isl_multi_aff_copy(build->values));
748 build = update_values(build, isl_basic_set_copy(bounds));
749 if (!build)
750 goto error;
751 set = isl_set_from_basic_set(isl_basic_set_copy(bounds));
752 if (isl_ast_build_has_affine_value(build, build->depth)) {
753 set = isl_set_eliminate(set, isl_dim_set, build->depth, 1);
754 set = isl_set_compute_divs(set);
755 build->pending = isl_set_intersect(build->pending,
756 isl_set_copy(set));
757 build->domain = isl_set_intersect(build->domain, set);
758 } else {
759 isl_basic_set *generated, *pending;
761 pending = isl_basic_set_copy(bounds);
762 pending = isl_basic_set_drop_constraints_involving_dims(pending,
763 isl_dim_set, build->depth, 1);
764 build->pending = isl_set_intersect(build->pending,
765 isl_set_from_basic_set(pending));
766 generated = isl_basic_set_copy(bounds);
767 generated = isl_basic_set_drop_constraints_not_involving_dims(
768 generated, isl_dim_set, build->depth, 1);
769 build->generated = isl_set_intersect(build->generated,
770 isl_set_from_basic_set(generated));
771 build->domain = isl_set_intersect(build->domain, set);
772 build = isl_ast_build_include_stride(build);
773 if (!build)
774 goto error;
776 isl_basic_set_free(bounds);
778 if (!build->domain || !build->pending || !build->generated)
779 return isl_ast_build_free(build);
781 return build;
782 error:
783 isl_ast_build_free(build);
784 isl_basic_set_free(bounds);
785 return NULL;
788 /* Intersect build->domain with "set", where "set" is specified
789 * in terms of the internal schedule domain.
791 static __isl_give isl_ast_build *isl_ast_build_restrict_internal(
792 __isl_take isl_ast_build *build, __isl_take isl_set *set)
794 build = isl_ast_build_cow(build);
795 if (!build)
796 goto error;
798 set = isl_set_compute_divs(set);
799 build->domain = isl_set_intersect(build->domain, set);
800 build->domain = isl_set_coalesce(build->domain);
802 if (!build->domain)
803 return isl_ast_build_free(build);
805 return build;
806 error:
807 isl_ast_build_free(build);
808 isl_set_free(set);
809 return NULL;
812 /* Intersect build->generated and build->domain with "set",
813 * where "set" is specified in terms of the internal schedule domain.
815 __isl_give isl_ast_build *isl_ast_build_restrict_generated(
816 __isl_take isl_ast_build *build, __isl_take isl_set *set)
818 set = isl_set_compute_divs(set);
819 build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
820 build = isl_ast_build_cow(build);
821 if (!build)
822 goto error;
824 build->generated = isl_set_intersect(build->generated, set);
825 build->generated = isl_set_coalesce(build->generated);
827 if (!build->generated)
828 return isl_ast_build_free(build);
830 return build;
831 error:
832 isl_ast_build_free(build);
833 isl_set_free(set);
834 return NULL;
837 /* Replace the set of pending constraints by "guard", which is then
838 * no longer considered as pending.
839 * That is, add "guard" to the generated constraints and clear all pending
840 * constraints, making the domain equal to the generated constraints.
842 __isl_give isl_ast_build *isl_ast_build_replace_pending_by_guard(
843 __isl_take isl_ast_build *build, __isl_take isl_set *guard)
845 build = isl_ast_build_restrict_generated(build, guard);
846 build = isl_ast_build_cow(build);
847 if (!build)
848 return NULL;
850 isl_set_free(build->domain);
851 build->domain = isl_set_copy(build->generated);
852 isl_set_free(build->pending);
853 build->pending = isl_set_universe(isl_set_get_space(build->domain));
855 if (!build->pending)
856 return isl_ast_build_free(build);
858 return build;
861 /* Intersect build->pending and build->domain with "set",
862 * where "set" is specified in terms of the internal schedule domain.
864 __isl_give isl_ast_build *isl_ast_build_restrict_pending(
865 __isl_take isl_ast_build *build, __isl_take isl_set *set)
867 set = isl_set_compute_divs(set);
868 build = isl_ast_build_restrict_internal(build, isl_set_copy(set));
869 build = isl_ast_build_cow(build);
870 if (!build)
871 goto error;
873 build->pending = isl_set_intersect(build->pending, set);
874 build->pending = isl_set_coalesce(build->pending);
876 if (!build->pending)
877 return isl_ast_build_free(build);
879 return build;
880 error:
881 isl_ast_build_free(build);
882 isl_set_free(set);
883 return NULL;
886 /* Intersect build->domain with "set", where "set" is specified
887 * in terms of the external schedule domain.
889 __isl_give isl_ast_build *isl_ast_build_restrict(
890 __isl_take isl_ast_build *build, __isl_take isl_set *set)
892 if (isl_set_is_params(set))
893 return isl_ast_build_restrict_generated(build, set);
895 if (isl_ast_build_need_schedule_map(build)) {
896 isl_multi_aff *ma;
897 ma = isl_ast_build_get_schedule_map_multi_aff(build);
898 set = isl_set_preimage_multi_aff(set, ma);
900 return isl_ast_build_restrict_generated(build, set);
903 /* Replace build->executed by "executed".
905 __isl_give isl_ast_build *isl_ast_build_set_executed(
906 __isl_take isl_ast_build *build, __isl_take isl_union_map *executed)
908 build = isl_ast_build_cow(build);
909 if (!build)
910 goto error;
912 isl_union_map_free(build->executed);
913 build->executed = executed;
915 return build;
916 error:
917 isl_ast_build_free(build);
918 isl_union_map_free(executed);
919 return NULL;
922 /* Return a copy of the current schedule domain.
924 __isl_give isl_set *isl_ast_build_get_domain(__isl_keep isl_ast_build *build)
926 return build ? isl_set_copy(build->domain) : NULL;
929 /* Return a copy of the set of pending constraints.
931 __isl_give isl_set *isl_ast_build_get_pending(
932 __isl_keep isl_ast_build *build)
934 return build ? isl_set_copy(build->pending) : NULL;
937 /* Return a copy of the set of generated constraints.
939 __isl_give isl_set *isl_ast_build_get_generated(
940 __isl_keep isl_ast_build *build)
942 return build ? isl_set_copy(build->generated) : NULL;
945 /* Return the number of variables of the given type
946 * in the (internal) schedule space.
948 unsigned isl_ast_build_dim(__isl_keep isl_ast_build *build,
949 enum isl_dim_type type)
951 if (!build)
952 return 0;
953 return isl_set_dim(build->domain, type);
956 /* Return the (schedule) space of "build".
958 * If "internal" is set, then this space is the space of the internal
959 * representation of the entire schedule, including those parts for
960 * which no code has been generated yet.
962 * If "internal" is not set, then this space is the external representation
963 * of the loops generated so far.
965 __isl_give isl_space *isl_ast_build_get_space(__isl_keep isl_ast_build *build,
966 int internal)
968 int i;
969 int dim;
970 isl_space *space;
972 if (!build)
973 return NULL;
975 space = isl_set_get_space(build->domain);
976 if (internal)
977 return space;
979 if (!isl_ast_build_need_schedule_map(build))
980 return space;
982 dim = isl_set_dim(build->domain, isl_dim_set);
983 space = isl_space_drop_dims(space, isl_dim_set,
984 build->depth, dim - build->depth);
985 for (i = build->depth - 1; i >= 0; --i)
986 if (isl_ast_build_has_affine_value(build, i))
987 space = isl_space_drop_dims(space, isl_dim_set, i, 1);
989 return space;
992 /* Return the external representation of the schedule space of "build",
993 * i.e., a space with a dimension for each loop generated so far,
994 * with the names of the dimensions set to the loop iterators.
996 __isl_give isl_space *isl_ast_build_get_schedule_space(
997 __isl_keep isl_ast_build *build)
999 isl_space *space;
1000 int i, skip;
1002 if (!build)
1003 return NULL;
1005 space = isl_ast_build_get_space(build, 0);
1007 skip = 0;
1008 for (i = 0; i < build->depth; ++i) {
1009 isl_id *id;
1011 if (isl_ast_build_has_affine_value(build, i)) {
1012 skip++;
1013 continue;
1016 id = isl_ast_build_get_iterator_id(build, i);
1017 space = isl_space_set_dim_id(space, isl_dim_set, i - skip, id);
1020 return space;
1023 /* Return the current schedule, as stored in build->executed, in terms
1024 * of the external schedule domain.
1026 __isl_give isl_union_map *isl_ast_build_get_schedule(
1027 __isl_keep isl_ast_build *build)
1029 isl_union_map *executed;
1030 isl_union_map *schedule;
1032 if (!build)
1033 return NULL;
1035 executed = isl_union_map_copy(build->executed);
1036 if (isl_ast_build_need_schedule_map(build)) {
1037 isl_map *proj = isl_ast_build_get_schedule_map(build);
1038 executed = isl_union_map_apply_domain(executed,
1039 isl_union_map_from_map(proj));
1041 schedule = isl_union_map_reverse(executed);
1043 return schedule;
1046 /* Return the iterator attached to the internal schedule dimension "pos".
1048 __isl_give isl_id *isl_ast_build_get_iterator_id(
1049 __isl_keep isl_ast_build *build, int pos)
1051 if (!build)
1052 return NULL;
1054 return isl_id_list_get_id(build->iterators, pos);
1057 /* Set the stride and offset of the current dimension to the given
1058 * value and expression.
1060 * If we had already found a stride before, then the two strides
1061 * are combined into a single stride.
1063 * In particular, if the new stride information is of the form
1065 * i = f + s (...)
1067 * and the old stride information is of the form
1069 * i = f2 + s2 (...)
1071 * then we compute the extended gcd of s and s2
1073 * a s + b s2 = g,
1075 * with g = gcd(s,s2), multiply the first equation with t1 = b s2/g
1076 * and the second with t2 = a s1/g.
1077 * This results in
1079 * i = (b s2 + a s1)/g i = t1 f + t2 f2 + (s s2)/g (...)
1081 * so that t1 f + t2 f2 is the combined offset and (s s2)/g = lcm(s,s2)
1082 * is the combined stride.
1084 static __isl_give isl_ast_build *set_stride(__isl_take isl_ast_build *build,
1085 __isl_take isl_val *stride, __isl_take isl_aff *offset)
1087 int pos;
1089 build = isl_ast_build_cow(build);
1090 if (!build || !stride || !offset)
1091 goto error;
1093 pos = build->depth;
1095 if (isl_ast_build_has_stride(build, pos)) {
1096 isl_val *stride2, *a, *b, *g;
1097 isl_aff *offset2;
1099 stride2 = isl_vec_get_element_val(build->strides, pos);
1100 g = isl_val_gcdext(isl_val_copy(stride), isl_val_copy(stride2),
1101 &a, &b);
1102 a = isl_val_mul(a, isl_val_copy(stride));
1103 a = isl_val_div(a, isl_val_copy(g));
1104 stride2 = isl_val_div(stride2, g);
1105 b = isl_val_mul(b, isl_val_copy(stride2));
1106 stride = isl_val_mul(stride, stride2);
1108 offset2 = isl_multi_aff_get_aff(build->offsets, pos);
1109 offset2 = isl_aff_scale_val(offset2, a);
1110 offset = isl_aff_scale_val(offset, b);
1111 offset = isl_aff_add(offset, offset2);
1114 build->strides = isl_vec_set_element_val(build->strides, pos, stride);
1115 build->offsets = isl_multi_aff_set_aff(build->offsets, pos, offset);
1116 if (!build->strides || !build->offsets)
1117 return isl_ast_build_free(build);
1119 return build;
1120 error:
1121 isl_val_free(stride);
1122 isl_aff_free(offset);
1123 return isl_ast_build_free(build);
1126 /* Return a set expressing the stride constraint at the current depth.
1128 * In particular, if the current iterator (i) is known to attain values
1130 * f + s a
1132 * where f is the offset and s is the stride, then the returned set
1133 * expresses the constraint
1135 * (f - i) mod s = 0
1137 __isl_give isl_set *isl_ast_build_get_stride_constraint(
1138 __isl_keep isl_ast_build *build)
1140 isl_aff *aff;
1141 isl_set *set;
1142 isl_val *stride;
1143 int pos;
1145 if (!build)
1146 return NULL;
1148 pos = build->depth;
1150 if (!isl_ast_build_has_stride(build, pos))
1151 return isl_set_universe(isl_ast_build_get_space(build, 1));
1153 stride = isl_ast_build_get_stride(build, pos);
1154 aff = isl_ast_build_get_offset(build, pos);
1155 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, pos, -1);
1156 aff = isl_aff_mod_val(aff, stride);
1157 set = isl_set_from_basic_set(isl_aff_zero_basic_set(aff));
1159 return set;
1162 /* Return the expansion implied by the stride and offset at the current
1163 * depth.
1165 * That is, return the mapping
1167 * [i_0, ..., i_{d-1}, i_d, i_{d+1}, ...]
1168 * -> [i_0, ..., i_{d-1}, s * i_d + offset(i), i_{d+1}, ...]
1170 * where s is the stride at the current depth d and offset(i) is
1171 * the corresponding offset.
1173 __isl_give isl_multi_aff *isl_ast_build_get_stride_expansion(
1174 __isl_keep isl_ast_build *build)
1176 isl_space *space;
1177 isl_multi_aff *ma;
1178 int pos;
1179 isl_aff *aff, *offset;
1180 isl_val *stride;
1182 if (!build)
1183 return NULL;
1185 pos = isl_ast_build_get_depth(build);
1186 space = isl_ast_build_get_space(build, 1);
1187 space = isl_space_map_from_set(space);
1188 ma = isl_multi_aff_identity(space);
1190 if (!isl_ast_build_has_stride(build, pos))
1191 return ma;
1193 offset = isl_ast_build_get_offset(build, pos);
1194 stride = isl_ast_build_get_stride(build, pos);
1195 aff = isl_multi_aff_get_aff(ma, pos);
1196 aff = isl_aff_scale_val(aff, stride);
1197 aff = isl_aff_add(aff, offset);
1198 ma = isl_multi_aff_set_aff(ma, pos, aff);
1200 return ma;
1203 /* Add constraints corresponding to any previously detected
1204 * stride on the current dimension to build->domain.
1206 __isl_give isl_ast_build *isl_ast_build_include_stride(
1207 __isl_take isl_ast_build *build)
1209 isl_set *set;
1211 if (!build)
1212 return NULL;
1213 if (!isl_ast_build_has_stride(build, build->depth))
1214 return build;
1215 build = isl_ast_build_cow(build);
1216 if (!build)
1217 return NULL;
1219 set = isl_ast_build_get_stride_constraint(build);
1221 build->domain = isl_set_intersect(build->domain, isl_set_copy(set));
1222 build->generated = isl_set_intersect(build->generated, set);
1223 if (!build->domain || !build->generated)
1224 return isl_ast_build_free(build);
1226 return build;
1229 /* Information used inside detect_stride.
1231 * "build" may be updated by detect_stride to include stride information.
1232 * "pos" is equal to build->depth.
1234 struct isl_detect_stride_data {
1235 isl_ast_build *build;
1236 int pos;
1239 /* Check if constraint "c" imposes any stride on dimension data->pos
1240 * and, if so, update the stride information in data->build.
1242 * In order to impose a stride on the dimension, "c" needs to be an equality
1243 * and it needs to involve the dimension. Note that "c" may also be
1244 * a div constraint and thus an inequality that we cannot use.
1246 * Let c be of the form
1248 * h(p) + g * v * i + g * stride * f(alpha) = 0
1250 * with h(p) an expression in terms of the parameters and outer dimensions
1251 * and f(alpha) an expression in terms of the existentially quantified
1252 * variables. Note that the inner dimensions have been eliminated so
1253 * they do not appear in "c".
1255 * If "stride" is not zero and not one, then it represents a non-trivial stride
1256 * on "i". We compute a and b such that
1258 * a v + b stride = 1
1260 * We have
1262 * g v i = -h(p) + g stride f(alpha)
1264 * a g v i = -a h(p) + g stride f(alpha)
1266 * a g v i + b g stride i = -a h(p) + g stride * (...)
1268 * g i = -a h(p) + g stride * (...)
1270 * i = -a h(p)/g + stride * (...)
1272 * The expression "-a h(p)/g" can therefore be used as offset.
1274 static int detect_stride(__isl_take isl_constraint *c, void *user)
1276 struct isl_detect_stride_data *data = user;
1277 int i, n_div;
1278 isl_ctx *ctx;
1279 isl_val *v, *stride, *m;
1281 if (!isl_constraint_is_equality(c) ||
1282 !isl_constraint_involves_dims(c, isl_dim_set, data->pos, 1)) {
1283 isl_constraint_free(c);
1284 return 0;
1287 ctx = isl_constraint_get_ctx(c);
1288 stride = isl_val_zero(ctx);
1289 n_div = isl_constraint_dim(c, isl_dim_div);
1290 for (i = 0; i < n_div; ++i) {
1291 v = isl_constraint_get_coefficient_val(c, isl_dim_div, i);
1292 stride = isl_val_gcd(stride, v);
1295 v = isl_constraint_get_coefficient_val(c, isl_dim_set, data->pos);
1296 m = isl_val_gcd(isl_val_copy(stride), isl_val_copy(v));
1297 stride = isl_val_div(stride, isl_val_copy(m));
1298 v = isl_val_div(v, isl_val_copy(m));
1300 if (!isl_val_is_zero(stride) && !isl_val_is_one(stride)) {
1301 isl_aff *aff;
1302 isl_val *gcd, *a, *b;
1304 gcd = isl_val_gcdext(v, isl_val_copy(stride), &a, &b);
1305 isl_val_free(gcd);
1306 isl_val_free(b);
1308 aff = isl_constraint_get_aff(c);
1309 for (i = 0; i < n_div; ++i)
1310 aff = isl_aff_set_coefficient_si(aff,
1311 isl_dim_div, i, 0);
1312 aff = isl_aff_set_coefficient_si(aff, isl_dim_in, data->pos, 0);
1313 a = isl_val_neg(a);
1314 aff = isl_aff_scale_val(aff, a);
1315 aff = isl_aff_scale_down_val(aff, m);
1316 data->build = set_stride(data->build, stride, aff);
1317 } else {
1318 isl_val_free(stride);
1319 isl_val_free(m);
1320 isl_val_free(v);
1323 isl_constraint_free(c);
1324 return 0;
1327 /* Check if the constraints in "set" imply any stride on the current
1328 * dimension and, if so, record the stride information in "build"
1329 * and return the updated "build".
1331 * We compute the affine hull and then check if any of the constraints
1332 * in the hull imposes any stride on the current dimension.
1334 * We assume that inner dimensions have been eliminated from "set"
1335 * by the caller. This is needed because the common stride
1336 * may be imposed by different inner dimensions on different parts of
1337 * the domain.
1339 __isl_give isl_ast_build *isl_ast_build_detect_strides(
1340 __isl_take isl_ast_build *build, __isl_take isl_set *set)
1342 isl_basic_set *hull;
1343 struct isl_detect_stride_data data;
1345 if (!build)
1346 goto error;
1348 data.build = build;
1349 data.pos = isl_ast_build_get_depth(build);
1350 hull = isl_set_affine_hull(set);
1352 if (isl_basic_set_foreach_constraint(hull, &detect_stride, &data) < 0)
1353 data.build = isl_ast_build_free(data.build);
1355 isl_basic_set_free(hull);
1356 return data.build;
1357 error:
1358 isl_set_free(set);
1359 return NULL;
1362 struct isl_ast_build_involves_data {
1363 int depth;
1364 int involves;
1367 /* Check if "map" involves the input dimension data->depth.
1369 static int involves_depth(__isl_take isl_map *map, void *user)
1371 struct isl_ast_build_involves_data *data = user;
1373 data->involves = isl_map_involves_dims(map, isl_dim_in, data->depth, 1);
1374 isl_map_free(map);
1376 if (data->involves < 0 || data->involves)
1377 return -1;
1378 return 0;
1381 /* Do any options depend on the value of the dimension at the current depth?
1383 int isl_ast_build_options_involve_depth(__isl_keep isl_ast_build *build)
1385 struct isl_ast_build_involves_data data;
1387 if (!build)
1388 return -1;
1390 data.depth = build->depth;
1391 data.involves = 0;
1393 if (isl_union_map_foreach_map(build->options,
1394 &involves_depth, &data) < 0) {
1395 if (data.involves < 0 || !data.involves)
1396 return -1;
1399 return data.involves;
1402 /* Construct the map
1404 * { [i] -> [i] : i < pos; [i] -> [i + 1] : i >= pos }
1406 * with "space" the parameter space of the constructed map.
1408 static __isl_give isl_map *construct_insertion_map(__isl_take isl_space *space,
1409 int pos)
1411 isl_constraint *c;
1412 isl_basic_map *bmap1, *bmap2;
1414 space = isl_space_set_from_params(space);
1415 space = isl_space_add_dims(space, isl_dim_set, 1);
1416 space = isl_space_map_from_set(space);
1417 c = isl_equality_alloc(isl_local_space_from_space(space));
1418 c = isl_constraint_set_coefficient_si(c, isl_dim_in, 0, 1);
1419 c = isl_constraint_set_coefficient_si(c, isl_dim_out, 0, -1);
1420 bmap1 = isl_basic_map_from_constraint(isl_constraint_copy(c));
1421 c = isl_constraint_set_constant_si(c, 1);
1422 bmap2 = isl_basic_map_from_constraint(c);
1424 bmap1 = isl_basic_map_upper_bound_si(bmap1, isl_dim_in, 0, pos - 1);
1425 bmap2 = isl_basic_map_lower_bound_si(bmap2, isl_dim_in, 0, pos);
1427 return isl_basic_map_union(bmap1, bmap2);
1430 static const char *option_str[] = {
1431 [atomic] = "atomic",
1432 [unroll] = "unroll",
1433 [separate] = "separate"
1436 /* Update the "options" to reflect the insertion of a dimension
1437 * at position "pos" in the schedule domain space.
1438 * "space" is the original domain space before the insertion and
1439 * may be named and/or structured.
1441 * The (relevant) input options all have "space" as domain, which
1442 * has to be mapped to the extended space.
1443 * The values of the ranges also refer to the schedule domain positions
1444 * and they therefore also need to be adjusted. In particular, values
1445 * smaller than pos do not need to change, while values greater than or
1446 * equal to pos need to be incremented.
1447 * That is, we need to apply the following map.
1449 * { atomic[i] -> atomic[i] : i < pos; [i] -> [i + 1] : i >= pos;
1450 * unroll[i] -> unroll[i] : i < pos; [i] -> [i + 1] : i >= pos;
1451 * separate[i] -> separate[i] : i < pos; [i] -> [i + 1] : i >= pos;
1452 * separation_class[[i] -> [c]]
1453 * -> separation_class[[i] -> [c]] : i < pos;
1454 * separation_class[[i] -> [c]]
1455 * -> separation_class[[i + 1] -> [c]] : i >= pos }
1457 static __isl_give isl_union_map *options_insert_dim(
1458 __isl_take isl_union_map *options, __isl_take isl_space *space, int pos)
1460 isl_map *map;
1461 isl_union_map *insertion;
1462 enum isl_ast_build_domain_type type;
1463 const char *name = "separation_class";
1465 space = isl_space_map_from_set(space);
1466 map = isl_map_identity(space);
1467 map = isl_map_insert_dims(map, isl_dim_out, pos, 1);
1468 options = isl_union_map_apply_domain(options,
1469 isl_union_map_from_map(map));
1471 if (!options)
1472 return NULL;
1474 map = construct_insertion_map(isl_union_map_get_space(options), pos);
1476 insertion = isl_union_map_empty(isl_union_map_get_space(options));
1478 for (type = atomic; type <= separate; ++type) {
1479 isl_map *map_type = isl_map_copy(map);
1480 const char *name = option_str[type];
1481 map_type = isl_map_set_tuple_name(map_type, isl_dim_in, name);
1482 map_type = isl_map_set_tuple_name(map_type, isl_dim_out, name);
1483 insertion = isl_union_map_add_map(insertion, map_type);
1486 map = isl_map_product(map, isl_map_identity(isl_map_get_space(map)));
1487 map = isl_map_set_tuple_name(map, isl_dim_in, name);
1488 map = isl_map_set_tuple_name(map, isl_dim_out, name);
1489 insertion = isl_union_map_add_map(insertion, map);
1491 options = isl_union_map_apply_range(options, insertion);
1493 return options;
1496 /* Insert a single dimension in the schedule domain at position "pos".
1497 * The new dimension is given an isl_id with the empty string as name.
1499 * The main difficulty is updating build->options to reflect the
1500 * extra dimension. This is handled in options_insert_dim.
1502 * Note that because of the dimension manipulations, the resulting
1503 * schedule domain space will always be unnamed and unstructured.
1504 * However, the original schedule domain space may be named and/or
1505 * structured, so we have to take this possibility into account
1506 * while performing the transformations.
1508 __isl_give isl_ast_build *isl_ast_build_insert_dim(
1509 __isl_take isl_ast_build *build, int pos)
1511 isl_ctx *ctx;
1512 isl_space *space, *ma_space;
1513 isl_id *id;
1514 isl_multi_aff *ma;
1516 build = isl_ast_build_cow(build);
1517 if (!build)
1518 return NULL;
1520 ctx = isl_ast_build_get_ctx(build);
1521 id = isl_id_alloc(ctx, "", NULL);
1522 space = isl_ast_build_get_space(build, 1);
1523 build->iterators = isl_id_list_insert(build->iterators, pos, id);
1524 build->domain = isl_set_insert_dims(build->domain,
1525 isl_dim_set, pos, 1);
1526 build->generated = isl_set_insert_dims(build->generated,
1527 isl_dim_set, pos, 1);
1528 build->pending = isl_set_insert_dims(build->pending,
1529 isl_dim_set, pos, 1);
1530 build->strides = isl_vec_insert_els(build->strides, pos, 1);
1531 build->strides = isl_vec_set_element_si(build->strides, pos, 1);
1532 ma_space = isl_space_params(isl_multi_aff_get_space(build->offsets));
1533 ma_space = isl_space_set_from_params(ma_space);
1534 ma_space = isl_space_add_dims(ma_space, isl_dim_set, 1);
1535 ma_space = isl_space_map_from_set(ma_space);
1536 ma = isl_multi_aff_zero(isl_space_copy(ma_space));
1537 build->offsets = isl_multi_aff_splice(build->offsets, pos, pos, ma);
1538 ma = isl_multi_aff_identity(ma_space);
1539 build->values = isl_multi_aff_splice(build->values, pos, pos, ma);
1540 build->options = options_insert_dim(build->options, space, pos);
1542 if (!build->iterators || !build->domain || !build->generated ||
1543 !build->pending || !build->values ||
1544 !build->strides || !build->offsets || !build->options)
1545 return isl_ast_build_free(build);
1547 return build;
1550 /* Scale down the current dimension by a factor of "m".
1551 * "umap" is an isl_union_map that implements the scaling down.
1552 * That is, it is of the form
1554 * { [.... i ....] -> [.... i' ....] : i = m i' }
1556 * This function is called right after the strides have been
1557 * detected, but before any constraints on the current dimension
1558 * have been included in build->domain.
1559 * We therefore only need to update stride, offset and the options.
1561 __isl_give isl_ast_build *isl_ast_build_scale_down(
1562 __isl_take isl_ast_build *build, __isl_take isl_val *m,
1563 __isl_take isl_union_map *umap)
1565 isl_aff *aff;
1566 isl_val *v;
1567 int depth;
1569 build = isl_ast_build_cow(build);
1570 if (!build || !umap || !m)
1571 goto error;
1573 depth = build->depth;
1575 v = isl_vec_get_element_val(build->strides, depth);
1576 v = isl_val_div(v, isl_val_copy(m));
1577 build->strides = isl_vec_set_element_val(build->strides, depth, v);
1579 aff = isl_multi_aff_get_aff(build->offsets, depth);
1580 aff = isl_aff_scale_down_val(aff, m);
1581 build->offsets = isl_multi_aff_set_aff(build->offsets, depth, aff);
1582 build->options = isl_union_map_apply_domain(build->options, umap);
1583 if (!build->strides || !build->offsets || !build->options)
1584 return isl_ast_build_free(build);
1586 return build;
1587 error:
1588 isl_val_free(m);
1589 isl_union_map_free(umap);
1590 return isl_ast_build_free(build);
1593 /* Return a list of "n" isl_ids called "c%d", with "%d" starting at "first".
1594 * If an isl_id with such a name already appears among the parameters
1595 * in build->domain, then adjust the name to "c%d_%d".
1597 static __isl_give isl_id_list *generate_names(isl_ctx *ctx, int n, int first,
1598 __isl_keep isl_ast_build *build)
1600 int i;
1601 isl_id_list *names;
1603 names = isl_id_list_alloc(ctx, n);
1604 for (i = 0; i < n; ++i) {
1605 isl_id *id;
1607 id = generate_name(ctx, first + i, build);
1608 names = isl_id_list_add(names, id);
1611 return names;
1614 /* Embed "options" into the given isl_ast_build space.
1616 * This function is called from within a nested call to
1617 * isl_ast_build_ast_from_schedule.
1618 * "options" refers to the additional schedule,
1619 * while space refers to both the space of the outer isl_ast_build and
1620 * that of the additional schedule.
1621 * Specifically, space is of the form
1623 * [I -> S]
1625 * while options lives in the space(s)
1627 * S -> *
1629 * We compute
1631 * [I -> S] -> S
1633 * and compose this with options, to obtain the new options
1634 * living in the space(s)
1636 * [I -> S] -> *
1638 static __isl_give isl_union_map *embed_options(
1639 __isl_take isl_union_map *options, __isl_take isl_space *space)
1641 isl_map *map;
1643 map = isl_map_universe(isl_space_unwrap(space));
1644 map = isl_map_range_map(map);
1646 options = isl_union_map_apply_range(
1647 isl_union_map_from_map(map), options);
1649 return options;
1652 /* Update "build" for use in a (possibly nested) code generation. That is,
1653 * extend "build" from an AST build on some domain O to an AST build
1654 * on domain [O -> S], with S corresponding to "space".
1655 * If the original domain is a parameter domain, then the new domain is
1656 * simply S.
1657 * "iterators" is a list of iterators for S, but the number of elements
1658 * may be smaller or greater than the number of set dimensions of S.
1659 * If "keep_iterators" is set, then any extra ids in build->iterators
1660 * are reused for S. Otherwise, these extra ids are dropped.
1662 * We first update build->outer_pos to the current depth.
1663 * This depth is zero in case this is the outermost code generation.
1665 * We then add additional ids such that the number of iterators is at least
1666 * equal to the dimension of the new build domain.
1668 * If the original domain is parametric, then we are constructing
1669 * an isl_ast_build for the outer code generation and we pass control
1670 * to isl_ast_build_init.
1672 * Otherwise, we adjust the fields of "build" to include "space".
1674 __isl_give isl_ast_build *isl_ast_build_product(
1675 __isl_take isl_ast_build *build, __isl_take isl_space *space)
1677 isl_ctx *ctx;
1678 isl_vec *strides;
1679 isl_set *set;
1680 isl_multi_aff *embedding;
1681 int dim, n_it;
1683 build = isl_ast_build_cow(build);
1684 if (!build)
1685 goto error;
1687 build->outer_pos = build->depth;
1689 ctx = isl_ast_build_get_ctx(build);
1690 dim = isl_set_dim(build->domain, isl_dim_set);
1691 dim += isl_space_dim(space, isl_dim_set);
1692 n_it = isl_id_list_n_id(build->iterators);
1693 if (n_it < dim) {
1694 isl_id_list *l;
1695 l = generate_names(ctx, dim - n_it, n_it, build);
1696 build->iterators = isl_id_list_concat(build->iterators, l);
1699 if (isl_set_is_params(build->domain))
1700 return isl_ast_build_init(build, space);
1702 set = isl_set_universe(isl_space_copy(space));
1703 build->domain = isl_set_product(build->domain, isl_set_copy(set));
1704 build->pending = isl_set_product(build->pending, isl_set_copy(set));
1705 build->generated = isl_set_product(build->generated, set);
1707 strides = isl_vec_alloc(ctx, isl_space_dim(space, isl_dim_set));
1708 strides = isl_vec_set_si(strides, 1);
1709 build->strides = isl_vec_concat(build->strides, strides);
1711 space = isl_space_map_from_set(space);
1712 build->offsets = isl_multi_aff_align_params(build->offsets,
1713 isl_space_copy(space));
1714 build->offsets = isl_multi_aff_product(build->offsets,
1715 isl_multi_aff_zero(isl_space_copy(space)));
1716 build->values = isl_multi_aff_align_params(build->values,
1717 isl_space_copy(space));
1718 embedding = isl_multi_aff_identity(space);
1719 build->values = isl_multi_aff_product(build->values, embedding);
1721 space = isl_ast_build_get_space(build, 1);
1722 build->options = embed_options(build->options, space);
1724 if (!build->iterators || !build->domain || !build->generated ||
1725 !build->pending || !build->values ||
1726 !build->strides || !build->offsets || !build->options)
1727 return isl_ast_build_free(build);
1729 return build;
1730 error:
1731 isl_ast_build_free(build);
1732 isl_space_free(space);
1733 return NULL;
1736 /* Does "aff" only attain non-negative values over build->domain?
1737 * That is, does it not attain any negative values?
1739 int isl_ast_build_aff_is_nonneg(__isl_keep isl_ast_build *build,
1740 __isl_keep isl_aff *aff)
1742 isl_set *test;
1743 int empty;
1745 if (!build)
1746 return -1;
1748 aff = isl_aff_copy(aff);
1749 test = isl_set_from_basic_set(isl_aff_neg_basic_set(aff));
1750 test = isl_set_intersect(test, isl_set_copy(build->domain));
1751 empty = isl_set_is_empty(test);
1752 isl_set_free(test);
1754 return empty;
1757 /* Does the dimension at (internal) position "pos" have a non-trivial stride?
1759 int isl_ast_build_has_stride(__isl_keep isl_ast_build *build, int pos)
1761 isl_val *v;
1762 int has_stride;
1764 if (!build)
1765 return -1;
1767 v = isl_vec_get_element_val(build->strides, pos);
1768 if (!v)
1769 return -1;
1770 has_stride = !isl_val_is_one(v);
1771 isl_val_free(v);
1773 return has_stride;
1776 /* Given that the dimension at position "pos" takes on values
1778 * f + s a
1780 * with a an integer, return s through *stride.
1782 __isl_give isl_val *isl_ast_build_get_stride(__isl_keep isl_ast_build *build,
1783 int pos)
1785 if (!build)
1786 return NULL;
1788 return isl_vec_get_element_val(build->strides, pos);
1791 /* Given that the dimension at position "pos" takes on values
1793 * f + s a
1795 * with a an integer, return f.
1797 __isl_give isl_aff *isl_ast_build_get_offset(
1798 __isl_keep isl_ast_build *build, int pos)
1800 if (!build)
1801 return NULL;
1803 return isl_multi_aff_get_aff(build->offsets, pos);
1806 /* Is the dimension at position "pos" known to attain only a single
1807 * value that, moreover, can be described by a single affine expression
1808 * in terms of the outer dimensions and parameters?
1810 * If not, then the correponding affine expression in build->values
1811 * is set to be equal to the same input dimension.
1812 * Otherwise, it is set to the requested expression in terms of
1813 * outer dimensions and parameters.
1815 int isl_ast_build_has_affine_value(__isl_keep isl_ast_build *build,
1816 int pos)
1818 isl_aff *aff;
1819 int involves;
1821 if (!build)
1822 return -1;
1824 aff = isl_multi_aff_get_aff(build->values, pos);
1825 involves = isl_aff_involves_dims(aff, isl_dim_in, pos, 1);
1826 isl_aff_free(aff);
1828 if (involves < 0)
1829 return -1;
1831 return !involves;
1834 /* Plug in the known values (fixed affine expressions in terms of
1835 * parameters and outer loop iterators) of all loop iterators
1836 * in the domain of "umap".
1838 * We simply precompose "umap" with build->values.
1840 __isl_give isl_union_map *isl_ast_build_substitute_values_union_map_domain(
1841 __isl_keep isl_ast_build *build, __isl_take isl_union_map *umap)
1843 isl_multi_aff *values;
1845 if (!build)
1846 return isl_union_map_free(umap);
1848 values = isl_multi_aff_copy(build->values);
1849 umap = isl_union_map_preimage_domain_multi_aff(umap, values);
1851 return umap;
1854 /* Is the current dimension known to attain only a single value?
1856 int isl_ast_build_has_value(__isl_keep isl_ast_build *build)
1858 if (!build)
1859 return -1;
1861 return build->value != NULL;
1864 /* Simplify the basic set "bset" based on what we know about
1865 * the iterators of already generated loops.
1867 * "bset" is assumed to live in the (internal) schedule domain.
1869 __isl_give isl_basic_set *isl_ast_build_compute_gist_basic_set(
1870 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
1872 if (!build)
1873 goto error;
1875 bset = isl_basic_set_preimage_multi_aff(bset,
1876 isl_multi_aff_copy(build->values));
1877 bset = isl_basic_set_gist(bset,
1878 isl_set_simple_hull(isl_set_copy(build->domain)));
1880 return bset;
1881 error:
1882 isl_basic_set_free(bset);
1883 return NULL;
1886 /* Simplify the set "set" based on what we know about
1887 * the iterators of already generated loops.
1889 * "set" is assumed to live in the (internal) schedule domain.
1891 __isl_give isl_set *isl_ast_build_compute_gist(
1892 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
1894 if (!build)
1895 goto error;
1897 set = isl_set_preimage_multi_aff(set,
1898 isl_multi_aff_copy(build->values));
1899 set = isl_set_gist(set, isl_set_copy(build->domain));
1901 return set;
1902 error:
1903 isl_set_free(set);
1904 return NULL;
1907 /* Include information about what we know about the iterators of
1908 * already generated loops to "set".
1910 * We currently only plug in the known affine values of outer loop
1911 * iterators.
1912 * In principle we could also introduce equalities or even other
1913 * constraints implied by the intersection of "set" and build->domain.
1915 __isl_give isl_set *isl_ast_build_specialize(__isl_keep isl_ast_build *build,
1916 __isl_take isl_set *set)
1918 if (!build)
1919 return isl_set_free(set);
1921 return isl_set_preimage_multi_aff(set,
1922 isl_multi_aff_copy(build->values));
1925 /* Simplify the map "map" based on what we know about
1926 * the iterators of already generated loops.
1928 * The domain of "map" is assumed to live in the (internal) schedule domain.
1930 __isl_give isl_map *isl_ast_build_compute_gist_map_domain(
1931 __isl_keep isl_ast_build *build, __isl_take isl_map *map)
1933 if (!build)
1934 goto error;
1936 map = isl_map_gist_domain(map, isl_set_copy(build->domain));
1938 return map;
1939 error:
1940 isl_map_free(map);
1941 return NULL;
1944 /* Simplify the affine expression "aff" based on what we know about
1945 * the iterators of already generated loops.
1947 * The domain of "aff" is assumed to live in the (internal) schedule domain.
1949 __isl_give isl_aff *isl_ast_build_compute_gist_aff(
1950 __isl_keep isl_ast_build *build, __isl_take isl_aff *aff)
1952 if (!build)
1953 goto error;
1955 aff = isl_aff_gist(aff, isl_set_copy(build->domain));
1957 return aff;
1958 error:
1959 isl_aff_free(aff);
1960 return NULL;
1963 /* Simplify the piecewise affine expression "aff" based on what we know about
1964 * the iterators of already generated loops.
1966 * The domain of "pa" is assumed to live in the (internal) schedule domain.
1968 __isl_give isl_pw_aff *isl_ast_build_compute_gist_pw_aff(
1969 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
1971 if (!build)
1972 goto error;
1974 if (!isl_set_is_params(build->domain))
1975 pa = isl_pw_aff_pullback_multi_aff(pa,
1976 isl_multi_aff_copy(build->values));
1977 pa = isl_pw_aff_gist(pa, isl_set_copy(build->domain));
1979 return pa;
1980 error:
1981 isl_pw_aff_free(pa);
1982 return NULL;
1985 /* Simplify the piecewise multi-affine expression "aff" based on what
1986 * we know about the iterators of already generated loops.
1988 * The domain of "pma" is assumed to live in the (internal) schedule domain.
1990 __isl_give isl_pw_multi_aff *isl_ast_build_compute_gist_pw_multi_aff(
1991 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
1993 if (!build)
1994 goto error;
1996 pma = isl_pw_multi_aff_pullback_multi_aff(pma,
1997 isl_multi_aff_copy(build->values));
1998 pma = isl_pw_multi_aff_gist(pma, isl_set_copy(build->domain));
2000 return pma;
2001 error:
2002 isl_pw_multi_aff_free(pma);
2003 return NULL;
2006 /* Extract the schedule domain of the given type from build->options
2007 * at the current depth.
2009 * In particular, find the subset of build->options that is of
2010 * the following form
2012 * schedule_domain -> type[depth]
2014 * and return the corresponding domain, after eliminating inner dimensions
2015 * and divs that depend on the current dimension.
2017 * Note that the domain of build->options has been reformulated
2018 * in terms of the internal build space in embed_options,
2019 * but the position is still that within the current code generation.
2021 __isl_give isl_set *isl_ast_build_get_option_domain(
2022 __isl_keep isl_ast_build *build,
2023 enum isl_ast_build_domain_type type)
2025 const char *name;
2026 isl_space *space;
2027 isl_map *option;
2028 isl_set *domain;
2029 int local_pos;
2031 if (!build)
2032 return NULL;
2034 name = option_str[type];
2035 local_pos = build->depth - build->outer_pos;
2037 space = isl_ast_build_get_space(build, 1);
2038 space = isl_space_from_domain(space);
2039 space = isl_space_add_dims(space, isl_dim_out, 1);
2040 space = isl_space_set_tuple_name(space, isl_dim_out, name);
2042 option = isl_union_map_extract_map(build->options, space);
2043 option = isl_map_fix_si(option, isl_dim_out, 0, local_pos);
2045 domain = isl_map_domain(option);
2046 domain = isl_ast_build_eliminate(build, domain);
2048 return domain;
2051 /* Extract the separation class mapping at the current depth.
2053 * In particular, find and return the subset of build->options that is of
2054 * the following form
2056 * schedule_domain -> separation_class[[depth] -> [class]]
2058 * The caller is expected to eliminate inner dimensions from the domain.
2060 * Note that the domain of build->options has been reformulated
2061 * in terms of the internal build space in embed_options,
2062 * but the position is still that within the current code generation.
2064 __isl_give isl_map *isl_ast_build_get_separation_class(
2065 __isl_keep isl_ast_build *build)
2067 isl_ctx *ctx;
2068 isl_space *space_sep, *space;
2069 isl_map *res;
2070 int local_pos;
2072 if (!build)
2073 return NULL;
2075 local_pos = build->depth - build->outer_pos;
2076 ctx = isl_ast_build_get_ctx(build);
2077 space_sep = isl_space_alloc(ctx, 0, 1, 1);
2078 space_sep = isl_space_wrap(space_sep);
2079 space_sep = isl_space_set_tuple_name(space_sep, isl_dim_set,
2080 "separation_class");
2081 space = isl_ast_build_get_space(build, 1);
2082 space_sep = isl_space_align_params(space_sep, isl_space_copy(space));
2083 space = isl_space_map_from_domain_and_range(space, space_sep);
2085 res = isl_union_map_extract_map(build->options, space);
2086 res = isl_map_fix_si(res, isl_dim_out, 0, local_pos);
2087 res = isl_map_coalesce(res);
2089 return res;
2092 /* Eliminate dimensions inner to the current dimension.
2094 __isl_give isl_set *isl_ast_build_eliminate_inner(
2095 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2097 int dim;
2098 int depth;
2100 if (!build)
2101 return isl_set_free(set);
2103 dim = isl_set_dim(set, isl_dim_set);
2104 depth = build->depth;
2105 set = isl_set_detect_equalities(set);
2106 set = isl_set_eliminate(set, isl_dim_set, depth + 1, dim - (depth + 1));
2108 return set;
2111 /* Eliminate unknown divs and divs that depend on the current dimension.
2113 * Note that during the elimination of unknown divs, we may discover
2114 * an explicit representation of some other unknown divs, which may
2115 * depend on the current dimension. We therefore need to eliminate
2116 * unknown divs first.
2118 __isl_give isl_set *isl_ast_build_eliminate_divs(
2119 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2121 int depth;
2123 if (!build)
2124 return isl_set_free(set);
2126 set = isl_set_remove_unknown_divs(set);
2127 depth = build->depth;
2128 set = isl_set_remove_divs_involving_dims(set, isl_dim_set, depth, 1);
2130 return set;
2133 /* Eliminate dimensions inner to the current dimension as well as
2134 * unknown divs and divs that depend on the current dimension.
2135 * The result then consists only of constraints that are independent
2136 * of the current dimension and upper and lower bounds on the current
2137 * dimension.
2139 __isl_give isl_set *isl_ast_build_eliminate(
2140 __isl_keep isl_ast_build *build, __isl_take isl_set *domain)
2142 domain = isl_ast_build_eliminate_inner(build, domain);
2143 domain = isl_ast_build_eliminate_divs(build, domain);
2144 return domain;
2147 /* Replace build->single_valued by "sv".
2149 __isl_give isl_ast_build *isl_ast_build_set_single_valued(
2150 __isl_take isl_ast_build *build, int sv)
2152 if (!build)
2153 return build;
2154 if (build->single_valued == sv)
2155 return build;
2156 build = isl_ast_build_cow(build);
2157 if (!build)
2158 return build;
2159 build->single_valued = sv;
2161 return build;