isl_map_partial_lexopt*: extract out isl_map_set_aligned_params
[isl.git] / isl_map.c
blob71eea8b139bf6661691bc67bda0955e1e3f2ca4c
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
22 #include <string.h>
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_blk.h>
26 #include <isl/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
30 #include <isl_seq.h>
31 #include <isl/set.h>
32 #include <isl/map.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
35 #include <isl_sort.h>
36 #include "isl_tab.h"
37 #include <isl/vec.h>
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
56 switch (type) {
57 case isl_dim_param: return dim->nparam;
58 case isl_dim_in: return dim->n_in;
59 case isl_dim_out: return dim->n_out;
60 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
61 default: return 0;
65 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
67 switch (type) {
68 case isl_dim_param: return 1;
69 case isl_dim_in: return 1 + dim->nparam;
70 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
71 default: return 0;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
76 enum isl_dim_type type)
78 if (!bmap)
79 return 0;
80 switch (type) {
81 case isl_dim_cst: return 1;
82 case isl_dim_param:
83 case isl_dim_in:
84 case isl_dim_out: return isl_space_dim(bmap->dim, type);
85 case isl_dim_div: return bmap->n_div;
86 case isl_dim_all: return isl_basic_map_total_dim(bmap);
87 default: return 0;
91 /* Return the space of "map".
93 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
95 return map ? map->dim : NULL;
98 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
100 return map ? n(map->dim, type) : 0;
103 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
105 return set ? n(set->dim, type) : 0;
108 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
109 enum isl_dim_type type)
111 isl_space *space;
113 if (!bmap)
114 return 0;
116 space = bmap->dim;
117 switch (type) {
118 case isl_dim_cst: return 0;
119 case isl_dim_param: return 1;
120 case isl_dim_in: return 1 + space->nparam;
121 case isl_dim_out: return 1 + space->nparam + space->n_in;
122 case isl_dim_div: return 1 + space->nparam + space->n_in +
123 space->n_out;
124 default: return 0;
128 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
129 enum isl_dim_type type)
131 return isl_basic_map_offset(bset, type);
134 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
136 return pos(map->dim, type);
139 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
140 enum isl_dim_type type)
142 return isl_basic_map_dim(bset, type);
145 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
147 return isl_basic_set_dim(bset, isl_dim_set);
150 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
152 return isl_basic_set_dim(bset, isl_dim_param);
155 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
157 if (!bset)
158 return 0;
159 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
162 unsigned isl_set_n_dim(__isl_keep isl_set *set)
164 return isl_set_dim(set, isl_dim_set);
167 unsigned isl_set_n_param(__isl_keep isl_set *set)
169 return isl_set_dim(set, isl_dim_param);
172 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
174 return bmap ? bmap->dim->n_in : 0;
177 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
179 return bmap ? bmap->dim->n_out : 0;
182 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
184 return bmap ? bmap->dim->nparam : 0;
187 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
189 return bmap ? bmap->n_div : 0;
192 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
194 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
197 unsigned isl_map_n_in(const struct isl_map *map)
199 return map ? map->dim->n_in : 0;
202 unsigned isl_map_n_out(const struct isl_map *map)
204 return map ? map->dim->n_out : 0;
207 unsigned isl_map_n_param(const struct isl_map *map)
209 return map ? map->dim->nparam : 0;
212 /* Do "bmap1" and "bmap2" have the same parameters?
214 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
215 __isl_keep isl_basic_map *bmap2)
217 isl_space *space1, *space2;
219 space1 = isl_basic_map_peek_space(bmap1);
220 space2 = isl_basic_map_peek_space(bmap2);
221 return isl_space_match(space1, isl_dim_param, space2, isl_dim_param);
224 /* Do "map1" and "map2" have the same parameters?
226 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
227 __isl_keep isl_map *map2)
229 isl_space *space1, *space2;
231 space1 = isl_map_peek_space(map1);
232 space2 = isl_map_peek_space(map2);
233 return isl_space_match(space1, isl_dim_param, space2, isl_dim_param);
236 /* Do "map" and "set" have the same parameters?
238 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
239 __isl_keep isl_set *set)
241 return isl_map_has_equal_params(map, set_to_map(set));
244 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
245 __isl_keep isl_set *set)
247 isl_bool m;
248 if (!map || !set)
249 return isl_bool_error;
250 m = isl_map_has_equal_params(map, set_to_map(set));
251 if (m < 0 || !m)
252 return m;
253 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
254 set->dim, isl_dim_set);
257 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
258 __isl_keep isl_basic_set *bset)
260 isl_bool m;
261 if (!bmap || !bset)
262 return isl_bool_error;
263 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
264 if (m < 0 || !m)
265 return m;
266 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
267 bset->dim, isl_dim_set);
270 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
271 __isl_keep isl_set *set)
273 isl_bool m;
274 if (!map || !set)
275 return isl_bool_error;
276 m = isl_map_has_equal_params(map, set_to_map(set));
277 if (m < 0 || !m)
278 return m;
279 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
280 set->dim, isl_dim_set);
283 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
284 __isl_keep isl_basic_set *bset)
286 isl_bool m;
287 if (!bmap || !bset)
288 return isl_bool_error;
289 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
290 if (m < 0 || !m)
291 return m;
292 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
293 bset->dim, isl_dim_set);
296 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
298 return bmap ? bmap->ctx : NULL;
301 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
303 return bset ? bset->ctx : NULL;
306 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
308 return map ? map->ctx : NULL;
311 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
313 return set ? set->ctx : NULL;
316 /* Return the space of "bmap".
318 __isl_keep isl_space *isl_basic_map_peek_space(
319 __isl_keep const isl_basic_map *bmap)
321 return bmap ? bmap->dim : NULL;
324 /* Return the space of "bset".
326 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
328 return isl_basic_map_peek_space(bset_to_bmap(bset));
331 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
333 return isl_space_copy(isl_basic_map_peek_space(bmap));
336 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
338 return isl_basic_map_get_space(bset_to_bmap(bset));
341 /* Extract the divs in "bmap" as a matrix.
343 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
345 int i;
346 isl_ctx *ctx;
347 isl_mat *div;
348 unsigned total;
349 unsigned cols;
351 if (!bmap)
352 return NULL;
354 ctx = isl_basic_map_get_ctx(bmap);
355 total = isl_space_dim(bmap->dim, isl_dim_all);
356 cols = 1 + 1 + total + bmap->n_div;
357 div = isl_mat_alloc(ctx, bmap->n_div, cols);
358 if (!div)
359 return NULL;
361 for (i = 0; i < bmap->n_div; ++i)
362 isl_seq_cpy(div->row[i], bmap->div[i], cols);
364 return div;
367 /* Extract the divs in "bset" as a matrix.
369 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
371 return isl_basic_map_get_divs(bset);
374 __isl_give isl_local_space *isl_basic_map_get_local_space(
375 __isl_keep isl_basic_map *bmap)
377 isl_mat *div;
379 if (!bmap)
380 return NULL;
382 div = isl_basic_map_get_divs(bmap);
383 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
386 __isl_give isl_local_space *isl_basic_set_get_local_space(
387 __isl_keep isl_basic_set *bset)
389 return isl_basic_map_get_local_space(bset);
392 /* For each known div d = floor(f/m), add the constraints
394 * f - m d >= 0
395 * -(f-(m-1)) + m d >= 0
397 * Do not finalize the result.
399 static __isl_give isl_basic_map *add_known_div_constraints(
400 __isl_take isl_basic_map *bmap)
402 int i;
403 unsigned n_div;
405 if (!bmap)
406 return NULL;
407 n_div = isl_basic_map_dim(bmap, isl_dim_div);
408 if (n_div == 0)
409 return bmap;
410 bmap = isl_basic_map_cow(bmap);
411 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
412 if (!bmap)
413 return NULL;
414 for (i = 0; i < n_div; ++i) {
415 if (isl_int_is_zero(bmap->div[i][0]))
416 continue;
417 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
418 return isl_basic_map_free(bmap);
421 return bmap;
424 __isl_give isl_basic_map *isl_basic_map_from_local_space(
425 __isl_take isl_local_space *ls)
427 int i;
428 int n_div;
429 isl_basic_map *bmap;
431 if (!ls)
432 return NULL;
434 n_div = isl_local_space_dim(ls, isl_dim_div);
435 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
436 n_div, 0, 2 * n_div);
438 for (i = 0; i < n_div; ++i)
439 if (isl_basic_map_alloc_div(bmap) < 0)
440 goto error;
442 for (i = 0; i < n_div; ++i)
443 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
444 bmap = add_known_div_constraints(bmap);
446 isl_local_space_free(ls);
447 return bmap;
448 error:
449 isl_local_space_free(ls);
450 isl_basic_map_free(bmap);
451 return NULL;
454 __isl_give isl_basic_set *isl_basic_set_from_local_space(
455 __isl_take isl_local_space *ls)
457 return isl_basic_map_from_local_space(ls);
460 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
462 return isl_space_copy(isl_map_peek_space(map));
465 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
467 if (!set)
468 return NULL;
469 return isl_space_copy(set->dim);
472 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
473 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
475 bmap = isl_basic_map_cow(bmap);
476 if (!bmap)
477 return NULL;
478 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
479 if (!bmap->dim)
480 goto error;
481 bmap = isl_basic_map_finalize(bmap);
482 return bmap;
483 error:
484 isl_basic_map_free(bmap);
485 return NULL;
488 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
489 __isl_take isl_basic_set *bset, const char *s)
491 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
494 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
495 enum isl_dim_type type)
497 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
500 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
501 enum isl_dim_type type, const char *s)
503 int i;
505 map = isl_map_cow(map);
506 if (!map)
507 return NULL;
509 map->dim = isl_space_set_tuple_name(map->dim, type, s);
510 if (!map->dim)
511 goto error;
513 for (i = 0; i < map->n; ++i) {
514 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
515 if (!map->p[i])
516 goto error;
519 return map;
520 error:
521 isl_map_free(map);
522 return NULL;
525 /* Replace the identifier of the tuple of type "type" by "id".
527 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
528 __isl_take isl_basic_map *bmap,
529 enum isl_dim_type type, __isl_take isl_id *id)
531 bmap = isl_basic_map_cow(bmap);
532 if (!bmap)
533 goto error;
534 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
535 if (!bmap->dim)
536 return isl_basic_map_free(bmap);
537 bmap = isl_basic_map_finalize(bmap);
538 return bmap;
539 error:
540 isl_id_free(id);
541 return NULL;
544 /* Replace the identifier of the tuple by "id".
546 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
547 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
549 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
552 /* Does the input or output tuple have a name?
554 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
556 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
559 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
560 enum isl_dim_type type)
562 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
565 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
566 const char *s)
568 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
569 isl_dim_set, s));
572 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
573 enum isl_dim_type type, __isl_take isl_id *id)
575 map = isl_map_cow(map);
576 if (!map)
577 goto error;
579 map->dim = isl_space_set_tuple_id(map->dim, type, id);
581 return isl_map_reset_space(map, isl_space_copy(map->dim));
582 error:
583 isl_id_free(id);
584 return NULL;
587 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
588 __isl_take isl_id *id)
590 return isl_map_set_tuple_id(set, isl_dim_set, id);
593 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
594 enum isl_dim_type type)
596 map = isl_map_cow(map);
597 if (!map)
598 return NULL;
600 map->dim = isl_space_reset_tuple_id(map->dim, type);
602 return isl_map_reset_space(map, isl_space_copy(map->dim));
605 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
607 return isl_map_reset_tuple_id(set, isl_dim_set);
610 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
612 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
615 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
616 enum isl_dim_type type)
618 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
621 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
623 return isl_map_has_tuple_id(set, isl_dim_set);
626 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
628 return isl_map_get_tuple_id(set, isl_dim_set);
631 /* Does the set tuple have a name?
633 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
635 if (!set)
636 return isl_bool_error;
637 return isl_space_has_tuple_name(set->dim, isl_dim_set);
641 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
643 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
646 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
648 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
651 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
652 enum isl_dim_type type, unsigned pos)
654 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
657 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
658 enum isl_dim_type type, unsigned pos)
660 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
663 /* Does the given dimension have a name?
665 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
666 enum isl_dim_type type, unsigned pos)
668 if (!map)
669 return isl_bool_error;
670 return isl_space_has_dim_name(map->dim, type, pos);
673 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
674 enum isl_dim_type type, unsigned pos)
676 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
679 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
680 enum isl_dim_type type, unsigned pos)
682 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
685 /* Does the given dimension have a name?
687 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
688 enum isl_dim_type type, unsigned pos)
690 if (!set)
691 return isl_bool_error;
692 return isl_space_has_dim_name(set->dim, type, pos);
695 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
696 __isl_take isl_basic_map *bmap,
697 enum isl_dim_type type, unsigned pos, const char *s)
699 bmap = isl_basic_map_cow(bmap);
700 if (!bmap)
701 return NULL;
702 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
703 if (!bmap->dim)
704 goto error;
705 return isl_basic_map_finalize(bmap);
706 error:
707 isl_basic_map_free(bmap);
708 return NULL;
711 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
712 enum isl_dim_type type, unsigned pos, const char *s)
714 int i;
716 map = isl_map_cow(map);
717 if (!map)
718 return NULL;
720 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
721 if (!map->dim)
722 goto error;
724 for (i = 0; i < map->n; ++i) {
725 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
726 if (!map->p[i])
727 goto error;
730 return map;
731 error:
732 isl_map_free(map);
733 return NULL;
736 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
737 __isl_take isl_basic_set *bset,
738 enum isl_dim_type type, unsigned pos, const char *s)
740 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
741 type, pos, s));
744 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
745 enum isl_dim_type type, unsigned pos, const char *s)
747 return set_from_map(isl_map_set_dim_name(set_to_map(set),
748 type, pos, s));
751 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
752 enum isl_dim_type type, unsigned pos)
754 if (!bmap)
755 return isl_bool_error;
756 return isl_space_has_dim_id(bmap->dim, type, pos);
759 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
760 enum isl_dim_type type, unsigned pos)
762 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
765 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
766 enum isl_dim_type type, unsigned pos)
768 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
771 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
772 enum isl_dim_type type, unsigned pos)
774 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
777 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
778 enum isl_dim_type type, unsigned pos)
780 return isl_map_has_dim_id(set, type, pos);
783 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
784 enum isl_dim_type type, unsigned pos)
786 return isl_map_get_dim_id(set, type, pos);
789 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
790 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
792 map = isl_map_cow(map);
793 if (!map)
794 goto error;
796 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
798 return isl_map_reset_space(map, isl_space_copy(map->dim));
799 error:
800 isl_id_free(id);
801 return NULL;
804 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
805 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
807 return isl_map_set_dim_id(set, type, pos, id);
810 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
811 __isl_keep isl_id *id)
813 if (!map)
814 return -1;
815 return isl_space_find_dim_by_id(map->dim, type, id);
818 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
819 __isl_keep isl_id *id)
821 return isl_map_find_dim_by_id(set, type, id);
824 /* Return the position of the dimension of the given type and name
825 * in "bmap".
826 * Return -1 if no such dimension can be found.
828 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
829 enum isl_dim_type type, const char *name)
831 if (!bmap)
832 return -1;
833 return isl_space_find_dim_by_name(bmap->dim, type, name);
836 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
837 const char *name)
839 if (!map)
840 return -1;
841 return isl_space_find_dim_by_name(map->dim, type, name);
844 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
845 const char *name)
847 return isl_map_find_dim_by_name(set, type, name);
850 /* Reset the user pointer on all identifiers of parameters and tuples
851 * of the space of "map".
853 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
855 isl_space *space;
857 space = isl_map_get_space(map);
858 space = isl_space_reset_user(space);
859 map = isl_map_reset_space(map, space);
861 return map;
864 /* Reset the user pointer on all identifiers of parameters and tuples
865 * of the space of "set".
867 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
869 return isl_map_reset_user(set);
872 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
874 if (!bmap)
875 return isl_bool_error;
876 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
879 /* Has "map" been marked as a rational map?
880 * In particular, have all basic maps in "map" been marked this way?
881 * An empty map is not considered to be rational.
882 * Maps where only some of the basic maps are marked rational
883 * are not allowed.
885 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
887 int i;
888 isl_bool rational;
890 if (!map)
891 return isl_bool_error;
892 if (map->n == 0)
893 return isl_bool_false;
894 rational = isl_basic_map_is_rational(map->p[0]);
895 if (rational < 0)
896 return rational;
897 for (i = 1; i < map->n; ++i) {
898 isl_bool rational_i;
900 rational_i = isl_basic_map_is_rational(map->p[i]);
901 if (rational_i < 0)
902 return rational_i;
903 if (rational != rational_i)
904 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
905 "mixed rational and integer basic maps "
906 "not supported", return isl_bool_error);
909 return rational;
912 /* Has "set" been marked as a rational set?
913 * In particular, have all basic set in "set" been marked this way?
914 * An empty set is not considered to be rational.
915 * Sets where only some of the basic sets are marked rational
916 * are not allowed.
918 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
920 return isl_map_is_rational(set);
923 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
925 return isl_basic_map_is_rational(bset);
928 /* Does "bmap" contain any rational points?
930 * If "bmap" has an equality for each dimension, equating the dimension
931 * to an integer constant, then it has no rational points, even if it
932 * is marked as rational.
934 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
936 isl_bool has_rational = isl_bool_true;
937 unsigned total;
939 if (!bmap)
940 return isl_bool_error;
941 if (isl_basic_map_plain_is_empty(bmap))
942 return isl_bool_false;
943 if (!isl_basic_map_is_rational(bmap))
944 return isl_bool_false;
945 bmap = isl_basic_map_copy(bmap);
946 bmap = isl_basic_map_implicit_equalities(bmap);
947 if (!bmap)
948 return isl_bool_error;
949 total = isl_basic_map_total_dim(bmap);
950 if (bmap->n_eq == total) {
951 int i, j;
952 for (i = 0; i < bmap->n_eq; ++i) {
953 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
954 if (j < 0)
955 break;
956 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
957 !isl_int_is_negone(bmap->eq[i][1 + j]))
958 break;
959 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
960 total - j - 1);
961 if (j >= 0)
962 break;
964 if (i == bmap->n_eq)
965 has_rational = isl_bool_false;
967 isl_basic_map_free(bmap);
969 return has_rational;
972 /* Does "map" contain any rational points?
974 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
976 int i;
977 isl_bool has_rational;
979 if (!map)
980 return isl_bool_error;
981 for (i = 0; i < map->n; ++i) {
982 has_rational = isl_basic_map_has_rational(map->p[i]);
983 if (has_rational < 0 || has_rational)
984 return has_rational;
986 return isl_bool_false;
989 /* Does "set" contain any rational points?
991 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
993 return isl_map_has_rational(set);
996 /* Is this basic set a parameter domain?
998 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1000 if (!bset)
1001 return isl_bool_error;
1002 return isl_space_is_params(bset->dim);
1005 /* Is this set a parameter domain?
1007 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1009 if (!set)
1010 return isl_bool_error;
1011 return isl_space_is_params(set->dim);
1014 /* Is this map actually a parameter domain?
1015 * Users should never call this function. Outside of isl,
1016 * a map can never be a parameter domain.
1018 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1020 if (!map)
1021 return isl_bool_error;
1022 return isl_space_is_params(map->dim);
1025 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
1026 struct isl_basic_map *bmap, unsigned extra,
1027 unsigned n_eq, unsigned n_ineq)
1029 int i;
1030 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1032 bmap->ctx = ctx;
1033 isl_ctx_ref(ctx);
1035 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1036 if (isl_blk_is_error(bmap->block))
1037 goto error;
1039 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1040 if ((n_ineq + n_eq) && !bmap->ineq)
1041 goto error;
1043 if (extra == 0) {
1044 bmap->block2 = isl_blk_empty();
1045 bmap->div = NULL;
1046 } else {
1047 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1048 if (isl_blk_is_error(bmap->block2))
1049 goto error;
1051 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1052 if (!bmap->div)
1053 goto error;
1056 for (i = 0; i < n_ineq + n_eq; ++i)
1057 bmap->ineq[i] = bmap->block.data + i * row_size;
1059 for (i = 0; i < extra; ++i)
1060 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1062 bmap->ref = 1;
1063 bmap->flags = 0;
1064 bmap->c_size = n_eq + n_ineq;
1065 bmap->eq = bmap->ineq + n_ineq;
1066 bmap->extra = extra;
1067 bmap->n_eq = 0;
1068 bmap->n_ineq = 0;
1069 bmap->n_div = 0;
1070 bmap->sample = NULL;
1072 return bmap;
1073 error:
1074 isl_basic_map_free(bmap);
1075 return NULL;
1078 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1079 unsigned nparam, unsigned dim, unsigned extra,
1080 unsigned n_eq, unsigned n_ineq)
1082 struct isl_basic_map *bmap;
1083 isl_space *space;
1085 space = isl_space_set_alloc(ctx, nparam, dim);
1086 if (!space)
1087 return NULL;
1089 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1090 return bset_from_bmap(bmap);
1093 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1094 unsigned extra, unsigned n_eq, unsigned n_ineq)
1096 struct isl_basic_map *bmap;
1097 if (!dim)
1098 return NULL;
1099 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1100 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1101 return bset_from_bmap(bmap);
1102 error:
1103 isl_space_free(dim);
1104 return NULL;
1107 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1108 unsigned extra, unsigned n_eq, unsigned n_ineq)
1110 struct isl_basic_map *bmap;
1112 if (!dim)
1113 return NULL;
1114 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1115 if (!bmap)
1116 goto error;
1117 bmap->dim = dim;
1119 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1120 error:
1121 isl_space_free(dim);
1122 return NULL;
1125 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1126 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1127 unsigned n_eq, unsigned n_ineq)
1129 struct isl_basic_map *bmap;
1130 isl_space *dim;
1132 dim = isl_space_alloc(ctx, nparam, in, out);
1133 if (!dim)
1134 return NULL;
1136 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1137 return bmap;
1140 static void dup_constraints(
1141 struct isl_basic_map *dst, struct isl_basic_map *src)
1143 int i;
1144 unsigned total = isl_basic_map_total_dim(src);
1146 for (i = 0; i < src->n_eq; ++i) {
1147 int j = isl_basic_map_alloc_equality(dst);
1148 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1151 for (i = 0; i < src->n_ineq; ++i) {
1152 int j = isl_basic_map_alloc_inequality(dst);
1153 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1156 for (i = 0; i < src->n_div; ++i) {
1157 int j = isl_basic_map_alloc_div(dst);
1158 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1160 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1163 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1165 struct isl_basic_map *dup;
1167 if (!bmap)
1168 return NULL;
1169 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1170 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1171 if (!dup)
1172 return NULL;
1173 dup_constraints(dup, bmap);
1174 dup->flags = bmap->flags;
1175 dup->sample = isl_vec_copy(bmap->sample);
1176 return dup;
1179 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1181 struct isl_basic_map *dup;
1183 dup = isl_basic_map_dup(bset_to_bmap(bset));
1184 return bset_from_bmap(dup);
1187 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1189 if (!bset)
1190 return NULL;
1192 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1193 bset->ref++;
1194 return bset;
1196 return isl_basic_set_dup(bset);
1199 struct isl_set *isl_set_copy(struct isl_set *set)
1201 if (!set)
1202 return NULL;
1204 set->ref++;
1205 return set;
1208 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1210 if (!bmap)
1211 return NULL;
1213 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1214 bmap->ref++;
1215 return bmap;
1217 bmap = isl_basic_map_dup(bmap);
1218 if (bmap)
1219 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1220 return bmap;
1223 struct isl_map *isl_map_copy(struct isl_map *map)
1225 if (!map)
1226 return NULL;
1228 map->ref++;
1229 return map;
1232 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1234 if (!bmap)
1235 return NULL;
1237 if (--bmap->ref > 0)
1238 return NULL;
1240 isl_ctx_deref(bmap->ctx);
1241 free(bmap->div);
1242 isl_blk_free(bmap->ctx, bmap->block2);
1243 free(bmap->ineq);
1244 isl_blk_free(bmap->ctx, bmap->block);
1245 isl_vec_free(bmap->sample);
1246 isl_space_free(bmap->dim);
1247 free(bmap);
1249 return NULL;
1252 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1254 return isl_basic_map_free(bset_to_bmap(bset));
1257 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1259 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1262 /* Check that "map" has only named parameters, reporting an error
1263 * if it does not.
1265 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1267 return isl_space_check_named_params(isl_map_peek_space(map));
1270 /* Check that "bmap1" and "bmap2" have the same parameters,
1271 * reporting an error if they do not.
1273 static isl_stat isl_basic_map_check_equal_params(
1274 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1276 isl_bool match;
1278 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1279 if (match < 0)
1280 return isl_stat_error;
1281 if (!match)
1282 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1283 "parameters don't match", return isl_stat_error);
1284 return isl_stat_ok;
1287 __isl_give isl_map *isl_map_align_params_map_map_and(
1288 __isl_take isl_map *map1, __isl_take isl_map *map2,
1289 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1290 __isl_take isl_map *map2))
1292 if (!map1 || !map2)
1293 goto error;
1294 if (isl_map_has_equal_params(map1, map2))
1295 return fn(map1, map2);
1296 if (isl_map_check_named_params(map1) < 0)
1297 goto error;
1298 if (isl_map_check_named_params(map2) < 0)
1299 goto error;
1300 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1301 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1302 return fn(map1, map2);
1303 error:
1304 isl_map_free(map1);
1305 isl_map_free(map2);
1306 return NULL;
1309 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1310 __isl_keep isl_map *map2,
1311 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1313 isl_bool r;
1315 if (!map1 || !map2)
1316 return isl_bool_error;
1317 if (isl_map_has_equal_params(map1, map2))
1318 return fn(map1, map2);
1319 if (isl_map_check_named_params(map1) < 0)
1320 return isl_bool_error;
1321 if (isl_map_check_named_params(map2) < 0)
1322 return isl_bool_error;
1323 map1 = isl_map_copy(map1);
1324 map2 = isl_map_copy(map2);
1325 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1326 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1327 r = fn(map1, map2);
1328 isl_map_free(map1);
1329 isl_map_free(map2);
1330 return r;
1333 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1335 struct isl_ctx *ctx;
1336 if (!bmap)
1337 return -1;
1338 ctx = bmap->ctx;
1339 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1340 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1341 return -1);
1342 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1343 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1344 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1345 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1346 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1347 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1348 isl_int *t;
1349 int j = isl_basic_map_alloc_inequality(bmap);
1350 if (j < 0)
1351 return -1;
1352 t = bmap->ineq[j];
1353 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1354 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1355 bmap->eq[-1] = t;
1356 bmap->n_eq++;
1357 bmap->n_ineq--;
1358 bmap->eq--;
1359 return 0;
1361 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1362 bmap->extra - bmap->n_div);
1363 return bmap->n_eq++;
1366 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1368 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1371 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1373 if (!bmap)
1374 return -1;
1375 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1376 bmap->n_eq -= n;
1377 return 0;
1380 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1382 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1385 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1387 isl_int *t;
1388 if (!bmap)
1389 return -1;
1390 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1392 if (pos != bmap->n_eq - 1) {
1393 t = bmap->eq[pos];
1394 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1395 bmap->eq[bmap->n_eq - 1] = t;
1397 bmap->n_eq--;
1398 return 0;
1401 /* Turn inequality "pos" of "bmap" into an equality.
1403 * In particular, we move the inequality in front of the equalities
1404 * and move the last inequality in the position of the moved inequality.
1405 * Note that isl_tab_make_equalities_explicit depends on this particular
1406 * change in the ordering of the constraints.
1408 void isl_basic_map_inequality_to_equality(
1409 struct isl_basic_map *bmap, unsigned pos)
1411 isl_int *t;
1413 t = bmap->ineq[pos];
1414 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1415 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1416 bmap->eq[-1] = t;
1417 bmap->n_eq++;
1418 bmap->n_ineq--;
1419 bmap->eq--;
1420 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1421 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1422 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1423 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1426 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1428 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1431 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1433 struct isl_ctx *ctx;
1434 if (!bmap)
1435 return -1;
1436 ctx = bmap->ctx;
1437 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1438 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1439 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1440 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1441 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1442 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1443 1 + isl_basic_map_total_dim(bmap),
1444 bmap->extra - bmap->n_div);
1445 return bmap->n_ineq++;
1448 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1450 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1453 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1455 if (!bmap)
1456 return -1;
1457 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1458 bmap->n_ineq -= n;
1459 return 0;
1462 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1464 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1467 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1469 isl_int *t;
1470 if (!bmap)
1471 return -1;
1472 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1474 if (pos != bmap->n_ineq - 1) {
1475 t = bmap->ineq[pos];
1476 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1477 bmap->ineq[bmap->n_ineq - 1] = t;
1478 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1480 bmap->n_ineq--;
1481 return 0;
1484 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1486 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1489 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1490 isl_int *eq)
1492 int k;
1494 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1495 if (!bmap)
1496 return NULL;
1497 k = isl_basic_map_alloc_equality(bmap);
1498 if (k < 0)
1499 goto error;
1500 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1501 return bmap;
1502 error:
1503 isl_basic_map_free(bmap);
1504 return NULL;
1507 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1508 isl_int *eq)
1510 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1513 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1514 isl_int *ineq)
1516 int k;
1518 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1519 if (!bmap)
1520 return NULL;
1521 k = isl_basic_map_alloc_inequality(bmap);
1522 if (k < 0)
1523 goto error;
1524 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1525 return bmap;
1526 error:
1527 isl_basic_map_free(bmap);
1528 return NULL;
1531 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1532 isl_int *ineq)
1534 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1537 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1539 if (!bmap)
1540 return -1;
1541 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1542 isl_seq_clr(bmap->div[bmap->n_div] +
1543 1 + 1 + isl_basic_map_total_dim(bmap),
1544 bmap->extra - bmap->n_div);
1545 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1546 return bmap->n_div++;
1549 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1551 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1554 /* Check that there are "n" dimensions of type "type" starting at "first"
1555 * in "bmap".
1557 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1558 enum isl_dim_type type, unsigned first, unsigned n)
1560 unsigned dim;
1562 if (!bmap)
1563 return isl_stat_error;
1564 dim = isl_basic_map_dim(bmap, type);
1565 if (first + n > dim || first + n < first)
1566 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1567 "position or range out of bounds",
1568 return isl_stat_error);
1569 return isl_stat_ok;
1572 /* Insert an extra integer division, prescribed by "div", to "bmap"
1573 * at (integer division) position "pos".
1575 * The integer division is first added at the end and then moved
1576 * into the right position.
1578 __isl_give isl_basic_map *isl_basic_map_insert_div(
1579 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1581 int i, k;
1583 bmap = isl_basic_map_cow(bmap);
1584 if (!bmap || !div)
1585 return isl_basic_map_free(bmap);
1587 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1588 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1589 "unexpected size", return isl_basic_map_free(bmap));
1590 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1591 return isl_basic_map_free(bmap);
1593 bmap = isl_basic_map_extend_space(bmap,
1594 isl_basic_map_get_space(bmap), 1, 0, 2);
1595 k = isl_basic_map_alloc_div(bmap);
1596 if (k < 0)
1597 return isl_basic_map_free(bmap);
1598 isl_seq_cpy(bmap->div[k], div->el, div->size);
1599 isl_int_set_si(bmap->div[k][div->size], 0);
1601 for (i = k; i > pos; --i)
1602 isl_basic_map_swap_div(bmap, i, i - 1);
1604 return bmap;
1607 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1609 if (!bmap)
1610 return isl_stat_error;
1611 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1612 bmap->n_div -= n;
1613 return isl_stat_ok;
1616 /* Copy constraint from src to dst, putting the vars of src at offset
1617 * dim_off in dst and the divs of src at offset div_off in dst.
1618 * If both sets are actually map, then dim_off applies to the input
1619 * variables.
1621 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1622 struct isl_basic_map *src_map, isl_int *src,
1623 unsigned in_off, unsigned out_off, unsigned div_off)
1625 unsigned src_nparam = isl_basic_map_n_param(src_map);
1626 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1627 unsigned src_in = isl_basic_map_n_in(src_map);
1628 unsigned dst_in = isl_basic_map_n_in(dst_map);
1629 unsigned src_out = isl_basic_map_n_out(src_map);
1630 unsigned dst_out = isl_basic_map_n_out(dst_map);
1631 isl_int_set(dst[0], src[0]);
1632 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1633 if (dst_nparam > src_nparam)
1634 isl_seq_clr(dst+1+src_nparam,
1635 dst_nparam - src_nparam);
1636 isl_seq_clr(dst+1+dst_nparam, in_off);
1637 isl_seq_cpy(dst+1+dst_nparam+in_off,
1638 src+1+src_nparam,
1639 isl_min(dst_in-in_off, src_in));
1640 if (dst_in-in_off > src_in)
1641 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1642 dst_in - in_off - src_in);
1643 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1644 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1645 src+1+src_nparam+src_in,
1646 isl_min(dst_out-out_off, src_out));
1647 if (dst_out-out_off > src_out)
1648 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1649 dst_out - out_off - src_out);
1650 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1651 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1652 src+1+src_nparam+src_in+src_out,
1653 isl_min(dst_map->extra-div_off, src_map->n_div));
1654 if (dst_map->n_div-div_off > src_map->n_div)
1655 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1656 div_off+src_map->n_div,
1657 dst_map->n_div - div_off - src_map->n_div);
1660 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1661 struct isl_basic_map *src_map, isl_int *src,
1662 unsigned in_off, unsigned out_off, unsigned div_off)
1664 isl_int_set(dst[0], src[0]);
1665 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1668 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1669 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1671 int i;
1672 unsigned div_off;
1674 if (!bmap1 || !bmap2)
1675 goto error;
1677 div_off = bmap1->n_div;
1679 for (i = 0; i < bmap2->n_eq; ++i) {
1680 int i1 = isl_basic_map_alloc_equality(bmap1);
1681 if (i1 < 0)
1682 goto error;
1683 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1684 i_pos, o_pos, div_off);
1687 for (i = 0; i < bmap2->n_ineq; ++i) {
1688 int i1 = isl_basic_map_alloc_inequality(bmap1);
1689 if (i1 < 0)
1690 goto error;
1691 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1692 i_pos, o_pos, div_off);
1695 for (i = 0; i < bmap2->n_div; ++i) {
1696 int i1 = isl_basic_map_alloc_div(bmap1);
1697 if (i1 < 0)
1698 goto error;
1699 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1700 i_pos, o_pos, div_off);
1703 isl_basic_map_free(bmap2);
1705 return bmap1;
1707 error:
1708 isl_basic_map_free(bmap1);
1709 isl_basic_map_free(bmap2);
1710 return NULL;
1713 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1714 struct isl_basic_set *bset2, unsigned pos)
1716 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1717 bset_to_bmap(bset2), 0, pos));
1720 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1721 __isl_take isl_space *dim, unsigned extra,
1722 unsigned n_eq, unsigned n_ineq)
1724 struct isl_basic_map *ext;
1725 unsigned flags;
1726 int dims_ok;
1728 if (!dim)
1729 goto error;
1731 if (!base)
1732 goto error;
1734 dims_ok = isl_space_is_equal(base->dim, dim) &&
1735 base->extra >= base->n_div + extra;
1737 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1738 room_for_ineq(base, n_ineq)) {
1739 isl_space_free(dim);
1740 return base;
1743 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1744 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1745 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1746 extra += base->extra;
1747 n_eq += base->n_eq;
1748 n_ineq += base->n_ineq;
1750 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1751 dim = NULL;
1752 if (!ext)
1753 goto error;
1755 if (dims_ok)
1756 ext->sample = isl_vec_copy(base->sample);
1757 flags = base->flags;
1758 ext = add_constraints(ext, base, 0, 0);
1759 if (ext) {
1760 ext->flags = flags;
1761 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1764 return ext;
1766 error:
1767 isl_space_free(dim);
1768 isl_basic_map_free(base);
1769 return NULL;
1772 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1773 __isl_take isl_space *dim, unsigned extra,
1774 unsigned n_eq, unsigned n_ineq)
1776 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1777 dim, extra, n_eq, n_ineq));
1780 struct isl_basic_map *isl_basic_map_extend_constraints(
1781 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1783 if (!base)
1784 return NULL;
1785 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1786 0, n_eq, n_ineq);
1789 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1790 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1791 unsigned n_eq, unsigned n_ineq)
1793 struct isl_basic_map *bmap;
1794 isl_space *dim;
1796 if (!base)
1797 return NULL;
1798 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1799 if (!dim)
1800 goto error;
1802 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1803 return bmap;
1804 error:
1805 isl_basic_map_free(base);
1806 return NULL;
1809 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1810 unsigned nparam, unsigned dim, unsigned extra,
1811 unsigned n_eq, unsigned n_ineq)
1813 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1814 nparam, 0, dim, extra, n_eq, n_ineq));
1817 struct isl_basic_set *isl_basic_set_extend_constraints(
1818 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1820 isl_basic_map *bmap = bset_to_bmap(base);
1821 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1822 return bset_from_bmap(bmap);
1825 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1827 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1830 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1832 if (!bmap)
1833 return NULL;
1835 if (bmap->ref > 1) {
1836 bmap->ref--;
1837 bmap = isl_basic_map_dup(bmap);
1839 if (bmap) {
1840 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1841 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1843 return bmap;
1846 /* Clear all cached information in "map", either because it is about
1847 * to be modified or because it is being freed.
1848 * Always return the same pointer that is passed in.
1849 * This is needed for the use in isl_map_free.
1851 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1853 isl_basic_map_free(map->cached_simple_hull[0]);
1854 isl_basic_map_free(map->cached_simple_hull[1]);
1855 map->cached_simple_hull[0] = NULL;
1856 map->cached_simple_hull[1] = NULL;
1857 return map;
1860 struct isl_set *isl_set_cow(struct isl_set *set)
1862 return isl_map_cow(set);
1865 /* Return an isl_map that is equal to "map" and that has only
1866 * a single reference.
1868 * If the original input already has only one reference, then
1869 * simply return it, but clear all cached information, since
1870 * it may be rendered invalid by the operations that will be
1871 * performed on the result.
1873 * Otherwise, create a duplicate (without any cached information).
1875 struct isl_map *isl_map_cow(struct isl_map *map)
1877 if (!map)
1878 return NULL;
1880 if (map->ref == 1)
1881 return clear_caches(map);
1882 map->ref--;
1883 return isl_map_dup(map);
1886 static void swap_vars(struct isl_blk blk, isl_int *a,
1887 unsigned a_len, unsigned b_len)
1889 isl_seq_cpy(blk.data, a+a_len, b_len);
1890 isl_seq_cpy(blk.data+b_len, a, a_len);
1891 isl_seq_cpy(a, blk.data, b_len+a_len);
1894 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1895 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1897 int i;
1898 struct isl_blk blk;
1900 if (!bmap)
1901 goto error;
1903 isl_assert(bmap->ctx,
1904 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1906 if (n1 == 0 || n2 == 0)
1907 return bmap;
1909 bmap = isl_basic_map_cow(bmap);
1910 if (!bmap)
1911 return NULL;
1913 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1914 if (isl_blk_is_error(blk))
1915 goto error;
1917 for (i = 0; i < bmap->n_eq; ++i)
1918 swap_vars(blk,
1919 bmap->eq[i] + pos, n1, n2);
1921 for (i = 0; i < bmap->n_ineq; ++i)
1922 swap_vars(blk,
1923 bmap->ineq[i] + pos, n1, n2);
1925 for (i = 0; i < bmap->n_div; ++i)
1926 swap_vars(blk,
1927 bmap->div[i]+1 + pos, n1, n2);
1929 isl_blk_free(bmap->ctx, blk);
1931 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1932 bmap = isl_basic_map_gauss(bmap, NULL);
1933 return isl_basic_map_finalize(bmap);
1934 error:
1935 isl_basic_map_free(bmap);
1936 return NULL;
1939 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1941 int i = 0;
1942 unsigned total;
1943 if (!bmap)
1944 goto error;
1945 total = isl_basic_map_total_dim(bmap);
1946 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
1947 return isl_basic_map_free(bmap);
1948 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1949 if (bmap->n_eq > 0)
1950 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1951 else {
1952 i = isl_basic_map_alloc_equality(bmap);
1953 if (i < 0)
1954 goto error;
1956 isl_int_set_si(bmap->eq[i][0], 1);
1957 isl_seq_clr(bmap->eq[i]+1, total);
1958 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1959 isl_vec_free(bmap->sample);
1960 bmap->sample = NULL;
1961 return isl_basic_map_finalize(bmap);
1962 error:
1963 isl_basic_map_free(bmap);
1964 return NULL;
1967 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1969 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
1972 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1973 * of "bmap").
1975 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1977 isl_int *t = bmap->div[a];
1978 bmap->div[a] = bmap->div[b];
1979 bmap->div[b] = t;
1982 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1983 * div definitions accordingly.
1985 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1987 int i;
1988 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1990 swap_div(bmap, a, b);
1992 for (i = 0; i < bmap->n_eq; ++i)
1993 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1995 for (i = 0; i < bmap->n_ineq; ++i)
1996 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1998 for (i = 0; i < bmap->n_div; ++i)
1999 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2000 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2003 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
2004 * div definitions accordingly.
2006 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
2008 isl_basic_map_swap_div(bset, a, b);
2011 /* Eliminate the specified n dimensions starting at first from the
2012 * constraints, without removing the dimensions from the space.
2013 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2015 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2016 enum isl_dim_type type, unsigned first, unsigned n)
2018 int i;
2020 if (!map)
2021 return NULL;
2022 if (n == 0)
2023 return map;
2025 if (first + n > isl_map_dim(map, type) || first + n < first)
2026 isl_die(map->ctx, isl_error_invalid,
2027 "index out of bounds", goto error);
2029 map = isl_map_cow(map);
2030 if (!map)
2031 return NULL;
2033 for (i = 0; i < map->n; ++i) {
2034 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2035 if (!map->p[i])
2036 goto error;
2038 return map;
2039 error:
2040 isl_map_free(map);
2041 return NULL;
2044 /* Eliminate the specified n dimensions starting at first from the
2045 * constraints, without removing the dimensions from the space.
2046 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2048 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2049 enum isl_dim_type type, unsigned first, unsigned n)
2051 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2054 /* Eliminate the specified n dimensions starting at first from the
2055 * constraints, without removing the dimensions from the space.
2056 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2058 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2059 unsigned first, unsigned n)
2061 return isl_set_eliminate(set, isl_dim_set, first, n);
2064 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2065 __isl_take isl_basic_map *bmap)
2067 if (!bmap)
2068 return NULL;
2069 bmap = isl_basic_map_eliminate_vars(bmap,
2070 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2071 if (!bmap)
2072 return NULL;
2073 bmap->n_div = 0;
2074 return isl_basic_map_finalize(bmap);
2077 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2078 __isl_take isl_basic_set *bset)
2080 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2083 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2085 int i;
2087 if (!map)
2088 return NULL;
2089 if (map->n == 0)
2090 return map;
2092 map = isl_map_cow(map);
2093 if (!map)
2094 return NULL;
2096 for (i = 0; i < map->n; ++i) {
2097 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2098 if (!map->p[i])
2099 goto error;
2101 return map;
2102 error:
2103 isl_map_free(map);
2104 return NULL;
2107 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2109 return isl_map_remove_divs(set);
2112 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2113 enum isl_dim_type type, unsigned first, unsigned n)
2115 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2116 return isl_basic_map_free(bmap);
2117 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2118 return bmap;
2119 bmap = isl_basic_map_eliminate_vars(bmap,
2120 isl_basic_map_offset(bmap, type) - 1 + first, n);
2121 if (!bmap)
2122 return bmap;
2123 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2124 return bmap;
2125 bmap = isl_basic_map_drop(bmap, type, first, n);
2126 return bmap;
2129 /* Return true if the definition of the given div (recursively) involves
2130 * any of the given variables.
2132 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2133 unsigned first, unsigned n)
2135 int i;
2136 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2138 if (isl_int_is_zero(bmap->div[div][0]))
2139 return isl_bool_false;
2140 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2141 return isl_bool_true;
2143 for (i = bmap->n_div - 1; i >= 0; --i) {
2144 isl_bool involves;
2146 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2147 continue;
2148 involves = div_involves_vars(bmap, i, first, n);
2149 if (involves < 0 || involves)
2150 return involves;
2153 return isl_bool_false;
2156 /* Try and add a lower and/or upper bound on "div" to "bmap"
2157 * based on inequality "i".
2158 * "total" is the total number of variables (excluding the divs).
2159 * "v" is a temporary object that can be used during the calculations.
2160 * If "lb" is set, then a lower bound should be constructed.
2161 * If "ub" is set, then an upper bound should be constructed.
2163 * The calling function has already checked that the inequality does not
2164 * reference "div", but we still need to check that the inequality is
2165 * of the right form. We'll consider the case where we want to construct
2166 * a lower bound. The construction of upper bounds is similar.
2168 * Let "div" be of the form
2170 * q = floor((a + f(x))/d)
2172 * We essentially check if constraint "i" is of the form
2174 * b + f(x) >= 0
2176 * so that we can use it to derive a lower bound on "div".
2177 * However, we allow a slightly more general form
2179 * b + g(x) >= 0
2181 * with the condition that the coefficients of g(x) - f(x) are all
2182 * divisible by d.
2183 * Rewriting this constraint as
2185 * 0 >= -b - g(x)
2187 * adding a + f(x) to both sides and dividing by d, we obtain
2189 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2191 * Taking the floor on both sides, we obtain
2193 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2195 * or
2197 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2199 * In the case of an upper bound, we construct the constraint
2201 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2204 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2205 __isl_take isl_basic_map *bmap, int div, int i,
2206 unsigned total, isl_int v, int lb, int ub)
2208 int j;
2210 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2211 if (lb) {
2212 isl_int_sub(v, bmap->ineq[i][1 + j],
2213 bmap->div[div][1 + 1 + j]);
2214 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2216 if (ub) {
2217 isl_int_add(v, bmap->ineq[i][1 + j],
2218 bmap->div[div][1 + 1 + j]);
2219 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2222 if (!lb && !ub)
2223 return bmap;
2225 bmap = isl_basic_map_cow(bmap);
2226 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2227 if (lb) {
2228 int k = isl_basic_map_alloc_inequality(bmap);
2229 if (k < 0)
2230 goto error;
2231 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2232 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2233 bmap->div[div][1 + j]);
2234 isl_int_cdiv_q(bmap->ineq[k][j],
2235 bmap->ineq[k][j], bmap->div[div][0]);
2237 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2239 if (ub) {
2240 int k = isl_basic_map_alloc_inequality(bmap);
2241 if (k < 0)
2242 goto error;
2243 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2244 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2245 bmap->div[div][1 + j]);
2246 isl_int_fdiv_q(bmap->ineq[k][j],
2247 bmap->ineq[k][j], bmap->div[div][0]);
2249 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2252 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2253 return bmap;
2254 error:
2255 isl_basic_map_free(bmap);
2256 return NULL;
2259 /* This function is called right before "div" is eliminated from "bmap"
2260 * using Fourier-Motzkin.
2261 * Look through the constraints of "bmap" for constraints on the argument
2262 * of the integer division and use them to construct constraints on the
2263 * integer division itself. These constraints can then be combined
2264 * during the Fourier-Motzkin elimination.
2265 * Note that it is only useful to introduce lower bounds on "div"
2266 * if "bmap" already contains upper bounds on "div" as the newly
2267 * introduce lower bounds can then be combined with the pre-existing
2268 * upper bounds. Similarly for upper bounds.
2269 * We therefore first check if "bmap" contains any lower and/or upper bounds
2270 * on "div".
2272 * It is interesting to note that the introduction of these constraints
2273 * can indeed lead to more accurate results, even when compared to
2274 * deriving constraints on the argument of "div" from constraints on "div".
2275 * Consider, for example, the set
2277 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2279 * The second constraint can be rewritten as
2281 * 2 * [(-i-2j+3)/4] + k >= 0
2283 * from which we can derive
2285 * -i - 2j + 3 >= -2k
2287 * or
2289 * i + 2j <= 3 + 2k
2291 * Combined with the first constraint, we obtain
2293 * -3 <= 3 + 2k or k >= -3
2295 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2296 * the first constraint, we obtain
2298 * [(i + 2j)/4] >= [-3/4] = -1
2300 * Combining this constraint with the second constraint, we obtain
2302 * k >= -2
2304 static __isl_give isl_basic_map *insert_bounds_on_div(
2305 __isl_take isl_basic_map *bmap, int div)
2307 int i;
2308 int check_lb, check_ub;
2309 isl_int v;
2310 unsigned total;
2312 if (!bmap)
2313 return NULL;
2315 if (isl_int_is_zero(bmap->div[div][0]))
2316 return bmap;
2318 total = isl_space_dim(bmap->dim, isl_dim_all);
2320 check_lb = 0;
2321 check_ub = 0;
2322 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2323 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2324 if (s > 0)
2325 check_ub = 1;
2326 if (s < 0)
2327 check_lb = 1;
2330 if (!check_lb && !check_ub)
2331 return bmap;
2333 isl_int_init(v);
2335 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2336 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2337 continue;
2339 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2340 check_lb, check_ub);
2343 isl_int_clear(v);
2345 return bmap;
2348 /* Remove all divs (recursively) involving any of the given dimensions
2349 * in their definitions.
2351 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2352 __isl_take isl_basic_map *bmap,
2353 enum isl_dim_type type, unsigned first, unsigned n)
2355 int i;
2357 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2358 return isl_basic_map_free(bmap);
2359 first += isl_basic_map_offset(bmap, type);
2361 for (i = bmap->n_div - 1; i >= 0; --i) {
2362 isl_bool involves;
2364 involves = div_involves_vars(bmap, i, first, n);
2365 if (involves < 0)
2366 return isl_basic_map_free(bmap);
2367 if (!involves)
2368 continue;
2369 bmap = insert_bounds_on_div(bmap, i);
2370 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2371 if (!bmap)
2372 return NULL;
2373 i = bmap->n_div;
2376 return bmap;
2379 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2380 __isl_take isl_basic_set *bset,
2381 enum isl_dim_type type, unsigned first, unsigned n)
2383 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2386 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2387 enum isl_dim_type type, unsigned first, unsigned n)
2389 int i;
2391 if (!map)
2392 return NULL;
2393 if (map->n == 0)
2394 return map;
2396 map = isl_map_cow(map);
2397 if (!map)
2398 return NULL;
2400 for (i = 0; i < map->n; ++i) {
2401 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2402 type, first, n);
2403 if (!map->p[i])
2404 goto error;
2406 return map;
2407 error:
2408 isl_map_free(map);
2409 return NULL;
2412 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2413 enum isl_dim_type type, unsigned first, unsigned n)
2415 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2416 type, first, n));
2419 /* Does the description of "bmap" depend on the specified dimensions?
2420 * We also check whether the dimensions appear in any of the div definitions.
2421 * In principle there is no need for this check. If the dimensions appear
2422 * in a div definition, they also appear in the defining constraints of that
2423 * div.
2425 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2426 enum isl_dim_type type, unsigned first, unsigned n)
2428 int i;
2430 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2431 return isl_bool_error;
2433 first += isl_basic_map_offset(bmap, type);
2434 for (i = 0; i < bmap->n_eq; ++i)
2435 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2436 return isl_bool_true;
2437 for (i = 0; i < bmap->n_ineq; ++i)
2438 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2439 return isl_bool_true;
2440 for (i = 0; i < bmap->n_div; ++i) {
2441 if (isl_int_is_zero(bmap->div[i][0]))
2442 continue;
2443 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2444 return isl_bool_true;
2447 return isl_bool_false;
2450 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2451 enum isl_dim_type type, unsigned first, unsigned n)
2453 int i;
2455 if (!map)
2456 return isl_bool_error;
2458 if (first + n > isl_map_dim(map, type))
2459 isl_die(map->ctx, isl_error_invalid,
2460 "index out of bounds", return isl_bool_error);
2462 for (i = 0; i < map->n; ++i) {
2463 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2464 type, first, n);
2465 if (involves < 0 || involves)
2466 return involves;
2469 return isl_bool_false;
2472 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2473 enum isl_dim_type type, unsigned first, unsigned n)
2475 return isl_basic_map_involves_dims(bset, type, first, n);
2478 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2479 enum isl_dim_type type, unsigned first, unsigned n)
2481 return isl_map_involves_dims(set, type, first, n);
2484 /* Drop all constraints in bmap that involve any of the dimensions
2485 * first to first+n-1.
2487 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2488 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2490 int i;
2492 if (n == 0)
2493 return bmap;
2495 bmap = isl_basic_map_cow(bmap);
2497 if (!bmap)
2498 return NULL;
2500 for (i = bmap->n_eq - 1; i >= 0; --i) {
2501 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2502 continue;
2503 isl_basic_map_drop_equality(bmap, i);
2506 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2507 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2508 continue;
2509 isl_basic_map_drop_inequality(bmap, i);
2512 bmap = isl_basic_map_add_known_div_constraints(bmap);
2513 return bmap;
2516 /* Drop all constraints in bset that involve any of the dimensions
2517 * first to first+n-1.
2519 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2520 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2522 return isl_basic_map_drop_constraints_involving(bset, first, n);
2525 /* Drop all constraints in bmap that do not involve any of the dimensions
2526 * first to first + n - 1 of the given type.
2528 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2529 __isl_take isl_basic_map *bmap,
2530 enum isl_dim_type type, unsigned first, unsigned n)
2532 int i;
2534 if (n == 0) {
2535 isl_space *space = isl_basic_map_get_space(bmap);
2536 isl_basic_map_free(bmap);
2537 return isl_basic_map_universe(space);
2539 bmap = isl_basic_map_cow(bmap);
2540 if (!bmap)
2541 return NULL;
2543 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2544 return isl_basic_map_free(bmap);
2546 first += isl_basic_map_offset(bmap, type) - 1;
2548 for (i = bmap->n_eq - 1; i >= 0; --i) {
2549 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2550 continue;
2551 isl_basic_map_drop_equality(bmap, i);
2554 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2555 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2556 continue;
2557 isl_basic_map_drop_inequality(bmap, i);
2560 bmap = isl_basic_map_add_known_div_constraints(bmap);
2561 return bmap;
2564 /* Drop all constraints in bset that do not involve any of the dimensions
2565 * first to first + n - 1 of the given type.
2567 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2568 __isl_take isl_basic_set *bset,
2569 enum isl_dim_type type, unsigned first, unsigned n)
2571 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2572 type, first, n);
2575 /* Drop all constraints in bmap that involve any of the dimensions
2576 * first to first + n - 1 of the given type.
2578 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2579 __isl_take isl_basic_map *bmap,
2580 enum isl_dim_type type, unsigned first, unsigned n)
2582 if (!bmap)
2583 return NULL;
2584 if (n == 0)
2585 return bmap;
2587 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2588 return isl_basic_map_free(bmap);
2590 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2591 first += isl_basic_map_offset(bmap, type) - 1;
2592 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2595 /* Drop all constraints in bset that involve any of the dimensions
2596 * first to first + n - 1 of the given type.
2598 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2599 __isl_take isl_basic_set *bset,
2600 enum isl_dim_type type, unsigned first, unsigned n)
2602 return isl_basic_map_drop_constraints_involving_dims(bset,
2603 type, first, n);
2606 /* Drop constraints from "map" by applying "drop" to each basic map.
2608 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2609 enum isl_dim_type type, unsigned first, unsigned n,
2610 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2611 enum isl_dim_type type, unsigned first, unsigned n))
2613 int i;
2614 unsigned dim;
2616 if (!map)
2617 return NULL;
2619 dim = isl_map_dim(map, type);
2620 if (first + n > dim || first + n < first)
2621 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2622 "index out of bounds", return isl_map_free(map));
2624 map = isl_map_cow(map);
2625 if (!map)
2626 return NULL;
2628 for (i = 0; i < map->n; ++i) {
2629 map->p[i] = drop(map->p[i], type, first, n);
2630 if (!map->p[i])
2631 return isl_map_free(map);
2634 if (map->n > 1)
2635 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2637 return map;
2640 /* Drop all constraints in map that involve any of the dimensions
2641 * first to first + n - 1 of the given type.
2643 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2644 __isl_take isl_map *map,
2645 enum isl_dim_type type, unsigned first, unsigned n)
2647 if (n == 0)
2648 return map;
2649 return drop_constraints(map, type, first, n,
2650 &isl_basic_map_drop_constraints_involving_dims);
2653 /* Drop all constraints in "map" that do not involve any of the dimensions
2654 * first to first + n - 1 of the given type.
2656 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2657 __isl_take isl_map *map,
2658 enum isl_dim_type type, unsigned first, unsigned n)
2660 if (n == 0) {
2661 isl_space *space = isl_map_get_space(map);
2662 isl_map_free(map);
2663 return isl_map_universe(space);
2665 return drop_constraints(map, type, first, n,
2666 &isl_basic_map_drop_constraints_not_involving_dims);
2669 /* Drop all constraints in set that involve any of the dimensions
2670 * first to first + n - 1 of the given type.
2672 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
2673 __isl_take isl_set *set,
2674 enum isl_dim_type type, unsigned first, unsigned n)
2676 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2679 /* Drop all constraints in "set" that do not involve any of the dimensions
2680 * first to first + n - 1 of the given type.
2682 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
2683 __isl_take isl_set *set,
2684 enum isl_dim_type type, unsigned first, unsigned n)
2686 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
2689 /* Does local variable "div" of "bmap" have a complete explicit representation?
2690 * Having a complete explicit representation requires not only
2691 * an explicit representation, but also that all local variables
2692 * that appear in this explicit representation in turn have
2693 * a complete explicit representation.
2695 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2697 int i;
2698 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2699 isl_bool marked;
2701 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2702 if (marked < 0 || marked)
2703 return isl_bool_not(marked);
2705 for (i = bmap->n_div - 1; i >= 0; --i) {
2706 isl_bool known;
2708 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2709 continue;
2710 known = isl_basic_map_div_is_known(bmap, i);
2711 if (known < 0 || !known)
2712 return known;
2715 return isl_bool_true;
2718 /* Remove all divs that are unknown or defined in terms of unknown divs.
2720 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2721 __isl_take isl_basic_map *bmap)
2723 int i;
2725 if (!bmap)
2726 return NULL;
2728 for (i = bmap->n_div - 1; i >= 0; --i) {
2729 if (isl_basic_map_div_is_known(bmap, i))
2730 continue;
2731 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2732 if (!bmap)
2733 return NULL;
2734 i = bmap->n_div;
2737 return bmap;
2740 /* Remove all divs that are unknown or defined in terms of unknown divs.
2742 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2743 __isl_take isl_basic_set *bset)
2745 return isl_basic_map_remove_unknown_divs(bset);
2748 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2750 int i;
2752 if (!map)
2753 return NULL;
2754 if (map->n == 0)
2755 return map;
2757 map = isl_map_cow(map);
2758 if (!map)
2759 return NULL;
2761 for (i = 0; i < map->n; ++i) {
2762 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2763 if (!map->p[i])
2764 goto error;
2766 return map;
2767 error:
2768 isl_map_free(map);
2769 return NULL;
2772 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2774 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
2777 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2778 __isl_take isl_basic_set *bset,
2779 enum isl_dim_type type, unsigned first, unsigned n)
2781 isl_basic_map *bmap = bset_to_bmap(bset);
2782 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2783 return bset_from_bmap(bmap);
2786 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2787 enum isl_dim_type type, unsigned first, unsigned n)
2789 int i;
2791 if (n == 0)
2792 return map;
2794 map = isl_map_cow(map);
2795 if (!map)
2796 return NULL;
2797 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2799 for (i = 0; i < map->n; ++i) {
2800 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2801 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2802 if (!map->p[i])
2803 goto error;
2805 map = isl_map_drop(map, type, first, n);
2806 return map;
2807 error:
2808 isl_map_free(map);
2809 return NULL;
2812 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2813 enum isl_dim_type type, unsigned first, unsigned n)
2815 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2816 type, first, n));
2819 /* Project out n inputs starting at first using Fourier-Motzkin */
2820 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2821 unsigned first, unsigned n)
2823 return isl_map_remove_dims(map, isl_dim_in, first, n);
2826 static void dump_term(struct isl_basic_map *bmap,
2827 isl_int c, int pos, FILE *out)
2829 const char *name;
2830 unsigned in = isl_basic_map_n_in(bmap);
2831 unsigned dim = in + isl_basic_map_n_out(bmap);
2832 unsigned nparam = isl_basic_map_n_param(bmap);
2833 if (!pos)
2834 isl_int_print(out, c, 0);
2835 else {
2836 if (!isl_int_is_one(c))
2837 isl_int_print(out, c, 0);
2838 if (pos < 1 + nparam) {
2839 name = isl_space_get_dim_name(bmap->dim,
2840 isl_dim_param, pos - 1);
2841 if (name)
2842 fprintf(out, "%s", name);
2843 else
2844 fprintf(out, "p%d", pos - 1);
2845 } else if (pos < 1 + nparam + in)
2846 fprintf(out, "i%d", pos - 1 - nparam);
2847 else if (pos < 1 + nparam + dim)
2848 fprintf(out, "o%d", pos - 1 - nparam - in);
2849 else
2850 fprintf(out, "e%d", pos - 1 - nparam - dim);
2854 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2855 int sign, FILE *out)
2857 int i;
2858 int first;
2859 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2860 isl_int v;
2862 isl_int_init(v);
2863 for (i = 0, first = 1; i < len; ++i) {
2864 if (isl_int_sgn(c[i]) * sign <= 0)
2865 continue;
2866 if (!first)
2867 fprintf(out, " + ");
2868 first = 0;
2869 isl_int_abs(v, c[i]);
2870 dump_term(bmap, v, i, out);
2872 isl_int_clear(v);
2873 if (first)
2874 fprintf(out, "0");
2877 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2878 const char *op, FILE *out, int indent)
2880 int i;
2882 fprintf(out, "%*s", indent, "");
2884 dump_constraint_sign(bmap, c, 1, out);
2885 fprintf(out, " %s ", op);
2886 dump_constraint_sign(bmap, c, -1, out);
2888 fprintf(out, "\n");
2890 for (i = bmap->n_div; i < bmap->extra; ++i) {
2891 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2892 continue;
2893 fprintf(out, "%*s", indent, "");
2894 fprintf(out, "ERROR: unused div coefficient not zero\n");
2895 abort();
2899 static void dump_constraints(struct isl_basic_map *bmap,
2900 isl_int **c, unsigned n,
2901 const char *op, FILE *out, int indent)
2903 int i;
2905 for (i = 0; i < n; ++i)
2906 dump_constraint(bmap, c[i], op, out, indent);
2909 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2911 int j;
2912 int first = 1;
2913 unsigned total = isl_basic_map_total_dim(bmap);
2915 for (j = 0; j < 1 + total; ++j) {
2916 if (isl_int_is_zero(exp[j]))
2917 continue;
2918 if (!first && isl_int_is_pos(exp[j]))
2919 fprintf(out, "+");
2920 dump_term(bmap, exp[j], j, out);
2921 first = 0;
2925 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2927 int i;
2929 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2930 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2932 for (i = 0; i < bmap->n_div; ++i) {
2933 fprintf(out, "%*s", indent, "");
2934 fprintf(out, "e%d = [(", i);
2935 dump_affine(bmap, bmap->div[i]+1, out);
2936 fprintf(out, ")/");
2937 isl_int_print(out, bmap->div[i][0], 0);
2938 fprintf(out, "]\n");
2942 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2943 FILE *out, int indent)
2945 if (!bset) {
2946 fprintf(out, "null basic set\n");
2947 return;
2950 fprintf(out, "%*s", indent, "");
2951 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2952 bset->ref, bset->dim->nparam, bset->dim->n_out,
2953 bset->extra, bset->flags);
2954 dump(bset_to_bmap(bset), out, indent);
2957 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2958 FILE *out, int indent)
2960 if (!bmap) {
2961 fprintf(out, "null basic map\n");
2962 return;
2965 fprintf(out, "%*s", indent, "");
2966 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2967 "flags: %x, n_name: %d\n",
2968 bmap->ref,
2969 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2970 bmap->extra, bmap->flags, bmap->dim->n_id);
2971 dump(bmap, out, indent);
2974 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2976 unsigned total;
2977 if (!bmap)
2978 return -1;
2979 total = isl_basic_map_total_dim(bmap);
2980 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2981 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2982 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2983 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2984 return 0;
2987 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2988 unsigned flags)
2990 if (!space)
2991 return NULL;
2992 if (isl_space_dim(space, isl_dim_in) != 0)
2993 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2994 "set cannot have input dimensions", goto error);
2995 return isl_map_alloc_space(space, n, flags);
2996 error:
2997 isl_space_free(space);
2998 return NULL;
3001 /* Make sure "map" has room for at least "n" more basic maps.
3003 struct isl_map *isl_map_grow(struct isl_map *map, int n)
3005 int i;
3006 struct isl_map *grown = NULL;
3008 if (!map)
3009 return NULL;
3010 isl_assert(map->ctx, n >= 0, goto error);
3011 if (map->n + n <= map->size)
3012 return map;
3013 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3014 if (!grown)
3015 goto error;
3016 for (i = 0; i < map->n; ++i) {
3017 grown->p[i] = isl_basic_map_copy(map->p[i]);
3018 if (!grown->p[i])
3019 goto error;
3020 grown->n++;
3022 isl_map_free(map);
3023 return grown;
3024 error:
3025 isl_map_free(grown);
3026 isl_map_free(map);
3027 return NULL;
3030 /* Make sure "set" has room for at least "n" more basic sets.
3032 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3034 return set_from_map(isl_map_grow(set_to_map(set), n));
3037 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
3039 return isl_map_from_basic_map(bset);
3042 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
3044 struct isl_map *map;
3046 if (!bmap)
3047 return NULL;
3049 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3050 return isl_map_add_basic_map(map, bmap);
3053 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3054 __isl_take isl_basic_set *bset)
3056 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3057 bset_to_bmap(bset)));
3060 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3062 return isl_map_free(set);
3065 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3067 int i;
3069 if (!set) {
3070 fprintf(out, "null set\n");
3071 return;
3074 fprintf(out, "%*s", indent, "");
3075 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3076 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3077 set->flags);
3078 for (i = 0; i < set->n; ++i) {
3079 fprintf(out, "%*s", indent, "");
3080 fprintf(out, "basic set %d:\n", i);
3081 isl_basic_set_print_internal(set->p[i], out, indent+4);
3085 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3087 int i;
3089 if (!map) {
3090 fprintf(out, "null map\n");
3091 return;
3094 fprintf(out, "%*s", indent, "");
3095 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3096 "flags: %x, n_name: %d\n",
3097 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3098 map->dim->n_out, map->flags, map->dim->n_id);
3099 for (i = 0; i < map->n; ++i) {
3100 fprintf(out, "%*s", indent, "");
3101 fprintf(out, "basic map %d:\n", i);
3102 isl_basic_map_print_internal(map->p[i], out, indent+4);
3106 struct isl_basic_map *isl_basic_map_intersect_domain(
3107 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3109 struct isl_basic_map *bmap_domain;
3111 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3112 goto error;
3114 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3115 isl_assert(bset->ctx,
3116 isl_basic_map_compatible_domain(bmap, bset), goto error);
3118 bmap = isl_basic_map_cow(bmap);
3119 if (!bmap)
3120 goto error;
3121 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3122 bset->n_div, bset->n_eq, bset->n_ineq);
3123 bmap_domain = isl_basic_map_from_domain(bset);
3124 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3126 bmap = isl_basic_map_simplify(bmap);
3127 return isl_basic_map_finalize(bmap);
3128 error:
3129 isl_basic_map_free(bmap);
3130 isl_basic_set_free(bset);
3131 return NULL;
3134 /* Check that the space of "bset" is the same as that of the range of "bmap".
3136 static isl_stat isl_basic_map_check_compatible_range(
3137 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3139 isl_bool ok;
3141 ok = isl_basic_map_compatible_range(bmap, bset);
3142 if (ok < 0)
3143 return isl_stat_error;
3144 if (!ok)
3145 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3146 "incompatible spaces", return isl_stat_error);
3148 return isl_stat_ok;
3151 struct isl_basic_map *isl_basic_map_intersect_range(
3152 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3154 struct isl_basic_map *bmap_range;
3156 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3157 goto error;
3159 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3160 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3161 goto error;
3163 if (isl_basic_set_plain_is_universe(bset)) {
3164 isl_basic_set_free(bset);
3165 return bmap;
3168 bmap = isl_basic_map_cow(bmap);
3169 if (!bmap)
3170 goto error;
3171 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3172 bset->n_div, bset->n_eq, bset->n_ineq);
3173 bmap_range = bset_to_bmap(bset);
3174 bmap = add_constraints(bmap, bmap_range, 0, 0);
3176 bmap = isl_basic_map_simplify(bmap);
3177 return isl_basic_map_finalize(bmap);
3178 error:
3179 isl_basic_map_free(bmap);
3180 isl_basic_set_free(bset);
3181 return NULL;
3184 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3185 __isl_keep isl_vec *vec)
3187 int i;
3188 unsigned total;
3189 isl_int s;
3191 if (!bmap || !vec)
3192 return isl_bool_error;
3194 total = 1 + isl_basic_map_total_dim(bmap);
3195 if (total != vec->size)
3196 return isl_bool_false;
3198 isl_int_init(s);
3200 for (i = 0; i < bmap->n_eq; ++i) {
3201 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3202 if (!isl_int_is_zero(s)) {
3203 isl_int_clear(s);
3204 return isl_bool_false;
3208 for (i = 0; i < bmap->n_ineq; ++i) {
3209 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3210 if (isl_int_is_neg(s)) {
3211 isl_int_clear(s);
3212 return isl_bool_false;
3216 isl_int_clear(s);
3218 return isl_bool_true;
3221 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3222 __isl_keep isl_vec *vec)
3224 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3227 struct isl_basic_map *isl_basic_map_intersect(
3228 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3230 struct isl_vec *sample = NULL;
3232 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3233 goto error;
3234 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3235 isl_space_dim(bmap1->dim, isl_dim_param) &&
3236 isl_space_dim(bmap2->dim, isl_dim_all) !=
3237 isl_space_dim(bmap2->dim, isl_dim_param))
3238 return isl_basic_map_intersect(bmap2, bmap1);
3240 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3241 isl_space_dim(bmap2->dim, isl_dim_param))
3242 isl_assert(bmap1->ctx,
3243 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3245 if (isl_basic_map_plain_is_empty(bmap1)) {
3246 isl_basic_map_free(bmap2);
3247 return bmap1;
3249 if (isl_basic_map_plain_is_empty(bmap2)) {
3250 isl_basic_map_free(bmap1);
3251 return bmap2;
3254 if (bmap1->sample &&
3255 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3256 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3257 sample = isl_vec_copy(bmap1->sample);
3258 else if (bmap2->sample &&
3259 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3260 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3261 sample = isl_vec_copy(bmap2->sample);
3263 bmap1 = isl_basic_map_cow(bmap1);
3264 if (!bmap1)
3265 goto error;
3266 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3267 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3268 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3270 if (!bmap1)
3271 isl_vec_free(sample);
3272 else if (sample) {
3273 isl_vec_free(bmap1->sample);
3274 bmap1->sample = sample;
3277 bmap1 = isl_basic_map_simplify(bmap1);
3278 return isl_basic_map_finalize(bmap1);
3279 error:
3280 if (sample)
3281 isl_vec_free(sample);
3282 isl_basic_map_free(bmap1);
3283 isl_basic_map_free(bmap2);
3284 return NULL;
3287 struct isl_basic_set *isl_basic_set_intersect(
3288 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3290 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3291 bset_to_bmap(bset2)));
3294 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3295 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3297 return isl_basic_set_intersect(bset1, bset2);
3300 /* Special case of isl_map_intersect, where both map1 and map2
3301 * are convex, without any divs and such that either map1 or map2
3302 * contains a single constraint. This constraint is then simply
3303 * added to the other map.
3305 static __isl_give isl_map *map_intersect_add_constraint(
3306 __isl_take isl_map *map1, __isl_take isl_map *map2)
3308 isl_assert(map1->ctx, map1->n == 1, goto error);
3309 isl_assert(map2->ctx, map1->n == 1, goto error);
3310 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3311 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3313 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3314 return isl_map_intersect(map2, map1);
3316 map1 = isl_map_cow(map1);
3317 if (!map1)
3318 goto error;
3319 if (isl_map_plain_is_empty(map1)) {
3320 isl_map_free(map2);
3321 return map1;
3323 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3324 if (map2->p[0]->n_eq == 1)
3325 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3326 else
3327 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3328 map2->p[0]->ineq[0]);
3330 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3331 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3332 if (!map1->p[0])
3333 goto error;
3335 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3336 isl_basic_map_free(map1->p[0]);
3337 map1->n = 0;
3340 isl_map_free(map2);
3342 return map1;
3343 error:
3344 isl_map_free(map1);
3345 isl_map_free(map2);
3346 return NULL;
3349 /* map2 may be either a parameter domain or a map living in the same
3350 * space as map1.
3352 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3353 __isl_take isl_map *map2)
3355 unsigned flags = 0;
3356 isl_map *result;
3357 int i, j;
3359 if (!map1 || !map2)
3360 goto error;
3362 if ((isl_map_plain_is_empty(map1) ||
3363 isl_map_plain_is_universe(map2)) &&
3364 isl_space_is_equal(map1->dim, map2->dim)) {
3365 isl_map_free(map2);
3366 return map1;
3368 if ((isl_map_plain_is_empty(map2) ||
3369 isl_map_plain_is_universe(map1)) &&
3370 isl_space_is_equal(map1->dim, map2->dim)) {
3371 isl_map_free(map1);
3372 return map2;
3375 if (map1->n == 1 && map2->n == 1 &&
3376 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3377 isl_space_is_equal(map1->dim, map2->dim) &&
3378 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3379 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3380 return map_intersect_add_constraint(map1, map2);
3382 if (isl_space_dim(map2->dim, isl_dim_all) !=
3383 isl_space_dim(map2->dim, isl_dim_param))
3384 isl_assert(map1->ctx,
3385 isl_space_is_equal(map1->dim, map2->dim), goto error);
3387 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3388 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3389 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3391 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3392 map1->n * map2->n, flags);
3393 if (!result)
3394 goto error;
3395 for (i = 0; i < map1->n; ++i)
3396 for (j = 0; j < map2->n; ++j) {
3397 struct isl_basic_map *part;
3398 part = isl_basic_map_intersect(
3399 isl_basic_map_copy(map1->p[i]),
3400 isl_basic_map_copy(map2->p[j]));
3401 if (isl_basic_map_is_empty(part) < 0)
3402 part = isl_basic_map_free(part);
3403 result = isl_map_add_basic_map(result, part);
3404 if (!result)
3405 goto error;
3407 isl_map_free(map1);
3408 isl_map_free(map2);
3409 return result;
3410 error:
3411 isl_map_free(map1);
3412 isl_map_free(map2);
3413 return NULL;
3416 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3417 __isl_take isl_map *map2)
3419 if (!map1 || !map2)
3420 goto error;
3421 if (!isl_space_is_equal(map1->dim, map2->dim))
3422 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3423 "spaces don't match", goto error);
3424 return map_intersect_internal(map1, map2);
3425 error:
3426 isl_map_free(map1);
3427 isl_map_free(map2);
3428 return NULL;
3431 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3432 __isl_take isl_map *map2)
3434 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3437 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3439 return set_from_map(isl_map_intersect(set_to_map(set1),
3440 set_to_map(set2)));
3443 /* map_intersect_internal accepts intersections
3444 * with parameter domains, so we can just call that function.
3446 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3447 __isl_take isl_set *params)
3449 return map_intersect_internal(map, params);
3452 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3453 __isl_take isl_map *map2)
3455 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3458 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3459 __isl_take isl_set *params)
3461 return isl_map_intersect_params(set, params);
3464 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3466 isl_space *space;
3467 unsigned pos, n1, n2;
3469 if (!bmap)
3470 return NULL;
3471 bmap = isl_basic_map_cow(bmap);
3472 if (!bmap)
3473 return NULL;
3474 space = isl_space_reverse(isl_space_copy(bmap->dim));
3475 pos = isl_basic_map_offset(bmap, isl_dim_in);
3476 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3477 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3478 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3479 return isl_basic_map_reset_space(bmap, space);
3482 static __isl_give isl_basic_map *basic_map_space_reset(
3483 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3485 isl_space *space;
3487 if (!bmap)
3488 return NULL;
3489 if (!isl_space_is_named_or_nested(bmap->dim, type))
3490 return bmap;
3492 space = isl_basic_map_get_space(bmap);
3493 space = isl_space_reset(space, type);
3494 bmap = isl_basic_map_reset_space(bmap, space);
3495 return bmap;
3498 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3499 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3500 unsigned pos, unsigned n)
3502 isl_bool rational;
3503 isl_space *res_dim;
3504 struct isl_basic_map *res;
3505 struct isl_dim_map *dim_map;
3506 unsigned total, off;
3507 enum isl_dim_type t;
3509 if (n == 0)
3510 return basic_map_space_reset(bmap, type);
3512 if (!bmap)
3513 return NULL;
3515 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3517 total = isl_basic_map_total_dim(bmap) + n;
3518 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3519 off = 0;
3520 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3521 if (t != type) {
3522 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3523 } else {
3524 unsigned size = isl_basic_map_dim(bmap, t);
3525 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3526 0, pos, off);
3527 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3528 pos, size - pos, off + pos + n);
3530 off += isl_space_dim(res_dim, t);
3532 isl_dim_map_div(dim_map, bmap, off);
3534 res = isl_basic_map_alloc_space(res_dim,
3535 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3536 rational = isl_basic_map_is_rational(bmap);
3537 if (rational < 0)
3538 res = isl_basic_map_free(res);
3539 if (rational)
3540 res = isl_basic_map_set_rational(res);
3541 if (isl_basic_map_plain_is_empty(bmap)) {
3542 isl_basic_map_free(bmap);
3543 free(dim_map);
3544 return isl_basic_map_set_to_empty(res);
3546 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3547 return isl_basic_map_finalize(res);
3550 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3551 __isl_take isl_basic_set *bset,
3552 enum isl_dim_type type, unsigned pos, unsigned n)
3554 return isl_basic_map_insert_dims(bset, type, pos, n);
3557 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3558 enum isl_dim_type type, unsigned n)
3560 if (!bmap)
3561 return NULL;
3562 return isl_basic_map_insert_dims(bmap, type,
3563 isl_basic_map_dim(bmap, type), n);
3566 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3567 enum isl_dim_type type, unsigned n)
3569 if (!bset)
3570 return NULL;
3571 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3572 return isl_basic_map_add_dims(bset, type, n);
3573 error:
3574 isl_basic_set_free(bset);
3575 return NULL;
3578 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3579 enum isl_dim_type type)
3581 isl_space *space;
3583 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3584 return map;
3586 space = isl_map_get_space(map);
3587 space = isl_space_reset(space, type);
3588 map = isl_map_reset_space(map, space);
3589 return map;
3592 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3593 enum isl_dim_type type, unsigned pos, unsigned n)
3595 int i;
3597 if (n == 0)
3598 return map_space_reset(map, type);
3600 map = isl_map_cow(map);
3601 if (!map)
3602 return NULL;
3604 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3605 if (!map->dim)
3606 goto error;
3608 for (i = 0; i < map->n; ++i) {
3609 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3610 if (!map->p[i])
3611 goto error;
3614 return map;
3615 error:
3616 isl_map_free(map);
3617 return NULL;
3620 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3621 enum isl_dim_type type, unsigned pos, unsigned n)
3623 return isl_map_insert_dims(set, type, pos, n);
3626 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3627 enum isl_dim_type type, unsigned n)
3629 if (!map)
3630 return NULL;
3631 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3634 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3635 enum isl_dim_type type, unsigned n)
3637 if (!set)
3638 return NULL;
3639 isl_assert(set->ctx, type != isl_dim_in, goto error);
3640 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3641 error:
3642 isl_set_free(set);
3643 return NULL;
3646 __isl_give isl_basic_map *isl_basic_map_move_dims(
3647 __isl_take isl_basic_map *bmap,
3648 enum isl_dim_type dst_type, unsigned dst_pos,
3649 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3651 struct isl_dim_map *dim_map;
3652 struct isl_basic_map *res;
3653 enum isl_dim_type t;
3654 unsigned total, off;
3656 if (!bmap)
3657 return NULL;
3658 if (n == 0)
3659 return bmap;
3661 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3662 return isl_basic_map_free(bmap);
3664 if (dst_type == src_type && dst_pos == src_pos)
3665 return bmap;
3667 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3669 if (pos(bmap->dim, dst_type) + dst_pos ==
3670 pos(bmap->dim, src_type) + src_pos +
3671 ((src_type < dst_type) ? n : 0)) {
3672 bmap = isl_basic_map_cow(bmap);
3673 if (!bmap)
3674 return NULL;
3676 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3677 src_type, src_pos, n);
3678 if (!bmap->dim)
3679 goto error;
3681 bmap = isl_basic_map_finalize(bmap);
3683 return bmap;
3686 total = isl_basic_map_total_dim(bmap);
3687 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3689 off = 0;
3690 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3691 unsigned size = isl_space_dim(bmap->dim, t);
3692 if (t == dst_type) {
3693 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3694 0, dst_pos, off);
3695 off += dst_pos;
3696 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3697 src_pos, n, off);
3698 off += n;
3699 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3700 dst_pos, size - dst_pos, off);
3701 off += size - dst_pos;
3702 } else if (t == src_type) {
3703 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3704 0, src_pos, off);
3705 off += src_pos;
3706 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3707 src_pos + n, size - src_pos - n, off);
3708 off += size - src_pos - n;
3709 } else {
3710 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3711 off += size;
3714 isl_dim_map_div(dim_map, bmap, off);
3716 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3717 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3718 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3719 if (!bmap)
3720 goto error;
3722 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3723 src_type, src_pos, n);
3724 if (!bmap->dim)
3725 goto error;
3727 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3728 bmap = isl_basic_map_gauss(bmap, NULL);
3729 bmap = isl_basic_map_finalize(bmap);
3731 return bmap;
3732 error:
3733 isl_basic_map_free(bmap);
3734 return NULL;
3737 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3738 enum isl_dim_type dst_type, unsigned dst_pos,
3739 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3741 isl_basic_map *bmap = bset_to_bmap(bset);
3742 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3743 src_type, src_pos, n);
3744 return bset_from_bmap(bmap);
3747 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3748 enum isl_dim_type dst_type, unsigned dst_pos,
3749 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3751 if (!set)
3752 return NULL;
3753 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3754 return set_from_map(isl_map_move_dims(set_to_map(set),
3755 dst_type, dst_pos, src_type, src_pos, n));
3756 error:
3757 isl_set_free(set);
3758 return NULL;
3761 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3762 enum isl_dim_type dst_type, unsigned dst_pos,
3763 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3765 int i;
3767 if (!map)
3768 return NULL;
3769 if (n == 0)
3770 return map;
3772 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3773 goto error);
3775 if (dst_type == src_type && dst_pos == src_pos)
3776 return map;
3778 isl_assert(map->ctx, dst_type != src_type, goto error);
3780 map = isl_map_cow(map);
3781 if (!map)
3782 return NULL;
3784 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3785 if (!map->dim)
3786 goto error;
3788 for (i = 0; i < map->n; ++i) {
3789 map->p[i] = isl_basic_map_move_dims(map->p[i],
3790 dst_type, dst_pos,
3791 src_type, src_pos, n);
3792 if (!map->p[i])
3793 goto error;
3796 return map;
3797 error:
3798 isl_map_free(map);
3799 return NULL;
3802 /* Move the specified dimensions to the last columns right before
3803 * the divs. Don't change the dimension specification of bmap.
3804 * That's the responsibility of the caller.
3806 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3807 enum isl_dim_type type, unsigned first, unsigned n)
3809 struct isl_dim_map *dim_map;
3810 struct isl_basic_map *res;
3811 enum isl_dim_type t;
3812 unsigned total, off;
3814 if (!bmap)
3815 return NULL;
3816 if (pos(bmap->dim, type) + first + n ==
3817 1 + isl_space_dim(bmap->dim, isl_dim_all))
3818 return bmap;
3820 total = isl_basic_map_total_dim(bmap);
3821 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3823 off = 0;
3824 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3825 unsigned size = isl_space_dim(bmap->dim, t);
3826 if (t == type) {
3827 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3828 0, first, off);
3829 off += first;
3830 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3831 first, n, total - bmap->n_div - n);
3832 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3833 first + n, size - (first + n), off);
3834 off += size - (first + n);
3835 } else {
3836 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3837 off += size;
3840 isl_dim_map_div(dim_map, bmap, off + n);
3842 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3843 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3844 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3845 return res;
3848 /* Insert "n" rows in the divs of "bmap".
3850 * The number of columns is not changed, which means that the last
3851 * dimensions of "bmap" are being reintepreted as the new divs.
3852 * The space of "bmap" is not adjusted, however, which means
3853 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3854 * from the space of "bmap" is the responsibility of the caller.
3856 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3857 int n)
3859 int i;
3860 size_t row_size;
3861 isl_int **new_div;
3862 isl_int *old;
3864 bmap = isl_basic_map_cow(bmap);
3865 if (!bmap)
3866 return NULL;
3868 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3869 old = bmap->block2.data;
3870 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3871 (bmap->extra + n) * (1 + row_size));
3872 if (!bmap->block2.data)
3873 return isl_basic_map_free(bmap);
3874 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3875 if (!new_div)
3876 return isl_basic_map_free(bmap);
3877 for (i = 0; i < n; ++i) {
3878 new_div[i] = bmap->block2.data +
3879 (bmap->extra + i) * (1 + row_size);
3880 isl_seq_clr(new_div[i], 1 + row_size);
3882 for (i = 0; i < bmap->extra; ++i)
3883 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3884 free(bmap->div);
3885 bmap->div = new_div;
3886 bmap->n_div += n;
3887 bmap->extra += n;
3889 return bmap;
3892 /* Drop constraints from "bmap" that only involve the variables
3893 * of "type" in the range [first, first + n] that are not related
3894 * to any of the variables outside that interval.
3895 * These constraints cannot influence the values for the variables
3896 * outside the interval, except in case they cause "bmap" to be empty.
3897 * Only drop the constraints if "bmap" is known to be non-empty.
3899 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3900 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3901 unsigned first, unsigned n)
3903 int i;
3904 int *groups;
3905 unsigned dim, n_div;
3906 isl_bool non_empty;
3908 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3909 if (non_empty < 0)
3910 return isl_basic_map_free(bmap);
3911 if (!non_empty)
3912 return bmap;
3914 dim = isl_basic_map_dim(bmap, isl_dim_all);
3915 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3916 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3917 if (!groups)
3918 return isl_basic_map_free(bmap);
3919 first += isl_basic_map_offset(bmap, type) - 1;
3920 for (i = 0; i < first; ++i)
3921 groups[i] = -1;
3922 for (i = first + n; i < dim - n_div; ++i)
3923 groups[i] = -1;
3925 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3927 return bmap;
3930 /* Turn the n dimensions of type type, starting at first
3931 * into existentially quantified variables.
3933 * If a subset of the projected out variables are unrelated
3934 * to any of the variables that remain, then the constraints
3935 * involving this subset are simply dropped first.
3937 __isl_give isl_basic_map *isl_basic_map_project_out(
3938 __isl_take isl_basic_map *bmap,
3939 enum isl_dim_type type, unsigned first, unsigned n)
3941 if (n == 0)
3942 return basic_map_space_reset(bmap, type);
3943 if (type == isl_dim_div)
3944 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3945 "cannot project out existentially quantified variables",
3946 return isl_basic_map_free(bmap));
3948 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3949 if (!bmap)
3950 return NULL;
3952 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3953 return isl_basic_map_remove_dims(bmap, type, first, n);
3955 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3956 return isl_basic_map_free(bmap);
3958 bmap = move_last(bmap, type, first, n);
3959 bmap = isl_basic_map_cow(bmap);
3960 bmap = insert_div_rows(bmap, n);
3961 if (!bmap)
3962 return NULL;
3964 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3965 if (!bmap->dim)
3966 goto error;
3967 bmap = isl_basic_map_simplify(bmap);
3968 bmap = isl_basic_map_drop_redundant_divs(bmap);
3969 return isl_basic_map_finalize(bmap);
3970 error:
3971 isl_basic_map_free(bmap);
3972 return NULL;
3975 /* Turn the n dimensions of type type, starting at first
3976 * into existentially quantified variables.
3978 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3979 enum isl_dim_type type, unsigned first, unsigned n)
3981 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3982 type, first, n));
3985 /* Turn the n dimensions of type type, starting at first
3986 * into existentially quantified variables.
3988 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3989 enum isl_dim_type type, unsigned first, unsigned n)
3991 int i;
3993 if (!map)
3994 return NULL;
3996 if (n == 0)
3997 return map_space_reset(map, type);
3999 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
4001 map = isl_map_cow(map);
4002 if (!map)
4003 return NULL;
4005 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4006 if (!map->dim)
4007 goto error;
4009 for (i = 0; i < map->n; ++i) {
4010 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4011 if (!map->p[i])
4012 goto error;
4015 return map;
4016 error:
4017 isl_map_free(map);
4018 return NULL;
4021 /* Turn the n dimensions of type type, starting at first
4022 * into existentially quantified variables.
4024 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4025 enum isl_dim_type type, unsigned first, unsigned n)
4027 return set_from_map(isl_map_project_out(set_to_map(set),
4028 type, first, n));
4031 /* Return a map that projects the elements in "set" onto their
4032 * "n" set dimensions starting at "first".
4033 * "type" should be equal to isl_dim_set.
4035 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4036 enum isl_dim_type type, unsigned first, unsigned n)
4038 int i;
4039 int dim;
4040 isl_map *map;
4042 if (!set)
4043 return NULL;
4044 if (type != isl_dim_set)
4045 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4046 "only set dimensions can be projected out", goto error);
4047 dim = isl_set_dim(set, isl_dim_set);
4048 if (first + n > dim || first + n < first)
4049 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4050 "index out of bounds", goto error);
4052 map = isl_map_from_domain(set);
4053 map = isl_map_add_dims(map, isl_dim_out, n);
4054 for (i = 0; i < n; ++i)
4055 map = isl_map_equate(map, isl_dim_in, first + i,
4056 isl_dim_out, i);
4057 return map;
4058 error:
4059 isl_set_free(set);
4060 return NULL;
4063 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4065 int i, j;
4067 for (i = 0; i < n; ++i) {
4068 j = isl_basic_map_alloc_div(bmap);
4069 if (j < 0)
4070 goto error;
4071 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4073 return bmap;
4074 error:
4075 isl_basic_map_free(bmap);
4076 return NULL;
4079 struct isl_basic_map *isl_basic_map_apply_range(
4080 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4082 isl_space *dim_result = NULL;
4083 struct isl_basic_map *bmap;
4084 unsigned n_in, n_out, n, nparam, total, pos;
4085 struct isl_dim_map *dim_map1, *dim_map2;
4087 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4088 goto error;
4089 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4090 bmap2->dim, isl_dim_in))
4091 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4092 "spaces don't match", goto error);
4094 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4095 isl_space_copy(bmap2->dim));
4097 n_in = isl_basic_map_n_in(bmap1);
4098 n_out = isl_basic_map_n_out(bmap2);
4099 n = isl_basic_map_n_out(bmap1);
4100 nparam = isl_basic_map_n_param(bmap1);
4102 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4103 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4104 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4105 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4106 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4107 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4108 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4109 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4110 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4111 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4112 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4114 bmap = isl_basic_map_alloc_space(dim_result,
4115 bmap1->n_div + bmap2->n_div + n,
4116 bmap1->n_eq + bmap2->n_eq,
4117 bmap1->n_ineq + bmap2->n_ineq);
4118 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4119 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4120 bmap = add_divs(bmap, n);
4121 bmap = isl_basic_map_simplify(bmap);
4122 bmap = isl_basic_map_drop_redundant_divs(bmap);
4123 return isl_basic_map_finalize(bmap);
4124 error:
4125 isl_basic_map_free(bmap1);
4126 isl_basic_map_free(bmap2);
4127 return NULL;
4130 struct isl_basic_set *isl_basic_set_apply(
4131 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4133 if (!bset || !bmap)
4134 goto error;
4136 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4137 goto error);
4139 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4140 bmap));
4141 error:
4142 isl_basic_set_free(bset);
4143 isl_basic_map_free(bmap);
4144 return NULL;
4147 struct isl_basic_map *isl_basic_map_apply_domain(
4148 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4150 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4151 goto error;
4152 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4153 bmap2->dim, isl_dim_in))
4154 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4155 "spaces don't match", goto error);
4157 bmap1 = isl_basic_map_reverse(bmap1);
4158 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4159 return isl_basic_map_reverse(bmap1);
4160 error:
4161 isl_basic_map_free(bmap1);
4162 isl_basic_map_free(bmap2);
4163 return NULL;
4166 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4167 * A \cap B -> f(A) + f(B)
4169 struct isl_basic_map *isl_basic_map_sum(
4170 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4172 unsigned n_in, n_out, nparam, total, pos;
4173 struct isl_basic_map *bmap = NULL;
4174 struct isl_dim_map *dim_map1, *dim_map2;
4175 int i;
4177 if (!bmap1 || !bmap2)
4178 goto error;
4180 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4181 goto error);
4183 nparam = isl_basic_map_n_param(bmap1);
4184 n_in = isl_basic_map_n_in(bmap1);
4185 n_out = isl_basic_map_n_out(bmap1);
4187 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4188 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4189 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4190 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4191 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4192 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4193 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4194 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4195 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4196 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4197 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4199 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4200 bmap1->n_div + bmap2->n_div + 2 * n_out,
4201 bmap1->n_eq + bmap2->n_eq + n_out,
4202 bmap1->n_ineq + bmap2->n_ineq);
4203 for (i = 0; i < n_out; ++i) {
4204 int j = isl_basic_map_alloc_equality(bmap);
4205 if (j < 0)
4206 goto error;
4207 isl_seq_clr(bmap->eq[j], 1+total);
4208 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4209 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4210 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4212 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4213 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4214 bmap = add_divs(bmap, 2 * n_out);
4216 bmap = isl_basic_map_simplify(bmap);
4217 return isl_basic_map_finalize(bmap);
4218 error:
4219 isl_basic_map_free(bmap);
4220 isl_basic_map_free(bmap1);
4221 isl_basic_map_free(bmap2);
4222 return NULL;
4225 /* Given two maps A -> f(A) and B -> g(B), construct a map
4226 * A \cap B -> f(A) + f(B)
4228 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
4230 struct isl_map *result;
4231 int i, j;
4233 if (!map1 || !map2)
4234 goto error;
4236 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4238 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4239 map1->n * map2->n, 0);
4240 if (!result)
4241 goto error;
4242 for (i = 0; i < map1->n; ++i)
4243 for (j = 0; j < map2->n; ++j) {
4244 struct isl_basic_map *part;
4245 part = isl_basic_map_sum(
4246 isl_basic_map_copy(map1->p[i]),
4247 isl_basic_map_copy(map2->p[j]));
4248 if (isl_basic_map_is_empty(part))
4249 isl_basic_map_free(part);
4250 else
4251 result = isl_map_add_basic_map(result, part);
4252 if (!result)
4253 goto error;
4255 isl_map_free(map1);
4256 isl_map_free(map2);
4257 return result;
4258 error:
4259 isl_map_free(map1);
4260 isl_map_free(map2);
4261 return NULL;
4264 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4265 __isl_take isl_set *set2)
4267 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4270 /* Given a basic map A -> f(A), construct A -> -f(A).
4272 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4274 int i, j;
4275 unsigned off, n;
4277 bmap = isl_basic_map_cow(bmap);
4278 if (!bmap)
4279 return NULL;
4281 n = isl_basic_map_dim(bmap, isl_dim_out);
4282 off = isl_basic_map_offset(bmap, isl_dim_out);
4283 for (i = 0; i < bmap->n_eq; ++i)
4284 for (j = 0; j < n; ++j)
4285 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4286 for (i = 0; i < bmap->n_ineq; ++i)
4287 for (j = 0; j < n; ++j)
4288 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4289 for (i = 0; i < bmap->n_div; ++i)
4290 for (j = 0; j < n; ++j)
4291 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4292 bmap = isl_basic_map_gauss(bmap, NULL);
4293 return isl_basic_map_finalize(bmap);
4296 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4298 return isl_basic_map_neg(bset);
4301 /* Given a map A -> f(A), construct A -> -f(A).
4303 struct isl_map *isl_map_neg(struct isl_map *map)
4305 int i;
4307 map = isl_map_cow(map);
4308 if (!map)
4309 return NULL;
4311 for (i = 0; i < map->n; ++i) {
4312 map->p[i] = isl_basic_map_neg(map->p[i]);
4313 if (!map->p[i])
4314 goto error;
4317 return map;
4318 error:
4319 isl_map_free(map);
4320 return NULL;
4323 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4325 return set_from_map(isl_map_neg(set_to_map(set)));
4328 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4329 * A -> floor(f(A)/d).
4331 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4332 isl_int d)
4334 unsigned n_in, n_out, nparam, total, pos;
4335 struct isl_basic_map *result = NULL;
4336 struct isl_dim_map *dim_map;
4337 int i;
4339 if (!bmap)
4340 return NULL;
4342 nparam = isl_basic_map_n_param(bmap);
4343 n_in = isl_basic_map_n_in(bmap);
4344 n_out = isl_basic_map_n_out(bmap);
4346 total = nparam + n_in + n_out + bmap->n_div + n_out;
4347 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4348 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4349 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4350 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4351 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4353 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4354 bmap->n_div + n_out,
4355 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4356 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4357 result = add_divs(result, n_out);
4358 for (i = 0; i < n_out; ++i) {
4359 int j;
4360 j = isl_basic_map_alloc_inequality(result);
4361 if (j < 0)
4362 goto error;
4363 isl_seq_clr(result->ineq[j], 1+total);
4364 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4365 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4366 j = isl_basic_map_alloc_inequality(result);
4367 if (j < 0)
4368 goto error;
4369 isl_seq_clr(result->ineq[j], 1+total);
4370 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4371 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4372 isl_int_sub_ui(result->ineq[j][0], d, 1);
4375 result = isl_basic_map_simplify(result);
4376 return isl_basic_map_finalize(result);
4377 error:
4378 isl_basic_map_free(result);
4379 return NULL;
4382 /* Given a map A -> f(A) and an integer d, construct a map
4383 * A -> floor(f(A)/d).
4385 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4387 int i;
4389 map = isl_map_cow(map);
4390 if (!map)
4391 return NULL;
4393 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4394 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4395 for (i = 0; i < map->n; ++i) {
4396 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4397 if (!map->p[i])
4398 goto error;
4401 return map;
4402 error:
4403 isl_map_free(map);
4404 return NULL;
4407 /* Given a map A -> f(A) and an integer d, construct a map
4408 * A -> floor(f(A)/d).
4410 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4411 __isl_take isl_val *d)
4413 if (!map || !d)
4414 goto error;
4415 if (!isl_val_is_int(d))
4416 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4417 "expecting integer denominator", goto error);
4418 map = isl_map_floordiv(map, d->n);
4419 isl_val_free(d);
4420 return map;
4421 error:
4422 isl_map_free(map);
4423 isl_val_free(d);
4424 return NULL;
4427 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4429 int i;
4430 unsigned nparam;
4431 unsigned n_in;
4433 i = isl_basic_map_alloc_equality(bmap);
4434 if (i < 0)
4435 goto error;
4436 nparam = isl_basic_map_n_param(bmap);
4437 n_in = isl_basic_map_n_in(bmap);
4438 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4439 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4440 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4441 return isl_basic_map_finalize(bmap);
4442 error:
4443 isl_basic_map_free(bmap);
4444 return NULL;
4447 /* Add a constraint to "bmap" expressing i_pos < o_pos
4449 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4451 int i;
4452 unsigned nparam;
4453 unsigned n_in;
4455 i = isl_basic_map_alloc_inequality(bmap);
4456 if (i < 0)
4457 goto error;
4458 nparam = isl_basic_map_n_param(bmap);
4459 n_in = isl_basic_map_n_in(bmap);
4460 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4461 isl_int_set_si(bmap->ineq[i][0], -1);
4462 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4463 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4464 return isl_basic_map_finalize(bmap);
4465 error:
4466 isl_basic_map_free(bmap);
4467 return NULL;
4470 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4472 static __isl_give isl_basic_map *var_less_or_equal(
4473 __isl_take isl_basic_map *bmap, unsigned pos)
4475 int i;
4476 unsigned nparam;
4477 unsigned n_in;
4479 i = isl_basic_map_alloc_inequality(bmap);
4480 if (i < 0)
4481 goto error;
4482 nparam = isl_basic_map_n_param(bmap);
4483 n_in = isl_basic_map_n_in(bmap);
4484 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4485 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4486 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4487 return isl_basic_map_finalize(bmap);
4488 error:
4489 isl_basic_map_free(bmap);
4490 return NULL;
4493 /* Add a constraint to "bmap" expressing i_pos > o_pos
4495 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4497 int i;
4498 unsigned nparam;
4499 unsigned n_in;
4501 i = isl_basic_map_alloc_inequality(bmap);
4502 if (i < 0)
4503 goto error;
4504 nparam = isl_basic_map_n_param(bmap);
4505 n_in = isl_basic_map_n_in(bmap);
4506 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4507 isl_int_set_si(bmap->ineq[i][0], -1);
4508 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4509 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4510 return isl_basic_map_finalize(bmap);
4511 error:
4512 isl_basic_map_free(bmap);
4513 return NULL;
4516 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4518 static __isl_give isl_basic_map *var_more_or_equal(
4519 __isl_take isl_basic_map *bmap, unsigned pos)
4521 int i;
4522 unsigned nparam;
4523 unsigned n_in;
4525 i = isl_basic_map_alloc_inequality(bmap);
4526 if (i < 0)
4527 goto error;
4528 nparam = isl_basic_map_n_param(bmap);
4529 n_in = isl_basic_map_n_in(bmap);
4530 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4531 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4532 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4533 return isl_basic_map_finalize(bmap);
4534 error:
4535 isl_basic_map_free(bmap);
4536 return NULL;
4539 __isl_give isl_basic_map *isl_basic_map_equal(
4540 __isl_take isl_space *dim, unsigned n_equal)
4542 int i;
4543 struct isl_basic_map *bmap;
4544 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4545 if (!bmap)
4546 return NULL;
4547 for (i = 0; i < n_equal && bmap; ++i)
4548 bmap = var_equal(bmap, i);
4549 return isl_basic_map_finalize(bmap);
4552 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4554 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4555 unsigned pos)
4557 int i;
4558 struct isl_basic_map *bmap;
4559 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4560 if (!bmap)
4561 return NULL;
4562 for (i = 0; i < pos && bmap; ++i)
4563 bmap = var_equal(bmap, i);
4564 if (bmap)
4565 bmap = var_less(bmap, pos);
4566 return isl_basic_map_finalize(bmap);
4569 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4571 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4572 __isl_take isl_space *dim, unsigned pos)
4574 int i;
4575 isl_basic_map *bmap;
4577 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4578 for (i = 0; i < pos; ++i)
4579 bmap = var_equal(bmap, i);
4580 bmap = var_less_or_equal(bmap, pos);
4581 return isl_basic_map_finalize(bmap);
4584 /* Return a relation on "dim" expressing i_pos > o_pos
4586 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4587 unsigned pos)
4589 int i;
4590 struct isl_basic_map *bmap;
4591 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4592 if (!bmap)
4593 return NULL;
4594 for (i = 0; i < pos && bmap; ++i)
4595 bmap = var_equal(bmap, i);
4596 if (bmap)
4597 bmap = var_more(bmap, pos);
4598 return isl_basic_map_finalize(bmap);
4601 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4603 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4604 __isl_take isl_space *dim, unsigned pos)
4606 int i;
4607 isl_basic_map *bmap;
4609 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4610 for (i = 0; i < pos; ++i)
4611 bmap = var_equal(bmap, i);
4612 bmap = var_more_or_equal(bmap, pos);
4613 return isl_basic_map_finalize(bmap);
4616 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4617 unsigned n, int equal)
4619 struct isl_map *map;
4620 int i;
4622 if (n == 0 && equal)
4623 return isl_map_universe(dims);
4625 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4627 for (i = 0; i + 1 < n; ++i)
4628 map = isl_map_add_basic_map(map,
4629 isl_basic_map_less_at(isl_space_copy(dims), i));
4630 if (n > 0) {
4631 if (equal)
4632 map = isl_map_add_basic_map(map,
4633 isl_basic_map_less_or_equal_at(dims, n - 1));
4634 else
4635 map = isl_map_add_basic_map(map,
4636 isl_basic_map_less_at(dims, n - 1));
4637 } else
4638 isl_space_free(dims);
4640 return map;
4643 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4645 if (!dims)
4646 return NULL;
4647 return map_lex_lte_first(dims, dims->n_out, equal);
4650 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4652 return map_lex_lte_first(dim, n, 0);
4655 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4657 return map_lex_lte_first(dim, n, 1);
4660 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4662 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4665 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4667 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4670 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4671 unsigned n, int equal)
4673 struct isl_map *map;
4674 int i;
4676 if (n == 0 && equal)
4677 return isl_map_universe(dims);
4679 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4681 for (i = 0; i + 1 < n; ++i)
4682 map = isl_map_add_basic_map(map,
4683 isl_basic_map_more_at(isl_space_copy(dims), i));
4684 if (n > 0) {
4685 if (equal)
4686 map = isl_map_add_basic_map(map,
4687 isl_basic_map_more_or_equal_at(dims, n - 1));
4688 else
4689 map = isl_map_add_basic_map(map,
4690 isl_basic_map_more_at(dims, n - 1));
4691 } else
4692 isl_space_free(dims);
4694 return map;
4697 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4699 if (!dims)
4700 return NULL;
4701 return map_lex_gte_first(dims, dims->n_out, equal);
4704 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4706 return map_lex_gte_first(dim, n, 0);
4709 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4711 return map_lex_gte_first(dim, n, 1);
4714 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4716 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4719 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4721 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4724 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4725 __isl_take isl_set *set2)
4727 isl_map *map;
4728 map = isl_map_lex_le(isl_set_get_space(set1));
4729 map = isl_map_intersect_domain(map, set1);
4730 map = isl_map_intersect_range(map, set2);
4731 return map;
4734 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4735 __isl_take isl_set *set2)
4737 isl_map *map;
4738 map = isl_map_lex_lt(isl_set_get_space(set1));
4739 map = isl_map_intersect_domain(map, set1);
4740 map = isl_map_intersect_range(map, set2);
4741 return map;
4744 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4745 __isl_take isl_set *set2)
4747 isl_map *map;
4748 map = isl_map_lex_ge(isl_set_get_space(set1));
4749 map = isl_map_intersect_domain(map, set1);
4750 map = isl_map_intersect_range(map, set2);
4751 return map;
4754 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4755 __isl_take isl_set *set2)
4757 isl_map *map;
4758 map = isl_map_lex_gt(isl_set_get_space(set1));
4759 map = isl_map_intersect_domain(map, set1);
4760 map = isl_map_intersect_range(map, set2);
4761 return map;
4764 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4765 __isl_take isl_map *map2)
4767 isl_map *map;
4768 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4769 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4770 map = isl_map_apply_range(map, isl_map_reverse(map2));
4771 return map;
4774 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4775 __isl_take isl_map *map2)
4777 isl_map *map;
4778 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4779 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4780 map = isl_map_apply_range(map, isl_map_reverse(map2));
4781 return map;
4784 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4785 __isl_take isl_map *map2)
4787 isl_map *map;
4788 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4789 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4790 map = isl_map_apply_range(map, isl_map_reverse(map2));
4791 return map;
4794 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4795 __isl_take isl_map *map2)
4797 isl_map *map;
4798 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4799 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4800 map = isl_map_apply_range(map, isl_map_reverse(map2));
4801 return map;
4804 /* For a div d = floor(f/m), add the constraint
4806 * f - m d >= 0
4808 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4809 unsigned pos, isl_int *div)
4811 int i;
4812 unsigned total = isl_basic_map_total_dim(bmap);
4814 i = isl_basic_map_alloc_inequality(bmap);
4815 if (i < 0)
4816 return isl_stat_error;
4817 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4818 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4820 return isl_stat_ok;
4823 /* For a div d = floor(f/m), add the constraint
4825 * -(f-(m-1)) + m d >= 0
4827 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4828 unsigned pos, isl_int *div)
4830 int i;
4831 unsigned total = isl_basic_map_total_dim(bmap);
4833 i = isl_basic_map_alloc_inequality(bmap);
4834 if (i < 0)
4835 return isl_stat_error;
4836 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4837 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4838 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4839 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4841 return isl_stat_ok;
4844 /* For a div d = floor(f/m), add the constraints
4846 * f - m d >= 0
4847 * -(f-(m-1)) + m d >= 0
4849 * Note that the second constraint is the negation of
4851 * f - m d >= m
4853 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4854 unsigned pos, isl_int *div)
4856 if (add_upper_div_constraint(bmap, pos, div) < 0)
4857 return -1;
4858 if (add_lower_div_constraint(bmap, pos, div) < 0)
4859 return -1;
4860 return 0;
4863 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4864 unsigned pos, isl_int *div)
4866 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4867 pos, div);
4870 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4872 unsigned total = isl_basic_map_total_dim(bmap);
4873 unsigned div_pos = total - bmap->n_div + div;
4875 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4876 bmap->div[div]);
4879 /* For each known div d = floor(f/m), add the constraints
4881 * f - m d >= 0
4882 * -(f-(m-1)) + m d >= 0
4884 * Remove duplicate constraints in case of some these div constraints
4885 * already appear in "bmap".
4887 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4888 __isl_take isl_basic_map *bmap)
4890 unsigned n_div;
4892 if (!bmap)
4893 return NULL;
4894 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4895 if (n_div == 0)
4896 return bmap;
4898 bmap = add_known_div_constraints(bmap);
4899 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4900 bmap = isl_basic_map_finalize(bmap);
4901 return bmap;
4904 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4906 * In particular, if this div is of the form d = floor(f/m),
4907 * then add the constraint
4909 * f - m d >= 0
4911 * if sign < 0 or the constraint
4913 * -(f-(m-1)) + m d >= 0
4915 * if sign > 0.
4917 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4918 unsigned div, int sign)
4920 unsigned total;
4921 unsigned div_pos;
4923 if (!bmap)
4924 return -1;
4926 total = isl_basic_map_total_dim(bmap);
4927 div_pos = total - bmap->n_div + div;
4929 if (sign < 0)
4930 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4931 else
4932 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4935 struct isl_basic_set *isl_basic_map_underlying_set(
4936 struct isl_basic_map *bmap)
4938 if (!bmap)
4939 goto error;
4940 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4941 bmap->n_div == 0 &&
4942 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4943 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4944 return bset_from_bmap(bmap);
4945 bmap = isl_basic_map_cow(bmap);
4946 if (!bmap)
4947 goto error;
4948 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4949 if (!bmap->dim)
4950 goto error;
4951 bmap->extra -= bmap->n_div;
4952 bmap->n_div = 0;
4953 bmap = isl_basic_map_finalize(bmap);
4954 return bset_from_bmap(bmap);
4955 error:
4956 isl_basic_map_free(bmap);
4957 return NULL;
4960 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4961 __isl_take isl_basic_set *bset)
4963 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4966 /* Replace each element in "list" by the result of applying
4967 * isl_basic_map_underlying_set to the element.
4969 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4970 __isl_take isl_basic_map_list *list)
4972 int i, n;
4974 if (!list)
4975 return NULL;
4977 n = isl_basic_map_list_n_basic_map(list);
4978 for (i = 0; i < n; ++i) {
4979 isl_basic_map *bmap;
4980 isl_basic_set *bset;
4982 bmap = isl_basic_map_list_get_basic_map(list, i);
4983 bset = isl_basic_set_underlying_set(bmap);
4984 list = isl_basic_set_list_set_basic_set(list, i, bset);
4987 return list;
4990 struct isl_basic_map *isl_basic_map_overlying_set(
4991 struct isl_basic_set *bset, struct isl_basic_map *like)
4993 struct isl_basic_map *bmap;
4994 struct isl_ctx *ctx;
4995 unsigned total;
4996 int i;
4998 if (!bset || !like)
4999 goto error;
5000 ctx = bset->ctx;
5001 isl_assert(ctx, bset->n_div == 0, goto error);
5002 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5003 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5004 goto error);
5005 if (like->n_div == 0) {
5006 isl_space *space = isl_basic_map_get_space(like);
5007 isl_basic_map_free(like);
5008 return isl_basic_map_reset_space(bset, space);
5010 bset = isl_basic_set_cow(bset);
5011 if (!bset)
5012 goto error;
5013 total = bset->dim->n_out + bset->extra;
5014 bmap = bset_to_bmap(bset);
5015 isl_space_free(bmap->dim);
5016 bmap->dim = isl_space_copy(like->dim);
5017 if (!bmap->dim)
5018 goto error;
5019 bmap->n_div = like->n_div;
5020 bmap->extra += like->n_div;
5021 if (bmap->extra) {
5022 unsigned ltotal;
5023 isl_int **div;
5024 ltotal = total - bmap->extra + like->extra;
5025 if (ltotal > total)
5026 ltotal = total;
5027 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5028 bmap->extra * (1 + 1 + total));
5029 if (isl_blk_is_error(bmap->block2))
5030 goto error;
5031 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5032 if (!div)
5033 goto error;
5034 bmap->div = div;
5035 for (i = 0; i < bmap->extra; ++i)
5036 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5037 for (i = 0; i < like->n_div; ++i) {
5038 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5039 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5041 bmap = isl_basic_map_add_known_div_constraints(bmap);
5043 isl_basic_map_free(like);
5044 bmap = isl_basic_map_simplify(bmap);
5045 bmap = isl_basic_map_finalize(bmap);
5046 return bmap;
5047 error:
5048 isl_basic_map_free(like);
5049 isl_basic_set_free(bset);
5050 return NULL;
5053 struct isl_basic_set *isl_basic_set_from_underlying_set(
5054 struct isl_basic_set *bset, struct isl_basic_set *like)
5056 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5057 bset_to_bmap(like)));
5060 struct isl_set *isl_map_underlying_set(struct isl_map *map)
5062 int i;
5064 map = isl_map_cow(map);
5065 if (!map)
5066 return NULL;
5067 map->dim = isl_space_cow(map->dim);
5068 if (!map->dim)
5069 goto error;
5071 for (i = 1; i < map->n; ++i)
5072 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5073 goto error);
5074 for (i = 0; i < map->n; ++i) {
5075 map->p[i] = bset_to_bmap(
5076 isl_basic_map_underlying_set(map->p[i]));
5077 if (!map->p[i])
5078 goto error;
5080 if (map->n == 0)
5081 map->dim = isl_space_underlying(map->dim, 0);
5082 else {
5083 isl_space_free(map->dim);
5084 map->dim = isl_space_copy(map->p[0]->dim);
5086 if (!map->dim)
5087 goto error;
5088 return set_from_map(map);
5089 error:
5090 isl_map_free(map);
5091 return NULL;
5094 /* Replace the space of "bmap" by "space".
5096 * If the space of "bmap" is identical to "space" (including the identifiers
5097 * of the input and output dimensions), then simply return the original input.
5099 __isl_give isl_basic_map *isl_basic_map_reset_space(
5100 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5102 isl_bool equal;
5103 isl_space *bmap_space;
5105 bmap_space = isl_basic_map_peek_space(bmap);
5106 equal = isl_space_is_equal(bmap_space, space);
5107 if (equal >= 0 && equal)
5108 equal = isl_space_match(bmap_space, isl_dim_in,
5109 space, isl_dim_in);
5110 if (equal >= 0 && equal)
5111 equal = isl_space_match(bmap_space, isl_dim_out,
5112 space, isl_dim_out);
5113 if (equal < 0)
5114 goto error;
5115 if (equal) {
5116 isl_space_free(space);
5117 return bmap;
5119 bmap = isl_basic_map_cow(bmap);
5120 if (!bmap || !space)
5121 goto error;
5123 isl_space_free(bmap->dim);
5124 bmap->dim = space;
5126 bmap = isl_basic_map_finalize(bmap);
5128 return bmap;
5129 error:
5130 isl_basic_map_free(bmap);
5131 isl_space_free(space);
5132 return NULL;
5135 __isl_give isl_basic_set *isl_basic_set_reset_space(
5136 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5138 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5139 dim));
5142 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5143 __isl_take isl_space *dim)
5145 int i;
5147 map = isl_map_cow(map);
5148 if (!map || !dim)
5149 goto error;
5151 for (i = 0; i < map->n; ++i) {
5152 map->p[i] = isl_basic_map_reset_space(map->p[i],
5153 isl_space_copy(dim));
5154 if (!map->p[i])
5155 goto error;
5157 isl_space_free(map->dim);
5158 map->dim = dim;
5160 return map;
5161 error:
5162 isl_map_free(map);
5163 isl_space_free(dim);
5164 return NULL;
5167 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5168 __isl_take isl_space *dim)
5170 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5173 /* Compute the parameter domain of the given basic set.
5175 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5177 isl_bool is_params;
5178 isl_space *space;
5179 unsigned n;
5181 is_params = isl_basic_set_is_params(bset);
5182 if (is_params < 0)
5183 return isl_basic_set_free(bset);
5184 if (is_params)
5185 return bset;
5187 n = isl_basic_set_dim(bset, isl_dim_set);
5188 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5189 space = isl_basic_set_get_space(bset);
5190 space = isl_space_params(space);
5191 bset = isl_basic_set_reset_space(bset, space);
5192 return bset;
5195 /* Construct a zero-dimensional basic set with the given parameter domain.
5197 __isl_give isl_basic_set *isl_basic_set_from_params(
5198 __isl_take isl_basic_set *bset)
5200 isl_space *space;
5201 space = isl_basic_set_get_space(bset);
5202 space = isl_space_set_from_params(space);
5203 bset = isl_basic_set_reset_space(bset, space);
5204 return bset;
5207 /* Compute the parameter domain of the given set.
5209 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5211 isl_space *space;
5212 unsigned n;
5214 if (isl_set_is_params(set))
5215 return set;
5217 n = isl_set_dim(set, isl_dim_set);
5218 set = isl_set_project_out(set, isl_dim_set, 0, n);
5219 space = isl_set_get_space(set);
5220 space = isl_space_params(space);
5221 set = isl_set_reset_space(set, space);
5222 return set;
5225 /* Construct a zero-dimensional set with the given parameter domain.
5227 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5229 isl_space *space;
5230 space = isl_set_get_space(set);
5231 space = isl_space_set_from_params(space);
5232 set = isl_set_reset_space(set, space);
5233 return set;
5236 /* Compute the parameter domain of the given map.
5238 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5240 isl_space *space;
5241 unsigned n;
5243 n = isl_map_dim(map, isl_dim_in);
5244 map = isl_map_project_out(map, isl_dim_in, 0, n);
5245 n = isl_map_dim(map, isl_dim_out);
5246 map = isl_map_project_out(map, isl_dim_out, 0, n);
5247 space = isl_map_get_space(map);
5248 space = isl_space_params(space);
5249 map = isl_map_reset_space(map, space);
5250 return map;
5253 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5255 isl_space *space;
5256 unsigned n_out;
5258 if (!bmap)
5259 return NULL;
5260 space = isl_space_domain(isl_basic_map_get_space(bmap));
5262 n_out = isl_basic_map_n_out(bmap);
5263 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5265 return isl_basic_map_reset_space(bmap, space);
5268 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5270 if (!bmap)
5271 return isl_bool_error;
5272 return isl_space_may_be_set(bmap->dim);
5275 /* Is this basic map actually a set?
5276 * Users should never call this function. Outside of isl,
5277 * the type should indicate whether something is a set or a map.
5279 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5281 if (!bmap)
5282 return isl_bool_error;
5283 return isl_space_is_set(bmap->dim);
5286 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5288 isl_bool is_set;
5290 is_set = isl_basic_map_is_set(bmap);
5291 if (is_set < 0)
5292 goto error;
5293 if (is_set)
5294 return bmap;
5295 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5296 error:
5297 isl_basic_map_free(bmap);
5298 return NULL;
5301 __isl_give isl_basic_map *isl_basic_map_domain_map(
5302 __isl_take isl_basic_map *bmap)
5304 int i;
5305 isl_space *dim;
5306 isl_basic_map *domain;
5307 int nparam, n_in, n_out;
5309 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5310 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5311 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5313 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5314 domain = isl_basic_map_universe(dim);
5316 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5317 bmap = isl_basic_map_apply_range(bmap, domain);
5318 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5320 for (i = 0; i < n_in; ++i)
5321 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5322 isl_dim_out, i);
5324 bmap = isl_basic_map_gauss(bmap, NULL);
5325 return isl_basic_map_finalize(bmap);
5328 __isl_give isl_basic_map *isl_basic_map_range_map(
5329 __isl_take isl_basic_map *bmap)
5331 int i;
5332 isl_space *dim;
5333 isl_basic_map *range;
5334 int nparam, n_in, n_out;
5336 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5337 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5338 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5340 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5341 range = isl_basic_map_universe(dim);
5343 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5344 bmap = isl_basic_map_apply_range(bmap, range);
5345 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5347 for (i = 0; i < n_out; ++i)
5348 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5349 isl_dim_out, i);
5351 bmap = isl_basic_map_gauss(bmap, NULL);
5352 return isl_basic_map_finalize(bmap);
5355 int isl_map_may_be_set(__isl_keep isl_map *map)
5357 if (!map)
5358 return -1;
5359 return isl_space_may_be_set(map->dim);
5362 /* Is this map actually a set?
5363 * Users should never call this function. Outside of isl,
5364 * the type should indicate whether something is a set or a map.
5366 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5368 if (!map)
5369 return isl_bool_error;
5370 return isl_space_is_set(map->dim);
5373 struct isl_set *isl_map_range(struct isl_map *map)
5375 int i;
5376 isl_bool is_set;
5377 struct isl_set *set;
5379 is_set = isl_map_is_set(map);
5380 if (is_set < 0)
5381 goto error;
5382 if (is_set)
5383 return set_from_map(map);
5385 map = isl_map_cow(map);
5386 if (!map)
5387 goto error;
5389 set = set_from_map(map);
5390 set->dim = isl_space_range(set->dim);
5391 if (!set->dim)
5392 goto error;
5393 for (i = 0; i < map->n; ++i) {
5394 set->p[i] = isl_basic_map_range(map->p[i]);
5395 if (!set->p[i])
5396 goto error;
5398 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5399 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5400 return set;
5401 error:
5402 isl_map_free(map);
5403 return NULL;
5406 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5408 int i;
5410 map = isl_map_cow(map);
5411 if (!map)
5412 return NULL;
5414 map->dim = isl_space_domain_map(map->dim);
5415 if (!map->dim)
5416 goto error;
5417 for (i = 0; i < map->n; ++i) {
5418 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5419 if (!map->p[i])
5420 goto error;
5422 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5423 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5424 return map;
5425 error:
5426 isl_map_free(map);
5427 return NULL;
5430 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5432 int i;
5433 isl_space *range_dim;
5435 map = isl_map_cow(map);
5436 if (!map)
5437 return NULL;
5439 range_dim = isl_space_range(isl_map_get_space(map));
5440 range_dim = isl_space_from_range(range_dim);
5441 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5442 map->dim = isl_space_join(map->dim, range_dim);
5443 if (!map->dim)
5444 goto error;
5445 for (i = 0; i < map->n; ++i) {
5446 map->p[i] = isl_basic_map_range_map(map->p[i]);
5447 if (!map->p[i])
5448 goto error;
5450 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5451 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5452 return map;
5453 error:
5454 isl_map_free(map);
5455 return NULL;
5458 /* Given a wrapped map of the form A[B -> C],
5459 * return the map A[B -> C] -> B.
5461 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5463 isl_id *id;
5464 isl_map *map;
5466 if (!set)
5467 return NULL;
5468 if (!isl_set_has_tuple_id(set))
5469 return isl_map_domain_map(isl_set_unwrap(set));
5471 id = isl_set_get_tuple_id(set);
5472 map = isl_map_domain_map(isl_set_unwrap(set));
5473 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5475 return map;
5478 __isl_give isl_basic_map *isl_basic_map_from_domain(
5479 __isl_take isl_basic_set *bset)
5481 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5484 __isl_give isl_basic_map *isl_basic_map_from_range(
5485 __isl_take isl_basic_set *bset)
5487 isl_space *space;
5488 space = isl_basic_set_get_space(bset);
5489 space = isl_space_from_range(space);
5490 bset = isl_basic_set_reset_space(bset, space);
5491 return bset_to_bmap(bset);
5494 /* Create a relation with the given set as range.
5495 * The domain of the created relation is a zero-dimensional
5496 * flat anonymous space.
5498 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5500 isl_space *space;
5501 space = isl_set_get_space(set);
5502 space = isl_space_from_range(space);
5503 set = isl_set_reset_space(set, space);
5504 return set_to_map(set);
5507 /* Create a relation with the given set as domain.
5508 * The range of the created relation is a zero-dimensional
5509 * flat anonymous space.
5511 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5513 return isl_map_reverse(isl_map_from_range(set));
5516 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5517 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5519 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5522 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5523 __isl_take isl_set *range)
5525 return isl_map_apply_range(isl_map_reverse(domain), range);
5528 /* Return a newly allocated isl_map with given space and flags and
5529 * room for "n" basic maps.
5530 * Make sure that all cached information is cleared.
5532 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5533 unsigned flags)
5535 struct isl_map *map;
5537 if (!space)
5538 return NULL;
5539 if (n < 0)
5540 isl_die(space->ctx, isl_error_internal,
5541 "negative number of basic maps", goto error);
5542 map = isl_calloc(space->ctx, struct isl_map,
5543 sizeof(struct isl_map) +
5544 (n - 1) * sizeof(struct isl_basic_map *));
5545 if (!map)
5546 goto error;
5548 map->ctx = space->ctx;
5549 isl_ctx_ref(map->ctx);
5550 map->ref = 1;
5551 map->size = n;
5552 map->n = 0;
5553 map->dim = space;
5554 map->flags = flags;
5555 return map;
5556 error:
5557 isl_space_free(space);
5558 return NULL;
5561 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5563 struct isl_basic_map *bmap;
5564 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5565 bmap = isl_basic_map_set_to_empty(bmap);
5566 return bmap;
5569 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5571 struct isl_basic_set *bset;
5572 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5573 bset = isl_basic_set_set_to_empty(bset);
5574 return bset;
5577 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5579 struct isl_basic_map *bmap;
5580 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5581 bmap = isl_basic_map_finalize(bmap);
5582 return bmap;
5585 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5587 struct isl_basic_set *bset;
5588 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5589 bset = isl_basic_set_finalize(bset);
5590 return bset;
5593 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5595 int i;
5596 unsigned total = isl_space_dim(dim, isl_dim_all);
5597 isl_basic_map *bmap;
5599 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5600 for (i = 0; i < total; ++i) {
5601 int k = isl_basic_map_alloc_inequality(bmap);
5602 if (k < 0)
5603 goto error;
5604 isl_seq_clr(bmap->ineq[k], 1 + total);
5605 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5607 return bmap;
5608 error:
5609 isl_basic_map_free(bmap);
5610 return NULL;
5613 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5615 return isl_basic_map_nat_universe(dim);
5618 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5620 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5623 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5625 return isl_map_nat_universe(dim);
5628 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5630 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5633 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5635 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5638 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5640 struct isl_map *map;
5641 if (!dim)
5642 return NULL;
5643 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5644 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5645 return map;
5648 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5650 struct isl_set *set;
5651 if (!dim)
5652 return NULL;
5653 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5654 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5655 return set;
5658 struct isl_map *isl_map_dup(struct isl_map *map)
5660 int i;
5661 struct isl_map *dup;
5663 if (!map)
5664 return NULL;
5665 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5666 for (i = 0; i < map->n; ++i)
5667 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5668 return dup;
5671 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5672 __isl_take isl_basic_map *bmap)
5674 if (!bmap || !map)
5675 goto error;
5676 if (isl_basic_map_plain_is_empty(bmap)) {
5677 isl_basic_map_free(bmap);
5678 return map;
5680 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5681 isl_assert(map->ctx, map->n < map->size, goto error);
5682 map->p[map->n] = bmap;
5683 map->n++;
5684 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5685 return map;
5686 error:
5687 if (map)
5688 isl_map_free(map);
5689 if (bmap)
5690 isl_basic_map_free(bmap);
5691 return NULL;
5694 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5696 int i;
5698 if (!map)
5699 return NULL;
5701 if (--map->ref > 0)
5702 return NULL;
5704 clear_caches(map);
5705 isl_ctx_deref(map->ctx);
5706 for (i = 0; i < map->n; ++i)
5707 isl_basic_map_free(map->p[i]);
5708 isl_space_free(map->dim);
5709 free(map);
5711 return NULL;
5714 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5715 struct isl_basic_map *bmap, unsigned pos, int value)
5717 int j;
5719 bmap = isl_basic_map_cow(bmap);
5720 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5721 j = isl_basic_map_alloc_equality(bmap);
5722 if (j < 0)
5723 goto error;
5724 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5725 isl_int_set_si(bmap->eq[j][pos], -1);
5726 isl_int_set_si(bmap->eq[j][0], value);
5727 bmap = isl_basic_map_simplify(bmap);
5728 return isl_basic_map_finalize(bmap);
5729 error:
5730 isl_basic_map_free(bmap);
5731 return NULL;
5734 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5735 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5737 int j;
5739 bmap = isl_basic_map_cow(bmap);
5740 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5741 j = isl_basic_map_alloc_equality(bmap);
5742 if (j < 0)
5743 goto error;
5744 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5745 isl_int_set_si(bmap->eq[j][pos], -1);
5746 isl_int_set(bmap->eq[j][0], value);
5747 bmap = isl_basic_map_simplify(bmap);
5748 return isl_basic_map_finalize(bmap);
5749 error:
5750 isl_basic_map_free(bmap);
5751 return NULL;
5754 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5755 enum isl_dim_type type, unsigned pos, int value)
5757 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5758 return isl_basic_map_free(bmap);
5759 return isl_basic_map_fix_pos_si(bmap,
5760 isl_basic_map_offset(bmap, type) + pos, value);
5763 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5764 enum isl_dim_type type, unsigned pos, isl_int value)
5766 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5767 return isl_basic_map_free(bmap);
5768 return isl_basic_map_fix_pos(bmap,
5769 isl_basic_map_offset(bmap, type) + pos, value);
5772 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5773 * to be equal to "v".
5775 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5776 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5778 if (!bmap || !v)
5779 goto error;
5780 if (!isl_val_is_int(v))
5781 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5782 "expecting integer value", goto error);
5783 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5784 goto error;
5785 pos += isl_basic_map_offset(bmap, type);
5786 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5787 isl_val_free(v);
5788 return bmap;
5789 error:
5790 isl_basic_map_free(bmap);
5791 isl_val_free(v);
5792 return NULL;
5795 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5796 * to be equal to "v".
5798 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5799 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5801 return isl_basic_map_fix_val(bset, type, pos, v);
5804 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5805 enum isl_dim_type type, unsigned pos, int value)
5807 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5808 type, pos, value));
5811 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5812 enum isl_dim_type type, unsigned pos, isl_int value)
5814 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5815 type, pos, value));
5818 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5819 unsigned input, int value)
5821 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5824 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5825 unsigned dim, int value)
5827 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5828 isl_dim_set, dim, value));
5831 static int remove_if_empty(__isl_keep isl_map *map, int i)
5833 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5835 if (empty < 0)
5836 return -1;
5837 if (!empty)
5838 return 0;
5840 isl_basic_map_free(map->p[i]);
5841 if (i != map->n - 1) {
5842 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5843 map->p[i] = map->p[map->n - 1];
5845 map->n--;
5847 return 0;
5850 /* Perform "fn" on each basic map of "map", where we may not be holding
5851 * the only reference to "map".
5852 * In particular, "fn" should be a semantics preserving operation
5853 * that we want to apply to all copies of "map". We therefore need
5854 * to be careful not to modify "map" in a way that breaks "map"
5855 * in case anything goes wrong.
5857 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5858 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5860 struct isl_basic_map *bmap;
5861 int i;
5863 if (!map)
5864 return NULL;
5866 for (i = map->n - 1; i >= 0; --i) {
5867 bmap = isl_basic_map_copy(map->p[i]);
5868 bmap = fn(bmap);
5869 if (!bmap)
5870 goto error;
5871 isl_basic_map_free(map->p[i]);
5872 map->p[i] = bmap;
5873 if (remove_if_empty(map, i) < 0)
5874 goto error;
5877 return map;
5878 error:
5879 isl_map_free(map);
5880 return NULL;
5883 struct isl_map *isl_map_fix_si(struct isl_map *map,
5884 enum isl_dim_type type, unsigned pos, int value)
5886 int i;
5888 map = isl_map_cow(map);
5889 if (!map)
5890 return NULL;
5892 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5893 for (i = map->n - 1; i >= 0; --i) {
5894 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5895 if (remove_if_empty(map, i) < 0)
5896 goto error;
5898 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5899 return map;
5900 error:
5901 isl_map_free(map);
5902 return NULL;
5905 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5906 enum isl_dim_type type, unsigned pos, int value)
5908 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
5911 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5912 enum isl_dim_type type, unsigned pos, isl_int value)
5914 int i;
5916 map = isl_map_cow(map);
5917 if (!map)
5918 return NULL;
5920 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5921 for (i = 0; i < map->n; ++i) {
5922 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5923 if (!map->p[i])
5924 goto error;
5926 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5927 return map;
5928 error:
5929 isl_map_free(map);
5930 return NULL;
5933 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5934 enum isl_dim_type type, unsigned pos, isl_int value)
5936 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
5939 /* Fix the value of the variable at position "pos" of type "type" of "map"
5940 * to be equal to "v".
5942 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5943 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5945 int i;
5947 map = isl_map_cow(map);
5948 if (!map || !v)
5949 goto error;
5951 if (!isl_val_is_int(v))
5952 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5953 "expecting integer value", goto error);
5954 if (pos >= isl_map_dim(map, type))
5955 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5956 "index out of bounds", goto error);
5957 for (i = map->n - 1; i >= 0; --i) {
5958 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5959 isl_val_copy(v));
5960 if (remove_if_empty(map, i) < 0)
5961 goto error;
5963 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5964 isl_val_free(v);
5965 return map;
5966 error:
5967 isl_map_free(map);
5968 isl_val_free(v);
5969 return NULL;
5972 /* Fix the value of the variable at position "pos" of type "type" of "set"
5973 * to be equal to "v".
5975 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5976 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5978 return isl_map_fix_val(set, type, pos, v);
5981 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5982 unsigned input, int value)
5984 return isl_map_fix_si(map, isl_dim_in, input, value);
5987 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5989 return set_from_map(isl_map_fix_si(set_to_map(set),
5990 isl_dim_set, dim, value));
5993 static __isl_give isl_basic_map *basic_map_bound_si(
5994 __isl_take isl_basic_map *bmap,
5995 enum isl_dim_type type, unsigned pos, int value, int upper)
5997 int j;
5999 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6000 return isl_basic_map_free(bmap);
6001 pos += isl_basic_map_offset(bmap, type);
6002 bmap = isl_basic_map_cow(bmap);
6003 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6004 j = isl_basic_map_alloc_inequality(bmap);
6005 if (j < 0)
6006 goto error;
6007 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6008 if (upper) {
6009 isl_int_set_si(bmap->ineq[j][pos], -1);
6010 isl_int_set_si(bmap->ineq[j][0], value);
6011 } else {
6012 isl_int_set_si(bmap->ineq[j][pos], 1);
6013 isl_int_set_si(bmap->ineq[j][0], -value);
6015 bmap = isl_basic_map_simplify(bmap);
6016 return isl_basic_map_finalize(bmap);
6017 error:
6018 isl_basic_map_free(bmap);
6019 return NULL;
6022 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6023 __isl_take isl_basic_map *bmap,
6024 enum isl_dim_type type, unsigned pos, int value)
6026 return basic_map_bound_si(bmap, type, pos, value, 0);
6029 /* Constrain the values of the given dimension to be no greater than "value".
6031 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6032 __isl_take isl_basic_map *bmap,
6033 enum isl_dim_type type, unsigned pos, int value)
6035 return basic_map_bound_si(bmap, type, pos, value, 1);
6038 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6039 enum isl_dim_type type, unsigned pos, int value, int upper)
6041 int i;
6043 map = isl_map_cow(map);
6044 if (!map)
6045 return NULL;
6047 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6048 for (i = 0; i < map->n; ++i) {
6049 map->p[i] = basic_map_bound_si(map->p[i],
6050 type, pos, value, upper);
6051 if (!map->p[i])
6052 goto error;
6054 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6055 return map;
6056 error:
6057 isl_map_free(map);
6058 return NULL;
6061 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6062 enum isl_dim_type type, unsigned pos, int value)
6064 return map_bound_si(map, type, pos, value, 0);
6067 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6068 enum isl_dim_type type, unsigned pos, int value)
6070 return map_bound_si(map, type, pos, value, 1);
6073 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6074 enum isl_dim_type type, unsigned pos, int value)
6076 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6077 type, pos, value));
6080 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6081 enum isl_dim_type type, unsigned pos, int value)
6083 return isl_map_upper_bound_si(set, type, pos, value);
6086 /* Bound the given variable of "bmap" from below (or above is "upper"
6087 * is set) to "value".
6089 static __isl_give isl_basic_map *basic_map_bound(
6090 __isl_take isl_basic_map *bmap,
6091 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6093 int j;
6095 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6096 return isl_basic_map_free(bmap);
6097 pos += isl_basic_map_offset(bmap, type);
6098 bmap = isl_basic_map_cow(bmap);
6099 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6100 j = isl_basic_map_alloc_inequality(bmap);
6101 if (j < 0)
6102 goto error;
6103 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6104 if (upper) {
6105 isl_int_set_si(bmap->ineq[j][pos], -1);
6106 isl_int_set(bmap->ineq[j][0], value);
6107 } else {
6108 isl_int_set_si(bmap->ineq[j][pos], 1);
6109 isl_int_neg(bmap->ineq[j][0], value);
6111 bmap = isl_basic_map_simplify(bmap);
6112 return isl_basic_map_finalize(bmap);
6113 error:
6114 isl_basic_map_free(bmap);
6115 return NULL;
6118 /* Bound the given variable of "map" from below (or above is "upper"
6119 * is set) to "value".
6121 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6122 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6124 int i;
6126 map = isl_map_cow(map);
6127 if (!map)
6128 return NULL;
6130 if (pos >= isl_map_dim(map, type))
6131 isl_die(map->ctx, isl_error_invalid,
6132 "index out of bounds", goto error);
6133 for (i = map->n - 1; i >= 0; --i) {
6134 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6135 if (remove_if_empty(map, i) < 0)
6136 goto error;
6138 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6139 return map;
6140 error:
6141 isl_map_free(map);
6142 return NULL;
6145 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6146 enum isl_dim_type type, unsigned pos, isl_int value)
6148 return map_bound(map, type, pos, value, 0);
6151 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6152 enum isl_dim_type type, unsigned pos, isl_int value)
6154 return map_bound(map, type, pos, value, 1);
6157 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6158 enum isl_dim_type type, unsigned pos, isl_int value)
6160 return isl_map_lower_bound(set, type, pos, value);
6163 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6164 enum isl_dim_type type, unsigned pos, isl_int value)
6166 return isl_map_upper_bound(set, type, pos, value);
6169 /* Force the values of the variable at position "pos" of type "type" of "set"
6170 * to be no smaller than "value".
6172 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6173 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6175 if (!value)
6176 goto error;
6177 if (!isl_val_is_int(value))
6178 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6179 "expecting integer value", goto error);
6180 set = isl_set_lower_bound(set, type, pos, value->n);
6181 isl_val_free(value);
6182 return set;
6183 error:
6184 isl_val_free(value);
6185 isl_set_free(set);
6186 return NULL;
6189 /* Force the values of the variable at position "pos" of type "type" of "set"
6190 * to be no greater than "value".
6192 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6193 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6195 if (!value)
6196 goto error;
6197 if (!isl_val_is_int(value))
6198 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6199 "expecting integer value", goto error);
6200 set = isl_set_upper_bound(set, type, pos, value->n);
6201 isl_val_free(value);
6202 return set;
6203 error:
6204 isl_val_free(value);
6205 isl_set_free(set);
6206 return NULL;
6209 struct isl_map *isl_map_reverse(struct isl_map *map)
6211 int i;
6213 map = isl_map_cow(map);
6214 if (!map)
6215 return NULL;
6217 map->dim = isl_space_reverse(map->dim);
6218 if (!map->dim)
6219 goto error;
6220 for (i = 0; i < map->n; ++i) {
6221 map->p[i] = isl_basic_map_reverse(map->p[i]);
6222 if (!map->p[i])
6223 goto error;
6225 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6226 return map;
6227 error:
6228 isl_map_free(map);
6229 return NULL;
6232 #undef TYPE
6233 #define TYPE isl_pw_multi_aff
6234 #undef SUFFIX
6235 #define SUFFIX _pw_multi_aff
6236 #undef EMPTY
6237 #define EMPTY isl_pw_multi_aff_empty
6238 #undef ADD
6239 #define ADD isl_pw_multi_aff_union_add
6240 #include "isl_map_lexopt_templ.c"
6242 /* Given a map "map", compute the lexicographically minimal
6243 * (or maximal) image element for each domain element in dom,
6244 * in the form of an isl_pw_multi_aff.
6245 * If "empty" is not NULL, then set *empty to those elements in dom that
6246 * do not have an image element.
6247 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6248 * should be computed over the domain of "map". "empty" is also NULL
6249 * in this case.
6251 * We first compute the lexicographically minimal or maximal element
6252 * in the first basic map. This results in a partial solution "res"
6253 * and a subset "todo" of dom that still need to be handled.
6254 * We then consider each of the remaining maps in "map" and successively
6255 * update both "res" and "todo".
6256 * If "empty" is NULL, then the todo sets are not needed and therefore
6257 * also not computed.
6259 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6260 __isl_take isl_map *map, __isl_take isl_set *dom,
6261 __isl_give isl_set **empty, unsigned flags)
6263 int i;
6264 int full;
6265 isl_pw_multi_aff *res;
6266 isl_set *todo;
6268 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6269 if (!map || (!full && !dom))
6270 goto error;
6272 if (isl_map_plain_is_empty(map)) {
6273 if (empty)
6274 *empty = dom;
6275 else
6276 isl_set_free(dom);
6277 return isl_pw_multi_aff_from_map(map);
6280 res = basic_map_partial_lexopt_pw_multi_aff(
6281 isl_basic_map_copy(map->p[0]),
6282 isl_set_copy(dom), empty, flags);
6284 if (empty)
6285 todo = *empty;
6286 for (i = 1; i < map->n; ++i) {
6287 isl_pw_multi_aff *res_i;
6289 res_i = basic_map_partial_lexopt_pw_multi_aff(
6290 isl_basic_map_copy(map->p[i]),
6291 isl_set_copy(dom), empty, flags);
6293 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6294 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6295 else
6296 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6298 if (empty)
6299 todo = isl_set_intersect(todo, *empty);
6302 isl_set_free(dom);
6303 isl_map_free(map);
6305 if (empty)
6306 *empty = todo;
6308 return res;
6309 error:
6310 if (empty)
6311 *empty = NULL;
6312 isl_set_free(dom);
6313 isl_map_free(map);
6314 return NULL;
6317 #undef TYPE
6318 #define TYPE isl_map
6319 #undef SUFFIX
6320 #define SUFFIX
6321 #undef EMPTY
6322 #define EMPTY isl_map_empty
6323 #undef ADD
6324 #define ADD isl_map_union_disjoint
6325 #include "isl_map_lexopt_templ.c"
6327 /* Given a map "map", compute the lexicographically minimal
6328 * (or maximal) image element for each domain element in "dom",
6329 * in the form of an isl_map.
6330 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6331 * do not have an image element.
6332 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6333 * should be computed over the domain of "map". "empty" is also NULL
6334 * in this case.
6336 * If the input consists of more than one disjunct, then first
6337 * compute the desired result in the form of an isl_pw_multi_aff and
6338 * then convert that into an isl_map.
6340 * This function used to have an explicit implementation in terms
6341 * of isl_maps, but it would continually intersect the domains of
6342 * partial results with the complement of the domain of the next
6343 * partial solution, potentially leading to an explosion in the number
6344 * of disjuncts if there are several disjuncts in the input.
6345 * An even earlier implementation of this function would look for
6346 * better results in the domain of the partial result and for extra
6347 * results in the complement of this domain, which would lead to
6348 * even more splintering.
6350 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6351 __isl_take isl_map *map, __isl_take isl_set *dom,
6352 __isl_give isl_set **empty, unsigned flags)
6354 int full;
6355 struct isl_map *res;
6356 isl_pw_multi_aff *pma;
6358 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6359 if (!map || (!full && !dom))
6360 goto error;
6362 if (isl_map_plain_is_empty(map)) {
6363 if (empty)
6364 *empty = dom;
6365 else
6366 isl_set_free(dom);
6367 return map;
6370 if (map->n == 1) {
6371 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6372 dom, empty, flags);
6373 isl_map_free(map);
6374 return res;
6377 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6378 flags);
6379 return isl_map_from_pw_multi_aff(pma);
6380 error:
6381 if (empty)
6382 *empty = NULL;
6383 isl_set_free(dom);
6384 isl_map_free(map);
6385 return NULL;
6388 __isl_give isl_map *isl_map_partial_lexmax(
6389 __isl_take isl_map *map, __isl_take isl_set *dom,
6390 __isl_give isl_set **empty)
6392 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6395 __isl_give isl_map *isl_map_partial_lexmin(
6396 __isl_take isl_map *map, __isl_take isl_set *dom,
6397 __isl_give isl_set **empty)
6399 return isl_map_partial_lexopt(map, dom, empty, 0);
6402 __isl_give isl_set *isl_set_partial_lexmin(
6403 __isl_take isl_set *set, __isl_take isl_set *dom,
6404 __isl_give isl_set **empty)
6406 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6407 dom, empty));
6410 __isl_give isl_set *isl_set_partial_lexmax(
6411 __isl_take isl_set *set, __isl_take isl_set *dom,
6412 __isl_give isl_set **empty)
6414 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6415 dom, empty));
6418 /* Compute the lexicographic minimum (or maximum if "flags" includes
6419 * ISL_OPT_MAX) of "bset" over its parametric domain.
6421 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6422 unsigned flags)
6424 return isl_basic_map_lexopt(bset, flags);
6427 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6429 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6432 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6434 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6437 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6439 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6442 /* Compute the lexicographic minimum of "bset" over its parametric domain
6443 * for the purpose of quantifier elimination.
6444 * That is, find an explicit representation for all the existentially
6445 * quantified variables in "bset" by computing their lexicographic
6446 * minimum.
6448 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6449 __isl_take isl_basic_set *bset)
6451 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6454 /* Extract the first and only affine expression from list
6455 * and then add it to *pwaff with the given dom.
6456 * This domain is known to be disjoint from other domains
6457 * because of the way isl_basic_map_foreach_lexmax works.
6459 static isl_stat update_dim_opt(__isl_take isl_basic_set *dom,
6460 __isl_take isl_aff_list *list, void *user)
6462 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6463 isl_aff *aff;
6464 isl_pw_aff **pwaff = user;
6465 isl_pw_aff *pwaff_i;
6467 if (!list)
6468 goto error;
6469 if (isl_aff_list_n_aff(list) != 1)
6470 isl_die(ctx, isl_error_internal,
6471 "expecting single element list", goto error);
6473 aff = isl_aff_list_get_aff(list, 0);
6474 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6476 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6478 isl_aff_list_free(list);
6480 return isl_stat_ok;
6481 error:
6482 isl_basic_set_free(dom);
6483 isl_aff_list_free(list);
6484 return isl_stat_error;
6487 /* Given a basic map with one output dimension, compute the minimum or
6488 * maximum of that dimension as an isl_pw_aff.
6490 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6491 * call update_dim_opt on each leaf of the result.
6493 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6494 int max)
6496 isl_space *dim = isl_basic_map_get_space(bmap);
6497 isl_pw_aff *pwaff;
6498 isl_stat r;
6500 dim = isl_space_from_domain(isl_space_domain(dim));
6501 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6502 pwaff = isl_pw_aff_empty(dim);
6504 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6505 if (r < 0)
6506 return isl_pw_aff_free(pwaff);
6508 return pwaff;
6511 /* Compute the minimum or maximum of the given output dimension
6512 * as a function of the parameters and the input dimensions,
6513 * but independently of the other output dimensions.
6515 * We first project out the other output dimension and then compute
6516 * the "lexicographic" maximum in each basic map, combining the results
6517 * using isl_pw_aff_union_max.
6519 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6520 int max)
6522 int i;
6523 isl_pw_aff *pwaff;
6524 unsigned n_out;
6526 n_out = isl_map_dim(map, isl_dim_out);
6527 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6528 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6529 if (!map)
6530 return NULL;
6532 if (map->n == 0) {
6533 isl_space *dim = isl_map_get_space(map);
6534 isl_map_free(map);
6535 return isl_pw_aff_empty(dim);
6538 pwaff = basic_map_dim_opt(map->p[0], max);
6539 for (i = 1; i < map->n; ++i) {
6540 isl_pw_aff *pwaff_i;
6542 pwaff_i = basic_map_dim_opt(map->p[i], max);
6543 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6546 isl_map_free(map);
6548 return pwaff;
6551 /* Compute the minimum of the given output dimension as a function of the
6552 * parameters and input dimensions, but independently of
6553 * the other output dimensions.
6555 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6557 return map_dim_opt(map, pos, 0);
6560 /* Compute the maximum of the given output dimension as a function of the
6561 * parameters and input dimensions, but independently of
6562 * the other output dimensions.
6564 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6566 return map_dim_opt(map, pos, 1);
6569 /* Compute the minimum or maximum of the given set dimension
6570 * as a function of the parameters,
6571 * but independently of the other set dimensions.
6573 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6574 int max)
6576 return map_dim_opt(set, pos, max);
6579 /* Compute the maximum of the given set dimension as a function of the
6580 * parameters, but independently of the other set dimensions.
6582 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6584 return set_dim_opt(set, pos, 1);
6587 /* Compute the minimum of the given set dimension as a function of the
6588 * parameters, but independently of the other set dimensions.
6590 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6592 return set_dim_opt(set, pos, 0);
6595 /* Apply a preimage specified by "mat" on the parameters of "bset".
6596 * bset is assumed to have only parameters and divs.
6598 static struct isl_basic_set *basic_set_parameter_preimage(
6599 struct isl_basic_set *bset, struct isl_mat *mat)
6601 unsigned nparam;
6603 if (!bset || !mat)
6604 goto error;
6606 bset->dim = isl_space_cow(bset->dim);
6607 if (!bset->dim)
6608 goto error;
6610 nparam = isl_basic_set_dim(bset, isl_dim_param);
6612 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6614 bset->dim->nparam = 0;
6615 bset->dim->n_out = nparam;
6616 bset = isl_basic_set_preimage(bset, mat);
6617 if (bset) {
6618 bset->dim->nparam = bset->dim->n_out;
6619 bset->dim->n_out = 0;
6621 return bset;
6622 error:
6623 isl_mat_free(mat);
6624 isl_basic_set_free(bset);
6625 return NULL;
6628 /* Apply a preimage specified by "mat" on the parameters of "set".
6629 * set is assumed to have only parameters and divs.
6631 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6632 __isl_take isl_mat *mat)
6634 isl_space *space;
6635 unsigned nparam;
6637 if (!set || !mat)
6638 goto error;
6640 nparam = isl_set_dim(set, isl_dim_param);
6642 if (mat->n_row != 1 + nparam)
6643 isl_die(isl_set_get_ctx(set), isl_error_internal,
6644 "unexpected number of rows", goto error);
6646 space = isl_set_get_space(set);
6647 space = isl_space_move_dims(space, isl_dim_set, 0,
6648 isl_dim_param, 0, nparam);
6649 set = isl_set_reset_space(set, space);
6650 set = isl_set_preimage(set, mat);
6651 nparam = isl_set_dim(set, isl_dim_out);
6652 space = isl_set_get_space(set);
6653 space = isl_space_move_dims(space, isl_dim_param, 0,
6654 isl_dim_out, 0, nparam);
6655 set = isl_set_reset_space(set, space);
6656 return set;
6657 error:
6658 isl_mat_free(mat);
6659 isl_set_free(set);
6660 return NULL;
6663 /* Intersect the basic set "bset" with the affine space specified by the
6664 * equalities in "eq".
6666 static struct isl_basic_set *basic_set_append_equalities(
6667 struct isl_basic_set *bset, struct isl_mat *eq)
6669 int i, k;
6670 unsigned len;
6672 if (!bset || !eq)
6673 goto error;
6675 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6676 eq->n_row, 0);
6677 if (!bset)
6678 goto error;
6680 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6681 for (i = 0; i < eq->n_row; ++i) {
6682 k = isl_basic_set_alloc_equality(bset);
6683 if (k < 0)
6684 goto error;
6685 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6686 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6688 isl_mat_free(eq);
6690 bset = isl_basic_set_gauss(bset, NULL);
6691 bset = isl_basic_set_finalize(bset);
6693 return bset;
6694 error:
6695 isl_mat_free(eq);
6696 isl_basic_set_free(bset);
6697 return NULL;
6700 /* Intersect the set "set" with the affine space specified by the
6701 * equalities in "eq".
6703 static struct isl_set *set_append_equalities(struct isl_set *set,
6704 struct isl_mat *eq)
6706 int i;
6708 if (!set || !eq)
6709 goto error;
6711 for (i = 0; i < set->n; ++i) {
6712 set->p[i] = basic_set_append_equalities(set->p[i],
6713 isl_mat_copy(eq));
6714 if (!set->p[i])
6715 goto error;
6717 isl_mat_free(eq);
6718 return set;
6719 error:
6720 isl_mat_free(eq);
6721 isl_set_free(set);
6722 return NULL;
6725 /* Given a basic set "bset" that only involves parameters and existentially
6726 * quantified variables, return the index of the first equality
6727 * that only involves parameters. If there is no such equality then
6728 * return bset->n_eq.
6730 * This function assumes that isl_basic_set_gauss has been called on "bset".
6732 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6734 int i, j;
6735 unsigned nparam, n_div;
6737 if (!bset)
6738 return -1;
6740 nparam = isl_basic_set_dim(bset, isl_dim_param);
6741 n_div = isl_basic_set_dim(bset, isl_dim_div);
6743 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6744 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6745 ++i;
6748 return i;
6751 /* Compute an explicit representation for the existentially quantified
6752 * variables in "bset" by computing the "minimal value" of the set
6753 * variables. Since there are no set variables, the computation of
6754 * the minimal value essentially computes an explicit representation
6755 * of the non-empty part(s) of "bset".
6757 * The input only involves parameters and existentially quantified variables.
6758 * All equalities among parameters have been removed.
6760 * Since the existentially quantified variables in the result are in general
6761 * going to be different from those in the input, we first replace
6762 * them by the minimal number of variables based on their equalities.
6763 * This should simplify the parametric integer programming.
6765 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6767 isl_morph *morph1, *morph2;
6768 isl_set *set;
6769 unsigned n;
6771 if (!bset)
6772 return NULL;
6773 if (bset->n_eq == 0)
6774 return isl_basic_set_lexmin_compute_divs(bset);
6776 morph1 = isl_basic_set_parameter_compression(bset);
6777 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6778 bset = isl_basic_set_lift(bset);
6779 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6780 bset = isl_morph_basic_set(morph2, bset);
6781 n = isl_basic_set_dim(bset, isl_dim_set);
6782 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6784 set = isl_basic_set_lexmin_compute_divs(bset);
6786 set = isl_morph_set(isl_morph_inverse(morph1), set);
6788 return set;
6791 /* Project the given basic set onto its parameter domain, possibly introducing
6792 * new, explicit, existential variables in the constraints.
6793 * The input has parameters and (possibly implicit) existential variables.
6794 * The output has the same parameters, but only
6795 * explicit existentially quantified variables.
6797 * The actual projection is performed by pip, but pip doesn't seem
6798 * to like equalities very much, so we first remove the equalities
6799 * among the parameters by performing a variable compression on
6800 * the parameters. Afterward, an inverse transformation is performed
6801 * and the equalities among the parameters are inserted back in.
6803 * The variable compression on the parameters may uncover additional
6804 * equalities that were only implicit before. We therefore check
6805 * if there are any new parameter equalities in the result and
6806 * if so recurse. The removal of parameter equalities is required
6807 * for the parameter compression performed by base_compute_divs.
6809 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6811 int i;
6812 struct isl_mat *eq;
6813 struct isl_mat *T, *T2;
6814 struct isl_set *set;
6815 unsigned nparam;
6817 bset = isl_basic_set_cow(bset);
6818 if (!bset)
6819 return NULL;
6821 if (bset->n_eq == 0)
6822 return base_compute_divs(bset);
6824 bset = isl_basic_set_gauss(bset, NULL);
6825 if (!bset)
6826 return NULL;
6827 if (isl_basic_set_plain_is_empty(bset))
6828 return isl_set_from_basic_set(bset);
6830 i = first_parameter_equality(bset);
6831 if (i == bset->n_eq)
6832 return base_compute_divs(bset);
6834 nparam = isl_basic_set_dim(bset, isl_dim_param);
6835 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6836 0, 1 + nparam);
6837 eq = isl_mat_cow(eq);
6838 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6839 if (T && T->n_col == 0) {
6840 isl_mat_free(T);
6841 isl_mat_free(T2);
6842 isl_mat_free(eq);
6843 bset = isl_basic_set_set_to_empty(bset);
6844 return isl_set_from_basic_set(bset);
6846 bset = basic_set_parameter_preimage(bset, T);
6848 i = first_parameter_equality(bset);
6849 if (!bset)
6850 set = NULL;
6851 else if (i == bset->n_eq)
6852 set = base_compute_divs(bset);
6853 else
6854 set = parameter_compute_divs(bset);
6855 set = set_parameter_preimage(set, T2);
6856 set = set_append_equalities(set, eq);
6857 return set;
6860 /* Insert the divs from "ls" before those of "bmap".
6862 * The number of columns is not changed, which means that the last
6863 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6864 * The caller is responsible for removing the same number of dimensions
6865 * from the space of "bmap".
6867 static __isl_give isl_basic_map *insert_divs_from_local_space(
6868 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6870 int i;
6871 int n_div;
6872 int old_n_div;
6874 n_div = isl_local_space_dim(ls, isl_dim_div);
6875 if (n_div == 0)
6876 return bmap;
6878 old_n_div = bmap->n_div;
6879 bmap = insert_div_rows(bmap, n_div);
6880 if (!bmap)
6881 return NULL;
6883 for (i = 0; i < n_div; ++i) {
6884 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6885 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6888 return bmap;
6891 /* Replace the space of "bmap" by the space and divs of "ls".
6893 * If "ls" has any divs, then we simplify the result since we may
6894 * have discovered some additional equalities that could simplify
6895 * the div expressions.
6897 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6898 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6900 int n_div;
6902 bmap = isl_basic_map_cow(bmap);
6903 if (!bmap || !ls)
6904 goto error;
6906 n_div = isl_local_space_dim(ls, isl_dim_div);
6907 bmap = insert_divs_from_local_space(bmap, ls);
6908 if (!bmap)
6909 goto error;
6911 isl_space_free(bmap->dim);
6912 bmap->dim = isl_local_space_get_space(ls);
6913 if (!bmap->dim)
6914 goto error;
6916 isl_local_space_free(ls);
6917 if (n_div > 0)
6918 bmap = isl_basic_map_simplify(bmap);
6919 bmap = isl_basic_map_finalize(bmap);
6920 return bmap;
6921 error:
6922 isl_basic_map_free(bmap);
6923 isl_local_space_free(ls);
6924 return NULL;
6927 /* Replace the space of "map" by the space and divs of "ls".
6929 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6930 __isl_take isl_local_space *ls)
6932 int i;
6934 map = isl_map_cow(map);
6935 if (!map || !ls)
6936 goto error;
6938 for (i = 0; i < map->n; ++i) {
6939 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6940 isl_local_space_copy(ls));
6941 if (!map->p[i])
6942 goto error;
6944 isl_space_free(map->dim);
6945 map->dim = isl_local_space_get_space(ls);
6946 if (!map->dim)
6947 goto error;
6949 isl_local_space_free(ls);
6950 return map;
6951 error:
6952 isl_local_space_free(ls);
6953 isl_map_free(map);
6954 return NULL;
6957 /* Compute an explicit representation for the existentially
6958 * quantified variables for which do not know any explicit representation yet.
6960 * We first sort the existentially quantified variables so that the
6961 * existentially quantified variables for which we already have an explicit
6962 * representation are placed before those for which we do not.
6963 * The input dimensions, the output dimensions and the existentially
6964 * quantified variables for which we already have an explicit
6965 * representation are then turned into parameters.
6966 * compute_divs returns a map with the same parameters and
6967 * no input or output dimensions and the dimension specification
6968 * is reset to that of the input, including the existentially quantified
6969 * variables for which we already had an explicit representation.
6971 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6973 struct isl_basic_set *bset;
6974 struct isl_set *set;
6975 struct isl_map *map;
6976 isl_space *dim;
6977 isl_local_space *ls;
6978 unsigned nparam;
6979 unsigned n_in;
6980 unsigned n_out;
6981 int n_known;
6982 int i;
6984 bmap = isl_basic_map_sort_divs(bmap);
6985 bmap = isl_basic_map_cow(bmap);
6986 if (!bmap)
6987 return NULL;
6989 n_known = isl_basic_map_first_unknown_div(bmap);
6990 if (n_known < 0)
6991 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6993 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6994 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6995 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6996 dim = isl_space_set_alloc(bmap->ctx,
6997 nparam + n_in + n_out + n_known, 0);
6998 if (!dim)
6999 goto error;
7001 ls = isl_basic_map_get_local_space(bmap);
7002 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7003 n_known, bmap->n_div - n_known);
7004 if (n_known > 0) {
7005 for (i = n_known; i < bmap->n_div; ++i)
7006 swap_div(bmap, i - n_known, i);
7007 bmap->n_div -= n_known;
7008 bmap->extra -= n_known;
7010 bmap = isl_basic_map_reset_space(bmap, dim);
7011 bset = bset_from_bmap(bmap);
7013 set = parameter_compute_divs(bset);
7014 map = set_to_map(set);
7015 map = replace_space_by_local_space(map, ls);
7017 return map;
7018 error:
7019 isl_basic_map_free(bmap);
7020 return NULL;
7023 /* Remove the explicit representation of local variable "div",
7024 * if there is any.
7026 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7027 __isl_take isl_basic_map *bmap, int div)
7029 isl_bool unknown;
7031 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7032 if (unknown < 0)
7033 return isl_basic_map_free(bmap);
7034 if (unknown)
7035 return bmap;
7037 bmap = isl_basic_map_cow(bmap);
7038 if (!bmap)
7039 return NULL;
7040 isl_int_set_si(bmap->div[div][0], 0);
7041 return bmap;
7044 /* Is local variable "div" of "bmap" marked as not having an explicit
7045 * representation?
7046 * Note that even if "div" is not marked in this way and therefore
7047 * has an explicit representation, this representation may still
7048 * depend (indirectly) on other local variables that do not
7049 * have an explicit representation.
7051 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7052 int div)
7054 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7055 return isl_bool_error;
7056 return isl_int_is_zero(bmap->div[div][0]);
7059 /* Return the position of the first local variable that does not
7060 * have an explicit representation.
7061 * Return the total number of local variables if they all have
7062 * an explicit representation.
7063 * Return -1 on error.
7065 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7067 int i;
7069 if (!bmap)
7070 return -1;
7072 for (i = 0; i < bmap->n_div; ++i) {
7073 if (!isl_basic_map_div_is_known(bmap, i))
7074 return i;
7076 return bmap->n_div;
7079 /* Return the position of the first local variable that does not
7080 * have an explicit representation.
7081 * Return the total number of local variables if they all have
7082 * an explicit representation.
7083 * Return -1 on error.
7085 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7087 return isl_basic_map_first_unknown_div(bset);
7090 /* Does "bmap" have an explicit representation for all local variables?
7092 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7094 int first, n;
7096 n = isl_basic_map_dim(bmap, isl_dim_div);
7097 first = isl_basic_map_first_unknown_div(bmap);
7098 if (first < 0)
7099 return isl_bool_error;
7100 return first == n;
7103 /* Do all basic maps in "map" have an explicit representation
7104 * for all local variables?
7106 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7108 int i;
7110 if (!map)
7111 return isl_bool_error;
7113 for (i = 0; i < map->n; ++i) {
7114 int known = isl_basic_map_divs_known(map->p[i]);
7115 if (known <= 0)
7116 return known;
7119 return isl_bool_true;
7122 /* If bmap contains any unknown divs, then compute explicit
7123 * expressions for them. However, this computation may be
7124 * quite expensive, so first try to remove divs that aren't
7125 * strictly needed.
7127 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7129 int known;
7130 struct isl_map *map;
7132 known = isl_basic_map_divs_known(bmap);
7133 if (known < 0)
7134 goto error;
7135 if (known)
7136 return isl_map_from_basic_map(bmap);
7138 bmap = isl_basic_map_drop_redundant_divs(bmap);
7140 known = isl_basic_map_divs_known(bmap);
7141 if (known < 0)
7142 goto error;
7143 if (known)
7144 return isl_map_from_basic_map(bmap);
7146 map = compute_divs(bmap);
7147 return map;
7148 error:
7149 isl_basic_map_free(bmap);
7150 return NULL;
7153 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7155 int i;
7156 int known;
7157 struct isl_map *res;
7159 if (!map)
7160 return NULL;
7161 if (map->n == 0)
7162 return map;
7164 known = isl_map_divs_known(map);
7165 if (known < 0) {
7166 isl_map_free(map);
7167 return NULL;
7169 if (known)
7170 return map;
7172 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7173 for (i = 1 ; i < map->n; ++i) {
7174 struct isl_map *r2;
7175 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7176 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7177 res = isl_map_union_disjoint(res, r2);
7178 else
7179 res = isl_map_union(res, r2);
7181 isl_map_free(map);
7183 return res;
7186 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7188 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7191 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7193 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7196 struct isl_set *isl_map_domain(struct isl_map *map)
7198 int i;
7199 struct isl_set *set;
7201 if (!map)
7202 goto error;
7204 map = isl_map_cow(map);
7205 if (!map)
7206 return NULL;
7208 set = set_from_map(map);
7209 set->dim = isl_space_domain(set->dim);
7210 if (!set->dim)
7211 goto error;
7212 for (i = 0; i < map->n; ++i) {
7213 set->p[i] = isl_basic_map_domain(map->p[i]);
7214 if (!set->p[i])
7215 goto error;
7217 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7218 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7219 return set;
7220 error:
7221 isl_map_free(map);
7222 return NULL;
7225 /* Return the union of "map1" and "map2", where we assume for now that
7226 * "map1" and "map2" are disjoint. Note that the basic maps inside
7227 * "map1" or "map2" may not be disjoint from each other.
7228 * Also note that this function is also called from isl_map_union,
7229 * which takes care of handling the situation where "map1" and "map2"
7230 * may not be disjoint.
7232 * If one of the inputs is empty, we can simply return the other input.
7233 * Similarly, if one of the inputs is universal, then it is equal to the union.
7235 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7236 __isl_take isl_map *map2)
7238 int i;
7239 unsigned flags = 0;
7240 struct isl_map *map = NULL;
7241 int is_universe;
7243 if (!map1 || !map2)
7244 goto error;
7246 if (!isl_space_is_equal(map1->dim, map2->dim))
7247 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7248 "spaces don't match", goto error);
7250 if (map1->n == 0) {
7251 isl_map_free(map1);
7252 return map2;
7254 if (map2->n == 0) {
7255 isl_map_free(map2);
7256 return map1;
7259 is_universe = isl_map_plain_is_universe(map1);
7260 if (is_universe < 0)
7261 goto error;
7262 if (is_universe) {
7263 isl_map_free(map2);
7264 return map1;
7267 is_universe = isl_map_plain_is_universe(map2);
7268 if (is_universe < 0)
7269 goto error;
7270 if (is_universe) {
7271 isl_map_free(map1);
7272 return map2;
7275 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7276 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7277 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7279 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7280 map1->n + map2->n, flags);
7281 if (!map)
7282 goto error;
7283 for (i = 0; i < map1->n; ++i) {
7284 map = isl_map_add_basic_map(map,
7285 isl_basic_map_copy(map1->p[i]));
7286 if (!map)
7287 goto error;
7289 for (i = 0; i < map2->n; ++i) {
7290 map = isl_map_add_basic_map(map,
7291 isl_basic_map_copy(map2->p[i]));
7292 if (!map)
7293 goto error;
7295 isl_map_free(map1);
7296 isl_map_free(map2);
7297 return map;
7298 error:
7299 isl_map_free(map);
7300 isl_map_free(map1);
7301 isl_map_free(map2);
7302 return NULL;
7305 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7306 * guaranteed to be disjoint by the caller.
7308 * Note that this functions is called from within isl_map_make_disjoint,
7309 * so we have to be careful not to touch the constraints of the inputs
7310 * in any way.
7312 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7313 __isl_take isl_map *map2)
7315 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7318 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7319 * not be disjoint. The parameters are assumed to have been aligned.
7321 * We currently simply call map_union_disjoint, the internal operation
7322 * of which does not really depend on the inputs being disjoint.
7323 * If the result contains more than one basic map, then we clear
7324 * the disjoint flag since the result may contain basic maps from
7325 * both inputs and these are not guaranteed to be disjoint.
7327 * As a special case, if "map1" and "map2" are obviously equal,
7328 * then we simply return "map1".
7330 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7331 __isl_take isl_map *map2)
7333 int equal;
7335 if (!map1 || !map2)
7336 goto error;
7338 equal = isl_map_plain_is_equal(map1, map2);
7339 if (equal < 0)
7340 goto error;
7341 if (equal) {
7342 isl_map_free(map2);
7343 return map1;
7346 map1 = map_union_disjoint(map1, map2);
7347 if (!map1)
7348 return NULL;
7349 if (map1->n > 1)
7350 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7351 return map1;
7352 error:
7353 isl_map_free(map1);
7354 isl_map_free(map2);
7355 return NULL;
7358 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7359 * not be disjoint.
7361 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7362 __isl_take isl_map *map2)
7364 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7367 struct isl_set *isl_set_union_disjoint(
7368 struct isl_set *set1, struct isl_set *set2)
7370 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7371 set_to_map(set2)));
7374 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7376 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7379 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7380 * the results.
7382 * "map" and "set" are assumed to be compatible and non-NULL.
7384 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7385 __isl_take isl_set *set,
7386 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7387 __isl_take isl_basic_set *bset))
7389 unsigned flags = 0;
7390 struct isl_map *result;
7391 int i, j;
7393 if (isl_set_plain_is_universe(set)) {
7394 isl_set_free(set);
7395 return map;
7398 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7399 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7400 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7402 result = isl_map_alloc_space(isl_space_copy(map->dim),
7403 map->n * set->n, flags);
7404 for (i = 0; result && i < map->n; ++i)
7405 for (j = 0; j < set->n; ++j) {
7406 result = isl_map_add_basic_map(result,
7407 fn(isl_basic_map_copy(map->p[i]),
7408 isl_basic_set_copy(set->p[j])));
7409 if (!result)
7410 break;
7413 isl_map_free(map);
7414 isl_set_free(set);
7415 return result;
7418 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7419 __isl_take isl_set *set)
7421 isl_bool ok;
7423 ok = isl_map_compatible_range(map, set);
7424 if (ok < 0)
7425 goto error;
7426 if (!ok)
7427 isl_die(set->ctx, isl_error_invalid,
7428 "incompatible spaces", goto error);
7430 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7431 error:
7432 isl_map_free(map);
7433 isl_set_free(set);
7434 return NULL;
7437 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7438 __isl_take isl_set *set)
7440 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7443 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7444 __isl_take isl_set *set)
7446 isl_bool ok;
7448 ok = isl_map_compatible_domain(map, set);
7449 if (ok < 0)
7450 goto error;
7451 if (!ok)
7452 isl_die(set->ctx, isl_error_invalid,
7453 "incompatible spaces", goto error);
7455 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7456 error:
7457 isl_map_free(map);
7458 isl_set_free(set);
7459 return NULL;
7462 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7463 __isl_take isl_set *set)
7465 return isl_map_align_params_map_map_and(map, set,
7466 &map_intersect_domain);
7469 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7470 __isl_take isl_map *map2)
7472 if (!map1 || !map2)
7473 goto error;
7474 map1 = isl_map_reverse(map1);
7475 map1 = isl_map_apply_range(map1, map2);
7476 return isl_map_reverse(map1);
7477 error:
7478 isl_map_free(map1);
7479 isl_map_free(map2);
7480 return NULL;
7483 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7484 __isl_take isl_map *map2)
7486 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7489 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7490 __isl_take isl_map *map2)
7492 isl_space *dim_result;
7493 struct isl_map *result;
7494 int i, j;
7496 if (!map1 || !map2)
7497 goto error;
7499 dim_result = isl_space_join(isl_space_copy(map1->dim),
7500 isl_space_copy(map2->dim));
7502 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7503 if (!result)
7504 goto error;
7505 for (i = 0; i < map1->n; ++i)
7506 for (j = 0; j < map2->n; ++j) {
7507 result = isl_map_add_basic_map(result,
7508 isl_basic_map_apply_range(
7509 isl_basic_map_copy(map1->p[i]),
7510 isl_basic_map_copy(map2->p[j])));
7511 if (!result)
7512 goto error;
7514 isl_map_free(map1);
7515 isl_map_free(map2);
7516 if (result && result->n <= 1)
7517 ISL_F_SET(result, ISL_MAP_DISJOINT);
7518 return result;
7519 error:
7520 isl_map_free(map1);
7521 isl_map_free(map2);
7522 return NULL;
7525 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7526 __isl_take isl_map *map2)
7528 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7532 * returns range - domain
7534 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7536 isl_space *target_space;
7537 struct isl_basic_set *bset;
7538 unsigned dim;
7539 unsigned nparam;
7540 int i;
7542 if (!bmap)
7543 goto error;
7544 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7545 bmap->dim, isl_dim_out),
7546 goto error);
7547 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7548 dim = isl_basic_map_n_in(bmap);
7549 nparam = isl_basic_map_n_param(bmap);
7550 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7551 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7552 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7553 for (i = 0; i < dim; ++i) {
7554 int j = isl_basic_map_alloc_equality(bmap);
7555 if (j < 0) {
7556 bmap = isl_basic_map_free(bmap);
7557 break;
7559 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7560 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7561 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7562 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7564 bset = isl_basic_map_domain(bmap);
7565 bset = isl_basic_set_reset_space(bset, target_space);
7566 return bset;
7567 error:
7568 isl_basic_map_free(bmap);
7569 return NULL;
7573 * returns range - domain
7575 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7577 int i;
7578 isl_space *dim;
7579 struct isl_set *result;
7581 if (!map)
7582 return NULL;
7584 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7585 map->dim, isl_dim_out),
7586 goto error);
7587 dim = isl_map_get_space(map);
7588 dim = isl_space_domain(dim);
7589 result = isl_set_alloc_space(dim, map->n, 0);
7590 if (!result)
7591 goto error;
7592 for (i = 0; i < map->n; ++i)
7593 result = isl_set_add_basic_set(result,
7594 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7595 isl_map_free(map);
7596 return result;
7597 error:
7598 isl_map_free(map);
7599 return NULL;
7603 * returns [domain -> range] -> range - domain
7605 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7606 __isl_take isl_basic_map *bmap)
7608 int i, k;
7609 isl_space *dim;
7610 isl_basic_map *domain;
7611 int nparam, n;
7612 unsigned total;
7614 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7615 bmap->dim, isl_dim_out))
7616 isl_die(bmap->ctx, isl_error_invalid,
7617 "domain and range don't match", goto error);
7619 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7620 n = isl_basic_map_dim(bmap, isl_dim_in);
7622 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7623 domain = isl_basic_map_universe(dim);
7625 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7626 bmap = isl_basic_map_apply_range(bmap, domain);
7627 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7629 total = isl_basic_map_total_dim(bmap);
7631 for (i = 0; i < n; ++i) {
7632 k = isl_basic_map_alloc_equality(bmap);
7633 if (k < 0)
7634 goto error;
7635 isl_seq_clr(bmap->eq[k], 1 + total);
7636 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7637 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7638 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7641 bmap = isl_basic_map_gauss(bmap, NULL);
7642 return isl_basic_map_finalize(bmap);
7643 error:
7644 isl_basic_map_free(bmap);
7645 return NULL;
7649 * returns [domain -> range] -> range - domain
7651 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7653 int i;
7654 isl_space *domain_dim;
7656 if (!map)
7657 return NULL;
7659 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7660 map->dim, isl_dim_out))
7661 isl_die(map->ctx, isl_error_invalid,
7662 "domain and range don't match", goto error);
7664 map = isl_map_cow(map);
7665 if (!map)
7666 return NULL;
7668 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7669 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7670 map->dim = isl_space_join(map->dim, domain_dim);
7671 if (!map->dim)
7672 goto error;
7673 for (i = 0; i < map->n; ++i) {
7674 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7675 if (!map->p[i])
7676 goto error;
7678 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7679 return map;
7680 error:
7681 isl_map_free(map);
7682 return NULL;
7685 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7687 struct isl_basic_map *bmap;
7688 unsigned nparam;
7689 unsigned dim;
7690 int i;
7692 if (!dims)
7693 return NULL;
7695 nparam = dims->nparam;
7696 dim = dims->n_out;
7697 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7698 if (!bmap)
7699 goto error;
7701 for (i = 0; i < dim; ++i) {
7702 int j = isl_basic_map_alloc_equality(bmap);
7703 if (j < 0)
7704 goto error;
7705 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7706 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7707 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7709 return isl_basic_map_finalize(bmap);
7710 error:
7711 isl_basic_map_free(bmap);
7712 return NULL;
7715 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7717 if (!dim)
7718 return NULL;
7719 if (dim->n_in != dim->n_out)
7720 isl_die(dim->ctx, isl_error_invalid,
7721 "number of input and output dimensions needs to be "
7722 "the same", goto error);
7723 return basic_map_identity(dim);
7724 error:
7725 isl_space_free(dim);
7726 return NULL;
7729 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7731 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7734 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7736 isl_space *dim = isl_set_get_space(set);
7737 isl_map *id;
7738 id = isl_map_identity(isl_space_map_from_set(dim));
7739 return isl_map_intersect_range(id, set);
7742 /* Construct a basic set with all set dimensions having only non-negative
7743 * values.
7745 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7746 __isl_take isl_space *space)
7748 int i;
7749 unsigned nparam;
7750 unsigned dim;
7751 struct isl_basic_set *bset;
7753 if (!space)
7754 return NULL;
7755 nparam = space->nparam;
7756 dim = space->n_out;
7757 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7758 if (!bset)
7759 return NULL;
7760 for (i = 0; i < dim; ++i) {
7761 int k = isl_basic_set_alloc_inequality(bset);
7762 if (k < 0)
7763 goto error;
7764 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7765 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7767 return bset;
7768 error:
7769 isl_basic_set_free(bset);
7770 return NULL;
7773 /* Construct the half-space x_pos >= 0.
7775 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7776 int pos)
7778 int k;
7779 isl_basic_set *nonneg;
7781 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7782 k = isl_basic_set_alloc_inequality(nonneg);
7783 if (k < 0)
7784 goto error;
7785 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7786 isl_int_set_si(nonneg->ineq[k][pos], 1);
7788 return isl_basic_set_finalize(nonneg);
7789 error:
7790 isl_basic_set_free(nonneg);
7791 return NULL;
7794 /* Construct the half-space x_pos <= -1.
7796 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7798 int k;
7799 isl_basic_set *neg;
7801 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7802 k = isl_basic_set_alloc_inequality(neg);
7803 if (k < 0)
7804 goto error;
7805 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7806 isl_int_set_si(neg->ineq[k][0], -1);
7807 isl_int_set_si(neg->ineq[k][pos], -1);
7809 return isl_basic_set_finalize(neg);
7810 error:
7811 isl_basic_set_free(neg);
7812 return NULL;
7815 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7816 enum isl_dim_type type, unsigned first, unsigned n)
7818 int i;
7819 unsigned offset;
7820 isl_basic_set *nonneg;
7821 isl_basic_set *neg;
7823 if (!set)
7824 return NULL;
7825 if (n == 0)
7826 return set;
7828 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7830 offset = pos(set->dim, type);
7831 for (i = 0; i < n; ++i) {
7832 nonneg = nonneg_halfspace(isl_set_get_space(set),
7833 offset + first + i);
7834 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7836 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7839 return set;
7840 error:
7841 isl_set_free(set);
7842 return NULL;
7845 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7846 int len,
7847 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7848 void *user)
7850 isl_set *half;
7852 if (!set)
7853 return isl_stat_error;
7854 if (isl_set_plain_is_empty(set)) {
7855 isl_set_free(set);
7856 return isl_stat_ok;
7858 if (first == len)
7859 return fn(set, signs, user);
7861 signs[first] = 1;
7862 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7863 1 + first));
7864 half = isl_set_intersect(half, isl_set_copy(set));
7865 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7866 goto error;
7868 signs[first] = -1;
7869 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7870 1 + first));
7871 half = isl_set_intersect(half, set);
7872 return foreach_orthant(half, signs, first + 1, len, fn, user);
7873 error:
7874 isl_set_free(set);
7875 return isl_stat_error;
7878 /* Call "fn" on the intersections of "set" with each of the orthants
7879 * (except for obviously empty intersections). The orthant is identified
7880 * by the signs array, with each entry having value 1 or -1 according
7881 * to the sign of the corresponding variable.
7883 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
7884 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7885 void *user)
7887 unsigned nparam;
7888 unsigned nvar;
7889 int *signs;
7890 isl_stat r;
7892 if (!set)
7893 return isl_stat_error;
7894 if (isl_set_plain_is_empty(set))
7895 return isl_stat_ok;
7897 nparam = isl_set_dim(set, isl_dim_param);
7898 nvar = isl_set_dim(set, isl_dim_set);
7900 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7902 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7903 fn, user);
7905 free(signs);
7907 return r;
7910 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7912 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
7915 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7916 __isl_keep isl_basic_map *bmap2)
7918 int is_subset;
7919 struct isl_map *map1;
7920 struct isl_map *map2;
7922 if (!bmap1 || !bmap2)
7923 return isl_bool_error;
7925 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7926 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7928 is_subset = isl_map_is_subset(map1, map2);
7930 isl_map_free(map1);
7931 isl_map_free(map2);
7933 return is_subset;
7936 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7937 __isl_keep isl_basic_set *bset2)
7939 return isl_basic_map_is_subset(bset1, bset2);
7942 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7943 __isl_keep isl_basic_map *bmap2)
7945 isl_bool is_subset;
7947 if (!bmap1 || !bmap2)
7948 return isl_bool_error;
7949 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7950 if (is_subset != isl_bool_true)
7951 return is_subset;
7952 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7953 return is_subset;
7956 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7957 __isl_keep isl_basic_set *bset2)
7959 return isl_basic_map_is_equal(
7960 bset_to_bmap(bset1), bset_to_bmap(bset2));
7963 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7965 int i;
7966 int is_empty;
7968 if (!map)
7969 return isl_bool_error;
7970 for (i = 0; i < map->n; ++i) {
7971 is_empty = isl_basic_map_is_empty(map->p[i]);
7972 if (is_empty < 0)
7973 return isl_bool_error;
7974 if (!is_empty)
7975 return isl_bool_false;
7977 return isl_bool_true;
7980 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7982 return map ? map->n == 0 : isl_bool_error;
7985 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7987 return set ? set->n == 0 : isl_bool_error;
7990 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7992 return isl_map_is_empty(set_to_map(set));
7995 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
7996 __isl_keep isl_map *map2)
7998 if (!map1 || !map2)
7999 return isl_bool_error;
8001 return isl_space_is_equal(map1->dim, map2->dim);
8004 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8005 __isl_keep isl_set *set2)
8007 if (!set1 || !set2)
8008 return isl_bool_error;
8010 return isl_space_is_equal(set1->dim, set2->dim);
8013 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8015 isl_bool is_subset;
8017 if (!map1 || !map2)
8018 return isl_bool_error;
8019 is_subset = isl_map_is_subset(map1, map2);
8020 if (is_subset != isl_bool_true)
8021 return is_subset;
8022 is_subset = isl_map_is_subset(map2, map1);
8023 return is_subset;
8026 /* Is "map1" equal to "map2"?
8028 * First check if they are obviously equal.
8029 * If not, then perform a more detailed analysis.
8031 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8033 isl_bool equal;
8035 equal = isl_map_plain_is_equal(map1, map2);
8036 if (equal < 0 || equal)
8037 return equal;
8038 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8041 isl_bool isl_basic_map_is_strict_subset(
8042 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8044 isl_bool is_subset;
8046 if (!bmap1 || !bmap2)
8047 return isl_bool_error;
8048 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8049 if (is_subset != isl_bool_true)
8050 return is_subset;
8051 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8052 if (is_subset == isl_bool_error)
8053 return is_subset;
8054 return !is_subset;
8057 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8058 __isl_keep isl_map *map2)
8060 isl_bool is_subset;
8062 if (!map1 || !map2)
8063 return isl_bool_error;
8064 is_subset = isl_map_is_subset(map1, map2);
8065 if (is_subset != isl_bool_true)
8066 return is_subset;
8067 is_subset = isl_map_is_subset(map2, map1);
8068 if (is_subset == isl_bool_error)
8069 return is_subset;
8070 return !is_subset;
8073 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8074 __isl_keep isl_set *set2)
8076 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8079 /* Is "bmap" obviously equal to the universe with the same space?
8081 * That is, does it not have any constraints?
8083 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8085 if (!bmap)
8086 return isl_bool_error;
8087 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8090 /* Is "bset" obviously equal to the universe with the same space?
8092 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8094 return isl_basic_map_plain_is_universe(bset);
8097 /* If "c" does not involve any existentially quantified variables,
8098 * then set *univ to false and abort
8100 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8102 isl_bool *univ = user;
8103 unsigned n;
8105 n = isl_constraint_dim(c, isl_dim_div);
8106 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8107 isl_constraint_free(c);
8108 if (*univ < 0 || !*univ)
8109 return isl_stat_error;
8110 return isl_stat_ok;
8113 /* Is "bmap" equal to the universe with the same space?
8115 * First check if it is obviously equal to the universe.
8116 * If not and if there are any constraints not involving
8117 * existentially quantified variables, then it is certainly
8118 * not equal to the universe.
8119 * Otherwise, check if the universe is a subset of "bmap".
8121 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8123 isl_bool univ;
8124 isl_basic_map *test;
8126 univ = isl_basic_map_plain_is_universe(bmap);
8127 if (univ < 0 || univ)
8128 return univ;
8129 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8130 return isl_bool_false;
8131 univ = isl_bool_true;
8132 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8133 univ)
8134 return isl_bool_error;
8135 if (univ < 0 || !univ)
8136 return univ;
8137 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8138 univ = isl_basic_map_is_subset(test, bmap);
8139 isl_basic_map_free(test);
8140 return univ;
8143 /* Is "bset" equal to the universe with the same space?
8145 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8147 return isl_basic_map_is_universe(bset);
8150 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8152 int i;
8154 if (!map)
8155 return isl_bool_error;
8157 for (i = 0; i < map->n; ++i) {
8158 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8159 if (r < 0 || r)
8160 return r;
8163 return isl_bool_false;
8166 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8168 return isl_map_plain_is_universe(set_to_map(set));
8171 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8173 struct isl_basic_set *bset = NULL;
8174 struct isl_vec *sample = NULL;
8175 isl_bool empty, non_empty;
8177 if (!bmap)
8178 return isl_bool_error;
8180 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8181 return isl_bool_true;
8183 if (isl_basic_map_plain_is_universe(bmap))
8184 return isl_bool_false;
8186 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8187 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8188 copy = isl_basic_map_remove_redundancies(copy);
8189 empty = isl_basic_map_plain_is_empty(copy);
8190 isl_basic_map_free(copy);
8191 return empty;
8194 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8195 if (non_empty < 0)
8196 return isl_bool_error;
8197 if (non_empty)
8198 return isl_bool_false;
8199 isl_vec_free(bmap->sample);
8200 bmap->sample = NULL;
8201 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8202 if (!bset)
8203 return isl_bool_error;
8204 sample = isl_basic_set_sample_vec(bset);
8205 if (!sample)
8206 return isl_bool_error;
8207 empty = sample->size == 0;
8208 isl_vec_free(bmap->sample);
8209 bmap->sample = sample;
8210 if (empty)
8211 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8213 return empty;
8216 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8218 if (!bmap)
8219 return isl_bool_error;
8220 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8223 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8225 if (!bset)
8226 return isl_bool_error;
8227 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8230 /* Is "bmap" known to be non-empty?
8232 * That is, is the cached sample still valid?
8234 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8236 unsigned total;
8238 if (!bmap)
8239 return isl_bool_error;
8240 if (!bmap->sample)
8241 return isl_bool_false;
8242 total = 1 + isl_basic_map_total_dim(bmap);
8243 if (bmap->sample->size != total)
8244 return isl_bool_false;
8245 return isl_basic_map_contains(bmap, bmap->sample);
8248 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8250 return isl_basic_map_is_empty(bset_to_bmap(bset));
8253 struct isl_map *isl_basic_map_union(
8254 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8256 struct isl_map *map;
8257 if (!bmap1 || !bmap2)
8258 goto error;
8260 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8262 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8263 if (!map)
8264 goto error;
8265 map = isl_map_add_basic_map(map, bmap1);
8266 map = isl_map_add_basic_map(map, bmap2);
8267 return map;
8268 error:
8269 isl_basic_map_free(bmap1);
8270 isl_basic_map_free(bmap2);
8271 return NULL;
8274 struct isl_set *isl_basic_set_union(
8275 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8277 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8278 bset_to_bmap(bset2)));
8281 /* Order divs such that any div only depends on previous divs */
8282 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8284 int i;
8285 unsigned off;
8287 if (!bmap)
8288 return NULL;
8290 off = isl_space_dim(bmap->dim, isl_dim_all);
8292 for (i = 0; i < bmap->n_div; ++i) {
8293 int pos;
8294 if (isl_int_is_zero(bmap->div[i][0]))
8295 continue;
8296 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8297 bmap->n_div-i);
8298 if (pos == -1)
8299 continue;
8300 if (pos == 0)
8301 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8302 "integer division depends on itself",
8303 return isl_basic_map_free(bmap));
8304 isl_basic_map_swap_div(bmap, i, i + pos);
8305 --i;
8307 return bmap;
8310 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8312 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8315 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8317 int i;
8319 if (!map)
8320 return 0;
8322 for (i = 0; i < map->n; ++i) {
8323 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8324 if (!map->p[i])
8325 goto error;
8328 return map;
8329 error:
8330 isl_map_free(map);
8331 return NULL;
8334 /* Sort the local variables of "bset".
8336 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8337 __isl_take isl_basic_set *bset)
8339 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8342 /* Apply the expansion computed by isl_merge_divs.
8343 * The expansion itself is given by "exp" while the resulting
8344 * list of divs is given by "div".
8346 * Move the integer divisions of "bmap" into the right position
8347 * according to "exp" and then introduce the additional integer
8348 * divisions, adding div constraints.
8349 * The moving should be done first to avoid moving coefficients
8350 * in the definitions of the extra integer divisions.
8352 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8353 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8355 int i, j;
8356 int n_div;
8358 bmap = isl_basic_map_cow(bmap);
8359 if (!bmap || !div)
8360 goto error;
8362 if (div->n_row < bmap->n_div)
8363 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8364 "not an expansion", goto error);
8366 n_div = bmap->n_div;
8367 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8368 div->n_row - n_div, 0,
8369 2 * (div->n_row - n_div));
8371 for (i = n_div; i < div->n_row; ++i)
8372 if (isl_basic_map_alloc_div(bmap) < 0)
8373 goto error;
8375 for (j = n_div - 1; j >= 0; --j) {
8376 if (exp[j] == j)
8377 break;
8378 isl_basic_map_swap_div(bmap, j, exp[j]);
8380 j = 0;
8381 for (i = 0; i < div->n_row; ++i) {
8382 if (j < n_div && exp[j] == i) {
8383 j++;
8384 } else {
8385 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8386 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8387 continue;
8388 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8389 goto error;
8393 isl_mat_free(div);
8394 return bmap;
8395 error:
8396 isl_basic_map_free(bmap);
8397 isl_mat_free(div);
8398 return NULL;
8401 /* Apply the expansion computed by isl_merge_divs.
8402 * The expansion itself is given by "exp" while the resulting
8403 * list of divs is given by "div".
8405 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8406 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8408 return isl_basic_map_expand_divs(bset, div, exp);
8411 /* Look for a div in dst that corresponds to the div "div" in src.
8412 * The divs before "div" in src and dst are assumed to be the same.
8414 * Returns -1 if no corresponding div was found and the position
8415 * of the corresponding div in dst otherwise.
8417 static int find_div(struct isl_basic_map *dst,
8418 struct isl_basic_map *src, unsigned div)
8420 int i;
8422 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8424 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8425 for (i = div; i < dst->n_div; ++i)
8426 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8427 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8428 dst->n_div - div) == -1)
8429 return i;
8430 return -1;
8433 /* Align the divs of "dst" to those of "src", adding divs from "src"
8434 * if needed. That is, make sure that the first src->n_div divs
8435 * of the result are equal to those of src.
8437 * The result is not finalized as by design it will have redundant
8438 * divs if any divs from "src" were copied.
8440 __isl_give isl_basic_map *isl_basic_map_align_divs(
8441 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8443 int i;
8444 int known, extended;
8445 unsigned total;
8447 if (!dst || !src)
8448 return isl_basic_map_free(dst);
8450 if (src->n_div == 0)
8451 return dst;
8453 known = isl_basic_map_divs_known(src);
8454 if (known < 0)
8455 return isl_basic_map_free(dst);
8456 if (!known)
8457 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8458 "some src divs are unknown",
8459 return isl_basic_map_free(dst));
8461 src = isl_basic_map_order_divs(src);
8463 extended = 0;
8464 total = isl_space_dim(src->dim, isl_dim_all);
8465 for (i = 0; i < src->n_div; ++i) {
8466 int j = find_div(dst, src, i);
8467 if (j < 0) {
8468 if (!extended) {
8469 int extra = src->n_div - i;
8470 dst = isl_basic_map_cow(dst);
8471 if (!dst)
8472 return NULL;
8473 dst = isl_basic_map_extend_space(dst,
8474 isl_space_copy(dst->dim),
8475 extra, 0, 2 * extra);
8476 extended = 1;
8478 j = isl_basic_map_alloc_div(dst);
8479 if (j < 0)
8480 return isl_basic_map_free(dst);
8481 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8482 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8483 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8484 return isl_basic_map_free(dst);
8486 if (j != i)
8487 isl_basic_map_swap_div(dst, i, j);
8489 return dst;
8492 struct isl_map *isl_map_align_divs(struct isl_map *map)
8494 int i;
8496 if (!map)
8497 return NULL;
8498 if (map->n == 0)
8499 return map;
8500 map = isl_map_compute_divs(map);
8501 map = isl_map_cow(map);
8502 if (!map)
8503 return NULL;
8505 for (i = 1; i < map->n; ++i)
8506 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8507 for (i = 1; i < map->n; ++i) {
8508 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8509 if (!map->p[i])
8510 return isl_map_free(map);
8513 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8514 return map;
8517 struct isl_set *isl_set_align_divs(struct isl_set *set)
8519 return set_from_map(isl_map_align_divs(set_to_map(set)));
8522 /* Align the divs of the basic maps in "map" to those
8523 * of the basic maps in "list", as well as to the other basic maps in "map".
8524 * The elements in "list" are assumed to have known divs.
8526 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8527 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8529 int i, n;
8531 map = isl_map_compute_divs(map);
8532 map = isl_map_cow(map);
8533 if (!map || !list)
8534 return isl_map_free(map);
8535 if (map->n == 0)
8536 return map;
8538 n = isl_basic_map_list_n_basic_map(list);
8539 for (i = 0; i < n; ++i) {
8540 isl_basic_map *bmap;
8542 bmap = isl_basic_map_list_get_basic_map(list, i);
8543 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8544 isl_basic_map_free(bmap);
8546 if (!map->p[0])
8547 return isl_map_free(map);
8549 return isl_map_align_divs(map);
8552 /* Align the divs of each element of "list" to those of "bmap".
8553 * Both "bmap" and the elements of "list" are assumed to have known divs.
8555 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8556 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8558 int i, n;
8560 if (!list || !bmap)
8561 return isl_basic_map_list_free(list);
8563 n = isl_basic_map_list_n_basic_map(list);
8564 for (i = 0; i < n; ++i) {
8565 isl_basic_map *bmap_i;
8567 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8568 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8569 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8572 return list;
8575 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8576 __isl_take isl_map *map)
8578 isl_bool ok;
8580 ok = isl_map_compatible_domain(map, set);
8581 if (ok < 0)
8582 goto error;
8583 if (!ok)
8584 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8585 "incompatible spaces", goto error);
8586 map = isl_map_intersect_domain(map, set);
8587 set = isl_map_range(map);
8588 return set;
8589 error:
8590 isl_set_free(set);
8591 isl_map_free(map);
8592 return NULL;
8595 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8596 __isl_take isl_map *map)
8598 return isl_map_align_params_map_map_and(set, map, &set_apply);
8601 /* There is no need to cow as removing empty parts doesn't change
8602 * the meaning of the set.
8604 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8606 int i;
8608 if (!map)
8609 return NULL;
8611 for (i = map->n - 1; i >= 0; --i)
8612 remove_if_empty(map, i);
8614 return map;
8617 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8619 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8622 /* Given two basic sets bset1 and bset2, compute the maximal difference
8623 * between the values of dimension pos in bset1 and those in bset2
8624 * for any common value of the parameters and dimensions preceding pos.
8626 static enum isl_lp_result basic_set_maximal_difference_at(
8627 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8628 int pos, isl_int *opt)
8630 isl_basic_map *bmap1;
8631 isl_basic_map *bmap2;
8632 struct isl_ctx *ctx;
8633 struct isl_vec *obj;
8634 unsigned total;
8635 unsigned nparam;
8636 unsigned dim1;
8637 enum isl_lp_result res;
8639 if (!bset1 || !bset2)
8640 return isl_lp_error;
8642 nparam = isl_basic_set_n_param(bset1);
8643 dim1 = isl_basic_set_n_dim(bset1);
8645 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8646 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8647 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8648 isl_dim_out, 0, pos);
8649 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8650 isl_dim_out, 0, pos);
8651 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8652 if (!bmap1)
8653 return isl_lp_error;
8655 total = isl_basic_map_total_dim(bmap1);
8656 ctx = bmap1->ctx;
8657 obj = isl_vec_alloc(ctx, 1 + total);
8658 if (!obj)
8659 goto error;
8660 isl_seq_clr(obj->block.data, 1 + total);
8661 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8662 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8663 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8664 opt, NULL, NULL);
8665 isl_basic_map_free(bmap1);
8666 isl_vec_free(obj);
8667 return res;
8668 error:
8669 isl_basic_map_free(bmap1);
8670 return isl_lp_error;
8673 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8674 * for any common value of the parameters and dimensions preceding pos
8675 * in both basic sets, the values of dimension pos in bset1 are
8676 * smaller or larger than those in bset2.
8678 * Returns
8679 * 1 if bset1 follows bset2
8680 * -1 if bset1 precedes bset2
8681 * 0 if bset1 and bset2 are incomparable
8682 * -2 if some error occurred.
8684 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8685 struct isl_basic_set *bset2, int pos)
8687 isl_int opt;
8688 enum isl_lp_result res;
8689 int cmp;
8691 isl_int_init(opt);
8693 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8695 if (res == isl_lp_empty)
8696 cmp = 0;
8697 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8698 res == isl_lp_unbounded)
8699 cmp = 1;
8700 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8701 cmp = -1;
8702 else
8703 cmp = -2;
8705 isl_int_clear(opt);
8706 return cmp;
8709 /* Given two basic sets bset1 and bset2, check whether
8710 * for any common value of the parameters and dimensions preceding pos
8711 * there is a value of dimension pos in bset1 that is larger
8712 * than a value of the same dimension in bset2.
8714 * Return
8715 * 1 if there exists such a pair
8716 * 0 if there is no such pair, but there is a pair of equal values
8717 * -1 otherwise
8718 * -2 if some error occurred.
8720 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8721 __isl_keep isl_basic_set *bset2, int pos)
8723 isl_int opt;
8724 enum isl_lp_result res;
8725 int cmp;
8727 isl_int_init(opt);
8729 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8731 if (res == isl_lp_empty)
8732 cmp = -1;
8733 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8734 res == isl_lp_unbounded)
8735 cmp = 1;
8736 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8737 cmp = -1;
8738 else if (res == isl_lp_ok)
8739 cmp = 0;
8740 else
8741 cmp = -2;
8743 isl_int_clear(opt);
8744 return cmp;
8747 /* Given two sets set1 and set2, check whether
8748 * for any common value of the parameters and dimensions preceding pos
8749 * there is a value of dimension pos in set1 that is larger
8750 * than a value of the same dimension in set2.
8752 * Return
8753 * 1 if there exists such a pair
8754 * 0 if there is no such pair, but there is a pair of equal values
8755 * -1 otherwise
8756 * -2 if some error occurred.
8758 int isl_set_follows_at(__isl_keep isl_set *set1,
8759 __isl_keep isl_set *set2, int pos)
8761 int i, j;
8762 int follows = -1;
8764 if (!set1 || !set2)
8765 return -2;
8767 for (i = 0; i < set1->n; ++i)
8768 for (j = 0; j < set2->n; ++j) {
8769 int f;
8770 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8771 if (f == 1 || f == -2)
8772 return f;
8773 if (f > follows)
8774 follows = f;
8777 return follows;
8780 static isl_bool isl_basic_map_plain_has_fixed_var(
8781 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
8783 int i;
8784 int d;
8785 unsigned total;
8787 if (!bmap)
8788 return isl_bool_error;
8789 total = isl_basic_map_total_dim(bmap);
8790 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8791 for (; d+1 > pos; --d)
8792 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8793 break;
8794 if (d != pos)
8795 continue;
8796 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8797 return isl_bool_false;
8798 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8799 return isl_bool_false;
8800 if (!isl_int_is_one(bmap->eq[i][1+d]))
8801 return isl_bool_false;
8802 if (val)
8803 isl_int_neg(*val, bmap->eq[i][0]);
8804 return isl_bool_true;
8806 return isl_bool_false;
8809 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8810 unsigned pos, isl_int *val)
8812 int i;
8813 isl_int v;
8814 isl_int tmp;
8815 isl_bool fixed;
8817 if (!map)
8818 return isl_bool_error;
8819 if (map->n == 0)
8820 return isl_bool_false;
8821 if (map->n == 1)
8822 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8823 isl_int_init(v);
8824 isl_int_init(tmp);
8825 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8826 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
8827 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8828 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
8829 fixed = isl_bool_false;
8831 if (val)
8832 isl_int_set(*val, v);
8833 isl_int_clear(tmp);
8834 isl_int_clear(v);
8835 return fixed;
8838 static isl_bool isl_basic_set_plain_has_fixed_var(
8839 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
8841 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
8842 pos, val);
8845 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8846 enum isl_dim_type type, unsigned pos, isl_int *val)
8848 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
8849 return isl_bool_error;
8850 return isl_basic_map_plain_has_fixed_var(bmap,
8851 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8854 /* If "bmap" obviously lies on a hyperplane where the given dimension
8855 * has a fixed value, then return that value.
8856 * Otherwise return NaN.
8858 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8859 __isl_keep isl_basic_map *bmap,
8860 enum isl_dim_type type, unsigned pos)
8862 isl_ctx *ctx;
8863 isl_val *v;
8864 isl_bool fixed;
8866 if (!bmap)
8867 return NULL;
8868 ctx = isl_basic_map_get_ctx(bmap);
8869 v = isl_val_alloc(ctx);
8870 if (!v)
8871 return NULL;
8872 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8873 if (fixed < 0)
8874 return isl_val_free(v);
8875 if (fixed) {
8876 isl_int_set_si(v->d, 1);
8877 return v;
8879 isl_val_free(v);
8880 return isl_val_nan(ctx);
8883 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
8884 enum isl_dim_type type, unsigned pos, isl_int *val)
8886 if (pos >= isl_map_dim(map, type))
8887 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8888 "position out of bounds", return isl_bool_error);
8889 return isl_map_plain_has_fixed_var(map,
8890 map_offset(map, type) - 1 + pos, val);
8893 /* If "map" obviously lies on a hyperplane where the given dimension
8894 * has a fixed value, then return that value.
8895 * Otherwise return NaN.
8897 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8898 enum isl_dim_type type, unsigned pos)
8900 isl_ctx *ctx;
8901 isl_val *v;
8902 isl_bool fixed;
8904 if (!map)
8905 return NULL;
8906 ctx = isl_map_get_ctx(map);
8907 v = isl_val_alloc(ctx);
8908 if (!v)
8909 return NULL;
8910 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8911 if (fixed < 0)
8912 return isl_val_free(v);
8913 if (fixed) {
8914 isl_int_set_si(v->d, 1);
8915 return v;
8917 isl_val_free(v);
8918 return isl_val_nan(ctx);
8921 /* If "set" obviously lies on a hyperplane where the given dimension
8922 * has a fixed value, then return that value.
8923 * Otherwise return NaN.
8925 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8926 enum isl_dim_type type, unsigned pos)
8928 return isl_map_plain_get_val_if_fixed(set, type, pos);
8931 isl_bool isl_set_plain_is_fixed(__isl_keep isl_set *set,
8932 enum isl_dim_type type, unsigned pos, isl_int *val)
8934 return isl_map_plain_is_fixed(set, type, pos, val);
8937 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8938 * then return this fixed value in *val.
8940 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8941 unsigned dim, isl_int *val)
8943 return isl_basic_set_plain_has_fixed_var(bset,
8944 isl_basic_set_n_param(bset) + dim, val);
8947 /* Return -1 if the constraint "c1" should be sorted before "c2"
8948 * and 1 if it should be sorted after "c2".
8949 * Return 0 if the two constraints are the same (up to the constant term).
8951 * In particular, if a constraint involves later variables than another
8952 * then it is sorted after this other constraint.
8953 * uset_gist depends on constraints without existentially quantified
8954 * variables sorting first.
8956 * For constraints that have the same latest variable, those
8957 * with the same coefficient for this latest variable (first in absolute value
8958 * and then in actual value) are grouped together.
8959 * This is useful for detecting pairs of constraints that can
8960 * be chained in their printed representation.
8962 * Finally, within a group, constraints are sorted according to
8963 * their coefficients (excluding the constant term).
8965 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8967 isl_int **c1 = (isl_int **) p1;
8968 isl_int **c2 = (isl_int **) p2;
8969 int l1, l2;
8970 unsigned size = *(unsigned *) arg;
8971 int cmp;
8973 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8974 l2 = isl_seq_last_non_zero(*c2 + 1, size);
8976 if (l1 != l2)
8977 return l1 - l2;
8979 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
8980 if (cmp != 0)
8981 return cmp;
8982 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
8983 if (cmp != 0)
8984 return -cmp;
8986 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
8989 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
8990 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
8991 * and 0 if the two constraints are the same (up to the constant term).
8993 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
8994 isl_int *c1, isl_int *c2)
8996 unsigned total;
8998 if (!bmap)
8999 return -2;
9000 total = isl_basic_map_total_dim(bmap);
9001 return sort_constraint_cmp(&c1, &c2, &total);
9004 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9005 __isl_take isl_basic_map *bmap)
9007 unsigned total;
9009 if (!bmap)
9010 return NULL;
9011 if (bmap->n_ineq == 0)
9012 return bmap;
9013 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9014 return bmap;
9015 total = isl_basic_map_total_dim(bmap);
9016 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9017 &sort_constraint_cmp, &total) < 0)
9018 return isl_basic_map_free(bmap);
9019 return bmap;
9022 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9023 __isl_take isl_basic_set *bset)
9025 isl_basic_map *bmap = bset_to_bmap(bset);
9026 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9029 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9031 if (!bmap)
9032 return NULL;
9033 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9034 return bmap;
9035 bmap = isl_basic_map_remove_redundancies(bmap);
9036 bmap = isl_basic_map_sort_constraints(bmap);
9037 if (bmap)
9038 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9039 return bmap;
9041 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9042 __isl_keep isl_basic_map *bmap2)
9044 int i, cmp;
9045 unsigned total;
9047 if (!bmap1 || !bmap2)
9048 return -1;
9050 if (bmap1 == bmap2)
9051 return 0;
9052 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9053 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9054 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9055 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9056 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9057 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9058 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9059 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9060 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9061 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9062 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9063 return 0;
9064 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9065 return 1;
9066 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9067 return -1;
9068 if (bmap1->n_eq != bmap2->n_eq)
9069 return bmap1->n_eq - bmap2->n_eq;
9070 if (bmap1->n_ineq != bmap2->n_ineq)
9071 return bmap1->n_ineq - bmap2->n_ineq;
9072 if (bmap1->n_div != bmap2->n_div)
9073 return bmap1->n_div - bmap2->n_div;
9074 total = isl_basic_map_total_dim(bmap1);
9075 for (i = 0; i < bmap1->n_eq; ++i) {
9076 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9077 if (cmp)
9078 return cmp;
9080 for (i = 0; i < bmap1->n_ineq; ++i) {
9081 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9082 if (cmp)
9083 return cmp;
9085 for (i = 0; i < bmap1->n_div; ++i) {
9086 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9087 if (cmp)
9088 return cmp;
9090 return 0;
9093 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9094 __isl_keep isl_basic_set *bset2)
9096 return isl_basic_map_plain_cmp(bset1, bset2);
9099 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9101 int i, cmp;
9103 if (set1 == set2)
9104 return 0;
9105 if (set1->n != set2->n)
9106 return set1->n - set2->n;
9108 for (i = 0; i < set1->n; ++i) {
9109 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9110 if (cmp)
9111 return cmp;
9114 return 0;
9117 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9118 __isl_keep isl_basic_map *bmap2)
9120 if (!bmap1 || !bmap2)
9121 return isl_bool_error;
9122 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9125 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9126 __isl_keep isl_basic_set *bset2)
9128 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9129 bset_to_bmap(bset2));
9132 static int qsort_bmap_cmp(const void *p1, const void *p2)
9134 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9135 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9137 return isl_basic_map_plain_cmp(bmap1, bmap2);
9140 /* Sort the basic maps of "map" and remove duplicate basic maps.
9142 * While removing basic maps, we make sure that the basic maps remain
9143 * sorted because isl_map_normalize expects the basic maps of the result
9144 * to be sorted.
9146 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9148 int i, j;
9150 map = isl_map_remove_empty_parts(map);
9151 if (!map)
9152 return NULL;
9153 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9154 for (i = map->n - 1; i >= 1; --i) {
9155 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9156 continue;
9157 isl_basic_map_free(map->p[i-1]);
9158 for (j = i; j < map->n; ++j)
9159 map->p[j - 1] = map->p[j];
9160 map->n--;
9163 return map;
9166 /* Remove obvious duplicates among the basic maps of "map".
9168 * Unlike isl_map_normalize, this function does not remove redundant
9169 * constraints and only removes duplicates that have exactly the same
9170 * constraints in the input. It does sort the constraints and
9171 * the basic maps to ease the detection of duplicates.
9173 * If "map" has already been normalized or if the basic maps are
9174 * disjoint, then there can be no duplicates.
9176 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9178 int i;
9179 isl_basic_map *bmap;
9181 if (!map)
9182 return NULL;
9183 if (map->n <= 1)
9184 return map;
9185 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9186 return map;
9187 for (i = 0; i < map->n; ++i) {
9188 bmap = isl_basic_map_copy(map->p[i]);
9189 bmap = isl_basic_map_sort_constraints(bmap);
9190 if (!bmap)
9191 return isl_map_free(map);
9192 isl_basic_map_free(map->p[i]);
9193 map->p[i] = bmap;
9196 map = sort_and_remove_duplicates(map);
9197 return map;
9200 /* We normalize in place, but if anything goes wrong we need
9201 * to return NULL, so we need to make sure we don't change the
9202 * meaning of any possible other copies of map.
9204 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9206 int i;
9207 struct isl_basic_map *bmap;
9209 if (!map)
9210 return NULL;
9211 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9212 return map;
9213 for (i = 0; i < map->n; ++i) {
9214 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9215 if (!bmap)
9216 goto error;
9217 isl_basic_map_free(map->p[i]);
9218 map->p[i] = bmap;
9221 map = sort_and_remove_duplicates(map);
9222 if (map)
9223 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9224 return map;
9225 error:
9226 isl_map_free(map);
9227 return NULL;
9230 struct isl_set *isl_set_normalize(struct isl_set *set)
9232 return set_from_map(isl_map_normalize(set_to_map(set)));
9235 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9236 __isl_keep isl_map *map2)
9238 int i;
9239 isl_bool equal;
9241 if (!map1 || !map2)
9242 return isl_bool_error;
9244 if (map1 == map2)
9245 return isl_bool_true;
9246 if (!isl_space_is_equal(map1->dim, map2->dim))
9247 return isl_bool_false;
9249 map1 = isl_map_copy(map1);
9250 map2 = isl_map_copy(map2);
9251 map1 = isl_map_normalize(map1);
9252 map2 = isl_map_normalize(map2);
9253 if (!map1 || !map2)
9254 goto error;
9255 equal = map1->n == map2->n;
9256 for (i = 0; equal && i < map1->n; ++i) {
9257 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9258 if (equal < 0)
9259 goto error;
9261 isl_map_free(map1);
9262 isl_map_free(map2);
9263 return equal;
9264 error:
9265 isl_map_free(map1);
9266 isl_map_free(map2);
9267 return isl_bool_error;
9270 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9271 __isl_keep isl_set *set2)
9273 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9276 /* Return the basic maps in "map" as a list.
9278 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9279 __isl_keep isl_map *map)
9281 int i;
9282 isl_ctx *ctx;
9283 isl_basic_map_list *list;
9285 if (!map)
9286 return NULL;
9287 ctx = isl_map_get_ctx(map);
9288 list = isl_basic_map_list_alloc(ctx, map->n);
9290 for (i = 0; i < map->n; ++i) {
9291 isl_basic_map *bmap;
9293 bmap = isl_basic_map_copy(map->p[i]);
9294 list = isl_basic_map_list_add(list, bmap);
9297 return list;
9300 /* Return the intersection of the elements in the non-empty list "list".
9301 * All elements are assumed to live in the same space.
9303 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9304 __isl_take isl_basic_map_list *list)
9306 int i, n;
9307 isl_basic_map *bmap;
9309 if (!list)
9310 return NULL;
9311 n = isl_basic_map_list_n_basic_map(list);
9312 if (n < 1)
9313 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9314 "expecting non-empty list", goto error);
9316 bmap = isl_basic_map_list_get_basic_map(list, 0);
9317 for (i = 1; i < n; ++i) {
9318 isl_basic_map *bmap_i;
9320 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9321 bmap = isl_basic_map_intersect(bmap, bmap_i);
9324 isl_basic_map_list_free(list);
9325 return bmap;
9326 error:
9327 isl_basic_map_list_free(list);
9328 return NULL;
9331 /* Return the intersection of the elements in the non-empty list "list".
9332 * All elements are assumed to live in the same space.
9334 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9335 __isl_take isl_basic_set_list *list)
9337 return isl_basic_map_list_intersect(list);
9340 /* Return the union of the elements of "list".
9341 * The list is required to have at least one element.
9343 __isl_give isl_set *isl_basic_set_list_union(
9344 __isl_take isl_basic_set_list *list)
9346 int i, n;
9347 isl_space *space;
9348 isl_basic_set *bset;
9349 isl_set *set;
9351 if (!list)
9352 return NULL;
9353 n = isl_basic_set_list_n_basic_set(list);
9354 if (n < 1)
9355 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9356 "expecting non-empty list", goto error);
9358 bset = isl_basic_set_list_get_basic_set(list, 0);
9359 space = isl_basic_set_get_space(bset);
9360 isl_basic_set_free(bset);
9362 set = isl_set_alloc_space(space, n, 0);
9363 for (i = 0; i < n; ++i) {
9364 bset = isl_basic_set_list_get_basic_set(list, i);
9365 set = isl_set_add_basic_set(set, bset);
9368 isl_basic_set_list_free(list);
9369 return set;
9370 error:
9371 isl_basic_set_list_free(list);
9372 return NULL;
9375 /* Return the union of the elements in the non-empty list "list".
9376 * All elements are assumed to live in the same space.
9378 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9380 int i, n;
9381 isl_set *set;
9383 if (!list)
9384 return NULL;
9385 n = isl_set_list_n_set(list);
9386 if (n < 1)
9387 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9388 "expecting non-empty list", goto error);
9390 set = isl_set_list_get_set(list, 0);
9391 for (i = 1; i < n; ++i) {
9392 isl_set *set_i;
9394 set_i = isl_set_list_get_set(list, i);
9395 set = isl_set_union(set, set_i);
9398 isl_set_list_free(list);
9399 return set;
9400 error:
9401 isl_set_list_free(list);
9402 return NULL;
9405 struct isl_basic_map *isl_basic_map_product(
9406 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9408 isl_space *dim_result = NULL;
9409 struct isl_basic_map *bmap;
9410 unsigned in1, in2, out1, out2, nparam, total, pos;
9411 struct isl_dim_map *dim_map1, *dim_map2;
9413 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9414 goto error;
9415 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9416 isl_space_copy(bmap2->dim));
9418 in1 = isl_basic_map_n_in(bmap1);
9419 in2 = isl_basic_map_n_in(bmap2);
9420 out1 = isl_basic_map_n_out(bmap1);
9421 out2 = isl_basic_map_n_out(bmap2);
9422 nparam = isl_basic_map_n_param(bmap1);
9424 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9425 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9426 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9427 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9428 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9429 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9430 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9431 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9432 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9433 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9434 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9436 bmap = isl_basic_map_alloc_space(dim_result,
9437 bmap1->n_div + bmap2->n_div,
9438 bmap1->n_eq + bmap2->n_eq,
9439 bmap1->n_ineq + bmap2->n_ineq);
9440 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9441 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9442 bmap = isl_basic_map_simplify(bmap);
9443 return isl_basic_map_finalize(bmap);
9444 error:
9445 isl_basic_map_free(bmap1);
9446 isl_basic_map_free(bmap2);
9447 return NULL;
9450 __isl_give isl_basic_map *isl_basic_map_flat_product(
9451 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9453 isl_basic_map *prod;
9455 prod = isl_basic_map_product(bmap1, bmap2);
9456 prod = isl_basic_map_flatten(prod);
9457 return prod;
9460 __isl_give isl_basic_set *isl_basic_set_flat_product(
9461 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9463 return isl_basic_map_flat_range_product(bset1, bset2);
9466 __isl_give isl_basic_map *isl_basic_map_domain_product(
9467 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9469 isl_space *space_result = NULL;
9470 isl_basic_map *bmap;
9471 unsigned in1, in2, out, nparam, total, pos;
9472 struct isl_dim_map *dim_map1, *dim_map2;
9474 if (!bmap1 || !bmap2)
9475 goto error;
9477 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9478 isl_space_copy(bmap2->dim));
9480 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9481 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9482 out = isl_basic_map_dim(bmap1, isl_dim_out);
9483 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9485 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9486 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9487 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9488 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9489 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9490 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9491 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9492 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9493 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9494 isl_dim_map_div(dim_map1, bmap1, pos += out);
9495 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9497 bmap = isl_basic_map_alloc_space(space_result,
9498 bmap1->n_div + bmap2->n_div,
9499 bmap1->n_eq + bmap2->n_eq,
9500 bmap1->n_ineq + bmap2->n_ineq);
9501 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9502 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9503 bmap = isl_basic_map_simplify(bmap);
9504 return isl_basic_map_finalize(bmap);
9505 error:
9506 isl_basic_map_free(bmap1);
9507 isl_basic_map_free(bmap2);
9508 return NULL;
9511 __isl_give isl_basic_map *isl_basic_map_range_product(
9512 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9514 isl_bool rational;
9515 isl_space *dim_result = NULL;
9516 isl_basic_map *bmap;
9517 unsigned in, out1, out2, nparam, total, pos;
9518 struct isl_dim_map *dim_map1, *dim_map2;
9520 rational = isl_basic_map_is_rational(bmap1);
9521 if (rational >= 0 && rational)
9522 rational = isl_basic_map_is_rational(bmap2);
9523 if (!bmap1 || !bmap2 || rational < 0)
9524 goto error;
9526 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9527 goto error;
9529 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9530 isl_space_copy(bmap2->dim));
9532 in = isl_basic_map_dim(bmap1, isl_dim_in);
9533 out1 = isl_basic_map_n_out(bmap1);
9534 out2 = isl_basic_map_n_out(bmap2);
9535 nparam = isl_basic_map_n_param(bmap1);
9537 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9538 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9539 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9540 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9541 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9542 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9543 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9544 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9545 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9546 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9547 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9549 bmap = isl_basic_map_alloc_space(dim_result,
9550 bmap1->n_div + bmap2->n_div,
9551 bmap1->n_eq + bmap2->n_eq,
9552 bmap1->n_ineq + bmap2->n_ineq);
9553 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9554 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9555 if (rational)
9556 bmap = isl_basic_map_set_rational(bmap);
9557 bmap = isl_basic_map_simplify(bmap);
9558 return isl_basic_map_finalize(bmap);
9559 error:
9560 isl_basic_map_free(bmap1);
9561 isl_basic_map_free(bmap2);
9562 return NULL;
9565 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9566 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9568 isl_basic_map *prod;
9570 prod = isl_basic_map_range_product(bmap1, bmap2);
9571 prod = isl_basic_map_flatten_range(prod);
9572 return prod;
9575 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9576 * and collect the results.
9577 * The result live in the space obtained by calling "space_product"
9578 * on the spaces of "map1" and "map2".
9579 * If "remove_duplicates" is set then the result may contain duplicates
9580 * (even if the inputs do not) and so we try and remove the obvious
9581 * duplicates.
9583 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9584 __isl_take isl_map *map2,
9585 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9586 __isl_take isl_space *right),
9587 __isl_give isl_basic_map *(*basic_map_product)(
9588 __isl_take isl_basic_map *left,
9589 __isl_take isl_basic_map *right),
9590 int remove_duplicates)
9592 unsigned flags = 0;
9593 struct isl_map *result;
9594 int i, j;
9595 isl_bool m;
9597 m = isl_map_has_equal_params(map1, map2);
9598 if (m < 0)
9599 goto error;
9600 if (!m)
9601 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
9602 "parameters don't match", goto error);
9604 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9605 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9606 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9608 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9609 isl_space_copy(map2->dim)),
9610 map1->n * map2->n, flags);
9611 if (!result)
9612 goto error;
9613 for (i = 0; i < map1->n; ++i)
9614 for (j = 0; j < map2->n; ++j) {
9615 struct isl_basic_map *part;
9616 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9617 isl_basic_map_copy(map2->p[j]));
9618 if (isl_basic_map_is_empty(part))
9619 isl_basic_map_free(part);
9620 else
9621 result = isl_map_add_basic_map(result, part);
9622 if (!result)
9623 goto error;
9625 if (remove_duplicates)
9626 result = isl_map_remove_obvious_duplicates(result);
9627 isl_map_free(map1);
9628 isl_map_free(map2);
9629 return result;
9630 error:
9631 isl_map_free(map1);
9632 isl_map_free(map2);
9633 return NULL;
9636 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9638 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9639 __isl_take isl_map *map2)
9641 return map_product(map1, map2, &isl_space_product,
9642 &isl_basic_map_product, 0);
9645 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9646 __isl_take isl_map *map2)
9648 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9651 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9653 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9654 __isl_take isl_map *map2)
9656 isl_map *prod;
9658 prod = isl_map_product(map1, map2);
9659 prod = isl_map_flatten(prod);
9660 return prod;
9663 /* Given two set A and B, construct its Cartesian product A x B.
9665 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9667 return isl_map_range_product(set1, set2);
9670 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9671 __isl_take isl_set *set2)
9673 return isl_map_flat_range_product(set1, set2);
9676 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9678 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9679 __isl_take isl_map *map2)
9681 return map_product(map1, map2, &isl_space_domain_product,
9682 &isl_basic_map_domain_product, 1);
9685 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9687 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9688 __isl_take isl_map *map2)
9690 return map_product(map1, map2, &isl_space_range_product,
9691 &isl_basic_map_range_product, 1);
9694 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9695 __isl_take isl_map *map2)
9697 return isl_map_align_params_map_map_and(map1, map2,
9698 &map_domain_product_aligned);
9701 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9702 __isl_take isl_map *map2)
9704 return isl_map_align_params_map_map_and(map1, map2,
9705 &map_range_product_aligned);
9708 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9710 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9712 isl_space *space;
9713 int total1, keep1, total2, keep2;
9715 if (!map)
9716 return NULL;
9717 if (!isl_space_domain_is_wrapping(map->dim) ||
9718 !isl_space_range_is_wrapping(map->dim))
9719 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9720 "not a product", return isl_map_free(map));
9722 space = isl_map_get_space(map);
9723 total1 = isl_space_dim(space, isl_dim_in);
9724 total2 = isl_space_dim(space, isl_dim_out);
9725 space = isl_space_factor_domain(space);
9726 keep1 = isl_space_dim(space, isl_dim_in);
9727 keep2 = isl_space_dim(space, isl_dim_out);
9728 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9729 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9730 map = isl_map_reset_space(map, space);
9732 return map;
9735 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9737 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9739 isl_space *space;
9740 int total1, keep1, total2, keep2;
9742 if (!map)
9743 return NULL;
9744 if (!isl_space_domain_is_wrapping(map->dim) ||
9745 !isl_space_range_is_wrapping(map->dim))
9746 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9747 "not a product", return isl_map_free(map));
9749 space = isl_map_get_space(map);
9750 total1 = isl_space_dim(space, isl_dim_in);
9751 total2 = isl_space_dim(space, isl_dim_out);
9752 space = isl_space_factor_range(space);
9753 keep1 = isl_space_dim(space, isl_dim_in);
9754 keep2 = isl_space_dim(space, isl_dim_out);
9755 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9756 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9757 map = isl_map_reset_space(map, space);
9759 return map;
9762 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9764 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9766 isl_space *space;
9767 int total, keep;
9769 if (!map)
9770 return NULL;
9771 if (!isl_space_domain_is_wrapping(map->dim))
9772 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9773 "domain is not a product", return isl_map_free(map));
9775 space = isl_map_get_space(map);
9776 total = isl_space_dim(space, isl_dim_in);
9777 space = isl_space_domain_factor_domain(space);
9778 keep = isl_space_dim(space, isl_dim_in);
9779 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9780 map = isl_map_reset_space(map, space);
9782 return map;
9785 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9787 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9789 isl_space *space;
9790 int total, keep;
9792 if (!map)
9793 return NULL;
9794 if (!isl_space_domain_is_wrapping(map->dim))
9795 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9796 "domain is not a product", return isl_map_free(map));
9798 space = isl_map_get_space(map);
9799 total = isl_space_dim(space, isl_dim_in);
9800 space = isl_space_domain_factor_range(space);
9801 keep = isl_space_dim(space, isl_dim_in);
9802 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9803 map = isl_map_reset_space(map, space);
9805 return map;
9808 /* Given a map A -> [B -> C], extract the map A -> B.
9810 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9812 isl_space *space;
9813 int total, keep;
9815 if (!map)
9816 return NULL;
9817 if (!isl_space_range_is_wrapping(map->dim))
9818 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9819 "range is not a product", return isl_map_free(map));
9821 space = isl_map_get_space(map);
9822 total = isl_space_dim(space, isl_dim_out);
9823 space = isl_space_range_factor_domain(space);
9824 keep = isl_space_dim(space, isl_dim_out);
9825 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9826 map = isl_map_reset_space(map, space);
9828 return map;
9831 /* Given a map A -> [B -> C], extract the map A -> C.
9833 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9835 isl_space *space;
9836 int total, keep;
9838 if (!map)
9839 return NULL;
9840 if (!isl_space_range_is_wrapping(map->dim))
9841 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9842 "range is not a product", return isl_map_free(map));
9844 space = isl_map_get_space(map);
9845 total = isl_space_dim(space, isl_dim_out);
9846 space = isl_space_range_factor_range(space);
9847 keep = isl_space_dim(space, isl_dim_out);
9848 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9849 map = isl_map_reset_space(map, space);
9851 return map;
9854 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9856 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9857 __isl_take isl_map *map2)
9859 isl_map *prod;
9861 prod = isl_map_domain_product(map1, map2);
9862 prod = isl_map_flatten_domain(prod);
9863 return prod;
9866 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9868 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9869 __isl_take isl_map *map2)
9871 isl_map *prod;
9873 prod = isl_map_range_product(map1, map2);
9874 prod = isl_map_flatten_range(prod);
9875 return prod;
9878 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9880 int i;
9881 uint32_t hash = isl_hash_init();
9882 unsigned total;
9884 if (!bmap)
9885 return 0;
9886 bmap = isl_basic_map_copy(bmap);
9887 bmap = isl_basic_map_normalize(bmap);
9888 if (!bmap)
9889 return 0;
9890 total = isl_basic_map_total_dim(bmap);
9891 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9892 for (i = 0; i < bmap->n_eq; ++i) {
9893 uint32_t c_hash;
9894 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9895 isl_hash_hash(hash, c_hash);
9897 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9898 for (i = 0; i < bmap->n_ineq; ++i) {
9899 uint32_t c_hash;
9900 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9901 isl_hash_hash(hash, c_hash);
9903 isl_hash_byte(hash, bmap->n_div & 0xFF);
9904 for (i = 0; i < bmap->n_div; ++i) {
9905 uint32_t c_hash;
9906 if (isl_int_is_zero(bmap->div[i][0]))
9907 continue;
9908 isl_hash_byte(hash, i & 0xFF);
9909 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9910 isl_hash_hash(hash, c_hash);
9912 isl_basic_map_free(bmap);
9913 return hash;
9916 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9918 return isl_basic_map_get_hash(bset_to_bmap(bset));
9921 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9923 int i;
9924 uint32_t hash;
9926 if (!map)
9927 return 0;
9928 map = isl_map_copy(map);
9929 map = isl_map_normalize(map);
9930 if (!map)
9931 return 0;
9933 hash = isl_hash_init();
9934 for (i = 0; i < map->n; ++i) {
9935 uint32_t bmap_hash;
9936 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9937 isl_hash_hash(hash, bmap_hash);
9940 isl_map_free(map);
9942 return hash;
9945 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9947 return isl_map_get_hash(set_to_map(set));
9950 /* Return the number of basic maps in the (current) representation of "map".
9952 int isl_map_n_basic_map(__isl_keep isl_map *map)
9954 return map ? map->n : 0;
9957 int isl_set_n_basic_set(__isl_keep isl_set *set)
9959 return set ? set->n : 0;
9962 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
9963 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9965 int i;
9967 if (!map)
9968 return isl_stat_error;
9970 for (i = 0; i < map->n; ++i)
9971 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9972 return isl_stat_error;
9974 return isl_stat_ok;
9977 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
9978 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9980 int i;
9982 if (!set)
9983 return isl_stat_error;
9985 for (i = 0; i < set->n; ++i)
9986 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9987 return isl_stat_error;
9989 return isl_stat_ok;
9992 /* Return a list of basic sets, the union of which is equal to "set".
9994 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
9995 __isl_keep isl_set *set)
9997 int i;
9998 isl_basic_set_list *list;
10000 if (!set)
10001 return NULL;
10003 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10004 for (i = 0; i < set->n; ++i) {
10005 isl_basic_set *bset;
10007 bset = isl_basic_set_copy(set->p[i]);
10008 list = isl_basic_set_list_add(list, bset);
10011 return list;
10014 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10016 isl_space *dim;
10018 if (!bset)
10019 return NULL;
10021 bset = isl_basic_set_cow(bset);
10022 if (!bset)
10023 return NULL;
10025 dim = isl_basic_set_get_space(bset);
10026 dim = isl_space_lift(dim, bset->n_div);
10027 if (!dim)
10028 goto error;
10029 isl_space_free(bset->dim);
10030 bset->dim = dim;
10031 bset->extra -= bset->n_div;
10032 bset->n_div = 0;
10034 bset = isl_basic_set_finalize(bset);
10036 return bset;
10037 error:
10038 isl_basic_set_free(bset);
10039 return NULL;
10042 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10044 int i;
10045 isl_space *dim;
10046 unsigned n_div;
10048 set = isl_set_align_divs(set);
10050 if (!set)
10051 return NULL;
10053 set = isl_set_cow(set);
10054 if (!set)
10055 return NULL;
10057 n_div = set->p[0]->n_div;
10058 dim = isl_set_get_space(set);
10059 dim = isl_space_lift(dim, n_div);
10060 if (!dim)
10061 goto error;
10062 isl_space_free(set->dim);
10063 set->dim = dim;
10065 for (i = 0; i < set->n; ++i) {
10066 set->p[i] = isl_basic_set_lift(set->p[i]);
10067 if (!set->p[i])
10068 goto error;
10071 return set;
10072 error:
10073 isl_set_free(set);
10074 return NULL;
10077 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10079 unsigned dim;
10080 int size = 0;
10082 if (!bset)
10083 return -1;
10085 dim = isl_basic_set_total_dim(bset);
10086 size += bset->n_eq * (1 + dim);
10087 size += bset->n_ineq * (1 + dim);
10088 size += bset->n_div * (2 + dim);
10090 return size;
10093 int isl_set_size(__isl_keep isl_set *set)
10095 int i;
10096 int size = 0;
10098 if (!set)
10099 return -1;
10101 for (i = 0; i < set->n; ++i)
10102 size += isl_basic_set_size(set->p[i]);
10104 return size;
10107 /* Check if there is any lower bound (if lower == 0) and/or upper
10108 * bound (if upper == 0) on the specified dim.
10110 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10111 enum isl_dim_type type, unsigned pos, int lower, int upper)
10113 int i;
10115 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10116 return isl_bool_error;
10118 pos += isl_basic_map_offset(bmap, type);
10120 for (i = 0; i < bmap->n_div; ++i) {
10121 if (isl_int_is_zero(bmap->div[i][0]))
10122 continue;
10123 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10124 return isl_bool_true;
10127 for (i = 0; i < bmap->n_eq; ++i)
10128 if (!isl_int_is_zero(bmap->eq[i][pos]))
10129 return isl_bool_true;
10131 for (i = 0; i < bmap->n_ineq; ++i) {
10132 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10133 if (sgn > 0)
10134 lower = 1;
10135 if (sgn < 0)
10136 upper = 1;
10139 return lower && upper;
10142 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10143 enum isl_dim_type type, unsigned pos)
10145 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10148 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10149 enum isl_dim_type type, unsigned pos)
10151 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10154 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10155 enum isl_dim_type type, unsigned pos)
10157 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10160 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10161 enum isl_dim_type type, unsigned pos)
10163 int i;
10165 if (!map)
10166 return isl_bool_error;
10168 for (i = 0; i < map->n; ++i) {
10169 isl_bool bounded;
10170 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10171 if (bounded < 0 || !bounded)
10172 return bounded;
10175 return isl_bool_true;
10178 /* Return true if the specified dim is involved in both an upper bound
10179 * and a lower bound.
10181 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10182 enum isl_dim_type type, unsigned pos)
10184 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10187 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10189 static isl_bool has_any_bound(__isl_keep isl_map *map,
10190 enum isl_dim_type type, unsigned pos,
10191 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10192 enum isl_dim_type type, unsigned pos))
10194 int i;
10196 if (!map)
10197 return isl_bool_error;
10199 for (i = 0; i < map->n; ++i) {
10200 isl_bool bounded;
10201 bounded = fn(map->p[i], type, pos);
10202 if (bounded < 0 || bounded)
10203 return bounded;
10206 return isl_bool_false;
10209 /* Return 1 if the specified dim is involved in any lower bound.
10211 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10212 enum isl_dim_type type, unsigned pos)
10214 return has_any_bound(set, type, pos,
10215 &isl_basic_map_dim_has_lower_bound);
10218 /* Return 1 if the specified dim is involved in any upper bound.
10220 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10221 enum isl_dim_type type, unsigned pos)
10223 return has_any_bound(set, type, pos,
10224 &isl_basic_map_dim_has_upper_bound);
10227 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10229 static isl_bool has_bound(__isl_keep isl_map *map,
10230 enum isl_dim_type type, unsigned pos,
10231 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10232 enum isl_dim_type type, unsigned pos))
10234 int i;
10236 if (!map)
10237 return isl_bool_error;
10239 for (i = 0; i < map->n; ++i) {
10240 isl_bool bounded;
10241 bounded = fn(map->p[i], type, pos);
10242 if (bounded < 0 || !bounded)
10243 return bounded;
10246 return isl_bool_true;
10249 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10251 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10252 enum isl_dim_type type, unsigned pos)
10254 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10257 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10259 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10260 enum isl_dim_type type, unsigned pos)
10262 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10265 /* For each of the "n" variables starting at "first", determine
10266 * the sign of the variable and put the results in the first "n"
10267 * elements of the array "signs".
10268 * Sign
10269 * 1 means that the variable is non-negative
10270 * -1 means that the variable is non-positive
10271 * 0 means the variable attains both positive and negative values.
10273 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10274 unsigned first, unsigned n, int *signs)
10276 isl_vec *bound = NULL;
10277 struct isl_tab *tab = NULL;
10278 struct isl_tab_undo *snap;
10279 int i;
10281 if (!bset || !signs)
10282 return isl_stat_error;
10284 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10285 tab = isl_tab_from_basic_set(bset, 0);
10286 if (!bound || !tab)
10287 goto error;
10289 isl_seq_clr(bound->el, bound->size);
10290 isl_int_set_si(bound->el[0], -1);
10292 snap = isl_tab_snap(tab);
10293 for (i = 0; i < n; ++i) {
10294 int empty;
10296 isl_int_set_si(bound->el[1 + first + i], -1);
10297 if (isl_tab_add_ineq(tab, bound->el) < 0)
10298 goto error;
10299 empty = tab->empty;
10300 isl_int_set_si(bound->el[1 + first + i], 0);
10301 if (isl_tab_rollback(tab, snap) < 0)
10302 goto error;
10304 if (empty) {
10305 signs[i] = 1;
10306 continue;
10309 isl_int_set_si(bound->el[1 + first + i], 1);
10310 if (isl_tab_add_ineq(tab, bound->el) < 0)
10311 goto error;
10312 empty = tab->empty;
10313 isl_int_set_si(bound->el[1 + first + i], 0);
10314 if (isl_tab_rollback(tab, snap) < 0)
10315 goto error;
10317 signs[i] = empty ? -1 : 0;
10320 isl_tab_free(tab);
10321 isl_vec_free(bound);
10322 return isl_stat_ok;
10323 error:
10324 isl_tab_free(tab);
10325 isl_vec_free(bound);
10326 return isl_stat_error;
10329 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10330 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10332 if (!bset || !signs)
10333 return isl_stat_error;
10334 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10335 return isl_stat_error);
10337 first += pos(bset->dim, type) - 1;
10338 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10341 /* Is it possible for the integer division "div" to depend (possibly
10342 * indirectly) on any output dimensions?
10344 * If the div is undefined, then we conservatively assume that it
10345 * may depend on them.
10346 * Otherwise, we check if it actually depends on them or on any integer
10347 * divisions that may depend on them.
10349 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10351 int i;
10352 unsigned n_out, o_out;
10353 unsigned n_div, o_div;
10355 if (isl_int_is_zero(bmap->div[div][0]))
10356 return isl_bool_true;
10358 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10359 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10361 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10362 return isl_bool_true;
10364 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10365 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10367 for (i = 0; i < n_div; ++i) {
10368 isl_bool may_involve;
10370 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10371 continue;
10372 may_involve = div_may_involve_output(bmap, i);
10373 if (may_involve < 0 || may_involve)
10374 return may_involve;
10377 return isl_bool_false;
10380 /* Return the first integer division of "bmap" in the range
10381 * [first, first + n[ that may depend on any output dimensions and
10382 * that has a non-zero coefficient in "c" (where the first coefficient
10383 * in "c" corresponds to integer division "first").
10385 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10386 isl_int *c, int first, int n)
10388 int k;
10390 if (!bmap)
10391 return -1;
10393 for (k = first; k < first + n; ++k) {
10394 isl_bool may_involve;
10396 if (isl_int_is_zero(c[k]))
10397 continue;
10398 may_involve = div_may_involve_output(bmap, k);
10399 if (may_involve < 0)
10400 return -1;
10401 if (may_involve)
10402 return k;
10405 return first + n;
10408 /* Look for a pair of inequality constraints in "bmap" of the form
10410 * -l + i >= 0 or i >= l
10411 * and
10412 * n + l - i >= 0 or i <= l + n
10414 * with n < "m" and i the output dimension at position "pos".
10415 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10416 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10417 * and earlier output dimensions, as well as integer divisions that do
10418 * not involve any of the output dimensions.
10420 * Return the index of the first inequality constraint or bmap->n_ineq
10421 * if no such pair can be found.
10423 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10424 int pos, isl_int m)
10426 int i, j;
10427 isl_ctx *ctx;
10428 unsigned total;
10429 unsigned n_div, o_div;
10430 unsigned n_out, o_out;
10431 int less;
10433 if (!bmap)
10434 return -1;
10436 ctx = isl_basic_map_get_ctx(bmap);
10437 total = isl_basic_map_total_dim(bmap);
10438 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10439 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10440 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10441 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10442 for (i = 0; i < bmap->n_ineq; ++i) {
10443 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10444 continue;
10445 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10446 n_out - (pos + 1)) != -1)
10447 continue;
10448 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10449 0, n_div) < n_div)
10450 continue;
10451 for (j = i + 1; j < bmap->n_ineq; ++j) {
10452 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10453 ctx->one))
10454 continue;
10455 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10456 bmap->ineq[j] + 1, total))
10457 continue;
10458 break;
10460 if (j >= bmap->n_ineq)
10461 continue;
10462 isl_int_add(bmap->ineq[i][0],
10463 bmap->ineq[i][0], bmap->ineq[j][0]);
10464 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10465 isl_int_sub(bmap->ineq[i][0],
10466 bmap->ineq[i][0], bmap->ineq[j][0]);
10467 if (!less)
10468 continue;
10469 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10470 return i;
10471 else
10472 return j;
10475 return bmap->n_ineq;
10478 /* Return the index of the equality of "bmap" that defines
10479 * the output dimension "pos" in terms of earlier dimensions.
10480 * The equality may also involve integer divisions, as long
10481 * as those integer divisions are defined in terms of
10482 * parameters or input dimensions.
10483 * In this case, *div is set to the number of integer divisions and
10484 * *ineq is set to the number of inequality constraints (provided
10485 * div and ineq are not NULL).
10487 * The equality may also involve a single integer division involving
10488 * the output dimensions (typically only output dimension "pos") as
10489 * long as the coefficient of output dimension "pos" is 1 or -1 and
10490 * there is a pair of constraints i >= l and i <= l + n, with i referring
10491 * to output dimension "pos", l an expression involving only earlier
10492 * dimensions and n smaller than the coefficient of the integer division
10493 * in the equality. In this case, the output dimension can be defined
10494 * in terms of a modulo expression that does not involve the integer division.
10495 * *div is then set to this single integer division and
10496 * *ineq is set to the index of constraint i >= l.
10498 * Return bmap->n_eq if there is no such equality.
10499 * Return -1 on error.
10501 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10502 int pos, int *div, int *ineq)
10504 int j, k, l;
10505 unsigned n_out, o_out;
10506 unsigned n_div, o_div;
10508 if (!bmap)
10509 return -1;
10511 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10512 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10513 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10514 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10516 if (ineq)
10517 *ineq = bmap->n_ineq;
10518 if (div)
10519 *div = n_div;
10520 for (j = 0; j < bmap->n_eq; ++j) {
10521 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10522 continue;
10523 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10524 n_out - (pos + 1)) != -1)
10525 continue;
10526 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10527 0, n_div);
10528 if (k >= n_div)
10529 return j;
10530 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10531 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10532 continue;
10533 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10534 k + 1, n_div - (k+1)) < n_div)
10535 continue;
10536 l = find_modulo_constraint_pair(bmap, pos,
10537 bmap->eq[j][o_div + k]);
10538 if (l < 0)
10539 return -1;
10540 if (l >= bmap->n_ineq)
10541 continue;
10542 if (div)
10543 *div = k;
10544 if (ineq)
10545 *ineq = l;
10546 return j;
10549 return bmap->n_eq;
10552 /* Check if the given basic map is obviously single-valued.
10553 * In particular, for each output dimension, check that there is
10554 * an equality that defines the output dimension in terms of
10555 * earlier dimensions.
10557 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10559 int i;
10560 unsigned n_out;
10562 if (!bmap)
10563 return isl_bool_error;
10565 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10567 for (i = 0; i < n_out; ++i) {
10568 int eq;
10570 eq = isl_basic_map_output_defining_equality(bmap, i,
10571 NULL, NULL);
10572 if (eq < 0)
10573 return isl_bool_error;
10574 if (eq >= bmap->n_eq)
10575 return isl_bool_false;
10578 return isl_bool_true;
10581 /* Check if the given basic map is single-valued.
10582 * We simply compute
10584 * M \circ M^-1
10586 * and check if the result is a subset of the identity mapping.
10588 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10590 isl_space *space;
10591 isl_basic_map *test;
10592 isl_basic_map *id;
10593 isl_bool sv;
10595 sv = isl_basic_map_plain_is_single_valued(bmap);
10596 if (sv < 0 || sv)
10597 return sv;
10599 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10600 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10602 space = isl_basic_map_get_space(bmap);
10603 space = isl_space_map_from_set(isl_space_range(space));
10604 id = isl_basic_map_identity(space);
10606 sv = isl_basic_map_is_subset(test, id);
10608 isl_basic_map_free(test);
10609 isl_basic_map_free(id);
10611 return sv;
10614 /* Check if the given map is obviously single-valued.
10616 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10618 if (!map)
10619 return isl_bool_error;
10620 if (map->n == 0)
10621 return isl_bool_true;
10622 if (map->n >= 2)
10623 return isl_bool_false;
10625 return isl_basic_map_plain_is_single_valued(map->p[0]);
10628 /* Check if the given map is single-valued.
10629 * We simply compute
10631 * M \circ M^-1
10633 * and check if the result is a subset of the identity mapping.
10635 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10637 isl_space *dim;
10638 isl_map *test;
10639 isl_map *id;
10640 isl_bool sv;
10642 sv = isl_map_plain_is_single_valued(map);
10643 if (sv < 0 || sv)
10644 return sv;
10646 test = isl_map_reverse(isl_map_copy(map));
10647 test = isl_map_apply_range(test, isl_map_copy(map));
10649 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10650 id = isl_map_identity(dim);
10652 sv = isl_map_is_subset(test, id);
10654 isl_map_free(test);
10655 isl_map_free(id);
10657 return sv;
10660 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10662 isl_bool in;
10664 map = isl_map_copy(map);
10665 map = isl_map_reverse(map);
10666 in = isl_map_is_single_valued(map);
10667 isl_map_free(map);
10669 return in;
10672 /* Check if the given map is obviously injective.
10674 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10676 isl_bool in;
10678 map = isl_map_copy(map);
10679 map = isl_map_reverse(map);
10680 in = isl_map_plain_is_single_valued(map);
10681 isl_map_free(map);
10683 return in;
10686 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10688 isl_bool sv;
10690 sv = isl_map_is_single_valued(map);
10691 if (sv < 0 || !sv)
10692 return sv;
10694 return isl_map_is_injective(map);
10697 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10699 return isl_map_is_single_valued(set_to_map(set));
10702 /* Does "map" only map elements to themselves?
10704 * If the domain and range spaces are different, then "map"
10705 * is considered not to be an identity relation, even if it is empty.
10706 * Otherwise, construct the maximal identity relation and
10707 * check whether "map" is a subset of this relation.
10709 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10711 isl_space *space;
10712 isl_map *id;
10713 isl_bool equal, is_identity;
10715 space = isl_map_get_space(map);
10716 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10717 isl_space_free(space);
10718 if (equal < 0 || !equal)
10719 return equal;
10721 id = isl_map_identity(isl_map_get_space(map));
10722 is_identity = isl_map_is_subset(map, id);
10723 isl_map_free(id);
10725 return is_identity;
10728 int isl_map_is_translation(__isl_keep isl_map *map)
10730 int ok;
10731 isl_set *delta;
10733 delta = isl_map_deltas(isl_map_copy(map));
10734 ok = isl_set_is_singleton(delta);
10735 isl_set_free(delta);
10737 return ok;
10740 static int unique(isl_int *p, unsigned pos, unsigned len)
10742 if (isl_seq_first_non_zero(p, pos) != -1)
10743 return 0;
10744 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10745 return 0;
10746 return 1;
10749 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10751 int i, j;
10752 unsigned nvar;
10753 unsigned ovar;
10755 if (!bset)
10756 return isl_bool_error;
10758 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10759 return isl_bool_false;
10761 nvar = isl_basic_set_dim(bset, isl_dim_set);
10762 ovar = isl_space_offset(bset->dim, isl_dim_set);
10763 for (j = 0; j < nvar; ++j) {
10764 int lower = 0, upper = 0;
10765 for (i = 0; i < bset->n_eq; ++i) {
10766 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10767 continue;
10768 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10769 return isl_bool_false;
10770 break;
10772 if (i < bset->n_eq)
10773 continue;
10774 for (i = 0; i < bset->n_ineq; ++i) {
10775 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10776 continue;
10777 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10778 return isl_bool_false;
10779 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10780 lower = 1;
10781 else
10782 upper = 1;
10784 if (!lower || !upper)
10785 return isl_bool_false;
10788 return isl_bool_true;
10791 isl_bool isl_set_is_box(__isl_keep isl_set *set)
10793 if (!set)
10794 return isl_bool_error;
10795 if (set->n != 1)
10796 return isl_bool_false;
10798 return isl_basic_set_is_box(set->p[0]);
10801 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10803 if (!bset)
10804 return isl_bool_error;
10806 return isl_space_is_wrapping(bset->dim);
10809 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
10811 if (!set)
10812 return isl_bool_error;
10814 return isl_space_is_wrapping(set->dim);
10817 /* Modify the space of "map" through a call to "change".
10818 * If "can_change" is set (not NULL), then first call it to check
10819 * if the modification is allowed, printing the error message "cannot_change"
10820 * if it is not.
10822 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
10823 isl_bool (*can_change)(__isl_keep isl_map *map),
10824 const char *cannot_change,
10825 __isl_give isl_space *(*change)(__isl_take isl_space *space))
10827 isl_bool ok;
10828 isl_space *space;
10830 if (!map)
10831 return NULL;
10833 ok = can_change ? can_change(map) : isl_bool_true;
10834 if (ok < 0)
10835 return isl_map_free(map);
10836 if (!ok)
10837 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
10838 return isl_map_free(map));
10840 space = change(isl_map_get_space(map));
10841 map = isl_map_reset_space(map, space);
10843 return map;
10846 /* Is the domain of "map" a wrapped relation?
10848 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10850 if (!map)
10851 return isl_bool_error;
10853 return isl_space_domain_is_wrapping(map->dim);
10856 /* Is the range of "map" a wrapped relation?
10858 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
10860 if (!map)
10861 return isl_bool_error;
10863 return isl_space_range_is_wrapping(map->dim);
10866 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10868 bmap = isl_basic_map_cow(bmap);
10869 if (!bmap)
10870 return NULL;
10872 bmap->dim = isl_space_wrap(bmap->dim);
10873 if (!bmap->dim)
10874 goto error;
10876 bmap = isl_basic_map_finalize(bmap);
10878 return bset_from_bmap(bmap);
10879 error:
10880 isl_basic_map_free(bmap);
10881 return NULL;
10884 /* Given a map A -> B, return the set (A -> B).
10886 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10888 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
10891 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10893 bset = isl_basic_set_cow(bset);
10894 if (!bset)
10895 return NULL;
10897 bset->dim = isl_space_unwrap(bset->dim);
10898 if (!bset->dim)
10899 goto error;
10901 bset = isl_basic_set_finalize(bset);
10903 return bset_to_bmap(bset);
10904 error:
10905 isl_basic_set_free(bset);
10906 return NULL;
10909 /* Given a set (A -> B), return the map A -> B.
10910 * Error out if "set" is not of the form (A -> B).
10912 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10914 return isl_map_change_space(set, &isl_set_is_wrapping,
10915 "not a wrapping set", &isl_space_unwrap);
10918 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10919 enum isl_dim_type type)
10921 if (!bmap)
10922 return NULL;
10924 if (!isl_space_is_named_or_nested(bmap->dim, type))
10925 return bmap;
10927 bmap = isl_basic_map_cow(bmap);
10928 if (!bmap)
10929 return NULL;
10931 bmap->dim = isl_space_reset(bmap->dim, type);
10932 if (!bmap->dim)
10933 goto error;
10935 bmap = isl_basic_map_finalize(bmap);
10937 return bmap;
10938 error:
10939 isl_basic_map_free(bmap);
10940 return NULL;
10943 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10944 enum isl_dim_type type)
10946 int i;
10948 if (!map)
10949 return NULL;
10951 if (!isl_space_is_named_or_nested(map->dim, type))
10952 return map;
10954 map = isl_map_cow(map);
10955 if (!map)
10956 return NULL;
10958 for (i = 0; i < map->n; ++i) {
10959 map->p[i] = isl_basic_map_reset(map->p[i], type);
10960 if (!map->p[i])
10961 goto error;
10963 map->dim = isl_space_reset(map->dim, type);
10964 if (!map->dim)
10965 goto error;
10967 return map;
10968 error:
10969 isl_map_free(map);
10970 return NULL;
10973 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10975 if (!bmap)
10976 return NULL;
10978 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10979 return bmap;
10981 bmap = isl_basic_map_cow(bmap);
10982 if (!bmap)
10983 return NULL;
10985 bmap->dim = isl_space_flatten(bmap->dim);
10986 if (!bmap->dim)
10987 goto error;
10989 bmap = isl_basic_map_finalize(bmap);
10991 return bmap;
10992 error:
10993 isl_basic_map_free(bmap);
10994 return NULL;
10997 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10999 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11002 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11003 __isl_take isl_basic_map *bmap)
11005 if (!bmap)
11006 return NULL;
11008 if (!bmap->dim->nested[0])
11009 return bmap;
11011 bmap = isl_basic_map_cow(bmap);
11012 if (!bmap)
11013 return NULL;
11015 bmap->dim = isl_space_flatten_domain(bmap->dim);
11016 if (!bmap->dim)
11017 goto error;
11019 bmap = isl_basic_map_finalize(bmap);
11021 return bmap;
11022 error:
11023 isl_basic_map_free(bmap);
11024 return NULL;
11027 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11028 __isl_take isl_basic_map *bmap)
11030 if (!bmap)
11031 return NULL;
11033 if (!bmap->dim->nested[1])
11034 return bmap;
11036 bmap = isl_basic_map_cow(bmap);
11037 if (!bmap)
11038 return NULL;
11040 bmap->dim = isl_space_flatten_range(bmap->dim);
11041 if (!bmap->dim)
11042 goto error;
11044 bmap = isl_basic_map_finalize(bmap);
11046 return bmap;
11047 error:
11048 isl_basic_map_free(bmap);
11049 return NULL;
11052 /* Remove any internal structure from the spaces of domain and range of "map".
11054 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11056 if (!map)
11057 return NULL;
11059 if (!map->dim->nested[0] && !map->dim->nested[1])
11060 return map;
11062 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11065 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11067 return set_from_map(isl_map_flatten(set_to_map(set)));
11070 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11072 isl_space *dim, *flat_dim;
11073 isl_map *map;
11075 dim = isl_set_get_space(set);
11076 flat_dim = isl_space_flatten(isl_space_copy(dim));
11077 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11078 map = isl_map_intersect_domain(map, set);
11080 return map;
11083 /* Remove any internal structure from the space of the domain of "map".
11085 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11087 if (!map)
11088 return NULL;
11090 if (!map->dim->nested[0])
11091 return map;
11093 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11096 /* Remove any internal structure from the space of the range of "map".
11098 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11100 if (!map)
11101 return NULL;
11103 if (!map->dim->nested[1])
11104 return map;
11106 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11109 /* Reorder the dimensions of "bmap" according to the given dim_map
11110 * and set the dimension specification to "dim" and
11111 * perform Gaussian elimination on the result.
11113 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11114 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11116 isl_basic_map *res;
11117 unsigned flags;
11119 bmap = isl_basic_map_cow(bmap);
11120 if (!bmap || !dim || !dim_map)
11121 goto error;
11123 flags = bmap->flags;
11124 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11125 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11126 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11127 res = isl_basic_map_alloc_space(dim,
11128 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11129 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11130 if (res)
11131 res->flags = flags;
11132 res = isl_basic_map_gauss(res, NULL);
11133 res = isl_basic_map_finalize(res);
11134 return res;
11135 error:
11136 free(dim_map);
11137 isl_basic_map_free(bmap);
11138 isl_space_free(dim);
11139 return NULL;
11142 /* Reorder the dimensions of "map" according to given reordering.
11144 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11145 __isl_take isl_reordering *r)
11147 int i;
11148 struct isl_dim_map *dim_map;
11150 map = isl_map_cow(map);
11151 dim_map = isl_dim_map_from_reordering(r);
11152 if (!map || !r || !dim_map)
11153 goto error;
11155 for (i = 0; i < map->n; ++i) {
11156 struct isl_dim_map *dim_map_i;
11158 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11160 map->p[i] = isl_basic_map_realign(map->p[i],
11161 isl_space_copy(r->dim), dim_map_i);
11163 if (!map->p[i])
11164 goto error;
11167 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11169 isl_reordering_free(r);
11170 free(dim_map);
11171 return map;
11172 error:
11173 free(dim_map);
11174 isl_map_free(map);
11175 isl_reordering_free(r);
11176 return NULL;
11179 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11180 __isl_take isl_reordering *r)
11182 return set_from_map(isl_map_realign(set_to_map(set), r));
11185 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11186 __isl_take isl_space *model)
11188 isl_ctx *ctx;
11189 isl_bool aligned;
11191 if (!map || !model)
11192 goto error;
11194 ctx = isl_space_get_ctx(model);
11195 if (!isl_space_has_named_params(model))
11196 isl_die(ctx, isl_error_invalid,
11197 "model has unnamed parameters", goto error);
11198 if (isl_map_check_named_params(map) < 0)
11199 goto error;
11200 aligned = isl_map_space_has_equal_params(map, model);
11201 if (aligned < 0)
11202 goto error;
11203 if (!aligned) {
11204 isl_reordering *exp;
11206 model = isl_space_drop_dims(model, isl_dim_in,
11207 0, isl_space_dim(model, isl_dim_in));
11208 model = isl_space_drop_dims(model, isl_dim_out,
11209 0, isl_space_dim(model, isl_dim_out));
11210 exp = isl_parameter_alignment_reordering(map->dim, model);
11211 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11212 map = isl_map_realign(map, exp);
11215 isl_space_free(model);
11216 return map;
11217 error:
11218 isl_space_free(model);
11219 isl_map_free(map);
11220 return NULL;
11223 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11224 __isl_take isl_space *model)
11226 return isl_map_align_params(set, model);
11229 /* Align the parameters of "bmap" to those of "model", introducing
11230 * additional parameters if needed.
11232 __isl_give isl_basic_map *isl_basic_map_align_params(
11233 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11235 isl_ctx *ctx;
11237 if (!bmap || !model)
11238 goto error;
11240 ctx = isl_space_get_ctx(model);
11241 if (!isl_space_has_named_params(model))
11242 isl_die(ctx, isl_error_invalid,
11243 "model has unnamed parameters", goto error);
11244 if (!isl_space_has_named_params(bmap->dim))
11245 isl_die(ctx, isl_error_invalid,
11246 "relation has unnamed parameters", goto error);
11247 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11248 isl_reordering *exp;
11249 struct isl_dim_map *dim_map;
11251 model = isl_space_drop_dims(model, isl_dim_in,
11252 0, isl_space_dim(model, isl_dim_in));
11253 model = isl_space_drop_dims(model, isl_dim_out,
11254 0, isl_space_dim(model, isl_dim_out));
11255 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11256 exp = isl_reordering_extend_space(exp,
11257 isl_basic_map_get_space(bmap));
11258 dim_map = isl_dim_map_from_reordering(exp);
11259 bmap = isl_basic_map_realign(bmap,
11260 exp ? isl_space_copy(exp->dim) : NULL,
11261 isl_dim_map_extend(dim_map, bmap));
11262 isl_reordering_free(exp);
11263 free(dim_map);
11266 isl_space_free(model);
11267 return bmap;
11268 error:
11269 isl_space_free(model);
11270 isl_basic_map_free(bmap);
11271 return NULL;
11274 /* Do "bset" and "space" have the same parameters?
11276 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11277 __isl_keep isl_space *space)
11279 isl_space *bset_space;
11281 bset_space = isl_basic_set_peek_space(bset);
11282 return isl_space_match(bset_space, isl_dim_param, space, isl_dim_param);
11285 /* Do "map" and "space" have the same parameters?
11287 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11288 __isl_keep isl_space *space)
11290 isl_space *map_space;
11292 map_space = isl_map_peek_space(map);
11293 return isl_space_match(map_space, isl_dim_param, space, isl_dim_param);
11296 /* Do "set" and "space" have the same parameters?
11298 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11299 __isl_keep isl_space *space)
11301 return isl_map_space_has_equal_params(set_to_map(set), space);
11304 /* Align the parameters of "bset" to those of "model", introducing
11305 * additional parameters if needed.
11307 __isl_give isl_basic_set *isl_basic_set_align_params(
11308 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11310 return isl_basic_map_align_params(bset, model);
11313 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11314 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11315 enum isl_dim_type c2, enum isl_dim_type c3,
11316 enum isl_dim_type c4, enum isl_dim_type c5)
11318 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11319 struct isl_mat *mat;
11320 int i, j, k;
11321 int pos;
11323 if (!bmap)
11324 return NULL;
11325 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11326 isl_basic_map_total_dim(bmap) + 1);
11327 if (!mat)
11328 return NULL;
11329 for (i = 0; i < bmap->n_eq; ++i)
11330 for (j = 0, pos = 0; j < 5; ++j) {
11331 int off = isl_basic_map_offset(bmap, c[j]);
11332 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11333 isl_int_set(mat->row[i][pos],
11334 bmap->eq[i][off + k]);
11335 ++pos;
11339 return mat;
11342 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11343 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11344 enum isl_dim_type c2, enum isl_dim_type c3,
11345 enum isl_dim_type c4, enum isl_dim_type c5)
11347 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11348 struct isl_mat *mat;
11349 int i, j, k;
11350 int pos;
11352 if (!bmap)
11353 return NULL;
11354 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11355 isl_basic_map_total_dim(bmap) + 1);
11356 if (!mat)
11357 return NULL;
11358 for (i = 0; i < bmap->n_ineq; ++i)
11359 for (j = 0, pos = 0; j < 5; ++j) {
11360 int off = isl_basic_map_offset(bmap, c[j]);
11361 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11362 isl_int_set(mat->row[i][pos],
11363 bmap->ineq[i][off + k]);
11364 ++pos;
11368 return mat;
11371 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11372 __isl_take isl_space *dim,
11373 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11374 enum isl_dim_type c2, enum isl_dim_type c3,
11375 enum isl_dim_type c4, enum isl_dim_type c5)
11377 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11378 isl_basic_map *bmap;
11379 unsigned total;
11380 unsigned extra;
11381 int i, j, k, l;
11382 int pos;
11384 if (!dim || !eq || !ineq)
11385 goto error;
11387 if (eq->n_col != ineq->n_col)
11388 isl_die(dim->ctx, isl_error_invalid,
11389 "equalities and inequalities matrices should have "
11390 "same number of columns", goto error);
11392 total = 1 + isl_space_dim(dim, isl_dim_all);
11394 if (eq->n_col < total)
11395 isl_die(dim->ctx, isl_error_invalid,
11396 "number of columns too small", goto error);
11398 extra = eq->n_col - total;
11400 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11401 eq->n_row, ineq->n_row);
11402 if (!bmap)
11403 goto error;
11404 for (i = 0; i < extra; ++i) {
11405 k = isl_basic_map_alloc_div(bmap);
11406 if (k < 0)
11407 goto error;
11408 isl_int_set_si(bmap->div[k][0], 0);
11410 for (i = 0; i < eq->n_row; ++i) {
11411 l = isl_basic_map_alloc_equality(bmap);
11412 if (l < 0)
11413 goto error;
11414 for (j = 0, pos = 0; j < 5; ++j) {
11415 int off = isl_basic_map_offset(bmap, c[j]);
11416 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11417 isl_int_set(bmap->eq[l][off + k],
11418 eq->row[i][pos]);
11419 ++pos;
11423 for (i = 0; i < ineq->n_row; ++i) {
11424 l = isl_basic_map_alloc_inequality(bmap);
11425 if (l < 0)
11426 goto error;
11427 for (j = 0, pos = 0; j < 5; ++j) {
11428 int off = isl_basic_map_offset(bmap, c[j]);
11429 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11430 isl_int_set(bmap->ineq[l][off + k],
11431 ineq->row[i][pos]);
11432 ++pos;
11437 isl_space_free(dim);
11438 isl_mat_free(eq);
11439 isl_mat_free(ineq);
11441 bmap = isl_basic_map_simplify(bmap);
11442 return isl_basic_map_finalize(bmap);
11443 error:
11444 isl_space_free(dim);
11445 isl_mat_free(eq);
11446 isl_mat_free(ineq);
11447 return NULL;
11450 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11451 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11452 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11454 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11455 c1, c2, c3, c4, isl_dim_in);
11458 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11459 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11460 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11462 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11463 c1, c2, c3, c4, isl_dim_in);
11466 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11467 __isl_take isl_space *dim,
11468 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11469 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11471 isl_basic_map *bmap;
11472 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11473 c1, c2, c3, c4, isl_dim_in);
11474 return bset_from_bmap(bmap);
11477 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11479 if (!bmap)
11480 return isl_bool_error;
11482 return isl_space_can_zip(bmap->dim);
11485 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11487 if (!map)
11488 return isl_bool_error;
11490 return isl_space_can_zip(map->dim);
11493 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11494 * (A -> C) -> (B -> D).
11496 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11498 unsigned pos;
11499 unsigned n1;
11500 unsigned n2;
11502 if (!bmap)
11503 return NULL;
11505 if (!isl_basic_map_can_zip(bmap))
11506 isl_die(bmap->ctx, isl_error_invalid,
11507 "basic map cannot be zipped", goto error);
11508 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11509 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11510 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11511 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11512 bmap = isl_basic_map_cow(bmap);
11513 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11514 if (!bmap)
11515 return NULL;
11516 bmap->dim = isl_space_zip(bmap->dim);
11517 if (!bmap->dim)
11518 goto error;
11519 bmap = isl_basic_map_mark_final(bmap);
11520 return bmap;
11521 error:
11522 isl_basic_map_free(bmap);
11523 return NULL;
11526 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11527 * (A -> C) -> (B -> D).
11529 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11531 int i;
11533 if (!map)
11534 return NULL;
11536 if (!isl_map_can_zip(map))
11537 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11538 goto error);
11540 map = isl_map_cow(map);
11541 if (!map)
11542 return NULL;
11544 for (i = 0; i < map->n; ++i) {
11545 map->p[i] = isl_basic_map_zip(map->p[i]);
11546 if (!map->p[i])
11547 goto error;
11550 map->dim = isl_space_zip(map->dim);
11551 if (!map->dim)
11552 goto error;
11554 return map;
11555 error:
11556 isl_map_free(map);
11557 return NULL;
11560 /* Can we apply isl_basic_map_curry to "bmap"?
11561 * That is, does it have a nested relation in its domain?
11563 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11565 if (!bmap)
11566 return isl_bool_error;
11568 return isl_space_can_curry(bmap->dim);
11571 /* Can we apply isl_map_curry to "map"?
11572 * That is, does it have a nested relation in its domain?
11574 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11576 if (!map)
11577 return isl_bool_error;
11579 return isl_space_can_curry(map->dim);
11582 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11583 * A -> (B -> C).
11585 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11588 if (!bmap)
11589 return NULL;
11591 if (!isl_basic_map_can_curry(bmap))
11592 isl_die(bmap->ctx, isl_error_invalid,
11593 "basic map cannot be curried", goto error);
11594 bmap = isl_basic_map_cow(bmap);
11595 if (!bmap)
11596 return NULL;
11597 bmap->dim = isl_space_curry(bmap->dim);
11598 if (!bmap->dim)
11599 goto error;
11600 bmap = isl_basic_map_mark_final(bmap);
11601 return bmap;
11602 error:
11603 isl_basic_map_free(bmap);
11604 return NULL;
11607 /* Given a map (A -> B) -> C, return the corresponding map
11608 * A -> (B -> C).
11610 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11612 return isl_map_change_space(map, &isl_map_can_curry,
11613 "map cannot be curried", &isl_space_curry);
11616 /* Can isl_map_range_curry be applied to "map"?
11617 * That is, does it have a nested relation in its range,
11618 * the domain of which is itself a nested relation?
11620 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11622 if (!map)
11623 return isl_bool_error;
11625 return isl_space_can_range_curry(map->dim);
11628 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11629 * A -> (B -> (C -> D)).
11631 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11633 return isl_map_change_space(map, &isl_map_can_range_curry,
11634 "map range cannot be curried",
11635 &isl_space_range_curry);
11638 /* Can we apply isl_basic_map_uncurry to "bmap"?
11639 * That is, does it have a nested relation in its domain?
11641 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11643 if (!bmap)
11644 return isl_bool_error;
11646 return isl_space_can_uncurry(bmap->dim);
11649 /* Can we apply isl_map_uncurry to "map"?
11650 * That is, does it have a nested relation in its domain?
11652 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11654 if (!map)
11655 return isl_bool_error;
11657 return isl_space_can_uncurry(map->dim);
11660 /* Given a basic map A -> (B -> C), return the corresponding basic map
11661 * (A -> B) -> C.
11663 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11666 if (!bmap)
11667 return NULL;
11669 if (!isl_basic_map_can_uncurry(bmap))
11670 isl_die(bmap->ctx, isl_error_invalid,
11671 "basic map cannot be uncurried",
11672 return isl_basic_map_free(bmap));
11673 bmap = isl_basic_map_cow(bmap);
11674 if (!bmap)
11675 return NULL;
11676 bmap->dim = isl_space_uncurry(bmap->dim);
11677 if (!bmap->dim)
11678 return isl_basic_map_free(bmap);
11679 bmap = isl_basic_map_mark_final(bmap);
11680 return bmap;
11683 /* Given a map A -> (B -> C), return the corresponding map
11684 * (A -> B) -> C.
11686 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11688 return isl_map_change_space(map, &isl_map_can_uncurry,
11689 "map cannot be uncurried", &isl_space_uncurry);
11692 /* Construct a basic map mapping the domain of the affine expression
11693 * to a one-dimensional range prescribed by the affine expression.
11694 * If "rational" is set, then construct a rational basic map.
11696 * A NaN affine expression cannot be converted to a basic map.
11698 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
11699 __isl_take isl_aff *aff, int rational)
11701 int k;
11702 int pos;
11703 isl_bool is_nan;
11704 isl_local_space *ls;
11705 isl_basic_map *bmap = NULL;
11707 if (!aff)
11708 return NULL;
11709 is_nan = isl_aff_is_nan(aff);
11710 if (is_nan < 0)
11711 goto error;
11712 if (is_nan)
11713 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11714 "cannot convert NaN", goto error);
11716 ls = isl_aff_get_local_space(aff);
11717 bmap = isl_basic_map_from_local_space(ls);
11718 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11719 k = isl_basic_map_alloc_equality(bmap);
11720 if (k < 0)
11721 goto error;
11723 pos = isl_basic_map_offset(bmap, isl_dim_out);
11724 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11725 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11726 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11727 aff->v->size - (pos + 1));
11729 isl_aff_free(aff);
11730 if (rational)
11731 bmap = isl_basic_map_set_rational(bmap);
11732 bmap = isl_basic_map_finalize(bmap);
11733 return bmap;
11734 error:
11735 isl_aff_free(aff);
11736 isl_basic_map_free(bmap);
11737 return NULL;
11740 /* Construct a basic map mapping the domain of the affine expression
11741 * to a one-dimensional range prescribed by the affine expression.
11743 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11745 return isl_basic_map_from_aff2(aff, 0);
11748 /* Construct a map mapping the domain of the affine expression
11749 * to a one-dimensional range prescribed by the affine expression.
11751 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11753 isl_basic_map *bmap;
11755 bmap = isl_basic_map_from_aff(aff);
11756 return isl_map_from_basic_map(bmap);
11759 /* Construct a basic map mapping the domain the multi-affine expression
11760 * to its range, with each dimension in the range equated to the
11761 * corresponding affine expression.
11762 * If "rational" is set, then construct a rational basic map.
11764 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
11765 __isl_take isl_multi_aff *maff, int rational)
11767 int i;
11768 isl_space *space;
11769 isl_basic_map *bmap;
11771 if (!maff)
11772 return NULL;
11774 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11775 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11776 "invalid space", goto error);
11778 space = isl_space_domain(isl_multi_aff_get_space(maff));
11779 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11780 if (rational)
11781 bmap = isl_basic_map_set_rational(bmap);
11783 for (i = 0; i < maff->n; ++i) {
11784 isl_aff *aff;
11785 isl_basic_map *bmap_i;
11787 aff = isl_aff_copy(maff->p[i]);
11788 bmap_i = isl_basic_map_from_aff2(aff, rational);
11790 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11793 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11795 isl_multi_aff_free(maff);
11796 return bmap;
11797 error:
11798 isl_multi_aff_free(maff);
11799 return NULL;
11802 /* Construct a basic map mapping the domain the multi-affine expression
11803 * to its range, with each dimension in the range equated to the
11804 * corresponding affine expression.
11806 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11807 __isl_take isl_multi_aff *ma)
11809 return isl_basic_map_from_multi_aff2(ma, 0);
11812 /* Construct a map mapping the domain the multi-affine expression
11813 * to its range, with each dimension in the range equated to the
11814 * corresponding affine expression.
11816 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11818 isl_basic_map *bmap;
11820 bmap = isl_basic_map_from_multi_aff(maff);
11821 return isl_map_from_basic_map(bmap);
11824 /* Construct a basic map mapping a domain in the given space to
11825 * to an n-dimensional range, with n the number of elements in the list,
11826 * where each coordinate in the range is prescribed by the
11827 * corresponding affine expression.
11828 * The domains of all affine expressions in the list are assumed to match
11829 * domain_dim.
11831 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11832 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11834 int i;
11835 isl_space *dim;
11836 isl_basic_map *bmap;
11838 if (!list)
11839 return NULL;
11841 dim = isl_space_from_domain(domain_dim);
11842 bmap = isl_basic_map_universe(dim);
11844 for (i = 0; i < list->n; ++i) {
11845 isl_aff *aff;
11846 isl_basic_map *bmap_i;
11848 aff = isl_aff_copy(list->p[i]);
11849 bmap_i = isl_basic_map_from_aff(aff);
11851 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11854 isl_aff_list_free(list);
11855 return bmap;
11858 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11859 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11861 return isl_map_equate(set, type1, pos1, type2, pos2);
11864 /* Construct a basic map where the given dimensions are equal to each other.
11866 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11867 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11869 isl_basic_map *bmap = NULL;
11870 int i;
11872 if (!space)
11873 return NULL;
11875 if (pos1 >= isl_space_dim(space, type1))
11876 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11877 "index out of bounds", goto error);
11878 if (pos2 >= isl_space_dim(space, type2))
11879 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11880 "index out of bounds", goto error);
11882 if (type1 == type2 && pos1 == pos2)
11883 return isl_basic_map_universe(space);
11885 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11886 i = isl_basic_map_alloc_equality(bmap);
11887 if (i < 0)
11888 goto error;
11889 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11890 pos1 += isl_basic_map_offset(bmap, type1);
11891 pos2 += isl_basic_map_offset(bmap, type2);
11892 isl_int_set_si(bmap->eq[i][pos1], -1);
11893 isl_int_set_si(bmap->eq[i][pos2], 1);
11894 bmap = isl_basic_map_finalize(bmap);
11895 isl_space_free(space);
11896 return bmap;
11897 error:
11898 isl_space_free(space);
11899 isl_basic_map_free(bmap);
11900 return NULL;
11903 /* Add a constraint imposing that the given two dimensions are equal.
11905 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11906 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11908 isl_basic_map *eq;
11910 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11912 bmap = isl_basic_map_intersect(bmap, eq);
11914 return bmap;
11917 /* Add a constraint imposing that the given two dimensions are equal.
11919 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11920 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11922 isl_basic_map *bmap;
11924 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11926 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11928 return map;
11931 /* Add a constraint imposing that the given two dimensions have opposite values.
11933 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11934 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11936 isl_basic_map *bmap = NULL;
11937 int i;
11939 if (!map)
11940 return NULL;
11942 if (pos1 >= isl_map_dim(map, type1))
11943 isl_die(map->ctx, isl_error_invalid,
11944 "index out of bounds", goto error);
11945 if (pos2 >= isl_map_dim(map, type2))
11946 isl_die(map->ctx, isl_error_invalid,
11947 "index out of bounds", goto error);
11949 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11950 i = isl_basic_map_alloc_equality(bmap);
11951 if (i < 0)
11952 goto error;
11953 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11954 pos1 += isl_basic_map_offset(bmap, type1);
11955 pos2 += isl_basic_map_offset(bmap, type2);
11956 isl_int_set_si(bmap->eq[i][pos1], 1);
11957 isl_int_set_si(bmap->eq[i][pos2], 1);
11958 bmap = isl_basic_map_finalize(bmap);
11960 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11962 return map;
11963 error:
11964 isl_basic_map_free(bmap);
11965 isl_map_free(map);
11966 return NULL;
11969 /* Construct a constraint imposing that the value of the first dimension is
11970 * greater than or equal to that of the second.
11972 static __isl_give isl_constraint *constraint_order_ge(
11973 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11974 enum isl_dim_type type2, int pos2)
11976 isl_constraint *c;
11978 if (!space)
11979 return NULL;
11981 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
11983 if (pos1 >= isl_constraint_dim(c, type1))
11984 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11985 "index out of bounds", return isl_constraint_free(c));
11986 if (pos2 >= isl_constraint_dim(c, type2))
11987 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11988 "index out of bounds", return isl_constraint_free(c));
11990 if (type1 == type2 && pos1 == pos2)
11991 return c;
11993 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11994 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11996 return c;
11999 /* Add a constraint imposing that the value of the first dimension is
12000 * greater than or equal to that of the second.
12002 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12003 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12005 isl_constraint *c;
12006 isl_space *space;
12008 if (type1 == type2 && pos1 == pos2)
12009 return bmap;
12010 space = isl_basic_map_get_space(bmap);
12011 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12012 bmap = isl_basic_map_add_constraint(bmap, c);
12014 return bmap;
12017 /* Add a constraint imposing that the value of the first dimension is
12018 * greater than or equal to that of the second.
12020 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12021 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12023 isl_constraint *c;
12024 isl_space *space;
12026 if (type1 == type2 && pos1 == pos2)
12027 return map;
12028 space = isl_map_get_space(map);
12029 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12030 map = isl_map_add_constraint(map, c);
12032 return map;
12035 /* Add a constraint imposing that the value of the first dimension is
12036 * less than or equal to that of the second.
12038 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12039 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12041 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12044 /* Construct a basic map where the value of the first dimension is
12045 * greater than that of the second.
12047 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12048 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12050 isl_basic_map *bmap = NULL;
12051 int i;
12053 if (!space)
12054 return NULL;
12056 if (pos1 >= isl_space_dim(space, type1))
12057 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12058 "index out of bounds", goto error);
12059 if (pos2 >= isl_space_dim(space, type2))
12060 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12061 "index out of bounds", goto error);
12063 if (type1 == type2 && pos1 == pos2)
12064 return isl_basic_map_empty(space);
12066 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12067 i = isl_basic_map_alloc_inequality(bmap);
12068 if (i < 0)
12069 return isl_basic_map_free(bmap);
12070 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12071 pos1 += isl_basic_map_offset(bmap, type1);
12072 pos2 += isl_basic_map_offset(bmap, type2);
12073 isl_int_set_si(bmap->ineq[i][pos1], 1);
12074 isl_int_set_si(bmap->ineq[i][pos2], -1);
12075 isl_int_set_si(bmap->ineq[i][0], -1);
12076 bmap = isl_basic_map_finalize(bmap);
12078 return bmap;
12079 error:
12080 isl_space_free(space);
12081 isl_basic_map_free(bmap);
12082 return NULL;
12085 /* Add a constraint imposing that the value of the first dimension is
12086 * greater than that of the second.
12088 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12089 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12091 isl_basic_map *gt;
12093 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12095 bmap = isl_basic_map_intersect(bmap, gt);
12097 return bmap;
12100 /* Add a constraint imposing that the value of the first dimension is
12101 * greater than that of the second.
12103 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12104 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12106 isl_basic_map *bmap;
12108 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12110 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12112 return map;
12115 /* Add a constraint imposing that the value of the first dimension is
12116 * smaller than that of the second.
12118 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12119 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12121 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12124 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12125 int pos)
12127 isl_aff *div;
12128 isl_local_space *ls;
12130 if (!bmap)
12131 return NULL;
12133 if (!isl_basic_map_divs_known(bmap))
12134 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12135 "some divs are unknown", return NULL);
12137 ls = isl_basic_map_get_local_space(bmap);
12138 div = isl_local_space_get_div(ls, pos);
12139 isl_local_space_free(ls);
12141 return div;
12144 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12145 int pos)
12147 return isl_basic_map_get_div(bset, pos);
12150 /* Plug in "subs" for dimension "type", "pos" of "bset".
12152 * Let i be the dimension to replace and let "subs" be of the form
12154 * f/d
12156 * Any integer division with a non-zero coefficient for i,
12158 * floor((a i + g)/m)
12160 * is replaced by
12162 * floor((a f + d g)/(m d))
12164 * Constraints of the form
12166 * a i + g
12168 * are replaced by
12170 * a f + d g
12172 * We currently require that "subs" is an integral expression.
12173 * Handling rational expressions may require us to add stride constraints
12174 * as we do in isl_basic_set_preimage_multi_aff.
12176 __isl_give isl_basic_set *isl_basic_set_substitute(
12177 __isl_take isl_basic_set *bset,
12178 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12180 int i;
12181 isl_int v;
12182 isl_ctx *ctx;
12184 if (bset && isl_basic_set_plain_is_empty(bset))
12185 return bset;
12187 bset = isl_basic_set_cow(bset);
12188 if (!bset || !subs)
12189 goto error;
12191 ctx = isl_basic_set_get_ctx(bset);
12192 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12193 isl_die(ctx, isl_error_invalid,
12194 "spaces don't match", goto error);
12195 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12196 isl_die(ctx, isl_error_unsupported,
12197 "cannot handle divs yet", goto error);
12198 if (!isl_int_is_one(subs->v->el[0]))
12199 isl_die(ctx, isl_error_invalid,
12200 "can only substitute integer expressions", goto error);
12202 pos += isl_basic_set_offset(bset, type);
12204 isl_int_init(v);
12206 for (i = 0; i < bset->n_eq; ++i) {
12207 if (isl_int_is_zero(bset->eq[i][pos]))
12208 continue;
12209 isl_int_set(v, bset->eq[i][pos]);
12210 isl_int_set_si(bset->eq[i][pos], 0);
12211 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12212 v, subs->v->el + 1, subs->v->size - 1);
12215 for (i = 0; i < bset->n_ineq; ++i) {
12216 if (isl_int_is_zero(bset->ineq[i][pos]))
12217 continue;
12218 isl_int_set(v, bset->ineq[i][pos]);
12219 isl_int_set_si(bset->ineq[i][pos], 0);
12220 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12221 v, subs->v->el + 1, subs->v->size - 1);
12224 for (i = 0; i < bset->n_div; ++i) {
12225 if (isl_int_is_zero(bset->div[i][1 + pos]))
12226 continue;
12227 isl_int_set(v, bset->div[i][1 + pos]);
12228 isl_int_set_si(bset->div[i][1 + pos], 0);
12229 isl_seq_combine(bset->div[i] + 1,
12230 subs->v->el[0], bset->div[i] + 1,
12231 v, subs->v->el + 1, subs->v->size - 1);
12232 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12235 isl_int_clear(v);
12237 bset = isl_basic_set_simplify(bset);
12238 return isl_basic_set_finalize(bset);
12239 error:
12240 isl_basic_set_free(bset);
12241 return NULL;
12244 /* Plug in "subs" for dimension "type", "pos" of "set".
12246 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12247 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12249 int i;
12251 if (set && isl_set_plain_is_empty(set))
12252 return set;
12254 set = isl_set_cow(set);
12255 if (!set || !subs)
12256 goto error;
12258 for (i = set->n - 1; i >= 0; --i) {
12259 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12260 if (remove_if_empty(set, i) < 0)
12261 goto error;
12264 return set;
12265 error:
12266 isl_set_free(set);
12267 return NULL;
12270 /* Check if the range of "ma" is compatible with the domain or range
12271 * (depending on "type") of "bmap".
12273 static isl_stat check_basic_map_compatible_range_multi_aff(
12274 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12275 __isl_keep isl_multi_aff *ma)
12277 isl_bool m;
12278 isl_space *ma_space;
12280 ma_space = isl_multi_aff_get_space(ma);
12282 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12283 if (m < 0)
12284 goto error;
12285 if (!m)
12286 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12287 "parameters don't match", goto error);
12288 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12289 if (m < 0)
12290 goto error;
12291 if (!m)
12292 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12293 "spaces don't match", goto error);
12295 isl_space_free(ma_space);
12296 return isl_stat_ok;
12297 error:
12298 isl_space_free(ma_space);
12299 return isl_stat_error;
12302 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12303 * coefficients before the transformed range of dimensions,
12304 * the "n_after" coefficients after the transformed range of dimensions
12305 * and the coefficients of the other divs in "bmap".
12307 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12308 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12310 int i;
12311 int n_param;
12312 int n_set;
12313 isl_local_space *ls;
12315 if (n_div == 0)
12316 return 0;
12318 ls = isl_aff_get_domain_local_space(ma->p[0]);
12319 if (!ls)
12320 return -1;
12322 n_param = isl_local_space_dim(ls, isl_dim_param);
12323 n_set = isl_local_space_dim(ls, isl_dim_set);
12324 for (i = 0; i < n_div; ++i) {
12325 int o_bmap = 0, o_ls = 0;
12327 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12328 o_bmap += 1 + 1 + n_param;
12329 o_ls += 1 + 1 + n_param;
12330 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12331 o_bmap += n_before;
12332 isl_seq_cpy(bmap->div[i] + o_bmap,
12333 ls->div->row[i] + o_ls, n_set);
12334 o_bmap += n_set;
12335 o_ls += n_set;
12336 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12337 o_bmap += n_after;
12338 isl_seq_cpy(bmap->div[i] + o_bmap,
12339 ls->div->row[i] + o_ls, n_div);
12340 o_bmap += n_div;
12341 o_ls += n_div;
12342 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12343 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12344 goto error;
12347 isl_local_space_free(ls);
12348 return 0;
12349 error:
12350 isl_local_space_free(ls);
12351 return -1;
12354 /* How many stride constraints does "ma" enforce?
12355 * That is, how many of the affine expressions have a denominator
12356 * different from one?
12358 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12360 int i;
12361 int strides = 0;
12363 for (i = 0; i < ma->n; ++i)
12364 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12365 strides++;
12367 return strides;
12370 /* For each affine expression in ma of the form
12372 * x_i = (f_i y + h_i)/m_i
12374 * with m_i different from one, add a constraint to "bmap"
12375 * of the form
12377 * f_i y + h_i = m_i alpha_i
12379 * with alpha_i an additional existentially quantified variable.
12381 * The input variables of "ma" correspond to a subset of the variables
12382 * of "bmap". There are "n_before" variables in "bmap" before this
12383 * subset and "n_after" variables after this subset.
12384 * The integer divisions of the affine expressions in "ma" are assumed
12385 * to have been aligned. There are "n_div_ma" of them and
12386 * they appear first in "bmap", straight after the "n_after" variables.
12388 static __isl_give isl_basic_map *add_ma_strides(
12389 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12390 int n_before, int n_after, int n_div_ma)
12392 int i, k;
12393 int div;
12394 int total;
12395 int n_param;
12396 int n_in;
12398 total = isl_basic_map_total_dim(bmap);
12399 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12400 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12401 for (i = 0; i < ma->n; ++i) {
12402 int o_bmap = 0, o_ma = 1;
12404 if (isl_int_is_one(ma->p[i]->v->el[0]))
12405 continue;
12406 div = isl_basic_map_alloc_div(bmap);
12407 k = isl_basic_map_alloc_equality(bmap);
12408 if (div < 0 || k < 0)
12409 goto error;
12410 isl_int_set_si(bmap->div[div][0], 0);
12411 isl_seq_cpy(bmap->eq[k] + o_bmap,
12412 ma->p[i]->v->el + o_ma, 1 + n_param);
12413 o_bmap += 1 + n_param;
12414 o_ma += 1 + n_param;
12415 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12416 o_bmap += n_before;
12417 isl_seq_cpy(bmap->eq[k] + o_bmap,
12418 ma->p[i]->v->el + o_ma, n_in);
12419 o_bmap += n_in;
12420 o_ma += n_in;
12421 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12422 o_bmap += n_after;
12423 isl_seq_cpy(bmap->eq[k] + o_bmap,
12424 ma->p[i]->v->el + o_ma, n_div_ma);
12425 o_bmap += n_div_ma;
12426 o_ma += n_div_ma;
12427 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12428 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12429 total++;
12432 return bmap;
12433 error:
12434 isl_basic_map_free(bmap);
12435 return NULL;
12438 /* Replace the domain or range space (depending on "type) of "space" by "set".
12440 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12441 enum isl_dim_type type, __isl_take isl_space *set)
12443 if (type == isl_dim_in) {
12444 space = isl_space_range(space);
12445 space = isl_space_map_from_domain_and_range(set, space);
12446 } else {
12447 space = isl_space_domain(space);
12448 space = isl_space_map_from_domain_and_range(space, set);
12451 return space;
12454 /* Compute the preimage of the domain or range (depending on "type")
12455 * of "bmap" under the function represented by "ma".
12456 * In other words, plug in "ma" in the domain or range of "bmap".
12457 * The result is a basic map that lives in the same space as "bmap"
12458 * except that the domain or range has been replaced by
12459 * the domain space of "ma".
12461 * If bmap is represented by
12463 * A(p) + S u + B x + T v + C(divs) >= 0,
12465 * where u and x are input and output dimensions if type == isl_dim_out
12466 * while x and v are input and output dimensions if type == isl_dim_in,
12467 * and ma is represented by
12469 * x = D(p) + F(y) + G(divs')
12471 * then the result is
12473 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12475 * The divs in the input set are similarly adjusted.
12476 * In particular
12478 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12480 * becomes
12482 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12483 * B_i G(divs') + c_i(divs))/n_i)
12485 * If bmap is not a rational map and if F(y) involves any denominators
12487 * x_i = (f_i y + h_i)/m_i
12489 * then additional constraints are added to ensure that we only
12490 * map back integer points. That is we enforce
12492 * f_i y + h_i = m_i alpha_i
12494 * with alpha_i an additional existentially quantified variable.
12496 * We first copy over the divs from "ma".
12497 * Then we add the modified constraints and divs from "bmap".
12498 * Finally, we add the stride constraints, if needed.
12500 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12501 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12502 __isl_take isl_multi_aff *ma)
12504 int i, k;
12505 isl_space *space;
12506 isl_basic_map *res = NULL;
12507 int n_before, n_after, n_div_bmap, n_div_ma;
12508 isl_int f, c1, c2, g;
12509 isl_bool rational;
12510 int strides;
12512 isl_int_init(f);
12513 isl_int_init(c1);
12514 isl_int_init(c2);
12515 isl_int_init(g);
12517 ma = isl_multi_aff_align_divs(ma);
12518 if (!bmap || !ma)
12519 goto error;
12520 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12521 goto error;
12523 if (type == isl_dim_in) {
12524 n_before = 0;
12525 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12526 } else {
12527 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12528 n_after = 0;
12530 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12531 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12533 space = isl_multi_aff_get_domain_space(ma);
12534 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12535 rational = isl_basic_map_is_rational(bmap);
12536 strides = rational ? 0 : multi_aff_strides(ma);
12537 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12538 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12539 if (rational)
12540 res = isl_basic_map_set_rational(res);
12542 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12543 if (isl_basic_map_alloc_div(res) < 0)
12544 goto error;
12546 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12547 goto error;
12549 for (i = 0; i < bmap->n_eq; ++i) {
12550 k = isl_basic_map_alloc_equality(res);
12551 if (k < 0)
12552 goto error;
12553 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12554 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12557 for (i = 0; i < bmap->n_ineq; ++i) {
12558 k = isl_basic_map_alloc_inequality(res);
12559 if (k < 0)
12560 goto error;
12561 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12562 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12565 for (i = 0; i < bmap->n_div; ++i) {
12566 if (isl_int_is_zero(bmap->div[i][0])) {
12567 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12568 continue;
12570 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12571 n_before, n_after, n_div_ma, n_div_bmap,
12572 f, c1, c2, g, 1);
12575 if (strides)
12576 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12578 isl_int_clear(f);
12579 isl_int_clear(c1);
12580 isl_int_clear(c2);
12581 isl_int_clear(g);
12582 isl_basic_map_free(bmap);
12583 isl_multi_aff_free(ma);
12584 res = isl_basic_map_simplify(res);
12585 return isl_basic_map_finalize(res);
12586 error:
12587 isl_int_clear(f);
12588 isl_int_clear(c1);
12589 isl_int_clear(c2);
12590 isl_int_clear(g);
12591 isl_basic_map_free(bmap);
12592 isl_multi_aff_free(ma);
12593 isl_basic_map_free(res);
12594 return NULL;
12597 /* Compute the preimage of "bset" under the function represented by "ma".
12598 * In other words, plug in "ma" in "bset". The result is a basic set
12599 * that lives in the domain space of "ma".
12601 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12602 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12604 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12607 /* Compute the preimage of the domain of "bmap" under the function
12608 * represented by "ma".
12609 * In other words, plug in "ma" in the domain of "bmap".
12610 * The result is a basic map that lives in the same space as "bmap"
12611 * except that the domain has been replaced by the domain space of "ma".
12613 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12614 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12616 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12619 /* Compute the preimage of the range of "bmap" under the function
12620 * represented by "ma".
12621 * In other words, plug in "ma" in the range of "bmap".
12622 * The result is a basic map that lives in the same space as "bmap"
12623 * except that the range has been replaced by the domain space of "ma".
12625 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12626 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12628 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12631 /* Check if the range of "ma" is compatible with the domain or range
12632 * (depending on "type") of "map".
12633 * Return isl_stat_error if anything is wrong.
12635 static isl_stat check_map_compatible_range_multi_aff(
12636 __isl_keep isl_map *map, enum isl_dim_type type,
12637 __isl_keep isl_multi_aff *ma)
12639 isl_bool m;
12640 isl_space *ma_space;
12642 ma_space = isl_multi_aff_get_space(ma);
12643 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12644 isl_space_free(ma_space);
12645 if (m < 0)
12646 return isl_stat_error;
12647 if (!m)
12648 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12649 "spaces don't match", return isl_stat_error);
12650 return isl_stat_ok;
12653 /* Compute the preimage of the domain or range (depending on "type")
12654 * of "map" under the function represented by "ma".
12655 * In other words, plug in "ma" in the domain or range of "map".
12656 * The result is a map that lives in the same space as "map"
12657 * except that the domain or range has been replaced by
12658 * the domain space of "ma".
12660 * The parameters are assumed to have been aligned.
12662 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12663 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12665 int i;
12666 isl_space *space;
12668 map = isl_map_cow(map);
12669 ma = isl_multi_aff_align_divs(ma);
12670 if (!map || !ma)
12671 goto error;
12672 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12673 goto error;
12675 for (i = 0; i < map->n; ++i) {
12676 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12677 isl_multi_aff_copy(ma));
12678 if (!map->p[i])
12679 goto error;
12682 space = isl_multi_aff_get_domain_space(ma);
12683 space = isl_space_set(isl_map_get_space(map), type, space);
12685 isl_space_free(map->dim);
12686 map->dim = space;
12687 if (!map->dim)
12688 goto error;
12690 isl_multi_aff_free(ma);
12691 if (map->n > 1)
12692 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12693 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12694 return map;
12695 error:
12696 isl_multi_aff_free(ma);
12697 isl_map_free(map);
12698 return NULL;
12701 /* Compute the preimage of the domain or range (depending on "type")
12702 * of "map" under the function represented by "ma".
12703 * In other words, plug in "ma" in the domain or range of "map".
12704 * The result is a map that lives in the same space as "map"
12705 * except that the domain or range has been replaced by
12706 * the domain space of "ma".
12708 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12709 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12711 isl_bool aligned;
12713 if (!map || !ma)
12714 goto error;
12716 aligned = isl_map_space_has_equal_params(map, ma->space);
12717 if (aligned < 0)
12718 goto error;
12719 if (aligned)
12720 return map_preimage_multi_aff(map, type, ma);
12722 if (isl_map_check_named_params(map) < 0)
12723 goto error;
12724 if (!isl_space_has_named_params(ma->space))
12725 isl_die(map->ctx, isl_error_invalid,
12726 "unaligned unnamed parameters", goto error);
12727 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12728 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12730 return map_preimage_multi_aff(map, type, ma);
12731 error:
12732 isl_multi_aff_free(ma);
12733 return isl_map_free(map);
12736 /* Compute the preimage of "set" under the function represented by "ma".
12737 * In other words, plug in "ma" in "set". The result is a set
12738 * that lives in the domain space of "ma".
12740 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12741 __isl_take isl_multi_aff *ma)
12743 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12746 /* Compute the preimage of the domain of "map" under the function
12747 * represented by "ma".
12748 * In other words, plug in "ma" in the domain of "map".
12749 * The result is a map that lives in the same space as "map"
12750 * except that the domain has been replaced by the domain space of "ma".
12752 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12753 __isl_take isl_multi_aff *ma)
12755 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12758 /* Compute the preimage of the range of "map" under the function
12759 * represented by "ma".
12760 * In other words, plug in "ma" in the range of "map".
12761 * The result is a map that lives in the same space as "map"
12762 * except that the range has been replaced by the domain space of "ma".
12764 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12765 __isl_take isl_multi_aff *ma)
12767 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12770 /* Compute the preimage of "map" under the function represented by "pma".
12771 * In other words, plug in "pma" in the domain or range of "map".
12772 * The result is a map that lives in the same space as "map",
12773 * except that the space of type "type" has been replaced by
12774 * the domain space of "pma".
12776 * The parameters of "map" and "pma" are assumed to have been aligned.
12778 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12779 __isl_take isl_map *map, enum isl_dim_type type,
12780 __isl_take isl_pw_multi_aff *pma)
12782 int i;
12783 isl_map *res;
12785 if (!pma)
12786 goto error;
12788 if (pma->n == 0) {
12789 isl_pw_multi_aff_free(pma);
12790 res = isl_map_empty(isl_map_get_space(map));
12791 isl_map_free(map);
12792 return res;
12795 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12796 isl_multi_aff_copy(pma->p[0].maff));
12797 if (type == isl_dim_in)
12798 res = isl_map_intersect_domain(res,
12799 isl_map_copy(pma->p[0].set));
12800 else
12801 res = isl_map_intersect_range(res,
12802 isl_map_copy(pma->p[0].set));
12804 for (i = 1; i < pma->n; ++i) {
12805 isl_map *res_i;
12807 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12808 isl_multi_aff_copy(pma->p[i].maff));
12809 if (type == isl_dim_in)
12810 res_i = isl_map_intersect_domain(res_i,
12811 isl_map_copy(pma->p[i].set));
12812 else
12813 res_i = isl_map_intersect_range(res_i,
12814 isl_map_copy(pma->p[i].set));
12815 res = isl_map_union(res, res_i);
12818 isl_pw_multi_aff_free(pma);
12819 isl_map_free(map);
12820 return res;
12821 error:
12822 isl_pw_multi_aff_free(pma);
12823 isl_map_free(map);
12824 return NULL;
12827 /* Compute the preimage of "map" under the function represented by "pma".
12828 * In other words, plug in "pma" in the domain or range of "map".
12829 * The result is a map that lives in the same space as "map",
12830 * except that the space of type "type" has been replaced by
12831 * the domain space of "pma".
12833 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12834 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12836 isl_bool aligned;
12838 if (!map || !pma)
12839 goto error;
12841 aligned = isl_map_space_has_equal_params(map, pma->dim);
12842 if (aligned < 0)
12843 goto error;
12844 if (aligned)
12845 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12847 if (isl_map_check_named_params(map) < 0)
12848 goto error;
12849 if (!isl_space_has_named_params(pma->dim))
12850 isl_die(map->ctx, isl_error_invalid,
12851 "unaligned unnamed parameters", goto error);
12852 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12853 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12855 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12856 error:
12857 isl_pw_multi_aff_free(pma);
12858 return isl_map_free(map);
12861 /* Compute the preimage of "set" under the function represented by "pma".
12862 * In other words, plug in "pma" in "set". The result is a set
12863 * that lives in the domain space of "pma".
12865 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12866 __isl_take isl_pw_multi_aff *pma)
12868 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12871 /* Compute the preimage of the domain of "map" under the function
12872 * represented by "pma".
12873 * In other words, plug in "pma" in the domain of "map".
12874 * The result is a map that lives in the same space as "map",
12875 * except that domain space has been replaced by the domain space of "pma".
12877 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12878 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12880 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12883 /* Compute the preimage of the range of "map" under the function
12884 * represented by "pma".
12885 * In other words, plug in "pma" in the range of "map".
12886 * The result is a map that lives in the same space as "map",
12887 * except that range space has been replaced by the domain space of "pma".
12889 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12890 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12892 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12895 /* Compute the preimage of "map" under the function represented by "mpa".
12896 * In other words, plug in "mpa" in the domain or range of "map".
12897 * The result is a map that lives in the same space as "map",
12898 * except that the space of type "type" has been replaced by
12899 * the domain space of "mpa".
12901 * If the map does not involve any constraints that refer to the
12902 * dimensions of the substituted space, then the only possible
12903 * effect of "mpa" on the map is to map the space to a different space.
12904 * We create a separate isl_multi_aff to effectuate this change
12905 * in order to avoid spurious splitting of the map along the pieces
12906 * of "mpa".
12908 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12909 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12911 int n;
12912 isl_pw_multi_aff *pma;
12914 if (!map || !mpa)
12915 goto error;
12917 n = isl_map_dim(map, type);
12918 if (!isl_map_involves_dims(map, type, 0, n)) {
12919 isl_space *space;
12920 isl_multi_aff *ma;
12922 space = isl_multi_pw_aff_get_space(mpa);
12923 isl_multi_pw_aff_free(mpa);
12924 ma = isl_multi_aff_zero(space);
12925 return isl_map_preimage_multi_aff(map, type, ma);
12928 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12929 return isl_map_preimage_pw_multi_aff(map, type, pma);
12930 error:
12931 isl_map_free(map);
12932 isl_multi_pw_aff_free(mpa);
12933 return NULL;
12936 /* Compute the preimage of "map" under the function represented by "mpa".
12937 * In other words, plug in "mpa" in the domain "map".
12938 * The result is a map that lives in the same space as "map",
12939 * except that domain space has been replaced by the domain space of "mpa".
12941 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12942 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12944 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12947 /* Compute the preimage of "set" by the function represented by "mpa".
12948 * In other words, plug in "mpa" in "set".
12950 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12951 __isl_take isl_multi_pw_aff *mpa)
12953 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
12956 /* Are the "n" "coefficients" starting at "first" of the integer division
12957 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
12958 * to each other?
12959 * The "coefficient" at position 0 is the denominator.
12960 * The "coefficient" at position 1 is the constant term.
12962 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
12963 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
12964 unsigned first, unsigned n)
12966 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
12967 return isl_bool_error;
12968 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
12969 return isl_bool_error;
12970 return isl_seq_eq(bmap1->div[pos1] + first,
12971 bmap2->div[pos2] + first, n);
12974 /* Are the integer division expressions at position "pos1" in "bmap1" and
12975 * "pos2" in "bmap2" equal to each other, except that the constant terms
12976 * are different?
12978 isl_bool isl_basic_map_equal_div_expr_except_constant(
12979 __isl_keep isl_basic_map *bmap1, int pos1,
12980 __isl_keep isl_basic_map *bmap2, int pos2)
12982 isl_bool equal;
12983 unsigned total;
12985 if (!bmap1 || !bmap2)
12986 return isl_bool_error;
12987 total = isl_basic_map_total_dim(bmap1);
12988 if (total != isl_basic_map_total_dim(bmap2))
12989 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
12990 "incomparable div expressions", return isl_bool_error);
12991 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
12992 0, 1);
12993 if (equal < 0 || !equal)
12994 return equal;
12995 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
12996 1, 1);
12997 if (equal < 0 || equal)
12998 return isl_bool_not(equal);
12999 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13000 2, total);
13003 /* Replace the numerator of the constant term of the integer division
13004 * expression at position "div" in "bmap" by "value".
13005 * The caller guarantees that this does not change the meaning
13006 * of the input.
13008 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13009 __isl_take isl_basic_map *bmap, int div, int value)
13011 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13012 return isl_basic_map_free(bmap);
13014 isl_int_set_si(bmap->div[div][1], value);
13016 return bmap;
13019 /* Is the point "inner" internal to inequality constraint "ineq"
13020 * of "bset"?
13021 * The point is considered to be internal to the inequality constraint,
13022 * if it strictly lies on the positive side of the inequality constraint,
13023 * or if it lies on the constraint and the constraint is lexico-positive.
13025 static isl_bool is_internal(__isl_keep isl_vec *inner,
13026 __isl_keep isl_basic_set *bset, int ineq)
13028 isl_ctx *ctx;
13029 int pos;
13030 unsigned total;
13032 if (!inner || !bset)
13033 return isl_bool_error;
13035 ctx = isl_basic_set_get_ctx(bset);
13036 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13037 &ctx->normalize_gcd);
13038 if (!isl_int_is_zero(ctx->normalize_gcd))
13039 return isl_int_is_nonneg(ctx->normalize_gcd);
13041 total = isl_basic_set_dim(bset, isl_dim_all);
13042 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13043 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13046 /* Tighten the inequality constraints of "bset" that are outward with respect
13047 * to the point "vec".
13048 * That is, tighten the constraints that are not satisfied by "vec".
13050 * "vec" is a point internal to some superset S of "bset" that is used
13051 * to make the subsets of S disjoint, by tightening one half of the constraints
13052 * that separate two subsets. In particular, the constraints of S
13053 * are all satisfied by "vec" and should not be tightened.
13054 * Of the internal constraints, those that have "vec" on the outside
13055 * are tightened. The shared facet is included in the adjacent subset
13056 * with the opposite constraint.
13057 * For constraints that saturate "vec", this criterion cannot be used
13058 * to determine which of the two sides should be tightened.
13059 * Instead, the sign of the first non-zero coefficient is used
13060 * to make this choice. Note that this second criterion is never used
13061 * on the constraints of S since "vec" is interior to "S".
13063 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13064 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13066 int j;
13068 bset = isl_basic_set_cow(bset);
13069 if (!bset)
13070 return NULL;
13071 for (j = 0; j < bset->n_ineq; ++j) {
13072 isl_bool internal;
13074 internal = is_internal(vec, bset, j);
13075 if (internal < 0)
13076 return isl_basic_set_free(bset);
13077 if (internal)
13078 continue;
13079 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13082 return bset;