doc: clean up isl_val documentation
[isl.git] / isl_map.c
blob0052be2cab65b81fb6bfe51f0ed2e55c0e1e03bf
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
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <string.h>
19 #include <isl_ctx_private.h>
20 #include <isl_map_private.h>
21 #include <isl_blk.h>
22 #include "isl_space_private.h"
23 #include "isl_equalities.h"
24 #include <isl_lp_private.h>
25 #include <isl_seq.h>
26 #include <isl/set.h>
27 #include <isl/map.h>
28 #include <isl_reordering.h>
29 #include "isl_sample.h"
30 #include <isl_sort.h>
31 #include "isl_tab.h"
32 #include <isl/vec.h>
33 #include <isl_mat_private.h>
34 #include <isl_vec_private.h>
35 #include <isl_dim_map.h>
36 #include <isl_local_space_private.h>
37 #include <isl_aff_private.h>
38 #include <isl_options_private.h>
39 #include <isl_morph.h>
40 #include <isl_val_private.h>
41 #include <isl/deprecated/map_int.h>
42 #include <isl/deprecated/set_int.h>
44 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
46 switch (type) {
47 case isl_dim_param: return dim->nparam;
48 case isl_dim_in: return dim->n_in;
49 case isl_dim_out: return dim->n_out;
50 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
51 default: return 0;
55 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
57 switch (type) {
58 case isl_dim_param: return 1;
59 case isl_dim_in: return 1 + dim->nparam;
60 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
61 default: return 0;
65 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
66 enum isl_dim_type type)
68 if (!bmap)
69 return 0;
70 switch (type) {
71 case isl_dim_cst: return 1;
72 case isl_dim_param:
73 case isl_dim_in:
74 case isl_dim_out: return isl_space_dim(bmap->dim, type);
75 case isl_dim_div: return bmap->n_div;
76 case isl_dim_all: return isl_basic_map_total_dim(bmap);
77 default: return 0;
81 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
83 return map ? n(map->dim, type) : 0;
86 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
88 return set ? n(set->dim, type) : 0;
91 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
92 enum isl_dim_type type)
94 isl_space *dim = bmap->dim;
95 switch (type) {
96 case isl_dim_cst: return 0;
97 case isl_dim_param: return 1;
98 case isl_dim_in: return 1 + dim->nparam;
99 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
100 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
101 default: return 0;
105 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
106 enum isl_dim_type type)
108 return isl_basic_map_offset(bset, type);
111 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
113 return pos(map->dim, type);
116 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
117 enum isl_dim_type type)
119 return isl_basic_map_dim(bset, type);
122 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
124 return isl_basic_set_dim(bset, isl_dim_set);
127 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
129 return isl_basic_set_dim(bset, isl_dim_param);
132 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
134 if (!bset)
135 return 0;
136 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
139 unsigned isl_set_n_dim(__isl_keep isl_set *set)
141 return isl_set_dim(set, isl_dim_set);
144 unsigned isl_set_n_param(__isl_keep isl_set *set)
146 return isl_set_dim(set, isl_dim_param);
149 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
151 return bmap ? bmap->dim->n_in : 0;
154 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
156 return bmap ? bmap->dim->n_out : 0;
159 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
161 return bmap ? bmap->dim->nparam : 0;
164 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
166 return bmap ? bmap->n_div : 0;
169 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
171 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
174 unsigned isl_map_n_in(const struct isl_map *map)
176 return map ? map->dim->n_in : 0;
179 unsigned isl_map_n_out(const struct isl_map *map)
181 return map ? map->dim->n_out : 0;
184 unsigned isl_map_n_param(const struct isl_map *map)
186 return map ? map->dim->nparam : 0;
189 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
191 int m;
192 if (!map || !set)
193 return -1;
194 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
195 if (m < 0 || !m)
196 return m;
197 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
198 set->dim, isl_dim_set);
201 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
202 struct isl_basic_set *bset)
204 int m;
205 if (!bmap || !bset)
206 return -1;
207 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
208 if (m < 0 || !m)
209 return m;
210 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
211 bset->dim, isl_dim_set);
214 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
216 int m;
217 if (!map || !set)
218 return -1;
219 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
220 if (m < 0 || !m)
221 return m;
222 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
223 set->dim, isl_dim_set);
226 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
227 struct isl_basic_set *bset)
229 int m;
230 if (!bmap || !bset)
231 return -1;
232 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
233 if (m < 0 || !m)
234 return m;
235 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
236 bset->dim, isl_dim_set);
239 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
241 return bmap ? bmap->ctx : NULL;
244 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
246 return bset ? bset->ctx : NULL;
249 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
251 return map ? map->ctx : NULL;
254 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
256 return set ? set->ctx : NULL;
259 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
261 if (!bmap)
262 return NULL;
263 return isl_space_copy(bmap->dim);
266 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
268 if (!bset)
269 return NULL;
270 return isl_space_copy(bset->dim);
273 /* Extract the divs in "bmap" as a matrix.
275 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
277 int i;
278 isl_ctx *ctx;
279 isl_mat *div;
280 unsigned total;
281 unsigned cols;
283 if (!bmap)
284 return NULL;
286 ctx = isl_basic_map_get_ctx(bmap);
287 total = isl_space_dim(bmap->dim, isl_dim_all);
288 cols = 1 + 1 + total + bmap->n_div;
289 div = isl_mat_alloc(ctx, bmap->n_div, cols);
290 if (!div)
291 return NULL;
293 for (i = 0; i < bmap->n_div; ++i)
294 isl_seq_cpy(div->row[i], bmap->div[i], cols);
296 return div;
299 /* Extract the divs in "bset" as a matrix.
301 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
303 return isl_basic_map_get_divs(bset);
306 __isl_give isl_local_space *isl_basic_map_get_local_space(
307 __isl_keep isl_basic_map *bmap)
309 isl_mat *div;
311 if (!bmap)
312 return NULL;
314 div = isl_basic_map_get_divs(bmap);
315 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
318 __isl_give isl_local_space *isl_basic_set_get_local_space(
319 __isl_keep isl_basic_set *bset)
321 return isl_basic_map_get_local_space(bset);
324 __isl_give isl_basic_map *isl_basic_map_from_local_space(
325 __isl_take isl_local_space *ls)
327 int i;
328 int n_div;
329 isl_basic_map *bmap;
331 if (!ls)
332 return NULL;
334 n_div = isl_local_space_dim(ls, isl_dim_div);
335 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
336 n_div, 0, 2 * n_div);
338 for (i = 0; i < n_div; ++i)
339 if (isl_basic_map_alloc_div(bmap) < 0)
340 goto error;
342 for (i = 0; i < n_div; ++i) {
343 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
344 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
345 goto error;
348 isl_local_space_free(ls);
349 return bmap;
350 error:
351 isl_local_space_free(ls);
352 isl_basic_map_free(bmap);
353 return NULL;
356 __isl_give isl_basic_set *isl_basic_set_from_local_space(
357 __isl_take isl_local_space *ls)
359 return isl_basic_map_from_local_space(ls);
362 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
364 if (!map)
365 return NULL;
366 return isl_space_copy(map->dim);
369 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
371 if (!set)
372 return NULL;
373 return isl_space_copy(set->dim);
376 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
377 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
379 bmap = isl_basic_map_cow(bmap);
380 if (!bmap)
381 return NULL;
382 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
383 if (!bmap->dim)
384 goto error;
385 bmap = isl_basic_map_finalize(bmap);
386 return bmap;
387 error:
388 isl_basic_map_free(bmap);
389 return NULL;
392 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
393 __isl_take isl_basic_set *bset, const char *s)
395 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
398 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
399 enum isl_dim_type type)
401 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
404 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
405 enum isl_dim_type type, const char *s)
407 int i;
409 map = isl_map_cow(map);
410 if (!map)
411 return NULL;
413 map->dim = isl_space_set_tuple_name(map->dim, type, s);
414 if (!map->dim)
415 goto error;
417 for (i = 0; i < map->n; ++i) {
418 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
419 if (!map->p[i])
420 goto error;
423 return map;
424 error:
425 isl_map_free(map);
426 return NULL;
429 /* Replace the identifier of the tuple of type "type" by "id".
431 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
432 __isl_take isl_basic_map *bmap,
433 enum isl_dim_type type, __isl_take isl_id *id)
435 bmap = isl_basic_map_cow(bmap);
436 if (!bmap)
437 goto error;
438 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
439 if (!bmap->dim)
440 return isl_basic_map_free(bmap);
441 bmap = isl_basic_map_finalize(bmap);
442 return bmap;
443 error:
444 isl_id_free(id);
445 return NULL;
448 /* Replace the identifier of the tuple by "id".
450 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
451 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
453 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
456 /* Does the input or output tuple have a name?
458 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
460 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
463 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
464 enum isl_dim_type type)
466 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
469 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
470 const char *s)
472 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
475 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
476 enum isl_dim_type type, __isl_take isl_id *id)
478 map = isl_map_cow(map);
479 if (!map)
480 goto error;
482 map->dim = isl_space_set_tuple_id(map->dim, type, id);
484 return isl_map_reset_space(map, isl_space_copy(map->dim));
485 error:
486 isl_id_free(id);
487 return NULL;
490 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
491 __isl_take isl_id *id)
493 return isl_map_set_tuple_id(set, isl_dim_set, id);
496 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
497 enum isl_dim_type type)
499 map = isl_map_cow(map);
500 if (!map)
501 return NULL;
503 map->dim = isl_space_reset_tuple_id(map->dim, type);
505 return isl_map_reset_space(map, isl_space_copy(map->dim));
508 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
510 return isl_map_reset_tuple_id(set, isl_dim_set);
513 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
515 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
518 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
519 enum isl_dim_type type)
521 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
524 int isl_set_has_tuple_id(__isl_keep isl_set *set)
526 return isl_map_has_tuple_id(set, isl_dim_set);
529 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
531 return isl_map_get_tuple_id(set, isl_dim_set);
534 /* Does the set tuple have a name?
536 int isl_set_has_tuple_name(__isl_keep isl_set *set)
538 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
542 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
544 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
547 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
549 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
552 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
553 enum isl_dim_type type, unsigned pos)
555 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
558 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
559 enum isl_dim_type type, unsigned pos)
561 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
564 /* Does the given dimension have a name?
566 int isl_map_has_dim_name(__isl_keep isl_map *map,
567 enum isl_dim_type type, unsigned pos)
569 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
572 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
573 enum isl_dim_type type, unsigned pos)
575 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
578 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
579 enum isl_dim_type type, unsigned pos)
581 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
584 /* Does the given dimension have a name?
586 int isl_set_has_dim_name(__isl_keep isl_set *set,
587 enum isl_dim_type type, unsigned pos)
589 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
592 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
593 __isl_take isl_basic_map *bmap,
594 enum isl_dim_type type, unsigned pos, const char *s)
596 bmap = isl_basic_map_cow(bmap);
597 if (!bmap)
598 return NULL;
599 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
600 if (!bmap->dim)
601 goto error;
602 return isl_basic_map_finalize(bmap);
603 error:
604 isl_basic_map_free(bmap);
605 return NULL;
608 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
609 enum isl_dim_type type, unsigned pos, const char *s)
611 int i;
613 map = isl_map_cow(map);
614 if (!map)
615 return NULL;
617 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
618 if (!map->dim)
619 goto error;
621 for (i = 0; i < map->n; ++i) {
622 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
623 if (!map->p[i])
624 goto error;
627 return map;
628 error:
629 isl_map_free(map);
630 return NULL;
633 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
634 __isl_take isl_basic_set *bset,
635 enum isl_dim_type type, unsigned pos, const char *s)
637 return (isl_basic_set *)isl_basic_map_set_dim_name(
638 (isl_basic_map *)bset, type, pos, s);
641 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
642 enum isl_dim_type type, unsigned pos, const char *s)
644 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
647 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
648 enum isl_dim_type type, unsigned pos)
650 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
653 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
654 enum isl_dim_type type, unsigned pos)
656 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
659 int isl_map_has_dim_id(__isl_keep isl_map *map,
660 enum isl_dim_type type, unsigned pos)
662 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
665 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
666 enum isl_dim_type type, unsigned pos)
668 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
671 int isl_set_has_dim_id(__isl_keep isl_set *set,
672 enum isl_dim_type type, unsigned pos)
674 return isl_map_has_dim_id(set, type, pos);
677 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
678 enum isl_dim_type type, unsigned pos)
680 return isl_map_get_dim_id(set, type, pos);
683 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
684 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
686 map = isl_map_cow(map);
687 if (!map)
688 goto error;
690 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
692 return isl_map_reset_space(map, isl_space_copy(map->dim));
693 error:
694 isl_id_free(id);
695 return NULL;
698 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
699 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
701 return isl_map_set_dim_id(set, type, pos, id);
704 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
705 __isl_keep isl_id *id)
707 if (!map)
708 return -1;
709 return isl_space_find_dim_by_id(map->dim, type, id);
712 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
713 __isl_keep isl_id *id)
715 return isl_map_find_dim_by_id(set, type, id);
718 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
719 const char *name)
721 if (!map)
722 return -1;
723 return isl_space_find_dim_by_name(map->dim, type, name);
726 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
727 const char *name)
729 return isl_map_find_dim_by_name(set, type, name);
732 /* Reset the user pointer on all identifiers of parameters and tuples
733 * of the space of "map".
735 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
737 isl_space *space;
739 space = isl_map_get_space(map);
740 space = isl_space_reset_user(space);
741 map = isl_map_reset_space(map, space);
743 return map;
746 /* Reset the user pointer on all identifiers of parameters and tuples
747 * of the space of "set".
749 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
751 return isl_map_reset_user(set);
754 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
756 if (!bmap)
757 return -1;
758 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
761 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
763 return isl_basic_map_is_rational(bset);
766 /* Does "bmap" contain any rational points?
768 * If "bmap" has an equality for each dimension, equating the dimension
769 * to an integer constant, then it has no rational points, even if it
770 * is marked as rational.
772 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
774 int has_rational = 1;
775 unsigned total;
777 if (!bmap)
778 return -1;
779 if (isl_basic_map_plain_is_empty(bmap))
780 return 0;
781 if (!isl_basic_map_is_rational(bmap))
782 return 0;
783 bmap = isl_basic_map_copy(bmap);
784 bmap = isl_basic_map_implicit_equalities(bmap);
785 if (!bmap)
786 return -1;
787 total = isl_basic_map_total_dim(bmap);
788 if (bmap->n_eq == total) {
789 int i, j;
790 for (i = 0; i < bmap->n_eq; ++i) {
791 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
792 if (j < 0)
793 break;
794 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
795 !isl_int_is_negone(bmap->eq[i][1 + j]))
796 break;
797 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
798 total - j - 1);
799 if (j >= 0)
800 break;
802 if (i == bmap->n_eq)
803 has_rational = 0;
805 isl_basic_map_free(bmap);
807 return has_rational;
810 /* Does "map" contain any rational points?
812 int isl_map_has_rational(__isl_keep isl_map *map)
814 int i;
815 int has_rational;
817 if (!map)
818 return -1;
819 for (i = 0; i < map->n; ++i) {
820 has_rational = isl_basic_map_has_rational(map->p[i]);
821 if (has_rational < 0)
822 return -1;
823 if (has_rational)
824 return 1;
826 return 0;
829 /* Does "set" contain any rational points?
831 int isl_set_has_rational(__isl_keep isl_set *set)
833 return isl_map_has_rational(set);
836 /* Is this basic set a parameter domain?
838 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
840 if (!bset)
841 return -1;
842 return isl_space_is_params(bset->dim);
845 /* Is this set a parameter domain?
847 int isl_set_is_params(__isl_keep isl_set *set)
849 if (!set)
850 return -1;
851 return isl_space_is_params(set->dim);
854 /* Is this map actually a parameter domain?
855 * Users should never call this function. Outside of isl,
856 * a map can never be a parameter domain.
858 int isl_map_is_params(__isl_keep isl_map *map)
860 if (!map)
861 return -1;
862 return isl_space_is_params(map->dim);
865 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
866 struct isl_basic_map *bmap, unsigned extra,
867 unsigned n_eq, unsigned n_ineq)
869 int i;
870 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
872 bmap->ctx = ctx;
873 isl_ctx_ref(ctx);
875 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
876 if (isl_blk_is_error(bmap->block))
877 goto error;
879 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
880 if ((n_ineq + n_eq) && !bmap->ineq)
881 goto error;
883 if (extra == 0) {
884 bmap->block2 = isl_blk_empty();
885 bmap->div = NULL;
886 } else {
887 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
888 if (isl_blk_is_error(bmap->block2))
889 goto error;
891 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
892 if (!bmap->div)
893 goto error;
896 for (i = 0; i < n_ineq + n_eq; ++i)
897 bmap->ineq[i] = bmap->block.data + i * row_size;
899 for (i = 0; i < extra; ++i)
900 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
902 bmap->ref = 1;
903 bmap->flags = 0;
904 bmap->c_size = n_eq + n_ineq;
905 bmap->eq = bmap->ineq + n_ineq;
906 bmap->extra = extra;
907 bmap->n_eq = 0;
908 bmap->n_ineq = 0;
909 bmap->n_div = 0;
910 bmap->sample = NULL;
912 return bmap;
913 error:
914 isl_basic_map_free(bmap);
915 return NULL;
918 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
919 unsigned nparam, unsigned dim, unsigned extra,
920 unsigned n_eq, unsigned n_ineq)
922 struct isl_basic_map *bmap;
923 isl_space *space;
925 space = isl_space_set_alloc(ctx, nparam, dim);
926 if (!space)
927 return NULL;
929 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
930 return (struct isl_basic_set *)bmap;
933 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
934 unsigned extra, unsigned n_eq, unsigned n_ineq)
936 struct isl_basic_map *bmap;
937 if (!dim)
938 return NULL;
939 isl_assert(dim->ctx, dim->n_in == 0, goto error);
940 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
941 return (struct isl_basic_set *)bmap;
942 error:
943 isl_space_free(dim);
944 return NULL;
947 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
948 unsigned extra, unsigned n_eq, unsigned n_ineq)
950 struct isl_basic_map *bmap;
952 if (!dim)
953 return NULL;
954 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
955 if (!bmap)
956 goto error;
957 bmap->dim = dim;
959 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
960 error:
961 isl_space_free(dim);
962 return NULL;
965 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
966 unsigned nparam, unsigned in, unsigned out, unsigned extra,
967 unsigned n_eq, unsigned n_ineq)
969 struct isl_basic_map *bmap;
970 isl_space *dim;
972 dim = isl_space_alloc(ctx, nparam, in, out);
973 if (!dim)
974 return NULL;
976 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
977 return bmap;
980 static void dup_constraints(
981 struct isl_basic_map *dst, struct isl_basic_map *src)
983 int i;
984 unsigned total = isl_basic_map_total_dim(src);
986 for (i = 0; i < src->n_eq; ++i) {
987 int j = isl_basic_map_alloc_equality(dst);
988 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
991 for (i = 0; i < src->n_ineq; ++i) {
992 int j = isl_basic_map_alloc_inequality(dst);
993 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
996 for (i = 0; i < src->n_div; ++i) {
997 int j = isl_basic_map_alloc_div(dst);
998 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1000 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1003 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1005 struct isl_basic_map *dup;
1007 if (!bmap)
1008 return NULL;
1009 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1010 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1011 if (!dup)
1012 return NULL;
1013 dup_constraints(dup, bmap);
1014 dup->flags = bmap->flags;
1015 dup->sample = isl_vec_copy(bmap->sample);
1016 return dup;
1019 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1021 struct isl_basic_map *dup;
1023 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1024 return (struct isl_basic_set *)dup;
1027 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1029 if (!bset)
1030 return NULL;
1032 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1033 bset->ref++;
1034 return bset;
1036 return isl_basic_set_dup(bset);
1039 struct isl_set *isl_set_copy(struct isl_set *set)
1041 if (!set)
1042 return NULL;
1044 set->ref++;
1045 return set;
1048 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1050 if (!bmap)
1051 return NULL;
1053 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1054 bmap->ref++;
1055 return bmap;
1057 bmap = isl_basic_map_dup(bmap);
1058 if (bmap)
1059 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1060 return bmap;
1063 struct isl_map *isl_map_copy(struct isl_map *map)
1065 if (!map)
1066 return NULL;
1068 map->ref++;
1069 return map;
1072 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1074 if (!bmap)
1075 return NULL;
1077 if (--bmap->ref > 0)
1078 return NULL;
1080 isl_ctx_deref(bmap->ctx);
1081 free(bmap->div);
1082 isl_blk_free(bmap->ctx, bmap->block2);
1083 free(bmap->ineq);
1084 isl_blk_free(bmap->ctx, bmap->block);
1085 isl_vec_free(bmap->sample);
1086 isl_space_free(bmap->dim);
1087 free(bmap);
1089 return NULL;
1092 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1094 return isl_basic_map_free((struct isl_basic_map *)bset);
1097 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1099 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1102 __isl_give isl_map *isl_map_align_params_map_map_and(
1103 __isl_take isl_map *map1, __isl_take isl_map *map2,
1104 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1105 __isl_take isl_map *map2))
1107 if (!map1 || !map2)
1108 goto error;
1109 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1110 return fn(map1, map2);
1111 if (!isl_space_has_named_params(map1->dim) ||
1112 !isl_space_has_named_params(map2->dim))
1113 isl_die(map1->ctx, isl_error_invalid,
1114 "unaligned unnamed parameters", goto error);
1115 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1116 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1117 return fn(map1, map2);
1118 error:
1119 isl_map_free(map1);
1120 isl_map_free(map2);
1121 return NULL;
1124 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1125 __isl_keep isl_map *map2,
1126 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1128 int r;
1130 if (!map1 || !map2)
1131 return -1;
1132 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1133 return fn(map1, map2);
1134 if (!isl_space_has_named_params(map1->dim) ||
1135 !isl_space_has_named_params(map2->dim))
1136 isl_die(map1->ctx, isl_error_invalid,
1137 "unaligned unnamed parameters", return -1);
1138 map1 = isl_map_copy(map1);
1139 map2 = isl_map_copy(map2);
1140 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1141 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1142 r = fn(map1, map2);
1143 isl_map_free(map1);
1144 isl_map_free(map2);
1145 return r;
1148 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1150 struct isl_ctx *ctx;
1151 if (!bmap)
1152 return -1;
1153 ctx = bmap->ctx;
1154 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1155 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1156 return -1);
1157 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1158 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1159 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1160 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1161 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1162 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1163 isl_int *t;
1164 int j = isl_basic_map_alloc_inequality(bmap);
1165 if (j < 0)
1166 return -1;
1167 t = bmap->ineq[j];
1168 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1169 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1170 bmap->eq[-1] = t;
1171 bmap->n_eq++;
1172 bmap->n_ineq--;
1173 bmap->eq--;
1174 return 0;
1176 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1177 bmap->extra - bmap->n_div);
1178 return bmap->n_eq++;
1181 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1183 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1186 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1188 if (!bmap)
1189 return -1;
1190 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1191 bmap->n_eq -= n;
1192 return 0;
1195 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1197 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1200 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1202 isl_int *t;
1203 if (!bmap)
1204 return -1;
1205 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1207 if (pos != bmap->n_eq - 1) {
1208 t = bmap->eq[pos];
1209 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1210 bmap->eq[bmap->n_eq - 1] = t;
1212 bmap->n_eq--;
1213 return 0;
1216 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1218 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1221 /* Turn inequality "pos" of "bmap" into an equality.
1223 * In particular, we move the inequality in front of the equalities
1224 * and move the last inequality in the position of the moved inequality.
1225 * Note that isl_tab_make_equalities_explicit depends on this particular
1226 * change in the ordering of the constraints.
1228 void isl_basic_map_inequality_to_equality(
1229 struct isl_basic_map *bmap, unsigned pos)
1231 isl_int *t;
1233 t = bmap->ineq[pos];
1234 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1235 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1236 bmap->eq[-1] = t;
1237 bmap->n_eq++;
1238 bmap->n_ineq--;
1239 bmap->eq--;
1240 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1241 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1242 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1243 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1246 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1248 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1251 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1253 struct isl_ctx *ctx;
1254 if (!bmap)
1255 return -1;
1256 ctx = bmap->ctx;
1257 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1258 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1259 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1260 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1261 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1262 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1263 1 + isl_basic_map_total_dim(bmap),
1264 bmap->extra - bmap->n_div);
1265 return bmap->n_ineq++;
1268 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1270 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1273 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1275 if (!bmap)
1276 return -1;
1277 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1278 bmap->n_ineq -= n;
1279 return 0;
1282 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1284 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1287 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1289 isl_int *t;
1290 if (!bmap)
1291 return -1;
1292 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1294 if (pos != bmap->n_ineq - 1) {
1295 t = bmap->ineq[pos];
1296 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1297 bmap->ineq[bmap->n_ineq - 1] = t;
1298 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1300 bmap->n_ineq--;
1301 return 0;
1304 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1306 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1309 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1310 isl_int *eq)
1312 int k;
1314 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1315 if (!bmap)
1316 return NULL;
1317 k = isl_basic_map_alloc_equality(bmap);
1318 if (k < 0)
1319 goto error;
1320 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1321 return bmap;
1322 error:
1323 isl_basic_map_free(bmap);
1324 return NULL;
1327 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1328 isl_int *eq)
1330 return (isl_basic_set *)
1331 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1334 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1335 isl_int *ineq)
1337 int k;
1339 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1340 if (!bmap)
1341 return NULL;
1342 k = isl_basic_map_alloc_inequality(bmap);
1343 if (k < 0)
1344 goto error;
1345 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1346 return bmap;
1347 error:
1348 isl_basic_map_free(bmap);
1349 return NULL;
1352 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1353 isl_int *ineq)
1355 return (isl_basic_set *)
1356 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1359 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1361 if (!bmap)
1362 return -1;
1363 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1364 isl_seq_clr(bmap->div[bmap->n_div] +
1365 1 + 1 + isl_basic_map_total_dim(bmap),
1366 bmap->extra - bmap->n_div);
1367 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1368 return bmap->n_div++;
1371 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1373 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1376 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1378 if (!bmap)
1379 return -1;
1380 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1381 bmap->n_div -= n;
1382 return 0;
1385 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1387 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1390 /* Copy constraint from src to dst, putting the vars of src at offset
1391 * dim_off in dst and the divs of src at offset div_off in dst.
1392 * If both sets are actually map, then dim_off applies to the input
1393 * variables.
1395 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1396 struct isl_basic_map *src_map, isl_int *src,
1397 unsigned in_off, unsigned out_off, unsigned div_off)
1399 unsigned src_nparam = isl_basic_map_n_param(src_map);
1400 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1401 unsigned src_in = isl_basic_map_n_in(src_map);
1402 unsigned dst_in = isl_basic_map_n_in(dst_map);
1403 unsigned src_out = isl_basic_map_n_out(src_map);
1404 unsigned dst_out = isl_basic_map_n_out(dst_map);
1405 isl_int_set(dst[0], src[0]);
1406 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1407 if (dst_nparam > src_nparam)
1408 isl_seq_clr(dst+1+src_nparam,
1409 dst_nparam - src_nparam);
1410 isl_seq_clr(dst+1+dst_nparam, in_off);
1411 isl_seq_cpy(dst+1+dst_nparam+in_off,
1412 src+1+src_nparam,
1413 isl_min(dst_in-in_off, src_in));
1414 if (dst_in-in_off > src_in)
1415 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1416 dst_in - in_off - src_in);
1417 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1418 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1419 src+1+src_nparam+src_in,
1420 isl_min(dst_out-out_off, src_out));
1421 if (dst_out-out_off > src_out)
1422 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1423 dst_out - out_off - src_out);
1424 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1425 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1426 src+1+src_nparam+src_in+src_out,
1427 isl_min(dst_map->extra-div_off, src_map->n_div));
1428 if (dst_map->n_div-div_off > src_map->n_div)
1429 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1430 div_off+src_map->n_div,
1431 dst_map->n_div - div_off - src_map->n_div);
1434 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1435 struct isl_basic_map *src_map, isl_int *src,
1436 unsigned in_off, unsigned out_off, unsigned div_off)
1438 isl_int_set(dst[0], src[0]);
1439 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1442 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1443 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1445 int i;
1446 unsigned div_off;
1448 if (!bmap1 || !bmap2)
1449 goto error;
1451 div_off = bmap1->n_div;
1453 for (i = 0; i < bmap2->n_eq; ++i) {
1454 int i1 = isl_basic_map_alloc_equality(bmap1);
1455 if (i1 < 0)
1456 goto error;
1457 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1458 i_pos, o_pos, div_off);
1461 for (i = 0; i < bmap2->n_ineq; ++i) {
1462 int i1 = isl_basic_map_alloc_inequality(bmap1);
1463 if (i1 < 0)
1464 goto error;
1465 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1466 i_pos, o_pos, div_off);
1469 for (i = 0; i < bmap2->n_div; ++i) {
1470 int i1 = isl_basic_map_alloc_div(bmap1);
1471 if (i1 < 0)
1472 goto error;
1473 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1474 i_pos, o_pos, div_off);
1477 isl_basic_map_free(bmap2);
1479 return bmap1;
1481 error:
1482 isl_basic_map_free(bmap1);
1483 isl_basic_map_free(bmap2);
1484 return NULL;
1487 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1488 struct isl_basic_set *bset2, unsigned pos)
1490 return (struct isl_basic_set *)
1491 add_constraints((struct isl_basic_map *)bset1,
1492 (struct isl_basic_map *)bset2, 0, pos);
1495 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1496 __isl_take isl_space *dim, unsigned extra,
1497 unsigned n_eq, unsigned n_ineq)
1499 struct isl_basic_map *ext;
1500 unsigned flags;
1501 int dims_ok;
1503 if (!dim)
1504 goto error;
1506 if (!base)
1507 goto error;
1509 dims_ok = isl_space_is_equal(base->dim, dim) &&
1510 base->extra >= base->n_div + extra;
1512 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1513 room_for_ineq(base, n_ineq)) {
1514 isl_space_free(dim);
1515 return base;
1518 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1519 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1520 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1521 extra += base->extra;
1522 n_eq += base->n_eq;
1523 n_ineq += base->n_ineq;
1525 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1526 dim = NULL;
1527 if (!ext)
1528 goto error;
1530 if (dims_ok)
1531 ext->sample = isl_vec_copy(base->sample);
1532 flags = base->flags;
1533 ext = add_constraints(ext, base, 0, 0);
1534 if (ext) {
1535 ext->flags = flags;
1536 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1539 return ext;
1541 error:
1542 isl_space_free(dim);
1543 isl_basic_map_free(base);
1544 return NULL;
1547 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1548 __isl_take isl_space *dim, unsigned extra,
1549 unsigned n_eq, unsigned n_ineq)
1551 return (struct isl_basic_set *)
1552 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1553 extra, n_eq, n_ineq);
1556 struct isl_basic_map *isl_basic_map_extend_constraints(
1557 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1559 if (!base)
1560 return NULL;
1561 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1562 0, n_eq, n_ineq);
1565 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1566 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1567 unsigned n_eq, unsigned n_ineq)
1569 struct isl_basic_map *bmap;
1570 isl_space *dim;
1572 if (!base)
1573 return NULL;
1574 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1575 if (!dim)
1576 goto error;
1578 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1579 return bmap;
1580 error:
1581 isl_basic_map_free(base);
1582 return NULL;
1585 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1586 unsigned nparam, unsigned dim, unsigned extra,
1587 unsigned n_eq, unsigned n_ineq)
1589 return (struct isl_basic_set *)
1590 isl_basic_map_extend((struct isl_basic_map *)base,
1591 nparam, 0, dim, extra, n_eq, n_ineq);
1594 struct isl_basic_set *isl_basic_set_extend_constraints(
1595 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1597 return (struct isl_basic_set *)
1598 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1599 n_eq, n_ineq);
1602 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1604 return (struct isl_basic_set *)
1605 isl_basic_map_cow((struct isl_basic_map *)bset);
1608 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1610 if (!bmap)
1611 return NULL;
1613 if (bmap->ref > 1) {
1614 bmap->ref--;
1615 bmap = isl_basic_map_dup(bmap);
1617 if (bmap)
1618 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1619 return bmap;
1622 struct isl_set *isl_set_cow(struct isl_set *set)
1624 if (!set)
1625 return NULL;
1627 if (set->ref == 1)
1628 return set;
1629 set->ref--;
1630 return isl_set_dup(set);
1633 struct isl_map *isl_map_cow(struct isl_map *map)
1635 if (!map)
1636 return NULL;
1638 if (map->ref == 1)
1639 return map;
1640 map->ref--;
1641 return isl_map_dup(map);
1644 static void swap_vars(struct isl_blk blk, isl_int *a,
1645 unsigned a_len, unsigned b_len)
1647 isl_seq_cpy(blk.data, a+a_len, b_len);
1648 isl_seq_cpy(blk.data+b_len, a, a_len);
1649 isl_seq_cpy(a, blk.data, b_len+a_len);
1652 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1653 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1655 int i;
1656 struct isl_blk blk;
1658 if (!bmap)
1659 goto error;
1661 isl_assert(bmap->ctx,
1662 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1664 if (n1 == 0 || n2 == 0)
1665 return bmap;
1667 bmap = isl_basic_map_cow(bmap);
1668 if (!bmap)
1669 return NULL;
1671 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1672 if (isl_blk_is_error(blk))
1673 goto error;
1675 for (i = 0; i < bmap->n_eq; ++i)
1676 swap_vars(blk,
1677 bmap->eq[i] + pos, n1, n2);
1679 for (i = 0; i < bmap->n_ineq; ++i)
1680 swap_vars(blk,
1681 bmap->ineq[i] + pos, n1, n2);
1683 for (i = 0; i < bmap->n_div; ++i)
1684 swap_vars(blk,
1685 bmap->div[i]+1 + pos, n1, n2);
1687 isl_blk_free(bmap->ctx, blk);
1689 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1690 bmap = isl_basic_map_gauss(bmap, NULL);
1691 return isl_basic_map_finalize(bmap);
1692 error:
1693 isl_basic_map_free(bmap);
1694 return NULL;
1697 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1698 __isl_take isl_basic_set *bset, unsigned n)
1700 unsigned dim;
1701 unsigned nparam;
1703 if (!bset)
1704 return NULL;
1706 nparam = isl_basic_set_n_param(bset);
1707 dim = isl_basic_set_n_dim(bset);
1708 isl_assert(bset->ctx, n <= dim, goto error);
1710 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1711 error:
1712 isl_basic_set_free(bset);
1713 return NULL;
1716 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1718 int i = 0;
1719 unsigned total;
1720 if (!bmap)
1721 goto error;
1722 total = isl_basic_map_total_dim(bmap);
1723 isl_basic_map_free_div(bmap, bmap->n_div);
1724 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1725 if (bmap->n_eq > 0)
1726 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1727 else {
1728 i = isl_basic_map_alloc_equality(bmap);
1729 if (i < 0)
1730 goto error;
1732 isl_int_set_si(bmap->eq[i][0], 1);
1733 isl_seq_clr(bmap->eq[i]+1, total);
1734 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1735 isl_vec_free(bmap->sample);
1736 bmap->sample = NULL;
1737 return isl_basic_map_finalize(bmap);
1738 error:
1739 isl_basic_map_free(bmap);
1740 return NULL;
1743 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1745 return (struct isl_basic_set *)
1746 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1749 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1750 * of "bmap").
1752 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1754 isl_int *t = bmap->div[a];
1755 bmap->div[a] = bmap->div[b];
1756 bmap->div[b] = t;
1759 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1760 * div definitions accordingly.
1762 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1764 int i;
1765 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1767 swap_div(bmap, a, b);
1769 for (i = 0; i < bmap->n_eq; ++i)
1770 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1772 for (i = 0; i < bmap->n_ineq; ++i)
1773 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1775 for (i = 0; i < bmap->n_div; ++i)
1776 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1777 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1780 /* Eliminate the specified n dimensions starting at first from the
1781 * constraints, without removing the dimensions from the space.
1782 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1784 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1785 enum isl_dim_type type, unsigned first, unsigned n)
1787 int i;
1789 if (!map)
1790 return NULL;
1791 if (n == 0)
1792 return map;
1794 if (first + n > isl_map_dim(map, type) || first + n < first)
1795 isl_die(map->ctx, isl_error_invalid,
1796 "index out of bounds", goto error);
1798 map = isl_map_cow(map);
1799 if (!map)
1800 return NULL;
1802 for (i = 0; i < map->n; ++i) {
1803 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1804 if (!map->p[i])
1805 goto error;
1807 return map;
1808 error:
1809 isl_map_free(map);
1810 return NULL;
1813 /* Eliminate the specified n dimensions starting at first from the
1814 * constraints, without removing the dimensions from the space.
1815 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1817 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1818 enum isl_dim_type type, unsigned first, unsigned n)
1820 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1823 /* Eliminate the specified n dimensions starting at first from the
1824 * constraints, without removing the dimensions from the space.
1825 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1827 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1828 unsigned first, unsigned n)
1830 return isl_set_eliminate(set, isl_dim_set, first, n);
1833 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1834 __isl_take isl_basic_map *bmap)
1836 if (!bmap)
1837 return NULL;
1838 bmap = isl_basic_map_eliminate_vars(bmap,
1839 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1840 if (!bmap)
1841 return NULL;
1842 bmap->n_div = 0;
1843 return isl_basic_map_finalize(bmap);
1846 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1847 __isl_take isl_basic_set *bset)
1849 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1850 (struct isl_basic_map *)bset);
1853 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1855 int i;
1857 if (!map)
1858 return NULL;
1859 if (map->n == 0)
1860 return map;
1862 map = isl_map_cow(map);
1863 if (!map)
1864 return NULL;
1866 for (i = 0; i < map->n; ++i) {
1867 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1868 if (!map->p[i])
1869 goto error;
1871 return map;
1872 error:
1873 isl_map_free(map);
1874 return NULL;
1877 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1879 return isl_map_remove_divs(set);
1882 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1883 enum isl_dim_type type, unsigned first, unsigned n)
1885 if (!bmap)
1886 return NULL;
1887 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1888 goto error);
1889 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1890 return bmap;
1891 bmap = isl_basic_map_eliminate_vars(bmap,
1892 isl_basic_map_offset(bmap, type) - 1 + first, n);
1893 if (!bmap)
1894 return bmap;
1895 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1896 return bmap;
1897 bmap = isl_basic_map_drop(bmap, type, first, n);
1898 return bmap;
1899 error:
1900 isl_basic_map_free(bmap);
1901 return NULL;
1904 /* Return true if the definition of the given div (recursively) involves
1905 * any of the given variables.
1907 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1908 unsigned first, unsigned n)
1910 int i;
1911 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1913 if (isl_int_is_zero(bmap->div[div][0]))
1914 return 0;
1915 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1916 return 1;
1918 for (i = bmap->n_div - 1; i >= 0; --i) {
1919 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1920 continue;
1921 if (div_involves_vars(bmap, i, first, n))
1922 return 1;
1925 return 0;
1928 /* Try and add a lower and/or upper bound on "div" to "bmap"
1929 * based on inequality "i".
1930 * "total" is the total number of variables (excluding the divs).
1931 * "v" is a temporary object that can be used during the calculations.
1932 * If "lb" is set, then a lower bound should be constructed.
1933 * If "ub" is set, then an upper bound should be constructed.
1935 * The calling function has already checked that the inequality does not
1936 * reference "div", but we still need to check that the inequality is
1937 * of the right form. We'll consider the case where we want to construct
1938 * a lower bound. The construction of upper bounds is similar.
1940 * Let "div" be of the form
1942 * q = floor((a + f(x))/d)
1944 * We essentially check if constraint "i" is of the form
1946 * b + f(x) >= 0
1948 * so that we can use it to derive a lower bound on "div".
1949 * However, we allow a slightly more general form
1951 * b + g(x) >= 0
1953 * with the condition that the coefficients of g(x) - f(x) are all
1954 * divisible by d.
1955 * Rewriting this constraint as
1957 * 0 >= -b - g(x)
1959 * adding a + f(x) to both sides and dividing by d, we obtain
1961 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1963 * Taking the floor on both sides, we obtain
1965 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1967 * or
1969 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1971 * In the case of an upper bound, we construct the constraint
1973 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1976 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1977 __isl_take isl_basic_map *bmap, int div, int i,
1978 unsigned total, isl_int v, int lb, int ub)
1980 int j;
1982 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1983 if (lb) {
1984 isl_int_sub(v, bmap->ineq[i][1 + j],
1985 bmap->div[div][1 + 1 + j]);
1986 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1988 if (ub) {
1989 isl_int_add(v, bmap->ineq[i][1 + j],
1990 bmap->div[div][1 + 1 + j]);
1991 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1994 if (!lb && !ub)
1995 return bmap;
1997 bmap = isl_basic_map_cow(bmap);
1998 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1999 if (lb) {
2000 int k = isl_basic_map_alloc_inequality(bmap);
2001 if (k < 0)
2002 goto error;
2003 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2004 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2005 bmap->div[div][1 + j]);
2006 isl_int_cdiv_q(bmap->ineq[k][j],
2007 bmap->ineq[k][j], bmap->div[div][0]);
2009 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2011 if (ub) {
2012 int k = isl_basic_map_alloc_inequality(bmap);
2013 if (k < 0)
2014 goto error;
2015 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2016 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2017 bmap->div[div][1 + j]);
2018 isl_int_fdiv_q(bmap->ineq[k][j],
2019 bmap->ineq[k][j], bmap->div[div][0]);
2021 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2024 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2025 return bmap;
2026 error:
2027 isl_basic_map_free(bmap);
2028 return NULL;
2031 /* This function is called right before "div" is eliminated from "bmap"
2032 * using Fourier-Motzkin.
2033 * Look through the constraints of "bmap" for constraints on the argument
2034 * of the integer division and use them to construct constraints on the
2035 * integer division itself. These constraints can then be combined
2036 * during the Fourier-Motzkin elimination.
2037 * Note that it is only useful to introduce lower bounds on "div"
2038 * if "bmap" already contains upper bounds on "div" as the newly
2039 * introduce lower bounds can then be combined with the pre-existing
2040 * upper bounds. Similarly for upper bounds.
2041 * We therefore first check if "bmap" contains any lower and/or upper bounds
2042 * on "div".
2044 * It is interesting to note that the introduction of these constraints
2045 * can indeed lead to more accurate results, even when compared to
2046 * deriving constraints on the argument of "div" from constraints on "div".
2047 * Consider, for example, the set
2049 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2051 * The second constraint can be rewritten as
2053 * 2 * [(-i-2j+3)/4] + k >= 0
2055 * from which we can derive
2057 * -i - 2j + 3 >= -2k
2059 * or
2061 * i + 2j <= 3 + 2k
2063 * Combined with the first constraint, we obtain
2065 * -3 <= 3 + 2k or k >= -3
2067 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2068 * the first constraint, we obtain
2070 * [(i + 2j)/4] >= [-3/4] = -1
2072 * Combining this constraint with the second constraint, we obtain
2074 * k >= -2
2076 static __isl_give isl_basic_map *insert_bounds_on_div(
2077 __isl_take isl_basic_map *bmap, int div)
2079 int i;
2080 int check_lb, check_ub;
2081 isl_int v;
2082 unsigned total;
2084 if (!bmap)
2085 return NULL;
2087 if (isl_int_is_zero(bmap->div[div][0]))
2088 return bmap;
2090 total = isl_space_dim(bmap->dim, isl_dim_all);
2092 check_lb = 0;
2093 check_ub = 0;
2094 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2095 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2096 if (s > 0)
2097 check_ub = 1;
2098 if (s < 0)
2099 check_lb = 1;
2102 if (!check_lb && !check_ub)
2103 return bmap;
2105 isl_int_init(v);
2107 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2108 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2109 continue;
2111 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2112 check_lb, check_ub);
2115 isl_int_clear(v);
2117 return bmap;
2120 /* Remove all divs (recursively) involving any of the given dimensions
2121 * in their definitions.
2123 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2124 __isl_take isl_basic_map *bmap,
2125 enum isl_dim_type type, unsigned first, unsigned n)
2127 int i;
2129 if (!bmap)
2130 return NULL;
2131 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2132 goto error);
2133 first += isl_basic_map_offset(bmap, type);
2135 for (i = bmap->n_div - 1; i >= 0; --i) {
2136 if (!div_involves_vars(bmap, i, first, n))
2137 continue;
2138 bmap = insert_bounds_on_div(bmap, i);
2139 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2140 if (!bmap)
2141 return NULL;
2142 i = bmap->n_div;
2145 return bmap;
2146 error:
2147 isl_basic_map_free(bmap);
2148 return NULL;
2151 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2152 __isl_take isl_basic_set *bset,
2153 enum isl_dim_type type, unsigned first, unsigned n)
2155 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2158 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2159 enum isl_dim_type type, unsigned first, unsigned n)
2161 int i;
2163 if (!map)
2164 return NULL;
2165 if (map->n == 0)
2166 return map;
2168 map = isl_map_cow(map);
2169 if (!map)
2170 return NULL;
2172 for (i = 0; i < map->n; ++i) {
2173 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2174 type, first, n);
2175 if (!map->p[i])
2176 goto error;
2178 return map;
2179 error:
2180 isl_map_free(map);
2181 return NULL;
2184 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2185 enum isl_dim_type type, unsigned first, unsigned n)
2187 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2188 type, first, n);
2191 /* Does the desciption of "bmap" depend on the specified dimensions?
2192 * We also check whether the dimensions appear in any of the div definitions.
2193 * In principle there is no need for this check. If the dimensions appear
2194 * in a div definition, they also appear in the defining constraints of that
2195 * div.
2197 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2198 enum isl_dim_type type, unsigned first, unsigned n)
2200 int i;
2202 if (!bmap)
2203 return -1;
2205 if (first + n > isl_basic_map_dim(bmap, type))
2206 isl_die(bmap->ctx, isl_error_invalid,
2207 "index out of bounds", return -1);
2209 first += isl_basic_map_offset(bmap, type);
2210 for (i = 0; i < bmap->n_eq; ++i)
2211 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2212 return 1;
2213 for (i = 0; i < bmap->n_ineq; ++i)
2214 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2215 return 1;
2216 for (i = 0; i < bmap->n_div; ++i) {
2217 if (isl_int_is_zero(bmap->div[i][0]))
2218 continue;
2219 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2220 return 1;
2223 return 0;
2226 int isl_map_involves_dims(__isl_keep isl_map *map,
2227 enum isl_dim_type type, unsigned first, unsigned n)
2229 int i;
2231 if (!map)
2232 return -1;
2234 if (first + n > isl_map_dim(map, type))
2235 isl_die(map->ctx, isl_error_invalid,
2236 "index out of bounds", return -1);
2238 for (i = 0; i < map->n; ++i) {
2239 int involves = isl_basic_map_involves_dims(map->p[i],
2240 type, first, n);
2241 if (involves < 0 || involves)
2242 return involves;
2245 return 0;
2248 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2249 enum isl_dim_type type, unsigned first, unsigned n)
2251 return isl_basic_map_involves_dims(bset, type, first, n);
2254 int isl_set_involves_dims(__isl_keep isl_set *set,
2255 enum isl_dim_type type, unsigned first, unsigned n)
2257 return isl_map_involves_dims(set, type, first, n);
2260 /* Return true if the definition of the given div is unknown or depends
2261 * on unknown divs.
2263 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2265 int i;
2266 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2268 if (isl_int_is_zero(bmap->div[div][0]))
2269 return 1;
2271 for (i = bmap->n_div - 1; i >= 0; --i) {
2272 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2273 continue;
2274 if (div_is_unknown(bmap, i))
2275 return 1;
2278 return 0;
2281 /* Remove all divs that are unknown or defined in terms of unknown divs.
2283 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2284 __isl_take isl_basic_map *bmap)
2286 int i;
2288 if (!bmap)
2289 return NULL;
2291 for (i = bmap->n_div - 1; i >= 0; --i) {
2292 if (!div_is_unknown(bmap, i))
2293 continue;
2294 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2295 if (!bmap)
2296 return NULL;
2297 i = bmap->n_div;
2300 return bmap;
2303 /* Remove all divs that are unknown or defined in terms of unknown divs.
2305 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2306 __isl_take isl_basic_set *bset)
2308 return isl_basic_map_remove_unknown_divs(bset);
2311 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2313 int i;
2315 if (!map)
2316 return NULL;
2317 if (map->n == 0)
2318 return map;
2320 map = isl_map_cow(map);
2321 if (!map)
2322 return NULL;
2324 for (i = 0; i < map->n; ++i) {
2325 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2326 if (!map->p[i])
2327 goto error;
2329 return map;
2330 error:
2331 isl_map_free(map);
2332 return NULL;
2335 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2337 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2340 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2341 __isl_take isl_basic_set *bset,
2342 enum isl_dim_type type, unsigned first, unsigned n)
2344 return (isl_basic_set *)
2345 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2348 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2349 enum isl_dim_type type, unsigned first, unsigned n)
2351 int i;
2353 if (n == 0)
2354 return map;
2356 map = isl_map_cow(map);
2357 if (!map)
2358 return NULL;
2359 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2361 for (i = 0; i < map->n; ++i) {
2362 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2363 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2364 if (!map->p[i])
2365 goto error;
2367 map = isl_map_drop(map, type, first, n);
2368 return map;
2369 error:
2370 isl_map_free(map);
2371 return NULL;
2374 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2375 enum isl_dim_type type, unsigned first, unsigned n)
2377 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2380 /* Project out n inputs starting at first using Fourier-Motzkin */
2381 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2382 unsigned first, unsigned n)
2384 return isl_map_remove_dims(map, isl_dim_in, first, n);
2387 static void dump_term(struct isl_basic_map *bmap,
2388 isl_int c, int pos, FILE *out)
2390 const char *name;
2391 unsigned in = isl_basic_map_n_in(bmap);
2392 unsigned dim = in + isl_basic_map_n_out(bmap);
2393 unsigned nparam = isl_basic_map_n_param(bmap);
2394 if (!pos)
2395 isl_int_print(out, c, 0);
2396 else {
2397 if (!isl_int_is_one(c))
2398 isl_int_print(out, c, 0);
2399 if (pos < 1 + nparam) {
2400 name = isl_space_get_dim_name(bmap->dim,
2401 isl_dim_param, pos - 1);
2402 if (name)
2403 fprintf(out, "%s", name);
2404 else
2405 fprintf(out, "p%d", pos - 1);
2406 } else if (pos < 1 + nparam + in)
2407 fprintf(out, "i%d", pos - 1 - nparam);
2408 else if (pos < 1 + nparam + dim)
2409 fprintf(out, "o%d", pos - 1 - nparam - in);
2410 else
2411 fprintf(out, "e%d", pos - 1 - nparam - dim);
2415 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2416 int sign, FILE *out)
2418 int i;
2419 int first;
2420 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2421 isl_int v;
2423 isl_int_init(v);
2424 for (i = 0, first = 1; i < len; ++i) {
2425 if (isl_int_sgn(c[i]) * sign <= 0)
2426 continue;
2427 if (!first)
2428 fprintf(out, " + ");
2429 first = 0;
2430 isl_int_abs(v, c[i]);
2431 dump_term(bmap, v, i, out);
2433 isl_int_clear(v);
2434 if (first)
2435 fprintf(out, "0");
2438 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2439 const char *op, FILE *out, int indent)
2441 int i;
2443 fprintf(out, "%*s", indent, "");
2445 dump_constraint_sign(bmap, c, 1, out);
2446 fprintf(out, " %s ", op);
2447 dump_constraint_sign(bmap, c, -1, out);
2449 fprintf(out, "\n");
2451 for (i = bmap->n_div; i < bmap->extra; ++i) {
2452 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2453 continue;
2454 fprintf(out, "%*s", indent, "");
2455 fprintf(out, "ERROR: unused div coefficient not zero\n");
2456 abort();
2460 static void dump_constraints(struct isl_basic_map *bmap,
2461 isl_int **c, unsigned n,
2462 const char *op, FILE *out, int indent)
2464 int i;
2466 for (i = 0; i < n; ++i)
2467 dump_constraint(bmap, c[i], op, out, indent);
2470 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2472 int j;
2473 int first = 1;
2474 unsigned total = isl_basic_map_total_dim(bmap);
2476 for (j = 0; j < 1 + total; ++j) {
2477 if (isl_int_is_zero(exp[j]))
2478 continue;
2479 if (!first && isl_int_is_pos(exp[j]))
2480 fprintf(out, "+");
2481 dump_term(bmap, exp[j], j, out);
2482 first = 0;
2486 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2488 int i;
2490 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2491 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2493 for (i = 0; i < bmap->n_div; ++i) {
2494 fprintf(out, "%*s", indent, "");
2495 fprintf(out, "e%d = [(", i);
2496 dump_affine(bmap, bmap->div[i]+1, out);
2497 fprintf(out, ")/");
2498 isl_int_print(out, bmap->div[i][0], 0);
2499 fprintf(out, "]\n");
2503 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2504 FILE *out, int indent)
2506 if (!bset) {
2507 fprintf(out, "null basic set\n");
2508 return;
2511 fprintf(out, "%*s", indent, "");
2512 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2513 bset->ref, bset->dim->nparam, bset->dim->n_out,
2514 bset->extra, bset->flags);
2515 dump((struct isl_basic_map *)bset, out, indent);
2518 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2519 FILE *out, int indent)
2521 if (!bmap) {
2522 fprintf(out, "null basic map\n");
2523 return;
2526 fprintf(out, "%*s", indent, "");
2527 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2528 "flags: %x, n_name: %d\n",
2529 bmap->ref,
2530 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2531 bmap->extra, bmap->flags, bmap->dim->n_id);
2532 dump(bmap, out, indent);
2535 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2537 unsigned total;
2538 if (!bmap)
2539 return -1;
2540 total = isl_basic_map_total_dim(bmap);
2541 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2542 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2543 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2544 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2545 return 0;
2548 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2549 unsigned flags)
2551 struct isl_set *set;
2553 if (!dim)
2554 return NULL;
2555 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2556 isl_assert(dim->ctx, n >= 0, goto error);
2557 set = isl_alloc(dim->ctx, struct isl_set,
2558 sizeof(struct isl_set) +
2559 (n - 1) * sizeof(struct isl_basic_set *));
2560 if (!set)
2561 goto error;
2563 set->ctx = dim->ctx;
2564 isl_ctx_ref(set->ctx);
2565 set->ref = 1;
2566 set->size = n;
2567 set->n = 0;
2568 set->dim = dim;
2569 set->flags = flags;
2570 return set;
2571 error:
2572 isl_space_free(dim);
2573 return NULL;
2576 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2577 unsigned nparam, unsigned dim, int n, unsigned flags)
2579 struct isl_set *set;
2580 isl_space *dims;
2582 dims = isl_space_alloc(ctx, nparam, 0, dim);
2583 if (!dims)
2584 return NULL;
2586 set = isl_set_alloc_space(dims, n, flags);
2587 return set;
2590 /* Make sure "map" has room for at least "n" more basic maps.
2592 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2594 int i;
2595 struct isl_map *grown = NULL;
2597 if (!map)
2598 return NULL;
2599 isl_assert(map->ctx, n >= 0, goto error);
2600 if (map->n + n <= map->size)
2601 return map;
2602 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2603 if (!grown)
2604 goto error;
2605 for (i = 0; i < map->n; ++i) {
2606 grown->p[i] = isl_basic_map_copy(map->p[i]);
2607 if (!grown->p[i])
2608 goto error;
2609 grown->n++;
2611 isl_map_free(map);
2612 return grown;
2613 error:
2614 isl_map_free(grown);
2615 isl_map_free(map);
2616 return NULL;
2619 /* Make sure "set" has room for at least "n" more basic sets.
2621 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2623 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2626 struct isl_set *isl_set_dup(struct isl_set *set)
2628 int i;
2629 struct isl_set *dup;
2631 if (!set)
2632 return NULL;
2634 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2635 if (!dup)
2636 return NULL;
2637 for (i = 0; i < set->n; ++i)
2638 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2639 return dup;
2642 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2644 return isl_map_from_basic_map(bset);
2647 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2649 struct isl_map *map;
2651 if (!bmap)
2652 return NULL;
2654 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2655 return isl_map_add_basic_map(map, bmap);
2658 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2659 __isl_take isl_basic_set *bset)
2661 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2662 (struct isl_basic_map *)bset);
2665 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2667 int i;
2669 if (!set)
2670 return NULL;
2672 if (--set->ref > 0)
2673 return NULL;
2675 isl_ctx_deref(set->ctx);
2676 for (i = 0; i < set->n; ++i)
2677 isl_basic_set_free(set->p[i]);
2678 isl_space_free(set->dim);
2679 free(set);
2681 return NULL;
2684 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2686 int i;
2688 if (!set) {
2689 fprintf(out, "null set\n");
2690 return;
2693 fprintf(out, "%*s", indent, "");
2694 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2695 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2696 set->flags);
2697 for (i = 0; i < set->n; ++i) {
2698 fprintf(out, "%*s", indent, "");
2699 fprintf(out, "basic set %d:\n", i);
2700 isl_basic_set_print_internal(set->p[i], out, indent+4);
2704 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2706 int i;
2708 if (!map) {
2709 fprintf(out, "null map\n");
2710 return;
2713 fprintf(out, "%*s", indent, "");
2714 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2715 "flags: %x, n_name: %d\n",
2716 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2717 map->dim->n_out, map->flags, map->dim->n_id);
2718 for (i = 0; i < map->n; ++i) {
2719 fprintf(out, "%*s", indent, "");
2720 fprintf(out, "basic map %d:\n", i);
2721 isl_basic_map_print_internal(map->p[i], out, indent+4);
2725 struct isl_basic_map *isl_basic_map_intersect_domain(
2726 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2728 struct isl_basic_map *bmap_domain;
2730 if (!bmap || !bset)
2731 goto error;
2733 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2734 bset->dim, isl_dim_param), goto error);
2736 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2737 isl_assert(bset->ctx,
2738 isl_basic_map_compatible_domain(bmap, bset), goto error);
2740 bmap = isl_basic_map_cow(bmap);
2741 if (!bmap)
2742 goto error;
2743 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2744 bset->n_div, bset->n_eq, bset->n_ineq);
2745 bmap_domain = isl_basic_map_from_domain(bset);
2746 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2748 bmap = isl_basic_map_simplify(bmap);
2749 return isl_basic_map_finalize(bmap);
2750 error:
2751 isl_basic_map_free(bmap);
2752 isl_basic_set_free(bset);
2753 return NULL;
2756 struct isl_basic_map *isl_basic_map_intersect_range(
2757 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2759 struct isl_basic_map *bmap_range;
2761 if (!bmap || !bset)
2762 goto error;
2764 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2765 bset->dim, isl_dim_param), goto error);
2767 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2768 isl_assert(bset->ctx,
2769 isl_basic_map_compatible_range(bmap, bset), goto error);
2771 if (isl_basic_set_is_universe(bset)) {
2772 isl_basic_set_free(bset);
2773 return bmap;
2776 bmap = isl_basic_map_cow(bmap);
2777 if (!bmap)
2778 goto error;
2779 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2780 bset->n_div, bset->n_eq, bset->n_ineq);
2781 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2782 bmap = add_constraints(bmap, bmap_range, 0, 0);
2784 bmap = isl_basic_map_simplify(bmap);
2785 return isl_basic_map_finalize(bmap);
2786 error:
2787 isl_basic_map_free(bmap);
2788 isl_basic_set_free(bset);
2789 return NULL;
2792 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2794 int i;
2795 unsigned total;
2796 isl_int s;
2798 if (!bmap || !vec)
2799 return -1;
2801 total = 1 + isl_basic_map_total_dim(bmap);
2802 if (total != vec->size)
2803 return -1;
2805 isl_int_init(s);
2807 for (i = 0; i < bmap->n_eq; ++i) {
2808 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2809 if (!isl_int_is_zero(s)) {
2810 isl_int_clear(s);
2811 return 0;
2815 for (i = 0; i < bmap->n_ineq; ++i) {
2816 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2817 if (isl_int_is_neg(s)) {
2818 isl_int_clear(s);
2819 return 0;
2823 isl_int_clear(s);
2825 return 1;
2828 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2830 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2833 struct isl_basic_map *isl_basic_map_intersect(
2834 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2836 struct isl_vec *sample = NULL;
2838 if (!bmap1 || !bmap2)
2839 goto error;
2841 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2842 bmap2->dim, isl_dim_param), goto error);
2843 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2844 isl_space_dim(bmap1->dim, isl_dim_param) &&
2845 isl_space_dim(bmap2->dim, isl_dim_all) !=
2846 isl_space_dim(bmap2->dim, isl_dim_param))
2847 return isl_basic_map_intersect(bmap2, bmap1);
2849 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2850 isl_space_dim(bmap2->dim, isl_dim_param))
2851 isl_assert(bmap1->ctx,
2852 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2854 if (bmap1->sample &&
2855 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2856 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2857 sample = isl_vec_copy(bmap1->sample);
2858 else if (bmap2->sample &&
2859 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2860 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2861 sample = isl_vec_copy(bmap2->sample);
2863 bmap1 = isl_basic_map_cow(bmap1);
2864 if (!bmap1)
2865 goto error;
2866 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2867 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2868 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2870 if (!bmap1)
2871 isl_vec_free(sample);
2872 else if (sample) {
2873 isl_vec_free(bmap1->sample);
2874 bmap1->sample = sample;
2877 bmap1 = isl_basic_map_simplify(bmap1);
2878 return isl_basic_map_finalize(bmap1);
2879 error:
2880 if (sample)
2881 isl_vec_free(sample);
2882 isl_basic_map_free(bmap1);
2883 isl_basic_map_free(bmap2);
2884 return NULL;
2887 struct isl_basic_set *isl_basic_set_intersect(
2888 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2890 return (struct isl_basic_set *)
2891 isl_basic_map_intersect(
2892 (struct isl_basic_map *)bset1,
2893 (struct isl_basic_map *)bset2);
2896 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2897 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2899 return isl_basic_set_intersect(bset1, bset2);
2902 /* Special case of isl_map_intersect, where both map1 and map2
2903 * are convex, without any divs and such that either map1 or map2
2904 * contains a single constraint. This constraint is then simply
2905 * added to the other map.
2907 static __isl_give isl_map *map_intersect_add_constraint(
2908 __isl_take isl_map *map1, __isl_take isl_map *map2)
2910 isl_assert(map1->ctx, map1->n == 1, goto error);
2911 isl_assert(map2->ctx, map1->n == 1, goto error);
2912 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2913 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2915 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2916 return isl_map_intersect(map2, map1);
2918 isl_assert(map2->ctx,
2919 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2921 map1 = isl_map_cow(map1);
2922 if (!map1)
2923 goto error;
2924 if (isl_map_plain_is_empty(map1)) {
2925 isl_map_free(map2);
2926 return map1;
2928 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2929 if (map2->p[0]->n_eq == 1)
2930 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2931 else
2932 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2933 map2->p[0]->ineq[0]);
2935 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2936 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2937 if (!map1->p[0])
2938 goto error;
2940 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2941 isl_basic_map_free(map1->p[0]);
2942 map1->n = 0;
2945 isl_map_free(map2);
2947 return map1;
2948 error:
2949 isl_map_free(map1);
2950 isl_map_free(map2);
2951 return NULL;
2954 /* map2 may be either a parameter domain or a map living in the same
2955 * space as map1.
2957 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2958 __isl_take isl_map *map2)
2960 unsigned flags = 0;
2961 isl_map *result;
2962 int i, j;
2964 if (!map1 || !map2)
2965 goto error;
2967 if ((isl_map_plain_is_empty(map1) ||
2968 isl_map_plain_is_universe(map2)) &&
2969 isl_space_is_equal(map1->dim, map2->dim)) {
2970 isl_map_free(map2);
2971 return map1;
2973 if ((isl_map_plain_is_empty(map2) ||
2974 isl_map_plain_is_universe(map1)) &&
2975 isl_space_is_equal(map1->dim, map2->dim)) {
2976 isl_map_free(map1);
2977 return map2;
2980 if (map1->n == 1 && map2->n == 1 &&
2981 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2982 isl_space_is_equal(map1->dim, map2->dim) &&
2983 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2984 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2985 return map_intersect_add_constraint(map1, map2);
2987 if (isl_space_dim(map2->dim, isl_dim_all) !=
2988 isl_space_dim(map2->dim, isl_dim_param))
2989 isl_assert(map1->ctx,
2990 isl_space_is_equal(map1->dim, map2->dim), goto error);
2992 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2993 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2994 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2996 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2997 map1->n * map2->n, flags);
2998 if (!result)
2999 goto error;
3000 for (i = 0; i < map1->n; ++i)
3001 for (j = 0; j < map2->n; ++j) {
3002 struct isl_basic_map *part;
3003 part = isl_basic_map_intersect(
3004 isl_basic_map_copy(map1->p[i]),
3005 isl_basic_map_copy(map2->p[j]));
3006 if (isl_basic_map_is_empty(part) < 0)
3007 part = isl_basic_map_free(part);
3008 result = isl_map_add_basic_map(result, part);
3009 if (!result)
3010 goto error;
3012 isl_map_free(map1);
3013 isl_map_free(map2);
3014 return result;
3015 error:
3016 isl_map_free(map1);
3017 isl_map_free(map2);
3018 return NULL;
3021 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3022 __isl_take isl_map *map2)
3024 if (!map1 || !map2)
3025 goto error;
3026 if (!isl_space_is_equal(map1->dim, map2->dim))
3027 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3028 "spaces don't match", goto error);
3029 return map_intersect_internal(map1, map2);
3030 error:
3031 isl_map_free(map1);
3032 isl_map_free(map2);
3033 return NULL;
3036 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3037 __isl_take isl_map *map2)
3039 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3042 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3044 return (struct isl_set *)
3045 isl_map_intersect((struct isl_map *)set1,
3046 (struct isl_map *)set2);
3049 /* map_intersect_internal accepts intersections
3050 * with parameter domains, so we can just call that function.
3052 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3053 __isl_take isl_set *params)
3055 return map_intersect_internal(map, params);
3058 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3059 __isl_take isl_map *map2)
3061 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3064 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3065 __isl_take isl_set *params)
3067 return isl_map_intersect_params(set, params);
3070 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3072 isl_space *dim;
3073 struct isl_basic_set *bset;
3074 unsigned in;
3076 if (!bmap)
3077 return NULL;
3078 bmap = isl_basic_map_cow(bmap);
3079 if (!bmap)
3080 return NULL;
3081 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3082 in = isl_basic_map_n_in(bmap);
3083 bset = isl_basic_set_from_basic_map(bmap);
3084 bset = isl_basic_set_swap_vars(bset, in);
3085 return isl_basic_map_from_basic_set(bset, dim);
3088 static __isl_give isl_basic_map *basic_map_space_reset(
3089 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3091 isl_space *space;
3093 if (!bmap)
3094 return NULL;
3095 if (!isl_space_is_named_or_nested(bmap->dim, type))
3096 return bmap;
3098 space = isl_basic_map_get_space(bmap);
3099 space = isl_space_reset(space, type);
3100 bmap = isl_basic_map_reset_space(bmap, space);
3101 return bmap;
3104 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3105 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3106 unsigned pos, unsigned n)
3108 isl_space *res_dim;
3109 struct isl_basic_map *res;
3110 struct isl_dim_map *dim_map;
3111 unsigned total, off;
3112 enum isl_dim_type t;
3114 if (n == 0)
3115 return basic_map_space_reset(bmap, type);
3117 if (!bmap)
3118 return NULL;
3120 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3122 total = isl_basic_map_total_dim(bmap) + n;
3123 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3124 off = 0;
3125 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3126 if (t != type) {
3127 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3128 } else {
3129 unsigned size = isl_basic_map_dim(bmap, t);
3130 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3131 0, pos, off);
3132 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3133 pos, size - pos, off + pos + n);
3135 off += isl_space_dim(res_dim, t);
3137 isl_dim_map_div(dim_map, bmap, off);
3139 res = isl_basic_map_alloc_space(res_dim,
3140 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3141 if (isl_basic_map_is_rational(bmap))
3142 res = isl_basic_map_set_rational(res);
3143 if (isl_basic_map_plain_is_empty(bmap)) {
3144 isl_basic_map_free(bmap);
3145 free(dim_map);
3146 return isl_basic_map_set_to_empty(res);
3148 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3149 return isl_basic_map_finalize(res);
3152 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3153 __isl_take isl_basic_set *bset,
3154 enum isl_dim_type type, unsigned pos, unsigned n)
3156 return isl_basic_map_insert_dims(bset, type, pos, n);
3159 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3160 enum isl_dim_type type, unsigned n)
3162 if (!bmap)
3163 return NULL;
3164 return isl_basic_map_insert_dims(bmap, type,
3165 isl_basic_map_dim(bmap, type), n);
3168 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3169 enum isl_dim_type type, unsigned n)
3171 if (!bset)
3172 return NULL;
3173 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3174 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3175 error:
3176 isl_basic_set_free(bset);
3177 return NULL;
3180 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3181 enum isl_dim_type type)
3183 isl_space *space;
3185 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3186 return map;
3188 space = isl_map_get_space(map);
3189 space = isl_space_reset(space, type);
3190 map = isl_map_reset_space(map, space);
3191 return map;
3194 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3195 enum isl_dim_type type, unsigned pos, unsigned n)
3197 int i;
3199 if (n == 0)
3200 return map_space_reset(map, type);
3202 map = isl_map_cow(map);
3203 if (!map)
3204 return NULL;
3206 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3207 if (!map->dim)
3208 goto error;
3210 for (i = 0; i < map->n; ++i) {
3211 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3212 if (!map->p[i])
3213 goto error;
3216 return map;
3217 error:
3218 isl_map_free(map);
3219 return NULL;
3222 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3223 enum isl_dim_type type, unsigned pos, unsigned n)
3225 return isl_map_insert_dims(set, type, pos, n);
3228 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3229 enum isl_dim_type type, unsigned n)
3231 if (!map)
3232 return NULL;
3233 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3236 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3237 enum isl_dim_type type, unsigned n)
3239 if (!set)
3240 return NULL;
3241 isl_assert(set->ctx, type != isl_dim_in, goto error);
3242 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3243 error:
3244 isl_set_free(set);
3245 return NULL;
3248 __isl_give isl_basic_map *isl_basic_map_move_dims(
3249 __isl_take isl_basic_map *bmap,
3250 enum isl_dim_type dst_type, unsigned dst_pos,
3251 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3253 struct isl_dim_map *dim_map;
3254 struct isl_basic_map *res;
3255 enum isl_dim_type t;
3256 unsigned total, off;
3258 if (!bmap)
3259 return NULL;
3260 if (n == 0)
3261 return bmap;
3263 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3264 goto error);
3266 if (dst_type == src_type && dst_pos == src_pos)
3267 return bmap;
3269 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3271 if (pos(bmap->dim, dst_type) + dst_pos ==
3272 pos(bmap->dim, src_type) + src_pos +
3273 ((src_type < dst_type) ? n : 0)) {
3274 bmap = isl_basic_map_cow(bmap);
3275 if (!bmap)
3276 return NULL;
3278 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3279 src_type, src_pos, n);
3280 if (!bmap->dim)
3281 goto error;
3283 bmap = isl_basic_map_finalize(bmap);
3285 return bmap;
3288 total = isl_basic_map_total_dim(bmap);
3289 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3291 off = 0;
3292 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3293 unsigned size = isl_space_dim(bmap->dim, t);
3294 if (t == dst_type) {
3295 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3296 0, dst_pos, off);
3297 off += dst_pos;
3298 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3299 src_pos, n, off);
3300 off += n;
3301 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3302 dst_pos, size - dst_pos, off);
3303 off += size - dst_pos;
3304 } else if (t == src_type) {
3305 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3306 0, src_pos, off);
3307 off += src_pos;
3308 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3309 src_pos + n, size - src_pos - n, off);
3310 off += size - src_pos - n;
3311 } else {
3312 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3313 off += size;
3316 isl_dim_map_div(dim_map, bmap, off);
3318 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3319 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3320 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3321 if (!bmap)
3322 goto error;
3324 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3325 src_type, src_pos, n);
3326 if (!bmap->dim)
3327 goto error;
3329 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3330 bmap = isl_basic_map_gauss(bmap, NULL);
3331 bmap = isl_basic_map_finalize(bmap);
3333 return bmap;
3334 error:
3335 isl_basic_map_free(bmap);
3336 return NULL;
3339 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3340 enum isl_dim_type dst_type, unsigned dst_pos,
3341 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3343 return (isl_basic_set *)isl_basic_map_move_dims(
3344 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3347 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3348 enum isl_dim_type dst_type, unsigned dst_pos,
3349 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3351 if (!set)
3352 return NULL;
3353 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3354 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3355 src_type, src_pos, n);
3356 error:
3357 isl_set_free(set);
3358 return NULL;
3361 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3362 enum isl_dim_type dst_type, unsigned dst_pos,
3363 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3365 int i;
3367 if (!map)
3368 return NULL;
3369 if (n == 0)
3370 return map;
3372 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3373 goto error);
3375 if (dst_type == src_type && dst_pos == src_pos)
3376 return map;
3378 isl_assert(map->ctx, dst_type != src_type, goto error);
3380 map = isl_map_cow(map);
3381 if (!map)
3382 return NULL;
3384 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3385 if (!map->dim)
3386 goto error;
3388 for (i = 0; i < map->n; ++i) {
3389 map->p[i] = isl_basic_map_move_dims(map->p[i],
3390 dst_type, dst_pos,
3391 src_type, src_pos, n);
3392 if (!map->p[i])
3393 goto error;
3396 return map;
3397 error:
3398 isl_map_free(map);
3399 return NULL;
3402 /* Move the specified dimensions to the last columns right before
3403 * the divs. Don't change the dimension specification of bmap.
3404 * That's the responsibility of the caller.
3406 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3407 enum isl_dim_type type, unsigned first, unsigned n)
3409 struct isl_dim_map *dim_map;
3410 struct isl_basic_map *res;
3411 enum isl_dim_type t;
3412 unsigned total, off;
3414 if (!bmap)
3415 return NULL;
3416 if (pos(bmap->dim, type) + first + n ==
3417 1 + isl_space_dim(bmap->dim, isl_dim_all))
3418 return bmap;
3420 total = isl_basic_map_total_dim(bmap);
3421 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3423 off = 0;
3424 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3425 unsigned size = isl_space_dim(bmap->dim, t);
3426 if (t == type) {
3427 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3428 0, first, off);
3429 off += first;
3430 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3431 first, n, total - bmap->n_div - n);
3432 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3433 first + n, size - (first + n), off);
3434 off += size - (first + n);
3435 } else {
3436 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3437 off += size;
3440 isl_dim_map_div(dim_map, bmap, off + n);
3442 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3443 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3444 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3445 return res;
3448 /* Insert "n" rows in the divs of "bmap".
3450 * The number of columns is not changed, which means that the last
3451 * dimensions of "bmap" are being reintepreted as the new divs.
3452 * The space of "bmap" is not adjusted, however, which means
3453 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3454 * from the space of "bmap" is the responsibility of the caller.
3456 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3457 int n)
3459 int i;
3460 size_t row_size;
3461 isl_int **new_div;
3462 isl_int *old;
3464 bmap = isl_basic_map_cow(bmap);
3465 if (!bmap)
3466 return NULL;
3468 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3469 old = bmap->block2.data;
3470 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3471 (bmap->extra + n) * (1 + row_size));
3472 if (!bmap->block2.data)
3473 return isl_basic_map_free(bmap);
3474 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3475 if (!new_div)
3476 return isl_basic_map_free(bmap);
3477 for (i = 0; i < n; ++i) {
3478 new_div[i] = bmap->block2.data +
3479 (bmap->extra + i) * (1 + row_size);
3480 isl_seq_clr(new_div[i], 1 + row_size);
3482 for (i = 0; i < bmap->extra; ++i)
3483 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3484 free(bmap->div);
3485 bmap->div = new_div;
3486 bmap->n_div += n;
3487 bmap->extra += n;
3489 return bmap;
3492 /* Turn the n dimensions of type type, starting at first
3493 * into existentially quantified variables.
3495 __isl_give isl_basic_map *isl_basic_map_project_out(
3496 __isl_take isl_basic_map *bmap,
3497 enum isl_dim_type type, unsigned first, unsigned n)
3499 if (n == 0)
3500 return basic_map_space_reset(bmap, type);
3502 if (!bmap)
3503 return NULL;
3505 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3506 return isl_basic_map_remove_dims(bmap, type, first, n);
3508 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3509 goto error);
3511 bmap = move_last(bmap, type, first, n);
3512 bmap = isl_basic_map_cow(bmap);
3513 bmap = insert_div_rows(bmap, n);
3514 if (!bmap)
3515 return NULL;
3517 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3518 if (!bmap->dim)
3519 goto error;
3520 bmap = isl_basic_map_simplify(bmap);
3521 bmap = isl_basic_map_drop_redundant_divs(bmap);
3522 return isl_basic_map_finalize(bmap);
3523 error:
3524 isl_basic_map_free(bmap);
3525 return NULL;
3528 /* Turn the n dimensions of type type, starting at first
3529 * into existentially quantified variables.
3531 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3532 enum isl_dim_type type, unsigned first, unsigned n)
3534 return (isl_basic_set *)isl_basic_map_project_out(
3535 (isl_basic_map *)bset, type, first, n);
3538 /* Turn the n dimensions of type type, starting at first
3539 * into existentially quantified variables.
3541 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3542 enum isl_dim_type type, unsigned first, unsigned n)
3544 int i;
3546 if (!map)
3547 return NULL;
3549 if (n == 0)
3550 return map_space_reset(map, type);
3552 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3554 map = isl_map_cow(map);
3555 if (!map)
3556 return NULL;
3558 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3559 if (!map->dim)
3560 goto error;
3562 for (i = 0; i < map->n; ++i) {
3563 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3564 if (!map->p[i])
3565 goto error;
3568 return map;
3569 error:
3570 isl_map_free(map);
3571 return NULL;
3574 /* Turn the n dimensions of type type, starting at first
3575 * into existentially quantified variables.
3577 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3578 enum isl_dim_type type, unsigned first, unsigned n)
3580 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3583 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3585 int i, j;
3587 for (i = 0; i < n; ++i) {
3588 j = isl_basic_map_alloc_div(bmap);
3589 if (j < 0)
3590 goto error;
3591 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3593 return bmap;
3594 error:
3595 isl_basic_map_free(bmap);
3596 return NULL;
3599 struct isl_basic_map *isl_basic_map_apply_range(
3600 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3602 isl_space *dim_result = NULL;
3603 struct isl_basic_map *bmap;
3604 unsigned n_in, n_out, n, nparam, total, pos;
3605 struct isl_dim_map *dim_map1, *dim_map2;
3607 if (!bmap1 || !bmap2)
3608 goto error;
3609 if (!isl_space_match(bmap1->dim, isl_dim_param,
3610 bmap2->dim, isl_dim_param))
3611 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3612 "parameters don't match", goto error);
3613 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3614 bmap2->dim, isl_dim_in))
3615 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3616 "spaces don't match", goto error);
3618 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3619 isl_space_copy(bmap2->dim));
3621 n_in = isl_basic_map_n_in(bmap1);
3622 n_out = isl_basic_map_n_out(bmap2);
3623 n = isl_basic_map_n_out(bmap1);
3624 nparam = isl_basic_map_n_param(bmap1);
3626 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3627 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3628 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3629 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3630 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3631 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3632 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3633 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3634 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3635 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3636 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3638 bmap = isl_basic_map_alloc_space(dim_result,
3639 bmap1->n_div + bmap2->n_div + n,
3640 bmap1->n_eq + bmap2->n_eq,
3641 bmap1->n_ineq + bmap2->n_ineq);
3642 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3643 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3644 bmap = add_divs(bmap, n);
3645 bmap = isl_basic_map_simplify(bmap);
3646 bmap = isl_basic_map_drop_redundant_divs(bmap);
3647 return isl_basic_map_finalize(bmap);
3648 error:
3649 isl_basic_map_free(bmap1);
3650 isl_basic_map_free(bmap2);
3651 return NULL;
3654 struct isl_basic_set *isl_basic_set_apply(
3655 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3657 if (!bset || !bmap)
3658 goto error;
3660 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3661 goto error);
3663 return (struct isl_basic_set *)
3664 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3665 error:
3666 isl_basic_set_free(bset);
3667 isl_basic_map_free(bmap);
3668 return NULL;
3671 struct isl_basic_map *isl_basic_map_apply_domain(
3672 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3674 if (!bmap1 || !bmap2)
3675 goto error;
3677 isl_assert(bmap1->ctx,
3678 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3679 isl_assert(bmap1->ctx,
3680 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3681 goto error);
3683 bmap1 = isl_basic_map_reverse(bmap1);
3684 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3685 return isl_basic_map_reverse(bmap1);
3686 error:
3687 isl_basic_map_free(bmap1);
3688 isl_basic_map_free(bmap2);
3689 return NULL;
3692 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3693 * A \cap B -> f(A) + f(B)
3695 struct isl_basic_map *isl_basic_map_sum(
3696 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3698 unsigned n_in, n_out, nparam, total, pos;
3699 struct isl_basic_map *bmap = NULL;
3700 struct isl_dim_map *dim_map1, *dim_map2;
3701 int i;
3703 if (!bmap1 || !bmap2)
3704 goto error;
3706 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3707 goto error);
3709 nparam = isl_basic_map_n_param(bmap1);
3710 n_in = isl_basic_map_n_in(bmap1);
3711 n_out = isl_basic_map_n_out(bmap1);
3713 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3714 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3715 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3716 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3717 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3718 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3719 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3720 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3721 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3722 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3723 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3725 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3726 bmap1->n_div + bmap2->n_div + 2 * n_out,
3727 bmap1->n_eq + bmap2->n_eq + n_out,
3728 bmap1->n_ineq + bmap2->n_ineq);
3729 for (i = 0; i < n_out; ++i) {
3730 int j = isl_basic_map_alloc_equality(bmap);
3731 if (j < 0)
3732 goto error;
3733 isl_seq_clr(bmap->eq[j], 1+total);
3734 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3735 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3736 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3738 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3739 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3740 bmap = add_divs(bmap, 2 * n_out);
3742 bmap = isl_basic_map_simplify(bmap);
3743 return isl_basic_map_finalize(bmap);
3744 error:
3745 isl_basic_map_free(bmap);
3746 isl_basic_map_free(bmap1);
3747 isl_basic_map_free(bmap2);
3748 return NULL;
3751 /* Given two maps A -> f(A) and B -> g(B), construct a map
3752 * A \cap B -> f(A) + f(B)
3754 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3756 struct isl_map *result;
3757 int i, j;
3759 if (!map1 || !map2)
3760 goto error;
3762 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3764 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3765 map1->n * map2->n, 0);
3766 if (!result)
3767 goto error;
3768 for (i = 0; i < map1->n; ++i)
3769 for (j = 0; j < map2->n; ++j) {
3770 struct isl_basic_map *part;
3771 part = isl_basic_map_sum(
3772 isl_basic_map_copy(map1->p[i]),
3773 isl_basic_map_copy(map2->p[j]));
3774 if (isl_basic_map_is_empty(part))
3775 isl_basic_map_free(part);
3776 else
3777 result = isl_map_add_basic_map(result, part);
3778 if (!result)
3779 goto error;
3781 isl_map_free(map1);
3782 isl_map_free(map2);
3783 return result;
3784 error:
3785 isl_map_free(map1);
3786 isl_map_free(map2);
3787 return NULL;
3790 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3791 __isl_take isl_set *set2)
3793 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3796 /* Given a basic map A -> f(A), construct A -> -f(A).
3798 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3800 int i, j;
3801 unsigned off, n;
3803 bmap = isl_basic_map_cow(bmap);
3804 if (!bmap)
3805 return NULL;
3807 n = isl_basic_map_dim(bmap, isl_dim_out);
3808 off = isl_basic_map_offset(bmap, isl_dim_out);
3809 for (i = 0; i < bmap->n_eq; ++i)
3810 for (j = 0; j < n; ++j)
3811 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3812 for (i = 0; i < bmap->n_ineq; ++i)
3813 for (j = 0; j < n; ++j)
3814 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3815 for (i = 0; i < bmap->n_div; ++i)
3816 for (j = 0; j < n; ++j)
3817 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3818 bmap = isl_basic_map_gauss(bmap, NULL);
3819 return isl_basic_map_finalize(bmap);
3822 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3824 return isl_basic_map_neg(bset);
3827 /* Given a map A -> f(A), construct A -> -f(A).
3829 struct isl_map *isl_map_neg(struct isl_map *map)
3831 int i;
3833 map = isl_map_cow(map);
3834 if (!map)
3835 return NULL;
3837 for (i = 0; i < map->n; ++i) {
3838 map->p[i] = isl_basic_map_neg(map->p[i]);
3839 if (!map->p[i])
3840 goto error;
3843 return map;
3844 error:
3845 isl_map_free(map);
3846 return NULL;
3849 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3851 return (isl_set *)isl_map_neg((isl_map *)set);
3854 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3855 * A -> floor(f(A)/d).
3857 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3858 isl_int d)
3860 unsigned n_in, n_out, nparam, total, pos;
3861 struct isl_basic_map *result = NULL;
3862 struct isl_dim_map *dim_map;
3863 int i;
3865 if (!bmap)
3866 return NULL;
3868 nparam = isl_basic_map_n_param(bmap);
3869 n_in = isl_basic_map_n_in(bmap);
3870 n_out = isl_basic_map_n_out(bmap);
3872 total = nparam + n_in + n_out + bmap->n_div + n_out;
3873 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3874 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3875 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3876 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3877 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3879 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3880 bmap->n_div + n_out,
3881 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3882 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3883 result = add_divs(result, n_out);
3884 for (i = 0; i < n_out; ++i) {
3885 int j;
3886 j = isl_basic_map_alloc_inequality(result);
3887 if (j < 0)
3888 goto error;
3889 isl_seq_clr(result->ineq[j], 1+total);
3890 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3891 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3892 j = isl_basic_map_alloc_inequality(result);
3893 if (j < 0)
3894 goto error;
3895 isl_seq_clr(result->ineq[j], 1+total);
3896 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3897 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3898 isl_int_sub_ui(result->ineq[j][0], d, 1);
3901 result = isl_basic_map_simplify(result);
3902 return isl_basic_map_finalize(result);
3903 error:
3904 isl_basic_map_free(result);
3905 return NULL;
3908 /* Given a map A -> f(A) and an integer d, construct a map
3909 * A -> floor(f(A)/d).
3911 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3913 int i;
3915 map = isl_map_cow(map);
3916 if (!map)
3917 return NULL;
3919 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3920 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3921 for (i = 0; i < map->n; ++i) {
3922 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3923 if (!map->p[i])
3924 goto error;
3927 return map;
3928 error:
3929 isl_map_free(map);
3930 return NULL;
3933 /* Given a map A -> f(A) and an integer d, construct a map
3934 * A -> floor(f(A)/d).
3936 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3937 __isl_take isl_val *d)
3939 if (!map || !d)
3940 goto error;
3941 if (!isl_val_is_int(d))
3942 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3943 "expecting integer denominator", goto error);
3944 map = isl_map_floordiv(map, d->n);
3945 isl_val_free(d);
3946 return map;
3947 error:
3948 isl_map_free(map);
3949 isl_val_free(d);
3950 return NULL;
3953 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3955 int i;
3956 unsigned nparam;
3957 unsigned n_in;
3959 i = isl_basic_map_alloc_equality(bmap);
3960 if (i < 0)
3961 goto error;
3962 nparam = isl_basic_map_n_param(bmap);
3963 n_in = isl_basic_map_n_in(bmap);
3964 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3965 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3966 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3967 return isl_basic_map_finalize(bmap);
3968 error:
3969 isl_basic_map_free(bmap);
3970 return NULL;
3973 /* Add a constraints to "bmap" expressing i_pos < o_pos
3975 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3977 int i;
3978 unsigned nparam;
3979 unsigned n_in;
3981 i = isl_basic_map_alloc_inequality(bmap);
3982 if (i < 0)
3983 goto error;
3984 nparam = isl_basic_map_n_param(bmap);
3985 n_in = isl_basic_map_n_in(bmap);
3986 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3987 isl_int_set_si(bmap->ineq[i][0], -1);
3988 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3989 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3990 return isl_basic_map_finalize(bmap);
3991 error:
3992 isl_basic_map_free(bmap);
3993 return NULL;
3996 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3998 static __isl_give isl_basic_map *var_less_or_equal(
3999 __isl_take isl_basic_map *bmap, unsigned pos)
4001 int i;
4002 unsigned nparam;
4003 unsigned n_in;
4005 i = isl_basic_map_alloc_inequality(bmap);
4006 if (i < 0)
4007 goto error;
4008 nparam = isl_basic_map_n_param(bmap);
4009 n_in = isl_basic_map_n_in(bmap);
4010 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4011 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4012 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4013 return isl_basic_map_finalize(bmap);
4014 error:
4015 isl_basic_map_free(bmap);
4016 return NULL;
4019 /* Add a constraints to "bmap" expressing i_pos > o_pos
4021 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4023 int i;
4024 unsigned nparam;
4025 unsigned n_in;
4027 i = isl_basic_map_alloc_inequality(bmap);
4028 if (i < 0)
4029 goto error;
4030 nparam = isl_basic_map_n_param(bmap);
4031 n_in = isl_basic_map_n_in(bmap);
4032 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4033 isl_int_set_si(bmap->ineq[i][0], -1);
4034 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4035 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4036 return isl_basic_map_finalize(bmap);
4037 error:
4038 isl_basic_map_free(bmap);
4039 return NULL;
4042 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4044 static __isl_give isl_basic_map *var_more_or_equal(
4045 __isl_take isl_basic_map *bmap, unsigned pos)
4047 int i;
4048 unsigned nparam;
4049 unsigned n_in;
4051 i = isl_basic_map_alloc_inequality(bmap);
4052 if (i < 0)
4053 goto error;
4054 nparam = isl_basic_map_n_param(bmap);
4055 n_in = isl_basic_map_n_in(bmap);
4056 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4057 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4058 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4059 return isl_basic_map_finalize(bmap);
4060 error:
4061 isl_basic_map_free(bmap);
4062 return NULL;
4065 __isl_give isl_basic_map *isl_basic_map_equal(
4066 __isl_take isl_space *dim, unsigned n_equal)
4068 int i;
4069 struct isl_basic_map *bmap;
4070 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4071 if (!bmap)
4072 return NULL;
4073 for (i = 0; i < n_equal && bmap; ++i)
4074 bmap = var_equal(bmap, i);
4075 return isl_basic_map_finalize(bmap);
4078 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4080 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4081 unsigned pos)
4083 int i;
4084 struct isl_basic_map *bmap;
4085 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4086 if (!bmap)
4087 return NULL;
4088 for (i = 0; i < pos && bmap; ++i)
4089 bmap = var_equal(bmap, i);
4090 if (bmap)
4091 bmap = var_less(bmap, pos);
4092 return isl_basic_map_finalize(bmap);
4095 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4097 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4098 __isl_take isl_space *dim, unsigned pos)
4100 int i;
4101 isl_basic_map *bmap;
4103 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4104 for (i = 0; i < pos; ++i)
4105 bmap = var_equal(bmap, i);
4106 bmap = var_less_or_equal(bmap, pos);
4107 return isl_basic_map_finalize(bmap);
4110 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4112 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4113 unsigned pos)
4115 int i;
4116 struct isl_basic_map *bmap;
4117 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4118 if (!bmap)
4119 return NULL;
4120 for (i = 0; i < pos && bmap; ++i)
4121 bmap = var_equal(bmap, i);
4122 if (bmap)
4123 bmap = var_more(bmap, pos);
4124 return isl_basic_map_finalize(bmap);
4127 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4129 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4130 __isl_take isl_space *dim, unsigned pos)
4132 int i;
4133 isl_basic_map *bmap;
4135 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4136 for (i = 0; i < pos; ++i)
4137 bmap = var_equal(bmap, i);
4138 bmap = var_more_or_equal(bmap, pos);
4139 return isl_basic_map_finalize(bmap);
4142 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4143 unsigned n, int equal)
4145 struct isl_map *map;
4146 int i;
4148 if (n == 0 && equal)
4149 return isl_map_universe(dims);
4151 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4153 for (i = 0; i + 1 < n; ++i)
4154 map = isl_map_add_basic_map(map,
4155 isl_basic_map_less_at(isl_space_copy(dims), i));
4156 if (n > 0) {
4157 if (equal)
4158 map = isl_map_add_basic_map(map,
4159 isl_basic_map_less_or_equal_at(dims, n - 1));
4160 else
4161 map = isl_map_add_basic_map(map,
4162 isl_basic_map_less_at(dims, n - 1));
4163 } else
4164 isl_space_free(dims);
4166 return map;
4169 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4171 if (!dims)
4172 return NULL;
4173 return map_lex_lte_first(dims, dims->n_out, equal);
4176 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4178 return map_lex_lte_first(dim, n, 0);
4181 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4183 return map_lex_lte_first(dim, n, 1);
4186 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4188 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4191 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4193 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4196 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4197 unsigned n, int equal)
4199 struct isl_map *map;
4200 int i;
4202 if (n == 0 && equal)
4203 return isl_map_universe(dims);
4205 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4207 for (i = 0; i + 1 < n; ++i)
4208 map = isl_map_add_basic_map(map,
4209 isl_basic_map_more_at(isl_space_copy(dims), i));
4210 if (n > 0) {
4211 if (equal)
4212 map = isl_map_add_basic_map(map,
4213 isl_basic_map_more_or_equal_at(dims, n - 1));
4214 else
4215 map = isl_map_add_basic_map(map,
4216 isl_basic_map_more_at(dims, n - 1));
4217 } else
4218 isl_space_free(dims);
4220 return map;
4223 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4225 if (!dims)
4226 return NULL;
4227 return map_lex_gte_first(dims, dims->n_out, equal);
4230 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4232 return map_lex_gte_first(dim, n, 0);
4235 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4237 return map_lex_gte_first(dim, n, 1);
4240 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4242 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4245 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4247 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4250 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4251 __isl_take isl_set *set2)
4253 isl_map *map;
4254 map = isl_map_lex_le(isl_set_get_space(set1));
4255 map = isl_map_intersect_domain(map, set1);
4256 map = isl_map_intersect_range(map, set2);
4257 return map;
4260 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4261 __isl_take isl_set *set2)
4263 isl_map *map;
4264 map = isl_map_lex_lt(isl_set_get_space(set1));
4265 map = isl_map_intersect_domain(map, set1);
4266 map = isl_map_intersect_range(map, set2);
4267 return map;
4270 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4271 __isl_take isl_set *set2)
4273 isl_map *map;
4274 map = isl_map_lex_ge(isl_set_get_space(set1));
4275 map = isl_map_intersect_domain(map, set1);
4276 map = isl_map_intersect_range(map, set2);
4277 return map;
4280 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4281 __isl_take isl_set *set2)
4283 isl_map *map;
4284 map = isl_map_lex_gt(isl_set_get_space(set1));
4285 map = isl_map_intersect_domain(map, set1);
4286 map = isl_map_intersect_range(map, set2);
4287 return map;
4290 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4291 __isl_take isl_map *map2)
4293 isl_map *map;
4294 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4295 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4296 map = isl_map_apply_range(map, isl_map_reverse(map2));
4297 return map;
4300 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4301 __isl_take isl_map *map2)
4303 isl_map *map;
4304 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4305 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4306 map = isl_map_apply_range(map, isl_map_reverse(map2));
4307 return map;
4310 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4311 __isl_take isl_map *map2)
4313 isl_map *map;
4314 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4315 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4316 map = isl_map_apply_range(map, isl_map_reverse(map2));
4317 return map;
4320 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4321 __isl_take isl_map *map2)
4323 isl_map *map;
4324 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4325 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4326 map = isl_map_apply_range(map, isl_map_reverse(map2));
4327 return map;
4330 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4331 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4333 struct isl_basic_map *bmap;
4335 bset = isl_basic_set_cow(bset);
4336 if (!bset || !dim)
4337 goto error;
4339 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4340 isl_space_free(bset->dim);
4341 bmap = (struct isl_basic_map *) bset;
4342 bmap->dim = dim;
4343 return isl_basic_map_finalize(bmap);
4344 error:
4345 isl_basic_set_free(bset);
4346 isl_space_free(dim);
4347 return NULL;
4350 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4352 if (!bmap)
4353 goto error;
4354 if (bmap->dim->n_in == 0)
4355 return (struct isl_basic_set *)bmap;
4356 bmap = isl_basic_map_cow(bmap);
4357 if (!bmap)
4358 goto error;
4359 bmap->dim = isl_space_as_set_space(bmap->dim);
4360 if (!bmap->dim)
4361 goto error;
4362 bmap = isl_basic_map_finalize(bmap);
4363 return (struct isl_basic_set *)bmap;
4364 error:
4365 isl_basic_map_free(bmap);
4366 return NULL;
4369 /* For a div d = floor(f/m), add the constraint
4371 * f - m d >= 0
4373 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4374 unsigned pos, isl_int *div)
4376 int i;
4377 unsigned total = isl_basic_map_total_dim(bmap);
4379 i = isl_basic_map_alloc_inequality(bmap);
4380 if (i < 0)
4381 return -1;
4382 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4383 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4385 return 0;
4388 /* For a div d = floor(f/m), add the constraint
4390 * -(f-(n-1)) + m d >= 0
4392 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4393 unsigned pos, isl_int *div)
4395 int i;
4396 unsigned total = isl_basic_map_total_dim(bmap);
4398 i = isl_basic_map_alloc_inequality(bmap);
4399 if (i < 0)
4400 return -1;
4401 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4402 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4403 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4404 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4406 return 0;
4409 /* For a div d = floor(f/m), add the constraints
4411 * f - m d >= 0
4412 * -(f-(n-1)) + m d >= 0
4414 * Note that the second constraint is the negation of
4416 * f - m d >= n
4418 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4419 unsigned pos, isl_int *div)
4421 if (add_upper_div_constraint(bmap, pos, div) < 0)
4422 return -1;
4423 if (add_lower_div_constraint(bmap, pos, div) < 0)
4424 return -1;
4425 return 0;
4428 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4429 unsigned pos, isl_int *div)
4431 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4432 pos, div);
4435 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4437 unsigned total = isl_basic_map_total_dim(bmap);
4438 unsigned div_pos = total - bmap->n_div + div;
4440 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4441 bmap->div[div]);
4444 /* For each known div d = floor(f/m), add the constraints
4446 * f - m d >= 0
4447 * -(f-(n-1)) + m d >= 0
4449 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4450 __isl_take isl_basic_map *bmap)
4452 int i;
4453 unsigned n_div;
4455 if (!bmap)
4456 return NULL;
4457 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4458 if (n_div == 0)
4459 return bmap;
4460 bmap = isl_basic_map_cow(bmap);
4461 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
4462 if (!bmap)
4463 return NULL;
4464 for (i = 0; i < n_div; ++i) {
4465 if (isl_int_is_zero(bmap->div[i][0]))
4466 continue;
4467 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4468 return isl_basic_map_free(bmap);
4471 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4472 bmap = isl_basic_map_finalize(bmap);
4473 return bmap;
4476 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4478 * In particular, if this div is of the form d = floor(f/m),
4479 * then add the constraint
4481 * f - m d >= 0
4483 * if sign < 0 or the constraint
4485 * -(f-(n-1)) + m d >= 0
4487 * if sign > 0.
4489 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4490 unsigned div, int sign)
4492 unsigned total;
4493 unsigned div_pos;
4495 if (!bmap)
4496 return -1;
4498 total = isl_basic_map_total_dim(bmap);
4499 div_pos = total - bmap->n_div + div;
4501 if (sign < 0)
4502 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4503 else
4504 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4507 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4509 return isl_basic_map_add_div_constraints(bset, div);
4512 struct isl_basic_set *isl_basic_map_underlying_set(
4513 struct isl_basic_map *bmap)
4515 if (!bmap)
4516 goto error;
4517 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4518 bmap->n_div == 0 &&
4519 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4520 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4521 return (struct isl_basic_set *)bmap;
4522 bmap = isl_basic_map_cow(bmap);
4523 if (!bmap)
4524 goto error;
4525 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4526 if (!bmap->dim)
4527 goto error;
4528 bmap->extra -= bmap->n_div;
4529 bmap->n_div = 0;
4530 bmap = isl_basic_map_finalize(bmap);
4531 return (struct isl_basic_set *)bmap;
4532 error:
4533 isl_basic_map_free(bmap);
4534 return NULL;
4537 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4538 __isl_take isl_basic_set *bset)
4540 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4543 /* Replace each element in "list" by the result of applying
4544 * isl_basic_set_underlying_set to the element.
4546 __isl_give isl_basic_set_list *isl_basic_set_list_underlying_set(
4547 __isl_take isl_basic_set_list *list)
4549 int i, n;
4551 if (!list)
4552 return NULL;
4554 n = isl_basic_set_list_n_basic_set(list);
4555 for (i = 0; i < n; ++i) {
4556 isl_basic_set *bset;
4558 bset = isl_basic_set_list_get_basic_set(list, i);
4559 bset = isl_basic_set_underlying_set(bset);
4560 list = isl_basic_set_list_set_basic_set(list, i, bset);
4563 return list;
4566 struct isl_basic_map *isl_basic_map_overlying_set(
4567 struct isl_basic_set *bset, struct isl_basic_map *like)
4569 struct isl_basic_map *bmap;
4570 struct isl_ctx *ctx;
4571 unsigned total;
4572 int i;
4574 if (!bset || !like)
4575 goto error;
4576 ctx = bset->ctx;
4577 isl_assert(ctx, bset->n_div == 0, goto error);
4578 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4579 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4580 goto error);
4581 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4582 isl_basic_map_free(like);
4583 return (struct isl_basic_map *)bset;
4585 bset = isl_basic_set_cow(bset);
4586 if (!bset)
4587 goto error;
4588 total = bset->dim->n_out + bset->extra;
4589 bmap = (struct isl_basic_map *)bset;
4590 isl_space_free(bmap->dim);
4591 bmap->dim = isl_space_copy(like->dim);
4592 if (!bmap->dim)
4593 goto error;
4594 bmap->n_div = like->n_div;
4595 bmap->extra += like->n_div;
4596 if (bmap->extra) {
4597 unsigned ltotal;
4598 isl_int **div;
4599 ltotal = total - bmap->extra + like->extra;
4600 if (ltotal > total)
4601 ltotal = total;
4602 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4603 bmap->extra * (1 + 1 + total));
4604 if (isl_blk_is_error(bmap->block2))
4605 goto error;
4606 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4607 if (!div)
4608 goto error;
4609 bmap->div = div;
4610 for (i = 0; i < bmap->extra; ++i)
4611 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4612 for (i = 0; i < like->n_div; ++i) {
4613 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4614 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4616 bmap = isl_basic_map_add_known_div_constraints(bmap);
4618 isl_basic_map_free(like);
4619 bmap = isl_basic_map_simplify(bmap);
4620 bmap = isl_basic_map_finalize(bmap);
4621 return bmap;
4622 error:
4623 isl_basic_map_free(like);
4624 isl_basic_set_free(bset);
4625 return NULL;
4628 struct isl_basic_set *isl_basic_set_from_underlying_set(
4629 struct isl_basic_set *bset, struct isl_basic_set *like)
4631 return (struct isl_basic_set *)
4632 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4635 struct isl_set *isl_set_from_underlying_set(
4636 struct isl_set *set, struct isl_basic_set *like)
4638 int i;
4640 if (!set || !like)
4641 goto error;
4642 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4643 goto error);
4644 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4645 isl_basic_set_free(like);
4646 return set;
4648 set = isl_set_cow(set);
4649 if (!set)
4650 goto error;
4651 for (i = 0; i < set->n; ++i) {
4652 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4653 isl_basic_set_copy(like));
4654 if (!set->p[i])
4655 goto error;
4657 isl_space_free(set->dim);
4658 set->dim = isl_space_copy(like->dim);
4659 if (!set->dim)
4660 goto error;
4661 isl_basic_set_free(like);
4662 return set;
4663 error:
4664 isl_basic_set_free(like);
4665 isl_set_free(set);
4666 return NULL;
4669 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4671 int i;
4673 map = isl_map_cow(map);
4674 if (!map)
4675 return NULL;
4676 map->dim = isl_space_cow(map->dim);
4677 if (!map->dim)
4678 goto error;
4680 for (i = 1; i < map->n; ++i)
4681 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4682 goto error);
4683 for (i = 0; i < map->n; ++i) {
4684 map->p[i] = (struct isl_basic_map *)
4685 isl_basic_map_underlying_set(map->p[i]);
4686 if (!map->p[i])
4687 goto error;
4689 if (map->n == 0)
4690 map->dim = isl_space_underlying(map->dim, 0);
4691 else {
4692 isl_space_free(map->dim);
4693 map->dim = isl_space_copy(map->p[0]->dim);
4695 if (!map->dim)
4696 goto error;
4697 return (struct isl_set *)map;
4698 error:
4699 isl_map_free(map);
4700 return NULL;
4703 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4705 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4708 __isl_give isl_basic_map *isl_basic_map_reset_space(
4709 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4711 bmap = isl_basic_map_cow(bmap);
4712 if (!bmap || !dim)
4713 goto error;
4715 isl_space_free(bmap->dim);
4716 bmap->dim = dim;
4718 bmap = isl_basic_map_finalize(bmap);
4720 return bmap;
4721 error:
4722 isl_basic_map_free(bmap);
4723 isl_space_free(dim);
4724 return NULL;
4727 __isl_give isl_basic_set *isl_basic_set_reset_space(
4728 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4730 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4731 dim);
4734 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4735 __isl_take isl_space *dim)
4737 int i;
4739 map = isl_map_cow(map);
4740 if (!map || !dim)
4741 goto error;
4743 for (i = 0; i < map->n; ++i) {
4744 map->p[i] = isl_basic_map_reset_space(map->p[i],
4745 isl_space_copy(dim));
4746 if (!map->p[i])
4747 goto error;
4749 isl_space_free(map->dim);
4750 map->dim = dim;
4752 return map;
4753 error:
4754 isl_map_free(map);
4755 isl_space_free(dim);
4756 return NULL;
4759 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4760 __isl_take isl_space *dim)
4762 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4765 /* Compute the parameter domain of the given basic set.
4767 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4769 isl_space *space;
4770 unsigned n;
4772 if (isl_basic_set_is_params(bset))
4773 return bset;
4775 n = isl_basic_set_dim(bset, isl_dim_set);
4776 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4777 space = isl_basic_set_get_space(bset);
4778 space = isl_space_params(space);
4779 bset = isl_basic_set_reset_space(bset, space);
4780 return bset;
4783 /* Construct a zero-dimensional basic set with the given parameter domain.
4785 __isl_give isl_basic_set *isl_basic_set_from_params(
4786 __isl_take isl_basic_set *bset)
4788 isl_space *space;
4789 space = isl_basic_set_get_space(bset);
4790 space = isl_space_set_from_params(space);
4791 bset = isl_basic_set_reset_space(bset, space);
4792 return bset;
4795 /* Compute the parameter domain of the given set.
4797 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4799 isl_space *space;
4800 unsigned n;
4802 if (isl_set_is_params(set))
4803 return set;
4805 n = isl_set_dim(set, isl_dim_set);
4806 set = isl_set_project_out(set, isl_dim_set, 0, n);
4807 space = isl_set_get_space(set);
4808 space = isl_space_params(space);
4809 set = isl_set_reset_space(set, space);
4810 return set;
4813 /* Construct a zero-dimensional set with the given parameter domain.
4815 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4817 isl_space *space;
4818 space = isl_set_get_space(set);
4819 space = isl_space_set_from_params(space);
4820 set = isl_set_reset_space(set, space);
4821 return set;
4824 /* Compute the parameter domain of the given map.
4826 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4828 isl_space *space;
4829 unsigned n;
4831 n = isl_map_dim(map, isl_dim_in);
4832 map = isl_map_project_out(map, isl_dim_in, 0, n);
4833 n = isl_map_dim(map, isl_dim_out);
4834 map = isl_map_project_out(map, isl_dim_out, 0, n);
4835 space = isl_map_get_space(map);
4836 space = isl_space_params(space);
4837 map = isl_map_reset_space(map, space);
4838 return map;
4841 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4843 isl_space *dim;
4844 struct isl_basic_set *domain;
4845 unsigned n_in;
4846 unsigned n_out;
4848 if (!bmap)
4849 return NULL;
4850 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4852 n_in = isl_basic_map_n_in(bmap);
4853 n_out = isl_basic_map_n_out(bmap);
4854 domain = isl_basic_set_from_basic_map(bmap);
4855 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4857 domain = isl_basic_set_reset_space(domain, dim);
4859 return domain;
4862 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4864 if (!bmap)
4865 return -1;
4866 return isl_space_may_be_set(bmap->dim);
4869 /* Is this basic map actually a set?
4870 * Users should never call this function. Outside of isl,
4871 * the type should indicate whether something is a set or a map.
4873 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4875 if (!bmap)
4876 return -1;
4877 return isl_space_is_set(bmap->dim);
4880 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4882 if (!bmap)
4883 return NULL;
4884 if (isl_basic_map_is_set(bmap))
4885 return bmap;
4886 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4889 __isl_give isl_basic_map *isl_basic_map_domain_map(
4890 __isl_take isl_basic_map *bmap)
4892 int i, k;
4893 isl_space *dim;
4894 isl_basic_map *domain;
4895 int nparam, n_in, n_out;
4896 unsigned total;
4898 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4899 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4900 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4902 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4903 domain = isl_basic_map_universe(dim);
4905 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4906 bmap = isl_basic_map_apply_range(bmap, domain);
4907 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4909 total = isl_basic_map_total_dim(bmap);
4911 for (i = 0; i < n_in; ++i) {
4912 k = isl_basic_map_alloc_equality(bmap);
4913 if (k < 0)
4914 goto error;
4915 isl_seq_clr(bmap->eq[k], 1 + total);
4916 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4917 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4920 bmap = isl_basic_map_gauss(bmap, NULL);
4921 return isl_basic_map_finalize(bmap);
4922 error:
4923 isl_basic_map_free(bmap);
4924 return NULL;
4927 __isl_give isl_basic_map *isl_basic_map_range_map(
4928 __isl_take isl_basic_map *bmap)
4930 int i, k;
4931 isl_space *dim;
4932 isl_basic_map *range;
4933 int nparam, n_in, n_out;
4934 unsigned total;
4936 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4937 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4938 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4940 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4941 range = isl_basic_map_universe(dim);
4943 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4944 bmap = isl_basic_map_apply_range(bmap, range);
4945 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4947 total = isl_basic_map_total_dim(bmap);
4949 for (i = 0; i < n_out; ++i) {
4950 k = isl_basic_map_alloc_equality(bmap);
4951 if (k < 0)
4952 goto error;
4953 isl_seq_clr(bmap->eq[k], 1 + total);
4954 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4955 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4958 bmap = isl_basic_map_gauss(bmap, NULL);
4959 return isl_basic_map_finalize(bmap);
4960 error:
4961 isl_basic_map_free(bmap);
4962 return NULL;
4965 int isl_map_may_be_set(__isl_keep isl_map *map)
4967 if (!map)
4968 return -1;
4969 return isl_space_may_be_set(map->dim);
4972 /* Is this map actually a set?
4973 * Users should never call this function. Outside of isl,
4974 * the type should indicate whether something is a set or a map.
4976 int isl_map_is_set(__isl_keep isl_map *map)
4978 if (!map)
4979 return -1;
4980 return isl_space_is_set(map->dim);
4983 struct isl_set *isl_map_range(struct isl_map *map)
4985 int i;
4986 struct isl_set *set;
4988 if (!map)
4989 goto error;
4990 if (isl_map_is_set(map))
4991 return (isl_set *)map;
4993 map = isl_map_cow(map);
4994 if (!map)
4995 goto error;
4997 set = (struct isl_set *) map;
4998 set->dim = isl_space_range(set->dim);
4999 if (!set->dim)
5000 goto error;
5001 for (i = 0; i < map->n; ++i) {
5002 set->p[i] = isl_basic_map_range(map->p[i]);
5003 if (!set->p[i])
5004 goto error;
5006 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5007 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5008 return set;
5009 error:
5010 isl_map_free(map);
5011 return NULL;
5014 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5016 int i;
5018 map = isl_map_cow(map);
5019 if (!map)
5020 return NULL;
5022 map->dim = isl_space_domain_map(map->dim);
5023 if (!map->dim)
5024 goto error;
5025 for (i = 0; i < map->n; ++i) {
5026 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5027 if (!map->p[i])
5028 goto error;
5030 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5031 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5032 return map;
5033 error:
5034 isl_map_free(map);
5035 return NULL;
5038 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5040 int i;
5041 isl_space *range_dim;
5043 map = isl_map_cow(map);
5044 if (!map)
5045 return NULL;
5047 range_dim = isl_space_range(isl_map_get_space(map));
5048 range_dim = isl_space_from_range(range_dim);
5049 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5050 map->dim = isl_space_join(map->dim, range_dim);
5051 if (!map->dim)
5052 goto error;
5053 for (i = 0; i < map->n; ++i) {
5054 map->p[i] = isl_basic_map_range_map(map->p[i]);
5055 if (!map->p[i])
5056 goto error;
5058 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5059 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5060 return map;
5061 error:
5062 isl_map_free(map);
5063 return NULL;
5066 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5067 __isl_take isl_space *dim)
5069 int i;
5070 struct isl_map *map = NULL;
5072 set = isl_set_cow(set);
5073 if (!set || !dim)
5074 goto error;
5075 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5076 map = (struct isl_map *)set;
5077 for (i = 0; i < set->n; ++i) {
5078 map->p[i] = isl_basic_map_from_basic_set(
5079 set->p[i], isl_space_copy(dim));
5080 if (!map->p[i])
5081 goto error;
5083 isl_space_free(map->dim);
5084 map->dim = dim;
5085 return map;
5086 error:
5087 isl_space_free(dim);
5088 isl_set_free(set);
5089 return NULL;
5092 __isl_give isl_basic_map *isl_basic_map_from_domain(
5093 __isl_take isl_basic_set *bset)
5095 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5098 __isl_give isl_basic_map *isl_basic_map_from_range(
5099 __isl_take isl_basic_set *bset)
5101 isl_space *space;
5102 space = isl_basic_set_get_space(bset);
5103 space = isl_space_from_range(space);
5104 bset = isl_basic_set_reset_space(bset, space);
5105 return (isl_basic_map *)bset;
5108 /* Create a relation with the given set as range.
5109 * The domain of the created relation is a zero-dimensional
5110 * flat anonymous space.
5112 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5114 isl_space *space;
5115 space = isl_set_get_space(set);
5116 space = isl_space_from_range(space);
5117 set = isl_set_reset_space(set, space);
5118 return (struct isl_map *)set;
5121 /* Create a relation with the given set as domain.
5122 * The range of the created relation is a zero-dimensional
5123 * flat anonymous space.
5125 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5127 return isl_map_reverse(isl_map_from_range(set));
5130 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5131 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5133 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5136 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5137 __isl_take isl_set *range)
5139 return isl_map_apply_range(isl_map_reverse(domain), range);
5142 struct isl_set *isl_set_from_map(struct isl_map *map)
5144 int i;
5145 struct isl_set *set = NULL;
5147 if (!map)
5148 return NULL;
5149 map = isl_map_cow(map);
5150 if (!map)
5151 return NULL;
5152 map->dim = isl_space_as_set_space(map->dim);
5153 if (!map->dim)
5154 goto error;
5155 set = (struct isl_set *)map;
5156 for (i = 0; i < map->n; ++i) {
5157 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
5158 if (!set->p[i])
5159 goto error;
5161 return set;
5162 error:
5163 isl_map_free(map);
5164 return NULL;
5167 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5168 unsigned flags)
5170 struct isl_map *map;
5172 if (!dim)
5173 return NULL;
5174 if (n < 0)
5175 isl_die(dim->ctx, isl_error_internal,
5176 "negative number of basic maps", goto error);
5177 map = isl_alloc(dim->ctx, struct isl_map,
5178 sizeof(struct isl_map) +
5179 (n - 1) * sizeof(struct isl_basic_map *));
5180 if (!map)
5181 goto error;
5183 map->ctx = dim->ctx;
5184 isl_ctx_ref(map->ctx);
5185 map->ref = 1;
5186 map->size = n;
5187 map->n = 0;
5188 map->dim = dim;
5189 map->flags = flags;
5190 return map;
5191 error:
5192 isl_space_free(dim);
5193 return NULL;
5196 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5197 unsigned nparam, unsigned in, unsigned out, int n,
5198 unsigned flags)
5200 struct isl_map *map;
5201 isl_space *dims;
5203 dims = isl_space_alloc(ctx, nparam, in, out);
5204 if (!dims)
5205 return NULL;
5207 map = isl_map_alloc_space(dims, n, flags);
5208 return map;
5211 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5213 struct isl_basic_map *bmap;
5214 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5215 bmap = isl_basic_map_set_to_empty(bmap);
5216 return bmap;
5219 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5221 struct isl_basic_set *bset;
5222 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5223 bset = isl_basic_set_set_to_empty(bset);
5224 return bset;
5227 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5229 struct isl_basic_map *bmap;
5230 if (!model)
5231 return NULL;
5232 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5233 bmap = isl_basic_map_set_to_empty(bmap);
5234 return bmap;
5237 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5239 struct isl_basic_map *bmap;
5240 if (!model)
5241 return NULL;
5242 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5243 bmap = isl_basic_map_set_to_empty(bmap);
5244 return bmap;
5247 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5249 struct isl_basic_set *bset;
5250 if (!model)
5251 return NULL;
5252 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5253 bset = isl_basic_set_set_to_empty(bset);
5254 return bset;
5257 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5259 struct isl_basic_map *bmap;
5260 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5261 bmap = isl_basic_map_finalize(bmap);
5262 return bmap;
5265 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5267 struct isl_basic_set *bset;
5268 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5269 bset = isl_basic_set_finalize(bset);
5270 return bset;
5273 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5275 int i;
5276 unsigned total = isl_space_dim(dim, isl_dim_all);
5277 isl_basic_map *bmap;
5279 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5280 for (i = 0; i < total; ++i) {
5281 int k = isl_basic_map_alloc_inequality(bmap);
5282 if (k < 0)
5283 goto error;
5284 isl_seq_clr(bmap->ineq[k], 1 + total);
5285 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5287 return bmap;
5288 error:
5289 isl_basic_map_free(bmap);
5290 return NULL;
5293 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5295 return isl_basic_map_nat_universe(dim);
5298 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5300 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5303 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5305 return isl_map_nat_universe(dim);
5308 __isl_give isl_basic_map *isl_basic_map_universe_like(
5309 __isl_keep isl_basic_map *model)
5311 if (!model)
5312 return NULL;
5313 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5316 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5318 if (!model)
5319 return NULL;
5320 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5323 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5324 __isl_keep isl_set *model)
5326 if (!model)
5327 return NULL;
5328 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5331 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5333 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5336 struct isl_map *isl_map_empty_like(struct isl_map *model)
5338 if (!model)
5339 return NULL;
5340 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5343 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5345 if (!model)
5346 return NULL;
5347 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5350 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5352 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5355 struct isl_set *isl_set_empty_like(struct isl_set *model)
5357 if (!model)
5358 return NULL;
5359 return isl_set_empty(isl_space_copy(model->dim));
5362 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5364 struct isl_map *map;
5365 if (!dim)
5366 return NULL;
5367 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5368 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5369 return map;
5372 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5374 struct isl_set *set;
5375 if (!dim)
5376 return NULL;
5377 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5378 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5379 return set;
5382 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5384 if (!model)
5385 return NULL;
5386 return isl_set_universe(isl_space_copy(model->dim));
5389 struct isl_map *isl_map_dup(struct isl_map *map)
5391 int i;
5392 struct isl_map *dup;
5394 if (!map)
5395 return NULL;
5396 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5397 for (i = 0; i < map->n; ++i)
5398 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5399 return dup;
5402 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5403 __isl_take isl_basic_map *bmap)
5405 if (!bmap || !map)
5406 goto error;
5407 if (isl_basic_map_plain_is_empty(bmap)) {
5408 isl_basic_map_free(bmap);
5409 return map;
5411 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5412 isl_assert(map->ctx, map->n < map->size, goto error);
5413 map->p[map->n] = bmap;
5414 map->n++;
5415 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5416 return map;
5417 error:
5418 if (map)
5419 isl_map_free(map);
5420 if (bmap)
5421 isl_basic_map_free(bmap);
5422 return NULL;
5425 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5427 int i;
5429 if (!map)
5430 return NULL;
5432 if (--map->ref > 0)
5433 return NULL;
5435 isl_ctx_deref(map->ctx);
5436 for (i = 0; i < map->n; ++i)
5437 isl_basic_map_free(map->p[i]);
5438 isl_space_free(map->dim);
5439 free(map);
5441 return NULL;
5444 struct isl_map *isl_map_extend(struct isl_map *base,
5445 unsigned nparam, unsigned n_in, unsigned n_out)
5447 int i;
5449 base = isl_map_cow(base);
5450 if (!base)
5451 return NULL;
5453 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5454 if (!base->dim)
5455 goto error;
5456 for (i = 0; i < base->n; ++i) {
5457 base->p[i] = isl_basic_map_extend_space(base->p[i],
5458 isl_space_copy(base->dim), 0, 0, 0);
5459 if (!base->p[i])
5460 goto error;
5462 return base;
5463 error:
5464 isl_map_free(base);
5465 return NULL;
5468 struct isl_set *isl_set_extend(struct isl_set *base,
5469 unsigned nparam, unsigned dim)
5471 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5472 nparam, 0, dim);
5475 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5476 struct isl_basic_map *bmap, unsigned pos, int value)
5478 int j;
5480 bmap = isl_basic_map_cow(bmap);
5481 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5482 j = isl_basic_map_alloc_equality(bmap);
5483 if (j < 0)
5484 goto error;
5485 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5486 isl_int_set_si(bmap->eq[j][pos], -1);
5487 isl_int_set_si(bmap->eq[j][0], value);
5488 bmap = isl_basic_map_simplify(bmap);
5489 return isl_basic_map_finalize(bmap);
5490 error:
5491 isl_basic_map_free(bmap);
5492 return NULL;
5495 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5496 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5498 int j;
5500 bmap = isl_basic_map_cow(bmap);
5501 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5502 j = isl_basic_map_alloc_equality(bmap);
5503 if (j < 0)
5504 goto error;
5505 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5506 isl_int_set_si(bmap->eq[j][pos], -1);
5507 isl_int_set(bmap->eq[j][0], value);
5508 bmap = isl_basic_map_simplify(bmap);
5509 return isl_basic_map_finalize(bmap);
5510 error:
5511 isl_basic_map_free(bmap);
5512 return NULL;
5515 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5516 enum isl_dim_type type, unsigned pos, int value)
5518 if (!bmap)
5519 return NULL;
5520 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5521 return isl_basic_map_fix_pos_si(bmap,
5522 isl_basic_map_offset(bmap, type) + pos, value);
5523 error:
5524 isl_basic_map_free(bmap);
5525 return NULL;
5528 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5529 enum isl_dim_type type, unsigned pos, isl_int value)
5531 if (!bmap)
5532 return NULL;
5533 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5534 return isl_basic_map_fix_pos(bmap,
5535 isl_basic_map_offset(bmap, type) + pos, value);
5536 error:
5537 isl_basic_map_free(bmap);
5538 return NULL;
5541 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5542 * to be equal to "v".
5544 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5545 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5547 if (!bmap || !v)
5548 goto error;
5549 if (!isl_val_is_int(v))
5550 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5551 "expecting integer value", goto error);
5552 if (pos >= isl_basic_map_dim(bmap, type))
5553 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5554 "index out of bounds", goto error);
5555 pos += isl_basic_map_offset(bmap, type);
5556 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5557 isl_val_free(v);
5558 return bmap;
5559 error:
5560 isl_basic_map_free(bmap);
5561 isl_val_free(v);
5562 return NULL;
5565 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5566 * to be equal to "v".
5568 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5569 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5571 return isl_basic_map_fix_val(bset, type, pos, v);
5574 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5575 enum isl_dim_type type, unsigned pos, int value)
5577 return (struct isl_basic_set *)
5578 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5579 type, pos, value);
5582 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5583 enum isl_dim_type type, unsigned pos, isl_int value)
5585 return (struct isl_basic_set *)
5586 isl_basic_map_fix((struct isl_basic_map *)bset,
5587 type, pos, value);
5590 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5591 unsigned input, int value)
5593 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5596 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5597 unsigned dim, int value)
5599 return (struct isl_basic_set *)
5600 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5601 isl_dim_set, dim, value);
5604 static int remove_if_empty(__isl_keep isl_map *map, int i)
5606 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5608 if (empty < 0)
5609 return -1;
5610 if (!empty)
5611 return 0;
5613 isl_basic_map_free(map->p[i]);
5614 if (i != map->n - 1) {
5615 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5616 map->p[i] = map->p[map->n - 1];
5618 map->n--;
5620 return 0;
5623 /* Perform "fn" on each basic map of "map", where we may not be holding
5624 * the only reference to "map".
5625 * In particular, "fn" should be a semantics preserving operation
5626 * that we want to apply to all copies of "map". We therefore need
5627 * to be careful not to modify "map" in a way that breaks "map"
5628 * in case anything goes wrong.
5630 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5631 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5633 struct isl_basic_map *bmap;
5634 int i;
5636 if (!map)
5637 return NULL;
5639 for (i = map->n - 1; i >= 0; --i) {
5640 bmap = isl_basic_map_copy(map->p[i]);
5641 bmap = fn(bmap);
5642 if (!bmap)
5643 goto error;
5644 isl_basic_map_free(map->p[i]);
5645 map->p[i] = bmap;
5646 if (remove_if_empty(map, i) < 0)
5647 goto error;
5650 return map;
5651 error:
5652 isl_map_free(map);
5653 return NULL;
5656 struct isl_map *isl_map_fix_si(struct isl_map *map,
5657 enum isl_dim_type type, unsigned pos, int value)
5659 int i;
5661 map = isl_map_cow(map);
5662 if (!map)
5663 return NULL;
5665 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5666 for (i = map->n - 1; i >= 0; --i) {
5667 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5668 if (remove_if_empty(map, i) < 0)
5669 goto error;
5671 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5672 return map;
5673 error:
5674 isl_map_free(map);
5675 return NULL;
5678 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5679 enum isl_dim_type type, unsigned pos, int value)
5681 return (struct isl_set *)
5682 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5685 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5686 enum isl_dim_type type, unsigned pos, isl_int value)
5688 int i;
5690 map = isl_map_cow(map);
5691 if (!map)
5692 return NULL;
5694 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5695 for (i = 0; i < map->n; ++i) {
5696 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5697 if (!map->p[i])
5698 goto error;
5700 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5701 return map;
5702 error:
5703 isl_map_free(map);
5704 return NULL;
5707 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5708 enum isl_dim_type type, unsigned pos, isl_int value)
5710 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5713 /* Fix the value of the variable at position "pos" of type "type" of "map"
5714 * to be equal to "v".
5716 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5717 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5719 int i;
5721 map = isl_map_cow(map);
5722 if (!map || !v)
5723 goto error;
5725 if (!isl_val_is_int(v))
5726 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5727 "expecting integer value", goto error);
5728 if (pos >= isl_map_dim(map, type))
5729 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5730 "index out of bounds", goto error);
5731 for (i = map->n - 1; i >= 0; --i) {
5732 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5733 isl_val_copy(v));
5734 if (remove_if_empty(map, i) < 0)
5735 goto error;
5737 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5738 isl_val_free(v);
5739 return map;
5740 error:
5741 isl_map_free(map);
5742 isl_val_free(v);
5743 return NULL;
5746 /* Fix the value of the variable at position "pos" of type "type" of "set"
5747 * to be equal to "v".
5749 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5750 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5752 return isl_map_fix_val(set, type, pos, v);
5755 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5756 unsigned input, int value)
5758 return isl_map_fix_si(map, isl_dim_in, input, value);
5761 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5763 return (struct isl_set *)
5764 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5767 static __isl_give isl_basic_map *basic_map_bound_si(
5768 __isl_take isl_basic_map *bmap,
5769 enum isl_dim_type type, unsigned pos, int value, int upper)
5771 int j;
5773 if (!bmap)
5774 return NULL;
5775 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5776 pos += isl_basic_map_offset(bmap, type);
5777 bmap = isl_basic_map_cow(bmap);
5778 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5779 j = isl_basic_map_alloc_inequality(bmap);
5780 if (j < 0)
5781 goto error;
5782 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5783 if (upper) {
5784 isl_int_set_si(bmap->ineq[j][pos], -1);
5785 isl_int_set_si(bmap->ineq[j][0], value);
5786 } else {
5787 isl_int_set_si(bmap->ineq[j][pos], 1);
5788 isl_int_set_si(bmap->ineq[j][0], -value);
5790 bmap = isl_basic_map_simplify(bmap);
5791 return isl_basic_map_finalize(bmap);
5792 error:
5793 isl_basic_map_free(bmap);
5794 return NULL;
5797 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5798 __isl_take isl_basic_map *bmap,
5799 enum isl_dim_type type, unsigned pos, int value)
5801 return basic_map_bound_si(bmap, type, pos, value, 0);
5804 /* Constrain the values of the given dimension to be no greater than "value".
5806 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5807 __isl_take isl_basic_map *bmap,
5808 enum isl_dim_type type, unsigned pos, int value)
5810 return basic_map_bound_si(bmap, type, pos, value, 1);
5813 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5814 unsigned dim, isl_int value)
5816 int j;
5818 bset = isl_basic_set_cow(bset);
5819 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5820 j = isl_basic_set_alloc_inequality(bset);
5821 if (j < 0)
5822 goto error;
5823 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5824 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5825 isl_int_neg(bset->ineq[j][0], value);
5826 bset = isl_basic_set_simplify(bset);
5827 return isl_basic_set_finalize(bset);
5828 error:
5829 isl_basic_set_free(bset);
5830 return NULL;
5833 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5834 enum isl_dim_type type, unsigned pos, int value, int upper)
5836 int i;
5838 map = isl_map_cow(map);
5839 if (!map)
5840 return NULL;
5842 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5843 for (i = 0; i < map->n; ++i) {
5844 map->p[i] = basic_map_bound_si(map->p[i],
5845 type, pos, value, upper);
5846 if (!map->p[i])
5847 goto error;
5849 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5850 return map;
5851 error:
5852 isl_map_free(map);
5853 return NULL;
5856 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5857 enum isl_dim_type type, unsigned pos, int value)
5859 return map_bound_si(map, type, pos, value, 0);
5862 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5863 enum isl_dim_type type, unsigned pos, int value)
5865 return map_bound_si(map, type, pos, value, 1);
5868 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5869 enum isl_dim_type type, unsigned pos, int value)
5871 return (struct isl_set *)
5872 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5875 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5876 enum isl_dim_type type, unsigned pos, int value)
5878 return isl_map_upper_bound_si(set, type, pos, value);
5881 /* Bound the given variable of "bmap" from below (or above is "upper"
5882 * is set) to "value".
5884 static __isl_give isl_basic_map *basic_map_bound(
5885 __isl_take isl_basic_map *bmap,
5886 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5888 int j;
5890 if (!bmap)
5891 return NULL;
5892 if (pos >= isl_basic_map_dim(bmap, type))
5893 isl_die(bmap->ctx, isl_error_invalid,
5894 "index out of bounds", goto error);
5895 pos += isl_basic_map_offset(bmap, type);
5896 bmap = isl_basic_map_cow(bmap);
5897 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5898 j = isl_basic_map_alloc_inequality(bmap);
5899 if (j < 0)
5900 goto error;
5901 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5902 if (upper) {
5903 isl_int_set_si(bmap->ineq[j][pos], -1);
5904 isl_int_set(bmap->ineq[j][0], value);
5905 } else {
5906 isl_int_set_si(bmap->ineq[j][pos], 1);
5907 isl_int_neg(bmap->ineq[j][0], value);
5909 bmap = isl_basic_map_simplify(bmap);
5910 return isl_basic_map_finalize(bmap);
5911 error:
5912 isl_basic_map_free(bmap);
5913 return NULL;
5916 /* Bound the given variable of "map" from below (or above is "upper"
5917 * is set) to "value".
5919 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5920 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5922 int i;
5924 map = isl_map_cow(map);
5925 if (!map)
5926 return NULL;
5928 if (pos >= isl_map_dim(map, type))
5929 isl_die(map->ctx, isl_error_invalid,
5930 "index out of bounds", goto error);
5931 for (i = map->n - 1; i >= 0; --i) {
5932 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5933 if (remove_if_empty(map, i) < 0)
5934 goto error;
5936 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5937 return map;
5938 error:
5939 isl_map_free(map);
5940 return NULL;
5943 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5944 enum isl_dim_type type, unsigned pos, isl_int value)
5946 return map_bound(map, type, pos, value, 0);
5949 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5950 enum isl_dim_type type, unsigned pos, isl_int value)
5952 return map_bound(map, type, pos, value, 1);
5955 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5956 enum isl_dim_type type, unsigned pos, isl_int value)
5958 return isl_map_lower_bound(set, type, pos, value);
5961 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5962 enum isl_dim_type type, unsigned pos, isl_int value)
5964 return isl_map_upper_bound(set, type, pos, value);
5967 /* Force the values of the variable at position "pos" of type "type" of "set"
5968 * to be no smaller than "value".
5970 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5971 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5973 if (!value)
5974 goto error;
5975 if (!isl_val_is_int(value))
5976 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5977 "expecting integer value", goto error);
5978 set = isl_set_lower_bound(set, type, pos, value->n);
5979 isl_val_free(value);
5980 return set;
5981 error:
5982 isl_val_free(value);
5983 isl_set_free(set);
5984 return NULL;
5987 /* Force the values of the variable at position "pos" of type "type" of "set"
5988 * to be no greater than "value".
5990 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5991 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5993 if (!value)
5994 goto error;
5995 if (!isl_val_is_int(value))
5996 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5997 "expecting integer value", goto error);
5998 set = isl_set_upper_bound(set, type, pos, value->n);
5999 isl_val_free(value);
6000 return set;
6001 error:
6002 isl_val_free(value);
6003 isl_set_free(set);
6004 return NULL;
6007 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6008 isl_int value)
6010 int i;
6012 set = isl_set_cow(set);
6013 if (!set)
6014 return NULL;
6016 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6017 for (i = 0; i < set->n; ++i) {
6018 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6019 if (!set->p[i])
6020 goto error;
6022 return set;
6023 error:
6024 isl_set_free(set);
6025 return NULL;
6028 struct isl_map *isl_map_reverse(struct isl_map *map)
6030 int i;
6032 map = isl_map_cow(map);
6033 if (!map)
6034 return NULL;
6036 map->dim = isl_space_reverse(map->dim);
6037 if (!map->dim)
6038 goto error;
6039 for (i = 0; i < map->n; ++i) {
6040 map->p[i] = isl_basic_map_reverse(map->p[i]);
6041 if (!map->p[i])
6042 goto error;
6044 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6045 return map;
6046 error:
6047 isl_map_free(map);
6048 return NULL;
6051 static struct isl_map *isl_basic_map_partial_lexopt(
6052 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6053 struct isl_set **empty, int max)
6055 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6058 struct isl_map *isl_basic_map_partial_lexmax(
6059 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6060 struct isl_set **empty)
6062 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6065 struct isl_map *isl_basic_map_partial_lexmin(
6066 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6067 struct isl_set **empty)
6069 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6072 struct isl_set *isl_basic_set_partial_lexmin(
6073 struct isl_basic_set *bset, struct isl_basic_set *dom,
6074 struct isl_set **empty)
6076 return (struct isl_set *)
6077 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6078 dom, empty);
6081 struct isl_set *isl_basic_set_partial_lexmax(
6082 struct isl_basic_set *bset, struct isl_basic_set *dom,
6083 struct isl_set **empty)
6085 return (struct isl_set *)
6086 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6087 dom, empty);
6090 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6091 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6092 __isl_give isl_set **empty)
6094 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6097 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6098 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6099 __isl_give isl_set **empty)
6101 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6104 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6105 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6106 __isl_give isl_set **empty)
6108 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6111 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6112 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6113 __isl_give isl_set **empty)
6115 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6118 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6119 __isl_take isl_basic_map *bmap, int max)
6121 isl_basic_set *dom = NULL;
6122 isl_space *dom_space;
6124 if (!bmap)
6125 goto error;
6126 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6127 dom = isl_basic_set_universe(dom_space);
6128 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6129 error:
6130 isl_basic_map_free(bmap);
6131 return NULL;
6134 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6135 __isl_take isl_basic_map *bmap)
6137 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6140 #undef TYPE
6141 #define TYPE isl_pw_multi_aff
6142 #undef SUFFIX
6143 #define SUFFIX _pw_multi_aff
6144 #undef EMPTY
6145 #define EMPTY isl_pw_multi_aff_empty
6146 #undef ADD
6147 #define ADD isl_pw_multi_aff_union_add
6148 #include "isl_map_lexopt_templ.c"
6150 /* Given a map "map", compute the lexicographically minimal
6151 * (or maximal) image element for each domain element in dom,
6152 * in the form of an isl_pw_multi_aff.
6153 * Set *empty to those elements in dom that do not have an image element.
6155 * We first compute the lexicographically minimal or maximal element
6156 * in the first basic map. This results in a partial solution "res"
6157 * and a subset "todo" of dom that still need to be handled.
6158 * We then consider each of the remaining maps in "map" and successively
6159 * update both "res" and "todo".
6161 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6162 __isl_take isl_map *map, __isl_take isl_set *dom,
6163 __isl_give isl_set **empty, int max)
6165 int i;
6166 isl_pw_multi_aff *res;
6167 isl_set *todo;
6169 if (!map || !dom)
6170 goto error;
6172 if (isl_map_plain_is_empty(map)) {
6173 if (empty)
6174 *empty = dom;
6175 else
6176 isl_set_free(dom);
6177 return isl_pw_multi_aff_from_map(map);
6180 res = basic_map_partial_lexopt_pw_multi_aff(
6181 isl_basic_map_copy(map->p[0]),
6182 isl_set_copy(dom), &todo, max);
6184 for (i = 1; i < map->n; ++i) {
6185 isl_pw_multi_aff *res_i;
6186 isl_set *todo_i;
6188 res_i = basic_map_partial_lexopt_pw_multi_aff(
6189 isl_basic_map_copy(map->p[i]),
6190 isl_set_copy(dom), &todo_i, max);
6192 if (max)
6193 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6194 else
6195 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6197 todo = isl_set_intersect(todo, todo_i);
6200 isl_set_free(dom);
6201 isl_map_free(map);
6203 if (empty)
6204 *empty = todo;
6205 else
6206 isl_set_free(todo);
6208 return res;
6209 error:
6210 if (empty)
6211 *empty = NULL;
6212 isl_set_free(dom);
6213 isl_map_free(map);
6214 return NULL;
6217 #undef TYPE
6218 #define TYPE isl_map
6219 #undef SUFFIX
6220 #define SUFFIX
6221 #undef EMPTY
6222 #define EMPTY isl_map_empty
6223 #undef ADD
6224 #define ADD isl_map_union_disjoint
6225 #include "isl_map_lexopt_templ.c"
6227 /* Given a map "map", compute the lexicographically minimal
6228 * (or maximal) image element for each domain element in dom.
6229 * Set *empty to those elements in dom that do not have an image element.
6231 * We first compute the lexicographically minimal or maximal element
6232 * in the first basic map. This results in a partial solution "res"
6233 * and a subset "todo" of dom that still need to be handled.
6234 * We then consider each of the remaining maps in "map" and successively
6235 * update both "res" and "todo".
6237 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6238 * Assume we are computing the lexicographical maximum.
6239 * We first compute the lexicographically maximal element in basic map i.
6240 * This results in a partial solution res_i and a subset todo_i.
6241 * Then we combine these results with those obtain for the first k basic maps
6242 * to obtain a result that is valid for the first k+1 basic maps.
6243 * In particular, the set where there is no solution is the set where
6244 * there is no solution for the first k basic maps and also no solution
6245 * for the ith basic map, i.e.,
6247 * todo^i = todo^k * todo_i
6249 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6250 * solutions, arbitrarily breaking ties in favor of res^k.
6251 * That is, when res^k(a) >= res_i(a), we pick res^k and
6252 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6253 * the lexicographic order.)
6254 * In practice, we compute
6256 * res^k * (res_i . "<=")
6258 * and
6260 * res_i * (res^k . "<")
6262 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6263 * where only one of res^k and res_i provides a solution and we simply pick
6264 * that one, i.e.,
6266 * res^k * todo_i
6267 * and
6268 * res_i * todo^k
6270 * Note that we only compute these intersections when dom(res^k) intersects
6271 * dom(res_i). Otherwise, the only effect of these intersections is to
6272 * potentially break up res^k and res_i into smaller pieces.
6273 * We want to avoid such splintering as much as possible.
6274 * In fact, an earlier implementation of this function would look for
6275 * better results in the domain of res^k and for extra results in todo^k,
6276 * but this would always result in a splintering according to todo^k,
6277 * even when the domain of basic map i is disjoint from the domains of
6278 * the previous basic maps.
6280 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6281 __isl_take isl_map *map, __isl_take isl_set *dom,
6282 __isl_give isl_set **empty, int max)
6284 int i;
6285 struct isl_map *res;
6286 struct isl_set *todo;
6288 if (!map || !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 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6300 isl_set_copy(dom), &todo, max);
6302 for (i = 1; i < map->n; ++i) {
6303 isl_map *lt, *le;
6304 isl_map *res_i;
6305 isl_set *todo_i;
6306 isl_space *dim = isl_space_range(isl_map_get_space(res));
6308 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6309 isl_set_copy(dom), &todo_i, max);
6311 if (max) {
6312 lt = isl_map_lex_lt(isl_space_copy(dim));
6313 le = isl_map_lex_le(dim);
6314 } else {
6315 lt = isl_map_lex_gt(isl_space_copy(dim));
6316 le = isl_map_lex_ge(dim);
6318 lt = isl_map_apply_range(isl_map_copy(res), lt);
6319 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6320 le = isl_map_apply_range(isl_map_copy(res_i), le);
6321 le = isl_map_intersect(le, isl_map_copy(res));
6323 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6324 res = isl_map_intersect_domain(res,
6325 isl_set_copy(todo_i));
6326 res_i = isl_map_intersect_domain(res_i,
6327 isl_set_copy(todo));
6330 res = isl_map_union_disjoint(res, res_i);
6331 res = isl_map_union_disjoint(res, lt);
6332 res = isl_map_union_disjoint(res, le);
6334 todo = isl_set_intersect(todo, todo_i);
6337 isl_set_free(dom);
6338 isl_map_free(map);
6340 if (empty)
6341 *empty = todo;
6342 else
6343 isl_set_free(todo);
6345 return res;
6346 error:
6347 if (empty)
6348 *empty = NULL;
6349 isl_set_free(dom);
6350 isl_map_free(map);
6351 return NULL;
6354 __isl_give isl_map *isl_map_partial_lexmax(
6355 __isl_take isl_map *map, __isl_take isl_set *dom,
6356 __isl_give isl_set **empty)
6358 return isl_map_partial_lexopt(map, dom, empty, 1);
6361 __isl_give isl_map *isl_map_partial_lexmin(
6362 __isl_take isl_map *map, __isl_take isl_set *dom,
6363 __isl_give isl_set **empty)
6365 return isl_map_partial_lexopt(map, dom, empty, 0);
6368 __isl_give isl_set *isl_set_partial_lexmin(
6369 __isl_take isl_set *set, __isl_take isl_set *dom,
6370 __isl_give isl_set **empty)
6372 return (struct isl_set *)
6373 isl_map_partial_lexmin((struct isl_map *)set,
6374 dom, empty);
6377 __isl_give isl_set *isl_set_partial_lexmax(
6378 __isl_take isl_set *set, __isl_take isl_set *dom,
6379 __isl_give isl_set **empty)
6381 return (struct isl_set *)
6382 isl_map_partial_lexmax((struct isl_map *)set,
6383 dom, empty);
6386 /* Compute the lexicographic minimum (or maximum if "max" is set)
6387 * of "bmap" over its domain.
6389 * Since we are not interested in the part of the domain space where
6390 * there is no solution, we initialize the domain to those constraints
6391 * of "bmap" that only involve the parameters and the input dimensions.
6392 * This relieves the parametric programming engine from detecting those
6393 * inequalities and transferring them to the context. More importantly,
6394 * it ensures that those inequalities are transferred first and not
6395 * intermixed with inequalities that actually split the domain.
6397 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6399 int n_div;
6400 int n_out;
6401 isl_basic_map *copy;
6402 isl_basic_set *dom;
6404 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6405 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6406 copy = isl_basic_map_copy(bmap);
6407 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6408 isl_dim_div, 0, n_div);
6409 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6410 isl_dim_out, 0, n_out);
6411 dom = isl_basic_map_domain(copy);
6412 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6415 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6417 return isl_basic_map_lexopt(bmap, 0);
6420 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6422 return isl_basic_map_lexopt(bmap, 1);
6425 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6427 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6430 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6432 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6435 /* Extract the first and only affine expression from list
6436 * and then add it to *pwaff with the given dom.
6437 * This domain is known to be disjoint from other domains
6438 * because of the way isl_basic_map_foreach_lexmax works.
6440 static int update_dim_opt(__isl_take isl_basic_set *dom,
6441 __isl_take isl_aff_list *list, void *user)
6443 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6444 isl_aff *aff;
6445 isl_pw_aff **pwaff = user;
6446 isl_pw_aff *pwaff_i;
6448 if (!list)
6449 goto error;
6450 if (isl_aff_list_n_aff(list) != 1)
6451 isl_die(ctx, isl_error_internal,
6452 "expecting single element list", goto error);
6454 aff = isl_aff_list_get_aff(list, 0);
6455 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6457 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6459 isl_aff_list_free(list);
6461 return 0;
6462 error:
6463 isl_basic_set_free(dom);
6464 isl_aff_list_free(list);
6465 return -1;
6468 /* Given a basic map with one output dimension, compute the minimum or
6469 * maximum of that dimension as an isl_pw_aff.
6471 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6472 * call update_dim_opt on each leaf of the result.
6474 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6475 int max)
6477 isl_space *dim = isl_basic_map_get_space(bmap);
6478 isl_pw_aff *pwaff;
6479 int r;
6481 dim = isl_space_from_domain(isl_space_domain(dim));
6482 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6483 pwaff = isl_pw_aff_empty(dim);
6485 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6486 if (r < 0)
6487 return isl_pw_aff_free(pwaff);
6489 return pwaff;
6492 /* Compute the minimum or maximum of the given output dimension
6493 * as a function of the parameters and the input dimensions,
6494 * but independently of the other output dimensions.
6496 * We first project out the other output dimension and then compute
6497 * the "lexicographic" maximum in each basic map, combining the results
6498 * using isl_pw_aff_union_max.
6500 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6501 int max)
6503 int i;
6504 isl_pw_aff *pwaff;
6505 unsigned n_out;
6507 n_out = isl_map_dim(map, isl_dim_out);
6508 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6509 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6510 if (!map)
6511 return NULL;
6513 if (map->n == 0) {
6514 isl_space *dim = isl_map_get_space(map);
6515 isl_map_free(map);
6516 return isl_pw_aff_empty(dim);
6519 pwaff = basic_map_dim_opt(map->p[0], max);
6520 for (i = 1; i < map->n; ++i) {
6521 isl_pw_aff *pwaff_i;
6523 pwaff_i = basic_map_dim_opt(map->p[i], max);
6524 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6527 isl_map_free(map);
6529 return pwaff;
6532 /* Compute the maximum of the given output dimension as a function of the
6533 * parameters and input dimensions, but independently of
6534 * the other output dimensions.
6536 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6538 return map_dim_opt(map, pos, 1);
6541 /* Compute the minimum or maximum of the given set dimension
6542 * as a function of the parameters,
6543 * but independently of the other set dimensions.
6545 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6546 int max)
6548 return map_dim_opt(set, pos, max);
6551 /* Compute the maximum of the given set dimension as a function of the
6552 * parameters, but independently of the other set dimensions.
6554 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6556 return set_dim_opt(set, pos, 1);
6559 /* Compute the minimum of the given set dimension as a function of the
6560 * parameters, but independently of the other set dimensions.
6562 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6564 return set_dim_opt(set, pos, 0);
6567 /* Apply a preimage specified by "mat" on the parameters of "bset".
6568 * bset is assumed to have only parameters and divs.
6570 static struct isl_basic_set *basic_set_parameter_preimage(
6571 struct isl_basic_set *bset, struct isl_mat *mat)
6573 unsigned nparam;
6575 if (!bset || !mat)
6576 goto error;
6578 bset->dim = isl_space_cow(bset->dim);
6579 if (!bset->dim)
6580 goto error;
6582 nparam = isl_basic_set_dim(bset, isl_dim_param);
6584 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6586 bset->dim->nparam = 0;
6587 bset->dim->n_out = nparam;
6588 bset = isl_basic_set_preimage(bset, mat);
6589 if (bset) {
6590 bset->dim->nparam = bset->dim->n_out;
6591 bset->dim->n_out = 0;
6593 return bset;
6594 error:
6595 isl_mat_free(mat);
6596 isl_basic_set_free(bset);
6597 return NULL;
6600 /* Apply a preimage specified by "mat" on the parameters of "set".
6601 * set is assumed to have only parameters and divs.
6603 static struct isl_set *set_parameter_preimage(
6604 struct isl_set *set, struct isl_mat *mat)
6606 isl_space *dim = NULL;
6607 unsigned nparam;
6609 if (!set || !mat)
6610 goto error;
6612 dim = isl_space_copy(set->dim);
6613 dim = isl_space_cow(dim);
6614 if (!dim)
6615 goto error;
6617 nparam = isl_set_dim(set, isl_dim_param);
6619 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6621 dim->nparam = 0;
6622 dim->n_out = nparam;
6623 isl_set_reset_space(set, dim);
6624 set = isl_set_preimage(set, mat);
6625 if (!set)
6626 goto error2;
6627 dim = isl_space_copy(set->dim);
6628 dim = isl_space_cow(dim);
6629 if (!dim)
6630 goto error2;
6631 dim->nparam = dim->n_out;
6632 dim->n_out = 0;
6633 isl_set_reset_space(set, dim);
6634 return set;
6635 error:
6636 isl_space_free(dim);
6637 isl_mat_free(mat);
6638 error2:
6639 isl_set_free(set);
6640 return NULL;
6643 /* Intersect the basic set "bset" with the affine space specified by the
6644 * equalities in "eq".
6646 static struct isl_basic_set *basic_set_append_equalities(
6647 struct isl_basic_set *bset, struct isl_mat *eq)
6649 int i, k;
6650 unsigned len;
6652 if (!bset || !eq)
6653 goto error;
6655 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6656 eq->n_row, 0);
6657 if (!bset)
6658 goto error;
6660 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6661 for (i = 0; i < eq->n_row; ++i) {
6662 k = isl_basic_set_alloc_equality(bset);
6663 if (k < 0)
6664 goto error;
6665 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6666 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6668 isl_mat_free(eq);
6670 bset = isl_basic_set_gauss(bset, NULL);
6671 bset = isl_basic_set_finalize(bset);
6673 return bset;
6674 error:
6675 isl_mat_free(eq);
6676 isl_basic_set_free(bset);
6677 return NULL;
6680 /* Intersect the set "set" with the affine space specified by the
6681 * equalities in "eq".
6683 static struct isl_set *set_append_equalities(struct isl_set *set,
6684 struct isl_mat *eq)
6686 int i;
6688 if (!set || !eq)
6689 goto error;
6691 for (i = 0; i < set->n; ++i) {
6692 set->p[i] = basic_set_append_equalities(set->p[i],
6693 isl_mat_copy(eq));
6694 if (!set->p[i])
6695 goto error;
6697 isl_mat_free(eq);
6698 return set;
6699 error:
6700 isl_mat_free(eq);
6701 isl_set_free(set);
6702 return NULL;
6705 /* Given a basic set "bset" that only involves parameters and existentially
6706 * quantified variables, return the index of the first equality
6707 * that only involves parameters. If there is no such equality then
6708 * return bset->n_eq.
6710 * This function assumes that isl_basic_set_gauss has been called on "bset".
6712 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6714 int i, j;
6715 unsigned nparam, n_div;
6717 if (!bset)
6718 return -1;
6720 nparam = isl_basic_set_dim(bset, isl_dim_param);
6721 n_div = isl_basic_set_dim(bset, isl_dim_div);
6723 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6724 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6725 ++i;
6728 return i;
6731 /* Compute an explicit representation for the existentially quantified
6732 * variables in "bset" by computing the "minimal value" of the set
6733 * variables. Since there are no set variables, the computation of
6734 * the minimal value essentially computes an explicit representation
6735 * of the non-empty part(s) of "bset".
6737 * The input only involves parameters and existentially quantified variables.
6738 * All equalities among parameters have been removed.
6740 * Since the existentially quantified variables in the result are in general
6741 * going to be different from those in the input, we first replace
6742 * them by the minimal number of variables based on their equalities.
6743 * This should simplify the parametric integer programming.
6745 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6747 isl_morph *morph1, *morph2;
6748 isl_set *set;
6749 unsigned n;
6751 if (!bset)
6752 return NULL;
6753 if (bset->n_eq == 0)
6754 return isl_basic_set_lexmin(bset);
6756 morph1 = isl_basic_set_parameter_compression(bset);
6757 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6758 bset = isl_basic_set_lift(bset);
6759 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6760 bset = isl_morph_basic_set(morph2, bset);
6761 n = isl_basic_set_dim(bset, isl_dim_set);
6762 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6764 set = isl_basic_set_lexmin(bset);
6766 set = isl_morph_set(isl_morph_inverse(morph1), set);
6768 return set;
6771 /* Project the given basic set onto its parameter domain, possibly introducing
6772 * new, explicit, existential variables in the constraints.
6773 * The input has parameters and (possibly implicit) existential variables.
6774 * The output has the same parameters, but only
6775 * explicit existentially quantified variables.
6777 * The actual projection is performed by pip, but pip doesn't seem
6778 * to like equalities very much, so we first remove the equalities
6779 * among the parameters by performing a variable compression on
6780 * the parameters. Afterward, an inverse transformation is performed
6781 * and the equalities among the parameters are inserted back in.
6783 * The variable compression on the parameters may uncover additional
6784 * equalities that were only implicit before. We therefore check
6785 * if there are any new parameter equalities in the result and
6786 * if so recurse. The removal of parameter equalities is required
6787 * for the parameter compression performed by base_compute_divs.
6789 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6791 int i;
6792 struct isl_mat *eq;
6793 struct isl_mat *T, *T2;
6794 struct isl_set *set;
6795 unsigned nparam;
6797 bset = isl_basic_set_cow(bset);
6798 if (!bset)
6799 return NULL;
6801 if (bset->n_eq == 0)
6802 return base_compute_divs(bset);
6804 bset = isl_basic_set_gauss(bset, NULL);
6805 if (!bset)
6806 return NULL;
6807 if (isl_basic_set_plain_is_empty(bset))
6808 return isl_set_from_basic_set(bset);
6810 i = first_parameter_equality(bset);
6811 if (i == bset->n_eq)
6812 return base_compute_divs(bset);
6814 nparam = isl_basic_set_dim(bset, isl_dim_param);
6815 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6816 0, 1 + nparam);
6817 eq = isl_mat_cow(eq);
6818 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6819 if (T && T->n_col == 0) {
6820 isl_mat_free(T);
6821 isl_mat_free(T2);
6822 isl_mat_free(eq);
6823 bset = isl_basic_set_set_to_empty(bset);
6824 return isl_set_from_basic_set(bset);
6826 bset = basic_set_parameter_preimage(bset, T);
6828 i = first_parameter_equality(bset);
6829 if (!bset)
6830 set = NULL;
6831 else if (i == bset->n_eq)
6832 set = base_compute_divs(bset);
6833 else
6834 set = parameter_compute_divs(bset);
6835 set = set_parameter_preimage(set, T2);
6836 set = set_append_equalities(set, eq);
6837 return set;
6840 /* Insert the divs from "ls" before those of "bmap".
6842 * The number of columns is not changed, which means that the last
6843 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6844 * The caller is responsible for removing the same number of dimensions
6845 * from the space of "bmap".
6847 static __isl_give isl_basic_map *insert_divs_from_local_space(
6848 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6850 int i;
6851 int n_div;
6852 int old_n_div;
6854 n_div = isl_local_space_dim(ls, isl_dim_div);
6855 if (n_div == 0)
6856 return bmap;
6858 old_n_div = bmap->n_div;
6859 bmap = insert_div_rows(bmap, n_div);
6860 if (!bmap)
6861 return NULL;
6863 for (i = 0; i < n_div; ++i) {
6864 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6865 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6868 return bmap;
6871 /* Replace the space of "bmap" by the space and divs of "ls".
6873 * If "ls" has any divs, then we simplify the result since we may
6874 * have discovered some additional equalities that could simplify
6875 * the div expressions.
6877 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6878 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6880 int n_div;
6882 bmap = isl_basic_map_cow(bmap);
6883 if (!bmap || !ls)
6884 goto error;
6886 n_div = isl_local_space_dim(ls, isl_dim_div);
6887 bmap = insert_divs_from_local_space(bmap, ls);
6888 if (!bmap)
6889 goto error;
6891 isl_space_free(bmap->dim);
6892 bmap->dim = isl_local_space_get_space(ls);
6893 if (!bmap->dim)
6894 goto error;
6896 isl_local_space_free(ls);
6897 if (n_div > 0)
6898 bmap = isl_basic_map_simplify(bmap);
6899 bmap = isl_basic_map_finalize(bmap);
6900 return bmap;
6901 error:
6902 isl_basic_map_free(bmap);
6903 isl_local_space_free(ls);
6904 return NULL;
6907 /* Replace the space of "map" by the space and divs of "ls".
6909 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6910 __isl_take isl_local_space *ls)
6912 int i;
6914 map = isl_map_cow(map);
6915 if (!map || !ls)
6916 goto error;
6918 for (i = 0; i < map->n; ++i) {
6919 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6920 isl_local_space_copy(ls));
6921 if (!map->p[i])
6922 goto error;
6924 isl_space_free(map->dim);
6925 map->dim = isl_local_space_get_space(ls);
6926 if (!map->dim)
6927 goto error;
6929 isl_local_space_free(ls);
6930 return map;
6931 error:
6932 isl_local_space_free(ls);
6933 isl_map_free(map);
6934 return NULL;
6937 /* Compute an explicit representation for the existentially
6938 * quantified variables for which do not know any explicit representation yet.
6940 * We first sort the existentially quantified variables so that the
6941 * existentially quantified variables for which we already have an explicit
6942 * representation are placed before those for which we do not.
6943 * The input dimensions, the output dimensions and the existentially
6944 * quantified variables for which we already have an explicit
6945 * representation are then turned into parameters.
6946 * compute_divs returns a map with the same parameters and
6947 * no input or output dimensions and the dimension specification
6948 * is reset to that of the input, including the existentially quantified
6949 * variables for which we already had an explicit representation.
6951 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6953 struct isl_basic_set *bset;
6954 struct isl_set *set;
6955 struct isl_map *map;
6956 isl_space *dim;
6957 isl_local_space *ls;
6958 unsigned nparam;
6959 unsigned n_in;
6960 unsigned n_out;
6961 unsigned n_known;
6962 int i;
6964 bmap = isl_basic_map_sort_divs(bmap);
6965 bmap = isl_basic_map_cow(bmap);
6966 if (!bmap)
6967 return NULL;
6969 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6970 if (isl_int_is_zero(bmap->div[n_known][0]))
6971 break;
6973 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6974 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6975 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6976 dim = isl_space_set_alloc(bmap->ctx,
6977 nparam + n_in + n_out + n_known, 0);
6978 if (!dim)
6979 goto error;
6981 ls = isl_basic_map_get_local_space(bmap);
6982 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6983 n_known, bmap->n_div - n_known);
6984 if (n_known > 0) {
6985 for (i = n_known; i < bmap->n_div; ++i)
6986 swap_div(bmap, i - n_known, i);
6987 bmap->n_div -= n_known;
6988 bmap->extra -= n_known;
6990 bmap = isl_basic_map_reset_space(bmap, dim);
6991 bset = (struct isl_basic_set *)bmap;
6993 set = parameter_compute_divs(bset);
6994 map = (struct isl_map *)set;
6995 map = replace_space_by_local_space(map, ls);
6997 return map;
6998 error:
6999 isl_basic_map_free(bmap);
7000 return NULL;
7003 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7005 int i;
7006 unsigned off;
7008 if (!bmap)
7009 return -1;
7011 off = isl_space_dim(bmap->dim, isl_dim_all);
7012 for (i = 0; i < bmap->n_div; ++i) {
7013 if (isl_int_is_zero(bmap->div[i][0]))
7014 return 0;
7015 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
7016 return -1);
7018 return 1;
7021 static int map_divs_known(__isl_keep isl_map *map)
7023 int i;
7025 if (!map)
7026 return -1;
7028 for (i = 0; i < map->n; ++i) {
7029 int known = isl_basic_map_divs_known(map->p[i]);
7030 if (known <= 0)
7031 return known;
7034 return 1;
7037 /* If bmap contains any unknown divs, then compute explicit
7038 * expressions for them. However, this computation may be
7039 * quite expensive, so first try to remove divs that aren't
7040 * strictly needed.
7042 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7044 int known;
7045 struct isl_map *map;
7047 known = isl_basic_map_divs_known(bmap);
7048 if (known < 0)
7049 goto error;
7050 if (known)
7051 return isl_map_from_basic_map(bmap);
7053 bmap = isl_basic_map_drop_redundant_divs(bmap);
7055 known = isl_basic_map_divs_known(bmap);
7056 if (known < 0)
7057 goto error;
7058 if (known)
7059 return isl_map_from_basic_map(bmap);
7061 map = compute_divs(bmap);
7062 return map;
7063 error:
7064 isl_basic_map_free(bmap);
7065 return NULL;
7068 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7070 int i;
7071 int known;
7072 struct isl_map *res;
7074 if (!map)
7075 return NULL;
7076 if (map->n == 0)
7077 return map;
7079 known = map_divs_known(map);
7080 if (known < 0) {
7081 isl_map_free(map);
7082 return NULL;
7084 if (known)
7085 return map;
7087 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7088 for (i = 1 ; i < map->n; ++i) {
7089 struct isl_map *r2;
7090 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7091 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7092 res = isl_map_union_disjoint(res, r2);
7093 else
7094 res = isl_map_union(res, r2);
7096 isl_map_free(map);
7098 return res;
7101 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7103 return (struct isl_set *)
7104 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7107 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7109 return (struct isl_set *)
7110 isl_map_compute_divs((struct isl_map *)set);
7113 struct isl_set *isl_map_domain(struct isl_map *map)
7115 int i;
7116 struct isl_set *set;
7118 if (!map)
7119 goto error;
7121 map = isl_map_cow(map);
7122 if (!map)
7123 return NULL;
7125 set = (struct isl_set *)map;
7126 set->dim = isl_space_domain(set->dim);
7127 if (!set->dim)
7128 goto error;
7129 for (i = 0; i < map->n; ++i) {
7130 set->p[i] = isl_basic_map_domain(map->p[i]);
7131 if (!set->p[i])
7132 goto error;
7134 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7135 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7136 return set;
7137 error:
7138 isl_map_free(map);
7139 return NULL;
7142 /* Return the union of "map1" and "map2", where we assume for now that
7143 * "map1" and "map2" are disjoint. Note that the basic maps inside
7144 * "map1" or "map2" may not be disjoint from each other.
7145 * Also note that this function is also called from isl_map_union,
7146 * which takes care of handling the situation where "map1" and "map2"
7147 * may not be disjoint.
7149 * If one of the inputs is empty, we can simply return the other input.
7150 * Similarly, if one of the inputs is universal, then it is equal to the union.
7152 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7153 __isl_take isl_map *map2)
7155 int i;
7156 unsigned flags = 0;
7157 struct isl_map *map = NULL;
7158 int is_universe;
7160 if (!map1 || !map2)
7161 goto error;
7163 if (!isl_space_is_equal(map1->dim, map2->dim))
7164 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7165 "spaces don't match", goto error);
7167 if (map1->n == 0) {
7168 isl_map_free(map1);
7169 return map2;
7171 if (map2->n == 0) {
7172 isl_map_free(map2);
7173 return map1;
7176 is_universe = isl_map_plain_is_universe(map1);
7177 if (is_universe < 0)
7178 goto error;
7179 if (is_universe) {
7180 isl_map_free(map2);
7181 return map1;
7184 is_universe = isl_map_plain_is_universe(map2);
7185 if (is_universe < 0)
7186 goto error;
7187 if (is_universe) {
7188 isl_map_free(map1);
7189 return map2;
7192 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7193 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7194 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7196 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7197 map1->n + map2->n, flags);
7198 if (!map)
7199 goto error;
7200 for (i = 0; i < map1->n; ++i) {
7201 map = isl_map_add_basic_map(map,
7202 isl_basic_map_copy(map1->p[i]));
7203 if (!map)
7204 goto error;
7206 for (i = 0; i < map2->n; ++i) {
7207 map = isl_map_add_basic_map(map,
7208 isl_basic_map_copy(map2->p[i]));
7209 if (!map)
7210 goto error;
7212 isl_map_free(map1);
7213 isl_map_free(map2);
7214 return map;
7215 error:
7216 isl_map_free(map);
7217 isl_map_free(map1);
7218 isl_map_free(map2);
7219 return NULL;
7222 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7223 * guaranteed to be disjoint by the caller.
7225 * Note that this functions is called from within isl_map_make_disjoint,
7226 * so we have to be careful not to touch the constraints of the inputs
7227 * in any way.
7229 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7230 __isl_take isl_map *map2)
7232 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7235 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7236 * not be disjoint. The parameters are assumed to have been aligned.
7238 * We currently simply call map_union_disjoint, the internal operation
7239 * of which does not really depend on the inputs being disjoint.
7240 * If the result contains more than one basic map, then we clear
7241 * the disjoint flag since the result may contain basic maps from
7242 * both inputs and these are not guaranteed to be disjoint.
7244 * As a special case, if "map1" and "map2" are obviously equal,
7245 * then we simply return "map1".
7247 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7248 __isl_take isl_map *map2)
7250 int equal;
7252 if (!map1 || !map2)
7253 goto error;
7255 equal = isl_map_plain_is_equal(map1, map2);
7256 if (equal < 0)
7257 goto error;
7258 if (equal) {
7259 isl_map_free(map2);
7260 return map1;
7263 map1 = map_union_disjoint(map1, map2);
7264 if (!map1)
7265 return NULL;
7266 if (map1->n > 1)
7267 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7268 return map1;
7269 error:
7270 isl_map_free(map1);
7271 isl_map_free(map2);
7272 return NULL;
7275 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7276 * not be disjoint.
7278 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7279 __isl_take isl_map *map2)
7281 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7284 struct isl_set *isl_set_union_disjoint(
7285 struct isl_set *set1, struct isl_set *set2)
7287 return (struct isl_set *)
7288 isl_map_union_disjoint(
7289 (struct isl_map *)set1, (struct isl_map *)set2);
7292 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7294 return (struct isl_set *)
7295 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7298 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7299 * the results.
7301 * "map" and "set" are assumed to be compatible and non-NULL.
7303 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7304 __isl_take isl_set *set,
7305 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7306 __isl_take isl_basic_set *bset))
7308 unsigned flags = 0;
7309 struct isl_map *result;
7310 int i, j;
7312 if (isl_set_plain_is_universe(set)) {
7313 isl_set_free(set);
7314 return map;
7317 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7318 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7319 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7321 result = isl_map_alloc_space(isl_space_copy(map->dim),
7322 map->n * set->n, flags);
7323 for (i = 0; result && i < map->n; ++i)
7324 for (j = 0; j < set->n; ++j) {
7325 result = isl_map_add_basic_map(result,
7326 fn(isl_basic_map_copy(map->p[i]),
7327 isl_basic_set_copy(set->p[j])));
7328 if (!result)
7329 break;
7332 isl_map_free(map);
7333 isl_set_free(set);
7334 return result;
7337 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7338 __isl_take isl_set *set)
7340 if (!map || !set)
7341 goto error;
7343 if (!isl_map_compatible_range(map, set))
7344 isl_die(set->ctx, isl_error_invalid,
7345 "incompatible spaces", goto error);
7347 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7348 error:
7349 isl_map_free(map);
7350 isl_set_free(set);
7351 return NULL;
7354 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7355 __isl_take isl_set *set)
7357 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7360 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7361 __isl_take isl_set *set)
7363 if (!map || !set)
7364 goto error;
7366 if (!isl_map_compatible_domain(map, set))
7367 isl_die(set->ctx, isl_error_invalid,
7368 "incompatible spaces", goto error);
7370 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7371 error:
7372 isl_map_free(map);
7373 isl_set_free(set);
7374 return NULL;
7377 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7378 __isl_take isl_set *set)
7380 return isl_map_align_params_map_map_and(map, set,
7381 &map_intersect_domain);
7384 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7385 __isl_take isl_map *map2)
7387 if (!map1 || !map2)
7388 goto error;
7389 map1 = isl_map_reverse(map1);
7390 map1 = isl_map_apply_range(map1, map2);
7391 return isl_map_reverse(map1);
7392 error:
7393 isl_map_free(map1);
7394 isl_map_free(map2);
7395 return NULL;
7398 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7399 __isl_take isl_map *map2)
7401 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7404 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7405 __isl_take isl_map *map2)
7407 isl_space *dim_result;
7408 struct isl_map *result;
7409 int i, j;
7411 if (!map1 || !map2)
7412 goto error;
7414 dim_result = isl_space_join(isl_space_copy(map1->dim),
7415 isl_space_copy(map2->dim));
7417 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7418 if (!result)
7419 goto error;
7420 for (i = 0; i < map1->n; ++i)
7421 for (j = 0; j < map2->n; ++j) {
7422 result = isl_map_add_basic_map(result,
7423 isl_basic_map_apply_range(
7424 isl_basic_map_copy(map1->p[i]),
7425 isl_basic_map_copy(map2->p[j])));
7426 if (!result)
7427 goto error;
7429 isl_map_free(map1);
7430 isl_map_free(map2);
7431 if (result && result->n <= 1)
7432 ISL_F_SET(result, ISL_MAP_DISJOINT);
7433 return result;
7434 error:
7435 isl_map_free(map1);
7436 isl_map_free(map2);
7437 return NULL;
7440 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7441 __isl_take isl_map *map2)
7443 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7447 * returns range - domain
7449 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7451 isl_space *dims, *target_dim;
7452 struct isl_basic_set *bset;
7453 unsigned dim;
7454 unsigned nparam;
7455 int i;
7457 if (!bmap)
7458 goto error;
7459 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7460 bmap->dim, isl_dim_out),
7461 goto error);
7462 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7463 dim = isl_basic_map_n_in(bmap);
7464 nparam = isl_basic_map_n_param(bmap);
7465 bset = isl_basic_set_from_basic_map(bmap);
7466 bset = isl_basic_set_cow(bset);
7467 dims = isl_basic_set_get_space(bset);
7468 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7469 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7470 bset = isl_basic_set_swap_vars(bset, 2*dim);
7471 for (i = 0; i < dim; ++i) {
7472 int j = isl_basic_map_alloc_equality(
7473 (struct isl_basic_map *)bset);
7474 if (j < 0) {
7475 bset = isl_basic_set_free(bset);
7476 break;
7478 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7479 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7480 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7481 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7483 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7484 bset = isl_basic_set_reset_space(bset, target_dim);
7485 return bset;
7486 error:
7487 isl_basic_map_free(bmap);
7488 return NULL;
7492 * returns range - domain
7494 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7496 int i;
7497 isl_space *dim;
7498 struct isl_set *result;
7500 if (!map)
7501 return NULL;
7503 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7504 map->dim, isl_dim_out),
7505 goto error);
7506 dim = isl_map_get_space(map);
7507 dim = isl_space_domain(dim);
7508 result = isl_set_alloc_space(dim, map->n, 0);
7509 if (!result)
7510 goto error;
7511 for (i = 0; i < map->n; ++i)
7512 result = isl_set_add_basic_set(result,
7513 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7514 isl_map_free(map);
7515 return result;
7516 error:
7517 isl_map_free(map);
7518 return NULL;
7522 * returns [domain -> range] -> range - domain
7524 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7525 __isl_take isl_basic_map *bmap)
7527 int i, k;
7528 isl_space *dim;
7529 isl_basic_map *domain;
7530 int nparam, n;
7531 unsigned total;
7533 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7534 bmap->dim, isl_dim_out))
7535 isl_die(bmap->ctx, isl_error_invalid,
7536 "domain and range don't match", goto error);
7538 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7539 n = isl_basic_map_dim(bmap, isl_dim_in);
7541 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7542 domain = isl_basic_map_universe(dim);
7544 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7545 bmap = isl_basic_map_apply_range(bmap, domain);
7546 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7548 total = isl_basic_map_total_dim(bmap);
7550 for (i = 0; i < n; ++i) {
7551 k = isl_basic_map_alloc_equality(bmap);
7552 if (k < 0)
7553 goto error;
7554 isl_seq_clr(bmap->eq[k], 1 + total);
7555 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7556 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7557 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7560 bmap = isl_basic_map_gauss(bmap, NULL);
7561 return isl_basic_map_finalize(bmap);
7562 error:
7563 isl_basic_map_free(bmap);
7564 return NULL;
7568 * returns [domain -> range] -> range - domain
7570 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7572 int i;
7573 isl_space *domain_dim;
7575 if (!map)
7576 return NULL;
7578 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7579 map->dim, isl_dim_out))
7580 isl_die(map->ctx, isl_error_invalid,
7581 "domain and range don't match", goto error);
7583 map = isl_map_cow(map);
7584 if (!map)
7585 return NULL;
7587 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7588 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7589 map->dim = isl_space_join(map->dim, domain_dim);
7590 if (!map->dim)
7591 goto error;
7592 for (i = 0; i < map->n; ++i) {
7593 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7594 if (!map->p[i])
7595 goto error;
7597 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7598 return map;
7599 error:
7600 isl_map_free(map);
7601 return NULL;
7604 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7606 struct isl_basic_map *bmap;
7607 unsigned nparam;
7608 unsigned dim;
7609 int i;
7611 if (!dims)
7612 return NULL;
7614 nparam = dims->nparam;
7615 dim = dims->n_out;
7616 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7617 if (!bmap)
7618 goto error;
7620 for (i = 0; i < dim; ++i) {
7621 int j = isl_basic_map_alloc_equality(bmap);
7622 if (j < 0)
7623 goto error;
7624 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7625 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7626 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7628 return isl_basic_map_finalize(bmap);
7629 error:
7630 isl_basic_map_free(bmap);
7631 return NULL;
7634 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7636 if (!dim)
7637 return NULL;
7638 if (dim->n_in != dim->n_out)
7639 isl_die(dim->ctx, isl_error_invalid,
7640 "number of input and output dimensions needs to be "
7641 "the same", goto error);
7642 return basic_map_identity(dim);
7643 error:
7644 isl_space_free(dim);
7645 return NULL;
7648 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7650 if (!model || !model->dim)
7651 return NULL;
7652 return isl_basic_map_identity(isl_space_copy(model->dim));
7655 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7657 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7660 struct isl_map *isl_map_identity_like(struct isl_map *model)
7662 if (!model || !model->dim)
7663 return NULL;
7664 return isl_map_identity(isl_space_copy(model->dim));
7667 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7669 if (!model || !model->dim)
7670 return NULL;
7671 return isl_map_identity(isl_space_copy(model->dim));
7674 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7676 isl_space *dim = isl_set_get_space(set);
7677 isl_map *id;
7678 id = isl_map_identity(isl_space_map_from_set(dim));
7679 return isl_map_intersect_range(id, set);
7682 /* Construct a basic set with all set dimensions having only non-negative
7683 * values.
7685 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7686 __isl_take isl_space *space)
7688 int i;
7689 unsigned nparam;
7690 unsigned dim;
7691 struct isl_basic_set *bset;
7693 if (!space)
7694 return NULL;
7695 nparam = space->nparam;
7696 dim = space->n_out;
7697 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7698 if (!bset)
7699 return NULL;
7700 for (i = 0; i < dim; ++i) {
7701 int k = isl_basic_set_alloc_inequality(bset);
7702 if (k < 0)
7703 goto error;
7704 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7705 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7707 return bset;
7708 error:
7709 isl_basic_set_free(bset);
7710 return NULL;
7713 /* Construct the half-space x_pos >= 0.
7715 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7716 int pos)
7718 int k;
7719 isl_basic_set *nonneg;
7721 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7722 k = isl_basic_set_alloc_inequality(nonneg);
7723 if (k < 0)
7724 goto error;
7725 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7726 isl_int_set_si(nonneg->ineq[k][pos], 1);
7728 return isl_basic_set_finalize(nonneg);
7729 error:
7730 isl_basic_set_free(nonneg);
7731 return NULL;
7734 /* Construct the half-space x_pos <= -1.
7736 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7738 int k;
7739 isl_basic_set *neg;
7741 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7742 k = isl_basic_set_alloc_inequality(neg);
7743 if (k < 0)
7744 goto error;
7745 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7746 isl_int_set_si(neg->ineq[k][0], -1);
7747 isl_int_set_si(neg->ineq[k][pos], -1);
7749 return isl_basic_set_finalize(neg);
7750 error:
7751 isl_basic_set_free(neg);
7752 return NULL;
7755 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7756 enum isl_dim_type type, unsigned first, unsigned n)
7758 int i;
7759 isl_basic_set *nonneg;
7760 isl_basic_set *neg;
7762 if (!set)
7763 return NULL;
7764 if (n == 0)
7765 return set;
7767 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7769 for (i = 0; i < n; ++i) {
7770 nonneg = nonneg_halfspace(isl_set_get_space(set),
7771 pos(set->dim, type) + first + i);
7772 neg = neg_halfspace(isl_set_get_space(set),
7773 pos(set->dim, type) + first + i);
7775 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7778 return set;
7779 error:
7780 isl_set_free(set);
7781 return NULL;
7784 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7785 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7786 void *user)
7788 isl_set *half;
7790 if (!set)
7791 return -1;
7792 if (isl_set_plain_is_empty(set)) {
7793 isl_set_free(set);
7794 return 0;
7796 if (first == len)
7797 return fn(set, signs, user);
7799 signs[first] = 1;
7800 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7801 1 + first));
7802 half = isl_set_intersect(half, isl_set_copy(set));
7803 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7804 goto error;
7806 signs[first] = -1;
7807 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7808 1 + first));
7809 half = isl_set_intersect(half, set);
7810 return foreach_orthant(half, signs, first + 1, len, fn, user);
7811 error:
7812 isl_set_free(set);
7813 return -1;
7816 /* Call "fn" on the intersections of "set" with each of the orthants
7817 * (except for obviously empty intersections). The orthant is identified
7818 * by the signs array, with each entry having value 1 or -1 according
7819 * to the sign of the corresponding variable.
7821 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7822 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7823 void *user)
7825 unsigned nparam;
7826 unsigned nvar;
7827 int *signs;
7828 int r;
7830 if (!set)
7831 return -1;
7832 if (isl_set_plain_is_empty(set))
7833 return 0;
7835 nparam = isl_set_dim(set, isl_dim_param);
7836 nvar = isl_set_dim(set, isl_dim_set);
7838 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7840 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7841 fn, user);
7843 free(signs);
7845 return r;
7848 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7850 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7853 int isl_basic_map_is_subset(
7854 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7856 int is_subset;
7857 struct isl_map *map1;
7858 struct isl_map *map2;
7860 if (!bmap1 || !bmap2)
7861 return -1;
7863 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7864 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7866 is_subset = isl_map_is_subset(map1, map2);
7868 isl_map_free(map1);
7869 isl_map_free(map2);
7871 return is_subset;
7874 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7875 __isl_keep isl_basic_set *bset2)
7877 return isl_basic_map_is_subset(bset1, bset2);
7880 int isl_basic_map_is_equal(
7881 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7883 int is_subset;
7885 if (!bmap1 || !bmap2)
7886 return -1;
7887 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7888 if (is_subset != 1)
7889 return is_subset;
7890 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7891 return is_subset;
7894 int isl_basic_set_is_equal(
7895 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7897 return isl_basic_map_is_equal(
7898 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7901 int isl_map_is_empty(struct isl_map *map)
7903 int i;
7904 int is_empty;
7906 if (!map)
7907 return -1;
7908 for (i = 0; i < map->n; ++i) {
7909 is_empty = isl_basic_map_is_empty(map->p[i]);
7910 if (is_empty < 0)
7911 return -1;
7912 if (!is_empty)
7913 return 0;
7915 return 1;
7918 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7920 return map ? map->n == 0 : -1;
7923 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7925 return isl_map_plain_is_empty(map);
7928 int isl_set_plain_is_empty(struct isl_set *set)
7930 return set ? set->n == 0 : -1;
7933 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7935 return isl_set_plain_is_empty(set);
7938 int isl_set_is_empty(struct isl_set *set)
7940 return isl_map_is_empty((struct isl_map *)set);
7943 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7945 if (!map1 || !map2)
7946 return -1;
7948 return isl_space_is_equal(map1->dim, map2->dim);
7951 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7953 if (!set1 || !set2)
7954 return -1;
7956 return isl_space_is_equal(set1->dim, set2->dim);
7959 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7961 int is_subset;
7963 if (!map1 || !map2)
7964 return -1;
7965 is_subset = isl_map_is_subset(map1, map2);
7966 if (is_subset != 1)
7967 return is_subset;
7968 is_subset = isl_map_is_subset(map2, map1);
7969 return is_subset;
7972 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7974 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7977 int isl_basic_map_is_strict_subset(
7978 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7980 int is_subset;
7982 if (!bmap1 || !bmap2)
7983 return -1;
7984 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7985 if (is_subset != 1)
7986 return is_subset;
7987 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7988 if (is_subset == -1)
7989 return is_subset;
7990 return !is_subset;
7993 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7995 int is_subset;
7997 if (!map1 || !map2)
7998 return -1;
7999 is_subset = isl_map_is_subset(map1, map2);
8000 if (is_subset != 1)
8001 return is_subset;
8002 is_subset = isl_map_is_subset(map2, map1);
8003 if (is_subset == -1)
8004 return is_subset;
8005 return !is_subset;
8008 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8010 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
8013 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
8015 if (!bmap)
8016 return -1;
8017 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8020 int isl_basic_set_is_universe(struct isl_basic_set *bset)
8022 if (!bset)
8023 return -1;
8024 return bset->n_eq == 0 && bset->n_ineq == 0;
8027 int isl_map_plain_is_universe(__isl_keep isl_map *map)
8029 int i;
8031 if (!map)
8032 return -1;
8034 for (i = 0; i < map->n; ++i) {
8035 int r = isl_basic_map_is_universe(map->p[i]);
8036 if (r < 0 || r)
8037 return r;
8040 return 0;
8043 int isl_set_plain_is_universe(__isl_keep isl_set *set)
8045 return isl_map_plain_is_universe((isl_map *) set);
8048 int isl_set_fast_is_universe(__isl_keep isl_set *set)
8050 return isl_set_plain_is_universe(set);
8053 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
8055 struct isl_basic_set *bset = NULL;
8056 struct isl_vec *sample = NULL;
8057 int empty;
8058 unsigned total;
8060 if (!bmap)
8061 return -1;
8063 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8064 return 1;
8066 if (isl_basic_map_is_universe(bmap))
8067 return 0;
8069 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8070 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8071 copy = isl_basic_map_remove_redundancies(copy);
8072 empty = isl_basic_map_plain_is_empty(copy);
8073 isl_basic_map_free(copy);
8074 return empty;
8077 total = 1 + isl_basic_map_total_dim(bmap);
8078 if (bmap->sample && bmap->sample->size == total) {
8079 int contains = isl_basic_map_contains(bmap, bmap->sample);
8080 if (contains < 0)
8081 return -1;
8082 if (contains)
8083 return 0;
8085 isl_vec_free(bmap->sample);
8086 bmap->sample = NULL;
8087 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8088 if (!bset)
8089 return -1;
8090 sample = isl_basic_set_sample_vec(bset);
8091 if (!sample)
8092 return -1;
8093 empty = sample->size == 0;
8094 isl_vec_free(bmap->sample);
8095 bmap->sample = sample;
8096 if (empty)
8097 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8099 return empty;
8102 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8104 if (!bmap)
8105 return -1;
8106 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8109 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
8111 return isl_basic_map_plain_is_empty(bmap);
8114 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8116 if (!bset)
8117 return -1;
8118 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8121 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
8123 return isl_basic_set_plain_is_empty(bset);
8126 int isl_basic_set_is_empty(struct isl_basic_set *bset)
8128 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8131 struct isl_map *isl_basic_map_union(
8132 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8134 struct isl_map *map;
8135 if (!bmap1 || !bmap2)
8136 goto error;
8138 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8140 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8141 if (!map)
8142 goto error;
8143 map = isl_map_add_basic_map(map, bmap1);
8144 map = isl_map_add_basic_map(map, bmap2);
8145 return map;
8146 error:
8147 isl_basic_map_free(bmap1);
8148 isl_basic_map_free(bmap2);
8149 return NULL;
8152 struct isl_set *isl_basic_set_union(
8153 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8155 return (struct isl_set *)isl_basic_map_union(
8156 (struct isl_basic_map *)bset1,
8157 (struct isl_basic_map *)bset2);
8160 /* Order divs such that any div only depends on previous divs */
8161 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8163 int i;
8164 unsigned off;
8166 if (!bmap)
8167 return NULL;
8169 off = isl_space_dim(bmap->dim, isl_dim_all);
8171 for (i = 0; i < bmap->n_div; ++i) {
8172 int pos;
8173 if (isl_int_is_zero(bmap->div[i][0]))
8174 continue;
8175 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8176 bmap->n_div-i);
8177 if (pos == -1)
8178 continue;
8179 isl_basic_map_swap_div(bmap, i, i + pos);
8180 --i;
8182 return bmap;
8185 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8187 return (struct isl_basic_set *)
8188 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8191 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8193 int i;
8195 if (!map)
8196 return 0;
8198 for (i = 0; i < map->n; ++i) {
8199 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8200 if (!map->p[i])
8201 goto error;
8204 return map;
8205 error:
8206 isl_map_free(map);
8207 return NULL;
8210 /* Apply the expansion computed by isl_merge_divs.
8211 * The expansion itself is given by "exp" while the resulting
8212 * list of divs is given by "div".
8214 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8215 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8217 int i, j;
8218 int n_div;
8220 bset = isl_basic_set_cow(bset);
8221 if (!bset || !div)
8222 goto error;
8224 if (div->n_row < bset->n_div)
8225 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8226 "not an expansion", goto error);
8228 n_div = bset->n_div;
8229 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8230 div->n_row - n_div, 0,
8231 2 * (div->n_row - n_div));
8233 for (i = n_div; i < div->n_row; ++i)
8234 if (isl_basic_set_alloc_div(bset) < 0)
8235 goto error;
8237 j = n_div - 1;
8238 for (i = div->n_row - 1; i >= 0; --i) {
8239 if (j >= 0 && exp[j] == i) {
8240 if (i != j)
8241 isl_basic_map_swap_div(bset, i, j);
8242 j--;
8243 } else {
8244 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8245 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8246 goto error;
8250 isl_mat_free(div);
8251 return bset;
8252 error:
8253 isl_basic_set_free(bset);
8254 isl_mat_free(div);
8255 return NULL;
8258 /* Look for a div in dst that corresponds to the div "div" in src.
8259 * The divs before "div" in src and dst are assumed to be the same.
8261 * Returns -1 if no corresponding div was found and the position
8262 * of the corresponding div in dst otherwise.
8264 static int find_div(struct isl_basic_map *dst,
8265 struct isl_basic_map *src, unsigned div)
8267 int i;
8269 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8271 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8272 for (i = div; i < dst->n_div; ++i)
8273 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8274 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8275 dst->n_div - div) == -1)
8276 return i;
8277 return -1;
8280 /* Align the divs of "dst" to those of "src", adding divs from "src"
8281 * if needed. That is, make sure that the first src->n_div divs
8282 * of the result are equal to those of src.
8284 * The result is not finalized as by design it will have redundant
8285 * divs if any divs from "src" were copied.
8287 __isl_give isl_basic_map *isl_basic_map_align_divs(
8288 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8290 int i;
8291 int known, extended;
8292 unsigned total;
8294 if (!dst || !src)
8295 return isl_basic_map_free(dst);
8297 if (src->n_div == 0)
8298 return dst;
8300 known = isl_basic_map_divs_known(src);
8301 if (known < 0)
8302 return isl_basic_map_free(dst);
8303 if (!known)
8304 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8305 "some src divs are unknown",
8306 return isl_basic_map_free(dst));
8308 src = isl_basic_map_order_divs(src);
8310 extended = 0;
8311 total = isl_space_dim(src->dim, isl_dim_all);
8312 for (i = 0; i < src->n_div; ++i) {
8313 int j = find_div(dst, src, i);
8314 if (j < 0) {
8315 if (!extended) {
8316 int extra = src->n_div - i;
8317 dst = isl_basic_map_cow(dst);
8318 if (!dst)
8319 return NULL;
8320 dst = isl_basic_map_extend_space(dst,
8321 isl_space_copy(dst->dim),
8322 extra, 0, 2 * extra);
8323 extended = 1;
8325 j = isl_basic_map_alloc_div(dst);
8326 if (j < 0)
8327 return isl_basic_map_free(dst);
8328 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8329 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8330 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8331 return isl_basic_map_free(dst);
8333 if (j != i)
8334 isl_basic_map_swap_div(dst, i, j);
8336 return dst;
8339 struct isl_basic_set *isl_basic_set_align_divs(
8340 struct isl_basic_set *dst, struct isl_basic_set *src)
8342 return (struct isl_basic_set *)isl_basic_map_align_divs(
8343 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8346 struct isl_map *isl_map_align_divs(struct isl_map *map)
8348 int i;
8350 if (!map)
8351 return NULL;
8352 if (map->n == 0)
8353 return map;
8354 map = isl_map_compute_divs(map);
8355 map = isl_map_cow(map);
8356 if (!map)
8357 return NULL;
8359 for (i = 1; i < map->n; ++i)
8360 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8361 for (i = 1; i < map->n; ++i) {
8362 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8363 if (!map->p[i])
8364 return isl_map_free(map);
8367 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8368 return map;
8371 struct isl_set *isl_set_align_divs(struct isl_set *set)
8373 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8376 /* Align the divs of the basic sets in "set" to those
8377 * of the basic sets in "list", as well as to the other basic sets in "set".
8378 * The elements in "list" are assumed to have known divs.
8380 __isl_give isl_set *isl_set_align_divs_to_basic_set_list(
8381 __isl_take isl_set *set, __isl_keep isl_basic_set_list *list)
8383 int i, n;
8385 set = isl_set_compute_divs(set);
8386 set = isl_set_cow(set);
8387 if (!set || !list)
8388 return isl_set_free(set);
8389 if (set->n == 0)
8390 return set;
8392 n = isl_basic_set_list_n_basic_set(list);
8393 for (i = 0; i < n; ++i) {
8394 isl_basic_set *bset;
8396 bset = isl_basic_set_list_get_basic_set(list, i);
8397 set->p[0] = isl_basic_set_align_divs(set->p[0], bset);
8398 isl_basic_set_free(bset);
8400 if (!set->p[0])
8401 return isl_set_free(set);
8403 return isl_set_align_divs(set);
8406 /* Align the divs of each element of "list" to those of "bset".
8407 * Both "bset" and the elements of "list" are assumed to have known divs.
8409 __isl_give isl_basic_set_list *isl_basic_set_list_align_divs_to_basic_set(
8410 __isl_take isl_basic_set_list *list, __isl_keep isl_basic_set *bset)
8412 int i, n;
8414 if (!list || !bset)
8415 return isl_basic_set_list_free(list);
8417 n = isl_basic_set_list_n_basic_set(list);
8418 for (i = 0; i < n; ++i) {
8419 isl_basic_set *bset_i;
8421 bset_i = isl_basic_set_list_get_basic_set(list, i);
8422 bset_i = isl_basic_set_align_divs(bset_i, bset);
8423 list = isl_basic_set_list_set_basic_set(list, i, bset_i);
8426 return list;
8429 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8430 __isl_take isl_map *map)
8432 if (!set || !map)
8433 goto error;
8434 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8435 map = isl_map_intersect_domain(map, set);
8436 set = isl_map_range(map);
8437 return set;
8438 error:
8439 isl_set_free(set);
8440 isl_map_free(map);
8441 return NULL;
8444 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8445 __isl_take isl_map *map)
8447 return isl_map_align_params_map_map_and(set, map, &set_apply);
8450 /* There is no need to cow as removing empty parts doesn't change
8451 * the meaning of the set.
8453 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8455 int i;
8457 if (!map)
8458 return NULL;
8460 for (i = map->n - 1; i >= 0; --i)
8461 remove_if_empty(map, i);
8463 return map;
8466 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8468 return (struct isl_set *)
8469 isl_map_remove_empty_parts((struct isl_map *)set);
8472 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8474 struct isl_basic_map *bmap;
8475 if (!map || map->n == 0)
8476 return NULL;
8477 bmap = map->p[map->n-1];
8478 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8479 return isl_basic_map_copy(bmap);
8482 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8484 return (struct isl_basic_set *)
8485 isl_map_copy_basic_map((struct isl_map *)set);
8488 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8489 __isl_keep isl_basic_map *bmap)
8491 int i;
8493 if (!map || !bmap)
8494 goto error;
8495 for (i = map->n-1; i >= 0; --i) {
8496 if (map->p[i] != bmap)
8497 continue;
8498 map = isl_map_cow(map);
8499 if (!map)
8500 goto error;
8501 isl_basic_map_free(map->p[i]);
8502 if (i != map->n-1) {
8503 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8504 map->p[i] = map->p[map->n-1];
8506 map->n--;
8507 return map;
8509 return map;
8510 error:
8511 isl_map_free(map);
8512 return NULL;
8515 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8516 struct isl_basic_set *bset)
8518 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8519 (struct isl_basic_map *)bset);
8522 /* Given two basic sets bset1 and bset2, compute the maximal difference
8523 * between the values of dimension pos in bset1 and those in bset2
8524 * for any common value of the parameters and dimensions preceding pos.
8526 static enum isl_lp_result basic_set_maximal_difference_at(
8527 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8528 int pos, isl_int *opt)
8530 isl_space *dims;
8531 struct isl_basic_map *bmap1 = NULL;
8532 struct isl_basic_map *bmap2 = NULL;
8533 struct isl_ctx *ctx;
8534 struct isl_vec *obj;
8535 unsigned total;
8536 unsigned nparam;
8537 unsigned dim1, dim2;
8538 enum isl_lp_result res;
8540 if (!bset1 || !bset2)
8541 return isl_lp_error;
8543 nparam = isl_basic_set_n_param(bset1);
8544 dim1 = isl_basic_set_n_dim(bset1);
8545 dim2 = isl_basic_set_n_dim(bset2);
8546 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8547 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8548 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8549 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8550 if (!bmap1 || !bmap2)
8551 goto error;
8552 bmap1 = isl_basic_map_cow(bmap1);
8553 bmap1 = isl_basic_map_extend(bmap1, nparam,
8554 pos, (dim1 - pos) + (dim2 - pos),
8555 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8556 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8557 if (!bmap1)
8558 goto error2;
8559 total = isl_basic_map_total_dim(bmap1);
8560 ctx = bmap1->ctx;
8561 obj = isl_vec_alloc(ctx, 1 + total);
8562 if (!obj)
8563 goto error2;
8564 isl_seq_clr(obj->block.data, 1 + total);
8565 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8566 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8567 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8568 opt, NULL, NULL);
8569 isl_basic_map_free(bmap1);
8570 isl_vec_free(obj);
8571 return res;
8572 error:
8573 isl_basic_map_free(bmap2);
8574 error2:
8575 isl_basic_map_free(bmap1);
8576 return isl_lp_error;
8579 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8580 * for any common value of the parameters and dimensions preceding pos
8581 * in both basic sets, the values of dimension pos in bset1 are
8582 * smaller or larger than those in bset2.
8584 * Returns
8585 * 1 if bset1 follows bset2
8586 * -1 if bset1 precedes bset2
8587 * 0 if bset1 and bset2 are incomparable
8588 * -2 if some error occurred.
8590 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8591 struct isl_basic_set *bset2, int pos)
8593 isl_int opt;
8594 enum isl_lp_result res;
8595 int cmp;
8597 isl_int_init(opt);
8599 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8601 if (res == isl_lp_empty)
8602 cmp = 0;
8603 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8604 res == isl_lp_unbounded)
8605 cmp = 1;
8606 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8607 cmp = -1;
8608 else
8609 cmp = -2;
8611 isl_int_clear(opt);
8612 return cmp;
8615 /* Given two basic sets bset1 and bset2, check whether
8616 * for any common value of the parameters and dimensions preceding pos
8617 * there is a value of dimension pos in bset1 that is larger
8618 * than a value of the same dimension in bset2.
8620 * Return
8621 * 1 if there exists such a pair
8622 * 0 if there is no such pair, but there is a pair of equal values
8623 * -1 otherwise
8624 * -2 if some error occurred.
8626 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8627 __isl_keep isl_basic_set *bset2, int pos)
8629 isl_int opt;
8630 enum isl_lp_result res;
8631 int cmp;
8633 isl_int_init(opt);
8635 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8637 if (res == isl_lp_empty)
8638 cmp = -1;
8639 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8640 res == isl_lp_unbounded)
8641 cmp = 1;
8642 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8643 cmp = -1;
8644 else if (res == isl_lp_ok)
8645 cmp = 0;
8646 else
8647 cmp = -2;
8649 isl_int_clear(opt);
8650 return cmp;
8653 /* Given two sets set1 and set2, check whether
8654 * for any common value of the parameters and dimensions preceding pos
8655 * there is a value of dimension pos in set1 that is larger
8656 * than a value of the same dimension in set2.
8658 * Return
8659 * 1 if there exists such a pair
8660 * 0 if there is no such pair, but there is a pair of equal values
8661 * -1 otherwise
8662 * -2 if some error occurred.
8664 int isl_set_follows_at(__isl_keep isl_set *set1,
8665 __isl_keep isl_set *set2, int pos)
8667 int i, j;
8668 int follows = -1;
8670 if (!set1 || !set2)
8671 return -2;
8673 for (i = 0; i < set1->n; ++i)
8674 for (j = 0; j < set2->n; ++j) {
8675 int f;
8676 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8677 if (f == 1 || f == -2)
8678 return f;
8679 if (f > follows)
8680 follows = f;
8683 return follows;
8686 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8687 unsigned pos, isl_int *val)
8689 int i;
8690 int d;
8691 unsigned total;
8693 if (!bmap)
8694 return -1;
8695 total = isl_basic_map_total_dim(bmap);
8696 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8697 for (; d+1 > pos; --d)
8698 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8699 break;
8700 if (d != pos)
8701 continue;
8702 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8703 return 0;
8704 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8705 return 0;
8706 if (!isl_int_is_one(bmap->eq[i][1+d]))
8707 return 0;
8708 if (val)
8709 isl_int_neg(*val, bmap->eq[i][0]);
8710 return 1;
8712 return 0;
8715 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8716 unsigned pos, isl_int *val)
8718 int i;
8719 isl_int v;
8720 isl_int tmp;
8721 int fixed;
8723 if (!map)
8724 return -1;
8725 if (map->n == 0)
8726 return 0;
8727 if (map->n == 1)
8728 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8729 isl_int_init(v);
8730 isl_int_init(tmp);
8731 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8732 for (i = 1; fixed == 1 && i < map->n; ++i) {
8733 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8734 if (fixed == 1 && isl_int_ne(tmp, v))
8735 fixed = 0;
8737 if (val)
8738 isl_int_set(*val, v);
8739 isl_int_clear(tmp);
8740 isl_int_clear(v);
8741 return fixed;
8744 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8745 unsigned pos, isl_int *val)
8747 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8748 pos, val);
8751 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8752 isl_int *val)
8754 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8757 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8758 enum isl_dim_type type, unsigned pos, isl_int *val)
8760 if (pos >= isl_basic_map_dim(bmap, type))
8761 return -1;
8762 return isl_basic_map_plain_has_fixed_var(bmap,
8763 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8766 /* If "bmap" obviously lies on a hyperplane where the given dimension
8767 * has a fixed value, then return that value.
8768 * Otherwise return NaN.
8770 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8771 __isl_keep isl_basic_map *bmap,
8772 enum isl_dim_type type, unsigned pos)
8774 isl_ctx *ctx;
8775 isl_val *v;
8776 int fixed;
8778 if (!bmap)
8779 return NULL;
8780 ctx = isl_basic_map_get_ctx(bmap);
8781 v = isl_val_alloc(ctx);
8782 if (!v)
8783 return NULL;
8784 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8785 if (fixed < 0)
8786 return isl_val_free(v);
8787 if (fixed) {
8788 isl_int_set_si(v->d, 1);
8789 return v;
8791 isl_val_free(v);
8792 return isl_val_nan(ctx);
8795 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8796 enum isl_dim_type type, unsigned pos, isl_int *val)
8798 if (pos >= isl_map_dim(map, type))
8799 return -1;
8800 return isl_map_plain_has_fixed_var(map,
8801 map_offset(map, type) - 1 + pos, val);
8804 /* If "map" obviously lies on a hyperplane where the given dimension
8805 * has a fixed value, then return that value.
8806 * Otherwise return NaN.
8808 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8809 enum isl_dim_type type, unsigned pos)
8811 isl_ctx *ctx;
8812 isl_val *v;
8813 int fixed;
8815 if (!map)
8816 return NULL;
8817 ctx = isl_map_get_ctx(map);
8818 v = isl_val_alloc(ctx);
8819 if (!v)
8820 return NULL;
8821 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8822 if (fixed < 0)
8823 return isl_val_free(v);
8824 if (fixed) {
8825 isl_int_set_si(v->d, 1);
8826 return v;
8828 isl_val_free(v);
8829 return isl_val_nan(ctx);
8832 /* If "set" obviously lies on a hyperplane where the given dimension
8833 * has a fixed value, then return that value.
8834 * Otherwise return NaN.
8836 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8837 enum isl_dim_type type, unsigned pos)
8839 return isl_map_plain_get_val_if_fixed(set, type, pos);
8842 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8843 enum isl_dim_type type, unsigned pos, isl_int *val)
8845 return isl_map_plain_is_fixed(set, type, pos, val);
8848 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8849 enum isl_dim_type type, unsigned pos, isl_int *val)
8851 return isl_map_plain_is_fixed(map, type, pos, val);
8854 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8855 * then return this fixed value in *val.
8857 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8858 unsigned dim, isl_int *val)
8860 return isl_basic_set_plain_has_fixed_var(bset,
8861 isl_basic_set_n_param(bset) + dim, val);
8864 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8865 * then return this fixed value in *val.
8867 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8868 unsigned dim, isl_int *val)
8870 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8873 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8874 unsigned dim, isl_int *val)
8876 return isl_set_plain_dim_is_fixed(set, dim, val);
8879 /* Check if input variable in has fixed value and if so and if val is not NULL,
8880 * then return this fixed value in *val.
8882 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8883 unsigned in, isl_int *val)
8885 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8888 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8889 * and if val is not NULL, then return this lower bound in *val.
8891 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8892 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8894 int i, i_eq = -1, i_ineq = -1;
8895 isl_int *c;
8896 unsigned total;
8897 unsigned nparam;
8899 if (!bset)
8900 return -1;
8901 total = isl_basic_set_total_dim(bset);
8902 nparam = isl_basic_set_n_param(bset);
8903 for (i = 0; i < bset->n_eq; ++i) {
8904 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8905 continue;
8906 if (i_eq != -1)
8907 return 0;
8908 i_eq = i;
8910 for (i = 0; i < bset->n_ineq; ++i) {
8911 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8912 continue;
8913 if (i_eq != -1 || i_ineq != -1)
8914 return 0;
8915 i_ineq = i;
8917 if (i_eq == -1 && i_ineq == -1)
8918 return 0;
8919 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8920 /* The coefficient should always be one due to normalization. */
8921 if (!isl_int_is_one(c[1+nparam+dim]))
8922 return 0;
8923 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8924 return 0;
8925 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8926 total - nparam - dim - 1) != -1)
8927 return 0;
8928 if (val)
8929 isl_int_neg(*val, c[0]);
8930 return 1;
8933 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8934 unsigned dim, isl_int *val)
8936 int i;
8937 isl_int v;
8938 isl_int tmp;
8939 int fixed;
8941 if (!set)
8942 return -1;
8943 if (set->n == 0)
8944 return 0;
8945 if (set->n == 1)
8946 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8947 dim, val);
8948 isl_int_init(v);
8949 isl_int_init(tmp);
8950 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8951 dim, &v);
8952 for (i = 1; fixed == 1 && i < set->n; ++i) {
8953 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8954 dim, &tmp);
8955 if (fixed == 1 && isl_int_ne(tmp, v))
8956 fixed = 0;
8958 if (val)
8959 isl_int_set(*val, v);
8960 isl_int_clear(tmp);
8961 isl_int_clear(v);
8962 return fixed;
8965 /* uset_gist depends on constraints without existentially quantified
8966 * variables sorting first.
8968 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8970 isl_int **c1 = (isl_int **) p1;
8971 isl_int **c2 = (isl_int **) p2;
8972 int l1, l2;
8973 unsigned size = *(unsigned *) arg;
8975 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8976 l2 = isl_seq_last_non_zero(*c2 + 1, size);
8978 if (l1 != l2)
8979 return l1 - l2;
8981 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
8984 static struct isl_basic_map *isl_basic_map_sort_constraints(
8985 struct isl_basic_map *bmap)
8987 unsigned total;
8989 if (!bmap)
8990 return NULL;
8991 if (bmap->n_ineq == 0)
8992 return bmap;
8993 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8994 return bmap;
8995 total = isl_basic_map_total_dim(bmap);
8996 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
8997 &sort_constraint_cmp, &total) < 0)
8998 return isl_basic_map_free(bmap);
8999 return bmap;
9002 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9003 __isl_take isl_basic_set *bset)
9005 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9006 (struct isl_basic_map *)bset);
9009 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9011 if (!bmap)
9012 return NULL;
9013 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9014 return bmap;
9015 bmap = isl_basic_map_remove_redundancies(bmap);
9016 bmap = isl_basic_map_sort_constraints(bmap);
9017 if (bmap)
9018 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9019 return bmap;
9022 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9024 return (struct isl_basic_set *)isl_basic_map_normalize(
9025 (struct isl_basic_map *)bset);
9028 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9029 const __isl_keep isl_basic_map *bmap2)
9031 int i, cmp;
9032 unsigned total;
9034 if (bmap1 == bmap2)
9035 return 0;
9036 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9037 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9038 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9039 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9040 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9041 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9042 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9043 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9044 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9045 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9046 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9047 return 0;
9048 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9049 return 1;
9050 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9051 return -1;
9052 if (bmap1->n_eq != bmap2->n_eq)
9053 return bmap1->n_eq - bmap2->n_eq;
9054 if (bmap1->n_ineq != bmap2->n_ineq)
9055 return bmap1->n_ineq - bmap2->n_ineq;
9056 if (bmap1->n_div != bmap2->n_div)
9057 return bmap1->n_div - bmap2->n_div;
9058 total = isl_basic_map_total_dim(bmap1);
9059 for (i = 0; i < bmap1->n_eq; ++i) {
9060 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9061 if (cmp)
9062 return cmp;
9064 for (i = 0; i < bmap1->n_ineq; ++i) {
9065 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9066 if (cmp)
9067 return cmp;
9069 for (i = 0; i < bmap1->n_div; ++i) {
9070 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9071 if (cmp)
9072 return cmp;
9074 return 0;
9077 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9078 const __isl_keep isl_basic_set *bset2)
9080 return isl_basic_map_plain_cmp(bset1, bset2);
9083 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9085 int i, cmp;
9087 if (set1 == set2)
9088 return 0;
9089 if (set1->n != set2->n)
9090 return set1->n - set2->n;
9092 for (i = 0; i < set1->n; ++i) {
9093 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9094 if (cmp)
9095 return cmp;
9098 return 0;
9101 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9102 __isl_keep isl_basic_map *bmap2)
9104 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9107 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9108 __isl_keep isl_basic_set *bset2)
9110 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9111 (isl_basic_map *)bset2);
9114 static int qsort_bmap_cmp(const void *p1, const void *p2)
9116 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9117 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9119 return isl_basic_map_plain_cmp(bmap1, bmap2);
9122 /* Sort the basic maps of "map" and remove duplicate basic maps.
9124 * While removing basic maps, we make sure that the basic maps remain
9125 * sorted because isl_map_normalize expects the basic maps of the result
9126 * to be sorted.
9128 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9130 int i, j;
9132 map = isl_map_remove_empty_parts(map);
9133 if (!map)
9134 return NULL;
9135 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9136 for (i = map->n - 1; i >= 1; --i) {
9137 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9138 continue;
9139 isl_basic_map_free(map->p[i-1]);
9140 for (j = i; j < map->n; ++j)
9141 map->p[j - 1] = map->p[j];
9142 map->n--;
9145 return map;
9148 /* Remove obvious duplicates among the basic maps of "map".
9150 * Unlike isl_map_normalize, this function does not remove redundant
9151 * constraints and only removes duplicates that have exactly the same
9152 * constraints in the input. It does sort the constraints and
9153 * the basic maps to ease the detection of duplicates.
9155 * If "map" has already been normalized or if the basic maps are
9156 * disjoint, then there can be no duplicates.
9158 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9160 int i;
9161 isl_basic_map *bmap;
9163 if (!map)
9164 return NULL;
9165 if (map->n <= 1)
9166 return map;
9167 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9168 return map;
9169 for (i = 0; i < map->n; ++i) {
9170 bmap = isl_basic_map_copy(map->p[i]);
9171 bmap = isl_basic_map_sort_constraints(bmap);
9172 if (!bmap)
9173 return isl_map_free(map);
9174 isl_basic_map_free(map->p[i]);
9175 map->p[i] = bmap;
9178 map = sort_and_remove_duplicates(map);
9179 return map;
9182 /* We normalize in place, but if anything goes wrong we need
9183 * to return NULL, so we need to make sure we don't change the
9184 * meaning of any possible other copies of map.
9186 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9188 int i;
9189 struct isl_basic_map *bmap;
9191 if (!map)
9192 return NULL;
9193 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9194 return map;
9195 for (i = 0; i < map->n; ++i) {
9196 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9197 if (!bmap)
9198 goto error;
9199 isl_basic_map_free(map->p[i]);
9200 map->p[i] = bmap;
9203 map = sort_and_remove_duplicates(map);
9204 if (map)
9205 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9206 return map;
9207 error:
9208 isl_map_free(map);
9209 return NULL;
9212 struct isl_set *isl_set_normalize(struct isl_set *set)
9214 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9217 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9219 int i;
9220 int equal;
9222 if (!map1 || !map2)
9223 return -1;
9225 if (map1 == map2)
9226 return 1;
9227 if (!isl_space_is_equal(map1->dim, map2->dim))
9228 return 0;
9230 map1 = isl_map_copy(map1);
9231 map2 = isl_map_copy(map2);
9232 map1 = isl_map_normalize(map1);
9233 map2 = isl_map_normalize(map2);
9234 if (!map1 || !map2)
9235 goto error;
9236 equal = map1->n == map2->n;
9237 for (i = 0; equal && i < map1->n; ++i) {
9238 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9239 if (equal < 0)
9240 goto error;
9242 isl_map_free(map1);
9243 isl_map_free(map2);
9244 return equal;
9245 error:
9246 isl_map_free(map1);
9247 isl_map_free(map2);
9248 return -1;
9251 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9253 return isl_map_plain_is_equal(map1, map2);
9256 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9258 return isl_map_plain_is_equal((struct isl_map *)set1,
9259 (struct isl_map *)set2);
9262 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9264 return isl_set_plain_is_equal(set1, set2);
9267 /* Return an interval that ranges from min to max (inclusive)
9269 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9270 isl_int min, isl_int max)
9272 int k;
9273 struct isl_basic_set *bset = NULL;
9275 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9276 if (!bset)
9277 goto error;
9279 k = isl_basic_set_alloc_inequality(bset);
9280 if (k < 0)
9281 goto error;
9282 isl_int_set_si(bset->ineq[k][1], 1);
9283 isl_int_neg(bset->ineq[k][0], min);
9285 k = isl_basic_set_alloc_inequality(bset);
9286 if (k < 0)
9287 goto error;
9288 isl_int_set_si(bset->ineq[k][1], -1);
9289 isl_int_set(bset->ineq[k][0], max);
9291 return bset;
9292 error:
9293 isl_basic_set_free(bset);
9294 return NULL;
9297 /* Return the basic sets in "set" as a list.
9299 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
9300 __isl_keep isl_set *set)
9302 int i;
9303 isl_ctx *ctx;
9304 isl_basic_set_list *list;
9306 if (!set)
9307 return NULL;
9308 ctx = isl_set_get_ctx(set);
9309 list = isl_basic_set_list_alloc(ctx, set->n);
9311 for (i = 0; i < set->n; ++i) {
9312 isl_basic_set *bset;
9314 bset = isl_basic_set_copy(set->p[i]);
9315 list = isl_basic_set_list_add(list, bset);
9318 return list;
9321 /* Return the intersection of the elements in the non-empty list "list".
9322 * All elements are assumed to live in the same space.
9324 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9325 __isl_take struct isl_basic_set_list *list)
9327 int i, n;
9328 isl_basic_set *bset;
9330 if (!list)
9331 return NULL;
9332 n = isl_basic_set_list_n_basic_set(list);
9333 if (n < 1)
9334 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9335 "expecting non-empty list", goto error);
9337 bset = isl_basic_set_list_get_basic_set(list, 0);
9338 for (i = 1; i < n; ++i) {
9339 isl_basic_set *bset_i;
9341 bset_i = isl_basic_set_list_get_basic_set(list, i);
9342 bset = isl_basic_set_intersect(bset, bset_i);
9345 isl_basic_set_list_free(list);
9346 return bset;
9347 error:
9348 isl_basic_set_list_free(list);
9349 return NULL;
9352 /* Return the Cartesian product of the basic sets in list (in the given order).
9354 __isl_give isl_basic_set *isl_basic_set_list_product(
9355 __isl_take struct isl_basic_set_list *list)
9357 int i;
9358 unsigned dim;
9359 unsigned nparam;
9360 unsigned extra;
9361 unsigned n_eq;
9362 unsigned n_ineq;
9363 struct isl_basic_set *product = NULL;
9365 if (!list)
9366 goto error;
9367 isl_assert(list->ctx, list->n > 0, goto error);
9368 isl_assert(list->ctx, list->p[0], goto error);
9369 nparam = isl_basic_set_n_param(list->p[0]);
9370 dim = isl_basic_set_n_dim(list->p[0]);
9371 extra = list->p[0]->n_div;
9372 n_eq = list->p[0]->n_eq;
9373 n_ineq = list->p[0]->n_ineq;
9374 for (i = 1; i < list->n; ++i) {
9375 isl_assert(list->ctx, list->p[i], goto error);
9376 isl_assert(list->ctx,
9377 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9378 dim += isl_basic_set_n_dim(list->p[i]);
9379 extra += list->p[i]->n_div;
9380 n_eq += list->p[i]->n_eq;
9381 n_ineq += list->p[i]->n_ineq;
9383 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9384 n_eq, n_ineq);
9385 if (!product)
9386 goto error;
9387 dim = 0;
9388 for (i = 0; i < list->n; ++i) {
9389 isl_basic_set_add_constraints(product,
9390 isl_basic_set_copy(list->p[i]), dim);
9391 dim += isl_basic_set_n_dim(list->p[i]);
9393 isl_basic_set_list_free(list);
9394 return product;
9395 error:
9396 isl_basic_set_free(product);
9397 isl_basic_set_list_free(list);
9398 return NULL;
9401 struct isl_basic_map *isl_basic_map_product(
9402 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9404 isl_space *dim_result = NULL;
9405 struct isl_basic_map *bmap;
9406 unsigned in1, in2, out1, out2, nparam, total, pos;
9407 struct isl_dim_map *dim_map1, *dim_map2;
9409 if (!bmap1 || !bmap2)
9410 goto error;
9412 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9413 bmap2->dim, isl_dim_param), goto error);
9414 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9415 isl_space_copy(bmap2->dim));
9417 in1 = isl_basic_map_n_in(bmap1);
9418 in2 = isl_basic_map_n_in(bmap2);
9419 out1 = isl_basic_map_n_out(bmap1);
9420 out2 = isl_basic_map_n_out(bmap2);
9421 nparam = isl_basic_map_n_param(bmap1);
9423 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9424 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9425 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9426 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9427 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9428 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9429 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9430 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9431 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9432 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9433 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9435 bmap = isl_basic_map_alloc_space(dim_result,
9436 bmap1->n_div + bmap2->n_div,
9437 bmap1->n_eq + bmap2->n_eq,
9438 bmap1->n_ineq + bmap2->n_ineq);
9439 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9440 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9441 bmap = isl_basic_map_simplify(bmap);
9442 return isl_basic_map_finalize(bmap);
9443 error:
9444 isl_basic_map_free(bmap1);
9445 isl_basic_map_free(bmap2);
9446 return NULL;
9449 __isl_give isl_basic_map *isl_basic_map_flat_product(
9450 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9452 isl_basic_map *prod;
9454 prod = isl_basic_map_product(bmap1, bmap2);
9455 prod = isl_basic_map_flatten(prod);
9456 return prod;
9459 __isl_give isl_basic_set *isl_basic_set_flat_product(
9460 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9462 return isl_basic_map_flat_range_product(bset1, bset2);
9465 __isl_give isl_basic_map *isl_basic_map_domain_product(
9466 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9468 isl_space *space_result = NULL;
9469 isl_basic_map *bmap;
9470 unsigned in1, in2, out, nparam, total, pos;
9471 struct isl_dim_map *dim_map1, *dim_map2;
9473 if (!bmap1 || !bmap2)
9474 goto error;
9476 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9477 isl_space_copy(bmap2->dim));
9479 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9480 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9481 out = isl_basic_map_dim(bmap1, isl_dim_out);
9482 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9484 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9485 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9486 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9487 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9488 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9489 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9490 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9491 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9492 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9493 isl_dim_map_div(dim_map1, bmap1, pos += out);
9494 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9496 bmap = isl_basic_map_alloc_space(space_result,
9497 bmap1->n_div + bmap2->n_div,
9498 bmap1->n_eq + bmap2->n_eq,
9499 bmap1->n_ineq + bmap2->n_ineq);
9500 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9501 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9502 bmap = isl_basic_map_simplify(bmap);
9503 return isl_basic_map_finalize(bmap);
9504 error:
9505 isl_basic_map_free(bmap1);
9506 isl_basic_map_free(bmap2);
9507 return NULL;
9510 __isl_give isl_basic_map *isl_basic_map_range_product(
9511 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9513 isl_space *dim_result = NULL;
9514 isl_basic_map *bmap;
9515 unsigned in, out1, out2, nparam, total, pos;
9516 struct isl_dim_map *dim_map1, *dim_map2;
9518 if (!bmap1 || !bmap2)
9519 goto error;
9521 if (!isl_space_match(bmap1->dim, isl_dim_param,
9522 bmap2->dim, isl_dim_param))
9523 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9524 "parameters don't match", goto error);
9526 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9527 isl_space_copy(bmap2->dim));
9529 in = isl_basic_map_dim(bmap1, isl_dim_in);
9530 out1 = isl_basic_map_n_out(bmap1);
9531 out2 = isl_basic_map_n_out(bmap2);
9532 nparam = isl_basic_map_n_param(bmap1);
9534 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9535 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9536 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9537 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9538 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9539 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9540 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9541 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9542 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9543 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9544 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9546 bmap = isl_basic_map_alloc_space(dim_result,
9547 bmap1->n_div + bmap2->n_div,
9548 bmap1->n_eq + bmap2->n_eq,
9549 bmap1->n_ineq + bmap2->n_ineq);
9550 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9551 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9552 bmap = isl_basic_map_simplify(bmap);
9553 return isl_basic_map_finalize(bmap);
9554 error:
9555 isl_basic_map_free(bmap1);
9556 isl_basic_map_free(bmap2);
9557 return NULL;
9560 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9561 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9563 isl_basic_map *prod;
9565 prod = isl_basic_map_range_product(bmap1, bmap2);
9566 prod = isl_basic_map_flatten_range(prod);
9567 return prod;
9570 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9571 * and collect the results.
9572 * The result live in the space obtained by calling "space_product"
9573 * on the spaces of "map1" and "map2".
9574 * If "remove_duplicates" is set then the result may contain duplicates
9575 * (even if the inputs do not) and so we try and remove the obvious
9576 * duplicates.
9578 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9579 __isl_take isl_map *map2,
9580 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9581 __isl_take isl_space *right),
9582 __isl_give isl_basic_map *(*basic_map_product)(
9583 __isl_take isl_basic_map *left,
9584 __isl_take isl_basic_map *right),
9585 int remove_duplicates)
9587 unsigned flags = 0;
9588 struct isl_map *result;
9589 int i, j;
9591 if (!map1 || !map2)
9592 goto error;
9594 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9595 map2->dim, isl_dim_param), goto error);
9597 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9598 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9599 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9601 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9602 isl_space_copy(map2->dim)),
9603 map1->n * map2->n, flags);
9604 if (!result)
9605 goto error;
9606 for (i = 0; i < map1->n; ++i)
9607 for (j = 0; j < map2->n; ++j) {
9608 struct isl_basic_map *part;
9609 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9610 isl_basic_map_copy(map2->p[j]));
9611 if (isl_basic_map_is_empty(part))
9612 isl_basic_map_free(part);
9613 else
9614 result = isl_map_add_basic_map(result, part);
9615 if (!result)
9616 goto error;
9618 if (remove_duplicates)
9619 result = isl_map_remove_obvious_duplicates(result);
9620 isl_map_free(map1);
9621 isl_map_free(map2);
9622 return result;
9623 error:
9624 isl_map_free(map1);
9625 isl_map_free(map2);
9626 return NULL;
9629 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9631 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9632 __isl_take isl_map *map2)
9634 return map_product(map1, map2, &isl_space_product,
9635 &isl_basic_map_product, 0);
9638 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9639 __isl_take isl_map *map2)
9641 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9644 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9646 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9647 __isl_take isl_map *map2)
9649 isl_map *prod;
9651 prod = isl_map_product(map1, map2);
9652 prod = isl_map_flatten(prod);
9653 return prod;
9656 /* Given two set A and B, construct its Cartesian product A x B.
9658 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9660 return isl_map_range_product(set1, set2);
9663 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9664 __isl_take isl_set *set2)
9666 return isl_map_flat_range_product(set1, set2);
9669 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9671 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9672 __isl_take isl_map *map2)
9674 return map_product(map1, map2, &isl_space_domain_product,
9675 &isl_basic_map_domain_product, 1);
9678 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9680 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9681 __isl_take isl_map *map2)
9683 return map_product(map1, map2, &isl_space_range_product,
9684 &isl_basic_map_range_product, 1);
9687 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9688 __isl_take isl_map *map2)
9690 return isl_map_align_params_map_map_and(map1, map2,
9691 &map_domain_product_aligned);
9694 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9695 __isl_take isl_map *map2)
9697 return isl_map_align_params_map_map_and(map1, map2,
9698 &map_range_product_aligned);
9701 /* Given a map A -> [B -> C], extract the map A -> B.
9703 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9705 isl_space *space;
9706 int total, keep;
9708 if (!map)
9709 return NULL;
9710 if (!isl_space_range_is_wrapping(map->dim))
9711 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9712 "range is not a product", return isl_map_free(map));
9714 space = isl_map_get_space(map);
9715 total = isl_space_dim(space, isl_dim_out);
9716 space = isl_space_range_factor_domain(space);
9717 keep = isl_space_dim(space, isl_dim_out);
9718 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9719 map = isl_map_reset_space(map, space);
9721 return map;
9724 /* Given a map A -> [B -> C], extract the map A -> C.
9726 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9728 isl_space *space;
9729 int total, keep;
9731 if (!map)
9732 return NULL;
9733 if (!isl_space_range_is_wrapping(map->dim))
9734 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9735 "range is not a product", return isl_map_free(map));
9737 space = isl_map_get_space(map);
9738 total = isl_space_dim(space, isl_dim_out);
9739 space = isl_space_range_factor_range(space);
9740 keep = isl_space_dim(space, isl_dim_out);
9741 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9742 map = isl_map_reset_space(map, space);
9744 return map;
9747 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9749 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9750 __isl_take isl_map *map2)
9752 isl_map *prod;
9754 prod = isl_map_domain_product(map1, map2);
9755 prod = isl_map_flatten_domain(prod);
9756 return prod;
9759 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9761 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9762 __isl_take isl_map *map2)
9764 isl_map *prod;
9766 prod = isl_map_range_product(map1, map2);
9767 prod = isl_map_flatten_range(prod);
9768 return prod;
9771 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9773 int i;
9774 uint32_t hash = isl_hash_init();
9775 unsigned total;
9777 if (!bmap)
9778 return 0;
9779 bmap = isl_basic_map_copy(bmap);
9780 bmap = isl_basic_map_normalize(bmap);
9781 if (!bmap)
9782 return 0;
9783 total = isl_basic_map_total_dim(bmap);
9784 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9785 for (i = 0; i < bmap->n_eq; ++i) {
9786 uint32_t c_hash;
9787 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9788 isl_hash_hash(hash, c_hash);
9790 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9791 for (i = 0; i < bmap->n_ineq; ++i) {
9792 uint32_t c_hash;
9793 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9794 isl_hash_hash(hash, c_hash);
9796 isl_hash_byte(hash, bmap->n_div & 0xFF);
9797 for (i = 0; i < bmap->n_div; ++i) {
9798 uint32_t c_hash;
9799 if (isl_int_is_zero(bmap->div[i][0]))
9800 continue;
9801 isl_hash_byte(hash, i & 0xFF);
9802 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9803 isl_hash_hash(hash, c_hash);
9805 isl_basic_map_free(bmap);
9806 return hash;
9809 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9811 return isl_basic_map_get_hash((isl_basic_map *)bset);
9814 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9816 int i;
9817 uint32_t hash;
9819 if (!map)
9820 return 0;
9821 map = isl_map_copy(map);
9822 map = isl_map_normalize(map);
9823 if (!map)
9824 return 0;
9826 hash = isl_hash_init();
9827 for (i = 0; i < map->n; ++i) {
9828 uint32_t bmap_hash;
9829 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9830 isl_hash_hash(hash, bmap_hash);
9833 isl_map_free(map);
9835 return hash;
9838 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9840 return isl_map_get_hash((isl_map *)set);
9843 /* Check if the value for dimension dim is completely determined
9844 * by the values of the other parameters and variables.
9845 * That is, check if dimension dim is involved in an equality.
9847 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9849 int i;
9850 unsigned nparam;
9852 if (!bset)
9853 return -1;
9854 nparam = isl_basic_set_n_param(bset);
9855 for (i = 0; i < bset->n_eq; ++i)
9856 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9857 return 1;
9858 return 0;
9861 /* Check if the value for dimension dim is completely determined
9862 * by the values of the other parameters and variables.
9863 * That is, check if dimension dim is involved in an equality
9864 * for each of the subsets.
9866 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9868 int i;
9870 if (!set)
9871 return -1;
9872 for (i = 0; i < set->n; ++i) {
9873 int unique;
9874 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9875 if (unique != 1)
9876 return unique;
9878 return 1;
9881 int isl_set_n_basic_set(__isl_keep isl_set *set)
9883 return set ? set->n : 0;
9886 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9887 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9889 int i;
9891 if (!map)
9892 return -1;
9894 for (i = 0; i < map->n; ++i)
9895 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9896 return -1;
9898 return 0;
9901 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9902 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9904 int i;
9906 if (!set)
9907 return -1;
9909 for (i = 0; i < set->n; ++i)
9910 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9911 return -1;
9913 return 0;
9916 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9918 isl_space *dim;
9920 if (!bset)
9921 return NULL;
9923 bset = isl_basic_set_cow(bset);
9924 if (!bset)
9925 return NULL;
9927 dim = isl_basic_set_get_space(bset);
9928 dim = isl_space_lift(dim, bset->n_div);
9929 if (!dim)
9930 goto error;
9931 isl_space_free(bset->dim);
9932 bset->dim = dim;
9933 bset->extra -= bset->n_div;
9934 bset->n_div = 0;
9936 bset = isl_basic_set_finalize(bset);
9938 return bset;
9939 error:
9940 isl_basic_set_free(bset);
9941 return NULL;
9944 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9946 int i;
9947 isl_space *dim;
9948 unsigned n_div;
9950 set = isl_set_align_divs(set);
9952 if (!set)
9953 return NULL;
9955 set = isl_set_cow(set);
9956 if (!set)
9957 return NULL;
9959 n_div = set->p[0]->n_div;
9960 dim = isl_set_get_space(set);
9961 dim = isl_space_lift(dim, n_div);
9962 if (!dim)
9963 goto error;
9964 isl_space_free(set->dim);
9965 set->dim = dim;
9967 for (i = 0; i < set->n; ++i) {
9968 set->p[i] = isl_basic_set_lift(set->p[i]);
9969 if (!set->p[i])
9970 goto error;
9973 return set;
9974 error:
9975 isl_set_free(set);
9976 return NULL;
9979 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9981 isl_space *dim;
9982 struct isl_basic_map *bmap;
9983 unsigned n_set;
9984 unsigned n_div;
9985 unsigned n_param;
9986 unsigned total;
9987 int i, k, l;
9989 set = isl_set_align_divs(set);
9991 if (!set)
9992 return NULL;
9994 dim = isl_set_get_space(set);
9995 if (set->n == 0 || set->p[0]->n_div == 0) {
9996 isl_set_free(set);
9997 return isl_map_identity(isl_space_map_from_set(dim));
10000 n_div = set->p[0]->n_div;
10001 dim = isl_space_map_from_set(dim);
10002 n_param = isl_space_dim(dim, isl_dim_param);
10003 n_set = isl_space_dim(dim, isl_dim_in);
10004 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10005 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10006 for (i = 0; i < n_set; ++i)
10007 bmap = var_equal(bmap, i);
10009 total = n_param + n_set + n_set + n_div;
10010 for (i = 0; i < n_div; ++i) {
10011 k = isl_basic_map_alloc_inequality(bmap);
10012 if (k < 0)
10013 goto error;
10014 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10015 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10016 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10017 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10018 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10019 set->p[0]->div[i][0]);
10021 l = isl_basic_map_alloc_inequality(bmap);
10022 if (l < 0)
10023 goto error;
10024 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10025 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10026 set->p[0]->div[i][0]);
10027 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10030 isl_set_free(set);
10031 bmap = isl_basic_map_simplify(bmap);
10032 bmap = isl_basic_map_finalize(bmap);
10033 return isl_map_from_basic_map(bmap);
10034 error:
10035 isl_set_free(set);
10036 isl_basic_map_free(bmap);
10037 return NULL;
10040 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10042 unsigned dim;
10043 int size = 0;
10045 if (!bset)
10046 return -1;
10048 dim = isl_basic_set_total_dim(bset);
10049 size += bset->n_eq * (1 + dim);
10050 size += bset->n_ineq * (1 + dim);
10051 size += bset->n_div * (2 + dim);
10053 return size;
10056 int isl_set_size(__isl_keep isl_set *set)
10058 int i;
10059 int size = 0;
10061 if (!set)
10062 return -1;
10064 for (i = 0; i < set->n; ++i)
10065 size += isl_basic_set_size(set->p[i]);
10067 return size;
10070 /* Check if there is any lower bound (if lower == 0) and/or upper
10071 * bound (if upper == 0) on the specified dim.
10073 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10074 enum isl_dim_type type, unsigned pos, int lower, int upper)
10076 int i;
10078 if (!bmap)
10079 return -1;
10081 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
10083 pos += isl_basic_map_offset(bmap, type);
10085 for (i = 0; i < bmap->n_div; ++i) {
10086 if (isl_int_is_zero(bmap->div[i][0]))
10087 continue;
10088 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10089 return 1;
10092 for (i = 0; i < bmap->n_eq; ++i)
10093 if (!isl_int_is_zero(bmap->eq[i][pos]))
10094 return 1;
10096 for (i = 0; i < bmap->n_ineq; ++i) {
10097 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10098 if (sgn > 0)
10099 lower = 1;
10100 if (sgn < 0)
10101 upper = 1;
10104 return lower && upper;
10107 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10108 enum isl_dim_type type, unsigned pos)
10110 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10113 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10114 enum isl_dim_type type, unsigned pos)
10116 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10119 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10120 enum isl_dim_type type, unsigned pos)
10122 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10125 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10126 enum isl_dim_type type, unsigned pos)
10128 int i;
10130 if (!map)
10131 return -1;
10133 for (i = 0; i < map->n; ++i) {
10134 int bounded;
10135 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10136 if (bounded < 0 || !bounded)
10137 return bounded;
10140 return 1;
10143 /* Return 1 if the specified dim is involved in both an upper bound
10144 * and a lower bound.
10146 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10147 enum isl_dim_type type, unsigned pos)
10149 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10152 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10154 static int has_any_bound(__isl_keep isl_map *map,
10155 enum isl_dim_type type, unsigned pos,
10156 int (*fn)(__isl_keep isl_basic_map *bmap,
10157 enum isl_dim_type type, unsigned pos))
10159 int i;
10161 if (!map)
10162 return -1;
10164 for (i = 0; i < map->n; ++i) {
10165 int bounded;
10166 bounded = fn(map->p[i], type, pos);
10167 if (bounded < 0 || bounded)
10168 return bounded;
10171 return 0;
10174 /* Return 1 if the specified dim is involved in any lower bound.
10176 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10177 enum isl_dim_type type, unsigned pos)
10179 return has_any_bound(set, type, pos,
10180 &isl_basic_map_dim_has_lower_bound);
10183 /* Return 1 if the specified dim is involved in any upper bound.
10185 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10186 enum isl_dim_type type, unsigned pos)
10188 return has_any_bound(set, type, pos,
10189 &isl_basic_map_dim_has_upper_bound);
10192 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10194 static int has_bound(__isl_keep isl_map *map,
10195 enum isl_dim_type type, unsigned pos,
10196 int (*fn)(__isl_keep isl_basic_map *bmap,
10197 enum isl_dim_type type, unsigned pos))
10199 int i;
10201 if (!map)
10202 return -1;
10204 for (i = 0; i < map->n; ++i) {
10205 int bounded;
10206 bounded = fn(map->p[i], type, pos);
10207 if (bounded < 0 || !bounded)
10208 return bounded;
10211 return 1;
10214 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10216 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10217 enum isl_dim_type type, unsigned pos)
10219 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10222 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10224 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10225 enum isl_dim_type type, unsigned pos)
10227 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10230 /* For each of the "n" variables starting at "first", determine
10231 * the sign of the variable and put the results in the first "n"
10232 * elements of the array "signs".
10233 * Sign
10234 * 1 means that the variable is non-negative
10235 * -1 means that the variable is non-positive
10236 * 0 means the variable attains both positive and negative values.
10238 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10239 unsigned first, unsigned n, int *signs)
10241 isl_vec *bound = NULL;
10242 struct isl_tab *tab = NULL;
10243 struct isl_tab_undo *snap;
10244 int i;
10246 if (!bset || !signs)
10247 return -1;
10249 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10250 tab = isl_tab_from_basic_set(bset, 0);
10251 if (!bound || !tab)
10252 goto error;
10254 isl_seq_clr(bound->el, bound->size);
10255 isl_int_set_si(bound->el[0], -1);
10257 snap = isl_tab_snap(tab);
10258 for (i = 0; i < n; ++i) {
10259 int empty;
10261 isl_int_set_si(bound->el[1 + first + i], -1);
10262 if (isl_tab_add_ineq(tab, bound->el) < 0)
10263 goto error;
10264 empty = tab->empty;
10265 isl_int_set_si(bound->el[1 + first + i], 0);
10266 if (isl_tab_rollback(tab, snap) < 0)
10267 goto error;
10269 if (empty) {
10270 signs[i] = 1;
10271 continue;
10274 isl_int_set_si(bound->el[1 + first + i], 1);
10275 if (isl_tab_add_ineq(tab, bound->el) < 0)
10276 goto error;
10277 empty = tab->empty;
10278 isl_int_set_si(bound->el[1 + first + i], 0);
10279 if (isl_tab_rollback(tab, snap) < 0)
10280 goto error;
10282 signs[i] = empty ? -1 : 0;
10285 isl_tab_free(tab);
10286 isl_vec_free(bound);
10287 return 0;
10288 error:
10289 isl_tab_free(tab);
10290 isl_vec_free(bound);
10291 return -1;
10294 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10295 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10297 if (!bset || !signs)
10298 return -1;
10299 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10300 return -1);
10302 first += pos(bset->dim, type) - 1;
10303 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10306 /* Is it possible for the integer division "div" to depend (possibly
10307 * indirectly) on any output dimensions?
10309 * If the div is undefined, then we conservatively assume that it
10310 * may depend on them.
10311 * Otherwise, we check if it actually depends on them or on any integer
10312 * divisions that may depend on them.
10314 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10316 int i;
10317 unsigned n_out, o_out;
10318 unsigned n_div, o_div;
10320 if (isl_int_is_zero(bmap->div[div][0]))
10321 return 1;
10323 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10324 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10326 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10327 return 1;
10329 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10330 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10332 for (i = 0; i < n_div; ++i) {
10333 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10334 continue;
10335 if (div_may_involve_output(bmap, i))
10336 return 1;
10339 return 0;
10342 /* Return the index of the equality of "bmap" that defines
10343 * the output dimension "pos" in terms of earlier dimensions.
10344 * The equality may also involve integer divisions, as long
10345 * as those integer divisions are defined in terms of
10346 * parameters or input dimensions.
10347 * Return bmap->n_eq if there is no such equality.
10348 * Return -1 on error.
10350 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10351 int pos)
10353 int j, k;
10354 unsigned n_out, o_out;
10355 unsigned n_div, o_div;
10357 if (!bmap)
10358 return -1;
10360 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10361 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10362 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10363 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10365 for (j = 0; j < bmap->n_eq; ++j) {
10366 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10367 continue;
10368 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10369 n_out - (pos + 1)) != -1)
10370 continue;
10371 for (k = 0; k < n_div; ++k) {
10372 if (isl_int_is_zero(bmap->eq[j][o_div + k]))
10373 continue;
10374 if (div_may_involve_output(bmap, k))
10375 break;
10377 if (k >= n_div)
10378 return j;
10381 return bmap->n_eq;
10384 /* Check if the given basic map is obviously single-valued.
10385 * In particular, for each output dimension, check that there is
10386 * an equality that defines the output dimension in terms of
10387 * earlier dimensions.
10389 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10391 int i;
10392 unsigned n_out;
10394 if (!bmap)
10395 return -1;
10397 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10399 for (i = 0; i < n_out; ++i) {
10400 int eq;
10402 eq = isl_basic_map_output_defining_equality(bmap, i);
10403 if (eq < 0)
10404 return -1;
10405 if (eq >= bmap->n_eq)
10406 return 0;
10409 return 1;
10412 /* Check if the given basic map is single-valued.
10413 * We simply compute
10415 * M \circ M^-1
10417 * and check if the result is a subset of the identity mapping.
10419 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10421 isl_space *space;
10422 isl_basic_map *test;
10423 isl_basic_map *id;
10424 int sv;
10426 sv = isl_basic_map_plain_is_single_valued(bmap);
10427 if (sv < 0 || sv)
10428 return sv;
10430 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10431 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10433 space = isl_basic_map_get_space(bmap);
10434 space = isl_space_map_from_set(isl_space_range(space));
10435 id = isl_basic_map_identity(space);
10437 sv = isl_basic_map_is_subset(test, id);
10439 isl_basic_map_free(test);
10440 isl_basic_map_free(id);
10442 return sv;
10445 /* Check if the given map is obviously single-valued.
10447 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10449 if (!map)
10450 return -1;
10451 if (map->n == 0)
10452 return 1;
10453 if (map->n >= 2)
10454 return 0;
10456 return isl_basic_map_plain_is_single_valued(map->p[0]);
10459 /* Check if the given map is single-valued.
10460 * We simply compute
10462 * M \circ M^-1
10464 * and check if the result is a subset of the identity mapping.
10466 int isl_map_is_single_valued(__isl_keep isl_map *map)
10468 isl_space *dim;
10469 isl_map *test;
10470 isl_map *id;
10471 int sv;
10473 sv = isl_map_plain_is_single_valued(map);
10474 if (sv < 0 || sv)
10475 return sv;
10477 test = isl_map_reverse(isl_map_copy(map));
10478 test = isl_map_apply_range(test, isl_map_copy(map));
10480 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10481 id = isl_map_identity(dim);
10483 sv = isl_map_is_subset(test, id);
10485 isl_map_free(test);
10486 isl_map_free(id);
10488 return sv;
10491 int isl_map_is_injective(__isl_keep isl_map *map)
10493 int in;
10495 map = isl_map_copy(map);
10496 map = isl_map_reverse(map);
10497 in = isl_map_is_single_valued(map);
10498 isl_map_free(map);
10500 return in;
10503 /* Check if the given map is obviously injective.
10505 int isl_map_plain_is_injective(__isl_keep isl_map *map)
10507 int in;
10509 map = isl_map_copy(map);
10510 map = isl_map_reverse(map);
10511 in = isl_map_plain_is_single_valued(map);
10512 isl_map_free(map);
10514 return in;
10517 int isl_map_is_bijective(__isl_keep isl_map *map)
10519 int sv;
10521 sv = isl_map_is_single_valued(map);
10522 if (sv < 0 || !sv)
10523 return sv;
10525 return isl_map_is_injective(map);
10528 int isl_set_is_singleton(__isl_keep isl_set *set)
10530 return isl_map_is_single_valued((isl_map *)set);
10533 int isl_map_is_translation(__isl_keep isl_map *map)
10535 int ok;
10536 isl_set *delta;
10538 delta = isl_map_deltas(isl_map_copy(map));
10539 ok = isl_set_is_singleton(delta);
10540 isl_set_free(delta);
10542 return ok;
10545 static int unique(isl_int *p, unsigned pos, unsigned len)
10547 if (isl_seq_first_non_zero(p, pos) != -1)
10548 return 0;
10549 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10550 return 0;
10551 return 1;
10554 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10556 int i, j;
10557 unsigned nvar;
10558 unsigned ovar;
10560 if (!bset)
10561 return -1;
10563 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10564 return 0;
10566 nvar = isl_basic_set_dim(bset, isl_dim_set);
10567 ovar = isl_space_offset(bset->dim, isl_dim_set);
10568 for (j = 0; j < nvar; ++j) {
10569 int lower = 0, upper = 0;
10570 for (i = 0; i < bset->n_eq; ++i) {
10571 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10572 continue;
10573 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10574 return 0;
10575 break;
10577 if (i < bset->n_eq)
10578 continue;
10579 for (i = 0; i < bset->n_ineq; ++i) {
10580 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10581 continue;
10582 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10583 return 0;
10584 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10585 lower = 1;
10586 else
10587 upper = 1;
10589 if (!lower || !upper)
10590 return 0;
10593 return 1;
10596 int isl_set_is_box(__isl_keep isl_set *set)
10598 if (!set)
10599 return -1;
10600 if (set->n != 1)
10601 return 0;
10603 return isl_basic_set_is_box(set->p[0]);
10606 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10608 if (!bset)
10609 return -1;
10611 return isl_space_is_wrapping(bset->dim);
10614 int isl_set_is_wrapping(__isl_keep isl_set *set)
10616 if (!set)
10617 return -1;
10619 return isl_space_is_wrapping(set->dim);
10622 /* Is the domain of "map" a wrapped relation?
10624 int isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10626 if (!map)
10627 return -1;
10629 return isl_space_domain_is_wrapping(map->dim);
10632 /* Is the range of "map" a wrapped relation?
10634 int isl_map_range_is_wrapping(__isl_keep isl_map *map)
10636 if (!map)
10637 return -1;
10639 return isl_space_range_is_wrapping(map->dim);
10642 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10644 bmap = isl_basic_map_cow(bmap);
10645 if (!bmap)
10646 return NULL;
10648 bmap->dim = isl_space_wrap(bmap->dim);
10649 if (!bmap->dim)
10650 goto error;
10652 bmap = isl_basic_map_finalize(bmap);
10654 return (isl_basic_set *)bmap;
10655 error:
10656 isl_basic_map_free(bmap);
10657 return NULL;
10660 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10662 int i;
10664 map = isl_map_cow(map);
10665 if (!map)
10666 return NULL;
10668 for (i = 0; i < map->n; ++i) {
10669 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10670 if (!map->p[i])
10671 goto error;
10673 map->dim = isl_space_wrap(map->dim);
10674 if (!map->dim)
10675 goto error;
10677 return (isl_set *)map;
10678 error:
10679 isl_map_free(map);
10680 return NULL;
10683 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10685 bset = isl_basic_set_cow(bset);
10686 if (!bset)
10687 return NULL;
10689 bset->dim = isl_space_unwrap(bset->dim);
10690 if (!bset->dim)
10691 goto error;
10693 bset = isl_basic_set_finalize(bset);
10695 return (isl_basic_map *)bset;
10696 error:
10697 isl_basic_set_free(bset);
10698 return NULL;
10701 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10703 int i;
10705 if (!set)
10706 return NULL;
10708 if (!isl_set_is_wrapping(set))
10709 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10710 goto error);
10712 set = isl_set_cow(set);
10713 if (!set)
10714 return NULL;
10716 for (i = 0; i < set->n; ++i) {
10717 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10718 if (!set->p[i])
10719 goto error;
10722 set->dim = isl_space_unwrap(set->dim);
10723 if (!set->dim)
10724 goto error;
10726 return (isl_map *)set;
10727 error:
10728 isl_set_free(set);
10729 return NULL;
10732 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10733 enum isl_dim_type type)
10735 if (!bmap)
10736 return NULL;
10738 if (!isl_space_is_named_or_nested(bmap->dim, type))
10739 return bmap;
10741 bmap = isl_basic_map_cow(bmap);
10742 if (!bmap)
10743 return NULL;
10745 bmap->dim = isl_space_reset(bmap->dim, type);
10746 if (!bmap->dim)
10747 goto error;
10749 bmap = isl_basic_map_finalize(bmap);
10751 return bmap;
10752 error:
10753 isl_basic_map_free(bmap);
10754 return NULL;
10757 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10758 enum isl_dim_type type)
10760 int i;
10762 if (!map)
10763 return NULL;
10765 if (!isl_space_is_named_or_nested(map->dim, type))
10766 return map;
10768 map = isl_map_cow(map);
10769 if (!map)
10770 return NULL;
10772 for (i = 0; i < map->n; ++i) {
10773 map->p[i] = isl_basic_map_reset(map->p[i], type);
10774 if (!map->p[i])
10775 goto error;
10777 map->dim = isl_space_reset(map->dim, type);
10778 if (!map->dim)
10779 goto error;
10781 return map;
10782 error:
10783 isl_map_free(map);
10784 return NULL;
10787 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10789 if (!bmap)
10790 return NULL;
10792 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10793 return bmap;
10795 bmap = isl_basic_map_cow(bmap);
10796 if (!bmap)
10797 return NULL;
10799 bmap->dim = isl_space_flatten(bmap->dim);
10800 if (!bmap->dim)
10801 goto error;
10803 bmap = isl_basic_map_finalize(bmap);
10805 return bmap;
10806 error:
10807 isl_basic_map_free(bmap);
10808 return NULL;
10811 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10813 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10816 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10817 __isl_take isl_basic_map *bmap)
10819 if (!bmap)
10820 return NULL;
10822 if (!bmap->dim->nested[0])
10823 return bmap;
10825 bmap = isl_basic_map_cow(bmap);
10826 if (!bmap)
10827 return NULL;
10829 bmap->dim = isl_space_flatten_domain(bmap->dim);
10830 if (!bmap->dim)
10831 goto error;
10833 bmap = isl_basic_map_finalize(bmap);
10835 return bmap;
10836 error:
10837 isl_basic_map_free(bmap);
10838 return NULL;
10841 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10842 __isl_take isl_basic_map *bmap)
10844 if (!bmap)
10845 return NULL;
10847 if (!bmap->dim->nested[1])
10848 return bmap;
10850 bmap = isl_basic_map_cow(bmap);
10851 if (!bmap)
10852 return NULL;
10854 bmap->dim = isl_space_flatten_range(bmap->dim);
10855 if (!bmap->dim)
10856 goto error;
10858 bmap = isl_basic_map_finalize(bmap);
10860 return bmap;
10861 error:
10862 isl_basic_map_free(bmap);
10863 return NULL;
10866 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10868 int i;
10870 if (!map)
10871 return NULL;
10873 if (!map->dim->nested[0] && !map->dim->nested[1])
10874 return map;
10876 map = isl_map_cow(map);
10877 if (!map)
10878 return NULL;
10880 for (i = 0; i < map->n; ++i) {
10881 map->p[i] = isl_basic_map_flatten(map->p[i]);
10882 if (!map->p[i])
10883 goto error;
10885 map->dim = isl_space_flatten(map->dim);
10886 if (!map->dim)
10887 goto error;
10889 return map;
10890 error:
10891 isl_map_free(map);
10892 return NULL;
10895 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10897 return (isl_set *)isl_map_flatten((isl_map *)set);
10900 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10902 isl_space *dim, *flat_dim;
10903 isl_map *map;
10905 dim = isl_set_get_space(set);
10906 flat_dim = isl_space_flatten(isl_space_copy(dim));
10907 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10908 map = isl_map_intersect_domain(map, set);
10910 return map;
10913 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10915 int i;
10917 if (!map)
10918 return NULL;
10920 if (!map->dim->nested[0])
10921 return map;
10923 map = isl_map_cow(map);
10924 if (!map)
10925 return NULL;
10927 for (i = 0; i < map->n; ++i) {
10928 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10929 if (!map->p[i])
10930 goto error;
10932 map->dim = isl_space_flatten_domain(map->dim);
10933 if (!map->dim)
10934 goto error;
10936 return map;
10937 error:
10938 isl_map_free(map);
10939 return NULL;
10942 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10944 int i;
10946 if (!map)
10947 return NULL;
10949 if (!map->dim->nested[1])
10950 return map;
10952 map = isl_map_cow(map);
10953 if (!map)
10954 return NULL;
10956 for (i = 0; i < map->n; ++i) {
10957 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10958 if (!map->p[i])
10959 goto error;
10961 map->dim = isl_space_flatten_range(map->dim);
10962 if (!map->dim)
10963 goto error;
10965 return map;
10966 error:
10967 isl_map_free(map);
10968 return NULL;
10971 /* Reorder the dimensions of "bmap" according to the given dim_map
10972 * and set the dimension specification to "dim".
10974 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10975 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10977 isl_basic_map *res;
10978 unsigned flags;
10980 bmap = isl_basic_map_cow(bmap);
10981 if (!bmap || !dim || !dim_map)
10982 goto error;
10984 flags = bmap->flags;
10985 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10986 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10987 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10988 res = isl_basic_map_alloc_space(dim,
10989 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10990 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10991 if (res)
10992 res->flags = flags;
10993 res = isl_basic_map_finalize(res);
10994 return res;
10995 error:
10996 free(dim_map);
10997 isl_basic_map_free(bmap);
10998 isl_space_free(dim);
10999 return NULL;
11002 /* Reorder the dimensions of "map" according to given reordering.
11004 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11005 __isl_take isl_reordering *r)
11007 int i;
11008 struct isl_dim_map *dim_map;
11010 map = isl_map_cow(map);
11011 dim_map = isl_dim_map_from_reordering(r);
11012 if (!map || !r || !dim_map)
11013 goto error;
11015 for (i = 0; i < map->n; ++i) {
11016 struct isl_dim_map *dim_map_i;
11018 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11020 map->p[i] = isl_basic_map_realign(map->p[i],
11021 isl_space_copy(r->dim), dim_map_i);
11023 if (!map->p[i])
11024 goto error;
11027 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11029 isl_reordering_free(r);
11030 free(dim_map);
11031 return map;
11032 error:
11033 free(dim_map);
11034 isl_map_free(map);
11035 isl_reordering_free(r);
11036 return NULL;
11039 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11040 __isl_take isl_reordering *r)
11042 return (isl_set *)isl_map_realign((isl_map *)set, r);
11045 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11046 __isl_take isl_space *model)
11048 isl_ctx *ctx;
11050 if (!map || !model)
11051 goto error;
11053 ctx = isl_space_get_ctx(model);
11054 if (!isl_space_has_named_params(model))
11055 isl_die(ctx, isl_error_invalid,
11056 "model has unnamed parameters", goto error);
11057 if (!isl_space_has_named_params(map->dim))
11058 isl_die(ctx, isl_error_invalid,
11059 "relation has unnamed parameters", goto error);
11060 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11061 isl_reordering *exp;
11063 model = isl_space_drop_dims(model, isl_dim_in,
11064 0, isl_space_dim(model, isl_dim_in));
11065 model = isl_space_drop_dims(model, isl_dim_out,
11066 0, isl_space_dim(model, isl_dim_out));
11067 exp = isl_parameter_alignment_reordering(map->dim, model);
11068 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11069 map = isl_map_realign(map, exp);
11072 isl_space_free(model);
11073 return map;
11074 error:
11075 isl_space_free(model);
11076 isl_map_free(map);
11077 return NULL;
11080 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11081 __isl_take isl_space *model)
11083 return isl_map_align_params(set, model);
11086 /* Align the parameters of "bmap" to those of "model", introducing
11087 * additional parameters if needed.
11089 __isl_give isl_basic_map *isl_basic_map_align_params(
11090 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11092 isl_ctx *ctx;
11094 if (!bmap || !model)
11095 goto error;
11097 ctx = isl_space_get_ctx(model);
11098 if (!isl_space_has_named_params(model))
11099 isl_die(ctx, isl_error_invalid,
11100 "model has unnamed parameters", goto error);
11101 if (!isl_space_has_named_params(bmap->dim))
11102 isl_die(ctx, isl_error_invalid,
11103 "relation has unnamed parameters", goto error);
11104 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11105 isl_reordering *exp;
11106 struct isl_dim_map *dim_map;
11108 model = isl_space_drop_dims(model, isl_dim_in,
11109 0, isl_space_dim(model, isl_dim_in));
11110 model = isl_space_drop_dims(model, isl_dim_out,
11111 0, isl_space_dim(model, isl_dim_out));
11112 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11113 exp = isl_reordering_extend_space(exp,
11114 isl_basic_map_get_space(bmap));
11115 dim_map = isl_dim_map_from_reordering(exp);
11116 bmap = isl_basic_map_realign(bmap,
11117 exp ? isl_space_copy(exp->dim) : NULL,
11118 isl_dim_map_extend(dim_map, bmap));
11119 isl_reordering_free(exp);
11120 free(dim_map);
11123 isl_space_free(model);
11124 return bmap;
11125 error:
11126 isl_space_free(model);
11127 isl_basic_map_free(bmap);
11128 return NULL;
11131 /* Align the parameters of "bset" to those of "model", introducing
11132 * additional parameters if needed.
11134 __isl_give isl_basic_set *isl_basic_set_align_params(
11135 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11137 return isl_basic_map_align_params(bset, model);
11140 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11141 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11142 enum isl_dim_type c2, enum isl_dim_type c3,
11143 enum isl_dim_type c4, enum isl_dim_type c5)
11145 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11146 struct isl_mat *mat;
11147 int i, j, k;
11148 int pos;
11150 if (!bmap)
11151 return NULL;
11152 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11153 isl_basic_map_total_dim(bmap) + 1);
11154 if (!mat)
11155 return NULL;
11156 for (i = 0; i < bmap->n_eq; ++i)
11157 for (j = 0, pos = 0; j < 5; ++j) {
11158 int off = isl_basic_map_offset(bmap, c[j]);
11159 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11160 isl_int_set(mat->row[i][pos],
11161 bmap->eq[i][off + k]);
11162 ++pos;
11166 return mat;
11169 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11170 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11171 enum isl_dim_type c2, enum isl_dim_type c3,
11172 enum isl_dim_type c4, enum isl_dim_type c5)
11174 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11175 struct isl_mat *mat;
11176 int i, j, k;
11177 int pos;
11179 if (!bmap)
11180 return NULL;
11181 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11182 isl_basic_map_total_dim(bmap) + 1);
11183 if (!mat)
11184 return NULL;
11185 for (i = 0; i < bmap->n_ineq; ++i)
11186 for (j = 0, pos = 0; j < 5; ++j) {
11187 int off = isl_basic_map_offset(bmap, c[j]);
11188 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11189 isl_int_set(mat->row[i][pos],
11190 bmap->ineq[i][off + k]);
11191 ++pos;
11195 return mat;
11198 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11199 __isl_take isl_space *dim,
11200 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11201 enum isl_dim_type c2, enum isl_dim_type c3,
11202 enum isl_dim_type c4, enum isl_dim_type c5)
11204 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11205 isl_basic_map *bmap;
11206 unsigned total;
11207 unsigned extra;
11208 int i, j, k, l;
11209 int pos;
11211 if (!dim || !eq || !ineq)
11212 goto error;
11214 if (eq->n_col != ineq->n_col)
11215 isl_die(dim->ctx, isl_error_invalid,
11216 "equalities and inequalities matrices should have "
11217 "same number of columns", goto error);
11219 total = 1 + isl_space_dim(dim, isl_dim_all);
11221 if (eq->n_col < total)
11222 isl_die(dim->ctx, isl_error_invalid,
11223 "number of columns too small", goto error);
11225 extra = eq->n_col - total;
11227 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11228 eq->n_row, ineq->n_row);
11229 if (!bmap)
11230 goto error;
11231 for (i = 0; i < extra; ++i) {
11232 k = isl_basic_map_alloc_div(bmap);
11233 if (k < 0)
11234 goto error;
11235 isl_int_set_si(bmap->div[k][0], 0);
11237 for (i = 0; i < eq->n_row; ++i) {
11238 l = isl_basic_map_alloc_equality(bmap);
11239 if (l < 0)
11240 goto error;
11241 for (j = 0, pos = 0; j < 5; ++j) {
11242 int off = isl_basic_map_offset(bmap, c[j]);
11243 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11244 isl_int_set(bmap->eq[l][off + k],
11245 eq->row[i][pos]);
11246 ++pos;
11250 for (i = 0; i < ineq->n_row; ++i) {
11251 l = isl_basic_map_alloc_inequality(bmap);
11252 if (l < 0)
11253 goto error;
11254 for (j = 0, pos = 0; j < 5; ++j) {
11255 int off = isl_basic_map_offset(bmap, c[j]);
11256 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11257 isl_int_set(bmap->ineq[l][off + k],
11258 ineq->row[i][pos]);
11259 ++pos;
11264 isl_space_free(dim);
11265 isl_mat_free(eq);
11266 isl_mat_free(ineq);
11268 bmap = isl_basic_map_simplify(bmap);
11269 return isl_basic_map_finalize(bmap);
11270 error:
11271 isl_space_free(dim);
11272 isl_mat_free(eq);
11273 isl_mat_free(ineq);
11274 return NULL;
11277 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11278 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11279 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11281 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11282 c1, c2, c3, c4, isl_dim_in);
11285 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11286 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11287 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11289 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11290 c1, c2, c3, c4, isl_dim_in);
11293 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11294 __isl_take isl_space *dim,
11295 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11296 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11298 return (isl_basic_set*)
11299 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11300 c1, c2, c3, c4, isl_dim_in);
11303 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11305 if (!bmap)
11306 return -1;
11308 return isl_space_can_zip(bmap->dim);
11311 int isl_map_can_zip(__isl_keep isl_map *map)
11313 if (!map)
11314 return -1;
11316 return isl_space_can_zip(map->dim);
11319 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11320 * (A -> C) -> (B -> D).
11322 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11324 unsigned pos;
11325 unsigned n1;
11326 unsigned n2;
11328 if (!bmap)
11329 return NULL;
11331 if (!isl_basic_map_can_zip(bmap))
11332 isl_die(bmap->ctx, isl_error_invalid,
11333 "basic map cannot be zipped", goto error);
11334 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11335 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11336 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11337 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11338 bmap = isl_basic_map_cow(bmap);
11339 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11340 if (!bmap)
11341 return NULL;
11342 bmap->dim = isl_space_zip(bmap->dim);
11343 if (!bmap->dim)
11344 goto error;
11345 return bmap;
11346 error:
11347 isl_basic_map_free(bmap);
11348 return NULL;
11351 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11352 * (A -> C) -> (B -> D).
11354 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11356 int i;
11358 if (!map)
11359 return NULL;
11361 if (!isl_map_can_zip(map))
11362 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11363 goto error);
11365 map = isl_map_cow(map);
11366 if (!map)
11367 return NULL;
11369 for (i = 0; i < map->n; ++i) {
11370 map->p[i] = isl_basic_map_zip(map->p[i]);
11371 if (!map->p[i])
11372 goto error;
11375 map->dim = isl_space_zip(map->dim);
11376 if (!map->dim)
11377 goto error;
11379 return map;
11380 error:
11381 isl_map_free(map);
11382 return NULL;
11385 /* Can we apply isl_basic_map_curry to "bmap"?
11386 * That is, does it have a nested relation in its domain?
11388 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11390 if (!bmap)
11391 return -1;
11393 return isl_space_can_curry(bmap->dim);
11396 /* Can we apply isl_map_curry to "map"?
11397 * That is, does it have a nested relation in its domain?
11399 int isl_map_can_curry(__isl_keep isl_map *map)
11401 if (!map)
11402 return -1;
11404 return isl_space_can_curry(map->dim);
11407 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11408 * A -> (B -> C).
11410 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11413 if (!bmap)
11414 return NULL;
11416 if (!isl_basic_map_can_curry(bmap))
11417 isl_die(bmap->ctx, isl_error_invalid,
11418 "basic map cannot be curried", goto error);
11419 bmap = isl_basic_map_cow(bmap);
11420 if (!bmap)
11421 return NULL;
11422 bmap->dim = isl_space_curry(bmap->dim);
11423 if (!bmap->dim)
11424 goto error;
11425 return bmap;
11426 error:
11427 isl_basic_map_free(bmap);
11428 return NULL;
11431 /* Given a map (A -> B) -> C, return the corresponding map
11432 * A -> (B -> C).
11434 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11436 int i;
11438 if (!map)
11439 return NULL;
11441 if (!isl_map_can_curry(map))
11442 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11443 goto error);
11445 map = isl_map_cow(map);
11446 if (!map)
11447 return NULL;
11449 for (i = 0; i < map->n; ++i) {
11450 map->p[i] = isl_basic_map_curry(map->p[i]);
11451 if (!map->p[i])
11452 goto error;
11455 map->dim = isl_space_curry(map->dim);
11456 if (!map->dim)
11457 goto error;
11459 return map;
11460 error:
11461 isl_map_free(map);
11462 return NULL;
11465 /* Can we apply isl_basic_map_uncurry to "bmap"?
11466 * That is, does it have a nested relation in its domain?
11468 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11470 if (!bmap)
11471 return -1;
11473 return isl_space_can_uncurry(bmap->dim);
11476 /* Can we apply isl_map_uncurry to "map"?
11477 * That is, does it have a nested relation in its domain?
11479 int isl_map_can_uncurry(__isl_keep isl_map *map)
11481 if (!map)
11482 return -1;
11484 return isl_space_can_uncurry(map->dim);
11487 /* Given a basic map A -> (B -> C), return the corresponding basic map
11488 * (A -> B) -> C.
11490 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11493 if (!bmap)
11494 return NULL;
11496 if (!isl_basic_map_can_uncurry(bmap))
11497 isl_die(bmap->ctx, isl_error_invalid,
11498 "basic map cannot be uncurried",
11499 return isl_basic_map_free(bmap));
11500 bmap = isl_basic_map_cow(bmap);
11501 if (!bmap)
11502 return NULL;
11503 bmap->dim = isl_space_uncurry(bmap->dim);
11504 if (!bmap->dim)
11505 return isl_basic_map_free(bmap);
11506 return bmap;
11509 /* Given a map A -> (B -> C), return the corresponding map
11510 * (A -> B) -> C.
11512 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11514 int i;
11516 if (!map)
11517 return NULL;
11519 if (!isl_map_can_uncurry(map))
11520 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11521 return isl_map_free(map));
11523 map = isl_map_cow(map);
11524 if (!map)
11525 return NULL;
11527 for (i = 0; i < map->n; ++i) {
11528 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11529 if (!map->p[i])
11530 return isl_map_free(map);
11533 map->dim = isl_space_uncurry(map->dim);
11534 if (!map->dim)
11535 return isl_map_free(map);
11537 return map;
11540 /* Construct a basic map mapping the domain of the affine expression
11541 * to a one-dimensional range prescribed by the affine expression.
11543 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11545 int k;
11546 int pos;
11547 isl_local_space *ls;
11548 isl_basic_map *bmap;
11550 if (!aff)
11551 return NULL;
11553 ls = isl_aff_get_local_space(aff);
11554 bmap = isl_basic_map_from_local_space(ls);
11555 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11556 k = isl_basic_map_alloc_equality(bmap);
11557 if (k < 0)
11558 goto error;
11560 pos = isl_basic_map_offset(bmap, isl_dim_out);
11561 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11562 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11563 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11564 aff->v->size - (pos + 1));
11566 isl_aff_free(aff);
11567 bmap = isl_basic_map_finalize(bmap);
11568 return bmap;
11569 error:
11570 isl_aff_free(aff);
11571 isl_basic_map_free(bmap);
11572 return NULL;
11575 /* Construct a map mapping the domain of the affine expression
11576 * to a one-dimensional range prescribed by the affine expression.
11578 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11580 isl_basic_map *bmap;
11582 bmap = isl_basic_map_from_aff(aff);
11583 return isl_map_from_basic_map(bmap);
11586 /* Construct a basic map mapping the domain the multi-affine expression
11587 * to its range, with each dimension in the range equated to the
11588 * corresponding affine expression.
11590 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11591 __isl_take isl_multi_aff *maff)
11593 int i;
11594 isl_space *space;
11595 isl_basic_map *bmap;
11597 if (!maff)
11598 return NULL;
11600 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11601 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11602 "invalid space", goto error);
11604 space = isl_space_domain(isl_multi_aff_get_space(maff));
11605 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11607 for (i = 0; i < maff->n; ++i) {
11608 isl_aff *aff;
11609 isl_basic_map *bmap_i;
11611 aff = isl_aff_copy(maff->p[i]);
11612 bmap_i = isl_basic_map_from_aff(aff);
11614 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11617 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11619 isl_multi_aff_free(maff);
11620 return bmap;
11621 error:
11622 isl_multi_aff_free(maff);
11623 return NULL;
11626 /* Construct a map mapping the domain the multi-affine expression
11627 * to its range, with each dimension in the range equated to the
11628 * corresponding affine expression.
11630 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11632 isl_basic_map *bmap;
11634 bmap = isl_basic_map_from_multi_aff(maff);
11635 return isl_map_from_basic_map(bmap);
11638 /* Construct a basic map mapping a domain in the given space to
11639 * to an n-dimensional range, with n the number of elements in the list,
11640 * where each coordinate in the range is prescribed by the
11641 * corresponding affine expression.
11642 * The domains of all affine expressions in the list are assumed to match
11643 * domain_dim.
11645 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11646 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11648 int i;
11649 isl_space *dim;
11650 isl_basic_map *bmap;
11652 if (!list)
11653 return NULL;
11655 dim = isl_space_from_domain(domain_dim);
11656 bmap = isl_basic_map_universe(dim);
11658 for (i = 0; i < list->n; ++i) {
11659 isl_aff *aff;
11660 isl_basic_map *bmap_i;
11662 aff = isl_aff_copy(list->p[i]);
11663 bmap_i = isl_basic_map_from_aff(aff);
11665 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11668 isl_aff_list_free(list);
11669 return bmap;
11672 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11673 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11675 return isl_map_equate(set, type1, pos1, type2, pos2);
11678 /* Construct a basic map where the given dimensions are equal to each other.
11680 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11681 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11683 isl_basic_map *bmap = NULL;
11684 int i;
11686 if (!space)
11687 return NULL;
11689 if (pos1 >= isl_space_dim(space, type1))
11690 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11691 "index out of bounds", goto error);
11692 if (pos2 >= isl_space_dim(space, type2))
11693 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11694 "index out of bounds", goto error);
11696 if (type1 == type2 && pos1 == pos2)
11697 return isl_basic_map_universe(space);
11699 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11700 i = isl_basic_map_alloc_equality(bmap);
11701 if (i < 0)
11702 goto error;
11703 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11704 pos1 += isl_basic_map_offset(bmap, type1);
11705 pos2 += isl_basic_map_offset(bmap, type2);
11706 isl_int_set_si(bmap->eq[i][pos1], -1);
11707 isl_int_set_si(bmap->eq[i][pos2], 1);
11708 bmap = isl_basic_map_finalize(bmap);
11709 isl_space_free(space);
11710 return bmap;
11711 error:
11712 isl_space_free(space);
11713 isl_basic_map_free(bmap);
11714 return NULL;
11717 /* Add a constraint imposing that the given two dimensions are equal.
11719 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11720 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11722 isl_basic_map *eq;
11724 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11726 bmap = isl_basic_map_intersect(bmap, eq);
11728 return bmap;
11731 /* Add a constraint imposing that the given two dimensions are equal.
11733 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11734 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11736 isl_basic_map *bmap;
11738 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11740 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11742 return map;
11745 /* Add a constraint imposing that the given two dimensions have opposite values.
11747 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11748 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11750 isl_basic_map *bmap = NULL;
11751 int i;
11753 if (!map)
11754 return NULL;
11756 if (pos1 >= isl_map_dim(map, type1))
11757 isl_die(map->ctx, isl_error_invalid,
11758 "index out of bounds", goto error);
11759 if (pos2 >= isl_map_dim(map, type2))
11760 isl_die(map->ctx, isl_error_invalid,
11761 "index out of bounds", goto error);
11763 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11764 i = isl_basic_map_alloc_equality(bmap);
11765 if (i < 0)
11766 goto error;
11767 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11768 pos1 += isl_basic_map_offset(bmap, type1);
11769 pos2 += isl_basic_map_offset(bmap, type2);
11770 isl_int_set_si(bmap->eq[i][pos1], 1);
11771 isl_int_set_si(bmap->eq[i][pos2], 1);
11772 bmap = isl_basic_map_finalize(bmap);
11774 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11776 return map;
11777 error:
11778 isl_basic_map_free(bmap);
11779 isl_map_free(map);
11780 return NULL;
11783 /* Construct a constraint imposing that the value of the first dimension is
11784 * greater than or equal to that of the second.
11786 static __isl_give isl_constraint *constraint_order_ge(
11787 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11788 enum isl_dim_type type2, int pos2)
11790 isl_constraint *c;
11792 if (!space)
11793 return NULL;
11795 c = isl_inequality_alloc(isl_local_space_from_space(space));
11797 if (pos1 >= isl_constraint_dim(c, type1))
11798 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11799 "index out of bounds", return isl_constraint_free(c));
11800 if (pos2 >= isl_constraint_dim(c, type2))
11801 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11802 "index out of bounds", return isl_constraint_free(c));
11804 if (type1 == type2 && pos1 == pos2)
11805 return c;
11807 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11808 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11810 return c;
11813 /* Add a constraint imposing that the value of the first dimension is
11814 * greater than or equal to that of the second.
11816 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11817 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11819 isl_constraint *c;
11820 isl_space *space;
11822 if (type1 == type2 && pos1 == pos2)
11823 return bmap;
11824 space = isl_basic_map_get_space(bmap);
11825 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11826 bmap = isl_basic_map_add_constraint(bmap, c);
11828 return bmap;
11831 /* Add a constraint imposing that the value of the first dimension is
11832 * greater than or equal to that of the second.
11834 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11835 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11837 isl_constraint *c;
11838 isl_space *space;
11840 if (type1 == type2 && pos1 == pos2)
11841 return map;
11842 space = isl_map_get_space(map);
11843 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11844 map = isl_map_add_constraint(map, c);
11846 return map;
11849 /* Add a constraint imposing that the value of the first dimension is
11850 * less than or equal to that of the second.
11852 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11853 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11855 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11858 /* Construct a basic map where the value of the first dimension is
11859 * greater than that of the second.
11861 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11862 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11864 isl_basic_map *bmap = NULL;
11865 int i;
11867 if (!space)
11868 return NULL;
11870 if (pos1 >= isl_space_dim(space, type1))
11871 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11872 "index out of bounds", goto error);
11873 if (pos2 >= isl_space_dim(space, type2))
11874 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11875 "index out of bounds", goto error);
11877 if (type1 == type2 && pos1 == pos2)
11878 return isl_basic_map_empty(space);
11880 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11881 i = isl_basic_map_alloc_inequality(bmap);
11882 if (i < 0)
11883 return isl_basic_map_free(bmap);
11884 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11885 pos1 += isl_basic_map_offset(bmap, type1);
11886 pos2 += isl_basic_map_offset(bmap, type2);
11887 isl_int_set_si(bmap->ineq[i][pos1], 1);
11888 isl_int_set_si(bmap->ineq[i][pos2], -1);
11889 isl_int_set_si(bmap->ineq[i][0], -1);
11890 bmap = isl_basic_map_finalize(bmap);
11892 return bmap;
11893 error:
11894 isl_space_free(space);
11895 isl_basic_map_free(bmap);
11896 return NULL;
11899 /* Add a constraint imposing that the value of the first dimension is
11900 * greater than that of the second.
11902 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11903 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11905 isl_basic_map *gt;
11907 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11909 bmap = isl_basic_map_intersect(bmap, gt);
11911 return bmap;
11914 /* Add a constraint imposing that the value of the first dimension is
11915 * greater than that of the second.
11917 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11918 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11920 isl_basic_map *bmap;
11922 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11924 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11926 return map;
11929 /* Add a constraint imposing that the value of the first dimension is
11930 * smaller than that of the second.
11932 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11933 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11935 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11938 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11939 int pos)
11941 isl_aff *div;
11942 isl_local_space *ls;
11944 if (!bmap)
11945 return NULL;
11947 if (!isl_basic_map_divs_known(bmap))
11948 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11949 "some divs are unknown", return NULL);
11951 ls = isl_basic_map_get_local_space(bmap);
11952 div = isl_local_space_get_div(ls, pos);
11953 isl_local_space_free(ls);
11955 return div;
11958 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11959 int pos)
11961 return isl_basic_map_get_div(bset, pos);
11964 /* Plug in "subs" for dimension "type", "pos" of "bset".
11966 * Let i be the dimension to replace and let "subs" be of the form
11968 * f/d
11970 * Any integer division with a non-zero coefficient for i,
11972 * floor((a i + g)/m)
11974 * is replaced by
11976 * floor((a f + d g)/(m d))
11978 * Constraints of the form
11980 * a i + g
11982 * are replaced by
11984 * a f + d g
11986 * We currently require that "subs" is an integral expression.
11987 * Handling rational expressions may require us to add stride constraints
11988 * as we do in isl_basic_set_preimage_multi_aff.
11990 __isl_give isl_basic_set *isl_basic_set_substitute(
11991 __isl_take isl_basic_set *bset,
11992 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11994 int i;
11995 isl_int v;
11996 isl_ctx *ctx;
11998 if (bset && isl_basic_set_plain_is_empty(bset))
11999 return bset;
12001 bset = isl_basic_set_cow(bset);
12002 if (!bset || !subs)
12003 goto error;
12005 ctx = isl_basic_set_get_ctx(bset);
12006 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12007 isl_die(ctx, isl_error_invalid,
12008 "spaces don't match", goto error);
12009 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12010 isl_die(ctx, isl_error_unsupported,
12011 "cannot handle divs yet", goto error);
12012 if (!isl_int_is_one(subs->v->el[0]))
12013 isl_die(ctx, isl_error_invalid,
12014 "can only substitute integer expressions", goto error);
12016 pos += isl_basic_set_offset(bset, type);
12018 isl_int_init(v);
12020 for (i = 0; i < bset->n_eq; ++i) {
12021 if (isl_int_is_zero(bset->eq[i][pos]))
12022 continue;
12023 isl_int_set(v, bset->eq[i][pos]);
12024 isl_int_set_si(bset->eq[i][pos], 0);
12025 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12026 v, subs->v->el + 1, subs->v->size - 1);
12029 for (i = 0; i < bset->n_ineq; ++i) {
12030 if (isl_int_is_zero(bset->ineq[i][pos]))
12031 continue;
12032 isl_int_set(v, bset->ineq[i][pos]);
12033 isl_int_set_si(bset->ineq[i][pos], 0);
12034 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12035 v, subs->v->el + 1, subs->v->size - 1);
12038 for (i = 0; i < bset->n_div; ++i) {
12039 if (isl_int_is_zero(bset->div[i][1 + pos]))
12040 continue;
12041 isl_int_set(v, bset->div[i][1 + pos]);
12042 isl_int_set_si(bset->div[i][1 + pos], 0);
12043 isl_seq_combine(bset->div[i] + 1,
12044 subs->v->el[0], bset->div[i] + 1,
12045 v, subs->v->el + 1, subs->v->size - 1);
12046 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12049 isl_int_clear(v);
12051 bset = isl_basic_set_simplify(bset);
12052 return isl_basic_set_finalize(bset);
12053 error:
12054 isl_basic_set_free(bset);
12055 return NULL;
12058 /* Plug in "subs" for dimension "type", "pos" of "set".
12060 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12061 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12063 int i;
12065 if (set && isl_set_plain_is_empty(set))
12066 return set;
12068 set = isl_set_cow(set);
12069 if (!set || !subs)
12070 goto error;
12072 for (i = set->n - 1; i >= 0; --i) {
12073 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12074 if (remove_if_empty(set, i) < 0)
12075 goto error;
12078 return set;
12079 error:
12080 isl_set_free(set);
12081 return NULL;
12084 /* Check if the range of "ma" is compatible with the domain or range
12085 * (depending on "type") of "bmap".
12086 * Return -1 if anything is wrong.
12088 static int check_basic_map_compatible_range_multi_aff(
12089 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12090 __isl_keep isl_multi_aff *ma)
12092 int m;
12093 isl_space *ma_space;
12095 ma_space = isl_multi_aff_get_space(ma);
12097 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12098 if (m < 0)
12099 goto error;
12100 if (!m)
12101 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12102 "parameters don't match", goto error);
12103 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12104 if (m < 0)
12105 goto error;
12106 if (!m)
12107 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12108 "spaces don't match", goto error);
12110 isl_space_free(ma_space);
12111 return m;
12112 error:
12113 isl_space_free(ma_space);
12114 return -1;
12117 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12118 * coefficients before the transformed range of dimensions,
12119 * the "n_after" coefficients after the transformed range of dimensions
12120 * and the coefficients of the other divs in "bmap".
12122 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12123 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12125 int i;
12126 int n_param;
12127 int n_set;
12128 isl_local_space *ls;
12130 if (n_div == 0)
12131 return 0;
12133 ls = isl_aff_get_domain_local_space(ma->p[0]);
12134 if (!ls)
12135 return -1;
12137 n_param = isl_local_space_dim(ls, isl_dim_param);
12138 n_set = isl_local_space_dim(ls, isl_dim_set);
12139 for (i = 0; i < n_div; ++i) {
12140 int o_bmap = 0, o_ls = 0;
12142 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12143 o_bmap += 1 + 1 + n_param;
12144 o_ls += 1 + 1 + n_param;
12145 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12146 o_bmap += n_before;
12147 isl_seq_cpy(bmap->div[i] + o_bmap,
12148 ls->div->row[i] + o_ls, n_set);
12149 o_bmap += n_set;
12150 o_ls += n_set;
12151 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12152 o_bmap += n_after;
12153 isl_seq_cpy(bmap->div[i] + o_bmap,
12154 ls->div->row[i] + o_ls, n_div);
12155 o_bmap += n_div;
12156 o_ls += n_div;
12157 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12158 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12159 goto error;
12162 isl_local_space_free(ls);
12163 return 0;
12164 error:
12165 isl_local_space_free(ls);
12166 return -1;
12169 /* How many stride constraints does "ma" enforce?
12170 * That is, how many of the affine expressions have a denominator
12171 * different from one?
12173 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12175 int i;
12176 int strides = 0;
12178 for (i = 0; i < ma->n; ++i)
12179 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12180 strides++;
12182 return strides;
12185 /* For each affine expression in ma of the form
12187 * x_i = (f_i y + h_i)/m_i
12189 * with m_i different from one, add a constraint to "bmap"
12190 * of the form
12192 * f_i y + h_i = m_i alpha_i
12194 * with alpha_i an additional existentially quantified variable.
12196 static __isl_give isl_basic_map *add_ma_strides(
12197 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12198 int n_before, int n_after)
12200 int i, k;
12201 int div;
12202 int total;
12203 int n_param;
12204 int n_in;
12205 int n_div;
12207 total = isl_basic_map_total_dim(bmap);
12208 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12209 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12210 n_div = isl_multi_aff_dim(ma, isl_dim_div);
12211 for (i = 0; i < ma->n; ++i) {
12212 int o_bmap = 0, o_ma = 1;
12214 if (isl_int_is_one(ma->p[i]->v->el[0]))
12215 continue;
12216 div = isl_basic_map_alloc_div(bmap);
12217 k = isl_basic_map_alloc_equality(bmap);
12218 if (div < 0 || k < 0)
12219 goto error;
12220 isl_int_set_si(bmap->div[div][0], 0);
12221 isl_seq_cpy(bmap->eq[k] + o_bmap,
12222 ma->p[i]->v->el + o_ma, 1 + n_param);
12223 o_bmap += 1 + n_param;
12224 o_ma += 1 + n_param;
12225 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12226 o_bmap += n_before;
12227 isl_seq_cpy(bmap->eq[k] + o_bmap,
12228 ma->p[i]->v->el + o_ma, n_in);
12229 o_bmap += n_in;
12230 o_ma += n_in;
12231 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12232 o_bmap += n_after;
12233 isl_seq_cpy(bmap->eq[k] + o_bmap,
12234 ma->p[i]->v->el + o_ma, n_div);
12235 o_bmap += n_div;
12236 o_ma += n_div;
12237 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12238 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12239 total++;
12242 return bmap;
12243 error:
12244 isl_basic_map_free(bmap);
12245 return NULL;
12248 /* Replace the domain or range space (depending on "type) of "space" by "set".
12250 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12251 enum isl_dim_type type, __isl_take isl_space *set)
12253 if (type == isl_dim_in) {
12254 space = isl_space_range(space);
12255 space = isl_space_map_from_domain_and_range(set, space);
12256 } else {
12257 space = isl_space_domain(space);
12258 space = isl_space_map_from_domain_and_range(space, set);
12261 return space;
12264 /* Compute the preimage of the domain or range (depending on "type")
12265 * of "bmap" under the function represented by "ma".
12266 * In other words, plug in "ma" in the domain or range of "bmap".
12267 * The result is a basic map that lives in the same space as "bmap"
12268 * except that the domain or range has been replaced by
12269 * the domain space of "ma".
12271 * If bmap is represented by
12273 * A(p) + S u + B x + T v + C(divs) >= 0,
12275 * where u and x are input and output dimensions if type == isl_dim_out
12276 * while x and v are input and output dimensions if type == isl_dim_in,
12277 * and ma is represented by
12279 * x = D(p) + F(y) + G(divs')
12281 * then the result is
12283 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12285 * The divs in the input set are similarly adjusted.
12286 * In particular
12288 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12290 * becomes
12292 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12293 * B_i G(divs') + c_i(divs))/n_i)
12295 * If bmap is not a rational map and if F(y) involves any denominators
12297 * x_i = (f_i y + h_i)/m_i
12299 * then additional constraints are added to ensure that we only
12300 * map back integer points. That is we enforce
12302 * f_i y + h_i = m_i alpha_i
12304 * with alpha_i an additional existentially quantified variable.
12306 * We first copy over the divs from "ma".
12307 * Then we add the modified constraints and divs from "bmap".
12308 * Finally, we add the stride constraints, if needed.
12310 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12311 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12312 __isl_take isl_multi_aff *ma)
12314 int i, k;
12315 isl_space *space;
12316 isl_basic_map *res = NULL;
12317 int n_before, n_after, n_div_bmap, n_div_ma;
12318 isl_int f, c1, c2, g;
12319 int rational, strides;
12321 isl_int_init(f);
12322 isl_int_init(c1);
12323 isl_int_init(c2);
12324 isl_int_init(g);
12326 ma = isl_multi_aff_align_divs(ma);
12327 if (!bmap || !ma)
12328 goto error;
12329 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12330 goto error;
12332 if (type == isl_dim_in) {
12333 n_before = 0;
12334 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12335 } else {
12336 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12337 n_after = 0;
12339 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12340 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12342 space = isl_multi_aff_get_domain_space(ma);
12343 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12344 rational = isl_basic_map_is_rational(bmap);
12345 strides = rational ? 0 : multi_aff_strides(ma);
12346 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12347 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12348 if (rational)
12349 res = isl_basic_map_set_rational(res);
12351 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12352 if (isl_basic_map_alloc_div(res) < 0)
12353 goto error;
12355 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12356 goto error;
12358 for (i = 0; i < bmap->n_eq; ++i) {
12359 k = isl_basic_map_alloc_equality(res);
12360 if (k < 0)
12361 goto error;
12362 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12363 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12366 for (i = 0; i < bmap->n_ineq; ++i) {
12367 k = isl_basic_map_alloc_inequality(res);
12368 if (k < 0)
12369 goto error;
12370 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12371 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12374 for (i = 0; i < bmap->n_div; ++i) {
12375 if (isl_int_is_zero(bmap->div[i][0])) {
12376 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12377 continue;
12379 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12380 n_before, n_after, n_div_ma, n_div_bmap,
12381 f, c1, c2, g, 1);
12384 if (strides)
12385 res = add_ma_strides(res, ma, n_before, n_after);
12387 isl_int_clear(f);
12388 isl_int_clear(c1);
12389 isl_int_clear(c2);
12390 isl_int_clear(g);
12391 isl_basic_map_free(bmap);
12392 isl_multi_aff_free(ma);
12393 res = isl_basic_set_simplify(res);
12394 return isl_basic_map_finalize(res);
12395 error:
12396 isl_int_clear(f);
12397 isl_int_clear(c1);
12398 isl_int_clear(c2);
12399 isl_int_clear(g);
12400 isl_basic_map_free(bmap);
12401 isl_multi_aff_free(ma);
12402 isl_basic_map_free(res);
12403 return NULL;
12406 /* Compute the preimage of "bset" under the function represented by "ma".
12407 * In other words, plug in "ma" in "bset". The result is a basic set
12408 * that lives in the domain space of "ma".
12410 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12411 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12413 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12416 /* Compute the preimage of the domain of "bmap" under the function
12417 * represented by "ma".
12418 * In other words, plug in "ma" in the domain of "bmap".
12419 * The result is a basic map that lives in the same space as "bmap"
12420 * except that the domain has been replaced by the domain space of "ma".
12422 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12423 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12425 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12428 /* Compute the preimage of the range of "bmap" under the function
12429 * represented by "ma".
12430 * In other words, plug in "ma" in the range of "bmap".
12431 * The result is a basic map that lives in the same space as "bmap"
12432 * except that the range has been replaced by the domain space of "ma".
12434 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12435 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12437 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12440 /* Check if the range of "ma" is compatible with the domain or range
12441 * (depending on "type") of "map".
12442 * Return -1 if anything is wrong.
12444 static int check_map_compatible_range_multi_aff(
12445 __isl_keep isl_map *map, enum isl_dim_type type,
12446 __isl_keep isl_multi_aff *ma)
12448 int m;
12449 isl_space *ma_space;
12451 ma_space = isl_multi_aff_get_space(ma);
12452 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12453 isl_space_free(ma_space);
12454 if (m >= 0 && !m)
12455 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12456 "spaces don't match", return -1);
12457 return m;
12460 /* Compute the preimage of the domain or range (depending on "type")
12461 * of "map" under the function represented by "ma".
12462 * In other words, plug in "ma" in the domain or range of "map".
12463 * The result is a map that lives in the same space as "map"
12464 * except that the domain or range has been replaced by
12465 * the domain space of "ma".
12467 * The parameters are assumed to have been aligned.
12469 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12470 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12472 int i;
12473 isl_space *space;
12475 map = isl_map_cow(map);
12476 ma = isl_multi_aff_align_divs(ma);
12477 if (!map || !ma)
12478 goto error;
12479 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12480 goto error;
12482 for (i = 0; i < map->n; ++i) {
12483 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12484 isl_multi_aff_copy(ma));
12485 if (!map->p[i])
12486 goto error;
12489 space = isl_multi_aff_get_domain_space(ma);
12490 space = isl_space_set(isl_map_get_space(map), type, space);
12492 isl_space_free(map->dim);
12493 map->dim = space;
12494 if (!map->dim)
12495 goto error;
12497 isl_multi_aff_free(ma);
12498 if (map->n > 1)
12499 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12500 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12501 return map;
12502 error:
12503 isl_multi_aff_free(ma);
12504 isl_map_free(map);
12505 return NULL;
12508 /* Compute the preimage of the domain or range (depending on "type")
12509 * of "map" under the function represented by "ma".
12510 * In other words, plug in "ma" in the domain or range of "map".
12511 * The result is a map that lives in the same space as "map"
12512 * except that the domain or range has been replaced by
12513 * the domain space of "ma".
12515 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12516 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12518 if (!map || !ma)
12519 goto error;
12521 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12522 return map_preimage_multi_aff(map, type, ma);
12524 if (!isl_space_has_named_params(map->dim) ||
12525 !isl_space_has_named_params(ma->space))
12526 isl_die(map->ctx, isl_error_invalid,
12527 "unaligned unnamed parameters", goto error);
12528 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12529 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12531 return map_preimage_multi_aff(map, type, ma);
12532 error:
12533 isl_multi_aff_free(ma);
12534 return isl_map_free(map);
12537 /* Compute the preimage of "set" under the function represented by "ma".
12538 * In other words, plug in "ma" in "set". The result is a set
12539 * that lives in the domain space of "ma".
12541 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12542 __isl_take isl_multi_aff *ma)
12544 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12547 /* Compute the preimage of the domain of "map" under the function
12548 * represented by "ma".
12549 * In other words, plug in "ma" in the domain of "map".
12550 * The result is a map that lives in the same space as "map"
12551 * except that the domain has been replaced by the domain space of "ma".
12553 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12554 __isl_take isl_multi_aff *ma)
12556 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12559 /* Compute the preimage of the range of "map" under the function
12560 * represented by "ma".
12561 * In other words, plug in "ma" in the range of "map".
12562 * The result is a map that lives in the same space as "map"
12563 * except that the range has been replaced by the domain space of "ma".
12565 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12566 __isl_take isl_multi_aff *ma)
12568 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12571 /* Compute the preimage of "map" under the function represented by "pma".
12572 * In other words, plug in "pma" in the domain or range of "map".
12573 * The result is a map that lives in the same space as "map",
12574 * except that the space of type "type" has been replaced by
12575 * the domain space of "pma".
12577 * The parameters of "map" and "pma" are assumed to have been aligned.
12579 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12580 __isl_take isl_map *map, enum isl_dim_type type,
12581 __isl_take isl_pw_multi_aff *pma)
12583 int i;
12584 isl_map *res;
12586 if (!pma)
12587 goto error;
12589 if (pma->n == 0) {
12590 isl_pw_multi_aff_free(pma);
12591 res = isl_map_empty(isl_map_get_space(map));
12592 isl_map_free(map);
12593 return res;
12596 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12597 isl_multi_aff_copy(pma->p[0].maff));
12598 if (type == isl_dim_in)
12599 res = isl_map_intersect_domain(res,
12600 isl_map_copy(pma->p[0].set));
12601 else
12602 res = isl_map_intersect_range(res,
12603 isl_map_copy(pma->p[0].set));
12605 for (i = 1; i < pma->n; ++i) {
12606 isl_map *res_i;
12608 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12609 isl_multi_aff_copy(pma->p[i].maff));
12610 if (type == isl_dim_in)
12611 res_i = isl_map_intersect_domain(res_i,
12612 isl_map_copy(pma->p[i].set));
12613 else
12614 res_i = isl_map_intersect_range(res_i,
12615 isl_map_copy(pma->p[i].set));
12616 res = isl_map_union(res, res_i);
12619 isl_pw_multi_aff_free(pma);
12620 isl_map_free(map);
12621 return res;
12622 error:
12623 isl_pw_multi_aff_free(pma);
12624 isl_map_free(map);
12625 return NULL;
12628 /* Compute the preimage of "map" under the function represented by "pma".
12629 * In other words, plug in "pma" in the domain or range of "map".
12630 * The result is a map that lives in the same space as "map",
12631 * except that the space of type "type" has been replaced by
12632 * the domain space of "pma".
12634 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12635 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12637 if (!map || !pma)
12638 goto error;
12640 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12641 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12643 if (!isl_space_has_named_params(map->dim) ||
12644 !isl_space_has_named_params(pma->dim))
12645 isl_die(map->ctx, isl_error_invalid,
12646 "unaligned unnamed parameters", goto error);
12647 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12648 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12650 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12651 error:
12652 isl_pw_multi_aff_free(pma);
12653 return isl_map_free(map);
12656 /* Compute the preimage of "set" under the function represented by "pma".
12657 * In other words, plug in "pma" in "set". The result is a set
12658 * that lives in the domain space of "pma".
12660 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12661 __isl_take isl_pw_multi_aff *pma)
12663 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12666 /* Compute the preimage of the domain of "map" under the function
12667 * represented by "pma".
12668 * In other words, plug in "pma" in the domain of "map".
12669 * The result is a map that lives in the same space as "map",
12670 * except that domain space has been replaced by the domain space of "pma".
12672 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12673 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12675 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12678 /* Compute the preimage of the range of "map" under the function
12679 * represented by "pma".
12680 * In other words, plug in "pma" in the range of "map".
12681 * The result is a map that lives in the same space as "map",
12682 * except that range space has been replaced by the domain space of "pma".
12684 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12685 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12687 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12690 /* Compute the preimage of "map" under the function represented by "mpa".
12691 * In other words, plug in "mpa" in the domain or range of "map".
12692 * The result is a map that lives in the same space as "map",
12693 * except that the space of type "type" has been replaced by
12694 * the domain space of "mpa".
12696 * If the map does not involve any constraints that refer to the
12697 * dimensions of the substituted space, then the only possible
12698 * effect of "mpa" on the map is to map the space to a different space.
12699 * We create a separate isl_multi_aff to effectuate this change
12700 * in order to avoid spurious splitting of the map along the pieces
12701 * of "mpa".
12703 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12704 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12706 int n;
12707 isl_pw_multi_aff *pma;
12709 if (!map || !mpa)
12710 goto error;
12712 n = isl_map_dim(map, type);
12713 if (!isl_map_involves_dims(map, type, 0, n)) {
12714 isl_space *space;
12715 isl_multi_aff *ma;
12717 space = isl_multi_pw_aff_get_space(mpa);
12718 isl_multi_pw_aff_free(mpa);
12719 ma = isl_multi_aff_zero(space);
12720 return isl_map_preimage_multi_aff(map, type, ma);
12723 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12724 return isl_map_preimage_pw_multi_aff(map, type, pma);
12725 error:
12726 isl_map_free(map);
12727 isl_multi_pw_aff_free(mpa);
12728 return NULL;
12731 /* Compute the preimage of "map" under the function represented by "mpa".
12732 * In other words, plug in "mpa" in the domain "map".
12733 * The result is a map that lives in the same space as "map",
12734 * except that domain space has been replaced by the domain space of "mpa".
12736 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12737 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12739 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12742 /* Compute the preimage of "set" by the function represented by "mpa".
12743 * In other words, plug in "mpa" in "set".
12745 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12746 __isl_take isl_multi_pw_aff *mpa)
12748 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);