interface/python.cc: document order of superclasses
[isl.git] / isl_map.c
blob27eff8e76303133d20c5016934ba05a44e61226c
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 = bset_to_bmap(bset);
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 static __isl_give isl_basic_map *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_internal(bset->dim, dim),
4566 goto error);
4567 isl_space_free(bset->dim);
4568 bmap = bset_to_bmap(bset);
4569 bmap->dim = dim;
4570 return isl_basic_map_finalize(bmap);
4571 error:
4572 isl_basic_set_free(bset);
4573 isl_space_free(dim);
4574 return NULL;
4577 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4578 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
4580 return basic_map_from_basic_set(bset, space);
4583 /* For a div d = floor(f/m), add the constraint
4585 * f - m d >= 0
4587 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4588 unsigned pos, isl_int *div)
4590 int i;
4591 unsigned total = isl_basic_map_total_dim(bmap);
4593 i = isl_basic_map_alloc_inequality(bmap);
4594 if (i < 0)
4595 return -1;
4596 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4597 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4599 return 0;
4602 /* For a div d = floor(f/m), add the constraint
4604 * -(f-(m-1)) + m d >= 0
4606 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4607 unsigned pos, isl_int *div)
4609 int i;
4610 unsigned total = isl_basic_map_total_dim(bmap);
4612 i = isl_basic_map_alloc_inequality(bmap);
4613 if (i < 0)
4614 return -1;
4615 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4616 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4617 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4618 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4620 return 0;
4623 /* For a div d = floor(f/m), add the constraints
4625 * f - m d >= 0
4626 * -(f-(m-1)) + m d >= 0
4628 * Note that the second constraint is the negation of
4630 * f - m d >= m
4632 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4633 unsigned pos, isl_int *div)
4635 if (add_upper_div_constraint(bmap, pos, div) < 0)
4636 return -1;
4637 if (add_lower_div_constraint(bmap, pos, div) < 0)
4638 return -1;
4639 return 0;
4642 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4643 unsigned pos, isl_int *div)
4645 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4646 pos, div);
4649 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4651 unsigned total = isl_basic_map_total_dim(bmap);
4652 unsigned div_pos = total - bmap->n_div + div;
4654 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4655 bmap->div[div]);
4658 /* For each known div d = floor(f/m), add the constraints
4660 * f - m d >= 0
4661 * -(f-(m-1)) + m d >= 0
4663 * Remove duplicate constraints in case of some these div constraints
4664 * already appear in "bmap".
4666 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4667 __isl_take isl_basic_map *bmap)
4669 unsigned n_div;
4671 if (!bmap)
4672 return NULL;
4673 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4674 if (n_div == 0)
4675 return bmap;
4677 bmap = add_known_div_constraints(bmap);
4678 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4679 bmap = isl_basic_map_finalize(bmap);
4680 return bmap;
4683 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4685 * In particular, if this div is of the form d = floor(f/m),
4686 * then add the constraint
4688 * f - m d >= 0
4690 * if sign < 0 or the constraint
4692 * -(f-(m-1)) + m d >= 0
4694 * if sign > 0.
4696 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4697 unsigned div, int sign)
4699 unsigned total;
4700 unsigned div_pos;
4702 if (!bmap)
4703 return -1;
4705 total = isl_basic_map_total_dim(bmap);
4706 div_pos = total - bmap->n_div + div;
4708 if (sign < 0)
4709 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4710 else
4711 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4714 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4716 return isl_basic_map_add_div_constraints(bset, div);
4719 struct isl_basic_set *isl_basic_map_underlying_set(
4720 struct isl_basic_map *bmap)
4722 if (!bmap)
4723 goto error;
4724 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4725 bmap->n_div == 0 &&
4726 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4727 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4728 return bset_from_bmap(bmap);
4729 bmap = isl_basic_map_cow(bmap);
4730 if (!bmap)
4731 goto error;
4732 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4733 if (!bmap->dim)
4734 goto error;
4735 bmap->extra -= bmap->n_div;
4736 bmap->n_div = 0;
4737 bmap = isl_basic_map_finalize(bmap);
4738 return bset_from_bmap(bmap);
4739 error:
4740 isl_basic_map_free(bmap);
4741 return NULL;
4744 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4745 __isl_take isl_basic_set *bset)
4747 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4750 /* Replace each element in "list" by the result of applying
4751 * isl_basic_map_underlying_set to the element.
4753 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4754 __isl_take isl_basic_map_list *list)
4756 int i, n;
4758 if (!list)
4759 return NULL;
4761 n = isl_basic_map_list_n_basic_map(list);
4762 for (i = 0; i < n; ++i) {
4763 isl_basic_map *bmap;
4764 isl_basic_set *bset;
4766 bmap = isl_basic_map_list_get_basic_map(list, i);
4767 bset = isl_basic_set_underlying_set(bmap);
4768 list = isl_basic_set_list_set_basic_set(list, i, bset);
4771 return list;
4774 struct isl_basic_map *isl_basic_map_overlying_set(
4775 struct isl_basic_set *bset, struct isl_basic_map *like)
4777 struct isl_basic_map *bmap;
4778 struct isl_ctx *ctx;
4779 unsigned total;
4780 int i;
4782 if (!bset || !like)
4783 goto error;
4784 ctx = bset->ctx;
4785 isl_assert(ctx, bset->n_div == 0, goto error);
4786 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4787 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4788 goto error);
4789 if (like->n_div == 0) {
4790 isl_space *space = isl_basic_map_get_space(like);
4791 isl_basic_map_free(like);
4792 return isl_basic_map_reset_space(bset, space);
4794 bset = isl_basic_set_cow(bset);
4795 if (!bset)
4796 goto error;
4797 total = bset->dim->n_out + bset->extra;
4798 bmap = bset_to_bmap(bset);
4799 isl_space_free(bmap->dim);
4800 bmap->dim = isl_space_copy(like->dim);
4801 if (!bmap->dim)
4802 goto error;
4803 bmap->n_div = like->n_div;
4804 bmap->extra += like->n_div;
4805 if (bmap->extra) {
4806 unsigned ltotal;
4807 isl_int **div;
4808 ltotal = total - bmap->extra + like->extra;
4809 if (ltotal > total)
4810 ltotal = total;
4811 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4812 bmap->extra * (1 + 1 + total));
4813 if (isl_blk_is_error(bmap->block2))
4814 goto error;
4815 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4816 if (!div)
4817 goto error;
4818 bmap->div = div;
4819 for (i = 0; i < bmap->extra; ++i)
4820 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4821 for (i = 0; i < like->n_div; ++i) {
4822 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4823 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4825 bmap = isl_basic_map_add_known_div_constraints(bmap);
4827 isl_basic_map_free(like);
4828 bmap = isl_basic_map_simplify(bmap);
4829 bmap = isl_basic_map_finalize(bmap);
4830 return bmap;
4831 error:
4832 isl_basic_map_free(like);
4833 isl_basic_set_free(bset);
4834 return NULL;
4837 struct isl_basic_set *isl_basic_set_from_underlying_set(
4838 struct isl_basic_set *bset, struct isl_basic_set *like)
4840 return bset_from_bmap(isl_basic_map_overlying_set(bset,
4841 bset_to_bmap(like)));
4844 struct isl_set *isl_set_from_underlying_set(
4845 struct isl_set *set, struct isl_basic_set *like)
4847 int i;
4849 if (!set || !like)
4850 goto error;
4851 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4852 goto error);
4853 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4854 isl_basic_set_free(like);
4855 return set;
4857 set = isl_set_cow(set);
4858 if (!set)
4859 goto error;
4860 for (i = 0; i < set->n; ++i) {
4861 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4862 isl_basic_set_copy(like));
4863 if (!set->p[i])
4864 goto error;
4866 isl_space_free(set->dim);
4867 set->dim = isl_space_copy(like->dim);
4868 if (!set->dim)
4869 goto error;
4870 isl_basic_set_free(like);
4871 return set;
4872 error:
4873 isl_basic_set_free(like);
4874 isl_set_free(set);
4875 return NULL;
4878 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4880 int i;
4882 map = isl_map_cow(map);
4883 if (!map)
4884 return NULL;
4885 map->dim = isl_space_cow(map->dim);
4886 if (!map->dim)
4887 goto error;
4889 for (i = 1; i < map->n; ++i)
4890 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4891 goto error);
4892 for (i = 0; i < map->n; ++i) {
4893 map->p[i] = bset_to_bmap(
4894 isl_basic_map_underlying_set(map->p[i]));
4895 if (!map->p[i])
4896 goto error;
4898 if (map->n == 0)
4899 map->dim = isl_space_underlying(map->dim, 0);
4900 else {
4901 isl_space_free(map->dim);
4902 map->dim = isl_space_copy(map->p[0]->dim);
4904 if (!map->dim)
4905 goto error;
4906 return set_from_map(map);
4907 error:
4908 isl_map_free(map);
4909 return NULL;
4912 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4914 return set_from_map(isl_map_underlying_set(set_to_map(set)));
4917 /* Replace the space of "bmap" by "space".
4919 * If the space of "bmap" is identical to "space" (including the identifiers
4920 * of the input and output dimensions), then simply return the original input.
4922 __isl_give isl_basic_map *isl_basic_map_reset_space(
4923 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4925 isl_bool equal;
4927 if (!bmap)
4928 goto error;
4929 equal = isl_space_is_equal(bmap->dim, space);
4930 if (equal >= 0 && equal)
4931 equal = isl_space_match(bmap->dim, isl_dim_in,
4932 space, isl_dim_in);
4933 if (equal >= 0 && equal)
4934 equal = isl_space_match(bmap->dim, isl_dim_out,
4935 space, isl_dim_out);
4936 if (equal < 0)
4937 goto error;
4938 if (equal) {
4939 isl_space_free(space);
4940 return bmap;
4942 bmap = isl_basic_map_cow(bmap);
4943 if (!bmap || !space)
4944 goto error;
4946 isl_space_free(bmap->dim);
4947 bmap->dim = space;
4949 bmap = isl_basic_map_finalize(bmap);
4951 return bmap;
4952 error:
4953 isl_basic_map_free(bmap);
4954 isl_space_free(space);
4955 return NULL;
4958 __isl_give isl_basic_set *isl_basic_set_reset_space(
4959 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4961 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
4962 dim));
4965 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4966 __isl_take isl_space *dim)
4968 int i;
4970 map = isl_map_cow(map);
4971 if (!map || !dim)
4972 goto error;
4974 for (i = 0; i < map->n; ++i) {
4975 map->p[i] = isl_basic_map_reset_space(map->p[i],
4976 isl_space_copy(dim));
4977 if (!map->p[i])
4978 goto error;
4980 isl_space_free(map->dim);
4981 map->dim = dim;
4983 return map;
4984 error:
4985 isl_map_free(map);
4986 isl_space_free(dim);
4987 return NULL;
4990 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4991 __isl_take isl_space *dim)
4993 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
4996 /* Compute the parameter domain of the given basic set.
4998 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5000 isl_space *space;
5001 unsigned n;
5003 if (isl_basic_set_is_params(bset))
5004 return bset;
5006 n = isl_basic_set_dim(bset, isl_dim_set);
5007 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5008 space = isl_basic_set_get_space(bset);
5009 space = isl_space_params(space);
5010 bset = isl_basic_set_reset_space(bset, space);
5011 return bset;
5014 /* Construct a zero-dimensional basic set with the given parameter domain.
5016 __isl_give isl_basic_set *isl_basic_set_from_params(
5017 __isl_take isl_basic_set *bset)
5019 isl_space *space;
5020 space = isl_basic_set_get_space(bset);
5021 space = isl_space_set_from_params(space);
5022 bset = isl_basic_set_reset_space(bset, space);
5023 return bset;
5026 /* Compute the parameter domain of the given set.
5028 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5030 isl_space *space;
5031 unsigned n;
5033 if (isl_set_is_params(set))
5034 return set;
5036 n = isl_set_dim(set, isl_dim_set);
5037 set = isl_set_project_out(set, isl_dim_set, 0, n);
5038 space = isl_set_get_space(set);
5039 space = isl_space_params(space);
5040 set = isl_set_reset_space(set, space);
5041 return set;
5044 /* Construct a zero-dimensional set with the given parameter domain.
5046 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5048 isl_space *space;
5049 space = isl_set_get_space(set);
5050 space = isl_space_set_from_params(space);
5051 set = isl_set_reset_space(set, space);
5052 return set;
5055 /* Compute the parameter domain of the given map.
5057 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5059 isl_space *space;
5060 unsigned n;
5062 n = isl_map_dim(map, isl_dim_in);
5063 map = isl_map_project_out(map, isl_dim_in, 0, n);
5064 n = isl_map_dim(map, isl_dim_out);
5065 map = isl_map_project_out(map, isl_dim_out, 0, n);
5066 space = isl_map_get_space(map);
5067 space = isl_space_params(space);
5068 map = isl_map_reset_space(map, space);
5069 return map;
5072 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5074 isl_space *space;
5075 unsigned n_out;
5077 if (!bmap)
5078 return NULL;
5079 space = isl_space_domain(isl_basic_map_get_space(bmap));
5081 n_out = isl_basic_map_n_out(bmap);
5082 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5084 return isl_basic_map_reset_space(bmap, space);
5087 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5089 if (!bmap)
5090 return -1;
5091 return isl_space_may_be_set(bmap->dim);
5094 /* Is this basic map actually a set?
5095 * Users should never call this function. Outside of isl,
5096 * the type should indicate whether something is a set or a map.
5098 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5100 if (!bmap)
5101 return -1;
5102 return isl_space_is_set(bmap->dim);
5105 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5107 if (!bmap)
5108 return NULL;
5109 if (isl_basic_map_is_set(bmap))
5110 return bmap;
5111 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5114 __isl_give isl_basic_map *isl_basic_map_domain_map(
5115 __isl_take isl_basic_map *bmap)
5117 int i, k;
5118 isl_space *dim;
5119 isl_basic_map *domain;
5120 int nparam, n_in, n_out;
5121 unsigned total;
5123 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5124 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5125 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5127 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5128 domain = isl_basic_map_universe(dim);
5130 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5131 bmap = isl_basic_map_apply_range(bmap, domain);
5132 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5134 total = isl_basic_map_total_dim(bmap);
5136 for (i = 0; i < n_in; ++i) {
5137 k = isl_basic_map_alloc_equality(bmap);
5138 if (k < 0)
5139 goto error;
5140 isl_seq_clr(bmap->eq[k], 1 + total);
5141 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5142 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5145 bmap = isl_basic_map_gauss(bmap, NULL);
5146 return isl_basic_map_finalize(bmap);
5147 error:
5148 isl_basic_map_free(bmap);
5149 return NULL;
5152 __isl_give isl_basic_map *isl_basic_map_range_map(
5153 __isl_take isl_basic_map *bmap)
5155 int i, k;
5156 isl_space *dim;
5157 isl_basic_map *range;
5158 int nparam, n_in, n_out;
5159 unsigned total;
5161 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5162 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5163 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5165 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5166 range = isl_basic_map_universe(dim);
5168 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5169 bmap = isl_basic_map_apply_range(bmap, range);
5170 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5172 total = isl_basic_map_total_dim(bmap);
5174 for (i = 0; i < n_out; ++i) {
5175 k = isl_basic_map_alloc_equality(bmap);
5176 if (k < 0)
5177 goto error;
5178 isl_seq_clr(bmap->eq[k], 1 + total);
5179 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5180 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5183 bmap = isl_basic_map_gauss(bmap, NULL);
5184 return isl_basic_map_finalize(bmap);
5185 error:
5186 isl_basic_map_free(bmap);
5187 return NULL;
5190 int isl_map_may_be_set(__isl_keep isl_map *map)
5192 if (!map)
5193 return -1;
5194 return isl_space_may_be_set(map->dim);
5197 /* Is this map actually a set?
5198 * Users should never call this function. Outside of isl,
5199 * the type should indicate whether something is a set or a map.
5201 int isl_map_is_set(__isl_keep isl_map *map)
5203 if (!map)
5204 return -1;
5205 return isl_space_is_set(map->dim);
5208 struct isl_set *isl_map_range(struct isl_map *map)
5210 int i;
5211 struct isl_set *set;
5213 if (!map)
5214 goto error;
5215 if (isl_map_is_set(map))
5216 return set_from_map(map);
5218 map = isl_map_cow(map);
5219 if (!map)
5220 goto error;
5222 set = set_from_map(map);
5223 set->dim = isl_space_range(set->dim);
5224 if (!set->dim)
5225 goto error;
5226 for (i = 0; i < map->n; ++i) {
5227 set->p[i] = isl_basic_map_range(map->p[i]);
5228 if (!set->p[i])
5229 goto error;
5231 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5232 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5233 return set;
5234 error:
5235 isl_map_free(map);
5236 return NULL;
5239 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5241 int i;
5243 map = isl_map_cow(map);
5244 if (!map)
5245 return NULL;
5247 map->dim = isl_space_domain_map(map->dim);
5248 if (!map->dim)
5249 goto error;
5250 for (i = 0; i < map->n; ++i) {
5251 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5252 if (!map->p[i])
5253 goto error;
5255 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5256 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5257 return map;
5258 error:
5259 isl_map_free(map);
5260 return NULL;
5263 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5265 int i;
5266 isl_space *range_dim;
5268 map = isl_map_cow(map);
5269 if (!map)
5270 return NULL;
5272 range_dim = isl_space_range(isl_map_get_space(map));
5273 range_dim = isl_space_from_range(range_dim);
5274 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5275 map->dim = isl_space_join(map->dim, range_dim);
5276 if (!map->dim)
5277 goto error;
5278 for (i = 0; i < map->n; ++i) {
5279 map->p[i] = isl_basic_map_range_map(map->p[i]);
5280 if (!map->p[i])
5281 goto error;
5283 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5284 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5285 return map;
5286 error:
5287 isl_map_free(map);
5288 return NULL;
5291 /* Given a wrapped map of the form A[B -> C],
5292 * return the map A[B -> C] -> B.
5294 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5296 isl_id *id;
5297 isl_map *map;
5299 if (!set)
5300 return NULL;
5301 if (!isl_set_has_tuple_id(set))
5302 return isl_map_domain_map(isl_set_unwrap(set));
5304 id = isl_set_get_tuple_id(set);
5305 map = isl_map_domain_map(isl_set_unwrap(set));
5306 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5308 return map;
5311 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5312 __isl_take isl_space *dim)
5314 int i;
5315 struct isl_map *map = NULL;
5317 set = isl_set_cow(set);
5318 if (!set || !dim)
5319 goto error;
5320 isl_assert(set->ctx, isl_space_compatible_internal(set->dim, dim),
5321 goto error);
5322 map = set_to_map(set);
5323 for (i = 0; i < set->n; ++i) {
5324 map->p[i] = basic_map_from_basic_set(
5325 set->p[i], isl_space_copy(dim));
5326 if (!map->p[i])
5327 goto error;
5329 isl_space_free(map->dim);
5330 map->dim = dim;
5331 return map;
5332 error:
5333 isl_space_free(dim);
5334 isl_set_free(set);
5335 return NULL;
5338 __isl_give isl_basic_map *isl_basic_map_from_domain(
5339 __isl_take isl_basic_set *bset)
5341 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5344 __isl_give isl_basic_map *isl_basic_map_from_range(
5345 __isl_take isl_basic_set *bset)
5347 isl_space *space;
5348 space = isl_basic_set_get_space(bset);
5349 space = isl_space_from_range(space);
5350 bset = isl_basic_set_reset_space(bset, space);
5351 return bset_to_bmap(bset);
5354 /* Create a relation with the given set as range.
5355 * The domain of the created relation is a zero-dimensional
5356 * flat anonymous space.
5358 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5360 isl_space *space;
5361 space = isl_set_get_space(set);
5362 space = isl_space_from_range(space);
5363 set = isl_set_reset_space(set, space);
5364 return set_to_map(set);
5367 /* Create a relation with the given set as domain.
5368 * The range of the created relation is a zero-dimensional
5369 * flat anonymous space.
5371 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5373 return isl_map_reverse(isl_map_from_range(set));
5376 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5377 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5379 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5382 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5383 __isl_take isl_set *range)
5385 return isl_map_apply_range(isl_map_reverse(domain), range);
5388 /* Return a newly allocated isl_map with given space and flags and
5389 * room for "n" basic maps.
5390 * Make sure that all cached information is cleared.
5392 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5393 unsigned flags)
5395 struct isl_map *map;
5397 if (!space)
5398 return NULL;
5399 if (n < 0)
5400 isl_die(space->ctx, isl_error_internal,
5401 "negative number of basic maps", goto error);
5402 map = isl_calloc(space->ctx, struct isl_map,
5403 sizeof(struct isl_map) +
5404 (n - 1) * sizeof(struct isl_basic_map *));
5405 if (!map)
5406 goto error;
5408 map->ctx = space->ctx;
5409 isl_ctx_ref(map->ctx);
5410 map->ref = 1;
5411 map->size = n;
5412 map->n = 0;
5413 map->dim = space;
5414 map->flags = flags;
5415 return map;
5416 error:
5417 isl_space_free(space);
5418 return NULL;
5421 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5422 unsigned nparam, unsigned in, unsigned out, int n,
5423 unsigned flags)
5425 struct isl_map *map;
5426 isl_space *dims;
5428 dims = isl_space_alloc(ctx, nparam, in, out);
5429 if (!dims)
5430 return NULL;
5432 map = isl_map_alloc_space(dims, n, flags);
5433 return map;
5436 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5438 struct isl_basic_map *bmap;
5439 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5440 bmap = isl_basic_map_set_to_empty(bmap);
5441 return bmap;
5444 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5446 struct isl_basic_set *bset;
5447 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5448 bset = isl_basic_set_set_to_empty(bset);
5449 return bset;
5452 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5454 struct isl_basic_map *bmap;
5455 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5456 bmap = isl_basic_map_finalize(bmap);
5457 return bmap;
5460 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5462 struct isl_basic_set *bset;
5463 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5464 bset = isl_basic_set_finalize(bset);
5465 return bset;
5468 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5470 int i;
5471 unsigned total = isl_space_dim(dim, isl_dim_all);
5472 isl_basic_map *bmap;
5474 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5475 for (i = 0; i < total; ++i) {
5476 int k = isl_basic_map_alloc_inequality(bmap);
5477 if (k < 0)
5478 goto error;
5479 isl_seq_clr(bmap->ineq[k], 1 + total);
5480 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5482 return bmap;
5483 error:
5484 isl_basic_map_free(bmap);
5485 return NULL;
5488 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5490 return isl_basic_map_nat_universe(dim);
5493 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5495 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5498 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5500 return isl_map_nat_universe(dim);
5503 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5505 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5508 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5510 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5513 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5515 struct isl_map *map;
5516 if (!dim)
5517 return NULL;
5518 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5519 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5520 return map;
5523 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5525 struct isl_set *set;
5526 if (!dim)
5527 return NULL;
5528 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5529 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5530 return set;
5533 struct isl_map *isl_map_dup(struct isl_map *map)
5535 int i;
5536 struct isl_map *dup;
5538 if (!map)
5539 return NULL;
5540 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5541 for (i = 0; i < map->n; ++i)
5542 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5543 return dup;
5546 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5547 __isl_take isl_basic_map *bmap)
5549 if (!bmap || !map)
5550 goto error;
5551 if (isl_basic_map_plain_is_empty(bmap)) {
5552 isl_basic_map_free(bmap);
5553 return map;
5555 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5556 isl_assert(map->ctx, map->n < map->size, goto error);
5557 map->p[map->n] = bmap;
5558 map->n++;
5559 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5560 return map;
5561 error:
5562 if (map)
5563 isl_map_free(map);
5564 if (bmap)
5565 isl_basic_map_free(bmap);
5566 return NULL;
5569 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5571 int i;
5573 if (!map)
5574 return NULL;
5576 if (--map->ref > 0)
5577 return NULL;
5579 clear_caches(map);
5580 isl_ctx_deref(map->ctx);
5581 for (i = 0; i < map->n; ++i)
5582 isl_basic_map_free(map->p[i]);
5583 isl_space_free(map->dim);
5584 free(map);
5586 return NULL;
5589 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5590 struct isl_basic_map *bmap, unsigned pos, int value)
5592 int j;
5594 bmap = isl_basic_map_cow(bmap);
5595 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5596 j = isl_basic_map_alloc_equality(bmap);
5597 if (j < 0)
5598 goto error;
5599 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5600 isl_int_set_si(bmap->eq[j][pos], -1);
5601 isl_int_set_si(bmap->eq[j][0], value);
5602 bmap = isl_basic_map_simplify(bmap);
5603 return isl_basic_map_finalize(bmap);
5604 error:
5605 isl_basic_map_free(bmap);
5606 return NULL;
5609 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5610 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5612 int j;
5614 bmap = isl_basic_map_cow(bmap);
5615 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5616 j = isl_basic_map_alloc_equality(bmap);
5617 if (j < 0)
5618 goto error;
5619 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5620 isl_int_set_si(bmap->eq[j][pos], -1);
5621 isl_int_set(bmap->eq[j][0], value);
5622 bmap = isl_basic_map_simplify(bmap);
5623 return isl_basic_map_finalize(bmap);
5624 error:
5625 isl_basic_map_free(bmap);
5626 return NULL;
5629 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5630 enum isl_dim_type type, unsigned pos, int value)
5632 if (!bmap)
5633 return NULL;
5634 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5635 return isl_basic_map_fix_pos_si(bmap,
5636 isl_basic_map_offset(bmap, type) + pos, value);
5637 error:
5638 isl_basic_map_free(bmap);
5639 return NULL;
5642 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5643 enum isl_dim_type type, unsigned pos, isl_int value)
5645 if (!bmap)
5646 return NULL;
5647 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5648 return isl_basic_map_fix_pos(bmap,
5649 isl_basic_map_offset(bmap, type) + pos, value);
5650 error:
5651 isl_basic_map_free(bmap);
5652 return NULL;
5655 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5656 * to be equal to "v".
5658 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5659 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5661 if (!bmap || !v)
5662 goto error;
5663 if (!isl_val_is_int(v))
5664 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5665 "expecting integer value", goto error);
5666 if (pos >= isl_basic_map_dim(bmap, type))
5667 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5668 "index out of bounds", goto error);
5669 pos += isl_basic_map_offset(bmap, type);
5670 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5671 isl_val_free(v);
5672 return bmap;
5673 error:
5674 isl_basic_map_free(bmap);
5675 isl_val_free(v);
5676 return NULL;
5679 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5680 * to be equal to "v".
5682 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5683 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5685 return isl_basic_map_fix_val(bset, type, pos, v);
5688 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5689 enum isl_dim_type type, unsigned pos, int value)
5691 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5692 type, pos, value));
5695 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5696 enum isl_dim_type type, unsigned pos, isl_int value)
5698 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5699 type, pos, value));
5702 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5703 unsigned input, int value)
5705 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5708 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5709 unsigned dim, int value)
5711 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5712 isl_dim_set, dim, value));
5715 static int remove_if_empty(__isl_keep isl_map *map, int i)
5717 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5719 if (empty < 0)
5720 return -1;
5721 if (!empty)
5722 return 0;
5724 isl_basic_map_free(map->p[i]);
5725 if (i != map->n - 1) {
5726 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5727 map->p[i] = map->p[map->n - 1];
5729 map->n--;
5731 return 0;
5734 /* Perform "fn" on each basic map of "map", where we may not be holding
5735 * the only reference to "map".
5736 * In particular, "fn" should be a semantics preserving operation
5737 * that we want to apply to all copies of "map". We therefore need
5738 * to be careful not to modify "map" in a way that breaks "map"
5739 * in case anything goes wrong.
5741 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5742 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5744 struct isl_basic_map *bmap;
5745 int i;
5747 if (!map)
5748 return NULL;
5750 for (i = map->n - 1; i >= 0; --i) {
5751 bmap = isl_basic_map_copy(map->p[i]);
5752 bmap = fn(bmap);
5753 if (!bmap)
5754 goto error;
5755 isl_basic_map_free(map->p[i]);
5756 map->p[i] = bmap;
5757 if (remove_if_empty(map, i) < 0)
5758 goto error;
5761 return map;
5762 error:
5763 isl_map_free(map);
5764 return NULL;
5767 struct isl_map *isl_map_fix_si(struct isl_map *map,
5768 enum isl_dim_type type, unsigned pos, int value)
5770 int i;
5772 map = isl_map_cow(map);
5773 if (!map)
5774 return NULL;
5776 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5777 for (i = map->n - 1; i >= 0; --i) {
5778 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5779 if (remove_if_empty(map, i) < 0)
5780 goto error;
5782 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5783 return map;
5784 error:
5785 isl_map_free(map);
5786 return NULL;
5789 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5790 enum isl_dim_type type, unsigned pos, int value)
5792 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
5795 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5796 enum isl_dim_type type, unsigned pos, isl_int value)
5798 int i;
5800 map = isl_map_cow(map);
5801 if (!map)
5802 return NULL;
5804 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5805 for (i = 0; i < map->n; ++i) {
5806 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5807 if (!map->p[i])
5808 goto error;
5810 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5811 return map;
5812 error:
5813 isl_map_free(map);
5814 return NULL;
5817 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5818 enum isl_dim_type type, unsigned pos, isl_int value)
5820 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
5823 /* Fix the value of the variable at position "pos" of type "type" of "map"
5824 * to be equal to "v".
5826 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5827 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5829 int i;
5831 map = isl_map_cow(map);
5832 if (!map || !v)
5833 goto error;
5835 if (!isl_val_is_int(v))
5836 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5837 "expecting integer value", goto error);
5838 if (pos >= isl_map_dim(map, type))
5839 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5840 "index out of bounds", goto error);
5841 for (i = map->n - 1; i >= 0; --i) {
5842 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5843 isl_val_copy(v));
5844 if (remove_if_empty(map, i) < 0)
5845 goto error;
5847 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5848 isl_val_free(v);
5849 return map;
5850 error:
5851 isl_map_free(map);
5852 isl_val_free(v);
5853 return NULL;
5856 /* Fix the value of the variable at position "pos" of type "type" of "set"
5857 * to be equal to "v".
5859 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5860 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5862 return isl_map_fix_val(set, type, pos, v);
5865 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5866 unsigned input, int value)
5868 return isl_map_fix_si(map, isl_dim_in, input, value);
5871 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5873 return set_from_map(isl_map_fix_si(set_to_map(set),
5874 isl_dim_set, dim, value));
5877 static __isl_give isl_basic_map *basic_map_bound_si(
5878 __isl_take isl_basic_map *bmap,
5879 enum isl_dim_type type, unsigned pos, int value, int upper)
5881 int j;
5883 if (!bmap)
5884 return NULL;
5885 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5886 pos += isl_basic_map_offset(bmap, type);
5887 bmap = isl_basic_map_cow(bmap);
5888 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5889 j = isl_basic_map_alloc_inequality(bmap);
5890 if (j < 0)
5891 goto error;
5892 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5893 if (upper) {
5894 isl_int_set_si(bmap->ineq[j][pos], -1);
5895 isl_int_set_si(bmap->ineq[j][0], value);
5896 } else {
5897 isl_int_set_si(bmap->ineq[j][pos], 1);
5898 isl_int_set_si(bmap->ineq[j][0], -value);
5900 bmap = isl_basic_map_simplify(bmap);
5901 return isl_basic_map_finalize(bmap);
5902 error:
5903 isl_basic_map_free(bmap);
5904 return NULL;
5907 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5908 __isl_take isl_basic_map *bmap,
5909 enum isl_dim_type type, unsigned pos, int value)
5911 return basic_map_bound_si(bmap, type, pos, value, 0);
5914 /* Constrain the values of the given dimension to be no greater than "value".
5916 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5917 __isl_take isl_basic_map *bmap,
5918 enum isl_dim_type type, unsigned pos, int value)
5920 return basic_map_bound_si(bmap, type, pos, value, 1);
5923 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5924 unsigned dim, isl_int value)
5926 int j;
5928 bset = isl_basic_set_cow(bset);
5929 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5930 j = isl_basic_set_alloc_inequality(bset);
5931 if (j < 0)
5932 goto error;
5933 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5934 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5935 isl_int_neg(bset->ineq[j][0], value);
5936 bset = isl_basic_set_simplify(bset);
5937 return isl_basic_set_finalize(bset);
5938 error:
5939 isl_basic_set_free(bset);
5940 return NULL;
5943 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5944 enum isl_dim_type type, unsigned pos, int value, int upper)
5946 int i;
5948 map = isl_map_cow(map);
5949 if (!map)
5950 return NULL;
5952 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5953 for (i = 0; i < map->n; ++i) {
5954 map->p[i] = basic_map_bound_si(map->p[i],
5955 type, pos, value, upper);
5956 if (!map->p[i])
5957 goto error;
5959 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5960 return map;
5961 error:
5962 isl_map_free(map);
5963 return NULL;
5966 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5967 enum isl_dim_type type, unsigned pos, int value)
5969 return map_bound_si(map, type, pos, value, 0);
5972 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5973 enum isl_dim_type type, unsigned pos, int value)
5975 return map_bound_si(map, type, pos, value, 1);
5978 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5979 enum isl_dim_type type, unsigned pos, int value)
5981 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
5982 type, pos, value));
5985 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5986 enum isl_dim_type type, unsigned pos, int value)
5988 return isl_map_upper_bound_si(set, type, pos, value);
5991 /* Bound the given variable of "bmap" from below (or above is "upper"
5992 * is set) to "value".
5994 static __isl_give isl_basic_map *basic_map_bound(
5995 __isl_take isl_basic_map *bmap,
5996 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5998 int j;
6000 if (!bmap)
6001 return NULL;
6002 if (pos >= isl_basic_map_dim(bmap, type))
6003 isl_die(bmap->ctx, isl_error_invalid,
6004 "index out of bounds", goto error);
6005 pos += isl_basic_map_offset(bmap, type);
6006 bmap = isl_basic_map_cow(bmap);
6007 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6008 j = isl_basic_map_alloc_inequality(bmap);
6009 if (j < 0)
6010 goto error;
6011 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6012 if (upper) {
6013 isl_int_set_si(bmap->ineq[j][pos], -1);
6014 isl_int_set(bmap->ineq[j][0], value);
6015 } else {
6016 isl_int_set_si(bmap->ineq[j][pos], 1);
6017 isl_int_neg(bmap->ineq[j][0], value);
6019 bmap = isl_basic_map_simplify(bmap);
6020 return isl_basic_map_finalize(bmap);
6021 error:
6022 isl_basic_map_free(bmap);
6023 return NULL;
6026 /* Bound the given variable of "map" from below (or above is "upper"
6027 * is set) to "value".
6029 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6030 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6032 int i;
6034 map = isl_map_cow(map);
6035 if (!map)
6036 return NULL;
6038 if (pos >= isl_map_dim(map, type))
6039 isl_die(map->ctx, isl_error_invalid,
6040 "index out of bounds", goto error);
6041 for (i = map->n - 1; i >= 0; --i) {
6042 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6043 if (remove_if_empty(map, i) < 0)
6044 goto error;
6046 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6047 return map;
6048 error:
6049 isl_map_free(map);
6050 return NULL;
6053 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6054 enum isl_dim_type type, unsigned pos, isl_int value)
6056 return map_bound(map, type, pos, value, 0);
6059 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6060 enum isl_dim_type type, unsigned pos, isl_int value)
6062 return map_bound(map, type, pos, value, 1);
6065 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6066 enum isl_dim_type type, unsigned pos, isl_int value)
6068 return isl_map_lower_bound(set, type, pos, value);
6071 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6072 enum isl_dim_type type, unsigned pos, isl_int value)
6074 return isl_map_upper_bound(set, type, pos, value);
6077 /* Force the values of the variable at position "pos" of type "type" of "set"
6078 * to be no smaller than "value".
6080 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6081 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6083 if (!value)
6084 goto error;
6085 if (!isl_val_is_int(value))
6086 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6087 "expecting integer value", goto error);
6088 set = isl_set_lower_bound(set, type, pos, value->n);
6089 isl_val_free(value);
6090 return set;
6091 error:
6092 isl_val_free(value);
6093 isl_set_free(set);
6094 return NULL;
6097 /* Force the values of the variable at position "pos" of type "type" of "set"
6098 * to be no greater than "value".
6100 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6101 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6103 if (!value)
6104 goto error;
6105 if (!isl_val_is_int(value))
6106 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6107 "expecting integer value", goto error);
6108 set = isl_set_upper_bound(set, type, pos, value->n);
6109 isl_val_free(value);
6110 return set;
6111 error:
6112 isl_val_free(value);
6113 isl_set_free(set);
6114 return NULL;
6117 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6118 isl_int value)
6120 int i;
6122 set = isl_set_cow(set);
6123 if (!set)
6124 return NULL;
6126 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6127 for (i = 0; i < set->n; ++i) {
6128 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6129 if (!set->p[i])
6130 goto error;
6132 return set;
6133 error:
6134 isl_set_free(set);
6135 return NULL;
6138 struct isl_map *isl_map_reverse(struct isl_map *map)
6140 int i;
6142 map = isl_map_cow(map);
6143 if (!map)
6144 return NULL;
6146 map->dim = isl_space_reverse(map->dim);
6147 if (!map->dim)
6148 goto error;
6149 for (i = 0; i < map->n; ++i) {
6150 map->p[i] = isl_basic_map_reverse(map->p[i]);
6151 if (!map->p[i])
6152 goto error;
6154 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6155 return map;
6156 error:
6157 isl_map_free(map);
6158 return NULL;
6161 #undef TYPE
6162 #define TYPE isl_pw_multi_aff
6163 #undef SUFFIX
6164 #define SUFFIX _pw_multi_aff
6165 #undef EMPTY
6166 #define EMPTY isl_pw_multi_aff_empty
6167 #undef ADD
6168 #define ADD isl_pw_multi_aff_union_add
6169 #include "isl_map_lexopt_templ.c"
6171 /* Given a map "map", compute the lexicographically minimal
6172 * (or maximal) image element for each domain element in dom,
6173 * in the form of an isl_pw_multi_aff.
6174 * If "empty" is not NULL, then set *empty to those elements in dom that
6175 * do not have an image element.
6176 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6177 * should be computed over the domain of "map". "empty" is also NULL
6178 * in this case.
6180 * We first compute the lexicographically minimal or maximal element
6181 * in the first basic map. This results in a partial solution "res"
6182 * and a subset "todo" of dom that still need to be handled.
6183 * We then consider each of the remaining maps in "map" and successively
6184 * update both "res" and "todo".
6185 * If "empty" is NULL, then the todo sets are not needed and therefore
6186 * also not computed.
6188 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6189 __isl_take isl_map *map, __isl_take isl_set *dom,
6190 __isl_give isl_set **empty, unsigned flags)
6192 int i;
6193 int full;
6194 isl_pw_multi_aff *res;
6195 isl_set *todo;
6197 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6198 if (!map || (!full && !dom))
6199 goto error;
6201 if (isl_map_plain_is_empty(map)) {
6202 if (empty)
6203 *empty = dom;
6204 else
6205 isl_set_free(dom);
6206 return isl_pw_multi_aff_from_map(map);
6209 res = basic_map_partial_lexopt_pw_multi_aff(
6210 isl_basic_map_copy(map->p[0]),
6211 isl_set_copy(dom), empty, flags);
6213 if (empty)
6214 todo = *empty;
6215 for (i = 1; i < map->n; ++i) {
6216 isl_pw_multi_aff *res_i;
6218 res_i = basic_map_partial_lexopt_pw_multi_aff(
6219 isl_basic_map_copy(map->p[i]),
6220 isl_set_copy(dom), empty, flags);
6222 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6223 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6224 else
6225 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6227 if (empty)
6228 todo = isl_set_intersect(todo, *empty);
6231 isl_set_free(dom);
6232 isl_map_free(map);
6234 if (empty)
6235 *empty = todo;
6237 return res;
6238 error:
6239 if (empty)
6240 *empty = NULL;
6241 isl_set_free(dom);
6242 isl_map_free(map);
6243 return NULL;
6246 #undef TYPE
6247 #define TYPE isl_map
6248 #undef SUFFIX
6249 #define SUFFIX
6250 #undef EMPTY
6251 #define EMPTY isl_map_empty
6252 #undef ADD
6253 #define ADD isl_map_union_disjoint
6254 #include "isl_map_lexopt_templ.c"
6256 /* Given a map "map", compute the lexicographically minimal
6257 * (or maximal) image element for each domain element in "dom",
6258 * in the form of an isl_map.
6259 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6260 * do not have an image element.
6261 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6262 * should be computed over the domain of "map". "empty" is also NULL
6263 * in this case.
6265 * If the input consists of more than one disjunct, then first
6266 * compute the desired result in the form of an isl_pw_multi_aff and
6267 * then convert that into an isl_map.
6269 * This function used to have an explicit implementation in terms
6270 * of isl_maps, but it would continually intersect the domains of
6271 * partial results with the complement of the domain of the next
6272 * partial solution, potentially leading to an explosion in the number
6273 * of disjuncts if there are several disjuncts in the input.
6274 * An even earlier implementation of this function would look for
6275 * better results in the domain of the partial result and for extra
6276 * results in the complement of this domain, which would lead to
6277 * even more splintering.
6279 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6280 __isl_take isl_map *map, __isl_take isl_set *dom,
6281 __isl_give isl_set **empty, unsigned flags)
6283 int full;
6284 struct isl_map *res;
6285 isl_pw_multi_aff *pma;
6287 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6288 if (!map || (!full && !dom))
6289 goto error;
6291 if (isl_map_plain_is_empty(map)) {
6292 if (empty)
6293 *empty = dom;
6294 else
6295 isl_set_free(dom);
6296 return map;
6299 if (map->n == 1) {
6300 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6301 dom, empty, flags);
6302 isl_map_free(map);
6303 return res;
6306 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6307 flags);
6308 return isl_map_from_pw_multi_aff(pma);
6309 error:
6310 if (empty)
6311 *empty = NULL;
6312 isl_set_free(dom);
6313 isl_map_free(map);
6314 return NULL;
6317 __isl_give isl_map *isl_map_partial_lexmax(
6318 __isl_take isl_map *map, __isl_take isl_set *dom,
6319 __isl_give isl_set **empty)
6321 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6324 __isl_give isl_map *isl_map_partial_lexmin(
6325 __isl_take isl_map *map, __isl_take isl_set *dom,
6326 __isl_give isl_set **empty)
6328 return isl_map_partial_lexopt(map, dom, empty, 0);
6331 __isl_give isl_set *isl_set_partial_lexmin(
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_lexmin(set_to_map(set),
6336 dom, empty));
6339 __isl_give isl_set *isl_set_partial_lexmax(
6340 __isl_take isl_set *set, __isl_take isl_set *dom,
6341 __isl_give isl_set **empty)
6343 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6344 dom, empty));
6347 /* Compute the lexicographic minimum (or maximum if "flags" includes
6348 * ISL_OPT_MAX) of "bset" over its parametric domain.
6350 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6351 unsigned flags)
6353 return isl_basic_map_lexopt(bset, flags);
6356 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6358 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6361 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6363 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6366 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6368 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6371 /* Compute the lexicographic minimum of "bset" over its parametric domain
6372 * for the purpose of quantifier elimination.
6373 * That is, find an explicit representation for all the existentially
6374 * quantified variables in "bset" by computing their lexicographic
6375 * minimum.
6377 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6378 __isl_take isl_basic_set *bset)
6380 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6383 /* Extract the first and only affine expression from list
6384 * and then add it to *pwaff with the given dom.
6385 * This domain is known to be disjoint from other domains
6386 * because of the way isl_basic_map_foreach_lexmax works.
6388 static int update_dim_opt(__isl_take isl_basic_set *dom,
6389 __isl_take isl_aff_list *list, void *user)
6391 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6392 isl_aff *aff;
6393 isl_pw_aff **pwaff = user;
6394 isl_pw_aff *pwaff_i;
6396 if (!list)
6397 goto error;
6398 if (isl_aff_list_n_aff(list) != 1)
6399 isl_die(ctx, isl_error_internal,
6400 "expecting single element list", goto error);
6402 aff = isl_aff_list_get_aff(list, 0);
6403 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6405 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6407 isl_aff_list_free(list);
6409 return 0;
6410 error:
6411 isl_basic_set_free(dom);
6412 isl_aff_list_free(list);
6413 return -1;
6416 /* Given a basic map with one output dimension, compute the minimum or
6417 * maximum of that dimension as an isl_pw_aff.
6419 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6420 * call update_dim_opt on each leaf of the result.
6422 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6423 int max)
6425 isl_space *dim = isl_basic_map_get_space(bmap);
6426 isl_pw_aff *pwaff;
6427 int r;
6429 dim = isl_space_from_domain(isl_space_domain(dim));
6430 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6431 pwaff = isl_pw_aff_empty(dim);
6433 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6434 if (r < 0)
6435 return isl_pw_aff_free(pwaff);
6437 return pwaff;
6440 /* Compute the minimum or maximum of the given output dimension
6441 * as a function of the parameters and the input dimensions,
6442 * but independently of the other output dimensions.
6444 * We first project out the other output dimension and then compute
6445 * the "lexicographic" maximum in each basic map, combining the results
6446 * using isl_pw_aff_union_max.
6448 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6449 int max)
6451 int i;
6452 isl_pw_aff *pwaff;
6453 unsigned n_out;
6455 n_out = isl_map_dim(map, isl_dim_out);
6456 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6457 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6458 if (!map)
6459 return NULL;
6461 if (map->n == 0) {
6462 isl_space *dim = isl_map_get_space(map);
6463 isl_map_free(map);
6464 return isl_pw_aff_empty(dim);
6467 pwaff = basic_map_dim_opt(map->p[0], max);
6468 for (i = 1; i < map->n; ++i) {
6469 isl_pw_aff *pwaff_i;
6471 pwaff_i = basic_map_dim_opt(map->p[i], max);
6472 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6475 isl_map_free(map);
6477 return pwaff;
6480 /* Compute the minimum of the given output dimension as a function of the
6481 * parameters and input dimensions, but independently of
6482 * the other output dimensions.
6484 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6486 return map_dim_opt(map, pos, 0);
6489 /* Compute the maximum of the given output dimension as a function of the
6490 * parameters and input dimensions, but independently of
6491 * the other output dimensions.
6493 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6495 return map_dim_opt(map, pos, 1);
6498 /* Compute the minimum or maximum of the given set dimension
6499 * as a function of the parameters,
6500 * but independently of the other set dimensions.
6502 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6503 int max)
6505 return map_dim_opt(set, pos, max);
6508 /* Compute the maximum 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_max(__isl_take isl_set *set, int pos)
6513 return set_dim_opt(set, pos, 1);
6516 /* Compute the minimum of the given set dimension as a function of the
6517 * parameters, but independently of the other set dimensions.
6519 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6521 return set_dim_opt(set, pos, 0);
6524 /* Apply a preimage specified by "mat" on the parameters of "bset".
6525 * bset is assumed to have only parameters and divs.
6527 static struct isl_basic_set *basic_set_parameter_preimage(
6528 struct isl_basic_set *bset, struct isl_mat *mat)
6530 unsigned nparam;
6532 if (!bset || !mat)
6533 goto error;
6535 bset->dim = isl_space_cow(bset->dim);
6536 if (!bset->dim)
6537 goto error;
6539 nparam = isl_basic_set_dim(bset, isl_dim_param);
6541 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6543 bset->dim->nparam = 0;
6544 bset->dim->n_out = nparam;
6545 bset = isl_basic_set_preimage(bset, mat);
6546 if (bset) {
6547 bset->dim->nparam = bset->dim->n_out;
6548 bset->dim->n_out = 0;
6550 return bset;
6551 error:
6552 isl_mat_free(mat);
6553 isl_basic_set_free(bset);
6554 return NULL;
6557 /* Apply a preimage specified by "mat" on the parameters of "set".
6558 * set is assumed to have only parameters and divs.
6560 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6561 __isl_take isl_mat *mat)
6563 isl_space *space;
6564 unsigned nparam;
6566 if (!set || !mat)
6567 goto error;
6569 nparam = isl_set_dim(set, isl_dim_param);
6571 if (mat->n_row != 1 + nparam)
6572 isl_die(isl_set_get_ctx(set), isl_error_internal,
6573 "unexpected number of rows", goto error);
6575 space = isl_set_get_space(set);
6576 space = isl_space_move_dims(space, isl_dim_set, 0,
6577 isl_dim_param, 0, nparam);
6578 set = isl_set_reset_space(set, space);
6579 set = isl_set_preimage(set, mat);
6580 nparam = isl_set_dim(set, isl_dim_out);
6581 space = isl_set_get_space(set);
6582 space = isl_space_move_dims(space, isl_dim_param, 0,
6583 isl_dim_out, 0, nparam);
6584 set = isl_set_reset_space(set, space);
6585 return set;
6586 error:
6587 isl_mat_free(mat);
6588 isl_set_free(set);
6589 return NULL;
6592 /* Intersect the basic set "bset" with the affine space specified by the
6593 * equalities in "eq".
6595 static struct isl_basic_set *basic_set_append_equalities(
6596 struct isl_basic_set *bset, struct isl_mat *eq)
6598 int i, k;
6599 unsigned len;
6601 if (!bset || !eq)
6602 goto error;
6604 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6605 eq->n_row, 0);
6606 if (!bset)
6607 goto error;
6609 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6610 for (i = 0; i < eq->n_row; ++i) {
6611 k = isl_basic_set_alloc_equality(bset);
6612 if (k < 0)
6613 goto error;
6614 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6615 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6617 isl_mat_free(eq);
6619 bset = isl_basic_set_gauss(bset, NULL);
6620 bset = isl_basic_set_finalize(bset);
6622 return bset;
6623 error:
6624 isl_mat_free(eq);
6625 isl_basic_set_free(bset);
6626 return NULL;
6629 /* Intersect the set "set" with the affine space specified by the
6630 * equalities in "eq".
6632 static struct isl_set *set_append_equalities(struct isl_set *set,
6633 struct isl_mat *eq)
6635 int i;
6637 if (!set || !eq)
6638 goto error;
6640 for (i = 0; i < set->n; ++i) {
6641 set->p[i] = basic_set_append_equalities(set->p[i],
6642 isl_mat_copy(eq));
6643 if (!set->p[i])
6644 goto error;
6646 isl_mat_free(eq);
6647 return set;
6648 error:
6649 isl_mat_free(eq);
6650 isl_set_free(set);
6651 return NULL;
6654 /* Given a basic set "bset" that only involves parameters and existentially
6655 * quantified variables, return the index of the first equality
6656 * that only involves parameters. If there is no such equality then
6657 * return bset->n_eq.
6659 * This function assumes that isl_basic_set_gauss has been called on "bset".
6661 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6663 int i, j;
6664 unsigned nparam, n_div;
6666 if (!bset)
6667 return -1;
6669 nparam = isl_basic_set_dim(bset, isl_dim_param);
6670 n_div = isl_basic_set_dim(bset, isl_dim_div);
6672 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6673 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6674 ++i;
6677 return i;
6680 /* Compute an explicit representation for the existentially quantified
6681 * variables in "bset" by computing the "minimal value" of the set
6682 * variables. Since there are no set variables, the computation of
6683 * the minimal value essentially computes an explicit representation
6684 * of the non-empty part(s) of "bset".
6686 * The input only involves parameters and existentially quantified variables.
6687 * All equalities among parameters have been removed.
6689 * Since the existentially quantified variables in the result are in general
6690 * going to be different from those in the input, we first replace
6691 * them by the minimal number of variables based on their equalities.
6692 * This should simplify the parametric integer programming.
6694 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6696 isl_morph *morph1, *morph2;
6697 isl_set *set;
6698 unsigned n;
6700 if (!bset)
6701 return NULL;
6702 if (bset->n_eq == 0)
6703 return isl_basic_set_lexmin_compute_divs(bset);
6705 morph1 = isl_basic_set_parameter_compression(bset);
6706 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6707 bset = isl_basic_set_lift(bset);
6708 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6709 bset = isl_morph_basic_set(morph2, bset);
6710 n = isl_basic_set_dim(bset, isl_dim_set);
6711 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6713 set = isl_basic_set_lexmin_compute_divs(bset);
6715 set = isl_morph_set(isl_morph_inverse(morph1), set);
6717 return set;
6720 /* Project the given basic set onto its parameter domain, possibly introducing
6721 * new, explicit, existential variables in the constraints.
6722 * The input has parameters and (possibly implicit) existential variables.
6723 * The output has the same parameters, but only
6724 * explicit existentially quantified variables.
6726 * The actual projection is performed by pip, but pip doesn't seem
6727 * to like equalities very much, so we first remove the equalities
6728 * among the parameters by performing a variable compression on
6729 * the parameters. Afterward, an inverse transformation is performed
6730 * and the equalities among the parameters are inserted back in.
6732 * The variable compression on the parameters may uncover additional
6733 * equalities that were only implicit before. We therefore check
6734 * if there are any new parameter equalities in the result and
6735 * if so recurse. The removal of parameter equalities is required
6736 * for the parameter compression performed by base_compute_divs.
6738 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6740 int i;
6741 struct isl_mat *eq;
6742 struct isl_mat *T, *T2;
6743 struct isl_set *set;
6744 unsigned nparam;
6746 bset = isl_basic_set_cow(bset);
6747 if (!bset)
6748 return NULL;
6750 if (bset->n_eq == 0)
6751 return base_compute_divs(bset);
6753 bset = isl_basic_set_gauss(bset, NULL);
6754 if (!bset)
6755 return NULL;
6756 if (isl_basic_set_plain_is_empty(bset))
6757 return isl_set_from_basic_set(bset);
6759 i = first_parameter_equality(bset);
6760 if (i == bset->n_eq)
6761 return base_compute_divs(bset);
6763 nparam = isl_basic_set_dim(bset, isl_dim_param);
6764 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6765 0, 1 + nparam);
6766 eq = isl_mat_cow(eq);
6767 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6768 if (T && T->n_col == 0) {
6769 isl_mat_free(T);
6770 isl_mat_free(T2);
6771 isl_mat_free(eq);
6772 bset = isl_basic_set_set_to_empty(bset);
6773 return isl_set_from_basic_set(bset);
6775 bset = basic_set_parameter_preimage(bset, T);
6777 i = first_parameter_equality(bset);
6778 if (!bset)
6779 set = NULL;
6780 else if (i == bset->n_eq)
6781 set = base_compute_divs(bset);
6782 else
6783 set = parameter_compute_divs(bset);
6784 set = set_parameter_preimage(set, T2);
6785 set = set_append_equalities(set, eq);
6786 return set;
6789 /* Insert the divs from "ls" before those of "bmap".
6791 * The number of columns is not changed, which means that the last
6792 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6793 * The caller is responsible for removing the same number of dimensions
6794 * from the space of "bmap".
6796 static __isl_give isl_basic_map *insert_divs_from_local_space(
6797 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6799 int i;
6800 int n_div;
6801 int old_n_div;
6803 n_div = isl_local_space_dim(ls, isl_dim_div);
6804 if (n_div == 0)
6805 return bmap;
6807 old_n_div = bmap->n_div;
6808 bmap = insert_div_rows(bmap, n_div);
6809 if (!bmap)
6810 return NULL;
6812 for (i = 0; i < n_div; ++i) {
6813 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6814 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6817 return bmap;
6820 /* Replace the space of "bmap" by the space and divs of "ls".
6822 * If "ls" has any divs, then we simplify the result since we may
6823 * have discovered some additional equalities that could simplify
6824 * the div expressions.
6826 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6827 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6829 int n_div;
6831 bmap = isl_basic_map_cow(bmap);
6832 if (!bmap || !ls)
6833 goto error;
6835 n_div = isl_local_space_dim(ls, isl_dim_div);
6836 bmap = insert_divs_from_local_space(bmap, ls);
6837 if (!bmap)
6838 goto error;
6840 isl_space_free(bmap->dim);
6841 bmap->dim = isl_local_space_get_space(ls);
6842 if (!bmap->dim)
6843 goto error;
6845 isl_local_space_free(ls);
6846 if (n_div > 0)
6847 bmap = isl_basic_map_simplify(bmap);
6848 bmap = isl_basic_map_finalize(bmap);
6849 return bmap;
6850 error:
6851 isl_basic_map_free(bmap);
6852 isl_local_space_free(ls);
6853 return NULL;
6856 /* Replace the space of "map" by the space and divs of "ls".
6858 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6859 __isl_take isl_local_space *ls)
6861 int i;
6863 map = isl_map_cow(map);
6864 if (!map || !ls)
6865 goto error;
6867 for (i = 0; i < map->n; ++i) {
6868 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6869 isl_local_space_copy(ls));
6870 if (!map->p[i])
6871 goto error;
6873 isl_space_free(map->dim);
6874 map->dim = isl_local_space_get_space(ls);
6875 if (!map->dim)
6876 goto error;
6878 isl_local_space_free(ls);
6879 return map;
6880 error:
6881 isl_local_space_free(ls);
6882 isl_map_free(map);
6883 return NULL;
6886 /* Compute an explicit representation for the existentially
6887 * quantified variables for which do not know any explicit representation yet.
6889 * We first sort the existentially quantified variables so that the
6890 * existentially quantified variables for which we already have an explicit
6891 * representation are placed before those for which we do not.
6892 * The input dimensions, the output dimensions and the existentially
6893 * quantified variables for which we already have an explicit
6894 * representation are then turned into parameters.
6895 * compute_divs returns a map with the same parameters and
6896 * no input or output dimensions and the dimension specification
6897 * is reset to that of the input, including the existentially quantified
6898 * variables for which we already had an explicit representation.
6900 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6902 struct isl_basic_set *bset;
6903 struct isl_set *set;
6904 struct isl_map *map;
6905 isl_space *dim;
6906 isl_local_space *ls;
6907 unsigned nparam;
6908 unsigned n_in;
6909 unsigned n_out;
6910 int n_known;
6911 int i;
6913 bmap = isl_basic_map_sort_divs(bmap);
6914 bmap = isl_basic_map_cow(bmap);
6915 if (!bmap)
6916 return NULL;
6918 n_known = isl_basic_map_first_unknown_div(bmap);
6919 if (n_known < 0)
6920 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6922 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6923 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6924 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6925 dim = isl_space_set_alloc(bmap->ctx,
6926 nparam + n_in + n_out + n_known, 0);
6927 if (!dim)
6928 goto error;
6930 ls = isl_basic_map_get_local_space(bmap);
6931 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6932 n_known, bmap->n_div - n_known);
6933 if (n_known > 0) {
6934 for (i = n_known; i < bmap->n_div; ++i)
6935 swap_div(bmap, i - n_known, i);
6936 bmap->n_div -= n_known;
6937 bmap->extra -= n_known;
6939 bmap = isl_basic_map_reset_space(bmap, dim);
6940 bset = bset_from_bmap(bmap);
6942 set = parameter_compute_divs(bset);
6943 map = set_to_map(set);
6944 map = replace_space_by_local_space(map, ls);
6946 return map;
6947 error:
6948 isl_basic_map_free(bmap);
6949 return NULL;
6952 /* Remove the explicit representation of local variable "div",
6953 * if there is any.
6955 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6956 __isl_take isl_basic_map *bmap, int div)
6958 isl_bool unknown;
6960 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
6961 if (unknown < 0)
6962 return isl_basic_map_free(bmap);
6963 if (unknown)
6964 return bmap;
6966 bmap = isl_basic_map_cow(bmap);
6967 if (!bmap)
6968 return NULL;
6969 isl_int_set_si(bmap->div[div][0], 0);
6970 return bmap;
6973 /* Is local variable "div" of "bmap" marked as not having an explicit
6974 * representation?
6975 * Note that even if "div" is not marked in this way and therefore
6976 * has an explicit representation, this representation may still
6977 * depend (indirectly) on other local variables that do not
6978 * have an explicit representation.
6980 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
6981 int div)
6983 if (!bmap)
6984 return isl_bool_error;
6985 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6986 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6987 "position out of bounds", return isl_bool_error);
6988 return isl_int_is_zero(bmap->div[div][0]);
6991 /* Return the position of the first local variable that does not
6992 * have an explicit representation.
6993 * Return the total number of local variables if they all have
6994 * an explicit representation.
6995 * Return -1 on error.
6997 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
6999 int i;
7001 if (!bmap)
7002 return -1;
7004 for (i = 0; i < bmap->n_div; ++i) {
7005 if (!isl_basic_map_div_is_known(bmap, i))
7006 return i;
7008 return bmap->n_div;
7011 /* Return the position of the first local variable that does not
7012 * have an explicit representation.
7013 * Return the total number of local variables if they all have
7014 * an explicit representation.
7015 * Return -1 on error.
7017 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7019 return isl_basic_map_first_unknown_div(bset);
7022 /* Does "bmap" have an explicit representation for all local variables?
7024 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7026 int first, n;
7028 n = isl_basic_map_dim(bmap, isl_dim_div);
7029 first = isl_basic_map_first_unknown_div(bmap);
7030 if (first < 0)
7031 return isl_bool_error;
7032 return first == n;
7035 /* Do all basic maps in "map" have an explicit representation
7036 * for all local variables?
7038 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7040 int i;
7042 if (!map)
7043 return isl_bool_error;
7045 for (i = 0; i < map->n; ++i) {
7046 int known = isl_basic_map_divs_known(map->p[i]);
7047 if (known <= 0)
7048 return known;
7051 return isl_bool_true;
7054 /* If bmap contains any unknown divs, then compute explicit
7055 * expressions for them. However, this computation may be
7056 * quite expensive, so first try to remove divs that aren't
7057 * strictly needed.
7059 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7061 int known;
7062 struct isl_map *map;
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 bmap = isl_basic_map_drop_redundant_divs(bmap);
7072 known = isl_basic_map_divs_known(bmap);
7073 if (known < 0)
7074 goto error;
7075 if (known)
7076 return isl_map_from_basic_map(bmap);
7078 map = compute_divs(bmap);
7079 return map;
7080 error:
7081 isl_basic_map_free(bmap);
7082 return NULL;
7085 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7087 int i;
7088 int known;
7089 struct isl_map *res;
7091 if (!map)
7092 return NULL;
7093 if (map->n == 0)
7094 return map;
7096 known = isl_map_divs_known(map);
7097 if (known < 0) {
7098 isl_map_free(map);
7099 return NULL;
7101 if (known)
7102 return map;
7104 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7105 for (i = 1 ; i < map->n; ++i) {
7106 struct isl_map *r2;
7107 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7108 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7109 res = isl_map_union_disjoint(res, r2);
7110 else
7111 res = isl_map_union(res, r2);
7113 isl_map_free(map);
7115 return res;
7118 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7120 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7123 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7125 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7128 struct isl_set *isl_map_domain(struct isl_map *map)
7130 int i;
7131 struct isl_set *set;
7133 if (!map)
7134 goto error;
7136 map = isl_map_cow(map);
7137 if (!map)
7138 return NULL;
7140 set = set_from_map(map);
7141 set->dim = isl_space_domain(set->dim);
7142 if (!set->dim)
7143 goto error;
7144 for (i = 0; i < map->n; ++i) {
7145 set->p[i] = isl_basic_map_domain(map->p[i]);
7146 if (!set->p[i])
7147 goto error;
7149 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7150 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7151 return set;
7152 error:
7153 isl_map_free(map);
7154 return NULL;
7157 /* Return the union of "map1" and "map2", where we assume for now that
7158 * "map1" and "map2" are disjoint. Note that the basic maps inside
7159 * "map1" or "map2" may not be disjoint from each other.
7160 * Also note that this function is also called from isl_map_union,
7161 * which takes care of handling the situation where "map1" and "map2"
7162 * may not be disjoint.
7164 * If one of the inputs is empty, we can simply return the other input.
7165 * Similarly, if one of the inputs is universal, then it is equal to the union.
7167 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7168 __isl_take isl_map *map2)
7170 int i;
7171 unsigned flags = 0;
7172 struct isl_map *map = NULL;
7173 int is_universe;
7175 if (!map1 || !map2)
7176 goto error;
7178 if (!isl_space_is_equal(map1->dim, map2->dim))
7179 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7180 "spaces don't match", goto error);
7182 if (map1->n == 0) {
7183 isl_map_free(map1);
7184 return map2;
7186 if (map2->n == 0) {
7187 isl_map_free(map2);
7188 return map1;
7191 is_universe = isl_map_plain_is_universe(map1);
7192 if (is_universe < 0)
7193 goto error;
7194 if (is_universe) {
7195 isl_map_free(map2);
7196 return map1;
7199 is_universe = isl_map_plain_is_universe(map2);
7200 if (is_universe < 0)
7201 goto error;
7202 if (is_universe) {
7203 isl_map_free(map1);
7204 return map2;
7207 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7208 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7209 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7211 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7212 map1->n + map2->n, flags);
7213 if (!map)
7214 goto error;
7215 for (i = 0; i < map1->n; ++i) {
7216 map = isl_map_add_basic_map(map,
7217 isl_basic_map_copy(map1->p[i]));
7218 if (!map)
7219 goto error;
7221 for (i = 0; i < map2->n; ++i) {
7222 map = isl_map_add_basic_map(map,
7223 isl_basic_map_copy(map2->p[i]));
7224 if (!map)
7225 goto error;
7227 isl_map_free(map1);
7228 isl_map_free(map2);
7229 return map;
7230 error:
7231 isl_map_free(map);
7232 isl_map_free(map1);
7233 isl_map_free(map2);
7234 return NULL;
7237 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7238 * guaranteed to be disjoint by the caller.
7240 * Note that this functions is called from within isl_map_make_disjoint,
7241 * so we have to be careful not to touch the constraints of the inputs
7242 * in any way.
7244 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7245 __isl_take isl_map *map2)
7247 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7250 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7251 * not be disjoint. The parameters are assumed to have been aligned.
7253 * We currently simply call map_union_disjoint, the internal operation
7254 * of which does not really depend on the inputs being disjoint.
7255 * If the result contains more than one basic map, then we clear
7256 * the disjoint flag since the result may contain basic maps from
7257 * both inputs and these are not guaranteed to be disjoint.
7259 * As a special case, if "map1" and "map2" are obviously equal,
7260 * then we simply return "map1".
7262 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7263 __isl_take isl_map *map2)
7265 int equal;
7267 if (!map1 || !map2)
7268 goto error;
7270 equal = isl_map_plain_is_equal(map1, map2);
7271 if (equal < 0)
7272 goto error;
7273 if (equal) {
7274 isl_map_free(map2);
7275 return map1;
7278 map1 = map_union_disjoint(map1, map2);
7279 if (!map1)
7280 return NULL;
7281 if (map1->n > 1)
7282 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7283 return map1;
7284 error:
7285 isl_map_free(map1);
7286 isl_map_free(map2);
7287 return NULL;
7290 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7291 * not be disjoint.
7293 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7294 __isl_take isl_map *map2)
7296 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7299 struct isl_set *isl_set_union_disjoint(
7300 struct isl_set *set1, struct isl_set *set2)
7302 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7303 set_to_map(set2)));
7306 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7308 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7311 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7312 * the results.
7314 * "map" and "set" are assumed to be compatible and non-NULL.
7316 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7317 __isl_take isl_set *set,
7318 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7319 __isl_take isl_basic_set *bset))
7321 unsigned flags = 0;
7322 struct isl_map *result;
7323 int i, j;
7325 if (isl_set_plain_is_universe(set)) {
7326 isl_set_free(set);
7327 return map;
7330 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7331 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7332 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7334 result = isl_map_alloc_space(isl_space_copy(map->dim),
7335 map->n * set->n, flags);
7336 for (i = 0; result && i < map->n; ++i)
7337 for (j = 0; j < set->n; ++j) {
7338 result = isl_map_add_basic_map(result,
7339 fn(isl_basic_map_copy(map->p[i]),
7340 isl_basic_set_copy(set->p[j])));
7341 if (!result)
7342 break;
7345 isl_map_free(map);
7346 isl_set_free(set);
7347 return result;
7350 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7351 __isl_take isl_set *set)
7353 if (!map || !set)
7354 goto error;
7356 if (!isl_map_compatible_range(map, set))
7357 isl_die(set->ctx, isl_error_invalid,
7358 "incompatible spaces", goto error);
7360 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7361 error:
7362 isl_map_free(map);
7363 isl_set_free(set);
7364 return NULL;
7367 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7368 __isl_take isl_set *set)
7370 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7373 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7374 __isl_take isl_set *set)
7376 if (!map || !set)
7377 goto error;
7379 if (!isl_map_compatible_domain(map, set))
7380 isl_die(set->ctx, isl_error_invalid,
7381 "incompatible spaces", goto error);
7383 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7384 error:
7385 isl_map_free(map);
7386 isl_set_free(set);
7387 return NULL;
7390 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7391 __isl_take isl_set *set)
7393 return isl_map_align_params_map_map_and(map, set,
7394 &map_intersect_domain);
7397 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7398 __isl_take isl_map *map2)
7400 if (!map1 || !map2)
7401 goto error;
7402 map1 = isl_map_reverse(map1);
7403 map1 = isl_map_apply_range(map1, map2);
7404 return isl_map_reverse(map1);
7405 error:
7406 isl_map_free(map1);
7407 isl_map_free(map2);
7408 return NULL;
7411 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7412 __isl_take isl_map *map2)
7414 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7417 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7418 __isl_take isl_map *map2)
7420 isl_space *dim_result;
7421 struct isl_map *result;
7422 int i, j;
7424 if (!map1 || !map2)
7425 goto error;
7427 dim_result = isl_space_join(isl_space_copy(map1->dim),
7428 isl_space_copy(map2->dim));
7430 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7431 if (!result)
7432 goto error;
7433 for (i = 0; i < map1->n; ++i)
7434 for (j = 0; j < map2->n; ++j) {
7435 result = isl_map_add_basic_map(result,
7436 isl_basic_map_apply_range(
7437 isl_basic_map_copy(map1->p[i]),
7438 isl_basic_map_copy(map2->p[j])));
7439 if (!result)
7440 goto error;
7442 isl_map_free(map1);
7443 isl_map_free(map2);
7444 if (result && result->n <= 1)
7445 ISL_F_SET(result, ISL_MAP_DISJOINT);
7446 return result;
7447 error:
7448 isl_map_free(map1);
7449 isl_map_free(map2);
7450 return NULL;
7453 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7454 __isl_take isl_map *map2)
7456 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7460 * returns range - domain
7462 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7464 isl_space *target_space;
7465 struct isl_basic_set *bset;
7466 unsigned dim;
7467 unsigned nparam;
7468 int i;
7470 if (!bmap)
7471 goto error;
7472 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7473 bmap->dim, isl_dim_out),
7474 goto error);
7475 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7476 dim = isl_basic_map_n_in(bmap);
7477 nparam = isl_basic_map_n_param(bmap);
7478 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7479 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7480 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7481 for (i = 0; i < dim; ++i) {
7482 int j = isl_basic_map_alloc_equality(bmap);
7483 if (j < 0) {
7484 bmap = isl_basic_map_free(bmap);
7485 break;
7487 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7488 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7489 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7490 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7492 bset = isl_basic_map_domain(bmap);
7493 bset = isl_basic_set_reset_space(bset, target_space);
7494 return bset;
7495 error:
7496 isl_basic_map_free(bmap);
7497 return NULL;
7501 * returns range - domain
7503 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7505 int i;
7506 isl_space *dim;
7507 struct isl_set *result;
7509 if (!map)
7510 return NULL;
7512 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7513 map->dim, isl_dim_out),
7514 goto error);
7515 dim = isl_map_get_space(map);
7516 dim = isl_space_domain(dim);
7517 result = isl_set_alloc_space(dim, map->n, 0);
7518 if (!result)
7519 goto error;
7520 for (i = 0; i < map->n; ++i)
7521 result = isl_set_add_basic_set(result,
7522 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7523 isl_map_free(map);
7524 return result;
7525 error:
7526 isl_map_free(map);
7527 return NULL;
7531 * returns [domain -> range] -> range - domain
7533 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7534 __isl_take isl_basic_map *bmap)
7536 int i, k;
7537 isl_space *dim;
7538 isl_basic_map *domain;
7539 int nparam, n;
7540 unsigned total;
7542 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7543 bmap->dim, isl_dim_out))
7544 isl_die(bmap->ctx, isl_error_invalid,
7545 "domain and range don't match", goto error);
7547 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7548 n = isl_basic_map_dim(bmap, isl_dim_in);
7550 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7551 domain = isl_basic_map_universe(dim);
7553 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7554 bmap = isl_basic_map_apply_range(bmap, domain);
7555 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7557 total = isl_basic_map_total_dim(bmap);
7559 for (i = 0; i < n; ++i) {
7560 k = isl_basic_map_alloc_equality(bmap);
7561 if (k < 0)
7562 goto error;
7563 isl_seq_clr(bmap->eq[k], 1 + total);
7564 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7565 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7566 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7569 bmap = isl_basic_map_gauss(bmap, NULL);
7570 return isl_basic_map_finalize(bmap);
7571 error:
7572 isl_basic_map_free(bmap);
7573 return NULL;
7577 * returns [domain -> range] -> range - domain
7579 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7581 int i;
7582 isl_space *domain_dim;
7584 if (!map)
7585 return NULL;
7587 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7588 map->dim, isl_dim_out))
7589 isl_die(map->ctx, isl_error_invalid,
7590 "domain and range don't match", goto error);
7592 map = isl_map_cow(map);
7593 if (!map)
7594 return NULL;
7596 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7597 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7598 map->dim = isl_space_join(map->dim, domain_dim);
7599 if (!map->dim)
7600 goto error;
7601 for (i = 0; i < map->n; ++i) {
7602 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7603 if (!map->p[i])
7604 goto error;
7606 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7607 return map;
7608 error:
7609 isl_map_free(map);
7610 return NULL;
7613 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7615 struct isl_basic_map *bmap;
7616 unsigned nparam;
7617 unsigned dim;
7618 int i;
7620 if (!dims)
7621 return NULL;
7623 nparam = dims->nparam;
7624 dim = dims->n_out;
7625 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7626 if (!bmap)
7627 goto error;
7629 for (i = 0; i < dim; ++i) {
7630 int j = isl_basic_map_alloc_equality(bmap);
7631 if (j < 0)
7632 goto error;
7633 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7634 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7635 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7637 return isl_basic_map_finalize(bmap);
7638 error:
7639 isl_basic_map_free(bmap);
7640 return NULL;
7643 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7645 if (!dim)
7646 return NULL;
7647 if (dim->n_in != dim->n_out)
7648 isl_die(dim->ctx, isl_error_invalid,
7649 "number of input and output dimensions needs to be "
7650 "the same", goto error);
7651 return basic_map_identity(dim);
7652 error:
7653 isl_space_free(dim);
7654 return NULL;
7657 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7659 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7662 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7664 isl_space *dim = isl_set_get_space(set);
7665 isl_map *id;
7666 id = isl_map_identity(isl_space_map_from_set(dim));
7667 return isl_map_intersect_range(id, set);
7670 /* Construct a basic set with all set dimensions having only non-negative
7671 * values.
7673 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7674 __isl_take isl_space *space)
7676 int i;
7677 unsigned nparam;
7678 unsigned dim;
7679 struct isl_basic_set *bset;
7681 if (!space)
7682 return NULL;
7683 nparam = space->nparam;
7684 dim = space->n_out;
7685 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7686 if (!bset)
7687 return NULL;
7688 for (i = 0; i < dim; ++i) {
7689 int k = isl_basic_set_alloc_inequality(bset);
7690 if (k < 0)
7691 goto error;
7692 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7693 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7695 return bset;
7696 error:
7697 isl_basic_set_free(bset);
7698 return NULL;
7701 /* Construct the half-space x_pos >= 0.
7703 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7704 int pos)
7706 int k;
7707 isl_basic_set *nonneg;
7709 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7710 k = isl_basic_set_alloc_inequality(nonneg);
7711 if (k < 0)
7712 goto error;
7713 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7714 isl_int_set_si(nonneg->ineq[k][pos], 1);
7716 return isl_basic_set_finalize(nonneg);
7717 error:
7718 isl_basic_set_free(nonneg);
7719 return NULL;
7722 /* Construct the half-space x_pos <= -1.
7724 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7726 int k;
7727 isl_basic_set *neg;
7729 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7730 k = isl_basic_set_alloc_inequality(neg);
7731 if (k < 0)
7732 goto error;
7733 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7734 isl_int_set_si(neg->ineq[k][0], -1);
7735 isl_int_set_si(neg->ineq[k][pos], -1);
7737 return isl_basic_set_finalize(neg);
7738 error:
7739 isl_basic_set_free(neg);
7740 return NULL;
7743 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7744 enum isl_dim_type type, unsigned first, unsigned n)
7746 int i;
7747 unsigned offset;
7748 isl_basic_set *nonneg;
7749 isl_basic_set *neg;
7751 if (!set)
7752 return NULL;
7753 if (n == 0)
7754 return set;
7756 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7758 offset = pos(set->dim, type);
7759 for (i = 0; i < n; ++i) {
7760 nonneg = nonneg_halfspace(isl_set_get_space(set),
7761 offset + first + i);
7762 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7764 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7767 return set;
7768 error:
7769 isl_set_free(set);
7770 return NULL;
7773 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7774 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7775 void *user)
7777 isl_set *half;
7779 if (!set)
7780 return -1;
7781 if (isl_set_plain_is_empty(set)) {
7782 isl_set_free(set);
7783 return 0;
7785 if (first == len)
7786 return fn(set, signs, user);
7788 signs[first] = 1;
7789 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7790 1 + first));
7791 half = isl_set_intersect(half, isl_set_copy(set));
7792 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7793 goto error;
7795 signs[first] = -1;
7796 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7797 1 + first));
7798 half = isl_set_intersect(half, set);
7799 return foreach_orthant(half, signs, first + 1, len, fn, user);
7800 error:
7801 isl_set_free(set);
7802 return -1;
7805 /* Call "fn" on the intersections of "set" with each of the orthants
7806 * (except for obviously empty intersections). The orthant is identified
7807 * by the signs array, with each entry having value 1 or -1 according
7808 * to the sign of the corresponding variable.
7810 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7811 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7812 void *user)
7814 unsigned nparam;
7815 unsigned nvar;
7816 int *signs;
7817 int r;
7819 if (!set)
7820 return -1;
7821 if (isl_set_plain_is_empty(set))
7822 return 0;
7824 nparam = isl_set_dim(set, isl_dim_param);
7825 nvar = isl_set_dim(set, isl_dim_set);
7827 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7829 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7830 fn, user);
7832 free(signs);
7834 return r;
7837 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7839 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
7842 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7843 __isl_keep isl_basic_map *bmap2)
7845 int is_subset;
7846 struct isl_map *map1;
7847 struct isl_map *map2;
7849 if (!bmap1 || !bmap2)
7850 return isl_bool_error;
7852 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7853 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7855 is_subset = isl_map_is_subset(map1, map2);
7857 isl_map_free(map1);
7858 isl_map_free(map2);
7860 return is_subset;
7863 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7864 __isl_keep isl_basic_set *bset2)
7866 return isl_basic_map_is_subset(bset1, bset2);
7869 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7870 __isl_keep isl_basic_map *bmap2)
7872 isl_bool is_subset;
7874 if (!bmap1 || !bmap2)
7875 return isl_bool_error;
7876 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7877 if (is_subset != isl_bool_true)
7878 return is_subset;
7879 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7880 return is_subset;
7883 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7884 __isl_keep isl_basic_set *bset2)
7886 return isl_basic_map_is_equal(
7887 bset_to_bmap(bset1), bset_to_bmap(bset2));
7890 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7892 int i;
7893 int is_empty;
7895 if (!map)
7896 return isl_bool_error;
7897 for (i = 0; i < map->n; ++i) {
7898 is_empty = isl_basic_map_is_empty(map->p[i]);
7899 if (is_empty < 0)
7900 return isl_bool_error;
7901 if (!is_empty)
7902 return isl_bool_false;
7904 return isl_bool_true;
7907 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7909 return map ? map->n == 0 : isl_bool_error;
7912 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7914 return set ? set->n == 0 : isl_bool_error;
7917 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7919 return isl_map_is_empty(set_to_map(set));
7922 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7924 if (!map1 || !map2)
7925 return -1;
7927 return isl_space_is_equal(map1->dim, map2->dim);
7930 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7932 if (!set1 || !set2)
7933 return -1;
7935 return isl_space_is_equal(set1->dim, set2->dim);
7938 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7940 isl_bool is_subset;
7942 if (!map1 || !map2)
7943 return isl_bool_error;
7944 is_subset = isl_map_is_subset(map1, map2);
7945 if (is_subset != isl_bool_true)
7946 return is_subset;
7947 is_subset = isl_map_is_subset(map2, map1);
7948 return is_subset;
7951 /* Is "map1" equal to "map2"?
7953 * First check if they are obviously equal.
7954 * If not, then perform a more detailed analysis.
7956 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7958 isl_bool equal;
7960 equal = isl_map_plain_is_equal(map1, map2);
7961 if (equal < 0 || equal)
7962 return equal;
7963 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7966 isl_bool isl_basic_map_is_strict_subset(
7967 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7969 isl_bool is_subset;
7971 if (!bmap1 || !bmap2)
7972 return isl_bool_error;
7973 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7974 if (is_subset != isl_bool_true)
7975 return is_subset;
7976 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7977 if (is_subset == isl_bool_error)
7978 return is_subset;
7979 return !is_subset;
7982 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7983 __isl_keep isl_map *map2)
7985 isl_bool is_subset;
7987 if (!map1 || !map2)
7988 return isl_bool_error;
7989 is_subset = isl_map_is_subset(map1, map2);
7990 if (is_subset != isl_bool_true)
7991 return is_subset;
7992 is_subset = isl_map_is_subset(map2, map1);
7993 if (is_subset == isl_bool_error)
7994 return is_subset;
7995 return !is_subset;
7998 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
7999 __isl_keep isl_set *set2)
8001 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8004 /* Is "bmap" obviously equal to the universe with the same space?
8006 * That is, does it not have any constraints?
8008 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8010 if (!bmap)
8011 return isl_bool_error;
8012 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8015 /* Is "bset" obviously equal to the universe with the same space?
8017 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8019 return isl_basic_map_plain_is_universe(bset);
8022 /* If "c" does not involve any existentially quantified variables,
8023 * then set *univ to false and abort
8025 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8027 isl_bool *univ = user;
8028 unsigned n;
8030 n = isl_constraint_dim(c, isl_dim_div);
8031 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8032 isl_constraint_free(c);
8033 if (*univ < 0 || !*univ)
8034 return isl_stat_error;
8035 return isl_stat_ok;
8038 /* Is "bmap" equal to the universe with the same space?
8040 * First check if it is obviously equal to the universe.
8041 * If not and if there are any constraints not involving
8042 * existentially quantified variables, then it is certainly
8043 * not equal to the universe.
8044 * Otherwise, check if the universe is a subset of "bmap".
8046 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8048 isl_bool univ;
8049 isl_basic_map *test;
8051 univ = isl_basic_map_plain_is_universe(bmap);
8052 if (univ < 0 || univ)
8053 return univ;
8054 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8055 return isl_bool_false;
8056 univ = isl_bool_true;
8057 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8058 univ)
8059 return isl_bool_error;
8060 if (univ < 0 || !univ)
8061 return univ;
8062 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8063 univ = isl_basic_map_is_subset(test, bmap);
8064 isl_basic_map_free(test);
8065 return univ;
8068 /* Is "bset" equal to the universe with the same space?
8070 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8072 return isl_basic_map_is_universe(bset);
8075 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8077 int i;
8079 if (!map)
8080 return isl_bool_error;
8082 for (i = 0; i < map->n; ++i) {
8083 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8084 if (r < 0 || r)
8085 return r;
8088 return isl_bool_false;
8091 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8093 return isl_map_plain_is_universe(set_to_map(set));
8096 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8098 struct isl_basic_set *bset = NULL;
8099 struct isl_vec *sample = NULL;
8100 isl_bool empty, non_empty;
8102 if (!bmap)
8103 return isl_bool_error;
8105 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8106 return isl_bool_true;
8108 if (isl_basic_map_plain_is_universe(bmap))
8109 return isl_bool_false;
8111 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8112 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8113 copy = isl_basic_map_remove_redundancies(copy);
8114 empty = isl_basic_map_plain_is_empty(copy);
8115 isl_basic_map_free(copy);
8116 return empty;
8119 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8120 if (non_empty < 0)
8121 return isl_bool_error;
8122 if (non_empty)
8123 return isl_bool_false;
8124 isl_vec_free(bmap->sample);
8125 bmap->sample = NULL;
8126 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8127 if (!bset)
8128 return isl_bool_error;
8129 sample = isl_basic_set_sample_vec(bset);
8130 if (!sample)
8131 return isl_bool_error;
8132 empty = sample->size == 0;
8133 isl_vec_free(bmap->sample);
8134 bmap->sample = sample;
8135 if (empty)
8136 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8138 return empty;
8141 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8143 if (!bmap)
8144 return isl_bool_error;
8145 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8148 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8150 if (!bset)
8151 return isl_bool_error;
8152 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8155 /* Is "bmap" known to be non-empty?
8157 * That is, is the cached sample still valid?
8159 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8161 unsigned total;
8163 if (!bmap)
8164 return isl_bool_error;
8165 if (!bmap->sample)
8166 return isl_bool_false;
8167 total = 1 + isl_basic_map_total_dim(bmap);
8168 if (bmap->sample->size != total)
8169 return isl_bool_false;
8170 return isl_basic_map_contains(bmap, bmap->sample);
8173 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8175 return isl_basic_map_is_empty(bset_to_bmap(bset));
8178 struct isl_map *isl_basic_map_union(
8179 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8181 struct isl_map *map;
8182 if (!bmap1 || !bmap2)
8183 goto error;
8185 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8187 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8188 if (!map)
8189 goto error;
8190 map = isl_map_add_basic_map(map, bmap1);
8191 map = isl_map_add_basic_map(map, bmap2);
8192 return map;
8193 error:
8194 isl_basic_map_free(bmap1);
8195 isl_basic_map_free(bmap2);
8196 return NULL;
8199 struct isl_set *isl_basic_set_union(
8200 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8202 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8203 bset_to_bmap(bset2)));
8206 /* Order divs such that any div only depends on previous divs */
8207 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8209 int i;
8210 unsigned off;
8212 if (!bmap)
8213 return NULL;
8215 off = isl_space_dim(bmap->dim, isl_dim_all);
8217 for (i = 0; i < bmap->n_div; ++i) {
8218 int pos;
8219 if (isl_int_is_zero(bmap->div[i][0]))
8220 continue;
8221 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8222 bmap->n_div-i);
8223 if (pos == -1)
8224 continue;
8225 if (pos == 0)
8226 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8227 "integer division depends on itself",
8228 return isl_basic_map_free(bmap));
8229 isl_basic_map_swap_div(bmap, i, i + pos);
8230 --i;
8232 return bmap;
8235 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8237 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8240 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8242 int i;
8244 if (!map)
8245 return 0;
8247 for (i = 0; i < map->n; ++i) {
8248 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8249 if (!map->p[i])
8250 goto error;
8253 return map;
8254 error:
8255 isl_map_free(map);
8256 return NULL;
8259 /* Sort the local variables of "bset".
8261 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8262 __isl_take isl_basic_set *bset)
8264 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8267 /* Apply the expansion computed by isl_merge_divs.
8268 * The expansion itself is given by "exp" while the resulting
8269 * list of divs is given by "div".
8271 * Move the integer divisions of "bmap" into the right position
8272 * according to "exp" and then introduce the additional integer
8273 * divisions, adding div constraints.
8274 * The moving should be done first to avoid moving coefficients
8275 * in the definitions of the extra integer divisions.
8277 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8278 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8280 int i, j;
8281 int n_div;
8283 bmap = isl_basic_map_cow(bmap);
8284 if (!bmap || !div)
8285 goto error;
8287 if (div->n_row < bmap->n_div)
8288 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8289 "not an expansion", goto error);
8291 n_div = bmap->n_div;
8292 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8293 div->n_row - n_div, 0,
8294 2 * (div->n_row - n_div));
8296 for (i = n_div; i < div->n_row; ++i)
8297 if (isl_basic_map_alloc_div(bmap) < 0)
8298 goto error;
8300 for (j = n_div - 1; j >= 0; --j) {
8301 if (exp[j] == j)
8302 break;
8303 isl_basic_map_swap_div(bmap, j, exp[j]);
8305 j = 0;
8306 for (i = 0; i < div->n_row; ++i) {
8307 if (j < n_div && exp[j] == i) {
8308 j++;
8309 } else {
8310 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8311 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8312 continue;
8313 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8314 goto error;
8318 isl_mat_free(div);
8319 return bmap;
8320 error:
8321 isl_basic_map_free(bmap);
8322 isl_mat_free(div);
8323 return NULL;
8326 /* Apply the expansion computed by isl_merge_divs.
8327 * The expansion itself is given by "exp" while the resulting
8328 * list of divs is given by "div".
8330 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8331 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8333 return isl_basic_map_expand_divs(bset, div, exp);
8336 /* Look for a div in dst that corresponds to the div "div" in src.
8337 * The divs before "div" in src and dst are assumed to be the same.
8339 * Returns -1 if no corresponding div was found and the position
8340 * of the corresponding div in dst otherwise.
8342 static int find_div(struct isl_basic_map *dst,
8343 struct isl_basic_map *src, unsigned div)
8345 int i;
8347 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8349 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8350 for (i = div; i < dst->n_div; ++i)
8351 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8352 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8353 dst->n_div - div) == -1)
8354 return i;
8355 return -1;
8358 /* Align the divs of "dst" to those of "src", adding divs from "src"
8359 * if needed. That is, make sure that the first src->n_div divs
8360 * of the result are equal to those of src.
8362 * The result is not finalized as by design it will have redundant
8363 * divs if any divs from "src" were copied.
8365 __isl_give isl_basic_map *isl_basic_map_align_divs(
8366 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8368 int i;
8369 int known, extended;
8370 unsigned total;
8372 if (!dst || !src)
8373 return isl_basic_map_free(dst);
8375 if (src->n_div == 0)
8376 return dst;
8378 known = isl_basic_map_divs_known(src);
8379 if (known < 0)
8380 return isl_basic_map_free(dst);
8381 if (!known)
8382 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8383 "some src divs are unknown",
8384 return isl_basic_map_free(dst));
8386 src = isl_basic_map_order_divs(src);
8388 extended = 0;
8389 total = isl_space_dim(src->dim, isl_dim_all);
8390 for (i = 0; i < src->n_div; ++i) {
8391 int j = find_div(dst, src, i);
8392 if (j < 0) {
8393 if (!extended) {
8394 int extra = src->n_div - i;
8395 dst = isl_basic_map_cow(dst);
8396 if (!dst)
8397 return NULL;
8398 dst = isl_basic_map_extend_space(dst,
8399 isl_space_copy(dst->dim),
8400 extra, 0, 2 * extra);
8401 extended = 1;
8403 j = isl_basic_map_alloc_div(dst);
8404 if (j < 0)
8405 return isl_basic_map_free(dst);
8406 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8407 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8408 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8409 return isl_basic_map_free(dst);
8411 if (j != i)
8412 isl_basic_map_swap_div(dst, i, j);
8414 return dst;
8417 struct isl_basic_set *isl_basic_set_align_divs(
8418 struct isl_basic_set *dst, struct isl_basic_set *src)
8420 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst),
8421 bset_to_bmap(src)));
8424 struct isl_map *isl_map_align_divs(struct isl_map *map)
8426 int i;
8428 if (!map)
8429 return NULL;
8430 if (map->n == 0)
8431 return map;
8432 map = isl_map_compute_divs(map);
8433 map = isl_map_cow(map);
8434 if (!map)
8435 return NULL;
8437 for (i = 1; i < map->n; ++i)
8438 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8439 for (i = 1; i < map->n; ++i) {
8440 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8441 if (!map->p[i])
8442 return isl_map_free(map);
8445 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8446 return map;
8449 struct isl_set *isl_set_align_divs(struct isl_set *set)
8451 return set_from_map(isl_map_align_divs(set_to_map(set)));
8454 /* Align the divs of the basic maps in "map" to those
8455 * of the basic maps in "list", as well as to the other basic maps in "map".
8456 * The elements in "list" are assumed to have known divs.
8458 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8459 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8461 int i, n;
8463 map = isl_map_compute_divs(map);
8464 map = isl_map_cow(map);
8465 if (!map || !list)
8466 return isl_map_free(map);
8467 if (map->n == 0)
8468 return map;
8470 n = isl_basic_map_list_n_basic_map(list);
8471 for (i = 0; i < n; ++i) {
8472 isl_basic_map *bmap;
8474 bmap = isl_basic_map_list_get_basic_map(list, i);
8475 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8476 isl_basic_map_free(bmap);
8478 if (!map->p[0])
8479 return isl_map_free(map);
8481 return isl_map_align_divs(map);
8484 /* Align the divs of each element of "list" to those of "bmap".
8485 * Both "bmap" and the elements of "list" are assumed to have known divs.
8487 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8488 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8490 int i, n;
8492 if (!list || !bmap)
8493 return isl_basic_map_list_free(list);
8495 n = isl_basic_map_list_n_basic_map(list);
8496 for (i = 0; i < n; ++i) {
8497 isl_basic_map *bmap_i;
8499 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8500 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8501 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8504 return list;
8507 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8508 __isl_take isl_map *map)
8510 if (!set || !map)
8511 goto error;
8512 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8513 map = isl_map_intersect_domain(map, set);
8514 set = isl_map_range(map);
8515 return set;
8516 error:
8517 isl_set_free(set);
8518 isl_map_free(map);
8519 return NULL;
8522 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8523 __isl_take isl_map *map)
8525 return isl_map_align_params_map_map_and(set, map, &set_apply);
8528 /* There is no need to cow as removing empty parts doesn't change
8529 * the meaning of the set.
8531 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8533 int i;
8535 if (!map)
8536 return NULL;
8538 for (i = map->n - 1; i >= 0; --i)
8539 remove_if_empty(map, i);
8541 return map;
8544 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8546 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8549 static __isl_give isl_basic_map *map_copy_basic_map(__isl_keep isl_map *map)
8551 struct isl_basic_map *bmap;
8552 if (!map || map->n == 0)
8553 return NULL;
8554 bmap = map->p[map->n-1];
8555 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8556 return isl_basic_map_copy(bmap);
8559 __isl_give isl_basic_map *isl_map_copy_basic_map(__isl_keep isl_map *map)
8561 return map_copy_basic_map(map);
8564 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8566 return bset_from_bmap(map_copy_basic_map(set_to_map(set)));
8569 static __isl_give isl_map *map_drop_basic_map(__isl_take isl_map *map,
8570 __isl_keep isl_basic_map *bmap)
8572 int i;
8574 if (!map || !bmap)
8575 goto error;
8576 for (i = map->n-1; i >= 0; --i) {
8577 if (map->p[i] != bmap)
8578 continue;
8579 map = isl_map_cow(map);
8580 if (!map)
8581 goto error;
8582 isl_basic_map_free(map->p[i]);
8583 if (i != map->n-1) {
8584 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8585 map->p[i] = map->p[map->n-1];
8587 map->n--;
8588 return map;
8590 return map;
8591 error:
8592 isl_map_free(map);
8593 return NULL;
8596 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8597 __isl_keep isl_basic_map *bmap)
8599 return map_drop_basic_map(map, bmap);
8602 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8603 struct isl_basic_set *bset)
8605 return set_from_map(map_drop_basic_map(set_to_map(set),
8606 bset_to_bmap(bset)));
8609 /* Given two basic sets bset1 and bset2, compute the maximal difference
8610 * between the values of dimension pos in bset1 and those in bset2
8611 * for any common value of the parameters and dimensions preceding pos.
8613 static enum isl_lp_result basic_set_maximal_difference_at(
8614 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8615 int pos, isl_int *opt)
8617 isl_basic_map *bmap1;
8618 isl_basic_map *bmap2;
8619 struct isl_ctx *ctx;
8620 struct isl_vec *obj;
8621 unsigned total;
8622 unsigned nparam;
8623 unsigned dim1;
8624 enum isl_lp_result res;
8626 if (!bset1 || !bset2)
8627 return isl_lp_error;
8629 nparam = isl_basic_set_n_param(bset1);
8630 dim1 = isl_basic_set_n_dim(bset1);
8632 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8633 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8634 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8635 isl_dim_out, 0, pos);
8636 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8637 isl_dim_out, 0, pos);
8638 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8639 if (!bmap1)
8640 return isl_lp_error;
8642 total = isl_basic_map_total_dim(bmap1);
8643 ctx = bmap1->ctx;
8644 obj = isl_vec_alloc(ctx, 1 + total);
8645 if (!obj)
8646 goto error;
8647 isl_seq_clr(obj->block.data, 1 + total);
8648 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8649 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8650 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8651 opt, NULL, NULL);
8652 isl_basic_map_free(bmap1);
8653 isl_vec_free(obj);
8654 return res;
8655 error:
8656 isl_basic_map_free(bmap1);
8657 return isl_lp_error;
8660 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8661 * for any common value of the parameters and dimensions preceding pos
8662 * in both basic sets, the values of dimension pos in bset1 are
8663 * smaller or larger than those in bset2.
8665 * Returns
8666 * 1 if bset1 follows bset2
8667 * -1 if bset1 precedes bset2
8668 * 0 if bset1 and bset2 are incomparable
8669 * -2 if some error occurred.
8671 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8672 struct isl_basic_set *bset2, int pos)
8674 isl_int opt;
8675 enum isl_lp_result res;
8676 int cmp;
8678 isl_int_init(opt);
8680 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8682 if (res == isl_lp_empty)
8683 cmp = 0;
8684 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8685 res == isl_lp_unbounded)
8686 cmp = 1;
8687 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8688 cmp = -1;
8689 else
8690 cmp = -2;
8692 isl_int_clear(opt);
8693 return cmp;
8696 /* Given two basic sets bset1 and bset2, check whether
8697 * for any common value of the parameters and dimensions preceding pos
8698 * there is a value of dimension pos in bset1 that is larger
8699 * than a value of the same dimension in bset2.
8701 * Return
8702 * 1 if there exists such a pair
8703 * 0 if there is no such pair, but there is a pair of equal values
8704 * -1 otherwise
8705 * -2 if some error occurred.
8707 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8708 __isl_keep isl_basic_set *bset2, int pos)
8710 isl_int opt;
8711 enum isl_lp_result res;
8712 int cmp;
8714 isl_int_init(opt);
8716 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8718 if (res == isl_lp_empty)
8719 cmp = -1;
8720 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8721 res == isl_lp_unbounded)
8722 cmp = 1;
8723 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8724 cmp = -1;
8725 else if (res == isl_lp_ok)
8726 cmp = 0;
8727 else
8728 cmp = -2;
8730 isl_int_clear(opt);
8731 return cmp;
8734 /* Given two sets set1 and set2, check whether
8735 * for any common value of the parameters and dimensions preceding pos
8736 * there is a value of dimension pos in set1 that is larger
8737 * than a value of the same dimension in set2.
8739 * Return
8740 * 1 if there exists such a pair
8741 * 0 if there is no such pair, but there is a pair of equal values
8742 * -1 otherwise
8743 * -2 if some error occurred.
8745 int isl_set_follows_at(__isl_keep isl_set *set1,
8746 __isl_keep isl_set *set2, int pos)
8748 int i, j;
8749 int follows = -1;
8751 if (!set1 || !set2)
8752 return -2;
8754 for (i = 0; i < set1->n; ++i)
8755 for (j = 0; j < set2->n; ++j) {
8756 int f;
8757 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8758 if (f == 1 || f == -2)
8759 return f;
8760 if (f > follows)
8761 follows = f;
8764 return follows;
8767 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8768 unsigned pos, isl_int *val)
8770 int i;
8771 int d;
8772 unsigned total;
8774 if (!bmap)
8775 return -1;
8776 total = isl_basic_map_total_dim(bmap);
8777 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8778 for (; d+1 > pos; --d)
8779 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8780 break;
8781 if (d != pos)
8782 continue;
8783 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8784 return 0;
8785 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8786 return 0;
8787 if (!isl_int_is_one(bmap->eq[i][1+d]))
8788 return 0;
8789 if (val)
8790 isl_int_neg(*val, bmap->eq[i][0]);
8791 return 1;
8793 return 0;
8796 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8797 unsigned pos, isl_int *val)
8799 int i;
8800 isl_int v;
8801 isl_int tmp;
8802 int fixed;
8804 if (!map)
8805 return -1;
8806 if (map->n == 0)
8807 return 0;
8808 if (map->n == 1)
8809 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8810 isl_int_init(v);
8811 isl_int_init(tmp);
8812 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8813 for (i = 1; fixed == 1 && i < map->n; ++i) {
8814 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8815 if (fixed == 1 && isl_int_ne(tmp, v))
8816 fixed = 0;
8818 if (val)
8819 isl_int_set(*val, v);
8820 isl_int_clear(tmp);
8821 isl_int_clear(v);
8822 return fixed;
8825 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8826 unsigned pos, isl_int *val)
8828 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
8829 pos, val);
8832 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8833 isl_int *val)
8835 return isl_map_plain_has_fixed_var(set_to_map(set), pos, val);
8838 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8839 enum isl_dim_type type, unsigned pos, isl_int *val)
8841 if (pos >= isl_basic_map_dim(bmap, type))
8842 return -1;
8843 return isl_basic_map_plain_has_fixed_var(bmap,
8844 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8847 /* If "bmap" obviously lies on a hyperplane where the given dimension
8848 * has a fixed value, then return that value.
8849 * Otherwise return NaN.
8851 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8852 __isl_keep isl_basic_map *bmap,
8853 enum isl_dim_type type, unsigned pos)
8855 isl_ctx *ctx;
8856 isl_val *v;
8857 int fixed;
8859 if (!bmap)
8860 return NULL;
8861 ctx = isl_basic_map_get_ctx(bmap);
8862 v = isl_val_alloc(ctx);
8863 if (!v)
8864 return NULL;
8865 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8866 if (fixed < 0)
8867 return isl_val_free(v);
8868 if (fixed) {
8869 isl_int_set_si(v->d, 1);
8870 return v;
8872 isl_val_free(v);
8873 return isl_val_nan(ctx);
8876 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8877 enum isl_dim_type type, unsigned pos, isl_int *val)
8879 if (pos >= isl_map_dim(map, type))
8880 return -1;
8881 return isl_map_plain_has_fixed_var(map,
8882 map_offset(map, type) - 1 + pos, val);
8885 /* If "map" obviously lies on a hyperplane where the given dimension
8886 * has a fixed value, then return that value.
8887 * Otherwise return NaN.
8889 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8890 enum isl_dim_type type, unsigned pos)
8892 isl_ctx *ctx;
8893 isl_val *v;
8894 int fixed;
8896 if (!map)
8897 return NULL;
8898 ctx = isl_map_get_ctx(map);
8899 v = isl_val_alloc(ctx);
8900 if (!v)
8901 return NULL;
8902 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8903 if (fixed < 0)
8904 return isl_val_free(v);
8905 if (fixed) {
8906 isl_int_set_si(v->d, 1);
8907 return v;
8909 isl_val_free(v);
8910 return isl_val_nan(ctx);
8913 /* If "set" obviously lies on a hyperplane where the given dimension
8914 * has a fixed value, then return that value.
8915 * Otherwise return NaN.
8917 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8918 enum isl_dim_type type, unsigned pos)
8920 return isl_map_plain_get_val_if_fixed(set, type, pos);
8923 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8924 enum isl_dim_type type, unsigned pos, isl_int *val)
8926 return isl_map_plain_is_fixed(set, type, pos, val);
8929 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8930 * then return this fixed value in *val.
8932 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8933 unsigned dim, isl_int *val)
8935 return isl_basic_set_plain_has_fixed_var(bset,
8936 isl_basic_set_n_param(bset) + dim, val);
8939 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8940 * then return this fixed value in *val.
8942 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8943 unsigned dim, isl_int *val)
8945 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8948 /* Check if input variable in has fixed value and if so and if val is not NULL,
8949 * then return this fixed value in *val.
8951 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8952 unsigned in, isl_int *val)
8954 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8957 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8958 * and if val is not NULL, then return this lower bound in *val.
8960 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8961 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8963 int i, i_eq = -1, i_ineq = -1;
8964 isl_int *c;
8965 unsigned total;
8966 unsigned nparam;
8968 if (!bset)
8969 return -1;
8970 total = isl_basic_set_total_dim(bset);
8971 nparam = isl_basic_set_n_param(bset);
8972 for (i = 0; i < bset->n_eq; ++i) {
8973 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8974 continue;
8975 if (i_eq != -1)
8976 return 0;
8977 i_eq = i;
8979 for (i = 0; i < bset->n_ineq; ++i) {
8980 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8981 continue;
8982 if (i_eq != -1 || i_ineq != -1)
8983 return 0;
8984 i_ineq = i;
8986 if (i_eq == -1 && i_ineq == -1)
8987 return 0;
8988 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8989 /* The coefficient should always be one due to normalization. */
8990 if (!isl_int_is_one(c[1+nparam+dim]))
8991 return 0;
8992 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8993 return 0;
8994 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8995 total - nparam - dim - 1) != -1)
8996 return 0;
8997 if (val)
8998 isl_int_neg(*val, c[0]);
8999 return 1;
9002 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
9003 unsigned dim, isl_int *val)
9005 int i;
9006 isl_int v;
9007 isl_int tmp;
9008 int fixed;
9010 if (!set)
9011 return -1;
9012 if (set->n == 0)
9013 return 0;
9014 if (set->n == 1)
9015 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9016 dim, val);
9017 isl_int_init(v);
9018 isl_int_init(tmp);
9019 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9020 dim, &v);
9021 for (i = 1; fixed == 1 && i < set->n; ++i) {
9022 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
9023 dim, &tmp);
9024 if (fixed == 1 && isl_int_ne(tmp, v))
9025 fixed = 0;
9027 if (val)
9028 isl_int_set(*val, v);
9029 isl_int_clear(tmp);
9030 isl_int_clear(v);
9031 return fixed;
9034 /* Return -1 if the constraint "c1" should be sorted before "c2"
9035 * and 1 if it should be sorted after "c2".
9036 * Return 0 if the two constraints are the same (up to the constant term).
9038 * In particular, if a constraint involves later variables than another
9039 * then it is sorted after this other constraint.
9040 * uset_gist depends on constraints without existentially quantified
9041 * variables sorting first.
9043 * For constraints that have the same latest variable, those
9044 * with the same coefficient for this latest variable (first in absolute value
9045 * and then in actual value) are grouped together.
9046 * This is useful for detecting pairs of constraints that can
9047 * be chained in their printed representation.
9049 * Finally, within a group, constraints are sorted according to
9050 * their coefficients (excluding the constant term).
9052 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9054 isl_int **c1 = (isl_int **) p1;
9055 isl_int **c2 = (isl_int **) p2;
9056 int l1, l2;
9057 unsigned size = *(unsigned *) arg;
9058 int cmp;
9060 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9061 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9063 if (l1 != l2)
9064 return l1 - l2;
9066 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9067 if (cmp != 0)
9068 return cmp;
9069 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9070 if (cmp != 0)
9071 return -cmp;
9073 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9076 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9077 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9078 * and 0 if the two constraints are the same (up to the constant term).
9080 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9081 isl_int *c1, isl_int *c2)
9083 unsigned total;
9085 if (!bmap)
9086 return -2;
9087 total = isl_basic_map_total_dim(bmap);
9088 return sort_constraint_cmp(&c1, &c2, &total);
9091 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9092 __isl_take isl_basic_map *bmap)
9094 unsigned total;
9096 if (!bmap)
9097 return NULL;
9098 if (bmap->n_ineq == 0)
9099 return bmap;
9100 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9101 return bmap;
9102 total = isl_basic_map_total_dim(bmap);
9103 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9104 &sort_constraint_cmp, &total) < 0)
9105 return isl_basic_map_free(bmap);
9106 return bmap;
9109 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9110 __isl_take isl_basic_set *bset)
9112 isl_basic_map *bmap = bset_to_bmap(bset);
9113 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9116 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9118 if (!bmap)
9119 return NULL;
9120 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9121 return bmap;
9122 bmap = isl_basic_map_remove_redundancies(bmap);
9123 bmap = isl_basic_map_sort_constraints(bmap);
9124 if (bmap)
9125 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9126 return bmap;
9129 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9131 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset)));
9134 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9135 const __isl_keep isl_basic_map *bmap2)
9137 int i, cmp;
9138 unsigned total;
9140 if (!bmap1 || !bmap2)
9141 return -1;
9143 if (bmap1 == bmap2)
9144 return 0;
9145 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9146 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9147 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9148 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9149 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9150 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9151 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9152 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9153 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9154 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9155 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9156 return 0;
9157 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9158 return 1;
9159 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9160 return -1;
9161 if (bmap1->n_eq != bmap2->n_eq)
9162 return bmap1->n_eq - bmap2->n_eq;
9163 if (bmap1->n_ineq != bmap2->n_ineq)
9164 return bmap1->n_ineq - bmap2->n_ineq;
9165 if (bmap1->n_div != bmap2->n_div)
9166 return bmap1->n_div - bmap2->n_div;
9167 total = isl_basic_map_total_dim(bmap1);
9168 for (i = 0; i < bmap1->n_eq; ++i) {
9169 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9170 if (cmp)
9171 return cmp;
9173 for (i = 0; i < bmap1->n_ineq; ++i) {
9174 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9175 if (cmp)
9176 return cmp;
9178 for (i = 0; i < bmap1->n_div; ++i) {
9179 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9180 if (cmp)
9181 return cmp;
9183 return 0;
9186 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9187 const __isl_keep isl_basic_set *bset2)
9189 return isl_basic_map_plain_cmp(bset1, bset2);
9192 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9194 int i, cmp;
9196 if (set1 == set2)
9197 return 0;
9198 if (set1->n != set2->n)
9199 return set1->n - set2->n;
9201 for (i = 0; i < set1->n; ++i) {
9202 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9203 if (cmp)
9204 return cmp;
9207 return 0;
9210 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9211 __isl_keep isl_basic_map *bmap2)
9213 if (!bmap1 || !bmap2)
9214 return isl_bool_error;
9215 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9218 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9219 __isl_keep isl_basic_set *bset2)
9221 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9222 bset_to_bmap(bset2));
9225 static int qsort_bmap_cmp(const void *p1, const void *p2)
9227 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9228 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9230 return isl_basic_map_plain_cmp(bmap1, bmap2);
9233 /* Sort the basic maps of "map" and remove duplicate basic maps.
9235 * While removing basic maps, we make sure that the basic maps remain
9236 * sorted because isl_map_normalize expects the basic maps of the result
9237 * to be sorted.
9239 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9241 int i, j;
9243 map = isl_map_remove_empty_parts(map);
9244 if (!map)
9245 return NULL;
9246 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9247 for (i = map->n - 1; i >= 1; --i) {
9248 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9249 continue;
9250 isl_basic_map_free(map->p[i-1]);
9251 for (j = i; j < map->n; ++j)
9252 map->p[j - 1] = map->p[j];
9253 map->n--;
9256 return map;
9259 /* Remove obvious duplicates among the basic maps of "map".
9261 * Unlike isl_map_normalize, this function does not remove redundant
9262 * constraints and only removes duplicates that have exactly the same
9263 * constraints in the input. It does sort the constraints and
9264 * the basic maps to ease the detection of duplicates.
9266 * If "map" has already been normalized or if the basic maps are
9267 * disjoint, then there can be no duplicates.
9269 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9271 int i;
9272 isl_basic_map *bmap;
9274 if (!map)
9275 return NULL;
9276 if (map->n <= 1)
9277 return map;
9278 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9279 return map;
9280 for (i = 0; i < map->n; ++i) {
9281 bmap = isl_basic_map_copy(map->p[i]);
9282 bmap = isl_basic_map_sort_constraints(bmap);
9283 if (!bmap)
9284 return isl_map_free(map);
9285 isl_basic_map_free(map->p[i]);
9286 map->p[i] = bmap;
9289 map = sort_and_remove_duplicates(map);
9290 return map;
9293 /* We normalize in place, but if anything goes wrong we need
9294 * to return NULL, so we need to make sure we don't change the
9295 * meaning of any possible other copies of map.
9297 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9299 int i;
9300 struct isl_basic_map *bmap;
9302 if (!map)
9303 return NULL;
9304 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9305 return map;
9306 for (i = 0; i < map->n; ++i) {
9307 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9308 if (!bmap)
9309 goto error;
9310 isl_basic_map_free(map->p[i]);
9311 map->p[i] = bmap;
9314 map = sort_and_remove_duplicates(map);
9315 if (map)
9316 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9317 return map;
9318 error:
9319 isl_map_free(map);
9320 return NULL;
9323 struct isl_set *isl_set_normalize(struct isl_set *set)
9325 return set_from_map(isl_map_normalize(set_to_map(set)));
9328 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9329 __isl_keep isl_map *map2)
9331 int i;
9332 isl_bool equal;
9334 if (!map1 || !map2)
9335 return isl_bool_error;
9337 if (map1 == map2)
9338 return isl_bool_true;
9339 if (!isl_space_is_equal(map1->dim, map2->dim))
9340 return isl_bool_false;
9342 map1 = isl_map_copy(map1);
9343 map2 = isl_map_copy(map2);
9344 map1 = isl_map_normalize(map1);
9345 map2 = isl_map_normalize(map2);
9346 if (!map1 || !map2)
9347 goto error;
9348 equal = map1->n == map2->n;
9349 for (i = 0; equal && i < map1->n; ++i) {
9350 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9351 if (equal < 0)
9352 goto error;
9354 isl_map_free(map1);
9355 isl_map_free(map2);
9356 return equal;
9357 error:
9358 isl_map_free(map1);
9359 isl_map_free(map2);
9360 return isl_bool_error;
9363 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9364 __isl_keep isl_set *set2)
9366 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9369 /* Return an interval that ranges from min to max (inclusive)
9371 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9372 isl_int min, isl_int max)
9374 int k;
9375 struct isl_basic_set *bset = NULL;
9377 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9378 if (!bset)
9379 goto error;
9381 k = isl_basic_set_alloc_inequality(bset);
9382 if (k < 0)
9383 goto error;
9384 isl_int_set_si(bset->ineq[k][1], 1);
9385 isl_int_neg(bset->ineq[k][0], min);
9387 k = isl_basic_set_alloc_inequality(bset);
9388 if (k < 0)
9389 goto error;
9390 isl_int_set_si(bset->ineq[k][1], -1);
9391 isl_int_set(bset->ineq[k][0], max);
9393 return bset;
9394 error:
9395 isl_basic_set_free(bset);
9396 return NULL;
9399 /* Return the basic maps in "map" as a list.
9401 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9402 __isl_keep isl_map *map)
9404 int i;
9405 isl_ctx *ctx;
9406 isl_basic_map_list *list;
9408 if (!map)
9409 return NULL;
9410 ctx = isl_map_get_ctx(map);
9411 list = isl_basic_map_list_alloc(ctx, map->n);
9413 for (i = 0; i < map->n; ++i) {
9414 isl_basic_map *bmap;
9416 bmap = isl_basic_map_copy(map->p[i]);
9417 list = isl_basic_map_list_add(list, bmap);
9420 return list;
9423 /* Return the intersection of the elements in the non-empty list "list".
9424 * All elements are assumed to live in the same space.
9426 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9427 __isl_take isl_basic_map_list *list)
9429 int i, n;
9430 isl_basic_map *bmap;
9432 if (!list)
9433 return NULL;
9434 n = isl_basic_map_list_n_basic_map(list);
9435 if (n < 1)
9436 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9437 "expecting non-empty list", goto error);
9439 bmap = isl_basic_map_list_get_basic_map(list, 0);
9440 for (i = 1; i < n; ++i) {
9441 isl_basic_map *bmap_i;
9443 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9444 bmap = isl_basic_map_intersect(bmap, bmap_i);
9447 isl_basic_map_list_free(list);
9448 return bmap;
9449 error:
9450 isl_basic_map_list_free(list);
9451 return NULL;
9454 /* Return the intersection of the elements in the non-empty list "list".
9455 * All elements are assumed to live in the same space.
9457 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9458 __isl_take isl_basic_set_list *list)
9460 return isl_basic_map_list_intersect(list);
9463 /* Return the union of the elements of "list".
9464 * The list is required to have at least one element.
9466 __isl_give isl_set *isl_basic_set_list_union(
9467 __isl_take isl_basic_set_list *list)
9469 int i, n;
9470 isl_space *space;
9471 isl_basic_set *bset;
9472 isl_set *set;
9474 if (!list)
9475 return NULL;
9476 n = isl_basic_set_list_n_basic_set(list);
9477 if (n < 1)
9478 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9479 "expecting non-empty list", goto error);
9481 bset = isl_basic_set_list_get_basic_set(list, 0);
9482 space = isl_basic_set_get_space(bset);
9483 isl_basic_set_free(bset);
9485 set = isl_set_alloc_space(space, n, 0);
9486 for (i = 0; i < n; ++i) {
9487 bset = isl_basic_set_list_get_basic_set(list, i);
9488 set = isl_set_add_basic_set(set, bset);
9491 isl_basic_set_list_free(list);
9492 return set;
9493 error:
9494 isl_basic_set_list_free(list);
9495 return NULL;
9498 /* Return the union of the elements in the non-empty list "list".
9499 * All elements are assumed to live in the same space.
9501 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9503 int i, n;
9504 isl_set *set;
9506 if (!list)
9507 return NULL;
9508 n = isl_set_list_n_set(list);
9509 if (n < 1)
9510 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9511 "expecting non-empty list", goto error);
9513 set = isl_set_list_get_set(list, 0);
9514 for (i = 1; i < n; ++i) {
9515 isl_set *set_i;
9517 set_i = isl_set_list_get_set(list, i);
9518 set = isl_set_union(set, set_i);
9521 isl_set_list_free(list);
9522 return set;
9523 error:
9524 isl_set_list_free(list);
9525 return NULL;
9528 /* Return the Cartesian product of the basic sets in list (in the given order).
9530 __isl_give isl_basic_set *isl_basic_set_list_product(
9531 __isl_take struct isl_basic_set_list *list)
9533 int i;
9534 unsigned dim;
9535 unsigned nparam;
9536 unsigned extra;
9537 unsigned n_eq;
9538 unsigned n_ineq;
9539 struct isl_basic_set *product = NULL;
9541 if (!list)
9542 goto error;
9543 isl_assert(list->ctx, list->n > 0, goto error);
9544 isl_assert(list->ctx, list->p[0], goto error);
9545 nparam = isl_basic_set_n_param(list->p[0]);
9546 dim = isl_basic_set_n_dim(list->p[0]);
9547 extra = list->p[0]->n_div;
9548 n_eq = list->p[0]->n_eq;
9549 n_ineq = list->p[0]->n_ineq;
9550 for (i = 1; i < list->n; ++i) {
9551 isl_assert(list->ctx, list->p[i], goto error);
9552 isl_assert(list->ctx,
9553 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9554 dim += isl_basic_set_n_dim(list->p[i]);
9555 extra += list->p[i]->n_div;
9556 n_eq += list->p[i]->n_eq;
9557 n_ineq += list->p[i]->n_ineq;
9559 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9560 n_eq, n_ineq);
9561 if (!product)
9562 goto error;
9563 dim = 0;
9564 for (i = 0; i < list->n; ++i) {
9565 isl_basic_set_add_constraints(product,
9566 isl_basic_set_copy(list->p[i]), dim);
9567 dim += isl_basic_set_n_dim(list->p[i]);
9569 isl_basic_set_list_free(list);
9570 return product;
9571 error:
9572 isl_basic_set_free(product);
9573 isl_basic_set_list_free(list);
9574 return NULL;
9577 struct isl_basic_map *isl_basic_map_product(
9578 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9580 isl_space *dim_result = NULL;
9581 struct isl_basic_map *bmap;
9582 unsigned in1, in2, out1, out2, nparam, total, pos;
9583 struct isl_dim_map *dim_map1, *dim_map2;
9585 if (!bmap1 || !bmap2)
9586 goto error;
9588 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9589 bmap2->dim, isl_dim_param), goto error);
9590 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9591 isl_space_copy(bmap2->dim));
9593 in1 = isl_basic_map_n_in(bmap1);
9594 in2 = isl_basic_map_n_in(bmap2);
9595 out1 = isl_basic_map_n_out(bmap1);
9596 out2 = isl_basic_map_n_out(bmap2);
9597 nparam = isl_basic_map_n_param(bmap1);
9599 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9600 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9601 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9602 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9603 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9604 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9605 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9606 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9607 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9608 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9609 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9611 bmap = isl_basic_map_alloc_space(dim_result,
9612 bmap1->n_div + bmap2->n_div,
9613 bmap1->n_eq + bmap2->n_eq,
9614 bmap1->n_ineq + bmap2->n_ineq);
9615 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9616 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9617 bmap = isl_basic_map_simplify(bmap);
9618 return isl_basic_map_finalize(bmap);
9619 error:
9620 isl_basic_map_free(bmap1);
9621 isl_basic_map_free(bmap2);
9622 return NULL;
9625 __isl_give isl_basic_map *isl_basic_map_flat_product(
9626 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9628 isl_basic_map *prod;
9630 prod = isl_basic_map_product(bmap1, bmap2);
9631 prod = isl_basic_map_flatten(prod);
9632 return prod;
9635 __isl_give isl_basic_set *isl_basic_set_flat_product(
9636 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9638 return isl_basic_map_flat_range_product(bset1, bset2);
9641 __isl_give isl_basic_map *isl_basic_map_domain_product(
9642 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9644 isl_space *space_result = NULL;
9645 isl_basic_map *bmap;
9646 unsigned in1, in2, out, nparam, total, pos;
9647 struct isl_dim_map *dim_map1, *dim_map2;
9649 if (!bmap1 || !bmap2)
9650 goto error;
9652 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9653 isl_space_copy(bmap2->dim));
9655 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9656 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9657 out = isl_basic_map_dim(bmap1, isl_dim_out);
9658 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9660 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9661 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9662 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9663 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9664 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9665 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9666 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9667 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9668 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9669 isl_dim_map_div(dim_map1, bmap1, pos += out);
9670 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9672 bmap = isl_basic_map_alloc_space(space_result,
9673 bmap1->n_div + bmap2->n_div,
9674 bmap1->n_eq + bmap2->n_eq,
9675 bmap1->n_ineq + bmap2->n_ineq);
9676 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9677 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9678 bmap = isl_basic_map_simplify(bmap);
9679 return isl_basic_map_finalize(bmap);
9680 error:
9681 isl_basic_map_free(bmap1);
9682 isl_basic_map_free(bmap2);
9683 return NULL;
9686 __isl_give isl_basic_map *isl_basic_map_range_product(
9687 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9689 int rational;
9690 isl_space *dim_result = NULL;
9691 isl_basic_map *bmap;
9692 unsigned in, out1, out2, nparam, total, pos;
9693 struct isl_dim_map *dim_map1, *dim_map2;
9695 rational = isl_basic_map_is_rational(bmap1);
9696 if (rational >= 0 && rational)
9697 rational = isl_basic_map_is_rational(bmap2);
9698 if (!bmap1 || !bmap2 || rational < 0)
9699 goto error;
9701 if (!isl_space_match(bmap1->dim, isl_dim_param,
9702 bmap2->dim, isl_dim_param))
9703 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9704 "parameters don't match", goto error);
9706 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9707 isl_space_copy(bmap2->dim));
9709 in = isl_basic_map_dim(bmap1, isl_dim_in);
9710 out1 = isl_basic_map_n_out(bmap1);
9711 out2 = isl_basic_map_n_out(bmap2);
9712 nparam = isl_basic_map_n_param(bmap1);
9714 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9715 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9716 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9717 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9718 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9719 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9720 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9721 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9722 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9723 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9724 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9726 bmap = isl_basic_map_alloc_space(dim_result,
9727 bmap1->n_div + bmap2->n_div,
9728 bmap1->n_eq + bmap2->n_eq,
9729 bmap1->n_ineq + bmap2->n_ineq);
9730 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9731 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9732 if (rational)
9733 bmap = isl_basic_map_set_rational(bmap);
9734 bmap = isl_basic_map_simplify(bmap);
9735 return isl_basic_map_finalize(bmap);
9736 error:
9737 isl_basic_map_free(bmap1);
9738 isl_basic_map_free(bmap2);
9739 return NULL;
9742 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9743 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9745 isl_basic_map *prod;
9747 prod = isl_basic_map_range_product(bmap1, bmap2);
9748 prod = isl_basic_map_flatten_range(prod);
9749 return prod;
9752 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9753 * and collect the results.
9754 * The result live in the space obtained by calling "space_product"
9755 * on the spaces of "map1" and "map2".
9756 * If "remove_duplicates" is set then the result may contain duplicates
9757 * (even if the inputs do not) and so we try and remove the obvious
9758 * duplicates.
9760 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9761 __isl_take isl_map *map2,
9762 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9763 __isl_take isl_space *right),
9764 __isl_give isl_basic_map *(*basic_map_product)(
9765 __isl_take isl_basic_map *left,
9766 __isl_take isl_basic_map *right),
9767 int remove_duplicates)
9769 unsigned flags = 0;
9770 struct isl_map *result;
9771 int i, j;
9773 if (!map1 || !map2)
9774 goto error;
9776 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9777 map2->dim, isl_dim_param), goto error);
9779 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9780 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9781 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9783 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9784 isl_space_copy(map2->dim)),
9785 map1->n * map2->n, flags);
9786 if (!result)
9787 goto error;
9788 for (i = 0; i < map1->n; ++i)
9789 for (j = 0; j < map2->n; ++j) {
9790 struct isl_basic_map *part;
9791 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9792 isl_basic_map_copy(map2->p[j]));
9793 if (isl_basic_map_is_empty(part))
9794 isl_basic_map_free(part);
9795 else
9796 result = isl_map_add_basic_map(result, part);
9797 if (!result)
9798 goto error;
9800 if (remove_duplicates)
9801 result = isl_map_remove_obvious_duplicates(result);
9802 isl_map_free(map1);
9803 isl_map_free(map2);
9804 return result;
9805 error:
9806 isl_map_free(map1);
9807 isl_map_free(map2);
9808 return NULL;
9811 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9813 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9814 __isl_take isl_map *map2)
9816 return map_product(map1, map2, &isl_space_product,
9817 &isl_basic_map_product, 0);
9820 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9821 __isl_take isl_map *map2)
9823 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9826 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9828 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9829 __isl_take isl_map *map2)
9831 isl_map *prod;
9833 prod = isl_map_product(map1, map2);
9834 prod = isl_map_flatten(prod);
9835 return prod;
9838 /* Given two set A and B, construct its Cartesian product A x B.
9840 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9842 return isl_map_range_product(set1, set2);
9845 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9846 __isl_take isl_set *set2)
9848 return isl_map_flat_range_product(set1, set2);
9851 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9853 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9854 __isl_take isl_map *map2)
9856 return map_product(map1, map2, &isl_space_domain_product,
9857 &isl_basic_map_domain_product, 1);
9860 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9862 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9863 __isl_take isl_map *map2)
9865 return map_product(map1, map2, &isl_space_range_product,
9866 &isl_basic_map_range_product, 1);
9869 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9870 __isl_take isl_map *map2)
9872 return isl_map_align_params_map_map_and(map1, map2,
9873 &map_domain_product_aligned);
9876 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9877 __isl_take isl_map *map2)
9879 return isl_map_align_params_map_map_and(map1, map2,
9880 &map_range_product_aligned);
9883 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9885 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9887 isl_space *space;
9888 int total1, keep1, total2, keep2;
9890 if (!map)
9891 return NULL;
9892 if (!isl_space_domain_is_wrapping(map->dim) ||
9893 !isl_space_range_is_wrapping(map->dim))
9894 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9895 "not a product", return isl_map_free(map));
9897 space = isl_map_get_space(map);
9898 total1 = isl_space_dim(space, isl_dim_in);
9899 total2 = isl_space_dim(space, isl_dim_out);
9900 space = isl_space_factor_domain(space);
9901 keep1 = isl_space_dim(space, isl_dim_in);
9902 keep2 = isl_space_dim(space, isl_dim_out);
9903 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9904 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9905 map = isl_map_reset_space(map, space);
9907 return map;
9910 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9912 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9914 isl_space *space;
9915 int total1, keep1, total2, keep2;
9917 if (!map)
9918 return NULL;
9919 if (!isl_space_domain_is_wrapping(map->dim) ||
9920 !isl_space_range_is_wrapping(map->dim))
9921 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9922 "not a product", return isl_map_free(map));
9924 space = isl_map_get_space(map);
9925 total1 = isl_space_dim(space, isl_dim_in);
9926 total2 = isl_space_dim(space, isl_dim_out);
9927 space = isl_space_factor_range(space);
9928 keep1 = isl_space_dim(space, isl_dim_in);
9929 keep2 = isl_space_dim(space, isl_dim_out);
9930 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9931 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9932 map = isl_map_reset_space(map, space);
9934 return map;
9937 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9939 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9941 isl_space *space;
9942 int total, keep;
9944 if (!map)
9945 return NULL;
9946 if (!isl_space_domain_is_wrapping(map->dim))
9947 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9948 "domain is not a product", return isl_map_free(map));
9950 space = isl_map_get_space(map);
9951 total = isl_space_dim(space, isl_dim_in);
9952 space = isl_space_domain_factor_domain(space);
9953 keep = isl_space_dim(space, isl_dim_in);
9954 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9955 map = isl_map_reset_space(map, space);
9957 return map;
9960 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9962 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9964 isl_space *space;
9965 int total, keep;
9967 if (!map)
9968 return NULL;
9969 if (!isl_space_domain_is_wrapping(map->dim))
9970 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9971 "domain is not a product", return isl_map_free(map));
9973 space = isl_map_get_space(map);
9974 total = isl_space_dim(space, isl_dim_in);
9975 space = isl_space_domain_factor_range(space);
9976 keep = isl_space_dim(space, isl_dim_in);
9977 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9978 map = isl_map_reset_space(map, space);
9980 return map;
9983 /* Given a map A -> [B -> C], extract the map A -> B.
9985 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9987 isl_space *space;
9988 int total, keep;
9990 if (!map)
9991 return NULL;
9992 if (!isl_space_range_is_wrapping(map->dim))
9993 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9994 "range is not a product", return isl_map_free(map));
9996 space = isl_map_get_space(map);
9997 total = isl_space_dim(space, isl_dim_out);
9998 space = isl_space_range_factor_domain(space);
9999 keep = isl_space_dim(space, isl_dim_out);
10000 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10001 map = isl_map_reset_space(map, space);
10003 return map;
10006 /* Given a map A -> [B -> C], extract the map A -> C.
10008 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10010 isl_space *space;
10011 int total, keep;
10013 if (!map)
10014 return NULL;
10015 if (!isl_space_range_is_wrapping(map->dim))
10016 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10017 "range is not a product", return isl_map_free(map));
10019 space = isl_map_get_space(map);
10020 total = isl_space_dim(space, isl_dim_out);
10021 space = isl_space_range_factor_range(space);
10022 keep = isl_space_dim(space, isl_dim_out);
10023 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10024 map = isl_map_reset_space(map, space);
10026 return map;
10029 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10031 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10032 __isl_take isl_map *map2)
10034 isl_map *prod;
10036 prod = isl_map_domain_product(map1, map2);
10037 prod = isl_map_flatten_domain(prod);
10038 return prod;
10041 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10043 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10044 __isl_take isl_map *map2)
10046 isl_map *prod;
10048 prod = isl_map_range_product(map1, map2);
10049 prod = isl_map_flatten_range(prod);
10050 return prod;
10053 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10055 int i;
10056 uint32_t hash = isl_hash_init();
10057 unsigned total;
10059 if (!bmap)
10060 return 0;
10061 bmap = isl_basic_map_copy(bmap);
10062 bmap = isl_basic_map_normalize(bmap);
10063 if (!bmap)
10064 return 0;
10065 total = isl_basic_map_total_dim(bmap);
10066 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10067 for (i = 0; i < bmap->n_eq; ++i) {
10068 uint32_t c_hash;
10069 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10070 isl_hash_hash(hash, c_hash);
10072 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10073 for (i = 0; i < bmap->n_ineq; ++i) {
10074 uint32_t c_hash;
10075 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10076 isl_hash_hash(hash, c_hash);
10078 isl_hash_byte(hash, bmap->n_div & 0xFF);
10079 for (i = 0; i < bmap->n_div; ++i) {
10080 uint32_t c_hash;
10081 if (isl_int_is_zero(bmap->div[i][0]))
10082 continue;
10083 isl_hash_byte(hash, i & 0xFF);
10084 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10085 isl_hash_hash(hash, c_hash);
10087 isl_basic_map_free(bmap);
10088 return hash;
10091 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10093 return isl_basic_map_get_hash(bset_to_bmap(bset));
10096 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10098 int i;
10099 uint32_t hash;
10101 if (!map)
10102 return 0;
10103 map = isl_map_copy(map);
10104 map = isl_map_normalize(map);
10105 if (!map)
10106 return 0;
10108 hash = isl_hash_init();
10109 for (i = 0; i < map->n; ++i) {
10110 uint32_t bmap_hash;
10111 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10112 isl_hash_hash(hash, bmap_hash);
10115 isl_map_free(map);
10117 return hash;
10120 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10122 return isl_map_get_hash(set_to_map(set));
10125 /* Check if the value for dimension dim is completely determined
10126 * by the values of the other parameters and variables.
10127 * That is, check if dimension dim is involved in an equality.
10129 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10131 int i;
10132 unsigned nparam;
10134 if (!bset)
10135 return -1;
10136 nparam = isl_basic_set_n_param(bset);
10137 for (i = 0; i < bset->n_eq; ++i)
10138 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10139 return 1;
10140 return 0;
10143 /* Check if the value for dimension dim is completely determined
10144 * by the values of the other parameters and variables.
10145 * That is, check if dimension dim is involved in an equality
10146 * for each of the subsets.
10148 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10150 int i;
10152 if (!set)
10153 return -1;
10154 for (i = 0; i < set->n; ++i) {
10155 int unique;
10156 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10157 if (unique != 1)
10158 return unique;
10160 return 1;
10163 /* Return the number of basic maps in the (current) representation of "map".
10165 int isl_map_n_basic_map(__isl_keep isl_map *map)
10167 return map ? map->n : 0;
10170 int isl_set_n_basic_set(__isl_keep isl_set *set)
10172 return set ? set->n : 0;
10175 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10176 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10178 int i;
10180 if (!map)
10181 return isl_stat_error;
10183 for (i = 0; i < map->n; ++i)
10184 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10185 return isl_stat_error;
10187 return isl_stat_ok;
10190 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10191 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10193 int i;
10195 if (!set)
10196 return isl_stat_error;
10198 for (i = 0; i < set->n; ++i)
10199 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10200 return isl_stat_error;
10202 return isl_stat_ok;
10205 /* Return a list of basic sets, the union of which is equal to "set".
10207 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10208 __isl_keep isl_set *set)
10210 int i;
10211 isl_basic_set_list *list;
10213 if (!set)
10214 return NULL;
10216 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10217 for (i = 0; i < set->n; ++i) {
10218 isl_basic_set *bset;
10220 bset = isl_basic_set_copy(set->p[i]);
10221 list = isl_basic_set_list_add(list, bset);
10224 return list;
10227 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10229 isl_space *dim;
10231 if (!bset)
10232 return NULL;
10234 bset = isl_basic_set_cow(bset);
10235 if (!bset)
10236 return NULL;
10238 dim = isl_basic_set_get_space(bset);
10239 dim = isl_space_lift(dim, bset->n_div);
10240 if (!dim)
10241 goto error;
10242 isl_space_free(bset->dim);
10243 bset->dim = dim;
10244 bset->extra -= bset->n_div;
10245 bset->n_div = 0;
10247 bset = isl_basic_set_finalize(bset);
10249 return bset;
10250 error:
10251 isl_basic_set_free(bset);
10252 return NULL;
10255 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10257 int i;
10258 isl_space *dim;
10259 unsigned n_div;
10261 set = isl_set_align_divs(set);
10263 if (!set)
10264 return NULL;
10266 set = isl_set_cow(set);
10267 if (!set)
10268 return NULL;
10270 n_div = set->p[0]->n_div;
10271 dim = isl_set_get_space(set);
10272 dim = isl_space_lift(dim, n_div);
10273 if (!dim)
10274 goto error;
10275 isl_space_free(set->dim);
10276 set->dim = dim;
10278 for (i = 0; i < set->n; ++i) {
10279 set->p[i] = isl_basic_set_lift(set->p[i]);
10280 if (!set->p[i])
10281 goto error;
10284 return set;
10285 error:
10286 isl_set_free(set);
10287 return NULL;
10290 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10292 isl_space *dim;
10293 struct isl_basic_map *bmap;
10294 unsigned n_set;
10295 unsigned n_div;
10296 unsigned n_param;
10297 unsigned total;
10298 int i, k, l;
10300 set = isl_set_align_divs(set);
10302 if (!set)
10303 return NULL;
10305 dim = isl_set_get_space(set);
10306 if (set->n == 0 || set->p[0]->n_div == 0) {
10307 isl_set_free(set);
10308 return isl_map_identity(isl_space_map_from_set(dim));
10311 n_div = set->p[0]->n_div;
10312 dim = isl_space_map_from_set(dim);
10313 n_param = isl_space_dim(dim, isl_dim_param);
10314 n_set = isl_space_dim(dim, isl_dim_in);
10315 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10316 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10317 for (i = 0; i < n_set; ++i)
10318 bmap = var_equal(bmap, i);
10320 total = n_param + n_set + n_set + n_div;
10321 for (i = 0; i < n_div; ++i) {
10322 k = isl_basic_map_alloc_inequality(bmap);
10323 if (k < 0)
10324 goto error;
10325 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10326 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10327 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10328 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10329 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10330 set->p[0]->div[i][0]);
10332 l = isl_basic_map_alloc_inequality(bmap);
10333 if (l < 0)
10334 goto error;
10335 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10336 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10337 set->p[0]->div[i][0]);
10338 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10341 isl_set_free(set);
10342 bmap = isl_basic_map_simplify(bmap);
10343 bmap = isl_basic_map_finalize(bmap);
10344 return isl_map_from_basic_map(bmap);
10345 error:
10346 isl_set_free(set);
10347 isl_basic_map_free(bmap);
10348 return NULL;
10351 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10353 unsigned dim;
10354 int size = 0;
10356 if (!bset)
10357 return -1;
10359 dim = isl_basic_set_total_dim(bset);
10360 size += bset->n_eq * (1 + dim);
10361 size += bset->n_ineq * (1 + dim);
10362 size += bset->n_div * (2 + dim);
10364 return size;
10367 int isl_set_size(__isl_keep isl_set *set)
10369 int i;
10370 int size = 0;
10372 if (!set)
10373 return -1;
10375 for (i = 0; i < set->n; ++i)
10376 size += isl_basic_set_size(set->p[i]);
10378 return size;
10381 /* Check if there is any lower bound (if lower == 0) and/or upper
10382 * bound (if upper == 0) on the specified dim.
10384 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10385 enum isl_dim_type type, unsigned pos, int lower, int upper)
10387 int i;
10389 if (!bmap)
10390 return isl_bool_error;
10392 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10393 return isl_bool_error);
10395 pos += isl_basic_map_offset(bmap, type);
10397 for (i = 0; i < bmap->n_div; ++i) {
10398 if (isl_int_is_zero(bmap->div[i][0]))
10399 continue;
10400 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10401 return isl_bool_true;
10404 for (i = 0; i < bmap->n_eq; ++i)
10405 if (!isl_int_is_zero(bmap->eq[i][pos]))
10406 return isl_bool_true;
10408 for (i = 0; i < bmap->n_ineq; ++i) {
10409 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10410 if (sgn > 0)
10411 lower = 1;
10412 if (sgn < 0)
10413 upper = 1;
10416 return lower && upper;
10419 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10420 enum isl_dim_type type, unsigned pos)
10422 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10425 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10426 enum isl_dim_type type, unsigned pos)
10428 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10431 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10432 enum isl_dim_type type, unsigned pos)
10434 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10437 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10438 enum isl_dim_type type, unsigned pos)
10440 int i;
10442 if (!map)
10443 return -1;
10445 for (i = 0; i < map->n; ++i) {
10446 int bounded;
10447 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10448 if (bounded < 0 || !bounded)
10449 return bounded;
10452 return 1;
10455 /* Return 1 if the specified dim is involved in both an upper bound
10456 * and a lower bound.
10458 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10459 enum isl_dim_type type, unsigned pos)
10461 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10464 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10466 static isl_bool has_any_bound(__isl_keep isl_map *map,
10467 enum isl_dim_type type, unsigned pos,
10468 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10469 enum isl_dim_type type, unsigned pos))
10471 int i;
10473 if (!map)
10474 return isl_bool_error;
10476 for (i = 0; i < map->n; ++i) {
10477 isl_bool bounded;
10478 bounded = fn(map->p[i], type, pos);
10479 if (bounded < 0 || bounded)
10480 return bounded;
10483 return isl_bool_false;
10486 /* Return 1 if the specified dim is involved in any lower bound.
10488 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10489 enum isl_dim_type type, unsigned pos)
10491 return has_any_bound(set, type, pos,
10492 &isl_basic_map_dim_has_lower_bound);
10495 /* Return 1 if the specified dim is involved in any upper bound.
10497 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10498 enum isl_dim_type type, unsigned pos)
10500 return has_any_bound(set, type, pos,
10501 &isl_basic_map_dim_has_upper_bound);
10504 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10506 static isl_bool has_bound(__isl_keep isl_map *map,
10507 enum isl_dim_type type, unsigned pos,
10508 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10509 enum isl_dim_type type, unsigned pos))
10511 int i;
10513 if (!map)
10514 return isl_bool_error;
10516 for (i = 0; i < map->n; ++i) {
10517 isl_bool bounded;
10518 bounded = fn(map->p[i], type, pos);
10519 if (bounded < 0 || !bounded)
10520 return bounded;
10523 return isl_bool_true;
10526 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10528 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10529 enum isl_dim_type type, unsigned pos)
10531 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10534 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10536 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10537 enum isl_dim_type type, unsigned pos)
10539 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10542 /* For each of the "n" variables starting at "first", determine
10543 * the sign of the variable and put the results in the first "n"
10544 * elements of the array "signs".
10545 * Sign
10546 * 1 means that the variable is non-negative
10547 * -1 means that the variable is non-positive
10548 * 0 means the variable attains both positive and negative values.
10550 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10551 unsigned first, unsigned n, int *signs)
10553 isl_vec *bound = NULL;
10554 struct isl_tab *tab = NULL;
10555 struct isl_tab_undo *snap;
10556 int i;
10558 if (!bset || !signs)
10559 return -1;
10561 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10562 tab = isl_tab_from_basic_set(bset, 0);
10563 if (!bound || !tab)
10564 goto error;
10566 isl_seq_clr(bound->el, bound->size);
10567 isl_int_set_si(bound->el[0], -1);
10569 snap = isl_tab_snap(tab);
10570 for (i = 0; i < n; ++i) {
10571 int empty;
10573 isl_int_set_si(bound->el[1 + first + i], -1);
10574 if (isl_tab_add_ineq(tab, bound->el) < 0)
10575 goto error;
10576 empty = tab->empty;
10577 isl_int_set_si(bound->el[1 + first + i], 0);
10578 if (isl_tab_rollback(tab, snap) < 0)
10579 goto error;
10581 if (empty) {
10582 signs[i] = 1;
10583 continue;
10586 isl_int_set_si(bound->el[1 + first + i], 1);
10587 if (isl_tab_add_ineq(tab, bound->el) < 0)
10588 goto error;
10589 empty = tab->empty;
10590 isl_int_set_si(bound->el[1 + first + i], 0);
10591 if (isl_tab_rollback(tab, snap) < 0)
10592 goto error;
10594 signs[i] = empty ? -1 : 0;
10597 isl_tab_free(tab);
10598 isl_vec_free(bound);
10599 return 0;
10600 error:
10601 isl_tab_free(tab);
10602 isl_vec_free(bound);
10603 return -1;
10606 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10607 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10609 if (!bset || !signs)
10610 return -1;
10611 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10612 return -1);
10614 first += pos(bset->dim, type) - 1;
10615 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10618 /* Is it possible for the integer division "div" to depend (possibly
10619 * indirectly) on any output dimensions?
10621 * If the div is undefined, then we conservatively assume that it
10622 * may depend on them.
10623 * Otherwise, we check if it actually depends on them or on any integer
10624 * divisions that may depend on them.
10626 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10628 int i;
10629 unsigned n_out, o_out;
10630 unsigned n_div, o_div;
10632 if (isl_int_is_zero(bmap->div[div][0]))
10633 return 1;
10635 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10636 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10638 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10639 return 1;
10641 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10642 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10644 for (i = 0; i < n_div; ++i) {
10645 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10646 continue;
10647 if (div_may_involve_output(bmap, i))
10648 return 1;
10651 return 0;
10654 /* Return the first integer division of "bmap" in the range
10655 * [first, first + n[ that may depend on any output dimensions and
10656 * that has a non-zero coefficient in "c" (where the first coefficient
10657 * in "c" corresponds to integer division "first").
10659 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10660 isl_int *c, int first, int n)
10662 int k;
10664 if (!bmap)
10665 return -1;
10667 for (k = first; k < first + n; ++k) {
10668 if (isl_int_is_zero(c[k]))
10669 continue;
10670 if (div_may_involve_output(bmap, k))
10671 return k;
10674 return first + n;
10677 /* Look for a pair of inequality constraints in "bmap" of the form
10679 * -l + i >= 0 or i >= l
10680 * and
10681 * n + l - i >= 0 or i <= l + n
10683 * with n < "m" and i the output dimension at position "pos".
10684 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10685 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10686 * and earlier output dimensions, as well as integer divisions that do
10687 * not involve any of the output dimensions.
10689 * Return the index of the first inequality constraint or bmap->n_ineq
10690 * if no such pair can be found.
10692 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10693 int pos, isl_int m)
10695 int i, j;
10696 isl_ctx *ctx;
10697 unsigned total;
10698 unsigned n_div, o_div;
10699 unsigned n_out, o_out;
10700 int less;
10702 if (!bmap)
10703 return -1;
10705 ctx = isl_basic_map_get_ctx(bmap);
10706 total = isl_basic_map_total_dim(bmap);
10707 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10708 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10709 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10710 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10711 for (i = 0; i < bmap->n_ineq; ++i) {
10712 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10713 continue;
10714 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10715 n_out - (pos + 1)) != -1)
10716 continue;
10717 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10718 0, n_div) < n_div)
10719 continue;
10720 for (j = i + 1; j < bmap->n_ineq; ++j) {
10721 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10722 ctx->one))
10723 continue;
10724 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10725 bmap->ineq[j] + 1, total))
10726 continue;
10727 break;
10729 if (j >= bmap->n_ineq)
10730 continue;
10731 isl_int_add(bmap->ineq[i][0],
10732 bmap->ineq[i][0], bmap->ineq[j][0]);
10733 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10734 isl_int_sub(bmap->ineq[i][0],
10735 bmap->ineq[i][0], bmap->ineq[j][0]);
10736 if (!less)
10737 continue;
10738 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10739 return i;
10740 else
10741 return j;
10744 return bmap->n_ineq;
10747 /* Return the index of the equality of "bmap" that defines
10748 * the output dimension "pos" in terms of earlier dimensions.
10749 * The equality may also involve integer divisions, as long
10750 * as those integer divisions are defined in terms of
10751 * parameters or input dimensions.
10752 * In this case, *div is set to the number of integer divisions and
10753 * *ineq is set to the number of inequality constraints (provided
10754 * div and ineq are not NULL).
10756 * The equality may also involve a single integer division involving
10757 * the output dimensions (typically only output dimension "pos") as
10758 * long as the coefficient of output dimension "pos" is 1 or -1 and
10759 * there is a pair of constraints i >= l and i <= l + n, with i referring
10760 * to output dimension "pos", l an expression involving only earlier
10761 * dimensions and n smaller than the coefficient of the integer division
10762 * in the equality. In this case, the output dimension can be defined
10763 * in terms of a modulo expression that does not involve the integer division.
10764 * *div is then set to this single integer division and
10765 * *ineq is set to the index of constraint i >= l.
10767 * Return bmap->n_eq if there is no such equality.
10768 * Return -1 on error.
10770 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10771 int pos, int *div, int *ineq)
10773 int j, k, l;
10774 unsigned n_out, o_out;
10775 unsigned n_div, o_div;
10777 if (!bmap)
10778 return -1;
10780 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10781 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10782 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10783 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10785 if (ineq)
10786 *ineq = bmap->n_ineq;
10787 if (div)
10788 *div = n_div;
10789 for (j = 0; j < bmap->n_eq; ++j) {
10790 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10791 continue;
10792 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10793 n_out - (pos + 1)) != -1)
10794 continue;
10795 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10796 0, n_div);
10797 if (k >= n_div)
10798 return j;
10799 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10800 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10801 continue;
10802 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10803 k + 1, n_div - (k+1)) < n_div)
10804 continue;
10805 l = find_modulo_constraint_pair(bmap, pos,
10806 bmap->eq[j][o_div + k]);
10807 if (l < 0)
10808 return -1;
10809 if (l >= bmap->n_ineq)
10810 continue;
10811 if (div)
10812 *div = k;
10813 if (ineq)
10814 *ineq = l;
10815 return j;
10818 return bmap->n_eq;
10821 /* Check if the given basic map is obviously single-valued.
10822 * In particular, for each output dimension, check that there is
10823 * an equality that defines the output dimension in terms of
10824 * earlier dimensions.
10826 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10828 int i;
10829 unsigned n_out;
10831 if (!bmap)
10832 return isl_bool_error;
10834 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10836 for (i = 0; i < n_out; ++i) {
10837 int eq;
10839 eq = isl_basic_map_output_defining_equality(bmap, i,
10840 NULL, NULL);
10841 if (eq < 0)
10842 return isl_bool_error;
10843 if (eq >= bmap->n_eq)
10844 return isl_bool_false;
10847 return isl_bool_true;
10850 /* Check if the given basic map is single-valued.
10851 * We simply compute
10853 * M \circ M^-1
10855 * and check if the result is a subset of the identity mapping.
10857 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10859 isl_space *space;
10860 isl_basic_map *test;
10861 isl_basic_map *id;
10862 isl_bool sv;
10864 sv = isl_basic_map_plain_is_single_valued(bmap);
10865 if (sv < 0 || sv)
10866 return sv;
10868 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10869 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10871 space = isl_basic_map_get_space(bmap);
10872 space = isl_space_map_from_set(isl_space_range(space));
10873 id = isl_basic_map_identity(space);
10875 sv = isl_basic_map_is_subset(test, id);
10877 isl_basic_map_free(test);
10878 isl_basic_map_free(id);
10880 return sv;
10883 /* Check if the given map is obviously single-valued.
10885 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10887 if (!map)
10888 return isl_bool_error;
10889 if (map->n == 0)
10890 return isl_bool_true;
10891 if (map->n >= 2)
10892 return isl_bool_false;
10894 return isl_basic_map_plain_is_single_valued(map->p[0]);
10897 /* Check if the given map is single-valued.
10898 * We simply compute
10900 * M \circ M^-1
10902 * and check if the result is a subset of the identity mapping.
10904 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10906 isl_space *dim;
10907 isl_map *test;
10908 isl_map *id;
10909 isl_bool sv;
10911 sv = isl_map_plain_is_single_valued(map);
10912 if (sv < 0 || sv)
10913 return sv;
10915 test = isl_map_reverse(isl_map_copy(map));
10916 test = isl_map_apply_range(test, isl_map_copy(map));
10918 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10919 id = isl_map_identity(dim);
10921 sv = isl_map_is_subset(test, id);
10923 isl_map_free(test);
10924 isl_map_free(id);
10926 return sv;
10929 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10931 isl_bool in;
10933 map = isl_map_copy(map);
10934 map = isl_map_reverse(map);
10935 in = isl_map_is_single_valued(map);
10936 isl_map_free(map);
10938 return in;
10941 /* Check if the given map is obviously injective.
10943 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10945 isl_bool in;
10947 map = isl_map_copy(map);
10948 map = isl_map_reverse(map);
10949 in = isl_map_plain_is_single_valued(map);
10950 isl_map_free(map);
10952 return in;
10955 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10957 isl_bool sv;
10959 sv = isl_map_is_single_valued(map);
10960 if (sv < 0 || !sv)
10961 return sv;
10963 return isl_map_is_injective(map);
10966 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10968 return isl_map_is_single_valued(set_to_map(set));
10971 /* Does "map" only map elements to themselves?
10973 * If the domain and range spaces are different, then "map"
10974 * is considered not to be an identity relation, even if it is empty.
10975 * Otherwise, construct the maximal identity relation and
10976 * check whether "map" is a subset of this relation.
10978 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10980 isl_space *space;
10981 isl_map *id;
10982 isl_bool equal, is_identity;
10984 space = isl_map_get_space(map);
10985 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10986 isl_space_free(space);
10987 if (equal < 0 || !equal)
10988 return equal;
10990 id = isl_map_identity(isl_map_get_space(map));
10991 is_identity = isl_map_is_subset(map, id);
10992 isl_map_free(id);
10994 return is_identity;
10997 int isl_map_is_translation(__isl_keep isl_map *map)
10999 int ok;
11000 isl_set *delta;
11002 delta = isl_map_deltas(isl_map_copy(map));
11003 ok = isl_set_is_singleton(delta);
11004 isl_set_free(delta);
11006 return ok;
11009 static int unique(isl_int *p, unsigned pos, unsigned len)
11011 if (isl_seq_first_non_zero(p, pos) != -1)
11012 return 0;
11013 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11014 return 0;
11015 return 1;
11018 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11020 int i, j;
11021 unsigned nvar;
11022 unsigned ovar;
11024 if (!bset)
11025 return -1;
11027 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11028 return 0;
11030 nvar = isl_basic_set_dim(bset, isl_dim_set);
11031 ovar = isl_space_offset(bset->dim, isl_dim_set);
11032 for (j = 0; j < nvar; ++j) {
11033 int lower = 0, upper = 0;
11034 for (i = 0; i < bset->n_eq; ++i) {
11035 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11036 continue;
11037 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11038 return 0;
11039 break;
11041 if (i < bset->n_eq)
11042 continue;
11043 for (i = 0; i < bset->n_ineq; ++i) {
11044 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11045 continue;
11046 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11047 return 0;
11048 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11049 lower = 1;
11050 else
11051 upper = 1;
11053 if (!lower || !upper)
11054 return 0;
11057 return 1;
11060 int isl_set_is_box(__isl_keep isl_set *set)
11062 if (!set)
11063 return -1;
11064 if (set->n != 1)
11065 return 0;
11067 return isl_basic_set_is_box(set->p[0]);
11070 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11072 if (!bset)
11073 return isl_bool_error;
11075 return isl_space_is_wrapping(bset->dim);
11078 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11080 if (!set)
11081 return isl_bool_error;
11083 return isl_space_is_wrapping(set->dim);
11086 /* Modify the space of "map" through a call to "change".
11087 * If "can_change" is set (not NULL), then first call it to check
11088 * if the modification is allowed, printing the error message "cannot_change"
11089 * if it is not.
11091 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11092 isl_bool (*can_change)(__isl_keep isl_map *map),
11093 const char *cannot_change,
11094 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11096 isl_bool ok;
11097 isl_space *space;
11099 if (!map)
11100 return NULL;
11102 ok = can_change ? can_change(map) : isl_bool_true;
11103 if (ok < 0)
11104 return isl_map_free(map);
11105 if (!ok)
11106 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11107 return isl_map_free(map));
11109 space = change(isl_map_get_space(map));
11110 map = isl_map_reset_space(map, space);
11112 return map;
11115 /* Is the domain of "map" a wrapped relation?
11117 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11119 if (!map)
11120 return isl_bool_error;
11122 return isl_space_domain_is_wrapping(map->dim);
11125 /* Is the range of "map" a wrapped relation?
11127 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11129 if (!map)
11130 return isl_bool_error;
11132 return isl_space_range_is_wrapping(map->dim);
11135 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11137 bmap = isl_basic_map_cow(bmap);
11138 if (!bmap)
11139 return NULL;
11141 bmap->dim = isl_space_wrap(bmap->dim);
11142 if (!bmap->dim)
11143 goto error;
11145 bmap = isl_basic_map_finalize(bmap);
11147 return bset_from_bmap(bmap);
11148 error:
11149 isl_basic_map_free(bmap);
11150 return NULL;
11153 /* Given a map A -> B, return the set (A -> B).
11155 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11157 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11160 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11162 bset = isl_basic_set_cow(bset);
11163 if (!bset)
11164 return NULL;
11166 bset->dim = isl_space_unwrap(bset->dim);
11167 if (!bset->dim)
11168 goto error;
11170 bset = isl_basic_set_finalize(bset);
11172 return bset_to_bmap(bset);
11173 error:
11174 isl_basic_set_free(bset);
11175 return NULL;
11178 /* Given a set (A -> B), return the map A -> B.
11179 * Error out if "set" is not of the form (A -> B).
11181 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11183 return isl_map_change_space(set, &isl_set_is_wrapping,
11184 "not a wrapping set", &isl_space_unwrap);
11187 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11188 enum isl_dim_type type)
11190 if (!bmap)
11191 return NULL;
11193 if (!isl_space_is_named_or_nested(bmap->dim, type))
11194 return bmap;
11196 bmap = isl_basic_map_cow(bmap);
11197 if (!bmap)
11198 return NULL;
11200 bmap->dim = isl_space_reset(bmap->dim, type);
11201 if (!bmap->dim)
11202 goto error;
11204 bmap = isl_basic_map_finalize(bmap);
11206 return bmap;
11207 error:
11208 isl_basic_map_free(bmap);
11209 return NULL;
11212 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11213 enum isl_dim_type type)
11215 int i;
11217 if (!map)
11218 return NULL;
11220 if (!isl_space_is_named_or_nested(map->dim, type))
11221 return map;
11223 map = isl_map_cow(map);
11224 if (!map)
11225 return NULL;
11227 for (i = 0; i < map->n; ++i) {
11228 map->p[i] = isl_basic_map_reset(map->p[i], type);
11229 if (!map->p[i])
11230 goto error;
11232 map->dim = isl_space_reset(map->dim, type);
11233 if (!map->dim)
11234 goto error;
11236 return map;
11237 error:
11238 isl_map_free(map);
11239 return NULL;
11242 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11244 if (!bmap)
11245 return NULL;
11247 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11248 return bmap;
11250 bmap = isl_basic_map_cow(bmap);
11251 if (!bmap)
11252 return NULL;
11254 bmap->dim = isl_space_flatten(bmap->dim);
11255 if (!bmap->dim)
11256 goto error;
11258 bmap = isl_basic_map_finalize(bmap);
11260 return bmap;
11261 error:
11262 isl_basic_map_free(bmap);
11263 return NULL;
11266 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11268 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11271 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11272 __isl_take isl_basic_map *bmap)
11274 if (!bmap)
11275 return NULL;
11277 if (!bmap->dim->nested[0])
11278 return bmap;
11280 bmap = isl_basic_map_cow(bmap);
11281 if (!bmap)
11282 return NULL;
11284 bmap->dim = isl_space_flatten_domain(bmap->dim);
11285 if (!bmap->dim)
11286 goto error;
11288 bmap = isl_basic_map_finalize(bmap);
11290 return bmap;
11291 error:
11292 isl_basic_map_free(bmap);
11293 return NULL;
11296 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11297 __isl_take isl_basic_map *bmap)
11299 if (!bmap)
11300 return NULL;
11302 if (!bmap->dim->nested[1])
11303 return bmap;
11305 bmap = isl_basic_map_cow(bmap);
11306 if (!bmap)
11307 return NULL;
11309 bmap->dim = isl_space_flatten_range(bmap->dim);
11310 if (!bmap->dim)
11311 goto error;
11313 bmap = isl_basic_map_finalize(bmap);
11315 return bmap;
11316 error:
11317 isl_basic_map_free(bmap);
11318 return NULL;
11321 /* Remove any internal structure from the spaces of domain and range of "map".
11323 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11325 if (!map)
11326 return NULL;
11328 if (!map->dim->nested[0] && !map->dim->nested[1])
11329 return map;
11331 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11334 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11336 return set_from_map(isl_map_flatten(set_to_map(set)));
11339 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11341 isl_space *dim, *flat_dim;
11342 isl_map *map;
11344 dim = isl_set_get_space(set);
11345 flat_dim = isl_space_flatten(isl_space_copy(dim));
11346 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11347 map = isl_map_intersect_domain(map, set);
11349 return map;
11352 /* Remove any internal structure from the space of the domain of "map".
11354 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11356 if (!map)
11357 return NULL;
11359 if (!map->dim->nested[0])
11360 return map;
11362 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11365 /* Remove any internal structure from the space of the range of "map".
11367 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11369 if (!map)
11370 return NULL;
11372 if (!map->dim->nested[1])
11373 return map;
11375 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11378 /* Reorder the dimensions of "bmap" according to the given dim_map
11379 * and set the dimension specification to "dim" and
11380 * perform Gaussian elimination on the result.
11382 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11383 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11385 isl_basic_map *res;
11386 unsigned flags;
11388 bmap = isl_basic_map_cow(bmap);
11389 if (!bmap || !dim || !dim_map)
11390 goto error;
11392 flags = bmap->flags;
11393 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11394 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11395 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11396 res = isl_basic_map_alloc_space(dim,
11397 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11398 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11399 if (res)
11400 res->flags = flags;
11401 res = isl_basic_map_gauss(res, NULL);
11402 res = isl_basic_map_finalize(res);
11403 return res;
11404 error:
11405 free(dim_map);
11406 isl_basic_map_free(bmap);
11407 isl_space_free(dim);
11408 return NULL;
11411 /* Reorder the dimensions of "map" according to given reordering.
11413 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11414 __isl_take isl_reordering *r)
11416 int i;
11417 struct isl_dim_map *dim_map;
11419 map = isl_map_cow(map);
11420 dim_map = isl_dim_map_from_reordering(r);
11421 if (!map || !r || !dim_map)
11422 goto error;
11424 for (i = 0; i < map->n; ++i) {
11425 struct isl_dim_map *dim_map_i;
11427 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11429 map->p[i] = isl_basic_map_realign(map->p[i],
11430 isl_space_copy(r->dim), dim_map_i);
11432 if (!map->p[i])
11433 goto error;
11436 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11438 isl_reordering_free(r);
11439 free(dim_map);
11440 return map;
11441 error:
11442 free(dim_map);
11443 isl_map_free(map);
11444 isl_reordering_free(r);
11445 return NULL;
11448 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11449 __isl_take isl_reordering *r)
11451 return set_from_map(isl_map_realign(set_to_map(set), r));
11454 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11455 __isl_take isl_space *model)
11457 isl_ctx *ctx;
11459 if (!map || !model)
11460 goto error;
11462 ctx = isl_space_get_ctx(model);
11463 if (!isl_space_has_named_params(model))
11464 isl_die(ctx, isl_error_invalid,
11465 "model has unnamed parameters", goto error);
11466 if (!isl_space_has_named_params(map->dim))
11467 isl_die(ctx, isl_error_invalid,
11468 "relation has unnamed parameters", goto error);
11469 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11470 isl_reordering *exp;
11472 model = isl_space_drop_dims(model, isl_dim_in,
11473 0, isl_space_dim(model, isl_dim_in));
11474 model = isl_space_drop_dims(model, isl_dim_out,
11475 0, isl_space_dim(model, isl_dim_out));
11476 exp = isl_parameter_alignment_reordering(map->dim, model);
11477 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11478 map = isl_map_realign(map, exp);
11481 isl_space_free(model);
11482 return map;
11483 error:
11484 isl_space_free(model);
11485 isl_map_free(map);
11486 return NULL;
11489 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11490 __isl_take isl_space *model)
11492 return isl_map_align_params(set, model);
11495 /* Align the parameters of "bmap" to those of "model", introducing
11496 * additional parameters if needed.
11498 __isl_give isl_basic_map *isl_basic_map_align_params(
11499 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11501 isl_ctx *ctx;
11503 if (!bmap || !model)
11504 goto error;
11506 ctx = isl_space_get_ctx(model);
11507 if (!isl_space_has_named_params(model))
11508 isl_die(ctx, isl_error_invalid,
11509 "model has unnamed parameters", goto error);
11510 if (!isl_space_has_named_params(bmap->dim))
11511 isl_die(ctx, isl_error_invalid,
11512 "relation has unnamed parameters", goto error);
11513 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11514 isl_reordering *exp;
11515 struct isl_dim_map *dim_map;
11517 model = isl_space_drop_dims(model, isl_dim_in,
11518 0, isl_space_dim(model, isl_dim_in));
11519 model = isl_space_drop_dims(model, isl_dim_out,
11520 0, isl_space_dim(model, isl_dim_out));
11521 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11522 exp = isl_reordering_extend_space(exp,
11523 isl_basic_map_get_space(bmap));
11524 dim_map = isl_dim_map_from_reordering(exp);
11525 bmap = isl_basic_map_realign(bmap,
11526 exp ? isl_space_copy(exp->dim) : NULL,
11527 isl_dim_map_extend(dim_map, bmap));
11528 isl_reordering_free(exp);
11529 free(dim_map);
11532 isl_space_free(model);
11533 return bmap;
11534 error:
11535 isl_space_free(model);
11536 isl_basic_map_free(bmap);
11537 return NULL;
11540 /* Align the parameters of "bset" to those of "model", introducing
11541 * additional parameters if needed.
11543 __isl_give isl_basic_set *isl_basic_set_align_params(
11544 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11546 return isl_basic_map_align_params(bset, model);
11549 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11550 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11551 enum isl_dim_type c2, enum isl_dim_type c3,
11552 enum isl_dim_type c4, enum isl_dim_type c5)
11554 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11555 struct isl_mat *mat;
11556 int i, j, k;
11557 int pos;
11559 if (!bmap)
11560 return NULL;
11561 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11562 isl_basic_map_total_dim(bmap) + 1);
11563 if (!mat)
11564 return NULL;
11565 for (i = 0; i < bmap->n_eq; ++i)
11566 for (j = 0, pos = 0; j < 5; ++j) {
11567 int off = isl_basic_map_offset(bmap, c[j]);
11568 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11569 isl_int_set(mat->row[i][pos],
11570 bmap->eq[i][off + k]);
11571 ++pos;
11575 return mat;
11578 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11579 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11580 enum isl_dim_type c2, enum isl_dim_type c3,
11581 enum isl_dim_type c4, enum isl_dim_type c5)
11583 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11584 struct isl_mat *mat;
11585 int i, j, k;
11586 int pos;
11588 if (!bmap)
11589 return NULL;
11590 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11591 isl_basic_map_total_dim(bmap) + 1);
11592 if (!mat)
11593 return NULL;
11594 for (i = 0; i < bmap->n_ineq; ++i)
11595 for (j = 0, pos = 0; j < 5; ++j) {
11596 int off = isl_basic_map_offset(bmap, c[j]);
11597 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11598 isl_int_set(mat->row[i][pos],
11599 bmap->ineq[i][off + k]);
11600 ++pos;
11604 return mat;
11607 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11608 __isl_take isl_space *dim,
11609 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11610 enum isl_dim_type c2, enum isl_dim_type c3,
11611 enum isl_dim_type c4, enum isl_dim_type c5)
11613 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11614 isl_basic_map *bmap;
11615 unsigned total;
11616 unsigned extra;
11617 int i, j, k, l;
11618 int pos;
11620 if (!dim || !eq || !ineq)
11621 goto error;
11623 if (eq->n_col != ineq->n_col)
11624 isl_die(dim->ctx, isl_error_invalid,
11625 "equalities and inequalities matrices should have "
11626 "same number of columns", goto error);
11628 total = 1 + isl_space_dim(dim, isl_dim_all);
11630 if (eq->n_col < total)
11631 isl_die(dim->ctx, isl_error_invalid,
11632 "number of columns too small", goto error);
11634 extra = eq->n_col - total;
11636 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11637 eq->n_row, ineq->n_row);
11638 if (!bmap)
11639 goto error;
11640 for (i = 0; i < extra; ++i) {
11641 k = isl_basic_map_alloc_div(bmap);
11642 if (k < 0)
11643 goto error;
11644 isl_int_set_si(bmap->div[k][0], 0);
11646 for (i = 0; i < eq->n_row; ++i) {
11647 l = isl_basic_map_alloc_equality(bmap);
11648 if (l < 0)
11649 goto error;
11650 for (j = 0, pos = 0; j < 5; ++j) {
11651 int off = isl_basic_map_offset(bmap, c[j]);
11652 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11653 isl_int_set(bmap->eq[l][off + k],
11654 eq->row[i][pos]);
11655 ++pos;
11659 for (i = 0; i < ineq->n_row; ++i) {
11660 l = isl_basic_map_alloc_inequality(bmap);
11661 if (l < 0)
11662 goto error;
11663 for (j = 0, pos = 0; j < 5; ++j) {
11664 int off = isl_basic_map_offset(bmap, c[j]);
11665 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11666 isl_int_set(bmap->ineq[l][off + k],
11667 ineq->row[i][pos]);
11668 ++pos;
11673 isl_space_free(dim);
11674 isl_mat_free(eq);
11675 isl_mat_free(ineq);
11677 bmap = isl_basic_map_simplify(bmap);
11678 return isl_basic_map_finalize(bmap);
11679 error:
11680 isl_space_free(dim);
11681 isl_mat_free(eq);
11682 isl_mat_free(ineq);
11683 return NULL;
11686 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11687 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11688 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11690 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11691 c1, c2, c3, c4, isl_dim_in);
11694 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11695 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11696 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11698 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11699 c1, c2, c3, c4, isl_dim_in);
11702 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11703 __isl_take isl_space *dim,
11704 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11705 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11707 isl_basic_map *bmap;
11708 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11709 c1, c2, c3, c4, isl_dim_in);
11710 return bset_from_bmap(bmap);
11713 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11715 if (!bmap)
11716 return isl_bool_error;
11718 return isl_space_can_zip(bmap->dim);
11721 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11723 if (!map)
11724 return isl_bool_error;
11726 return isl_space_can_zip(map->dim);
11729 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11730 * (A -> C) -> (B -> D).
11732 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11734 unsigned pos;
11735 unsigned n1;
11736 unsigned n2;
11738 if (!bmap)
11739 return NULL;
11741 if (!isl_basic_map_can_zip(bmap))
11742 isl_die(bmap->ctx, isl_error_invalid,
11743 "basic map cannot be zipped", goto error);
11744 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11745 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11746 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11747 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11748 bmap = isl_basic_map_cow(bmap);
11749 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11750 if (!bmap)
11751 return NULL;
11752 bmap->dim = isl_space_zip(bmap->dim);
11753 if (!bmap->dim)
11754 goto error;
11755 bmap = isl_basic_map_mark_final(bmap);
11756 return bmap;
11757 error:
11758 isl_basic_map_free(bmap);
11759 return NULL;
11762 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11763 * (A -> C) -> (B -> D).
11765 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11767 int i;
11769 if (!map)
11770 return NULL;
11772 if (!isl_map_can_zip(map))
11773 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11774 goto error);
11776 map = isl_map_cow(map);
11777 if (!map)
11778 return NULL;
11780 for (i = 0; i < map->n; ++i) {
11781 map->p[i] = isl_basic_map_zip(map->p[i]);
11782 if (!map->p[i])
11783 goto error;
11786 map->dim = isl_space_zip(map->dim);
11787 if (!map->dim)
11788 goto error;
11790 return map;
11791 error:
11792 isl_map_free(map);
11793 return NULL;
11796 /* Can we apply isl_basic_map_curry to "bmap"?
11797 * That is, does it have a nested relation in its domain?
11799 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11801 if (!bmap)
11802 return isl_bool_error;
11804 return isl_space_can_curry(bmap->dim);
11807 /* Can we apply isl_map_curry to "map"?
11808 * That is, does it have a nested relation in its domain?
11810 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11812 if (!map)
11813 return isl_bool_error;
11815 return isl_space_can_curry(map->dim);
11818 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11819 * A -> (B -> C).
11821 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11824 if (!bmap)
11825 return NULL;
11827 if (!isl_basic_map_can_curry(bmap))
11828 isl_die(bmap->ctx, isl_error_invalid,
11829 "basic map cannot be curried", goto error);
11830 bmap = isl_basic_map_cow(bmap);
11831 if (!bmap)
11832 return NULL;
11833 bmap->dim = isl_space_curry(bmap->dim);
11834 if (!bmap->dim)
11835 goto error;
11836 bmap = isl_basic_map_mark_final(bmap);
11837 return bmap;
11838 error:
11839 isl_basic_map_free(bmap);
11840 return NULL;
11843 /* Given a map (A -> B) -> C, return the corresponding map
11844 * A -> (B -> C).
11846 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11848 return isl_map_change_space(map, &isl_map_can_curry,
11849 "map cannot be curried", &isl_space_curry);
11852 /* Can isl_map_range_curry be applied to "map"?
11853 * That is, does it have a nested relation in its range,
11854 * the domain of which is itself a nested relation?
11856 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11858 if (!map)
11859 return isl_bool_error;
11861 return isl_space_can_range_curry(map->dim);
11864 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11865 * A -> (B -> (C -> D)).
11867 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11869 return isl_map_change_space(map, &isl_map_can_range_curry,
11870 "map range cannot be curried",
11871 &isl_space_range_curry);
11874 /* Can we apply isl_basic_map_uncurry to "bmap"?
11875 * That is, does it have a nested relation in its domain?
11877 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11879 if (!bmap)
11880 return isl_bool_error;
11882 return isl_space_can_uncurry(bmap->dim);
11885 /* Can we apply isl_map_uncurry to "map"?
11886 * That is, does it have a nested relation in its domain?
11888 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11890 if (!map)
11891 return isl_bool_error;
11893 return isl_space_can_uncurry(map->dim);
11896 /* Given a basic map A -> (B -> C), return the corresponding basic map
11897 * (A -> B) -> C.
11899 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11902 if (!bmap)
11903 return NULL;
11905 if (!isl_basic_map_can_uncurry(bmap))
11906 isl_die(bmap->ctx, isl_error_invalid,
11907 "basic map cannot be uncurried",
11908 return isl_basic_map_free(bmap));
11909 bmap = isl_basic_map_cow(bmap);
11910 if (!bmap)
11911 return NULL;
11912 bmap->dim = isl_space_uncurry(bmap->dim);
11913 if (!bmap->dim)
11914 return isl_basic_map_free(bmap);
11915 bmap = isl_basic_map_mark_final(bmap);
11916 return bmap;
11919 /* Given a map A -> (B -> C), return the corresponding map
11920 * (A -> B) -> C.
11922 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11924 return isl_map_change_space(map, &isl_map_can_uncurry,
11925 "map cannot be uncurried", &isl_space_uncurry);
11928 /* Construct a basic map mapping the domain of the affine expression
11929 * to a one-dimensional range prescribed by the affine expression.
11930 * If "rational" is set, then construct a rational basic map.
11932 * A NaN affine expression cannot be converted to a basic map.
11934 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
11935 __isl_take isl_aff *aff, int rational)
11937 int k;
11938 int pos;
11939 isl_bool is_nan;
11940 isl_local_space *ls;
11941 isl_basic_map *bmap = NULL;
11943 if (!aff)
11944 return NULL;
11945 is_nan = isl_aff_is_nan(aff);
11946 if (is_nan < 0)
11947 goto error;
11948 if (is_nan)
11949 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11950 "cannot convert NaN", goto error);
11952 ls = isl_aff_get_local_space(aff);
11953 bmap = isl_basic_map_from_local_space(ls);
11954 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11955 k = isl_basic_map_alloc_equality(bmap);
11956 if (k < 0)
11957 goto error;
11959 pos = isl_basic_map_offset(bmap, isl_dim_out);
11960 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11961 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11962 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11963 aff->v->size - (pos + 1));
11965 isl_aff_free(aff);
11966 if (rational)
11967 bmap = isl_basic_map_set_rational(bmap);
11968 bmap = isl_basic_map_finalize(bmap);
11969 return bmap;
11970 error:
11971 isl_aff_free(aff);
11972 isl_basic_map_free(bmap);
11973 return NULL;
11976 /* Construct a basic map mapping the domain of the affine expression
11977 * to a one-dimensional range prescribed by the affine expression.
11979 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11981 return isl_basic_map_from_aff2(aff, 0);
11984 /* Construct a map mapping the domain of the affine expression
11985 * to a one-dimensional range prescribed by the affine expression.
11987 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11989 isl_basic_map *bmap;
11991 bmap = isl_basic_map_from_aff(aff);
11992 return isl_map_from_basic_map(bmap);
11995 /* Construct a basic map mapping the domain the multi-affine expression
11996 * to its range, with each dimension in the range equated to the
11997 * corresponding affine expression.
11998 * If "rational" is set, then construct a rational basic map.
12000 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12001 __isl_take isl_multi_aff *maff, int rational)
12003 int i;
12004 isl_space *space;
12005 isl_basic_map *bmap;
12007 if (!maff)
12008 return NULL;
12010 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12011 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12012 "invalid space", goto error);
12014 space = isl_space_domain(isl_multi_aff_get_space(maff));
12015 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12016 if (rational)
12017 bmap = isl_basic_map_set_rational(bmap);
12019 for (i = 0; i < maff->n; ++i) {
12020 isl_aff *aff;
12021 isl_basic_map *bmap_i;
12023 aff = isl_aff_copy(maff->p[i]);
12024 bmap_i = isl_basic_map_from_aff2(aff, rational);
12026 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12029 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12031 isl_multi_aff_free(maff);
12032 return bmap;
12033 error:
12034 isl_multi_aff_free(maff);
12035 return NULL;
12038 /* Construct a basic map mapping the domain the multi-affine expression
12039 * to its range, with each dimension in the range equated to the
12040 * corresponding affine expression.
12042 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12043 __isl_take isl_multi_aff *ma)
12045 return isl_basic_map_from_multi_aff2(ma, 0);
12048 /* Construct a map mapping the domain the multi-affine expression
12049 * to its range, with each dimension in the range equated to the
12050 * corresponding affine expression.
12052 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12054 isl_basic_map *bmap;
12056 bmap = isl_basic_map_from_multi_aff(maff);
12057 return isl_map_from_basic_map(bmap);
12060 /* Construct a basic map mapping a domain in the given space to
12061 * to an n-dimensional range, with n the number of elements in the list,
12062 * where each coordinate in the range is prescribed by the
12063 * corresponding affine expression.
12064 * The domains of all affine expressions in the list are assumed to match
12065 * domain_dim.
12067 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12068 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12070 int i;
12071 isl_space *dim;
12072 isl_basic_map *bmap;
12074 if (!list)
12075 return NULL;
12077 dim = isl_space_from_domain(domain_dim);
12078 bmap = isl_basic_map_universe(dim);
12080 for (i = 0; i < list->n; ++i) {
12081 isl_aff *aff;
12082 isl_basic_map *bmap_i;
12084 aff = isl_aff_copy(list->p[i]);
12085 bmap_i = isl_basic_map_from_aff(aff);
12087 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12090 isl_aff_list_free(list);
12091 return bmap;
12094 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12095 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12097 return isl_map_equate(set, type1, pos1, type2, pos2);
12100 /* Construct a basic map where the given dimensions are equal to each other.
12102 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12103 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12105 isl_basic_map *bmap = NULL;
12106 int i;
12108 if (!space)
12109 return NULL;
12111 if (pos1 >= isl_space_dim(space, type1))
12112 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12113 "index out of bounds", goto error);
12114 if (pos2 >= isl_space_dim(space, type2))
12115 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12116 "index out of bounds", goto error);
12118 if (type1 == type2 && pos1 == pos2)
12119 return isl_basic_map_universe(space);
12121 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12122 i = isl_basic_map_alloc_equality(bmap);
12123 if (i < 0)
12124 goto error;
12125 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12126 pos1 += isl_basic_map_offset(bmap, type1);
12127 pos2 += isl_basic_map_offset(bmap, type2);
12128 isl_int_set_si(bmap->eq[i][pos1], -1);
12129 isl_int_set_si(bmap->eq[i][pos2], 1);
12130 bmap = isl_basic_map_finalize(bmap);
12131 isl_space_free(space);
12132 return bmap;
12133 error:
12134 isl_space_free(space);
12135 isl_basic_map_free(bmap);
12136 return NULL;
12139 /* Add a constraint imposing that the given two dimensions are equal.
12141 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12142 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12144 isl_basic_map *eq;
12146 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12148 bmap = isl_basic_map_intersect(bmap, eq);
12150 return bmap;
12153 /* Add a constraint imposing that the given two dimensions are equal.
12155 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12156 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12158 isl_basic_map *bmap;
12160 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12162 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12164 return map;
12167 /* Add a constraint imposing that the given two dimensions have opposite values.
12169 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12170 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12172 isl_basic_map *bmap = NULL;
12173 int i;
12175 if (!map)
12176 return NULL;
12178 if (pos1 >= isl_map_dim(map, type1))
12179 isl_die(map->ctx, isl_error_invalid,
12180 "index out of bounds", goto error);
12181 if (pos2 >= isl_map_dim(map, type2))
12182 isl_die(map->ctx, isl_error_invalid,
12183 "index out of bounds", goto error);
12185 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12186 i = isl_basic_map_alloc_equality(bmap);
12187 if (i < 0)
12188 goto error;
12189 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12190 pos1 += isl_basic_map_offset(bmap, type1);
12191 pos2 += isl_basic_map_offset(bmap, type2);
12192 isl_int_set_si(bmap->eq[i][pos1], 1);
12193 isl_int_set_si(bmap->eq[i][pos2], 1);
12194 bmap = isl_basic_map_finalize(bmap);
12196 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12198 return map;
12199 error:
12200 isl_basic_map_free(bmap);
12201 isl_map_free(map);
12202 return NULL;
12205 /* Construct a constraint imposing that the value of the first dimension is
12206 * greater than or equal to that of the second.
12208 static __isl_give isl_constraint *constraint_order_ge(
12209 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12210 enum isl_dim_type type2, int pos2)
12212 isl_constraint *c;
12214 if (!space)
12215 return NULL;
12217 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12219 if (pos1 >= isl_constraint_dim(c, type1))
12220 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12221 "index out of bounds", return isl_constraint_free(c));
12222 if (pos2 >= isl_constraint_dim(c, type2))
12223 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12224 "index out of bounds", return isl_constraint_free(c));
12226 if (type1 == type2 && pos1 == pos2)
12227 return c;
12229 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12230 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12232 return c;
12235 /* Add a constraint imposing that the value of the first dimension is
12236 * greater than or equal to that of the second.
12238 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12239 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12241 isl_constraint *c;
12242 isl_space *space;
12244 if (type1 == type2 && pos1 == pos2)
12245 return bmap;
12246 space = isl_basic_map_get_space(bmap);
12247 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12248 bmap = isl_basic_map_add_constraint(bmap, c);
12250 return bmap;
12253 /* Add a constraint imposing that the value of the first dimension is
12254 * greater than or equal to that of the second.
12256 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12257 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12259 isl_constraint *c;
12260 isl_space *space;
12262 if (type1 == type2 && pos1 == pos2)
12263 return map;
12264 space = isl_map_get_space(map);
12265 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12266 map = isl_map_add_constraint(map, c);
12268 return map;
12271 /* Add a constraint imposing that the value of the first dimension is
12272 * less than or equal to that of the second.
12274 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12275 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12277 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12280 /* Construct a basic map where the value of the first dimension is
12281 * greater than that of the second.
12283 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12284 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12286 isl_basic_map *bmap = NULL;
12287 int i;
12289 if (!space)
12290 return NULL;
12292 if (pos1 >= isl_space_dim(space, type1))
12293 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12294 "index out of bounds", goto error);
12295 if (pos2 >= isl_space_dim(space, type2))
12296 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12297 "index out of bounds", goto error);
12299 if (type1 == type2 && pos1 == pos2)
12300 return isl_basic_map_empty(space);
12302 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12303 i = isl_basic_map_alloc_inequality(bmap);
12304 if (i < 0)
12305 return isl_basic_map_free(bmap);
12306 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12307 pos1 += isl_basic_map_offset(bmap, type1);
12308 pos2 += isl_basic_map_offset(bmap, type2);
12309 isl_int_set_si(bmap->ineq[i][pos1], 1);
12310 isl_int_set_si(bmap->ineq[i][pos2], -1);
12311 isl_int_set_si(bmap->ineq[i][0], -1);
12312 bmap = isl_basic_map_finalize(bmap);
12314 return bmap;
12315 error:
12316 isl_space_free(space);
12317 isl_basic_map_free(bmap);
12318 return NULL;
12321 /* Add a constraint imposing that the value of the first dimension is
12322 * greater than that of the second.
12324 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12325 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12327 isl_basic_map *gt;
12329 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12331 bmap = isl_basic_map_intersect(bmap, gt);
12333 return bmap;
12336 /* Add a constraint imposing that the value of the first dimension is
12337 * greater than that of the second.
12339 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12340 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12342 isl_basic_map *bmap;
12344 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12346 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12348 return map;
12351 /* Add a constraint imposing that the value of the first dimension is
12352 * smaller than that of the second.
12354 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12355 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12357 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12360 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12361 int pos)
12363 isl_aff *div;
12364 isl_local_space *ls;
12366 if (!bmap)
12367 return NULL;
12369 if (!isl_basic_map_divs_known(bmap))
12370 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12371 "some divs are unknown", return NULL);
12373 ls = isl_basic_map_get_local_space(bmap);
12374 div = isl_local_space_get_div(ls, pos);
12375 isl_local_space_free(ls);
12377 return div;
12380 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12381 int pos)
12383 return isl_basic_map_get_div(bset, pos);
12386 /* Plug in "subs" for dimension "type", "pos" of "bset".
12388 * Let i be the dimension to replace and let "subs" be of the form
12390 * f/d
12392 * Any integer division with a non-zero coefficient for i,
12394 * floor((a i + g)/m)
12396 * is replaced by
12398 * floor((a f + d g)/(m d))
12400 * Constraints of the form
12402 * a i + g
12404 * are replaced by
12406 * a f + d g
12408 * We currently require that "subs" is an integral expression.
12409 * Handling rational expressions may require us to add stride constraints
12410 * as we do in isl_basic_set_preimage_multi_aff.
12412 __isl_give isl_basic_set *isl_basic_set_substitute(
12413 __isl_take isl_basic_set *bset,
12414 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12416 int i;
12417 isl_int v;
12418 isl_ctx *ctx;
12420 if (bset && isl_basic_set_plain_is_empty(bset))
12421 return bset;
12423 bset = isl_basic_set_cow(bset);
12424 if (!bset || !subs)
12425 goto error;
12427 ctx = isl_basic_set_get_ctx(bset);
12428 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12429 isl_die(ctx, isl_error_invalid,
12430 "spaces don't match", goto error);
12431 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12432 isl_die(ctx, isl_error_unsupported,
12433 "cannot handle divs yet", goto error);
12434 if (!isl_int_is_one(subs->v->el[0]))
12435 isl_die(ctx, isl_error_invalid,
12436 "can only substitute integer expressions", goto error);
12438 pos += isl_basic_set_offset(bset, type);
12440 isl_int_init(v);
12442 for (i = 0; i < bset->n_eq; ++i) {
12443 if (isl_int_is_zero(bset->eq[i][pos]))
12444 continue;
12445 isl_int_set(v, bset->eq[i][pos]);
12446 isl_int_set_si(bset->eq[i][pos], 0);
12447 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12448 v, subs->v->el + 1, subs->v->size - 1);
12451 for (i = 0; i < bset->n_ineq; ++i) {
12452 if (isl_int_is_zero(bset->ineq[i][pos]))
12453 continue;
12454 isl_int_set(v, bset->ineq[i][pos]);
12455 isl_int_set_si(bset->ineq[i][pos], 0);
12456 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12457 v, subs->v->el + 1, subs->v->size - 1);
12460 for (i = 0; i < bset->n_div; ++i) {
12461 if (isl_int_is_zero(bset->div[i][1 + pos]))
12462 continue;
12463 isl_int_set(v, bset->div[i][1 + pos]);
12464 isl_int_set_si(bset->div[i][1 + pos], 0);
12465 isl_seq_combine(bset->div[i] + 1,
12466 subs->v->el[0], bset->div[i] + 1,
12467 v, subs->v->el + 1, subs->v->size - 1);
12468 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12471 isl_int_clear(v);
12473 bset = isl_basic_set_simplify(bset);
12474 return isl_basic_set_finalize(bset);
12475 error:
12476 isl_basic_set_free(bset);
12477 return NULL;
12480 /* Plug in "subs" for dimension "type", "pos" of "set".
12482 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12483 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12485 int i;
12487 if (set && isl_set_plain_is_empty(set))
12488 return set;
12490 set = isl_set_cow(set);
12491 if (!set || !subs)
12492 goto error;
12494 for (i = set->n - 1; i >= 0; --i) {
12495 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12496 if (remove_if_empty(set, i) < 0)
12497 goto error;
12500 return set;
12501 error:
12502 isl_set_free(set);
12503 return NULL;
12506 /* Check if the range of "ma" is compatible with the domain or range
12507 * (depending on "type") of "bmap".
12508 * Return -1 if anything is wrong.
12510 static int check_basic_map_compatible_range_multi_aff(
12511 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12512 __isl_keep isl_multi_aff *ma)
12514 int m;
12515 isl_space *ma_space;
12517 ma_space = isl_multi_aff_get_space(ma);
12519 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12520 if (m < 0)
12521 goto error;
12522 if (!m)
12523 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12524 "parameters don't match", goto error);
12525 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12526 if (m < 0)
12527 goto error;
12528 if (!m)
12529 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12530 "spaces don't match", goto error);
12532 isl_space_free(ma_space);
12533 return m;
12534 error:
12535 isl_space_free(ma_space);
12536 return -1;
12539 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12540 * coefficients before the transformed range of dimensions,
12541 * the "n_after" coefficients after the transformed range of dimensions
12542 * and the coefficients of the other divs in "bmap".
12544 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12545 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12547 int i;
12548 int n_param;
12549 int n_set;
12550 isl_local_space *ls;
12552 if (n_div == 0)
12553 return 0;
12555 ls = isl_aff_get_domain_local_space(ma->p[0]);
12556 if (!ls)
12557 return -1;
12559 n_param = isl_local_space_dim(ls, isl_dim_param);
12560 n_set = isl_local_space_dim(ls, isl_dim_set);
12561 for (i = 0; i < n_div; ++i) {
12562 int o_bmap = 0, o_ls = 0;
12564 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12565 o_bmap += 1 + 1 + n_param;
12566 o_ls += 1 + 1 + n_param;
12567 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12568 o_bmap += n_before;
12569 isl_seq_cpy(bmap->div[i] + o_bmap,
12570 ls->div->row[i] + o_ls, n_set);
12571 o_bmap += n_set;
12572 o_ls += n_set;
12573 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12574 o_bmap += n_after;
12575 isl_seq_cpy(bmap->div[i] + o_bmap,
12576 ls->div->row[i] + o_ls, n_div);
12577 o_bmap += n_div;
12578 o_ls += n_div;
12579 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12580 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12581 goto error;
12584 isl_local_space_free(ls);
12585 return 0;
12586 error:
12587 isl_local_space_free(ls);
12588 return -1;
12591 /* How many stride constraints does "ma" enforce?
12592 * That is, how many of the affine expressions have a denominator
12593 * different from one?
12595 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12597 int i;
12598 int strides = 0;
12600 for (i = 0; i < ma->n; ++i)
12601 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12602 strides++;
12604 return strides;
12607 /* For each affine expression in ma of the form
12609 * x_i = (f_i y + h_i)/m_i
12611 * with m_i different from one, add a constraint to "bmap"
12612 * of the form
12614 * f_i y + h_i = m_i alpha_i
12616 * with alpha_i an additional existentially quantified variable.
12618 * The input variables of "ma" correspond to a subset of the variables
12619 * of "bmap". There are "n_before" variables in "bmap" before this
12620 * subset and "n_after" variables after this subset.
12621 * The integer divisions of the affine expressions in "ma" are assumed
12622 * to have been aligned. There are "n_div_ma" of them and
12623 * they appear first in "bmap", straight after the "n_after" variables.
12625 static __isl_give isl_basic_map *add_ma_strides(
12626 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12627 int n_before, int n_after, int n_div_ma)
12629 int i, k;
12630 int div;
12631 int total;
12632 int n_param;
12633 int n_in;
12635 total = isl_basic_map_total_dim(bmap);
12636 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12637 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12638 for (i = 0; i < ma->n; ++i) {
12639 int o_bmap = 0, o_ma = 1;
12641 if (isl_int_is_one(ma->p[i]->v->el[0]))
12642 continue;
12643 div = isl_basic_map_alloc_div(bmap);
12644 k = isl_basic_map_alloc_equality(bmap);
12645 if (div < 0 || k < 0)
12646 goto error;
12647 isl_int_set_si(bmap->div[div][0], 0);
12648 isl_seq_cpy(bmap->eq[k] + o_bmap,
12649 ma->p[i]->v->el + o_ma, 1 + n_param);
12650 o_bmap += 1 + n_param;
12651 o_ma += 1 + n_param;
12652 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12653 o_bmap += n_before;
12654 isl_seq_cpy(bmap->eq[k] + o_bmap,
12655 ma->p[i]->v->el + o_ma, n_in);
12656 o_bmap += n_in;
12657 o_ma += n_in;
12658 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12659 o_bmap += n_after;
12660 isl_seq_cpy(bmap->eq[k] + o_bmap,
12661 ma->p[i]->v->el + o_ma, n_div_ma);
12662 o_bmap += n_div_ma;
12663 o_ma += n_div_ma;
12664 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12665 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12666 total++;
12669 return bmap;
12670 error:
12671 isl_basic_map_free(bmap);
12672 return NULL;
12675 /* Replace the domain or range space (depending on "type) of "space" by "set".
12677 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12678 enum isl_dim_type type, __isl_take isl_space *set)
12680 if (type == isl_dim_in) {
12681 space = isl_space_range(space);
12682 space = isl_space_map_from_domain_and_range(set, space);
12683 } else {
12684 space = isl_space_domain(space);
12685 space = isl_space_map_from_domain_and_range(space, set);
12688 return space;
12691 /* Compute the preimage of the domain or range (depending on "type")
12692 * of "bmap" under the function represented by "ma".
12693 * In other words, plug in "ma" in the domain or range of "bmap".
12694 * The result is a basic map that lives in the same space as "bmap"
12695 * except that the domain or range has been replaced by
12696 * the domain space of "ma".
12698 * If bmap is represented by
12700 * A(p) + S u + B x + T v + C(divs) >= 0,
12702 * where u and x are input and output dimensions if type == isl_dim_out
12703 * while x and v are input and output dimensions if type == isl_dim_in,
12704 * and ma is represented by
12706 * x = D(p) + F(y) + G(divs')
12708 * then the result is
12710 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12712 * The divs in the input set are similarly adjusted.
12713 * In particular
12715 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12717 * becomes
12719 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12720 * B_i G(divs') + c_i(divs))/n_i)
12722 * If bmap is not a rational map and if F(y) involves any denominators
12724 * x_i = (f_i y + h_i)/m_i
12726 * then additional constraints are added to ensure that we only
12727 * map back integer points. That is we enforce
12729 * f_i y + h_i = m_i alpha_i
12731 * with alpha_i an additional existentially quantified variable.
12733 * We first copy over the divs from "ma".
12734 * Then we add the modified constraints and divs from "bmap".
12735 * Finally, we add the stride constraints, if needed.
12737 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12738 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12739 __isl_take isl_multi_aff *ma)
12741 int i, k;
12742 isl_space *space;
12743 isl_basic_map *res = NULL;
12744 int n_before, n_after, n_div_bmap, n_div_ma;
12745 isl_int f, c1, c2, g;
12746 int rational, strides;
12748 isl_int_init(f);
12749 isl_int_init(c1);
12750 isl_int_init(c2);
12751 isl_int_init(g);
12753 ma = isl_multi_aff_align_divs(ma);
12754 if (!bmap || !ma)
12755 goto error;
12756 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12757 goto error;
12759 if (type == isl_dim_in) {
12760 n_before = 0;
12761 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12762 } else {
12763 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12764 n_after = 0;
12766 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12767 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12769 space = isl_multi_aff_get_domain_space(ma);
12770 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12771 rational = isl_basic_map_is_rational(bmap);
12772 strides = rational ? 0 : multi_aff_strides(ma);
12773 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12774 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12775 if (rational)
12776 res = isl_basic_map_set_rational(res);
12778 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12779 if (isl_basic_map_alloc_div(res) < 0)
12780 goto error;
12782 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12783 goto error;
12785 for (i = 0; i < bmap->n_eq; ++i) {
12786 k = isl_basic_map_alloc_equality(res);
12787 if (k < 0)
12788 goto error;
12789 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12790 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12793 for (i = 0; i < bmap->n_ineq; ++i) {
12794 k = isl_basic_map_alloc_inequality(res);
12795 if (k < 0)
12796 goto error;
12797 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12798 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12801 for (i = 0; i < bmap->n_div; ++i) {
12802 if (isl_int_is_zero(bmap->div[i][0])) {
12803 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12804 continue;
12806 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12807 n_before, n_after, n_div_ma, n_div_bmap,
12808 f, c1, c2, g, 1);
12811 if (strides)
12812 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12814 isl_int_clear(f);
12815 isl_int_clear(c1);
12816 isl_int_clear(c2);
12817 isl_int_clear(g);
12818 isl_basic_map_free(bmap);
12819 isl_multi_aff_free(ma);
12820 res = isl_basic_set_simplify(res);
12821 return isl_basic_map_finalize(res);
12822 error:
12823 isl_int_clear(f);
12824 isl_int_clear(c1);
12825 isl_int_clear(c2);
12826 isl_int_clear(g);
12827 isl_basic_map_free(bmap);
12828 isl_multi_aff_free(ma);
12829 isl_basic_map_free(res);
12830 return NULL;
12833 /* Compute the preimage of "bset" under the function represented by "ma".
12834 * In other words, plug in "ma" in "bset". The result is a basic set
12835 * that lives in the domain space of "ma".
12837 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12838 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12840 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12843 /* Compute the preimage of the domain of "bmap" under the function
12844 * represented by "ma".
12845 * In other words, plug in "ma" in the domain of "bmap".
12846 * The result is a basic map that lives in the same space as "bmap"
12847 * except that the domain has been replaced by the domain space of "ma".
12849 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12850 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12852 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12855 /* Compute the preimage of the range of "bmap" under the function
12856 * represented by "ma".
12857 * In other words, plug in "ma" in the range of "bmap".
12858 * The result is a basic map that lives in the same space as "bmap"
12859 * except that the range has been replaced by the domain space of "ma".
12861 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12862 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12864 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12867 /* Check if the range of "ma" is compatible with the domain or range
12868 * (depending on "type") of "map".
12869 * Return -1 if anything is wrong.
12871 static int check_map_compatible_range_multi_aff(
12872 __isl_keep isl_map *map, enum isl_dim_type type,
12873 __isl_keep isl_multi_aff *ma)
12875 int m;
12876 isl_space *ma_space;
12878 ma_space = isl_multi_aff_get_space(ma);
12879 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12880 isl_space_free(ma_space);
12881 if (m >= 0 && !m)
12882 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12883 "spaces don't match", return -1);
12884 return m;
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 * The parameters are assumed to have been aligned.
12896 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12897 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12899 int i;
12900 isl_space *space;
12902 map = isl_map_cow(map);
12903 ma = isl_multi_aff_align_divs(ma);
12904 if (!map || !ma)
12905 goto error;
12906 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12907 goto error;
12909 for (i = 0; i < map->n; ++i) {
12910 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12911 isl_multi_aff_copy(ma));
12912 if (!map->p[i])
12913 goto error;
12916 space = isl_multi_aff_get_domain_space(ma);
12917 space = isl_space_set(isl_map_get_space(map), type, space);
12919 isl_space_free(map->dim);
12920 map->dim = space;
12921 if (!map->dim)
12922 goto error;
12924 isl_multi_aff_free(ma);
12925 if (map->n > 1)
12926 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12927 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12928 return map;
12929 error:
12930 isl_multi_aff_free(ma);
12931 isl_map_free(map);
12932 return NULL;
12935 /* Compute the preimage of the domain or range (depending on "type")
12936 * of "map" under the function represented by "ma".
12937 * In other words, plug in "ma" in the domain or range of "map".
12938 * The result is a map that lives in the same space as "map"
12939 * except that the domain or range has been replaced by
12940 * the domain space of "ma".
12942 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12943 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12945 if (!map || !ma)
12946 goto error;
12948 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12949 return map_preimage_multi_aff(map, type, ma);
12951 if (!isl_space_has_named_params(map->dim) ||
12952 !isl_space_has_named_params(ma->space))
12953 isl_die(map->ctx, isl_error_invalid,
12954 "unaligned unnamed parameters", goto error);
12955 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12956 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12958 return map_preimage_multi_aff(map, type, ma);
12959 error:
12960 isl_multi_aff_free(ma);
12961 return isl_map_free(map);
12964 /* Compute the preimage of "set" under the function represented by "ma".
12965 * In other words, plug in "ma" in "set". The result is a set
12966 * that lives in the domain space of "ma".
12968 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12969 __isl_take isl_multi_aff *ma)
12971 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12974 /* Compute the preimage of the domain of "map" under the function
12975 * represented by "ma".
12976 * In other words, plug in "ma" in the domain of "map".
12977 * The result is a map that lives in the same space as "map"
12978 * except that the domain has been replaced by the domain space of "ma".
12980 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12981 __isl_take isl_multi_aff *ma)
12983 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12986 /* Compute the preimage of the range of "map" under the function
12987 * represented by "ma".
12988 * In other words, plug in "ma" in the range of "map".
12989 * The result is a map that lives in the same space as "map"
12990 * except that the range has been replaced by the domain space of "ma".
12992 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12993 __isl_take isl_multi_aff *ma)
12995 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12998 /* Compute the preimage of "map" under the function represented by "pma".
12999 * In other words, plug in "pma" in the domain or range of "map".
13000 * The result is a map that lives in the same space as "map",
13001 * except that the space of type "type" has been replaced by
13002 * the domain space of "pma".
13004 * The parameters of "map" and "pma" are assumed to have been aligned.
13006 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13007 __isl_take isl_map *map, enum isl_dim_type type,
13008 __isl_take isl_pw_multi_aff *pma)
13010 int i;
13011 isl_map *res;
13013 if (!pma)
13014 goto error;
13016 if (pma->n == 0) {
13017 isl_pw_multi_aff_free(pma);
13018 res = isl_map_empty(isl_map_get_space(map));
13019 isl_map_free(map);
13020 return res;
13023 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13024 isl_multi_aff_copy(pma->p[0].maff));
13025 if (type == isl_dim_in)
13026 res = isl_map_intersect_domain(res,
13027 isl_map_copy(pma->p[0].set));
13028 else
13029 res = isl_map_intersect_range(res,
13030 isl_map_copy(pma->p[0].set));
13032 for (i = 1; i < pma->n; ++i) {
13033 isl_map *res_i;
13035 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13036 isl_multi_aff_copy(pma->p[i].maff));
13037 if (type == isl_dim_in)
13038 res_i = isl_map_intersect_domain(res_i,
13039 isl_map_copy(pma->p[i].set));
13040 else
13041 res_i = isl_map_intersect_range(res_i,
13042 isl_map_copy(pma->p[i].set));
13043 res = isl_map_union(res, res_i);
13046 isl_pw_multi_aff_free(pma);
13047 isl_map_free(map);
13048 return res;
13049 error:
13050 isl_pw_multi_aff_free(pma);
13051 isl_map_free(map);
13052 return NULL;
13055 /* Compute the preimage of "map" under the function represented by "pma".
13056 * In other words, plug in "pma" in the domain or range of "map".
13057 * The result is a map that lives in the same space as "map",
13058 * except that the space of type "type" has been replaced by
13059 * the domain space of "pma".
13061 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13062 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13064 if (!map || !pma)
13065 goto error;
13067 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13068 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13070 if (!isl_space_has_named_params(map->dim) ||
13071 !isl_space_has_named_params(pma->dim))
13072 isl_die(map->ctx, isl_error_invalid,
13073 "unaligned unnamed parameters", goto error);
13074 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13075 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13077 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13078 error:
13079 isl_pw_multi_aff_free(pma);
13080 return isl_map_free(map);
13083 /* Compute the preimage of "set" under the function represented by "pma".
13084 * In other words, plug in "pma" in "set". The result is a set
13085 * that lives in the domain space of "pma".
13087 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13088 __isl_take isl_pw_multi_aff *pma)
13090 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13093 /* Compute the preimage of the domain of "map" under the function
13094 * represented by "pma".
13095 * In other words, plug in "pma" in the domain of "map".
13096 * The result is a map that lives in the same space as "map",
13097 * except that domain space has been replaced by the domain space of "pma".
13099 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13100 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13102 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13105 /* Compute the preimage of the range of "map" under the function
13106 * represented by "pma".
13107 * In other words, plug in "pma" in the range of "map".
13108 * The result is a map that lives in the same space as "map",
13109 * except that range space has been replaced by the domain space of "pma".
13111 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13112 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13114 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13117 /* Compute the preimage of "map" under the function represented by "mpa".
13118 * In other words, plug in "mpa" in the domain or range of "map".
13119 * The result is a map that lives in the same space as "map",
13120 * except that the space of type "type" has been replaced by
13121 * the domain space of "mpa".
13123 * If the map does not involve any constraints that refer to the
13124 * dimensions of the substituted space, then the only possible
13125 * effect of "mpa" on the map is to map the space to a different space.
13126 * We create a separate isl_multi_aff to effectuate this change
13127 * in order to avoid spurious splitting of the map along the pieces
13128 * of "mpa".
13130 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13131 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13133 int n;
13134 isl_pw_multi_aff *pma;
13136 if (!map || !mpa)
13137 goto error;
13139 n = isl_map_dim(map, type);
13140 if (!isl_map_involves_dims(map, type, 0, n)) {
13141 isl_space *space;
13142 isl_multi_aff *ma;
13144 space = isl_multi_pw_aff_get_space(mpa);
13145 isl_multi_pw_aff_free(mpa);
13146 ma = isl_multi_aff_zero(space);
13147 return isl_map_preimage_multi_aff(map, type, ma);
13150 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13151 return isl_map_preimage_pw_multi_aff(map, type, pma);
13152 error:
13153 isl_map_free(map);
13154 isl_multi_pw_aff_free(mpa);
13155 return NULL;
13158 /* Compute the preimage of "map" under the function represented by "mpa".
13159 * In other words, plug in "mpa" in the domain "map".
13160 * The result is a map that lives in the same space as "map",
13161 * except that domain space has been replaced by the domain space of "mpa".
13163 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13164 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13166 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13169 /* Compute the preimage of "set" by the function represented by "mpa".
13170 * In other words, plug in "mpa" in "set".
13172 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13173 __isl_take isl_multi_pw_aff *mpa)
13175 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13178 /* Is the point "inner" internal to inequality constraint "ineq"
13179 * of "bset"?
13180 * The point is considered to be internal to the inequality constraint,
13181 * if it strictly lies on the positive side of the inequality constraint,
13182 * or if it lies on the constraint and the constraint is lexico-positive.
13184 static isl_bool is_internal(__isl_keep isl_vec *inner,
13185 __isl_keep isl_basic_set *bset, int ineq)
13187 isl_ctx *ctx;
13188 int pos;
13189 unsigned total;
13191 if (!inner || !bset)
13192 return isl_bool_error;
13194 ctx = isl_basic_set_get_ctx(bset);
13195 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13196 &ctx->normalize_gcd);
13197 if (!isl_int_is_zero(ctx->normalize_gcd))
13198 return isl_int_is_nonneg(ctx->normalize_gcd);
13200 total = isl_basic_set_dim(bset, isl_dim_all);
13201 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13202 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13205 /* Tighten the inequality constraints of "bset" that are outward with respect
13206 * to the point "vec".
13207 * That is, tighten the constraints that are not satisfied by "vec".
13209 * "vec" is a point internal to some superset S of "bset" that is used
13210 * to make the subsets of S disjoint, by tightening one half of the constraints
13211 * that separate two subsets. In particular, the constraints of S
13212 * are all satisfied by "vec" and should not be tightened.
13213 * Of the internal constraints, those that have "vec" on the outside
13214 * are tightened. The shared facet is included in the adjacent subset
13215 * with the opposite constraint.
13216 * For constraints that saturate "vec", this criterion cannot be used
13217 * to determine which of the two sides should be tightened.
13218 * Instead, the sign of the first non-zero coefficient is used
13219 * to make this choice. Note that this second criterion is never used
13220 * on the constraints of S since "vec" is interior to "S".
13222 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13223 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13225 int j;
13227 bset = isl_basic_set_cow(bset);
13228 if (!bset)
13229 return NULL;
13230 for (j = 0; j < bset->n_ineq; ++j) {
13231 isl_bool internal;
13233 internal = is_internal(vec, bset, j);
13234 if (internal < 0)
13235 return isl_basic_set_free(bset);
13236 if (internal)
13237 continue;
13238 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13241 return bset;