add isl_union_map_involves_dims
[isl.git] / isl_map.c
blob6c50ab818c0ad199af4c721b77af787de4d8bf14
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 /* Return the position of the dimension of the given type and name
719 * in "bmap".
720 * Return -1 if no such dimension can be found.
722 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
723 enum isl_dim_type type, const char *name)
725 if (!bmap)
726 return -1;
727 return isl_space_find_dim_by_name(bmap->dim, type, name);
730 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
731 const char *name)
733 if (!map)
734 return -1;
735 return isl_space_find_dim_by_name(map->dim, type, name);
738 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
739 const char *name)
741 return isl_map_find_dim_by_name(set, type, name);
744 /* Reset the user pointer on all identifiers of parameters and tuples
745 * of the space of "map".
747 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
749 isl_space *space;
751 space = isl_map_get_space(map);
752 space = isl_space_reset_user(space);
753 map = isl_map_reset_space(map, space);
755 return map;
758 /* Reset the user pointer on all identifiers of parameters and tuples
759 * of the space of "set".
761 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
763 return isl_map_reset_user(set);
766 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
768 if (!bmap)
769 return -1;
770 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
773 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
775 return isl_basic_map_is_rational(bset);
778 /* Does "bmap" contain any rational points?
780 * If "bmap" has an equality for each dimension, equating the dimension
781 * to an integer constant, then it has no rational points, even if it
782 * is marked as rational.
784 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
786 int has_rational = 1;
787 unsigned total;
789 if (!bmap)
790 return -1;
791 if (isl_basic_map_plain_is_empty(bmap))
792 return 0;
793 if (!isl_basic_map_is_rational(bmap))
794 return 0;
795 bmap = isl_basic_map_copy(bmap);
796 bmap = isl_basic_map_implicit_equalities(bmap);
797 if (!bmap)
798 return -1;
799 total = isl_basic_map_total_dim(bmap);
800 if (bmap->n_eq == total) {
801 int i, j;
802 for (i = 0; i < bmap->n_eq; ++i) {
803 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
804 if (j < 0)
805 break;
806 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
807 !isl_int_is_negone(bmap->eq[i][1 + j]))
808 break;
809 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
810 total - j - 1);
811 if (j >= 0)
812 break;
814 if (i == bmap->n_eq)
815 has_rational = 0;
817 isl_basic_map_free(bmap);
819 return has_rational;
822 /* Does "map" contain any rational points?
824 int isl_map_has_rational(__isl_keep isl_map *map)
826 int i;
827 int has_rational;
829 if (!map)
830 return -1;
831 for (i = 0; i < map->n; ++i) {
832 has_rational = isl_basic_map_has_rational(map->p[i]);
833 if (has_rational < 0)
834 return -1;
835 if (has_rational)
836 return 1;
838 return 0;
841 /* Does "set" contain any rational points?
843 int isl_set_has_rational(__isl_keep isl_set *set)
845 return isl_map_has_rational(set);
848 /* Is this basic set a parameter domain?
850 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
852 if (!bset)
853 return -1;
854 return isl_space_is_params(bset->dim);
857 /* Is this set a parameter domain?
859 int isl_set_is_params(__isl_keep isl_set *set)
861 if (!set)
862 return -1;
863 return isl_space_is_params(set->dim);
866 /* Is this map actually a parameter domain?
867 * Users should never call this function. Outside of isl,
868 * a map can never be a parameter domain.
870 int isl_map_is_params(__isl_keep isl_map *map)
872 if (!map)
873 return -1;
874 return isl_space_is_params(map->dim);
877 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
878 struct isl_basic_map *bmap, unsigned extra,
879 unsigned n_eq, unsigned n_ineq)
881 int i;
882 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
884 bmap->ctx = ctx;
885 isl_ctx_ref(ctx);
887 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
888 if (isl_blk_is_error(bmap->block))
889 goto error;
891 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
892 if ((n_ineq + n_eq) && !bmap->ineq)
893 goto error;
895 if (extra == 0) {
896 bmap->block2 = isl_blk_empty();
897 bmap->div = NULL;
898 } else {
899 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
900 if (isl_blk_is_error(bmap->block2))
901 goto error;
903 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
904 if (!bmap->div)
905 goto error;
908 for (i = 0; i < n_ineq + n_eq; ++i)
909 bmap->ineq[i] = bmap->block.data + i * row_size;
911 for (i = 0; i < extra; ++i)
912 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
914 bmap->ref = 1;
915 bmap->flags = 0;
916 bmap->c_size = n_eq + n_ineq;
917 bmap->eq = bmap->ineq + n_ineq;
918 bmap->extra = extra;
919 bmap->n_eq = 0;
920 bmap->n_ineq = 0;
921 bmap->n_div = 0;
922 bmap->sample = NULL;
924 return bmap;
925 error:
926 isl_basic_map_free(bmap);
927 return NULL;
930 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
931 unsigned nparam, unsigned dim, unsigned extra,
932 unsigned n_eq, unsigned n_ineq)
934 struct isl_basic_map *bmap;
935 isl_space *space;
937 space = isl_space_set_alloc(ctx, nparam, dim);
938 if (!space)
939 return NULL;
941 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
942 return (struct isl_basic_set *)bmap;
945 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
946 unsigned extra, unsigned n_eq, unsigned n_ineq)
948 struct isl_basic_map *bmap;
949 if (!dim)
950 return NULL;
951 isl_assert(dim->ctx, dim->n_in == 0, goto error);
952 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
953 return (struct isl_basic_set *)bmap;
954 error:
955 isl_space_free(dim);
956 return NULL;
959 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
960 unsigned extra, unsigned n_eq, unsigned n_ineq)
962 struct isl_basic_map *bmap;
964 if (!dim)
965 return NULL;
966 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
967 if (!bmap)
968 goto error;
969 bmap->dim = dim;
971 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
972 error:
973 isl_space_free(dim);
974 return NULL;
977 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
978 unsigned nparam, unsigned in, unsigned out, unsigned extra,
979 unsigned n_eq, unsigned n_ineq)
981 struct isl_basic_map *bmap;
982 isl_space *dim;
984 dim = isl_space_alloc(ctx, nparam, in, out);
985 if (!dim)
986 return NULL;
988 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
989 return bmap;
992 static void dup_constraints(
993 struct isl_basic_map *dst, struct isl_basic_map *src)
995 int i;
996 unsigned total = isl_basic_map_total_dim(src);
998 for (i = 0; i < src->n_eq; ++i) {
999 int j = isl_basic_map_alloc_equality(dst);
1000 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1003 for (i = 0; i < src->n_ineq; ++i) {
1004 int j = isl_basic_map_alloc_inequality(dst);
1005 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1008 for (i = 0; i < src->n_div; ++i) {
1009 int j = isl_basic_map_alloc_div(dst);
1010 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1012 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1015 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1017 struct isl_basic_map *dup;
1019 if (!bmap)
1020 return NULL;
1021 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1022 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1023 if (!dup)
1024 return NULL;
1025 dup_constraints(dup, bmap);
1026 dup->flags = bmap->flags;
1027 dup->sample = isl_vec_copy(bmap->sample);
1028 return dup;
1031 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1033 struct isl_basic_map *dup;
1035 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1036 return (struct isl_basic_set *)dup;
1039 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1041 if (!bset)
1042 return NULL;
1044 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1045 bset->ref++;
1046 return bset;
1048 return isl_basic_set_dup(bset);
1051 struct isl_set *isl_set_copy(struct isl_set *set)
1053 if (!set)
1054 return NULL;
1056 set->ref++;
1057 return set;
1060 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1062 if (!bmap)
1063 return NULL;
1065 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1066 bmap->ref++;
1067 return bmap;
1069 bmap = isl_basic_map_dup(bmap);
1070 if (bmap)
1071 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1072 return bmap;
1075 struct isl_map *isl_map_copy(struct isl_map *map)
1077 if (!map)
1078 return NULL;
1080 map->ref++;
1081 return map;
1084 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1086 if (!bmap)
1087 return NULL;
1089 if (--bmap->ref > 0)
1090 return NULL;
1092 isl_ctx_deref(bmap->ctx);
1093 free(bmap->div);
1094 isl_blk_free(bmap->ctx, bmap->block2);
1095 free(bmap->ineq);
1096 isl_blk_free(bmap->ctx, bmap->block);
1097 isl_vec_free(bmap->sample);
1098 isl_space_free(bmap->dim);
1099 free(bmap);
1101 return NULL;
1104 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1106 return isl_basic_map_free((struct isl_basic_map *)bset);
1109 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1111 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1114 __isl_give isl_map *isl_map_align_params_map_map_and(
1115 __isl_take isl_map *map1, __isl_take isl_map *map2,
1116 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1117 __isl_take isl_map *map2))
1119 if (!map1 || !map2)
1120 goto error;
1121 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1122 return fn(map1, map2);
1123 if (!isl_space_has_named_params(map1->dim) ||
1124 !isl_space_has_named_params(map2->dim))
1125 isl_die(map1->ctx, isl_error_invalid,
1126 "unaligned unnamed parameters", goto error);
1127 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1128 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1129 return fn(map1, map2);
1130 error:
1131 isl_map_free(map1);
1132 isl_map_free(map2);
1133 return NULL;
1136 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1137 __isl_keep isl_map *map2,
1138 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1140 int r;
1142 if (!map1 || !map2)
1143 return -1;
1144 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1145 return fn(map1, map2);
1146 if (!isl_space_has_named_params(map1->dim) ||
1147 !isl_space_has_named_params(map2->dim))
1148 isl_die(map1->ctx, isl_error_invalid,
1149 "unaligned unnamed parameters", return -1);
1150 map1 = isl_map_copy(map1);
1151 map2 = isl_map_copy(map2);
1152 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1153 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1154 r = fn(map1, map2);
1155 isl_map_free(map1);
1156 isl_map_free(map2);
1157 return r;
1160 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1162 struct isl_ctx *ctx;
1163 if (!bmap)
1164 return -1;
1165 ctx = bmap->ctx;
1166 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1167 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1168 return -1);
1169 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1170 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1171 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1172 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1173 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1174 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1175 isl_int *t;
1176 int j = isl_basic_map_alloc_inequality(bmap);
1177 if (j < 0)
1178 return -1;
1179 t = bmap->ineq[j];
1180 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1181 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1182 bmap->eq[-1] = t;
1183 bmap->n_eq++;
1184 bmap->n_ineq--;
1185 bmap->eq--;
1186 return 0;
1188 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1189 bmap->extra - bmap->n_div);
1190 return bmap->n_eq++;
1193 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1195 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1198 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1200 if (!bmap)
1201 return -1;
1202 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1203 bmap->n_eq -= n;
1204 return 0;
1207 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1209 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1212 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1214 isl_int *t;
1215 if (!bmap)
1216 return -1;
1217 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1219 if (pos != bmap->n_eq - 1) {
1220 t = bmap->eq[pos];
1221 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1222 bmap->eq[bmap->n_eq - 1] = t;
1224 bmap->n_eq--;
1225 return 0;
1228 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1230 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1233 /* Turn inequality "pos" of "bmap" into an equality.
1235 * In particular, we move the inequality in front of the equalities
1236 * and move the last inequality in the position of the moved inequality.
1237 * Note that isl_tab_make_equalities_explicit depends on this particular
1238 * change in the ordering of the constraints.
1240 void isl_basic_map_inequality_to_equality(
1241 struct isl_basic_map *bmap, unsigned pos)
1243 isl_int *t;
1245 t = bmap->ineq[pos];
1246 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1247 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1248 bmap->eq[-1] = t;
1249 bmap->n_eq++;
1250 bmap->n_ineq--;
1251 bmap->eq--;
1252 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1253 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1254 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1255 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1258 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1260 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1263 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1265 struct isl_ctx *ctx;
1266 if (!bmap)
1267 return -1;
1268 ctx = bmap->ctx;
1269 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1270 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1272 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1273 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1274 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1275 1 + isl_basic_map_total_dim(bmap),
1276 bmap->extra - bmap->n_div);
1277 return bmap->n_ineq++;
1280 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1282 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1285 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1287 if (!bmap)
1288 return -1;
1289 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1290 bmap->n_ineq -= n;
1291 return 0;
1294 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1296 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1299 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1301 isl_int *t;
1302 if (!bmap)
1303 return -1;
1304 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1306 if (pos != bmap->n_ineq - 1) {
1307 t = bmap->ineq[pos];
1308 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1309 bmap->ineq[bmap->n_ineq - 1] = t;
1310 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1312 bmap->n_ineq--;
1313 return 0;
1316 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1318 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1321 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1322 isl_int *eq)
1324 int k;
1326 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1327 if (!bmap)
1328 return NULL;
1329 k = isl_basic_map_alloc_equality(bmap);
1330 if (k < 0)
1331 goto error;
1332 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1333 return bmap;
1334 error:
1335 isl_basic_map_free(bmap);
1336 return NULL;
1339 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1340 isl_int *eq)
1342 return (isl_basic_set *)
1343 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1346 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1347 isl_int *ineq)
1349 int k;
1351 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1352 if (!bmap)
1353 return NULL;
1354 k = isl_basic_map_alloc_inequality(bmap);
1355 if (k < 0)
1356 goto error;
1357 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1358 return bmap;
1359 error:
1360 isl_basic_map_free(bmap);
1361 return NULL;
1364 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1365 isl_int *ineq)
1367 return (isl_basic_set *)
1368 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1371 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1373 if (!bmap)
1374 return -1;
1375 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1376 isl_seq_clr(bmap->div[bmap->n_div] +
1377 1 + 1 + isl_basic_map_total_dim(bmap),
1378 bmap->extra - bmap->n_div);
1379 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1380 return bmap->n_div++;
1383 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1385 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1388 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1390 if (!bmap)
1391 return -1;
1392 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1393 bmap->n_div -= n;
1394 return 0;
1397 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1399 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1402 /* Copy constraint from src to dst, putting the vars of src at offset
1403 * dim_off in dst and the divs of src at offset div_off in dst.
1404 * If both sets are actually map, then dim_off applies to the input
1405 * variables.
1407 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1408 struct isl_basic_map *src_map, isl_int *src,
1409 unsigned in_off, unsigned out_off, unsigned div_off)
1411 unsigned src_nparam = isl_basic_map_n_param(src_map);
1412 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1413 unsigned src_in = isl_basic_map_n_in(src_map);
1414 unsigned dst_in = isl_basic_map_n_in(dst_map);
1415 unsigned src_out = isl_basic_map_n_out(src_map);
1416 unsigned dst_out = isl_basic_map_n_out(dst_map);
1417 isl_int_set(dst[0], src[0]);
1418 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1419 if (dst_nparam > src_nparam)
1420 isl_seq_clr(dst+1+src_nparam,
1421 dst_nparam - src_nparam);
1422 isl_seq_clr(dst+1+dst_nparam, in_off);
1423 isl_seq_cpy(dst+1+dst_nparam+in_off,
1424 src+1+src_nparam,
1425 isl_min(dst_in-in_off, src_in));
1426 if (dst_in-in_off > src_in)
1427 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1428 dst_in - in_off - src_in);
1429 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1430 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1431 src+1+src_nparam+src_in,
1432 isl_min(dst_out-out_off, src_out));
1433 if (dst_out-out_off > src_out)
1434 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1435 dst_out - out_off - src_out);
1436 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1437 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1438 src+1+src_nparam+src_in+src_out,
1439 isl_min(dst_map->extra-div_off, src_map->n_div));
1440 if (dst_map->n_div-div_off > src_map->n_div)
1441 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1442 div_off+src_map->n_div,
1443 dst_map->n_div - div_off - src_map->n_div);
1446 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1447 struct isl_basic_map *src_map, isl_int *src,
1448 unsigned in_off, unsigned out_off, unsigned div_off)
1450 isl_int_set(dst[0], src[0]);
1451 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1454 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1455 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1457 int i;
1458 unsigned div_off;
1460 if (!bmap1 || !bmap2)
1461 goto error;
1463 div_off = bmap1->n_div;
1465 for (i = 0; i < bmap2->n_eq; ++i) {
1466 int i1 = isl_basic_map_alloc_equality(bmap1);
1467 if (i1 < 0)
1468 goto error;
1469 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1470 i_pos, o_pos, div_off);
1473 for (i = 0; i < bmap2->n_ineq; ++i) {
1474 int i1 = isl_basic_map_alloc_inequality(bmap1);
1475 if (i1 < 0)
1476 goto error;
1477 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1478 i_pos, o_pos, div_off);
1481 for (i = 0; i < bmap2->n_div; ++i) {
1482 int i1 = isl_basic_map_alloc_div(bmap1);
1483 if (i1 < 0)
1484 goto error;
1485 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1486 i_pos, o_pos, div_off);
1489 isl_basic_map_free(bmap2);
1491 return bmap1;
1493 error:
1494 isl_basic_map_free(bmap1);
1495 isl_basic_map_free(bmap2);
1496 return NULL;
1499 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1500 struct isl_basic_set *bset2, unsigned pos)
1502 return (struct isl_basic_set *)
1503 add_constraints((struct isl_basic_map *)bset1,
1504 (struct isl_basic_map *)bset2, 0, pos);
1507 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1508 __isl_take isl_space *dim, unsigned extra,
1509 unsigned n_eq, unsigned n_ineq)
1511 struct isl_basic_map *ext;
1512 unsigned flags;
1513 int dims_ok;
1515 if (!dim)
1516 goto error;
1518 if (!base)
1519 goto error;
1521 dims_ok = isl_space_is_equal(base->dim, dim) &&
1522 base->extra >= base->n_div + extra;
1524 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1525 room_for_ineq(base, n_ineq)) {
1526 isl_space_free(dim);
1527 return base;
1530 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1531 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1532 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1533 extra += base->extra;
1534 n_eq += base->n_eq;
1535 n_ineq += base->n_ineq;
1537 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1538 dim = NULL;
1539 if (!ext)
1540 goto error;
1542 if (dims_ok)
1543 ext->sample = isl_vec_copy(base->sample);
1544 flags = base->flags;
1545 ext = add_constraints(ext, base, 0, 0);
1546 if (ext) {
1547 ext->flags = flags;
1548 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1551 return ext;
1553 error:
1554 isl_space_free(dim);
1555 isl_basic_map_free(base);
1556 return NULL;
1559 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1560 __isl_take isl_space *dim, unsigned extra,
1561 unsigned n_eq, unsigned n_ineq)
1563 return (struct isl_basic_set *)
1564 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1565 extra, n_eq, n_ineq);
1568 struct isl_basic_map *isl_basic_map_extend_constraints(
1569 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1571 if (!base)
1572 return NULL;
1573 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1574 0, n_eq, n_ineq);
1577 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1578 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1579 unsigned n_eq, unsigned n_ineq)
1581 struct isl_basic_map *bmap;
1582 isl_space *dim;
1584 if (!base)
1585 return NULL;
1586 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1587 if (!dim)
1588 goto error;
1590 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1591 return bmap;
1592 error:
1593 isl_basic_map_free(base);
1594 return NULL;
1597 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1598 unsigned nparam, unsigned dim, unsigned extra,
1599 unsigned n_eq, unsigned n_ineq)
1601 return (struct isl_basic_set *)
1602 isl_basic_map_extend((struct isl_basic_map *)base,
1603 nparam, 0, dim, extra, n_eq, n_ineq);
1606 struct isl_basic_set *isl_basic_set_extend_constraints(
1607 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1609 return (struct isl_basic_set *)
1610 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1611 n_eq, n_ineq);
1614 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1616 return (struct isl_basic_set *)
1617 isl_basic_map_cow((struct isl_basic_map *)bset);
1620 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1622 if (!bmap)
1623 return NULL;
1625 if (bmap->ref > 1) {
1626 bmap->ref--;
1627 bmap = isl_basic_map_dup(bmap);
1629 if (bmap)
1630 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1631 return bmap;
1634 struct isl_set *isl_set_cow(struct isl_set *set)
1636 if (!set)
1637 return NULL;
1639 if (set->ref == 1)
1640 return set;
1641 set->ref--;
1642 return isl_set_dup(set);
1645 struct isl_map *isl_map_cow(struct isl_map *map)
1647 if (!map)
1648 return NULL;
1650 if (map->ref == 1)
1651 return map;
1652 map->ref--;
1653 return isl_map_dup(map);
1656 static void swap_vars(struct isl_blk blk, isl_int *a,
1657 unsigned a_len, unsigned b_len)
1659 isl_seq_cpy(blk.data, a+a_len, b_len);
1660 isl_seq_cpy(blk.data+b_len, a, a_len);
1661 isl_seq_cpy(a, blk.data, b_len+a_len);
1664 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1665 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1667 int i;
1668 struct isl_blk blk;
1670 if (!bmap)
1671 goto error;
1673 isl_assert(bmap->ctx,
1674 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1676 if (n1 == 0 || n2 == 0)
1677 return bmap;
1679 bmap = isl_basic_map_cow(bmap);
1680 if (!bmap)
1681 return NULL;
1683 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1684 if (isl_blk_is_error(blk))
1685 goto error;
1687 for (i = 0; i < bmap->n_eq; ++i)
1688 swap_vars(blk,
1689 bmap->eq[i] + pos, n1, n2);
1691 for (i = 0; i < bmap->n_ineq; ++i)
1692 swap_vars(blk,
1693 bmap->ineq[i] + pos, n1, n2);
1695 for (i = 0; i < bmap->n_div; ++i)
1696 swap_vars(blk,
1697 bmap->div[i]+1 + pos, n1, n2);
1699 isl_blk_free(bmap->ctx, blk);
1701 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1702 bmap = isl_basic_map_gauss(bmap, NULL);
1703 return isl_basic_map_finalize(bmap);
1704 error:
1705 isl_basic_map_free(bmap);
1706 return NULL;
1709 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1710 __isl_take isl_basic_set *bset, unsigned n)
1712 unsigned dim;
1713 unsigned nparam;
1715 if (!bset)
1716 return NULL;
1718 nparam = isl_basic_set_n_param(bset);
1719 dim = isl_basic_set_n_dim(bset);
1720 isl_assert(bset->ctx, n <= dim, goto error);
1722 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1723 error:
1724 isl_basic_set_free(bset);
1725 return NULL;
1728 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1730 int i = 0;
1731 unsigned total;
1732 if (!bmap)
1733 goto error;
1734 total = isl_basic_map_total_dim(bmap);
1735 isl_basic_map_free_div(bmap, bmap->n_div);
1736 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1737 if (bmap->n_eq > 0)
1738 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1739 else {
1740 i = isl_basic_map_alloc_equality(bmap);
1741 if (i < 0)
1742 goto error;
1744 isl_int_set_si(bmap->eq[i][0], 1);
1745 isl_seq_clr(bmap->eq[i]+1, total);
1746 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1747 isl_vec_free(bmap->sample);
1748 bmap->sample = NULL;
1749 return isl_basic_map_finalize(bmap);
1750 error:
1751 isl_basic_map_free(bmap);
1752 return NULL;
1755 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1757 return (struct isl_basic_set *)
1758 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1761 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1762 * of "bmap").
1764 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1766 isl_int *t = bmap->div[a];
1767 bmap->div[a] = bmap->div[b];
1768 bmap->div[b] = t;
1771 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1772 * div definitions accordingly.
1774 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1776 int i;
1777 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1779 swap_div(bmap, a, b);
1781 for (i = 0; i < bmap->n_eq; ++i)
1782 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1784 for (i = 0; i < bmap->n_ineq; ++i)
1785 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1787 for (i = 0; i < bmap->n_div; ++i)
1788 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1789 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1792 /* Eliminate the specified n dimensions starting at first from the
1793 * constraints, without removing the dimensions from the space.
1794 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1796 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1797 enum isl_dim_type type, unsigned first, unsigned n)
1799 int i;
1801 if (!map)
1802 return NULL;
1803 if (n == 0)
1804 return map;
1806 if (first + n > isl_map_dim(map, type) || first + n < first)
1807 isl_die(map->ctx, isl_error_invalid,
1808 "index out of bounds", goto error);
1810 map = isl_map_cow(map);
1811 if (!map)
1812 return NULL;
1814 for (i = 0; i < map->n; ++i) {
1815 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1816 if (!map->p[i])
1817 goto error;
1819 return map;
1820 error:
1821 isl_map_free(map);
1822 return NULL;
1825 /* Eliminate the specified n dimensions starting at first from the
1826 * constraints, without removing the dimensions from the space.
1827 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1829 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1830 enum isl_dim_type type, unsigned first, unsigned n)
1832 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1835 /* Eliminate the specified n dimensions starting at first from the
1836 * constraints, without removing the dimensions from the space.
1837 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1839 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1840 unsigned first, unsigned n)
1842 return isl_set_eliminate(set, isl_dim_set, first, n);
1845 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1846 __isl_take isl_basic_map *bmap)
1848 if (!bmap)
1849 return NULL;
1850 bmap = isl_basic_map_eliminate_vars(bmap,
1851 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1852 if (!bmap)
1853 return NULL;
1854 bmap->n_div = 0;
1855 return isl_basic_map_finalize(bmap);
1858 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1859 __isl_take isl_basic_set *bset)
1861 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1862 (struct isl_basic_map *)bset);
1865 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1867 int i;
1869 if (!map)
1870 return NULL;
1871 if (map->n == 0)
1872 return map;
1874 map = isl_map_cow(map);
1875 if (!map)
1876 return NULL;
1878 for (i = 0; i < map->n; ++i) {
1879 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1880 if (!map->p[i])
1881 goto error;
1883 return map;
1884 error:
1885 isl_map_free(map);
1886 return NULL;
1889 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1891 return isl_map_remove_divs(set);
1894 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1895 enum isl_dim_type type, unsigned first, unsigned n)
1897 if (!bmap)
1898 return NULL;
1899 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1900 goto error);
1901 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1902 return bmap;
1903 bmap = isl_basic_map_eliminate_vars(bmap,
1904 isl_basic_map_offset(bmap, type) - 1 + first, n);
1905 if (!bmap)
1906 return bmap;
1907 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1908 return bmap;
1909 bmap = isl_basic_map_drop(bmap, type, first, n);
1910 return bmap;
1911 error:
1912 isl_basic_map_free(bmap);
1913 return NULL;
1916 /* Return true if the definition of the given div (recursively) involves
1917 * any of the given variables.
1919 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1920 unsigned first, unsigned n)
1922 int i;
1923 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1925 if (isl_int_is_zero(bmap->div[div][0]))
1926 return 0;
1927 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1928 return 1;
1930 for (i = bmap->n_div - 1; i >= 0; --i) {
1931 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1932 continue;
1933 if (div_involves_vars(bmap, i, first, n))
1934 return 1;
1937 return 0;
1940 /* Try and add a lower and/or upper bound on "div" to "bmap"
1941 * based on inequality "i".
1942 * "total" is the total number of variables (excluding the divs).
1943 * "v" is a temporary object that can be used during the calculations.
1944 * If "lb" is set, then a lower bound should be constructed.
1945 * If "ub" is set, then an upper bound should be constructed.
1947 * The calling function has already checked that the inequality does not
1948 * reference "div", but we still need to check that the inequality is
1949 * of the right form. We'll consider the case where we want to construct
1950 * a lower bound. The construction of upper bounds is similar.
1952 * Let "div" be of the form
1954 * q = floor((a + f(x))/d)
1956 * We essentially check if constraint "i" is of the form
1958 * b + f(x) >= 0
1960 * so that we can use it to derive a lower bound on "div".
1961 * However, we allow a slightly more general form
1963 * b + g(x) >= 0
1965 * with the condition that the coefficients of g(x) - f(x) are all
1966 * divisible by d.
1967 * Rewriting this constraint as
1969 * 0 >= -b - g(x)
1971 * adding a + f(x) to both sides and dividing by d, we obtain
1973 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1975 * Taking the floor on both sides, we obtain
1977 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1979 * or
1981 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1983 * In the case of an upper bound, we construct the constraint
1985 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1988 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1989 __isl_take isl_basic_map *bmap, int div, int i,
1990 unsigned total, isl_int v, int lb, int ub)
1992 int j;
1994 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1995 if (lb) {
1996 isl_int_sub(v, bmap->ineq[i][1 + j],
1997 bmap->div[div][1 + 1 + j]);
1998 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2000 if (ub) {
2001 isl_int_add(v, bmap->ineq[i][1 + j],
2002 bmap->div[div][1 + 1 + j]);
2003 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2006 if (!lb && !ub)
2007 return bmap;
2009 bmap = isl_basic_map_cow(bmap);
2010 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2011 if (lb) {
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_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2017 bmap->div[div][1 + j]);
2018 isl_int_cdiv_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);
2023 if (ub) {
2024 int k = isl_basic_map_alloc_inequality(bmap);
2025 if (k < 0)
2026 goto error;
2027 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2028 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2029 bmap->div[div][1 + j]);
2030 isl_int_fdiv_q(bmap->ineq[k][j],
2031 bmap->ineq[k][j], bmap->div[div][0]);
2033 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2036 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2037 return bmap;
2038 error:
2039 isl_basic_map_free(bmap);
2040 return NULL;
2043 /* This function is called right before "div" is eliminated from "bmap"
2044 * using Fourier-Motzkin.
2045 * Look through the constraints of "bmap" for constraints on the argument
2046 * of the integer division and use them to construct constraints on the
2047 * integer division itself. These constraints can then be combined
2048 * during the Fourier-Motzkin elimination.
2049 * Note that it is only useful to introduce lower bounds on "div"
2050 * if "bmap" already contains upper bounds on "div" as the newly
2051 * introduce lower bounds can then be combined with the pre-existing
2052 * upper bounds. Similarly for upper bounds.
2053 * We therefore first check if "bmap" contains any lower and/or upper bounds
2054 * on "div".
2056 * It is interesting to note that the introduction of these constraints
2057 * can indeed lead to more accurate results, even when compared to
2058 * deriving constraints on the argument of "div" from constraints on "div".
2059 * Consider, for example, the set
2061 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2063 * The second constraint can be rewritten as
2065 * 2 * [(-i-2j+3)/4] + k >= 0
2067 * from which we can derive
2069 * -i - 2j + 3 >= -2k
2071 * or
2073 * i + 2j <= 3 + 2k
2075 * Combined with the first constraint, we obtain
2077 * -3 <= 3 + 2k or k >= -3
2079 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2080 * the first constraint, we obtain
2082 * [(i + 2j)/4] >= [-3/4] = -1
2084 * Combining this constraint with the second constraint, we obtain
2086 * k >= -2
2088 static __isl_give isl_basic_map *insert_bounds_on_div(
2089 __isl_take isl_basic_map *bmap, int div)
2091 int i;
2092 int check_lb, check_ub;
2093 isl_int v;
2094 unsigned total;
2096 if (!bmap)
2097 return NULL;
2099 if (isl_int_is_zero(bmap->div[div][0]))
2100 return bmap;
2102 total = isl_space_dim(bmap->dim, isl_dim_all);
2104 check_lb = 0;
2105 check_ub = 0;
2106 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2107 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2108 if (s > 0)
2109 check_ub = 1;
2110 if (s < 0)
2111 check_lb = 1;
2114 if (!check_lb && !check_ub)
2115 return bmap;
2117 isl_int_init(v);
2119 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2120 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2121 continue;
2123 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2124 check_lb, check_ub);
2127 isl_int_clear(v);
2129 return bmap;
2132 /* Remove all divs (recursively) involving any of the given dimensions
2133 * in their definitions.
2135 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2136 __isl_take isl_basic_map *bmap,
2137 enum isl_dim_type type, unsigned first, unsigned n)
2139 int i;
2141 if (!bmap)
2142 return NULL;
2143 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2144 goto error);
2145 first += isl_basic_map_offset(bmap, type);
2147 for (i = bmap->n_div - 1; i >= 0; --i) {
2148 if (!div_involves_vars(bmap, i, first, n))
2149 continue;
2150 bmap = insert_bounds_on_div(bmap, i);
2151 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2152 if (!bmap)
2153 return NULL;
2154 i = bmap->n_div;
2157 return bmap;
2158 error:
2159 isl_basic_map_free(bmap);
2160 return NULL;
2163 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2164 __isl_take isl_basic_set *bset,
2165 enum isl_dim_type type, unsigned first, unsigned n)
2167 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2170 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2171 enum isl_dim_type type, unsigned first, unsigned n)
2173 int i;
2175 if (!map)
2176 return NULL;
2177 if (map->n == 0)
2178 return map;
2180 map = isl_map_cow(map);
2181 if (!map)
2182 return NULL;
2184 for (i = 0; i < map->n; ++i) {
2185 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2186 type, first, n);
2187 if (!map->p[i])
2188 goto error;
2190 return map;
2191 error:
2192 isl_map_free(map);
2193 return NULL;
2196 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2197 enum isl_dim_type type, unsigned first, unsigned n)
2199 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2200 type, first, n);
2203 /* Does the desciption of "bmap" depend on the specified dimensions?
2204 * We also check whether the dimensions appear in any of the div definitions.
2205 * In principle there is no need for this check. If the dimensions appear
2206 * in a div definition, they also appear in the defining constraints of that
2207 * div.
2209 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2210 enum isl_dim_type type, unsigned first, unsigned n)
2212 int i;
2214 if (!bmap)
2215 return -1;
2217 if (first + n > isl_basic_map_dim(bmap, type))
2218 isl_die(bmap->ctx, isl_error_invalid,
2219 "index out of bounds", return -1);
2221 first += isl_basic_map_offset(bmap, type);
2222 for (i = 0; i < bmap->n_eq; ++i)
2223 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2224 return 1;
2225 for (i = 0; i < bmap->n_ineq; ++i)
2226 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2227 return 1;
2228 for (i = 0; i < bmap->n_div; ++i) {
2229 if (isl_int_is_zero(bmap->div[i][0]))
2230 continue;
2231 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2232 return 1;
2235 return 0;
2238 int isl_map_involves_dims(__isl_keep isl_map *map,
2239 enum isl_dim_type type, unsigned first, unsigned n)
2241 int i;
2243 if (!map)
2244 return -1;
2246 if (first + n > isl_map_dim(map, type))
2247 isl_die(map->ctx, isl_error_invalid,
2248 "index out of bounds", return -1);
2250 for (i = 0; i < map->n; ++i) {
2251 int involves = isl_basic_map_involves_dims(map->p[i],
2252 type, first, n);
2253 if (involves < 0 || involves)
2254 return involves;
2257 return 0;
2260 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2261 enum isl_dim_type type, unsigned first, unsigned n)
2263 return isl_basic_map_involves_dims(bset, type, first, n);
2266 int isl_set_involves_dims(__isl_keep isl_set *set,
2267 enum isl_dim_type type, unsigned first, unsigned n)
2269 return isl_map_involves_dims(set, type, first, n);
2272 /* Return true if the definition of the given div is unknown or depends
2273 * on unknown divs.
2275 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2277 int i;
2278 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2280 if (isl_int_is_zero(bmap->div[div][0]))
2281 return 1;
2283 for (i = bmap->n_div - 1; i >= 0; --i) {
2284 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2285 continue;
2286 if (div_is_unknown(bmap, i))
2287 return 1;
2290 return 0;
2293 /* Remove all divs that are unknown or defined in terms of unknown divs.
2295 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2296 __isl_take isl_basic_map *bmap)
2298 int i;
2300 if (!bmap)
2301 return NULL;
2303 for (i = bmap->n_div - 1; i >= 0; --i) {
2304 if (!div_is_unknown(bmap, i))
2305 continue;
2306 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2307 if (!bmap)
2308 return NULL;
2309 i = bmap->n_div;
2312 return bmap;
2315 /* Remove all divs that are unknown or defined in terms of unknown divs.
2317 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2318 __isl_take isl_basic_set *bset)
2320 return isl_basic_map_remove_unknown_divs(bset);
2323 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2325 int i;
2327 if (!map)
2328 return NULL;
2329 if (map->n == 0)
2330 return map;
2332 map = isl_map_cow(map);
2333 if (!map)
2334 return NULL;
2336 for (i = 0; i < map->n; ++i) {
2337 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2338 if (!map->p[i])
2339 goto error;
2341 return map;
2342 error:
2343 isl_map_free(map);
2344 return NULL;
2347 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2349 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2352 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2353 __isl_take isl_basic_set *bset,
2354 enum isl_dim_type type, unsigned first, unsigned n)
2356 return (isl_basic_set *)
2357 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2360 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2361 enum isl_dim_type type, unsigned first, unsigned n)
2363 int i;
2365 if (n == 0)
2366 return map;
2368 map = isl_map_cow(map);
2369 if (!map)
2370 return NULL;
2371 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2373 for (i = 0; i < map->n; ++i) {
2374 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2375 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2376 if (!map->p[i])
2377 goto error;
2379 map = isl_map_drop(map, type, first, n);
2380 return map;
2381 error:
2382 isl_map_free(map);
2383 return NULL;
2386 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2387 enum isl_dim_type type, unsigned first, unsigned n)
2389 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2392 /* Project out n inputs starting at first using Fourier-Motzkin */
2393 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2394 unsigned first, unsigned n)
2396 return isl_map_remove_dims(map, isl_dim_in, first, n);
2399 static void dump_term(struct isl_basic_map *bmap,
2400 isl_int c, int pos, FILE *out)
2402 const char *name;
2403 unsigned in = isl_basic_map_n_in(bmap);
2404 unsigned dim = in + isl_basic_map_n_out(bmap);
2405 unsigned nparam = isl_basic_map_n_param(bmap);
2406 if (!pos)
2407 isl_int_print(out, c, 0);
2408 else {
2409 if (!isl_int_is_one(c))
2410 isl_int_print(out, c, 0);
2411 if (pos < 1 + nparam) {
2412 name = isl_space_get_dim_name(bmap->dim,
2413 isl_dim_param, pos - 1);
2414 if (name)
2415 fprintf(out, "%s", name);
2416 else
2417 fprintf(out, "p%d", pos - 1);
2418 } else if (pos < 1 + nparam + in)
2419 fprintf(out, "i%d", pos - 1 - nparam);
2420 else if (pos < 1 + nparam + dim)
2421 fprintf(out, "o%d", pos - 1 - nparam - in);
2422 else
2423 fprintf(out, "e%d", pos - 1 - nparam - dim);
2427 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2428 int sign, FILE *out)
2430 int i;
2431 int first;
2432 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2433 isl_int v;
2435 isl_int_init(v);
2436 for (i = 0, first = 1; i < len; ++i) {
2437 if (isl_int_sgn(c[i]) * sign <= 0)
2438 continue;
2439 if (!first)
2440 fprintf(out, " + ");
2441 first = 0;
2442 isl_int_abs(v, c[i]);
2443 dump_term(bmap, v, i, out);
2445 isl_int_clear(v);
2446 if (first)
2447 fprintf(out, "0");
2450 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2451 const char *op, FILE *out, int indent)
2453 int i;
2455 fprintf(out, "%*s", indent, "");
2457 dump_constraint_sign(bmap, c, 1, out);
2458 fprintf(out, " %s ", op);
2459 dump_constraint_sign(bmap, c, -1, out);
2461 fprintf(out, "\n");
2463 for (i = bmap->n_div; i < bmap->extra; ++i) {
2464 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2465 continue;
2466 fprintf(out, "%*s", indent, "");
2467 fprintf(out, "ERROR: unused div coefficient not zero\n");
2468 abort();
2472 static void dump_constraints(struct isl_basic_map *bmap,
2473 isl_int **c, unsigned n,
2474 const char *op, FILE *out, int indent)
2476 int i;
2478 for (i = 0; i < n; ++i)
2479 dump_constraint(bmap, c[i], op, out, indent);
2482 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2484 int j;
2485 int first = 1;
2486 unsigned total = isl_basic_map_total_dim(bmap);
2488 for (j = 0; j < 1 + total; ++j) {
2489 if (isl_int_is_zero(exp[j]))
2490 continue;
2491 if (!first && isl_int_is_pos(exp[j]))
2492 fprintf(out, "+");
2493 dump_term(bmap, exp[j], j, out);
2494 first = 0;
2498 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2500 int i;
2502 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2503 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2505 for (i = 0; i < bmap->n_div; ++i) {
2506 fprintf(out, "%*s", indent, "");
2507 fprintf(out, "e%d = [(", i);
2508 dump_affine(bmap, bmap->div[i]+1, out);
2509 fprintf(out, ")/");
2510 isl_int_print(out, bmap->div[i][0], 0);
2511 fprintf(out, "]\n");
2515 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2516 FILE *out, int indent)
2518 if (!bset) {
2519 fprintf(out, "null basic set\n");
2520 return;
2523 fprintf(out, "%*s", indent, "");
2524 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2525 bset->ref, bset->dim->nparam, bset->dim->n_out,
2526 bset->extra, bset->flags);
2527 dump((struct isl_basic_map *)bset, out, indent);
2530 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2531 FILE *out, int indent)
2533 if (!bmap) {
2534 fprintf(out, "null basic map\n");
2535 return;
2538 fprintf(out, "%*s", indent, "");
2539 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2540 "flags: %x, n_name: %d\n",
2541 bmap->ref,
2542 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2543 bmap->extra, bmap->flags, bmap->dim->n_id);
2544 dump(bmap, out, indent);
2547 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2549 unsigned total;
2550 if (!bmap)
2551 return -1;
2552 total = isl_basic_map_total_dim(bmap);
2553 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2554 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2555 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2556 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2557 return 0;
2560 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2561 unsigned flags)
2563 struct isl_set *set;
2565 if (!dim)
2566 return NULL;
2567 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2568 isl_assert(dim->ctx, n >= 0, goto error);
2569 set = isl_alloc(dim->ctx, struct isl_set,
2570 sizeof(struct isl_set) +
2571 (n - 1) * sizeof(struct isl_basic_set *));
2572 if (!set)
2573 goto error;
2575 set->ctx = dim->ctx;
2576 isl_ctx_ref(set->ctx);
2577 set->ref = 1;
2578 set->size = n;
2579 set->n = 0;
2580 set->dim = dim;
2581 set->flags = flags;
2582 return set;
2583 error:
2584 isl_space_free(dim);
2585 return NULL;
2588 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2589 unsigned nparam, unsigned dim, int n, unsigned flags)
2591 struct isl_set *set;
2592 isl_space *dims;
2594 dims = isl_space_alloc(ctx, nparam, 0, dim);
2595 if (!dims)
2596 return NULL;
2598 set = isl_set_alloc_space(dims, n, flags);
2599 return set;
2602 /* Make sure "map" has room for at least "n" more basic maps.
2604 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2606 int i;
2607 struct isl_map *grown = NULL;
2609 if (!map)
2610 return NULL;
2611 isl_assert(map->ctx, n >= 0, goto error);
2612 if (map->n + n <= map->size)
2613 return map;
2614 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2615 if (!grown)
2616 goto error;
2617 for (i = 0; i < map->n; ++i) {
2618 grown->p[i] = isl_basic_map_copy(map->p[i]);
2619 if (!grown->p[i])
2620 goto error;
2621 grown->n++;
2623 isl_map_free(map);
2624 return grown;
2625 error:
2626 isl_map_free(grown);
2627 isl_map_free(map);
2628 return NULL;
2631 /* Make sure "set" has room for at least "n" more basic sets.
2633 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2635 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2638 struct isl_set *isl_set_dup(struct isl_set *set)
2640 int i;
2641 struct isl_set *dup;
2643 if (!set)
2644 return NULL;
2646 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2647 if (!dup)
2648 return NULL;
2649 for (i = 0; i < set->n; ++i)
2650 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2651 return dup;
2654 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2656 return isl_map_from_basic_map(bset);
2659 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2661 struct isl_map *map;
2663 if (!bmap)
2664 return NULL;
2666 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2667 return isl_map_add_basic_map(map, bmap);
2670 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2671 __isl_take isl_basic_set *bset)
2673 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2674 (struct isl_basic_map *)bset);
2677 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2679 int i;
2681 if (!set)
2682 return NULL;
2684 if (--set->ref > 0)
2685 return NULL;
2687 isl_ctx_deref(set->ctx);
2688 for (i = 0; i < set->n; ++i)
2689 isl_basic_set_free(set->p[i]);
2690 isl_space_free(set->dim);
2691 free(set);
2693 return NULL;
2696 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2698 int i;
2700 if (!set) {
2701 fprintf(out, "null set\n");
2702 return;
2705 fprintf(out, "%*s", indent, "");
2706 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2707 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2708 set->flags);
2709 for (i = 0; i < set->n; ++i) {
2710 fprintf(out, "%*s", indent, "");
2711 fprintf(out, "basic set %d:\n", i);
2712 isl_basic_set_print_internal(set->p[i], out, indent+4);
2716 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2718 int i;
2720 if (!map) {
2721 fprintf(out, "null map\n");
2722 return;
2725 fprintf(out, "%*s", indent, "");
2726 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2727 "flags: %x, n_name: %d\n",
2728 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2729 map->dim->n_out, map->flags, map->dim->n_id);
2730 for (i = 0; i < map->n; ++i) {
2731 fprintf(out, "%*s", indent, "");
2732 fprintf(out, "basic map %d:\n", i);
2733 isl_basic_map_print_internal(map->p[i], out, indent+4);
2737 struct isl_basic_map *isl_basic_map_intersect_domain(
2738 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2740 struct isl_basic_map *bmap_domain;
2742 if (!bmap || !bset)
2743 goto error;
2745 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2746 bset->dim, isl_dim_param), goto error);
2748 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2749 isl_assert(bset->ctx,
2750 isl_basic_map_compatible_domain(bmap, bset), goto error);
2752 bmap = isl_basic_map_cow(bmap);
2753 if (!bmap)
2754 goto error;
2755 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2756 bset->n_div, bset->n_eq, bset->n_ineq);
2757 bmap_domain = isl_basic_map_from_domain(bset);
2758 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2760 bmap = isl_basic_map_simplify(bmap);
2761 return isl_basic_map_finalize(bmap);
2762 error:
2763 isl_basic_map_free(bmap);
2764 isl_basic_set_free(bset);
2765 return NULL;
2768 struct isl_basic_map *isl_basic_map_intersect_range(
2769 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2771 struct isl_basic_map *bmap_range;
2773 if (!bmap || !bset)
2774 goto error;
2776 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2777 bset->dim, isl_dim_param), goto error);
2779 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2780 isl_assert(bset->ctx,
2781 isl_basic_map_compatible_range(bmap, bset), goto error);
2783 if (isl_basic_set_is_universe(bset)) {
2784 isl_basic_set_free(bset);
2785 return bmap;
2788 bmap = isl_basic_map_cow(bmap);
2789 if (!bmap)
2790 goto error;
2791 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2792 bset->n_div, bset->n_eq, bset->n_ineq);
2793 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2794 bmap = add_constraints(bmap, bmap_range, 0, 0);
2796 bmap = isl_basic_map_simplify(bmap);
2797 return isl_basic_map_finalize(bmap);
2798 error:
2799 isl_basic_map_free(bmap);
2800 isl_basic_set_free(bset);
2801 return NULL;
2804 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2806 int i;
2807 unsigned total;
2808 isl_int s;
2810 if (!bmap || !vec)
2811 return -1;
2813 total = 1 + isl_basic_map_total_dim(bmap);
2814 if (total != vec->size)
2815 return -1;
2817 isl_int_init(s);
2819 for (i = 0; i < bmap->n_eq; ++i) {
2820 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2821 if (!isl_int_is_zero(s)) {
2822 isl_int_clear(s);
2823 return 0;
2827 for (i = 0; i < bmap->n_ineq; ++i) {
2828 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2829 if (isl_int_is_neg(s)) {
2830 isl_int_clear(s);
2831 return 0;
2835 isl_int_clear(s);
2837 return 1;
2840 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2842 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2845 struct isl_basic_map *isl_basic_map_intersect(
2846 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2848 struct isl_vec *sample = NULL;
2850 if (!bmap1 || !bmap2)
2851 goto error;
2853 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2854 bmap2->dim, isl_dim_param), goto error);
2855 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2856 isl_space_dim(bmap1->dim, isl_dim_param) &&
2857 isl_space_dim(bmap2->dim, isl_dim_all) !=
2858 isl_space_dim(bmap2->dim, isl_dim_param))
2859 return isl_basic_map_intersect(bmap2, bmap1);
2861 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2862 isl_space_dim(bmap2->dim, isl_dim_param))
2863 isl_assert(bmap1->ctx,
2864 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2866 if (bmap1->sample &&
2867 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2868 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2869 sample = isl_vec_copy(bmap1->sample);
2870 else if (bmap2->sample &&
2871 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2872 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2873 sample = isl_vec_copy(bmap2->sample);
2875 bmap1 = isl_basic_map_cow(bmap1);
2876 if (!bmap1)
2877 goto error;
2878 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2879 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2880 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2882 if (!bmap1)
2883 isl_vec_free(sample);
2884 else if (sample) {
2885 isl_vec_free(bmap1->sample);
2886 bmap1->sample = sample;
2889 bmap1 = isl_basic_map_simplify(bmap1);
2890 return isl_basic_map_finalize(bmap1);
2891 error:
2892 if (sample)
2893 isl_vec_free(sample);
2894 isl_basic_map_free(bmap1);
2895 isl_basic_map_free(bmap2);
2896 return NULL;
2899 struct isl_basic_set *isl_basic_set_intersect(
2900 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2902 return (struct isl_basic_set *)
2903 isl_basic_map_intersect(
2904 (struct isl_basic_map *)bset1,
2905 (struct isl_basic_map *)bset2);
2908 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2909 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2911 return isl_basic_set_intersect(bset1, bset2);
2914 /* Special case of isl_map_intersect, where both map1 and map2
2915 * are convex, without any divs and such that either map1 or map2
2916 * contains a single constraint. This constraint is then simply
2917 * added to the other map.
2919 static __isl_give isl_map *map_intersect_add_constraint(
2920 __isl_take isl_map *map1, __isl_take isl_map *map2)
2922 isl_assert(map1->ctx, map1->n == 1, goto error);
2923 isl_assert(map2->ctx, map1->n == 1, goto error);
2924 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2925 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2927 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2928 return isl_map_intersect(map2, map1);
2930 isl_assert(map2->ctx,
2931 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2933 map1 = isl_map_cow(map1);
2934 if (!map1)
2935 goto error;
2936 if (isl_map_plain_is_empty(map1)) {
2937 isl_map_free(map2);
2938 return map1;
2940 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2941 if (map2->p[0]->n_eq == 1)
2942 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2943 else
2944 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2945 map2->p[0]->ineq[0]);
2947 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2948 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2949 if (!map1->p[0])
2950 goto error;
2952 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2953 isl_basic_map_free(map1->p[0]);
2954 map1->n = 0;
2957 isl_map_free(map2);
2959 return map1;
2960 error:
2961 isl_map_free(map1);
2962 isl_map_free(map2);
2963 return NULL;
2966 /* map2 may be either a parameter domain or a map living in the same
2967 * space as map1.
2969 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2970 __isl_take isl_map *map2)
2972 unsigned flags = 0;
2973 isl_map *result;
2974 int i, j;
2976 if (!map1 || !map2)
2977 goto error;
2979 if ((isl_map_plain_is_empty(map1) ||
2980 isl_map_plain_is_universe(map2)) &&
2981 isl_space_is_equal(map1->dim, map2->dim)) {
2982 isl_map_free(map2);
2983 return map1;
2985 if ((isl_map_plain_is_empty(map2) ||
2986 isl_map_plain_is_universe(map1)) &&
2987 isl_space_is_equal(map1->dim, map2->dim)) {
2988 isl_map_free(map1);
2989 return map2;
2992 if (map1->n == 1 && map2->n == 1 &&
2993 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2994 isl_space_is_equal(map1->dim, map2->dim) &&
2995 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2996 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2997 return map_intersect_add_constraint(map1, map2);
2999 if (isl_space_dim(map2->dim, isl_dim_all) !=
3000 isl_space_dim(map2->dim, isl_dim_param))
3001 isl_assert(map1->ctx,
3002 isl_space_is_equal(map1->dim, map2->dim), goto error);
3004 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3005 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3006 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3008 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3009 map1->n * map2->n, flags);
3010 if (!result)
3011 goto error;
3012 for (i = 0; i < map1->n; ++i)
3013 for (j = 0; j < map2->n; ++j) {
3014 struct isl_basic_map *part;
3015 part = isl_basic_map_intersect(
3016 isl_basic_map_copy(map1->p[i]),
3017 isl_basic_map_copy(map2->p[j]));
3018 if (isl_basic_map_is_empty(part) < 0)
3019 part = isl_basic_map_free(part);
3020 result = isl_map_add_basic_map(result, part);
3021 if (!result)
3022 goto error;
3024 isl_map_free(map1);
3025 isl_map_free(map2);
3026 return result;
3027 error:
3028 isl_map_free(map1);
3029 isl_map_free(map2);
3030 return NULL;
3033 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3034 __isl_take isl_map *map2)
3036 if (!map1 || !map2)
3037 goto error;
3038 if (!isl_space_is_equal(map1->dim, map2->dim))
3039 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3040 "spaces don't match", goto error);
3041 return map_intersect_internal(map1, map2);
3042 error:
3043 isl_map_free(map1);
3044 isl_map_free(map2);
3045 return NULL;
3048 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3049 __isl_take isl_map *map2)
3051 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3054 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3056 return (struct isl_set *)
3057 isl_map_intersect((struct isl_map *)set1,
3058 (struct isl_map *)set2);
3061 /* map_intersect_internal accepts intersections
3062 * with parameter domains, so we can just call that function.
3064 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3065 __isl_take isl_set *params)
3067 return map_intersect_internal(map, params);
3070 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3071 __isl_take isl_map *map2)
3073 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3076 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3077 __isl_take isl_set *params)
3079 return isl_map_intersect_params(set, params);
3082 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3084 isl_space *dim;
3085 struct isl_basic_set *bset;
3086 unsigned in;
3088 if (!bmap)
3089 return NULL;
3090 bmap = isl_basic_map_cow(bmap);
3091 if (!bmap)
3092 return NULL;
3093 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3094 in = isl_basic_map_n_in(bmap);
3095 bset = isl_basic_set_from_basic_map(bmap);
3096 bset = isl_basic_set_swap_vars(bset, in);
3097 return isl_basic_map_from_basic_set(bset, dim);
3100 static __isl_give isl_basic_map *basic_map_space_reset(
3101 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3103 isl_space *space;
3105 if (!bmap)
3106 return NULL;
3107 if (!isl_space_is_named_or_nested(bmap->dim, type))
3108 return bmap;
3110 space = isl_basic_map_get_space(bmap);
3111 space = isl_space_reset(space, type);
3112 bmap = isl_basic_map_reset_space(bmap, space);
3113 return bmap;
3116 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3117 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3118 unsigned pos, unsigned n)
3120 isl_space *res_dim;
3121 struct isl_basic_map *res;
3122 struct isl_dim_map *dim_map;
3123 unsigned total, off;
3124 enum isl_dim_type t;
3126 if (n == 0)
3127 return basic_map_space_reset(bmap, type);
3129 if (!bmap)
3130 return NULL;
3132 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3134 total = isl_basic_map_total_dim(bmap) + n;
3135 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3136 off = 0;
3137 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3138 if (t != type) {
3139 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3140 } else {
3141 unsigned size = isl_basic_map_dim(bmap, t);
3142 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3143 0, pos, off);
3144 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3145 pos, size - pos, off + pos + n);
3147 off += isl_space_dim(res_dim, t);
3149 isl_dim_map_div(dim_map, bmap, off);
3151 res = isl_basic_map_alloc_space(res_dim,
3152 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3153 if (isl_basic_map_is_rational(bmap))
3154 res = isl_basic_map_set_rational(res);
3155 if (isl_basic_map_plain_is_empty(bmap)) {
3156 isl_basic_map_free(bmap);
3157 free(dim_map);
3158 return isl_basic_map_set_to_empty(res);
3160 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3161 return isl_basic_map_finalize(res);
3164 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3165 __isl_take isl_basic_set *bset,
3166 enum isl_dim_type type, unsigned pos, unsigned n)
3168 return isl_basic_map_insert_dims(bset, type, pos, n);
3171 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3172 enum isl_dim_type type, unsigned n)
3174 if (!bmap)
3175 return NULL;
3176 return isl_basic_map_insert_dims(bmap, type,
3177 isl_basic_map_dim(bmap, type), n);
3180 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3181 enum isl_dim_type type, unsigned n)
3183 if (!bset)
3184 return NULL;
3185 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3186 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3187 error:
3188 isl_basic_set_free(bset);
3189 return NULL;
3192 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3193 enum isl_dim_type type)
3195 isl_space *space;
3197 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3198 return map;
3200 space = isl_map_get_space(map);
3201 space = isl_space_reset(space, type);
3202 map = isl_map_reset_space(map, space);
3203 return map;
3206 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3207 enum isl_dim_type type, unsigned pos, unsigned n)
3209 int i;
3211 if (n == 0)
3212 return map_space_reset(map, type);
3214 map = isl_map_cow(map);
3215 if (!map)
3216 return NULL;
3218 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3219 if (!map->dim)
3220 goto error;
3222 for (i = 0; i < map->n; ++i) {
3223 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3224 if (!map->p[i])
3225 goto error;
3228 return map;
3229 error:
3230 isl_map_free(map);
3231 return NULL;
3234 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3235 enum isl_dim_type type, unsigned pos, unsigned n)
3237 return isl_map_insert_dims(set, type, pos, n);
3240 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3241 enum isl_dim_type type, unsigned n)
3243 if (!map)
3244 return NULL;
3245 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3248 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3249 enum isl_dim_type type, unsigned n)
3251 if (!set)
3252 return NULL;
3253 isl_assert(set->ctx, type != isl_dim_in, goto error);
3254 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3255 error:
3256 isl_set_free(set);
3257 return NULL;
3260 __isl_give isl_basic_map *isl_basic_map_move_dims(
3261 __isl_take isl_basic_map *bmap,
3262 enum isl_dim_type dst_type, unsigned dst_pos,
3263 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3265 struct isl_dim_map *dim_map;
3266 struct isl_basic_map *res;
3267 enum isl_dim_type t;
3268 unsigned total, off;
3270 if (!bmap)
3271 return NULL;
3272 if (n == 0)
3273 return bmap;
3275 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3276 goto error);
3278 if (dst_type == src_type && dst_pos == src_pos)
3279 return bmap;
3281 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3283 if (pos(bmap->dim, dst_type) + dst_pos ==
3284 pos(bmap->dim, src_type) + src_pos +
3285 ((src_type < dst_type) ? n : 0)) {
3286 bmap = isl_basic_map_cow(bmap);
3287 if (!bmap)
3288 return NULL;
3290 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3291 src_type, src_pos, n);
3292 if (!bmap->dim)
3293 goto error;
3295 bmap = isl_basic_map_finalize(bmap);
3297 return bmap;
3300 total = isl_basic_map_total_dim(bmap);
3301 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3303 off = 0;
3304 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3305 unsigned size = isl_space_dim(bmap->dim, t);
3306 if (t == dst_type) {
3307 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3308 0, dst_pos, off);
3309 off += dst_pos;
3310 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3311 src_pos, n, off);
3312 off += n;
3313 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3314 dst_pos, size - dst_pos, off);
3315 off += size - dst_pos;
3316 } else if (t == src_type) {
3317 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3318 0, src_pos, off);
3319 off += src_pos;
3320 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3321 src_pos + n, size - src_pos - n, off);
3322 off += size - src_pos - n;
3323 } else {
3324 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3325 off += size;
3328 isl_dim_map_div(dim_map, bmap, off);
3330 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3331 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3332 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3333 if (!bmap)
3334 goto error;
3336 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3337 src_type, src_pos, n);
3338 if (!bmap->dim)
3339 goto error;
3341 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3342 bmap = isl_basic_map_gauss(bmap, NULL);
3343 bmap = isl_basic_map_finalize(bmap);
3345 return bmap;
3346 error:
3347 isl_basic_map_free(bmap);
3348 return NULL;
3351 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3352 enum isl_dim_type dst_type, unsigned dst_pos,
3353 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3355 return (isl_basic_set *)isl_basic_map_move_dims(
3356 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3359 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3360 enum isl_dim_type dst_type, unsigned dst_pos,
3361 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3363 if (!set)
3364 return NULL;
3365 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3366 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3367 src_type, src_pos, n);
3368 error:
3369 isl_set_free(set);
3370 return NULL;
3373 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3374 enum isl_dim_type dst_type, unsigned dst_pos,
3375 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3377 int i;
3379 if (!map)
3380 return NULL;
3381 if (n == 0)
3382 return map;
3384 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3385 goto error);
3387 if (dst_type == src_type && dst_pos == src_pos)
3388 return map;
3390 isl_assert(map->ctx, dst_type != src_type, goto error);
3392 map = isl_map_cow(map);
3393 if (!map)
3394 return NULL;
3396 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3397 if (!map->dim)
3398 goto error;
3400 for (i = 0; i < map->n; ++i) {
3401 map->p[i] = isl_basic_map_move_dims(map->p[i],
3402 dst_type, dst_pos,
3403 src_type, src_pos, n);
3404 if (!map->p[i])
3405 goto error;
3408 return map;
3409 error:
3410 isl_map_free(map);
3411 return NULL;
3414 /* Move the specified dimensions to the last columns right before
3415 * the divs. Don't change the dimension specification of bmap.
3416 * That's the responsibility of the caller.
3418 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3419 enum isl_dim_type type, unsigned first, unsigned n)
3421 struct isl_dim_map *dim_map;
3422 struct isl_basic_map *res;
3423 enum isl_dim_type t;
3424 unsigned total, off;
3426 if (!bmap)
3427 return NULL;
3428 if (pos(bmap->dim, type) + first + n ==
3429 1 + isl_space_dim(bmap->dim, isl_dim_all))
3430 return bmap;
3432 total = isl_basic_map_total_dim(bmap);
3433 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3435 off = 0;
3436 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3437 unsigned size = isl_space_dim(bmap->dim, t);
3438 if (t == type) {
3439 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3440 0, first, off);
3441 off += first;
3442 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3443 first, n, total - bmap->n_div - n);
3444 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3445 first + n, size - (first + n), off);
3446 off += size - (first + n);
3447 } else {
3448 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3449 off += size;
3452 isl_dim_map_div(dim_map, bmap, off + n);
3454 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3455 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3456 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3457 return res;
3460 /* Insert "n" rows in the divs of "bmap".
3462 * The number of columns is not changed, which means that the last
3463 * dimensions of "bmap" are being reintepreted as the new divs.
3464 * The space of "bmap" is not adjusted, however, which means
3465 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3466 * from the space of "bmap" is the responsibility of the caller.
3468 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3469 int n)
3471 int i;
3472 size_t row_size;
3473 isl_int **new_div;
3474 isl_int *old;
3476 bmap = isl_basic_map_cow(bmap);
3477 if (!bmap)
3478 return NULL;
3480 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3481 old = bmap->block2.data;
3482 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3483 (bmap->extra + n) * (1 + row_size));
3484 if (!bmap->block2.data)
3485 return isl_basic_map_free(bmap);
3486 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3487 if (!new_div)
3488 return isl_basic_map_free(bmap);
3489 for (i = 0; i < n; ++i) {
3490 new_div[i] = bmap->block2.data +
3491 (bmap->extra + i) * (1 + row_size);
3492 isl_seq_clr(new_div[i], 1 + row_size);
3494 for (i = 0; i < bmap->extra; ++i)
3495 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3496 free(bmap->div);
3497 bmap->div = new_div;
3498 bmap->n_div += n;
3499 bmap->extra += n;
3501 return bmap;
3504 /* Turn the n dimensions of type type, starting at first
3505 * into existentially quantified variables.
3507 __isl_give isl_basic_map *isl_basic_map_project_out(
3508 __isl_take isl_basic_map *bmap,
3509 enum isl_dim_type type, unsigned first, unsigned n)
3511 if (n == 0)
3512 return basic_map_space_reset(bmap, type);
3514 if (!bmap)
3515 return NULL;
3517 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3518 return isl_basic_map_remove_dims(bmap, type, first, n);
3520 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3521 goto error);
3523 bmap = move_last(bmap, type, first, n);
3524 bmap = isl_basic_map_cow(bmap);
3525 bmap = insert_div_rows(bmap, n);
3526 if (!bmap)
3527 return NULL;
3529 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3530 if (!bmap->dim)
3531 goto error;
3532 bmap = isl_basic_map_simplify(bmap);
3533 bmap = isl_basic_map_drop_redundant_divs(bmap);
3534 return isl_basic_map_finalize(bmap);
3535 error:
3536 isl_basic_map_free(bmap);
3537 return NULL;
3540 /* Turn the n dimensions of type type, starting at first
3541 * into existentially quantified variables.
3543 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3544 enum isl_dim_type type, unsigned first, unsigned n)
3546 return (isl_basic_set *)isl_basic_map_project_out(
3547 (isl_basic_map *)bset, type, first, n);
3550 /* Turn the n dimensions of type type, starting at first
3551 * into existentially quantified variables.
3553 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3554 enum isl_dim_type type, unsigned first, unsigned n)
3556 int i;
3558 if (!map)
3559 return NULL;
3561 if (n == 0)
3562 return map_space_reset(map, type);
3564 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3566 map = isl_map_cow(map);
3567 if (!map)
3568 return NULL;
3570 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3571 if (!map->dim)
3572 goto error;
3574 for (i = 0; i < map->n; ++i) {
3575 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3576 if (!map->p[i])
3577 goto error;
3580 return map;
3581 error:
3582 isl_map_free(map);
3583 return NULL;
3586 /* Turn the n dimensions of type type, starting at first
3587 * into existentially quantified variables.
3589 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3590 enum isl_dim_type type, unsigned first, unsigned n)
3592 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3595 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3597 int i, j;
3599 for (i = 0; i < n; ++i) {
3600 j = isl_basic_map_alloc_div(bmap);
3601 if (j < 0)
3602 goto error;
3603 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3605 return bmap;
3606 error:
3607 isl_basic_map_free(bmap);
3608 return NULL;
3611 struct isl_basic_map *isl_basic_map_apply_range(
3612 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3614 isl_space *dim_result = NULL;
3615 struct isl_basic_map *bmap;
3616 unsigned n_in, n_out, n, nparam, total, pos;
3617 struct isl_dim_map *dim_map1, *dim_map2;
3619 if (!bmap1 || !bmap2)
3620 goto error;
3621 if (!isl_space_match(bmap1->dim, isl_dim_param,
3622 bmap2->dim, isl_dim_param))
3623 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3624 "parameters don't match", goto error);
3625 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3626 bmap2->dim, isl_dim_in))
3627 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3628 "spaces don't match", goto error);
3630 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3631 isl_space_copy(bmap2->dim));
3633 n_in = isl_basic_map_n_in(bmap1);
3634 n_out = isl_basic_map_n_out(bmap2);
3635 n = isl_basic_map_n_out(bmap1);
3636 nparam = isl_basic_map_n_param(bmap1);
3638 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3639 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3640 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3641 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3642 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3643 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3644 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3645 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3646 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3647 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3648 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3650 bmap = isl_basic_map_alloc_space(dim_result,
3651 bmap1->n_div + bmap2->n_div + n,
3652 bmap1->n_eq + bmap2->n_eq,
3653 bmap1->n_ineq + bmap2->n_ineq);
3654 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3655 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3656 bmap = add_divs(bmap, n);
3657 bmap = isl_basic_map_simplify(bmap);
3658 bmap = isl_basic_map_drop_redundant_divs(bmap);
3659 return isl_basic_map_finalize(bmap);
3660 error:
3661 isl_basic_map_free(bmap1);
3662 isl_basic_map_free(bmap2);
3663 return NULL;
3666 struct isl_basic_set *isl_basic_set_apply(
3667 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3669 if (!bset || !bmap)
3670 goto error;
3672 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3673 goto error);
3675 return (struct isl_basic_set *)
3676 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3677 error:
3678 isl_basic_set_free(bset);
3679 isl_basic_map_free(bmap);
3680 return NULL;
3683 struct isl_basic_map *isl_basic_map_apply_domain(
3684 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3686 if (!bmap1 || !bmap2)
3687 goto error;
3689 isl_assert(bmap1->ctx,
3690 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3691 isl_assert(bmap1->ctx,
3692 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3693 goto error);
3695 bmap1 = isl_basic_map_reverse(bmap1);
3696 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3697 return isl_basic_map_reverse(bmap1);
3698 error:
3699 isl_basic_map_free(bmap1);
3700 isl_basic_map_free(bmap2);
3701 return NULL;
3704 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3705 * A \cap B -> f(A) + f(B)
3707 struct isl_basic_map *isl_basic_map_sum(
3708 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3710 unsigned n_in, n_out, nparam, total, pos;
3711 struct isl_basic_map *bmap = NULL;
3712 struct isl_dim_map *dim_map1, *dim_map2;
3713 int i;
3715 if (!bmap1 || !bmap2)
3716 goto error;
3718 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3719 goto error);
3721 nparam = isl_basic_map_n_param(bmap1);
3722 n_in = isl_basic_map_n_in(bmap1);
3723 n_out = isl_basic_map_n_out(bmap1);
3725 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3726 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3727 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3728 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3729 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3730 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3731 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3732 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3733 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3734 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3735 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3737 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3738 bmap1->n_div + bmap2->n_div + 2 * n_out,
3739 bmap1->n_eq + bmap2->n_eq + n_out,
3740 bmap1->n_ineq + bmap2->n_ineq);
3741 for (i = 0; i < n_out; ++i) {
3742 int j = isl_basic_map_alloc_equality(bmap);
3743 if (j < 0)
3744 goto error;
3745 isl_seq_clr(bmap->eq[j], 1+total);
3746 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3747 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3748 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3750 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3751 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3752 bmap = add_divs(bmap, 2 * n_out);
3754 bmap = isl_basic_map_simplify(bmap);
3755 return isl_basic_map_finalize(bmap);
3756 error:
3757 isl_basic_map_free(bmap);
3758 isl_basic_map_free(bmap1);
3759 isl_basic_map_free(bmap2);
3760 return NULL;
3763 /* Given two maps A -> f(A) and B -> g(B), construct a map
3764 * A \cap B -> f(A) + f(B)
3766 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3768 struct isl_map *result;
3769 int i, j;
3771 if (!map1 || !map2)
3772 goto error;
3774 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3776 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3777 map1->n * map2->n, 0);
3778 if (!result)
3779 goto error;
3780 for (i = 0; i < map1->n; ++i)
3781 for (j = 0; j < map2->n; ++j) {
3782 struct isl_basic_map *part;
3783 part = isl_basic_map_sum(
3784 isl_basic_map_copy(map1->p[i]),
3785 isl_basic_map_copy(map2->p[j]));
3786 if (isl_basic_map_is_empty(part))
3787 isl_basic_map_free(part);
3788 else
3789 result = isl_map_add_basic_map(result, part);
3790 if (!result)
3791 goto error;
3793 isl_map_free(map1);
3794 isl_map_free(map2);
3795 return result;
3796 error:
3797 isl_map_free(map1);
3798 isl_map_free(map2);
3799 return NULL;
3802 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3803 __isl_take isl_set *set2)
3805 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3808 /* Given a basic map A -> f(A), construct A -> -f(A).
3810 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3812 int i, j;
3813 unsigned off, n;
3815 bmap = isl_basic_map_cow(bmap);
3816 if (!bmap)
3817 return NULL;
3819 n = isl_basic_map_dim(bmap, isl_dim_out);
3820 off = isl_basic_map_offset(bmap, isl_dim_out);
3821 for (i = 0; i < bmap->n_eq; ++i)
3822 for (j = 0; j < n; ++j)
3823 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3824 for (i = 0; i < bmap->n_ineq; ++i)
3825 for (j = 0; j < n; ++j)
3826 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3827 for (i = 0; i < bmap->n_div; ++i)
3828 for (j = 0; j < n; ++j)
3829 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3830 bmap = isl_basic_map_gauss(bmap, NULL);
3831 return isl_basic_map_finalize(bmap);
3834 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3836 return isl_basic_map_neg(bset);
3839 /* Given a map A -> f(A), construct A -> -f(A).
3841 struct isl_map *isl_map_neg(struct isl_map *map)
3843 int i;
3845 map = isl_map_cow(map);
3846 if (!map)
3847 return NULL;
3849 for (i = 0; i < map->n; ++i) {
3850 map->p[i] = isl_basic_map_neg(map->p[i]);
3851 if (!map->p[i])
3852 goto error;
3855 return map;
3856 error:
3857 isl_map_free(map);
3858 return NULL;
3861 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3863 return (isl_set *)isl_map_neg((isl_map *)set);
3866 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3867 * A -> floor(f(A)/d).
3869 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3870 isl_int d)
3872 unsigned n_in, n_out, nparam, total, pos;
3873 struct isl_basic_map *result = NULL;
3874 struct isl_dim_map *dim_map;
3875 int i;
3877 if (!bmap)
3878 return NULL;
3880 nparam = isl_basic_map_n_param(bmap);
3881 n_in = isl_basic_map_n_in(bmap);
3882 n_out = isl_basic_map_n_out(bmap);
3884 total = nparam + n_in + n_out + bmap->n_div + n_out;
3885 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3886 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3887 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3888 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3889 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3891 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3892 bmap->n_div + n_out,
3893 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3894 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3895 result = add_divs(result, n_out);
3896 for (i = 0; i < n_out; ++i) {
3897 int j;
3898 j = isl_basic_map_alloc_inequality(result);
3899 if (j < 0)
3900 goto error;
3901 isl_seq_clr(result->ineq[j], 1+total);
3902 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3903 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3904 j = isl_basic_map_alloc_inequality(result);
3905 if (j < 0)
3906 goto error;
3907 isl_seq_clr(result->ineq[j], 1+total);
3908 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3909 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3910 isl_int_sub_ui(result->ineq[j][0], d, 1);
3913 result = isl_basic_map_simplify(result);
3914 return isl_basic_map_finalize(result);
3915 error:
3916 isl_basic_map_free(result);
3917 return NULL;
3920 /* Given a map A -> f(A) and an integer d, construct a map
3921 * A -> floor(f(A)/d).
3923 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3925 int i;
3927 map = isl_map_cow(map);
3928 if (!map)
3929 return NULL;
3931 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3932 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3933 for (i = 0; i < map->n; ++i) {
3934 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3935 if (!map->p[i])
3936 goto error;
3939 return map;
3940 error:
3941 isl_map_free(map);
3942 return NULL;
3945 /* Given a map A -> f(A) and an integer d, construct a map
3946 * A -> floor(f(A)/d).
3948 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3949 __isl_take isl_val *d)
3951 if (!map || !d)
3952 goto error;
3953 if (!isl_val_is_int(d))
3954 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3955 "expecting integer denominator", goto error);
3956 map = isl_map_floordiv(map, d->n);
3957 isl_val_free(d);
3958 return map;
3959 error:
3960 isl_map_free(map);
3961 isl_val_free(d);
3962 return NULL;
3965 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3967 int i;
3968 unsigned nparam;
3969 unsigned n_in;
3971 i = isl_basic_map_alloc_equality(bmap);
3972 if (i < 0)
3973 goto error;
3974 nparam = isl_basic_map_n_param(bmap);
3975 n_in = isl_basic_map_n_in(bmap);
3976 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3977 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3978 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3979 return isl_basic_map_finalize(bmap);
3980 error:
3981 isl_basic_map_free(bmap);
3982 return NULL;
3985 /* Add a constraints to "bmap" expressing i_pos < o_pos
3987 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3989 int i;
3990 unsigned nparam;
3991 unsigned n_in;
3993 i = isl_basic_map_alloc_inequality(bmap);
3994 if (i < 0)
3995 goto error;
3996 nparam = isl_basic_map_n_param(bmap);
3997 n_in = isl_basic_map_n_in(bmap);
3998 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3999 isl_int_set_si(bmap->ineq[i][0], -1);
4000 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4001 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4002 return isl_basic_map_finalize(bmap);
4003 error:
4004 isl_basic_map_free(bmap);
4005 return NULL;
4008 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4010 static __isl_give isl_basic_map *var_less_or_equal(
4011 __isl_take isl_basic_map *bmap, unsigned pos)
4013 int i;
4014 unsigned nparam;
4015 unsigned n_in;
4017 i = isl_basic_map_alloc_inequality(bmap);
4018 if (i < 0)
4019 goto error;
4020 nparam = isl_basic_map_n_param(bmap);
4021 n_in = isl_basic_map_n_in(bmap);
4022 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4023 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4024 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4025 return isl_basic_map_finalize(bmap);
4026 error:
4027 isl_basic_map_free(bmap);
4028 return NULL;
4031 /* Add a constraints to "bmap" expressing i_pos > o_pos
4033 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4035 int i;
4036 unsigned nparam;
4037 unsigned n_in;
4039 i = isl_basic_map_alloc_inequality(bmap);
4040 if (i < 0)
4041 goto error;
4042 nparam = isl_basic_map_n_param(bmap);
4043 n_in = isl_basic_map_n_in(bmap);
4044 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4045 isl_int_set_si(bmap->ineq[i][0], -1);
4046 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4047 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4048 return isl_basic_map_finalize(bmap);
4049 error:
4050 isl_basic_map_free(bmap);
4051 return NULL;
4054 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4056 static __isl_give isl_basic_map *var_more_or_equal(
4057 __isl_take isl_basic_map *bmap, unsigned pos)
4059 int i;
4060 unsigned nparam;
4061 unsigned n_in;
4063 i = isl_basic_map_alloc_inequality(bmap);
4064 if (i < 0)
4065 goto error;
4066 nparam = isl_basic_map_n_param(bmap);
4067 n_in = isl_basic_map_n_in(bmap);
4068 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4069 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4070 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4071 return isl_basic_map_finalize(bmap);
4072 error:
4073 isl_basic_map_free(bmap);
4074 return NULL;
4077 __isl_give isl_basic_map *isl_basic_map_equal(
4078 __isl_take isl_space *dim, unsigned n_equal)
4080 int i;
4081 struct isl_basic_map *bmap;
4082 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4083 if (!bmap)
4084 return NULL;
4085 for (i = 0; i < n_equal && bmap; ++i)
4086 bmap = var_equal(bmap, i);
4087 return isl_basic_map_finalize(bmap);
4090 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4092 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4093 unsigned pos)
4095 int i;
4096 struct isl_basic_map *bmap;
4097 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4098 if (!bmap)
4099 return NULL;
4100 for (i = 0; i < pos && bmap; ++i)
4101 bmap = var_equal(bmap, i);
4102 if (bmap)
4103 bmap = var_less(bmap, pos);
4104 return isl_basic_map_finalize(bmap);
4107 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4109 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4110 __isl_take isl_space *dim, unsigned pos)
4112 int i;
4113 isl_basic_map *bmap;
4115 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4116 for (i = 0; i < pos; ++i)
4117 bmap = var_equal(bmap, i);
4118 bmap = var_less_or_equal(bmap, pos);
4119 return isl_basic_map_finalize(bmap);
4122 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4124 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4125 unsigned pos)
4127 int i;
4128 struct isl_basic_map *bmap;
4129 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4130 if (!bmap)
4131 return NULL;
4132 for (i = 0; i < pos && bmap; ++i)
4133 bmap = var_equal(bmap, i);
4134 if (bmap)
4135 bmap = var_more(bmap, pos);
4136 return isl_basic_map_finalize(bmap);
4139 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4141 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4142 __isl_take isl_space *dim, unsigned pos)
4144 int i;
4145 isl_basic_map *bmap;
4147 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4148 for (i = 0; i < pos; ++i)
4149 bmap = var_equal(bmap, i);
4150 bmap = var_more_or_equal(bmap, pos);
4151 return isl_basic_map_finalize(bmap);
4154 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4155 unsigned n, int equal)
4157 struct isl_map *map;
4158 int i;
4160 if (n == 0 && equal)
4161 return isl_map_universe(dims);
4163 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4165 for (i = 0; i + 1 < n; ++i)
4166 map = isl_map_add_basic_map(map,
4167 isl_basic_map_less_at(isl_space_copy(dims), i));
4168 if (n > 0) {
4169 if (equal)
4170 map = isl_map_add_basic_map(map,
4171 isl_basic_map_less_or_equal_at(dims, n - 1));
4172 else
4173 map = isl_map_add_basic_map(map,
4174 isl_basic_map_less_at(dims, n - 1));
4175 } else
4176 isl_space_free(dims);
4178 return map;
4181 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4183 if (!dims)
4184 return NULL;
4185 return map_lex_lte_first(dims, dims->n_out, equal);
4188 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4190 return map_lex_lte_first(dim, n, 0);
4193 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4195 return map_lex_lte_first(dim, n, 1);
4198 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4200 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4203 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4205 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4208 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4209 unsigned n, int equal)
4211 struct isl_map *map;
4212 int i;
4214 if (n == 0 && equal)
4215 return isl_map_universe(dims);
4217 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4219 for (i = 0; i + 1 < n; ++i)
4220 map = isl_map_add_basic_map(map,
4221 isl_basic_map_more_at(isl_space_copy(dims), i));
4222 if (n > 0) {
4223 if (equal)
4224 map = isl_map_add_basic_map(map,
4225 isl_basic_map_more_or_equal_at(dims, n - 1));
4226 else
4227 map = isl_map_add_basic_map(map,
4228 isl_basic_map_more_at(dims, n - 1));
4229 } else
4230 isl_space_free(dims);
4232 return map;
4235 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4237 if (!dims)
4238 return NULL;
4239 return map_lex_gte_first(dims, dims->n_out, equal);
4242 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4244 return map_lex_gte_first(dim, n, 0);
4247 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4249 return map_lex_gte_first(dim, n, 1);
4252 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4254 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4257 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4259 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4262 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4263 __isl_take isl_set *set2)
4265 isl_map *map;
4266 map = isl_map_lex_le(isl_set_get_space(set1));
4267 map = isl_map_intersect_domain(map, set1);
4268 map = isl_map_intersect_range(map, set2);
4269 return map;
4272 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4273 __isl_take isl_set *set2)
4275 isl_map *map;
4276 map = isl_map_lex_lt(isl_set_get_space(set1));
4277 map = isl_map_intersect_domain(map, set1);
4278 map = isl_map_intersect_range(map, set2);
4279 return map;
4282 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4283 __isl_take isl_set *set2)
4285 isl_map *map;
4286 map = isl_map_lex_ge(isl_set_get_space(set1));
4287 map = isl_map_intersect_domain(map, set1);
4288 map = isl_map_intersect_range(map, set2);
4289 return map;
4292 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4293 __isl_take isl_set *set2)
4295 isl_map *map;
4296 map = isl_map_lex_gt(isl_set_get_space(set1));
4297 map = isl_map_intersect_domain(map, set1);
4298 map = isl_map_intersect_range(map, set2);
4299 return map;
4302 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4303 __isl_take isl_map *map2)
4305 isl_map *map;
4306 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4307 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4308 map = isl_map_apply_range(map, isl_map_reverse(map2));
4309 return map;
4312 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4313 __isl_take isl_map *map2)
4315 isl_map *map;
4316 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4317 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4318 map = isl_map_apply_range(map, isl_map_reverse(map2));
4319 return map;
4322 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4323 __isl_take isl_map *map2)
4325 isl_map *map;
4326 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4327 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4328 map = isl_map_apply_range(map, isl_map_reverse(map2));
4329 return map;
4332 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4333 __isl_take isl_map *map2)
4335 isl_map *map;
4336 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4337 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4338 map = isl_map_apply_range(map, isl_map_reverse(map2));
4339 return map;
4342 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4343 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4345 struct isl_basic_map *bmap;
4347 bset = isl_basic_set_cow(bset);
4348 if (!bset || !dim)
4349 goto error;
4351 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4352 isl_space_free(bset->dim);
4353 bmap = (struct isl_basic_map *) bset;
4354 bmap->dim = dim;
4355 return isl_basic_map_finalize(bmap);
4356 error:
4357 isl_basic_set_free(bset);
4358 isl_space_free(dim);
4359 return NULL;
4362 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4364 if (!bmap)
4365 goto error;
4366 if (bmap->dim->n_in == 0)
4367 return (struct isl_basic_set *)bmap;
4368 bmap = isl_basic_map_cow(bmap);
4369 if (!bmap)
4370 goto error;
4371 bmap->dim = isl_space_as_set_space(bmap->dim);
4372 if (!bmap->dim)
4373 goto error;
4374 bmap = isl_basic_map_finalize(bmap);
4375 return (struct isl_basic_set *)bmap;
4376 error:
4377 isl_basic_map_free(bmap);
4378 return NULL;
4381 /* For a div d = floor(f/m), add the constraint
4383 * f - m d >= 0
4385 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4386 unsigned pos, isl_int *div)
4388 int i;
4389 unsigned total = isl_basic_map_total_dim(bmap);
4391 i = isl_basic_map_alloc_inequality(bmap);
4392 if (i < 0)
4393 return -1;
4394 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4395 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4397 return 0;
4400 /* For a div d = floor(f/m), add the constraint
4402 * -(f-(n-1)) + m d >= 0
4404 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4405 unsigned pos, isl_int *div)
4407 int i;
4408 unsigned total = isl_basic_map_total_dim(bmap);
4410 i = isl_basic_map_alloc_inequality(bmap);
4411 if (i < 0)
4412 return -1;
4413 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4414 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4415 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4416 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4418 return 0;
4421 /* For a div d = floor(f/m), add the constraints
4423 * f - m d >= 0
4424 * -(f-(n-1)) + m d >= 0
4426 * Note that the second constraint is the negation of
4428 * f - m d >= n
4430 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4431 unsigned pos, isl_int *div)
4433 if (add_upper_div_constraint(bmap, pos, div) < 0)
4434 return -1;
4435 if (add_lower_div_constraint(bmap, pos, div) < 0)
4436 return -1;
4437 return 0;
4440 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4441 unsigned pos, isl_int *div)
4443 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4444 pos, div);
4447 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4449 unsigned total = isl_basic_map_total_dim(bmap);
4450 unsigned div_pos = total - bmap->n_div + div;
4452 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4453 bmap->div[div]);
4456 /* For each known div d = floor(f/m), add the constraints
4458 * f - m d >= 0
4459 * -(f-(n-1)) + m d >= 0
4461 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4462 __isl_take isl_basic_map *bmap)
4464 int i;
4465 unsigned n_div;
4467 if (!bmap)
4468 return NULL;
4469 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4470 if (n_div == 0)
4471 return bmap;
4472 bmap = isl_basic_map_cow(bmap);
4473 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
4474 if (!bmap)
4475 return NULL;
4476 for (i = 0; i < n_div; ++i) {
4477 if (isl_int_is_zero(bmap->div[i][0]))
4478 continue;
4479 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4480 return isl_basic_map_free(bmap);
4483 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4484 bmap = isl_basic_map_finalize(bmap);
4485 return bmap;
4488 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4490 * In particular, if this div is of the form d = floor(f/m),
4491 * then add the constraint
4493 * f - m d >= 0
4495 * if sign < 0 or the constraint
4497 * -(f-(n-1)) + m d >= 0
4499 * if sign > 0.
4501 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4502 unsigned div, int sign)
4504 unsigned total;
4505 unsigned div_pos;
4507 if (!bmap)
4508 return -1;
4510 total = isl_basic_map_total_dim(bmap);
4511 div_pos = total - bmap->n_div + div;
4513 if (sign < 0)
4514 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4515 else
4516 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4519 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4521 return isl_basic_map_add_div_constraints(bset, div);
4524 struct isl_basic_set *isl_basic_map_underlying_set(
4525 struct isl_basic_map *bmap)
4527 if (!bmap)
4528 goto error;
4529 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4530 bmap->n_div == 0 &&
4531 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4532 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4533 return (struct isl_basic_set *)bmap;
4534 bmap = isl_basic_map_cow(bmap);
4535 if (!bmap)
4536 goto error;
4537 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4538 if (!bmap->dim)
4539 goto error;
4540 bmap->extra -= bmap->n_div;
4541 bmap->n_div = 0;
4542 bmap = isl_basic_map_finalize(bmap);
4543 return (struct isl_basic_set *)bmap;
4544 error:
4545 isl_basic_map_free(bmap);
4546 return NULL;
4549 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4550 __isl_take isl_basic_set *bset)
4552 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4555 /* Replace each element in "list" by the result of applying
4556 * isl_basic_set_underlying_set to the element.
4558 __isl_give isl_basic_set_list *isl_basic_set_list_underlying_set(
4559 __isl_take isl_basic_set_list *list)
4561 int i, n;
4563 if (!list)
4564 return NULL;
4566 n = isl_basic_set_list_n_basic_set(list);
4567 for (i = 0; i < n; ++i) {
4568 isl_basic_set *bset;
4570 bset = isl_basic_set_list_get_basic_set(list, i);
4571 bset = isl_basic_set_underlying_set(bset);
4572 list = isl_basic_set_list_set_basic_set(list, i, bset);
4575 return list;
4578 struct isl_basic_map *isl_basic_map_overlying_set(
4579 struct isl_basic_set *bset, struct isl_basic_map *like)
4581 struct isl_basic_map *bmap;
4582 struct isl_ctx *ctx;
4583 unsigned total;
4584 int i;
4586 if (!bset || !like)
4587 goto error;
4588 ctx = bset->ctx;
4589 isl_assert(ctx, bset->n_div == 0, goto error);
4590 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4591 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4592 goto error);
4593 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4594 isl_basic_map_free(like);
4595 return (struct isl_basic_map *)bset;
4597 bset = isl_basic_set_cow(bset);
4598 if (!bset)
4599 goto error;
4600 total = bset->dim->n_out + bset->extra;
4601 bmap = (struct isl_basic_map *)bset;
4602 isl_space_free(bmap->dim);
4603 bmap->dim = isl_space_copy(like->dim);
4604 if (!bmap->dim)
4605 goto error;
4606 bmap->n_div = like->n_div;
4607 bmap->extra += like->n_div;
4608 if (bmap->extra) {
4609 unsigned ltotal;
4610 isl_int **div;
4611 ltotal = total - bmap->extra + like->extra;
4612 if (ltotal > total)
4613 ltotal = total;
4614 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4615 bmap->extra * (1 + 1 + total));
4616 if (isl_blk_is_error(bmap->block2))
4617 goto error;
4618 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4619 if (!div)
4620 goto error;
4621 bmap->div = div;
4622 for (i = 0; i < bmap->extra; ++i)
4623 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4624 for (i = 0; i < like->n_div; ++i) {
4625 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4626 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4628 bmap = isl_basic_map_add_known_div_constraints(bmap);
4630 isl_basic_map_free(like);
4631 bmap = isl_basic_map_simplify(bmap);
4632 bmap = isl_basic_map_finalize(bmap);
4633 return bmap;
4634 error:
4635 isl_basic_map_free(like);
4636 isl_basic_set_free(bset);
4637 return NULL;
4640 struct isl_basic_set *isl_basic_set_from_underlying_set(
4641 struct isl_basic_set *bset, struct isl_basic_set *like)
4643 return (struct isl_basic_set *)
4644 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4647 struct isl_set *isl_set_from_underlying_set(
4648 struct isl_set *set, struct isl_basic_set *like)
4650 int i;
4652 if (!set || !like)
4653 goto error;
4654 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4655 goto error);
4656 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4657 isl_basic_set_free(like);
4658 return set;
4660 set = isl_set_cow(set);
4661 if (!set)
4662 goto error;
4663 for (i = 0; i < set->n; ++i) {
4664 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4665 isl_basic_set_copy(like));
4666 if (!set->p[i])
4667 goto error;
4669 isl_space_free(set->dim);
4670 set->dim = isl_space_copy(like->dim);
4671 if (!set->dim)
4672 goto error;
4673 isl_basic_set_free(like);
4674 return set;
4675 error:
4676 isl_basic_set_free(like);
4677 isl_set_free(set);
4678 return NULL;
4681 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4683 int i;
4685 map = isl_map_cow(map);
4686 if (!map)
4687 return NULL;
4688 map->dim = isl_space_cow(map->dim);
4689 if (!map->dim)
4690 goto error;
4692 for (i = 1; i < map->n; ++i)
4693 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4694 goto error);
4695 for (i = 0; i < map->n; ++i) {
4696 map->p[i] = (struct isl_basic_map *)
4697 isl_basic_map_underlying_set(map->p[i]);
4698 if (!map->p[i])
4699 goto error;
4701 if (map->n == 0)
4702 map->dim = isl_space_underlying(map->dim, 0);
4703 else {
4704 isl_space_free(map->dim);
4705 map->dim = isl_space_copy(map->p[0]->dim);
4707 if (!map->dim)
4708 goto error;
4709 return (struct isl_set *)map;
4710 error:
4711 isl_map_free(map);
4712 return NULL;
4715 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4717 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4720 __isl_give isl_basic_map *isl_basic_map_reset_space(
4721 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4723 bmap = isl_basic_map_cow(bmap);
4724 if (!bmap || !dim)
4725 goto error;
4727 isl_space_free(bmap->dim);
4728 bmap->dim = dim;
4730 bmap = isl_basic_map_finalize(bmap);
4732 return bmap;
4733 error:
4734 isl_basic_map_free(bmap);
4735 isl_space_free(dim);
4736 return NULL;
4739 __isl_give isl_basic_set *isl_basic_set_reset_space(
4740 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4742 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4743 dim);
4746 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4747 __isl_take isl_space *dim)
4749 int i;
4751 map = isl_map_cow(map);
4752 if (!map || !dim)
4753 goto error;
4755 for (i = 0; i < map->n; ++i) {
4756 map->p[i] = isl_basic_map_reset_space(map->p[i],
4757 isl_space_copy(dim));
4758 if (!map->p[i])
4759 goto error;
4761 isl_space_free(map->dim);
4762 map->dim = dim;
4764 return map;
4765 error:
4766 isl_map_free(map);
4767 isl_space_free(dim);
4768 return NULL;
4771 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4772 __isl_take isl_space *dim)
4774 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4777 /* Compute the parameter domain of the given basic set.
4779 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4781 isl_space *space;
4782 unsigned n;
4784 if (isl_basic_set_is_params(bset))
4785 return bset;
4787 n = isl_basic_set_dim(bset, isl_dim_set);
4788 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4789 space = isl_basic_set_get_space(bset);
4790 space = isl_space_params(space);
4791 bset = isl_basic_set_reset_space(bset, space);
4792 return bset;
4795 /* Construct a zero-dimensional basic set with the given parameter domain.
4797 __isl_give isl_basic_set *isl_basic_set_from_params(
4798 __isl_take isl_basic_set *bset)
4800 isl_space *space;
4801 space = isl_basic_set_get_space(bset);
4802 space = isl_space_set_from_params(space);
4803 bset = isl_basic_set_reset_space(bset, space);
4804 return bset;
4807 /* Compute the parameter domain of the given set.
4809 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4811 isl_space *space;
4812 unsigned n;
4814 if (isl_set_is_params(set))
4815 return set;
4817 n = isl_set_dim(set, isl_dim_set);
4818 set = isl_set_project_out(set, isl_dim_set, 0, n);
4819 space = isl_set_get_space(set);
4820 space = isl_space_params(space);
4821 set = isl_set_reset_space(set, space);
4822 return set;
4825 /* Construct a zero-dimensional set with the given parameter domain.
4827 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4829 isl_space *space;
4830 space = isl_set_get_space(set);
4831 space = isl_space_set_from_params(space);
4832 set = isl_set_reset_space(set, space);
4833 return set;
4836 /* Compute the parameter domain of the given map.
4838 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4840 isl_space *space;
4841 unsigned n;
4843 n = isl_map_dim(map, isl_dim_in);
4844 map = isl_map_project_out(map, isl_dim_in, 0, n);
4845 n = isl_map_dim(map, isl_dim_out);
4846 map = isl_map_project_out(map, isl_dim_out, 0, n);
4847 space = isl_map_get_space(map);
4848 space = isl_space_params(space);
4849 map = isl_map_reset_space(map, space);
4850 return map;
4853 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4855 isl_space *dim;
4856 struct isl_basic_set *domain;
4857 unsigned n_in;
4858 unsigned n_out;
4860 if (!bmap)
4861 return NULL;
4862 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4864 n_in = isl_basic_map_n_in(bmap);
4865 n_out = isl_basic_map_n_out(bmap);
4866 domain = isl_basic_set_from_basic_map(bmap);
4867 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4869 domain = isl_basic_set_reset_space(domain, dim);
4871 return domain;
4874 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4876 if (!bmap)
4877 return -1;
4878 return isl_space_may_be_set(bmap->dim);
4881 /* Is this basic map actually a set?
4882 * Users should never call this function. Outside of isl,
4883 * the type should indicate whether something is a set or a map.
4885 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4887 if (!bmap)
4888 return -1;
4889 return isl_space_is_set(bmap->dim);
4892 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4894 if (!bmap)
4895 return NULL;
4896 if (isl_basic_map_is_set(bmap))
4897 return bmap;
4898 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4901 __isl_give isl_basic_map *isl_basic_map_domain_map(
4902 __isl_take isl_basic_map *bmap)
4904 int i, k;
4905 isl_space *dim;
4906 isl_basic_map *domain;
4907 int nparam, n_in, n_out;
4908 unsigned total;
4910 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4911 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4912 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4914 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4915 domain = isl_basic_map_universe(dim);
4917 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4918 bmap = isl_basic_map_apply_range(bmap, domain);
4919 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4921 total = isl_basic_map_total_dim(bmap);
4923 for (i = 0; i < n_in; ++i) {
4924 k = isl_basic_map_alloc_equality(bmap);
4925 if (k < 0)
4926 goto error;
4927 isl_seq_clr(bmap->eq[k], 1 + total);
4928 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4929 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4932 bmap = isl_basic_map_gauss(bmap, NULL);
4933 return isl_basic_map_finalize(bmap);
4934 error:
4935 isl_basic_map_free(bmap);
4936 return NULL;
4939 __isl_give isl_basic_map *isl_basic_map_range_map(
4940 __isl_take isl_basic_map *bmap)
4942 int i, k;
4943 isl_space *dim;
4944 isl_basic_map *range;
4945 int nparam, n_in, n_out;
4946 unsigned total;
4948 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4949 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4950 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4952 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4953 range = isl_basic_map_universe(dim);
4955 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4956 bmap = isl_basic_map_apply_range(bmap, range);
4957 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4959 total = isl_basic_map_total_dim(bmap);
4961 for (i = 0; i < n_out; ++i) {
4962 k = isl_basic_map_alloc_equality(bmap);
4963 if (k < 0)
4964 goto error;
4965 isl_seq_clr(bmap->eq[k], 1 + total);
4966 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4967 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4970 bmap = isl_basic_map_gauss(bmap, NULL);
4971 return isl_basic_map_finalize(bmap);
4972 error:
4973 isl_basic_map_free(bmap);
4974 return NULL;
4977 int isl_map_may_be_set(__isl_keep isl_map *map)
4979 if (!map)
4980 return -1;
4981 return isl_space_may_be_set(map->dim);
4984 /* Is this map actually a set?
4985 * Users should never call this function. Outside of isl,
4986 * the type should indicate whether something is a set or a map.
4988 int isl_map_is_set(__isl_keep isl_map *map)
4990 if (!map)
4991 return -1;
4992 return isl_space_is_set(map->dim);
4995 struct isl_set *isl_map_range(struct isl_map *map)
4997 int i;
4998 struct isl_set *set;
5000 if (!map)
5001 goto error;
5002 if (isl_map_is_set(map))
5003 return (isl_set *)map;
5005 map = isl_map_cow(map);
5006 if (!map)
5007 goto error;
5009 set = (struct isl_set *) map;
5010 set->dim = isl_space_range(set->dim);
5011 if (!set->dim)
5012 goto error;
5013 for (i = 0; i < map->n; ++i) {
5014 set->p[i] = isl_basic_map_range(map->p[i]);
5015 if (!set->p[i])
5016 goto error;
5018 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5019 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5020 return set;
5021 error:
5022 isl_map_free(map);
5023 return NULL;
5026 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5028 int i;
5030 map = isl_map_cow(map);
5031 if (!map)
5032 return NULL;
5034 map->dim = isl_space_domain_map(map->dim);
5035 if (!map->dim)
5036 goto error;
5037 for (i = 0; i < map->n; ++i) {
5038 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5039 if (!map->p[i])
5040 goto error;
5042 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5043 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5044 return map;
5045 error:
5046 isl_map_free(map);
5047 return NULL;
5050 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5052 int i;
5053 isl_space *range_dim;
5055 map = isl_map_cow(map);
5056 if (!map)
5057 return NULL;
5059 range_dim = isl_space_range(isl_map_get_space(map));
5060 range_dim = isl_space_from_range(range_dim);
5061 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5062 map->dim = isl_space_join(map->dim, range_dim);
5063 if (!map->dim)
5064 goto error;
5065 for (i = 0; i < map->n; ++i) {
5066 map->p[i] = isl_basic_map_range_map(map->p[i]);
5067 if (!map->p[i])
5068 goto error;
5070 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5071 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5072 return map;
5073 error:
5074 isl_map_free(map);
5075 return NULL;
5078 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5079 __isl_take isl_space *dim)
5081 int i;
5082 struct isl_map *map = NULL;
5084 set = isl_set_cow(set);
5085 if (!set || !dim)
5086 goto error;
5087 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5088 map = (struct isl_map *)set;
5089 for (i = 0; i < set->n; ++i) {
5090 map->p[i] = isl_basic_map_from_basic_set(
5091 set->p[i], isl_space_copy(dim));
5092 if (!map->p[i])
5093 goto error;
5095 isl_space_free(map->dim);
5096 map->dim = dim;
5097 return map;
5098 error:
5099 isl_space_free(dim);
5100 isl_set_free(set);
5101 return NULL;
5104 __isl_give isl_basic_map *isl_basic_map_from_domain(
5105 __isl_take isl_basic_set *bset)
5107 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5110 __isl_give isl_basic_map *isl_basic_map_from_range(
5111 __isl_take isl_basic_set *bset)
5113 isl_space *space;
5114 space = isl_basic_set_get_space(bset);
5115 space = isl_space_from_range(space);
5116 bset = isl_basic_set_reset_space(bset, space);
5117 return (isl_basic_map *)bset;
5120 /* Create a relation with the given set as range.
5121 * The domain of the created relation is a zero-dimensional
5122 * flat anonymous space.
5124 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5126 isl_space *space;
5127 space = isl_set_get_space(set);
5128 space = isl_space_from_range(space);
5129 set = isl_set_reset_space(set, space);
5130 return (struct isl_map *)set;
5133 /* Create a relation with the given set as domain.
5134 * The range of the created relation is a zero-dimensional
5135 * flat anonymous space.
5137 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5139 return isl_map_reverse(isl_map_from_range(set));
5142 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5143 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5145 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5148 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5149 __isl_take isl_set *range)
5151 return isl_map_apply_range(isl_map_reverse(domain), range);
5154 struct isl_set *isl_set_from_map(struct isl_map *map)
5156 int i;
5157 struct isl_set *set = NULL;
5159 if (!map)
5160 return NULL;
5161 map = isl_map_cow(map);
5162 if (!map)
5163 return NULL;
5164 map->dim = isl_space_as_set_space(map->dim);
5165 if (!map->dim)
5166 goto error;
5167 set = (struct isl_set *)map;
5168 for (i = 0; i < map->n; ++i) {
5169 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
5170 if (!set->p[i])
5171 goto error;
5173 return set;
5174 error:
5175 isl_map_free(map);
5176 return NULL;
5179 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5180 unsigned flags)
5182 struct isl_map *map;
5184 if (!dim)
5185 return NULL;
5186 if (n < 0)
5187 isl_die(dim->ctx, isl_error_internal,
5188 "negative number of basic maps", goto error);
5189 map = isl_alloc(dim->ctx, struct isl_map,
5190 sizeof(struct isl_map) +
5191 (n - 1) * sizeof(struct isl_basic_map *));
5192 if (!map)
5193 goto error;
5195 map->ctx = dim->ctx;
5196 isl_ctx_ref(map->ctx);
5197 map->ref = 1;
5198 map->size = n;
5199 map->n = 0;
5200 map->dim = dim;
5201 map->flags = flags;
5202 return map;
5203 error:
5204 isl_space_free(dim);
5205 return NULL;
5208 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5209 unsigned nparam, unsigned in, unsigned out, int n,
5210 unsigned flags)
5212 struct isl_map *map;
5213 isl_space *dims;
5215 dims = isl_space_alloc(ctx, nparam, in, out);
5216 if (!dims)
5217 return NULL;
5219 map = isl_map_alloc_space(dims, n, flags);
5220 return map;
5223 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5225 struct isl_basic_map *bmap;
5226 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5227 bmap = isl_basic_map_set_to_empty(bmap);
5228 return bmap;
5231 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5233 struct isl_basic_set *bset;
5234 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5235 bset = isl_basic_set_set_to_empty(bset);
5236 return bset;
5239 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5241 struct isl_basic_map *bmap;
5242 if (!model)
5243 return NULL;
5244 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5245 bmap = isl_basic_map_set_to_empty(bmap);
5246 return bmap;
5249 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5251 struct isl_basic_map *bmap;
5252 if (!model)
5253 return NULL;
5254 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5255 bmap = isl_basic_map_set_to_empty(bmap);
5256 return bmap;
5259 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5261 struct isl_basic_set *bset;
5262 if (!model)
5263 return NULL;
5264 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5265 bset = isl_basic_set_set_to_empty(bset);
5266 return bset;
5269 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5271 struct isl_basic_map *bmap;
5272 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5273 bmap = isl_basic_map_finalize(bmap);
5274 return bmap;
5277 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5279 struct isl_basic_set *bset;
5280 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5281 bset = isl_basic_set_finalize(bset);
5282 return bset;
5285 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5287 int i;
5288 unsigned total = isl_space_dim(dim, isl_dim_all);
5289 isl_basic_map *bmap;
5291 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5292 for (i = 0; i < total; ++i) {
5293 int k = isl_basic_map_alloc_inequality(bmap);
5294 if (k < 0)
5295 goto error;
5296 isl_seq_clr(bmap->ineq[k], 1 + total);
5297 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5299 return bmap;
5300 error:
5301 isl_basic_map_free(bmap);
5302 return NULL;
5305 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5307 return isl_basic_map_nat_universe(dim);
5310 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5312 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5315 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5317 return isl_map_nat_universe(dim);
5320 __isl_give isl_basic_map *isl_basic_map_universe_like(
5321 __isl_keep isl_basic_map *model)
5323 if (!model)
5324 return NULL;
5325 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5328 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5330 if (!model)
5331 return NULL;
5332 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5335 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5336 __isl_keep isl_set *model)
5338 if (!model)
5339 return NULL;
5340 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5343 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5345 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5348 struct isl_map *isl_map_empty_like(struct isl_map *model)
5350 if (!model)
5351 return NULL;
5352 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5355 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5357 if (!model)
5358 return NULL;
5359 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5362 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5364 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5367 struct isl_set *isl_set_empty_like(struct isl_set *model)
5369 if (!model)
5370 return NULL;
5371 return isl_set_empty(isl_space_copy(model->dim));
5374 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5376 struct isl_map *map;
5377 if (!dim)
5378 return NULL;
5379 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5380 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5381 return map;
5384 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5386 struct isl_set *set;
5387 if (!dim)
5388 return NULL;
5389 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5390 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5391 return set;
5394 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5396 if (!model)
5397 return NULL;
5398 return isl_set_universe(isl_space_copy(model->dim));
5401 struct isl_map *isl_map_dup(struct isl_map *map)
5403 int i;
5404 struct isl_map *dup;
5406 if (!map)
5407 return NULL;
5408 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5409 for (i = 0; i < map->n; ++i)
5410 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5411 return dup;
5414 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5415 __isl_take isl_basic_map *bmap)
5417 if (!bmap || !map)
5418 goto error;
5419 if (isl_basic_map_plain_is_empty(bmap)) {
5420 isl_basic_map_free(bmap);
5421 return map;
5423 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5424 isl_assert(map->ctx, map->n < map->size, goto error);
5425 map->p[map->n] = bmap;
5426 map->n++;
5427 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5428 return map;
5429 error:
5430 if (map)
5431 isl_map_free(map);
5432 if (bmap)
5433 isl_basic_map_free(bmap);
5434 return NULL;
5437 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5439 int i;
5441 if (!map)
5442 return NULL;
5444 if (--map->ref > 0)
5445 return NULL;
5447 isl_ctx_deref(map->ctx);
5448 for (i = 0; i < map->n; ++i)
5449 isl_basic_map_free(map->p[i]);
5450 isl_space_free(map->dim);
5451 free(map);
5453 return NULL;
5456 struct isl_map *isl_map_extend(struct isl_map *base,
5457 unsigned nparam, unsigned n_in, unsigned n_out)
5459 int i;
5461 base = isl_map_cow(base);
5462 if (!base)
5463 return NULL;
5465 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5466 if (!base->dim)
5467 goto error;
5468 for (i = 0; i < base->n; ++i) {
5469 base->p[i] = isl_basic_map_extend_space(base->p[i],
5470 isl_space_copy(base->dim), 0, 0, 0);
5471 if (!base->p[i])
5472 goto error;
5474 return base;
5475 error:
5476 isl_map_free(base);
5477 return NULL;
5480 struct isl_set *isl_set_extend(struct isl_set *base,
5481 unsigned nparam, unsigned dim)
5483 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5484 nparam, 0, dim);
5487 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5488 struct isl_basic_map *bmap, unsigned pos, int value)
5490 int j;
5492 bmap = isl_basic_map_cow(bmap);
5493 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5494 j = isl_basic_map_alloc_equality(bmap);
5495 if (j < 0)
5496 goto error;
5497 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5498 isl_int_set_si(bmap->eq[j][pos], -1);
5499 isl_int_set_si(bmap->eq[j][0], value);
5500 bmap = isl_basic_map_simplify(bmap);
5501 return isl_basic_map_finalize(bmap);
5502 error:
5503 isl_basic_map_free(bmap);
5504 return NULL;
5507 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5508 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5510 int j;
5512 bmap = isl_basic_map_cow(bmap);
5513 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5514 j = isl_basic_map_alloc_equality(bmap);
5515 if (j < 0)
5516 goto error;
5517 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5518 isl_int_set_si(bmap->eq[j][pos], -1);
5519 isl_int_set(bmap->eq[j][0], value);
5520 bmap = isl_basic_map_simplify(bmap);
5521 return isl_basic_map_finalize(bmap);
5522 error:
5523 isl_basic_map_free(bmap);
5524 return NULL;
5527 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5528 enum isl_dim_type type, unsigned pos, int value)
5530 if (!bmap)
5531 return NULL;
5532 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5533 return isl_basic_map_fix_pos_si(bmap,
5534 isl_basic_map_offset(bmap, type) + pos, value);
5535 error:
5536 isl_basic_map_free(bmap);
5537 return NULL;
5540 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5541 enum isl_dim_type type, unsigned pos, isl_int value)
5543 if (!bmap)
5544 return NULL;
5545 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5546 return isl_basic_map_fix_pos(bmap,
5547 isl_basic_map_offset(bmap, type) + pos, value);
5548 error:
5549 isl_basic_map_free(bmap);
5550 return NULL;
5553 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5554 * to be equal to "v".
5556 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5557 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5559 if (!bmap || !v)
5560 goto error;
5561 if (!isl_val_is_int(v))
5562 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5563 "expecting integer value", goto error);
5564 if (pos >= isl_basic_map_dim(bmap, type))
5565 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5566 "index out of bounds", goto error);
5567 pos += isl_basic_map_offset(bmap, type);
5568 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5569 isl_val_free(v);
5570 return bmap;
5571 error:
5572 isl_basic_map_free(bmap);
5573 isl_val_free(v);
5574 return NULL;
5577 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5578 * to be equal to "v".
5580 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5581 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5583 return isl_basic_map_fix_val(bset, type, pos, v);
5586 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5587 enum isl_dim_type type, unsigned pos, int value)
5589 return (struct isl_basic_set *)
5590 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5591 type, pos, value);
5594 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5595 enum isl_dim_type type, unsigned pos, isl_int value)
5597 return (struct isl_basic_set *)
5598 isl_basic_map_fix((struct isl_basic_map *)bset,
5599 type, pos, value);
5602 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5603 unsigned input, int value)
5605 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5608 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5609 unsigned dim, int value)
5611 return (struct isl_basic_set *)
5612 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5613 isl_dim_set, dim, value);
5616 static int remove_if_empty(__isl_keep isl_map *map, int i)
5618 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5620 if (empty < 0)
5621 return -1;
5622 if (!empty)
5623 return 0;
5625 isl_basic_map_free(map->p[i]);
5626 if (i != map->n - 1) {
5627 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5628 map->p[i] = map->p[map->n - 1];
5630 map->n--;
5632 return 0;
5635 /* Perform "fn" on each basic map of "map", where we may not be holding
5636 * the only reference to "map".
5637 * In particular, "fn" should be a semantics preserving operation
5638 * that we want to apply to all copies of "map". We therefore need
5639 * to be careful not to modify "map" in a way that breaks "map"
5640 * in case anything goes wrong.
5642 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5643 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5645 struct isl_basic_map *bmap;
5646 int i;
5648 if (!map)
5649 return NULL;
5651 for (i = map->n - 1; i >= 0; --i) {
5652 bmap = isl_basic_map_copy(map->p[i]);
5653 bmap = fn(bmap);
5654 if (!bmap)
5655 goto error;
5656 isl_basic_map_free(map->p[i]);
5657 map->p[i] = bmap;
5658 if (remove_if_empty(map, i) < 0)
5659 goto error;
5662 return map;
5663 error:
5664 isl_map_free(map);
5665 return NULL;
5668 struct isl_map *isl_map_fix_si(struct isl_map *map,
5669 enum isl_dim_type type, unsigned pos, int value)
5671 int i;
5673 map = isl_map_cow(map);
5674 if (!map)
5675 return NULL;
5677 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5678 for (i = map->n - 1; i >= 0; --i) {
5679 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5680 if (remove_if_empty(map, i) < 0)
5681 goto error;
5683 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5684 return map;
5685 error:
5686 isl_map_free(map);
5687 return NULL;
5690 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5691 enum isl_dim_type type, unsigned pos, int value)
5693 return (struct isl_set *)
5694 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5697 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5698 enum isl_dim_type type, unsigned pos, isl_int value)
5700 int i;
5702 map = isl_map_cow(map);
5703 if (!map)
5704 return NULL;
5706 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5707 for (i = 0; i < map->n; ++i) {
5708 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5709 if (!map->p[i])
5710 goto error;
5712 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5713 return map;
5714 error:
5715 isl_map_free(map);
5716 return NULL;
5719 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5720 enum isl_dim_type type, unsigned pos, isl_int value)
5722 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5725 /* Fix the value of the variable at position "pos" of type "type" of "map"
5726 * to be equal to "v".
5728 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5729 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5731 int i;
5733 map = isl_map_cow(map);
5734 if (!map || !v)
5735 goto error;
5737 if (!isl_val_is_int(v))
5738 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5739 "expecting integer value", goto error);
5740 if (pos >= isl_map_dim(map, type))
5741 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5742 "index out of bounds", goto error);
5743 for (i = map->n - 1; i >= 0; --i) {
5744 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5745 isl_val_copy(v));
5746 if (remove_if_empty(map, i) < 0)
5747 goto error;
5749 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5750 isl_val_free(v);
5751 return map;
5752 error:
5753 isl_map_free(map);
5754 isl_val_free(v);
5755 return NULL;
5758 /* Fix the value of the variable at position "pos" of type "type" of "set"
5759 * to be equal to "v".
5761 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5762 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5764 return isl_map_fix_val(set, type, pos, v);
5767 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5768 unsigned input, int value)
5770 return isl_map_fix_si(map, isl_dim_in, input, value);
5773 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5775 return (struct isl_set *)
5776 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5779 static __isl_give isl_basic_map *basic_map_bound_si(
5780 __isl_take isl_basic_map *bmap,
5781 enum isl_dim_type type, unsigned pos, int value, int upper)
5783 int j;
5785 if (!bmap)
5786 return NULL;
5787 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5788 pos += isl_basic_map_offset(bmap, type);
5789 bmap = isl_basic_map_cow(bmap);
5790 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5791 j = isl_basic_map_alloc_inequality(bmap);
5792 if (j < 0)
5793 goto error;
5794 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5795 if (upper) {
5796 isl_int_set_si(bmap->ineq[j][pos], -1);
5797 isl_int_set_si(bmap->ineq[j][0], value);
5798 } else {
5799 isl_int_set_si(bmap->ineq[j][pos], 1);
5800 isl_int_set_si(bmap->ineq[j][0], -value);
5802 bmap = isl_basic_map_simplify(bmap);
5803 return isl_basic_map_finalize(bmap);
5804 error:
5805 isl_basic_map_free(bmap);
5806 return NULL;
5809 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5810 __isl_take isl_basic_map *bmap,
5811 enum isl_dim_type type, unsigned pos, int value)
5813 return basic_map_bound_si(bmap, type, pos, value, 0);
5816 /* Constrain the values of the given dimension to be no greater than "value".
5818 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5819 __isl_take isl_basic_map *bmap,
5820 enum isl_dim_type type, unsigned pos, int value)
5822 return basic_map_bound_si(bmap, type, pos, value, 1);
5825 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5826 unsigned dim, isl_int value)
5828 int j;
5830 bset = isl_basic_set_cow(bset);
5831 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5832 j = isl_basic_set_alloc_inequality(bset);
5833 if (j < 0)
5834 goto error;
5835 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5836 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5837 isl_int_neg(bset->ineq[j][0], value);
5838 bset = isl_basic_set_simplify(bset);
5839 return isl_basic_set_finalize(bset);
5840 error:
5841 isl_basic_set_free(bset);
5842 return NULL;
5845 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5846 enum isl_dim_type type, unsigned pos, int value, int upper)
5848 int i;
5850 map = isl_map_cow(map);
5851 if (!map)
5852 return NULL;
5854 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5855 for (i = 0; i < map->n; ++i) {
5856 map->p[i] = basic_map_bound_si(map->p[i],
5857 type, pos, value, upper);
5858 if (!map->p[i])
5859 goto error;
5861 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5862 return map;
5863 error:
5864 isl_map_free(map);
5865 return NULL;
5868 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5869 enum isl_dim_type type, unsigned pos, int value)
5871 return map_bound_si(map, type, pos, value, 0);
5874 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5875 enum isl_dim_type type, unsigned pos, int value)
5877 return map_bound_si(map, type, pos, value, 1);
5880 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5881 enum isl_dim_type type, unsigned pos, int value)
5883 return (struct isl_set *)
5884 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5887 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5888 enum isl_dim_type type, unsigned pos, int value)
5890 return isl_map_upper_bound_si(set, type, pos, value);
5893 /* Bound the given variable of "bmap" from below (or above is "upper"
5894 * is set) to "value".
5896 static __isl_give isl_basic_map *basic_map_bound(
5897 __isl_take isl_basic_map *bmap,
5898 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5900 int j;
5902 if (!bmap)
5903 return NULL;
5904 if (pos >= isl_basic_map_dim(bmap, type))
5905 isl_die(bmap->ctx, isl_error_invalid,
5906 "index out of bounds", goto error);
5907 pos += isl_basic_map_offset(bmap, type);
5908 bmap = isl_basic_map_cow(bmap);
5909 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5910 j = isl_basic_map_alloc_inequality(bmap);
5911 if (j < 0)
5912 goto error;
5913 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5914 if (upper) {
5915 isl_int_set_si(bmap->ineq[j][pos], -1);
5916 isl_int_set(bmap->ineq[j][0], value);
5917 } else {
5918 isl_int_set_si(bmap->ineq[j][pos], 1);
5919 isl_int_neg(bmap->ineq[j][0], value);
5921 bmap = isl_basic_map_simplify(bmap);
5922 return isl_basic_map_finalize(bmap);
5923 error:
5924 isl_basic_map_free(bmap);
5925 return NULL;
5928 /* Bound the given variable of "map" from below (or above is "upper"
5929 * is set) to "value".
5931 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5932 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5934 int i;
5936 map = isl_map_cow(map);
5937 if (!map)
5938 return NULL;
5940 if (pos >= isl_map_dim(map, type))
5941 isl_die(map->ctx, isl_error_invalid,
5942 "index out of bounds", goto error);
5943 for (i = map->n - 1; i >= 0; --i) {
5944 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5945 if (remove_if_empty(map, i) < 0)
5946 goto error;
5948 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5949 return map;
5950 error:
5951 isl_map_free(map);
5952 return NULL;
5955 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5956 enum isl_dim_type type, unsigned pos, isl_int value)
5958 return map_bound(map, type, pos, value, 0);
5961 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5962 enum isl_dim_type type, unsigned pos, isl_int value)
5964 return map_bound(map, type, pos, value, 1);
5967 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5968 enum isl_dim_type type, unsigned pos, isl_int value)
5970 return isl_map_lower_bound(set, type, pos, value);
5973 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5974 enum isl_dim_type type, unsigned pos, isl_int value)
5976 return isl_map_upper_bound(set, type, pos, value);
5979 /* Force the values of the variable at position "pos" of type "type" of "set"
5980 * to be no smaller than "value".
5982 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5983 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5985 if (!value)
5986 goto error;
5987 if (!isl_val_is_int(value))
5988 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5989 "expecting integer value", goto error);
5990 set = isl_set_lower_bound(set, type, pos, value->n);
5991 isl_val_free(value);
5992 return set;
5993 error:
5994 isl_val_free(value);
5995 isl_set_free(set);
5996 return NULL;
5999 /* Force the values of the variable at position "pos" of type "type" of "set"
6000 * to be no greater than "value".
6002 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6003 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6005 if (!value)
6006 goto error;
6007 if (!isl_val_is_int(value))
6008 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6009 "expecting integer value", goto error);
6010 set = isl_set_upper_bound(set, type, pos, value->n);
6011 isl_val_free(value);
6012 return set;
6013 error:
6014 isl_val_free(value);
6015 isl_set_free(set);
6016 return NULL;
6019 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6020 isl_int value)
6022 int i;
6024 set = isl_set_cow(set);
6025 if (!set)
6026 return NULL;
6028 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6029 for (i = 0; i < set->n; ++i) {
6030 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6031 if (!set->p[i])
6032 goto error;
6034 return set;
6035 error:
6036 isl_set_free(set);
6037 return NULL;
6040 struct isl_map *isl_map_reverse(struct isl_map *map)
6042 int i;
6044 map = isl_map_cow(map);
6045 if (!map)
6046 return NULL;
6048 map->dim = isl_space_reverse(map->dim);
6049 if (!map->dim)
6050 goto error;
6051 for (i = 0; i < map->n; ++i) {
6052 map->p[i] = isl_basic_map_reverse(map->p[i]);
6053 if (!map->p[i])
6054 goto error;
6056 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6057 return map;
6058 error:
6059 isl_map_free(map);
6060 return NULL;
6063 static struct isl_map *isl_basic_map_partial_lexopt(
6064 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6065 struct isl_set **empty, int max)
6067 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6070 struct isl_map *isl_basic_map_partial_lexmax(
6071 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6072 struct isl_set **empty)
6074 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6077 struct isl_map *isl_basic_map_partial_lexmin(
6078 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6079 struct isl_set **empty)
6081 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6084 struct isl_set *isl_basic_set_partial_lexmin(
6085 struct isl_basic_set *bset, struct isl_basic_set *dom,
6086 struct isl_set **empty)
6088 return (struct isl_set *)
6089 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6090 dom, empty);
6093 struct isl_set *isl_basic_set_partial_lexmax(
6094 struct isl_basic_set *bset, struct isl_basic_set *dom,
6095 struct isl_set **empty)
6097 return (struct isl_set *)
6098 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6099 dom, empty);
6102 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6103 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6104 __isl_give isl_set **empty)
6106 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6109 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6110 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6111 __isl_give isl_set **empty)
6113 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6116 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6117 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6118 __isl_give isl_set **empty)
6120 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6123 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6124 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6125 __isl_give isl_set **empty)
6127 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6130 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6131 __isl_take isl_basic_map *bmap, int max)
6133 isl_basic_set *dom = NULL;
6134 isl_space *dom_space;
6136 if (!bmap)
6137 goto error;
6138 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6139 dom = isl_basic_set_universe(dom_space);
6140 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6141 error:
6142 isl_basic_map_free(bmap);
6143 return NULL;
6146 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6147 __isl_take isl_basic_map *bmap)
6149 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6152 #undef TYPE
6153 #define TYPE isl_pw_multi_aff
6154 #undef SUFFIX
6155 #define SUFFIX _pw_multi_aff
6156 #undef EMPTY
6157 #define EMPTY isl_pw_multi_aff_empty
6158 #undef ADD
6159 #define ADD isl_pw_multi_aff_union_add
6160 #include "isl_map_lexopt_templ.c"
6162 /* Given a map "map", compute the lexicographically minimal
6163 * (or maximal) image element for each domain element in dom,
6164 * in the form of an isl_pw_multi_aff.
6165 * Set *empty to those elements in dom that do not have an image element.
6167 * We first compute the lexicographically minimal or maximal element
6168 * in the first basic map. This results in a partial solution "res"
6169 * and a subset "todo" of dom that still need to be handled.
6170 * We then consider each of the remaining maps in "map" and successively
6171 * update both "res" and "todo".
6173 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6174 __isl_take isl_map *map, __isl_take isl_set *dom,
6175 __isl_give isl_set **empty, int max)
6177 int i;
6178 isl_pw_multi_aff *res;
6179 isl_set *todo;
6181 if (!map || !dom)
6182 goto error;
6184 if (isl_map_plain_is_empty(map)) {
6185 if (empty)
6186 *empty = dom;
6187 else
6188 isl_set_free(dom);
6189 return isl_pw_multi_aff_from_map(map);
6192 res = basic_map_partial_lexopt_pw_multi_aff(
6193 isl_basic_map_copy(map->p[0]),
6194 isl_set_copy(dom), &todo, max);
6196 for (i = 1; i < map->n; ++i) {
6197 isl_pw_multi_aff *res_i;
6198 isl_set *todo_i;
6200 res_i = basic_map_partial_lexopt_pw_multi_aff(
6201 isl_basic_map_copy(map->p[i]),
6202 isl_set_copy(dom), &todo_i, max);
6204 if (max)
6205 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6206 else
6207 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6209 todo = isl_set_intersect(todo, todo_i);
6212 isl_set_free(dom);
6213 isl_map_free(map);
6215 if (empty)
6216 *empty = todo;
6217 else
6218 isl_set_free(todo);
6220 return res;
6221 error:
6222 if (empty)
6223 *empty = NULL;
6224 isl_set_free(dom);
6225 isl_map_free(map);
6226 return NULL;
6229 #undef TYPE
6230 #define TYPE isl_map
6231 #undef SUFFIX
6232 #define SUFFIX
6233 #undef EMPTY
6234 #define EMPTY isl_map_empty
6235 #undef ADD
6236 #define ADD isl_map_union_disjoint
6237 #include "isl_map_lexopt_templ.c"
6239 /* Given a map "map", compute the lexicographically minimal
6240 * (or maximal) image element for each domain element in dom.
6241 * Set *empty to those elements in dom that do not have an image element.
6243 * We first compute the lexicographically minimal or maximal element
6244 * in the first basic map. This results in a partial solution "res"
6245 * and a subset "todo" of dom that still need to be handled.
6246 * We then consider each of the remaining maps in "map" and successively
6247 * update both "res" and "todo".
6249 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6250 * Assume we are computing the lexicographical maximum.
6251 * We first compute the lexicographically maximal element in basic map i.
6252 * This results in a partial solution res_i and a subset todo_i.
6253 * Then we combine these results with those obtain for the first k basic maps
6254 * to obtain a result that is valid for the first k+1 basic maps.
6255 * In particular, the set where there is no solution is the set where
6256 * there is no solution for the first k basic maps and also no solution
6257 * for the ith basic map, i.e.,
6259 * todo^i = todo^k * todo_i
6261 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6262 * solutions, arbitrarily breaking ties in favor of res^k.
6263 * That is, when res^k(a) >= res_i(a), we pick res^k and
6264 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6265 * the lexicographic order.)
6266 * In practice, we compute
6268 * res^k * (res_i . "<=")
6270 * and
6272 * res_i * (res^k . "<")
6274 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6275 * where only one of res^k and res_i provides a solution and we simply pick
6276 * that one, i.e.,
6278 * res^k * todo_i
6279 * and
6280 * res_i * todo^k
6282 * Note that we only compute these intersections when dom(res^k) intersects
6283 * dom(res_i). Otherwise, the only effect of these intersections is to
6284 * potentially break up res^k and res_i into smaller pieces.
6285 * We want to avoid such splintering as much as possible.
6286 * In fact, an earlier implementation of this function would look for
6287 * better results in the domain of res^k and for extra results in todo^k,
6288 * but this would always result in a splintering according to todo^k,
6289 * even when the domain of basic map i is disjoint from the domains of
6290 * the previous basic maps.
6292 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6293 __isl_take isl_map *map, __isl_take isl_set *dom,
6294 __isl_give isl_set **empty, int max)
6296 int i;
6297 struct isl_map *res;
6298 struct isl_set *todo;
6300 if (!map || !dom)
6301 goto error;
6303 if (isl_map_plain_is_empty(map)) {
6304 if (empty)
6305 *empty = dom;
6306 else
6307 isl_set_free(dom);
6308 return map;
6311 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6312 isl_set_copy(dom), &todo, max);
6314 for (i = 1; i < map->n; ++i) {
6315 isl_map *lt, *le;
6316 isl_map *res_i;
6317 isl_set *todo_i;
6318 isl_space *dim = isl_space_range(isl_map_get_space(res));
6320 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6321 isl_set_copy(dom), &todo_i, max);
6323 if (max) {
6324 lt = isl_map_lex_lt(isl_space_copy(dim));
6325 le = isl_map_lex_le(dim);
6326 } else {
6327 lt = isl_map_lex_gt(isl_space_copy(dim));
6328 le = isl_map_lex_ge(dim);
6330 lt = isl_map_apply_range(isl_map_copy(res), lt);
6331 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6332 le = isl_map_apply_range(isl_map_copy(res_i), le);
6333 le = isl_map_intersect(le, isl_map_copy(res));
6335 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6336 res = isl_map_intersect_domain(res,
6337 isl_set_copy(todo_i));
6338 res_i = isl_map_intersect_domain(res_i,
6339 isl_set_copy(todo));
6342 res = isl_map_union_disjoint(res, res_i);
6343 res = isl_map_union_disjoint(res, lt);
6344 res = isl_map_union_disjoint(res, le);
6346 todo = isl_set_intersect(todo, todo_i);
6349 isl_set_free(dom);
6350 isl_map_free(map);
6352 if (empty)
6353 *empty = todo;
6354 else
6355 isl_set_free(todo);
6357 return res;
6358 error:
6359 if (empty)
6360 *empty = NULL;
6361 isl_set_free(dom);
6362 isl_map_free(map);
6363 return NULL;
6366 __isl_give isl_map *isl_map_partial_lexmax(
6367 __isl_take isl_map *map, __isl_take isl_set *dom,
6368 __isl_give isl_set **empty)
6370 return isl_map_partial_lexopt(map, dom, empty, 1);
6373 __isl_give isl_map *isl_map_partial_lexmin(
6374 __isl_take isl_map *map, __isl_take isl_set *dom,
6375 __isl_give isl_set **empty)
6377 return isl_map_partial_lexopt(map, dom, empty, 0);
6380 __isl_give isl_set *isl_set_partial_lexmin(
6381 __isl_take isl_set *set, __isl_take isl_set *dom,
6382 __isl_give isl_set **empty)
6384 return (struct isl_set *)
6385 isl_map_partial_lexmin((struct isl_map *)set,
6386 dom, empty);
6389 __isl_give isl_set *isl_set_partial_lexmax(
6390 __isl_take isl_set *set, __isl_take isl_set *dom,
6391 __isl_give isl_set **empty)
6393 return (struct isl_set *)
6394 isl_map_partial_lexmax((struct isl_map *)set,
6395 dom, empty);
6398 /* Compute the lexicographic minimum (or maximum if "max" is set)
6399 * of "bmap" over its domain.
6401 * Since we are not interested in the part of the domain space where
6402 * there is no solution, we initialize the domain to those constraints
6403 * of "bmap" that only involve the parameters and the input dimensions.
6404 * This relieves the parametric programming engine from detecting those
6405 * inequalities and transferring them to the context. More importantly,
6406 * it ensures that those inequalities are transferred first and not
6407 * intermixed with inequalities that actually split the domain.
6409 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6411 int n_div;
6412 int n_out;
6413 isl_basic_map *copy;
6414 isl_basic_set *dom;
6416 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6417 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6418 copy = isl_basic_map_copy(bmap);
6419 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6420 isl_dim_div, 0, n_div);
6421 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6422 isl_dim_out, 0, n_out);
6423 dom = isl_basic_map_domain(copy);
6424 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6427 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6429 return isl_basic_map_lexopt(bmap, 0);
6432 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6434 return isl_basic_map_lexopt(bmap, 1);
6437 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6439 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6442 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6444 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6447 /* Extract the first and only affine expression from list
6448 * and then add it to *pwaff with the given dom.
6449 * This domain is known to be disjoint from other domains
6450 * because of the way isl_basic_map_foreach_lexmax works.
6452 static int update_dim_opt(__isl_take isl_basic_set *dom,
6453 __isl_take isl_aff_list *list, void *user)
6455 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6456 isl_aff *aff;
6457 isl_pw_aff **pwaff = user;
6458 isl_pw_aff *pwaff_i;
6460 if (!list)
6461 goto error;
6462 if (isl_aff_list_n_aff(list) != 1)
6463 isl_die(ctx, isl_error_internal,
6464 "expecting single element list", goto error);
6466 aff = isl_aff_list_get_aff(list, 0);
6467 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6469 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6471 isl_aff_list_free(list);
6473 return 0;
6474 error:
6475 isl_basic_set_free(dom);
6476 isl_aff_list_free(list);
6477 return -1;
6480 /* Given a basic map with one output dimension, compute the minimum or
6481 * maximum of that dimension as an isl_pw_aff.
6483 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6484 * call update_dim_opt on each leaf of the result.
6486 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6487 int max)
6489 isl_space *dim = isl_basic_map_get_space(bmap);
6490 isl_pw_aff *pwaff;
6491 int r;
6493 dim = isl_space_from_domain(isl_space_domain(dim));
6494 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6495 pwaff = isl_pw_aff_empty(dim);
6497 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6498 if (r < 0)
6499 return isl_pw_aff_free(pwaff);
6501 return pwaff;
6504 /* Compute the minimum or maximum of the given output dimension
6505 * as a function of the parameters and the input dimensions,
6506 * but independently of the other output dimensions.
6508 * We first project out the other output dimension and then compute
6509 * the "lexicographic" maximum in each basic map, combining the results
6510 * using isl_pw_aff_union_max.
6512 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6513 int max)
6515 int i;
6516 isl_pw_aff *pwaff;
6517 unsigned n_out;
6519 n_out = isl_map_dim(map, isl_dim_out);
6520 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6521 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6522 if (!map)
6523 return NULL;
6525 if (map->n == 0) {
6526 isl_space *dim = isl_map_get_space(map);
6527 isl_map_free(map);
6528 return isl_pw_aff_empty(dim);
6531 pwaff = basic_map_dim_opt(map->p[0], max);
6532 for (i = 1; i < map->n; ++i) {
6533 isl_pw_aff *pwaff_i;
6535 pwaff_i = basic_map_dim_opt(map->p[i], max);
6536 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6539 isl_map_free(map);
6541 return pwaff;
6544 /* Compute the maximum of the given output dimension as a function of the
6545 * parameters and input dimensions, but independently of
6546 * the other output dimensions.
6548 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6550 return map_dim_opt(map, pos, 1);
6553 /* Compute the minimum or maximum of the given set dimension
6554 * as a function of the parameters,
6555 * but independently of the other set dimensions.
6557 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6558 int max)
6560 return map_dim_opt(set, pos, max);
6563 /* Compute the maximum of the given set dimension as a function of the
6564 * parameters, but independently of the other set dimensions.
6566 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6568 return set_dim_opt(set, pos, 1);
6571 /* Compute the minimum of the given set dimension as a function of the
6572 * parameters, but independently of the other set dimensions.
6574 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6576 return set_dim_opt(set, pos, 0);
6579 /* Apply a preimage specified by "mat" on the parameters of "bset".
6580 * bset is assumed to have only parameters and divs.
6582 static struct isl_basic_set *basic_set_parameter_preimage(
6583 struct isl_basic_set *bset, struct isl_mat *mat)
6585 unsigned nparam;
6587 if (!bset || !mat)
6588 goto error;
6590 bset->dim = isl_space_cow(bset->dim);
6591 if (!bset->dim)
6592 goto error;
6594 nparam = isl_basic_set_dim(bset, isl_dim_param);
6596 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6598 bset->dim->nparam = 0;
6599 bset->dim->n_out = nparam;
6600 bset = isl_basic_set_preimage(bset, mat);
6601 if (bset) {
6602 bset->dim->nparam = bset->dim->n_out;
6603 bset->dim->n_out = 0;
6605 return bset;
6606 error:
6607 isl_mat_free(mat);
6608 isl_basic_set_free(bset);
6609 return NULL;
6612 /* Apply a preimage specified by "mat" on the parameters of "set".
6613 * set is assumed to have only parameters and divs.
6615 static struct isl_set *set_parameter_preimage(
6616 struct isl_set *set, struct isl_mat *mat)
6618 isl_space *dim = NULL;
6619 unsigned nparam;
6621 if (!set || !mat)
6622 goto error;
6624 dim = isl_space_copy(set->dim);
6625 dim = isl_space_cow(dim);
6626 if (!dim)
6627 goto error;
6629 nparam = isl_set_dim(set, isl_dim_param);
6631 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6633 dim->nparam = 0;
6634 dim->n_out = nparam;
6635 isl_set_reset_space(set, dim);
6636 set = isl_set_preimage(set, mat);
6637 if (!set)
6638 goto error2;
6639 dim = isl_space_copy(set->dim);
6640 dim = isl_space_cow(dim);
6641 if (!dim)
6642 goto error2;
6643 dim->nparam = dim->n_out;
6644 dim->n_out = 0;
6645 isl_set_reset_space(set, dim);
6646 return set;
6647 error:
6648 isl_space_free(dim);
6649 isl_mat_free(mat);
6650 error2:
6651 isl_set_free(set);
6652 return NULL;
6655 /* Intersect the basic set "bset" with the affine space specified by the
6656 * equalities in "eq".
6658 static struct isl_basic_set *basic_set_append_equalities(
6659 struct isl_basic_set *bset, struct isl_mat *eq)
6661 int i, k;
6662 unsigned len;
6664 if (!bset || !eq)
6665 goto error;
6667 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6668 eq->n_row, 0);
6669 if (!bset)
6670 goto error;
6672 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6673 for (i = 0; i < eq->n_row; ++i) {
6674 k = isl_basic_set_alloc_equality(bset);
6675 if (k < 0)
6676 goto error;
6677 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6678 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6680 isl_mat_free(eq);
6682 bset = isl_basic_set_gauss(bset, NULL);
6683 bset = isl_basic_set_finalize(bset);
6685 return bset;
6686 error:
6687 isl_mat_free(eq);
6688 isl_basic_set_free(bset);
6689 return NULL;
6692 /* Intersect the set "set" with the affine space specified by the
6693 * equalities in "eq".
6695 static struct isl_set *set_append_equalities(struct isl_set *set,
6696 struct isl_mat *eq)
6698 int i;
6700 if (!set || !eq)
6701 goto error;
6703 for (i = 0; i < set->n; ++i) {
6704 set->p[i] = basic_set_append_equalities(set->p[i],
6705 isl_mat_copy(eq));
6706 if (!set->p[i])
6707 goto error;
6709 isl_mat_free(eq);
6710 return set;
6711 error:
6712 isl_mat_free(eq);
6713 isl_set_free(set);
6714 return NULL;
6717 /* Given a basic set "bset" that only involves parameters and existentially
6718 * quantified variables, return the index of the first equality
6719 * that only involves parameters. If there is no such equality then
6720 * return bset->n_eq.
6722 * This function assumes that isl_basic_set_gauss has been called on "bset".
6724 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6726 int i, j;
6727 unsigned nparam, n_div;
6729 if (!bset)
6730 return -1;
6732 nparam = isl_basic_set_dim(bset, isl_dim_param);
6733 n_div = isl_basic_set_dim(bset, isl_dim_div);
6735 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6736 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6737 ++i;
6740 return i;
6743 /* Compute an explicit representation for the existentially quantified
6744 * variables in "bset" by computing the "minimal value" of the set
6745 * variables. Since there are no set variables, the computation of
6746 * the minimal value essentially computes an explicit representation
6747 * of the non-empty part(s) of "bset".
6749 * The input only involves parameters and existentially quantified variables.
6750 * All equalities among parameters have been removed.
6752 * Since the existentially quantified variables in the result are in general
6753 * going to be different from those in the input, we first replace
6754 * them by the minimal number of variables based on their equalities.
6755 * This should simplify the parametric integer programming.
6757 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6759 isl_morph *morph1, *morph2;
6760 isl_set *set;
6761 unsigned n;
6763 if (!bset)
6764 return NULL;
6765 if (bset->n_eq == 0)
6766 return isl_basic_set_lexmin(bset);
6768 morph1 = isl_basic_set_parameter_compression(bset);
6769 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6770 bset = isl_basic_set_lift(bset);
6771 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6772 bset = isl_morph_basic_set(morph2, bset);
6773 n = isl_basic_set_dim(bset, isl_dim_set);
6774 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6776 set = isl_basic_set_lexmin(bset);
6778 set = isl_morph_set(isl_morph_inverse(morph1), set);
6780 return set;
6783 /* Project the given basic set onto its parameter domain, possibly introducing
6784 * new, explicit, existential variables in the constraints.
6785 * The input has parameters and (possibly implicit) existential variables.
6786 * The output has the same parameters, but only
6787 * explicit existentially quantified variables.
6789 * The actual projection is performed by pip, but pip doesn't seem
6790 * to like equalities very much, so we first remove the equalities
6791 * among the parameters by performing a variable compression on
6792 * the parameters. Afterward, an inverse transformation is performed
6793 * and the equalities among the parameters are inserted back in.
6795 * The variable compression on the parameters may uncover additional
6796 * equalities that were only implicit before. We therefore check
6797 * if there are any new parameter equalities in the result and
6798 * if so recurse. The removal of parameter equalities is required
6799 * for the parameter compression performed by base_compute_divs.
6801 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6803 int i;
6804 struct isl_mat *eq;
6805 struct isl_mat *T, *T2;
6806 struct isl_set *set;
6807 unsigned nparam;
6809 bset = isl_basic_set_cow(bset);
6810 if (!bset)
6811 return NULL;
6813 if (bset->n_eq == 0)
6814 return base_compute_divs(bset);
6816 bset = isl_basic_set_gauss(bset, NULL);
6817 if (!bset)
6818 return NULL;
6819 if (isl_basic_set_plain_is_empty(bset))
6820 return isl_set_from_basic_set(bset);
6822 i = first_parameter_equality(bset);
6823 if (i == bset->n_eq)
6824 return base_compute_divs(bset);
6826 nparam = isl_basic_set_dim(bset, isl_dim_param);
6827 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6828 0, 1 + nparam);
6829 eq = isl_mat_cow(eq);
6830 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6831 if (T && T->n_col == 0) {
6832 isl_mat_free(T);
6833 isl_mat_free(T2);
6834 isl_mat_free(eq);
6835 bset = isl_basic_set_set_to_empty(bset);
6836 return isl_set_from_basic_set(bset);
6838 bset = basic_set_parameter_preimage(bset, T);
6840 i = first_parameter_equality(bset);
6841 if (!bset)
6842 set = NULL;
6843 else if (i == bset->n_eq)
6844 set = base_compute_divs(bset);
6845 else
6846 set = parameter_compute_divs(bset);
6847 set = set_parameter_preimage(set, T2);
6848 set = set_append_equalities(set, eq);
6849 return set;
6852 /* Insert the divs from "ls" before those of "bmap".
6854 * The number of columns is not changed, which means that the last
6855 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6856 * The caller is responsible for removing the same number of dimensions
6857 * from the space of "bmap".
6859 static __isl_give isl_basic_map *insert_divs_from_local_space(
6860 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6862 int i;
6863 int n_div;
6864 int old_n_div;
6866 n_div = isl_local_space_dim(ls, isl_dim_div);
6867 if (n_div == 0)
6868 return bmap;
6870 old_n_div = bmap->n_div;
6871 bmap = insert_div_rows(bmap, n_div);
6872 if (!bmap)
6873 return NULL;
6875 for (i = 0; i < n_div; ++i) {
6876 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6877 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6880 return bmap;
6883 /* Replace the space of "bmap" by the space and divs of "ls".
6885 * If "ls" has any divs, then we simplify the result since we may
6886 * have discovered some additional equalities that could simplify
6887 * the div expressions.
6889 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6890 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6892 int n_div;
6894 bmap = isl_basic_map_cow(bmap);
6895 if (!bmap || !ls)
6896 goto error;
6898 n_div = isl_local_space_dim(ls, isl_dim_div);
6899 bmap = insert_divs_from_local_space(bmap, ls);
6900 if (!bmap)
6901 goto error;
6903 isl_space_free(bmap->dim);
6904 bmap->dim = isl_local_space_get_space(ls);
6905 if (!bmap->dim)
6906 goto error;
6908 isl_local_space_free(ls);
6909 if (n_div > 0)
6910 bmap = isl_basic_map_simplify(bmap);
6911 bmap = isl_basic_map_finalize(bmap);
6912 return bmap;
6913 error:
6914 isl_basic_map_free(bmap);
6915 isl_local_space_free(ls);
6916 return NULL;
6919 /* Replace the space of "map" by the space and divs of "ls".
6921 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6922 __isl_take isl_local_space *ls)
6924 int i;
6926 map = isl_map_cow(map);
6927 if (!map || !ls)
6928 goto error;
6930 for (i = 0; i < map->n; ++i) {
6931 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6932 isl_local_space_copy(ls));
6933 if (!map->p[i])
6934 goto error;
6936 isl_space_free(map->dim);
6937 map->dim = isl_local_space_get_space(ls);
6938 if (!map->dim)
6939 goto error;
6941 isl_local_space_free(ls);
6942 return map;
6943 error:
6944 isl_local_space_free(ls);
6945 isl_map_free(map);
6946 return NULL;
6949 /* Compute an explicit representation for the existentially
6950 * quantified variables for which do not know any explicit representation yet.
6952 * We first sort the existentially quantified variables so that the
6953 * existentially quantified variables for which we already have an explicit
6954 * representation are placed before those for which we do not.
6955 * The input dimensions, the output dimensions and the existentially
6956 * quantified variables for which we already have an explicit
6957 * representation are then turned into parameters.
6958 * compute_divs returns a map with the same parameters and
6959 * no input or output dimensions and the dimension specification
6960 * is reset to that of the input, including the existentially quantified
6961 * variables for which we already had an explicit representation.
6963 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6965 struct isl_basic_set *bset;
6966 struct isl_set *set;
6967 struct isl_map *map;
6968 isl_space *dim;
6969 isl_local_space *ls;
6970 unsigned nparam;
6971 unsigned n_in;
6972 unsigned n_out;
6973 unsigned n_known;
6974 int i;
6976 bmap = isl_basic_map_sort_divs(bmap);
6977 bmap = isl_basic_map_cow(bmap);
6978 if (!bmap)
6979 return NULL;
6981 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6982 if (isl_int_is_zero(bmap->div[n_known][0]))
6983 break;
6985 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6986 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6987 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6988 dim = isl_space_set_alloc(bmap->ctx,
6989 nparam + n_in + n_out + n_known, 0);
6990 if (!dim)
6991 goto error;
6993 ls = isl_basic_map_get_local_space(bmap);
6994 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6995 n_known, bmap->n_div - n_known);
6996 if (n_known > 0) {
6997 for (i = n_known; i < bmap->n_div; ++i)
6998 swap_div(bmap, i - n_known, i);
6999 bmap->n_div -= n_known;
7000 bmap->extra -= n_known;
7002 bmap = isl_basic_map_reset_space(bmap, dim);
7003 bset = (struct isl_basic_set *)bmap;
7005 set = parameter_compute_divs(bset);
7006 map = (struct isl_map *)set;
7007 map = replace_space_by_local_space(map, ls);
7009 return map;
7010 error:
7011 isl_basic_map_free(bmap);
7012 return NULL;
7015 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7017 int i;
7018 unsigned off;
7020 if (!bmap)
7021 return -1;
7023 off = isl_space_dim(bmap->dim, isl_dim_all);
7024 for (i = 0; i < bmap->n_div; ++i) {
7025 if (isl_int_is_zero(bmap->div[i][0]))
7026 return 0;
7027 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
7028 return -1);
7030 return 1;
7033 static int map_divs_known(__isl_keep isl_map *map)
7035 int i;
7037 if (!map)
7038 return -1;
7040 for (i = 0; i < map->n; ++i) {
7041 int known = isl_basic_map_divs_known(map->p[i]);
7042 if (known <= 0)
7043 return known;
7046 return 1;
7049 /* If bmap contains any unknown divs, then compute explicit
7050 * expressions for them. However, this computation may be
7051 * quite expensive, so first try to remove divs that aren't
7052 * strictly needed.
7054 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7056 int known;
7057 struct isl_map *map;
7059 known = isl_basic_map_divs_known(bmap);
7060 if (known < 0)
7061 goto error;
7062 if (known)
7063 return isl_map_from_basic_map(bmap);
7065 bmap = isl_basic_map_drop_redundant_divs(bmap);
7067 known = isl_basic_map_divs_known(bmap);
7068 if (known < 0)
7069 goto error;
7070 if (known)
7071 return isl_map_from_basic_map(bmap);
7073 map = compute_divs(bmap);
7074 return map;
7075 error:
7076 isl_basic_map_free(bmap);
7077 return NULL;
7080 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7082 int i;
7083 int known;
7084 struct isl_map *res;
7086 if (!map)
7087 return NULL;
7088 if (map->n == 0)
7089 return map;
7091 known = map_divs_known(map);
7092 if (known < 0) {
7093 isl_map_free(map);
7094 return NULL;
7096 if (known)
7097 return map;
7099 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7100 for (i = 1 ; i < map->n; ++i) {
7101 struct isl_map *r2;
7102 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7103 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7104 res = isl_map_union_disjoint(res, r2);
7105 else
7106 res = isl_map_union(res, r2);
7108 isl_map_free(map);
7110 return res;
7113 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7115 return (struct isl_set *)
7116 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7119 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7121 return (struct isl_set *)
7122 isl_map_compute_divs((struct isl_map *)set);
7125 struct isl_set *isl_map_domain(struct isl_map *map)
7127 int i;
7128 struct isl_set *set;
7130 if (!map)
7131 goto error;
7133 map = isl_map_cow(map);
7134 if (!map)
7135 return NULL;
7137 set = (struct isl_set *)map;
7138 set->dim = isl_space_domain(set->dim);
7139 if (!set->dim)
7140 goto error;
7141 for (i = 0; i < map->n; ++i) {
7142 set->p[i] = isl_basic_map_domain(map->p[i]);
7143 if (!set->p[i])
7144 goto error;
7146 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7147 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7148 return set;
7149 error:
7150 isl_map_free(map);
7151 return NULL;
7154 /* Return the union of "map1" and "map2", where we assume for now that
7155 * "map1" and "map2" are disjoint. Note that the basic maps inside
7156 * "map1" or "map2" may not be disjoint from each other.
7157 * Also note that this function is also called from isl_map_union,
7158 * which takes care of handling the situation where "map1" and "map2"
7159 * may not be disjoint.
7161 * If one of the inputs is empty, we can simply return the other input.
7162 * Similarly, if one of the inputs is universal, then it is equal to the union.
7164 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7165 __isl_take isl_map *map2)
7167 int i;
7168 unsigned flags = 0;
7169 struct isl_map *map = NULL;
7170 int is_universe;
7172 if (!map1 || !map2)
7173 goto error;
7175 if (!isl_space_is_equal(map1->dim, map2->dim))
7176 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7177 "spaces don't match", goto error);
7179 if (map1->n == 0) {
7180 isl_map_free(map1);
7181 return map2;
7183 if (map2->n == 0) {
7184 isl_map_free(map2);
7185 return map1;
7188 is_universe = isl_map_plain_is_universe(map1);
7189 if (is_universe < 0)
7190 goto error;
7191 if (is_universe) {
7192 isl_map_free(map2);
7193 return map1;
7196 is_universe = isl_map_plain_is_universe(map2);
7197 if (is_universe < 0)
7198 goto error;
7199 if (is_universe) {
7200 isl_map_free(map1);
7201 return map2;
7204 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7205 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7206 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7208 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7209 map1->n + map2->n, flags);
7210 if (!map)
7211 goto error;
7212 for (i = 0; i < map1->n; ++i) {
7213 map = isl_map_add_basic_map(map,
7214 isl_basic_map_copy(map1->p[i]));
7215 if (!map)
7216 goto error;
7218 for (i = 0; i < map2->n; ++i) {
7219 map = isl_map_add_basic_map(map,
7220 isl_basic_map_copy(map2->p[i]));
7221 if (!map)
7222 goto error;
7224 isl_map_free(map1);
7225 isl_map_free(map2);
7226 return map;
7227 error:
7228 isl_map_free(map);
7229 isl_map_free(map1);
7230 isl_map_free(map2);
7231 return NULL;
7234 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7235 * guaranteed to be disjoint by the caller.
7237 * Note that this functions is called from within isl_map_make_disjoint,
7238 * so we have to be careful not to touch the constraints of the inputs
7239 * in any way.
7241 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7242 __isl_take isl_map *map2)
7244 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7247 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7248 * not be disjoint. The parameters are assumed to have been aligned.
7250 * We currently simply call map_union_disjoint, the internal operation
7251 * of which does not really depend on the inputs being disjoint.
7252 * If the result contains more than one basic map, then we clear
7253 * the disjoint flag since the result may contain basic maps from
7254 * both inputs and these are not guaranteed to be disjoint.
7256 * As a special case, if "map1" and "map2" are obviously equal,
7257 * then we simply return "map1".
7259 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7260 __isl_take isl_map *map2)
7262 int equal;
7264 if (!map1 || !map2)
7265 goto error;
7267 equal = isl_map_plain_is_equal(map1, map2);
7268 if (equal < 0)
7269 goto error;
7270 if (equal) {
7271 isl_map_free(map2);
7272 return map1;
7275 map1 = map_union_disjoint(map1, map2);
7276 if (!map1)
7277 return NULL;
7278 if (map1->n > 1)
7279 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7280 return map1;
7281 error:
7282 isl_map_free(map1);
7283 isl_map_free(map2);
7284 return NULL;
7287 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7288 * not be disjoint.
7290 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7291 __isl_take isl_map *map2)
7293 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7296 struct isl_set *isl_set_union_disjoint(
7297 struct isl_set *set1, struct isl_set *set2)
7299 return (struct isl_set *)
7300 isl_map_union_disjoint(
7301 (struct isl_map *)set1, (struct isl_map *)set2);
7304 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7306 return (struct isl_set *)
7307 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7310 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7311 * the results.
7313 * "map" and "set" are assumed to be compatible and non-NULL.
7315 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7316 __isl_take isl_set *set,
7317 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7318 __isl_take isl_basic_set *bset))
7320 unsigned flags = 0;
7321 struct isl_map *result;
7322 int i, j;
7324 if (isl_set_plain_is_universe(set)) {
7325 isl_set_free(set);
7326 return map;
7329 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7330 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7331 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7333 result = isl_map_alloc_space(isl_space_copy(map->dim),
7334 map->n * set->n, flags);
7335 for (i = 0; result && i < map->n; ++i)
7336 for (j = 0; j < set->n; ++j) {
7337 result = isl_map_add_basic_map(result,
7338 fn(isl_basic_map_copy(map->p[i]),
7339 isl_basic_set_copy(set->p[j])));
7340 if (!result)
7341 break;
7344 isl_map_free(map);
7345 isl_set_free(set);
7346 return result;
7349 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7350 __isl_take isl_set *set)
7352 if (!map || !set)
7353 goto error;
7355 if (!isl_map_compatible_range(map, set))
7356 isl_die(set->ctx, isl_error_invalid,
7357 "incompatible spaces", goto error);
7359 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7360 error:
7361 isl_map_free(map);
7362 isl_set_free(set);
7363 return NULL;
7366 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7367 __isl_take isl_set *set)
7369 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7372 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7373 __isl_take isl_set *set)
7375 if (!map || !set)
7376 goto error;
7378 if (!isl_map_compatible_domain(map, set))
7379 isl_die(set->ctx, isl_error_invalid,
7380 "incompatible spaces", goto error);
7382 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7383 error:
7384 isl_map_free(map);
7385 isl_set_free(set);
7386 return NULL;
7389 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7390 __isl_take isl_set *set)
7392 return isl_map_align_params_map_map_and(map, set,
7393 &map_intersect_domain);
7396 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7397 __isl_take isl_map *map2)
7399 if (!map1 || !map2)
7400 goto error;
7401 map1 = isl_map_reverse(map1);
7402 map1 = isl_map_apply_range(map1, map2);
7403 return isl_map_reverse(map1);
7404 error:
7405 isl_map_free(map1);
7406 isl_map_free(map2);
7407 return NULL;
7410 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7411 __isl_take isl_map *map2)
7413 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7416 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7417 __isl_take isl_map *map2)
7419 isl_space *dim_result;
7420 struct isl_map *result;
7421 int i, j;
7423 if (!map1 || !map2)
7424 goto error;
7426 dim_result = isl_space_join(isl_space_copy(map1->dim),
7427 isl_space_copy(map2->dim));
7429 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7430 if (!result)
7431 goto error;
7432 for (i = 0; i < map1->n; ++i)
7433 for (j = 0; j < map2->n; ++j) {
7434 result = isl_map_add_basic_map(result,
7435 isl_basic_map_apply_range(
7436 isl_basic_map_copy(map1->p[i]),
7437 isl_basic_map_copy(map2->p[j])));
7438 if (!result)
7439 goto error;
7441 isl_map_free(map1);
7442 isl_map_free(map2);
7443 if (result && result->n <= 1)
7444 ISL_F_SET(result, ISL_MAP_DISJOINT);
7445 return result;
7446 error:
7447 isl_map_free(map1);
7448 isl_map_free(map2);
7449 return NULL;
7452 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7453 __isl_take isl_map *map2)
7455 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7459 * returns range - domain
7461 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7463 isl_space *dims, *target_dim;
7464 struct isl_basic_set *bset;
7465 unsigned dim;
7466 unsigned nparam;
7467 int i;
7469 if (!bmap)
7470 goto error;
7471 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7472 bmap->dim, isl_dim_out),
7473 goto error);
7474 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7475 dim = isl_basic_map_n_in(bmap);
7476 nparam = isl_basic_map_n_param(bmap);
7477 bset = isl_basic_set_from_basic_map(bmap);
7478 bset = isl_basic_set_cow(bset);
7479 dims = isl_basic_set_get_space(bset);
7480 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7481 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7482 bset = isl_basic_set_swap_vars(bset, 2*dim);
7483 for (i = 0; i < dim; ++i) {
7484 int j = isl_basic_map_alloc_equality(
7485 (struct isl_basic_map *)bset);
7486 if (j < 0) {
7487 bset = isl_basic_set_free(bset);
7488 break;
7490 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7491 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7492 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7493 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7495 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7496 bset = isl_basic_set_reset_space(bset, target_dim);
7497 return bset;
7498 error:
7499 isl_basic_map_free(bmap);
7500 return NULL;
7504 * returns range - domain
7506 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7508 int i;
7509 isl_space *dim;
7510 struct isl_set *result;
7512 if (!map)
7513 return NULL;
7515 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7516 map->dim, isl_dim_out),
7517 goto error);
7518 dim = isl_map_get_space(map);
7519 dim = isl_space_domain(dim);
7520 result = isl_set_alloc_space(dim, map->n, 0);
7521 if (!result)
7522 goto error;
7523 for (i = 0; i < map->n; ++i)
7524 result = isl_set_add_basic_set(result,
7525 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7526 isl_map_free(map);
7527 return result;
7528 error:
7529 isl_map_free(map);
7530 return NULL;
7534 * returns [domain -> range] -> range - domain
7536 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7537 __isl_take isl_basic_map *bmap)
7539 int i, k;
7540 isl_space *dim;
7541 isl_basic_map *domain;
7542 int nparam, n;
7543 unsigned total;
7545 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7546 bmap->dim, isl_dim_out))
7547 isl_die(bmap->ctx, isl_error_invalid,
7548 "domain and range don't match", goto error);
7550 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7551 n = isl_basic_map_dim(bmap, isl_dim_in);
7553 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7554 domain = isl_basic_map_universe(dim);
7556 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7557 bmap = isl_basic_map_apply_range(bmap, domain);
7558 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7560 total = isl_basic_map_total_dim(bmap);
7562 for (i = 0; i < n; ++i) {
7563 k = isl_basic_map_alloc_equality(bmap);
7564 if (k < 0)
7565 goto error;
7566 isl_seq_clr(bmap->eq[k], 1 + total);
7567 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7568 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7569 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7572 bmap = isl_basic_map_gauss(bmap, NULL);
7573 return isl_basic_map_finalize(bmap);
7574 error:
7575 isl_basic_map_free(bmap);
7576 return NULL;
7580 * returns [domain -> range] -> range - domain
7582 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7584 int i;
7585 isl_space *domain_dim;
7587 if (!map)
7588 return NULL;
7590 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7591 map->dim, isl_dim_out))
7592 isl_die(map->ctx, isl_error_invalid,
7593 "domain and range don't match", goto error);
7595 map = isl_map_cow(map);
7596 if (!map)
7597 return NULL;
7599 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7600 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7601 map->dim = isl_space_join(map->dim, domain_dim);
7602 if (!map->dim)
7603 goto error;
7604 for (i = 0; i < map->n; ++i) {
7605 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7606 if (!map->p[i])
7607 goto error;
7609 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7610 return map;
7611 error:
7612 isl_map_free(map);
7613 return NULL;
7616 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7618 struct isl_basic_map *bmap;
7619 unsigned nparam;
7620 unsigned dim;
7621 int i;
7623 if (!dims)
7624 return NULL;
7626 nparam = dims->nparam;
7627 dim = dims->n_out;
7628 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7629 if (!bmap)
7630 goto error;
7632 for (i = 0; i < dim; ++i) {
7633 int j = isl_basic_map_alloc_equality(bmap);
7634 if (j < 0)
7635 goto error;
7636 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7637 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7638 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7640 return isl_basic_map_finalize(bmap);
7641 error:
7642 isl_basic_map_free(bmap);
7643 return NULL;
7646 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7648 if (!dim)
7649 return NULL;
7650 if (dim->n_in != dim->n_out)
7651 isl_die(dim->ctx, isl_error_invalid,
7652 "number of input and output dimensions needs to be "
7653 "the same", goto error);
7654 return basic_map_identity(dim);
7655 error:
7656 isl_space_free(dim);
7657 return NULL;
7660 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7662 if (!model || !model->dim)
7663 return NULL;
7664 return isl_basic_map_identity(isl_space_copy(model->dim));
7667 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7669 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7672 struct isl_map *isl_map_identity_like(struct isl_map *model)
7674 if (!model || !model->dim)
7675 return NULL;
7676 return isl_map_identity(isl_space_copy(model->dim));
7679 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7681 if (!model || !model->dim)
7682 return NULL;
7683 return isl_map_identity(isl_space_copy(model->dim));
7686 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7688 isl_space *dim = isl_set_get_space(set);
7689 isl_map *id;
7690 id = isl_map_identity(isl_space_map_from_set(dim));
7691 return isl_map_intersect_range(id, set);
7694 /* Construct a basic set with all set dimensions having only non-negative
7695 * values.
7697 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7698 __isl_take isl_space *space)
7700 int i;
7701 unsigned nparam;
7702 unsigned dim;
7703 struct isl_basic_set *bset;
7705 if (!space)
7706 return NULL;
7707 nparam = space->nparam;
7708 dim = space->n_out;
7709 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7710 if (!bset)
7711 return NULL;
7712 for (i = 0; i < dim; ++i) {
7713 int k = isl_basic_set_alloc_inequality(bset);
7714 if (k < 0)
7715 goto error;
7716 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7717 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7719 return bset;
7720 error:
7721 isl_basic_set_free(bset);
7722 return NULL;
7725 /* Construct the half-space x_pos >= 0.
7727 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7728 int pos)
7730 int k;
7731 isl_basic_set *nonneg;
7733 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7734 k = isl_basic_set_alloc_inequality(nonneg);
7735 if (k < 0)
7736 goto error;
7737 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7738 isl_int_set_si(nonneg->ineq[k][pos], 1);
7740 return isl_basic_set_finalize(nonneg);
7741 error:
7742 isl_basic_set_free(nonneg);
7743 return NULL;
7746 /* Construct the half-space x_pos <= -1.
7748 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7750 int k;
7751 isl_basic_set *neg;
7753 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7754 k = isl_basic_set_alloc_inequality(neg);
7755 if (k < 0)
7756 goto error;
7757 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7758 isl_int_set_si(neg->ineq[k][0], -1);
7759 isl_int_set_si(neg->ineq[k][pos], -1);
7761 return isl_basic_set_finalize(neg);
7762 error:
7763 isl_basic_set_free(neg);
7764 return NULL;
7767 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7768 enum isl_dim_type type, unsigned first, unsigned n)
7770 int i;
7771 isl_basic_set *nonneg;
7772 isl_basic_set *neg;
7774 if (!set)
7775 return NULL;
7776 if (n == 0)
7777 return set;
7779 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7781 for (i = 0; i < n; ++i) {
7782 nonneg = nonneg_halfspace(isl_set_get_space(set),
7783 pos(set->dim, type) + first + i);
7784 neg = neg_halfspace(isl_set_get_space(set),
7785 pos(set->dim, type) + first + i);
7787 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7790 return set;
7791 error:
7792 isl_set_free(set);
7793 return NULL;
7796 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7797 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7798 void *user)
7800 isl_set *half;
7802 if (!set)
7803 return -1;
7804 if (isl_set_plain_is_empty(set)) {
7805 isl_set_free(set);
7806 return 0;
7808 if (first == len)
7809 return fn(set, signs, user);
7811 signs[first] = 1;
7812 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7813 1 + first));
7814 half = isl_set_intersect(half, isl_set_copy(set));
7815 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7816 goto error;
7818 signs[first] = -1;
7819 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7820 1 + first));
7821 half = isl_set_intersect(half, set);
7822 return foreach_orthant(half, signs, first + 1, len, fn, user);
7823 error:
7824 isl_set_free(set);
7825 return -1;
7828 /* Call "fn" on the intersections of "set" with each of the orthants
7829 * (except for obviously empty intersections). The orthant is identified
7830 * by the signs array, with each entry having value 1 or -1 according
7831 * to the sign of the corresponding variable.
7833 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7834 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7835 void *user)
7837 unsigned nparam;
7838 unsigned nvar;
7839 int *signs;
7840 int r;
7842 if (!set)
7843 return -1;
7844 if (isl_set_plain_is_empty(set))
7845 return 0;
7847 nparam = isl_set_dim(set, isl_dim_param);
7848 nvar = isl_set_dim(set, isl_dim_set);
7850 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7852 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7853 fn, user);
7855 free(signs);
7857 return r;
7860 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7862 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7865 int isl_basic_map_is_subset(
7866 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7868 int is_subset;
7869 struct isl_map *map1;
7870 struct isl_map *map2;
7872 if (!bmap1 || !bmap2)
7873 return -1;
7875 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7876 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7878 is_subset = isl_map_is_subset(map1, map2);
7880 isl_map_free(map1);
7881 isl_map_free(map2);
7883 return is_subset;
7886 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7887 __isl_keep isl_basic_set *bset2)
7889 return isl_basic_map_is_subset(bset1, bset2);
7892 int isl_basic_map_is_equal(
7893 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7895 int is_subset;
7897 if (!bmap1 || !bmap2)
7898 return -1;
7899 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7900 if (is_subset != 1)
7901 return is_subset;
7902 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7903 return is_subset;
7906 int isl_basic_set_is_equal(
7907 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7909 return isl_basic_map_is_equal(
7910 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7913 int isl_map_is_empty(struct isl_map *map)
7915 int i;
7916 int is_empty;
7918 if (!map)
7919 return -1;
7920 for (i = 0; i < map->n; ++i) {
7921 is_empty = isl_basic_map_is_empty(map->p[i]);
7922 if (is_empty < 0)
7923 return -1;
7924 if (!is_empty)
7925 return 0;
7927 return 1;
7930 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7932 return map ? map->n == 0 : -1;
7935 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7937 return isl_map_plain_is_empty(map);
7940 int isl_set_plain_is_empty(struct isl_set *set)
7942 return set ? set->n == 0 : -1;
7945 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7947 return isl_set_plain_is_empty(set);
7950 int isl_set_is_empty(struct isl_set *set)
7952 return isl_map_is_empty((struct isl_map *)set);
7955 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7957 if (!map1 || !map2)
7958 return -1;
7960 return isl_space_is_equal(map1->dim, map2->dim);
7963 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7965 if (!set1 || !set2)
7966 return -1;
7968 return isl_space_is_equal(set1->dim, set2->dim);
7971 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7973 int is_subset;
7975 if (!map1 || !map2)
7976 return -1;
7977 is_subset = isl_map_is_subset(map1, map2);
7978 if (is_subset != 1)
7979 return is_subset;
7980 is_subset = isl_map_is_subset(map2, map1);
7981 return is_subset;
7984 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7986 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7989 int isl_basic_map_is_strict_subset(
7990 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7992 int is_subset;
7994 if (!bmap1 || !bmap2)
7995 return -1;
7996 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7997 if (is_subset != 1)
7998 return is_subset;
7999 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8000 if (is_subset == -1)
8001 return is_subset;
8002 return !is_subset;
8005 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
8007 int is_subset;
8009 if (!map1 || !map2)
8010 return -1;
8011 is_subset = isl_map_is_subset(map1, map2);
8012 if (is_subset != 1)
8013 return is_subset;
8014 is_subset = isl_map_is_subset(map2, map1);
8015 if (is_subset == -1)
8016 return is_subset;
8017 return !is_subset;
8020 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8022 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
8025 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
8027 if (!bmap)
8028 return -1;
8029 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8032 int isl_basic_set_is_universe(struct isl_basic_set *bset)
8034 if (!bset)
8035 return -1;
8036 return bset->n_eq == 0 && bset->n_ineq == 0;
8039 int isl_map_plain_is_universe(__isl_keep isl_map *map)
8041 int i;
8043 if (!map)
8044 return -1;
8046 for (i = 0; i < map->n; ++i) {
8047 int r = isl_basic_map_is_universe(map->p[i]);
8048 if (r < 0 || r)
8049 return r;
8052 return 0;
8055 int isl_set_plain_is_universe(__isl_keep isl_set *set)
8057 return isl_map_plain_is_universe((isl_map *) set);
8060 int isl_set_fast_is_universe(__isl_keep isl_set *set)
8062 return isl_set_plain_is_universe(set);
8065 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
8067 struct isl_basic_set *bset = NULL;
8068 struct isl_vec *sample = NULL;
8069 int empty;
8070 unsigned total;
8072 if (!bmap)
8073 return -1;
8075 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8076 return 1;
8078 if (isl_basic_map_is_universe(bmap))
8079 return 0;
8081 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8082 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8083 copy = isl_basic_map_remove_redundancies(copy);
8084 empty = isl_basic_map_plain_is_empty(copy);
8085 isl_basic_map_free(copy);
8086 return empty;
8089 total = 1 + isl_basic_map_total_dim(bmap);
8090 if (bmap->sample && bmap->sample->size == total) {
8091 int contains = isl_basic_map_contains(bmap, bmap->sample);
8092 if (contains < 0)
8093 return -1;
8094 if (contains)
8095 return 0;
8097 isl_vec_free(bmap->sample);
8098 bmap->sample = NULL;
8099 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8100 if (!bset)
8101 return -1;
8102 sample = isl_basic_set_sample_vec(bset);
8103 if (!sample)
8104 return -1;
8105 empty = sample->size == 0;
8106 isl_vec_free(bmap->sample);
8107 bmap->sample = sample;
8108 if (empty)
8109 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8111 return empty;
8114 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8116 if (!bmap)
8117 return -1;
8118 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8121 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
8123 return isl_basic_map_plain_is_empty(bmap);
8126 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8128 if (!bset)
8129 return -1;
8130 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8133 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
8135 return isl_basic_set_plain_is_empty(bset);
8138 int isl_basic_set_is_empty(struct isl_basic_set *bset)
8140 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8143 struct isl_map *isl_basic_map_union(
8144 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8146 struct isl_map *map;
8147 if (!bmap1 || !bmap2)
8148 goto error;
8150 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8152 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8153 if (!map)
8154 goto error;
8155 map = isl_map_add_basic_map(map, bmap1);
8156 map = isl_map_add_basic_map(map, bmap2);
8157 return map;
8158 error:
8159 isl_basic_map_free(bmap1);
8160 isl_basic_map_free(bmap2);
8161 return NULL;
8164 struct isl_set *isl_basic_set_union(
8165 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8167 return (struct isl_set *)isl_basic_map_union(
8168 (struct isl_basic_map *)bset1,
8169 (struct isl_basic_map *)bset2);
8172 /* Order divs such that any div only depends on previous divs */
8173 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8175 int i;
8176 unsigned off;
8178 if (!bmap)
8179 return NULL;
8181 off = isl_space_dim(bmap->dim, isl_dim_all);
8183 for (i = 0; i < bmap->n_div; ++i) {
8184 int pos;
8185 if (isl_int_is_zero(bmap->div[i][0]))
8186 continue;
8187 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8188 bmap->n_div-i);
8189 if (pos == -1)
8190 continue;
8191 isl_basic_map_swap_div(bmap, i, i + pos);
8192 --i;
8194 return bmap;
8197 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8199 return (struct isl_basic_set *)
8200 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8203 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8205 int i;
8207 if (!map)
8208 return 0;
8210 for (i = 0; i < map->n; ++i) {
8211 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8212 if (!map->p[i])
8213 goto error;
8216 return map;
8217 error:
8218 isl_map_free(map);
8219 return NULL;
8222 /* Apply the expansion computed by isl_merge_divs.
8223 * The expansion itself is given by "exp" while the resulting
8224 * list of divs is given by "div".
8226 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8227 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8229 int i, j;
8230 int n_div;
8232 bset = isl_basic_set_cow(bset);
8233 if (!bset || !div)
8234 goto error;
8236 if (div->n_row < bset->n_div)
8237 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8238 "not an expansion", goto error);
8240 n_div = bset->n_div;
8241 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8242 div->n_row - n_div, 0,
8243 2 * (div->n_row - n_div));
8245 for (i = n_div; i < div->n_row; ++i)
8246 if (isl_basic_set_alloc_div(bset) < 0)
8247 goto error;
8249 j = n_div - 1;
8250 for (i = div->n_row - 1; i >= 0; --i) {
8251 if (j >= 0 && exp[j] == i) {
8252 if (i != j)
8253 isl_basic_map_swap_div(bset, i, j);
8254 j--;
8255 } else {
8256 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8257 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8258 goto error;
8262 isl_mat_free(div);
8263 return bset;
8264 error:
8265 isl_basic_set_free(bset);
8266 isl_mat_free(div);
8267 return NULL;
8270 /* Look for a div in dst that corresponds to the div "div" in src.
8271 * The divs before "div" in src and dst are assumed to be the same.
8273 * Returns -1 if no corresponding div was found and the position
8274 * of the corresponding div in dst otherwise.
8276 static int find_div(struct isl_basic_map *dst,
8277 struct isl_basic_map *src, unsigned div)
8279 int i;
8281 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8283 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8284 for (i = div; i < dst->n_div; ++i)
8285 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8286 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8287 dst->n_div - div) == -1)
8288 return i;
8289 return -1;
8292 /* Align the divs of "dst" to those of "src", adding divs from "src"
8293 * if needed. That is, make sure that the first src->n_div divs
8294 * of the result are equal to those of src.
8296 * The result is not finalized as by design it will have redundant
8297 * divs if any divs from "src" were copied.
8299 __isl_give isl_basic_map *isl_basic_map_align_divs(
8300 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8302 int i;
8303 int known, extended;
8304 unsigned total;
8306 if (!dst || !src)
8307 return isl_basic_map_free(dst);
8309 if (src->n_div == 0)
8310 return dst;
8312 known = isl_basic_map_divs_known(src);
8313 if (known < 0)
8314 return isl_basic_map_free(dst);
8315 if (!known)
8316 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8317 "some src divs are unknown",
8318 return isl_basic_map_free(dst));
8320 src = isl_basic_map_order_divs(src);
8322 extended = 0;
8323 total = isl_space_dim(src->dim, isl_dim_all);
8324 for (i = 0; i < src->n_div; ++i) {
8325 int j = find_div(dst, src, i);
8326 if (j < 0) {
8327 if (!extended) {
8328 int extra = src->n_div - i;
8329 dst = isl_basic_map_cow(dst);
8330 if (!dst)
8331 return NULL;
8332 dst = isl_basic_map_extend_space(dst,
8333 isl_space_copy(dst->dim),
8334 extra, 0, 2 * extra);
8335 extended = 1;
8337 j = isl_basic_map_alloc_div(dst);
8338 if (j < 0)
8339 return isl_basic_map_free(dst);
8340 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8341 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8342 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8343 return isl_basic_map_free(dst);
8345 if (j != i)
8346 isl_basic_map_swap_div(dst, i, j);
8348 return dst;
8351 struct isl_basic_set *isl_basic_set_align_divs(
8352 struct isl_basic_set *dst, struct isl_basic_set *src)
8354 return (struct isl_basic_set *)isl_basic_map_align_divs(
8355 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8358 struct isl_map *isl_map_align_divs(struct isl_map *map)
8360 int i;
8362 if (!map)
8363 return NULL;
8364 if (map->n == 0)
8365 return map;
8366 map = isl_map_compute_divs(map);
8367 map = isl_map_cow(map);
8368 if (!map)
8369 return NULL;
8371 for (i = 1; i < map->n; ++i)
8372 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8373 for (i = 1; i < map->n; ++i) {
8374 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8375 if (!map->p[i])
8376 return isl_map_free(map);
8379 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8380 return map;
8383 struct isl_set *isl_set_align_divs(struct isl_set *set)
8385 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8388 /* Align the divs of the basic sets in "set" to those
8389 * of the basic sets in "list", as well as to the other basic sets in "set".
8390 * The elements in "list" are assumed to have known divs.
8392 __isl_give isl_set *isl_set_align_divs_to_basic_set_list(
8393 __isl_take isl_set *set, __isl_keep isl_basic_set_list *list)
8395 int i, n;
8397 set = isl_set_compute_divs(set);
8398 set = isl_set_cow(set);
8399 if (!set || !list)
8400 return isl_set_free(set);
8401 if (set->n == 0)
8402 return set;
8404 n = isl_basic_set_list_n_basic_set(list);
8405 for (i = 0; i < n; ++i) {
8406 isl_basic_set *bset;
8408 bset = isl_basic_set_list_get_basic_set(list, i);
8409 set->p[0] = isl_basic_set_align_divs(set->p[0], bset);
8410 isl_basic_set_free(bset);
8412 if (!set->p[0])
8413 return isl_set_free(set);
8415 return isl_set_align_divs(set);
8418 /* Align the divs of each element of "list" to those of "bset".
8419 * Both "bset" and the elements of "list" are assumed to have known divs.
8421 __isl_give isl_basic_set_list *isl_basic_set_list_align_divs_to_basic_set(
8422 __isl_take isl_basic_set_list *list, __isl_keep isl_basic_set *bset)
8424 int i, n;
8426 if (!list || !bset)
8427 return isl_basic_set_list_free(list);
8429 n = isl_basic_set_list_n_basic_set(list);
8430 for (i = 0; i < n; ++i) {
8431 isl_basic_set *bset_i;
8433 bset_i = isl_basic_set_list_get_basic_set(list, i);
8434 bset_i = isl_basic_set_align_divs(bset_i, bset);
8435 list = isl_basic_set_list_set_basic_set(list, i, bset_i);
8438 return list;
8441 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8442 __isl_take isl_map *map)
8444 if (!set || !map)
8445 goto error;
8446 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8447 map = isl_map_intersect_domain(map, set);
8448 set = isl_map_range(map);
8449 return set;
8450 error:
8451 isl_set_free(set);
8452 isl_map_free(map);
8453 return NULL;
8456 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8457 __isl_take isl_map *map)
8459 return isl_map_align_params_map_map_and(set, map, &set_apply);
8462 /* There is no need to cow as removing empty parts doesn't change
8463 * the meaning of the set.
8465 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8467 int i;
8469 if (!map)
8470 return NULL;
8472 for (i = map->n - 1; i >= 0; --i)
8473 remove_if_empty(map, i);
8475 return map;
8478 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8480 return (struct isl_set *)
8481 isl_map_remove_empty_parts((struct isl_map *)set);
8484 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8486 struct isl_basic_map *bmap;
8487 if (!map || map->n == 0)
8488 return NULL;
8489 bmap = map->p[map->n-1];
8490 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8491 return isl_basic_map_copy(bmap);
8494 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8496 return (struct isl_basic_set *)
8497 isl_map_copy_basic_map((struct isl_map *)set);
8500 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8501 __isl_keep isl_basic_map *bmap)
8503 int i;
8505 if (!map || !bmap)
8506 goto error;
8507 for (i = map->n-1; i >= 0; --i) {
8508 if (map->p[i] != bmap)
8509 continue;
8510 map = isl_map_cow(map);
8511 if (!map)
8512 goto error;
8513 isl_basic_map_free(map->p[i]);
8514 if (i != map->n-1) {
8515 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8516 map->p[i] = map->p[map->n-1];
8518 map->n--;
8519 return map;
8521 return map;
8522 error:
8523 isl_map_free(map);
8524 return NULL;
8527 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8528 struct isl_basic_set *bset)
8530 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8531 (struct isl_basic_map *)bset);
8534 /* Given two basic sets bset1 and bset2, compute the maximal difference
8535 * between the values of dimension pos in bset1 and those in bset2
8536 * for any common value of the parameters and dimensions preceding pos.
8538 static enum isl_lp_result basic_set_maximal_difference_at(
8539 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8540 int pos, isl_int *opt)
8542 isl_space *dims;
8543 struct isl_basic_map *bmap1 = NULL;
8544 struct isl_basic_map *bmap2 = NULL;
8545 struct isl_ctx *ctx;
8546 struct isl_vec *obj;
8547 unsigned total;
8548 unsigned nparam;
8549 unsigned dim1, dim2;
8550 enum isl_lp_result res;
8552 if (!bset1 || !bset2)
8553 return isl_lp_error;
8555 nparam = isl_basic_set_n_param(bset1);
8556 dim1 = isl_basic_set_n_dim(bset1);
8557 dim2 = isl_basic_set_n_dim(bset2);
8558 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8559 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8560 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8561 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8562 if (!bmap1 || !bmap2)
8563 goto error;
8564 bmap1 = isl_basic_map_cow(bmap1);
8565 bmap1 = isl_basic_map_extend(bmap1, nparam,
8566 pos, (dim1 - pos) + (dim2 - pos),
8567 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8568 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8569 if (!bmap1)
8570 goto error2;
8571 total = isl_basic_map_total_dim(bmap1);
8572 ctx = bmap1->ctx;
8573 obj = isl_vec_alloc(ctx, 1 + total);
8574 if (!obj)
8575 goto error2;
8576 isl_seq_clr(obj->block.data, 1 + total);
8577 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8578 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8579 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8580 opt, NULL, NULL);
8581 isl_basic_map_free(bmap1);
8582 isl_vec_free(obj);
8583 return res;
8584 error:
8585 isl_basic_map_free(bmap2);
8586 error2:
8587 isl_basic_map_free(bmap1);
8588 return isl_lp_error;
8591 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8592 * for any common value of the parameters and dimensions preceding pos
8593 * in both basic sets, the values of dimension pos in bset1 are
8594 * smaller or larger than those in bset2.
8596 * Returns
8597 * 1 if bset1 follows bset2
8598 * -1 if bset1 precedes bset2
8599 * 0 if bset1 and bset2 are incomparable
8600 * -2 if some error occurred.
8602 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8603 struct isl_basic_set *bset2, int pos)
8605 isl_int opt;
8606 enum isl_lp_result res;
8607 int cmp;
8609 isl_int_init(opt);
8611 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8613 if (res == isl_lp_empty)
8614 cmp = 0;
8615 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8616 res == isl_lp_unbounded)
8617 cmp = 1;
8618 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8619 cmp = -1;
8620 else
8621 cmp = -2;
8623 isl_int_clear(opt);
8624 return cmp;
8627 /* Given two basic sets bset1 and bset2, check whether
8628 * for any common value of the parameters and dimensions preceding pos
8629 * there is a value of dimension pos in bset1 that is larger
8630 * than a value of the same dimension in bset2.
8632 * Return
8633 * 1 if there exists such a pair
8634 * 0 if there is no such pair, but there is a pair of equal values
8635 * -1 otherwise
8636 * -2 if some error occurred.
8638 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8639 __isl_keep isl_basic_set *bset2, int pos)
8641 isl_int opt;
8642 enum isl_lp_result res;
8643 int cmp;
8645 isl_int_init(opt);
8647 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8649 if (res == isl_lp_empty)
8650 cmp = -1;
8651 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8652 res == isl_lp_unbounded)
8653 cmp = 1;
8654 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8655 cmp = -1;
8656 else if (res == isl_lp_ok)
8657 cmp = 0;
8658 else
8659 cmp = -2;
8661 isl_int_clear(opt);
8662 return cmp;
8665 /* Given two sets set1 and set2, check whether
8666 * for any common value of the parameters and dimensions preceding pos
8667 * there is a value of dimension pos in set1 that is larger
8668 * than a value of the same dimension in set2.
8670 * Return
8671 * 1 if there exists such a pair
8672 * 0 if there is no such pair, but there is a pair of equal values
8673 * -1 otherwise
8674 * -2 if some error occurred.
8676 int isl_set_follows_at(__isl_keep isl_set *set1,
8677 __isl_keep isl_set *set2, int pos)
8679 int i, j;
8680 int follows = -1;
8682 if (!set1 || !set2)
8683 return -2;
8685 for (i = 0; i < set1->n; ++i)
8686 for (j = 0; j < set2->n; ++j) {
8687 int f;
8688 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8689 if (f == 1 || f == -2)
8690 return f;
8691 if (f > follows)
8692 follows = f;
8695 return follows;
8698 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8699 unsigned pos, isl_int *val)
8701 int i;
8702 int d;
8703 unsigned total;
8705 if (!bmap)
8706 return -1;
8707 total = isl_basic_map_total_dim(bmap);
8708 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8709 for (; d+1 > pos; --d)
8710 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8711 break;
8712 if (d != pos)
8713 continue;
8714 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8715 return 0;
8716 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8717 return 0;
8718 if (!isl_int_is_one(bmap->eq[i][1+d]))
8719 return 0;
8720 if (val)
8721 isl_int_neg(*val, bmap->eq[i][0]);
8722 return 1;
8724 return 0;
8727 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8728 unsigned pos, isl_int *val)
8730 int i;
8731 isl_int v;
8732 isl_int tmp;
8733 int fixed;
8735 if (!map)
8736 return -1;
8737 if (map->n == 0)
8738 return 0;
8739 if (map->n == 1)
8740 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8741 isl_int_init(v);
8742 isl_int_init(tmp);
8743 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8744 for (i = 1; fixed == 1 && i < map->n; ++i) {
8745 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8746 if (fixed == 1 && isl_int_ne(tmp, v))
8747 fixed = 0;
8749 if (val)
8750 isl_int_set(*val, v);
8751 isl_int_clear(tmp);
8752 isl_int_clear(v);
8753 return fixed;
8756 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8757 unsigned pos, isl_int *val)
8759 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8760 pos, val);
8763 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8764 isl_int *val)
8766 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8769 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8770 enum isl_dim_type type, unsigned pos, isl_int *val)
8772 if (pos >= isl_basic_map_dim(bmap, type))
8773 return -1;
8774 return isl_basic_map_plain_has_fixed_var(bmap,
8775 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8778 /* If "bmap" obviously lies on a hyperplane where the given dimension
8779 * has a fixed value, then return that value.
8780 * Otherwise return NaN.
8782 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8783 __isl_keep isl_basic_map *bmap,
8784 enum isl_dim_type type, unsigned pos)
8786 isl_ctx *ctx;
8787 isl_val *v;
8788 int fixed;
8790 if (!bmap)
8791 return NULL;
8792 ctx = isl_basic_map_get_ctx(bmap);
8793 v = isl_val_alloc(ctx);
8794 if (!v)
8795 return NULL;
8796 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8797 if (fixed < 0)
8798 return isl_val_free(v);
8799 if (fixed) {
8800 isl_int_set_si(v->d, 1);
8801 return v;
8803 isl_val_free(v);
8804 return isl_val_nan(ctx);
8807 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8808 enum isl_dim_type type, unsigned pos, isl_int *val)
8810 if (pos >= isl_map_dim(map, type))
8811 return -1;
8812 return isl_map_plain_has_fixed_var(map,
8813 map_offset(map, type) - 1 + pos, val);
8816 /* If "map" obviously lies on a hyperplane where the given dimension
8817 * has a fixed value, then return that value.
8818 * Otherwise return NaN.
8820 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8821 enum isl_dim_type type, unsigned pos)
8823 isl_ctx *ctx;
8824 isl_val *v;
8825 int fixed;
8827 if (!map)
8828 return NULL;
8829 ctx = isl_map_get_ctx(map);
8830 v = isl_val_alloc(ctx);
8831 if (!v)
8832 return NULL;
8833 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8834 if (fixed < 0)
8835 return isl_val_free(v);
8836 if (fixed) {
8837 isl_int_set_si(v->d, 1);
8838 return v;
8840 isl_val_free(v);
8841 return isl_val_nan(ctx);
8844 /* If "set" obviously lies on a hyperplane where the given dimension
8845 * has a fixed value, then return that value.
8846 * Otherwise return NaN.
8848 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8849 enum isl_dim_type type, unsigned pos)
8851 return isl_map_plain_get_val_if_fixed(set, type, pos);
8854 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8855 enum isl_dim_type type, unsigned pos, isl_int *val)
8857 return isl_map_plain_is_fixed(set, type, pos, val);
8860 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8861 enum isl_dim_type type, unsigned pos, isl_int *val)
8863 return isl_map_plain_is_fixed(map, type, pos, val);
8866 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8867 * then return this fixed value in *val.
8869 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8870 unsigned dim, isl_int *val)
8872 return isl_basic_set_plain_has_fixed_var(bset,
8873 isl_basic_set_n_param(bset) + dim, val);
8876 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8877 * then return this fixed value in *val.
8879 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8880 unsigned dim, isl_int *val)
8882 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8885 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8886 unsigned dim, isl_int *val)
8888 return isl_set_plain_dim_is_fixed(set, dim, val);
8891 /* Check if input variable in has fixed value and if so and if val is not NULL,
8892 * then return this fixed value in *val.
8894 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8895 unsigned in, isl_int *val)
8897 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8900 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8901 * and if val is not NULL, then return this lower bound in *val.
8903 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8904 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8906 int i, i_eq = -1, i_ineq = -1;
8907 isl_int *c;
8908 unsigned total;
8909 unsigned nparam;
8911 if (!bset)
8912 return -1;
8913 total = isl_basic_set_total_dim(bset);
8914 nparam = isl_basic_set_n_param(bset);
8915 for (i = 0; i < bset->n_eq; ++i) {
8916 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8917 continue;
8918 if (i_eq != -1)
8919 return 0;
8920 i_eq = i;
8922 for (i = 0; i < bset->n_ineq; ++i) {
8923 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8924 continue;
8925 if (i_eq != -1 || i_ineq != -1)
8926 return 0;
8927 i_ineq = i;
8929 if (i_eq == -1 && i_ineq == -1)
8930 return 0;
8931 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8932 /* The coefficient should always be one due to normalization. */
8933 if (!isl_int_is_one(c[1+nparam+dim]))
8934 return 0;
8935 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8936 return 0;
8937 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8938 total - nparam - dim - 1) != -1)
8939 return 0;
8940 if (val)
8941 isl_int_neg(*val, c[0]);
8942 return 1;
8945 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8946 unsigned dim, isl_int *val)
8948 int i;
8949 isl_int v;
8950 isl_int tmp;
8951 int fixed;
8953 if (!set)
8954 return -1;
8955 if (set->n == 0)
8956 return 0;
8957 if (set->n == 1)
8958 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8959 dim, val);
8960 isl_int_init(v);
8961 isl_int_init(tmp);
8962 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8963 dim, &v);
8964 for (i = 1; fixed == 1 && i < set->n; ++i) {
8965 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8966 dim, &tmp);
8967 if (fixed == 1 && isl_int_ne(tmp, v))
8968 fixed = 0;
8970 if (val)
8971 isl_int_set(*val, v);
8972 isl_int_clear(tmp);
8973 isl_int_clear(v);
8974 return fixed;
8977 /* uset_gist depends on constraints without existentially quantified
8978 * variables sorting first.
8980 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8982 isl_int **c1 = (isl_int **) p1;
8983 isl_int **c2 = (isl_int **) p2;
8984 int l1, l2;
8985 unsigned size = *(unsigned *) arg;
8987 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8988 l2 = isl_seq_last_non_zero(*c2 + 1, size);
8990 if (l1 != l2)
8991 return l1 - l2;
8993 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
8996 static struct isl_basic_map *isl_basic_map_sort_constraints(
8997 struct isl_basic_map *bmap)
8999 unsigned total;
9001 if (!bmap)
9002 return NULL;
9003 if (bmap->n_ineq == 0)
9004 return bmap;
9005 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9006 return bmap;
9007 total = isl_basic_map_total_dim(bmap);
9008 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9009 &sort_constraint_cmp, &total) < 0)
9010 return isl_basic_map_free(bmap);
9011 return bmap;
9014 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9015 __isl_take isl_basic_set *bset)
9017 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9018 (struct isl_basic_map *)bset);
9021 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9023 if (!bmap)
9024 return NULL;
9025 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9026 return bmap;
9027 bmap = isl_basic_map_remove_redundancies(bmap);
9028 bmap = isl_basic_map_sort_constraints(bmap);
9029 if (bmap)
9030 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9031 return bmap;
9034 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9036 return (struct isl_basic_set *)isl_basic_map_normalize(
9037 (struct isl_basic_map *)bset);
9040 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9041 const __isl_keep isl_basic_map *bmap2)
9043 int i, cmp;
9044 unsigned total;
9046 if (bmap1 == bmap2)
9047 return 0;
9048 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9049 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9050 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9051 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9052 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9053 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9054 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9055 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9056 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9057 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9058 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9059 return 0;
9060 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9061 return 1;
9062 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9063 return -1;
9064 if (bmap1->n_eq != bmap2->n_eq)
9065 return bmap1->n_eq - bmap2->n_eq;
9066 if (bmap1->n_ineq != bmap2->n_ineq)
9067 return bmap1->n_ineq - bmap2->n_ineq;
9068 if (bmap1->n_div != bmap2->n_div)
9069 return bmap1->n_div - bmap2->n_div;
9070 total = isl_basic_map_total_dim(bmap1);
9071 for (i = 0; i < bmap1->n_eq; ++i) {
9072 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9073 if (cmp)
9074 return cmp;
9076 for (i = 0; i < bmap1->n_ineq; ++i) {
9077 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9078 if (cmp)
9079 return cmp;
9081 for (i = 0; i < bmap1->n_div; ++i) {
9082 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9083 if (cmp)
9084 return cmp;
9086 return 0;
9089 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9090 const __isl_keep isl_basic_set *bset2)
9092 return isl_basic_map_plain_cmp(bset1, bset2);
9095 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9097 int i, cmp;
9099 if (set1 == set2)
9100 return 0;
9101 if (set1->n != set2->n)
9102 return set1->n - set2->n;
9104 for (i = 0; i < set1->n; ++i) {
9105 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9106 if (cmp)
9107 return cmp;
9110 return 0;
9113 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9114 __isl_keep isl_basic_map *bmap2)
9116 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9119 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9120 __isl_keep isl_basic_set *bset2)
9122 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9123 (isl_basic_map *)bset2);
9126 static int qsort_bmap_cmp(const void *p1, const void *p2)
9128 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9129 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9131 return isl_basic_map_plain_cmp(bmap1, bmap2);
9134 /* Sort the basic maps of "map" and remove duplicate basic maps.
9136 * While removing basic maps, we make sure that the basic maps remain
9137 * sorted because isl_map_normalize expects the basic maps of the result
9138 * to be sorted.
9140 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9142 int i, j;
9144 map = isl_map_remove_empty_parts(map);
9145 if (!map)
9146 return NULL;
9147 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9148 for (i = map->n - 1; i >= 1; --i) {
9149 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9150 continue;
9151 isl_basic_map_free(map->p[i-1]);
9152 for (j = i; j < map->n; ++j)
9153 map->p[j - 1] = map->p[j];
9154 map->n--;
9157 return map;
9160 /* Remove obvious duplicates among the basic maps of "map".
9162 * Unlike isl_map_normalize, this function does not remove redundant
9163 * constraints and only removes duplicates that have exactly the same
9164 * constraints in the input. It does sort the constraints and
9165 * the basic maps to ease the detection of duplicates.
9167 * If "map" has already been normalized or if the basic maps are
9168 * disjoint, then there can be no duplicates.
9170 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9172 int i;
9173 isl_basic_map *bmap;
9175 if (!map)
9176 return NULL;
9177 if (map->n <= 1)
9178 return map;
9179 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9180 return map;
9181 for (i = 0; i < map->n; ++i) {
9182 bmap = isl_basic_map_copy(map->p[i]);
9183 bmap = isl_basic_map_sort_constraints(bmap);
9184 if (!bmap)
9185 return isl_map_free(map);
9186 isl_basic_map_free(map->p[i]);
9187 map->p[i] = bmap;
9190 map = sort_and_remove_duplicates(map);
9191 return map;
9194 /* We normalize in place, but if anything goes wrong we need
9195 * to return NULL, so we need to make sure we don't change the
9196 * meaning of any possible other copies of map.
9198 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9200 int i;
9201 struct isl_basic_map *bmap;
9203 if (!map)
9204 return NULL;
9205 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9206 return map;
9207 for (i = 0; i < map->n; ++i) {
9208 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9209 if (!bmap)
9210 goto error;
9211 isl_basic_map_free(map->p[i]);
9212 map->p[i] = bmap;
9215 map = sort_and_remove_duplicates(map);
9216 if (map)
9217 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9218 return map;
9219 error:
9220 isl_map_free(map);
9221 return NULL;
9224 struct isl_set *isl_set_normalize(struct isl_set *set)
9226 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9229 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9231 int i;
9232 int equal;
9234 if (!map1 || !map2)
9235 return -1;
9237 if (map1 == map2)
9238 return 1;
9239 if (!isl_space_is_equal(map1->dim, map2->dim))
9240 return 0;
9242 map1 = isl_map_copy(map1);
9243 map2 = isl_map_copy(map2);
9244 map1 = isl_map_normalize(map1);
9245 map2 = isl_map_normalize(map2);
9246 if (!map1 || !map2)
9247 goto error;
9248 equal = map1->n == map2->n;
9249 for (i = 0; equal && i < map1->n; ++i) {
9250 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9251 if (equal < 0)
9252 goto error;
9254 isl_map_free(map1);
9255 isl_map_free(map2);
9256 return equal;
9257 error:
9258 isl_map_free(map1);
9259 isl_map_free(map2);
9260 return -1;
9263 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9265 return isl_map_plain_is_equal(map1, map2);
9268 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9270 return isl_map_plain_is_equal((struct isl_map *)set1,
9271 (struct isl_map *)set2);
9274 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9276 return isl_set_plain_is_equal(set1, set2);
9279 /* Return an interval that ranges from min to max (inclusive)
9281 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9282 isl_int min, isl_int max)
9284 int k;
9285 struct isl_basic_set *bset = NULL;
9287 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9288 if (!bset)
9289 goto error;
9291 k = isl_basic_set_alloc_inequality(bset);
9292 if (k < 0)
9293 goto error;
9294 isl_int_set_si(bset->ineq[k][1], 1);
9295 isl_int_neg(bset->ineq[k][0], min);
9297 k = isl_basic_set_alloc_inequality(bset);
9298 if (k < 0)
9299 goto error;
9300 isl_int_set_si(bset->ineq[k][1], -1);
9301 isl_int_set(bset->ineq[k][0], max);
9303 return bset;
9304 error:
9305 isl_basic_set_free(bset);
9306 return NULL;
9309 /* Return the basic sets in "set" as a list.
9311 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
9312 __isl_keep isl_set *set)
9314 int i;
9315 isl_ctx *ctx;
9316 isl_basic_set_list *list;
9318 if (!set)
9319 return NULL;
9320 ctx = isl_set_get_ctx(set);
9321 list = isl_basic_set_list_alloc(ctx, set->n);
9323 for (i = 0; i < set->n; ++i) {
9324 isl_basic_set *bset;
9326 bset = isl_basic_set_copy(set->p[i]);
9327 list = isl_basic_set_list_add(list, bset);
9330 return list;
9333 /* Return the intersection of the elements in the non-empty list "list".
9334 * All elements are assumed to live in the same space.
9336 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9337 __isl_take struct isl_basic_set_list *list)
9339 int i, n;
9340 isl_basic_set *bset;
9342 if (!list)
9343 return NULL;
9344 n = isl_basic_set_list_n_basic_set(list);
9345 if (n < 1)
9346 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9347 "expecting non-empty list", goto error);
9349 bset = isl_basic_set_list_get_basic_set(list, 0);
9350 for (i = 1; i < n; ++i) {
9351 isl_basic_set *bset_i;
9353 bset_i = isl_basic_set_list_get_basic_set(list, i);
9354 bset = isl_basic_set_intersect(bset, bset_i);
9357 isl_basic_set_list_free(list);
9358 return bset;
9359 error:
9360 isl_basic_set_list_free(list);
9361 return NULL;
9364 /* Return the Cartesian product of the basic sets in list (in the given order).
9366 __isl_give isl_basic_set *isl_basic_set_list_product(
9367 __isl_take struct isl_basic_set_list *list)
9369 int i;
9370 unsigned dim;
9371 unsigned nparam;
9372 unsigned extra;
9373 unsigned n_eq;
9374 unsigned n_ineq;
9375 struct isl_basic_set *product = NULL;
9377 if (!list)
9378 goto error;
9379 isl_assert(list->ctx, list->n > 0, goto error);
9380 isl_assert(list->ctx, list->p[0], goto error);
9381 nparam = isl_basic_set_n_param(list->p[0]);
9382 dim = isl_basic_set_n_dim(list->p[0]);
9383 extra = list->p[0]->n_div;
9384 n_eq = list->p[0]->n_eq;
9385 n_ineq = list->p[0]->n_ineq;
9386 for (i = 1; i < list->n; ++i) {
9387 isl_assert(list->ctx, list->p[i], goto error);
9388 isl_assert(list->ctx,
9389 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9390 dim += isl_basic_set_n_dim(list->p[i]);
9391 extra += list->p[i]->n_div;
9392 n_eq += list->p[i]->n_eq;
9393 n_ineq += list->p[i]->n_ineq;
9395 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9396 n_eq, n_ineq);
9397 if (!product)
9398 goto error;
9399 dim = 0;
9400 for (i = 0; i < list->n; ++i) {
9401 isl_basic_set_add_constraints(product,
9402 isl_basic_set_copy(list->p[i]), dim);
9403 dim += isl_basic_set_n_dim(list->p[i]);
9405 isl_basic_set_list_free(list);
9406 return product;
9407 error:
9408 isl_basic_set_free(product);
9409 isl_basic_set_list_free(list);
9410 return NULL;
9413 struct isl_basic_map *isl_basic_map_product(
9414 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9416 isl_space *dim_result = NULL;
9417 struct isl_basic_map *bmap;
9418 unsigned in1, in2, out1, out2, nparam, total, pos;
9419 struct isl_dim_map *dim_map1, *dim_map2;
9421 if (!bmap1 || !bmap2)
9422 goto error;
9424 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9425 bmap2->dim, isl_dim_param), goto error);
9426 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9427 isl_space_copy(bmap2->dim));
9429 in1 = isl_basic_map_n_in(bmap1);
9430 in2 = isl_basic_map_n_in(bmap2);
9431 out1 = isl_basic_map_n_out(bmap1);
9432 out2 = isl_basic_map_n_out(bmap2);
9433 nparam = isl_basic_map_n_param(bmap1);
9435 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9436 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9437 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9438 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9439 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9440 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9441 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9442 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9443 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9444 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9445 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9447 bmap = isl_basic_map_alloc_space(dim_result,
9448 bmap1->n_div + bmap2->n_div,
9449 bmap1->n_eq + bmap2->n_eq,
9450 bmap1->n_ineq + bmap2->n_ineq);
9451 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9452 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9453 bmap = isl_basic_map_simplify(bmap);
9454 return isl_basic_map_finalize(bmap);
9455 error:
9456 isl_basic_map_free(bmap1);
9457 isl_basic_map_free(bmap2);
9458 return NULL;
9461 __isl_give isl_basic_map *isl_basic_map_flat_product(
9462 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9464 isl_basic_map *prod;
9466 prod = isl_basic_map_product(bmap1, bmap2);
9467 prod = isl_basic_map_flatten(prod);
9468 return prod;
9471 __isl_give isl_basic_set *isl_basic_set_flat_product(
9472 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9474 return isl_basic_map_flat_range_product(bset1, bset2);
9477 __isl_give isl_basic_map *isl_basic_map_domain_product(
9478 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9480 isl_space *space_result = NULL;
9481 isl_basic_map *bmap;
9482 unsigned in1, in2, out, nparam, total, pos;
9483 struct isl_dim_map *dim_map1, *dim_map2;
9485 if (!bmap1 || !bmap2)
9486 goto error;
9488 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9489 isl_space_copy(bmap2->dim));
9491 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9492 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9493 out = isl_basic_map_dim(bmap1, isl_dim_out);
9494 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9496 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9497 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9498 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9499 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9500 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9501 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9502 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9503 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9504 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9505 isl_dim_map_div(dim_map1, bmap1, pos += out);
9506 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9508 bmap = isl_basic_map_alloc_space(space_result,
9509 bmap1->n_div + bmap2->n_div,
9510 bmap1->n_eq + bmap2->n_eq,
9511 bmap1->n_ineq + bmap2->n_ineq);
9512 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9513 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9514 bmap = isl_basic_map_simplify(bmap);
9515 return isl_basic_map_finalize(bmap);
9516 error:
9517 isl_basic_map_free(bmap1);
9518 isl_basic_map_free(bmap2);
9519 return NULL;
9522 __isl_give isl_basic_map *isl_basic_map_range_product(
9523 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9525 isl_space *dim_result = NULL;
9526 isl_basic_map *bmap;
9527 unsigned in, out1, out2, nparam, total, pos;
9528 struct isl_dim_map *dim_map1, *dim_map2;
9530 if (!bmap1 || !bmap2)
9531 goto error;
9533 if (!isl_space_match(bmap1->dim, isl_dim_param,
9534 bmap2->dim, isl_dim_param))
9535 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9536 "parameters don't match", goto error);
9538 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9539 isl_space_copy(bmap2->dim));
9541 in = isl_basic_map_dim(bmap1, isl_dim_in);
9542 out1 = isl_basic_map_n_out(bmap1);
9543 out2 = isl_basic_map_n_out(bmap2);
9544 nparam = isl_basic_map_n_param(bmap1);
9546 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9547 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9548 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9549 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9550 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9551 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9552 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9553 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9554 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9555 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9556 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9558 bmap = isl_basic_map_alloc_space(dim_result,
9559 bmap1->n_div + bmap2->n_div,
9560 bmap1->n_eq + bmap2->n_eq,
9561 bmap1->n_ineq + bmap2->n_ineq);
9562 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9563 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9564 bmap = isl_basic_map_simplify(bmap);
9565 return isl_basic_map_finalize(bmap);
9566 error:
9567 isl_basic_map_free(bmap1);
9568 isl_basic_map_free(bmap2);
9569 return NULL;
9572 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9573 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9575 isl_basic_map *prod;
9577 prod = isl_basic_map_range_product(bmap1, bmap2);
9578 prod = isl_basic_map_flatten_range(prod);
9579 return prod;
9582 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9583 * and collect the results.
9584 * The result live in the space obtained by calling "space_product"
9585 * on the spaces of "map1" and "map2".
9586 * If "remove_duplicates" is set then the result may contain duplicates
9587 * (even if the inputs do not) and so we try and remove the obvious
9588 * duplicates.
9590 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9591 __isl_take isl_map *map2,
9592 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9593 __isl_take isl_space *right),
9594 __isl_give isl_basic_map *(*basic_map_product)(
9595 __isl_take isl_basic_map *left,
9596 __isl_take isl_basic_map *right),
9597 int remove_duplicates)
9599 unsigned flags = 0;
9600 struct isl_map *result;
9601 int i, j;
9603 if (!map1 || !map2)
9604 goto error;
9606 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9607 map2->dim, isl_dim_param), goto error);
9609 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9610 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9611 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9613 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9614 isl_space_copy(map2->dim)),
9615 map1->n * map2->n, flags);
9616 if (!result)
9617 goto error;
9618 for (i = 0; i < map1->n; ++i)
9619 for (j = 0; j < map2->n; ++j) {
9620 struct isl_basic_map *part;
9621 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9622 isl_basic_map_copy(map2->p[j]));
9623 if (isl_basic_map_is_empty(part))
9624 isl_basic_map_free(part);
9625 else
9626 result = isl_map_add_basic_map(result, part);
9627 if (!result)
9628 goto error;
9630 if (remove_duplicates)
9631 result = isl_map_remove_obvious_duplicates(result);
9632 isl_map_free(map1);
9633 isl_map_free(map2);
9634 return result;
9635 error:
9636 isl_map_free(map1);
9637 isl_map_free(map2);
9638 return NULL;
9641 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9643 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9644 __isl_take isl_map *map2)
9646 return map_product(map1, map2, &isl_space_product,
9647 &isl_basic_map_product, 0);
9650 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9651 __isl_take isl_map *map2)
9653 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9656 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9658 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9659 __isl_take isl_map *map2)
9661 isl_map *prod;
9663 prod = isl_map_product(map1, map2);
9664 prod = isl_map_flatten(prod);
9665 return prod;
9668 /* Given two set A and B, construct its Cartesian product A x B.
9670 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9672 return isl_map_range_product(set1, set2);
9675 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9676 __isl_take isl_set *set2)
9678 return isl_map_flat_range_product(set1, set2);
9681 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9683 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9684 __isl_take isl_map *map2)
9686 return map_product(map1, map2, &isl_space_domain_product,
9687 &isl_basic_map_domain_product, 1);
9690 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9692 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9693 __isl_take isl_map *map2)
9695 return map_product(map1, map2, &isl_space_range_product,
9696 &isl_basic_map_range_product, 1);
9699 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9700 __isl_take isl_map *map2)
9702 return isl_map_align_params_map_map_and(map1, map2,
9703 &map_domain_product_aligned);
9706 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9707 __isl_take isl_map *map2)
9709 return isl_map_align_params_map_map_and(map1, map2,
9710 &map_range_product_aligned);
9713 /* Given a map A -> [B -> C], extract the map A -> B.
9715 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9717 isl_space *space;
9718 int total, keep;
9720 if (!map)
9721 return NULL;
9722 if (!isl_space_range_is_wrapping(map->dim))
9723 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9724 "range is not a product", return isl_map_free(map));
9726 space = isl_map_get_space(map);
9727 total = isl_space_dim(space, isl_dim_out);
9728 space = isl_space_range_factor_domain(space);
9729 keep = isl_space_dim(space, isl_dim_out);
9730 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9731 map = isl_map_reset_space(map, space);
9733 return map;
9736 /* Given a map A -> [B -> C], extract the map A -> C.
9738 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9740 isl_space *space;
9741 int total, keep;
9743 if (!map)
9744 return NULL;
9745 if (!isl_space_range_is_wrapping(map->dim))
9746 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9747 "range is not a product", return isl_map_free(map));
9749 space = isl_map_get_space(map);
9750 total = isl_space_dim(space, isl_dim_out);
9751 space = isl_space_range_factor_range(space);
9752 keep = isl_space_dim(space, isl_dim_out);
9753 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9754 map = isl_map_reset_space(map, space);
9756 return map;
9759 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9761 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9762 __isl_take isl_map *map2)
9764 isl_map *prod;
9766 prod = isl_map_domain_product(map1, map2);
9767 prod = isl_map_flatten_domain(prod);
9768 return prod;
9771 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9773 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9774 __isl_take isl_map *map2)
9776 isl_map *prod;
9778 prod = isl_map_range_product(map1, map2);
9779 prod = isl_map_flatten_range(prod);
9780 return prod;
9783 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9785 int i;
9786 uint32_t hash = isl_hash_init();
9787 unsigned total;
9789 if (!bmap)
9790 return 0;
9791 bmap = isl_basic_map_copy(bmap);
9792 bmap = isl_basic_map_normalize(bmap);
9793 if (!bmap)
9794 return 0;
9795 total = isl_basic_map_total_dim(bmap);
9796 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9797 for (i = 0; i < bmap->n_eq; ++i) {
9798 uint32_t c_hash;
9799 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9800 isl_hash_hash(hash, c_hash);
9802 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9803 for (i = 0; i < bmap->n_ineq; ++i) {
9804 uint32_t c_hash;
9805 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9806 isl_hash_hash(hash, c_hash);
9808 isl_hash_byte(hash, bmap->n_div & 0xFF);
9809 for (i = 0; i < bmap->n_div; ++i) {
9810 uint32_t c_hash;
9811 if (isl_int_is_zero(bmap->div[i][0]))
9812 continue;
9813 isl_hash_byte(hash, i & 0xFF);
9814 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9815 isl_hash_hash(hash, c_hash);
9817 isl_basic_map_free(bmap);
9818 return hash;
9821 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9823 return isl_basic_map_get_hash((isl_basic_map *)bset);
9826 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9828 int i;
9829 uint32_t hash;
9831 if (!map)
9832 return 0;
9833 map = isl_map_copy(map);
9834 map = isl_map_normalize(map);
9835 if (!map)
9836 return 0;
9838 hash = isl_hash_init();
9839 for (i = 0; i < map->n; ++i) {
9840 uint32_t bmap_hash;
9841 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9842 isl_hash_hash(hash, bmap_hash);
9845 isl_map_free(map);
9847 return hash;
9850 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9852 return isl_map_get_hash((isl_map *)set);
9855 /* Check if the value for dimension dim is completely determined
9856 * by the values of the other parameters and variables.
9857 * That is, check if dimension dim is involved in an equality.
9859 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9861 int i;
9862 unsigned nparam;
9864 if (!bset)
9865 return -1;
9866 nparam = isl_basic_set_n_param(bset);
9867 for (i = 0; i < bset->n_eq; ++i)
9868 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9869 return 1;
9870 return 0;
9873 /* Check if the value for dimension dim is completely determined
9874 * by the values of the other parameters and variables.
9875 * That is, check if dimension dim is involved in an equality
9876 * for each of the subsets.
9878 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9880 int i;
9882 if (!set)
9883 return -1;
9884 for (i = 0; i < set->n; ++i) {
9885 int unique;
9886 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9887 if (unique != 1)
9888 return unique;
9890 return 1;
9893 /* Return the number of basic maps in the (current) representation of "map".
9895 int isl_map_n_basic_map(__isl_keep isl_map *map)
9897 return map ? map->n : 0;
9900 int isl_set_n_basic_set(__isl_keep isl_set *set)
9902 return set ? set->n : 0;
9905 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9906 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9908 int i;
9910 if (!map)
9911 return -1;
9913 for (i = 0; i < map->n; ++i)
9914 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9915 return -1;
9917 return 0;
9920 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9921 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9923 int i;
9925 if (!set)
9926 return -1;
9928 for (i = 0; i < set->n; ++i)
9929 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9930 return -1;
9932 return 0;
9935 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9937 isl_space *dim;
9939 if (!bset)
9940 return NULL;
9942 bset = isl_basic_set_cow(bset);
9943 if (!bset)
9944 return NULL;
9946 dim = isl_basic_set_get_space(bset);
9947 dim = isl_space_lift(dim, bset->n_div);
9948 if (!dim)
9949 goto error;
9950 isl_space_free(bset->dim);
9951 bset->dim = dim;
9952 bset->extra -= bset->n_div;
9953 bset->n_div = 0;
9955 bset = isl_basic_set_finalize(bset);
9957 return bset;
9958 error:
9959 isl_basic_set_free(bset);
9960 return NULL;
9963 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9965 int i;
9966 isl_space *dim;
9967 unsigned n_div;
9969 set = isl_set_align_divs(set);
9971 if (!set)
9972 return NULL;
9974 set = isl_set_cow(set);
9975 if (!set)
9976 return NULL;
9978 n_div = set->p[0]->n_div;
9979 dim = isl_set_get_space(set);
9980 dim = isl_space_lift(dim, n_div);
9981 if (!dim)
9982 goto error;
9983 isl_space_free(set->dim);
9984 set->dim = dim;
9986 for (i = 0; i < set->n; ++i) {
9987 set->p[i] = isl_basic_set_lift(set->p[i]);
9988 if (!set->p[i])
9989 goto error;
9992 return set;
9993 error:
9994 isl_set_free(set);
9995 return NULL;
9998 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10000 isl_space *dim;
10001 struct isl_basic_map *bmap;
10002 unsigned n_set;
10003 unsigned n_div;
10004 unsigned n_param;
10005 unsigned total;
10006 int i, k, l;
10008 set = isl_set_align_divs(set);
10010 if (!set)
10011 return NULL;
10013 dim = isl_set_get_space(set);
10014 if (set->n == 0 || set->p[0]->n_div == 0) {
10015 isl_set_free(set);
10016 return isl_map_identity(isl_space_map_from_set(dim));
10019 n_div = set->p[0]->n_div;
10020 dim = isl_space_map_from_set(dim);
10021 n_param = isl_space_dim(dim, isl_dim_param);
10022 n_set = isl_space_dim(dim, isl_dim_in);
10023 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10024 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10025 for (i = 0; i < n_set; ++i)
10026 bmap = var_equal(bmap, i);
10028 total = n_param + n_set + n_set + n_div;
10029 for (i = 0; i < n_div; ++i) {
10030 k = isl_basic_map_alloc_inequality(bmap);
10031 if (k < 0)
10032 goto error;
10033 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10034 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10035 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10036 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10037 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10038 set->p[0]->div[i][0]);
10040 l = isl_basic_map_alloc_inequality(bmap);
10041 if (l < 0)
10042 goto error;
10043 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10044 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10045 set->p[0]->div[i][0]);
10046 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10049 isl_set_free(set);
10050 bmap = isl_basic_map_simplify(bmap);
10051 bmap = isl_basic_map_finalize(bmap);
10052 return isl_map_from_basic_map(bmap);
10053 error:
10054 isl_set_free(set);
10055 isl_basic_map_free(bmap);
10056 return NULL;
10059 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10061 unsigned dim;
10062 int size = 0;
10064 if (!bset)
10065 return -1;
10067 dim = isl_basic_set_total_dim(bset);
10068 size += bset->n_eq * (1 + dim);
10069 size += bset->n_ineq * (1 + dim);
10070 size += bset->n_div * (2 + dim);
10072 return size;
10075 int isl_set_size(__isl_keep isl_set *set)
10077 int i;
10078 int size = 0;
10080 if (!set)
10081 return -1;
10083 for (i = 0; i < set->n; ++i)
10084 size += isl_basic_set_size(set->p[i]);
10086 return size;
10089 /* Check if there is any lower bound (if lower == 0) and/or upper
10090 * bound (if upper == 0) on the specified dim.
10092 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10093 enum isl_dim_type type, unsigned pos, int lower, int upper)
10095 int i;
10097 if (!bmap)
10098 return -1;
10100 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
10102 pos += isl_basic_map_offset(bmap, type);
10104 for (i = 0; i < bmap->n_div; ++i) {
10105 if (isl_int_is_zero(bmap->div[i][0]))
10106 continue;
10107 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10108 return 1;
10111 for (i = 0; i < bmap->n_eq; ++i)
10112 if (!isl_int_is_zero(bmap->eq[i][pos]))
10113 return 1;
10115 for (i = 0; i < bmap->n_ineq; ++i) {
10116 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10117 if (sgn > 0)
10118 lower = 1;
10119 if (sgn < 0)
10120 upper = 1;
10123 return lower && upper;
10126 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10127 enum isl_dim_type type, unsigned pos)
10129 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10132 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10133 enum isl_dim_type type, unsigned pos)
10135 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10138 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10139 enum isl_dim_type type, unsigned pos)
10141 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10144 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10145 enum isl_dim_type type, unsigned pos)
10147 int i;
10149 if (!map)
10150 return -1;
10152 for (i = 0; i < map->n; ++i) {
10153 int bounded;
10154 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10155 if (bounded < 0 || !bounded)
10156 return bounded;
10159 return 1;
10162 /* Return 1 if the specified dim is involved in both an upper bound
10163 * and a lower bound.
10165 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10166 enum isl_dim_type type, unsigned pos)
10168 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10171 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10173 static int has_any_bound(__isl_keep isl_map *map,
10174 enum isl_dim_type type, unsigned pos,
10175 int (*fn)(__isl_keep isl_basic_map *bmap,
10176 enum isl_dim_type type, unsigned pos))
10178 int i;
10180 if (!map)
10181 return -1;
10183 for (i = 0; i < map->n; ++i) {
10184 int bounded;
10185 bounded = fn(map->p[i], type, pos);
10186 if (bounded < 0 || bounded)
10187 return bounded;
10190 return 0;
10193 /* Return 1 if the specified dim is involved in any lower bound.
10195 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10196 enum isl_dim_type type, unsigned pos)
10198 return has_any_bound(set, type, pos,
10199 &isl_basic_map_dim_has_lower_bound);
10202 /* Return 1 if the specified dim is involved in any upper bound.
10204 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10205 enum isl_dim_type type, unsigned pos)
10207 return has_any_bound(set, type, pos,
10208 &isl_basic_map_dim_has_upper_bound);
10211 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10213 static int has_bound(__isl_keep isl_map *map,
10214 enum isl_dim_type type, unsigned pos,
10215 int (*fn)(__isl_keep isl_basic_map *bmap,
10216 enum isl_dim_type type, unsigned pos))
10218 int i;
10220 if (!map)
10221 return -1;
10223 for (i = 0; i < map->n; ++i) {
10224 int bounded;
10225 bounded = fn(map->p[i], type, pos);
10226 if (bounded < 0 || !bounded)
10227 return bounded;
10230 return 1;
10233 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10235 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10236 enum isl_dim_type type, unsigned pos)
10238 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10241 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10243 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10244 enum isl_dim_type type, unsigned pos)
10246 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10249 /* For each of the "n" variables starting at "first", determine
10250 * the sign of the variable and put the results in the first "n"
10251 * elements of the array "signs".
10252 * Sign
10253 * 1 means that the variable is non-negative
10254 * -1 means that the variable is non-positive
10255 * 0 means the variable attains both positive and negative values.
10257 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10258 unsigned first, unsigned n, int *signs)
10260 isl_vec *bound = NULL;
10261 struct isl_tab *tab = NULL;
10262 struct isl_tab_undo *snap;
10263 int i;
10265 if (!bset || !signs)
10266 return -1;
10268 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10269 tab = isl_tab_from_basic_set(bset, 0);
10270 if (!bound || !tab)
10271 goto error;
10273 isl_seq_clr(bound->el, bound->size);
10274 isl_int_set_si(bound->el[0], -1);
10276 snap = isl_tab_snap(tab);
10277 for (i = 0; i < n; ++i) {
10278 int empty;
10280 isl_int_set_si(bound->el[1 + first + i], -1);
10281 if (isl_tab_add_ineq(tab, bound->el) < 0)
10282 goto error;
10283 empty = tab->empty;
10284 isl_int_set_si(bound->el[1 + first + i], 0);
10285 if (isl_tab_rollback(tab, snap) < 0)
10286 goto error;
10288 if (empty) {
10289 signs[i] = 1;
10290 continue;
10293 isl_int_set_si(bound->el[1 + first + i], 1);
10294 if (isl_tab_add_ineq(tab, bound->el) < 0)
10295 goto error;
10296 empty = tab->empty;
10297 isl_int_set_si(bound->el[1 + first + i], 0);
10298 if (isl_tab_rollback(tab, snap) < 0)
10299 goto error;
10301 signs[i] = empty ? -1 : 0;
10304 isl_tab_free(tab);
10305 isl_vec_free(bound);
10306 return 0;
10307 error:
10308 isl_tab_free(tab);
10309 isl_vec_free(bound);
10310 return -1;
10313 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10314 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10316 if (!bset || !signs)
10317 return -1;
10318 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10319 return -1);
10321 first += pos(bset->dim, type) - 1;
10322 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10325 /* Is it possible for the integer division "div" to depend (possibly
10326 * indirectly) on any output dimensions?
10328 * If the div is undefined, then we conservatively assume that it
10329 * may depend on them.
10330 * Otherwise, we check if it actually depends on them or on any integer
10331 * divisions that may depend on them.
10333 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10335 int i;
10336 unsigned n_out, o_out;
10337 unsigned n_div, o_div;
10339 if (isl_int_is_zero(bmap->div[div][0]))
10340 return 1;
10342 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10343 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10345 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10346 return 1;
10348 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10349 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10351 for (i = 0; i < n_div; ++i) {
10352 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10353 continue;
10354 if (div_may_involve_output(bmap, i))
10355 return 1;
10358 return 0;
10361 /* Return the index of the equality of "bmap" that defines
10362 * the output dimension "pos" in terms of earlier dimensions.
10363 * The equality may also involve integer divisions, as long
10364 * as those integer divisions are defined in terms of
10365 * parameters or input dimensions.
10366 * Return bmap->n_eq if there is no such equality.
10367 * Return -1 on error.
10369 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10370 int pos)
10372 int j, k;
10373 unsigned n_out, o_out;
10374 unsigned n_div, o_div;
10376 if (!bmap)
10377 return -1;
10379 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10380 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10381 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10382 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10384 for (j = 0; j < bmap->n_eq; ++j) {
10385 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10386 continue;
10387 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10388 n_out - (pos + 1)) != -1)
10389 continue;
10390 for (k = 0; k < n_div; ++k) {
10391 if (isl_int_is_zero(bmap->eq[j][o_div + k]))
10392 continue;
10393 if (div_may_involve_output(bmap, k))
10394 break;
10396 if (k >= n_div)
10397 return j;
10400 return bmap->n_eq;
10403 /* Check if the given basic map is obviously single-valued.
10404 * In particular, for each output dimension, check that there is
10405 * an equality that defines the output dimension in terms of
10406 * earlier dimensions.
10408 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10410 int i;
10411 unsigned n_out;
10413 if (!bmap)
10414 return -1;
10416 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10418 for (i = 0; i < n_out; ++i) {
10419 int eq;
10421 eq = isl_basic_map_output_defining_equality(bmap, i);
10422 if (eq < 0)
10423 return -1;
10424 if (eq >= bmap->n_eq)
10425 return 0;
10428 return 1;
10431 /* Check if the given basic map is single-valued.
10432 * We simply compute
10434 * M \circ M^-1
10436 * and check if the result is a subset of the identity mapping.
10438 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10440 isl_space *space;
10441 isl_basic_map *test;
10442 isl_basic_map *id;
10443 int sv;
10445 sv = isl_basic_map_plain_is_single_valued(bmap);
10446 if (sv < 0 || sv)
10447 return sv;
10449 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10450 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10452 space = isl_basic_map_get_space(bmap);
10453 space = isl_space_map_from_set(isl_space_range(space));
10454 id = isl_basic_map_identity(space);
10456 sv = isl_basic_map_is_subset(test, id);
10458 isl_basic_map_free(test);
10459 isl_basic_map_free(id);
10461 return sv;
10464 /* Check if the given map is obviously single-valued.
10466 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10468 if (!map)
10469 return -1;
10470 if (map->n == 0)
10471 return 1;
10472 if (map->n >= 2)
10473 return 0;
10475 return isl_basic_map_plain_is_single_valued(map->p[0]);
10478 /* Check if the given map is single-valued.
10479 * We simply compute
10481 * M \circ M^-1
10483 * and check if the result is a subset of the identity mapping.
10485 int isl_map_is_single_valued(__isl_keep isl_map *map)
10487 isl_space *dim;
10488 isl_map *test;
10489 isl_map *id;
10490 int sv;
10492 sv = isl_map_plain_is_single_valued(map);
10493 if (sv < 0 || sv)
10494 return sv;
10496 test = isl_map_reverse(isl_map_copy(map));
10497 test = isl_map_apply_range(test, isl_map_copy(map));
10499 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10500 id = isl_map_identity(dim);
10502 sv = isl_map_is_subset(test, id);
10504 isl_map_free(test);
10505 isl_map_free(id);
10507 return sv;
10510 int isl_map_is_injective(__isl_keep isl_map *map)
10512 int in;
10514 map = isl_map_copy(map);
10515 map = isl_map_reverse(map);
10516 in = isl_map_is_single_valued(map);
10517 isl_map_free(map);
10519 return in;
10522 /* Check if the given map is obviously injective.
10524 int isl_map_plain_is_injective(__isl_keep isl_map *map)
10526 int in;
10528 map = isl_map_copy(map);
10529 map = isl_map_reverse(map);
10530 in = isl_map_plain_is_single_valued(map);
10531 isl_map_free(map);
10533 return in;
10536 int isl_map_is_bijective(__isl_keep isl_map *map)
10538 int sv;
10540 sv = isl_map_is_single_valued(map);
10541 if (sv < 0 || !sv)
10542 return sv;
10544 return isl_map_is_injective(map);
10547 int isl_set_is_singleton(__isl_keep isl_set *set)
10549 return isl_map_is_single_valued((isl_map *)set);
10552 int isl_map_is_translation(__isl_keep isl_map *map)
10554 int ok;
10555 isl_set *delta;
10557 delta = isl_map_deltas(isl_map_copy(map));
10558 ok = isl_set_is_singleton(delta);
10559 isl_set_free(delta);
10561 return ok;
10564 static int unique(isl_int *p, unsigned pos, unsigned len)
10566 if (isl_seq_first_non_zero(p, pos) != -1)
10567 return 0;
10568 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10569 return 0;
10570 return 1;
10573 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10575 int i, j;
10576 unsigned nvar;
10577 unsigned ovar;
10579 if (!bset)
10580 return -1;
10582 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10583 return 0;
10585 nvar = isl_basic_set_dim(bset, isl_dim_set);
10586 ovar = isl_space_offset(bset->dim, isl_dim_set);
10587 for (j = 0; j < nvar; ++j) {
10588 int lower = 0, upper = 0;
10589 for (i = 0; i < bset->n_eq; ++i) {
10590 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10591 continue;
10592 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10593 return 0;
10594 break;
10596 if (i < bset->n_eq)
10597 continue;
10598 for (i = 0; i < bset->n_ineq; ++i) {
10599 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10600 continue;
10601 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10602 return 0;
10603 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10604 lower = 1;
10605 else
10606 upper = 1;
10608 if (!lower || !upper)
10609 return 0;
10612 return 1;
10615 int isl_set_is_box(__isl_keep isl_set *set)
10617 if (!set)
10618 return -1;
10619 if (set->n != 1)
10620 return 0;
10622 return isl_basic_set_is_box(set->p[0]);
10625 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10627 if (!bset)
10628 return -1;
10630 return isl_space_is_wrapping(bset->dim);
10633 int isl_set_is_wrapping(__isl_keep isl_set *set)
10635 if (!set)
10636 return -1;
10638 return isl_space_is_wrapping(set->dim);
10641 /* Is the domain of "map" a wrapped relation?
10643 int isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10645 if (!map)
10646 return -1;
10648 return isl_space_domain_is_wrapping(map->dim);
10651 /* Is the range of "map" a wrapped relation?
10653 int isl_map_range_is_wrapping(__isl_keep isl_map *map)
10655 if (!map)
10656 return -1;
10658 return isl_space_range_is_wrapping(map->dim);
10661 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10663 bmap = isl_basic_map_cow(bmap);
10664 if (!bmap)
10665 return NULL;
10667 bmap->dim = isl_space_wrap(bmap->dim);
10668 if (!bmap->dim)
10669 goto error;
10671 bmap = isl_basic_map_finalize(bmap);
10673 return (isl_basic_set *)bmap;
10674 error:
10675 isl_basic_map_free(bmap);
10676 return NULL;
10679 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10681 int i;
10683 map = isl_map_cow(map);
10684 if (!map)
10685 return NULL;
10687 for (i = 0; i < map->n; ++i) {
10688 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10689 if (!map->p[i])
10690 goto error;
10692 map->dim = isl_space_wrap(map->dim);
10693 if (!map->dim)
10694 goto error;
10696 return (isl_set *)map;
10697 error:
10698 isl_map_free(map);
10699 return NULL;
10702 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10704 bset = isl_basic_set_cow(bset);
10705 if (!bset)
10706 return NULL;
10708 bset->dim = isl_space_unwrap(bset->dim);
10709 if (!bset->dim)
10710 goto error;
10712 bset = isl_basic_set_finalize(bset);
10714 return (isl_basic_map *)bset;
10715 error:
10716 isl_basic_set_free(bset);
10717 return NULL;
10720 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10722 int i;
10724 if (!set)
10725 return NULL;
10727 if (!isl_set_is_wrapping(set))
10728 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10729 goto error);
10731 set = isl_set_cow(set);
10732 if (!set)
10733 return NULL;
10735 for (i = 0; i < set->n; ++i) {
10736 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10737 if (!set->p[i])
10738 goto error;
10741 set->dim = isl_space_unwrap(set->dim);
10742 if (!set->dim)
10743 goto error;
10745 return (isl_map *)set;
10746 error:
10747 isl_set_free(set);
10748 return NULL;
10751 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10752 enum isl_dim_type type)
10754 if (!bmap)
10755 return NULL;
10757 if (!isl_space_is_named_or_nested(bmap->dim, type))
10758 return bmap;
10760 bmap = isl_basic_map_cow(bmap);
10761 if (!bmap)
10762 return NULL;
10764 bmap->dim = isl_space_reset(bmap->dim, type);
10765 if (!bmap->dim)
10766 goto error;
10768 bmap = isl_basic_map_finalize(bmap);
10770 return bmap;
10771 error:
10772 isl_basic_map_free(bmap);
10773 return NULL;
10776 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10777 enum isl_dim_type type)
10779 int i;
10781 if (!map)
10782 return NULL;
10784 if (!isl_space_is_named_or_nested(map->dim, type))
10785 return map;
10787 map = isl_map_cow(map);
10788 if (!map)
10789 return NULL;
10791 for (i = 0; i < map->n; ++i) {
10792 map->p[i] = isl_basic_map_reset(map->p[i], type);
10793 if (!map->p[i])
10794 goto error;
10796 map->dim = isl_space_reset(map->dim, type);
10797 if (!map->dim)
10798 goto error;
10800 return map;
10801 error:
10802 isl_map_free(map);
10803 return NULL;
10806 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10808 if (!bmap)
10809 return NULL;
10811 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10812 return bmap;
10814 bmap = isl_basic_map_cow(bmap);
10815 if (!bmap)
10816 return NULL;
10818 bmap->dim = isl_space_flatten(bmap->dim);
10819 if (!bmap->dim)
10820 goto error;
10822 bmap = isl_basic_map_finalize(bmap);
10824 return bmap;
10825 error:
10826 isl_basic_map_free(bmap);
10827 return NULL;
10830 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10832 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10835 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10836 __isl_take isl_basic_map *bmap)
10838 if (!bmap)
10839 return NULL;
10841 if (!bmap->dim->nested[0])
10842 return bmap;
10844 bmap = isl_basic_map_cow(bmap);
10845 if (!bmap)
10846 return NULL;
10848 bmap->dim = isl_space_flatten_domain(bmap->dim);
10849 if (!bmap->dim)
10850 goto error;
10852 bmap = isl_basic_map_finalize(bmap);
10854 return bmap;
10855 error:
10856 isl_basic_map_free(bmap);
10857 return NULL;
10860 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10861 __isl_take isl_basic_map *bmap)
10863 if (!bmap)
10864 return NULL;
10866 if (!bmap->dim->nested[1])
10867 return bmap;
10869 bmap = isl_basic_map_cow(bmap);
10870 if (!bmap)
10871 return NULL;
10873 bmap->dim = isl_space_flatten_range(bmap->dim);
10874 if (!bmap->dim)
10875 goto error;
10877 bmap = isl_basic_map_finalize(bmap);
10879 return bmap;
10880 error:
10881 isl_basic_map_free(bmap);
10882 return NULL;
10885 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10887 int i;
10889 if (!map)
10890 return NULL;
10892 if (!map->dim->nested[0] && !map->dim->nested[1])
10893 return map;
10895 map = isl_map_cow(map);
10896 if (!map)
10897 return NULL;
10899 for (i = 0; i < map->n; ++i) {
10900 map->p[i] = isl_basic_map_flatten(map->p[i]);
10901 if (!map->p[i])
10902 goto error;
10904 map->dim = isl_space_flatten(map->dim);
10905 if (!map->dim)
10906 goto error;
10908 return map;
10909 error:
10910 isl_map_free(map);
10911 return NULL;
10914 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10916 return (isl_set *)isl_map_flatten((isl_map *)set);
10919 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10921 isl_space *dim, *flat_dim;
10922 isl_map *map;
10924 dim = isl_set_get_space(set);
10925 flat_dim = isl_space_flatten(isl_space_copy(dim));
10926 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10927 map = isl_map_intersect_domain(map, set);
10929 return map;
10932 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10934 int i;
10936 if (!map)
10937 return NULL;
10939 if (!map->dim->nested[0])
10940 return map;
10942 map = isl_map_cow(map);
10943 if (!map)
10944 return NULL;
10946 for (i = 0; i < map->n; ++i) {
10947 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10948 if (!map->p[i])
10949 goto error;
10951 map->dim = isl_space_flatten_domain(map->dim);
10952 if (!map->dim)
10953 goto error;
10955 return map;
10956 error:
10957 isl_map_free(map);
10958 return NULL;
10961 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10963 int i;
10965 if (!map)
10966 return NULL;
10968 if (!map->dim->nested[1])
10969 return map;
10971 map = isl_map_cow(map);
10972 if (!map)
10973 return NULL;
10975 for (i = 0; i < map->n; ++i) {
10976 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10977 if (!map->p[i])
10978 goto error;
10980 map->dim = isl_space_flatten_range(map->dim);
10981 if (!map->dim)
10982 goto error;
10984 return map;
10985 error:
10986 isl_map_free(map);
10987 return NULL;
10990 /* Reorder the dimensions of "bmap" according to the given dim_map
10991 * and set the dimension specification to "dim".
10993 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10994 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10996 isl_basic_map *res;
10997 unsigned flags;
10999 bmap = isl_basic_map_cow(bmap);
11000 if (!bmap || !dim || !dim_map)
11001 goto error;
11003 flags = bmap->flags;
11004 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11005 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11006 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11007 res = isl_basic_map_alloc_space(dim,
11008 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11009 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11010 if (res)
11011 res->flags = flags;
11012 res = isl_basic_map_finalize(res);
11013 return res;
11014 error:
11015 free(dim_map);
11016 isl_basic_map_free(bmap);
11017 isl_space_free(dim);
11018 return NULL;
11021 /* Reorder the dimensions of "map" according to given reordering.
11023 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11024 __isl_take isl_reordering *r)
11026 int i;
11027 struct isl_dim_map *dim_map;
11029 map = isl_map_cow(map);
11030 dim_map = isl_dim_map_from_reordering(r);
11031 if (!map || !r || !dim_map)
11032 goto error;
11034 for (i = 0; i < map->n; ++i) {
11035 struct isl_dim_map *dim_map_i;
11037 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11039 map->p[i] = isl_basic_map_realign(map->p[i],
11040 isl_space_copy(r->dim), dim_map_i);
11042 if (!map->p[i])
11043 goto error;
11046 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11048 isl_reordering_free(r);
11049 free(dim_map);
11050 return map;
11051 error:
11052 free(dim_map);
11053 isl_map_free(map);
11054 isl_reordering_free(r);
11055 return NULL;
11058 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11059 __isl_take isl_reordering *r)
11061 return (isl_set *)isl_map_realign((isl_map *)set, r);
11064 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11065 __isl_take isl_space *model)
11067 isl_ctx *ctx;
11069 if (!map || !model)
11070 goto error;
11072 ctx = isl_space_get_ctx(model);
11073 if (!isl_space_has_named_params(model))
11074 isl_die(ctx, isl_error_invalid,
11075 "model has unnamed parameters", goto error);
11076 if (!isl_space_has_named_params(map->dim))
11077 isl_die(ctx, isl_error_invalid,
11078 "relation has unnamed parameters", goto error);
11079 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11080 isl_reordering *exp;
11082 model = isl_space_drop_dims(model, isl_dim_in,
11083 0, isl_space_dim(model, isl_dim_in));
11084 model = isl_space_drop_dims(model, isl_dim_out,
11085 0, isl_space_dim(model, isl_dim_out));
11086 exp = isl_parameter_alignment_reordering(map->dim, model);
11087 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11088 map = isl_map_realign(map, exp);
11091 isl_space_free(model);
11092 return map;
11093 error:
11094 isl_space_free(model);
11095 isl_map_free(map);
11096 return NULL;
11099 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11100 __isl_take isl_space *model)
11102 return isl_map_align_params(set, model);
11105 /* Align the parameters of "bmap" to those of "model", introducing
11106 * additional parameters if needed.
11108 __isl_give isl_basic_map *isl_basic_map_align_params(
11109 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11111 isl_ctx *ctx;
11113 if (!bmap || !model)
11114 goto error;
11116 ctx = isl_space_get_ctx(model);
11117 if (!isl_space_has_named_params(model))
11118 isl_die(ctx, isl_error_invalid,
11119 "model has unnamed parameters", goto error);
11120 if (!isl_space_has_named_params(bmap->dim))
11121 isl_die(ctx, isl_error_invalid,
11122 "relation has unnamed parameters", goto error);
11123 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11124 isl_reordering *exp;
11125 struct isl_dim_map *dim_map;
11127 model = isl_space_drop_dims(model, isl_dim_in,
11128 0, isl_space_dim(model, isl_dim_in));
11129 model = isl_space_drop_dims(model, isl_dim_out,
11130 0, isl_space_dim(model, isl_dim_out));
11131 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11132 exp = isl_reordering_extend_space(exp,
11133 isl_basic_map_get_space(bmap));
11134 dim_map = isl_dim_map_from_reordering(exp);
11135 bmap = isl_basic_map_realign(bmap,
11136 exp ? isl_space_copy(exp->dim) : NULL,
11137 isl_dim_map_extend(dim_map, bmap));
11138 isl_reordering_free(exp);
11139 free(dim_map);
11142 isl_space_free(model);
11143 return bmap;
11144 error:
11145 isl_space_free(model);
11146 isl_basic_map_free(bmap);
11147 return NULL;
11150 /* Align the parameters of "bset" to those of "model", introducing
11151 * additional parameters if needed.
11153 __isl_give isl_basic_set *isl_basic_set_align_params(
11154 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11156 return isl_basic_map_align_params(bset, model);
11159 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11160 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11161 enum isl_dim_type c2, enum isl_dim_type c3,
11162 enum isl_dim_type c4, enum isl_dim_type c5)
11164 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11165 struct isl_mat *mat;
11166 int i, j, k;
11167 int pos;
11169 if (!bmap)
11170 return NULL;
11171 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11172 isl_basic_map_total_dim(bmap) + 1);
11173 if (!mat)
11174 return NULL;
11175 for (i = 0; i < bmap->n_eq; ++i)
11176 for (j = 0, pos = 0; j < 5; ++j) {
11177 int off = isl_basic_map_offset(bmap, c[j]);
11178 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11179 isl_int_set(mat->row[i][pos],
11180 bmap->eq[i][off + k]);
11181 ++pos;
11185 return mat;
11188 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11189 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11190 enum isl_dim_type c2, enum isl_dim_type c3,
11191 enum isl_dim_type c4, enum isl_dim_type c5)
11193 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11194 struct isl_mat *mat;
11195 int i, j, k;
11196 int pos;
11198 if (!bmap)
11199 return NULL;
11200 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11201 isl_basic_map_total_dim(bmap) + 1);
11202 if (!mat)
11203 return NULL;
11204 for (i = 0; i < bmap->n_ineq; ++i)
11205 for (j = 0, pos = 0; j < 5; ++j) {
11206 int off = isl_basic_map_offset(bmap, c[j]);
11207 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11208 isl_int_set(mat->row[i][pos],
11209 bmap->ineq[i][off + k]);
11210 ++pos;
11214 return mat;
11217 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11218 __isl_take isl_space *dim,
11219 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11220 enum isl_dim_type c2, enum isl_dim_type c3,
11221 enum isl_dim_type c4, enum isl_dim_type c5)
11223 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11224 isl_basic_map *bmap;
11225 unsigned total;
11226 unsigned extra;
11227 int i, j, k, l;
11228 int pos;
11230 if (!dim || !eq || !ineq)
11231 goto error;
11233 if (eq->n_col != ineq->n_col)
11234 isl_die(dim->ctx, isl_error_invalid,
11235 "equalities and inequalities matrices should have "
11236 "same number of columns", goto error);
11238 total = 1 + isl_space_dim(dim, isl_dim_all);
11240 if (eq->n_col < total)
11241 isl_die(dim->ctx, isl_error_invalid,
11242 "number of columns too small", goto error);
11244 extra = eq->n_col - total;
11246 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11247 eq->n_row, ineq->n_row);
11248 if (!bmap)
11249 goto error;
11250 for (i = 0; i < extra; ++i) {
11251 k = isl_basic_map_alloc_div(bmap);
11252 if (k < 0)
11253 goto error;
11254 isl_int_set_si(bmap->div[k][0], 0);
11256 for (i = 0; i < eq->n_row; ++i) {
11257 l = isl_basic_map_alloc_equality(bmap);
11258 if (l < 0)
11259 goto error;
11260 for (j = 0, pos = 0; j < 5; ++j) {
11261 int off = isl_basic_map_offset(bmap, c[j]);
11262 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11263 isl_int_set(bmap->eq[l][off + k],
11264 eq->row[i][pos]);
11265 ++pos;
11269 for (i = 0; i < ineq->n_row; ++i) {
11270 l = isl_basic_map_alloc_inequality(bmap);
11271 if (l < 0)
11272 goto error;
11273 for (j = 0, pos = 0; j < 5; ++j) {
11274 int off = isl_basic_map_offset(bmap, c[j]);
11275 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11276 isl_int_set(bmap->ineq[l][off + k],
11277 ineq->row[i][pos]);
11278 ++pos;
11283 isl_space_free(dim);
11284 isl_mat_free(eq);
11285 isl_mat_free(ineq);
11287 bmap = isl_basic_map_simplify(bmap);
11288 return isl_basic_map_finalize(bmap);
11289 error:
11290 isl_space_free(dim);
11291 isl_mat_free(eq);
11292 isl_mat_free(ineq);
11293 return NULL;
11296 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11297 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11298 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11300 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11301 c1, c2, c3, c4, isl_dim_in);
11304 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11305 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11306 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11308 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11309 c1, c2, c3, c4, isl_dim_in);
11312 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11313 __isl_take isl_space *dim,
11314 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11315 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11317 return (isl_basic_set*)
11318 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11319 c1, c2, c3, c4, isl_dim_in);
11322 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11324 if (!bmap)
11325 return -1;
11327 return isl_space_can_zip(bmap->dim);
11330 int isl_map_can_zip(__isl_keep isl_map *map)
11332 if (!map)
11333 return -1;
11335 return isl_space_can_zip(map->dim);
11338 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11339 * (A -> C) -> (B -> D).
11341 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11343 unsigned pos;
11344 unsigned n1;
11345 unsigned n2;
11347 if (!bmap)
11348 return NULL;
11350 if (!isl_basic_map_can_zip(bmap))
11351 isl_die(bmap->ctx, isl_error_invalid,
11352 "basic map cannot be zipped", goto error);
11353 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11354 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11355 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11356 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11357 bmap = isl_basic_map_cow(bmap);
11358 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11359 if (!bmap)
11360 return NULL;
11361 bmap->dim = isl_space_zip(bmap->dim);
11362 if (!bmap->dim)
11363 goto error;
11364 return bmap;
11365 error:
11366 isl_basic_map_free(bmap);
11367 return NULL;
11370 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11371 * (A -> C) -> (B -> D).
11373 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11375 int i;
11377 if (!map)
11378 return NULL;
11380 if (!isl_map_can_zip(map))
11381 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11382 goto error);
11384 map = isl_map_cow(map);
11385 if (!map)
11386 return NULL;
11388 for (i = 0; i < map->n; ++i) {
11389 map->p[i] = isl_basic_map_zip(map->p[i]);
11390 if (!map->p[i])
11391 goto error;
11394 map->dim = isl_space_zip(map->dim);
11395 if (!map->dim)
11396 goto error;
11398 return map;
11399 error:
11400 isl_map_free(map);
11401 return NULL;
11404 /* Can we apply isl_basic_map_curry to "bmap"?
11405 * That is, does it have a nested relation in its domain?
11407 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11409 if (!bmap)
11410 return -1;
11412 return isl_space_can_curry(bmap->dim);
11415 /* Can we apply isl_map_curry to "map"?
11416 * That is, does it have a nested relation in its domain?
11418 int isl_map_can_curry(__isl_keep isl_map *map)
11420 if (!map)
11421 return -1;
11423 return isl_space_can_curry(map->dim);
11426 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11427 * A -> (B -> C).
11429 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11432 if (!bmap)
11433 return NULL;
11435 if (!isl_basic_map_can_curry(bmap))
11436 isl_die(bmap->ctx, isl_error_invalid,
11437 "basic map cannot be curried", goto error);
11438 bmap = isl_basic_map_cow(bmap);
11439 if (!bmap)
11440 return NULL;
11441 bmap->dim = isl_space_curry(bmap->dim);
11442 if (!bmap->dim)
11443 goto error;
11444 return bmap;
11445 error:
11446 isl_basic_map_free(bmap);
11447 return NULL;
11450 /* Given a map (A -> B) -> C, return the corresponding map
11451 * A -> (B -> C).
11453 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11455 int i;
11457 if (!map)
11458 return NULL;
11460 if (!isl_map_can_curry(map))
11461 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11462 goto error);
11464 map = isl_map_cow(map);
11465 if (!map)
11466 return NULL;
11468 for (i = 0; i < map->n; ++i) {
11469 map->p[i] = isl_basic_map_curry(map->p[i]);
11470 if (!map->p[i])
11471 goto error;
11474 map->dim = isl_space_curry(map->dim);
11475 if (!map->dim)
11476 goto error;
11478 return map;
11479 error:
11480 isl_map_free(map);
11481 return NULL;
11484 /* Can we apply isl_basic_map_uncurry to "bmap"?
11485 * That is, does it have a nested relation in its domain?
11487 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11489 if (!bmap)
11490 return -1;
11492 return isl_space_can_uncurry(bmap->dim);
11495 /* Can we apply isl_map_uncurry to "map"?
11496 * That is, does it have a nested relation in its domain?
11498 int isl_map_can_uncurry(__isl_keep isl_map *map)
11500 if (!map)
11501 return -1;
11503 return isl_space_can_uncurry(map->dim);
11506 /* Given a basic map A -> (B -> C), return the corresponding basic map
11507 * (A -> B) -> C.
11509 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11512 if (!bmap)
11513 return NULL;
11515 if (!isl_basic_map_can_uncurry(bmap))
11516 isl_die(bmap->ctx, isl_error_invalid,
11517 "basic map cannot be uncurried",
11518 return isl_basic_map_free(bmap));
11519 bmap = isl_basic_map_cow(bmap);
11520 if (!bmap)
11521 return NULL;
11522 bmap->dim = isl_space_uncurry(bmap->dim);
11523 if (!bmap->dim)
11524 return isl_basic_map_free(bmap);
11525 return bmap;
11528 /* Given a map A -> (B -> C), return the corresponding map
11529 * (A -> B) -> C.
11531 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11533 int i;
11535 if (!map)
11536 return NULL;
11538 if (!isl_map_can_uncurry(map))
11539 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11540 return isl_map_free(map));
11542 map = isl_map_cow(map);
11543 if (!map)
11544 return NULL;
11546 for (i = 0; i < map->n; ++i) {
11547 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11548 if (!map->p[i])
11549 return isl_map_free(map);
11552 map->dim = isl_space_uncurry(map->dim);
11553 if (!map->dim)
11554 return isl_map_free(map);
11556 return map;
11559 /* Construct a basic map mapping the domain of the affine expression
11560 * to a one-dimensional range prescribed by the affine expression.
11562 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11564 int k;
11565 int pos;
11566 isl_local_space *ls;
11567 isl_basic_map *bmap;
11569 if (!aff)
11570 return NULL;
11572 ls = isl_aff_get_local_space(aff);
11573 bmap = isl_basic_map_from_local_space(ls);
11574 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11575 k = isl_basic_map_alloc_equality(bmap);
11576 if (k < 0)
11577 goto error;
11579 pos = isl_basic_map_offset(bmap, isl_dim_out);
11580 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11581 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11582 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11583 aff->v->size - (pos + 1));
11585 isl_aff_free(aff);
11586 bmap = isl_basic_map_finalize(bmap);
11587 return bmap;
11588 error:
11589 isl_aff_free(aff);
11590 isl_basic_map_free(bmap);
11591 return NULL;
11594 /* Construct a map mapping the domain of the affine expression
11595 * to a one-dimensional range prescribed by the affine expression.
11597 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11599 isl_basic_map *bmap;
11601 bmap = isl_basic_map_from_aff(aff);
11602 return isl_map_from_basic_map(bmap);
11605 /* Construct a basic map mapping the domain the multi-affine expression
11606 * to its range, with each dimension in the range equated to the
11607 * corresponding affine expression.
11609 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11610 __isl_take isl_multi_aff *maff)
11612 int i;
11613 isl_space *space;
11614 isl_basic_map *bmap;
11616 if (!maff)
11617 return NULL;
11619 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11620 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11621 "invalid space", goto error);
11623 space = isl_space_domain(isl_multi_aff_get_space(maff));
11624 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11626 for (i = 0; i < maff->n; ++i) {
11627 isl_aff *aff;
11628 isl_basic_map *bmap_i;
11630 aff = isl_aff_copy(maff->p[i]);
11631 bmap_i = isl_basic_map_from_aff(aff);
11633 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11636 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11638 isl_multi_aff_free(maff);
11639 return bmap;
11640 error:
11641 isl_multi_aff_free(maff);
11642 return NULL;
11645 /* Construct a map mapping the domain the multi-affine expression
11646 * to its range, with each dimension in the range equated to the
11647 * corresponding affine expression.
11649 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11651 isl_basic_map *bmap;
11653 bmap = isl_basic_map_from_multi_aff(maff);
11654 return isl_map_from_basic_map(bmap);
11657 /* Construct a basic map mapping a domain in the given space to
11658 * to an n-dimensional range, with n the number of elements in the list,
11659 * where each coordinate in the range is prescribed by the
11660 * corresponding affine expression.
11661 * The domains of all affine expressions in the list are assumed to match
11662 * domain_dim.
11664 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11665 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11667 int i;
11668 isl_space *dim;
11669 isl_basic_map *bmap;
11671 if (!list)
11672 return NULL;
11674 dim = isl_space_from_domain(domain_dim);
11675 bmap = isl_basic_map_universe(dim);
11677 for (i = 0; i < list->n; ++i) {
11678 isl_aff *aff;
11679 isl_basic_map *bmap_i;
11681 aff = isl_aff_copy(list->p[i]);
11682 bmap_i = isl_basic_map_from_aff(aff);
11684 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11687 isl_aff_list_free(list);
11688 return bmap;
11691 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11692 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11694 return isl_map_equate(set, type1, pos1, type2, pos2);
11697 /* Construct a basic map where the given dimensions are equal to each other.
11699 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11700 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11702 isl_basic_map *bmap = NULL;
11703 int i;
11705 if (!space)
11706 return NULL;
11708 if (pos1 >= isl_space_dim(space, type1))
11709 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11710 "index out of bounds", goto error);
11711 if (pos2 >= isl_space_dim(space, type2))
11712 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11713 "index out of bounds", goto error);
11715 if (type1 == type2 && pos1 == pos2)
11716 return isl_basic_map_universe(space);
11718 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11719 i = isl_basic_map_alloc_equality(bmap);
11720 if (i < 0)
11721 goto error;
11722 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11723 pos1 += isl_basic_map_offset(bmap, type1);
11724 pos2 += isl_basic_map_offset(bmap, type2);
11725 isl_int_set_si(bmap->eq[i][pos1], -1);
11726 isl_int_set_si(bmap->eq[i][pos2], 1);
11727 bmap = isl_basic_map_finalize(bmap);
11728 isl_space_free(space);
11729 return bmap;
11730 error:
11731 isl_space_free(space);
11732 isl_basic_map_free(bmap);
11733 return NULL;
11736 /* Add a constraint imposing that the given two dimensions are equal.
11738 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11739 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11741 isl_basic_map *eq;
11743 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11745 bmap = isl_basic_map_intersect(bmap, eq);
11747 return bmap;
11750 /* Add a constraint imposing that the given two dimensions are equal.
11752 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11753 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11755 isl_basic_map *bmap;
11757 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11759 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11761 return map;
11764 /* Add a constraint imposing that the given two dimensions have opposite values.
11766 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11767 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11769 isl_basic_map *bmap = NULL;
11770 int i;
11772 if (!map)
11773 return NULL;
11775 if (pos1 >= isl_map_dim(map, type1))
11776 isl_die(map->ctx, isl_error_invalid,
11777 "index out of bounds", goto error);
11778 if (pos2 >= isl_map_dim(map, type2))
11779 isl_die(map->ctx, isl_error_invalid,
11780 "index out of bounds", goto error);
11782 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11783 i = isl_basic_map_alloc_equality(bmap);
11784 if (i < 0)
11785 goto error;
11786 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11787 pos1 += isl_basic_map_offset(bmap, type1);
11788 pos2 += isl_basic_map_offset(bmap, type2);
11789 isl_int_set_si(bmap->eq[i][pos1], 1);
11790 isl_int_set_si(bmap->eq[i][pos2], 1);
11791 bmap = isl_basic_map_finalize(bmap);
11793 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11795 return map;
11796 error:
11797 isl_basic_map_free(bmap);
11798 isl_map_free(map);
11799 return NULL;
11802 /* Construct a constraint imposing that the value of the first dimension is
11803 * greater than or equal to that of the second.
11805 static __isl_give isl_constraint *constraint_order_ge(
11806 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11807 enum isl_dim_type type2, int pos2)
11809 isl_constraint *c;
11811 if (!space)
11812 return NULL;
11814 c = isl_inequality_alloc(isl_local_space_from_space(space));
11816 if (pos1 >= isl_constraint_dim(c, type1))
11817 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11818 "index out of bounds", return isl_constraint_free(c));
11819 if (pos2 >= isl_constraint_dim(c, type2))
11820 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11821 "index out of bounds", return isl_constraint_free(c));
11823 if (type1 == type2 && pos1 == pos2)
11824 return c;
11826 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11827 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11829 return c;
11832 /* Add a constraint imposing that the value of the first dimension is
11833 * greater than or equal to that of the second.
11835 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11836 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11838 isl_constraint *c;
11839 isl_space *space;
11841 if (type1 == type2 && pos1 == pos2)
11842 return bmap;
11843 space = isl_basic_map_get_space(bmap);
11844 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11845 bmap = isl_basic_map_add_constraint(bmap, c);
11847 return bmap;
11850 /* Add a constraint imposing that the value of the first dimension is
11851 * greater than or equal to that of the second.
11853 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11854 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11856 isl_constraint *c;
11857 isl_space *space;
11859 if (type1 == type2 && pos1 == pos2)
11860 return map;
11861 space = isl_map_get_space(map);
11862 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11863 map = isl_map_add_constraint(map, c);
11865 return map;
11868 /* Add a constraint imposing that the value of the first dimension is
11869 * less than or equal to that of the second.
11871 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11872 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11874 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11877 /* Construct a basic map where the value of the first dimension is
11878 * greater than that of the second.
11880 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11881 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11883 isl_basic_map *bmap = NULL;
11884 int i;
11886 if (!space)
11887 return NULL;
11889 if (pos1 >= isl_space_dim(space, type1))
11890 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11891 "index out of bounds", goto error);
11892 if (pos2 >= isl_space_dim(space, type2))
11893 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11894 "index out of bounds", goto error);
11896 if (type1 == type2 && pos1 == pos2)
11897 return isl_basic_map_empty(space);
11899 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11900 i = isl_basic_map_alloc_inequality(bmap);
11901 if (i < 0)
11902 return isl_basic_map_free(bmap);
11903 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11904 pos1 += isl_basic_map_offset(bmap, type1);
11905 pos2 += isl_basic_map_offset(bmap, type2);
11906 isl_int_set_si(bmap->ineq[i][pos1], 1);
11907 isl_int_set_si(bmap->ineq[i][pos2], -1);
11908 isl_int_set_si(bmap->ineq[i][0], -1);
11909 bmap = isl_basic_map_finalize(bmap);
11911 return bmap;
11912 error:
11913 isl_space_free(space);
11914 isl_basic_map_free(bmap);
11915 return NULL;
11918 /* Add a constraint imposing that the value of the first dimension is
11919 * greater than that of the second.
11921 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11922 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11924 isl_basic_map *gt;
11926 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11928 bmap = isl_basic_map_intersect(bmap, gt);
11930 return bmap;
11933 /* Add a constraint imposing that the value of the first dimension is
11934 * greater than that of the second.
11936 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11937 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11939 isl_basic_map *bmap;
11941 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11943 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11945 return map;
11948 /* Add a constraint imposing that the value of the first dimension is
11949 * smaller than that of the second.
11951 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11952 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11954 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11957 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11958 int pos)
11960 isl_aff *div;
11961 isl_local_space *ls;
11963 if (!bmap)
11964 return NULL;
11966 if (!isl_basic_map_divs_known(bmap))
11967 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11968 "some divs are unknown", return NULL);
11970 ls = isl_basic_map_get_local_space(bmap);
11971 div = isl_local_space_get_div(ls, pos);
11972 isl_local_space_free(ls);
11974 return div;
11977 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11978 int pos)
11980 return isl_basic_map_get_div(bset, pos);
11983 /* Plug in "subs" for dimension "type", "pos" of "bset".
11985 * Let i be the dimension to replace and let "subs" be of the form
11987 * f/d
11989 * Any integer division with a non-zero coefficient for i,
11991 * floor((a i + g)/m)
11993 * is replaced by
11995 * floor((a f + d g)/(m d))
11997 * Constraints of the form
11999 * a i + g
12001 * are replaced by
12003 * a f + d g
12005 * We currently require that "subs" is an integral expression.
12006 * Handling rational expressions may require us to add stride constraints
12007 * as we do in isl_basic_set_preimage_multi_aff.
12009 __isl_give isl_basic_set *isl_basic_set_substitute(
12010 __isl_take isl_basic_set *bset,
12011 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12013 int i;
12014 isl_int v;
12015 isl_ctx *ctx;
12017 if (bset && isl_basic_set_plain_is_empty(bset))
12018 return bset;
12020 bset = isl_basic_set_cow(bset);
12021 if (!bset || !subs)
12022 goto error;
12024 ctx = isl_basic_set_get_ctx(bset);
12025 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12026 isl_die(ctx, isl_error_invalid,
12027 "spaces don't match", goto error);
12028 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12029 isl_die(ctx, isl_error_unsupported,
12030 "cannot handle divs yet", goto error);
12031 if (!isl_int_is_one(subs->v->el[0]))
12032 isl_die(ctx, isl_error_invalid,
12033 "can only substitute integer expressions", goto error);
12035 pos += isl_basic_set_offset(bset, type);
12037 isl_int_init(v);
12039 for (i = 0; i < bset->n_eq; ++i) {
12040 if (isl_int_is_zero(bset->eq[i][pos]))
12041 continue;
12042 isl_int_set(v, bset->eq[i][pos]);
12043 isl_int_set_si(bset->eq[i][pos], 0);
12044 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12045 v, subs->v->el + 1, subs->v->size - 1);
12048 for (i = 0; i < bset->n_ineq; ++i) {
12049 if (isl_int_is_zero(bset->ineq[i][pos]))
12050 continue;
12051 isl_int_set(v, bset->ineq[i][pos]);
12052 isl_int_set_si(bset->ineq[i][pos], 0);
12053 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12054 v, subs->v->el + 1, subs->v->size - 1);
12057 for (i = 0; i < bset->n_div; ++i) {
12058 if (isl_int_is_zero(bset->div[i][1 + pos]))
12059 continue;
12060 isl_int_set(v, bset->div[i][1 + pos]);
12061 isl_int_set_si(bset->div[i][1 + pos], 0);
12062 isl_seq_combine(bset->div[i] + 1,
12063 subs->v->el[0], bset->div[i] + 1,
12064 v, subs->v->el + 1, subs->v->size - 1);
12065 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12068 isl_int_clear(v);
12070 bset = isl_basic_set_simplify(bset);
12071 return isl_basic_set_finalize(bset);
12072 error:
12073 isl_basic_set_free(bset);
12074 return NULL;
12077 /* Plug in "subs" for dimension "type", "pos" of "set".
12079 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12080 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12082 int i;
12084 if (set && isl_set_plain_is_empty(set))
12085 return set;
12087 set = isl_set_cow(set);
12088 if (!set || !subs)
12089 goto error;
12091 for (i = set->n - 1; i >= 0; --i) {
12092 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12093 if (remove_if_empty(set, i) < 0)
12094 goto error;
12097 return set;
12098 error:
12099 isl_set_free(set);
12100 return NULL;
12103 /* Check if the range of "ma" is compatible with the domain or range
12104 * (depending on "type") of "bmap".
12105 * Return -1 if anything is wrong.
12107 static int check_basic_map_compatible_range_multi_aff(
12108 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12109 __isl_keep isl_multi_aff *ma)
12111 int m;
12112 isl_space *ma_space;
12114 ma_space = isl_multi_aff_get_space(ma);
12116 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12117 if (m < 0)
12118 goto error;
12119 if (!m)
12120 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12121 "parameters don't match", goto error);
12122 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12123 if (m < 0)
12124 goto error;
12125 if (!m)
12126 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12127 "spaces don't match", goto error);
12129 isl_space_free(ma_space);
12130 return m;
12131 error:
12132 isl_space_free(ma_space);
12133 return -1;
12136 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12137 * coefficients before the transformed range of dimensions,
12138 * the "n_after" coefficients after the transformed range of dimensions
12139 * and the coefficients of the other divs in "bmap".
12141 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12142 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12144 int i;
12145 int n_param;
12146 int n_set;
12147 isl_local_space *ls;
12149 if (n_div == 0)
12150 return 0;
12152 ls = isl_aff_get_domain_local_space(ma->p[0]);
12153 if (!ls)
12154 return -1;
12156 n_param = isl_local_space_dim(ls, isl_dim_param);
12157 n_set = isl_local_space_dim(ls, isl_dim_set);
12158 for (i = 0; i < n_div; ++i) {
12159 int o_bmap = 0, o_ls = 0;
12161 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12162 o_bmap += 1 + 1 + n_param;
12163 o_ls += 1 + 1 + n_param;
12164 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12165 o_bmap += n_before;
12166 isl_seq_cpy(bmap->div[i] + o_bmap,
12167 ls->div->row[i] + o_ls, n_set);
12168 o_bmap += n_set;
12169 o_ls += n_set;
12170 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12171 o_bmap += n_after;
12172 isl_seq_cpy(bmap->div[i] + o_bmap,
12173 ls->div->row[i] + o_ls, n_div);
12174 o_bmap += n_div;
12175 o_ls += n_div;
12176 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12177 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12178 goto error;
12181 isl_local_space_free(ls);
12182 return 0;
12183 error:
12184 isl_local_space_free(ls);
12185 return -1;
12188 /* How many stride constraints does "ma" enforce?
12189 * That is, how many of the affine expressions have a denominator
12190 * different from one?
12192 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12194 int i;
12195 int strides = 0;
12197 for (i = 0; i < ma->n; ++i)
12198 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12199 strides++;
12201 return strides;
12204 /* For each affine expression in ma of the form
12206 * x_i = (f_i y + h_i)/m_i
12208 * with m_i different from one, add a constraint to "bmap"
12209 * of the form
12211 * f_i y + h_i = m_i alpha_i
12213 * with alpha_i an additional existentially quantified variable.
12215 static __isl_give isl_basic_map *add_ma_strides(
12216 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12217 int n_before, int n_after)
12219 int i, k;
12220 int div;
12221 int total;
12222 int n_param;
12223 int n_in;
12224 int n_div;
12226 total = isl_basic_map_total_dim(bmap);
12227 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12228 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12229 n_div = isl_multi_aff_dim(ma, isl_dim_div);
12230 for (i = 0; i < ma->n; ++i) {
12231 int o_bmap = 0, o_ma = 1;
12233 if (isl_int_is_one(ma->p[i]->v->el[0]))
12234 continue;
12235 div = isl_basic_map_alloc_div(bmap);
12236 k = isl_basic_map_alloc_equality(bmap);
12237 if (div < 0 || k < 0)
12238 goto error;
12239 isl_int_set_si(bmap->div[div][0], 0);
12240 isl_seq_cpy(bmap->eq[k] + o_bmap,
12241 ma->p[i]->v->el + o_ma, 1 + n_param);
12242 o_bmap += 1 + n_param;
12243 o_ma += 1 + n_param;
12244 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12245 o_bmap += n_before;
12246 isl_seq_cpy(bmap->eq[k] + o_bmap,
12247 ma->p[i]->v->el + o_ma, n_in);
12248 o_bmap += n_in;
12249 o_ma += n_in;
12250 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12251 o_bmap += n_after;
12252 isl_seq_cpy(bmap->eq[k] + o_bmap,
12253 ma->p[i]->v->el + o_ma, n_div);
12254 o_bmap += n_div;
12255 o_ma += n_div;
12256 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12257 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12258 total++;
12261 return bmap;
12262 error:
12263 isl_basic_map_free(bmap);
12264 return NULL;
12267 /* Replace the domain or range space (depending on "type) of "space" by "set".
12269 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12270 enum isl_dim_type type, __isl_take isl_space *set)
12272 if (type == isl_dim_in) {
12273 space = isl_space_range(space);
12274 space = isl_space_map_from_domain_and_range(set, space);
12275 } else {
12276 space = isl_space_domain(space);
12277 space = isl_space_map_from_domain_and_range(space, set);
12280 return space;
12283 /* Compute the preimage of the domain or range (depending on "type")
12284 * of "bmap" under the function represented by "ma".
12285 * In other words, plug in "ma" in the domain or range of "bmap".
12286 * The result is a basic map that lives in the same space as "bmap"
12287 * except that the domain or range has been replaced by
12288 * the domain space of "ma".
12290 * If bmap is represented by
12292 * A(p) + S u + B x + T v + C(divs) >= 0,
12294 * where u and x are input and output dimensions if type == isl_dim_out
12295 * while x and v are input and output dimensions if type == isl_dim_in,
12296 * and ma is represented by
12298 * x = D(p) + F(y) + G(divs')
12300 * then the result is
12302 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12304 * The divs in the input set are similarly adjusted.
12305 * In particular
12307 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12309 * becomes
12311 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12312 * B_i G(divs') + c_i(divs))/n_i)
12314 * If bmap is not a rational map and if F(y) involves any denominators
12316 * x_i = (f_i y + h_i)/m_i
12318 * then additional constraints are added to ensure that we only
12319 * map back integer points. That is we enforce
12321 * f_i y + h_i = m_i alpha_i
12323 * with alpha_i an additional existentially quantified variable.
12325 * We first copy over the divs from "ma".
12326 * Then we add the modified constraints and divs from "bmap".
12327 * Finally, we add the stride constraints, if needed.
12329 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12330 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12331 __isl_take isl_multi_aff *ma)
12333 int i, k;
12334 isl_space *space;
12335 isl_basic_map *res = NULL;
12336 int n_before, n_after, n_div_bmap, n_div_ma;
12337 isl_int f, c1, c2, g;
12338 int rational, strides;
12340 isl_int_init(f);
12341 isl_int_init(c1);
12342 isl_int_init(c2);
12343 isl_int_init(g);
12345 ma = isl_multi_aff_align_divs(ma);
12346 if (!bmap || !ma)
12347 goto error;
12348 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12349 goto error;
12351 if (type == isl_dim_in) {
12352 n_before = 0;
12353 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12354 } else {
12355 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12356 n_after = 0;
12358 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12359 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12361 space = isl_multi_aff_get_domain_space(ma);
12362 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12363 rational = isl_basic_map_is_rational(bmap);
12364 strides = rational ? 0 : multi_aff_strides(ma);
12365 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12366 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12367 if (rational)
12368 res = isl_basic_map_set_rational(res);
12370 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12371 if (isl_basic_map_alloc_div(res) < 0)
12372 goto error;
12374 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12375 goto error;
12377 for (i = 0; i < bmap->n_eq; ++i) {
12378 k = isl_basic_map_alloc_equality(res);
12379 if (k < 0)
12380 goto error;
12381 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12382 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12385 for (i = 0; i < bmap->n_ineq; ++i) {
12386 k = isl_basic_map_alloc_inequality(res);
12387 if (k < 0)
12388 goto error;
12389 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12390 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12393 for (i = 0; i < bmap->n_div; ++i) {
12394 if (isl_int_is_zero(bmap->div[i][0])) {
12395 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12396 continue;
12398 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12399 n_before, n_after, n_div_ma, n_div_bmap,
12400 f, c1, c2, g, 1);
12403 if (strides)
12404 res = add_ma_strides(res, ma, n_before, n_after);
12406 isl_int_clear(f);
12407 isl_int_clear(c1);
12408 isl_int_clear(c2);
12409 isl_int_clear(g);
12410 isl_basic_map_free(bmap);
12411 isl_multi_aff_free(ma);
12412 res = isl_basic_set_simplify(res);
12413 return isl_basic_map_finalize(res);
12414 error:
12415 isl_int_clear(f);
12416 isl_int_clear(c1);
12417 isl_int_clear(c2);
12418 isl_int_clear(g);
12419 isl_basic_map_free(bmap);
12420 isl_multi_aff_free(ma);
12421 isl_basic_map_free(res);
12422 return NULL;
12425 /* Compute the preimage of "bset" under the function represented by "ma".
12426 * In other words, plug in "ma" in "bset". The result is a basic set
12427 * that lives in the domain space of "ma".
12429 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12430 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12432 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12435 /* Compute the preimage of the domain of "bmap" under the function
12436 * represented by "ma".
12437 * In other words, plug in "ma" in the domain of "bmap".
12438 * The result is a basic map that lives in the same space as "bmap"
12439 * except that the domain has been replaced by the domain space of "ma".
12441 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12442 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12444 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12447 /* Compute the preimage of the range of "bmap" under the function
12448 * represented by "ma".
12449 * In other words, plug in "ma" in the range of "bmap".
12450 * The result is a basic map that lives in the same space as "bmap"
12451 * except that the range has been replaced by the domain space of "ma".
12453 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12454 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12456 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12459 /* Check if the range of "ma" is compatible with the domain or range
12460 * (depending on "type") of "map".
12461 * Return -1 if anything is wrong.
12463 static int check_map_compatible_range_multi_aff(
12464 __isl_keep isl_map *map, enum isl_dim_type type,
12465 __isl_keep isl_multi_aff *ma)
12467 int m;
12468 isl_space *ma_space;
12470 ma_space = isl_multi_aff_get_space(ma);
12471 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12472 isl_space_free(ma_space);
12473 if (m >= 0 && !m)
12474 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12475 "spaces don't match", return -1);
12476 return m;
12479 /* Compute the preimage of the domain or range (depending on "type")
12480 * of "map" under the function represented by "ma".
12481 * In other words, plug in "ma" in the domain or range of "map".
12482 * The result is a map that lives in the same space as "map"
12483 * except that the domain or range has been replaced by
12484 * the domain space of "ma".
12486 * The parameters are assumed to have been aligned.
12488 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12489 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12491 int i;
12492 isl_space *space;
12494 map = isl_map_cow(map);
12495 ma = isl_multi_aff_align_divs(ma);
12496 if (!map || !ma)
12497 goto error;
12498 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12499 goto error;
12501 for (i = 0; i < map->n; ++i) {
12502 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12503 isl_multi_aff_copy(ma));
12504 if (!map->p[i])
12505 goto error;
12508 space = isl_multi_aff_get_domain_space(ma);
12509 space = isl_space_set(isl_map_get_space(map), type, space);
12511 isl_space_free(map->dim);
12512 map->dim = space;
12513 if (!map->dim)
12514 goto error;
12516 isl_multi_aff_free(ma);
12517 if (map->n > 1)
12518 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12519 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12520 return map;
12521 error:
12522 isl_multi_aff_free(ma);
12523 isl_map_free(map);
12524 return NULL;
12527 /* Compute the preimage of the domain or range (depending on "type")
12528 * of "map" under the function represented by "ma".
12529 * In other words, plug in "ma" in the domain or range of "map".
12530 * The result is a map that lives in the same space as "map"
12531 * except that the domain or range has been replaced by
12532 * the domain space of "ma".
12534 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12535 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12537 if (!map || !ma)
12538 goto error;
12540 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12541 return map_preimage_multi_aff(map, type, ma);
12543 if (!isl_space_has_named_params(map->dim) ||
12544 !isl_space_has_named_params(ma->space))
12545 isl_die(map->ctx, isl_error_invalid,
12546 "unaligned unnamed parameters", goto error);
12547 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12548 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12550 return map_preimage_multi_aff(map, type, ma);
12551 error:
12552 isl_multi_aff_free(ma);
12553 return isl_map_free(map);
12556 /* Compute the preimage of "set" under the function represented by "ma".
12557 * In other words, plug in "ma" in "set". The result is a set
12558 * that lives in the domain space of "ma".
12560 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12561 __isl_take isl_multi_aff *ma)
12563 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12566 /* Compute the preimage of the domain of "map" under the function
12567 * represented by "ma".
12568 * In other words, plug in "ma" in the domain of "map".
12569 * The result is a map that lives in the same space as "map"
12570 * except that the domain has been replaced by the domain space of "ma".
12572 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12573 __isl_take isl_multi_aff *ma)
12575 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12578 /* Compute the preimage of the range of "map" under the function
12579 * represented by "ma".
12580 * In other words, plug in "ma" in the range of "map".
12581 * The result is a map that lives in the same space as "map"
12582 * except that the range has been replaced by the domain space of "ma".
12584 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12585 __isl_take isl_multi_aff *ma)
12587 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12590 /* Compute the preimage of "map" under the function represented by "pma".
12591 * In other words, plug in "pma" in the domain or range of "map".
12592 * The result is a map that lives in the same space as "map",
12593 * except that the space of type "type" has been replaced by
12594 * the domain space of "pma".
12596 * The parameters of "map" and "pma" are assumed to have been aligned.
12598 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12599 __isl_take isl_map *map, enum isl_dim_type type,
12600 __isl_take isl_pw_multi_aff *pma)
12602 int i;
12603 isl_map *res;
12605 if (!pma)
12606 goto error;
12608 if (pma->n == 0) {
12609 isl_pw_multi_aff_free(pma);
12610 res = isl_map_empty(isl_map_get_space(map));
12611 isl_map_free(map);
12612 return res;
12615 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12616 isl_multi_aff_copy(pma->p[0].maff));
12617 if (type == isl_dim_in)
12618 res = isl_map_intersect_domain(res,
12619 isl_map_copy(pma->p[0].set));
12620 else
12621 res = isl_map_intersect_range(res,
12622 isl_map_copy(pma->p[0].set));
12624 for (i = 1; i < pma->n; ++i) {
12625 isl_map *res_i;
12627 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12628 isl_multi_aff_copy(pma->p[i].maff));
12629 if (type == isl_dim_in)
12630 res_i = isl_map_intersect_domain(res_i,
12631 isl_map_copy(pma->p[i].set));
12632 else
12633 res_i = isl_map_intersect_range(res_i,
12634 isl_map_copy(pma->p[i].set));
12635 res = isl_map_union(res, res_i);
12638 isl_pw_multi_aff_free(pma);
12639 isl_map_free(map);
12640 return res;
12641 error:
12642 isl_pw_multi_aff_free(pma);
12643 isl_map_free(map);
12644 return NULL;
12647 /* Compute the preimage of "map" under the function represented by "pma".
12648 * In other words, plug in "pma" in the domain or range of "map".
12649 * The result is a map that lives in the same space as "map",
12650 * except that the space of type "type" has been replaced by
12651 * the domain space of "pma".
12653 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12654 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12656 if (!map || !pma)
12657 goto error;
12659 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12660 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12662 if (!isl_space_has_named_params(map->dim) ||
12663 !isl_space_has_named_params(pma->dim))
12664 isl_die(map->ctx, isl_error_invalid,
12665 "unaligned unnamed parameters", goto error);
12666 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12667 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12669 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12670 error:
12671 isl_pw_multi_aff_free(pma);
12672 return isl_map_free(map);
12675 /* Compute the preimage of "set" under the function represented by "pma".
12676 * In other words, plug in "pma" in "set". The result is a set
12677 * that lives in the domain space of "pma".
12679 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12680 __isl_take isl_pw_multi_aff *pma)
12682 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12685 /* Compute the preimage of the domain of "map" under the function
12686 * represented by "pma".
12687 * In other words, plug in "pma" in the domain of "map".
12688 * The result is a map that lives in the same space as "map",
12689 * except that domain space has been replaced by the domain space of "pma".
12691 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12692 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12694 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12697 /* Compute the preimage of the range of "map" under the function
12698 * represented by "pma".
12699 * In other words, plug in "pma" in the range of "map".
12700 * The result is a map that lives in the same space as "map",
12701 * except that range space has been replaced by the domain space of "pma".
12703 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12704 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12706 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12709 /* Compute the preimage of "map" under the function represented by "mpa".
12710 * In other words, plug in "mpa" in the domain or range of "map".
12711 * The result is a map that lives in the same space as "map",
12712 * except that the space of type "type" has been replaced by
12713 * the domain space of "mpa".
12715 * If the map does not involve any constraints that refer to the
12716 * dimensions of the substituted space, then the only possible
12717 * effect of "mpa" on the map is to map the space to a different space.
12718 * We create a separate isl_multi_aff to effectuate this change
12719 * in order to avoid spurious splitting of the map along the pieces
12720 * of "mpa".
12722 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12723 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12725 int n;
12726 isl_pw_multi_aff *pma;
12728 if (!map || !mpa)
12729 goto error;
12731 n = isl_map_dim(map, type);
12732 if (!isl_map_involves_dims(map, type, 0, n)) {
12733 isl_space *space;
12734 isl_multi_aff *ma;
12736 space = isl_multi_pw_aff_get_space(mpa);
12737 isl_multi_pw_aff_free(mpa);
12738 ma = isl_multi_aff_zero(space);
12739 return isl_map_preimage_multi_aff(map, type, ma);
12742 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12743 return isl_map_preimage_pw_multi_aff(map, type, pma);
12744 error:
12745 isl_map_free(map);
12746 isl_multi_pw_aff_free(mpa);
12747 return NULL;
12750 /* Compute the preimage of "map" under the function represented by "mpa".
12751 * In other words, plug in "mpa" in the domain "map".
12752 * The result is a map that lives in the same space as "map",
12753 * except that domain space has been replaced by the domain space of "mpa".
12755 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12756 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12758 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12761 /* Compute the preimage of "set" by the function represented by "mpa".
12762 * In other words, plug in "mpa" in "set".
12764 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12765 __isl_take isl_multi_pw_aff *mpa)
12767 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);