isl_map_simplify.c: fix typo in comment
[isl.git] / isl_map.c
blob3b9abfc9024b2137e35e3e410ba95b44516be1d1
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 Sven Verdoolaege
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
19 #include <string.h>
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
22 #include <isl_blk.h>
23 #include <isl/constraint.h>
24 #include "isl_space_private.h"
25 #include "isl_equalities.h"
26 #include <isl_lp_private.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl/map.h>
30 #include <isl_reordering.h>
31 #include "isl_sample.h"
32 #include <isl_sort.h>
33 #include "isl_tab.h"
34 #include <isl/vec.h>
35 #include <isl_mat_private.h>
36 #include <isl_vec_private.h>
37 #include <isl_dim_map.h>
38 #include <isl_local_space_private.h>
39 #include <isl_aff_private.h>
40 #include <isl_options_private.h>
41 #include <isl_morph.h>
42 #include <isl_val_private.h>
43 #include <isl/deprecated/map_int.h>
44 #include <isl/deprecated/set_int.h>
46 #include <bset_to_bmap.c>
47 #include <bset_from_bmap.c>
48 #include <set_to_map.c>
49 #include <set_from_map.c>
51 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
53 switch (type) {
54 case isl_dim_param: return dim->nparam;
55 case isl_dim_in: return dim->n_in;
56 case isl_dim_out: return dim->n_out;
57 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
58 default: return 0;
62 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
64 switch (type) {
65 case isl_dim_param: return 1;
66 case isl_dim_in: return 1 + dim->nparam;
67 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
68 default: return 0;
72 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
73 enum isl_dim_type type)
75 if (!bmap)
76 return 0;
77 switch (type) {
78 case isl_dim_cst: return 1;
79 case isl_dim_param:
80 case isl_dim_in:
81 case isl_dim_out: return isl_space_dim(bmap->dim, type);
82 case isl_dim_div: return bmap->n_div;
83 case isl_dim_all: return isl_basic_map_total_dim(bmap);
84 default: return 0;
88 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
90 return map ? n(map->dim, type) : 0;
93 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
95 return set ? n(set->dim, type) : 0;
98 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
99 enum isl_dim_type type)
101 isl_space *space;
103 if (!bmap)
104 return 0;
106 space = bmap->dim;
107 switch (type) {
108 case isl_dim_cst: return 0;
109 case isl_dim_param: return 1;
110 case isl_dim_in: return 1 + space->nparam;
111 case isl_dim_out: return 1 + space->nparam + space->n_in;
112 case isl_dim_div: return 1 + space->nparam + space->n_in +
113 space->n_out;
114 default: return 0;
118 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
119 enum isl_dim_type type)
121 return isl_basic_map_offset(bset, type);
124 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
126 return pos(map->dim, type);
129 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
130 enum isl_dim_type type)
132 return isl_basic_map_dim(bset, type);
135 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
137 return isl_basic_set_dim(bset, isl_dim_set);
140 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
142 return isl_basic_set_dim(bset, isl_dim_param);
145 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
147 if (!bset)
148 return 0;
149 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
152 unsigned isl_set_n_dim(__isl_keep isl_set *set)
154 return isl_set_dim(set, isl_dim_set);
157 unsigned isl_set_n_param(__isl_keep isl_set *set)
159 return isl_set_dim(set, isl_dim_param);
162 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
164 return bmap ? bmap->dim->n_in : 0;
167 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
169 return bmap ? bmap->dim->n_out : 0;
172 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
174 return bmap ? bmap->dim->nparam : 0;
177 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
179 return bmap ? bmap->n_div : 0;
182 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
184 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
187 unsigned isl_map_n_in(const struct isl_map *map)
189 return map ? map->dim->n_in : 0;
192 unsigned isl_map_n_out(const struct isl_map *map)
194 return map ? map->dim->n_out : 0;
197 unsigned isl_map_n_param(const struct isl_map *map)
199 return map ? map->dim->nparam : 0;
202 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
204 int m;
205 if (!map || !set)
206 return -1;
207 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
208 if (m < 0 || !m)
209 return m;
210 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
211 set->dim, isl_dim_set);
214 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
215 __isl_keep isl_basic_set *bset)
217 isl_bool m;
218 if (!bmap || !bset)
219 return isl_bool_error;
220 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
221 if (m < 0 || !m)
222 return m;
223 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
224 bset->dim, isl_dim_set);
227 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
229 int m;
230 if (!map || !set)
231 return -1;
232 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
233 if (m < 0 || !m)
234 return m;
235 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
236 set->dim, isl_dim_set);
239 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
240 struct isl_basic_set *bset)
242 int m;
243 if (!bmap || !bset)
244 return -1;
245 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
246 if (m < 0 || !m)
247 return m;
248 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
249 bset->dim, isl_dim_set);
252 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
254 return bmap ? bmap->ctx : NULL;
257 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
259 return bset ? bset->ctx : NULL;
262 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
264 return map ? map->ctx : NULL;
267 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
269 return set ? set->ctx : NULL;
272 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
274 if (!bmap)
275 return NULL;
276 return isl_space_copy(bmap->dim);
279 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
281 if (!bset)
282 return NULL;
283 return isl_space_copy(bset->dim);
286 /* Extract the divs in "bmap" as a matrix.
288 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
290 int i;
291 isl_ctx *ctx;
292 isl_mat *div;
293 unsigned total;
294 unsigned cols;
296 if (!bmap)
297 return NULL;
299 ctx = isl_basic_map_get_ctx(bmap);
300 total = isl_space_dim(bmap->dim, isl_dim_all);
301 cols = 1 + 1 + total + bmap->n_div;
302 div = isl_mat_alloc(ctx, bmap->n_div, cols);
303 if (!div)
304 return NULL;
306 for (i = 0; i < bmap->n_div; ++i)
307 isl_seq_cpy(div->row[i], bmap->div[i], cols);
309 return div;
312 /* Extract the divs in "bset" as a matrix.
314 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
316 return isl_basic_map_get_divs(bset);
319 __isl_give isl_local_space *isl_basic_map_get_local_space(
320 __isl_keep isl_basic_map *bmap)
322 isl_mat *div;
324 if (!bmap)
325 return NULL;
327 div = isl_basic_map_get_divs(bmap);
328 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
331 __isl_give isl_local_space *isl_basic_set_get_local_space(
332 __isl_keep isl_basic_set *bset)
334 return isl_basic_map_get_local_space(bset);
337 /* For each known div d = floor(f/m), add the constraints
339 * f - m d >= 0
340 * -(f-(m-1)) + m d >= 0
342 * Do not finalize the result.
344 static __isl_give isl_basic_map *add_known_div_constraints(
345 __isl_take isl_basic_map *bmap)
347 int i;
348 unsigned n_div;
350 if (!bmap)
351 return NULL;
352 n_div = isl_basic_map_dim(bmap, isl_dim_div);
353 if (n_div == 0)
354 return bmap;
355 bmap = isl_basic_map_cow(bmap);
356 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
357 if (!bmap)
358 return NULL;
359 for (i = 0; i < n_div; ++i) {
360 if (isl_int_is_zero(bmap->div[i][0]))
361 continue;
362 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
363 return isl_basic_map_free(bmap);
366 return bmap;
369 __isl_give isl_basic_map *isl_basic_map_from_local_space(
370 __isl_take isl_local_space *ls)
372 int i;
373 int n_div;
374 isl_basic_map *bmap;
376 if (!ls)
377 return NULL;
379 n_div = isl_local_space_dim(ls, isl_dim_div);
380 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
381 n_div, 0, 2 * n_div);
383 for (i = 0; i < n_div; ++i)
384 if (isl_basic_map_alloc_div(bmap) < 0)
385 goto error;
387 for (i = 0; i < n_div; ++i)
388 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
389 bmap = add_known_div_constraints(bmap);
391 isl_local_space_free(ls);
392 return bmap;
393 error:
394 isl_local_space_free(ls);
395 isl_basic_map_free(bmap);
396 return NULL;
399 __isl_give isl_basic_set *isl_basic_set_from_local_space(
400 __isl_take isl_local_space *ls)
402 return isl_basic_map_from_local_space(ls);
405 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
407 if (!map)
408 return NULL;
409 return isl_space_copy(map->dim);
412 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
414 if (!set)
415 return NULL;
416 return isl_space_copy(set->dim);
419 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
420 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
422 bmap = isl_basic_map_cow(bmap);
423 if (!bmap)
424 return NULL;
425 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
426 if (!bmap->dim)
427 goto error;
428 bmap = isl_basic_map_finalize(bmap);
429 return bmap;
430 error:
431 isl_basic_map_free(bmap);
432 return NULL;
435 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
436 __isl_take isl_basic_set *bset, const char *s)
438 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
441 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
442 enum isl_dim_type type)
444 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
447 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
448 enum isl_dim_type type, const char *s)
450 int i;
452 map = isl_map_cow(map);
453 if (!map)
454 return NULL;
456 map->dim = isl_space_set_tuple_name(map->dim, type, s);
457 if (!map->dim)
458 goto error;
460 for (i = 0; i < map->n; ++i) {
461 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
462 if (!map->p[i])
463 goto error;
466 return map;
467 error:
468 isl_map_free(map);
469 return NULL;
472 /* Replace the identifier of the tuple of type "type" by "id".
474 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
475 __isl_take isl_basic_map *bmap,
476 enum isl_dim_type type, __isl_take isl_id *id)
478 bmap = isl_basic_map_cow(bmap);
479 if (!bmap)
480 goto error;
481 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
482 if (!bmap->dim)
483 return isl_basic_map_free(bmap);
484 bmap = isl_basic_map_finalize(bmap);
485 return bmap;
486 error:
487 isl_id_free(id);
488 return NULL;
491 /* Replace the identifier of the tuple by "id".
493 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
494 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
496 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
499 /* Does the input or output tuple have a name?
501 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
503 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
506 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
507 enum isl_dim_type type)
509 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
512 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
513 const char *s)
515 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
516 isl_dim_set, s));
519 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
520 enum isl_dim_type type, __isl_take isl_id *id)
522 map = isl_map_cow(map);
523 if (!map)
524 goto error;
526 map->dim = isl_space_set_tuple_id(map->dim, type, id);
528 return isl_map_reset_space(map, isl_space_copy(map->dim));
529 error:
530 isl_id_free(id);
531 return NULL;
534 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
535 __isl_take isl_id *id)
537 return isl_map_set_tuple_id(set, isl_dim_set, id);
540 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
541 enum isl_dim_type type)
543 map = isl_map_cow(map);
544 if (!map)
545 return NULL;
547 map->dim = isl_space_reset_tuple_id(map->dim, type);
549 return isl_map_reset_space(map, isl_space_copy(map->dim));
552 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
554 return isl_map_reset_tuple_id(set, isl_dim_set);
557 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
559 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
562 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
563 enum isl_dim_type type)
565 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
568 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
570 return isl_map_has_tuple_id(set, isl_dim_set);
573 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
575 return isl_map_get_tuple_id(set, isl_dim_set);
578 /* Does the set tuple have a name?
580 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
582 if (!set)
583 return isl_bool_error;
584 return isl_space_has_tuple_name(set->dim, isl_dim_set);
588 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
590 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
593 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
595 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
598 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
599 enum isl_dim_type type, unsigned pos)
601 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
604 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
605 enum isl_dim_type type, unsigned pos)
607 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
610 /* Does the given dimension have a name?
612 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
613 enum isl_dim_type type, unsigned pos)
615 if (!map)
616 return isl_bool_error;
617 return isl_space_has_dim_name(map->dim, type, pos);
620 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
621 enum isl_dim_type type, unsigned pos)
623 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
626 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
627 enum isl_dim_type type, unsigned pos)
629 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
632 /* Does the given dimension have a name?
634 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
635 enum isl_dim_type type, unsigned pos)
637 if (!set)
638 return isl_bool_error;
639 return isl_space_has_dim_name(set->dim, type, pos);
642 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
643 __isl_take isl_basic_map *bmap,
644 enum isl_dim_type type, unsigned pos, const char *s)
646 bmap = isl_basic_map_cow(bmap);
647 if (!bmap)
648 return NULL;
649 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
650 if (!bmap->dim)
651 goto error;
652 return isl_basic_map_finalize(bmap);
653 error:
654 isl_basic_map_free(bmap);
655 return NULL;
658 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
659 enum isl_dim_type type, unsigned pos, const char *s)
661 int i;
663 map = isl_map_cow(map);
664 if (!map)
665 return NULL;
667 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
668 if (!map->dim)
669 goto error;
671 for (i = 0; i < map->n; ++i) {
672 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
673 if (!map->p[i])
674 goto error;
677 return map;
678 error:
679 isl_map_free(map);
680 return NULL;
683 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
684 __isl_take isl_basic_set *bset,
685 enum isl_dim_type type, unsigned pos, const char *s)
687 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
688 type, pos, s));
691 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
692 enum isl_dim_type type, unsigned pos, const char *s)
694 return set_from_map(isl_map_set_dim_name(set_to_map(set),
695 type, pos, s));
698 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
699 enum isl_dim_type type, unsigned pos)
701 if (!bmap)
702 return isl_bool_error;
703 return isl_space_has_dim_id(bmap->dim, type, pos);
706 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
707 enum isl_dim_type type, unsigned pos)
709 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
712 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
713 enum isl_dim_type type, unsigned pos)
715 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
718 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
719 enum isl_dim_type type, unsigned pos)
721 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
724 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
725 enum isl_dim_type type, unsigned pos)
727 return isl_map_has_dim_id(set, type, pos);
730 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
731 enum isl_dim_type type, unsigned pos)
733 return isl_map_get_dim_id(set, type, pos);
736 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
737 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
739 map = isl_map_cow(map);
740 if (!map)
741 goto error;
743 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
745 return isl_map_reset_space(map, isl_space_copy(map->dim));
746 error:
747 isl_id_free(id);
748 return NULL;
751 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
752 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
754 return isl_map_set_dim_id(set, type, pos, id);
757 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
758 __isl_keep isl_id *id)
760 if (!map)
761 return -1;
762 return isl_space_find_dim_by_id(map->dim, type, id);
765 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
766 __isl_keep isl_id *id)
768 return isl_map_find_dim_by_id(set, type, id);
771 /* Return the position of the dimension of the given type and name
772 * in "bmap".
773 * Return -1 if no such dimension can be found.
775 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
776 enum isl_dim_type type, const char *name)
778 if (!bmap)
779 return -1;
780 return isl_space_find_dim_by_name(bmap->dim, type, name);
783 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
784 const char *name)
786 if (!map)
787 return -1;
788 return isl_space_find_dim_by_name(map->dim, type, name);
791 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
792 const char *name)
794 return isl_map_find_dim_by_name(set, type, name);
797 /* Reset the user pointer on all identifiers of parameters and tuples
798 * of the space of "map".
800 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
802 isl_space *space;
804 space = isl_map_get_space(map);
805 space = isl_space_reset_user(space);
806 map = isl_map_reset_space(map, space);
808 return map;
811 /* Reset the user pointer on all identifiers of parameters and tuples
812 * of the space of "set".
814 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
816 return isl_map_reset_user(set);
819 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
821 if (!bmap)
822 return -1;
823 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
826 /* Has "map" been marked as a rational map?
827 * In particular, have all basic maps in "map" been marked this way?
828 * An empty map is not considered to be rational.
829 * Maps where only some of the basic maps are marked rational
830 * are not allowed.
832 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
834 int i;
835 isl_bool rational;
837 if (!map)
838 return isl_bool_error;
839 if (map->n == 0)
840 return isl_bool_false;
841 rational = isl_basic_map_is_rational(map->p[0]);
842 if (rational < 0)
843 return rational;
844 for (i = 1; i < map->n; ++i) {
845 isl_bool rational_i;
847 rational_i = isl_basic_map_is_rational(map->p[i]);
848 if (rational_i < 0)
849 return rational;
850 if (rational != rational_i)
851 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
852 "mixed rational and integer basic maps "
853 "not supported", return isl_bool_error);
856 return rational;
859 /* Has "set" been marked as a rational set?
860 * In particular, have all basic set in "set" been marked this way?
861 * An empty set is not considered to be rational.
862 * Sets where only some of the basic sets are marked rational
863 * are not allowed.
865 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
867 return isl_map_is_rational(set);
870 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
872 return isl_basic_map_is_rational(bset);
875 /* Does "bmap" contain any rational points?
877 * If "bmap" has an equality for each dimension, equating the dimension
878 * to an integer constant, then it has no rational points, even if it
879 * is marked as rational.
881 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
883 int has_rational = 1;
884 unsigned total;
886 if (!bmap)
887 return -1;
888 if (isl_basic_map_plain_is_empty(bmap))
889 return 0;
890 if (!isl_basic_map_is_rational(bmap))
891 return 0;
892 bmap = isl_basic_map_copy(bmap);
893 bmap = isl_basic_map_implicit_equalities(bmap);
894 if (!bmap)
895 return -1;
896 total = isl_basic_map_total_dim(bmap);
897 if (bmap->n_eq == total) {
898 int i, j;
899 for (i = 0; i < bmap->n_eq; ++i) {
900 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
901 if (j < 0)
902 break;
903 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
904 !isl_int_is_negone(bmap->eq[i][1 + j]))
905 break;
906 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
907 total - j - 1);
908 if (j >= 0)
909 break;
911 if (i == bmap->n_eq)
912 has_rational = 0;
914 isl_basic_map_free(bmap);
916 return has_rational;
919 /* Does "map" contain any rational points?
921 int isl_map_has_rational(__isl_keep isl_map *map)
923 int i;
924 int has_rational;
926 if (!map)
927 return -1;
928 for (i = 0; i < map->n; ++i) {
929 has_rational = isl_basic_map_has_rational(map->p[i]);
930 if (has_rational < 0)
931 return -1;
932 if (has_rational)
933 return 1;
935 return 0;
938 /* Does "set" contain any rational points?
940 int isl_set_has_rational(__isl_keep isl_set *set)
942 return isl_map_has_rational(set);
945 /* Is this basic set a parameter domain?
947 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
949 if (!bset)
950 return -1;
951 return isl_space_is_params(bset->dim);
954 /* Is this set a parameter domain?
956 isl_bool isl_set_is_params(__isl_keep isl_set *set)
958 if (!set)
959 return isl_bool_error;
960 return isl_space_is_params(set->dim);
963 /* Is this map actually a parameter domain?
964 * Users should never call this function. Outside of isl,
965 * a map can never be a parameter domain.
967 int isl_map_is_params(__isl_keep isl_map *map)
969 if (!map)
970 return -1;
971 return isl_space_is_params(map->dim);
974 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
975 struct isl_basic_map *bmap, unsigned extra,
976 unsigned n_eq, unsigned n_ineq)
978 int i;
979 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
981 bmap->ctx = ctx;
982 isl_ctx_ref(ctx);
984 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
985 if (isl_blk_is_error(bmap->block))
986 goto error;
988 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
989 if ((n_ineq + n_eq) && !bmap->ineq)
990 goto error;
992 if (extra == 0) {
993 bmap->block2 = isl_blk_empty();
994 bmap->div = NULL;
995 } else {
996 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
997 if (isl_blk_is_error(bmap->block2))
998 goto error;
1000 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1001 if (!bmap->div)
1002 goto error;
1005 for (i = 0; i < n_ineq + n_eq; ++i)
1006 bmap->ineq[i] = bmap->block.data + i * row_size;
1008 for (i = 0; i < extra; ++i)
1009 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1011 bmap->ref = 1;
1012 bmap->flags = 0;
1013 bmap->c_size = n_eq + n_ineq;
1014 bmap->eq = bmap->ineq + n_ineq;
1015 bmap->extra = extra;
1016 bmap->n_eq = 0;
1017 bmap->n_ineq = 0;
1018 bmap->n_div = 0;
1019 bmap->sample = NULL;
1021 return bmap;
1022 error:
1023 isl_basic_map_free(bmap);
1024 return NULL;
1027 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1028 unsigned nparam, unsigned dim, unsigned extra,
1029 unsigned n_eq, unsigned n_ineq)
1031 struct isl_basic_map *bmap;
1032 isl_space *space;
1034 space = isl_space_set_alloc(ctx, nparam, dim);
1035 if (!space)
1036 return NULL;
1038 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1039 return bset_from_bmap(bmap);
1042 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1043 unsigned extra, unsigned n_eq, unsigned n_ineq)
1045 struct isl_basic_map *bmap;
1046 if (!dim)
1047 return NULL;
1048 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1049 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1050 return bset_from_bmap(bmap);
1051 error:
1052 isl_space_free(dim);
1053 return NULL;
1056 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1057 unsigned extra, unsigned n_eq, unsigned n_ineq)
1059 struct isl_basic_map *bmap;
1061 if (!dim)
1062 return NULL;
1063 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1064 if (!bmap)
1065 goto error;
1066 bmap->dim = dim;
1068 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1069 error:
1070 isl_space_free(dim);
1071 return NULL;
1074 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1075 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1076 unsigned n_eq, unsigned n_ineq)
1078 struct isl_basic_map *bmap;
1079 isl_space *dim;
1081 dim = isl_space_alloc(ctx, nparam, in, out);
1082 if (!dim)
1083 return NULL;
1085 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1086 return bmap;
1089 static void dup_constraints(
1090 struct isl_basic_map *dst, struct isl_basic_map *src)
1092 int i;
1093 unsigned total = isl_basic_map_total_dim(src);
1095 for (i = 0; i < src->n_eq; ++i) {
1096 int j = isl_basic_map_alloc_equality(dst);
1097 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1100 for (i = 0; i < src->n_ineq; ++i) {
1101 int j = isl_basic_map_alloc_inequality(dst);
1102 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1105 for (i = 0; i < src->n_div; ++i) {
1106 int j = isl_basic_map_alloc_div(dst);
1107 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1109 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1112 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1114 struct isl_basic_map *dup;
1116 if (!bmap)
1117 return NULL;
1118 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1119 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1120 if (!dup)
1121 return NULL;
1122 dup_constraints(dup, bmap);
1123 dup->flags = bmap->flags;
1124 dup->sample = isl_vec_copy(bmap->sample);
1125 return dup;
1128 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1130 struct isl_basic_map *dup;
1132 dup = isl_basic_map_dup(bset_to_bmap(bset));
1133 return bset_from_bmap(dup);
1136 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1138 if (!bset)
1139 return NULL;
1141 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1142 bset->ref++;
1143 return bset;
1145 return isl_basic_set_dup(bset);
1148 struct isl_set *isl_set_copy(struct isl_set *set)
1150 if (!set)
1151 return NULL;
1153 set->ref++;
1154 return set;
1157 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1159 if (!bmap)
1160 return NULL;
1162 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1163 bmap->ref++;
1164 return bmap;
1166 bmap = isl_basic_map_dup(bmap);
1167 if (bmap)
1168 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1169 return bmap;
1172 struct isl_map *isl_map_copy(struct isl_map *map)
1174 if (!map)
1175 return NULL;
1177 map->ref++;
1178 return map;
1181 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1183 if (!bmap)
1184 return NULL;
1186 if (--bmap->ref > 0)
1187 return NULL;
1189 isl_ctx_deref(bmap->ctx);
1190 free(bmap->div);
1191 isl_blk_free(bmap->ctx, bmap->block2);
1192 free(bmap->ineq);
1193 isl_blk_free(bmap->ctx, bmap->block);
1194 isl_vec_free(bmap->sample);
1195 isl_space_free(bmap->dim);
1196 free(bmap);
1198 return NULL;
1201 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1203 return isl_basic_map_free(bset_to_bmap(bset));
1206 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1208 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1211 __isl_give isl_map *isl_map_align_params_map_map_and(
1212 __isl_take isl_map *map1, __isl_take isl_map *map2,
1213 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1214 __isl_take isl_map *map2))
1216 if (!map1 || !map2)
1217 goto error;
1218 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1219 return fn(map1, map2);
1220 if (!isl_space_has_named_params(map1->dim) ||
1221 !isl_space_has_named_params(map2->dim))
1222 isl_die(map1->ctx, isl_error_invalid,
1223 "unaligned unnamed parameters", goto error);
1224 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1225 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1226 return fn(map1, map2);
1227 error:
1228 isl_map_free(map1);
1229 isl_map_free(map2);
1230 return NULL;
1233 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1234 __isl_keep isl_map *map2,
1235 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1237 isl_bool r;
1239 if (!map1 || !map2)
1240 return isl_bool_error;
1241 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1242 return fn(map1, map2);
1243 if (!isl_space_has_named_params(map1->dim) ||
1244 !isl_space_has_named_params(map2->dim))
1245 isl_die(map1->ctx, isl_error_invalid,
1246 "unaligned unnamed parameters", return isl_bool_error);
1247 map1 = isl_map_copy(map1);
1248 map2 = isl_map_copy(map2);
1249 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1250 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1251 r = fn(map1, map2);
1252 isl_map_free(map1);
1253 isl_map_free(map2);
1254 return r;
1257 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1259 struct isl_ctx *ctx;
1260 if (!bmap)
1261 return -1;
1262 ctx = bmap->ctx;
1263 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1264 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1265 return -1);
1266 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1267 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1268 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1269 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1270 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1271 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1272 isl_int *t;
1273 int j = isl_basic_map_alloc_inequality(bmap);
1274 if (j < 0)
1275 return -1;
1276 t = bmap->ineq[j];
1277 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1278 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1279 bmap->eq[-1] = t;
1280 bmap->n_eq++;
1281 bmap->n_ineq--;
1282 bmap->eq--;
1283 return 0;
1285 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1286 bmap->extra - bmap->n_div);
1287 return bmap->n_eq++;
1290 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1292 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1295 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1297 if (!bmap)
1298 return -1;
1299 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1300 bmap->n_eq -= n;
1301 return 0;
1304 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1306 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1309 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1311 isl_int *t;
1312 if (!bmap)
1313 return -1;
1314 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1316 if (pos != bmap->n_eq - 1) {
1317 t = bmap->eq[pos];
1318 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1319 bmap->eq[bmap->n_eq - 1] = t;
1321 bmap->n_eq--;
1322 return 0;
1325 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1327 return isl_basic_map_drop_equality(bset_to_bmap(bset), pos);
1330 /* Turn inequality "pos" of "bmap" into an equality.
1332 * In particular, we move the inequality in front of the equalities
1333 * and move the last inequality in the position of the moved inequality.
1334 * Note that isl_tab_make_equalities_explicit depends on this particular
1335 * change in the ordering of the constraints.
1337 void isl_basic_map_inequality_to_equality(
1338 struct isl_basic_map *bmap, unsigned pos)
1340 isl_int *t;
1342 t = bmap->ineq[pos];
1343 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1344 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1345 bmap->eq[-1] = t;
1346 bmap->n_eq++;
1347 bmap->n_ineq--;
1348 bmap->eq--;
1349 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1350 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1351 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1352 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1355 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1357 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1360 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1362 struct isl_ctx *ctx;
1363 if (!bmap)
1364 return -1;
1365 ctx = bmap->ctx;
1366 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1367 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1368 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1369 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1370 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1371 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1372 1 + isl_basic_map_total_dim(bmap),
1373 bmap->extra - bmap->n_div);
1374 return bmap->n_ineq++;
1377 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1379 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1382 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1384 if (!bmap)
1385 return -1;
1386 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1387 bmap->n_ineq -= n;
1388 return 0;
1391 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1393 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1396 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1398 isl_int *t;
1399 if (!bmap)
1400 return -1;
1401 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1403 if (pos != bmap->n_ineq - 1) {
1404 t = bmap->ineq[pos];
1405 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1406 bmap->ineq[bmap->n_ineq - 1] = t;
1407 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1409 bmap->n_ineq--;
1410 return 0;
1413 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1415 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1418 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1419 isl_int *eq)
1421 int k;
1423 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1424 if (!bmap)
1425 return NULL;
1426 k = isl_basic_map_alloc_equality(bmap);
1427 if (k < 0)
1428 goto error;
1429 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1430 return bmap;
1431 error:
1432 isl_basic_map_free(bmap);
1433 return NULL;
1436 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1437 isl_int *eq)
1439 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1442 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1443 isl_int *ineq)
1445 int k;
1447 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1448 if (!bmap)
1449 return NULL;
1450 k = isl_basic_map_alloc_inequality(bmap);
1451 if (k < 0)
1452 goto error;
1453 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1454 return bmap;
1455 error:
1456 isl_basic_map_free(bmap);
1457 return NULL;
1460 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1461 isl_int *ineq)
1463 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1466 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1468 if (!bmap)
1469 return -1;
1470 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1471 isl_seq_clr(bmap->div[bmap->n_div] +
1472 1 + 1 + isl_basic_map_total_dim(bmap),
1473 bmap->extra - bmap->n_div);
1474 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1475 return bmap->n_div++;
1478 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1480 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1483 /* Insert an extra integer division, prescribed by "div", to "bmap"
1484 * at (integer division) position "pos".
1486 * The integer division is first added at the end and then moved
1487 * into the right position.
1489 __isl_give isl_basic_map *isl_basic_map_insert_div(
1490 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1492 int i, k, n_div;
1494 bmap = isl_basic_map_cow(bmap);
1495 if (!bmap || !div)
1496 return isl_basic_map_free(bmap);
1498 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1499 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1500 "unexpected size", return isl_basic_map_free(bmap));
1501 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1502 if (pos < 0 || pos > n_div)
1503 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1504 "invalid position", return isl_basic_map_free(bmap));
1506 bmap = isl_basic_map_extend_space(bmap,
1507 isl_basic_map_get_space(bmap), 1, 0, 2);
1508 k = isl_basic_map_alloc_div(bmap);
1509 if (k < 0)
1510 return isl_basic_map_free(bmap);
1511 isl_seq_cpy(bmap->div[k], div->el, div->size);
1512 isl_int_set_si(bmap->div[k][div->size], 0);
1514 for (i = k; i > pos; --i)
1515 isl_basic_map_swap_div(bmap, i, i - 1);
1517 return bmap;
1520 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1522 if (!bmap)
1523 return -1;
1524 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1525 bmap->n_div -= n;
1526 return 0;
1529 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1531 return isl_basic_map_free_div(bset_to_bmap(bset), n);
1534 /* Copy constraint from src to dst, putting the vars of src at offset
1535 * dim_off in dst and the divs of src at offset div_off in dst.
1536 * If both sets are actually map, then dim_off applies to the input
1537 * variables.
1539 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1540 struct isl_basic_map *src_map, isl_int *src,
1541 unsigned in_off, unsigned out_off, unsigned div_off)
1543 unsigned src_nparam = isl_basic_map_n_param(src_map);
1544 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1545 unsigned src_in = isl_basic_map_n_in(src_map);
1546 unsigned dst_in = isl_basic_map_n_in(dst_map);
1547 unsigned src_out = isl_basic_map_n_out(src_map);
1548 unsigned dst_out = isl_basic_map_n_out(dst_map);
1549 isl_int_set(dst[0], src[0]);
1550 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1551 if (dst_nparam > src_nparam)
1552 isl_seq_clr(dst+1+src_nparam,
1553 dst_nparam - src_nparam);
1554 isl_seq_clr(dst+1+dst_nparam, in_off);
1555 isl_seq_cpy(dst+1+dst_nparam+in_off,
1556 src+1+src_nparam,
1557 isl_min(dst_in-in_off, src_in));
1558 if (dst_in-in_off > src_in)
1559 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1560 dst_in - in_off - src_in);
1561 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1562 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1563 src+1+src_nparam+src_in,
1564 isl_min(dst_out-out_off, src_out));
1565 if (dst_out-out_off > src_out)
1566 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1567 dst_out - out_off - src_out);
1568 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1569 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1570 src+1+src_nparam+src_in+src_out,
1571 isl_min(dst_map->extra-div_off, src_map->n_div));
1572 if (dst_map->n_div-div_off > src_map->n_div)
1573 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1574 div_off+src_map->n_div,
1575 dst_map->n_div - div_off - src_map->n_div);
1578 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1579 struct isl_basic_map *src_map, isl_int *src,
1580 unsigned in_off, unsigned out_off, unsigned div_off)
1582 isl_int_set(dst[0], src[0]);
1583 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1586 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1587 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1589 int i;
1590 unsigned div_off;
1592 if (!bmap1 || !bmap2)
1593 goto error;
1595 div_off = bmap1->n_div;
1597 for (i = 0; i < bmap2->n_eq; ++i) {
1598 int i1 = isl_basic_map_alloc_equality(bmap1);
1599 if (i1 < 0)
1600 goto error;
1601 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1602 i_pos, o_pos, div_off);
1605 for (i = 0; i < bmap2->n_ineq; ++i) {
1606 int i1 = isl_basic_map_alloc_inequality(bmap1);
1607 if (i1 < 0)
1608 goto error;
1609 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1610 i_pos, o_pos, div_off);
1613 for (i = 0; i < bmap2->n_div; ++i) {
1614 int i1 = isl_basic_map_alloc_div(bmap1);
1615 if (i1 < 0)
1616 goto error;
1617 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1618 i_pos, o_pos, div_off);
1621 isl_basic_map_free(bmap2);
1623 return bmap1;
1625 error:
1626 isl_basic_map_free(bmap1);
1627 isl_basic_map_free(bmap2);
1628 return NULL;
1631 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1632 struct isl_basic_set *bset2, unsigned pos)
1634 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1635 bset_to_bmap(bset2), 0, pos));
1638 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1639 __isl_take isl_space *dim, unsigned extra,
1640 unsigned n_eq, unsigned n_ineq)
1642 struct isl_basic_map *ext;
1643 unsigned flags;
1644 int dims_ok;
1646 if (!dim)
1647 goto error;
1649 if (!base)
1650 goto error;
1652 dims_ok = isl_space_is_equal(base->dim, dim) &&
1653 base->extra >= base->n_div + extra;
1655 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1656 room_for_ineq(base, n_ineq)) {
1657 isl_space_free(dim);
1658 return base;
1661 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1662 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1663 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1664 extra += base->extra;
1665 n_eq += base->n_eq;
1666 n_ineq += base->n_ineq;
1668 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1669 dim = NULL;
1670 if (!ext)
1671 goto error;
1673 if (dims_ok)
1674 ext->sample = isl_vec_copy(base->sample);
1675 flags = base->flags;
1676 ext = add_constraints(ext, base, 0, 0);
1677 if (ext) {
1678 ext->flags = flags;
1679 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1682 return ext;
1684 error:
1685 isl_space_free(dim);
1686 isl_basic_map_free(base);
1687 return NULL;
1690 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1691 __isl_take isl_space *dim, unsigned extra,
1692 unsigned n_eq, unsigned n_ineq)
1694 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1695 dim, extra, n_eq, n_ineq));
1698 struct isl_basic_map *isl_basic_map_extend_constraints(
1699 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1701 if (!base)
1702 return NULL;
1703 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1704 0, n_eq, n_ineq);
1707 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1708 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1709 unsigned n_eq, unsigned n_ineq)
1711 struct isl_basic_map *bmap;
1712 isl_space *dim;
1714 if (!base)
1715 return NULL;
1716 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1717 if (!dim)
1718 goto error;
1720 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1721 return bmap;
1722 error:
1723 isl_basic_map_free(base);
1724 return NULL;
1727 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1728 unsigned nparam, unsigned dim, unsigned extra,
1729 unsigned n_eq, unsigned n_ineq)
1731 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1732 nparam, 0, dim, extra, n_eq, n_ineq));
1735 struct isl_basic_set *isl_basic_set_extend_constraints(
1736 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1738 isl_basic_map *bmap = bset_to_bmap(base);
1739 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1740 return bset_from_bmap(bmap);
1743 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1745 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1748 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1750 if (!bmap)
1751 return NULL;
1753 if (bmap->ref > 1) {
1754 bmap->ref--;
1755 bmap = isl_basic_map_dup(bmap);
1757 if (bmap) {
1758 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1759 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1761 return bmap;
1764 /* Clear all cached information in "map", either because it is about
1765 * to be modified or because it is being freed.
1766 * Always return the same pointer that is passed in.
1767 * This is needed for the use in isl_map_free.
1769 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1771 isl_basic_map_free(map->cached_simple_hull[0]);
1772 isl_basic_map_free(map->cached_simple_hull[1]);
1773 map->cached_simple_hull[0] = NULL;
1774 map->cached_simple_hull[1] = NULL;
1775 return map;
1778 struct isl_set *isl_set_cow(struct isl_set *set)
1780 return isl_map_cow(set);
1783 /* Return an isl_map that is equal to "map" and that has only
1784 * a single reference.
1786 * If the original input already has only one reference, then
1787 * simply return it, but clear all cached information, since
1788 * it may be rendered invalid by the operations that will be
1789 * performed on the result.
1791 * Otherwise, create a duplicate (without any cached information).
1793 struct isl_map *isl_map_cow(struct isl_map *map)
1795 if (!map)
1796 return NULL;
1798 if (map->ref == 1)
1799 return clear_caches(map);
1800 map->ref--;
1801 return isl_map_dup(map);
1804 static void swap_vars(struct isl_blk blk, isl_int *a,
1805 unsigned a_len, unsigned b_len)
1807 isl_seq_cpy(blk.data, a+a_len, b_len);
1808 isl_seq_cpy(blk.data+b_len, a, a_len);
1809 isl_seq_cpy(a, blk.data, b_len+a_len);
1812 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1813 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1815 int i;
1816 struct isl_blk blk;
1818 if (!bmap)
1819 goto error;
1821 isl_assert(bmap->ctx,
1822 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1824 if (n1 == 0 || n2 == 0)
1825 return bmap;
1827 bmap = isl_basic_map_cow(bmap);
1828 if (!bmap)
1829 return NULL;
1831 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1832 if (isl_blk_is_error(blk))
1833 goto error;
1835 for (i = 0; i < bmap->n_eq; ++i)
1836 swap_vars(blk,
1837 bmap->eq[i] + pos, n1, n2);
1839 for (i = 0; i < bmap->n_ineq; ++i)
1840 swap_vars(blk,
1841 bmap->ineq[i] + pos, n1, n2);
1843 for (i = 0; i < bmap->n_div; ++i)
1844 swap_vars(blk,
1845 bmap->div[i]+1 + pos, n1, n2);
1847 isl_blk_free(bmap->ctx, blk);
1849 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1850 bmap = isl_basic_map_gauss(bmap, NULL);
1851 return isl_basic_map_finalize(bmap);
1852 error:
1853 isl_basic_map_free(bmap);
1854 return NULL;
1857 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1859 int i = 0;
1860 unsigned total;
1861 if (!bmap)
1862 goto error;
1863 total = isl_basic_map_total_dim(bmap);
1864 isl_basic_map_free_div(bmap, bmap->n_div);
1865 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1866 if (bmap->n_eq > 0)
1867 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1868 else {
1869 i = isl_basic_map_alloc_equality(bmap);
1870 if (i < 0)
1871 goto error;
1873 isl_int_set_si(bmap->eq[i][0], 1);
1874 isl_seq_clr(bmap->eq[i]+1, total);
1875 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1876 isl_vec_free(bmap->sample);
1877 bmap->sample = NULL;
1878 return isl_basic_map_finalize(bmap);
1879 error:
1880 isl_basic_map_free(bmap);
1881 return NULL;
1884 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1886 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
1889 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1890 * of "bmap").
1892 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1894 isl_int *t = bmap->div[a];
1895 bmap->div[a] = bmap->div[b];
1896 bmap->div[b] = t;
1899 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1900 * div definitions accordingly.
1902 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1904 int i;
1905 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1907 swap_div(bmap, a, b);
1909 for (i = 0; i < bmap->n_eq; ++i)
1910 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1912 for (i = 0; i < bmap->n_ineq; ++i)
1913 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1915 for (i = 0; i < bmap->n_div; ++i)
1916 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1917 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1920 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1921 * div definitions accordingly.
1923 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1925 isl_basic_map_swap_div(bset, a, b);
1928 /* Eliminate the specified n dimensions starting at first from the
1929 * constraints, without removing the dimensions from the space.
1930 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1932 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1933 enum isl_dim_type type, unsigned first, unsigned n)
1935 int i;
1937 if (!map)
1938 return NULL;
1939 if (n == 0)
1940 return map;
1942 if (first + n > isl_map_dim(map, type) || first + n < first)
1943 isl_die(map->ctx, isl_error_invalid,
1944 "index out of bounds", goto error);
1946 map = isl_map_cow(map);
1947 if (!map)
1948 return NULL;
1950 for (i = 0; i < map->n; ++i) {
1951 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1952 if (!map->p[i])
1953 goto error;
1955 return map;
1956 error:
1957 isl_map_free(map);
1958 return NULL;
1961 /* Eliminate the specified n dimensions starting at first from the
1962 * constraints, without removing the dimensions from the space.
1963 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1965 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1966 enum isl_dim_type type, unsigned first, unsigned n)
1968 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
1971 /* Eliminate the specified n dimensions starting at first from the
1972 * constraints, without removing the dimensions from the space.
1973 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1975 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1976 unsigned first, unsigned n)
1978 return isl_set_eliminate(set, isl_dim_set, first, n);
1981 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1982 __isl_take isl_basic_map *bmap)
1984 if (!bmap)
1985 return NULL;
1986 bmap = isl_basic_map_eliminate_vars(bmap,
1987 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1988 if (!bmap)
1989 return NULL;
1990 bmap->n_div = 0;
1991 return isl_basic_map_finalize(bmap);
1994 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1995 __isl_take isl_basic_set *bset)
1997 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2000 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2002 int i;
2004 if (!map)
2005 return NULL;
2006 if (map->n == 0)
2007 return map;
2009 map = isl_map_cow(map);
2010 if (!map)
2011 return NULL;
2013 for (i = 0; i < map->n; ++i) {
2014 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2015 if (!map->p[i])
2016 goto error;
2018 return map;
2019 error:
2020 isl_map_free(map);
2021 return NULL;
2024 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2026 return isl_map_remove_divs(set);
2029 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2030 enum isl_dim_type type, unsigned first, unsigned n)
2032 if (!bmap)
2033 return NULL;
2034 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2035 goto error);
2036 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2037 return bmap;
2038 bmap = isl_basic_map_eliminate_vars(bmap,
2039 isl_basic_map_offset(bmap, type) - 1 + first, n);
2040 if (!bmap)
2041 return bmap;
2042 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2043 return bmap;
2044 bmap = isl_basic_map_drop(bmap, type, first, n);
2045 return bmap;
2046 error:
2047 isl_basic_map_free(bmap);
2048 return NULL;
2051 /* Return true if the definition of the given div (recursively) involves
2052 * any of the given variables.
2054 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2055 unsigned first, unsigned n)
2057 int i;
2058 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2060 if (isl_int_is_zero(bmap->div[div][0]))
2061 return 0;
2062 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2063 return 1;
2065 for (i = bmap->n_div - 1; i >= 0; --i) {
2066 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2067 continue;
2068 if (div_involves_vars(bmap, i, first, n))
2069 return 1;
2072 return 0;
2075 /* Try and add a lower and/or upper bound on "div" to "bmap"
2076 * based on inequality "i".
2077 * "total" is the total number of variables (excluding the divs).
2078 * "v" is a temporary object that can be used during the calculations.
2079 * If "lb" is set, then a lower bound should be constructed.
2080 * If "ub" is set, then an upper bound should be constructed.
2082 * The calling function has already checked that the inequality does not
2083 * reference "div", but we still need to check that the inequality is
2084 * of the right form. We'll consider the case where we want to construct
2085 * a lower bound. The construction of upper bounds is similar.
2087 * Let "div" be of the form
2089 * q = floor((a + f(x))/d)
2091 * We essentially check if constraint "i" is of the form
2093 * b + f(x) >= 0
2095 * so that we can use it to derive a lower bound on "div".
2096 * However, we allow a slightly more general form
2098 * b + g(x) >= 0
2100 * with the condition that the coefficients of g(x) - f(x) are all
2101 * divisible by d.
2102 * Rewriting this constraint as
2104 * 0 >= -b - g(x)
2106 * adding a + f(x) to both sides and dividing by d, we obtain
2108 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2110 * Taking the floor on both sides, we obtain
2112 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2114 * or
2116 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2118 * In the case of an upper bound, we construct the constraint
2120 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2123 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2124 __isl_take isl_basic_map *bmap, int div, int i,
2125 unsigned total, isl_int v, int lb, int ub)
2127 int j;
2129 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2130 if (lb) {
2131 isl_int_sub(v, bmap->ineq[i][1 + j],
2132 bmap->div[div][1 + 1 + j]);
2133 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2135 if (ub) {
2136 isl_int_add(v, bmap->ineq[i][1 + j],
2137 bmap->div[div][1 + 1 + j]);
2138 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2141 if (!lb && !ub)
2142 return bmap;
2144 bmap = isl_basic_map_cow(bmap);
2145 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2146 if (lb) {
2147 int k = isl_basic_map_alloc_inequality(bmap);
2148 if (k < 0)
2149 goto error;
2150 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2151 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2152 bmap->div[div][1 + j]);
2153 isl_int_cdiv_q(bmap->ineq[k][j],
2154 bmap->ineq[k][j], bmap->div[div][0]);
2156 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2158 if (ub) {
2159 int k = isl_basic_map_alloc_inequality(bmap);
2160 if (k < 0)
2161 goto error;
2162 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2163 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2164 bmap->div[div][1 + j]);
2165 isl_int_fdiv_q(bmap->ineq[k][j],
2166 bmap->ineq[k][j], bmap->div[div][0]);
2168 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2171 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2172 return bmap;
2173 error:
2174 isl_basic_map_free(bmap);
2175 return NULL;
2178 /* This function is called right before "div" is eliminated from "bmap"
2179 * using Fourier-Motzkin.
2180 * Look through the constraints of "bmap" for constraints on the argument
2181 * of the integer division and use them to construct constraints on the
2182 * integer division itself. These constraints can then be combined
2183 * during the Fourier-Motzkin elimination.
2184 * Note that it is only useful to introduce lower bounds on "div"
2185 * if "bmap" already contains upper bounds on "div" as the newly
2186 * introduce lower bounds can then be combined with the pre-existing
2187 * upper bounds. Similarly for upper bounds.
2188 * We therefore first check if "bmap" contains any lower and/or upper bounds
2189 * on "div".
2191 * It is interesting to note that the introduction of these constraints
2192 * can indeed lead to more accurate results, even when compared to
2193 * deriving constraints on the argument of "div" from constraints on "div".
2194 * Consider, for example, the set
2196 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2198 * The second constraint can be rewritten as
2200 * 2 * [(-i-2j+3)/4] + k >= 0
2202 * from which we can derive
2204 * -i - 2j + 3 >= -2k
2206 * or
2208 * i + 2j <= 3 + 2k
2210 * Combined with the first constraint, we obtain
2212 * -3 <= 3 + 2k or k >= -3
2214 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2215 * the first constraint, we obtain
2217 * [(i + 2j)/4] >= [-3/4] = -1
2219 * Combining this constraint with the second constraint, we obtain
2221 * k >= -2
2223 static __isl_give isl_basic_map *insert_bounds_on_div(
2224 __isl_take isl_basic_map *bmap, int div)
2226 int i;
2227 int check_lb, check_ub;
2228 isl_int v;
2229 unsigned total;
2231 if (!bmap)
2232 return NULL;
2234 if (isl_int_is_zero(bmap->div[div][0]))
2235 return bmap;
2237 total = isl_space_dim(bmap->dim, isl_dim_all);
2239 check_lb = 0;
2240 check_ub = 0;
2241 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2242 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2243 if (s > 0)
2244 check_ub = 1;
2245 if (s < 0)
2246 check_lb = 1;
2249 if (!check_lb && !check_ub)
2250 return bmap;
2252 isl_int_init(v);
2254 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2255 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2256 continue;
2258 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2259 check_lb, check_ub);
2262 isl_int_clear(v);
2264 return bmap;
2267 /* Remove all divs (recursively) involving any of the given dimensions
2268 * in their definitions.
2270 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2271 __isl_take isl_basic_map *bmap,
2272 enum isl_dim_type type, unsigned first, unsigned n)
2274 int i;
2276 if (!bmap)
2277 return NULL;
2278 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2279 goto error);
2280 first += isl_basic_map_offset(bmap, type);
2282 for (i = bmap->n_div - 1; i >= 0; --i) {
2283 if (!div_involves_vars(bmap, i, first, n))
2284 continue;
2285 bmap = insert_bounds_on_div(bmap, i);
2286 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2287 if (!bmap)
2288 return NULL;
2289 i = bmap->n_div;
2292 return bmap;
2293 error:
2294 isl_basic_map_free(bmap);
2295 return NULL;
2298 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2299 __isl_take isl_basic_set *bset,
2300 enum isl_dim_type type, unsigned first, unsigned n)
2302 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2305 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2306 enum isl_dim_type type, unsigned first, unsigned n)
2308 int i;
2310 if (!map)
2311 return NULL;
2312 if (map->n == 0)
2313 return map;
2315 map = isl_map_cow(map);
2316 if (!map)
2317 return NULL;
2319 for (i = 0; i < map->n; ++i) {
2320 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2321 type, first, n);
2322 if (!map->p[i])
2323 goto error;
2325 return map;
2326 error:
2327 isl_map_free(map);
2328 return NULL;
2331 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2332 enum isl_dim_type type, unsigned first, unsigned n)
2334 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2335 type, first, n));
2338 /* Does the description of "bmap" depend on the specified dimensions?
2339 * We also check whether the dimensions appear in any of the div definitions.
2340 * In principle there is no need for this check. If the dimensions appear
2341 * in a div definition, they also appear in the defining constraints of that
2342 * div.
2344 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2345 enum isl_dim_type type, unsigned first, unsigned n)
2347 int i;
2349 if (!bmap)
2350 return isl_bool_error;
2352 if (first + n > isl_basic_map_dim(bmap, type))
2353 isl_die(bmap->ctx, isl_error_invalid,
2354 "index out of bounds", return isl_bool_error);
2356 first += isl_basic_map_offset(bmap, type);
2357 for (i = 0; i < bmap->n_eq; ++i)
2358 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2359 return isl_bool_true;
2360 for (i = 0; i < bmap->n_ineq; ++i)
2361 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2362 return isl_bool_true;
2363 for (i = 0; i < bmap->n_div; ++i) {
2364 if (isl_int_is_zero(bmap->div[i][0]))
2365 continue;
2366 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2367 return isl_bool_true;
2370 return isl_bool_false;
2373 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2374 enum isl_dim_type type, unsigned first, unsigned n)
2376 int i;
2378 if (!map)
2379 return isl_bool_error;
2381 if (first + n > isl_map_dim(map, type))
2382 isl_die(map->ctx, isl_error_invalid,
2383 "index out of bounds", return isl_bool_error);
2385 for (i = 0; i < map->n; ++i) {
2386 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2387 type, first, n);
2388 if (involves < 0 || involves)
2389 return involves;
2392 return isl_bool_false;
2395 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2396 enum isl_dim_type type, unsigned first, unsigned n)
2398 return isl_basic_map_involves_dims(bset, type, first, n);
2401 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2402 enum isl_dim_type type, unsigned first, unsigned n)
2404 return isl_map_involves_dims(set, type, first, n);
2407 /* Does local variable "div" of "bmap" have a complete explicit representation?
2408 * Having a complete explicit representation requires not only
2409 * an explicit representation, but also that all local variables
2410 * that appear in this explicit representation in turn have
2411 * a complete explicit representation.
2413 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2415 int i;
2416 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2417 isl_bool marked;
2419 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2420 if (marked < 0 || marked)
2421 return isl_bool_not(marked);
2423 for (i = bmap->n_div - 1; i >= 0; --i) {
2424 isl_bool known;
2426 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2427 continue;
2428 known = isl_basic_map_div_is_known(bmap, i);
2429 if (known < 0 || !known)
2430 return known;
2433 return isl_bool_true;
2436 /* Does local variable "div" of "bset" have a complete explicit representation?
2438 isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2440 return isl_basic_map_div_is_known(bset, div);
2443 /* Remove all divs that are unknown or defined in terms of unknown divs.
2445 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2446 __isl_take isl_basic_map *bmap)
2448 int i;
2450 if (!bmap)
2451 return NULL;
2453 for (i = bmap->n_div - 1; i >= 0; --i) {
2454 if (isl_basic_map_div_is_known(bmap, i))
2455 continue;
2456 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2457 if (!bmap)
2458 return NULL;
2459 i = bmap->n_div;
2462 return bmap;
2465 /* Remove all divs that are unknown or defined in terms of unknown divs.
2467 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2468 __isl_take isl_basic_set *bset)
2470 return isl_basic_map_remove_unknown_divs(bset);
2473 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2475 int i;
2477 if (!map)
2478 return NULL;
2479 if (map->n == 0)
2480 return map;
2482 map = isl_map_cow(map);
2483 if (!map)
2484 return NULL;
2486 for (i = 0; i < map->n; ++i) {
2487 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2488 if (!map->p[i])
2489 goto error;
2491 return map;
2492 error:
2493 isl_map_free(map);
2494 return NULL;
2497 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2499 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
2502 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2503 __isl_take isl_basic_set *bset,
2504 enum isl_dim_type type, unsigned first, unsigned n)
2506 isl_basic_map *bmap = bset_to_bmap(bset);
2507 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2508 return bset_from_bmap(bmap);
2511 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2512 enum isl_dim_type type, unsigned first, unsigned n)
2514 int i;
2516 if (n == 0)
2517 return map;
2519 map = isl_map_cow(map);
2520 if (!map)
2521 return NULL;
2522 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2524 for (i = 0; i < map->n; ++i) {
2525 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2526 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2527 if (!map->p[i])
2528 goto error;
2530 map = isl_map_drop(map, type, first, n);
2531 return map;
2532 error:
2533 isl_map_free(map);
2534 return NULL;
2537 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2538 enum isl_dim_type type, unsigned first, unsigned n)
2540 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2541 type, first, n));
2544 /* Project out n inputs starting at first using Fourier-Motzkin */
2545 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2546 unsigned first, unsigned n)
2548 return isl_map_remove_dims(map, isl_dim_in, first, n);
2551 static void dump_term(struct isl_basic_map *bmap,
2552 isl_int c, int pos, FILE *out)
2554 const char *name;
2555 unsigned in = isl_basic_map_n_in(bmap);
2556 unsigned dim = in + isl_basic_map_n_out(bmap);
2557 unsigned nparam = isl_basic_map_n_param(bmap);
2558 if (!pos)
2559 isl_int_print(out, c, 0);
2560 else {
2561 if (!isl_int_is_one(c))
2562 isl_int_print(out, c, 0);
2563 if (pos < 1 + nparam) {
2564 name = isl_space_get_dim_name(bmap->dim,
2565 isl_dim_param, pos - 1);
2566 if (name)
2567 fprintf(out, "%s", name);
2568 else
2569 fprintf(out, "p%d", pos - 1);
2570 } else if (pos < 1 + nparam + in)
2571 fprintf(out, "i%d", pos - 1 - nparam);
2572 else if (pos < 1 + nparam + dim)
2573 fprintf(out, "o%d", pos - 1 - nparam - in);
2574 else
2575 fprintf(out, "e%d", pos - 1 - nparam - dim);
2579 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2580 int sign, FILE *out)
2582 int i;
2583 int first;
2584 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2585 isl_int v;
2587 isl_int_init(v);
2588 for (i = 0, first = 1; i < len; ++i) {
2589 if (isl_int_sgn(c[i]) * sign <= 0)
2590 continue;
2591 if (!first)
2592 fprintf(out, " + ");
2593 first = 0;
2594 isl_int_abs(v, c[i]);
2595 dump_term(bmap, v, i, out);
2597 isl_int_clear(v);
2598 if (first)
2599 fprintf(out, "0");
2602 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2603 const char *op, FILE *out, int indent)
2605 int i;
2607 fprintf(out, "%*s", indent, "");
2609 dump_constraint_sign(bmap, c, 1, out);
2610 fprintf(out, " %s ", op);
2611 dump_constraint_sign(bmap, c, -1, out);
2613 fprintf(out, "\n");
2615 for (i = bmap->n_div; i < bmap->extra; ++i) {
2616 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2617 continue;
2618 fprintf(out, "%*s", indent, "");
2619 fprintf(out, "ERROR: unused div coefficient not zero\n");
2620 abort();
2624 static void dump_constraints(struct isl_basic_map *bmap,
2625 isl_int **c, unsigned n,
2626 const char *op, FILE *out, int indent)
2628 int i;
2630 for (i = 0; i < n; ++i)
2631 dump_constraint(bmap, c[i], op, out, indent);
2634 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2636 int j;
2637 int first = 1;
2638 unsigned total = isl_basic_map_total_dim(bmap);
2640 for (j = 0; j < 1 + total; ++j) {
2641 if (isl_int_is_zero(exp[j]))
2642 continue;
2643 if (!first && isl_int_is_pos(exp[j]))
2644 fprintf(out, "+");
2645 dump_term(bmap, exp[j], j, out);
2646 first = 0;
2650 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2652 int i;
2654 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2655 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2657 for (i = 0; i < bmap->n_div; ++i) {
2658 fprintf(out, "%*s", indent, "");
2659 fprintf(out, "e%d = [(", i);
2660 dump_affine(bmap, bmap->div[i]+1, out);
2661 fprintf(out, ")/");
2662 isl_int_print(out, bmap->div[i][0], 0);
2663 fprintf(out, "]\n");
2667 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2668 FILE *out, int indent)
2670 if (!bset) {
2671 fprintf(out, "null basic set\n");
2672 return;
2675 fprintf(out, "%*s", indent, "");
2676 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2677 bset->ref, bset->dim->nparam, bset->dim->n_out,
2678 bset->extra, bset->flags);
2679 dump(bset_to_bmap(bset), out, indent);
2682 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2683 FILE *out, int indent)
2685 if (!bmap) {
2686 fprintf(out, "null basic map\n");
2687 return;
2690 fprintf(out, "%*s", indent, "");
2691 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2692 "flags: %x, n_name: %d\n",
2693 bmap->ref,
2694 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2695 bmap->extra, bmap->flags, bmap->dim->n_id);
2696 dump(bmap, out, indent);
2699 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2701 unsigned total;
2702 if (!bmap)
2703 return -1;
2704 total = isl_basic_map_total_dim(bmap);
2705 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2706 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2707 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2708 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2709 return 0;
2712 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2713 unsigned flags)
2715 if (!space)
2716 return NULL;
2717 if (isl_space_dim(space, isl_dim_in) != 0)
2718 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2719 "set cannot have input dimensions", goto error);
2720 return isl_map_alloc_space(space, n, flags);
2721 error:
2722 isl_space_free(space);
2723 return NULL;
2726 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2727 unsigned nparam, unsigned dim, int n, unsigned flags)
2729 struct isl_set *set;
2730 isl_space *dims;
2732 dims = isl_space_alloc(ctx, nparam, 0, dim);
2733 if (!dims)
2734 return NULL;
2736 set = isl_set_alloc_space(dims, n, flags);
2737 return set;
2740 /* Make sure "map" has room for at least "n" more basic maps.
2742 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2744 int i;
2745 struct isl_map *grown = NULL;
2747 if (!map)
2748 return NULL;
2749 isl_assert(map->ctx, n >= 0, goto error);
2750 if (map->n + n <= map->size)
2751 return map;
2752 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2753 if (!grown)
2754 goto error;
2755 for (i = 0; i < map->n; ++i) {
2756 grown->p[i] = isl_basic_map_copy(map->p[i]);
2757 if (!grown->p[i])
2758 goto error;
2759 grown->n++;
2761 isl_map_free(map);
2762 return grown;
2763 error:
2764 isl_map_free(grown);
2765 isl_map_free(map);
2766 return NULL;
2769 /* Make sure "set" has room for at least "n" more basic sets.
2771 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2773 return set_from_map(isl_map_grow(set_to_map(set), n));
2776 struct isl_set *isl_set_dup(struct isl_set *set)
2778 int i;
2779 struct isl_set *dup;
2781 if (!set)
2782 return NULL;
2784 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2785 if (!dup)
2786 return NULL;
2787 for (i = 0; i < set->n; ++i)
2788 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2789 return dup;
2792 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2794 return isl_map_from_basic_map(bset);
2797 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2799 struct isl_map *map;
2801 if (!bmap)
2802 return NULL;
2804 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2805 return isl_map_add_basic_map(map, bmap);
2808 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2809 __isl_take isl_basic_set *bset)
2811 return set_from_map(isl_map_add_basic_map(set_to_map(set),
2812 bset_to_bmap(bset)));
2815 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2817 return isl_map_free(set);
2820 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2822 int i;
2824 if (!set) {
2825 fprintf(out, "null set\n");
2826 return;
2829 fprintf(out, "%*s", indent, "");
2830 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2831 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2832 set->flags);
2833 for (i = 0; i < set->n; ++i) {
2834 fprintf(out, "%*s", indent, "");
2835 fprintf(out, "basic set %d:\n", i);
2836 isl_basic_set_print_internal(set->p[i], out, indent+4);
2840 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2842 int i;
2844 if (!map) {
2845 fprintf(out, "null map\n");
2846 return;
2849 fprintf(out, "%*s", indent, "");
2850 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2851 "flags: %x, n_name: %d\n",
2852 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2853 map->dim->n_out, map->flags, map->dim->n_id);
2854 for (i = 0; i < map->n; ++i) {
2855 fprintf(out, "%*s", indent, "");
2856 fprintf(out, "basic map %d:\n", i);
2857 isl_basic_map_print_internal(map->p[i], out, indent+4);
2861 struct isl_basic_map *isl_basic_map_intersect_domain(
2862 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2864 struct isl_basic_map *bmap_domain;
2866 if (!bmap || !bset)
2867 goto error;
2869 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2870 bset->dim, isl_dim_param), goto error);
2872 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2873 isl_assert(bset->ctx,
2874 isl_basic_map_compatible_domain(bmap, bset), goto error);
2876 bmap = isl_basic_map_cow(bmap);
2877 if (!bmap)
2878 goto error;
2879 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2880 bset->n_div, bset->n_eq, bset->n_ineq);
2881 bmap_domain = isl_basic_map_from_domain(bset);
2882 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2884 bmap = isl_basic_map_simplify(bmap);
2885 return isl_basic_map_finalize(bmap);
2886 error:
2887 isl_basic_map_free(bmap);
2888 isl_basic_set_free(bset);
2889 return NULL;
2892 struct isl_basic_map *isl_basic_map_intersect_range(
2893 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2895 struct isl_basic_map *bmap_range;
2897 if (!bmap || !bset)
2898 goto error;
2900 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2901 bset->dim, isl_dim_param), goto error);
2903 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2904 isl_assert(bset->ctx,
2905 isl_basic_map_compatible_range(bmap, bset), goto error);
2907 if (isl_basic_set_plain_is_universe(bset)) {
2908 isl_basic_set_free(bset);
2909 return bmap;
2912 bmap = isl_basic_map_cow(bmap);
2913 if (!bmap)
2914 goto error;
2915 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2916 bset->n_div, bset->n_eq, bset->n_ineq);
2917 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2918 bmap = add_constraints(bmap, bmap_range, 0, 0);
2920 bmap = isl_basic_map_simplify(bmap);
2921 return isl_basic_map_finalize(bmap);
2922 error:
2923 isl_basic_map_free(bmap);
2924 isl_basic_set_free(bset);
2925 return NULL;
2928 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2929 __isl_keep isl_vec *vec)
2931 int i;
2932 unsigned total;
2933 isl_int s;
2935 if (!bmap || !vec)
2936 return isl_bool_error;
2938 total = 1 + isl_basic_map_total_dim(bmap);
2939 if (total != vec->size)
2940 return isl_bool_error;
2942 isl_int_init(s);
2944 for (i = 0; i < bmap->n_eq; ++i) {
2945 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2946 if (!isl_int_is_zero(s)) {
2947 isl_int_clear(s);
2948 return isl_bool_false;
2952 for (i = 0; i < bmap->n_ineq; ++i) {
2953 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2954 if (isl_int_is_neg(s)) {
2955 isl_int_clear(s);
2956 return isl_bool_false;
2960 isl_int_clear(s);
2962 return isl_bool_true;
2965 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2966 __isl_keep isl_vec *vec)
2968 return isl_basic_map_contains(bset_to_bmap(bset), vec);
2971 struct isl_basic_map *isl_basic_map_intersect(
2972 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2974 struct isl_vec *sample = NULL;
2976 if (!bmap1 || !bmap2)
2977 goto error;
2979 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2980 bmap2->dim, isl_dim_param), goto error);
2981 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2982 isl_space_dim(bmap1->dim, isl_dim_param) &&
2983 isl_space_dim(bmap2->dim, isl_dim_all) !=
2984 isl_space_dim(bmap2->dim, isl_dim_param))
2985 return isl_basic_map_intersect(bmap2, bmap1);
2987 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2988 isl_space_dim(bmap2->dim, isl_dim_param))
2989 isl_assert(bmap1->ctx,
2990 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2992 if (isl_basic_map_plain_is_empty(bmap1)) {
2993 isl_basic_map_free(bmap2);
2994 return bmap1;
2996 if (isl_basic_map_plain_is_empty(bmap2)) {
2997 isl_basic_map_free(bmap1);
2998 return bmap2;
3001 if (bmap1->sample &&
3002 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3003 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3004 sample = isl_vec_copy(bmap1->sample);
3005 else if (bmap2->sample &&
3006 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3007 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3008 sample = isl_vec_copy(bmap2->sample);
3010 bmap1 = isl_basic_map_cow(bmap1);
3011 if (!bmap1)
3012 goto error;
3013 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3014 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3015 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3017 if (!bmap1)
3018 isl_vec_free(sample);
3019 else if (sample) {
3020 isl_vec_free(bmap1->sample);
3021 bmap1->sample = sample;
3024 bmap1 = isl_basic_map_simplify(bmap1);
3025 return isl_basic_map_finalize(bmap1);
3026 error:
3027 if (sample)
3028 isl_vec_free(sample);
3029 isl_basic_map_free(bmap1);
3030 isl_basic_map_free(bmap2);
3031 return NULL;
3034 struct isl_basic_set *isl_basic_set_intersect(
3035 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3037 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3038 bset_to_bmap(bset2)));
3041 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3042 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3044 return isl_basic_set_intersect(bset1, bset2);
3047 /* Special case of isl_map_intersect, where both map1 and map2
3048 * are convex, without any divs and such that either map1 or map2
3049 * contains a single constraint. This constraint is then simply
3050 * added to the other map.
3052 static __isl_give isl_map *map_intersect_add_constraint(
3053 __isl_take isl_map *map1, __isl_take isl_map *map2)
3055 isl_assert(map1->ctx, map1->n == 1, goto error);
3056 isl_assert(map2->ctx, map1->n == 1, goto error);
3057 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3058 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3060 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3061 return isl_map_intersect(map2, map1);
3063 isl_assert(map2->ctx,
3064 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
3066 map1 = isl_map_cow(map1);
3067 if (!map1)
3068 goto error;
3069 if (isl_map_plain_is_empty(map1)) {
3070 isl_map_free(map2);
3071 return map1;
3073 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3074 if (map2->p[0]->n_eq == 1)
3075 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3076 else
3077 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3078 map2->p[0]->ineq[0]);
3080 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3081 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3082 if (!map1->p[0])
3083 goto error;
3085 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3086 isl_basic_map_free(map1->p[0]);
3087 map1->n = 0;
3090 isl_map_free(map2);
3092 return map1;
3093 error:
3094 isl_map_free(map1);
3095 isl_map_free(map2);
3096 return NULL;
3099 /* map2 may be either a parameter domain or a map living in the same
3100 * space as map1.
3102 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3103 __isl_take isl_map *map2)
3105 unsigned flags = 0;
3106 isl_map *result;
3107 int i, j;
3109 if (!map1 || !map2)
3110 goto error;
3112 if ((isl_map_plain_is_empty(map1) ||
3113 isl_map_plain_is_universe(map2)) &&
3114 isl_space_is_equal(map1->dim, map2->dim)) {
3115 isl_map_free(map2);
3116 return map1;
3118 if ((isl_map_plain_is_empty(map2) ||
3119 isl_map_plain_is_universe(map1)) &&
3120 isl_space_is_equal(map1->dim, map2->dim)) {
3121 isl_map_free(map1);
3122 return map2;
3125 if (map1->n == 1 && map2->n == 1 &&
3126 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3127 isl_space_is_equal(map1->dim, map2->dim) &&
3128 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3129 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3130 return map_intersect_add_constraint(map1, map2);
3132 if (isl_space_dim(map2->dim, isl_dim_all) !=
3133 isl_space_dim(map2->dim, isl_dim_param))
3134 isl_assert(map1->ctx,
3135 isl_space_is_equal(map1->dim, map2->dim), goto error);
3137 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3138 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3139 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3141 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3142 map1->n * map2->n, flags);
3143 if (!result)
3144 goto error;
3145 for (i = 0; i < map1->n; ++i)
3146 for (j = 0; j < map2->n; ++j) {
3147 struct isl_basic_map *part;
3148 part = isl_basic_map_intersect(
3149 isl_basic_map_copy(map1->p[i]),
3150 isl_basic_map_copy(map2->p[j]));
3151 if (isl_basic_map_is_empty(part) < 0)
3152 part = isl_basic_map_free(part);
3153 result = isl_map_add_basic_map(result, part);
3154 if (!result)
3155 goto error;
3157 isl_map_free(map1);
3158 isl_map_free(map2);
3159 return result;
3160 error:
3161 isl_map_free(map1);
3162 isl_map_free(map2);
3163 return NULL;
3166 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3167 __isl_take isl_map *map2)
3169 if (!map1 || !map2)
3170 goto error;
3171 if (!isl_space_is_equal(map1->dim, map2->dim))
3172 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3173 "spaces don't match", goto error);
3174 return map_intersect_internal(map1, map2);
3175 error:
3176 isl_map_free(map1);
3177 isl_map_free(map2);
3178 return NULL;
3181 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3182 __isl_take isl_map *map2)
3184 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3187 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3189 return set_from_map(isl_map_intersect(set_to_map(set1),
3190 set_to_map(set2)));
3193 /* map_intersect_internal accepts intersections
3194 * with parameter domains, so we can just call that function.
3196 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3197 __isl_take isl_set *params)
3199 return map_intersect_internal(map, params);
3202 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3203 __isl_take isl_map *map2)
3205 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3208 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3209 __isl_take isl_set *params)
3211 return isl_map_intersect_params(set, params);
3214 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3216 isl_space *space;
3217 unsigned pos, n1, n2;
3219 if (!bmap)
3220 return NULL;
3221 bmap = isl_basic_map_cow(bmap);
3222 if (!bmap)
3223 return NULL;
3224 space = isl_space_reverse(isl_space_copy(bmap->dim));
3225 pos = isl_basic_map_offset(bmap, isl_dim_in);
3226 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3227 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3228 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3229 return isl_basic_map_reset_space(bmap, space);
3232 static __isl_give isl_basic_map *basic_map_space_reset(
3233 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3235 isl_space *space;
3237 if (!bmap)
3238 return NULL;
3239 if (!isl_space_is_named_or_nested(bmap->dim, type))
3240 return bmap;
3242 space = isl_basic_map_get_space(bmap);
3243 space = isl_space_reset(space, type);
3244 bmap = isl_basic_map_reset_space(bmap, space);
3245 return bmap;
3248 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3249 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3250 unsigned pos, unsigned n)
3252 isl_space *res_dim;
3253 struct isl_basic_map *res;
3254 struct isl_dim_map *dim_map;
3255 unsigned total, off;
3256 enum isl_dim_type t;
3258 if (n == 0)
3259 return basic_map_space_reset(bmap, type);
3261 if (!bmap)
3262 return NULL;
3264 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3266 total = isl_basic_map_total_dim(bmap) + n;
3267 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3268 off = 0;
3269 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3270 if (t != type) {
3271 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3272 } else {
3273 unsigned size = isl_basic_map_dim(bmap, t);
3274 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3275 0, pos, off);
3276 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3277 pos, size - pos, off + pos + n);
3279 off += isl_space_dim(res_dim, t);
3281 isl_dim_map_div(dim_map, bmap, off);
3283 res = isl_basic_map_alloc_space(res_dim,
3284 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3285 if (isl_basic_map_is_rational(bmap))
3286 res = isl_basic_map_set_rational(res);
3287 if (isl_basic_map_plain_is_empty(bmap)) {
3288 isl_basic_map_free(bmap);
3289 free(dim_map);
3290 return isl_basic_map_set_to_empty(res);
3292 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3293 return isl_basic_map_finalize(res);
3296 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3297 __isl_take isl_basic_set *bset,
3298 enum isl_dim_type type, unsigned pos, unsigned n)
3300 return isl_basic_map_insert_dims(bset, type, pos, n);
3303 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3304 enum isl_dim_type type, unsigned n)
3306 if (!bmap)
3307 return NULL;
3308 return isl_basic_map_insert_dims(bmap, type,
3309 isl_basic_map_dim(bmap, type), n);
3312 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3313 enum isl_dim_type type, unsigned n)
3315 if (!bset)
3316 return NULL;
3317 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3318 return isl_basic_map_add_dims(bset, type, n);
3319 error:
3320 isl_basic_set_free(bset);
3321 return NULL;
3324 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3325 enum isl_dim_type type)
3327 isl_space *space;
3329 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3330 return map;
3332 space = isl_map_get_space(map);
3333 space = isl_space_reset(space, type);
3334 map = isl_map_reset_space(map, space);
3335 return map;
3338 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3339 enum isl_dim_type type, unsigned pos, unsigned n)
3341 int i;
3343 if (n == 0)
3344 return map_space_reset(map, type);
3346 map = isl_map_cow(map);
3347 if (!map)
3348 return NULL;
3350 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3351 if (!map->dim)
3352 goto error;
3354 for (i = 0; i < map->n; ++i) {
3355 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3356 if (!map->p[i])
3357 goto error;
3360 return map;
3361 error:
3362 isl_map_free(map);
3363 return NULL;
3366 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3367 enum isl_dim_type type, unsigned pos, unsigned n)
3369 return isl_map_insert_dims(set, type, pos, n);
3372 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3373 enum isl_dim_type type, unsigned n)
3375 if (!map)
3376 return NULL;
3377 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3380 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3381 enum isl_dim_type type, unsigned n)
3383 if (!set)
3384 return NULL;
3385 isl_assert(set->ctx, type != isl_dim_in, goto error);
3386 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3387 error:
3388 isl_set_free(set);
3389 return NULL;
3392 __isl_give isl_basic_map *isl_basic_map_move_dims(
3393 __isl_take isl_basic_map *bmap,
3394 enum isl_dim_type dst_type, unsigned dst_pos,
3395 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3397 struct isl_dim_map *dim_map;
3398 struct isl_basic_map *res;
3399 enum isl_dim_type t;
3400 unsigned total, off;
3402 if (!bmap)
3403 return NULL;
3404 if (n == 0)
3405 return bmap;
3407 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3408 goto error);
3410 if (dst_type == src_type && dst_pos == src_pos)
3411 return bmap;
3413 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3415 if (pos(bmap->dim, dst_type) + dst_pos ==
3416 pos(bmap->dim, src_type) + src_pos +
3417 ((src_type < dst_type) ? n : 0)) {
3418 bmap = isl_basic_map_cow(bmap);
3419 if (!bmap)
3420 return NULL;
3422 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3423 src_type, src_pos, n);
3424 if (!bmap->dim)
3425 goto error;
3427 bmap = isl_basic_map_finalize(bmap);
3429 return bmap;
3432 total = isl_basic_map_total_dim(bmap);
3433 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3435 off = 0;
3436 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3437 unsigned size = isl_space_dim(bmap->dim, t);
3438 if (t == dst_type) {
3439 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3440 0, dst_pos, off);
3441 off += dst_pos;
3442 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3443 src_pos, n, off);
3444 off += n;
3445 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3446 dst_pos, size - dst_pos, off);
3447 off += size - dst_pos;
3448 } else if (t == src_type) {
3449 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3450 0, src_pos, off);
3451 off += src_pos;
3452 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3453 src_pos + n, size - src_pos - n, off);
3454 off += size - src_pos - n;
3455 } else {
3456 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3457 off += size;
3460 isl_dim_map_div(dim_map, bmap, off);
3462 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3463 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3464 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3465 if (!bmap)
3466 goto error;
3468 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3469 src_type, src_pos, n);
3470 if (!bmap->dim)
3471 goto error;
3473 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3474 bmap = isl_basic_map_gauss(bmap, NULL);
3475 bmap = isl_basic_map_finalize(bmap);
3477 return bmap;
3478 error:
3479 isl_basic_map_free(bmap);
3480 return NULL;
3483 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3484 enum isl_dim_type dst_type, unsigned dst_pos,
3485 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3487 isl_basic_map *bmap = bset_to_bmap(bset);
3488 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3489 src_type, src_pos, n);
3490 return bset_from_bmap(bmap);
3493 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3494 enum isl_dim_type dst_type, unsigned dst_pos,
3495 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3497 if (!set)
3498 return NULL;
3499 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3500 return set_from_map(isl_map_move_dims(set_to_map(set),
3501 dst_type, dst_pos, src_type, src_pos, n));
3502 error:
3503 isl_set_free(set);
3504 return NULL;
3507 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3508 enum isl_dim_type dst_type, unsigned dst_pos,
3509 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3511 int i;
3513 if (!map)
3514 return NULL;
3515 if (n == 0)
3516 return map;
3518 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3519 goto error);
3521 if (dst_type == src_type && dst_pos == src_pos)
3522 return map;
3524 isl_assert(map->ctx, dst_type != src_type, goto error);
3526 map = isl_map_cow(map);
3527 if (!map)
3528 return NULL;
3530 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3531 if (!map->dim)
3532 goto error;
3534 for (i = 0; i < map->n; ++i) {
3535 map->p[i] = isl_basic_map_move_dims(map->p[i],
3536 dst_type, dst_pos,
3537 src_type, src_pos, n);
3538 if (!map->p[i])
3539 goto error;
3542 return map;
3543 error:
3544 isl_map_free(map);
3545 return NULL;
3548 /* Move the specified dimensions to the last columns right before
3549 * the divs. Don't change the dimension specification of bmap.
3550 * That's the responsibility of the caller.
3552 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3553 enum isl_dim_type type, unsigned first, unsigned n)
3555 struct isl_dim_map *dim_map;
3556 struct isl_basic_map *res;
3557 enum isl_dim_type t;
3558 unsigned total, off;
3560 if (!bmap)
3561 return NULL;
3562 if (pos(bmap->dim, type) + first + n ==
3563 1 + isl_space_dim(bmap->dim, isl_dim_all))
3564 return bmap;
3566 total = isl_basic_map_total_dim(bmap);
3567 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3569 off = 0;
3570 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3571 unsigned size = isl_space_dim(bmap->dim, t);
3572 if (t == type) {
3573 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3574 0, first, off);
3575 off += first;
3576 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3577 first, n, total - bmap->n_div - n);
3578 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3579 first + n, size - (first + n), off);
3580 off += size - (first + n);
3581 } else {
3582 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3583 off += size;
3586 isl_dim_map_div(dim_map, bmap, off + n);
3588 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3589 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3590 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3591 return res;
3594 /* Insert "n" rows in the divs of "bmap".
3596 * The number of columns is not changed, which means that the last
3597 * dimensions of "bmap" are being reintepreted as the new divs.
3598 * The space of "bmap" is not adjusted, however, which means
3599 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3600 * from the space of "bmap" is the responsibility of the caller.
3602 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3603 int n)
3605 int i;
3606 size_t row_size;
3607 isl_int **new_div;
3608 isl_int *old;
3610 bmap = isl_basic_map_cow(bmap);
3611 if (!bmap)
3612 return NULL;
3614 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3615 old = bmap->block2.data;
3616 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3617 (bmap->extra + n) * (1 + row_size));
3618 if (!bmap->block2.data)
3619 return isl_basic_map_free(bmap);
3620 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3621 if (!new_div)
3622 return isl_basic_map_free(bmap);
3623 for (i = 0; i < n; ++i) {
3624 new_div[i] = bmap->block2.data +
3625 (bmap->extra + i) * (1 + row_size);
3626 isl_seq_clr(new_div[i], 1 + row_size);
3628 for (i = 0; i < bmap->extra; ++i)
3629 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3630 free(bmap->div);
3631 bmap->div = new_div;
3632 bmap->n_div += n;
3633 bmap->extra += n;
3635 return bmap;
3638 /* Drop constraints from "bmap" that only involve the variables
3639 * of "type" in the range [first, first + n] that are not related
3640 * to any of the variables outside that interval.
3641 * These constraints cannot influence the values for the variables
3642 * outside the interval, except in case they cause "bmap" to be empty.
3643 * Only drop the constraints if "bmap" is known to be non-empty.
3645 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3646 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3647 unsigned first, unsigned n)
3649 int i;
3650 int *groups;
3651 unsigned dim, n_div;
3652 isl_bool non_empty;
3654 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3655 if (non_empty < 0)
3656 return isl_basic_map_free(bmap);
3657 if (!non_empty)
3658 return bmap;
3660 dim = isl_basic_map_dim(bmap, isl_dim_all);
3661 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3662 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3663 if (!groups)
3664 return isl_basic_map_free(bmap);
3665 first += isl_basic_map_offset(bmap, type) - 1;
3666 for (i = 0; i < first; ++i)
3667 groups[i] = -1;
3668 for (i = first + n; i < dim - n_div; ++i)
3669 groups[i] = -1;
3671 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3673 return bmap;
3676 /* Turn the n dimensions of type type, starting at first
3677 * into existentially quantified variables.
3679 * If a subset of the projected out variables are unrelated
3680 * to any of the variables that remain, then the constraints
3681 * involving this subset are simply dropped first.
3683 __isl_give isl_basic_map *isl_basic_map_project_out(
3684 __isl_take isl_basic_map *bmap,
3685 enum isl_dim_type type, unsigned first, unsigned n)
3687 if (n == 0)
3688 return basic_map_space_reset(bmap, type);
3689 if (type == isl_dim_div)
3690 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3691 "cannot project out existentially quantified variables",
3692 return isl_basic_map_free(bmap));
3694 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3695 if (!bmap)
3696 return NULL;
3698 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3699 return isl_basic_map_remove_dims(bmap, type, first, n);
3701 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3702 goto error);
3704 bmap = move_last(bmap, type, first, n);
3705 bmap = isl_basic_map_cow(bmap);
3706 bmap = insert_div_rows(bmap, n);
3707 if (!bmap)
3708 return NULL;
3710 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3711 if (!bmap->dim)
3712 goto error;
3713 bmap = isl_basic_map_simplify(bmap);
3714 bmap = isl_basic_map_drop_redundant_divs(bmap);
3715 return isl_basic_map_finalize(bmap);
3716 error:
3717 isl_basic_map_free(bmap);
3718 return NULL;
3721 /* Turn the n dimensions of type type, starting at first
3722 * into existentially quantified variables.
3724 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3725 enum isl_dim_type type, unsigned first, unsigned n)
3727 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3728 type, first, n));
3731 /* Turn the n dimensions of type type, starting at first
3732 * into existentially quantified variables.
3734 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3735 enum isl_dim_type type, unsigned first, unsigned n)
3737 int i;
3739 if (!map)
3740 return NULL;
3742 if (n == 0)
3743 return map_space_reset(map, type);
3745 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3747 map = isl_map_cow(map);
3748 if (!map)
3749 return NULL;
3751 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3752 if (!map->dim)
3753 goto error;
3755 for (i = 0; i < map->n; ++i) {
3756 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3757 if (!map->p[i])
3758 goto error;
3761 return map;
3762 error:
3763 isl_map_free(map);
3764 return NULL;
3767 /* Turn the n dimensions of type type, starting at first
3768 * into existentially quantified variables.
3770 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3771 enum isl_dim_type type, unsigned first, unsigned n)
3773 return set_from_map(isl_map_project_out(set_to_map(set),
3774 type, first, n));
3777 /* Return a map that projects the elements in "set" onto their
3778 * "n" set dimensions starting at "first".
3779 * "type" should be equal to isl_dim_set.
3781 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3782 enum isl_dim_type type, unsigned first, unsigned n)
3784 int i;
3785 int dim;
3786 isl_map *map;
3788 if (!set)
3789 return NULL;
3790 if (type != isl_dim_set)
3791 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3792 "only set dimensions can be projected out", goto error);
3793 dim = isl_set_dim(set, isl_dim_set);
3794 if (first + n > dim || first + n < first)
3795 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3796 "index out of bounds", goto error);
3798 map = isl_map_from_domain(set);
3799 map = isl_map_add_dims(map, isl_dim_out, n);
3800 for (i = 0; i < n; ++i)
3801 map = isl_map_equate(map, isl_dim_in, first + i,
3802 isl_dim_out, i);
3803 return map;
3804 error:
3805 isl_set_free(set);
3806 return NULL;
3809 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3811 int i, j;
3813 for (i = 0; i < n; ++i) {
3814 j = isl_basic_map_alloc_div(bmap);
3815 if (j < 0)
3816 goto error;
3817 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3819 return bmap;
3820 error:
3821 isl_basic_map_free(bmap);
3822 return NULL;
3825 struct isl_basic_map *isl_basic_map_apply_range(
3826 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3828 isl_space *dim_result = NULL;
3829 struct isl_basic_map *bmap;
3830 unsigned n_in, n_out, n, nparam, total, pos;
3831 struct isl_dim_map *dim_map1, *dim_map2;
3833 if (!bmap1 || !bmap2)
3834 goto error;
3835 if (!isl_space_match(bmap1->dim, isl_dim_param,
3836 bmap2->dim, isl_dim_param))
3837 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3838 "parameters don't match", goto error);
3839 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3840 bmap2->dim, isl_dim_in))
3841 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3842 "spaces don't match", goto error);
3844 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3845 isl_space_copy(bmap2->dim));
3847 n_in = isl_basic_map_n_in(bmap1);
3848 n_out = isl_basic_map_n_out(bmap2);
3849 n = isl_basic_map_n_out(bmap1);
3850 nparam = isl_basic_map_n_param(bmap1);
3852 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3853 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3854 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3855 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3856 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3857 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3858 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3859 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3860 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3861 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3862 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3864 bmap = isl_basic_map_alloc_space(dim_result,
3865 bmap1->n_div + bmap2->n_div + n,
3866 bmap1->n_eq + bmap2->n_eq,
3867 bmap1->n_ineq + bmap2->n_ineq);
3868 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3869 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3870 bmap = add_divs(bmap, n);
3871 bmap = isl_basic_map_simplify(bmap);
3872 bmap = isl_basic_map_drop_redundant_divs(bmap);
3873 return isl_basic_map_finalize(bmap);
3874 error:
3875 isl_basic_map_free(bmap1);
3876 isl_basic_map_free(bmap2);
3877 return NULL;
3880 struct isl_basic_set *isl_basic_set_apply(
3881 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3883 if (!bset || !bmap)
3884 goto error;
3886 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3887 goto error);
3889 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
3890 bmap));
3891 error:
3892 isl_basic_set_free(bset);
3893 isl_basic_map_free(bmap);
3894 return NULL;
3897 struct isl_basic_map *isl_basic_map_apply_domain(
3898 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3900 if (!bmap1 || !bmap2)
3901 goto error;
3903 isl_assert(bmap1->ctx,
3904 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3905 isl_assert(bmap1->ctx,
3906 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3907 goto error);
3909 bmap1 = isl_basic_map_reverse(bmap1);
3910 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3911 return isl_basic_map_reverse(bmap1);
3912 error:
3913 isl_basic_map_free(bmap1);
3914 isl_basic_map_free(bmap2);
3915 return NULL;
3918 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3919 * A \cap B -> f(A) + f(B)
3921 struct isl_basic_map *isl_basic_map_sum(
3922 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3924 unsigned n_in, n_out, nparam, total, pos;
3925 struct isl_basic_map *bmap = NULL;
3926 struct isl_dim_map *dim_map1, *dim_map2;
3927 int i;
3929 if (!bmap1 || !bmap2)
3930 goto error;
3932 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3933 goto error);
3935 nparam = isl_basic_map_n_param(bmap1);
3936 n_in = isl_basic_map_n_in(bmap1);
3937 n_out = isl_basic_map_n_out(bmap1);
3939 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3940 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3941 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3942 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3943 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3944 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3945 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3946 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3947 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3948 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3949 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3951 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3952 bmap1->n_div + bmap2->n_div + 2 * n_out,
3953 bmap1->n_eq + bmap2->n_eq + n_out,
3954 bmap1->n_ineq + bmap2->n_ineq);
3955 for (i = 0; i < n_out; ++i) {
3956 int j = isl_basic_map_alloc_equality(bmap);
3957 if (j < 0)
3958 goto error;
3959 isl_seq_clr(bmap->eq[j], 1+total);
3960 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3961 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3962 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3964 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3965 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3966 bmap = add_divs(bmap, 2 * n_out);
3968 bmap = isl_basic_map_simplify(bmap);
3969 return isl_basic_map_finalize(bmap);
3970 error:
3971 isl_basic_map_free(bmap);
3972 isl_basic_map_free(bmap1);
3973 isl_basic_map_free(bmap2);
3974 return NULL;
3977 /* Given two maps A -> f(A) and B -> g(B), construct a map
3978 * A \cap B -> f(A) + f(B)
3980 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3982 struct isl_map *result;
3983 int i, j;
3985 if (!map1 || !map2)
3986 goto error;
3988 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3990 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3991 map1->n * map2->n, 0);
3992 if (!result)
3993 goto error;
3994 for (i = 0; i < map1->n; ++i)
3995 for (j = 0; j < map2->n; ++j) {
3996 struct isl_basic_map *part;
3997 part = isl_basic_map_sum(
3998 isl_basic_map_copy(map1->p[i]),
3999 isl_basic_map_copy(map2->p[j]));
4000 if (isl_basic_map_is_empty(part))
4001 isl_basic_map_free(part);
4002 else
4003 result = isl_map_add_basic_map(result, part);
4004 if (!result)
4005 goto error;
4007 isl_map_free(map1);
4008 isl_map_free(map2);
4009 return result;
4010 error:
4011 isl_map_free(map1);
4012 isl_map_free(map2);
4013 return NULL;
4016 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4017 __isl_take isl_set *set2)
4019 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4022 /* Given a basic map A -> f(A), construct A -> -f(A).
4024 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4026 int i, j;
4027 unsigned off, n;
4029 bmap = isl_basic_map_cow(bmap);
4030 if (!bmap)
4031 return NULL;
4033 n = isl_basic_map_dim(bmap, isl_dim_out);
4034 off = isl_basic_map_offset(bmap, isl_dim_out);
4035 for (i = 0; i < bmap->n_eq; ++i)
4036 for (j = 0; j < n; ++j)
4037 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4038 for (i = 0; i < bmap->n_ineq; ++i)
4039 for (j = 0; j < n; ++j)
4040 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4041 for (i = 0; i < bmap->n_div; ++i)
4042 for (j = 0; j < n; ++j)
4043 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4044 bmap = isl_basic_map_gauss(bmap, NULL);
4045 return isl_basic_map_finalize(bmap);
4048 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4050 return isl_basic_map_neg(bset);
4053 /* Given a map A -> f(A), construct A -> -f(A).
4055 struct isl_map *isl_map_neg(struct isl_map *map)
4057 int i;
4059 map = isl_map_cow(map);
4060 if (!map)
4061 return NULL;
4063 for (i = 0; i < map->n; ++i) {
4064 map->p[i] = isl_basic_map_neg(map->p[i]);
4065 if (!map->p[i])
4066 goto error;
4069 return map;
4070 error:
4071 isl_map_free(map);
4072 return NULL;
4075 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4077 return set_from_map(isl_map_neg(set_to_map(set)));
4080 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4081 * A -> floor(f(A)/d).
4083 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4084 isl_int d)
4086 unsigned n_in, n_out, nparam, total, pos;
4087 struct isl_basic_map *result = NULL;
4088 struct isl_dim_map *dim_map;
4089 int i;
4091 if (!bmap)
4092 return NULL;
4094 nparam = isl_basic_map_n_param(bmap);
4095 n_in = isl_basic_map_n_in(bmap);
4096 n_out = isl_basic_map_n_out(bmap);
4098 total = nparam + n_in + n_out + bmap->n_div + n_out;
4099 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4100 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4101 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4102 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4103 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4105 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4106 bmap->n_div + n_out,
4107 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4108 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4109 result = add_divs(result, n_out);
4110 for (i = 0; i < n_out; ++i) {
4111 int j;
4112 j = isl_basic_map_alloc_inequality(result);
4113 if (j < 0)
4114 goto error;
4115 isl_seq_clr(result->ineq[j], 1+total);
4116 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4117 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4118 j = isl_basic_map_alloc_inequality(result);
4119 if (j < 0)
4120 goto error;
4121 isl_seq_clr(result->ineq[j], 1+total);
4122 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4123 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4124 isl_int_sub_ui(result->ineq[j][0], d, 1);
4127 result = isl_basic_map_simplify(result);
4128 return isl_basic_map_finalize(result);
4129 error:
4130 isl_basic_map_free(result);
4131 return NULL;
4134 /* Given a map A -> f(A) and an integer d, construct a map
4135 * A -> floor(f(A)/d).
4137 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4139 int i;
4141 map = isl_map_cow(map);
4142 if (!map)
4143 return NULL;
4145 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4146 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4147 for (i = 0; i < map->n; ++i) {
4148 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4149 if (!map->p[i])
4150 goto error;
4153 return map;
4154 error:
4155 isl_map_free(map);
4156 return NULL;
4159 /* Given a map A -> f(A) and an integer d, construct a map
4160 * A -> floor(f(A)/d).
4162 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4163 __isl_take isl_val *d)
4165 if (!map || !d)
4166 goto error;
4167 if (!isl_val_is_int(d))
4168 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4169 "expecting integer denominator", goto error);
4170 map = isl_map_floordiv(map, d->n);
4171 isl_val_free(d);
4172 return map;
4173 error:
4174 isl_map_free(map);
4175 isl_val_free(d);
4176 return NULL;
4179 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4181 int i;
4182 unsigned nparam;
4183 unsigned n_in;
4185 i = isl_basic_map_alloc_equality(bmap);
4186 if (i < 0)
4187 goto error;
4188 nparam = isl_basic_map_n_param(bmap);
4189 n_in = isl_basic_map_n_in(bmap);
4190 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4191 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4192 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4193 return isl_basic_map_finalize(bmap);
4194 error:
4195 isl_basic_map_free(bmap);
4196 return NULL;
4199 /* Add a constraint to "bmap" expressing i_pos < o_pos
4201 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4203 int i;
4204 unsigned nparam;
4205 unsigned n_in;
4207 i = isl_basic_map_alloc_inequality(bmap);
4208 if (i < 0)
4209 goto error;
4210 nparam = isl_basic_map_n_param(bmap);
4211 n_in = isl_basic_map_n_in(bmap);
4212 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4213 isl_int_set_si(bmap->ineq[i][0], -1);
4214 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4215 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4216 return isl_basic_map_finalize(bmap);
4217 error:
4218 isl_basic_map_free(bmap);
4219 return NULL;
4222 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4224 static __isl_give isl_basic_map *var_less_or_equal(
4225 __isl_take isl_basic_map *bmap, unsigned pos)
4227 int i;
4228 unsigned nparam;
4229 unsigned n_in;
4231 i = isl_basic_map_alloc_inequality(bmap);
4232 if (i < 0)
4233 goto error;
4234 nparam = isl_basic_map_n_param(bmap);
4235 n_in = isl_basic_map_n_in(bmap);
4236 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4237 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4238 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4239 return isl_basic_map_finalize(bmap);
4240 error:
4241 isl_basic_map_free(bmap);
4242 return NULL;
4245 /* Add a constraint to "bmap" expressing i_pos > o_pos
4247 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4249 int i;
4250 unsigned nparam;
4251 unsigned n_in;
4253 i = isl_basic_map_alloc_inequality(bmap);
4254 if (i < 0)
4255 goto error;
4256 nparam = isl_basic_map_n_param(bmap);
4257 n_in = isl_basic_map_n_in(bmap);
4258 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4259 isl_int_set_si(bmap->ineq[i][0], -1);
4260 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4261 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4262 return isl_basic_map_finalize(bmap);
4263 error:
4264 isl_basic_map_free(bmap);
4265 return NULL;
4268 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4270 static __isl_give isl_basic_map *var_more_or_equal(
4271 __isl_take isl_basic_map *bmap, unsigned pos)
4273 int i;
4274 unsigned nparam;
4275 unsigned n_in;
4277 i = isl_basic_map_alloc_inequality(bmap);
4278 if (i < 0)
4279 goto error;
4280 nparam = isl_basic_map_n_param(bmap);
4281 n_in = isl_basic_map_n_in(bmap);
4282 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4283 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4284 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4285 return isl_basic_map_finalize(bmap);
4286 error:
4287 isl_basic_map_free(bmap);
4288 return NULL;
4291 __isl_give isl_basic_map *isl_basic_map_equal(
4292 __isl_take isl_space *dim, unsigned n_equal)
4294 int i;
4295 struct isl_basic_map *bmap;
4296 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4297 if (!bmap)
4298 return NULL;
4299 for (i = 0; i < n_equal && bmap; ++i)
4300 bmap = var_equal(bmap, i);
4301 return isl_basic_map_finalize(bmap);
4304 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4306 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4307 unsigned pos)
4309 int i;
4310 struct isl_basic_map *bmap;
4311 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4312 if (!bmap)
4313 return NULL;
4314 for (i = 0; i < pos && bmap; ++i)
4315 bmap = var_equal(bmap, i);
4316 if (bmap)
4317 bmap = var_less(bmap, pos);
4318 return isl_basic_map_finalize(bmap);
4321 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4323 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4324 __isl_take isl_space *dim, unsigned pos)
4326 int i;
4327 isl_basic_map *bmap;
4329 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4330 for (i = 0; i < pos; ++i)
4331 bmap = var_equal(bmap, i);
4332 bmap = var_less_or_equal(bmap, pos);
4333 return isl_basic_map_finalize(bmap);
4336 /* Return a relation on "dim" expressing i_pos > o_pos
4338 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4339 unsigned pos)
4341 int i;
4342 struct isl_basic_map *bmap;
4343 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4344 if (!bmap)
4345 return NULL;
4346 for (i = 0; i < pos && bmap; ++i)
4347 bmap = var_equal(bmap, i);
4348 if (bmap)
4349 bmap = var_more(bmap, pos);
4350 return isl_basic_map_finalize(bmap);
4353 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4355 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4356 __isl_take isl_space *dim, unsigned pos)
4358 int i;
4359 isl_basic_map *bmap;
4361 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4362 for (i = 0; i < pos; ++i)
4363 bmap = var_equal(bmap, i);
4364 bmap = var_more_or_equal(bmap, pos);
4365 return isl_basic_map_finalize(bmap);
4368 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4369 unsigned n, int equal)
4371 struct isl_map *map;
4372 int i;
4374 if (n == 0 && equal)
4375 return isl_map_universe(dims);
4377 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4379 for (i = 0; i + 1 < n; ++i)
4380 map = isl_map_add_basic_map(map,
4381 isl_basic_map_less_at(isl_space_copy(dims), i));
4382 if (n > 0) {
4383 if (equal)
4384 map = isl_map_add_basic_map(map,
4385 isl_basic_map_less_or_equal_at(dims, n - 1));
4386 else
4387 map = isl_map_add_basic_map(map,
4388 isl_basic_map_less_at(dims, n - 1));
4389 } else
4390 isl_space_free(dims);
4392 return map;
4395 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4397 if (!dims)
4398 return NULL;
4399 return map_lex_lte_first(dims, dims->n_out, equal);
4402 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4404 return map_lex_lte_first(dim, n, 0);
4407 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4409 return map_lex_lte_first(dim, n, 1);
4412 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4414 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4417 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4419 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4422 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4423 unsigned n, int equal)
4425 struct isl_map *map;
4426 int i;
4428 if (n == 0 && equal)
4429 return isl_map_universe(dims);
4431 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4433 for (i = 0; i + 1 < n; ++i)
4434 map = isl_map_add_basic_map(map,
4435 isl_basic_map_more_at(isl_space_copy(dims), i));
4436 if (n > 0) {
4437 if (equal)
4438 map = isl_map_add_basic_map(map,
4439 isl_basic_map_more_or_equal_at(dims, n - 1));
4440 else
4441 map = isl_map_add_basic_map(map,
4442 isl_basic_map_more_at(dims, n - 1));
4443 } else
4444 isl_space_free(dims);
4446 return map;
4449 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4451 if (!dims)
4452 return NULL;
4453 return map_lex_gte_first(dims, dims->n_out, equal);
4456 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4458 return map_lex_gte_first(dim, n, 0);
4461 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4463 return map_lex_gte_first(dim, n, 1);
4466 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4468 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4471 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4473 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4476 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4477 __isl_take isl_set *set2)
4479 isl_map *map;
4480 map = isl_map_lex_le(isl_set_get_space(set1));
4481 map = isl_map_intersect_domain(map, set1);
4482 map = isl_map_intersect_range(map, set2);
4483 return map;
4486 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4487 __isl_take isl_set *set2)
4489 isl_map *map;
4490 map = isl_map_lex_lt(isl_set_get_space(set1));
4491 map = isl_map_intersect_domain(map, set1);
4492 map = isl_map_intersect_range(map, set2);
4493 return map;
4496 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4497 __isl_take isl_set *set2)
4499 isl_map *map;
4500 map = isl_map_lex_ge(isl_set_get_space(set1));
4501 map = isl_map_intersect_domain(map, set1);
4502 map = isl_map_intersect_range(map, set2);
4503 return map;
4506 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4507 __isl_take isl_set *set2)
4509 isl_map *map;
4510 map = isl_map_lex_gt(isl_set_get_space(set1));
4511 map = isl_map_intersect_domain(map, set1);
4512 map = isl_map_intersect_range(map, set2);
4513 return map;
4516 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4517 __isl_take isl_map *map2)
4519 isl_map *map;
4520 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4521 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4522 map = isl_map_apply_range(map, isl_map_reverse(map2));
4523 return map;
4526 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4527 __isl_take isl_map *map2)
4529 isl_map *map;
4530 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4531 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4532 map = isl_map_apply_range(map, isl_map_reverse(map2));
4533 return map;
4536 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4537 __isl_take isl_map *map2)
4539 isl_map *map;
4540 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4541 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4542 map = isl_map_apply_range(map, isl_map_reverse(map2));
4543 return map;
4546 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4547 __isl_take isl_map *map2)
4549 isl_map *map;
4550 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4551 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4552 map = isl_map_apply_range(map, isl_map_reverse(map2));
4553 return map;
4556 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4557 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4559 struct isl_basic_map *bmap;
4561 bset = isl_basic_set_cow(bset);
4562 if (!bset || !dim)
4563 goto error;
4565 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4566 isl_space_free(bset->dim);
4567 bmap = bset_to_bmap(bset);
4568 bmap->dim = dim;
4569 return isl_basic_map_finalize(bmap);
4570 error:
4571 isl_basic_set_free(bset);
4572 isl_space_free(dim);
4573 return NULL;
4576 /* For a div d = floor(f/m), add the constraint
4578 * f - m d >= 0
4580 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4581 unsigned pos, isl_int *div)
4583 int i;
4584 unsigned total = isl_basic_map_total_dim(bmap);
4586 i = isl_basic_map_alloc_inequality(bmap);
4587 if (i < 0)
4588 return -1;
4589 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4590 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4592 return 0;
4595 /* For a div d = floor(f/m), add the constraint
4597 * -(f-(m-1)) + m d >= 0
4599 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4600 unsigned pos, isl_int *div)
4602 int i;
4603 unsigned total = isl_basic_map_total_dim(bmap);
4605 i = isl_basic_map_alloc_inequality(bmap);
4606 if (i < 0)
4607 return -1;
4608 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4609 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4610 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4611 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4613 return 0;
4616 /* For a div d = floor(f/m), add the constraints
4618 * f - m d >= 0
4619 * -(f-(m-1)) + m d >= 0
4621 * Note that the second constraint is the negation of
4623 * f - m d >= m
4625 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4626 unsigned pos, isl_int *div)
4628 if (add_upper_div_constraint(bmap, pos, div) < 0)
4629 return -1;
4630 if (add_lower_div_constraint(bmap, pos, div) < 0)
4631 return -1;
4632 return 0;
4635 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4636 unsigned pos, isl_int *div)
4638 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4639 pos, div);
4642 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4644 unsigned total = isl_basic_map_total_dim(bmap);
4645 unsigned div_pos = total - bmap->n_div + div;
4647 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4648 bmap->div[div]);
4651 /* For each known div d = floor(f/m), add the constraints
4653 * f - m d >= 0
4654 * -(f-(m-1)) + m d >= 0
4656 * Remove duplicate constraints in case of some these div constraints
4657 * already appear in "bmap".
4659 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4660 __isl_take isl_basic_map *bmap)
4662 unsigned n_div;
4664 if (!bmap)
4665 return NULL;
4666 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4667 if (n_div == 0)
4668 return bmap;
4670 bmap = add_known_div_constraints(bmap);
4671 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4672 bmap = isl_basic_map_finalize(bmap);
4673 return bmap;
4676 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4678 * In particular, if this div is of the form d = floor(f/m),
4679 * then add the constraint
4681 * f - m d >= 0
4683 * if sign < 0 or the constraint
4685 * -(f-(m-1)) + m d >= 0
4687 * if sign > 0.
4689 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4690 unsigned div, int sign)
4692 unsigned total;
4693 unsigned div_pos;
4695 if (!bmap)
4696 return -1;
4698 total = isl_basic_map_total_dim(bmap);
4699 div_pos = total - bmap->n_div + div;
4701 if (sign < 0)
4702 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4703 else
4704 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4707 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4709 return isl_basic_map_add_div_constraints(bset, div);
4712 struct isl_basic_set *isl_basic_map_underlying_set(
4713 struct isl_basic_map *bmap)
4715 if (!bmap)
4716 goto error;
4717 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4718 bmap->n_div == 0 &&
4719 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4720 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4721 return bset_from_bmap(bmap);
4722 bmap = isl_basic_map_cow(bmap);
4723 if (!bmap)
4724 goto error;
4725 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4726 if (!bmap->dim)
4727 goto error;
4728 bmap->extra -= bmap->n_div;
4729 bmap->n_div = 0;
4730 bmap = isl_basic_map_finalize(bmap);
4731 return bset_from_bmap(bmap);
4732 error:
4733 isl_basic_map_free(bmap);
4734 return NULL;
4737 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4738 __isl_take isl_basic_set *bset)
4740 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4743 /* Replace each element in "list" by the result of applying
4744 * isl_basic_map_underlying_set to the element.
4746 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4747 __isl_take isl_basic_map_list *list)
4749 int i, n;
4751 if (!list)
4752 return NULL;
4754 n = isl_basic_map_list_n_basic_map(list);
4755 for (i = 0; i < n; ++i) {
4756 isl_basic_map *bmap;
4757 isl_basic_set *bset;
4759 bmap = isl_basic_map_list_get_basic_map(list, i);
4760 bset = isl_basic_set_underlying_set(bmap);
4761 list = isl_basic_set_list_set_basic_set(list, i, bset);
4764 return list;
4767 struct isl_basic_map *isl_basic_map_overlying_set(
4768 struct isl_basic_set *bset, struct isl_basic_map *like)
4770 struct isl_basic_map *bmap;
4771 struct isl_ctx *ctx;
4772 unsigned total;
4773 int i;
4775 if (!bset || !like)
4776 goto error;
4777 ctx = bset->ctx;
4778 isl_assert(ctx, bset->n_div == 0, goto error);
4779 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4780 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4781 goto error);
4782 if (like->n_div == 0) {
4783 isl_space *space = isl_basic_map_get_space(like);
4784 isl_basic_map_free(like);
4785 return isl_basic_map_reset_space(bset, space);
4787 bset = isl_basic_set_cow(bset);
4788 if (!bset)
4789 goto error;
4790 total = bset->dim->n_out + bset->extra;
4791 bmap = bset_to_bmap(bset);
4792 isl_space_free(bmap->dim);
4793 bmap->dim = isl_space_copy(like->dim);
4794 if (!bmap->dim)
4795 goto error;
4796 bmap->n_div = like->n_div;
4797 bmap->extra += like->n_div;
4798 if (bmap->extra) {
4799 unsigned ltotal;
4800 isl_int **div;
4801 ltotal = total - bmap->extra + like->extra;
4802 if (ltotal > total)
4803 ltotal = total;
4804 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4805 bmap->extra * (1 + 1 + total));
4806 if (isl_blk_is_error(bmap->block2))
4807 goto error;
4808 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4809 if (!div)
4810 goto error;
4811 bmap->div = div;
4812 for (i = 0; i < bmap->extra; ++i)
4813 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4814 for (i = 0; i < like->n_div; ++i) {
4815 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4816 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4818 bmap = isl_basic_map_add_known_div_constraints(bmap);
4820 isl_basic_map_free(like);
4821 bmap = isl_basic_map_simplify(bmap);
4822 bmap = isl_basic_map_finalize(bmap);
4823 return bmap;
4824 error:
4825 isl_basic_map_free(like);
4826 isl_basic_set_free(bset);
4827 return NULL;
4830 struct isl_basic_set *isl_basic_set_from_underlying_set(
4831 struct isl_basic_set *bset, struct isl_basic_set *like)
4833 return bset_from_bmap(isl_basic_map_overlying_set(bset,
4834 bset_to_bmap(like)));
4837 struct isl_set *isl_set_from_underlying_set(
4838 struct isl_set *set, struct isl_basic_set *like)
4840 int i;
4842 if (!set || !like)
4843 goto error;
4844 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4845 goto error);
4846 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4847 isl_basic_set_free(like);
4848 return set;
4850 set = isl_set_cow(set);
4851 if (!set)
4852 goto error;
4853 for (i = 0; i < set->n; ++i) {
4854 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4855 isl_basic_set_copy(like));
4856 if (!set->p[i])
4857 goto error;
4859 isl_space_free(set->dim);
4860 set->dim = isl_space_copy(like->dim);
4861 if (!set->dim)
4862 goto error;
4863 isl_basic_set_free(like);
4864 return set;
4865 error:
4866 isl_basic_set_free(like);
4867 isl_set_free(set);
4868 return NULL;
4871 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4873 int i;
4875 map = isl_map_cow(map);
4876 if (!map)
4877 return NULL;
4878 map->dim = isl_space_cow(map->dim);
4879 if (!map->dim)
4880 goto error;
4882 for (i = 1; i < map->n; ++i)
4883 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4884 goto error);
4885 for (i = 0; i < map->n; ++i) {
4886 map->p[i] = bset_to_bmap(
4887 isl_basic_map_underlying_set(map->p[i]));
4888 if (!map->p[i])
4889 goto error;
4891 if (map->n == 0)
4892 map->dim = isl_space_underlying(map->dim, 0);
4893 else {
4894 isl_space_free(map->dim);
4895 map->dim = isl_space_copy(map->p[0]->dim);
4897 if (!map->dim)
4898 goto error;
4899 return set_from_map(map);
4900 error:
4901 isl_map_free(map);
4902 return NULL;
4905 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4907 return set_from_map(isl_map_underlying_set(set_to_map(set)));
4910 /* Replace the space of "bmap" by "space".
4912 * If the space of "bmap" is identical to "space" (including the identifiers
4913 * of the input and output dimensions), then simply return the original input.
4915 __isl_give isl_basic_map *isl_basic_map_reset_space(
4916 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4918 isl_bool equal;
4920 if (!bmap)
4921 goto error;
4922 equal = isl_space_is_equal(bmap->dim, space);
4923 if (equal >= 0 && equal)
4924 equal = isl_space_match(bmap->dim, isl_dim_in,
4925 space, isl_dim_in);
4926 if (equal >= 0 && equal)
4927 equal = isl_space_match(bmap->dim, isl_dim_out,
4928 space, isl_dim_out);
4929 if (equal < 0)
4930 goto error;
4931 if (equal) {
4932 isl_space_free(space);
4933 return bmap;
4935 bmap = isl_basic_map_cow(bmap);
4936 if (!bmap || !space)
4937 goto error;
4939 isl_space_free(bmap->dim);
4940 bmap->dim = space;
4942 bmap = isl_basic_map_finalize(bmap);
4944 return bmap;
4945 error:
4946 isl_basic_map_free(bmap);
4947 isl_space_free(space);
4948 return NULL;
4951 __isl_give isl_basic_set *isl_basic_set_reset_space(
4952 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4954 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
4955 dim));
4958 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4959 __isl_take isl_space *dim)
4961 int i;
4963 map = isl_map_cow(map);
4964 if (!map || !dim)
4965 goto error;
4967 for (i = 0; i < map->n; ++i) {
4968 map->p[i] = isl_basic_map_reset_space(map->p[i],
4969 isl_space_copy(dim));
4970 if (!map->p[i])
4971 goto error;
4973 isl_space_free(map->dim);
4974 map->dim = dim;
4976 return map;
4977 error:
4978 isl_map_free(map);
4979 isl_space_free(dim);
4980 return NULL;
4983 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4984 __isl_take isl_space *dim)
4986 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
4989 /* Compute the parameter domain of the given basic set.
4991 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4993 isl_space *space;
4994 unsigned n;
4996 if (isl_basic_set_is_params(bset))
4997 return bset;
4999 n = isl_basic_set_dim(bset, isl_dim_set);
5000 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5001 space = isl_basic_set_get_space(bset);
5002 space = isl_space_params(space);
5003 bset = isl_basic_set_reset_space(bset, space);
5004 return bset;
5007 /* Construct a zero-dimensional basic set with the given parameter domain.
5009 __isl_give isl_basic_set *isl_basic_set_from_params(
5010 __isl_take isl_basic_set *bset)
5012 isl_space *space;
5013 space = isl_basic_set_get_space(bset);
5014 space = isl_space_set_from_params(space);
5015 bset = isl_basic_set_reset_space(bset, space);
5016 return bset;
5019 /* Compute the parameter domain of the given set.
5021 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5023 isl_space *space;
5024 unsigned n;
5026 if (isl_set_is_params(set))
5027 return set;
5029 n = isl_set_dim(set, isl_dim_set);
5030 set = isl_set_project_out(set, isl_dim_set, 0, n);
5031 space = isl_set_get_space(set);
5032 space = isl_space_params(space);
5033 set = isl_set_reset_space(set, space);
5034 return set;
5037 /* Construct a zero-dimensional set with the given parameter domain.
5039 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5041 isl_space *space;
5042 space = isl_set_get_space(set);
5043 space = isl_space_set_from_params(space);
5044 set = isl_set_reset_space(set, space);
5045 return set;
5048 /* Compute the parameter domain of the given map.
5050 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5052 isl_space *space;
5053 unsigned n;
5055 n = isl_map_dim(map, isl_dim_in);
5056 map = isl_map_project_out(map, isl_dim_in, 0, n);
5057 n = isl_map_dim(map, isl_dim_out);
5058 map = isl_map_project_out(map, isl_dim_out, 0, n);
5059 space = isl_map_get_space(map);
5060 space = isl_space_params(space);
5061 map = isl_map_reset_space(map, space);
5062 return map;
5065 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5067 isl_space *space;
5068 unsigned n_out;
5070 if (!bmap)
5071 return NULL;
5072 space = isl_space_domain(isl_basic_map_get_space(bmap));
5074 n_out = isl_basic_map_n_out(bmap);
5075 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5077 return isl_basic_map_reset_space(bmap, space);
5080 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5082 if (!bmap)
5083 return -1;
5084 return isl_space_may_be_set(bmap->dim);
5087 /* Is this basic map actually a set?
5088 * Users should never call this function. Outside of isl,
5089 * the type should indicate whether something is a set or a map.
5091 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5093 if (!bmap)
5094 return -1;
5095 return isl_space_is_set(bmap->dim);
5098 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5100 if (!bmap)
5101 return NULL;
5102 if (isl_basic_map_is_set(bmap))
5103 return bmap;
5104 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5107 __isl_give isl_basic_map *isl_basic_map_domain_map(
5108 __isl_take isl_basic_map *bmap)
5110 int i, k;
5111 isl_space *dim;
5112 isl_basic_map *domain;
5113 int nparam, n_in, n_out;
5114 unsigned total;
5116 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5117 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5118 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5120 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5121 domain = isl_basic_map_universe(dim);
5123 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5124 bmap = isl_basic_map_apply_range(bmap, domain);
5125 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5127 total = isl_basic_map_total_dim(bmap);
5129 for (i = 0; i < n_in; ++i) {
5130 k = isl_basic_map_alloc_equality(bmap);
5131 if (k < 0)
5132 goto error;
5133 isl_seq_clr(bmap->eq[k], 1 + total);
5134 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5135 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5138 bmap = isl_basic_map_gauss(bmap, NULL);
5139 return isl_basic_map_finalize(bmap);
5140 error:
5141 isl_basic_map_free(bmap);
5142 return NULL;
5145 __isl_give isl_basic_map *isl_basic_map_range_map(
5146 __isl_take isl_basic_map *bmap)
5148 int i, k;
5149 isl_space *dim;
5150 isl_basic_map *range;
5151 int nparam, n_in, n_out;
5152 unsigned total;
5154 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5155 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5156 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5158 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5159 range = isl_basic_map_universe(dim);
5161 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5162 bmap = isl_basic_map_apply_range(bmap, range);
5163 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5165 total = isl_basic_map_total_dim(bmap);
5167 for (i = 0; i < n_out; ++i) {
5168 k = isl_basic_map_alloc_equality(bmap);
5169 if (k < 0)
5170 goto error;
5171 isl_seq_clr(bmap->eq[k], 1 + total);
5172 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5173 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5176 bmap = isl_basic_map_gauss(bmap, NULL);
5177 return isl_basic_map_finalize(bmap);
5178 error:
5179 isl_basic_map_free(bmap);
5180 return NULL;
5183 int isl_map_may_be_set(__isl_keep isl_map *map)
5185 if (!map)
5186 return -1;
5187 return isl_space_may_be_set(map->dim);
5190 /* Is this map actually a set?
5191 * Users should never call this function. Outside of isl,
5192 * the type should indicate whether something is a set or a map.
5194 int isl_map_is_set(__isl_keep isl_map *map)
5196 if (!map)
5197 return -1;
5198 return isl_space_is_set(map->dim);
5201 struct isl_set *isl_map_range(struct isl_map *map)
5203 int i;
5204 struct isl_set *set;
5206 if (!map)
5207 goto error;
5208 if (isl_map_is_set(map))
5209 return set_from_map(map);
5211 map = isl_map_cow(map);
5212 if (!map)
5213 goto error;
5215 set = set_from_map(map);
5216 set->dim = isl_space_range(set->dim);
5217 if (!set->dim)
5218 goto error;
5219 for (i = 0; i < map->n; ++i) {
5220 set->p[i] = isl_basic_map_range(map->p[i]);
5221 if (!set->p[i])
5222 goto error;
5224 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5225 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5226 return set;
5227 error:
5228 isl_map_free(map);
5229 return NULL;
5232 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5234 int i;
5236 map = isl_map_cow(map);
5237 if (!map)
5238 return NULL;
5240 map->dim = isl_space_domain_map(map->dim);
5241 if (!map->dim)
5242 goto error;
5243 for (i = 0; i < map->n; ++i) {
5244 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5245 if (!map->p[i])
5246 goto error;
5248 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5249 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5250 return map;
5251 error:
5252 isl_map_free(map);
5253 return NULL;
5256 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5258 int i;
5259 isl_space *range_dim;
5261 map = isl_map_cow(map);
5262 if (!map)
5263 return NULL;
5265 range_dim = isl_space_range(isl_map_get_space(map));
5266 range_dim = isl_space_from_range(range_dim);
5267 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5268 map->dim = isl_space_join(map->dim, range_dim);
5269 if (!map->dim)
5270 goto error;
5271 for (i = 0; i < map->n; ++i) {
5272 map->p[i] = isl_basic_map_range_map(map->p[i]);
5273 if (!map->p[i])
5274 goto error;
5276 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5277 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5278 return map;
5279 error:
5280 isl_map_free(map);
5281 return NULL;
5284 /* Given a wrapped map of the form A[B -> C],
5285 * return the map A[B -> C] -> B.
5287 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5289 isl_id *id;
5290 isl_map *map;
5292 if (!set)
5293 return NULL;
5294 if (!isl_set_has_tuple_id(set))
5295 return isl_map_domain_map(isl_set_unwrap(set));
5297 id = isl_set_get_tuple_id(set);
5298 map = isl_map_domain_map(isl_set_unwrap(set));
5299 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5301 return map;
5304 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5305 __isl_take isl_space *dim)
5307 int i;
5308 struct isl_map *map = NULL;
5310 set = isl_set_cow(set);
5311 if (!set || !dim)
5312 goto error;
5313 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5314 map = set_to_map(set);
5315 for (i = 0; i < set->n; ++i) {
5316 map->p[i] = isl_basic_map_from_basic_set(
5317 set->p[i], isl_space_copy(dim));
5318 if (!map->p[i])
5319 goto error;
5321 isl_space_free(map->dim);
5322 map->dim = dim;
5323 return map;
5324 error:
5325 isl_space_free(dim);
5326 isl_set_free(set);
5327 return NULL;
5330 __isl_give isl_basic_map *isl_basic_map_from_domain(
5331 __isl_take isl_basic_set *bset)
5333 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5336 __isl_give isl_basic_map *isl_basic_map_from_range(
5337 __isl_take isl_basic_set *bset)
5339 isl_space *space;
5340 space = isl_basic_set_get_space(bset);
5341 space = isl_space_from_range(space);
5342 bset = isl_basic_set_reset_space(bset, space);
5343 return bset_to_bmap(bset);
5346 /* Create a relation with the given set as range.
5347 * The domain of the created relation is a zero-dimensional
5348 * flat anonymous space.
5350 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5352 isl_space *space;
5353 space = isl_set_get_space(set);
5354 space = isl_space_from_range(space);
5355 set = isl_set_reset_space(set, space);
5356 return set_to_map(set);
5359 /* Create a relation with the given set as domain.
5360 * The range of the created relation is a zero-dimensional
5361 * flat anonymous space.
5363 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5365 return isl_map_reverse(isl_map_from_range(set));
5368 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5369 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5371 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5374 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5375 __isl_take isl_set *range)
5377 return isl_map_apply_range(isl_map_reverse(domain), range);
5380 /* Return a newly allocated isl_map with given space and flags and
5381 * room for "n" basic maps.
5382 * Make sure that all cached information is cleared.
5384 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5385 unsigned flags)
5387 struct isl_map *map;
5389 if (!space)
5390 return NULL;
5391 if (n < 0)
5392 isl_die(space->ctx, isl_error_internal,
5393 "negative number of basic maps", goto error);
5394 map = isl_calloc(space->ctx, struct isl_map,
5395 sizeof(struct isl_map) +
5396 (n - 1) * sizeof(struct isl_basic_map *));
5397 if (!map)
5398 goto error;
5400 map->ctx = space->ctx;
5401 isl_ctx_ref(map->ctx);
5402 map->ref = 1;
5403 map->size = n;
5404 map->n = 0;
5405 map->dim = space;
5406 map->flags = flags;
5407 return map;
5408 error:
5409 isl_space_free(space);
5410 return NULL;
5413 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5414 unsigned nparam, unsigned in, unsigned out, int n,
5415 unsigned flags)
5417 struct isl_map *map;
5418 isl_space *dims;
5420 dims = isl_space_alloc(ctx, nparam, in, out);
5421 if (!dims)
5422 return NULL;
5424 map = isl_map_alloc_space(dims, n, flags);
5425 return map;
5428 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5430 struct isl_basic_map *bmap;
5431 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5432 bmap = isl_basic_map_set_to_empty(bmap);
5433 return bmap;
5436 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5438 struct isl_basic_set *bset;
5439 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5440 bset = isl_basic_set_set_to_empty(bset);
5441 return bset;
5444 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5446 struct isl_basic_map *bmap;
5447 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5448 bmap = isl_basic_map_finalize(bmap);
5449 return bmap;
5452 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5454 struct isl_basic_set *bset;
5455 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5456 bset = isl_basic_set_finalize(bset);
5457 return bset;
5460 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5462 int i;
5463 unsigned total = isl_space_dim(dim, isl_dim_all);
5464 isl_basic_map *bmap;
5466 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5467 for (i = 0; i < total; ++i) {
5468 int k = isl_basic_map_alloc_inequality(bmap);
5469 if (k < 0)
5470 goto error;
5471 isl_seq_clr(bmap->ineq[k], 1 + total);
5472 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5474 return bmap;
5475 error:
5476 isl_basic_map_free(bmap);
5477 return NULL;
5480 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5482 return isl_basic_map_nat_universe(dim);
5485 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5487 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5490 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5492 return isl_map_nat_universe(dim);
5495 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5497 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5500 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5502 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5505 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5507 struct isl_map *map;
5508 if (!dim)
5509 return NULL;
5510 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5511 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5512 return map;
5515 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5517 struct isl_set *set;
5518 if (!dim)
5519 return NULL;
5520 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5521 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5522 return set;
5525 struct isl_map *isl_map_dup(struct isl_map *map)
5527 int i;
5528 struct isl_map *dup;
5530 if (!map)
5531 return NULL;
5532 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5533 for (i = 0; i < map->n; ++i)
5534 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5535 return dup;
5538 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5539 __isl_take isl_basic_map *bmap)
5541 if (!bmap || !map)
5542 goto error;
5543 if (isl_basic_map_plain_is_empty(bmap)) {
5544 isl_basic_map_free(bmap);
5545 return map;
5547 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5548 isl_assert(map->ctx, map->n < map->size, goto error);
5549 map->p[map->n] = bmap;
5550 map->n++;
5551 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5552 return map;
5553 error:
5554 if (map)
5555 isl_map_free(map);
5556 if (bmap)
5557 isl_basic_map_free(bmap);
5558 return NULL;
5561 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5563 int i;
5565 if (!map)
5566 return NULL;
5568 if (--map->ref > 0)
5569 return NULL;
5571 clear_caches(map);
5572 isl_ctx_deref(map->ctx);
5573 for (i = 0; i < map->n; ++i)
5574 isl_basic_map_free(map->p[i]);
5575 isl_space_free(map->dim);
5576 free(map);
5578 return NULL;
5581 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5582 struct isl_basic_map *bmap, unsigned pos, int value)
5584 int j;
5586 bmap = isl_basic_map_cow(bmap);
5587 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5588 j = isl_basic_map_alloc_equality(bmap);
5589 if (j < 0)
5590 goto error;
5591 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5592 isl_int_set_si(bmap->eq[j][pos], -1);
5593 isl_int_set_si(bmap->eq[j][0], value);
5594 bmap = isl_basic_map_simplify(bmap);
5595 return isl_basic_map_finalize(bmap);
5596 error:
5597 isl_basic_map_free(bmap);
5598 return NULL;
5601 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5602 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5604 int j;
5606 bmap = isl_basic_map_cow(bmap);
5607 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5608 j = isl_basic_map_alloc_equality(bmap);
5609 if (j < 0)
5610 goto error;
5611 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5612 isl_int_set_si(bmap->eq[j][pos], -1);
5613 isl_int_set(bmap->eq[j][0], value);
5614 bmap = isl_basic_map_simplify(bmap);
5615 return isl_basic_map_finalize(bmap);
5616 error:
5617 isl_basic_map_free(bmap);
5618 return NULL;
5621 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5622 enum isl_dim_type type, unsigned pos, int value)
5624 if (!bmap)
5625 return NULL;
5626 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5627 return isl_basic_map_fix_pos_si(bmap,
5628 isl_basic_map_offset(bmap, type) + pos, value);
5629 error:
5630 isl_basic_map_free(bmap);
5631 return NULL;
5634 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5635 enum isl_dim_type type, unsigned pos, isl_int value)
5637 if (!bmap)
5638 return NULL;
5639 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5640 return isl_basic_map_fix_pos(bmap,
5641 isl_basic_map_offset(bmap, type) + pos, value);
5642 error:
5643 isl_basic_map_free(bmap);
5644 return NULL;
5647 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5648 * to be equal to "v".
5650 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5651 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5653 if (!bmap || !v)
5654 goto error;
5655 if (!isl_val_is_int(v))
5656 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5657 "expecting integer value", goto error);
5658 if (pos >= isl_basic_map_dim(bmap, type))
5659 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5660 "index out of bounds", goto error);
5661 pos += isl_basic_map_offset(bmap, type);
5662 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5663 isl_val_free(v);
5664 return bmap;
5665 error:
5666 isl_basic_map_free(bmap);
5667 isl_val_free(v);
5668 return NULL;
5671 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5672 * to be equal to "v".
5674 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5675 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5677 return isl_basic_map_fix_val(bset, type, pos, v);
5680 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5681 enum isl_dim_type type, unsigned pos, int value)
5683 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5684 type, pos, value));
5687 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5688 enum isl_dim_type type, unsigned pos, isl_int value)
5690 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5691 type, pos, value));
5694 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5695 unsigned input, int value)
5697 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5700 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5701 unsigned dim, int value)
5703 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5704 isl_dim_set, dim, value));
5707 static int remove_if_empty(__isl_keep isl_map *map, int i)
5709 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5711 if (empty < 0)
5712 return -1;
5713 if (!empty)
5714 return 0;
5716 isl_basic_map_free(map->p[i]);
5717 if (i != map->n - 1) {
5718 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5719 map->p[i] = map->p[map->n - 1];
5721 map->n--;
5723 return 0;
5726 /* Perform "fn" on each basic map of "map", where we may not be holding
5727 * the only reference to "map".
5728 * In particular, "fn" should be a semantics preserving operation
5729 * that we want to apply to all copies of "map". We therefore need
5730 * to be careful not to modify "map" in a way that breaks "map"
5731 * in case anything goes wrong.
5733 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5734 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5736 struct isl_basic_map *bmap;
5737 int i;
5739 if (!map)
5740 return NULL;
5742 for (i = map->n - 1; i >= 0; --i) {
5743 bmap = isl_basic_map_copy(map->p[i]);
5744 bmap = fn(bmap);
5745 if (!bmap)
5746 goto error;
5747 isl_basic_map_free(map->p[i]);
5748 map->p[i] = bmap;
5749 if (remove_if_empty(map, i) < 0)
5750 goto error;
5753 return map;
5754 error:
5755 isl_map_free(map);
5756 return NULL;
5759 struct isl_map *isl_map_fix_si(struct isl_map *map,
5760 enum isl_dim_type type, unsigned pos, int value)
5762 int i;
5764 map = isl_map_cow(map);
5765 if (!map)
5766 return NULL;
5768 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5769 for (i = map->n - 1; i >= 0; --i) {
5770 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5771 if (remove_if_empty(map, i) < 0)
5772 goto error;
5774 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5775 return map;
5776 error:
5777 isl_map_free(map);
5778 return NULL;
5781 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5782 enum isl_dim_type type, unsigned pos, int value)
5784 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
5787 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5788 enum isl_dim_type type, unsigned pos, isl_int value)
5790 int i;
5792 map = isl_map_cow(map);
5793 if (!map)
5794 return NULL;
5796 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5797 for (i = 0; i < map->n; ++i) {
5798 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5799 if (!map->p[i])
5800 goto error;
5802 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5803 return map;
5804 error:
5805 isl_map_free(map);
5806 return NULL;
5809 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5810 enum isl_dim_type type, unsigned pos, isl_int value)
5812 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
5815 /* Fix the value of the variable at position "pos" of type "type" of "map"
5816 * to be equal to "v".
5818 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5819 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5821 int i;
5823 map = isl_map_cow(map);
5824 if (!map || !v)
5825 goto error;
5827 if (!isl_val_is_int(v))
5828 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5829 "expecting integer value", goto error);
5830 if (pos >= isl_map_dim(map, type))
5831 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5832 "index out of bounds", goto error);
5833 for (i = map->n - 1; i >= 0; --i) {
5834 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5835 isl_val_copy(v));
5836 if (remove_if_empty(map, i) < 0)
5837 goto error;
5839 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5840 isl_val_free(v);
5841 return map;
5842 error:
5843 isl_map_free(map);
5844 isl_val_free(v);
5845 return NULL;
5848 /* Fix the value of the variable at position "pos" of type "type" of "set"
5849 * to be equal to "v".
5851 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5852 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5854 return isl_map_fix_val(set, type, pos, v);
5857 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5858 unsigned input, int value)
5860 return isl_map_fix_si(map, isl_dim_in, input, value);
5863 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5865 return set_from_map(isl_map_fix_si(set_to_map(set),
5866 isl_dim_set, dim, value));
5869 static __isl_give isl_basic_map *basic_map_bound_si(
5870 __isl_take isl_basic_map *bmap,
5871 enum isl_dim_type type, unsigned pos, int value, int upper)
5873 int j;
5875 if (!bmap)
5876 return NULL;
5877 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5878 pos += isl_basic_map_offset(bmap, type);
5879 bmap = isl_basic_map_cow(bmap);
5880 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5881 j = isl_basic_map_alloc_inequality(bmap);
5882 if (j < 0)
5883 goto error;
5884 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5885 if (upper) {
5886 isl_int_set_si(bmap->ineq[j][pos], -1);
5887 isl_int_set_si(bmap->ineq[j][0], value);
5888 } else {
5889 isl_int_set_si(bmap->ineq[j][pos], 1);
5890 isl_int_set_si(bmap->ineq[j][0], -value);
5892 bmap = isl_basic_map_simplify(bmap);
5893 return isl_basic_map_finalize(bmap);
5894 error:
5895 isl_basic_map_free(bmap);
5896 return NULL;
5899 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5900 __isl_take isl_basic_map *bmap,
5901 enum isl_dim_type type, unsigned pos, int value)
5903 return basic_map_bound_si(bmap, type, pos, value, 0);
5906 /* Constrain the values of the given dimension to be no greater than "value".
5908 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5909 __isl_take isl_basic_map *bmap,
5910 enum isl_dim_type type, unsigned pos, int value)
5912 return basic_map_bound_si(bmap, type, pos, value, 1);
5915 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5916 unsigned dim, isl_int value)
5918 int j;
5920 bset = isl_basic_set_cow(bset);
5921 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5922 j = isl_basic_set_alloc_inequality(bset);
5923 if (j < 0)
5924 goto error;
5925 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5926 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5927 isl_int_neg(bset->ineq[j][0], value);
5928 bset = isl_basic_set_simplify(bset);
5929 return isl_basic_set_finalize(bset);
5930 error:
5931 isl_basic_set_free(bset);
5932 return NULL;
5935 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5936 enum isl_dim_type type, unsigned pos, int value, int upper)
5938 int i;
5940 map = isl_map_cow(map);
5941 if (!map)
5942 return NULL;
5944 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5945 for (i = 0; i < map->n; ++i) {
5946 map->p[i] = basic_map_bound_si(map->p[i],
5947 type, pos, value, upper);
5948 if (!map->p[i])
5949 goto error;
5951 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5952 return map;
5953 error:
5954 isl_map_free(map);
5955 return NULL;
5958 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5959 enum isl_dim_type type, unsigned pos, int value)
5961 return map_bound_si(map, type, pos, value, 0);
5964 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5965 enum isl_dim_type type, unsigned pos, int value)
5967 return map_bound_si(map, type, pos, value, 1);
5970 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5971 enum isl_dim_type type, unsigned pos, int value)
5973 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
5974 type, pos, value));
5977 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5978 enum isl_dim_type type, unsigned pos, int value)
5980 return isl_map_upper_bound_si(set, type, pos, value);
5983 /* Bound the given variable of "bmap" from below (or above is "upper"
5984 * is set) to "value".
5986 static __isl_give isl_basic_map *basic_map_bound(
5987 __isl_take isl_basic_map *bmap,
5988 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5990 int j;
5992 if (!bmap)
5993 return NULL;
5994 if (pos >= isl_basic_map_dim(bmap, type))
5995 isl_die(bmap->ctx, isl_error_invalid,
5996 "index out of bounds", goto error);
5997 pos += isl_basic_map_offset(bmap, type);
5998 bmap = isl_basic_map_cow(bmap);
5999 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6000 j = isl_basic_map_alloc_inequality(bmap);
6001 if (j < 0)
6002 goto error;
6003 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6004 if (upper) {
6005 isl_int_set_si(bmap->ineq[j][pos], -1);
6006 isl_int_set(bmap->ineq[j][0], value);
6007 } else {
6008 isl_int_set_si(bmap->ineq[j][pos], 1);
6009 isl_int_neg(bmap->ineq[j][0], value);
6011 bmap = isl_basic_map_simplify(bmap);
6012 return isl_basic_map_finalize(bmap);
6013 error:
6014 isl_basic_map_free(bmap);
6015 return NULL;
6018 /* Bound the given variable of "map" from below (or above is "upper"
6019 * is set) to "value".
6021 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6022 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6024 int i;
6026 map = isl_map_cow(map);
6027 if (!map)
6028 return NULL;
6030 if (pos >= isl_map_dim(map, type))
6031 isl_die(map->ctx, isl_error_invalid,
6032 "index out of bounds", goto error);
6033 for (i = map->n - 1; i >= 0; --i) {
6034 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6035 if (remove_if_empty(map, i) < 0)
6036 goto error;
6038 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6039 return map;
6040 error:
6041 isl_map_free(map);
6042 return NULL;
6045 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6046 enum isl_dim_type type, unsigned pos, isl_int value)
6048 return map_bound(map, type, pos, value, 0);
6051 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6052 enum isl_dim_type type, unsigned pos, isl_int value)
6054 return map_bound(map, type, pos, value, 1);
6057 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6058 enum isl_dim_type type, unsigned pos, isl_int value)
6060 return isl_map_lower_bound(set, type, pos, value);
6063 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6064 enum isl_dim_type type, unsigned pos, isl_int value)
6066 return isl_map_upper_bound(set, type, pos, value);
6069 /* Force the values of the variable at position "pos" of type "type" of "set"
6070 * to be no smaller than "value".
6072 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6073 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6075 if (!value)
6076 goto error;
6077 if (!isl_val_is_int(value))
6078 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6079 "expecting integer value", goto error);
6080 set = isl_set_lower_bound(set, type, pos, value->n);
6081 isl_val_free(value);
6082 return set;
6083 error:
6084 isl_val_free(value);
6085 isl_set_free(set);
6086 return NULL;
6089 /* Force the values of the variable at position "pos" of type "type" of "set"
6090 * to be no greater than "value".
6092 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6093 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6095 if (!value)
6096 goto error;
6097 if (!isl_val_is_int(value))
6098 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6099 "expecting integer value", goto error);
6100 set = isl_set_upper_bound(set, type, pos, value->n);
6101 isl_val_free(value);
6102 return set;
6103 error:
6104 isl_val_free(value);
6105 isl_set_free(set);
6106 return NULL;
6109 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6110 isl_int value)
6112 int i;
6114 set = isl_set_cow(set);
6115 if (!set)
6116 return NULL;
6118 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6119 for (i = 0; i < set->n; ++i) {
6120 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6121 if (!set->p[i])
6122 goto error;
6124 return set;
6125 error:
6126 isl_set_free(set);
6127 return NULL;
6130 struct isl_map *isl_map_reverse(struct isl_map *map)
6132 int i;
6134 map = isl_map_cow(map);
6135 if (!map)
6136 return NULL;
6138 map->dim = isl_space_reverse(map->dim);
6139 if (!map->dim)
6140 goto error;
6141 for (i = 0; i < map->n; ++i) {
6142 map->p[i] = isl_basic_map_reverse(map->p[i]);
6143 if (!map->p[i])
6144 goto error;
6146 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6147 return map;
6148 error:
6149 isl_map_free(map);
6150 return NULL;
6153 #undef TYPE
6154 #define TYPE isl_pw_multi_aff
6155 #undef SUFFIX
6156 #define SUFFIX _pw_multi_aff
6157 #undef EMPTY
6158 #define EMPTY isl_pw_multi_aff_empty
6159 #undef ADD
6160 #define ADD isl_pw_multi_aff_union_add
6161 #include "isl_map_lexopt_templ.c"
6163 /* Given a map "map", compute the lexicographically minimal
6164 * (or maximal) image element for each domain element in dom,
6165 * in the form of an isl_pw_multi_aff.
6166 * If "empty" is not NULL, then set *empty to those elements in dom that
6167 * do not have an image element.
6168 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6169 * should be computed over the domain of "map". "empty" is also NULL
6170 * in this case.
6172 * We first compute the lexicographically minimal or maximal element
6173 * in the first basic map. This results in a partial solution "res"
6174 * and a subset "todo" of dom that still need to be handled.
6175 * We then consider each of the remaining maps in "map" and successively
6176 * update both "res" and "todo".
6177 * If "empty" is NULL, then the todo sets are not needed and therefore
6178 * also not computed.
6180 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6181 __isl_take isl_map *map, __isl_take isl_set *dom,
6182 __isl_give isl_set **empty, unsigned flags)
6184 int i;
6185 int full;
6186 isl_pw_multi_aff *res;
6187 isl_set *todo;
6189 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6190 if (!map || (!full && !dom))
6191 goto error;
6193 if (isl_map_plain_is_empty(map)) {
6194 if (empty)
6195 *empty = dom;
6196 else
6197 isl_set_free(dom);
6198 return isl_pw_multi_aff_from_map(map);
6201 res = basic_map_partial_lexopt_pw_multi_aff(
6202 isl_basic_map_copy(map->p[0]),
6203 isl_set_copy(dom), empty, flags);
6205 if (empty)
6206 todo = *empty;
6207 for (i = 1; i < map->n; ++i) {
6208 isl_pw_multi_aff *res_i;
6210 res_i = basic_map_partial_lexopt_pw_multi_aff(
6211 isl_basic_map_copy(map->p[i]),
6212 isl_set_copy(dom), empty, flags);
6214 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6215 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6216 else
6217 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6219 if (empty)
6220 todo = isl_set_intersect(todo, *empty);
6223 isl_set_free(dom);
6224 isl_map_free(map);
6226 if (empty)
6227 *empty = todo;
6229 return res;
6230 error:
6231 if (empty)
6232 *empty = NULL;
6233 isl_set_free(dom);
6234 isl_map_free(map);
6235 return NULL;
6238 #undef TYPE
6239 #define TYPE isl_map
6240 #undef SUFFIX
6241 #define SUFFIX
6242 #undef EMPTY
6243 #define EMPTY isl_map_empty
6244 #undef ADD
6245 #define ADD isl_map_union_disjoint
6246 #include "isl_map_lexopt_templ.c"
6248 /* Given a map "map", compute the lexicographically minimal
6249 * (or maximal) image element for each domain element in "dom",
6250 * in the form of an isl_map.
6251 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6252 * do not have an image element.
6253 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6254 * should be computed over the domain of "map". "empty" is also NULL
6255 * in this case.
6257 * If the input consists of more than one disjunct, then first
6258 * compute the desired result in the form of an isl_pw_multi_aff and
6259 * then convert that into an isl_map.
6261 * This function used to have an explicit implementation in terms
6262 * of isl_maps, but it would continually intersect the domains of
6263 * partial results with the complement of the domain of the next
6264 * partial solution, potentially leading to an explosion in the number
6265 * of disjuncts if there are several disjuncts in the input.
6266 * An even earlier implementation of this function would look for
6267 * better results in the domain of the partial result and for extra
6268 * results in the complement of this domain, which would lead to
6269 * even more splintering.
6271 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6272 __isl_take isl_map *map, __isl_take isl_set *dom,
6273 __isl_give isl_set **empty, unsigned flags)
6275 int full;
6276 struct isl_map *res;
6277 isl_pw_multi_aff *pma;
6279 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6280 if (!map || (!full && !dom))
6281 goto error;
6283 if (isl_map_plain_is_empty(map)) {
6284 if (empty)
6285 *empty = dom;
6286 else
6287 isl_set_free(dom);
6288 return map;
6291 if (map->n == 1) {
6292 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6293 dom, empty, flags);
6294 isl_map_free(map);
6295 return res;
6298 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6299 flags);
6300 return isl_map_from_pw_multi_aff(pma);
6301 error:
6302 if (empty)
6303 *empty = NULL;
6304 isl_set_free(dom);
6305 isl_map_free(map);
6306 return NULL;
6309 __isl_give isl_map *isl_map_partial_lexmax(
6310 __isl_take isl_map *map, __isl_take isl_set *dom,
6311 __isl_give isl_set **empty)
6313 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6316 __isl_give isl_map *isl_map_partial_lexmin(
6317 __isl_take isl_map *map, __isl_take isl_set *dom,
6318 __isl_give isl_set **empty)
6320 return isl_map_partial_lexopt(map, dom, empty, 0);
6323 __isl_give isl_set *isl_set_partial_lexmin(
6324 __isl_take isl_set *set, __isl_take isl_set *dom,
6325 __isl_give isl_set **empty)
6327 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6328 dom, empty));
6331 __isl_give isl_set *isl_set_partial_lexmax(
6332 __isl_take isl_set *set, __isl_take isl_set *dom,
6333 __isl_give isl_set **empty)
6335 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6336 dom, empty));
6339 /* Compute the lexicographic minimum (or maximum if "flags" includes
6340 * ISL_OPT_MAX) of "bset" over its parametric domain.
6342 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6343 unsigned flags)
6345 return isl_basic_map_lexopt(bset, flags);
6348 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6350 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6353 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6355 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6358 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6360 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6363 /* Compute the lexicographic minimum of "bset" over its parametric domain
6364 * for the purpose of quantifier elimination.
6365 * That is, find an explicit representation for all the existentially
6366 * quantified variables in "bset" by computing their lexicographic
6367 * minimum.
6369 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6370 __isl_take isl_basic_set *bset)
6372 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6375 /* Extract the first and only affine expression from list
6376 * and then add it to *pwaff with the given dom.
6377 * This domain is known to be disjoint from other domains
6378 * because of the way isl_basic_map_foreach_lexmax works.
6380 static int update_dim_opt(__isl_take isl_basic_set *dom,
6381 __isl_take isl_aff_list *list, void *user)
6383 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6384 isl_aff *aff;
6385 isl_pw_aff **pwaff = user;
6386 isl_pw_aff *pwaff_i;
6388 if (!list)
6389 goto error;
6390 if (isl_aff_list_n_aff(list) != 1)
6391 isl_die(ctx, isl_error_internal,
6392 "expecting single element list", goto error);
6394 aff = isl_aff_list_get_aff(list, 0);
6395 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6397 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6399 isl_aff_list_free(list);
6401 return 0;
6402 error:
6403 isl_basic_set_free(dom);
6404 isl_aff_list_free(list);
6405 return -1;
6408 /* Given a basic map with one output dimension, compute the minimum or
6409 * maximum of that dimension as an isl_pw_aff.
6411 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6412 * call update_dim_opt on each leaf of the result.
6414 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6415 int max)
6417 isl_space *dim = isl_basic_map_get_space(bmap);
6418 isl_pw_aff *pwaff;
6419 int r;
6421 dim = isl_space_from_domain(isl_space_domain(dim));
6422 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6423 pwaff = isl_pw_aff_empty(dim);
6425 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6426 if (r < 0)
6427 return isl_pw_aff_free(pwaff);
6429 return pwaff;
6432 /* Compute the minimum or maximum of the given output dimension
6433 * as a function of the parameters and the input dimensions,
6434 * but independently of the other output dimensions.
6436 * We first project out the other output dimension and then compute
6437 * the "lexicographic" maximum in each basic map, combining the results
6438 * using isl_pw_aff_union_max.
6440 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6441 int max)
6443 int i;
6444 isl_pw_aff *pwaff;
6445 unsigned n_out;
6447 n_out = isl_map_dim(map, isl_dim_out);
6448 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6449 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6450 if (!map)
6451 return NULL;
6453 if (map->n == 0) {
6454 isl_space *dim = isl_map_get_space(map);
6455 isl_map_free(map);
6456 return isl_pw_aff_empty(dim);
6459 pwaff = basic_map_dim_opt(map->p[0], max);
6460 for (i = 1; i < map->n; ++i) {
6461 isl_pw_aff *pwaff_i;
6463 pwaff_i = basic_map_dim_opt(map->p[i], max);
6464 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6467 isl_map_free(map);
6469 return pwaff;
6472 /* Compute the minimum of the given output dimension as a function of the
6473 * parameters and input dimensions, but independently of
6474 * the other output dimensions.
6476 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6478 return map_dim_opt(map, pos, 0);
6481 /* Compute the maximum of the given output dimension as a function of the
6482 * parameters and input dimensions, but independently of
6483 * the other output dimensions.
6485 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6487 return map_dim_opt(map, pos, 1);
6490 /* Compute the minimum or maximum of the given set dimension
6491 * as a function of the parameters,
6492 * but independently of the other set dimensions.
6494 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6495 int max)
6497 return map_dim_opt(set, pos, max);
6500 /* Compute the maximum of the given set dimension as a function of the
6501 * parameters, but independently of the other set dimensions.
6503 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6505 return set_dim_opt(set, pos, 1);
6508 /* Compute the minimum of the given set dimension as a function of the
6509 * parameters, but independently of the other set dimensions.
6511 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6513 return set_dim_opt(set, pos, 0);
6516 /* Apply a preimage specified by "mat" on the parameters of "bset".
6517 * bset is assumed to have only parameters and divs.
6519 static struct isl_basic_set *basic_set_parameter_preimage(
6520 struct isl_basic_set *bset, struct isl_mat *mat)
6522 unsigned nparam;
6524 if (!bset || !mat)
6525 goto error;
6527 bset->dim = isl_space_cow(bset->dim);
6528 if (!bset->dim)
6529 goto error;
6531 nparam = isl_basic_set_dim(bset, isl_dim_param);
6533 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6535 bset->dim->nparam = 0;
6536 bset->dim->n_out = nparam;
6537 bset = isl_basic_set_preimage(bset, mat);
6538 if (bset) {
6539 bset->dim->nparam = bset->dim->n_out;
6540 bset->dim->n_out = 0;
6542 return bset;
6543 error:
6544 isl_mat_free(mat);
6545 isl_basic_set_free(bset);
6546 return NULL;
6549 /* Apply a preimage specified by "mat" on the parameters of "set".
6550 * set is assumed to have only parameters and divs.
6552 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6553 __isl_take isl_mat *mat)
6555 isl_space *space;
6556 unsigned nparam;
6558 if (!set || !mat)
6559 goto error;
6561 nparam = isl_set_dim(set, isl_dim_param);
6563 if (mat->n_row != 1 + nparam)
6564 isl_die(isl_set_get_ctx(set), isl_error_internal,
6565 "unexpected number of rows", goto error);
6567 space = isl_set_get_space(set);
6568 space = isl_space_move_dims(space, isl_dim_set, 0,
6569 isl_dim_param, 0, nparam);
6570 set = isl_set_reset_space(set, space);
6571 set = isl_set_preimage(set, mat);
6572 nparam = isl_set_dim(set, isl_dim_out);
6573 space = isl_set_get_space(set);
6574 space = isl_space_move_dims(space, isl_dim_param, 0,
6575 isl_dim_out, 0, nparam);
6576 set = isl_set_reset_space(set, space);
6577 return set;
6578 error:
6579 isl_mat_free(mat);
6580 isl_set_free(set);
6581 return NULL;
6584 /* Intersect the basic set "bset" with the affine space specified by the
6585 * equalities in "eq".
6587 static struct isl_basic_set *basic_set_append_equalities(
6588 struct isl_basic_set *bset, struct isl_mat *eq)
6590 int i, k;
6591 unsigned len;
6593 if (!bset || !eq)
6594 goto error;
6596 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6597 eq->n_row, 0);
6598 if (!bset)
6599 goto error;
6601 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6602 for (i = 0; i < eq->n_row; ++i) {
6603 k = isl_basic_set_alloc_equality(bset);
6604 if (k < 0)
6605 goto error;
6606 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6607 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6609 isl_mat_free(eq);
6611 bset = isl_basic_set_gauss(bset, NULL);
6612 bset = isl_basic_set_finalize(bset);
6614 return bset;
6615 error:
6616 isl_mat_free(eq);
6617 isl_basic_set_free(bset);
6618 return NULL;
6621 /* Intersect the set "set" with the affine space specified by the
6622 * equalities in "eq".
6624 static struct isl_set *set_append_equalities(struct isl_set *set,
6625 struct isl_mat *eq)
6627 int i;
6629 if (!set || !eq)
6630 goto error;
6632 for (i = 0; i < set->n; ++i) {
6633 set->p[i] = basic_set_append_equalities(set->p[i],
6634 isl_mat_copy(eq));
6635 if (!set->p[i])
6636 goto error;
6638 isl_mat_free(eq);
6639 return set;
6640 error:
6641 isl_mat_free(eq);
6642 isl_set_free(set);
6643 return NULL;
6646 /* Given a basic set "bset" that only involves parameters and existentially
6647 * quantified variables, return the index of the first equality
6648 * that only involves parameters. If there is no such equality then
6649 * return bset->n_eq.
6651 * This function assumes that isl_basic_set_gauss has been called on "bset".
6653 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6655 int i, j;
6656 unsigned nparam, n_div;
6658 if (!bset)
6659 return -1;
6661 nparam = isl_basic_set_dim(bset, isl_dim_param);
6662 n_div = isl_basic_set_dim(bset, isl_dim_div);
6664 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6665 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6666 ++i;
6669 return i;
6672 /* Compute an explicit representation for the existentially quantified
6673 * variables in "bset" by computing the "minimal value" of the set
6674 * variables. Since there are no set variables, the computation of
6675 * the minimal value essentially computes an explicit representation
6676 * of the non-empty part(s) of "bset".
6678 * The input only involves parameters and existentially quantified variables.
6679 * All equalities among parameters have been removed.
6681 * Since the existentially quantified variables in the result are in general
6682 * going to be different from those in the input, we first replace
6683 * them by the minimal number of variables based on their equalities.
6684 * This should simplify the parametric integer programming.
6686 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6688 isl_morph *morph1, *morph2;
6689 isl_set *set;
6690 unsigned n;
6692 if (!bset)
6693 return NULL;
6694 if (bset->n_eq == 0)
6695 return isl_basic_set_lexmin_compute_divs(bset);
6697 morph1 = isl_basic_set_parameter_compression(bset);
6698 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6699 bset = isl_basic_set_lift(bset);
6700 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6701 bset = isl_morph_basic_set(morph2, bset);
6702 n = isl_basic_set_dim(bset, isl_dim_set);
6703 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6705 set = isl_basic_set_lexmin_compute_divs(bset);
6707 set = isl_morph_set(isl_morph_inverse(morph1), set);
6709 return set;
6712 /* Project the given basic set onto its parameter domain, possibly introducing
6713 * new, explicit, existential variables in the constraints.
6714 * The input has parameters and (possibly implicit) existential variables.
6715 * The output has the same parameters, but only
6716 * explicit existentially quantified variables.
6718 * The actual projection is performed by pip, but pip doesn't seem
6719 * to like equalities very much, so we first remove the equalities
6720 * among the parameters by performing a variable compression on
6721 * the parameters. Afterward, an inverse transformation is performed
6722 * and the equalities among the parameters are inserted back in.
6724 * The variable compression on the parameters may uncover additional
6725 * equalities that were only implicit before. We therefore check
6726 * if there are any new parameter equalities in the result and
6727 * if so recurse. The removal of parameter equalities is required
6728 * for the parameter compression performed by base_compute_divs.
6730 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6732 int i;
6733 struct isl_mat *eq;
6734 struct isl_mat *T, *T2;
6735 struct isl_set *set;
6736 unsigned nparam;
6738 bset = isl_basic_set_cow(bset);
6739 if (!bset)
6740 return NULL;
6742 if (bset->n_eq == 0)
6743 return base_compute_divs(bset);
6745 bset = isl_basic_set_gauss(bset, NULL);
6746 if (!bset)
6747 return NULL;
6748 if (isl_basic_set_plain_is_empty(bset))
6749 return isl_set_from_basic_set(bset);
6751 i = first_parameter_equality(bset);
6752 if (i == bset->n_eq)
6753 return base_compute_divs(bset);
6755 nparam = isl_basic_set_dim(bset, isl_dim_param);
6756 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6757 0, 1 + nparam);
6758 eq = isl_mat_cow(eq);
6759 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6760 if (T && T->n_col == 0) {
6761 isl_mat_free(T);
6762 isl_mat_free(T2);
6763 isl_mat_free(eq);
6764 bset = isl_basic_set_set_to_empty(bset);
6765 return isl_set_from_basic_set(bset);
6767 bset = basic_set_parameter_preimage(bset, T);
6769 i = first_parameter_equality(bset);
6770 if (!bset)
6771 set = NULL;
6772 else if (i == bset->n_eq)
6773 set = base_compute_divs(bset);
6774 else
6775 set = parameter_compute_divs(bset);
6776 set = set_parameter_preimage(set, T2);
6777 set = set_append_equalities(set, eq);
6778 return set;
6781 /* Insert the divs from "ls" before those of "bmap".
6783 * The number of columns is not changed, which means that the last
6784 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6785 * The caller is responsible for removing the same number of dimensions
6786 * from the space of "bmap".
6788 static __isl_give isl_basic_map *insert_divs_from_local_space(
6789 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6791 int i;
6792 int n_div;
6793 int old_n_div;
6795 n_div = isl_local_space_dim(ls, isl_dim_div);
6796 if (n_div == 0)
6797 return bmap;
6799 old_n_div = bmap->n_div;
6800 bmap = insert_div_rows(bmap, n_div);
6801 if (!bmap)
6802 return NULL;
6804 for (i = 0; i < n_div; ++i) {
6805 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6806 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6809 return bmap;
6812 /* Replace the space of "bmap" by the space and divs of "ls".
6814 * If "ls" has any divs, then we simplify the result since we may
6815 * have discovered some additional equalities that could simplify
6816 * the div expressions.
6818 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6819 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6821 int n_div;
6823 bmap = isl_basic_map_cow(bmap);
6824 if (!bmap || !ls)
6825 goto error;
6827 n_div = isl_local_space_dim(ls, isl_dim_div);
6828 bmap = insert_divs_from_local_space(bmap, ls);
6829 if (!bmap)
6830 goto error;
6832 isl_space_free(bmap->dim);
6833 bmap->dim = isl_local_space_get_space(ls);
6834 if (!bmap->dim)
6835 goto error;
6837 isl_local_space_free(ls);
6838 if (n_div > 0)
6839 bmap = isl_basic_map_simplify(bmap);
6840 bmap = isl_basic_map_finalize(bmap);
6841 return bmap;
6842 error:
6843 isl_basic_map_free(bmap);
6844 isl_local_space_free(ls);
6845 return NULL;
6848 /* Replace the space of "map" by the space and divs of "ls".
6850 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6851 __isl_take isl_local_space *ls)
6853 int i;
6855 map = isl_map_cow(map);
6856 if (!map || !ls)
6857 goto error;
6859 for (i = 0; i < map->n; ++i) {
6860 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6861 isl_local_space_copy(ls));
6862 if (!map->p[i])
6863 goto error;
6865 isl_space_free(map->dim);
6866 map->dim = isl_local_space_get_space(ls);
6867 if (!map->dim)
6868 goto error;
6870 isl_local_space_free(ls);
6871 return map;
6872 error:
6873 isl_local_space_free(ls);
6874 isl_map_free(map);
6875 return NULL;
6878 /* Compute an explicit representation for the existentially
6879 * quantified variables for which do not know any explicit representation yet.
6881 * We first sort the existentially quantified variables so that the
6882 * existentially quantified variables for which we already have an explicit
6883 * representation are placed before those for which we do not.
6884 * The input dimensions, the output dimensions and the existentially
6885 * quantified variables for which we already have an explicit
6886 * representation are then turned into parameters.
6887 * compute_divs returns a map with the same parameters and
6888 * no input or output dimensions and the dimension specification
6889 * is reset to that of the input, including the existentially quantified
6890 * variables for which we already had an explicit representation.
6892 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6894 struct isl_basic_set *bset;
6895 struct isl_set *set;
6896 struct isl_map *map;
6897 isl_space *dim;
6898 isl_local_space *ls;
6899 unsigned nparam;
6900 unsigned n_in;
6901 unsigned n_out;
6902 int n_known;
6903 int i;
6905 bmap = isl_basic_map_sort_divs(bmap);
6906 bmap = isl_basic_map_cow(bmap);
6907 if (!bmap)
6908 return NULL;
6910 n_known = isl_basic_map_first_unknown_div(bmap);
6911 if (n_known < 0)
6912 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6914 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6915 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6916 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6917 dim = isl_space_set_alloc(bmap->ctx,
6918 nparam + n_in + n_out + n_known, 0);
6919 if (!dim)
6920 goto error;
6922 ls = isl_basic_map_get_local_space(bmap);
6923 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6924 n_known, bmap->n_div - n_known);
6925 if (n_known > 0) {
6926 for (i = n_known; i < bmap->n_div; ++i)
6927 swap_div(bmap, i - n_known, i);
6928 bmap->n_div -= n_known;
6929 bmap->extra -= n_known;
6931 bmap = isl_basic_map_reset_space(bmap, dim);
6932 bset = bset_from_bmap(bmap);
6934 set = parameter_compute_divs(bset);
6935 map = set_to_map(set);
6936 map = replace_space_by_local_space(map, ls);
6938 return map;
6939 error:
6940 isl_basic_map_free(bmap);
6941 return NULL;
6944 /* Remove the explicit representation of local variable "div",
6945 * if there is any.
6947 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6948 __isl_take isl_basic_map *bmap, int div)
6950 isl_bool unknown;
6952 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
6953 if (unknown < 0)
6954 return isl_basic_map_free(bmap);
6955 if (unknown)
6956 return bmap;
6958 bmap = isl_basic_map_cow(bmap);
6959 if (!bmap)
6960 return NULL;
6961 isl_int_set_si(bmap->div[div][0], 0);
6962 return bmap;
6965 /* Is local variable "div" of "bmap" marked as not having an explicit
6966 * representation?
6967 * Note that even if "div" is not marked in this way and therefore
6968 * has an explicit representation, this representation may still
6969 * depend (indirectly) on other local variables that do not
6970 * have an explicit representation.
6972 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
6973 int div)
6975 if (!bmap)
6976 return isl_bool_error;
6977 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6978 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6979 "position out of bounds", return isl_bool_error);
6980 return isl_int_is_zero(bmap->div[div][0]);
6983 /* Return the position of the first local variable that does not
6984 * have an explicit representation.
6985 * Return the total number of local variables if they all have
6986 * an explicit representation.
6987 * Return -1 on error.
6989 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
6991 int i;
6993 if (!bmap)
6994 return -1;
6996 for (i = 0; i < bmap->n_div; ++i) {
6997 if (!isl_basic_map_div_is_known(bmap, i))
6998 return i;
7000 return bmap->n_div;
7003 /* Return the position of the first local variable that does not
7004 * have an explicit representation.
7005 * Return the total number of local variables if they all have
7006 * an explicit representation.
7007 * Return -1 on error.
7009 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7011 return isl_basic_map_first_unknown_div(bset);
7014 /* Does "bmap" have an explicit representation for all local variables?
7016 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7018 int first, n;
7020 n = isl_basic_map_dim(bmap, isl_dim_div);
7021 first = isl_basic_map_first_unknown_div(bmap);
7022 if (first < 0)
7023 return isl_bool_error;
7024 return first == n;
7027 /* Do all basic maps in "map" have an explicit representation
7028 * for all local variables?
7030 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7032 int i;
7034 if (!map)
7035 return isl_bool_error;
7037 for (i = 0; i < map->n; ++i) {
7038 int known = isl_basic_map_divs_known(map->p[i]);
7039 if (known <= 0)
7040 return known;
7043 return isl_bool_true;
7046 /* If bmap contains any unknown divs, then compute explicit
7047 * expressions for them. However, this computation may be
7048 * quite expensive, so first try to remove divs that aren't
7049 * strictly needed.
7051 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7053 int known;
7054 struct isl_map *map;
7056 known = isl_basic_map_divs_known(bmap);
7057 if (known < 0)
7058 goto error;
7059 if (known)
7060 return isl_map_from_basic_map(bmap);
7062 bmap = isl_basic_map_drop_redundant_divs(bmap);
7064 known = isl_basic_map_divs_known(bmap);
7065 if (known < 0)
7066 goto error;
7067 if (known)
7068 return isl_map_from_basic_map(bmap);
7070 map = compute_divs(bmap);
7071 return map;
7072 error:
7073 isl_basic_map_free(bmap);
7074 return NULL;
7077 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7079 int i;
7080 int known;
7081 struct isl_map *res;
7083 if (!map)
7084 return NULL;
7085 if (map->n == 0)
7086 return map;
7088 known = isl_map_divs_known(map);
7089 if (known < 0) {
7090 isl_map_free(map);
7091 return NULL;
7093 if (known)
7094 return map;
7096 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7097 for (i = 1 ; i < map->n; ++i) {
7098 struct isl_map *r2;
7099 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7100 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7101 res = isl_map_union_disjoint(res, r2);
7102 else
7103 res = isl_map_union(res, r2);
7105 isl_map_free(map);
7107 return res;
7110 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7112 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7115 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7117 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7120 struct isl_set *isl_map_domain(struct isl_map *map)
7122 int i;
7123 struct isl_set *set;
7125 if (!map)
7126 goto error;
7128 map = isl_map_cow(map);
7129 if (!map)
7130 return NULL;
7132 set = set_from_map(map);
7133 set->dim = isl_space_domain(set->dim);
7134 if (!set->dim)
7135 goto error;
7136 for (i = 0; i < map->n; ++i) {
7137 set->p[i] = isl_basic_map_domain(map->p[i]);
7138 if (!set->p[i])
7139 goto error;
7141 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7142 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7143 return set;
7144 error:
7145 isl_map_free(map);
7146 return NULL;
7149 /* Return the union of "map1" and "map2", where we assume for now that
7150 * "map1" and "map2" are disjoint. Note that the basic maps inside
7151 * "map1" or "map2" may not be disjoint from each other.
7152 * Also note that this function is also called from isl_map_union,
7153 * which takes care of handling the situation where "map1" and "map2"
7154 * may not be disjoint.
7156 * If one of the inputs is empty, we can simply return the other input.
7157 * Similarly, if one of the inputs is universal, then it is equal to the union.
7159 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7160 __isl_take isl_map *map2)
7162 int i;
7163 unsigned flags = 0;
7164 struct isl_map *map = NULL;
7165 int is_universe;
7167 if (!map1 || !map2)
7168 goto error;
7170 if (!isl_space_is_equal(map1->dim, map2->dim))
7171 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7172 "spaces don't match", goto error);
7174 if (map1->n == 0) {
7175 isl_map_free(map1);
7176 return map2;
7178 if (map2->n == 0) {
7179 isl_map_free(map2);
7180 return map1;
7183 is_universe = isl_map_plain_is_universe(map1);
7184 if (is_universe < 0)
7185 goto error;
7186 if (is_universe) {
7187 isl_map_free(map2);
7188 return map1;
7191 is_universe = isl_map_plain_is_universe(map2);
7192 if (is_universe < 0)
7193 goto error;
7194 if (is_universe) {
7195 isl_map_free(map1);
7196 return map2;
7199 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7200 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7201 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7203 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7204 map1->n + map2->n, flags);
7205 if (!map)
7206 goto error;
7207 for (i = 0; i < map1->n; ++i) {
7208 map = isl_map_add_basic_map(map,
7209 isl_basic_map_copy(map1->p[i]));
7210 if (!map)
7211 goto error;
7213 for (i = 0; i < map2->n; ++i) {
7214 map = isl_map_add_basic_map(map,
7215 isl_basic_map_copy(map2->p[i]));
7216 if (!map)
7217 goto error;
7219 isl_map_free(map1);
7220 isl_map_free(map2);
7221 return map;
7222 error:
7223 isl_map_free(map);
7224 isl_map_free(map1);
7225 isl_map_free(map2);
7226 return NULL;
7229 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7230 * guaranteed to be disjoint by the caller.
7232 * Note that this functions is called from within isl_map_make_disjoint,
7233 * so we have to be careful not to touch the constraints of the inputs
7234 * in any way.
7236 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7237 __isl_take isl_map *map2)
7239 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7242 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7243 * not be disjoint. The parameters are assumed to have been aligned.
7245 * We currently simply call map_union_disjoint, the internal operation
7246 * of which does not really depend on the inputs being disjoint.
7247 * If the result contains more than one basic map, then we clear
7248 * the disjoint flag since the result may contain basic maps from
7249 * both inputs and these are not guaranteed to be disjoint.
7251 * As a special case, if "map1" and "map2" are obviously equal,
7252 * then we simply return "map1".
7254 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7255 __isl_take isl_map *map2)
7257 int equal;
7259 if (!map1 || !map2)
7260 goto error;
7262 equal = isl_map_plain_is_equal(map1, map2);
7263 if (equal < 0)
7264 goto error;
7265 if (equal) {
7266 isl_map_free(map2);
7267 return map1;
7270 map1 = map_union_disjoint(map1, map2);
7271 if (!map1)
7272 return NULL;
7273 if (map1->n > 1)
7274 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7275 return map1;
7276 error:
7277 isl_map_free(map1);
7278 isl_map_free(map2);
7279 return NULL;
7282 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7283 * not be disjoint.
7285 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7286 __isl_take isl_map *map2)
7288 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7291 struct isl_set *isl_set_union_disjoint(
7292 struct isl_set *set1, struct isl_set *set2)
7294 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7295 set_to_map(set2)));
7298 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7300 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7303 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7304 * the results.
7306 * "map" and "set" are assumed to be compatible and non-NULL.
7308 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7309 __isl_take isl_set *set,
7310 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7311 __isl_take isl_basic_set *bset))
7313 unsigned flags = 0;
7314 struct isl_map *result;
7315 int i, j;
7317 if (isl_set_plain_is_universe(set)) {
7318 isl_set_free(set);
7319 return map;
7322 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7323 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7324 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7326 result = isl_map_alloc_space(isl_space_copy(map->dim),
7327 map->n * set->n, flags);
7328 for (i = 0; result && i < map->n; ++i)
7329 for (j = 0; j < set->n; ++j) {
7330 result = isl_map_add_basic_map(result,
7331 fn(isl_basic_map_copy(map->p[i]),
7332 isl_basic_set_copy(set->p[j])));
7333 if (!result)
7334 break;
7337 isl_map_free(map);
7338 isl_set_free(set);
7339 return result;
7342 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7343 __isl_take isl_set *set)
7345 if (!map || !set)
7346 goto error;
7348 if (!isl_map_compatible_range(map, set))
7349 isl_die(set->ctx, isl_error_invalid,
7350 "incompatible spaces", goto error);
7352 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7353 error:
7354 isl_map_free(map);
7355 isl_set_free(set);
7356 return NULL;
7359 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7360 __isl_take isl_set *set)
7362 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7365 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7366 __isl_take isl_set *set)
7368 if (!map || !set)
7369 goto error;
7371 if (!isl_map_compatible_domain(map, set))
7372 isl_die(set->ctx, isl_error_invalid,
7373 "incompatible spaces", goto error);
7375 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7376 error:
7377 isl_map_free(map);
7378 isl_set_free(set);
7379 return NULL;
7382 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7383 __isl_take isl_set *set)
7385 return isl_map_align_params_map_map_and(map, set,
7386 &map_intersect_domain);
7389 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7390 __isl_take isl_map *map2)
7392 if (!map1 || !map2)
7393 goto error;
7394 map1 = isl_map_reverse(map1);
7395 map1 = isl_map_apply_range(map1, map2);
7396 return isl_map_reverse(map1);
7397 error:
7398 isl_map_free(map1);
7399 isl_map_free(map2);
7400 return NULL;
7403 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7404 __isl_take isl_map *map2)
7406 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7409 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7410 __isl_take isl_map *map2)
7412 isl_space *dim_result;
7413 struct isl_map *result;
7414 int i, j;
7416 if (!map1 || !map2)
7417 goto error;
7419 dim_result = isl_space_join(isl_space_copy(map1->dim),
7420 isl_space_copy(map2->dim));
7422 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7423 if (!result)
7424 goto error;
7425 for (i = 0; i < map1->n; ++i)
7426 for (j = 0; j < map2->n; ++j) {
7427 result = isl_map_add_basic_map(result,
7428 isl_basic_map_apply_range(
7429 isl_basic_map_copy(map1->p[i]),
7430 isl_basic_map_copy(map2->p[j])));
7431 if (!result)
7432 goto error;
7434 isl_map_free(map1);
7435 isl_map_free(map2);
7436 if (result && result->n <= 1)
7437 ISL_F_SET(result, ISL_MAP_DISJOINT);
7438 return result;
7439 error:
7440 isl_map_free(map1);
7441 isl_map_free(map2);
7442 return NULL;
7445 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7446 __isl_take isl_map *map2)
7448 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7452 * returns range - domain
7454 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7456 isl_space *target_space;
7457 struct isl_basic_set *bset;
7458 unsigned dim;
7459 unsigned nparam;
7460 int i;
7462 if (!bmap)
7463 goto error;
7464 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7465 bmap->dim, isl_dim_out),
7466 goto error);
7467 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7468 dim = isl_basic_map_n_in(bmap);
7469 nparam = isl_basic_map_n_param(bmap);
7470 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7471 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7472 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7473 for (i = 0; i < dim; ++i) {
7474 int j = isl_basic_map_alloc_equality(bmap);
7475 if (j < 0) {
7476 bmap = isl_basic_map_free(bmap);
7477 break;
7479 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7480 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7481 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7482 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7484 bset = isl_basic_map_domain(bmap);
7485 bset = isl_basic_set_reset_space(bset, target_space);
7486 return bset;
7487 error:
7488 isl_basic_map_free(bmap);
7489 return NULL;
7493 * returns range - domain
7495 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7497 int i;
7498 isl_space *dim;
7499 struct isl_set *result;
7501 if (!map)
7502 return NULL;
7504 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7505 map->dim, isl_dim_out),
7506 goto error);
7507 dim = isl_map_get_space(map);
7508 dim = isl_space_domain(dim);
7509 result = isl_set_alloc_space(dim, map->n, 0);
7510 if (!result)
7511 goto error;
7512 for (i = 0; i < map->n; ++i)
7513 result = isl_set_add_basic_set(result,
7514 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7515 isl_map_free(map);
7516 return result;
7517 error:
7518 isl_map_free(map);
7519 return NULL;
7523 * returns [domain -> range] -> range - domain
7525 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7526 __isl_take isl_basic_map *bmap)
7528 int i, k;
7529 isl_space *dim;
7530 isl_basic_map *domain;
7531 int nparam, n;
7532 unsigned total;
7534 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7535 bmap->dim, isl_dim_out))
7536 isl_die(bmap->ctx, isl_error_invalid,
7537 "domain and range don't match", goto error);
7539 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7540 n = isl_basic_map_dim(bmap, isl_dim_in);
7542 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7543 domain = isl_basic_map_universe(dim);
7545 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7546 bmap = isl_basic_map_apply_range(bmap, domain);
7547 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7549 total = isl_basic_map_total_dim(bmap);
7551 for (i = 0; i < n; ++i) {
7552 k = isl_basic_map_alloc_equality(bmap);
7553 if (k < 0)
7554 goto error;
7555 isl_seq_clr(bmap->eq[k], 1 + total);
7556 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7557 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7558 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7561 bmap = isl_basic_map_gauss(bmap, NULL);
7562 return isl_basic_map_finalize(bmap);
7563 error:
7564 isl_basic_map_free(bmap);
7565 return NULL;
7569 * returns [domain -> range] -> range - domain
7571 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7573 int i;
7574 isl_space *domain_dim;
7576 if (!map)
7577 return NULL;
7579 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7580 map->dim, isl_dim_out))
7581 isl_die(map->ctx, isl_error_invalid,
7582 "domain and range don't match", goto error);
7584 map = isl_map_cow(map);
7585 if (!map)
7586 return NULL;
7588 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7589 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7590 map->dim = isl_space_join(map->dim, domain_dim);
7591 if (!map->dim)
7592 goto error;
7593 for (i = 0; i < map->n; ++i) {
7594 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7595 if (!map->p[i])
7596 goto error;
7598 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7599 return map;
7600 error:
7601 isl_map_free(map);
7602 return NULL;
7605 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7607 struct isl_basic_map *bmap;
7608 unsigned nparam;
7609 unsigned dim;
7610 int i;
7612 if (!dims)
7613 return NULL;
7615 nparam = dims->nparam;
7616 dim = dims->n_out;
7617 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7618 if (!bmap)
7619 goto error;
7621 for (i = 0; i < dim; ++i) {
7622 int j = isl_basic_map_alloc_equality(bmap);
7623 if (j < 0)
7624 goto error;
7625 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7626 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7627 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7629 return isl_basic_map_finalize(bmap);
7630 error:
7631 isl_basic_map_free(bmap);
7632 return NULL;
7635 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7637 if (!dim)
7638 return NULL;
7639 if (dim->n_in != dim->n_out)
7640 isl_die(dim->ctx, isl_error_invalid,
7641 "number of input and output dimensions needs to be "
7642 "the same", goto error);
7643 return basic_map_identity(dim);
7644 error:
7645 isl_space_free(dim);
7646 return NULL;
7649 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7651 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7654 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7656 isl_space *dim = isl_set_get_space(set);
7657 isl_map *id;
7658 id = isl_map_identity(isl_space_map_from_set(dim));
7659 return isl_map_intersect_range(id, set);
7662 /* Construct a basic set with all set dimensions having only non-negative
7663 * values.
7665 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7666 __isl_take isl_space *space)
7668 int i;
7669 unsigned nparam;
7670 unsigned dim;
7671 struct isl_basic_set *bset;
7673 if (!space)
7674 return NULL;
7675 nparam = space->nparam;
7676 dim = space->n_out;
7677 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7678 if (!bset)
7679 return NULL;
7680 for (i = 0; i < dim; ++i) {
7681 int k = isl_basic_set_alloc_inequality(bset);
7682 if (k < 0)
7683 goto error;
7684 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7685 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7687 return bset;
7688 error:
7689 isl_basic_set_free(bset);
7690 return NULL;
7693 /* Construct the half-space x_pos >= 0.
7695 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7696 int pos)
7698 int k;
7699 isl_basic_set *nonneg;
7701 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7702 k = isl_basic_set_alloc_inequality(nonneg);
7703 if (k < 0)
7704 goto error;
7705 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7706 isl_int_set_si(nonneg->ineq[k][pos], 1);
7708 return isl_basic_set_finalize(nonneg);
7709 error:
7710 isl_basic_set_free(nonneg);
7711 return NULL;
7714 /* Construct the half-space x_pos <= -1.
7716 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7718 int k;
7719 isl_basic_set *neg;
7721 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7722 k = isl_basic_set_alloc_inequality(neg);
7723 if (k < 0)
7724 goto error;
7725 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7726 isl_int_set_si(neg->ineq[k][0], -1);
7727 isl_int_set_si(neg->ineq[k][pos], -1);
7729 return isl_basic_set_finalize(neg);
7730 error:
7731 isl_basic_set_free(neg);
7732 return NULL;
7735 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7736 enum isl_dim_type type, unsigned first, unsigned n)
7738 int i;
7739 unsigned offset;
7740 isl_basic_set *nonneg;
7741 isl_basic_set *neg;
7743 if (!set)
7744 return NULL;
7745 if (n == 0)
7746 return set;
7748 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7750 offset = pos(set->dim, type);
7751 for (i = 0; i < n; ++i) {
7752 nonneg = nonneg_halfspace(isl_set_get_space(set),
7753 offset + first + i);
7754 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7756 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7759 return set;
7760 error:
7761 isl_set_free(set);
7762 return NULL;
7765 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7766 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7767 void *user)
7769 isl_set *half;
7771 if (!set)
7772 return -1;
7773 if (isl_set_plain_is_empty(set)) {
7774 isl_set_free(set);
7775 return 0;
7777 if (first == len)
7778 return fn(set, signs, user);
7780 signs[first] = 1;
7781 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7782 1 + first));
7783 half = isl_set_intersect(half, isl_set_copy(set));
7784 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7785 goto error;
7787 signs[first] = -1;
7788 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7789 1 + first));
7790 half = isl_set_intersect(half, set);
7791 return foreach_orthant(half, signs, first + 1, len, fn, user);
7792 error:
7793 isl_set_free(set);
7794 return -1;
7797 /* Call "fn" on the intersections of "set" with each of the orthants
7798 * (except for obviously empty intersections). The orthant is identified
7799 * by the signs array, with each entry having value 1 or -1 according
7800 * to the sign of the corresponding variable.
7802 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7803 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7804 void *user)
7806 unsigned nparam;
7807 unsigned nvar;
7808 int *signs;
7809 int r;
7811 if (!set)
7812 return -1;
7813 if (isl_set_plain_is_empty(set))
7814 return 0;
7816 nparam = isl_set_dim(set, isl_dim_param);
7817 nvar = isl_set_dim(set, isl_dim_set);
7819 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7821 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7822 fn, user);
7824 free(signs);
7826 return r;
7829 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7831 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
7834 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7835 __isl_keep isl_basic_map *bmap2)
7837 int is_subset;
7838 struct isl_map *map1;
7839 struct isl_map *map2;
7841 if (!bmap1 || !bmap2)
7842 return isl_bool_error;
7844 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7845 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7847 is_subset = isl_map_is_subset(map1, map2);
7849 isl_map_free(map1);
7850 isl_map_free(map2);
7852 return is_subset;
7855 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7856 __isl_keep isl_basic_set *bset2)
7858 return isl_basic_map_is_subset(bset1, bset2);
7861 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7862 __isl_keep isl_basic_map *bmap2)
7864 isl_bool is_subset;
7866 if (!bmap1 || !bmap2)
7867 return isl_bool_error;
7868 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7869 if (is_subset != isl_bool_true)
7870 return is_subset;
7871 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7872 return is_subset;
7875 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7876 __isl_keep isl_basic_set *bset2)
7878 return isl_basic_map_is_equal(
7879 bset_to_bmap(bset1), bset_to_bmap(bset2));
7882 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7884 int i;
7885 int is_empty;
7887 if (!map)
7888 return isl_bool_error;
7889 for (i = 0; i < map->n; ++i) {
7890 is_empty = isl_basic_map_is_empty(map->p[i]);
7891 if (is_empty < 0)
7892 return isl_bool_error;
7893 if (!is_empty)
7894 return isl_bool_false;
7896 return isl_bool_true;
7899 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7901 return map ? map->n == 0 : isl_bool_error;
7904 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7906 return set ? set->n == 0 : isl_bool_error;
7909 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7911 return isl_map_is_empty(set_to_map(set));
7914 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7916 if (!map1 || !map2)
7917 return -1;
7919 return isl_space_is_equal(map1->dim, map2->dim);
7922 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7924 if (!set1 || !set2)
7925 return -1;
7927 return isl_space_is_equal(set1->dim, set2->dim);
7930 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7932 isl_bool is_subset;
7934 if (!map1 || !map2)
7935 return isl_bool_error;
7936 is_subset = isl_map_is_subset(map1, map2);
7937 if (is_subset != isl_bool_true)
7938 return is_subset;
7939 is_subset = isl_map_is_subset(map2, map1);
7940 return is_subset;
7943 /* Is "map1" equal to "map2"?
7945 * First check if they are obviously equal.
7946 * If not, then perform a more detailed analysis.
7948 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7950 isl_bool equal;
7952 equal = isl_map_plain_is_equal(map1, map2);
7953 if (equal < 0 || equal)
7954 return equal;
7955 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7958 isl_bool isl_basic_map_is_strict_subset(
7959 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7961 isl_bool is_subset;
7963 if (!bmap1 || !bmap2)
7964 return isl_bool_error;
7965 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7966 if (is_subset != isl_bool_true)
7967 return is_subset;
7968 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7969 if (is_subset == isl_bool_error)
7970 return is_subset;
7971 return !is_subset;
7974 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7975 __isl_keep isl_map *map2)
7977 isl_bool is_subset;
7979 if (!map1 || !map2)
7980 return isl_bool_error;
7981 is_subset = isl_map_is_subset(map1, map2);
7982 if (is_subset != isl_bool_true)
7983 return is_subset;
7984 is_subset = isl_map_is_subset(map2, map1);
7985 if (is_subset == isl_bool_error)
7986 return is_subset;
7987 return !is_subset;
7990 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
7991 __isl_keep isl_set *set2)
7993 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
7996 /* Is "bmap" obviously equal to the universe with the same space?
7998 * That is, does it not have any constraints?
8000 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8002 if (!bmap)
8003 return isl_bool_error;
8004 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8007 /* Is "bset" obviously equal to the universe with the same space?
8009 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8011 return isl_basic_map_plain_is_universe(bset);
8014 /* If "c" does not involve any existentially quantified variables,
8015 * then set *univ to false and abort
8017 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8019 isl_bool *univ = user;
8020 unsigned n;
8022 n = isl_constraint_dim(c, isl_dim_div);
8023 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8024 isl_constraint_free(c);
8025 if (*univ < 0 || !*univ)
8026 return isl_stat_error;
8027 return isl_stat_ok;
8030 /* Is "bmap" equal to the universe with the same space?
8032 * First check if it is obviously equal to the universe.
8033 * If not and if there are any constraints not involving
8034 * existentially quantified variables, then it is certainly
8035 * not equal to the universe.
8036 * Otherwise, check if the universe is a subset of "bmap".
8038 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8040 isl_bool univ;
8041 isl_basic_map *test;
8043 univ = isl_basic_map_plain_is_universe(bmap);
8044 if (univ < 0 || univ)
8045 return univ;
8046 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8047 return isl_bool_false;
8048 univ = isl_bool_true;
8049 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8050 univ)
8051 return isl_bool_error;
8052 if (univ < 0 || !univ)
8053 return univ;
8054 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8055 univ = isl_basic_map_is_subset(test, bmap);
8056 isl_basic_map_free(test);
8057 return univ;
8060 /* Is "bset" equal to the universe with the same space?
8062 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8064 return isl_basic_map_is_universe(bset);
8067 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8069 int i;
8071 if (!map)
8072 return isl_bool_error;
8074 for (i = 0; i < map->n; ++i) {
8075 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8076 if (r < 0 || r)
8077 return r;
8080 return isl_bool_false;
8083 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8085 return isl_map_plain_is_universe(set_to_map(set));
8088 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8090 struct isl_basic_set *bset = NULL;
8091 struct isl_vec *sample = NULL;
8092 isl_bool empty, non_empty;
8094 if (!bmap)
8095 return isl_bool_error;
8097 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8098 return isl_bool_true;
8100 if (isl_basic_map_plain_is_universe(bmap))
8101 return isl_bool_false;
8103 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8104 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8105 copy = isl_basic_map_remove_redundancies(copy);
8106 empty = isl_basic_map_plain_is_empty(copy);
8107 isl_basic_map_free(copy);
8108 return empty;
8111 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8112 if (non_empty < 0)
8113 return isl_bool_error;
8114 if (non_empty)
8115 return isl_bool_false;
8116 isl_vec_free(bmap->sample);
8117 bmap->sample = NULL;
8118 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8119 if (!bset)
8120 return isl_bool_error;
8121 sample = isl_basic_set_sample_vec(bset);
8122 if (!sample)
8123 return isl_bool_error;
8124 empty = sample->size == 0;
8125 isl_vec_free(bmap->sample);
8126 bmap->sample = sample;
8127 if (empty)
8128 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8130 return empty;
8133 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8135 if (!bmap)
8136 return isl_bool_error;
8137 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8140 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8142 if (!bset)
8143 return isl_bool_error;
8144 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8147 /* Is "bmap" known to be non-empty?
8149 * That is, is the cached sample still valid?
8151 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8153 unsigned total;
8155 if (!bmap)
8156 return isl_bool_error;
8157 if (!bmap->sample)
8158 return isl_bool_false;
8159 total = 1 + isl_basic_map_total_dim(bmap);
8160 if (bmap->sample->size != total)
8161 return isl_bool_false;
8162 return isl_basic_map_contains(bmap, bmap->sample);
8165 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8167 return isl_basic_map_is_empty(bset_to_bmap(bset));
8170 struct isl_map *isl_basic_map_union(
8171 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8173 struct isl_map *map;
8174 if (!bmap1 || !bmap2)
8175 goto error;
8177 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8179 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8180 if (!map)
8181 goto error;
8182 map = isl_map_add_basic_map(map, bmap1);
8183 map = isl_map_add_basic_map(map, bmap2);
8184 return map;
8185 error:
8186 isl_basic_map_free(bmap1);
8187 isl_basic_map_free(bmap2);
8188 return NULL;
8191 struct isl_set *isl_basic_set_union(
8192 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8194 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8195 bset_to_bmap(bset2)));
8198 /* Order divs such that any div only depends on previous divs */
8199 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8201 int i;
8202 unsigned off;
8204 if (!bmap)
8205 return NULL;
8207 off = isl_space_dim(bmap->dim, isl_dim_all);
8209 for (i = 0; i < bmap->n_div; ++i) {
8210 int pos;
8211 if (isl_int_is_zero(bmap->div[i][0]))
8212 continue;
8213 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8214 bmap->n_div-i);
8215 if (pos == -1)
8216 continue;
8217 if (pos == 0)
8218 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8219 "integer division depends on itself",
8220 return isl_basic_map_free(bmap));
8221 isl_basic_map_swap_div(bmap, i, i + pos);
8222 --i;
8224 return bmap;
8227 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8229 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8232 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8234 int i;
8236 if (!map)
8237 return 0;
8239 for (i = 0; i < map->n; ++i) {
8240 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8241 if (!map->p[i])
8242 goto error;
8245 return map;
8246 error:
8247 isl_map_free(map);
8248 return NULL;
8251 /* Sort the local variables of "bset".
8253 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8254 __isl_take isl_basic_set *bset)
8256 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8259 /* Apply the expansion computed by isl_merge_divs.
8260 * The expansion itself is given by "exp" while the resulting
8261 * list of divs is given by "div".
8263 * Move the integer divisions of "bmap" into the right position
8264 * according to "exp" and then introduce the additional integer
8265 * divisions, adding div constraints.
8266 * The moving should be done first to avoid moving coefficients
8267 * in the definitions of the extra integer divisions.
8269 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8270 __isl_take isl_basic_set *bmap, __isl_take isl_mat *div, int *exp)
8272 int i, j;
8273 int n_div;
8275 bmap = isl_basic_map_cow(bmap);
8276 if (!bmap || !div)
8277 goto error;
8279 if (div->n_row < bmap->n_div)
8280 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8281 "not an expansion", goto error);
8283 n_div = bmap->n_div;
8284 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8285 div->n_row - n_div, 0,
8286 2 * (div->n_row - n_div));
8288 for (i = n_div; i < div->n_row; ++i)
8289 if (isl_basic_map_alloc_div(bmap) < 0)
8290 goto error;
8292 for (j = n_div - 1; j >= 0; --j) {
8293 if (exp[j] == j)
8294 break;
8295 isl_basic_map_swap_div(bmap, j, exp[j]);
8297 j = 0;
8298 for (i = 0; i < div->n_row; ++i) {
8299 if (j < n_div && exp[j] == i) {
8300 j++;
8301 } else {
8302 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8303 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8304 continue;
8305 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8306 goto error;
8310 isl_mat_free(div);
8311 return bmap;
8312 error:
8313 isl_basic_map_free(bmap);
8314 isl_mat_free(div);
8315 return NULL;
8318 /* Apply the expansion computed by isl_merge_divs.
8319 * The expansion itself is given by "exp" while the resulting
8320 * list of divs is given by "div".
8322 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8323 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8325 return isl_basic_map_expand_divs(bset, div, exp);
8328 /* Look for a div in dst that corresponds to the div "div" in src.
8329 * The divs before "div" in src and dst are assumed to be the same.
8331 * Returns -1 if no corresponding div was found and the position
8332 * of the corresponding div in dst otherwise.
8334 static int find_div(struct isl_basic_map *dst,
8335 struct isl_basic_map *src, unsigned div)
8337 int i;
8339 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8341 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8342 for (i = div; i < dst->n_div; ++i)
8343 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8344 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8345 dst->n_div - div) == -1)
8346 return i;
8347 return -1;
8350 /* Align the divs of "dst" to those of "src", adding divs from "src"
8351 * if needed. That is, make sure that the first src->n_div divs
8352 * of the result are equal to those of src.
8354 * The result is not finalized as by design it will have redundant
8355 * divs if any divs from "src" were copied.
8357 __isl_give isl_basic_map *isl_basic_map_align_divs(
8358 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8360 int i;
8361 int known, extended;
8362 unsigned total;
8364 if (!dst || !src)
8365 return isl_basic_map_free(dst);
8367 if (src->n_div == 0)
8368 return dst;
8370 known = isl_basic_map_divs_known(src);
8371 if (known < 0)
8372 return isl_basic_map_free(dst);
8373 if (!known)
8374 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8375 "some src divs are unknown",
8376 return isl_basic_map_free(dst));
8378 src = isl_basic_map_order_divs(src);
8380 extended = 0;
8381 total = isl_space_dim(src->dim, isl_dim_all);
8382 for (i = 0; i < src->n_div; ++i) {
8383 int j = find_div(dst, src, i);
8384 if (j < 0) {
8385 if (!extended) {
8386 int extra = src->n_div - i;
8387 dst = isl_basic_map_cow(dst);
8388 if (!dst)
8389 return NULL;
8390 dst = isl_basic_map_extend_space(dst,
8391 isl_space_copy(dst->dim),
8392 extra, 0, 2 * extra);
8393 extended = 1;
8395 j = isl_basic_map_alloc_div(dst);
8396 if (j < 0)
8397 return isl_basic_map_free(dst);
8398 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8399 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8400 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8401 return isl_basic_map_free(dst);
8403 if (j != i)
8404 isl_basic_map_swap_div(dst, i, j);
8406 return dst;
8409 struct isl_basic_set *isl_basic_set_align_divs(
8410 struct isl_basic_set *dst, struct isl_basic_set *src)
8412 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst),
8413 bset_to_bmap(src)));
8416 struct isl_map *isl_map_align_divs(struct isl_map *map)
8418 int i;
8420 if (!map)
8421 return NULL;
8422 if (map->n == 0)
8423 return map;
8424 map = isl_map_compute_divs(map);
8425 map = isl_map_cow(map);
8426 if (!map)
8427 return NULL;
8429 for (i = 1; i < map->n; ++i)
8430 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8431 for (i = 1; i < map->n; ++i) {
8432 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8433 if (!map->p[i])
8434 return isl_map_free(map);
8437 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8438 return map;
8441 struct isl_set *isl_set_align_divs(struct isl_set *set)
8443 return set_from_map(isl_map_align_divs(set_to_map(set)));
8446 /* Align the divs of the basic maps in "map" to those
8447 * of the basic maps in "list", as well as to the other basic maps in "map".
8448 * The elements in "list" are assumed to have known divs.
8450 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8451 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8453 int i, n;
8455 map = isl_map_compute_divs(map);
8456 map = isl_map_cow(map);
8457 if (!map || !list)
8458 return isl_map_free(map);
8459 if (map->n == 0)
8460 return map;
8462 n = isl_basic_map_list_n_basic_map(list);
8463 for (i = 0; i < n; ++i) {
8464 isl_basic_map *bmap;
8466 bmap = isl_basic_map_list_get_basic_map(list, i);
8467 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8468 isl_basic_map_free(bmap);
8470 if (!map->p[0])
8471 return isl_map_free(map);
8473 return isl_map_align_divs(map);
8476 /* Align the divs of each element of "list" to those of "bmap".
8477 * Both "bmap" and the elements of "list" are assumed to have known divs.
8479 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8480 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8482 int i, n;
8484 if (!list || !bmap)
8485 return isl_basic_map_list_free(list);
8487 n = isl_basic_map_list_n_basic_map(list);
8488 for (i = 0; i < n; ++i) {
8489 isl_basic_map *bmap_i;
8491 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8492 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8493 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8496 return list;
8499 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8500 __isl_take isl_map *map)
8502 if (!set || !map)
8503 goto error;
8504 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8505 map = isl_map_intersect_domain(map, set);
8506 set = isl_map_range(map);
8507 return set;
8508 error:
8509 isl_set_free(set);
8510 isl_map_free(map);
8511 return NULL;
8514 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8515 __isl_take isl_map *map)
8517 return isl_map_align_params_map_map_and(set, map, &set_apply);
8520 /* There is no need to cow as removing empty parts doesn't change
8521 * the meaning of the set.
8523 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8525 int i;
8527 if (!map)
8528 return NULL;
8530 for (i = map->n - 1; i >= 0; --i)
8531 remove_if_empty(map, i);
8533 return map;
8536 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8538 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8541 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8543 struct isl_basic_map *bmap;
8544 if (!map || map->n == 0)
8545 return NULL;
8546 bmap = map->p[map->n-1];
8547 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8548 return isl_basic_map_copy(bmap);
8551 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8553 return bset_from_bmap(isl_map_copy_basic_map(set_to_map(set)));
8556 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8557 __isl_keep isl_basic_map *bmap)
8559 int i;
8561 if (!map || !bmap)
8562 goto error;
8563 for (i = map->n-1; i >= 0; --i) {
8564 if (map->p[i] != bmap)
8565 continue;
8566 map = isl_map_cow(map);
8567 if (!map)
8568 goto error;
8569 isl_basic_map_free(map->p[i]);
8570 if (i != map->n-1) {
8571 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8572 map->p[i] = map->p[map->n-1];
8574 map->n--;
8575 return map;
8577 return map;
8578 error:
8579 isl_map_free(map);
8580 return NULL;
8583 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8584 struct isl_basic_set *bset)
8586 return set_from_map(isl_map_drop_basic_map(set_to_map(set),
8587 bset_to_bmap(bset)));
8590 /* Given two basic sets bset1 and bset2, compute the maximal difference
8591 * between the values of dimension pos in bset1 and those in bset2
8592 * for any common value of the parameters and dimensions preceding pos.
8594 static enum isl_lp_result basic_set_maximal_difference_at(
8595 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8596 int pos, isl_int *opt)
8598 isl_space *dims;
8599 struct isl_basic_map *bmap1 = NULL;
8600 struct isl_basic_map *bmap2 = NULL;
8601 struct isl_ctx *ctx;
8602 struct isl_vec *obj;
8603 unsigned total;
8604 unsigned nparam;
8605 unsigned dim1, dim2;
8606 enum isl_lp_result res;
8608 if (!bset1 || !bset2)
8609 return isl_lp_error;
8611 nparam = isl_basic_set_n_param(bset1);
8612 dim1 = isl_basic_set_n_dim(bset1);
8613 dim2 = isl_basic_set_n_dim(bset2);
8614 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8615 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8616 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8617 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8618 if (!bmap1 || !bmap2)
8619 goto error;
8620 bmap1 = isl_basic_map_cow(bmap1);
8621 bmap1 = isl_basic_map_extend(bmap1, nparam,
8622 pos, (dim1 - pos) + (dim2 - pos),
8623 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8624 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8625 if (!bmap1)
8626 goto error2;
8627 total = isl_basic_map_total_dim(bmap1);
8628 ctx = bmap1->ctx;
8629 obj = isl_vec_alloc(ctx, 1 + total);
8630 if (!obj)
8631 goto error2;
8632 isl_seq_clr(obj->block.data, 1 + total);
8633 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8634 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8635 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8636 opt, NULL, NULL);
8637 isl_basic_map_free(bmap1);
8638 isl_vec_free(obj);
8639 return res;
8640 error:
8641 isl_basic_map_free(bmap2);
8642 error2:
8643 isl_basic_map_free(bmap1);
8644 return isl_lp_error;
8647 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8648 * for any common value of the parameters and dimensions preceding pos
8649 * in both basic sets, the values of dimension pos in bset1 are
8650 * smaller or larger than those in bset2.
8652 * Returns
8653 * 1 if bset1 follows bset2
8654 * -1 if bset1 precedes bset2
8655 * 0 if bset1 and bset2 are incomparable
8656 * -2 if some error occurred.
8658 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8659 struct isl_basic_set *bset2, int pos)
8661 isl_int opt;
8662 enum isl_lp_result res;
8663 int cmp;
8665 isl_int_init(opt);
8667 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8669 if (res == isl_lp_empty)
8670 cmp = 0;
8671 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8672 res == isl_lp_unbounded)
8673 cmp = 1;
8674 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8675 cmp = -1;
8676 else
8677 cmp = -2;
8679 isl_int_clear(opt);
8680 return cmp;
8683 /* Given two basic sets bset1 and bset2, check whether
8684 * for any common value of the parameters and dimensions preceding pos
8685 * there is a value of dimension pos in bset1 that is larger
8686 * than a value of the same dimension in bset2.
8688 * Return
8689 * 1 if there exists such a pair
8690 * 0 if there is no such pair, but there is a pair of equal values
8691 * -1 otherwise
8692 * -2 if some error occurred.
8694 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8695 __isl_keep isl_basic_set *bset2, int pos)
8697 isl_int opt;
8698 enum isl_lp_result res;
8699 int cmp;
8701 isl_int_init(opt);
8703 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8705 if (res == isl_lp_empty)
8706 cmp = -1;
8707 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8708 res == isl_lp_unbounded)
8709 cmp = 1;
8710 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8711 cmp = -1;
8712 else if (res == isl_lp_ok)
8713 cmp = 0;
8714 else
8715 cmp = -2;
8717 isl_int_clear(opt);
8718 return cmp;
8721 /* Given two sets set1 and set2, check whether
8722 * for any common value of the parameters and dimensions preceding pos
8723 * there is a value of dimension pos in set1 that is larger
8724 * than a value of the same dimension in set2.
8726 * Return
8727 * 1 if there exists such a pair
8728 * 0 if there is no such pair, but there is a pair of equal values
8729 * -1 otherwise
8730 * -2 if some error occurred.
8732 int isl_set_follows_at(__isl_keep isl_set *set1,
8733 __isl_keep isl_set *set2, int pos)
8735 int i, j;
8736 int follows = -1;
8738 if (!set1 || !set2)
8739 return -2;
8741 for (i = 0; i < set1->n; ++i)
8742 for (j = 0; j < set2->n; ++j) {
8743 int f;
8744 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8745 if (f == 1 || f == -2)
8746 return f;
8747 if (f > follows)
8748 follows = f;
8751 return follows;
8754 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8755 unsigned pos, isl_int *val)
8757 int i;
8758 int d;
8759 unsigned total;
8761 if (!bmap)
8762 return -1;
8763 total = isl_basic_map_total_dim(bmap);
8764 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8765 for (; d+1 > pos; --d)
8766 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8767 break;
8768 if (d != pos)
8769 continue;
8770 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8771 return 0;
8772 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8773 return 0;
8774 if (!isl_int_is_one(bmap->eq[i][1+d]))
8775 return 0;
8776 if (val)
8777 isl_int_neg(*val, bmap->eq[i][0]);
8778 return 1;
8780 return 0;
8783 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8784 unsigned pos, isl_int *val)
8786 int i;
8787 isl_int v;
8788 isl_int tmp;
8789 int fixed;
8791 if (!map)
8792 return -1;
8793 if (map->n == 0)
8794 return 0;
8795 if (map->n == 1)
8796 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8797 isl_int_init(v);
8798 isl_int_init(tmp);
8799 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8800 for (i = 1; fixed == 1 && i < map->n; ++i) {
8801 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8802 if (fixed == 1 && isl_int_ne(tmp, v))
8803 fixed = 0;
8805 if (val)
8806 isl_int_set(*val, v);
8807 isl_int_clear(tmp);
8808 isl_int_clear(v);
8809 return fixed;
8812 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8813 unsigned pos, isl_int *val)
8815 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
8816 pos, val);
8819 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8820 isl_int *val)
8822 return isl_map_plain_has_fixed_var(set_to_map(set), pos, val);
8825 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8826 enum isl_dim_type type, unsigned pos, isl_int *val)
8828 if (pos >= isl_basic_map_dim(bmap, type))
8829 return -1;
8830 return isl_basic_map_plain_has_fixed_var(bmap,
8831 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8834 /* If "bmap" obviously lies on a hyperplane where the given dimension
8835 * has a fixed value, then return that value.
8836 * Otherwise return NaN.
8838 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8839 __isl_keep isl_basic_map *bmap,
8840 enum isl_dim_type type, unsigned pos)
8842 isl_ctx *ctx;
8843 isl_val *v;
8844 int fixed;
8846 if (!bmap)
8847 return NULL;
8848 ctx = isl_basic_map_get_ctx(bmap);
8849 v = isl_val_alloc(ctx);
8850 if (!v)
8851 return NULL;
8852 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8853 if (fixed < 0)
8854 return isl_val_free(v);
8855 if (fixed) {
8856 isl_int_set_si(v->d, 1);
8857 return v;
8859 isl_val_free(v);
8860 return isl_val_nan(ctx);
8863 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8864 enum isl_dim_type type, unsigned pos, isl_int *val)
8866 if (pos >= isl_map_dim(map, type))
8867 return -1;
8868 return isl_map_plain_has_fixed_var(map,
8869 map_offset(map, type) - 1 + pos, val);
8872 /* If "map" obviously lies on a hyperplane where the given dimension
8873 * has a fixed value, then return that value.
8874 * Otherwise return NaN.
8876 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8877 enum isl_dim_type type, unsigned pos)
8879 isl_ctx *ctx;
8880 isl_val *v;
8881 int fixed;
8883 if (!map)
8884 return NULL;
8885 ctx = isl_map_get_ctx(map);
8886 v = isl_val_alloc(ctx);
8887 if (!v)
8888 return NULL;
8889 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8890 if (fixed < 0)
8891 return isl_val_free(v);
8892 if (fixed) {
8893 isl_int_set_si(v->d, 1);
8894 return v;
8896 isl_val_free(v);
8897 return isl_val_nan(ctx);
8900 /* If "set" obviously lies on a hyperplane where the given dimension
8901 * has a fixed value, then return that value.
8902 * Otherwise return NaN.
8904 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8905 enum isl_dim_type type, unsigned pos)
8907 return isl_map_plain_get_val_if_fixed(set, type, pos);
8910 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8911 enum isl_dim_type type, unsigned pos, isl_int *val)
8913 return isl_map_plain_is_fixed(set, type, pos, val);
8916 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8917 * then return this fixed value in *val.
8919 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8920 unsigned dim, isl_int *val)
8922 return isl_basic_set_plain_has_fixed_var(bset,
8923 isl_basic_set_n_param(bset) + dim, val);
8926 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8927 * then return this fixed value in *val.
8929 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8930 unsigned dim, isl_int *val)
8932 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8935 /* Check if input variable in has fixed value and if so and if val is not NULL,
8936 * then return this fixed value in *val.
8938 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8939 unsigned in, isl_int *val)
8941 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8944 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8945 * and if val is not NULL, then return this lower bound in *val.
8947 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8948 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8950 int i, i_eq = -1, i_ineq = -1;
8951 isl_int *c;
8952 unsigned total;
8953 unsigned nparam;
8955 if (!bset)
8956 return -1;
8957 total = isl_basic_set_total_dim(bset);
8958 nparam = isl_basic_set_n_param(bset);
8959 for (i = 0; i < bset->n_eq; ++i) {
8960 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8961 continue;
8962 if (i_eq != -1)
8963 return 0;
8964 i_eq = i;
8966 for (i = 0; i < bset->n_ineq; ++i) {
8967 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8968 continue;
8969 if (i_eq != -1 || i_ineq != -1)
8970 return 0;
8971 i_ineq = i;
8973 if (i_eq == -1 && i_ineq == -1)
8974 return 0;
8975 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8976 /* The coefficient should always be one due to normalization. */
8977 if (!isl_int_is_one(c[1+nparam+dim]))
8978 return 0;
8979 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8980 return 0;
8981 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8982 total - nparam - dim - 1) != -1)
8983 return 0;
8984 if (val)
8985 isl_int_neg(*val, c[0]);
8986 return 1;
8989 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8990 unsigned dim, isl_int *val)
8992 int i;
8993 isl_int v;
8994 isl_int tmp;
8995 int fixed;
8997 if (!set)
8998 return -1;
8999 if (set->n == 0)
9000 return 0;
9001 if (set->n == 1)
9002 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9003 dim, val);
9004 isl_int_init(v);
9005 isl_int_init(tmp);
9006 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9007 dim, &v);
9008 for (i = 1; fixed == 1 && i < set->n; ++i) {
9009 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
9010 dim, &tmp);
9011 if (fixed == 1 && isl_int_ne(tmp, v))
9012 fixed = 0;
9014 if (val)
9015 isl_int_set(*val, v);
9016 isl_int_clear(tmp);
9017 isl_int_clear(v);
9018 return fixed;
9021 /* Return -1 if the constraint "c1" should be sorted before "c2"
9022 * and 1 if it should be sorted after "c2".
9023 * Return 0 if the two constraints are the same (up to the constant term).
9025 * In particular, if a constraint involves later variables than another
9026 * then it is sorted after this other constraint.
9027 * uset_gist depends on constraints without existentially quantified
9028 * variables sorting first.
9030 * For constraints that have the same latest variable, those
9031 * with the same coefficient for this latest variable (first in absolute value
9032 * and then in actual value) are grouped together.
9033 * This is useful for detecting pairs of constraints that can
9034 * be chained in their printed representation.
9036 * Finally, within a group, constraints are sorted according to
9037 * their coefficients (excluding the constant term).
9039 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9041 isl_int **c1 = (isl_int **) p1;
9042 isl_int **c2 = (isl_int **) p2;
9043 int l1, l2;
9044 unsigned size = *(unsigned *) arg;
9045 int cmp;
9047 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9048 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9050 if (l1 != l2)
9051 return l1 - l2;
9053 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9054 if (cmp != 0)
9055 return cmp;
9056 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9057 if (cmp != 0)
9058 return -cmp;
9060 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9063 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9064 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9065 * and 0 if the two constraints are the same (up to the constant term).
9067 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9068 isl_int *c1, isl_int *c2)
9070 unsigned total;
9072 if (!bmap)
9073 return -2;
9074 total = isl_basic_map_total_dim(bmap);
9075 return sort_constraint_cmp(&c1, &c2, &total);
9078 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9079 __isl_take isl_basic_map *bmap)
9081 unsigned total;
9083 if (!bmap)
9084 return NULL;
9085 if (bmap->n_ineq == 0)
9086 return bmap;
9087 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9088 return bmap;
9089 total = isl_basic_map_total_dim(bmap);
9090 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9091 &sort_constraint_cmp, &total) < 0)
9092 return isl_basic_map_free(bmap);
9093 return bmap;
9096 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9097 __isl_take isl_basic_set *bset)
9099 isl_basic_map *bmap = bset_to_bmap(bset);
9100 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9103 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9105 if (!bmap)
9106 return NULL;
9107 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9108 return bmap;
9109 bmap = isl_basic_map_remove_redundancies(bmap);
9110 bmap = isl_basic_map_sort_constraints(bmap);
9111 if (bmap)
9112 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9113 return bmap;
9116 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9118 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset)));
9121 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9122 const __isl_keep isl_basic_map *bmap2)
9124 int i, cmp;
9125 unsigned total;
9127 if (!bmap1 || !bmap2)
9128 return -1;
9130 if (bmap1 == bmap2)
9131 return 0;
9132 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9133 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9134 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9135 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9136 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9137 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9138 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9139 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9140 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9141 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9142 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9143 return 0;
9144 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9145 return 1;
9146 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9147 return -1;
9148 if (bmap1->n_eq != bmap2->n_eq)
9149 return bmap1->n_eq - bmap2->n_eq;
9150 if (bmap1->n_ineq != bmap2->n_ineq)
9151 return bmap1->n_ineq - bmap2->n_ineq;
9152 if (bmap1->n_div != bmap2->n_div)
9153 return bmap1->n_div - bmap2->n_div;
9154 total = isl_basic_map_total_dim(bmap1);
9155 for (i = 0; i < bmap1->n_eq; ++i) {
9156 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9157 if (cmp)
9158 return cmp;
9160 for (i = 0; i < bmap1->n_ineq; ++i) {
9161 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9162 if (cmp)
9163 return cmp;
9165 for (i = 0; i < bmap1->n_div; ++i) {
9166 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9167 if (cmp)
9168 return cmp;
9170 return 0;
9173 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9174 const __isl_keep isl_basic_set *bset2)
9176 return isl_basic_map_plain_cmp(bset1, bset2);
9179 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9181 int i, cmp;
9183 if (set1 == set2)
9184 return 0;
9185 if (set1->n != set2->n)
9186 return set1->n - set2->n;
9188 for (i = 0; i < set1->n; ++i) {
9189 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9190 if (cmp)
9191 return cmp;
9194 return 0;
9197 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9198 __isl_keep isl_basic_map *bmap2)
9200 if (!bmap1 || !bmap2)
9201 return isl_bool_error;
9202 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9205 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9206 __isl_keep isl_basic_set *bset2)
9208 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9209 bset_to_bmap(bset2));
9212 static int qsort_bmap_cmp(const void *p1, const void *p2)
9214 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9215 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9217 return isl_basic_map_plain_cmp(bmap1, bmap2);
9220 /* Sort the basic maps of "map" and remove duplicate basic maps.
9222 * While removing basic maps, we make sure that the basic maps remain
9223 * sorted because isl_map_normalize expects the basic maps of the result
9224 * to be sorted.
9226 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9228 int i, j;
9230 map = isl_map_remove_empty_parts(map);
9231 if (!map)
9232 return NULL;
9233 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9234 for (i = map->n - 1; i >= 1; --i) {
9235 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9236 continue;
9237 isl_basic_map_free(map->p[i-1]);
9238 for (j = i; j < map->n; ++j)
9239 map->p[j - 1] = map->p[j];
9240 map->n--;
9243 return map;
9246 /* Remove obvious duplicates among the basic maps of "map".
9248 * Unlike isl_map_normalize, this function does not remove redundant
9249 * constraints and only removes duplicates that have exactly the same
9250 * constraints in the input. It does sort the constraints and
9251 * the basic maps to ease the detection of duplicates.
9253 * If "map" has already been normalized or if the basic maps are
9254 * disjoint, then there can be no duplicates.
9256 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9258 int i;
9259 isl_basic_map *bmap;
9261 if (!map)
9262 return NULL;
9263 if (map->n <= 1)
9264 return map;
9265 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9266 return map;
9267 for (i = 0; i < map->n; ++i) {
9268 bmap = isl_basic_map_copy(map->p[i]);
9269 bmap = isl_basic_map_sort_constraints(bmap);
9270 if (!bmap)
9271 return isl_map_free(map);
9272 isl_basic_map_free(map->p[i]);
9273 map->p[i] = bmap;
9276 map = sort_and_remove_duplicates(map);
9277 return map;
9280 /* We normalize in place, but if anything goes wrong we need
9281 * to return NULL, so we need to make sure we don't change the
9282 * meaning of any possible other copies of map.
9284 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9286 int i;
9287 struct isl_basic_map *bmap;
9289 if (!map)
9290 return NULL;
9291 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9292 return map;
9293 for (i = 0; i < map->n; ++i) {
9294 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9295 if (!bmap)
9296 goto error;
9297 isl_basic_map_free(map->p[i]);
9298 map->p[i] = bmap;
9301 map = sort_and_remove_duplicates(map);
9302 if (map)
9303 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9304 return map;
9305 error:
9306 isl_map_free(map);
9307 return NULL;
9310 struct isl_set *isl_set_normalize(struct isl_set *set)
9312 return set_from_map(isl_map_normalize(set_to_map(set)));
9315 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9316 __isl_keep isl_map *map2)
9318 int i;
9319 isl_bool equal;
9321 if (!map1 || !map2)
9322 return isl_bool_error;
9324 if (map1 == map2)
9325 return isl_bool_true;
9326 if (!isl_space_is_equal(map1->dim, map2->dim))
9327 return isl_bool_false;
9329 map1 = isl_map_copy(map1);
9330 map2 = isl_map_copy(map2);
9331 map1 = isl_map_normalize(map1);
9332 map2 = isl_map_normalize(map2);
9333 if (!map1 || !map2)
9334 goto error;
9335 equal = map1->n == map2->n;
9336 for (i = 0; equal && i < map1->n; ++i) {
9337 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9338 if (equal < 0)
9339 goto error;
9341 isl_map_free(map1);
9342 isl_map_free(map2);
9343 return equal;
9344 error:
9345 isl_map_free(map1);
9346 isl_map_free(map2);
9347 return isl_bool_error;
9350 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9351 __isl_keep isl_set *set2)
9353 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9356 /* Return an interval that ranges from min to max (inclusive)
9358 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9359 isl_int min, isl_int max)
9361 int k;
9362 struct isl_basic_set *bset = NULL;
9364 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9365 if (!bset)
9366 goto error;
9368 k = isl_basic_set_alloc_inequality(bset);
9369 if (k < 0)
9370 goto error;
9371 isl_int_set_si(bset->ineq[k][1], 1);
9372 isl_int_neg(bset->ineq[k][0], min);
9374 k = isl_basic_set_alloc_inequality(bset);
9375 if (k < 0)
9376 goto error;
9377 isl_int_set_si(bset->ineq[k][1], -1);
9378 isl_int_set(bset->ineq[k][0], max);
9380 return bset;
9381 error:
9382 isl_basic_set_free(bset);
9383 return NULL;
9386 /* Return the basic maps in "map" as a list.
9388 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9389 __isl_keep isl_map *map)
9391 int i;
9392 isl_ctx *ctx;
9393 isl_basic_map_list *list;
9395 if (!map)
9396 return NULL;
9397 ctx = isl_map_get_ctx(map);
9398 list = isl_basic_map_list_alloc(ctx, map->n);
9400 for (i = 0; i < map->n; ++i) {
9401 isl_basic_map *bmap;
9403 bmap = isl_basic_map_copy(map->p[i]);
9404 list = isl_basic_map_list_add(list, bmap);
9407 return list;
9410 /* Return the intersection of the elements in the non-empty list "list".
9411 * All elements are assumed to live in the same space.
9413 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9414 __isl_take isl_basic_map_list *list)
9416 int i, n;
9417 isl_basic_map *bmap;
9419 if (!list)
9420 return NULL;
9421 n = isl_basic_map_list_n_basic_map(list);
9422 if (n < 1)
9423 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9424 "expecting non-empty list", goto error);
9426 bmap = isl_basic_map_list_get_basic_map(list, 0);
9427 for (i = 1; i < n; ++i) {
9428 isl_basic_map *bmap_i;
9430 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9431 bmap = isl_basic_map_intersect(bmap, bmap_i);
9434 isl_basic_map_list_free(list);
9435 return bmap;
9436 error:
9437 isl_basic_map_list_free(list);
9438 return NULL;
9441 /* Return the intersection of the elements in the non-empty list "list".
9442 * All elements are assumed to live in the same space.
9444 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9445 __isl_take isl_basic_set_list *list)
9447 return isl_basic_map_list_intersect(list);
9450 /* Return the union of the elements in the non-empty list "list".
9451 * All elements are assumed to live in the same space.
9453 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9455 int i, n;
9456 isl_set *set;
9458 if (!list)
9459 return NULL;
9460 n = isl_set_list_n_set(list);
9461 if (n < 1)
9462 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9463 "expecting non-empty list", goto error);
9465 set = isl_set_list_get_set(list, 0);
9466 for (i = 1; i < n; ++i) {
9467 isl_set *set_i;
9469 set_i = isl_set_list_get_set(list, i);
9470 set = isl_set_union(set, set_i);
9473 isl_set_list_free(list);
9474 return set;
9475 error:
9476 isl_set_list_free(list);
9477 return NULL;
9480 /* Return the Cartesian product of the basic sets in list (in the given order).
9482 __isl_give isl_basic_set *isl_basic_set_list_product(
9483 __isl_take struct isl_basic_set_list *list)
9485 int i;
9486 unsigned dim;
9487 unsigned nparam;
9488 unsigned extra;
9489 unsigned n_eq;
9490 unsigned n_ineq;
9491 struct isl_basic_set *product = NULL;
9493 if (!list)
9494 goto error;
9495 isl_assert(list->ctx, list->n > 0, goto error);
9496 isl_assert(list->ctx, list->p[0], goto error);
9497 nparam = isl_basic_set_n_param(list->p[0]);
9498 dim = isl_basic_set_n_dim(list->p[0]);
9499 extra = list->p[0]->n_div;
9500 n_eq = list->p[0]->n_eq;
9501 n_ineq = list->p[0]->n_ineq;
9502 for (i = 1; i < list->n; ++i) {
9503 isl_assert(list->ctx, list->p[i], goto error);
9504 isl_assert(list->ctx,
9505 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9506 dim += isl_basic_set_n_dim(list->p[i]);
9507 extra += list->p[i]->n_div;
9508 n_eq += list->p[i]->n_eq;
9509 n_ineq += list->p[i]->n_ineq;
9511 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9512 n_eq, n_ineq);
9513 if (!product)
9514 goto error;
9515 dim = 0;
9516 for (i = 0; i < list->n; ++i) {
9517 isl_basic_set_add_constraints(product,
9518 isl_basic_set_copy(list->p[i]), dim);
9519 dim += isl_basic_set_n_dim(list->p[i]);
9521 isl_basic_set_list_free(list);
9522 return product;
9523 error:
9524 isl_basic_set_free(product);
9525 isl_basic_set_list_free(list);
9526 return NULL;
9529 struct isl_basic_map *isl_basic_map_product(
9530 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9532 isl_space *dim_result = NULL;
9533 struct isl_basic_map *bmap;
9534 unsigned in1, in2, out1, out2, nparam, total, pos;
9535 struct isl_dim_map *dim_map1, *dim_map2;
9537 if (!bmap1 || !bmap2)
9538 goto error;
9540 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9541 bmap2->dim, isl_dim_param), goto error);
9542 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9543 isl_space_copy(bmap2->dim));
9545 in1 = isl_basic_map_n_in(bmap1);
9546 in2 = isl_basic_map_n_in(bmap2);
9547 out1 = isl_basic_map_n_out(bmap1);
9548 out2 = isl_basic_map_n_out(bmap2);
9549 nparam = isl_basic_map_n_param(bmap1);
9551 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9552 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9553 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9554 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9555 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9556 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9557 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9558 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9559 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9560 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9561 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9563 bmap = isl_basic_map_alloc_space(dim_result,
9564 bmap1->n_div + bmap2->n_div,
9565 bmap1->n_eq + bmap2->n_eq,
9566 bmap1->n_ineq + bmap2->n_ineq);
9567 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9568 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9569 bmap = isl_basic_map_simplify(bmap);
9570 return isl_basic_map_finalize(bmap);
9571 error:
9572 isl_basic_map_free(bmap1);
9573 isl_basic_map_free(bmap2);
9574 return NULL;
9577 __isl_give isl_basic_map *isl_basic_map_flat_product(
9578 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9580 isl_basic_map *prod;
9582 prod = isl_basic_map_product(bmap1, bmap2);
9583 prod = isl_basic_map_flatten(prod);
9584 return prod;
9587 __isl_give isl_basic_set *isl_basic_set_flat_product(
9588 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9590 return isl_basic_map_flat_range_product(bset1, bset2);
9593 __isl_give isl_basic_map *isl_basic_map_domain_product(
9594 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9596 isl_space *space_result = NULL;
9597 isl_basic_map *bmap;
9598 unsigned in1, in2, out, nparam, total, pos;
9599 struct isl_dim_map *dim_map1, *dim_map2;
9601 if (!bmap1 || !bmap2)
9602 goto error;
9604 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9605 isl_space_copy(bmap2->dim));
9607 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9608 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9609 out = isl_basic_map_dim(bmap1, isl_dim_out);
9610 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9612 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9613 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9614 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9615 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9616 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9617 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9618 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9619 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9620 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9621 isl_dim_map_div(dim_map1, bmap1, pos += out);
9622 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9624 bmap = isl_basic_map_alloc_space(space_result,
9625 bmap1->n_div + bmap2->n_div,
9626 bmap1->n_eq + bmap2->n_eq,
9627 bmap1->n_ineq + bmap2->n_ineq);
9628 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9629 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9630 bmap = isl_basic_map_simplify(bmap);
9631 return isl_basic_map_finalize(bmap);
9632 error:
9633 isl_basic_map_free(bmap1);
9634 isl_basic_map_free(bmap2);
9635 return NULL;
9638 __isl_give isl_basic_map *isl_basic_map_range_product(
9639 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9641 int rational;
9642 isl_space *dim_result = NULL;
9643 isl_basic_map *bmap;
9644 unsigned in, out1, out2, nparam, total, pos;
9645 struct isl_dim_map *dim_map1, *dim_map2;
9647 rational = isl_basic_map_is_rational(bmap1);
9648 if (rational >= 0 && rational)
9649 rational = isl_basic_map_is_rational(bmap2);
9650 if (!bmap1 || !bmap2 || rational < 0)
9651 goto error;
9653 if (!isl_space_match(bmap1->dim, isl_dim_param,
9654 bmap2->dim, isl_dim_param))
9655 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9656 "parameters don't match", goto error);
9658 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9659 isl_space_copy(bmap2->dim));
9661 in = isl_basic_map_dim(bmap1, isl_dim_in);
9662 out1 = isl_basic_map_n_out(bmap1);
9663 out2 = isl_basic_map_n_out(bmap2);
9664 nparam = isl_basic_map_n_param(bmap1);
9666 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9667 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9668 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9669 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9670 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9671 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9672 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9673 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9674 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9675 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9676 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9678 bmap = isl_basic_map_alloc_space(dim_result,
9679 bmap1->n_div + bmap2->n_div,
9680 bmap1->n_eq + bmap2->n_eq,
9681 bmap1->n_ineq + bmap2->n_ineq);
9682 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9683 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9684 if (rational)
9685 bmap = isl_basic_map_set_rational(bmap);
9686 bmap = isl_basic_map_simplify(bmap);
9687 return isl_basic_map_finalize(bmap);
9688 error:
9689 isl_basic_map_free(bmap1);
9690 isl_basic_map_free(bmap2);
9691 return NULL;
9694 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9695 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9697 isl_basic_map *prod;
9699 prod = isl_basic_map_range_product(bmap1, bmap2);
9700 prod = isl_basic_map_flatten_range(prod);
9701 return prod;
9704 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9705 * and collect the results.
9706 * The result live in the space obtained by calling "space_product"
9707 * on the spaces of "map1" and "map2".
9708 * If "remove_duplicates" is set then the result may contain duplicates
9709 * (even if the inputs do not) and so we try and remove the obvious
9710 * duplicates.
9712 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9713 __isl_take isl_map *map2,
9714 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9715 __isl_take isl_space *right),
9716 __isl_give isl_basic_map *(*basic_map_product)(
9717 __isl_take isl_basic_map *left,
9718 __isl_take isl_basic_map *right),
9719 int remove_duplicates)
9721 unsigned flags = 0;
9722 struct isl_map *result;
9723 int i, j;
9725 if (!map1 || !map2)
9726 goto error;
9728 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9729 map2->dim, isl_dim_param), goto error);
9731 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9732 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9733 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9735 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9736 isl_space_copy(map2->dim)),
9737 map1->n * map2->n, flags);
9738 if (!result)
9739 goto error;
9740 for (i = 0; i < map1->n; ++i)
9741 for (j = 0; j < map2->n; ++j) {
9742 struct isl_basic_map *part;
9743 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9744 isl_basic_map_copy(map2->p[j]));
9745 if (isl_basic_map_is_empty(part))
9746 isl_basic_map_free(part);
9747 else
9748 result = isl_map_add_basic_map(result, part);
9749 if (!result)
9750 goto error;
9752 if (remove_duplicates)
9753 result = isl_map_remove_obvious_duplicates(result);
9754 isl_map_free(map1);
9755 isl_map_free(map2);
9756 return result;
9757 error:
9758 isl_map_free(map1);
9759 isl_map_free(map2);
9760 return NULL;
9763 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9765 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9766 __isl_take isl_map *map2)
9768 return map_product(map1, map2, &isl_space_product,
9769 &isl_basic_map_product, 0);
9772 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9773 __isl_take isl_map *map2)
9775 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9778 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9780 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9781 __isl_take isl_map *map2)
9783 isl_map *prod;
9785 prod = isl_map_product(map1, map2);
9786 prod = isl_map_flatten(prod);
9787 return prod;
9790 /* Given two set A and B, construct its Cartesian product A x B.
9792 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9794 return isl_map_range_product(set1, set2);
9797 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9798 __isl_take isl_set *set2)
9800 return isl_map_flat_range_product(set1, set2);
9803 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9805 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9806 __isl_take isl_map *map2)
9808 return map_product(map1, map2, &isl_space_domain_product,
9809 &isl_basic_map_domain_product, 1);
9812 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9814 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9815 __isl_take isl_map *map2)
9817 return map_product(map1, map2, &isl_space_range_product,
9818 &isl_basic_map_range_product, 1);
9821 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9822 __isl_take isl_map *map2)
9824 return isl_map_align_params_map_map_and(map1, map2,
9825 &map_domain_product_aligned);
9828 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9829 __isl_take isl_map *map2)
9831 return isl_map_align_params_map_map_and(map1, map2,
9832 &map_range_product_aligned);
9835 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9837 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9839 isl_space *space;
9840 int total1, keep1, total2, keep2;
9842 if (!map)
9843 return NULL;
9844 if (!isl_space_domain_is_wrapping(map->dim) ||
9845 !isl_space_range_is_wrapping(map->dim))
9846 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9847 "not a product", return isl_map_free(map));
9849 space = isl_map_get_space(map);
9850 total1 = isl_space_dim(space, isl_dim_in);
9851 total2 = isl_space_dim(space, isl_dim_out);
9852 space = isl_space_factor_domain(space);
9853 keep1 = isl_space_dim(space, isl_dim_in);
9854 keep2 = isl_space_dim(space, isl_dim_out);
9855 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9856 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9857 map = isl_map_reset_space(map, space);
9859 return map;
9862 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9864 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9866 isl_space *space;
9867 int total1, keep1, total2, keep2;
9869 if (!map)
9870 return NULL;
9871 if (!isl_space_domain_is_wrapping(map->dim) ||
9872 !isl_space_range_is_wrapping(map->dim))
9873 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9874 "not a product", return isl_map_free(map));
9876 space = isl_map_get_space(map);
9877 total1 = isl_space_dim(space, isl_dim_in);
9878 total2 = isl_space_dim(space, isl_dim_out);
9879 space = isl_space_factor_range(space);
9880 keep1 = isl_space_dim(space, isl_dim_in);
9881 keep2 = isl_space_dim(space, isl_dim_out);
9882 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9883 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9884 map = isl_map_reset_space(map, space);
9886 return map;
9889 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9891 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9893 isl_space *space;
9894 int total, keep;
9896 if (!map)
9897 return NULL;
9898 if (!isl_space_domain_is_wrapping(map->dim))
9899 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9900 "domain is not a product", return isl_map_free(map));
9902 space = isl_map_get_space(map);
9903 total = isl_space_dim(space, isl_dim_in);
9904 space = isl_space_domain_factor_domain(space);
9905 keep = isl_space_dim(space, isl_dim_in);
9906 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9907 map = isl_map_reset_space(map, space);
9909 return map;
9912 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9914 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9916 isl_space *space;
9917 int total, keep;
9919 if (!map)
9920 return NULL;
9921 if (!isl_space_domain_is_wrapping(map->dim))
9922 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9923 "domain is not a product", return isl_map_free(map));
9925 space = isl_map_get_space(map);
9926 total = isl_space_dim(space, isl_dim_in);
9927 space = isl_space_domain_factor_range(space);
9928 keep = isl_space_dim(space, isl_dim_in);
9929 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9930 map = isl_map_reset_space(map, space);
9932 return map;
9935 /* Given a map A -> [B -> C], extract the map A -> B.
9937 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9939 isl_space *space;
9940 int total, keep;
9942 if (!map)
9943 return NULL;
9944 if (!isl_space_range_is_wrapping(map->dim))
9945 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9946 "range is not a product", return isl_map_free(map));
9948 space = isl_map_get_space(map);
9949 total = isl_space_dim(space, isl_dim_out);
9950 space = isl_space_range_factor_domain(space);
9951 keep = isl_space_dim(space, isl_dim_out);
9952 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9953 map = isl_map_reset_space(map, space);
9955 return map;
9958 /* Given a map A -> [B -> C], extract the map A -> C.
9960 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9962 isl_space *space;
9963 int total, keep;
9965 if (!map)
9966 return NULL;
9967 if (!isl_space_range_is_wrapping(map->dim))
9968 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9969 "range is not a product", return isl_map_free(map));
9971 space = isl_map_get_space(map);
9972 total = isl_space_dim(space, isl_dim_out);
9973 space = isl_space_range_factor_range(space);
9974 keep = isl_space_dim(space, isl_dim_out);
9975 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9976 map = isl_map_reset_space(map, space);
9978 return map;
9981 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9983 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9984 __isl_take isl_map *map2)
9986 isl_map *prod;
9988 prod = isl_map_domain_product(map1, map2);
9989 prod = isl_map_flatten_domain(prod);
9990 return prod;
9993 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9995 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9996 __isl_take isl_map *map2)
9998 isl_map *prod;
10000 prod = isl_map_range_product(map1, map2);
10001 prod = isl_map_flatten_range(prod);
10002 return prod;
10005 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10007 int i;
10008 uint32_t hash = isl_hash_init();
10009 unsigned total;
10011 if (!bmap)
10012 return 0;
10013 bmap = isl_basic_map_copy(bmap);
10014 bmap = isl_basic_map_normalize(bmap);
10015 if (!bmap)
10016 return 0;
10017 total = isl_basic_map_total_dim(bmap);
10018 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10019 for (i = 0; i < bmap->n_eq; ++i) {
10020 uint32_t c_hash;
10021 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10022 isl_hash_hash(hash, c_hash);
10024 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10025 for (i = 0; i < bmap->n_ineq; ++i) {
10026 uint32_t c_hash;
10027 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10028 isl_hash_hash(hash, c_hash);
10030 isl_hash_byte(hash, bmap->n_div & 0xFF);
10031 for (i = 0; i < bmap->n_div; ++i) {
10032 uint32_t c_hash;
10033 if (isl_int_is_zero(bmap->div[i][0]))
10034 continue;
10035 isl_hash_byte(hash, i & 0xFF);
10036 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10037 isl_hash_hash(hash, c_hash);
10039 isl_basic_map_free(bmap);
10040 return hash;
10043 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10045 return isl_basic_map_get_hash(bset_to_bmap(bset));
10048 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10050 int i;
10051 uint32_t hash;
10053 if (!map)
10054 return 0;
10055 map = isl_map_copy(map);
10056 map = isl_map_normalize(map);
10057 if (!map)
10058 return 0;
10060 hash = isl_hash_init();
10061 for (i = 0; i < map->n; ++i) {
10062 uint32_t bmap_hash;
10063 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10064 isl_hash_hash(hash, bmap_hash);
10067 isl_map_free(map);
10069 return hash;
10072 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10074 return isl_map_get_hash(set_to_map(set));
10077 /* Check if the value for dimension dim is completely determined
10078 * by the values of the other parameters and variables.
10079 * That is, check if dimension dim is involved in an equality.
10081 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10083 int i;
10084 unsigned nparam;
10086 if (!bset)
10087 return -1;
10088 nparam = isl_basic_set_n_param(bset);
10089 for (i = 0; i < bset->n_eq; ++i)
10090 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10091 return 1;
10092 return 0;
10095 /* Check if the value for dimension dim is completely determined
10096 * by the values of the other parameters and variables.
10097 * That is, check if dimension dim is involved in an equality
10098 * for each of the subsets.
10100 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10102 int i;
10104 if (!set)
10105 return -1;
10106 for (i = 0; i < set->n; ++i) {
10107 int unique;
10108 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10109 if (unique != 1)
10110 return unique;
10112 return 1;
10115 /* Return the number of basic maps in the (current) representation of "map".
10117 int isl_map_n_basic_map(__isl_keep isl_map *map)
10119 return map ? map->n : 0;
10122 int isl_set_n_basic_set(__isl_keep isl_set *set)
10124 return set ? set->n : 0;
10127 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10128 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10130 int i;
10132 if (!map)
10133 return isl_stat_error;
10135 for (i = 0; i < map->n; ++i)
10136 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10137 return isl_stat_error;
10139 return isl_stat_ok;
10142 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10143 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10145 int i;
10147 if (!set)
10148 return isl_stat_error;
10150 for (i = 0; i < set->n; ++i)
10151 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10152 return isl_stat_error;
10154 return isl_stat_ok;
10157 /* Return a list of basic sets, the union of which is equal to "set".
10159 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10160 __isl_keep isl_set *set)
10162 int i;
10163 isl_basic_set_list *list;
10165 if (!set)
10166 return NULL;
10168 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10169 for (i = 0; i < set->n; ++i) {
10170 isl_basic_set *bset;
10172 bset = isl_basic_set_copy(set->p[i]);
10173 list = isl_basic_set_list_add(list, bset);
10176 return list;
10179 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10181 isl_space *dim;
10183 if (!bset)
10184 return NULL;
10186 bset = isl_basic_set_cow(bset);
10187 if (!bset)
10188 return NULL;
10190 dim = isl_basic_set_get_space(bset);
10191 dim = isl_space_lift(dim, bset->n_div);
10192 if (!dim)
10193 goto error;
10194 isl_space_free(bset->dim);
10195 bset->dim = dim;
10196 bset->extra -= bset->n_div;
10197 bset->n_div = 0;
10199 bset = isl_basic_set_finalize(bset);
10201 return bset;
10202 error:
10203 isl_basic_set_free(bset);
10204 return NULL;
10207 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10209 int i;
10210 isl_space *dim;
10211 unsigned n_div;
10213 set = isl_set_align_divs(set);
10215 if (!set)
10216 return NULL;
10218 set = isl_set_cow(set);
10219 if (!set)
10220 return NULL;
10222 n_div = set->p[0]->n_div;
10223 dim = isl_set_get_space(set);
10224 dim = isl_space_lift(dim, n_div);
10225 if (!dim)
10226 goto error;
10227 isl_space_free(set->dim);
10228 set->dim = dim;
10230 for (i = 0; i < set->n; ++i) {
10231 set->p[i] = isl_basic_set_lift(set->p[i]);
10232 if (!set->p[i])
10233 goto error;
10236 return set;
10237 error:
10238 isl_set_free(set);
10239 return NULL;
10242 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10244 isl_space *dim;
10245 struct isl_basic_map *bmap;
10246 unsigned n_set;
10247 unsigned n_div;
10248 unsigned n_param;
10249 unsigned total;
10250 int i, k, l;
10252 set = isl_set_align_divs(set);
10254 if (!set)
10255 return NULL;
10257 dim = isl_set_get_space(set);
10258 if (set->n == 0 || set->p[0]->n_div == 0) {
10259 isl_set_free(set);
10260 return isl_map_identity(isl_space_map_from_set(dim));
10263 n_div = set->p[0]->n_div;
10264 dim = isl_space_map_from_set(dim);
10265 n_param = isl_space_dim(dim, isl_dim_param);
10266 n_set = isl_space_dim(dim, isl_dim_in);
10267 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10268 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10269 for (i = 0; i < n_set; ++i)
10270 bmap = var_equal(bmap, i);
10272 total = n_param + n_set + n_set + n_div;
10273 for (i = 0; i < n_div; ++i) {
10274 k = isl_basic_map_alloc_inequality(bmap);
10275 if (k < 0)
10276 goto error;
10277 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10278 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10279 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10280 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10281 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10282 set->p[0]->div[i][0]);
10284 l = isl_basic_map_alloc_inequality(bmap);
10285 if (l < 0)
10286 goto error;
10287 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10288 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10289 set->p[0]->div[i][0]);
10290 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10293 isl_set_free(set);
10294 bmap = isl_basic_map_simplify(bmap);
10295 bmap = isl_basic_map_finalize(bmap);
10296 return isl_map_from_basic_map(bmap);
10297 error:
10298 isl_set_free(set);
10299 isl_basic_map_free(bmap);
10300 return NULL;
10303 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10305 unsigned dim;
10306 int size = 0;
10308 if (!bset)
10309 return -1;
10311 dim = isl_basic_set_total_dim(bset);
10312 size += bset->n_eq * (1 + dim);
10313 size += bset->n_ineq * (1 + dim);
10314 size += bset->n_div * (2 + dim);
10316 return size;
10319 int isl_set_size(__isl_keep isl_set *set)
10321 int i;
10322 int size = 0;
10324 if (!set)
10325 return -1;
10327 for (i = 0; i < set->n; ++i)
10328 size += isl_basic_set_size(set->p[i]);
10330 return size;
10333 /* Check if there is any lower bound (if lower == 0) and/or upper
10334 * bound (if upper == 0) on the specified dim.
10336 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10337 enum isl_dim_type type, unsigned pos, int lower, int upper)
10339 int i;
10341 if (!bmap)
10342 return isl_bool_error;
10344 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10345 return isl_bool_error);
10347 pos += isl_basic_map_offset(bmap, type);
10349 for (i = 0; i < bmap->n_div; ++i) {
10350 if (isl_int_is_zero(bmap->div[i][0]))
10351 continue;
10352 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10353 return isl_bool_true;
10356 for (i = 0; i < bmap->n_eq; ++i)
10357 if (!isl_int_is_zero(bmap->eq[i][pos]))
10358 return isl_bool_true;
10360 for (i = 0; i < bmap->n_ineq; ++i) {
10361 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10362 if (sgn > 0)
10363 lower = 1;
10364 if (sgn < 0)
10365 upper = 1;
10368 return lower && upper;
10371 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10372 enum isl_dim_type type, unsigned pos)
10374 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10377 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10378 enum isl_dim_type type, unsigned pos)
10380 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10383 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10384 enum isl_dim_type type, unsigned pos)
10386 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10389 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10390 enum isl_dim_type type, unsigned pos)
10392 int i;
10394 if (!map)
10395 return -1;
10397 for (i = 0; i < map->n; ++i) {
10398 int bounded;
10399 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10400 if (bounded < 0 || !bounded)
10401 return bounded;
10404 return 1;
10407 /* Return 1 if the specified dim is involved in both an upper bound
10408 * and a lower bound.
10410 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10411 enum isl_dim_type type, unsigned pos)
10413 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10416 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10418 static isl_bool has_any_bound(__isl_keep isl_map *map,
10419 enum isl_dim_type type, unsigned pos,
10420 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10421 enum isl_dim_type type, unsigned pos))
10423 int i;
10425 if (!map)
10426 return isl_bool_error;
10428 for (i = 0; i < map->n; ++i) {
10429 isl_bool bounded;
10430 bounded = fn(map->p[i], type, pos);
10431 if (bounded < 0 || bounded)
10432 return bounded;
10435 return isl_bool_false;
10438 /* Return 1 if the specified dim is involved in any lower bound.
10440 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10441 enum isl_dim_type type, unsigned pos)
10443 return has_any_bound(set, type, pos,
10444 &isl_basic_map_dim_has_lower_bound);
10447 /* Return 1 if the specified dim is involved in any upper bound.
10449 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10450 enum isl_dim_type type, unsigned pos)
10452 return has_any_bound(set, type, pos,
10453 &isl_basic_map_dim_has_upper_bound);
10456 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10458 static isl_bool has_bound(__isl_keep isl_map *map,
10459 enum isl_dim_type type, unsigned pos,
10460 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10461 enum isl_dim_type type, unsigned pos))
10463 int i;
10465 if (!map)
10466 return isl_bool_error;
10468 for (i = 0; i < map->n; ++i) {
10469 isl_bool bounded;
10470 bounded = fn(map->p[i], type, pos);
10471 if (bounded < 0 || !bounded)
10472 return bounded;
10475 return isl_bool_true;
10478 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10480 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10481 enum isl_dim_type type, unsigned pos)
10483 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10486 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10488 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10489 enum isl_dim_type type, unsigned pos)
10491 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10494 /* For each of the "n" variables starting at "first", determine
10495 * the sign of the variable and put the results in the first "n"
10496 * elements of the array "signs".
10497 * Sign
10498 * 1 means that the variable is non-negative
10499 * -1 means that the variable is non-positive
10500 * 0 means the variable attains both positive and negative values.
10502 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10503 unsigned first, unsigned n, int *signs)
10505 isl_vec *bound = NULL;
10506 struct isl_tab *tab = NULL;
10507 struct isl_tab_undo *snap;
10508 int i;
10510 if (!bset || !signs)
10511 return -1;
10513 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10514 tab = isl_tab_from_basic_set(bset, 0);
10515 if (!bound || !tab)
10516 goto error;
10518 isl_seq_clr(bound->el, bound->size);
10519 isl_int_set_si(bound->el[0], -1);
10521 snap = isl_tab_snap(tab);
10522 for (i = 0; i < n; ++i) {
10523 int empty;
10525 isl_int_set_si(bound->el[1 + first + i], -1);
10526 if (isl_tab_add_ineq(tab, bound->el) < 0)
10527 goto error;
10528 empty = tab->empty;
10529 isl_int_set_si(bound->el[1 + first + i], 0);
10530 if (isl_tab_rollback(tab, snap) < 0)
10531 goto error;
10533 if (empty) {
10534 signs[i] = 1;
10535 continue;
10538 isl_int_set_si(bound->el[1 + first + i], 1);
10539 if (isl_tab_add_ineq(tab, bound->el) < 0)
10540 goto error;
10541 empty = tab->empty;
10542 isl_int_set_si(bound->el[1 + first + i], 0);
10543 if (isl_tab_rollback(tab, snap) < 0)
10544 goto error;
10546 signs[i] = empty ? -1 : 0;
10549 isl_tab_free(tab);
10550 isl_vec_free(bound);
10551 return 0;
10552 error:
10553 isl_tab_free(tab);
10554 isl_vec_free(bound);
10555 return -1;
10558 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10559 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10561 if (!bset || !signs)
10562 return -1;
10563 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10564 return -1);
10566 first += pos(bset->dim, type) - 1;
10567 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10570 /* Is it possible for the integer division "div" to depend (possibly
10571 * indirectly) on any output dimensions?
10573 * If the div is undefined, then we conservatively assume that it
10574 * may depend on them.
10575 * Otherwise, we check if it actually depends on them or on any integer
10576 * divisions that may depend on them.
10578 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10580 int i;
10581 unsigned n_out, o_out;
10582 unsigned n_div, o_div;
10584 if (isl_int_is_zero(bmap->div[div][0]))
10585 return 1;
10587 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10588 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10590 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10591 return 1;
10593 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10594 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10596 for (i = 0; i < n_div; ++i) {
10597 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10598 continue;
10599 if (div_may_involve_output(bmap, i))
10600 return 1;
10603 return 0;
10606 /* Return the first integer division of "bmap" in the range
10607 * [first, first + n[ that may depend on any output dimensions and
10608 * that has a non-zero coefficient in "c" (where the first coefficient
10609 * in "c" corresponds to integer division "first").
10611 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10612 isl_int *c, int first, int n)
10614 int k;
10616 if (!bmap)
10617 return -1;
10619 for (k = first; k < first + n; ++k) {
10620 if (isl_int_is_zero(c[k]))
10621 continue;
10622 if (div_may_involve_output(bmap, k))
10623 return k;
10626 return first + n;
10629 /* Look for a pair of inequality constraints in "bmap" of the form
10631 * -l + i >= 0 or i >= l
10632 * and
10633 * n + l - i >= 0 or i <= l + n
10635 * with n < "m" and i the output dimension at position "pos".
10636 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10637 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10638 * and earlier output dimensions, as well as integer divisions that do
10639 * not involve any of the output dimensions.
10641 * Return the index of the first inequality constraint or bmap->n_ineq
10642 * if no such pair can be found.
10644 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10645 int pos, isl_int m)
10647 int i, j;
10648 isl_ctx *ctx;
10649 unsigned total;
10650 unsigned n_div, o_div;
10651 unsigned n_out, o_out;
10652 int less;
10654 if (!bmap)
10655 return -1;
10657 ctx = isl_basic_map_get_ctx(bmap);
10658 total = isl_basic_map_total_dim(bmap);
10659 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10660 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10661 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10662 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10663 for (i = 0; i < bmap->n_ineq; ++i) {
10664 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10665 continue;
10666 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10667 n_out - (pos + 1)) != -1)
10668 continue;
10669 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10670 0, n_div) < n_div)
10671 continue;
10672 for (j = i + 1; j < bmap->n_ineq; ++j) {
10673 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10674 ctx->one))
10675 continue;
10676 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10677 bmap->ineq[j] + 1, total))
10678 continue;
10679 break;
10681 if (j >= bmap->n_ineq)
10682 continue;
10683 isl_int_add(bmap->ineq[i][0],
10684 bmap->ineq[i][0], bmap->ineq[j][0]);
10685 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10686 isl_int_sub(bmap->ineq[i][0],
10687 bmap->ineq[i][0], bmap->ineq[j][0]);
10688 if (!less)
10689 continue;
10690 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10691 return i;
10692 else
10693 return j;
10696 return bmap->n_ineq;
10699 /* Return the index of the equality of "bmap" that defines
10700 * the output dimension "pos" in terms of earlier dimensions.
10701 * The equality may also involve integer divisions, as long
10702 * as those integer divisions are defined in terms of
10703 * parameters or input dimensions.
10704 * In this case, *div is set to the number of integer divisions and
10705 * *ineq is set to the number of inequality constraints (provided
10706 * div and ineq are not NULL).
10708 * The equality may also involve a single integer division involving
10709 * the output dimensions (typically only output dimension "pos") as
10710 * long as the coefficient of output dimension "pos" is 1 or -1 and
10711 * there is a pair of constraints i >= l and i <= l + n, with i referring
10712 * to output dimension "pos", l an expression involving only earlier
10713 * dimensions and n smaller than the coefficient of the integer division
10714 * in the equality. In this case, the output dimension can be defined
10715 * in terms of a modulo expression that does not involve the integer division.
10716 * *div is then set to this single integer division and
10717 * *ineq is set to the index of constraint i >= l.
10719 * Return bmap->n_eq if there is no such equality.
10720 * Return -1 on error.
10722 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10723 int pos, int *div, int *ineq)
10725 int j, k, l;
10726 unsigned n_out, o_out;
10727 unsigned n_div, o_div;
10729 if (!bmap)
10730 return -1;
10732 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10733 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10734 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10735 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10737 if (ineq)
10738 *ineq = bmap->n_ineq;
10739 if (div)
10740 *div = n_div;
10741 for (j = 0; j < bmap->n_eq; ++j) {
10742 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10743 continue;
10744 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10745 n_out - (pos + 1)) != -1)
10746 continue;
10747 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10748 0, n_div);
10749 if (k >= n_div)
10750 return j;
10751 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10752 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10753 continue;
10754 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10755 k + 1, n_div - (k+1)) < n_div)
10756 continue;
10757 l = find_modulo_constraint_pair(bmap, pos,
10758 bmap->eq[j][o_div + k]);
10759 if (l < 0)
10760 return -1;
10761 if (l >= bmap->n_ineq)
10762 continue;
10763 if (div)
10764 *div = k;
10765 if (ineq)
10766 *ineq = l;
10767 return j;
10770 return bmap->n_eq;
10773 /* Check if the given basic map is obviously single-valued.
10774 * In particular, for each output dimension, check that there is
10775 * an equality that defines the output dimension in terms of
10776 * earlier dimensions.
10778 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10780 int i;
10781 unsigned n_out;
10783 if (!bmap)
10784 return isl_bool_error;
10786 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10788 for (i = 0; i < n_out; ++i) {
10789 int eq;
10791 eq = isl_basic_map_output_defining_equality(bmap, i,
10792 NULL, NULL);
10793 if (eq < 0)
10794 return isl_bool_error;
10795 if (eq >= bmap->n_eq)
10796 return isl_bool_false;
10799 return isl_bool_true;
10802 /* Check if the given basic map is single-valued.
10803 * We simply compute
10805 * M \circ M^-1
10807 * and check if the result is a subset of the identity mapping.
10809 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10811 isl_space *space;
10812 isl_basic_map *test;
10813 isl_basic_map *id;
10814 isl_bool sv;
10816 sv = isl_basic_map_plain_is_single_valued(bmap);
10817 if (sv < 0 || sv)
10818 return sv;
10820 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10821 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10823 space = isl_basic_map_get_space(bmap);
10824 space = isl_space_map_from_set(isl_space_range(space));
10825 id = isl_basic_map_identity(space);
10827 sv = isl_basic_map_is_subset(test, id);
10829 isl_basic_map_free(test);
10830 isl_basic_map_free(id);
10832 return sv;
10835 /* Check if the given map is obviously single-valued.
10837 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10839 if (!map)
10840 return isl_bool_error;
10841 if (map->n == 0)
10842 return isl_bool_true;
10843 if (map->n >= 2)
10844 return isl_bool_false;
10846 return isl_basic_map_plain_is_single_valued(map->p[0]);
10849 /* Check if the given map is single-valued.
10850 * We simply compute
10852 * M \circ M^-1
10854 * and check if the result is a subset of the identity mapping.
10856 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10858 isl_space *dim;
10859 isl_map *test;
10860 isl_map *id;
10861 isl_bool sv;
10863 sv = isl_map_plain_is_single_valued(map);
10864 if (sv < 0 || sv)
10865 return sv;
10867 test = isl_map_reverse(isl_map_copy(map));
10868 test = isl_map_apply_range(test, isl_map_copy(map));
10870 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10871 id = isl_map_identity(dim);
10873 sv = isl_map_is_subset(test, id);
10875 isl_map_free(test);
10876 isl_map_free(id);
10878 return sv;
10881 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10883 isl_bool in;
10885 map = isl_map_copy(map);
10886 map = isl_map_reverse(map);
10887 in = isl_map_is_single_valued(map);
10888 isl_map_free(map);
10890 return in;
10893 /* Check if the given map is obviously injective.
10895 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10897 isl_bool in;
10899 map = isl_map_copy(map);
10900 map = isl_map_reverse(map);
10901 in = isl_map_plain_is_single_valued(map);
10902 isl_map_free(map);
10904 return in;
10907 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10909 isl_bool sv;
10911 sv = isl_map_is_single_valued(map);
10912 if (sv < 0 || !sv)
10913 return sv;
10915 return isl_map_is_injective(map);
10918 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10920 return isl_map_is_single_valued(set_to_map(set));
10923 /* Does "map" only map elements to themselves?
10925 * If the domain and range spaces are different, then "map"
10926 * is considered not to be an identity relation, even if it is empty.
10927 * Otherwise, construct the maximal identity relation and
10928 * check whether "map" is a subset of this relation.
10930 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10932 isl_space *space;
10933 isl_map *id;
10934 isl_bool equal, is_identity;
10936 space = isl_map_get_space(map);
10937 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10938 isl_space_free(space);
10939 if (equal < 0 || !equal)
10940 return equal;
10942 id = isl_map_identity(isl_map_get_space(map));
10943 is_identity = isl_map_is_subset(map, id);
10944 isl_map_free(id);
10946 return is_identity;
10949 int isl_map_is_translation(__isl_keep isl_map *map)
10951 int ok;
10952 isl_set *delta;
10954 delta = isl_map_deltas(isl_map_copy(map));
10955 ok = isl_set_is_singleton(delta);
10956 isl_set_free(delta);
10958 return ok;
10961 static int unique(isl_int *p, unsigned pos, unsigned len)
10963 if (isl_seq_first_non_zero(p, pos) != -1)
10964 return 0;
10965 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10966 return 0;
10967 return 1;
10970 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10972 int i, j;
10973 unsigned nvar;
10974 unsigned ovar;
10976 if (!bset)
10977 return -1;
10979 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10980 return 0;
10982 nvar = isl_basic_set_dim(bset, isl_dim_set);
10983 ovar = isl_space_offset(bset->dim, isl_dim_set);
10984 for (j = 0; j < nvar; ++j) {
10985 int lower = 0, upper = 0;
10986 for (i = 0; i < bset->n_eq; ++i) {
10987 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10988 continue;
10989 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10990 return 0;
10991 break;
10993 if (i < bset->n_eq)
10994 continue;
10995 for (i = 0; i < bset->n_ineq; ++i) {
10996 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10997 continue;
10998 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10999 return 0;
11000 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11001 lower = 1;
11002 else
11003 upper = 1;
11005 if (!lower || !upper)
11006 return 0;
11009 return 1;
11012 int isl_set_is_box(__isl_keep isl_set *set)
11014 if (!set)
11015 return -1;
11016 if (set->n != 1)
11017 return 0;
11019 return isl_basic_set_is_box(set->p[0]);
11022 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11024 if (!bset)
11025 return isl_bool_error;
11027 return isl_space_is_wrapping(bset->dim);
11030 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11032 if (!set)
11033 return isl_bool_error;
11035 return isl_space_is_wrapping(set->dim);
11038 /* Modify the space of "map" through a call to "change".
11039 * If "can_change" is set (not NULL), then first call it to check
11040 * if the modification is allowed, printing the error message "cannot_change"
11041 * if it is not.
11043 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11044 isl_bool (*can_change)(__isl_keep isl_map *map),
11045 const char *cannot_change,
11046 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11048 isl_bool ok;
11049 isl_space *space;
11051 if (!map)
11052 return NULL;
11054 ok = can_change ? can_change(map) : isl_bool_true;
11055 if (ok < 0)
11056 return isl_map_free(map);
11057 if (!ok)
11058 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11059 return isl_map_free(map));
11061 space = change(isl_map_get_space(map));
11062 map = isl_map_reset_space(map, space);
11064 return map;
11067 /* Is the domain of "map" a wrapped relation?
11069 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11071 if (!map)
11072 return isl_bool_error;
11074 return isl_space_domain_is_wrapping(map->dim);
11077 /* Is the range of "map" a wrapped relation?
11079 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11081 if (!map)
11082 return isl_bool_error;
11084 return isl_space_range_is_wrapping(map->dim);
11087 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11089 bmap = isl_basic_map_cow(bmap);
11090 if (!bmap)
11091 return NULL;
11093 bmap->dim = isl_space_wrap(bmap->dim);
11094 if (!bmap->dim)
11095 goto error;
11097 bmap = isl_basic_map_finalize(bmap);
11099 return bset_from_bmap(bmap);
11100 error:
11101 isl_basic_map_free(bmap);
11102 return NULL;
11105 /* Given a map A -> B, return the set (A -> B).
11107 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11109 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11112 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11114 bset = isl_basic_set_cow(bset);
11115 if (!bset)
11116 return NULL;
11118 bset->dim = isl_space_unwrap(bset->dim);
11119 if (!bset->dim)
11120 goto error;
11122 bset = isl_basic_set_finalize(bset);
11124 return bset_to_bmap(bset);
11125 error:
11126 isl_basic_set_free(bset);
11127 return NULL;
11130 /* Given a set (A -> B), return the map A -> B.
11131 * Error out if "set" is not of the form (A -> B).
11133 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11135 return isl_map_change_space(set, &isl_set_is_wrapping,
11136 "not a wrapping set", &isl_space_unwrap);
11139 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11140 enum isl_dim_type type)
11142 if (!bmap)
11143 return NULL;
11145 if (!isl_space_is_named_or_nested(bmap->dim, type))
11146 return bmap;
11148 bmap = isl_basic_map_cow(bmap);
11149 if (!bmap)
11150 return NULL;
11152 bmap->dim = isl_space_reset(bmap->dim, type);
11153 if (!bmap->dim)
11154 goto error;
11156 bmap = isl_basic_map_finalize(bmap);
11158 return bmap;
11159 error:
11160 isl_basic_map_free(bmap);
11161 return NULL;
11164 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11165 enum isl_dim_type type)
11167 int i;
11169 if (!map)
11170 return NULL;
11172 if (!isl_space_is_named_or_nested(map->dim, type))
11173 return map;
11175 map = isl_map_cow(map);
11176 if (!map)
11177 return NULL;
11179 for (i = 0; i < map->n; ++i) {
11180 map->p[i] = isl_basic_map_reset(map->p[i], type);
11181 if (!map->p[i])
11182 goto error;
11184 map->dim = isl_space_reset(map->dim, type);
11185 if (!map->dim)
11186 goto error;
11188 return map;
11189 error:
11190 isl_map_free(map);
11191 return NULL;
11194 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11196 if (!bmap)
11197 return NULL;
11199 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11200 return bmap;
11202 bmap = isl_basic_map_cow(bmap);
11203 if (!bmap)
11204 return NULL;
11206 bmap->dim = isl_space_flatten(bmap->dim);
11207 if (!bmap->dim)
11208 goto error;
11210 bmap = isl_basic_map_finalize(bmap);
11212 return bmap;
11213 error:
11214 isl_basic_map_free(bmap);
11215 return NULL;
11218 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11220 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11223 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11224 __isl_take isl_basic_map *bmap)
11226 if (!bmap)
11227 return NULL;
11229 if (!bmap->dim->nested[0])
11230 return bmap;
11232 bmap = isl_basic_map_cow(bmap);
11233 if (!bmap)
11234 return NULL;
11236 bmap->dim = isl_space_flatten_domain(bmap->dim);
11237 if (!bmap->dim)
11238 goto error;
11240 bmap = isl_basic_map_finalize(bmap);
11242 return bmap;
11243 error:
11244 isl_basic_map_free(bmap);
11245 return NULL;
11248 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11249 __isl_take isl_basic_map *bmap)
11251 if (!bmap)
11252 return NULL;
11254 if (!bmap->dim->nested[1])
11255 return bmap;
11257 bmap = isl_basic_map_cow(bmap);
11258 if (!bmap)
11259 return NULL;
11261 bmap->dim = isl_space_flatten_range(bmap->dim);
11262 if (!bmap->dim)
11263 goto error;
11265 bmap = isl_basic_map_finalize(bmap);
11267 return bmap;
11268 error:
11269 isl_basic_map_free(bmap);
11270 return NULL;
11273 /* Remove any internal structure from the spaces of domain and range of "map".
11275 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11277 if (!map)
11278 return NULL;
11280 if (!map->dim->nested[0] && !map->dim->nested[1])
11281 return map;
11283 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11286 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11288 return set_from_map(isl_map_flatten(set_to_map(set)));
11291 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11293 isl_space *dim, *flat_dim;
11294 isl_map *map;
11296 dim = isl_set_get_space(set);
11297 flat_dim = isl_space_flatten(isl_space_copy(dim));
11298 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11299 map = isl_map_intersect_domain(map, set);
11301 return map;
11304 /* Remove any internal structure from the space of the domain of "map".
11306 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11308 if (!map)
11309 return NULL;
11311 if (!map->dim->nested[0])
11312 return map;
11314 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11317 /* Remove any internal structure from the space of the range of "map".
11319 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11321 if (!map)
11322 return NULL;
11324 if (!map->dim->nested[1])
11325 return map;
11327 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11330 /* Reorder the dimensions of "bmap" according to the given dim_map
11331 * and set the dimension specification to "dim" and
11332 * perform Gaussian elimination on the result.
11334 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11335 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11337 isl_basic_map *res;
11338 unsigned flags;
11340 bmap = isl_basic_map_cow(bmap);
11341 if (!bmap || !dim || !dim_map)
11342 goto error;
11344 flags = bmap->flags;
11345 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11346 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11347 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11348 res = isl_basic_map_alloc_space(dim,
11349 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11350 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11351 if (res)
11352 res->flags = flags;
11353 res = isl_basic_map_gauss(res, NULL);
11354 res = isl_basic_map_finalize(res);
11355 return res;
11356 error:
11357 free(dim_map);
11358 isl_basic_map_free(bmap);
11359 isl_space_free(dim);
11360 return NULL;
11363 /* Reorder the dimensions of "map" according to given reordering.
11365 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11366 __isl_take isl_reordering *r)
11368 int i;
11369 struct isl_dim_map *dim_map;
11371 map = isl_map_cow(map);
11372 dim_map = isl_dim_map_from_reordering(r);
11373 if (!map || !r || !dim_map)
11374 goto error;
11376 for (i = 0; i < map->n; ++i) {
11377 struct isl_dim_map *dim_map_i;
11379 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11381 map->p[i] = isl_basic_map_realign(map->p[i],
11382 isl_space_copy(r->dim), dim_map_i);
11384 if (!map->p[i])
11385 goto error;
11388 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11390 isl_reordering_free(r);
11391 free(dim_map);
11392 return map;
11393 error:
11394 free(dim_map);
11395 isl_map_free(map);
11396 isl_reordering_free(r);
11397 return NULL;
11400 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11401 __isl_take isl_reordering *r)
11403 return set_from_map(isl_map_realign(set_to_map(set), r));
11406 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11407 __isl_take isl_space *model)
11409 isl_ctx *ctx;
11411 if (!map || !model)
11412 goto error;
11414 ctx = isl_space_get_ctx(model);
11415 if (!isl_space_has_named_params(model))
11416 isl_die(ctx, isl_error_invalid,
11417 "model has unnamed parameters", goto error);
11418 if (!isl_space_has_named_params(map->dim))
11419 isl_die(ctx, isl_error_invalid,
11420 "relation has unnamed parameters", goto error);
11421 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11422 isl_reordering *exp;
11424 model = isl_space_drop_dims(model, isl_dim_in,
11425 0, isl_space_dim(model, isl_dim_in));
11426 model = isl_space_drop_dims(model, isl_dim_out,
11427 0, isl_space_dim(model, isl_dim_out));
11428 exp = isl_parameter_alignment_reordering(map->dim, model);
11429 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11430 map = isl_map_realign(map, exp);
11433 isl_space_free(model);
11434 return map;
11435 error:
11436 isl_space_free(model);
11437 isl_map_free(map);
11438 return NULL;
11441 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11442 __isl_take isl_space *model)
11444 return isl_map_align_params(set, model);
11447 /* Align the parameters of "bmap" to those of "model", introducing
11448 * additional parameters if needed.
11450 __isl_give isl_basic_map *isl_basic_map_align_params(
11451 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11453 isl_ctx *ctx;
11455 if (!bmap || !model)
11456 goto error;
11458 ctx = isl_space_get_ctx(model);
11459 if (!isl_space_has_named_params(model))
11460 isl_die(ctx, isl_error_invalid,
11461 "model has unnamed parameters", goto error);
11462 if (!isl_space_has_named_params(bmap->dim))
11463 isl_die(ctx, isl_error_invalid,
11464 "relation has unnamed parameters", goto error);
11465 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11466 isl_reordering *exp;
11467 struct isl_dim_map *dim_map;
11469 model = isl_space_drop_dims(model, isl_dim_in,
11470 0, isl_space_dim(model, isl_dim_in));
11471 model = isl_space_drop_dims(model, isl_dim_out,
11472 0, isl_space_dim(model, isl_dim_out));
11473 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11474 exp = isl_reordering_extend_space(exp,
11475 isl_basic_map_get_space(bmap));
11476 dim_map = isl_dim_map_from_reordering(exp);
11477 bmap = isl_basic_map_realign(bmap,
11478 exp ? isl_space_copy(exp->dim) : NULL,
11479 isl_dim_map_extend(dim_map, bmap));
11480 isl_reordering_free(exp);
11481 free(dim_map);
11484 isl_space_free(model);
11485 return bmap;
11486 error:
11487 isl_space_free(model);
11488 isl_basic_map_free(bmap);
11489 return NULL;
11492 /* Align the parameters of "bset" to those of "model", introducing
11493 * additional parameters if needed.
11495 __isl_give isl_basic_set *isl_basic_set_align_params(
11496 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11498 return isl_basic_map_align_params(bset, model);
11501 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11502 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11503 enum isl_dim_type c2, enum isl_dim_type c3,
11504 enum isl_dim_type c4, enum isl_dim_type c5)
11506 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11507 struct isl_mat *mat;
11508 int i, j, k;
11509 int pos;
11511 if (!bmap)
11512 return NULL;
11513 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11514 isl_basic_map_total_dim(bmap) + 1);
11515 if (!mat)
11516 return NULL;
11517 for (i = 0; i < bmap->n_eq; ++i)
11518 for (j = 0, pos = 0; j < 5; ++j) {
11519 int off = isl_basic_map_offset(bmap, c[j]);
11520 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11521 isl_int_set(mat->row[i][pos],
11522 bmap->eq[i][off + k]);
11523 ++pos;
11527 return mat;
11530 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11531 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11532 enum isl_dim_type c2, enum isl_dim_type c3,
11533 enum isl_dim_type c4, enum isl_dim_type c5)
11535 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11536 struct isl_mat *mat;
11537 int i, j, k;
11538 int pos;
11540 if (!bmap)
11541 return NULL;
11542 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11543 isl_basic_map_total_dim(bmap) + 1);
11544 if (!mat)
11545 return NULL;
11546 for (i = 0; i < bmap->n_ineq; ++i)
11547 for (j = 0, pos = 0; j < 5; ++j) {
11548 int off = isl_basic_map_offset(bmap, c[j]);
11549 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11550 isl_int_set(mat->row[i][pos],
11551 bmap->ineq[i][off + k]);
11552 ++pos;
11556 return mat;
11559 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11560 __isl_take isl_space *dim,
11561 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11562 enum isl_dim_type c2, enum isl_dim_type c3,
11563 enum isl_dim_type c4, enum isl_dim_type c5)
11565 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11566 isl_basic_map *bmap;
11567 unsigned total;
11568 unsigned extra;
11569 int i, j, k, l;
11570 int pos;
11572 if (!dim || !eq || !ineq)
11573 goto error;
11575 if (eq->n_col != ineq->n_col)
11576 isl_die(dim->ctx, isl_error_invalid,
11577 "equalities and inequalities matrices should have "
11578 "same number of columns", goto error);
11580 total = 1 + isl_space_dim(dim, isl_dim_all);
11582 if (eq->n_col < total)
11583 isl_die(dim->ctx, isl_error_invalid,
11584 "number of columns too small", goto error);
11586 extra = eq->n_col - total;
11588 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11589 eq->n_row, ineq->n_row);
11590 if (!bmap)
11591 goto error;
11592 for (i = 0; i < extra; ++i) {
11593 k = isl_basic_map_alloc_div(bmap);
11594 if (k < 0)
11595 goto error;
11596 isl_int_set_si(bmap->div[k][0], 0);
11598 for (i = 0; i < eq->n_row; ++i) {
11599 l = isl_basic_map_alloc_equality(bmap);
11600 if (l < 0)
11601 goto error;
11602 for (j = 0, pos = 0; j < 5; ++j) {
11603 int off = isl_basic_map_offset(bmap, c[j]);
11604 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11605 isl_int_set(bmap->eq[l][off + k],
11606 eq->row[i][pos]);
11607 ++pos;
11611 for (i = 0; i < ineq->n_row; ++i) {
11612 l = isl_basic_map_alloc_inequality(bmap);
11613 if (l < 0)
11614 goto error;
11615 for (j = 0, pos = 0; j < 5; ++j) {
11616 int off = isl_basic_map_offset(bmap, c[j]);
11617 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11618 isl_int_set(bmap->ineq[l][off + k],
11619 ineq->row[i][pos]);
11620 ++pos;
11625 isl_space_free(dim);
11626 isl_mat_free(eq);
11627 isl_mat_free(ineq);
11629 bmap = isl_basic_map_simplify(bmap);
11630 return isl_basic_map_finalize(bmap);
11631 error:
11632 isl_space_free(dim);
11633 isl_mat_free(eq);
11634 isl_mat_free(ineq);
11635 return NULL;
11638 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11639 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11640 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11642 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11643 c1, c2, c3, c4, isl_dim_in);
11646 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11647 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11648 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11650 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11651 c1, c2, c3, c4, isl_dim_in);
11654 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11655 __isl_take isl_space *dim,
11656 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11657 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11659 isl_basic_map *bmap;
11660 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11661 c1, c2, c3, c4, isl_dim_in);
11662 return bset_from_bmap(bmap);
11665 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11667 if (!bmap)
11668 return isl_bool_error;
11670 return isl_space_can_zip(bmap->dim);
11673 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11675 if (!map)
11676 return isl_bool_error;
11678 return isl_space_can_zip(map->dim);
11681 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11682 * (A -> C) -> (B -> D).
11684 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11686 unsigned pos;
11687 unsigned n1;
11688 unsigned n2;
11690 if (!bmap)
11691 return NULL;
11693 if (!isl_basic_map_can_zip(bmap))
11694 isl_die(bmap->ctx, isl_error_invalid,
11695 "basic map cannot be zipped", goto error);
11696 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11697 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11698 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11699 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11700 bmap = isl_basic_map_cow(bmap);
11701 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11702 if (!bmap)
11703 return NULL;
11704 bmap->dim = isl_space_zip(bmap->dim);
11705 if (!bmap->dim)
11706 goto error;
11707 bmap = isl_basic_map_mark_final(bmap);
11708 return bmap;
11709 error:
11710 isl_basic_map_free(bmap);
11711 return NULL;
11714 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11715 * (A -> C) -> (B -> D).
11717 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11719 int i;
11721 if (!map)
11722 return NULL;
11724 if (!isl_map_can_zip(map))
11725 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11726 goto error);
11728 map = isl_map_cow(map);
11729 if (!map)
11730 return NULL;
11732 for (i = 0; i < map->n; ++i) {
11733 map->p[i] = isl_basic_map_zip(map->p[i]);
11734 if (!map->p[i])
11735 goto error;
11738 map->dim = isl_space_zip(map->dim);
11739 if (!map->dim)
11740 goto error;
11742 return map;
11743 error:
11744 isl_map_free(map);
11745 return NULL;
11748 /* Can we apply isl_basic_map_curry to "bmap"?
11749 * That is, does it have a nested relation in its domain?
11751 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11753 if (!bmap)
11754 return isl_bool_error;
11756 return isl_space_can_curry(bmap->dim);
11759 /* Can we apply isl_map_curry to "map"?
11760 * That is, does it have a nested relation in its domain?
11762 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11764 if (!map)
11765 return isl_bool_error;
11767 return isl_space_can_curry(map->dim);
11770 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11771 * A -> (B -> C).
11773 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11776 if (!bmap)
11777 return NULL;
11779 if (!isl_basic_map_can_curry(bmap))
11780 isl_die(bmap->ctx, isl_error_invalid,
11781 "basic map cannot be curried", goto error);
11782 bmap = isl_basic_map_cow(bmap);
11783 if (!bmap)
11784 return NULL;
11785 bmap->dim = isl_space_curry(bmap->dim);
11786 if (!bmap->dim)
11787 goto error;
11788 bmap = isl_basic_map_mark_final(bmap);
11789 return bmap;
11790 error:
11791 isl_basic_map_free(bmap);
11792 return NULL;
11795 /* Given a map (A -> B) -> C, return the corresponding map
11796 * A -> (B -> C).
11798 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11800 return isl_map_change_space(map, &isl_map_can_curry,
11801 "map cannot be curried", &isl_space_curry);
11804 /* Can isl_map_range_curry be applied to "map"?
11805 * That is, does it have a nested relation in its range,
11806 * the domain of which is itself a nested relation?
11808 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11810 if (!map)
11811 return isl_bool_error;
11813 return isl_space_can_range_curry(map->dim);
11816 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11817 * A -> (B -> (C -> D)).
11819 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11821 return isl_map_change_space(map, &isl_map_can_range_curry,
11822 "map range cannot be curried",
11823 &isl_space_range_curry);
11826 /* Can we apply isl_basic_map_uncurry to "bmap"?
11827 * That is, does it have a nested relation in its domain?
11829 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11831 if (!bmap)
11832 return isl_bool_error;
11834 return isl_space_can_uncurry(bmap->dim);
11837 /* Can we apply isl_map_uncurry to "map"?
11838 * That is, does it have a nested relation in its domain?
11840 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11842 if (!map)
11843 return isl_bool_error;
11845 return isl_space_can_uncurry(map->dim);
11848 /* Given a basic map A -> (B -> C), return the corresponding basic map
11849 * (A -> B) -> C.
11851 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11854 if (!bmap)
11855 return NULL;
11857 if (!isl_basic_map_can_uncurry(bmap))
11858 isl_die(bmap->ctx, isl_error_invalid,
11859 "basic map cannot be uncurried",
11860 return isl_basic_map_free(bmap));
11861 bmap = isl_basic_map_cow(bmap);
11862 if (!bmap)
11863 return NULL;
11864 bmap->dim = isl_space_uncurry(bmap->dim);
11865 if (!bmap->dim)
11866 return isl_basic_map_free(bmap);
11867 bmap = isl_basic_map_mark_final(bmap);
11868 return bmap;
11871 /* Given a map A -> (B -> C), return the corresponding map
11872 * (A -> B) -> C.
11874 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11876 return isl_map_change_space(map, &isl_map_can_uncurry,
11877 "map cannot be uncurried", &isl_space_uncurry);
11880 /* Construct a basic map mapping the domain of the affine expression
11881 * to a one-dimensional range prescribed by the affine expression.
11882 * If "rational" is set, then construct a rational basic map.
11884 * A NaN affine expression cannot be converted to a basic map.
11886 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
11887 __isl_take isl_aff *aff, int rational)
11889 int k;
11890 int pos;
11891 isl_bool is_nan;
11892 isl_local_space *ls;
11893 isl_basic_map *bmap = NULL;
11895 if (!aff)
11896 return NULL;
11897 is_nan = isl_aff_is_nan(aff);
11898 if (is_nan < 0)
11899 goto error;
11900 if (is_nan)
11901 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11902 "cannot convert NaN", goto error);
11904 ls = isl_aff_get_local_space(aff);
11905 bmap = isl_basic_map_from_local_space(ls);
11906 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11907 k = isl_basic_map_alloc_equality(bmap);
11908 if (k < 0)
11909 goto error;
11911 pos = isl_basic_map_offset(bmap, isl_dim_out);
11912 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11913 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11914 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11915 aff->v->size - (pos + 1));
11917 isl_aff_free(aff);
11918 if (rational)
11919 bmap = isl_basic_map_set_rational(bmap);
11920 bmap = isl_basic_map_finalize(bmap);
11921 return bmap;
11922 error:
11923 isl_aff_free(aff);
11924 isl_basic_map_free(bmap);
11925 return NULL;
11928 /* Construct a basic map mapping the domain of the affine expression
11929 * to a one-dimensional range prescribed by the affine expression.
11931 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11933 return isl_basic_map_from_aff2(aff, 0);
11936 /* Construct a map mapping the domain of the affine expression
11937 * to a one-dimensional range prescribed by the affine expression.
11939 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11941 isl_basic_map *bmap;
11943 bmap = isl_basic_map_from_aff(aff);
11944 return isl_map_from_basic_map(bmap);
11947 /* Construct a basic map mapping the domain the multi-affine expression
11948 * to its range, with each dimension in the range equated to the
11949 * corresponding affine expression.
11950 * If "rational" is set, then construct a rational basic map.
11952 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
11953 __isl_take isl_multi_aff *maff, int rational)
11955 int i;
11956 isl_space *space;
11957 isl_basic_map *bmap;
11959 if (!maff)
11960 return NULL;
11962 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11963 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11964 "invalid space", goto error);
11966 space = isl_space_domain(isl_multi_aff_get_space(maff));
11967 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11968 if (rational)
11969 bmap = isl_basic_map_set_rational(bmap);
11971 for (i = 0; i < maff->n; ++i) {
11972 isl_aff *aff;
11973 isl_basic_map *bmap_i;
11975 aff = isl_aff_copy(maff->p[i]);
11976 bmap_i = isl_basic_map_from_aff2(aff, rational);
11978 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11981 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11983 isl_multi_aff_free(maff);
11984 return bmap;
11985 error:
11986 isl_multi_aff_free(maff);
11987 return NULL;
11990 /* Construct a basic map mapping the domain the multi-affine expression
11991 * to its range, with each dimension in the range equated to the
11992 * corresponding affine expression.
11994 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11995 __isl_take isl_multi_aff *ma)
11997 return isl_basic_map_from_multi_aff2(ma, 0);
12000 /* Construct a map mapping the domain the multi-affine expression
12001 * to its range, with each dimension in the range equated to the
12002 * corresponding affine expression.
12004 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12006 isl_basic_map *bmap;
12008 bmap = isl_basic_map_from_multi_aff(maff);
12009 return isl_map_from_basic_map(bmap);
12012 /* Construct a basic map mapping a domain in the given space to
12013 * to an n-dimensional range, with n the number of elements in the list,
12014 * where each coordinate in the range is prescribed by the
12015 * corresponding affine expression.
12016 * The domains of all affine expressions in the list are assumed to match
12017 * domain_dim.
12019 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12020 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12022 int i;
12023 isl_space *dim;
12024 isl_basic_map *bmap;
12026 if (!list)
12027 return NULL;
12029 dim = isl_space_from_domain(domain_dim);
12030 bmap = isl_basic_map_universe(dim);
12032 for (i = 0; i < list->n; ++i) {
12033 isl_aff *aff;
12034 isl_basic_map *bmap_i;
12036 aff = isl_aff_copy(list->p[i]);
12037 bmap_i = isl_basic_map_from_aff(aff);
12039 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12042 isl_aff_list_free(list);
12043 return bmap;
12046 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12047 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12049 return isl_map_equate(set, type1, pos1, type2, pos2);
12052 /* Construct a basic map where the given dimensions are equal to each other.
12054 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12055 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12057 isl_basic_map *bmap = NULL;
12058 int i;
12060 if (!space)
12061 return NULL;
12063 if (pos1 >= isl_space_dim(space, type1))
12064 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12065 "index out of bounds", goto error);
12066 if (pos2 >= isl_space_dim(space, type2))
12067 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12068 "index out of bounds", goto error);
12070 if (type1 == type2 && pos1 == pos2)
12071 return isl_basic_map_universe(space);
12073 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12074 i = isl_basic_map_alloc_equality(bmap);
12075 if (i < 0)
12076 goto error;
12077 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12078 pos1 += isl_basic_map_offset(bmap, type1);
12079 pos2 += isl_basic_map_offset(bmap, type2);
12080 isl_int_set_si(bmap->eq[i][pos1], -1);
12081 isl_int_set_si(bmap->eq[i][pos2], 1);
12082 bmap = isl_basic_map_finalize(bmap);
12083 isl_space_free(space);
12084 return bmap;
12085 error:
12086 isl_space_free(space);
12087 isl_basic_map_free(bmap);
12088 return NULL;
12091 /* Add a constraint imposing that the given two dimensions are equal.
12093 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12094 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12096 isl_basic_map *eq;
12098 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12100 bmap = isl_basic_map_intersect(bmap, eq);
12102 return bmap;
12105 /* Add a constraint imposing that the given two dimensions are equal.
12107 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12108 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12110 isl_basic_map *bmap;
12112 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12114 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12116 return map;
12119 /* Add a constraint imposing that the given two dimensions have opposite values.
12121 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12122 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12124 isl_basic_map *bmap = NULL;
12125 int i;
12127 if (!map)
12128 return NULL;
12130 if (pos1 >= isl_map_dim(map, type1))
12131 isl_die(map->ctx, isl_error_invalid,
12132 "index out of bounds", goto error);
12133 if (pos2 >= isl_map_dim(map, type2))
12134 isl_die(map->ctx, isl_error_invalid,
12135 "index out of bounds", goto error);
12137 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12138 i = isl_basic_map_alloc_equality(bmap);
12139 if (i < 0)
12140 goto error;
12141 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12142 pos1 += isl_basic_map_offset(bmap, type1);
12143 pos2 += isl_basic_map_offset(bmap, type2);
12144 isl_int_set_si(bmap->eq[i][pos1], 1);
12145 isl_int_set_si(bmap->eq[i][pos2], 1);
12146 bmap = isl_basic_map_finalize(bmap);
12148 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12150 return map;
12151 error:
12152 isl_basic_map_free(bmap);
12153 isl_map_free(map);
12154 return NULL;
12157 /* Construct a constraint imposing that the value of the first dimension is
12158 * greater than or equal to that of the second.
12160 static __isl_give isl_constraint *constraint_order_ge(
12161 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12162 enum isl_dim_type type2, int pos2)
12164 isl_constraint *c;
12166 if (!space)
12167 return NULL;
12169 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12171 if (pos1 >= isl_constraint_dim(c, type1))
12172 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12173 "index out of bounds", return isl_constraint_free(c));
12174 if (pos2 >= isl_constraint_dim(c, type2))
12175 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12176 "index out of bounds", return isl_constraint_free(c));
12178 if (type1 == type2 && pos1 == pos2)
12179 return c;
12181 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12182 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12184 return c;
12187 /* Add a constraint imposing that the value of the first dimension is
12188 * greater than or equal to that of the second.
12190 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12191 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12193 isl_constraint *c;
12194 isl_space *space;
12196 if (type1 == type2 && pos1 == pos2)
12197 return bmap;
12198 space = isl_basic_map_get_space(bmap);
12199 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12200 bmap = isl_basic_map_add_constraint(bmap, c);
12202 return bmap;
12205 /* Add a constraint imposing that the value of the first dimension is
12206 * greater than or equal to that of the second.
12208 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12209 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12211 isl_constraint *c;
12212 isl_space *space;
12214 if (type1 == type2 && pos1 == pos2)
12215 return map;
12216 space = isl_map_get_space(map);
12217 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12218 map = isl_map_add_constraint(map, c);
12220 return map;
12223 /* Add a constraint imposing that the value of the first dimension is
12224 * less than or equal to that of the second.
12226 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12227 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12229 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12232 /* Construct a basic map where the value of the first dimension is
12233 * greater than that of the second.
12235 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12236 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12238 isl_basic_map *bmap = NULL;
12239 int i;
12241 if (!space)
12242 return NULL;
12244 if (pos1 >= isl_space_dim(space, type1))
12245 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12246 "index out of bounds", goto error);
12247 if (pos2 >= isl_space_dim(space, type2))
12248 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12249 "index out of bounds", goto error);
12251 if (type1 == type2 && pos1 == pos2)
12252 return isl_basic_map_empty(space);
12254 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12255 i = isl_basic_map_alloc_inequality(bmap);
12256 if (i < 0)
12257 return isl_basic_map_free(bmap);
12258 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12259 pos1 += isl_basic_map_offset(bmap, type1);
12260 pos2 += isl_basic_map_offset(bmap, type2);
12261 isl_int_set_si(bmap->ineq[i][pos1], 1);
12262 isl_int_set_si(bmap->ineq[i][pos2], -1);
12263 isl_int_set_si(bmap->ineq[i][0], -1);
12264 bmap = isl_basic_map_finalize(bmap);
12266 return bmap;
12267 error:
12268 isl_space_free(space);
12269 isl_basic_map_free(bmap);
12270 return NULL;
12273 /* Add a constraint imposing that the value of the first dimension is
12274 * greater than that of the second.
12276 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12277 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12279 isl_basic_map *gt;
12281 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12283 bmap = isl_basic_map_intersect(bmap, gt);
12285 return bmap;
12288 /* Add a constraint imposing that the value of the first dimension is
12289 * greater than that of the second.
12291 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12292 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12294 isl_basic_map *bmap;
12296 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12298 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12300 return map;
12303 /* Add a constraint imposing that the value of the first dimension is
12304 * smaller than that of the second.
12306 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12307 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12309 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12312 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12313 int pos)
12315 isl_aff *div;
12316 isl_local_space *ls;
12318 if (!bmap)
12319 return NULL;
12321 if (!isl_basic_map_divs_known(bmap))
12322 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12323 "some divs are unknown", return NULL);
12325 ls = isl_basic_map_get_local_space(bmap);
12326 div = isl_local_space_get_div(ls, pos);
12327 isl_local_space_free(ls);
12329 return div;
12332 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12333 int pos)
12335 return isl_basic_map_get_div(bset, pos);
12338 /* Plug in "subs" for dimension "type", "pos" of "bset".
12340 * Let i be the dimension to replace and let "subs" be of the form
12342 * f/d
12344 * Any integer division with a non-zero coefficient for i,
12346 * floor((a i + g)/m)
12348 * is replaced by
12350 * floor((a f + d g)/(m d))
12352 * Constraints of the form
12354 * a i + g
12356 * are replaced by
12358 * a f + d g
12360 * We currently require that "subs" is an integral expression.
12361 * Handling rational expressions may require us to add stride constraints
12362 * as we do in isl_basic_set_preimage_multi_aff.
12364 __isl_give isl_basic_set *isl_basic_set_substitute(
12365 __isl_take isl_basic_set *bset,
12366 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12368 int i;
12369 isl_int v;
12370 isl_ctx *ctx;
12372 if (bset && isl_basic_set_plain_is_empty(bset))
12373 return bset;
12375 bset = isl_basic_set_cow(bset);
12376 if (!bset || !subs)
12377 goto error;
12379 ctx = isl_basic_set_get_ctx(bset);
12380 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12381 isl_die(ctx, isl_error_invalid,
12382 "spaces don't match", goto error);
12383 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12384 isl_die(ctx, isl_error_unsupported,
12385 "cannot handle divs yet", goto error);
12386 if (!isl_int_is_one(subs->v->el[0]))
12387 isl_die(ctx, isl_error_invalid,
12388 "can only substitute integer expressions", goto error);
12390 pos += isl_basic_set_offset(bset, type);
12392 isl_int_init(v);
12394 for (i = 0; i < bset->n_eq; ++i) {
12395 if (isl_int_is_zero(bset->eq[i][pos]))
12396 continue;
12397 isl_int_set(v, bset->eq[i][pos]);
12398 isl_int_set_si(bset->eq[i][pos], 0);
12399 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12400 v, subs->v->el + 1, subs->v->size - 1);
12403 for (i = 0; i < bset->n_ineq; ++i) {
12404 if (isl_int_is_zero(bset->ineq[i][pos]))
12405 continue;
12406 isl_int_set(v, bset->ineq[i][pos]);
12407 isl_int_set_si(bset->ineq[i][pos], 0);
12408 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12409 v, subs->v->el + 1, subs->v->size - 1);
12412 for (i = 0; i < bset->n_div; ++i) {
12413 if (isl_int_is_zero(bset->div[i][1 + pos]))
12414 continue;
12415 isl_int_set(v, bset->div[i][1 + pos]);
12416 isl_int_set_si(bset->div[i][1 + pos], 0);
12417 isl_seq_combine(bset->div[i] + 1,
12418 subs->v->el[0], bset->div[i] + 1,
12419 v, subs->v->el + 1, subs->v->size - 1);
12420 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12423 isl_int_clear(v);
12425 bset = isl_basic_set_simplify(bset);
12426 return isl_basic_set_finalize(bset);
12427 error:
12428 isl_basic_set_free(bset);
12429 return NULL;
12432 /* Plug in "subs" for dimension "type", "pos" of "set".
12434 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12435 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12437 int i;
12439 if (set && isl_set_plain_is_empty(set))
12440 return set;
12442 set = isl_set_cow(set);
12443 if (!set || !subs)
12444 goto error;
12446 for (i = set->n - 1; i >= 0; --i) {
12447 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12448 if (remove_if_empty(set, i) < 0)
12449 goto error;
12452 return set;
12453 error:
12454 isl_set_free(set);
12455 return NULL;
12458 /* Check if the range of "ma" is compatible with the domain or range
12459 * (depending on "type") of "bmap".
12460 * Return -1 if anything is wrong.
12462 static int check_basic_map_compatible_range_multi_aff(
12463 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12464 __isl_keep isl_multi_aff *ma)
12466 int m;
12467 isl_space *ma_space;
12469 ma_space = isl_multi_aff_get_space(ma);
12471 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12472 if (m < 0)
12473 goto error;
12474 if (!m)
12475 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12476 "parameters don't match", goto error);
12477 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12478 if (m < 0)
12479 goto error;
12480 if (!m)
12481 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12482 "spaces don't match", goto error);
12484 isl_space_free(ma_space);
12485 return m;
12486 error:
12487 isl_space_free(ma_space);
12488 return -1;
12491 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12492 * coefficients before the transformed range of dimensions,
12493 * the "n_after" coefficients after the transformed range of dimensions
12494 * and the coefficients of the other divs in "bmap".
12496 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12497 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12499 int i;
12500 int n_param;
12501 int n_set;
12502 isl_local_space *ls;
12504 if (n_div == 0)
12505 return 0;
12507 ls = isl_aff_get_domain_local_space(ma->p[0]);
12508 if (!ls)
12509 return -1;
12511 n_param = isl_local_space_dim(ls, isl_dim_param);
12512 n_set = isl_local_space_dim(ls, isl_dim_set);
12513 for (i = 0; i < n_div; ++i) {
12514 int o_bmap = 0, o_ls = 0;
12516 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12517 o_bmap += 1 + 1 + n_param;
12518 o_ls += 1 + 1 + n_param;
12519 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12520 o_bmap += n_before;
12521 isl_seq_cpy(bmap->div[i] + o_bmap,
12522 ls->div->row[i] + o_ls, n_set);
12523 o_bmap += n_set;
12524 o_ls += n_set;
12525 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12526 o_bmap += n_after;
12527 isl_seq_cpy(bmap->div[i] + o_bmap,
12528 ls->div->row[i] + o_ls, n_div);
12529 o_bmap += n_div;
12530 o_ls += n_div;
12531 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12532 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12533 goto error;
12536 isl_local_space_free(ls);
12537 return 0;
12538 error:
12539 isl_local_space_free(ls);
12540 return -1;
12543 /* How many stride constraints does "ma" enforce?
12544 * That is, how many of the affine expressions have a denominator
12545 * different from one?
12547 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12549 int i;
12550 int strides = 0;
12552 for (i = 0; i < ma->n; ++i)
12553 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12554 strides++;
12556 return strides;
12559 /* For each affine expression in ma of the form
12561 * x_i = (f_i y + h_i)/m_i
12563 * with m_i different from one, add a constraint to "bmap"
12564 * of the form
12566 * f_i y + h_i = m_i alpha_i
12568 * with alpha_i an additional existentially quantified variable.
12570 * The input variables of "ma" correspond to a subset of the variables
12571 * of "bmap". There are "n_before" variables in "bmap" before this
12572 * subset and "n_after" variables after this subset.
12573 * The integer divisions of the affine expressions in "ma" are assumed
12574 * to have been aligned. There are "n_div_ma" of them and
12575 * they appear first in "bmap", straight after the "n_after" variables.
12577 static __isl_give isl_basic_map *add_ma_strides(
12578 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12579 int n_before, int n_after, int n_div_ma)
12581 int i, k;
12582 int div;
12583 int total;
12584 int n_param;
12585 int n_in;
12587 total = isl_basic_map_total_dim(bmap);
12588 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12589 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12590 for (i = 0; i < ma->n; ++i) {
12591 int o_bmap = 0, o_ma = 1;
12593 if (isl_int_is_one(ma->p[i]->v->el[0]))
12594 continue;
12595 div = isl_basic_map_alloc_div(bmap);
12596 k = isl_basic_map_alloc_equality(bmap);
12597 if (div < 0 || k < 0)
12598 goto error;
12599 isl_int_set_si(bmap->div[div][0], 0);
12600 isl_seq_cpy(bmap->eq[k] + o_bmap,
12601 ma->p[i]->v->el + o_ma, 1 + n_param);
12602 o_bmap += 1 + n_param;
12603 o_ma += 1 + n_param;
12604 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12605 o_bmap += n_before;
12606 isl_seq_cpy(bmap->eq[k] + o_bmap,
12607 ma->p[i]->v->el + o_ma, n_in);
12608 o_bmap += n_in;
12609 o_ma += n_in;
12610 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12611 o_bmap += n_after;
12612 isl_seq_cpy(bmap->eq[k] + o_bmap,
12613 ma->p[i]->v->el + o_ma, n_div_ma);
12614 o_bmap += n_div_ma;
12615 o_ma += n_div_ma;
12616 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12617 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12618 total++;
12621 return bmap;
12622 error:
12623 isl_basic_map_free(bmap);
12624 return NULL;
12627 /* Replace the domain or range space (depending on "type) of "space" by "set".
12629 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12630 enum isl_dim_type type, __isl_take isl_space *set)
12632 if (type == isl_dim_in) {
12633 space = isl_space_range(space);
12634 space = isl_space_map_from_domain_and_range(set, space);
12635 } else {
12636 space = isl_space_domain(space);
12637 space = isl_space_map_from_domain_and_range(space, set);
12640 return space;
12643 /* Compute the preimage of the domain or range (depending on "type")
12644 * of "bmap" under the function represented by "ma".
12645 * In other words, plug in "ma" in the domain or range of "bmap".
12646 * The result is a basic map that lives in the same space as "bmap"
12647 * except that the domain or range has been replaced by
12648 * the domain space of "ma".
12650 * If bmap is represented by
12652 * A(p) + S u + B x + T v + C(divs) >= 0,
12654 * where u and x are input and output dimensions if type == isl_dim_out
12655 * while x and v are input and output dimensions if type == isl_dim_in,
12656 * and ma is represented by
12658 * x = D(p) + F(y) + G(divs')
12660 * then the result is
12662 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12664 * The divs in the input set are similarly adjusted.
12665 * In particular
12667 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12669 * becomes
12671 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12672 * B_i G(divs') + c_i(divs))/n_i)
12674 * If bmap is not a rational map and if F(y) involves any denominators
12676 * x_i = (f_i y + h_i)/m_i
12678 * then additional constraints are added to ensure that we only
12679 * map back integer points. That is we enforce
12681 * f_i y + h_i = m_i alpha_i
12683 * with alpha_i an additional existentially quantified variable.
12685 * We first copy over the divs from "ma".
12686 * Then we add the modified constraints and divs from "bmap".
12687 * Finally, we add the stride constraints, if needed.
12689 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12690 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12691 __isl_take isl_multi_aff *ma)
12693 int i, k;
12694 isl_space *space;
12695 isl_basic_map *res = NULL;
12696 int n_before, n_after, n_div_bmap, n_div_ma;
12697 isl_int f, c1, c2, g;
12698 int rational, strides;
12700 isl_int_init(f);
12701 isl_int_init(c1);
12702 isl_int_init(c2);
12703 isl_int_init(g);
12705 ma = isl_multi_aff_align_divs(ma);
12706 if (!bmap || !ma)
12707 goto error;
12708 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12709 goto error;
12711 if (type == isl_dim_in) {
12712 n_before = 0;
12713 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12714 } else {
12715 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12716 n_after = 0;
12718 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12719 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12721 space = isl_multi_aff_get_domain_space(ma);
12722 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12723 rational = isl_basic_map_is_rational(bmap);
12724 strides = rational ? 0 : multi_aff_strides(ma);
12725 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12726 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12727 if (rational)
12728 res = isl_basic_map_set_rational(res);
12730 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12731 if (isl_basic_map_alloc_div(res) < 0)
12732 goto error;
12734 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12735 goto error;
12737 for (i = 0; i < bmap->n_eq; ++i) {
12738 k = isl_basic_map_alloc_equality(res);
12739 if (k < 0)
12740 goto error;
12741 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12742 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12745 for (i = 0; i < bmap->n_ineq; ++i) {
12746 k = isl_basic_map_alloc_inequality(res);
12747 if (k < 0)
12748 goto error;
12749 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12750 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12753 for (i = 0; i < bmap->n_div; ++i) {
12754 if (isl_int_is_zero(bmap->div[i][0])) {
12755 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12756 continue;
12758 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12759 n_before, n_after, n_div_ma, n_div_bmap,
12760 f, c1, c2, g, 1);
12763 if (strides)
12764 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12766 isl_int_clear(f);
12767 isl_int_clear(c1);
12768 isl_int_clear(c2);
12769 isl_int_clear(g);
12770 isl_basic_map_free(bmap);
12771 isl_multi_aff_free(ma);
12772 res = isl_basic_set_simplify(res);
12773 return isl_basic_map_finalize(res);
12774 error:
12775 isl_int_clear(f);
12776 isl_int_clear(c1);
12777 isl_int_clear(c2);
12778 isl_int_clear(g);
12779 isl_basic_map_free(bmap);
12780 isl_multi_aff_free(ma);
12781 isl_basic_map_free(res);
12782 return NULL;
12785 /* Compute the preimage of "bset" under the function represented by "ma".
12786 * In other words, plug in "ma" in "bset". The result is a basic set
12787 * that lives in the domain space of "ma".
12789 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12790 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12792 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12795 /* Compute the preimage of the domain of "bmap" under the function
12796 * represented by "ma".
12797 * In other words, plug in "ma" in the domain of "bmap".
12798 * The result is a basic map that lives in the same space as "bmap"
12799 * except that the domain has been replaced by the domain space of "ma".
12801 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12802 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12804 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12807 /* Compute the preimage of the range of "bmap" under the function
12808 * represented by "ma".
12809 * In other words, plug in "ma" in the range of "bmap".
12810 * The result is a basic map that lives in the same space as "bmap"
12811 * except that the range has been replaced by the domain space of "ma".
12813 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12814 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12816 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12819 /* Check if the range of "ma" is compatible with the domain or range
12820 * (depending on "type") of "map".
12821 * Return -1 if anything is wrong.
12823 static int check_map_compatible_range_multi_aff(
12824 __isl_keep isl_map *map, enum isl_dim_type type,
12825 __isl_keep isl_multi_aff *ma)
12827 int m;
12828 isl_space *ma_space;
12830 ma_space = isl_multi_aff_get_space(ma);
12831 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12832 isl_space_free(ma_space);
12833 if (m >= 0 && !m)
12834 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12835 "spaces don't match", return -1);
12836 return m;
12839 /* Compute the preimage of the domain or range (depending on "type")
12840 * of "map" under the function represented by "ma".
12841 * In other words, plug in "ma" in the domain or range of "map".
12842 * The result is a map that lives in the same space as "map"
12843 * except that the domain or range has been replaced by
12844 * the domain space of "ma".
12846 * The parameters are assumed to have been aligned.
12848 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12849 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12851 int i;
12852 isl_space *space;
12854 map = isl_map_cow(map);
12855 ma = isl_multi_aff_align_divs(ma);
12856 if (!map || !ma)
12857 goto error;
12858 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12859 goto error;
12861 for (i = 0; i < map->n; ++i) {
12862 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12863 isl_multi_aff_copy(ma));
12864 if (!map->p[i])
12865 goto error;
12868 space = isl_multi_aff_get_domain_space(ma);
12869 space = isl_space_set(isl_map_get_space(map), type, space);
12871 isl_space_free(map->dim);
12872 map->dim = space;
12873 if (!map->dim)
12874 goto error;
12876 isl_multi_aff_free(ma);
12877 if (map->n > 1)
12878 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12879 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12880 return map;
12881 error:
12882 isl_multi_aff_free(ma);
12883 isl_map_free(map);
12884 return NULL;
12887 /* Compute the preimage of the domain or range (depending on "type")
12888 * of "map" under the function represented by "ma".
12889 * In other words, plug in "ma" in the domain or range of "map".
12890 * The result is a map that lives in the same space as "map"
12891 * except that the domain or range has been replaced by
12892 * the domain space of "ma".
12894 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12895 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12897 if (!map || !ma)
12898 goto error;
12900 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12901 return map_preimage_multi_aff(map, type, ma);
12903 if (!isl_space_has_named_params(map->dim) ||
12904 !isl_space_has_named_params(ma->space))
12905 isl_die(map->ctx, isl_error_invalid,
12906 "unaligned unnamed parameters", goto error);
12907 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12908 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12910 return map_preimage_multi_aff(map, type, ma);
12911 error:
12912 isl_multi_aff_free(ma);
12913 return isl_map_free(map);
12916 /* Compute the preimage of "set" under the function represented by "ma".
12917 * In other words, plug in "ma" in "set". The result is a set
12918 * that lives in the domain space of "ma".
12920 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12921 __isl_take isl_multi_aff *ma)
12923 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12926 /* Compute the preimage of the domain of "map" under the function
12927 * represented by "ma".
12928 * In other words, plug in "ma" in the domain of "map".
12929 * The result is a map that lives in the same space as "map"
12930 * except that the domain has been replaced by the domain space of "ma".
12932 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12933 __isl_take isl_multi_aff *ma)
12935 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12938 /* Compute the preimage of the range of "map" under the function
12939 * represented by "ma".
12940 * In other words, plug in "ma" in the range of "map".
12941 * The result is a map that lives in the same space as "map"
12942 * except that the range has been replaced by the domain space of "ma".
12944 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12945 __isl_take isl_multi_aff *ma)
12947 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12950 /* Compute the preimage of "map" under the function represented by "pma".
12951 * In other words, plug in "pma" in the domain or range of "map".
12952 * The result is a map that lives in the same space as "map",
12953 * except that the space of type "type" has been replaced by
12954 * the domain space of "pma".
12956 * The parameters of "map" and "pma" are assumed to have been aligned.
12958 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12959 __isl_take isl_map *map, enum isl_dim_type type,
12960 __isl_take isl_pw_multi_aff *pma)
12962 int i;
12963 isl_map *res;
12965 if (!pma)
12966 goto error;
12968 if (pma->n == 0) {
12969 isl_pw_multi_aff_free(pma);
12970 res = isl_map_empty(isl_map_get_space(map));
12971 isl_map_free(map);
12972 return res;
12975 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12976 isl_multi_aff_copy(pma->p[0].maff));
12977 if (type == isl_dim_in)
12978 res = isl_map_intersect_domain(res,
12979 isl_map_copy(pma->p[0].set));
12980 else
12981 res = isl_map_intersect_range(res,
12982 isl_map_copy(pma->p[0].set));
12984 for (i = 1; i < pma->n; ++i) {
12985 isl_map *res_i;
12987 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12988 isl_multi_aff_copy(pma->p[i].maff));
12989 if (type == isl_dim_in)
12990 res_i = isl_map_intersect_domain(res_i,
12991 isl_map_copy(pma->p[i].set));
12992 else
12993 res_i = isl_map_intersect_range(res_i,
12994 isl_map_copy(pma->p[i].set));
12995 res = isl_map_union(res, res_i);
12998 isl_pw_multi_aff_free(pma);
12999 isl_map_free(map);
13000 return res;
13001 error:
13002 isl_pw_multi_aff_free(pma);
13003 isl_map_free(map);
13004 return NULL;
13007 /* Compute the preimage of "map" under the function represented by "pma".
13008 * In other words, plug in "pma" in the domain or range of "map".
13009 * The result is a map that lives in the same space as "map",
13010 * except that the space of type "type" has been replaced by
13011 * the domain space of "pma".
13013 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13014 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13016 if (!map || !pma)
13017 goto error;
13019 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13020 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13022 if (!isl_space_has_named_params(map->dim) ||
13023 !isl_space_has_named_params(pma->dim))
13024 isl_die(map->ctx, isl_error_invalid,
13025 "unaligned unnamed parameters", goto error);
13026 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13027 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13029 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13030 error:
13031 isl_pw_multi_aff_free(pma);
13032 return isl_map_free(map);
13035 /* Compute the preimage of "set" under the function represented by "pma".
13036 * In other words, plug in "pma" in "set". The result is a set
13037 * that lives in the domain space of "pma".
13039 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13040 __isl_take isl_pw_multi_aff *pma)
13042 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13045 /* Compute the preimage of the domain of "map" under the function
13046 * represented by "pma".
13047 * In other words, plug in "pma" in the domain of "map".
13048 * The result is a map that lives in the same space as "map",
13049 * except that domain space has been replaced by the domain space of "pma".
13051 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13052 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13054 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13057 /* Compute the preimage of the range of "map" under the function
13058 * represented by "pma".
13059 * In other words, plug in "pma" in the range of "map".
13060 * The result is a map that lives in the same space as "map",
13061 * except that range space has been replaced by the domain space of "pma".
13063 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13064 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13066 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13069 /* Compute the preimage of "map" under the function represented by "mpa".
13070 * In other words, plug in "mpa" in the domain or range of "map".
13071 * The result is a map that lives in the same space as "map",
13072 * except that the space of type "type" has been replaced by
13073 * the domain space of "mpa".
13075 * If the map does not involve any constraints that refer to the
13076 * dimensions of the substituted space, then the only possible
13077 * effect of "mpa" on the map is to map the space to a different space.
13078 * We create a separate isl_multi_aff to effectuate this change
13079 * in order to avoid spurious splitting of the map along the pieces
13080 * of "mpa".
13082 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13083 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13085 int n;
13086 isl_pw_multi_aff *pma;
13088 if (!map || !mpa)
13089 goto error;
13091 n = isl_map_dim(map, type);
13092 if (!isl_map_involves_dims(map, type, 0, n)) {
13093 isl_space *space;
13094 isl_multi_aff *ma;
13096 space = isl_multi_pw_aff_get_space(mpa);
13097 isl_multi_pw_aff_free(mpa);
13098 ma = isl_multi_aff_zero(space);
13099 return isl_map_preimage_multi_aff(map, type, ma);
13102 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13103 return isl_map_preimage_pw_multi_aff(map, type, pma);
13104 error:
13105 isl_map_free(map);
13106 isl_multi_pw_aff_free(mpa);
13107 return NULL;
13110 /* Compute the preimage of "map" under the function represented by "mpa".
13111 * In other words, plug in "mpa" in the domain "map".
13112 * The result is a map that lives in the same space as "map",
13113 * except that domain space has been replaced by the domain space of "mpa".
13115 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13116 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13118 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13121 /* Compute the preimage of "set" by the function represented by "mpa".
13122 * In other words, plug in "mpa" in "set".
13124 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13125 __isl_take isl_multi_pw_aff *mpa)
13127 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);