isl_union_map_is_single_valued: add a couple of extra tests
[isl.git] / isl_map.c
blobccc5f152dce87491adde7fb34a4dfa10f04080b3
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_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
200 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
201 struct isl_basic_set *bset)
203 int m;
204 if (!bmap || !bset)
205 return -1;
206 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
207 if (m < 0 || !m)
208 return m;
209 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
212 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
214 int m;
215 if (!map || !set)
216 return -1;
217 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
218 if (m < 0 || !m)
219 return m;
220 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
223 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
224 struct isl_basic_set *bset)
226 int m;
227 if (!bmap || !bset)
228 return -1;
229 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
230 if (m < 0 || !m)
231 return m;
232 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
235 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
237 return bmap ? bmap->ctx : NULL;
240 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
242 return bset ? bset->ctx : NULL;
245 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
247 return map ? map->ctx : NULL;
250 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
252 return set ? set->ctx : NULL;
255 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
257 if (!bmap)
258 return NULL;
259 return isl_space_copy(bmap->dim);
262 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
264 if (!bset)
265 return NULL;
266 return isl_space_copy(bset->dim);
269 /* Extract the divs in "bmap" as a matrix.
271 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
273 int i;
274 isl_ctx *ctx;
275 isl_mat *div;
276 unsigned total;
277 unsigned cols;
279 if (!bmap)
280 return NULL;
282 ctx = isl_basic_map_get_ctx(bmap);
283 total = isl_space_dim(bmap->dim, isl_dim_all);
284 cols = 1 + 1 + total + bmap->n_div;
285 div = isl_mat_alloc(ctx, bmap->n_div, cols);
286 if (!div)
287 return NULL;
289 for (i = 0; i < bmap->n_div; ++i)
290 isl_seq_cpy(div->row[i], bmap->div[i], cols);
292 return div;
295 /* Extract the divs in "bset" as a matrix.
297 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
299 return isl_basic_map_get_divs(bset);
302 __isl_give isl_local_space *isl_basic_map_get_local_space(
303 __isl_keep isl_basic_map *bmap)
305 isl_mat *div;
307 if (!bmap)
308 return NULL;
310 div = isl_basic_map_get_divs(bmap);
311 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
314 __isl_give isl_local_space *isl_basic_set_get_local_space(
315 __isl_keep isl_basic_set *bset)
317 return isl_basic_map_get_local_space(bset);
320 __isl_give isl_basic_map *isl_basic_map_from_local_space(
321 __isl_take isl_local_space *ls)
323 int i;
324 int n_div;
325 isl_basic_map *bmap;
327 if (!ls)
328 return NULL;
330 n_div = isl_local_space_dim(ls, isl_dim_div);
331 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
332 n_div, 0, 2 * n_div);
334 for (i = 0; i < n_div; ++i)
335 if (isl_basic_map_alloc_div(bmap) < 0)
336 goto error;
338 for (i = 0; i < n_div; ++i) {
339 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
340 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
341 goto error;
344 isl_local_space_free(ls);
345 return bmap;
346 error:
347 isl_local_space_free(ls);
348 isl_basic_map_free(bmap);
349 return NULL;
352 __isl_give isl_basic_set *isl_basic_set_from_local_space(
353 __isl_take isl_local_space *ls)
355 return isl_basic_map_from_local_space(ls);
358 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
360 if (!map)
361 return NULL;
362 return isl_space_copy(map->dim);
365 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
367 if (!set)
368 return NULL;
369 return isl_space_copy(set->dim);
372 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
373 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
375 bmap = isl_basic_map_cow(bmap);
376 if (!bmap)
377 return NULL;
378 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
379 if (!bmap->dim)
380 goto error;
381 bmap = isl_basic_map_finalize(bmap);
382 return bmap;
383 error:
384 isl_basic_map_free(bmap);
385 return NULL;
388 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
389 __isl_take isl_basic_set *bset, const char *s)
391 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
394 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
395 enum isl_dim_type type)
397 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
400 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
401 enum isl_dim_type type, const char *s)
403 int i;
405 map = isl_map_cow(map);
406 if (!map)
407 return NULL;
409 map->dim = isl_space_set_tuple_name(map->dim, type, s);
410 if (!map->dim)
411 goto error;
413 for (i = 0; i < map->n; ++i) {
414 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
415 if (!map->p[i])
416 goto error;
419 return map;
420 error:
421 isl_map_free(map);
422 return NULL;
425 /* Replace the identifier of the tuple of type "type" by "id".
427 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
428 __isl_take isl_basic_map *bmap,
429 enum isl_dim_type type, __isl_take isl_id *id)
431 bmap = isl_basic_map_cow(bmap);
432 if (!bmap)
433 goto error;
434 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
435 if (!bmap->dim)
436 return isl_basic_map_free(bmap);
437 bmap = isl_basic_map_finalize(bmap);
438 return bmap;
439 error:
440 isl_id_free(id);
441 return NULL;
444 /* Replace the identifier of the tuple by "id".
446 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
447 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
449 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
452 /* Does the input or output tuple have a name?
454 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
456 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
459 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
460 enum isl_dim_type type)
462 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
465 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
466 const char *s)
468 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
471 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
472 enum isl_dim_type type, __isl_take isl_id *id)
474 map = isl_map_cow(map);
475 if (!map)
476 goto error;
478 map->dim = isl_space_set_tuple_id(map->dim, type, id);
480 return isl_map_reset_space(map, isl_space_copy(map->dim));
481 error:
482 isl_id_free(id);
483 return NULL;
486 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
487 __isl_take isl_id *id)
489 return isl_map_set_tuple_id(set, isl_dim_set, id);
492 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
493 enum isl_dim_type type)
495 map = isl_map_cow(map);
496 if (!map)
497 return NULL;
499 map->dim = isl_space_reset_tuple_id(map->dim, type);
501 return isl_map_reset_space(map, isl_space_copy(map->dim));
504 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
506 return isl_map_reset_tuple_id(set, isl_dim_set);
509 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
511 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
514 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
515 enum isl_dim_type type)
517 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
520 int isl_set_has_tuple_id(__isl_keep isl_set *set)
522 return isl_map_has_tuple_id(set, isl_dim_set);
525 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
527 return isl_map_get_tuple_id(set, isl_dim_set);
530 /* Does the set tuple have a name?
532 int isl_set_has_tuple_name(__isl_keep isl_set *set)
534 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
538 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
540 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
543 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
545 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
548 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
549 enum isl_dim_type type, unsigned pos)
551 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
554 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
555 enum isl_dim_type type, unsigned pos)
557 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
560 /* Does the given dimension have a name?
562 int isl_map_has_dim_name(__isl_keep isl_map *map,
563 enum isl_dim_type type, unsigned pos)
565 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
568 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
569 enum isl_dim_type type, unsigned pos)
571 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
574 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
575 enum isl_dim_type type, unsigned pos)
577 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
580 /* Does the given dimension have a name?
582 int isl_set_has_dim_name(__isl_keep isl_set *set,
583 enum isl_dim_type type, unsigned pos)
585 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
588 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
589 __isl_take isl_basic_map *bmap,
590 enum isl_dim_type type, unsigned pos, const char *s)
592 bmap = isl_basic_map_cow(bmap);
593 if (!bmap)
594 return NULL;
595 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
596 if (!bmap->dim)
597 goto error;
598 return isl_basic_map_finalize(bmap);
599 error:
600 isl_basic_map_free(bmap);
601 return NULL;
604 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
605 enum isl_dim_type type, unsigned pos, const char *s)
607 int i;
609 map = isl_map_cow(map);
610 if (!map)
611 return NULL;
613 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
614 if (!map->dim)
615 goto error;
617 for (i = 0; i < map->n; ++i) {
618 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
619 if (!map->p[i])
620 goto error;
623 return map;
624 error:
625 isl_map_free(map);
626 return NULL;
629 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
630 __isl_take isl_basic_set *bset,
631 enum isl_dim_type type, unsigned pos, const char *s)
633 return (isl_basic_set *)isl_basic_map_set_dim_name(
634 (isl_basic_map *)bset, type, pos, s);
637 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
638 enum isl_dim_type type, unsigned pos, const char *s)
640 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
643 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
644 enum isl_dim_type type, unsigned pos)
646 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
649 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
650 enum isl_dim_type type, unsigned pos)
652 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
655 int isl_map_has_dim_id(__isl_keep isl_map *map,
656 enum isl_dim_type type, unsigned pos)
658 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
661 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
662 enum isl_dim_type type, unsigned pos)
664 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
667 int isl_set_has_dim_id(__isl_keep isl_set *set,
668 enum isl_dim_type type, unsigned pos)
670 return isl_map_has_dim_id(set, type, pos);
673 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
674 enum isl_dim_type type, unsigned pos)
676 return isl_map_get_dim_id(set, type, pos);
679 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
680 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
682 map = isl_map_cow(map);
683 if (!map)
684 goto error;
686 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
688 return isl_map_reset_space(map, isl_space_copy(map->dim));
689 error:
690 isl_id_free(id);
691 return NULL;
694 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
695 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
697 return isl_map_set_dim_id(set, type, pos, id);
700 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
701 __isl_keep isl_id *id)
703 if (!map)
704 return -1;
705 return isl_space_find_dim_by_id(map->dim, type, id);
708 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
709 __isl_keep isl_id *id)
711 return isl_map_find_dim_by_id(set, type, id);
714 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
715 const char *name)
717 if (!map)
718 return -1;
719 return isl_space_find_dim_by_name(map->dim, type, name);
722 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
723 const char *name)
725 return isl_map_find_dim_by_name(set, type, name);
728 /* Reset the user pointer on all identifiers of parameters and tuples
729 * of the space of "map".
731 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
733 isl_space *space;
735 space = isl_map_get_space(map);
736 space = isl_space_reset_user(space);
737 map = isl_map_reset_space(map, space);
739 return map;
742 /* Reset the user pointer on all identifiers of parameters and tuples
743 * of the space of "set".
745 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
747 return isl_map_reset_user(set);
750 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
752 if (!bmap)
753 return -1;
754 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
757 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
759 return isl_basic_map_is_rational(bset);
762 /* Does "bmap" contain any rational points?
764 * If "bmap" has an equality for each dimension, equating the dimension
765 * to an integer constant, then it has no rational points, even if it
766 * is marked as rational.
768 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
770 int has_rational = 1;
771 unsigned total;
773 if (!bmap)
774 return -1;
775 if (isl_basic_map_plain_is_empty(bmap))
776 return 0;
777 if (!isl_basic_map_is_rational(bmap))
778 return 0;
779 bmap = isl_basic_map_copy(bmap);
780 bmap = isl_basic_map_implicit_equalities(bmap);
781 if (!bmap)
782 return -1;
783 total = isl_basic_map_total_dim(bmap);
784 if (bmap->n_eq == total) {
785 int i, j;
786 for (i = 0; i < bmap->n_eq; ++i) {
787 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
788 if (j < 0)
789 break;
790 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
791 !isl_int_is_negone(bmap->eq[i][1 + j]))
792 break;
793 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
794 total - j - 1);
795 if (j >= 0)
796 break;
798 if (i == bmap->n_eq)
799 has_rational = 0;
801 isl_basic_map_free(bmap);
803 return has_rational;
806 /* Does "map" contain any rational points?
808 int isl_map_has_rational(__isl_keep isl_map *map)
810 int i;
811 int has_rational;
813 if (!map)
814 return -1;
815 for (i = 0; i < map->n; ++i) {
816 has_rational = isl_basic_map_has_rational(map->p[i]);
817 if (has_rational < 0)
818 return -1;
819 if (has_rational)
820 return 1;
822 return 0;
825 /* Does "set" contain any rational points?
827 int isl_set_has_rational(__isl_keep isl_set *set)
829 return isl_map_has_rational(set);
832 /* Is this basic set a parameter domain?
834 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
836 if (!bset)
837 return -1;
838 return isl_space_is_params(bset->dim);
841 /* Is this set a parameter domain?
843 int isl_set_is_params(__isl_keep isl_set *set)
845 if (!set)
846 return -1;
847 return isl_space_is_params(set->dim);
850 /* Is this map actually a parameter domain?
851 * Users should never call this function. Outside of isl,
852 * a map can never be a parameter domain.
854 int isl_map_is_params(__isl_keep isl_map *map)
856 if (!map)
857 return -1;
858 return isl_space_is_params(map->dim);
861 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
862 struct isl_basic_map *bmap, unsigned extra,
863 unsigned n_eq, unsigned n_ineq)
865 int i;
866 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
868 bmap->ctx = ctx;
869 isl_ctx_ref(ctx);
871 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
872 if (isl_blk_is_error(bmap->block))
873 goto error;
875 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
876 if ((n_ineq + n_eq) && !bmap->ineq)
877 goto error;
879 if (extra == 0) {
880 bmap->block2 = isl_blk_empty();
881 bmap->div = NULL;
882 } else {
883 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
884 if (isl_blk_is_error(bmap->block2))
885 goto error;
887 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
888 if (!bmap->div)
889 goto error;
892 for (i = 0; i < n_ineq + n_eq; ++i)
893 bmap->ineq[i] = bmap->block.data + i * row_size;
895 for (i = 0; i < extra; ++i)
896 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
898 bmap->ref = 1;
899 bmap->flags = 0;
900 bmap->c_size = n_eq + n_ineq;
901 bmap->eq = bmap->ineq + n_ineq;
902 bmap->extra = extra;
903 bmap->n_eq = 0;
904 bmap->n_ineq = 0;
905 bmap->n_div = 0;
906 bmap->sample = NULL;
908 return bmap;
909 error:
910 isl_basic_map_free(bmap);
911 return NULL;
914 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
915 unsigned nparam, unsigned dim, unsigned extra,
916 unsigned n_eq, unsigned n_ineq)
918 struct isl_basic_map *bmap;
919 isl_space *space;
921 space = isl_space_set_alloc(ctx, nparam, dim);
922 if (!space)
923 return NULL;
925 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
926 return (struct isl_basic_set *)bmap;
929 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
930 unsigned extra, unsigned n_eq, unsigned n_ineq)
932 struct isl_basic_map *bmap;
933 if (!dim)
934 return NULL;
935 isl_assert(dim->ctx, dim->n_in == 0, goto error);
936 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
937 return (struct isl_basic_set *)bmap;
938 error:
939 isl_space_free(dim);
940 return NULL;
943 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
944 unsigned extra, unsigned n_eq, unsigned n_ineq)
946 struct isl_basic_map *bmap;
948 if (!dim)
949 return NULL;
950 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
951 if (!bmap)
952 goto error;
953 bmap->dim = dim;
955 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
956 error:
957 isl_space_free(dim);
958 return NULL;
961 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
962 unsigned nparam, unsigned in, unsigned out, unsigned extra,
963 unsigned n_eq, unsigned n_ineq)
965 struct isl_basic_map *bmap;
966 isl_space *dim;
968 dim = isl_space_alloc(ctx, nparam, in, out);
969 if (!dim)
970 return NULL;
972 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
973 return bmap;
976 static void dup_constraints(
977 struct isl_basic_map *dst, struct isl_basic_map *src)
979 int i;
980 unsigned total = isl_basic_map_total_dim(src);
982 for (i = 0; i < src->n_eq; ++i) {
983 int j = isl_basic_map_alloc_equality(dst);
984 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
987 for (i = 0; i < src->n_ineq; ++i) {
988 int j = isl_basic_map_alloc_inequality(dst);
989 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
992 for (i = 0; i < src->n_div; ++i) {
993 int j = isl_basic_map_alloc_div(dst);
994 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
996 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
999 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1001 struct isl_basic_map *dup;
1003 if (!bmap)
1004 return NULL;
1005 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1006 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1007 if (!dup)
1008 return NULL;
1009 dup_constraints(dup, bmap);
1010 dup->flags = bmap->flags;
1011 dup->sample = isl_vec_copy(bmap->sample);
1012 return dup;
1015 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1017 struct isl_basic_map *dup;
1019 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1020 return (struct isl_basic_set *)dup;
1023 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1025 if (!bset)
1026 return NULL;
1028 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1029 bset->ref++;
1030 return bset;
1032 return isl_basic_set_dup(bset);
1035 struct isl_set *isl_set_copy(struct isl_set *set)
1037 if (!set)
1038 return NULL;
1040 set->ref++;
1041 return set;
1044 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1046 if (!bmap)
1047 return NULL;
1049 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1050 bmap->ref++;
1051 return bmap;
1053 bmap = isl_basic_map_dup(bmap);
1054 if (bmap)
1055 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1056 return bmap;
1059 struct isl_map *isl_map_copy(struct isl_map *map)
1061 if (!map)
1062 return NULL;
1064 map->ref++;
1065 return map;
1068 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1070 if (!bmap)
1071 return NULL;
1073 if (--bmap->ref > 0)
1074 return NULL;
1076 isl_ctx_deref(bmap->ctx);
1077 free(bmap->div);
1078 isl_blk_free(bmap->ctx, bmap->block2);
1079 free(bmap->ineq);
1080 isl_blk_free(bmap->ctx, bmap->block);
1081 isl_vec_free(bmap->sample);
1082 isl_space_free(bmap->dim);
1083 free(bmap);
1085 return NULL;
1088 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1090 return isl_basic_map_free((struct isl_basic_map *)bset);
1093 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1095 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1098 __isl_give isl_map *isl_map_align_params_map_map_and(
1099 __isl_take isl_map *map1, __isl_take isl_map *map2,
1100 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1101 __isl_take isl_map *map2))
1103 if (!map1 || !map2)
1104 goto error;
1105 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1106 return fn(map1, map2);
1107 if (!isl_space_has_named_params(map1->dim) ||
1108 !isl_space_has_named_params(map2->dim))
1109 isl_die(map1->ctx, isl_error_invalid,
1110 "unaligned unnamed parameters", goto error);
1111 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1112 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1113 return fn(map1, map2);
1114 error:
1115 isl_map_free(map1);
1116 isl_map_free(map2);
1117 return NULL;
1120 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1121 __isl_keep isl_map *map2,
1122 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1124 int r;
1126 if (!map1 || !map2)
1127 return -1;
1128 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1129 return fn(map1, map2);
1130 if (!isl_space_has_named_params(map1->dim) ||
1131 !isl_space_has_named_params(map2->dim))
1132 isl_die(map1->ctx, isl_error_invalid,
1133 "unaligned unnamed parameters", return -1);
1134 map1 = isl_map_copy(map1);
1135 map2 = isl_map_copy(map2);
1136 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1137 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1138 r = fn(map1, map2);
1139 isl_map_free(map1);
1140 isl_map_free(map2);
1141 return r;
1144 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1146 struct isl_ctx *ctx;
1147 if (!bmap)
1148 return -1;
1149 ctx = bmap->ctx;
1150 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1151 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1152 return -1);
1153 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1154 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1155 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1156 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1157 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1158 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1159 isl_int *t;
1160 int j = isl_basic_map_alloc_inequality(bmap);
1161 if (j < 0)
1162 return -1;
1163 t = bmap->ineq[j];
1164 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1165 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1166 bmap->eq[-1] = t;
1167 bmap->n_eq++;
1168 bmap->n_ineq--;
1169 bmap->eq--;
1170 return 0;
1172 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1173 bmap->extra - bmap->n_div);
1174 return bmap->n_eq++;
1177 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1179 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1182 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1184 if (!bmap)
1185 return -1;
1186 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1187 bmap->n_eq -= n;
1188 return 0;
1191 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1193 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1196 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1198 isl_int *t;
1199 if (!bmap)
1200 return -1;
1201 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1203 if (pos != bmap->n_eq - 1) {
1204 t = bmap->eq[pos];
1205 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1206 bmap->eq[bmap->n_eq - 1] = t;
1208 bmap->n_eq--;
1209 return 0;
1212 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1214 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1217 /* Turn inequality "pos" of "bmap" into an equality.
1219 * In particular, we move the inequality in front of the equalities
1220 * and move the last inequality in the position of the moved inequality.
1221 * Note that isl_tab_make_equalities_explicit depends on this particular
1222 * change in the ordering of the constraints.
1224 void isl_basic_map_inequality_to_equality(
1225 struct isl_basic_map *bmap, unsigned pos)
1227 isl_int *t;
1229 t = bmap->ineq[pos];
1230 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1231 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1232 bmap->eq[-1] = t;
1233 bmap->n_eq++;
1234 bmap->n_ineq--;
1235 bmap->eq--;
1236 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1237 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1238 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1239 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1242 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1244 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1247 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1249 struct isl_ctx *ctx;
1250 if (!bmap)
1251 return -1;
1252 ctx = bmap->ctx;
1253 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1254 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1255 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1256 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1257 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1258 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1259 1 + isl_basic_map_total_dim(bmap),
1260 bmap->extra - bmap->n_div);
1261 return bmap->n_ineq++;
1264 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1266 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1269 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1271 if (!bmap)
1272 return -1;
1273 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1274 bmap->n_ineq -= n;
1275 return 0;
1278 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1280 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1283 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1285 isl_int *t;
1286 if (!bmap)
1287 return -1;
1288 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1290 if (pos != bmap->n_ineq - 1) {
1291 t = bmap->ineq[pos];
1292 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1293 bmap->ineq[bmap->n_ineq - 1] = t;
1294 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1296 bmap->n_ineq--;
1297 return 0;
1300 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1302 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1305 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1306 isl_int *eq)
1308 int k;
1310 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1311 if (!bmap)
1312 return NULL;
1313 k = isl_basic_map_alloc_equality(bmap);
1314 if (k < 0)
1315 goto error;
1316 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1317 return bmap;
1318 error:
1319 isl_basic_map_free(bmap);
1320 return NULL;
1323 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1324 isl_int *eq)
1326 return (isl_basic_set *)
1327 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1330 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1331 isl_int *ineq)
1333 int k;
1335 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1336 if (!bmap)
1337 return NULL;
1338 k = isl_basic_map_alloc_inequality(bmap);
1339 if (k < 0)
1340 goto error;
1341 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1342 return bmap;
1343 error:
1344 isl_basic_map_free(bmap);
1345 return NULL;
1348 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1349 isl_int *ineq)
1351 return (isl_basic_set *)
1352 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1355 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1357 if (!bmap)
1358 return -1;
1359 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1360 isl_seq_clr(bmap->div[bmap->n_div] +
1361 1 + 1 + isl_basic_map_total_dim(bmap),
1362 bmap->extra - bmap->n_div);
1363 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1364 return bmap->n_div++;
1367 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1369 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1372 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1374 if (!bmap)
1375 return -1;
1376 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1377 bmap->n_div -= n;
1378 return 0;
1381 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1383 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1386 /* Copy constraint from src to dst, putting the vars of src at offset
1387 * dim_off in dst and the divs of src at offset div_off in dst.
1388 * If both sets are actually map, then dim_off applies to the input
1389 * variables.
1391 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1392 struct isl_basic_map *src_map, isl_int *src,
1393 unsigned in_off, unsigned out_off, unsigned div_off)
1395 unsigned src_nparam = isl_basic_map_n_param(src_map);
1396 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1397 unsigned src_in = isl_basic_map_n_in(src_map);
1398 unsigned dst_in = isl_basic_map_n_in(dst_map);
1399 unsigned src_out = isl_basic_map_n_out(src_map);
1400 unsigned dst_out = isl_basic_map_n_out(dst_map);
1401 isl_int_set(dst[0], src[0]);
1402 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1403 if (dst_nparam > src_nparam)
1404 isl_seq_clr(dst+1+src_nparam,
1405 dst_nparam - src_nparam);
1406 isl_seq_clr(dst+1+dst_nparam, in_off);
1407 isl_seq_cpy(dst+1+dst_nparam+in_off,
1408 src+1+src_nparam,
1409 isl_min(dst_in-in_off, src_in));
1410 if (dst_in-in_off > src_in)
1411 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1412 dst_in - in_off - src_in);
1413 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1414 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1415 src+1+src_nparam+src_in,
1416 isl_min(dst_out-out_off, src_out));
1417 if (dst_out-out_off > src_out)
1418 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1419 dst_out - out_off - src_out);
1420 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1421 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1422 src+1+src_nparam+src_in+src_out,
1423 isl_min(dst_map->extra-div_off, src_map->n_div));
1424 if (dst_map->n_div-div_off > src_map->n_div)
1425 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1426 div_off+src_map->n_div,
1427 dst_map->n_div - div_off - src_map->n_div);
1430 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1431 struct isl_basic_map *src_map, isl_int *src,
1432 unsigned in_off, unsigned out_off, unsigned div_off)
1434 isl_int_set(dst[0], src[0]);
1435 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1438 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1439 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1441 int i;
1442 unsigned div_off;
1444 if (!bmap1 || !bmap2)
1445 goto error;
1447 div_off = bmap1->n_div;
1449 for (i = 0; i < bmap2->n_eq; ++i) {
1450 int i1 = isl_basic_map_alloc_equality(bmap1);
1451 if (i1 < 0)
1452 goto error;
1453 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1454 i_pos, o_pos, div_off);
1457 for (i = 0; i < bmap2->n_ineq; ++i) {
1458 int i1 = isl_basic_map_alloc_inequality(bmap1);
1459 if (i1 < 0)
1460 goto error;
1461 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1462 i_pos, o_pos, div_off);
1465 for (i = 0; i < bmap2->n_div; ++i) {
1466 int i1 = isl_basic_map_alloc_div(bmap1);
1467 if (i1 < 0)
1468 goto error;
1469 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1470 i_pos, o_pos, div_off);
1473 isl_basic_map_free(bmap2);
1475 return bmap1;
1477 error:
1478 isl_basic_map_free(bmap1);
1479 isl_basic_map_free(bmap2);
1480 return NULL;
1483 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1484 struct isl_basic_set *bset2, unsigned pos)
1486 return (struct isl_basic_set *)
1487 add_constraints((struct isl_basic_map *)bset1,
1488 (struct isl_basic_map *)bset2, 0, pos);
1491 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1492 __isl_take isl_space *dim, unsigned extra,
1493 unsigned n_eq, unsigned n_ineq)
1495 struct isl_basic_map *ext;
1496 unsigned flags;
1497 int dims_ok;
1499 if (!dim)
1500 goto error;
1502 if (!base)
1503 goto error;
1505 dims_ok = isl_space_is_equal(base->dim, dim) &&
1506 base->extra >= base->n_div + extra;
1508 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1509 room_for_ineq(base, n_ineq)) {
1510 isl_space_free(dim);
1511 return base;
1514 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1515 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1516 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1517 extra += base->extra;
1518 n_eq += base->n_eq;
1519 n_ineq += base->n_ineq;
1521 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1522 dim = NULL;
1523 if (!ext)
1524 goto error;
1526 if (dims_ok)
1527 ext->sample = isl_vec_copy(base->sample);
1528 flags = base->flags;
1529 ext = add_constraints(ext, base, 0, 0);
1530 if (ext) {
1531 ext->flags = flags;
1532 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1535 return ext;
1537 error:
1538 isl_space_free(dim);
1539 isl_basic_map_free(base);
1540 return NULL;
1543 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1544 __isl_take isl_space *dim, unsigned extra,
1545 unsigned n_eq, unsigned n_ineq)
1547 return (struct isl_basic_set *)
1548 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1549 extra, n_eq, n_ineq);
1552 struct isl_basic_map *isl_basic_map_extend_constraints(
1553 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1555 if (!base)
1556 return NULL;
1557 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1558 0, n_eq, n_ineq);
1561 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1562 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1563 unsigned n_eq, unsigned n_ineq)
1565 struct isl_basic_map *bmap;
1566 isl_space *dim;
1568 if (!base)
1569 return NULL;
1570 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1571 if (!dim)
1572 goto error;
1574 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1575 return bmap;
1576 error:
1577 isl_basic_map_free(base);
1578 return NULL;
1581 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1582 unsigned nparam, unsigned dim, unsigned extra,
1583 unsigned n_eq, unsigned n_ineq)
1585 return (struct isl_basic_set *)
1586 isl_basic_map_extend((struct isl_basic_map *)base,
1587 nparam, 0, dim, extra, n_eq, n_ineq);
1590 struct isl_basic_set *isl_basic_set_extend_constraints(
1591 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1593 return (struct isl_basic_set *)
1594 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1595 n_eq, n_ineq);
1598 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1600 return (struct isl_basic_set *)
1601 isl_basic_map_cow((struct isl_basic_map *)bset);
1604 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1606 if (!bmap)
1607 return NULL;
1609 if (bmap->ref > 1) {
1610 bmap->ref--;
1611 bmap = isl_basic_map_dup(bmap);
1613 if (bmap)
1614 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1615 return bmap;
1618 struct isl_set *isl_set_cow(struct isl_set *set)
1620 if (!set)
1621 return NULL;
1623 if (set->ref == 1)
1624 return set;
1625 set->ref--;
1626 return isl_set_dup(set);
1629 struct isl_map *isl_map_cow(struct isl_map *map)
1631 if (!map)
1632 return NULL;
1634 if (map->ref == 1)
1635 return map;
1636 map->ref--;
1637 return isl_map_dup(map);
1640 static void swap_vars(struct isl_blk blk, isl_int *a,
1641 unsigned a_len, unsigned b_len)
1643 isl_seq_cpy(blk.data, a+a_len, b_len);
1644 isl_seq_cpy(blk.data+b_len, a, a_len);
1645 isl_seq_cpy(a, blk.data, b_len+a_len);
1648 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1649 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1651 int i;
1652 struct isl_blk blk;
1654 if (!bmap)
1655 goto error;
1657 isl_assert(bmap->ctx,
1658 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1660 if (n1 == 0 || n2 == 0)
1661 return bmap;
1663 bmap = isl_basic_map_cow(bmap);
1664 if (!bmap)
1665 return NULL;
1667 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1668 if (isl_blk_is_error(blk))
1669 goto error;
1671 for (i = 0; i < bmap->n_eq; ++i)
1672 swap_vars(blk,
1673 bmap->eq[i] + pos, n1, n2);
1675 for (i = 0; i < bmap->n_ineq; ++i)
1676 swap_vars(blk,
1677 bmap->ineq[i] + pos, n1, n2);
1679 for (i = 0; i < bmap->n_div; ++i)
1680 swap_vars(blk,
1681 bmap->div[i]+1 + pos, n1, n2);
1683 isl_blk_free(bmap->ctx, blk);
1685 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1686 bmap = isl_basic_map_gauss(bmap, NULL);
1687 return isl_basic_map_finalize(bmap);
1688 error:
1689 isl_basic_map_free(bmap);
1690 return NULL;
1693 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1694 __isl_take isl_basic_set *bset, unsigned n)
1696 unsigned dim;
1697 unsigned nparam;
1699 if (!bset)
1700 return NULL;
1702 nparam = isl_basic_set_n_param(bset);
1703 dim = isl_basic_set_n_dim(bset);
1704 isl_assert(bset->ctx, n <= dim, goto error);
1706 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1707 error:
1708 isl_basic_set_free(bset);
1709 return NULL;
1712 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1714 int i = 0;
1715 unsigned total;
1716 if (!bmap)
1717 goto error;
1718 total = isl_basic_map_total_dim(bmap);
1719 isl_basic_map_free_div(bmap, bmap->n_div);
1720 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1721 if (bmap->n_eq > 0)
1722 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1723 else {
1724 i = isl_basic_map_alloc_equality(bmap);
1725 if (i < 0)
1726 goto error;
1728 isl_int_set_si(bmap->eq[i][0], 1);
1729 isl_seq_clr(bmap->eq[i]+1, total);
1730 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1731 isl_vec_free(bmap->sample);
1732 bmap->sample = NULL;
1733 return isl_basic_map_finalize(bmap);
1734 error:
1735 isl_basic_map_free(bmap);
1736 return NULL;
1739 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1741 return (struct isl_basic_set *)
1742 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1745 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1746 * of "bmap").
1748 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1750 isl_int *t = bmap->div[a];
1751 bmap->div[a] = bmap->div[b];
1752 bmap->div[b] = t;
1755 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1756 * div definitions accordingly.
1758 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1760 int i;
1761 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1763 swap_div(bmap, a, b);
1765 for (i = 0; i < bmap->n_eq; ++i)
1766 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1768 for (i = 0; i < bmap->n_ineq; ++i)
1769 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1771 for (i = 0; i < bmap->n_div; ++i)
1772 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1773 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1776 /* Eliminate the specified n dimensions starting at first from the
1777 * constraints, without removing the dimensions from the space.
1778 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1780 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1781 enum isl_dim_type type, unsigned first, unsigned n)
1783 int i;
1785 if (!map)
1786 return NULL;
1787 if (n == 0)
1788 return map;
1790 if (first + n > isl_map_dim(map, type) || first + n < first)
1791 isl_die(map->ctx, isl_error_invalid,
1792 "index out of bounds", goto error);
1794 map = isl_map_cow(map);
1795 if (!map)
1796 return NULL;
1798 for (i = 0; i < map->n; ++i) {
1799 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1800 if (!map->p[i])
1801 goto error;
1803 return map;
1804 error:
1805 isl_map_free(map);
1806 return NULL;
1809 /* Eliminate the specified n dimensions starting at first from the
1810 * constraints, without removing the dimensions from the space.
1811 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1813 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1814 enum isl_dim_type type, unsigned first, unsigned n)
1816 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1819 /* Eliminate the specified n dimensions starting at first from the
1820 * constraints, without removing the dimensions from the space.
1821 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1823 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1824 unsigned first, unsigned n)
1826 return isl_set_eliminate(set, isl_dim_set, first, n);
1829 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1830 __isl_take isl_basic_map *bmap)
1832 if (!bmap)
1833 return NULL;
1834 bmap = isl_basic_map_eliminate_vars(bmap,
1835 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1836 if (!bmap)
1837 return NULL;
1838 bmap->n_div = 0;
1839 return isl_basic_map_finalize(bmap);
1842 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1843 __isl_take isl_basic_set *bset)
1845 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1846 (struct isl_basic_map *)bset);
1849 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1851 int i;
1853 if (!map)
1854 return NULL;
1855 if (map->n == 0)
1856 return map;
1858 map = isl_map_cow(map);
1859 if (!map)
1860 return NULL;
1862 for (i = 0; i < map->n; ++i) {
1863 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1864 if (!map->p[i])
1865 goto error;
1867 return map;
1868 error:
1869 isl_map_free(map);
1870 return NULL;
1873 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1875 return isl_map_remove_divs(set);
1878 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1879 enum isl_dim_type type, unsigned first, unsigned n)
1881 if (!bmap)
1882 return NULL;
1883 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1884 goto error);
1885 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1886 return bmap;
1887 bmap = isl_basic_map_eliminate_vars(bmap,
1888 isl_basic_map_offset(bmap, type) - 1 + first, n);
1889 if (!bmap)
1890 return bmap;
1891 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1892 return bmap;
1893 bmap = isl_basic_map_drop(bmap, type, first, n);
1894 return bmap;
1895 error:
1896 isl_basic_map_free(bmap);
1897 return NULL;
1900 /* Return true if the definition of the given div (recursively) involves
1901 * any of the given variables.
1903 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1904 unsigned first, unsigned n)
1906 int i;
1907 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1909 if (isl_int_is_zero(bmap->div[div][0]))
1910 return 0;
1911 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1912 return 1;
1914 for (i = bmap->n_div - 1; i >= 0; --i) {
1915 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1916 continue;
1917 if (div_involves_vars(bmap, i, first, n))
1918 return 1;
1921 return 0;
1924 /* Try and add a lower and/or upper bound on "div" to "bmap"
1925 * based on inequality "i".
1926 * "total" is the total number of variables (excluding the divs).
1927 * "v" is a temporary object that can be used during the calculations.
1928 * If "lb" is set, then a lower bound should be constructed.
1929 * If "ub" is set, then an upper bound should be constructed.
1931 * The calling function has already checked that the inequality does not
1932 * reference "div", but we still need to check that the inequality is
1933 * of the right form. We'll consider the case where we want to construct
1934 * a lower bound. The construction of upper bounds is similar.
1936 * Let "div" be of the form
1938 * q = floor((a + f(x))/d)
1940 * We essentially check if constraint "i" is of the form
1942 * b + f(x) >= 0
1944 * so that we can use it to derive a lower bound on "div".
1945 * However, we allow a slightly more general form
1947 * b + g(x) >= 0
1949 * with the condition that the coefficients of g(x) - f(x) are all
1950 * divisible by d.
1951 * Rewriting this constraint as
1953 * 0 >= -b - g(x)
1955 * adding a + f(x) to both sides and dividing by d, we obtain
1957 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1959 * Taking the floor on both sides, we obtain
1961 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1963 * or
1965 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1967 * In the case of an upper bound, we construct the constraint
1969 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1972 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1973 __isl_take isl_basic_map *bmap, int div, int i,
1974 unsigned total, isl_int v, int lb, int ub)
1976 int j;
1978 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1979 if (lb) {
1980 isl_int_sub(v, bmap->ineq[i][1 + j],
1981 bmap->div[div][1 + 1 + j]);
1982 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1984 if (ub) {
1985 isl_int_add(v, bmap->ineq[i][1 + j],
1986 bmap->div[div][1 + 1 + j]);
1987 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1990 if (!lb && !ub)
1991 return bmap;
1993 bmap = isl_basic_map_cow(bmap);
1994 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1995 if (lb) {
1996 int k = isl_basic_map_alloc_inequality(bmap);
1997 if (k < 0)
1998 goto error;
1999 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2000 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2001 bmap->div[div][1 + j]);
2002 isl_int_cdiv_q(bmap->ineq[k][j],
2003 bmap->ineq[k][j], bmap->div[div][0]);
2005 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2007 if (ub) {
2008 int k = isl_basic_map_alloc_inequality(bmap);
2009 if (k < 0)
2010 goto error;
2011 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2012 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2013 bmap->div[div][1 + j]);
2014 isl_int_fdiv_q(bmap->ineq[k][j],
2015 bmap->ineq[k][j], bmap->div[div][0]);
2017 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2020 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2021 return bmap;
2022 error:
2023 isl_basic_map_free(bmap);
2024 return NULL;
2027 /* This function is called right before "div" is eliminated from "bmap"
2028 * using Fourier-Motzkin.
2029 * Look through the constraints of "bmap" for constraints on the argument
2030 * of the integer division and use them to construct constraints on the
2031 * integer division itself. These constraints can then be combined
2032 * during the Fourier-Motzkin elimination.
2033 * Note that it is only useful to introduce lower bounds on "div"
2034 * if "bmap" already contains upper bounds on "div" as the newly
2035 * introduce lower bounds can then be combined with the pre-existing
2036 * upper bounds. Similarly for upper bounds.
2037 * We therefore first check if "bmap" contains any lower and/or upper bounds
2038 * on "div".
2040 * It is interesting to note that the introduction of these constraints
2041 * can indeed lead to more accurate results, even when compared to
2042 * deriving constraints on the argument of "div" from constraints on "div".
2043 * Consider, for example, the set
2045 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2047 * The second constraint can be rewritten as
2049 * 2 * [(-i-2j+3)/4] + k >= 0
2051 * from which we can derive
2053 * -i - 2j + 3 >= -2k
2055 * or
2057 * i + 2j <= 3 + 2k
2059 * Combined with the first constraint, we obtain
2061 * -3 <= 3 + 2k or k >= -3
2063 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2064 * the first constraint, we obtain
2066 * [(i + 2j)/4] >= [-3/4] = -1
2068 * Combining this constraint with the second constraint, we obtain
2070 * k >= -2
2072 static __isl_give isl_basic_map *insert_bounds_on_div(
2073 __isl_take isl_basic_map *bmap, int div)
2075 int i;
2076 int check_lb, check_ub;
2077 isl_int v;
2078 unsigned total;
2080 if (!bmap)
2081 return NULL;
2083 if (isl_int_is_zero(bmap->div[div][0]))
2084 return bmap;
2086 total = isl_space_dim(bmap->dim, isl_dim_all);
2088 check_lb = 0;
2089 check_ub = 0;
2090 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2091 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2092 if (s > 0)
2093 check_ub = 1;
2094 if (s < 0)
2095 check_lb = 1;
2098 if (!check_lb && !check_ub)
2099 return bmap;
2101 isl_int_init(v);
2103 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2104 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2105 continue;
2107 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2108 check_lb, check_ub);
2111 isl_int_clear(v);
2113 return bmap;
2116 /* Remove all divs (recursively) involving any of the given dimensions
2117 * in their definitions.
2119 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2120 __isl_take isl_basic_map *bmap,
2121 enum isl_dim_type type, unsigned first, unsigned n)
2123 int i;
2125 if (!bmap)
2126 return NULL;
2127 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2128 goto error);
2129 first += isl_basic_map_offset(bmap, type);
2131 for (i = bmap->n_div - 1; i >= 0; --i) {
2132 if (!div_involves_vars(bmap, i, first, n))
2133 continue;
2134 bmap = insert_bounds_on_div(bmap, i);
2135 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2136 if (!bmap)
2137 return NULL;
2138 i = bmap->n_div;
2141 return bmap;
2142 error:
2143 isl_basic_map_free(bmap);
2144 return NULL;
2147 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2148 __isl_take isl_basic_set *bset,
2149 enum isl_dim_type type, unsigned first, unsigned n)
2151 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2154 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2155 enum isl_dim_type type, unsigned first, unsigned n)
2157 int i;
2159 if (!map)
2160 return NULL;
2161 if (map->n == 0)
2162 return map;
2164 map = isl_map_cow(map);
2165 if (!map)
2166 return NULL;
2168 for (i = 0; i < map->n; ++i) {
2169 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2170 type, first, n);
2171 if (!map->p[i])
2172 goto error;
2174 return map;
2175 error:
2176 isl_map_free(map);
2177 return NULL;
2180 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2181 enum isl_dim_type type, unsigned first, unsigned n)
2183 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2184 type, first, n);
2187 /* Does the desciption of "bmap" depend on the specified dimensions?
2188 * We also check whether the dimensions appear in any of the div definitions.
2189 * In principle there is no need for this check. If the dimensions appear
2190 * in a div definition, they also appear in the defining constraints of that
2191 * div.
2193 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2194 enum isl_dim_type type, unsigned first, unsigned n)
2196 int i;
2198 if (!bmap)
2199 return -1;
2201 if (first + n > isl_basic_map_dim(bmap, type))
2202 isl_die(bmap->ctx, isl_error_invalid,
2203 "index out of bounds", return -1);
2205 first += isl_basic_map_offset(bmap, type);
2206 for (i = 0; i < bmap->n_eq; ++i)
2207 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2208 return 1;
2209 for (i = 0; i < bmap->n_ineq; ++i)
2210 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2211 return 1;
2212 for (i = 0; i < bmap->n_div; ++i) {
2213 if (isl_int_is_zero(bmap->div[i][0]))
2214 continue;
2215 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2216 return 1;
2219 return 0;
2222 int isl_map_involves_dims(__isl_keep isl_map *map,
2223 enum isl_dim_type type, unsigned first, unsigned n)
2225 int i;
2227 if (!map)
2228 return -1;
2230 if (first + n > isl_map_dim(map, type))
2231 isl_die(map->ctx, isl_error_invalid,
2232 "index out of bounds", return -1);
2234 for (i = 0; i < map->n; ++i) {
2235 int involves = isl_basic_map_involves_dims(map->p[i],
2236 type, first, n);
2237 if (involves < 0 || involves)
2238 return involves;
2241 return 0;
2244 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2245 enum isl_dim_type type, unsigned first, unsigned n)
2247 return isl_basic_map_involves_dims(bset, type, first, n);
2250 int isl_set_involves_dims(__isl_keep isl_set *set,
2251 enum isl_dim_type type, unsigned first, unsigned n)
2253 return isl_map_involves_dims(set, type, first, n);
2256 /* Return true if the definition of the given div is unknown or depends
2257 * on unknown divs.
2259 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2261 int i;
2262 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2264 if (isl_int_is_zero(bmap->div[div][0]))
2265 return 1;
2267 for (i = bmap->n_div - 1; i >= 0; --i) {
2268 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2269 continue;
2270 if (div_is_unknown(bmap, i))
2271 return 1;
2274 return 0;
2277 /* Remove all divs that are unknown or defined in terms of unknown divs.
2279 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2280 __isl_take isl_basic_map *bmap)
2282 int i;
2284 if (!bmap)
2285 return NULL;
2287 for (i = bmap->n_div - 1; i >= 0; --i) {
2288 if (!div_is_unknown(bmap, i))
2289 continue;
2290 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2291 if (!bmap)
2292 return NULL;
2293 i = bmap->n_div;
2296 return bmap;
2299 /* Remove all divs that are unknown or defined in terms of unknown divs.
2301 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2302 __isl_take isl_basic_set *bset)
2304 return isl_basic_map_remove_unknown_divs(bset);
2307 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2309 int i;
2311 if (!map)
2312 return NULL;
2313 if (map->n == 0)
2314 return map;
2316 map = isl_map_cow(map);
2317 if (!map)
2318 return NULL;
2320 for (i = 0; i < map->n; ++i) {
2321 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2322 if (!map->p[i])
2323 goto error;
2325 return map;
2326 error:
2327 isl_map_free(map);
2328 return NULL;
2331 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2333 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2336 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2337 __isl_take isl_basic_set *bset,
2338 enum isl_dim_type type, unsigned first, unsigned n)
2340 return (isl_basic_set *)
2341 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2344 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2345 enum isl_dim_type type, unsigned first, unsigned n)
2347 int i;
2349 if (n == 0)
2350 return map;
2352 map = isl_map_cow(map);
2353 if (!map)
2354 return NULL;
2355 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2357 for (i = 0; i < map->n; ++i) {
2358 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2359 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2360 if (!map->p[i])
2361 goto error;
2363 map = isl_map_drop(map, type, first, n);
2364 return map;
2365 error:
2366 isl_map_free(map);
2367 return NULL;
2370 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2371 enum isl_dim_type type, unsigned first, unsigned n)
2373 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2376 /* Project out n inputs starting at first using Fourier-Motzkin */
2377 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2378 unsigned first, unsigned n)
2380 return isl_map_remove_dims(map, isl_dim_in, first, n);
2383 static void dump_term(struct isl_basic_map *bmap,
2384 isl_int c, int pos, FILE *out)
2386 const char *name;
2387 unsigned in = isl_basic_map_n_in(bmap);
2388 unsigned dim = in + isl_basic_map_n_out(bmap);
2389 unsigned nparam = isl_basic_map_n_param(bmap);
2390 if (!pos)
2391 isl_int_print(out, c, 0);
2392 else {
2393 if (!isl_int_is_one(c))
2394 isl_int_print(out, c, 0);
2395 if (pos < 1 + nparam) {
2396 name = isl_space_get_dim_name(bmap->dim,
2397 isl_dim_param, pos - 1);
2398 if (name)
2399 fprintf(out, "%s", name);
2400 else
2401 fprintf(out, "p%d", pos - 1);
2402 } else if (pos < 1 + nparam + in)
2403 fprintf(out, "i%d", pos - 1 - nparam);
2404 else if (pos < 1 + nparam + dim)
2405 fprintf(out, "o%d", pos - 1 - nparam - in);
2406 else
2407 fprintf(out, "e%d", pos - 1 - nparam - dim);
2411 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2412 int sign, FILE *out)
2414 int i;
2415 int first;
2416 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2417 isl_int v;
2419 isl_int_init(v);
2420 for (i = 0, first = 1; i < len; ++i) {
2421 if (isl_int_sgn(c[i]) * sign <= 0)
2422 continue;
2423 if (!first)
2424 fprintf(out, " + ");
2425 first = 0;
2426 isl_int_abs(v, c[i]);
2427 dump_term(bmap, v, i, out);
2429 isl_int_clear(v);
2430 if (first)
2431 fprintf(out, "0");
2434 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2435 const char *op, FILE *out, int indent)
2437 int i;
2439 fprintf(out, "%*s", indent, "");
2441 dump_constraint_sign(bmap, c, 1, out);
2442 fprintf(out, " %s ", op);
2443 dump_constraint_sign(bmap, c, -1, out);
2445 fprintf(out, "\n");
2447 for (i = bmap->n_div; i < bmap->extra; ++i) {
2448 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2449 continue;
2450 fprintf(out, "%*s", indent, "");
2451 fprintf(out, "ERROR: unused div coefficient not zero\n");
2452 abort();
2456 static void dump_constraints(struct isl_basic_map *bmap,
2457 isl_int **c, unsigned n,
2458 const char *op, FILE *out, int indent)
2460 int i;
2462 for (i = 0; i < n; ++i)
2463 dump_constraint(bmap, c[i], op, out, indent);
2466 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2468 int j;
2469 int first = 1;
2470 unsigned total = isl_basic_map_total_dim(bmap);
2472 for (j = 0; j < 1 + total; ++j) {
2473 if (isl_int_is_zero(exp[j]))
2474 continue;
2475 if (!first && isl_int_is_pos(exp[j]))
2476 fprintf(out, "+");
2477 dump_term(bmap, exp[j], j, out);
2478 first = 0;
2482 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2484 int i;
2486 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2487 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2489 for (i = 0; i < bmap->n_div; ++i) {
2490 fprintf(out, "%*s", indent, "");
2491 fprintf(out, "e%d = [(", i);
2492 dump_affine(bmap, bmap->div[i]+1, out);
2493 fprintf(out, ")/");
2494 isl_int_print(out, bmap->div[i][0], 0);
2495 fprintf(out, "]\n");
2499 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2500 FILE *out, int indent)
2502 if (!bset) {
2503 fprintf(out, "null basic set\n");
2504 return;
2507 fprintf(out, "%*s", indent, "");
2508 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2509 bset->ref, bset->dim->nparam, bset->dim->n_out,
2510 bset->extra, bset->flags);
2511 dump((struct isl_basic_map *)bset, out, indent);
2514 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2515 FILE *out, int indent)
2517 if (!bmap) {
2518 fprintf(out, "null basic map\n");
2519 return;
2522 fprintf(out, "%*s", indent, "");
2523 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2524 "flags: %x, n_name: %d\n",
2525 bmap->ref,
2526 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2527 bmap->extra, bmap->flags, bmap->dim->n_id);
2528 dump(bmap, out, indent);
2531 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2533 unsigned total;
2534 if (!bmap)
2535 return -1;
2536 total = isl_basic_map_total_dim(bmap);
2537 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2538 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2539 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2540 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2541 return 0;
2544 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2545 unsigned flags)
2547 struct isl_set *set;
2549 if (!dim)
2550 return NULL;
2551 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2552 isl_assert(dim->ctx, n >= 0, goto error);
2553 set = isl_alloc(dim->ctx, struct isl_set,
2554 sizeof(struct isl_set) +
2555 (n - 1) * sizeof(struct isl_basic_set *));
2556 if (!set)
2557 goto error;
2559 set->ctx = dim->ctx;
2560 isl_ctx_ref(set->ctx);
2561 set->ref = 1;
2562 set->size = n;
2563 set->n = 0;
2564 set->dim = dim;
2565 set->flags = flags;
2566 return set;
2567 error:
2568 isl_space_free(dim);
2569 return NULL;
2572 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2573 unsigned nparam, unsigned dim, int n, unsigned flags)
2575 struct isl_set *set;
2576 isl_space *dims;
2578 dims = isl_space_alloc(ctx, nparam, 0, dim);
2579 if (!dims)
2580 return NULL;
2582 set = isl_set_alloc_space(dims, n, flags);
2583 return set;
2586 /* Make sure "map" has room for at least "n" more basic maps.
2588 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2590 int i;
2591 struct isl_map *grown = NULL;
2593 if (!map)
2594 return NULL;
2595 isl_assert(map->ctx, n >= 0, goto error);
2596 if (map->n + n <= map->size)
2597 return map;
2598 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2599 if (!grown)
2600 goto error;
2601 for (i = 0; i < map->n; ++i) {
2602 grown->p[i] = isl_basic_map_copy(map->p[i]);
2603 if (!grown->p[i])
2604 goto error;
2605 grown->n++;
2607 isl_map_free(map);
2608 return grown;
2609 error:
2610 isl_map_free(grown);
2611 isl_map_free(map);
2612 return NULL;
2615 /* Make sure "set" has room for at least "n" more basic sets.
2617 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2619 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2622 struct isl_set *isl_set_dup(struct isl_set *set)
2624 int i;
2625 struct isl_set *dup;
2627 if (!set)
2628 return NULL;
2630 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2631 if (!dup)
2632 return NULL;
2633 for (i = 0; i < set->n; ++i)
2634 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2635 return dup;
2638 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2640 return isl_map_from_basic_map(bset);
2643 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2645 struct isl_map *map;
2647 if (!bmap)
2648 return NULL;
2650 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2651 return isl_map_add_basic_map(map, bmap);
2654 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2655 __isl_take isl_basic_set *bset)
2657 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2658 (struct isl_basic_map *)bset);
2661 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2663 int i;
2665 if (!set)
2666 return NULL;
2668 if (--set->ref > 0)
2669 return NULL;
2671 isl_ctx_deref(set->ctx);
2672 for (i = 0; i < set->n; ++i)
2673 isl_basic_set_free(set->p[i]);
2674 isl_space_free(set->dim);
2675 free(set);
2677 return NULL;
2680 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2682 int i;
2684 if (!set) {
2685 fprintf(out, "null set\n");
2686 return;
2689 fprintf(out, "%*s", indent, "");
2690 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2691 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2692 set->flags);
2693 for (i = 0; i < set->n; ++i) {
2694 fprintf(out, "%*s", indent, "");
2695 fprintf(out, "basic set %d:\n", i);
2696 isl_basic_set_print_internal(set->p[i], out, indent+4);
2700 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2702 int i;
2704 if (!map) {
2705 fprintf(out, "null map\n");
2706 return;
2709 fprintf(out, "%*s", indent, "");
2710 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2711 "flags: %x, n_name: %d\n",
2712 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2713 map->dim->n_out, map->flags, map->dim->n_id);
2714 for (i = 0; i < map->n; ++i) {
2715 fprintf(out, "%*s", indent, "");
2716 fprintf(out, "basic map %d:\n", i);
2717 isl_basic_map_print_internal(map->p[i], out, indent+4);
2721 struct isl_basic_map *isl_basic_map_intersect_domain(
2722 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2724 struct isl_basic_map *bmap_domain;
2726 if (!bmap || !bset)
2727 goto error;
2729 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2730 bset->dim, isl_dim_param), goto error);
2732 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2733 isl_assert(bset->ctx,
2734 isl_basic_map_compatible_domain(bmap, bset), goto error);
2736 bmap = isl_basic_map_cow(bmap);
2737 if (!bmap)
2738 goto error;
2739 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2740 bset->n_div, bset->n_eq, bset->n_ineq);
2741 bmap_domain = isl_basic_map_from_domain(bset);
2742 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2744 bmap = isl_basic_map_simplify(bmap);
2745 return isl_basic_map_finalize(bmap);
2746 error:
2747 isl_basic_map_free(bmap);
2748 isl_basic_set_free(bset);
2749 return NULL;
2752 struct isl_basic_map *isl_basic_map_intersect_range(
2753 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2755 struct isl_basic_map *bmap_range;
2757 if (!bmap || !bset)
2758 goto error;
2760 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2761 bset->dim, isl_dim_param), goto error);
2763 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2764 isl_assert(bset->ctx,
2765 isl_basic_map_compatible_range(bmap, bset), goto error);
2767 if (isl_basic_set_is_universe(bset)) {
2768 isl_basic_set_free(bset);
2769 return bmap;
2772 bmap = isl_basic_map_cow(bmap);
2773 if (!bmap)
2774 goto error;
2775 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2776 bset->n_div, bset->n_eq, bset->n_ineq);
2777 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2778 bmap = add_constraints(bmap, bmap_range, 0, 0);
2780 bmap = isl_basic_map_simplify(bmap);
2781 return isl_basic_map_finalize(bmap);
2782 error:
2783 isl_basic_map_free(bmap);
2784 isl_basic_set_free(bset);
2785 return NULL;
2788 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2790 int i;
2791 unsigned total;
2792 isl_int s;
2794 if (!bmap || !vec)
2795 return -1;
2797 total = 1 + isl_basic_map_total_dim(bmap);
2798 if (total != vec->size)
2799 return -1;
2801 isl_int_init(s);
2803 for (i = 0; i < bmap->n_eq; ++i) {
2804 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2805 if (!isl_int_is_zero(s)) {
2806 isl_int_clear(s);
2807 return 0;
2811 for (i = 0; i < bmap->n_ineq; ++i) {
2812 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2813 if (isl_int_is_neg(s)) {
2814 isl_int_clear(s);
2815 return 0;
2819 isl_int_clear(s);
2821 return 1;
2824 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2826 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2829 struct isl_basic_map *isl_basic_map_intersect(
2830 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2832 struct isl_vec *sample = NULL;
2834 if (!bmap1 || !bmap2)
2835 goto error;
2837 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2838 bmap2->dim, isl_dim_param), goto error);
2839 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2840 isl_space_dim(bmap1->dim, isl_dim_param) &&
2841 isl_space_dim(bmap2->dim, isl_dim_all) !=
2842 isl_space_dim(bmap2->dim, isl_dim_param))
2843 return isl_basic_map_intersect(bmap2, bmap1);
2845 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2846 isl_space_dim(bmap2->dim, isl_dim_param))
2847 isl_assert(bmap1->ctx,
2848 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2850 if (bmap1->sample &&
2851 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2852 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2853 sample = isl_vec_copy(bmap1->sample);
2854 else if (bmap2->sample &&
2855 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2856 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2857 sample = isl_vec_copy(bmap2->sample);
2859 bmap1 = isl_basic_map_cow(bmap1);
2860 if (!bmap1)
2861 goto error;
2862 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2863 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2864 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2866 if (!bmap1)
2867 isl_vec_free(sample);
2868 else if (sample) {
2869 isl_vec_free(bmap1->sample);
2870 bmap1->sample = sample;
2873 bmap1 = isl_basic_map_simplify(bmap1);
2874 return isl_basic_map_finalize(bmap1);
2875 error:
2876 if (sample)
2877 isl_vec_free(sample);
2878 isl_basic_map_free(bmap1);
2879 isl_basic_map_free(bmap2);
2880 return NULL;
2883 struct isl_basic_set *isl_basic_set_intersect(
2884 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2886 return (struct isl_basic_set *)
2887 isl_basic_map_intersect(
2888 (struct isl_basic_map *)bset1,
2889 (struct isl_basic_map *)bset2);
2892 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2893 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2895 return isl_basic_set_intersect(bset1, bset2);
2898 /* Special case of isl_map_intersect, where both map1 and map2
2899 * are convex, without any divs and such that either map1 or map2
2900 * contains a single constraint. This constraint is then simply
2901 * added to the other map.
2903 static __isl_give isl_map *map_intersect_add_constraint(
2904 __isl_take isl_map *map1, __isl_take isl_map *map2)
2906 isl_assert(map1->ctx, map1->n == 1, goto error);
2907 isl_assert(map2->ctx, map1->n == 1, goto error);
2908 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2909 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2911 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2912 return isl_map_intersect(map2, map1);
2914 isl_assert(map2->ctx,
2915 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2917 map1 = isl_map_cow(map1);
2918 if (!map1)
2919 goto error;
2920 if (isl_map_plain_is_empty(map1)) {
2921 isl_map_free(map2);
2922 return map1;
2924 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2925 if (map2->p[0]->n_eq == 1)
2926 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2927 else
2928 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2929 map2->p[0]->ineq[0]);
2931 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2932 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2933 if (!map1->p[0])
2934 goto error;
2936 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2937 isl_basic_map_free(map1->p[0]);
2938 map1->n = 0;
2941 isl_map_free(map2);
2943 return map1;
2944 error:
2945 isl_map_free(map1);
2946 isl_map_free(map2);
2947 return NULL;
2950 /* map2 may be either a parameter domain or a map living in the same
2951 * space as map1.
2953 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2954 __isl_take isl_map *map2)
2956 unsigned flags = 0;
2957 isl_map *result;
2958 int i, j;
2960 if (!map1 || !map2)
2961 goto error;
2963 if ((isl_map_plain_is_empty(map1) ||
2964 isl_map_plain_is_universe(map2)) &&
2965 isl_space_is_equal(map1->dim, map2->dim)) {
2966 isl_map_free(map2);
2967 return map1;
2969 if ((isl_map_plain_is_empty(map2) ||
2970 isl_map_plain_is_universe(map1)) &&
2971 isl_space_is_equal(map1->dim, map2->dim)) {
2972 isl_map_free(map1);
2973 return map2;
2976 if (map1->n == 1 && map2->n == 1 &&
2977 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2978 isl_space_is_equal(map1->dim, map2->dim) &&
2979 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2980 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2981 return map_intersect_add_constraint(map1, map2);
2983 if (isl_space_dim(map2->dim, isl_dim_all) !=
2984 isl_space_dim(map2->dim, isl_dim_param))
2985 isl_assert(map1->ctx,
2986 isl_space_is_equal(map1->dim, map2->dim), goto error);
2988 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2989 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2990 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2992 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2993 map1->n * map2->n, flags);
2994 if (!result)
2995 goto error;
2996 for (i = 0; i < map1->n; ++i)
2997 for (j = 0; j < map2->n; ++j) {
2998 struct isl_basic_map *part;
2999 part = isl_basic_map_intersect(
3000 isl_basic_map_copy(map1->p[i]),
3001 isl_basic_map_copy(map2->p[j]));
3002 if (isl_basic_map_is_empty(part) < 0)
3003 part = isl_basic_map_free(part);
3004 result = isl_map_add_basic_map(result, part);
3005 if (!result)
3006 goto error;
3008 isl_map_free(map1);
3009 isl_map_free(map2);
3010 return result;
3011 error:
3012 isl_map_free(map1);
3013 isl_map_free(map2);
3014 return NULL;
3017 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3018 __isl_take isl_map *map2)
3020 if (!map1 || !map2)
3021 goto error;
3022 if (!isl_space_is_equal(map1->dim, map2->dim))
3023 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3024 "spaces don't match", goto error);
3025 return map_intersect_internal(map1, map2);
3026 error:
3027 isl_map_free(map1);
3028 isl_map_free(map2);
3029 return NULL;
3032 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3033 __isl_take isl_map *map2)
3035 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3038 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3040 return (struct isl_set *)
3041 isl_map_intersect((struct isl_map *)set1,
3042 (struct isl_map *)set2);
3045 /* map_intersect_internal accepts intersections
3046 * with parameter domains, so we can just call that function.
3048 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3049 __isl_take isl_set *params)
3051 return map_intersect_internal(map, params);
3054 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3055 __isl_take isl_map *map2)
3057 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3060 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3061 __isl_take isl_set *params)
3063 return isl_map_intersect_params(set, params);
3066 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3068 isl_space *dim;
3069 struct isl_basic_set *bset;
3070 unsigned in;
3072 if (!bmap)
3073 return NULL;
3074 bmap = isl_basic_map_cow(bmap);
3075 if (!bmap)
3076 return NULL;
3077 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3078 in = isl_basic_map_n_in(bmap);
3079 bset = isl_basic_set_from_basic_map(bmap);
3080 bset = isl_basic_set_swap_vars(bset, in);
3081 return isl_basic_map_from_basic_set(bset, dim);
3084 static __isl_give isl_basic_map *basic_map_space_reset(
3085 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3087 isl_space *space;
3089 if (!bmap)
3090 return NULL;
3091 if (!isl_space_is_named_or_nested(bmap->dim, type))
3092 return bmap;
3094 space = isl_basic_map_get_space(bmap);
3095 space = isl_space_reset(space, type);
3096 bmap = isl_basic_map_reset_space(bmap, space);
3097 return bmap;
3100 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3101 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3102 unsigned pos, unsigned n)
3104 isl_space *res_dim;
3105 struct isl_basic_map *res;
3106 struct isl_dim_map *dim_map;
3107 unsigned total, off;
3108 enum isl_dim_type t;
3110 if (n == 0)
3111 return basic_map_space_reset(bmap, type);
3113 if (!bmap)
3114 return NULL;
3116 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3118 total = isl_basic_map_total_dim(bmap) + n;
3119 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3120 off = 0;
3121 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3122 if (t != type) {
3123 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3124 } else {
3125 unsigned size = isl_basic_map_dim(bmap, t);
3126 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3127 0, pos, off);
3128 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3129 pos, size - pos, off + pos + n);
3131 off += isl_space_dim(res_dim, t);
3133 isl_dim_map_div(dim_map, bmap, off);
3135 res = isl_basic_map_alloc_space(res_dim,
3136 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3137 if (isl_basic_map_is_rational(bmap))
3138 res = isl_basic_map_set_rational(res);
3139 if (isl_basic_map_plain_is_empty(bmap)) {
3140 isl_basic_map_free(bmap);
3141 free(dim_map);
3142 return isl_basic_map_set_to_empty(res);
3144 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3145 return isl_basic_map_finalize(res);
3148 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3149 __isl_take isl_basic_set *bset,
3150 enum isl_dim_type type, unsigned pos, unsigned n)
3152 return isl_basic_map_insert_dims(bset, type, pos, n);
3155 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3156 enum isl_dim_type type, unsigned n)
3158 if (!bmap)
3159 return NULL;
3160 return isl_basic_map_insert_dims(bmap, type,
3161 isl_basic_map_dim(bmap, type), n);
3164 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3165 enum isl_dim_type type, unsigned n)
3167 if (!bset)
3168 return NULL;
3169 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3170 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3171 error:
3172 isl_basic_set_free(bset);
3173 return NULL;
3176 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3177 enum isl_dim_type type)
3179 isl_space *space;
3181 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3182 return map;
3184 space = isl_map_get_space(map);
3185 space = isl_space_reset(space, type);
3186 map = isl_map_reset_space(map, space);
3187 return map;
3190 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3191 enum isl_dim_type type, unsigned pos, unsigned n)
3193 int i;
3195 if (n == 0)
3196 return map_space_reset(map, type);
3198 map = isl_map_cow(map);
3199 if (!map)
3200 return NULL;
3202 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3203 if (!map->dim)
3204 goto error;
3206 for (i = 0; i < map->n; ++i) {
3207 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3208 if (!map->p[i])
3209 goto error;
3212 return map;
3213 error:
3214 isl_map_free(map);
3215 return NULL;
3218 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3219 enum isl_dim_type type, unsigned pos, unsigned n)
3221 return isl_map_insert_dims(set, type, pos, n);
3224 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3225 enum isl_dim_type type, unsigned n)
3227 if (!map)
3228 return NULL;
3229 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3232 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3233 enum isl_dim_type type, unsigned n)
3235 if (!set)
3236 return NULL;
3237 isl_assert(set->ctx, type != isl_dim_in, goto error);
3238 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3239 error:
3240 isl_set_free(set);
3241 return NULL;
3244 __isl_give isl_basic_map *isl_basic_map_move_dims(
3245 __isl_take isl_basic_map *bmap,
3246 enum isl_dim_type dst_type, unsigned dst_pos,
3247 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3249 struct isl_dim_map *dim_map;
3250 struct isl_basic_map *res;
3251 enum isl_dim_type t;
3252 unsigned total, off;
3254 if (!bmap)
3255 return NULL;
3256 if (n == 0)
3257 return bmap;
3259 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3260 goto error);
3262 if (dst_type == src_type && dst_pos == src_pos)
3263 return bmap;
3265 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3267 if (pos(bmap->dim, dst_type) + dst_pos ==
3268 pos(bmap->dim, src_type) + src_pos +
3269 ((src_type < dst_type) ? n : 0)) {
3270 bmap = isl_basic_map_cow(bmap);
3271 if (!bmap)
3272 return NULL;
3274 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3275 src_type, src_pos, n);
3276 if (!bmap->dim)
3277 goto error;
3279 bmap = isl_basic_map_finalize(bmap);
3281 return bmap;
3284 total = isl_basic_map_total_dim(bmap);
3285 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3287 off = 0;
3288 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3289 unsigned size = isl_space_dim(bmap->dim, t);
3290 if (t == dst_type) {
3291 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3292 0, dst_pos, off);
3293 off += dst_pos;
3294 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3295 src_pos, n, off);
3296 off += n;
3297 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3298 dst_pos, size - dst_pos, off);
3299 off += size - dst_pos;
3300 } else if (t == src_type) {
3301 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3302 0, src_pos, off);
3303 off += src_pos;
3304 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3305 src_pos + n, size - src_pos - n, off);
3306 off += size - src_pos - n;
3307 } else {
3308 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3309 off += size;
3312 isl_dim_map_div(dim_map, bmap, off);
3314 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3315 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3316 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3317 if (!bmap)
3318 goto error;
3320 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3321 src_type, src_pos, n);
3322 if (!bmap->dim)
3323 goto error;
3325 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3326 bmap = isl_basic_map_gauss(bmap, NULL);
3327 bmap = isl_basic_map_finalize(bmap);
3329 return bmap;
3330 error:
3331 isl_basic_map_free(bmap);
3332 return NULL;
3335 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3336 enum isl_dim_type dst_type, unsigned dst_pos,
3337 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3339 return (isl_basic_set *)isl_basic_map_move_dims(
3340 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3343 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3344 enum isl_dim_type dst_type, unsigned dst_pos,
3345 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3347 if (!set)
3348 return NULL;
3349 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3350 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3351 src_type, src_pos, n);
3352 error:
3353 isl_set_free(set);
3354 return NULL;
3357 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3358 enum isl_dim_type dst_type, unsigned dst_pos,
3359 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3361 int i;
3363 if (!map)
3364 return NULL;
3365 if (n == 0)
3366 return map;
3368 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3369 goto error);
3371 if (dst_type == src_type && dst_pos == src_pos)
3372 return map;
3374 isl_assert(map->ctx, dst_type != src_type, goto error);
3376 map = isl_map_cow(map);
3377 if (!map)
3378 return NULL;
3380 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3381 if (!map->dim)
3382 goto error;
3384 for (i = 0; i < map->n; ++i) {
3385 map->p[i] = isl_basic_map_move_dims(map->p[i],
3386 dst_type, dst_pos,
3387 src_type, src_pos, n);
3388 if (!map->p[i])
3389 goto error;
3392 return map;
3393 error:
3394 isl_map_free(map);
3395 return NULL;
3398 /* Move the specified dimensions to the last columns right before
3399 * the divs. Don't change the dimension specification of bmap.
3400 * That's the responsibility of the caller.
3402 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3403 enum isl_dim_type type, unsigned first, unsigned n)
3405 struct isl_dim_map *dim_map;
3406 struct isl_basic_map *res;
3407 enum isl_dim_type t;
3408 unsigned total, off;
3410 if (!bmap)
3411 return NULL;
3412 if (pos(bmap->dim, type) + first + n ==
3413 1 + isl_space_dim(bmap->dim, isl_dim_all))
3414 return bmap;
3416 total = isl_basic_map_total_dim(bmap);
3417 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3419 off = 0;
3420 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3421 unsigned size = isl_space_dim(bmap->dim, t);
3422 if (t == type) {
3423 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3424 0, first, off);
3425 off += first;
3426 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3427 first, n, total - bmap->n_div - n);
3428 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3429 first + n, size - (first + n), off);
3430 off += size - (first + n);
3431 } else {
3432 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3433 off += size;
3436 isl_dim_map_div(dim_map, bmap, off + n);
3438 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3439 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3440 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3441 return res;
3444 /* Insert "n" rows in the divs of "bmap".
3446 * The number of columns is not changed, which means that the last
3447 * dimensions of "bmap" are being reintepreted as the new divs.
3448 * The space of "bmap" is not adjusted, however, which means
3449 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3450 * from the space of "bmap" is the responsibility of the caller.
3452 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3453 int n)
3455 int i;
3456 size_t row_size;
3457 isl_int **new_div;
3458 isl_int *old;
3460 bmap = isl_basic_map_cow(bmap);
3461 if (!bmap)
3462 return NULL;
3464 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3465 old = bmap->block2.data;
3466 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3467 (bmap->extra + n) * (1 + row_size));
3468 if (!bmap->block2.data)
3469 return isl_basic_map_free(bmap);
3470 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3471 if (!new_div)
3472 return isl_basic_map_free(bmap);
3473 for (i = 0; i < n; ++i) {
3474 new_div[i] = bmap->block2.data +
3475 (bmap->extra + i) * (1 + row_size);
3476 isl_seq_clr(new_div[i], 1 + row_size);
3478 for (i = 0; i < bmap->extra; ++i)
3479 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3480 free(bmap->div);
3481 bmap->div = new_div;
3482 bmap->n_div += n;
3483 bmap->extra += n;
3485 return bmap;
3488 /* Turn the n dimensions of type type, starting at first
3489 * into existentially quantified variables.
3491 __isl_give isl_basic_map *isl_basic_map_project_out(
3492 __isl_take isl_basic_map *bmap,
3493 enum isl_dim_type type, unsigned first, unsigned n)
3495 if (n == 0)
3496 return basic_map_space_reset(bmap, type);
3498 if (!bmap)
3499 return NULL;
3501 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3502 return isl_basic_map_remove_dims(bmap, type, first, n);
3504 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3505 goto error);
3507 bmap = move_last(bmap, type, first, n);
3508 bmap = isl_basic_map_cow(bmap);
3509 bmap = insert_div_rows(bmap, n);
3510 if (!bmap)
3511 return NULL;
3513 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3514 if (!bmap->dim)
3515 goto error;
3516 bmap = isl_basic_map_simplify(bmap);
3517 bmap = isl_basic_map_drop_redundant_divs(bmap);
3518 return isl_basic_map_finalize(bmap);
3519 error:
3520 isl_basic_map_free(bmap);
3521 return NULL;
3524 /* Turn the n dimensions of type type, starting at first
3525 * into existentially quantified variables.
3527 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3528 enum isl_dim_type type, unsigned first, unsigned n)
3530 return (isl_basic_set *)isl_basic_map_project_out(
3531 (isl_basic_map *)bset, type, first, n);
3534 /* Turn the n dimensions of type type, starting at first
3535 * into existentially quantified variables.
3537 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3538 enum isl_dim_type type, unsigned first, unsigned n)
3540 int i;
3542 if (!map)
3543 return NULL;
3545 if (n == 0)
3546 return map_space_reset(map, type);
3548 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3550 map = isl_map_cow(map);
3551 if (!map)
3552 return NULL;
3554 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3555 if (!map->dim)
3556 goto error;
3558 for (i = 0; i < map->n; ++i) {
3559 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3560 if (!map->p[i])
3561 goto error;
3564 return map;
3565 error:
3566 isl_map_free(map);
3567 return NULL;
3570 /* Turn the n dimensions of type type, starting at first
3571 * into existentially quantified variables.
3573 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3574 enum isl_dim_type type, unsigned first, unsigned n)
3576 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3579 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3581 int i, j;
3583 for (i = 0; i < n; ++i) {
3584 j = isl_basic_map_alloc_div(bmap);
3585 if (j < 0)
3586 goto error;
3587 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3589 return bmap;
3590 error:
3591 isl_basic_map_free(bmap);
3592 return NULL;
3595 struct isl_basic_map *isl_basic_map_apply_range(
3596 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3598 isl_space *dim_result = NULL;
3599 struct isl_basic_map *bmap;
3600 unsigned n_in, n_out, n, nparam, total, pos;
3601 struct isl_dim_map *dim_map1, *dim_map2;
3603 if (!bmap1 || !bmap2)
3604 goto error;
3605 if (!isl_space_match(bmap1->dim, isl_dim_param,
3606 bmap2->dim, isl_dim_param))
3607 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3608 "parameters don't match", goto error);
3609 if (!isl_space_tuple_match(bmap1->dim, isl_dim_out,
3610 bmap2->dim, isl_dim_in))
3611 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3612 "spaces don't match", goto error);
3614 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3615 isl_space_copy(bmap2->dim));
3617 n_in = isl_basic_map_n_in(bmap1);
3618 n_out = isl_basic_map_n_out(bmap2);
3619 n = isl_basic_map_n_out(bmap1);
3620 nparam = isl_basic_map_n_param(bmap1);
3622 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3623 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3624 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3625 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3626 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3627 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3628 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3629 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3630 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3631 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3632 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3634 bmap = isl_basic_map_alloc_space(dim_result,
3635 bmap1->n_div + bmap2->n_div + n,
3636 bmap1->n_eq + bmap2->n_eq,
3637 bmap1->n_ineq + bmap2->n_ineq);
3638 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3639 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3640 bmap = add_divs(bmap, n);
3641 bmap = isl_basic_map_simplify(bmap);
3642 bmap = isl_basic_map_drop_redundant_divs(bmap);
3643 return isl_basic_map_finalize(bmap);
3644 error:
3645 isl_basic_map_free(bmap1);
3646 isl_basic_map_free(bmap2);
3647 return NULL;
3650 struct isl_basic_set *isl_basic_set_apply(
3651 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3653 if (!bset || !bmap)
3654 goto error;
3656 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3657 goto error);
3659 return (struct isl_basic_set *)
3660 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3661 error:
3662 isl_basic_set_free(bset);
3663 isl_basic_map_free(bmap);
3664 return NULL;
3667 struct isl_basic_map *isl_basic_map_apply_domain(
3668 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3670 if (!bmap1 || !bmap2)
3671 goto error;
3673 isl_assert(bmap1->ctx,
3674 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3675 isl_assert(bmap1->ctx,
3676 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3677 goto error);
3679 bmap1 = isl_basic_map_reverse(bmap1);
3680 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3681 return isl_basic_map_reverse(bmap1);
3682 error:
3683 isl_basic_map_free(bmap1);
3684 isl_basic_map_free(bmap2);
3685 return NULL;
3688 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3689 * A \cap B -> f(A) + f(B)
3691 struct isl_basic_map *isl_basic_map_sum(
3692 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3694 unsigned n_in, n_out, nparam, total, pos;
3695 struct isl_basic_map *bmap = NULL;
3696 struct isl_dim_map *dim_map1, *dim_map2;
3697 int i;
3699 if (!bmap1 || !bmap2)
3700 goto error;
3702 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3703 goto error);
3705 nparam = isl_basic_map_n_param(bmap1);
3706 n_in = isl_basic_map_n_in(bmap1);
3707 n_out = isl_basic_map_n_out(bmap1);
3709 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3710 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3711 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3712 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3713 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3714 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3715 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3716 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3717 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3718 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3719 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3721 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3722 bmap1->n_div + bmap2->n_div + 2 * n_out,
3723 bmap1->n_eq + bmap2->n_eq + n_out,
3724 bmap1->n_ineq + bmap2->n_ineq);
3725 for (i = 0; i < n_out; ++i) {
3726 int j = isl_basic_map_alloc_equality(bmap);
3727 if (j < 0)
3728 goto error;
3729 isl_seq_clr(bmap->eq[j], 1+total);
3730 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3731 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3732 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3734 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3735 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3736 bmap = add_divs(bmap, 2 * n_out);
3738 bmap = isl_basic_map_simplify(bmap);
3739 return isl_basic_map_finalize(bmap);
3740 error:
3741 isl_basic_map_free(bmap);
3742 isl_basic_map_free(bmap1);
3743 isl_basic_map_free(bmap2);
3744 return NULL;
3747 /* Given two maps A -> f(A) and B -> g(B), construct a map
3748 * A \cap B -> f(A) + f(B)
3750 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3752 struct isl_map *result;
3753 int i, j;
3755 if (!map1 || !map2)
3756 goto error;
3758 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3760 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3761 map1->n * map2->n, 0);
3762 if (!result)
3763 goto error;
3764 for (i = 0; i < map1->n; ++i)
3765 for (j = 0; j < map2->n; ++j) {
3766 struct isl_basic_map *part;
3767 part = isl_basic_map_sum(
3768 isl_basic_map_copy(map1->p[i]),
3769 isl_basic_map_copy(map2->p[j]));
3770 if (isl_basic_map_is_empty(part))
3771 isl_basic_map_free(part);
3772 else
3773 result = isl_map_add_basic_map(result, part);
3774 if (!result)
3775 goto error;
3777 isl_map_free(map1);
3778 isl_map_free(map2);
3779 return result;
3780 error:
3781 isl_map_free(map1);
3782 isl_map_free(map2);
3783 return NULL;
3786 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3787 __isl_take isl_set *set2)
3789 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3792 /* Given a basic map A -> f(A), construct A -> -f(A).
3794 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3796 int i, j;
3797 unsigned off, n;
3799 bmap = isl_basic_map_cow(bmap);
3800 if (!bmap)
3801 return NULL;
3803 n = isl_basic_map_dim(bmap, isl_dim_out);
3804 off = isl_basic_map_offset(bmap, isl_dim_out);
3805 for (i = 0; i < bmap->n_eq; ++i)
3806 for (j = 0; j < n; ++j)
3807 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3808 for (i = 0; i < bmap->n_ineq; ++i)
3809 for (j = 0; j < n; ++j)
3810 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3811 for (i = 0; i < bmap->n_div; ++i)
3812 for (j = 0; j < n; ++j)
3813 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3814 bmap = isl_basic_map_gauss(bmap, NULL);
3815 return isl_basic_map_finalize(bmap);
3818 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3820 return isl_basic_map_neg(bset);
3823 /* Given a map A -> f(A), construct A -> -f(A).
3825 struct isl_map *isl_map_neg(struct isl_map *map)
3827 int i;
3829 map = isl_map_cow(map);
3830 if (!map)
3831 return NULL;
3833 for (i = 0; i < map->n; ++i) {
3834 map->p[i] = isl_basic_map_neg(map->p[i]);
3835 if (!map->p[i])
3836 goto error;
3839 return map;
3840 error:
3841 isl_map_free(map);
3842 return NULL;
3845 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3847 return (isl_set *)isl_map_neg((isl_map *)set);
3850 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3851 * A -> floor(f(A)/d).
3853 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3854 isl_int d)
3856 unsigned n_in, n_out, nparam, total, pos;
3857 struct isl_basic_map *result = NULL;
3858 struct isl_dim_map *dim_map;
3859 int i;
3861 if (!bmap)
3862 return NULL;
3864 nparam = isl_basic_map_n_param(bmap);
3865 n_in = isl_basic_map_n_in(bmap);
3866 n_out = isl_basic_map_n_out(bmap);
3868 total = nparam + n_in + n_out + bmap->n_div + n_out;
3869 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3870 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3871 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3872 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3873 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3875 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3876 bmap->n_div + n_out,
3877 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3878 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3879 result = add_divs(result, n_out);
3880 for (i = 0; i < n_out; ++i) {
3881 int j;
3882 j = isl_basic_map_alloc_inequality(result);
3883 if (j < 0)
3884 goto error;
3885 isl_seq_clr(result->ineq[j], 1+total);
3886 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3887 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3888 j = isl_basic_map_alloc_inequality(result);
3889 if (j < 0)
3890 goto error;
3891 isl_seq_clr(result->ineq[j], 1+total);
3892 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3893 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3894 isl_int_sub_ui(result->ineq[j][0], d, 1);
3897 result = isl_basic_map_simplify(result);
3898 return isl_basic_map_finalize(result);
3899 error:
3900 isl_basic_map_free(result);
3901 return NULL;
3904 /* Given a map A -> f(A) and an integer d, construct a map
3905 * A -> floor(f(A)/d).
3907 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3909 int i;
3911 map = isl_map_cow(map);
3912 if (!map)
3913 return NULL;
3915 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3916 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3917 for (i = 0; i < map->n; ++i) {
3918 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3919 if (!map->p[i])
3920 goto error;
3923 return map;
3924 error:
3925 isl_map_free(map);
3926 return NULL;
3929 /* Given a map A -> f(A) and an integer d, construct a map
3930 * A -> floor(f(A)/d).
3932 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3933 __isl_take isl_val *d)
3935 if (!map || !d)
3936 goto error;
3937 if (!isl_val_is_int(d))
3938 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3939 "expecting integer denominator", goto error);
3940 map = isl_map_floordiv(map, d->n);
3941 isl_val_free(d);
3942 return map;
3943 error:
3944 isl_map_free(map);
3945 isl_val_free(d);
3946 return NULL;
3949 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3951 int i;
3952 unsigned nparam;
3953 unsigned n_in;
3955 i = isl_basic_map_alloc_equality(bmap);
3956 if (i < 0)
3957 goto error;
3958 nparam = isl_basic_map_n_param(bmap);
3959 n_in = isl_basic_map_n_in(bmap);
3960 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3961 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3962 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3963 return isl_basic_map_finalize(bmap);
3964 error:
3965 isl_basic_map_free(bmap);
3966 return NULL;
3969 /* Add a constraints to "bmap" expressing i_pos < o_pos
3971 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3973 int i;
3974 unsigned nparam;
3975 unsigned n_in;
3977 i = isl_basic_map_alloc_inequality(bmap);
3978 if (i < 0)
3979 goto error;
3980 nparam = isl_basic_map_n_param(bmap);
3981 n_in = isl_basic_map_n_in(bmap);
3982 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3983 isl_int_set_si(bmap->ineq[i][0], -1);
3984 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3985 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3986 return isl_basic_map_finalize(bmap);
3987 error:
3988 isl_basic_map_free(bmap);
3989 return NULL;
3992 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3994 static __isl_give isl_basic_map *var_less_or_equal(
3995 __isl_take isl_basic_map *bmap, unsigned pos)
3997 int i;
3998 unsigned nparam;
3999 unsigned n_in;
4001 i = isl_basic_map_alloc_inequality(bmap);
4002 if (i < 0)
4003 goto error;
4004 nparam = isl_basic_map_n_param(bmap);
4005 n_in = isl_basic_map_n_in(bmap);
4006 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4007 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4008 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4009 return isl_basic_map_finalize(bmap);
4010 error:
4011 isl_basic_map_free(bmap);
4012 return NULL;
4015 /* Add a constraints to "bmap" expressing i_pos > o_pos
4017 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4019 int i;
4020 unsigned nparam;
4021 unsigned n_in;
4023 i = isl_basic_map_alloc_inequality(bmap);
4024 if (i < 0)
4025 goto error;
4026 nparam = isl_basic_map_n_param(bmap);
4027 n_in = isl_basic_map_n_in(bmap);
4028 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4029 isl_int_set_si(bmap->ineq[i][0], -1);
4030 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4031 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4032 return isl_basic_map_finalize(bmap);
4033 error:
4034 isl_basic_map_free(bmap);
4035 return NULL;
4038 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4040 static __isl_give isl_basic_map *var_more_or_equal(
4041 __isl_take isl_basic_map *bmap, unsigned pos)
4043 int i;
4044 unsigned nparam;
4045 unsigned n_in;
4047 i = isl_basic_map_alloc_inequality(bmap);
4048 if (i < 0)
4049 goto error;
4050 nparam = isl_basic_map_n_param(bmap);
4051 n_in = isl_basic_map_n_in(bmap);
4052 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4053 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4054 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4055 return isl_basic_map_finalize(bmap);
4056 error:
4057 isl_basic_map_free(bmap);
4058 return NULL;
4061 __isl_give isl_basic_map *isl_basic_map_equal(
4062 __isl_take isl_space *dim, unsigned n_equal)
4064 int i;
4065 struct isl_basic_map *bmap;
4066 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4067 if (!bmap)
4068 return NULL;
4069 for (i = 0; i < n_equal && bmap; ++i)
4070 bmap = var_equal(bmap, i);
4071 return isl_basic_map_finalize(bmap);
4074 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4076 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4077 unsigned pos)
4079 int i;
4080 struct isl_basic_map *bmap;
4081 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4082 if (!bmap)
4083 return NULL;
4084 for (i = 0; i < pos && bmap; ++i)
4085 bmap = var_equal(bmap, i);
4086 if (bmap)
4087 bmap = var_less(bmap, pos);
4088 return isl_basic_map_finalize(bmap);
4091 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4093 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4094 __isl_take isl_space *dim, unsigned pos)
4096 int i;
4097 isl_basic_map *bmap;
4099 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4100 for (i = 0; i < pos; ++i)
4101 bmap = var_equal(bmap, i);
4102 bmap = var_less_or_equal(bmap, pos);
4103 return isl_basic_map_finalize(bmap);
4106 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4108 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4109 unsigned pos)
4111 int i;
4112 struct isl_basic_map *bmap;
4113 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4114 if (!bmap)
4115 return NULL;
4116 for (i = 0; i < pos && bmap; ++i)
4117 bmap = var_equal(bmap, i);
4118 if (bmap)
4119 bmap = var_more(bmap, pos);
4120 return isl_basic_map_finalize(bmap);
4123 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4125 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4126 __isl_take isl_space *dim, unsigned pos)
4128 int i;
4129 isl_basic_map *bmap;
4131 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4132 for (i = 0; i < pos; ++i)
4133 bmap = var_equal(bmap, i);
4134 bmap = var_more_or_equal(bmap, pos);
4135 return isl_basic_map_finalize(bmap);
4138 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4139 unsigned n, int equal)
4141 struct isl_map *map;
4142 int i;
4144 if (n == 0 && equal)
4145 return isl_map_universe(dims);
4147 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4149 for (i = 0; i + 1 < n; ++i)
4150 map = isl_map_add_basic_map(map,
4151 isl_basic_map_less_at(isl_space_copy(dims), i));
4152 if (n > 0) {
4153 if (equal)
4154 map = isl_map_add_basic_map(map,
4155 isl_basic_map_less_or_equal_at(dims, n - 1));
4156 else
4157 map = isl_map_add_basic_map(map,
4158 isl_basic_map_less_at(dims, n - 1));
4159 } else
4160 isl_space_free(dims);
4162 return map;
4165 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4167 if (!dims)
4168 return NULL;
4169 return map_lex_lte_first(dims, dims->n_out, equal);
4172 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4174 return map_lex_lte_first(dim, n, 0);
4177 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4179 return map_lex_lte_first(dim, n, 1);
4182 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4184 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4187 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4189 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4192 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4193 unsigned n, int equal)
4195 struct isl_map *map;
4196 int i;
4198 if (n == 0 && equal)
4199 return isl_map_universe(dims);
4201 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4203 for (i = 0; i + 1 < n; ++i)
4204 map = isl_map_add_basic_map(map,
4205 isl_basic_map_more_at(isl_space_copy(dims), i));
4206 if (n > 0) {
4207 if (equal)
4208 map = isl_map_add_basic_map(map,
4209 isl_basic_map_more_or_equal_at(dims, n - 1));
4210 else
4211 map = isl_map_add_basic_map(map,
4212 isl_basic_map_more_at(dims, n - 1));
4213 } else
4214 isl_space_free(dims);
4216 return map;
4219 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4221 if (!dims)
4222 return NULL;
4223 return map_lex_gte_first(dims, dims->n_out, equal);
4226 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4228 return map_lex_gte_first(dim, n, 0);
4231 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4233 return map_lex_gte_first(dim, n, 1);
4236 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4238 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4241 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4243 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4246 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4247 __isl_take isl_set *set2)
4249 isl_map *map;
4250 map = isl_map_lex_le(isl_set_get_space(set1));
4251 map = isl_map_intersect_domain(map, set1);
4252 map = isl_map_intersect_range(map, set2);
4253 return map;
4256 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4257 __isl_take isl_set *set2)
4259 isl_map *map;
4260 map = isl_map_lex_lt(isl_set_get_space(set1));
4261 map = isl_map_intersect_domain(map, set1);
4262 map = isl_map_intersect_range(map, set2);
4263 return map;
4266 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4267 __isl_take isl_set *set2)
4269 isl_map *map;
4270 map = isl_map_lex_ge(isl_set_get_space(set1));
4271 map = isl_map_intersect_domain(map, set1);
4272 map = isl_map_intersect_range(map, set2);
4273 return map;
4276 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4277 __isl_take isl_set *set2)
4279 isl_map *map;
4280 map = isl_map_lex_gt(isl_set_get_space(set1));
4281 map = isl_map_intersect_domain(map, set1);
4282 map = isl_map_intersect_range(map, set2);
4283 return map;
4286 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4287 __isl_take isl_map *map2)
4289 isl_map *map;
4290 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4291 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4292 map = isl_map_apply_range(map, isl_map_reverse(map2));
4293 return map;
4296 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4297 __isl_take isl_map *map2)
4299 isl_map *map;
4300 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4301 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4302 map = isl_map_apply_range(map, isl_map_reverse(map2));
4303 return map;
4306 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4307 __isl_take isl_map *map2)
4309 isl_map *map;
4310 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4311 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4312 map = isl_map_apply_range(map, isl_map_reverse(map2));
4313 return map;
4316 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4317 __isl_take isl_map *map2)
4319 isl_map *map;
4320 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4321 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4322 map = isl_map_apply_range(map, isl_map_reverse(map2));
4323 return map;
4326 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4327 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4329 struct isl_basic_map *bmap;
4331 bset = isl_basic_set_cow(bset);
4332 if (!bset || !dim)
4333 goto error;
4335 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4336 isl_space_free(bset->dim);
4337 bmap = (struct isl_basic_map *) bset;
4338 bmap->dim = dim;
4339 return isl_basic_map_finalize(bmap);
4340 error:
4341 isl_basic_set_free(bset);
4342 isl_space_free(dim);
4343 return NULL;
4346 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4348 if (!bmap)
4349 goto error;
4350 if (bmap->dim->n_in == 0)
4351 return (struct isl_basic_set *)bmap;
4352 bmap = isl_basic_map_cow(bmap);
4353 if (!bmap)
4354 goto error;
4355 bmap->dim = isl_space_as_set_space(bmap->dim);
4356 if (!bmap->dim)
4357 goto error;
4358 bmap = isl_basic_map_finalize(bmap);
4359 return (struct isl_basic_set *)bmap;
4360 error:
4361 isl_basic_map_free(bmap);
4362 return NULL;
4365 /* For a div d = floor(f/m), add the constraint
4367 * f - m d >= 0
4369 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4370 unsigned pos, isl_int *div)
4372 int i;
4373 unsigned total = isl_basic_map_total_dim(bmap);
4375 i = isl_basic_map_alloc_inequality(bmap);
4376 if (i < 0)
4377 return -1;
4378 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4379 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4381 return 0;
4384 /* For a div d = floor(f/m), add the constraint
4386 * -(f-(n-1)) + m d >= 0
4388 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4389 unsigned pos, isl_int *div)
4391 int i;
4392 unsigned total = isl_basic_map_total_dim(bmap);
4394 i = isl_basic_map_alloc_inequality(bmap);
4395 if (i < 0)
4396 return -1;
4397 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4398 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4399 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4400 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4402 return 0;
4405 /* For a div d = floor(f/m), add the constraints
4407 * f - m d >= 0
4408 * -(f-(n-1)) + m d >= 0
4410 * Note that the second constraint is the negation of
4412 * f - m d >= n
4414 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4415 unsigned pos, isl_int *div)
4417 if (add_upper_div_constraint(bmap, pos, div) < 0)
4418 return -1;
4419 if (add_lower_div_constraint(bmap, pos, div) < 0)
4420 return -1;
4421 return 0;
4424 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4425 unsigned pos, isl_int *div)
4427 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4428 pos, div);
4431 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4433 unsigned total = isl_basic_map_total_dim(bmap);
4434 unsigned div_pos = total - bmap->n_div + div;
4436 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4437 bmap->div[div]);
4440 /* For each known div d = floor(f/m), add the constraints
4442 * f - m d >= 0
4443 * -(f-(n-1)) + m d >= 0
4445 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4446 __isl_take isl_basic_map *bmap)
4448 int i;
4449 unsigned n_div;
4451 if (!bmap)
4452 return NULL;
4453 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4454 if (n_div == 0)
4455 return bmap;
4456 bmap = isl_basic_map_cow(bmap);
4457 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
4458 if (!bmap)
4459 return NULL;
4460 for (i = 0; i < n_div; ++i) {
4461 if (isl_int_is_zero(bmap->div[i][0]))
4462 continue;
4463 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4464 return isl_basic_map_free(bmap);
4467 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4468 bmap = isl_basic_map_finalize(bmap);
4469 return bmap;
4472 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4474 * In particular, if this div is of the form d = floor(f/m),
4475 * then add the constraint
4477 * f - m d >= 0
4479 * if sign < 0 or the constraint
4481 * -(f-(n-1)) + m d >= 0
4483 * if sign > 0.
4485 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4486 unsigned div, int sign)
4488 unsigned total;
4489 unsigned div_pos;
4491 if (!bmap)
4492 return -1;
4494 total = isl_basic_map_total_dim(bmap);
4495 div_pos = total - bmap->n_div + div;
4497 if (sign < 0)
4498 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4499 else
4500 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4503 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4505 return isl_basic_map_add_div_constraints(bset, div);
4508 struct isl_basic_set *isl_basic_map_underlying_set(
4509 struct isl_basic_map *bmap)
4511 if (!bmap)
4512 goto error;
4513 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4514 bmap->n_div == 0 &&
4515 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4516 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4517 return (struct isl_basic_set *)bmap;
4518 bmap = isl_basic_map_cow(bmap);
4519 if (!bmap)
4520 goto error;
4521 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4522 if (!bmap->dim)
4523 goto error;
4524 bmap->extra -= bmap->n_div;
4525 bmap->n_div = 0;
4526 bmap = isl_basic_map_finalize(bmap);
4527 return (struct isl_basic_set *)bmap;
4528 error:
4529 isl_basic_map_free(bmap);
4530 return NULL;
4533 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4534 __isl_take isl_basic_set *bset)
4536 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4539 /* Replace each element in "list" by the result of applying
4540 * isl_basic_set_underlying_set to the element.
4542 __isl_give isl_basic_set_list *isl_basic_set_list_underlying_set(
4543 __isl_take isl_basic_set_list *list)
4545 int i, n;
4547 if (!list)
4548 return NULL;
4550 n = isl_basic_set_list_n_basic_set(list);
4551 for (i = 0; i < n; ++i) {
4552 isl_basic_set *bset;
4554 bset = isl_basic_set_list_get_basic_set(list, i);
4555 bset = isl_basic_set_underlying_set(bset);
4556 list = isl_basic_set_list_set_basic_set(list, i, bset);
4559 return list;
4562 struct isl_basic_map *isl_basic_map_overlying_set(
4563 struct isl_basic_set *bset, struct isl_basic_map *like)
4565 struct isl_basic_map *bmap;
4566 struct isl_ctx *ctx;
4567 unsigned total;
4568 int i;
4570 if (!bset || !like)
4571 goto error;
4572 ctx = bset->ctx;
4573 isl_assert(ctx, bset->n_div == 0, goto error);
4574 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4575 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4576 goto error);
4577 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4578 isl_basic_map_free(like);
4579 return (struct isl_basic_map *)bset;
4581 bset = isl_basic_set_cow(bset);
4582 if (!bset)
4583 goto error;
4584 total = bset->dim->n_out + bset->extra;
4585 bmap = (struct isl_basic_map *)bset;
4586 isl_space_free(bmap->dim);
4587 bmap->dim = isl_space_copy(like->dim);
4588 if (!bmap->dim)
4589 goto error;
4590 bmap->n_div = like->n_div;
4591 bmap->extra += like->n_div;
4592 if (bmap->extra) {
4593 unsigned ltotal;
4594 isl_int **div;
4595 ltotal = total - bmap->extra + like->extra;
4596 if (ltotal > total)
4597 ltotal = total;
4598 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4599 bmap->extra * (1 + 1 + total));
4600 if (isl_blk_is_error(bmap->block2))
4601 goto error;
4602 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4603 if (!div)
4604 goto error;
4605 bmap->div = div;
4606 for (i = 0; i < bmap->extra; ++i)
4607 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4608 for (i = 0; i < like->n_div; ++i) {
4609 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4610 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4612 bmap = isl_basic_map_add_known_div_constraints(bmap);
4614 isl_basic_map_free(like);
4615 bmap = isl_basic_map_simplify(bmap);
4616 bmap = isl_basic_map_finalize(bmap);
4617 return bmap;
4618 error:
4619 isl_basic_map_free(like);
4620 isl_basic_set_free(bset);
4621 return NULL;
4624 struct isl_basic_set *isl_basic_set_from_underlying_set(
4625 struct isl_basic_set *bset, struct isl_basic_set *like)
4627 return (struct isl_basic_set *)
4628 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4631 struct isl_set *isl_set_from_underlying_set(
4632 struct isl_set *set, struct isl_basic_set *like)
4634 int i;
4636 if (!set || !like)
4637 goto error;
4638 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4639 goto error);
4640 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4641 isl_basic_set_free(like);
4642 return set;
4644 set = isl_set_cow(set);
4645 if (!set)
4646 goto error;
4647 for (i = 0; i < set->n; ++i) {
4648 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4649 isl_basic_set_copy(like));
4650 if (!set->p[i])
4651 goto error;
4653 isl_space_free(set->dim);
4654 set->dim = isl_space_copy(like->dim);
4655 if (!set->dim)
4656 goto error;
4657 isl_basic_set_free(like);
4658 return set;
4659 error:
4660 isl_basic_set_free(like);
4661 isl_set_free(set);
4662 return NULL;
4665 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4667 int i;
4669 map = isl_map_cow(map);
4670 if (!map)
4671 return NULL;
4672 map->dim = isl_space_cow(map->dim);
4673 if (!map->dim)
4674 goto error;
4676 for (i = 1; i < map->n; ++i)
4677 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4678 goto error);
4679 for (i = 0; i < map->n; ++i) {
4680 map->p[i] = (struct isl_basic_map *)
4681 isl_basic_map_underlying_set(map->p[i]);
4682 if (!map->p[i])
4683 goto error;
4685 if (map->n == 0)
4686 map->dim = isl_space_underlying(map->dim, 0);
4687 else {
4688 isl_space_free(map->dim);
4689 map->dim = isl_space_copy(map->p[0]->dim);
4691 if (!map->dim)
4692 goto error;
4693 return (struct isl_set *)map;
4694 error:
4695 isl_map_free(map);
4696 return NULL;
4699 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4701 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4704 __isl_give isl_basic_map *isl_basic_map_reset_space(
4705 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4707 bmap = isl_basic_map_cow(bmap);
4708 if (!bmap || !dim)
4709 goto error;
4711 isl_space_free(bmap->dim);
4712 bmap->dim = dim;
4714 bmap = isl_basic_map_finalize(bmap);
4716 return bmap;
4717 error:
4718 isl_basic_map_free(bmap);
4719 isl_space_free(dim);
4720 return NULL;
4723 __isl_give isl_basic_set *isl_basic_set_reset_space(
4724 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4726 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4727 dim);
4730 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4731 __isl_take isl_space *dim)
4733 int i;
4735 map = isl_map_cow(map);
4736 if (!map || !dim)
4737 goto error;
4739 for (i = 0; i < map->n; ++i) {
4740 map->p[i] = isl_basic_map_reset_space(map->p[i],
4741 isl_space_copy(dim));
4742 if (!map->p[i])
4743 goto error;
4745 isl_space_free(map->dim);
4746 map->dim = dim;
4748 return map;
4749 error:
4750 isl_map_free(map);
4751 isl_space_free(dim);
4752 return NULL;
4755 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4756 __isl_take isl_space *dim)
4758 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4761 /* Compute the parameter domain of the given basic set.
4763 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4765 isl_space *space;
4766 unsigned n;
4768 if (isl_basic_set_is_params(bset))
4769 return bset;
4771 n = isl_basic_set_dim(bset, isl_dim_set);
4772 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4773 space = isl_basic_set_get_space(bset);
4774 space = isl_space_params(space);
4775 bset = isl_basic_set_reset_space(bset, space);
4776 return bset;
4779 /* Construct a zero-dimensional basic set with the given parameter domain.
4781 __isl_give isl_basic_set *isl_basic_set_from_params(
4782 __isl_take isl_basic_set *bset)
4784 isl_space *space;
4785 space = isl_basic_set_get_space(bset);
4786 space = isl_space_set_from_params(space);
4787 bset = isl_basic_set_reset_space(bset, space);
4788 return bset;
4791 /* Compute the parameter domain of the given set.
4793 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4795 isl_space *space;
4796 unsigned n;
4798 if (isl_set_is_params(set))
4799 return set;
4801 n = isl_set_dim(set, isl_dim_set);
4802 set = isl_set_project_out(set, isl_dim_set, 0, n);
4803 space = isl_set_get_space(set);
4804 space = isl_space_params(space);
4805 set = isl_set_reset_space(set, space);
4806 return set;
4809 /* Construct a zero-dimensional set with the given parameter domain.
4811 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4813 isl_space *space;
4814 space = isl_set_get_space(set);
4815 space = isl_space_set_from_params(space);
4816 set = isl_set_reset_space(set, space);
4817 return set;
4820 /* Compute the parameter domain of the given map.
4822 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4824 isl_space *space;
4825 unsigned n;
4827 n = isl_map_dim(map, isl_dim_in);
4828 map = isl_map_project_out(map, isl_dim_in, 0, n);
4829 n = isl_map_dim(map, isl_dim_out);
4830 map = isl_map_project_out(map, isl_dim_out, 0, n);
4831 space = isl_map_get_space(map);
4832 space = isl_space_params(space);
4833 map = isl_map_reset_space(map, space);
4834 return map;
4837 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4839 isl_space *dim;
4840 struct isl_basic_set *domain;
4841 unsigned n_in;
4842 unsigned n_out;
4844 if (!bmap)
4845 return NULL;
4846 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4848 n_in = isl_basic_map_n_in(bmap);
4849 n_out = isl_basic_map_n_out(bmap);
4850 domain = isl_basic_set_from_basic_map(bmap);
4851 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4853 domain = isl_basic_set_reset_space(domain, dim);
4855 return domain;
4858 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4860 if (!bmap)
4861 return -1;
4862 return isl_space_may_be_set(bmap->dim);
4865 /* Is this basic map actually a set?
4866 * Users should never call this function. Outside of isl,
4867 * the type should indicate whether something is a set or a map.
4869 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4871 if (!bmap)
4872 return -1;
4873 return isl_space_is_set(bmap->dim);
4876 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4878 if (!bmap)
4879 return NULL;
4880 if (isl_basic_map_is_set(bmap))
4881 return bmap;
4882 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4885 __isl_give isl_basic_map *isl_basic_map_domain_map(
4886 __isl_take isl_basic_map *bmap)
4888 int i, k;
4889 isl_space *dim;
4890 isl_basic_map *domain;
4891 int nparam, n_in, n_out;
4892 unsigned total;
4894 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4895 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4896 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4898 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4899 domain = isl_basic_map_universe(dim);
4901 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4902 bmap = isl_basic_map_apply_range(bmap, domain);
4903 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4905 total = isl_basic_map_total_dim(bmap);
4907 for (i = 0; i < n_in; ++i) {
4908 k = isl_basic_map_alloc_equality(bmap);
4909 if (k < 0)
4910 goto error;
4911 isl_seq_clr(bmap->eq[k], 1 + total);
4912 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4913 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4916 bmap = isl_basic_map_gauss(bmap, NULL);
4917 return isl_basic_map_finalize(bmap);
4918 error:
4919 isl_basic_map_free(bmap);
4920 return NULL;
4923 __isl_give isl_basic_map *isl_basic_map_range_map(
4924 __isl_take isl_basic_map *bmap)
4926 int i, k;
4927 isl_space *dim;
4928 isl_basic_map *range;
4929 int nparam, n_in, n_out;
4930 unsigned total;
4932 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4933 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4934 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4936 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4937 range = isl_basic_map_universe(dim);
4939 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4940 bmap = isl_basic_map_apply_range(bmap, range);
4941 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4943 total = isl_basic_map_total_dim(bmap);
4945 for (i = 0; i < n_out; ++i) {
4946 k = isl_basic_map_alloc_equality(bmap);
4947 if (k < 0)
4948 goto error;
4949 isl_seq_clr(bmap->eq[k], 1 + total);
4950 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4951 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4954 bmap = isl_basic_map_gauss(bmap, NULL);
4955 return isl_basic_map_finalize(bmap);
4956 error:
4957 isl_basic_map_free(bmap);
4958 return NULL;
4961 int isl_map_may_be_set(__isl_keep isl_map *map)
4963 if (!map)
4964 return -1;
4965 return isl_space_may_be_set(map->dim);
4968 /* Is this map actually a set?
4969 * Users should never call this function. Outside of isl,
4970 * the type should indicate whether something is a set or a map.
4972 int isl_map_is_set(__isl_keep isl_map *map)
4974 if (!map)
4975 return -1;
4976 return isl_space_is_set(map->dim);
4979 struct isl_set *isl_map_range(struct isl_map *map)
4981 int i;
4982 struct isl_set *set;
4984 if (!map)
4985 goto error;
4986 if (isl_map_is_set(map))
4987 return (isl_set *)map;
4989 map = isl_map_cow(map);
4990 if (!map)
4991 goto error;
4993 set = (struct isl_set *) map;
4994 set->dim = isl_space_range(set->dim);
4995 if (!set->dim)
4996 goto error;
4997 for (i = 0; i < map->n; ++i) {
4998 set->p[i] = isl_basic_map_range(map->p[i]);
4999 if (!set->p[i])
5000 goto error;
5002 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5003 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5004 return set;
5005 error:
5006 isl_map_free(map);
5007 return NULL;
5010 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5012 int i;
5014 map = isl_map_cow(map);
5015 if (!map)
5016 return NULL;
5018 map->dim = isl_space_domain_map(map->dim);
5019 if (!map->dim)
5020 goto error;
5021 for (i = 0; i < map->n; ++i) {
5022 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5023 if (!map->p[i])
5024 goto error;
5026 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5027 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5028 return map;
5029 error:
5030 isl_map_free(map);
5031 return NULL;
5034 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5036 int i;
5037 isl_space *range_dim;
5039 map = isl_map_cow(map);
5040 if (!map)
5041 return NULL;
5043 range_dim = isl_space_range(isl_map_get_space(map));
5044 range_dim = isl_space_from_range(range_dim);
5045 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5046 map->dim = isl_space_join(map->dim, range_dim);
5047 if (!map->dim)
5048 goto error;
5049 for (i = 0; i < map->n; ++i) {
5050 map->p[i] = isl_basic_map_range_map(map->p[i]);
5051 if (!map->p[i])
5052 goto error;
5054 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5055 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5056 return map;
5057 error:
5058 isl_map_free(map);
5059 return NULL;
5062 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5063 __isl_take isl_space *dim)
5065 int i;
5066 struct isl_map *map = NULL;
5068 set = isl_set_cow(set);
5069 if (!set || !dim)
5070 goto error;
5071 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5072 map = (struct isl_map *)set;
5073 for (i = 0; i < set->n; ++i) {
5074 map->p[i] = isl_basic_map_from_basic_set(
5075 set->p[i], isl_space_copy(dim));
5076 if (!map->p[i])
5077 goto error;
5079 isl_space_free(map->dim);
5080 map->dim = dim;
5081 return map;
5082 error:
5083 isl_space_free(dim);
5084 isl_set_free(set);
5085 return NULL;
5088 __isl_give isl_basic_map *isl_basic_map_from_domain(
5089 __isl_take isl_basic_set *bset)
5091 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5094 __isl_give isl_basic_map *isl_basic_map_from_range(
5095 __isl_take isl_basic_set *bset)
5097 isl_space *space;
5098 space = isl_basic_set_get_space(bset);
5099 space = isl_space_from_range(space);
5100 bset = isl_basic_set_reset_space(bset, space);
5101 return (isl_basic_map *)bset;
5104 /* Create a relation with the given set as range.
5105 * The domain of the created relation is a zero-dimensional
5106 * flat anonymous space.
5108 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5110 isl_space *space;
5111 space = isl_set_get_space(set);
5112 space = isl_space_from_range(space);
5113 set = isl_set_reset_space(set, space);
5114 return (struct isl_map *)set;
5117 /* Create a relation with the given set as domain.
5118 * The range of the created relation is a zero-dimensional
5119 * flat anonymous space.
5121 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5123 return isl_map_reverse(isl_map_from_range(set));
5126 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5127 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5129 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5132 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5133 __isl_take isl_set *range)
5135 return isl_map_apply_range(isl_map_reverse(domain), range);
5138 struct isl_set *isl_set_from_map(struct isl_map *map)
5140 int i;
5141 struct isl_set *set = NULL;
5143 if (!map)
5144 return NULL;
5145 map = isl_map_cow(map);
5146 if (!map)
5147 return NULL;
5148 map->dim = isl_space_as_set_space(map->dim);
5149 if (!map->dim)
5150 goto error;
5151 set = (struct isl_set *)map;
5152 for (i = 0; i < map->n; ++i) {
5153 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
5154 if (!set->p[i])
5155 goto error;
5157 return set;
5158 error:
5159 isl_map_free(map);
5160 return NULL;
5163 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5164 unsigned flags)
5166 struct isl_map *map;
5168 if (!dim)
5169 return NULL;
5170 if (n < 0)
5171 isl_die(dim->ctx, isl_error_internal,
5172 "negative number of basic maps", goto error);
5173 map = isl_alloc(dim->ctx, struct isl_map,
5174 sizeof(struct isl_map) +
5175 (n - 1) * sizeof(struct isl_basic_map *));
5176 if (!map)
5177 goto error;
5179 map->ctx = dim->ctx;
5180 isl_ctx_ref(map->ctx);
5181 map->ref = 1;
5182 map->size = n;
5183 map->n = 0;
5184 map->dim = dim;
5185 map->flags = flags;
5186 return map;
5187 error:
5188 isl_space_free(dim);
5189 return NULL;
5192 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5193 unsigned nparam, unsigned in, unsigned out, int n,
5194 unsigned flags)
5196 struct isl_map *map;
5197 isl_space *dims;
5199 dims = isl_space_alloc(ctx, nparam, in, out);
5200 if (!dims)
5201 return NULL;
5203 map = isl_map_alloc_space(dims, n, flags);
5204 return map;
5207 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5209 struct isl_basic_map *bmap;
5210 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5211 bmap = isl_basic_map_set_to_empty(bmap);
5212 return bmap;
5215 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5217 struct isl_basic_set *bset;
5218 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5219 bset = isl_basic_set_set_to_empty(bset);
5220 return bset;
5223 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5225 struct isl_basic_map *bmap;
5226 if (!model)
5227 return NULL;
5228 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5229 bmap = isl_basic_map_set_to_empty(bmap);
5230 return bmap;
5233 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5235 struct isl_basic_map *bmap;
5236 if (!model)
5237 return NULL;
5238 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5239 bmap = isl_basic_map_set_to_empty(bmap);
5240 return bmap;
5243 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5245 struct isl_basic_set *bset;
5246 if (!model)
5247 return NULL;
5248 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5249 bset = isl_basic_set_set_to_empty(bset);
5250 return bset;
5253 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5255 struct isl_basic_map *bmap;
5256 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5257 bmap = isl_basic_map_finalize(bmap);
5258 return bmap;
5261 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5263 struct isl_basic_set *bset;
5264 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5265 bset = isl_basic_set_finalize(bset);
5266 return bset;
5269 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5271 int i;
5272 unsigned total = isl_space_dim(dim, isl_dim_all);
5273 isl_basic_map *bmap;
5275 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5276 for (i = 0; i < total; ++i) {
5277 int k = isl_basic_map_alloc_inequality(bmap);
5278 if (k < 0)
5279 goto error;
5280 isl_seq_clr(bmap->ineq[k], 1 + total);
5281 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5283 return bmap;
5284 error:
5285 isl_basic_map_free(bmap);
5286 return NULL;
5289 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5291 return isl_basic_map_nat_universe(dim);
5294 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5296 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5299 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5301 return isl_map_nat_universe(dim);
5304 __isl_give isl_basic_map *isl_basic_map_universe_like(
5305 __isl_keep isl_basic_map *model)
5307 if (!model)
5308 return NULL;
5309 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5312 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5314 if (!model)
5315 return NULL;
5316 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5319 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5320 __isl_keep isl_set *model)
5322 if (!model)
5323 return NULL;
5324 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5327 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5329 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5332 struct isl_map *isl_map_empty_like(struct isl_map *model)
5334 if (!model)
5335 return NULL;
5336 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5339 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5341 if (!model)
5342 return NULL;
5343 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5346 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5348 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5351 struct isl_set *isl_set_empty_like(struct isl_set *model)
5353 if (!model)
5354 return NULL;
5355 return isl_set_empty(isl_space_copy(model->dim));
5358 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5360 struct isl_map *map;
5361 if (!dim)
5362 return NULL;
5363 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5364 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5365 return map;
5368 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5370 struct isl_set *set;
5371 if (!dim)
5372 return NULL;
5373 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5374 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5375 return set;
5378 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5380 if (!model)
5381 return NULL;
5382 return isl_set_universe(isl_space_copy(model->dim));
5385 struct isl_map *isl_map_dup(struct isl_map *map)
5387 int i;
5388 struct isl_map *dup;
5390 if (!map)
5391 return NULL;
5392 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5393 for (i = 0; i < map->n; ++i)
5394 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5395 return dup;
5398 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5399 __isl_take isl_basic_map *bmap)
5401 if (!bmap || !map)
5402 goto error;
5403 if (isl_basic_map_plain_is_empty(bmap)) {
5404 isl_basic_map_free(bmap);
5405 return map;
5407 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5408 isl_assert(map->ctx, map->n < map->size, goto error);
5409 map->p[map->n] = bmap;
5410 map->n++;
5411 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5412 return map;
5413 error:
5414 if (map)
5415 isl_map_free(map);
5416 if (bmap)
5417 isl_basic_map_free(bmap);
5418 return NULL;
5421 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5423 int i;
5425 if (!map)
5426 return NULL;
5428 if (--map->ref > 0)
5429 return NULL;
5431 isl_ctx_deref(map->ctx);
5432 for (i = 0; i < map->n; ++i)
5433 isl_basic_map_free(map->p[i]);
5434 isl_space_free(map->dim);
5435 free(map);
5437 return NULL;
5440 struct isl_map *isl_map_extend(struct isl_map *base,
5441 unsigned nparam, unsigned n_in, unsigned n_out)
5443 int i;
5445 base = isl_map_cow(base);
5446 if (!base)
5447 return NULL;
5449 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5450 if (!base->dim)
5451 goto error;
5452 for (i = 0; i < base->n; ++i) {
5453 base->p[i] = isl_basic_map_extend_space(base->p[i],
5454 isl_space_copy(base->dim), 0, 0, 0);
5455 if (!base->p[i])
5456 goto error;
5458 return base;
5459 error:
5460 isl_map_free(base);
5461 return NULL;
5464 struct isl_set *isl_set_extend(struct isl_set *base,
5465 unsigned nparam, unsigned dim)
5467 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5468 nparam, 0, dim);
5471 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5472 struct isl_basic_map *bmap, unsigned pos, int value)
5474 int j;
5476 bmap = isl_basic_map_cow(bmap);
5477 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5478 j = isl_basic_map_alloc_equality(bmap);
5479 if (j < 0)
5480 goto error;
5481 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5482 isl_int_set_si(bmap->eq[j][pos], -1);
5483 isl_int_set_si(bmap->eq[j][0], value);
5484 bmap = isl_basic_map_simplify(bmap);
5485 return isl_basic_map_finalize(bmap);
5486 error:
5487 isl_basic_map_free(bmap);
5488 return NULL;
5491 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5492 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5494 int j;
5496 bmap = isl_basic_map_cow(bmap);
5497 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5498 j = isl_basic_map_alloc_equality(bmap);
5499 if (j < 0)
5500 goto error;
5501 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5502 isl_int_set_si(bmap->eq[j][pos], -1);
5503 isl_int_set(bmap->eq[j][0], value);
5504 bmap = isl_basic_map_simplify(bmap);
5505 return isl_basic_map_finalize(bmap);
5506 error:
5507 isl_basic_map_free(bmap);
5508 return NULL;
5511 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5512 enum isl_dim_type type, unsigned pos, int value)
5514 if (!bmap)
5515 return NULL;
5516 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5517 return isl_basic_map_fix_pos_si(bmap,
5518 isl_basic_map_offset(bmap, type) + pos, value);
5519 error:
5520 isl_basic_map_free(bmap);
5521 return NULL;
5524 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5525 enum isl_dim_type type, unsigned pos, isl_int value)
5527 if (!bmap)
5528 return NULL;
5529 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5530 return isl_basic_map_fix_pos(bmap,
5531 isl_basic_map_offset(bmap, type) + pos, value);
5532 error:
5533 isl_basic_map_free(bmap);
5534 return NULL;
5537 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5538 * to be equal to "v".
5540 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5541 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5543 if (!bmap || !v)
5544 goto error;
5545 if (!isl_val_is_int(v))
5546 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5547 "expecting integer value", goto error);
5548 if (pos >= isl_basic_map_dim(bmap, type))
5549 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5550 "index out of bounds", goto error);
5551 pos += isl_basic_map_offset(bmap, type);
5552 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5553 isl_val_free(v);
5554 return bmap;
5555 error:
5556 isl_basic_map_free(bmap);
5557 isl_val_free(v);
5558 return NULL;
5561 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5562 * to be equal to "v".
5564 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5565 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5567 return isl_basic_map_fix_val(bset, type, pos, v);
5570 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5571 enum isl_dim_type type, unsigned pos, int value)
5573 return (struct isl_basic_set *)
5574 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5575 type, pos, value);
5578 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5579 enum isl_dim_type type, unsigned pos, isl_int value)
5581 return (struct isl_basic_set *)
5582 isl_basic_map_fix((struct isl_basic_map *)bset,
5583 type, pos, value);
5586 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5587 unsigned input, int value)
5589 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5592 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5593 unsigned dim, int value)
5595 return (struct isl_basic_set *)
5596 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5597 isl_dim_set, dim, value);
5600 static int remove_if_empty(__isl_keep isl_map *map, int i)
5602 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5604 if (empty < 0)
5605 return -1;
5606 if (!empty)
5607 return 0;
5609 isl_basic_map_free(map->p[i]);
5610 if (i != map->n - 1) {
5611 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5612 map->p[i] = map->p[map->n - 1];
5614 map->n--;
5616 return 0;
5619 /* Perform "fn" on each basic map of "map", where we may not be holding
5620 * the only reference to "map".
5621 * In particular, "fn" should be a semantics preserving operation
5622 * that we want to apply to all copies of "map". We therefore need
5623 * to be careful not to modify "map" in a way that breaks "map"
5624 * in case anything goes wrong.
5626 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5627 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5629 struct isl_basic_map *bmap;
5630 int i;
5632 if (!map)
5633 return NULL;
5635 for (i = map->n - 1; i >= 0; --i) {
5636 bmap = isl_basic_map_copy(map->p[i]);
5637 bmap = fn(bmap);
5638 if (!bmap)
5639 goto error;
5640 isl_basic_map_free(map->p[i]);
5641 map->p[i] = bmap;
5642 if (remove_if_empty(map, i) < 0)
5643 goto error;
5646 return map;
5647 error:
5648 isl_map_free(map);
5649 return NULL;
5652 struct isl_map *isl_map_fix_si(struct isl_map *map,
5653 enum isl_dim_type type, unsigned pos, int value)
5655 int i;
5657 map = isl_map_cow(map);
5658 if (!map)
5659 return NULL;
5661 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5662 for (i = map->n - 1; i >= 0; --i) {
5663 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5664 if (remove_if_empty(map, i) < 0)
5665 goto error;
5667 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5668 return map;
5669 error:
5670 isl_map_free(map);
5671 return NULL;
5674 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5675 enum isl_dim_type type, unsigned pos, int value)
5677 return (struct isl_set *)
5678 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5681 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5682 enum isl_dim_type type, unsigned pos, isl_int value)
5684 int i;
5686 map = isl_map_cow(map);
5687 if (!map)
5688 return NULL;
5690 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5691 for (i = 0; i < map->n; ++i) {
5692 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5693 if (!map->p[i])
5694 goto error;
5696 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5697 return map;
5698 error:
5699 isl_map_free(map);
5700 return NULL;
5703 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5704 enum isl_dim_type type, unsigned pos, isl_int value)
5706 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5709 /* Fix the value of the variable at position "pos" of type "type" of "map"
5710 * to be equal to "v".
5712 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5713 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5715 int i;
5717 map = isl_map_cow(map);
5718 if (!map || !v)
5719 goto error;
5721 if (!isl_val_is_int(v))
5722 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5723 "expecting integer value", goto error);
5724 if (pos >= isl_map_dim(map, type))
5725 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5726 "index out of bounds", goto error);
5727 for (i = map->n - 1; i >= 0; --i) {
5728 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5729 isl_val_copy(v));
5730 if (remove_if_empty(map, i) < 0)
5731 goto error;
5733 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5734 isl_val_free(v);
5735 return map;
5736 error:
5737 isl_map_free(map);
5738 isl_val_free(v);
5739 return NULL;
5742 /* Fix the value of the variable at position "pos" of type "type" of "set"
5743 * to be equal to "v".
5745 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5746 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5748 return isl_map_fix_val(set, type, pos, v);
5751 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5752 unsigned input, int value)
5754 return isl_map_fix_si(map, isl_dim_in, input, value);
5757 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5759 return (struct isl_set *)
5760 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5763 static __isl_give isl_basic_map *basic_map_bound_si(
5764 __isl_take isl_basic_map *bmap,
5765 enum isl_dim_type type, unsigned pos, int value, int upper)
5767 int j;
5769 if (!bmap)
5770 return NULL;
5771 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5772 pos += isl_basic_map_offset(bmap, type);
5773 bmap = isl_basic_map_cow(bmap);
5774 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5775 j = isl_basic_map_alloc_inequality(bmap);
5776 if (j < 0)
5777 goto error;
5778 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5779 if (upper) {
5780 isl_int_set_si(bmap->ineq[j][pos], -1);
5781 isl_int_set_si(bmap->ineq[j][0], value);
5782 } else {
5783 isl_int_set_si(bmap->ineq[j][pos], 1);
5784 isl_int_set_si(bmap->ineq[j][0], -value);
5786 bmap = isl_basic_map_simplify(bmap);
5787 return isl_basic_map_finalize(bmap);
5788 error:
5789 isl_basic_map_free(bmap);
5790 return NULL;
5793 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5794 __isl_take isl_basic_map *bmap,
5795 enum isl_dim_type type, unsigned pos, int value)
5797 return basic_map_bound_si(bmap, type, pos, value, 0);
5800 /* Constrain the values of the given dimension to be no greater than "value".
5802 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5803 __isl_take isl_basic_map *bmap,
5804 enum isl_dim_type type, unsigned pos, int value)
5806 return basic_map_bound_si(bmap, type, pos, value, 1);
5809 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5810 unsigned dim, isl_int value)
5812 int j;
5814 bset = isl_basic_set_cow(bset);
5815 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5816 j = isl_basic_set_alloc_inequality(bset);
5817 if (j < 0)
5818 goto error;
5819 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5820 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5821 isl_int_neg(bset->ineq[j][0], value);
5822 bset = isl_basic_set_simplify(bset);
5823 return isl_basic_set_finalize(bset);
5824 error:
5825 isl_basic_set_free(bset);
5826 return NULL;
5829 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5830 enum isl_dim_type type, unsigned pos, int value, int upper)
5832 int i;
5834 map = isl_map_cow(map);
5835 if (!map)
5836 return NULL;
5838 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5839 for (i = 0; i < map->n; ++i) {
5840 map->p[i] = basic_map_bound_si(map->p[i],
5841 type, pos, value, upper);
5842 if (!map->p[i])
5843 goto error;
5845 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5846 return map;
5847 error:
5848 isl_map_free(map);
5849 return NULL;
5852 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5853 enum isl_dim_type type, unsigned pos, int value)
5855 return map_bound_si(map, type, pos, value, 0);
5858 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5859 enum isl_dim_type type, unsigned pos, int value)
5861 return map_bound_si(map, type, pos, value, 1);
5864 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5865 enum isl_dim_type type, unsigned pos, int value)
5867 return (struct isl_set *)
5868 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5871 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5872 enum isl_dim_type type, unsigned pos, int value)
5874 return isl_map_upper_bound_si(set, type, pos, value);
5877 /* Bound the given variable of "bmap" from below (or above is "upper"
5878 * is set) to "value".
5880 static __isl_give isl_basic_map *basic_map_bound(
5881 __isl_take isl_basic_map *bmap,
5882 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5884 int j;
5886 if (!bmap)
5887 return NULL;
5888 if (pos >= isl_basic_map_dim(bmap, type))
5889 isl_die(bmap->ctx, isl_error_invalid,
5890 "index out of bounds", goto error);
5891 pos += isl_basic_map_offset(bmap, type);
5892 bmap = isl_basic_map_cow(bmap);
5893 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5894 j = isl_basic_map_alloc_inequality(bmap);
5895 if (j < 0)
5896 goto error;
5897 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5898 if (upper) {
5899 isl_int_set_si(bmap->ineq[j][pos], -1);
5900 isl_int_set(bmap->ineq[j][0], value);
5901 } else {
5902 isl_int_set_si(bmap->ineq[j][pos], 1);
5903 isl_int_neg(bmap->ineq[j][0], value);
5905 bmap = isl_basic_map_simplify(bmap);
5906 return isl_basic_map_finalize(bmap);
5907 error:
5908 isl_basic_map_free(bmap);
5909 return NULL;
5912 /* Bound the given variable of "map" from below (or above is "upper"
5913 * is set) to "value".
5915 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5916 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5918 int i;
5920 map = isl_map_cow(map);
5921 if (!map)
5922 return NULL;
5924 if (pos >= isl_map_dim(map, type))
5925 isl_die(map->ctx, isl_error_invalid,
5926 "index out of bounds", goto error);
5927 for (i = map->n - 1; i >= 0; --i) {
5928 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5929 if (remove_if_empty(map, i) < 0)
5930 goto error;
5932 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5933 return map;
5934 error:
5935 isl_map_free(map);
5936 return NULL;
5939 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5940 enum isl_dim_type type, unsigned pos, isl_int value)
5942 return map_bound(map, type, pos, value, 0);
5945 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5946 enum isl_dim_type type, unsigned pos, isl_int value)
5948 return map_bound(map, type, pos, value, 1);
5951 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5952 enum isl_dim_type type, unsigned pos, isl_int value)
5954 return isl_map_lower_bound(set, type, pos, value);
5957 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5958 enum isl_dim_type type, unsigned pos, isl_int value)
5960 return isl_map_upper_bound(set, type, pos, value);
5963 /* Force the values of the variable at position "pos" of type "type" of "set"
5964 * to be no smaller than "value".
5966 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5967 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5969 if (!value)
5970 goto error;
5971 if (!isl_val_is_int(value))
5972 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5973 "expecting integer value", goto error);
5974 set = isl_set_lower_bound(set, type, pos, value->n);
5975 isl_val_free(value);
5976 return set;
5977 error:
5978 isl_val_free(value);
5979 isl_set_free(set);
5980 return NULL;
5983 /* Force the values of the variable at position "pos" of type "type" of "set"
5984 * to be no greater than "value".
5986 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5987 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5989 if (!value)
5990 goto error;
5991 if (!isl_val_is_int(value))
5992 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5993 "expecting integer value", goto error);
5994 set = isl_set_upper_bound(set, type, pos, value->n);
5995 isl_val_free(value);
5996 return set;
5997 error:
5998 isl_val_free(value);
5999 isl_set_free(set);
6000 return NULL;
6003 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6004 isl_int value)
6006 int i;
6008 set = isl_set_cow(set);
6009 if (!set)
6010 return NULL;
6012 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6013 for (i = 0; i < set->n; ++i) {
6014 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6015 if (!set->p[i])
6016 goto error;
6018 return set;
6019 error:
6020 isl_set_free(set);
6021 return NULL;
6024 struct isl_map *isl_map_reverse(struct isl_map *map)
6026 int i;
6028 map = isl_map_cow(map);
6029 if (!map)
6030 return NULL;
6032 map->dim = isl_space_reverse(map->dim);
6033 if (!map->dim)
6034 goto error;
6035 for (i = 0; i < map->n; ++i) {
6036 map->p[i] = isl_basic_map_reverse(map->p[i]);
6037 if (!map->p[i])
6038 goto error;
6040 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6041 return map;
6042 error:
6043 isl_map_free(map);
6044 return NULL;
6047 static struct isl_map *isl_basic_map_partial_lexopt(
6048 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6049 struct isl_set **empty, int max)
6051 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6054 struct isl_map *isl_basic_map_partial_lexmax(
6055 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6056 struct isl_set **empty)
6058 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6061 struct isl_map *isl_basic_map_partial_lexmin(
6062 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6063 struct isl_set **empty)
6065 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6068 struct isl_set *isl_basic_set_partial_lexmin(
6069 struct isl_basic_set *bset, struct isl_basic_set *dom,
6070 struct isl_set **empty)
6072 return (struct isl_set *)
6073 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6074 dom, empty);
6077 struct isl_set *isl_basic_set_partial_lexmax(
6078 struct isl_basic_set *bset, struct isl_basic_set *dom,
6079 struct isl_set **empty)
6081 return (struct isl_set *)
6082 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6083 dom, empty);
6086 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6087 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6088 __isl_give isl_set **empty)
6090 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6093 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6094 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6095 __isl_give isl_set **empty)
6097 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6100 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6101 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6102 __isl_give isl_set **empty)
6104 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6107 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6108 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6109 __isl_give isl_set **empty)
6111 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6114 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6115 __isl_take isl_basic_map *bmap, int max)
6117 isl_basic_set *dom = NULL;
6118 isl_space *dom_space;
6120 if (!bmap)
6121 goto error;
6122 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6123 dom = isl_basic_set_universe(dom_space);
6124 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6125 error:
6126 isl_basic_map_free(bmap);
6127 return NULL;
6130 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6131 __isl_take isl_basic_map *bmap)
6133 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6136 #undef TYPE
6137 #define TYPE isl_pw_multi_aff
6138 #undef SUFFIX
6139 #define SUFFIX _pw_multi_aff
6140 #undef EMPTY
6141 #define EMPTY isl_pw_multi_aff_empty
6142 #undef ADD
6143 #define ADD isl_pw_multi_aff_union_add
6144 #include "isl_map_lexopt_templ.c"
6146 /* Given a map "map", compute the lexicographically minimal
6147 * (or maximal) image element for each domain element in dom,
6148 * in the form of an isl_pw_multi_aff.
6149 * Set *empty to those elements in dom that do not have an image element.
6151 * We first compute the lexicographically minimal or maximal element
6152 * in the first basic map. This results in a partial solution "res"
6153 * and a subset "todo" of dom that still need to be handled.
6154 * We then consider each of the remaining maps in "map" and successively
6155 * update both "res" and "todo".
6157 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6158 __isl_take isl_map *map, __isl_take isl_set *dom,
6159 __isl_give isl_set **empty, int max)
6161 int i;
6162 isl_pw_multi_aff *res;
6163 isl_set *todo;
6165 if (!map || !dom)
6166 goto error;
6168 if (isl_map_plain_is_empty(map)) {
6169 if (empty)
6170 *empty = dom;
6171 else
6172 isl_set_free(dom);
6173 return isl_pw_multi_aff_from_map(map);
6176 res = basic_map_partial_lexopt_pw_multi_aff(
6177 isl_basic_map_copy(map->p[0]),
6178 isl_set_copy(dom), &todo, max);
6180 for (i = 1; i < map->n; ++i) {
6181 isl_pw_multi_aff *res_i;
6182 isl_set *todo_i;
6184 res_i = basic_map_partial_lexopt_pw_multi_aff(
6185 isl_basic_map_copy(map->p[i]),
6186 isl_set_copy(dom), &todo_i, max);
6188 if (max)
6189 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6190 else
6191 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6193 todo = isl_set_intersect(todo, todo_i);
6196 isl_set_free(dom);
6197 isl_map_free(map);
6199 if (empty)
6200 *empty = todo;
6201 else
6202 isl_set_free(todo);
6204 return res;
6205 error:
6206 if (empty)
6207 *empty = NULL;
6208 isl_set_free(dom);
6209 isl_map_free(map);
6210 return NULL;
6213 #undef TYPE
6214 #define TYPE isl_map
6215 #undef SUFFIX
6216 #define SUFFIX
6217 #undef EMPTY
6218 #define EMPTY isl_map_empty
6219 #undef ADD
6220 #define ADD isl_map_union_disjoint
6221 #include "isl_map_lexopt_templ.c"
6223 /* Given a map "map", compute the lexicographically minimal
6224 * (or maximal) image element for each domain element in dom.
6225 * Set *empty to those elements in dom that do not have an image element.
6227 * We first compute the lexicographically minimal or maximal element
6228 * in the first basic map. This results in a partial solution "res"
6229 * and a subset "todo" of dom that still need to be handled.
6230 * We then consider each of the remaining maps in "map" and successively
6231 * update both "res" and "todo".
6233 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6234 * Assume we are computing the lexicographical maximum.
6235 * We first compute the lexicographically maximal element in basic map i.
6236 * This results in a partial solution res_i and a subset todo_i.
6237 * Then we combine these results with those obtain for the first k basic maps
6238 * to obtain a result that is valid for the first k+1 basic maps.
6239 * In particular, the set where there is no solution is the set where
6240 * there is no solution for the first k basic maps and also no solution
6241 * for the ith basic map, i.e.,
6243 * todo^i = todo^k * todo_i
6245 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6246 * solutions, arbitrarily breaking ties in favor of res^k.
6247 * That is, when res^k(a) >= res_i(a), we pick res^k and
6248 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6249 * the lexicographic order.)
6250 * In practice, we compute
6252 * res^k * (res_i . "<=")
6254 * and
6256 * res_i * (res^k . "<")
6258 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6259 * where only one of res^k and res_i provides a solution and we simply pick
6260 * that one, i.e.,
6262 * res^k * todo_i
6263 * and
6264 * res_i * todo^k
6266 * Note that we only compute these intersections when dom(res^k) intersects
6267 * dom(res_i). Otherwise, the only effect of these intersections is to
6268 * potentially break up res^k and res_i into smaller pieces.
6269 * We want to avoid such splintering as much as possible.
6270 * In fact, an earlier implementation of this function would look for
6271 * better results in the domain of res^k and for extra results in todo^k,
6272 * but this would always result in a splintering according to todo^k,
6273 * even when the domain of basic map i is disjoint from the domains of
6274 * the previous basic maps.
6276 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6277 __isl_take isl_map *map, __isl_take isl_set *dom,
6278 __isl_give isl_set **empty, int max)
6280 int i;
6281 struct isl_map *res;
6282 struct isl_set *todo;
6284 if (!map || !dom)
6285 goto error;
6287 if (isl_map_plain_is_empty(map)) {
6288 if (empty)
6289 *empty = dom;
6290 else
6291 isl_set_free(dom);
6292 return map;
6295 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6296 isl_set_copy(dom), &todo, max);
6298 for (i = 1; i < map->n; ++i) {
6299 isl_map *lt, *le;
6300 isl_map *res_i;
6301 isl_set *todo_i;
6302 isl_space *dim = isl_space_range(isl_map_get_space(res));
6304 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6305 isl_set_copy(dom), &todo_i, max);
6307 if (max) {
6308 lt = isl_map_lex_lt(isl_space_copy(dim));
6309 le = isl_map_lex_le(dim);
6310 } else {
6311 lt = isl_map_lex_gt(isl_space_copy(dim));
6312 le = isl_map_lex_ge(dim);
6314 lt = isl_map_apply_range(isl_map_copy(res), lt);
6315 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6316 le = isl_map_apply_range(isl_map_copy(res_i), le);
6317 le = isl_map_intersect(le, isl_map_copy(res));
6319 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6320 res = isl_map_intersect_domain(res,
6321 isl_set_copy(todo_i));
6322 res_i = isl_map_intersect_domain(res_i,
6323 isl_set_copy(todo));
6326 res = isl_map_union_disjoint(res, res_i);
6327 res = isl_map_union_disjoint(res, lt);
6328 res = isl_map_union_disjoint(res, le);
6330 todo = isl_set_intersect(todo, todo_i);
6333 isl_set_free(dom);
6334 isl_map_free(map);
6336 if (empty)
6337 *empty = todo;
6338 else
6339 isl_set_free(todo);
6341 return res;
6342 error:
6343 if (empty)
6344 *empty = NULL;
6345 isl_set_free(dom);
6346 isl_map_free(map);
6347 return NULL;
6350 __isl_give isl_map *isl_map_partial_lexmax(
6351 __isl_take isl_map *map, __isl_take isl_set *dom,
6352 __isl_give isl_set **empty)
6354 return isl_map_partial_lexopt(map, dom, empty, 1);
6357 __isl_give isl_map *isl_map_partial_lexmin(
6358 __isl_take isl_map *map, __isl_take isl_set *dom,
6359 __isl_give isl_set **empty)
6361 return isl_map_partial_lexopt(map, dom, empty, 0);
6364 __isl_give isl_set *isl_set_partial_lexmin(
6365 __isl_take isl_set *set, __isl_take isl_set *dom,
6366 __isl_give isl_set **empty)
6368 return (struct isl_set *)
6369 isl_map_partial_lexmin((struct isl_map *)set,
6370 dom, empty);
6373 __isl_give isl_set *isl_set_partial_lexmax(
6374 __isl_take isl_set *set, __isl_take isl_set *dom,
6375 __isl_give isl_set **empty)
6377 return (struct isl_set *)
6378 isl_map_partial_lexmax((struct isl_map *)set,
6379 dom, empty);
6382 /* Compute the lexicographic minimum (or maximum if "max" is set)
6383 * of "bmap" over its domain.
6385 * Since we are not interested in the part of the domain space where
6386 * there is no solution, we initialize the domain to those constraints
6387 * of "bmap" that only involve the parameters and the input dimensions.
6388 * This relieves the parametric programming engine from detecting those
6389 * inequalities and transferring them to the context. More importantly,
6390 * it ensures that those inequalities are transferred first and not
6391 * intermixed with inequalities that actually split the domain.
6393 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6395 int n_div;
6396 int n_out;
6397 isl_basic_map *copy;
6398 isl_basic_set *dom;
6400 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6401 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6402 copy = isl_basic_map_copy(bmap);
6403 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6404 isl_dim_div, 0, n_div);
6405 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6406 isl_dim_out, 0, n_out);
6407 dom = isl_basic_map_domain(copy);
6408 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6411 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6413 return isl_basic_map_lexopt(bmap, 0);
6416 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6418 return isl_basic_map_lexopt(bmap, 1);
6421 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6423 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6426 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6428 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6431 /* Extract the first and only affine expression from list
6432 * and then add it to *pwaff with the given dom.
6433 * This domain is known to be disjoint from other domains
6434 * because of the way isl_basic_map_foreach_lexmax works.
6436 static int update_dim_opt(__isl_take isl_basic_set *dom,
6437 __isl_take isl_aff_list *list, void *user)
6439 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6440 isl_aff *aff;
6441 isl_pw_aff **pwaff = user;
6442 isl_pw_aff *pwaff_i;
6444 if (!list)
6445 goto error;
6446 if (isl_aff_list_n_aff(list) != 1)
6447 isl_die(ctx, isl_error_internal,
6448 "expecting single element list", goto error);
6450 aff = isl_aff_list_get_aff(list, 0);
6451 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6453 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6455 isl_aff_list_free(list);
6457 return 0;
6458 error:
6459 isl_basic_set_free(dom);
6460 isl_aff_list_free(list);
6461 return -1;
6464 /* Given a basic map with one output dimension, compute the minimum or
6465 * maximum of that dimension as an isl_pw_aff.
6467 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6468 * call update_dim_opt on each leaf of the result.
6470 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6471 int max)
6473 isl_space *dim = isl_basic_map_get_space(bmap);
6474 isl_pw_aff *pwaff;
6475 int r;
6477 dim = isl_space_from_domain(isl_space_domain(dim));
6478 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6479 pwaff = isl_pw_aff_empty(dim);
6481 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6482 if (r < 0)
6483 return isl_pw_aff_free(pwaff);
6485 return pwaff;
6488 /* Compute the minimum or maximum of the given output dimension
6489 * as a function of the parameters and the input dimensions,
6490 * but independently of the other output dimensions.
6492 * We first project out the other output dimension and then compute
6493 * the "lexicographic" maximum in each basic map, combining the results
6494 * using isl_pw_aff_union_max.
6496 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6497 int max)
6499 int i;
6500 isl_pw_aff *pwaff;
6501 unsigned n_out;
6503 n_out = isl_map_dim(map, isl_dim_out);
6504 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6505 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6506 if (!map)
6507 return NULL;
6509 if (map->n == 0) {
6510 isl_space *dim = isl_map_get_space(map);
6511 isl_map_free(map);
6512 return isl_pw_aff_empty(dim);
6515 pwaff = basic_map_dim_opt(map->p[0], max);
6516 for (i = 1; i < map->n; ++i) {
6517 isl_pw_aff *pwaff_i;
6519 pwaff_i = basic_map_dim_opt(map->p[i], max);
6520 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6523 isl_map_free(map);
6525 return pwaff;
6528 /* Compute the maximum of the given output dimension as a function of the
6529 * parameters and input dimensions, but independently of
6530 * the other output dimensions.
6532 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6534 return map_dim_opt(map, pos, 1);
6537 /* Compute the minimum or maximum of the given set dimension
6538 * as a function of the parameters,
6539 * but independently of the other set dimensions.
6541 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6542 int max)
6544 return map_dim_opt(set, pos, max);
6547 /* Compute the maximum of the given set dimension as a function of the
6548 * parameters, but independently of the other set dimensions.
6550 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6552 return set_dim_opt(set, pos, 1);
6555 /* Compute the minimum of the given set dimension as a function of the
6556 * parameters, but independently of the other set dimensions.
6558 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6560 return set_dim_opt(set, pos, 0);
6563 /* Apply a preimage specified by "mat" on the parameters of "bset".
6564 * bset is assumed to have only parameters and divs.
6566 static struct isl_basic_set *basic_set_parameter_preimage(
6567 struct isl_basic_set *bset, struct isl_mat *mat)
6569 unsigned nparam;
6571 if (!bset || !mat)
6572 goto error;
6574 bset->dim = isl_space_cow(bset->dim);
6575 if (!bset->dim)
6576 goto error;
6578 nparam = isl_basic_set_dim(bset, isl_dim_param);
6580 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6582 bset->dim->nparam = 0;
6583 bset->dim->n_out = nparam;
6584 bset = isl_basic_set_preimage(bset, mat);
6585 if (bset) {
6586 bset->dim->nparam = bset->dim->n_out;
6587 bset->dim->n_out = 0;
6589 return bset;
6590 error:
6591 isl_mat_free(mat);
6592 isl_basic_set_free(bset);
6593 return NULL;
6596 /* Apply a preimage specified by "mat" on the parameters of "set".
6597 * set is assumed to have only parameters and divs.
6599 static struct isl_set *set_parameter_preimage(
6600 struct isl_set *set, struct isl_mat *mat)
6602 isl_space *dim = NULL;
6603 unsigned nparam;
6605 if (!set || !mat)
6606 goto error;
6608 dim = isl_space_copy(set->dim);
6609 dim = isl_space_cow(dim);
6610 if (!dim)
6611 goto error;
6613 nparam = isl_set_dim(set, isl_dim_param);
6615 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6617 dim->nparam = 0;
6618 dim->n_out = nparam;
6619 isl_set_reset_space(set, dim);
6620 set = isl_set_preimage(set, mat);
6621 if (!set)
6622 goto error2;
6623 dim = isl_space_copy(set->dim);
6624 dim = isl_space_cow(dim);
6625 if (!dim)
6626 goto error2;
6627 dim->nparam = dim->n_out;
6628 dim->n_out = 0;
6629 isl_set_reset_space(set, dim);
6630 return set;
6631 error:
6632 isl_space_free(dim);
6633 isl_mat_free(mat);
6634 error2:
6635 isl_set_free(set);
6636 return NULL;
6639 /* Intersect the basic set "bset" with the affine space specified by the
6640 * equalities in "eq".
6642 static struct isl_basic_set *basic_set_append_equalities(
6643 struct isl_basic_set *bset, struct isl_mat *eq)
6645 int i, k;
6646 unsigned len;
6648 if (!bset || !eq)
6649 goto error;
6651 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6652 eq->n_row, 0);
6653 if (!bset)
6654 goto error;
6656 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6657 for (i = 0; i < eq->n_row; ++i) {
6658 k = isl_basic_set_alloc_equality(bset);
6659 if (k < 0)
6660 goto error;
6661 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6662 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6664 isl_mat_free(eq);
6666 bset = isl_basic_set_gauss(bset, NULL);
6667 bset = isl_basic_set_finalize(bset);
6669 return bset;
6670 error:
6671 isl_mat_free(eq);
6672 isl_basic_set_free(bset);
6673 return NULL;
6676 /* Intersect the set "set" with the affine space specified by the
6677 * equalities in "eq".
6679 static struct isl_set *set_append_equalities(struct isl_set *set,
6680 struct isl_mat *eq)
6682 int i;
6684 if (!set || !eq)
6685 goto error;
6687 for (i = 0; i < set->n; ++i) {
6688 set->p[i] = basic_set_append_equalities(set->p[i],
6689 isl_mat_copy(eq));
6690 if (!set->p[i])
6691 goto error;
6693 isl_mat_free(eq);
6694 return set;
6695 error:
6696 isl_mat_free(eq);
6697 isl_set_free(set);
6698 return NULL;
6701 /* Given a basic set "bset" that only involves parameters and existentially
6702 * quantified variables, return the index of the first equality
6703 * that only involves parameters. If there is no such equality then
6704 * return bset->n_eq.
6706 * This function assumes that isl_basic_set_gauss has been called on "bset".
6708 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6710 int i, j;
6711 unsigned nparam, n_div;
6713 if (!bset)
6714 return -1;
6716 nparam = isl_basic_set_dim(bset, isl_dim_param);
6717 n_div = isl_basic_set_dim(bset, isl_dim_div);
6719 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6720 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6721 ++i;
6724 return i;
6727 /* Compute an explicit representation for the existentially quantified
6728 * variables in "bset" by computing the "minimal value" of the set
6729 * variables. Since there are no set variables, the computation of
6730 * the minimal value essentially computes an explicit representation
6731 * of the non-empty part(s) of "bset".
6733 * The input only involves parameters and existentially quantified variables.
6734 * All equalities among parameters have been removed.
6736 * Since the existentially quantified variables in the result are in general
6737 * going to be different from those in the input, we first replace
6738 * them by the minimal number of variables based on their equalities.
6739 * This should simplify the parametric integer programming.
6741 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6743 isl_morph *morph1, *morph2;
6744 isl_set *set;
6745 unsigned n;
6747 if (!bset)
6748 return NULL;
6749 if (bset->n_eq == 0)
6750 return isl_basic_set_lexmin(bset);
6752 morph1 = isl_basic_set_parameter_compression(bset);
6753 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6754 bset = isl_basic_set_lift(bset);
6755 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6756 bset = isl_morph_basic_set(morph2, bset);
6757 n = isl_basic_set_dim(bset, isl_dim_set);
6758 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6760 set = isl_basic_set_lexmin(bset);
6762 set = isl_morph_set(isl_morph_inverse(morph1), set);
6764 return set;
6767 /* Project the given basic set onto its parameter domain, possibly introducing
6768 * new, explicit, existential variables in the constraints.
6769 * The input has parameters and (possibly implicit) existential variables.
6770 * The output has the same parameters, but only
6771 * explicit existentially quantified variables.
6773 * The actual projection is performed by pip, but pip doesn't seem
6774 * to like equalities very much, so we first remove the equalities
6775 * among the parameters by performing a variable compression on
6776 * the parameters. Afterward, an inverse transformation is performed
6777 * and the equalities among the parameters are inserted back in.
6779 * The variable compression on the parameters may uncover additional
6780 * equalities that were only implicit before. We therefore check
6781 * if there are any new parameter equalities in the result and
6782 * if so recurse. The removal of parameter equalities is required
6783 * for the parameter compression performed by base_compute_divs.
6785 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6787 int i;
6788 struct isl_mat *eq;
6789 struct isl_mat *T, *T2;
6790 struct isl_set *set;
6791 unsigned nparam;
6793 bset = isl_basic_set_cow(bset);
6794 if (!bset)
6795 return NULL;
6797 if (bset->n_eq == 0)
6798 return base_compute_divs(bset);
6800 bset = isl_basic_set_gauss(bset, NULL);
6801 if (!bset)
6802 return NULL;
6803 if (isl_basic_set_plain_is_empty(bset))
6804 return isl_set_from_basic_set(bset);
6806 i = first_parameter_equality(bset);
6807 if (i == bset->n_eq)
6808 return base_compute_divs(bset);
6810 nparam = isl_basic_set_dim(bset, isl_dim_param);
6811 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6812 0, 1 + nparam);
6813 eq = isl_mat_cow(eq);
6814 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6815 if (T && T->n_col == 0) {
6816 isl_mat_free(T);
6817 isl_mat_free(T2);
6818 isl_mat_free(eq);
6819 bset = isl_basic_set_set_to_empty(bset);
6820 return isl_set_from_basic_set(bset);
6822 bset = basic_set_parameter_preimage(bset, T);
6824 i = first_parameter_equality(bset);
6825 if (!bset)
6826 set = NULL;
6827 else if (i == bset->n_eq)
6828 set = base_compute_divs(bset);
6829 else
6830 set = parameter_compute_divs(bset);
6831 set = set_parameter_preimage(set, T2);
6832 set = set_append_equalities(set, eq);
6833 return set;
6836 /* Insert the divs from "ls" before those of "bmap".
6838 * The number of columns is not changed, which means that the last
6839 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6840 * The caller is responsible for removing the same number of dimensions
6841 * from the space of "bmap".
6843 static __isl_give isl_basic_map *insert_divs_from_local_space(
6844 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6846 int i;
6847 int n_div;
6848 int old_n_div;
6850 n_div = isl_local_space_dim(ls, isl_dim_div);
6851 if (n_div == 0)
6852 return bmap;
6854 old_n_div = bmap->n_div;
6855 bmap = insert_div_rows(bmap, n_div);
6856 if (!bmap)
6857 return NULL;
6859 for (i = 0; i < n_div; ++i) {
6860 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6861 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6864 return bmap;
6867 /* Replace the space of "bmap" by the space and divs of "ls".
6869 * If "ls" has any divs, then we simplify the result since we may
6870 * have discovered some additional equalities that could simplify
6871 * the div expressions.
6873 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6874 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6876 int n_div;
6878 bmap = isl_basic_map_cow(bmap);
6879 if (!bmap || !ls)
6880 goto error;
6882 n_div = isl_local_space_dim(ls, isl_dim_div);
6883 bmap = insert_divs_from_local_space(bmap, ls);
6884 if (!bmap)
6885 goto error;
6887 isl_space_free(bmap->dim);
6888 bmap->dim = isl_local_space_get_space(ls);
6889 if (!bmap->dim)
6890 goto error;
6892 isl_local_space_free(ls);
6893 if (n_div > 0)
6894 bmap = isl_basic_map_simplify(bmap);
6895 bmap = isl_basic_map_finalize(bmap);
6896 return bmap;
6897 error:
6898 isl_basic_map_free(bmap);
6899 isl_local_space_free(ls);
6900 return NULL;
6903 /* Replace the space of "map" by the space and divs of "ls".
6905 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6906 __isl_take isl_local_space *ls)
6908 int i;
6910 map = isl_map_cow(map);
6911 if (!map || !ls)
6912 goto error;
6914 for (i = 0; i < map->n; ++i) {
6915 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6916 isl_local_space_copy(ls));
6917 if (!map->p[i])
6918 goto error;
6920 isl_space_free(map->dim);
6921 map->dim = isl_local_space_get_space(ls);
6922 if (!map->dim)
6923 goto error;
6925 isl_local_space_free(ls);
6926 return map;
6927 error:
6928 isl_local_space_free(ls);
6929 isl_map_free(map);
6930 return NULL;
6933 /* Compute an explicit representation for the existentially
6934 * quantified variables for which do not know any explicit representation yet.
6936 * We first sort the existentially quantified variables so that the
6937 * existentially quantified variables for which we already have an explicit
6938 * representation are placed before those for which we do not.
6939 * The input dimensions, the output dimensions and the existentially
6940 * quantified variables for which we already have an explicit
6941 * representation are then turned into parameters.
6942 * compute_divs returns a map with the same parameters and
6943 * no input or output dimensions and the dimension specification
6944 * is reset to that of the input, including the existentially quantified
6945 * variables for which we already had an explicit representation.
6947 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6949 struct isl_basic_set *bset;
6950 struct isl_set *set;
6951 struct isl_map *map;
6952 isl_space *dim;
6953 isl_local_space *ls;
6954 unsigned nparam;
6955 unsigned n_in;
6956 unsigned n_out;
6957 unsigned n_known;
6958 int i;
6960 bmap = isl_basic_map_sort_divs(bmap);
6961 bmap = isl_basic_map_cow(bmap);
6962 if (!bmap)
6963 return NULL;
6965 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6966 if (isl_int_is_zero(bmap->div[n_known][0]))
6967 break;
6969 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6970 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6971 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6972 dim = isl_space_set_alloc(bmap->ctx,
6973 nparam + n_in + n_out + n_known, 0);
6974 if (!dim)
6975 goto error;
6977 ls = isl_basic_map_get_local_space(bmap);
6978 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6979 n_known, bmap->n_div - n_known);
6980 if (n_known > 0) {
6981 for (i = n_known; i < bmap->n_div; ++i)
6982 swap_div(bmap, i - n_known, i);
6983 bmap->n_div -= n_known;
6984 bmap->extra -= n_known;
6986 bmap = isl_basic_map_reset_space(bmap, dim);
6987 bset = (struct isl_basic_set *)bmap;
6989 set = parameter_compute_divs(bset);
6990 map = (struct isl_map *)set;
6991 map = replace_space_by_local_space(map, ls);
6993 return map;
6994 error:
6995 isl_basic_map_free(bmap);
6996 return NULL;
6999 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7001 int i;
7002 unsigned off;
7004 if (!bmap)
7005 return -1;
7007 off = isl_space_dim(bmap->dim, isl_dim_all);
7008 for (i = 0; i < bmap->n_div; ++i) {
7009 if (isl_int_is_zero(bmap->div[i][0]))
7010 return 0;
7011 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
7012 return -1);
7014 return 1;
7017 static int map_divs_known(__isl_keep isl_map *map)
7019 int i;
7021 if (!map)
7022 return -1;
7024 for (i = 0; i < map->n; ++i) {
7025 int known = isl_basic_map_divs_known(map->p[i]);
7026 if (known <= 0)
7027 return known;
7030 return 1;
7033 /* If bmap contains any unknown divs, then compute explicit
7034 * expressions for them. However, this computation may be
7035 * quite expensive, so first try to remove divs that aren't
7036 * strictly needed.
7038 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7040 int known;
7041 struct isl_map *map;
7043 known = isl_basic_map_divs_known(bmap);
7044 if (known < 0)
7045 goto error;
7046 if (known)
7047 return isl_map_from_basic_map(bmap);
7049 bmap = isl_basic_map_drop_redundant_divs(bmap);
7051 known = isl_basic_map_divs_known(bmap);
7052 if (known < 0)
7053 goto error;
7054 if (known)
7055 return isl_map_from_basic_map(bmap);
7057 map = compute_divs(bmap);
7058 return map;
7059 error:
7060 isl_basic_map_free(bmap);
7061 return NULL;
7064 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7066 int i;
7067 int known;
7068 struct isl_map *res;
7070 if (!map)
7071 return NULL;
7072 if (map->n == 0)
7073 return map;
7075 known = map_divs_known(map);
7076 if (known < 0) {
7077 isl_map_free(map);
7078 return NULL;
7080 if (known)
7081 return map;
7083 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7084 for (i = 1 ; i < map->n; ++i) {
7085 struct isl_map *r2;
7086 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7087 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7088 res = isl_map_union_disjoint(res, r2);
7089 else
7090 res = isl_map_union(res, r2);
7092 isl_map_free(map);
7094 return res;
7097 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7099 return (struct isl_set *)
7100 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7103 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7105 return (struct isl_set *)
7106 isl_map_compute_divs((struct isl_map *)set);
7109 struct isl_set *isl_map_domain(struct isl_map *map)
7111 int i;
7112 struct isl_set *set;
7114 if (!map)
7115 goto error;
7117 map = isl_map_cow(map);
7118 if (!map)
7119 return NULL;
7121 set = (struct isl_set *)map;
7122 set->dim = isl_space_domain(set->dim);
7123 if (!set->dim)
7124 goto error;
7125 for (i = 0; i < map->n; ++i) {
7126 set->p[i] = isl_basic_map_domain(map->p[i]);
7127 if (!set->p[i])
7128 goto error;
7130 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7131 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7132 return set;
7133 error:
7134 isl_map_free(map);
7135 return NULL;
7138 /* Return the union of "map1" and "map2", where we assume for now that
7139 * "map1" and "map2" are disjoint. Note that the basic maps inside
7140 * "map1" or "map2" may not be disjoint from each other.
7141 * Also note that this function is also called from isl_map_union,
7142 * which takes care of handling the situation where "map1" and "map2"
7143 * may not be disjoint.
7145 * If one of the inputs is empty, we can simply return the other input.
7146 * Similarly, if one of the inputs is universal, then it is equal to the union.
7148 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7149 __isl_take isl_map *map2)
7151 int i;
7152 unsigned flags = 0;
7153 struct isl_map *map = NULL;
7154 int is_universe;
7156 if (!map1 || !map2)
7157 goto error;
7159 if (!isl_space_is_equal(map1->dim, map2->dim))
7160 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7161 "spaces don't match", goto error);
7163 if (map1->n == 0) {
7164 isl_map_free(map1);
7165 return map2;
7167 if (map2->n == 0) {
7168 isl_map_free(map2);
7169 return map1;
7172 is_universe = isl_map_plain_is_universe(map1);
7173 if (is_universe < 0)
7174 goto error;
7175 if (is_universe) {
7176 isl_map_free(map2);
7177 return map1;
7180 is_universe = isl_map_plain_is_universe(map2);
7181 if (is_universe < 0)
7182 goto error;
7183 if (is_universe) {
7184 isl_map_free(map1);
7185 return map2;
7188 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7189 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7190 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7192 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7193 map1->n + map2->n, flags);
7194 if (!map)
7195 goto error;
7196 for (i = 0; i < map1->n; ++i) {
7197 map = isl_map_add_basic_map(map,
7198 isl_basic_map_copy(map1->p[i]));
7199 if (!map)
7200 goto error;
7202 for (i = 0; i < map2->n; ++i) {
7203 map = isl_map_add_basic_map(map,
7204 isl_basic_map_copy(map2->p[i]));
7205 if (!map)
7206 goto error;
7208 isl_map_free(map1);
7209 isl_map_free(map2);
7210 return map;
7211 error:
7212 isl_map_free(map);
7213 isl_map_free(map1);
7214 isl_map_free(map2);
7215 return NULL;
7218 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7219 * guaranteed to be disjoint by the caller.
7221 * Note that this functions is called from within isl_map_make_disjoint,
7222 * so we have to be careful not to touch the constraints of the inputs
7223 * in any way.
7225 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7226 __isl_take isl_map *map2)
7228 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7231 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7232 * not be disjoint. The parameters are assumed to have been aligned.
7234 * We currently simply call map_union_disjoint, the internal operation
7235 * of which does not really depend on the inputs being disjoint.
7236 * If the result contains more than one basic map, then we clear
7237 * the disjoint flag since the result may contain basic maps from
7238 * both inputs and these are not guaranteed to be disjoint.
7240 * As a special case, if "map1" and "map2" are obviously equal,
7241 * then we simply return "map1".
7243 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7244 __isl_take isl_map *map2)
7246 int equal;
7248 if (!map1 || !map2)
7249 goto error;
7251 equal = isl_map_plain_is_equal(map1, map2);
7252 if (equal < 0)
7253 goto error;
7254 if (equal) {
7255 isl_map_free(map2);
7256 return map1;
7259 map1 = map_union_disjoint(map1, map2);
7260 if (!map1)
7261 return NULL;
7262 if (map1->n > 1)
7263 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7264 return map1;
7265 error:
7266 isl_map_free(map1);
7267 isl_map_free(map2);
7268 return NULL;
7271 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7272 * not be disjoint.
7274 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7275 __isl_take isl_map *map2)
7277 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7280 struct isl_set *isl_set_union_disjoint(
7281 struct isl_set *set1, struct isl_set *set2)
7283 return (struct isl_set *)
7284 isl_map_union_disjoint(
7285 (struct isl_map *)set1, (struct isl_map *)set2);
7288 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7290 return (struct isl_set *)
7291 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7294 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7295 * the results.
7297 * "map" and "set" are assumed to be compatible and non-NULL.
7299 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7300 __isl_take isl_set *set,
7301 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7302 __isl_take isl_basic_set *bset))
7304 unsigned flags = 0;
7305 struct isl_map *result;
7306 int i, j;
7308 if (isl_set_plain_is_universe(set)) {
7309 isl_set_free(set);
7310 return map;
7313 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7314 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7315 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7317 result = isl_map_alloc_space(isl_space_copy(map->dim),
7318 map->n * set->n, flags);
7319 for (i = 0; result && i < map->n; ++i)
7320 for (j = 0; j < set->n; ++j) {
7321 result = isl_map_add_basic_map(result,
7322 fn(isl_basic_map_copy(map->p[i]),
7323 isl_basic_set_copy(set->p[j])));
7324 if (!result)
7325 break;
7328 isl_map_free(map);
7329 isl_set_free(set);
7330 return result;
7333 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7334 __isl_take isl_set *set)
7336 if (!map || !set)
7337 goto error;
7339 if (!isl_map_compatible_range(map, set))
7340 isl_die(set->ctx, isl_error_invalid,
7341 "incompatible spaces", goto error);
7343 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7344 error:
7345 isl_map_free(map);
7346 isl_set_free(set);
7347 return NULL;
7350 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7351 __isl_take isl_set *set)
7353 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7356 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7357 __isl_take isl_set *set)
7359 if (!map || !set)
7360 goto error;
7362 if (!isl_map_compatible_domain(map, set))
7363 isl_die(set->ctx, isl_error_invalid,
7364 "incompatible spaces", goto error);
7366 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7367 error:
7368 isl_map_free(map);
7369 isl_set_free(set);
7370 return NULL;
7373 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7374 __isl_take isl_set *set)
7376 return isl_map_align_params_map_map_and(map, set,
7377 &map_intersect_domain);
7380 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7381 __isl_take isl_map *map2)
7383 if (!map1 || !map2)
7384 goto error;
7385 map1 = isl_map_reverse(map1);
7386 map1 = isl_map_apply_range(map1, map2);
7387 return isl_map_reverse(map1);
7388 error:
7389 isl_map_free(map1);
7390 isl_map_free(map2);
7391 return NULL;
7394 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7395 __isl_take isl_map *map2)
7397 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7400 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7401 __isl_take isl_map *map2)
7403 isl_space *dim_result;
7404 struct isl_map *result;
7405 int i, j;
7407 if (!map1 || !map2)
7408 goto error;
7410 dim_result = isl_space_join(isl_space_copy(map1->dim),
7411 isl_space_copy(map2->dim));
7413 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7414 if (!result)
7415 goto error;
7416 for (i = 0; i < map1->n; ++i)
7417 for (j = 0; j < map2->n; ++j) {
7418 result = isl_map_add_basic_map(result,
7419 isl_basic_map_apply_range(
7420 isl_basic_map_copy(map1->p[i]),
7421 isl_basic_map_copy(map2->p[j])));
7422 if (!result)
7423 goto error;
7425 isl_map_free(map1);
7426 isl_map_free(map2);
7427 if (result && result->n <= 1)
7428 ISL_F_SET(result, ISL_MAP_DISJOINT);
7429 return result;
7430 error:
7431 isl_map_free(map1);
7432 isl_map_free(map2);
7433 return NULL;
7436 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7437 __isl_take isl_map *map2)
7439 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7443 * returns range - domain
7445 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7447 isl_space *dims, *target_dim;
7448 struct isl_basic_set *bset;
7449 unsigned dim;
7450 unsigned nparam;
7451 int i;
7453 if (!bmap)
7454 goto error;
7455 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
7456 bmap->dim, isl_dim_out),
7457 goto error);
7458 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7459 dim = isl_basic_map_n_in(bmap);
7460 nparam = isl_basic_map_n_param(bmap);
7461 bset = isl_basic_set_from_basic_map(bmap);
7462 bset = isl_basic_set_cow(bset);
7463 dims = isl_basic_set_get_space(bset);
7464 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7465 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7466 bset = isl_basic_set_swap_vars(bset, 2*dim);
7467 for (i = 0; i < dim; ++i) {
7468 int j = isl_basic_map_alloc_equality(
7469 (struct isl_basic_map *)bset);
7470 if (j < 0) {
7471 bset = isl_basic_set_free(bset);
7472 break;
7474 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7475 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7476 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7477 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7479 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7480 bset = isl_basic_set_reset_space(bset, target_dim);
7481 return bset;
7482 error:
7483 isl_basic_map_free(bmap);
7484 return NULL;
7488 * returns range - domain
7490 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7492 int i;
7493 isl_space *dim;
7494 struct isl_set *result;
7496 if (!map)
7497 return NULL;
7499 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
7500 map->dim, isl_dim_out),
7501 goto error);
7502 dim = isl_map_get_space(map);
7503 dim = isl_space_domain(dim);
7504 result = isl_set_alloc_space(dim, map->n, 0);
7505 if (!result)
7506 goto error;
7507 for (i = 0; i < map->n; ++i)
7508 result = isl_set_add_basic_set(result,
7509 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7510 isl_map_free(map);
7511 return result;
7512 error:
7513 isl_map_free(map);
7514 return NULL;
7518 * returns [domain -> range] -> range - domain
7520 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7521 __isl_take isl_basic_map *bmap)
7523 int i, k;
7524 isl_space *dim;
7525 isl_basic_map *domain;
7526 int nparam, n;
7527 unsigned total;
7529 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
7530 isl_die(bmap->ctx, isl_error_invalid,
7531 "domain and range don't match", goto error);
7533 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7534 n = isl_basic_map_dim(bmap, isl_dim_in);
7536 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7537 domain = isl_basic_map_universe(dim);
7539 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7540 bmap = isl_basic_map_apply_range(bmap, domain);
7541 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7543 total = isl_basic_map_total_dim(bmap);
7545 for (i = 0; i < n; ++i) {
7546 k = isl_basic_map_alloc_equality(bmap);
7547 if (k < 0)
7548 goto error;
7549 isl_seq_clr(bmap->eq[k], 1 + total);
7550 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7551 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7552 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7555 bmap = isl_basic_map_gauss(bmap, NULL);
7556 return isl_basic_map_finalize(bmap);
7557 error:
7558 isl_basic_map_free(bmap);
7559 return NULL;
7563 * returns [domain -> range] -> range - domain
7565 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7567 int i;
7568 isl_space *domain_dim;
7570 if (!map)
7571 return NULL;
7573 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
7574 isl_die(map->ctx, isl_error_invalid,
7575 "domain and range don't match", goto error);
7577 map = isl_map_cow(map);
7578 if (!map)
7579 return NULL;
7581 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7582 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7583 map->dim = isl_space_join(map->dim, domain_dim);
7584 if (!map->dim)
7585 goto error;
7586 for (i = 0; i < map->n; ++i) {
7587 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7588 if (!map->p[i])
7589 goto error;
7591 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7592 return map;
7593 error:
7594 isl_map_free(map);
7595 return NULL;
7598 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7600 struct isl_basic_map *bmap;
7601 unsigned nparam;
7602 unsigned dim;
7603 int i;
7605 if (!dims)
7606 return NULL;
7608 nparam = dims->nparam;
7609 dim = dims->n_out;
7610 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7611 if (!bmap)
7612 goto error;
7614 for (i = 0; i < dim; ++i) {
7615 int j = isl_basic_map_alloc_equality(bmap);
7616 if (j < 0)
7617 goto error;
7618 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7619 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7620 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7622 return isl_basic_map_finalize(bmap);
7623 error:
7624 isl_basic_map_free(bmap);
7625 return NULL;
7628 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7630 if (!dim)
7631 return NULL;
7632 if (dim->n_in != dim->n_out)
7633 isl_die(dim->ctx, isl_error_invalid,
7634 "number of input and output dimensions needs to be "
7635 "the same", goto error);
7636 return basic_map_identity(dim);
7637 error:
7638 isl_space_free(dim);
7639 return NULL;
7642 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7644 if (!model || !model->dim)
7645 return NULL;
7646 return isl_basic_map_identity(isl_space_copy(model->dim));
7649 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7651 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7654 struct isl_map *isl_map_identity_like(struct isl_map *model)
7656 if (!model || !model->dim)
7657 return NULL;
7658 return isl_map_identity(isl_space_copy(model->dim));
7661 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7663 if (!model || !model->dim)
7664 return NULL;
7665 return isl_map_identity(isl_space_copy(model->dim));
7668 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7670 isl_space *dim = isl_set_get_space(set);
7671 isl_map *id;
7672 id = isl_map_identity(isl_space_map_from_set(dim));
7673 return isl_map_intersect_range(id, set);
7676 /* Construct a basic set with all set dimensions having only non-negative
7677 * values.
7679 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7680 __isl_take isl_space *space)
7682 int i;
7683 unsigned nparam;
7684 unsigned dim;
7685 struct isl_basic_set *bset;
7687 if (!space)
7688 return NULL;
7689 nparam = space->nparam;
7690 dim = space->n_out;
7691 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7692 if (!bset)
7693 return NULL;
7694 for (i = 0; i < dim; ++i) {
7695 int k = isl_basic_set_alloc_inequality(bset);
7696 if (k < 0)
7697 goto error;
7698 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7699 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7701 return bset;
7702 error:
7703 isl_basic_set_free(bset);
7704 return NULL;
7707 /* Construct the half-space x_pos >= 0.
7709 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7710 int pos)
7712 int k;
7713 isl_basic_set *nonneg;
7715 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7716 k = isl_basic_set_alloc_inequality(nonneg);
7717 if (k < 0)
7718 goto error;
7719 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7720 isl_int_set_si(nonneg->ineq[k][pos], 1);
7722 return isl_basic_set_finalize(nonneg);
7723 error:
7724 isl_basic_set_free(nonneg);
7725 return NULL;
7728 /* Construct the half-space x_pos <= -1.
7730 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7732 int k;
7733 isl_basic_set *neg;
7735 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7736 k = isl_basic_set_alloc_inequality(neg);
7737 if (k < 0)
7738 goto error;
7739 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7740 isl_int_set_si(neg->ineq[k][0], -1);
7741 isl_int_set_si(neg->ineq[k][pos], -1);
7743 return isl_basic_set_finalize(neg);
7744 error:
7745 isl_basic_set_free(neg);
7746 return NULL;
7749 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7750 enum isl_dim_type type, unsigned first, unsigned n)
7752 int i;
7753 isl_basic_set *nonneg;
7754 isl_basic_set *neg;
7756 if (!set)
7757 return NULL;
7758 if (n == 0)
7759 return set;
7761 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7763 for (i = 0; i < n; ++i) {
7764 nonneg = nonneg_halfspace(isl_set_get_space(set),
7765 pos(set->dim, type) + first + i);
7766 neg = neg_halfspace(isl_set_get_space(set),
7767 pos(set->dim, type) + first + i);
7769 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7772 return set;
7773 error:
7774 isl_set_free(set);
7775 return NULL;
7778 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7779 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7780 void *user)
7782 isl_set *half;
7784 if (!set)
7785 return -1;
7786 if (isl_set_plain_is_empty(set)) {
7787 isl_set_free(set);
7788 return 0;
7790 if (first == len)
7791 return fn(set, signs, user);
7793 signs[first] = 1;
7794 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7795 1 + first));
7796 half = isl_set_intersect(half, isl_set_copy(set));
7797 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7798 goto error;
7800 signs[first] = -1;
7801 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7802 1 + first));
7803 half = isl_set_intersect(half, set);
7804 return foreach_orthant(half, signs, first + 1, len, fn, user);
7805 error:
7806 isl_set_free(set);
7807 return -1;
7810 /* Call "fn" on the intersections of "set" with each of the orthants
7811 * (except for obviously empty intersections). The orthant is identified
7812 * by the signs array, with each entry having value 1 or -1 according
7813 * to the sign of the corresponding variable.
7815 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7816 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7817 void *user)
7819 unsigned nparam;
7820 unsigned nvar;
7821 int *signs;
7822 int r;
7824 if (!set)
7825 return -1;
7826 if (isl_set_plain_is_empty(set))
7827 return 0;
7829 nparam = isl_set_dim(set, isl_dim_param);
7830 nvar = isl_set_dim(set, isl_dim_set);
7832 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7834 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7835 fn, user);
7837 free(signs);
7839 return r;
7842 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7844 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7847 int isl_basic_map_is_subset(
7848 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7850 int is_subset;
7851 struct isl_map *map1;
7852 struct isl_map *map2;
7854 if (!bmap1 || !bmap2)
7855 return -1;
7857 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7858 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7860 is_subset = isl_map_is_subset(map1, map2);
7862 isl_map_free(map1);
7863 isl_map_free(map2);
7865 return is_subset;
7868 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7869 __isl_keep isl_basic_set *bset2)
7871 return isl_basic_map_is_subset(bset1, bset2);
7874 int isl_basic_map_is_equal(
7875 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7877 int is_subset;
7879 if (!bmap1 || !bmap2)
7880 return -1;
7881 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7882 if (is_subset != 1)
7883 return is_subset;
7884 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7885 return is_subset;
7888 int isl_basic_set_is_equal(
7889 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7891 return isl_basic_map_is_equal(
7892 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7895 int isl_map_is_empty(struct isl_map *map)
7897 int i;
7898 int is_empty;
7900 if (!map)
7901 return -1;
7902 for (i = 0; i < map->n; ++i) {
7903 is_empty = isl_basic_map_is_empty(map->p[i]);
7904 if (is_empty < 0)
7905 return -1;
7906 if (!is_empty)
7907 return 0;
7909 return 1;
7912 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7914 return map ? map->n == 0 : -1;
7917 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7919 return isl_map_plain_is_empty(map);
7922 int isl_set_plain_is_empty(struct isl_set *set)
7924 return set ? set->n == 0 : -1;
7927 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7929 return isl_set_plain_is_empty(set);
7932 int isl_set_is_empty(struct isl_set *set)
7934 return isl_map_is_empty((struct isl_map *)set);
7937 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7939 if (!map1 || !map2)
7940 return -1;
7942 return isl_space_is_equal(map1->dim, map2->dim);
7945 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7947 if (!set1 || !set2)
7948 return -1;
7950 return isl_space_is_equal(set1->dim, set2->dim);
7953 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7955 int is_subset;
7957 if (!map1 || !map2)
7958 return -1;
7959 is_subset = isl_map_is_subset(map1, map2);
7960 if (is_subset != 1)
7961 return is_subset;
7962 is_subset = isl_map_is_subset(map2, map1);
7963 return is_subset;
7966 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7968 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7971 int isl_basic_map_is_strict_subset(
7972 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7974 int is_subset;
7976 if (!bmap1 || !bmap2)
7977 return -1;
7978 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7979 if (is_subset != 1)
7980 return is_subset;
7981 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7982 if (is_subset == -1)
7983 return is_subset;
7984 return !is_subset;
7987 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7989 int is_subset;
7991 if (!map1 || !map2)
7992 return -1;
7993 is_subset = isl_map_is_subset(map1, map2);
7994 if (is_subset != 1)
7995 return is_subset;
7996 is_subset = isl_map_is_subset(map2, map1);
7997 if (is_subset == -1)
7998 return is_subset;
7999 return !is_subset;
8002 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8004 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
8007 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
8009 if (!bmap)
8010 return -1;
8011 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8014 int isl_basic_set_is_universe(struct isl_basic_set *bset)
8016 if (!bset)
8017 return -1;
8018 return bset->n_eq == 0 && bset->n_ineq == 0;
8021 int isl_map_plain_is_universe(__isl_keep isl_map *map)
8023 int i;
8025 if (!map)
8026 return -1;
8028 for (i = 0; i < map->n; ++i) {
8029 int r = isl_basic_map_is_universe(map->p[i]);
8030 if (r < 0 || r)
8031 return r;
8034 return 0;
8037 int isl_set_plain_is_universe(__isl_keep isl_set *set)
8039 return isl_map_plain_is_universe((isl_map *) set);
8042 int isl_set_fast_is_universe(__isl_keep isl_set *set)
8044 return isl_set_plain_is_universe(set);
8047 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
8049 struct isl_basic_set *bset = NULL;
8050 struct isl_vec *sample = NULL;
8051 int empty;
8052 unsigned total;
8054 if (!bmap)
8055 return -1;
8057 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8058 return 1;
8060 if (isl_basic_map_is_universe(bmap))
8061 return 0;
8063 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8064 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8065 copy = isl_basic_map_remove_redundancies(copy);
8066 empty = isl_basic_map_plain_is_empty(copy);
8067 isl_basic_map_free(copy);
8068 return empty;
8071 total = 1 + isl_basic_map_total_dim(bmap);
8072 if (bmap->sample && bmap->sample->size == total) {
8073 int contains = isl_basic_map_contains(bmap, bmap->sample);
8074 if (contains < 0)
8075 return -1;
8076 if (contains)
8077 return 0;
8079 isl_vec_free(bmap->sample);
8080 bmap->sample = NULL;
8081 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8082 if (!bset)
8083 return -1;
8084 sample = isl_basic_set_sample_vec(bset);
8085 if (!sample)
8086 return -1;
8087 empty = sample->size == 0;
8088 isl_vec_free(bmap->sample);
8089 bmap->sample = sample;
8090 if (empty)
8091 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8093 return empty;
8096 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8098 if (!bmap)
8099 return -1;
8100 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8103 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
8105 return isl_basic_map_plain_is_empty(bmap);
8108 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8110 if (!bset)
8111 return -1;
8112 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8115 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
8117 return isl_basic_set_plain_is_empty(bset);
8120 int isl_basic_set_is_empty(struct isl_basic_set *bset)
8122 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8125 struct isl_map *isl_basic_map_union(
8126 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8128 struct isl_map *map;
8129 if (!bmap1 || !bmap2)
8130 goto error;
8132 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8134 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8135 if (!map)
8136 goto error;
8137 map = isl_map_add_basic_map(map, bmap1);
8138 map = isl_map_add_basic_map(map, bmap2);
8139 return map;
8140 error:
8141 isl_basic_map_free(bmap1);
8142 isl_basic_map_free(bmap2);
8143 return NULL;
8146 struct isl_set *isl_basic_set_union(
8147 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8149 return (struct isl_set *)isl_basic_map_union(
8150 (struct isl_basic_map *)bset1,
8151 (struct isl_basic_map *)bset2);
8154 /* Order divs such that any div only depends on previous divs */
8155 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8157 int i;
8158 unsigned off;
8160 if (!bmap)
8161 return NULL;
8163 off = isl_space_dim(bmap->dim, isl_dim_all);
8165 for (i = 0; i < bmap->n_div; ++i) {
8166 int pos;
8167 if (isl_int_is_zero(bmap->div[i][0]))
8168 continue;
8169 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8170 bmap->n_div-i);
8171 if (pos == -1)
8172 continue;
8173 isl_basic_map_swap_div(bmap, i, i + pos);
8174 --i;
8176 return bmap;
8179 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8181 return (struct isl_basic_set *)
8182 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8185 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8187 int i;
8189 if (!map)
8190 return 0;
8192 for (i = 0; i < map->n; ++i) {
8193 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8194 if (!map->p[i])
8195 goto error;
8198 return map;
8199 error:
8200 isl_map_free(map);
8201 return NULL;
8204 /* Apply the expansion computed by isl_merge_divs.
8205 * The expansion itself is given by "exp" while the resulting
8206 * list of divs is given by "div".
8208 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8209 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8211 int i, j;
8212 int n_div;
8214 bset = isl_basic_set_cow(bset);
8215 if (!bset || !div)
8216 goto error;
8218 if (div->n_row < bset->n_div)
8219 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8220 "not an expansion", goto error);
8222 n_div = bset->n_div;
8223 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8224 div->n_row - n_div, 0,
8225 2 * (div->n_row - n_div));
8227 for (i = n_div; i < div->n_row; ++i)
8228 if (isl_basic_set_alloc_div(bset) < 0)
8229 goto error;
8231 j = n_div - 1;
8232 for (i = div->n_row - 1; i >= 0; --i) {
8233 if (j >= 0 && exp[j] == i) {
8234 if (i != j)
8235 isl_basic_map_swap_div(bset, i, j);
8236 j--;
8237 } else {
8238 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8239 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8240 goto error;
8244 isl_mat_free(div);
8245 return bset;
8246 error:
8247 isl_basic_set_free(bset);
8248 isl_mat_free(div);
8249 return NULL;
8252 /* Look for a div in dst that corresponds to the div "div" in src.
8253 * The divs before "div" in src and dst are assumed to be the same.
8255 * Returns -1 if no corresponding div was found and the position
8256 * of the corresponding div in dst otherwise.
8258 static int find_div(struct isl_basic_map *dst,
8259 struct isl_basic_map *src, unsigned div)
8261 int i;
8263 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8265 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8266 for (i = div; i < dst->n_div; ++i)
8267 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8268 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8269 dst->n_div - div) == -1)
8270 return i;
8271 return -1;
8274 /* Align the divs of "dst" to those of "src", adding divs from "src"
8275 * if needed. That is, make sure that the first src->n_div divs
8276 * of the result are equal to those of src.
8278 * The result is not finalized as by design it will have redundant
8279 * divs if any divs from "src" were copied.
8281 __isl_give isl_basic_map *isl_basic_map_align_divs(
8282 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8284 int i;
8285 int known, extended;
8286 unsigned total;
8288 if (!dst || !src)
8289 return isl_basic_map_free(dst);
8291 if (src->n_div == 0)
8292 return dst;
8294 known = isl_basic_map_divs_known(src);
8295 if (known < 0)
8296 return isl_basic_map_free(dst);
8297 if (!known)
8298 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8299 "some src divs are unknown",
8300 return isl_basic_map_free(dst));
8302 src = isl_basic_map_order_divs(src);
8304 extended = 0;
8305 total = isl_space_dim(src->dim, isl_dim_all);
8306 for (i = 0; i < src->n_div; ++i) {
8307 int j = find_div(dst, src, i);
8308 if (j < 0) {
8309 if (!extended) {
8310 int extra = src->n_div - i;
8311 dst = isl_basic_map_cow(dst);
8312 if (!dst)
8313 return NULL;
8314 dst = isl_basic_map_extend_space(dst,
8315 isl_space_copy(dst->dim),
8316 extra, 0, 2 * extra);
8317 extended = 1;
8319 j = isl_basic_map_alloc_div(dst);
8320 if (j < 0)
8321 return isl_basic_map_free(dst);
8322 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8323 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8324 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8325 return isl_basic_map_free(dst);
8327 if (j != i)
8328 isl_basic_map_swap_div(dst, i, j);
8330 return dst;
8333 struct isl_basic_set *isl_basic_set_align_divs(
8334 struct isl_basic_set *dst, struct isl_basic_set *src)
8336 return (struct isl_basic_set *)isl_basic_map_align_divs(
8337 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8340 struct isl_map *isl_map_align_divs(struct isl_map *map)
8342 int i;
8344 if (!map)
8345 return NULL;
8346 if (map->n == 0)
8347 return map;
8348 map = isl_map_compute_divs(map);
8349 map = isl_map_cow(map);
8350 if (!map)
8351 return NULL;
8353 for (i = 1; i < map->n; ++i)
8354 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8355 for (i = 1; i < map->n; ++i) {
8356 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8357 if (!map->p[i])
8358 return isl_map_free(map);
8361 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8362 return map;
8365 struct isl_set *isl_set_align_divs(struct isl_set *set)
8367 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8370 /* Align the divs of the basic sets in "set" to those
8371 * of the basic sets in "list", as well as to the other basic sets in "set".
8372 * The elements in "list" are assumed to have known divs.
8374 __isl_give isl_set *isl_set_align_divs_to_basic_set_list(
8375 __isl_take isl_set *set, __isl_keep isl_basic_set_list *list)
8377 int i, n;
8379 set = isl_set_compute_divs(set);
8380 set = isl_set_cow(set);
8381 if (!set || !list)
8382 return isl_set_free(set);
8383 if (set->n == 0)
8384 return set;
8386 n = isl_basic_set_list_n_basic_set(list);
8387 for (i = 0; i < n; ++i) {
8388 isl_basic_set *bset;
8390 bset = isl_basic_set_list_get_basic_set(list, i);
8391 set->p[0] = isl_basic_set_align_divs(set->p[0], bset);
8392 isl_basic_set_free(bset);
8394 if (!set->p[0])
8395 return isl_set_free(set);
8397 return isl_set_align_divs(set);
8400 /* Align the divs of each element of "list" to those of "bset".
8401 * Both "bset" and the elements of "list" are assumed to have known divs.
8403 __isl_give isl_basic_set_list *isl_basic_set_list_align_divs_to_basic_set(
8404 __isl_take isl_basic_set_list *list, __isl_keep isl_basic_set *bset)
8406 int i, n;
8408 if (!list || !bset)
8409 return isl_basic_set_list_free(list);
8411 n = isl_basic_set_list_n_basic_set(list);
8412 for (i = 0; i < n; ++i) {
8413 isl_basic_set *bset_i;
8415 bset_i = isl_basic_set_list_get_basic_set(list, i);
8416 bset_i = isl_basic_set_align_divs(bset_i, bset);
8417 list = isl_basic_set_list_set_basic_set(list, i, bset_i);
8420 return list;
8423 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8424 __isl_take isl_map *map)
8426 if (!set || !map)
8427 goto error;
8428 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8429 map = isl_map_intersect_domain(map, set);
8430 set = isl_map_range(map);
8431 return set;
8432 error:
8433 isl_set_free(set);
8434 isl_map_free(map);
8435 return NULL;
8438 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8439 __isl_take isl_map *map)
8441 return isl_map_align_params_map_map_and(set, map, &set_apply);
8444 /* There is no need to cow as removing empty parts doesn't change
8445 * the meaning of the set.
8447 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8449 int i;
8451 if (!map)
8452 return NULL;
8454 for (i = map->n - 1; i >= 0; --i)
8455 remove_if_empty(map, i);
8457 return map;
8460 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8462 return (struct isl_set *)
8463 isl_map_remove_empty_parts((struct isl_map *)set);
8466 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8468 struct isl_basic_map *bmap;
8469 if (!map || map->n == 0)
8470 return NULL;
8471 bmap = map->p[map->n-1];
8472 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8473 return isl_basic_map_copy(bmap);
8476 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8478 return (struct isl_basic_set *)
8479 isl_map_copy_basic_map((struct isl_map *)set);
8482 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8483 __isl_keep isl_basic_map *bmap)
8485 int i;
8487 if (!map || !bmap)
8488 goto error;
8489 for (i = map->n-1; i >= 0; --i) {
8490 if (map->p[i] != bmap)
8491 continue;
8492 map = isl_map_cow(map);
8493 if (!map)
8494 goto error;
8495 isl_basic_map_free(map->p[i]);
8496 if (i != map->n-1) {
8497 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8498 map->p[i] = map->p[map->n-1];
8500 map->n--;
8501 return map;
8503 return map;
8504 error:
8505 isl_map_free(map);
8506 return NULL;
8509 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8510 struct isl_basic_set *bset)
8512 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8513 (struct isl_basic_map *)bset);
8516 /* Given two basic sets bset1 and bset2, compute the maximal difference
8517 * between the values of dimension pos in bset1 and those in bset2
8518 * for any common value of the parameters and dimensions preceding pos.
8520 static enum isl_lp_result basic_set_maximal_difference_at(
8521 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8522 int pos, isl_int *opt)
8524 isl_space *dims;
8525 struct isl_basic_map *bmap1 = NULL;
8526 struct isl_basic_map *bmap2 = NULL;
8527 struct isl_ctx *ctx;
8528 struct isl_vec *obj;
8529 unsigned total;
8530 unsigned nparam;
8531 unsigned dim1, dim2;
8532 enum isl_lp_result res;
8534 if (!bset1 || !bset2)
8535 return isl_lp_error;
8537 nparam = isl_basic_set_n_param(bset1);
8538 dim1 = isl_basic_set_n_dim(bset1);
8539 dim2 = isl_basic_set_n_dim(bset2);
8540 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8541 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8542 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8543 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8544 if (!bmap1 || !bmap2)
8545 goto error;
8546 bmap1 = isl_basic_map_cow(bmap1);
8547 bmap1 = isl_basic_map_extend(bmap1, nparam,
8548 pos, (dim1 - pos) + (dim2 - pos),
8549 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8550 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8551 if (!bmap1)
8552 goto error2;
8553 total = isl_basic_map_total_dim(bmap1);
8554 ctx = bmap1->ctx;
8555 obj = isl_vec_alloc(ctx, 1 + total);
8556 if (!obj)
8557 goto error2;
8558 isl_seq_clr(obj->block.data, 1 + total);
8559 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8560 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8561 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8562 opt, NULL, NULL);
8563 isl_basic_map_free(bmap1);
8564 isl_vec_free(obj);
8565 return res;
8566 error:
8567 isl_basic_map_free(bmap2);
8568 error2:
8569 isl_basic_map_free(bmap1);
8570 return isl_lp_error;
8573 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8574 * for any common value of the parameters and dimensions preceding pos
8575 * in both basic sets, the values of dimension pos in bset1 are
8576 * smaller or larger than those in bset2.
8578 * Returns
8579 * 1 if bset1 follows bset2
8580 * -1 if bset1 precedes bset2
8581 * 0 if bset1 and bset2 are incomparable
8582 * -2 if some error occurred.
8584 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8585 struct isl_basic_set *bset2, int pos)
8587 isl_int opt;
8588 enum isl_lp_result res;
8589 int cmp;
8591 isl_int_init(opt);
8593 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8595 if (res == isl_lp_empty)
8596 cmp = 0;
8597 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8598 res == isl_lp_unbounded)
8599 cmp = 1;
8600 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8601 cmp = -1;
8602 else
8603 cmp = -2;
8605 isl_int_clear(opt);
8606 return cmp;
8609 /* Given two basic sets bset1 and bset2, check whether
8610 * for any common value of the parameters and dimensions preceding pos
8611 * there is a value of dimension pos in bset1 that is larger
8612 * than a value of the same dimension in bset2.
8614 * Return
8615 * 1 if there exists such a pair
8616 * 0 if there is no such pair, but there is a pair of equal values
8617 * -1 otherwise
8618 * -2 if some error occurred.
8620 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8621 __isl_keep isl_basic_set *bset2, int pos)
8623 isl_int opt;
8624 enum isl_lp_result res;
8625 int cmp;
8627 isl_int_init(opt);
8629 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8631 if (res == isl_lp_empty)
8632 cmp = -1;
8633 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8634 res == isl_lp_unbounded)
8635 cmp = 1;
8636 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8637 cmp = -1;
8638 else if (res == isl_lp_ok)
8639 cmp = 0;
8640 else
8641 cmp = -2;
8643 isl_int_clear(opt);
8644 return cmp;
8647 /* Given two sets set1 and set2, check whether
8648 * for any common value of the parameters and dimensions preceding pos
8649 * there is a value of dimension pos in set1 that is larger
8650 * than a value of the same dimension in set2.
8652 * Return
8653 * 1 if there exists such a pair
8654 * 0 if there is no such pair, but there is a pair of equal values
8655 * -1 otherwise
8656 * -2 if some error occurred.
8658 int isl_set_follows_at(__isl_keep isl_set *set1,
8659 __isl_keep isl_set *set2, int pos)
8661 int i, j;
8662 int follows = -1;
8664 if (!set1 || !set2)
8665 return -2;
8667 for (i = 0; i < set1->n; ++i)
8668 for (j = 0; j < set2->n; ++j) {
8669 int f;
8670 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8671 if (f == 1 || f == -2)
8672 return f;
8673 if (f > follows)
8674 follows = f;
8677 return follows;
8680 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8681 unsigned pos, isl_int *val)
8683 int i;
8684 int d;
8685 unsigned total;
8687 if (!bmap)
8688 return -1;
8689 total = isl_basic_map_total_dim(bmap);
8690 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8691 for (; d+1 > pos; --d)
8692 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8693 break;
8694 if (d != pos)
8695 continue;
8696 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8697 return 0;
8698 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8699 return 0;
8700 if (!isl_int_is_one(bmap->eq[i][1+d]))
8701 return 0;
8702 if (val)
8703 isl_int_neg(*val, bmap->eq[i][0]);
8704 return 1;
8706 return 0;
8709 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8710 unsigned pos, isl_int *val)
8712 int i;
8713 isl_int v;
8714 isl_int tmp;
8715 int fixed;
8717 if (!map)
8718 return -1;
8719 if (map->n == 0)
8720 return 0;
8721 if (map->n == 1)
8722 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8723 isl_int_init(v);
8724 isl_int_init(tmp);
8725 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8726 for (i = 1; fixed == 1 && i < map->n; ++i) {
8727 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8728 if (fixed == 1 && isl_int_ne(tmp, v))
8729 fixed = 0;
8731 if (val)
8732 isl_int_set(*val, v);
8733 isl_int_clear(tmp);
8734 isl_int_clear(v);
8735 return fixed;
8738 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8739 unsigned pos, isl_int *val)
8741 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8742 pos, val);
8745 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8746 isl_int *val)
8748 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8751 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8752 enum isl_dim_type type, unsigned pos, isl_int *val)
8754 if (pos >= isl_basic_map_dim(bmap, type))
8755 return -1;
8756 return isl_basic_map_plain_has_fixed_var(bmap,
8757 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8760 /* If "bmap" obviously lies on a hyperplane where the given dimension
8761 * has a fixed value, then return that value.
8762 * Otherwise return NaN.
8764 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8765 __isl_keep isl_basic_map *bmap,
8766 enum isl_dim_type type, unsigned pos)
8768 isl_ctx *ctx;
8769 isl_val *v;
8770 int fixed;
8772 if (!bmap)
8773 return NULL;
8774 ctx = isl_basic_map_get_ctx(bmap);
8775 v = isl_val_alloc(ctx);
8776 if (!v)
8777 return NULL;
8778 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8779 if (fixed < 0)
8780 return isl_val_free(v);
8781 if (fixed) {
8782 isl_int_set_si(v->d, 1);
8783 return v;
8785 isl_val_free(v);
8786 return isl_val_nan(ctx);
8789 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8790 enum isl_dim_type type, unsigned pos, isl_int *val)
8792 if (pos >= isl_map_dim(map, type))
8793 return -1;
8794 return isl_map_plain_has_fixed_var(map,
8795 map_offset(map, type) - 1 + pos, val);
8798 /* If "map" obviously lies on a hyperplane where the given dimension
8799 * has a fixed value, then return that value.
8800 * Otherwise return NaN.
8802 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8803 enum isl_dim_type type, unsigned pos)
8805 isl_ctx *ctx;
8806 isl_val *v;
8807 int fixed;
8809 if (!map)
8810 return NULL;
8811 ctx = isl_map_get_ctx(map);
8812 v = isl_val_alloc(ctx);
8813 if (!v)
8814 return NULL;
8815 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8816 if (fixed < 0)
8817 return isl_val_free(v);
8818 if (fixed) {
8819 isl_int_set_si(v->d, 1);
8820 return v;
8822 isl_val_free(v);
8823 return isl_val_nan(ctx);
8826 /* If "set" obviously lies on a hyperplane where the given dimension
8827 * has a fixed value, then return that value.
8828 * Otherwise return NaN.
8830 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8831 enum isl_dim_type type, unsigned pos)
8833 return isl_map_plain_get_val_if_fixed(set, type, pos);
8836 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8837 enum isl_dim_type type, unsigned pos, isl_int *val)
8839 return isl_map_plain_is_fixed(set, type, pos, val);
8842 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8843 enum isl_dim_type type, unsigned pos, isl_int *val)
8845 return isl_map_plain_is_fixed(map, type, pos, val);
8848 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8849 * then return this fixed value in *val.
8851 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8852 unsigned dim, isl_int *val)
8854 return isl_basic_set_plain_has_fixed_var(bset,
8855 isl_basic_set_n_param(bset) + dim, val);
8858 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8859 * then return this fixed value in *val.
8861 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8862 unsigned dim, isl_int *val)
8864 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8867 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8868 unsigned dim, isl_int *val)
8870 return isl_set_plain_dim_is_fixed(set, dim, val);
8873 /* Check if input variable in has fixed value and if so and if val is not NULL,
8874 * then return this fixed value in *val.
8876 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8877 unsigned in, isl_int *val)
8879 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8882 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8883 * and if val is not NULL, then return this lower bound in *val.
8885 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8886 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8888 int i, i_eq = -1, i_ineq = -1;
8889 isl_int *c;
8890 unsigned total;
8891 unsigned nparam;
8893 if (!bset)
8894 return -1;
8895 total = isl_basic_set_total_dim(bset);
8896 nparam = isl_basic_set_n_param(bset);
8897 for (i = 0; i < bset->n_eq; ++i) {
8898 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8899 continue;
8900 if (i_eq != -1)
8901 return 0;
8902 i_eq = i;
8904 for (i = 0; i < bset->n_ineq; ++i) {
8905 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8906 continue;
8907 if (i_eq != -1 || i_ineq != -1)
8908 return 0;
8909 i_ineq = i;
8911 if (i_eq == -1 && i_ineq == -1)
8912 return 0;
8913 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8914 /* The coefficient should always be one due to normalization. */
8915 if (!isl_int_is_one(c[1+nparam+dim]))
8916 return 0;
8917 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8918 return 0;
8919 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8920 total - nparam - dim - 1) != -1)
8921 return 0;
8922 if (val)
8923 isl_int_neg(*val, c[0]);
8924 return 1;
8927 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8928 unsigned dim, isl_int *val)
8930 int i;
8931 isl_int v;
8932 isl_int tmp;
8933 int fixed;
8935 if (!set)
8936 return -1;
8937 if (set->n == 0)
8938 return 0;
8939 if (set->n == 1)
8940 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8941 dim, val);
8942 isl_int_init(v);
8943 isl_int_init(tmp);
8944 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8945 dim, &v);
8946 for (i = 1; fixed == 1 && i < set->n; ++i) {
8947 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8948 dim, &tmp);
8949 if (fixed == 1 && isl_int_ne(tmp, v))
8950 fixed = 0;
8952 if (val)
8953 isl_int_set(*val, v);
8954 isl_int_clear(tmp);
8955 isl_int_clear(v);
8956 return fixed;
8959 /* uset_gist depends on constraints without existentially quantified
8960 * variables sorting first.
8962 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8964 isl_int **c1 = (isl_int **) p1;
8965 isl_int **c2 = (isl_int **) p2;
8966 int l1, l2;
8967 unsigned size = *(unsigned *) arg;
8969 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8970 l2 = isl_seq_last_non_zero(*c2 + 1, size);
8972 if (l1 != l2)
8973 return l1 - l2;
8975 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
8978 static struct isl_basic_map *isl_basic_map_sort_constraints(
8979 struct isl_basic_map *bmap)
8981 unsigned total;
8983 if (!bmap)
8984 return NULL;
8985 if (bmap->n_ineq == 0)
8986 return bmap;
8987 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8988 return bmap;
8989 total = isl_basic_map_total_dim(bmap);
8990 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
8991 &sort_constraint_cmp, &total) < 0)
8992 return isl_basic_map_free(bmap);
8993 return bmap;
8996 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8997 __isl_take isl_basic_set *bset)
8999 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9000 (struct isl_basic_map *)bset);
9003 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9005 if (!bmap)
9006 return NULL;
9007 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9008 return bmap;
9009 bmap = isl_basic_map_remove_redundancies(bmap);
9010 bmap = isl_basic_map_sort_constraints(bmap);
9011 if (bmap)
9012 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9013 return bmap;
9016 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9018 return (struct isl_basic_set *)isl_basic_map_normalize(
9019 (struct isl_basic_map *)bset);
9022 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9023 const __isl_keep isl_basic_map *bmap2)
9025 int i, cmp;
9026 unsigned total;
9028 if (bmap1 == bmap2)
9029 return 0;
9030 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9031 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9032 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9033 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9034 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9035 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9036 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9037 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9038 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9039 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9040 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9041 return 0;
9042 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9043 return 1;
9044 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9045 return -1;
9046 if (bmap1->n_eq != bmap2->n_eq)
9047 return bmap1->n_eq - bmap2->n_eq;
9048 if (bmap1->n_ineq != bmap2->n_ineq)
9049 return bmap1->n_ineq - bmap2->n_ineq;
9050 if (bmap1->n_div != bmap2->n_div)
9051 return bmap1->n_div - bmap2->n_div;
9052 total = isl_basic_map_total_dim(bmap1);
9053 for (i = 0; i < bmap1->n_eq; ++i) {
9054 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9055 if (cmp)
9056 return cmp;
9058 for (i = 0; i < bmap1->n_ineq; ++i) {
9059 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9060 if (cmp)
9061 return cmp;
9063 for (i = 0; i < bmap1->n_div; ++i) {
9064 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9065 if (cmp)
9066 return cmp;
9068 return 0;
9071 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9072 const __isl_keep isl_basic_set *bset2)
9074 return isl_basic_map_plain_cmp(bset1, bset2);
9077 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9079 int i, cmp;
9081 if (set1 == set2)
9082 return 0;
9083 if (set1->n != set2->n)
9084 return set1->n - set2->n;
9086 for (i = 0; i < set1->n; ++i) {
9087 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9088 if (cmp)
9089 return cmp;
9092 return 0;
9095 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9096 __isl_keep isl_basic_map *bmap2)
9098 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9101 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9102 __isl_keep isl_basic_set *bset2)
9104 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9105 (isl_basic_map *)bset2);
9108 static int qsort_bmap_cmp(const void *p1, const void *p2)
9110 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9111 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9113 return isl_basic_map_plain_cmp(bmap1, bmap2);
9116 /* Sort the basic maps of "map" and remove duplicate basic maps.
9118 * While removing basic maps, we make sure that the basic maps remain
9119 * sorted because isl_map_normalize expects the basic maps of the result
9120 * to be sorted.
9122 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9124 int i, j;
9126 map = isl_map_remove_empty_parts(map);
9127 if (!map)
9128 return NULL;
9129 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9130 for (i = map->n - 1; i >= 1; --i) {
9131 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9132 continue;
9133 isl_basic_map_free(map->p[i-1]);
9134 for (j = i; j < map->n; ++j)
9135 map->p[j - 1] = map->p[j];
9136 map->n--;
9139 return map;
9142 /* Remove obvious duplicates among the basic maps of "map".
9144 * Unlike isl_map_normalize, this function does not remove redundant
9145 * constraints and only removes duplicates that have exactly the same
9146 * constraints in the input. It does sort the constraints and
9147 * the basic maps to ease the detection of duplicates.
9149 * If "map" has already been normalized or if the basic maps are
9150 * disjoint, then there can be no duplicates.
9152 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9154 int i;
9155 isl_basic_map *bmap;
9157 if (!map)
9158 return NULL;
9159 if (map->n <= 1)
9160 return map;
9161 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9162 return map;
9163 for (i = 0; i < map->n; ++i) {
9164 bmap = isl_basic_map_copy(map->p[i]);
9165 bmap = isl_basic_map_sort_constraints(bmap);
9166 if (!bmap)
9167 return isl_map_free(map);
9168 isl_basic_map_free(map->p[i]);
9169 map->p[i] = bmap;
9172 map = sort_and_remove_duplicates(map);
9173 return map;
9176 /* We normalize in place, but if anything goes wrong we need
9177 * to return NULL, so we need to make sure we don't change the
9178 * meaning of any possible other copies of map.
9180 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9182 int i;
9183 struct isl_basic_map *bmap;
9185 if (!map)
9186 return NULL;
9187 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9188 return map;
9189 for (i = 0; i < map->n; ++i) {
9190 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9191 if (!bmap)
9192 goto error;
9193 isl_basic_map_free(map->p[i]);
9194 map->p[i] = bmap;
9197 map = sort_and_remove_duplicates(map);
9198 if (map)
9199 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9200 return map;
9201 error:
9202 isl_map_free(map);
9203 return NULL;
9206 struct isl_set *isl_set_normalize(struct isl_set *set)
9208 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9211 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9213 int i;
9214 int equal;
9216 if (!map1 || !map2)
9217 return -1;
9219 if (map1 == map2)
9220 return 1;
9221 if (!isl_space_is_equal(map1->dim, map2->dim))
9222 return 0;
9224 map1 = isl_map_copy(map1);
9225 map2 = isl_map_copy(map2);
9226 map1 = isl_map_normalize(map1);
9227 map2 = isl_map_normalize(map2);
9228 if (!map1 || !map2)
9229 goto error;
9230 equal = map1->n == map2->n;
9231 for (i = 0; equal && i < map1->n; ++i) {
9232 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9233 if (equal < 0)
9234 goto error;
9236 isl_map_free(map1);
9237 isl_map_free(map2);
9238 return equal;
9239 error:
9240 isl_map_free(map1);
9241 isl_map_free(map2);
9242 return -1;
9245 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9247 return isl_map_plain_is_equal(map1, map2);
9250 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9252 return isl_map_plain_is_equal((struct isl_map *)set1,
9253 (struct isl_map *)set2);
9256 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9258 return isl_set_plain_is_equal(set1, set2);
9261 /* Return an interval that ranges from min to max (inclusive)
9263 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9264 isl_int min, isl_int max)
9266 int k;
9267 struct isl_basic_set *bset = NULL;
9269 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9270 if (!bset)
9271 goto error;
9273 k = isl_basic_set_alloc_inequality(bset);
9274 if (k < 0)
9275 goto error;
9276 isl_int_set_si(bset->ineq[k][1], 1);
9277 isl_int_neg(bset->ineq[k][0], min);
9279 k = isl_basic_set_alloc_inequality(bset);
9280 if (k < 0)
9281 goto error;
9282 isl_int_set_si(bset->ineq[k][1], -1);
9283 isl_int_set(bset->ineq[k][0], max);
9285 return bset;
9286 error:
9287 isl_basic_set_free(bset);
9288 return NULL;
9291 /* Return the basic sets in "set" as a list.
9293 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
9294 __isl_keep isl_set *set)
9296 int i;
9297 isl_ctx *ctx;
9298 isl_basic_set_list *list;
9300 if (!set)
9301 return NULL;
9302 ctx = isl_set_get_ctx(set);
9303 list = isl_basic_set_list_alloc(ctx, set->n);
9305 for (i = 0; i < set->n; ++i) {
9306 isl_basic_set *bset;
9308 bset = isl_basic_set_copy(set->p[i]);
9309 list = isl_basic_set_list_add(list, bset);
9312 return list;
9315 /* Return the intersection of the elements in the non-empty list "list".
9316 * All elements are assumed to live in the same space.
9318 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9319 __isl_take struct isl_basic_set_list *list)
9321 int i, n;
9322 isl_basic_set *bset;
9324 if (!list)
9325 return NULL;
9326 n = isl_basic_set_list_n_basic_set(list);
9327 if (n < 1)
9328 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9329 "expecting non-empty list", goto error);
9331 bset = isl_basic_set_list_get_basic_set(list, 0);
9332 for (i = 1; i < n; ++i) {
9333 isl_basic_set *bset_i;
9335 bset_i = isl_basic_set_list_get_basic_set(list, i);
9336 bset = isl_basic_set_intersect(bset, bset_i);
9339 isl_basic_set_list_free(list);
9340 return bset;
9341 error:
9342 isl_basic_set_list_free(list);
9343 return NULL;
9346 /* Return the Cartesian product of the basic sets in list (in the given order).
9348 __isl_give isl_basic_set *isl_basic_set_list_product(
9349 __isl_take struct isl_basic_set_list *list)
9351 int i;
9352 unsigned dim;
9353 unsigned nparam;
9354 unsigned extra;
9355 unsigned n_eq;
9356 unsigned n_ineq;
9357 struct isl_basic_set *product = NULL;
9359 if (!list)
9360 goto error;
9361 isl_assert(list->ctx, list->n > 0, goto error);
9362 isl_assert(list->ctx, list->p[0], goto error);
9363 nparam = isl_basic_set_n_param(list->p[0]);
9364 dim = isl_basic_set_n_dim(list->p[0]);
9365 extra = list->p[0]->n_div;
9366 n_eq = list->p[0]->n_eq;
9367 n_ineq = list->p[0]->n_ineq;
9368 for (i = 1; i < list->n; ++i) {
9369 isl_assert(list->ctx, list->p[i], goto error);
9370 isl_assert(list->ctx,
9371 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9372 dim += isl_basic_set_n_dim(list->p[i]);
9373 extra += list->p[i]->n_div;
9374 n_eq += list->p[i]->n_eq;
9375 n_ineq += list->p[i]->n_ineq;
9377 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9378 n_eq, n_ineq);
9379 if (!product)
9380 goto error;
9381 dim = 0;
9382 for (i = 0; i < list->n; ++i) {
9383 isl_basic_set_add_constraints(product,
9384 isl_basic_set_copy(list->p[i]), dim);
9385 dim += isl_basic_set_n_dim(list->p[i]);
9387 isl_basic_set_list_free(list);
9388 return product;
9389 error:
9390 isl_basic_set_free(product);
9391 isl_basic_set_list_free(list);
9392 return NULL;
9395 struct isl_basic_map *isl_basic_map_product(
9396 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9398 isl_space *dim_result = NULL;
9399 struct isl_basic_map *bmap;
9400 unsigned in1, in2, out1, out2, nparam, total, pos;
9401 struct isl_dim_map *dim_map1, *dim_map2;
9403 if (!bmap1 || !bmap2)
9404 goto error;
9406 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9407 bmap2->dim, isl_dim_param), goto error);
9408 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9409 isl_space_copy(bmap2->dim));
9411 in1 = isl_basic_map_n_in(bmap1);
9412 in2 = isl_basic_map_n_in(bmap2);
9413 out1 = isl_basic_map_n_out(bmap1);
9414 out2 = isl_basic_map_n_out(bmap2);
9415 nparam = isl_basic_map_n_param(bmap1);
9417 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9418 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9419 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9420 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9421 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9422 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9423 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9424 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9425 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9426 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9427 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9429 bmap = isl_basic_map_alloc_space(dim_result,
9430 bmap1->n_div + bmap2->n_div,
9431 bmap1->n_eq + bmap2->n_eq,
9432 bmap1->n_ineq + bmap2->n_ineq);
9433 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9434 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9435 bmap = isl_basic_map_simplify(bmap);
9436 return isl_basic_map_finalize(bmap);
9437 error:
9438 isl_basic_map_free(bmap1);
9439 isl_basic_map_free(bmap2);
9440 return NULL;
9443 __isl_give isl_basic_map *isl_basic_map_flat_product(
9444 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9446 isl_basic_map *prod;
9448 prod = isl_basic_map_product(bmap1, bmap2);
9449 prod = isl_basic_map_flatten(prod);
9450 return prod;
9453 __isl_give isl_basic_set *isl_basic_set_flat_product(
9454 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9456 return isl_basic_map_flat_range_product(bset1, bset2);
9459 __isl_give isl_basic_map *isl_basic_map_domain_product(
9460 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9462 isl_space *space_result = NULL;
9463 isl_basic_map *bmap;
9464 unsigned in1, in2, out, nparam, total, pos;
9465 struct isl_dim_map *dim_map1, *dim_map2;
9467 if (!bmap1 || !bmap2)
9468 goto error;
9470 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9471 isl_space_copy(bmap2->dim));
9473 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9474 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9475 out = isl_basic_map_dim(bmap1, isl_dim_out);
9476 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9478 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9479 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9480 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9481 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9482 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9483 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9484 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9485 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9486 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9487 isl_dim_map_div(dim_map1, bmap1, pos += out);
9488 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9490 bmap = isl_basic_map_alloc_space(space_result,
9491 bmap1->n_div + bmap2->n_div,
9492 bmap1->n_eq + bmap2->n_eq,
9493 bmap1->n_ineq + bmap2->n_ineq);
9494 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9495 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9496 bmap = isl_basic_map_simplify(bmap);
9497 return isl_basic_map_finalize(bmap);
9498 error:
9499 isl_basic_map_free(bmap1);
9500 isl_basic_map_free(bmap2);
9501 return NULL;
9504 __isl_give isl_basic_map *isl_basic_map_range_product(
9505 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9507 isl_space *dim_result = NULL;
9508 isl_basic_map *bmap;
9509 unsigned in, out1, out2, nparam, total, pos;
9510 struct isl_dim_map *dim_map1, *dim_map2;
9512 if (!bmap1 || !bmap2)
9513 goto error;
9515 if (!isl_space_match(bmap1->dim, isl_dim_param,
9516 bmap2->dim, isl_dim_param))
9517 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9518 "parameters don't match", goto error);
9520 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9521 isl_space_copy(bmap2->dim));
9523 in = isl_basic_map_dim(bmap1, isl_dim_in);
9524 out1 = isl_basic_map_n_out(bmap1);
9525 out2 = isl_basic_map_n_out(bmap2);
9526 nparam = isl_basic_map_n_param(bmap1);
9528 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9529 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9530 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9531 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9532 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9533 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9534 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9535 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9536 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9537 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9538 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9540 bmap = isl_basic_map_alloc_space(dim_result,
9541 bmap1->n_div + bmap2->n_div,
9542 bmap1->n_eq + bmap2->n_eq,
9543 bmap1->n_ineq + bmap2->n_ineq);
9544 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9545 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9546 bmap = isl_basic_map_simplify(bmap);
9547 return isl_basic_map_finalize(bmap);
9548 error:
9549 isl_basic_map_free(bmap1);
9550 isl_basic_map_free(bmap2);
9551 return NULL;
9554 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9555 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9557 isl_basic_map *prod;
9559 prod = isl_basic_map_range_product(bmap1, bmap2);
9560 prod = isl_basic_map_flatten_range(prod);
9561 return prod;
9564 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9565 * and collect the results.
9566 * The result live in the space obtained by calling "space_product"
9567 * on the spaces of "map1" and "map2".
9568 * If "remove_duplicates" is set then the result may contain duplicates
9569 * (even if the inputs do not) and so we try and remove the obvious
9570 * duplicates.
9572 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9573 __isl_take isl_map *map2,
9574 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9575 __isl_take isl_space *right),
9576 __isl_give isl_basic_map *(*basic_map_product)(
9577 __isl_take isl_basic_map *left,
9578 __isl_take isl_basic_map *right),
9579 int remove_duplicates)
9581 unsigned flags = 0;
9582 struct isl_map *result;
9583 int i, j;
9585 if (!map1 || !map2)
9586 goto error;
9588 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9589 map2->dim, isl_dim_param), goto error);
9591 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9592 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9593 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9595 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9596 isl_space_copy(map2->dim)),
9597 map1->n * map2->n, flags);
9598 if (!result)
9599 goto error;
9600 for (i = 0; i < map1->n; ++i)
9601 for (j = 0; j < map2->n; ++j) {
9602 struct isl_basic_map *part;
9603 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9604 isl_basic_map_copy(map2->p[j]));
9605 if (isl_basic_map_is_empty(part))
9606 isl_basic_map_free(part);
9607 else
9608 result = isl_map_add_basic_map(result, part);
9609 if (!result)
9610 goto error;
9612 if (remove_duplicates)
9613 result = isl_map_remove_obvious_duplicates(result);
9614 isl_map_free(map1);
9615 isl_map_free(map2);
9616 return result;
9617 error:
9618 isl_map_free(map1);
9619 isl_map_free(map2);
9620 return NULL;
9623 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9625 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9626 __isl_take isl_map *map2)
9628 return map_product(map1, map2, &isl_space_product,
9629 &isl_basic_map_product, 0);
9632 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9633 __isl_take isl_map *map2)
9635 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9638 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9640 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9641 __isl_take isl_map *map2)
9643 isl_map *prod;
9645 prod = isl_map_product(map1, map2);
9646 prod = isl_map_flatten(prod);
9647 return prod;
9650 /* Given two set A and B, construct its Cartesian product A x B.
9652 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9654 return isl_map_range_product(set1, set2);
9657 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9658 __isl_take isl_set *set2)
9660 return isl_map_flat_range_product(set1, set2);
9663 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9665 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9666 __isl_take isl_map *map2)
9668 return map_product(map1, map2, &isl_space_domain_product,
9669 &isl_basic_map_domain_product, 1);
9672 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9674 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9675 __isl_take isl_map *map2)
9677 return map_product(map1, map2, &isl_space_range_product,
9678 &isl_basic_map_range_product, 1);
9681 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9682 __isl_take isl_map *map2)
9684 return isl_map_align_params_map_map_and(map1, map2,
9685 &map_domain_product_aligned);
9688 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9689 __isl_take isl_map *map2)
9691 return isl_map_align_params_map_map_and(map1, map2,
9692 &map_range_product_aligned);
9695 /* Given a map A -> [B -> C], extract the map A -> B.
9697 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9699 isl_space *space;
9700 int total, keep;
9702 if (!map)
9703 return NULL;
9704 if (!isl_space_range_is_wrapping(map->dim))
9705 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9706 "range is not a product", return isl_map_free(map));
9708 space = isl_map_get_space(map);
9709 total = isl_space_dim(space, isl_dim_out);
9710 space = isl_space_range_factor_domain(space);
9711 keep = isl_space_dim(space, isl_dim_out);
9712 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9713 map = isl_map_reset_space(map, space);
9715 return map;
9718 /* Given a map A -> [B -> C], extract the map A -> C.
9720 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9722 isl_space *space;
9723 int total, keep;
9725 if (!map)
9726 return NULL;
9727 if (!isl_space_range_is_wrapping(map->dim))
9728 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9729 "range is not a product", return isl_map_free(map));
9731 space = isl_map_get_space(map);
9732 total = isl_space_dim(space, isl_dim_out);
9733 space = isl_space_range_factor_range(space);
9734 keep = isl_space_dim(space, isl_dim_out);
9735 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9736 map = isl_map_reset_space(map, space);
9738 return map;
9741 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9743 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9744 __isl_take isl_map *map2)
9746 isl_map *prod;
9748 prod = isl_map_domain_product(map1, map2);
9749 prod = isl_map_flatten_domain(prod);
9750 return prod;
9753 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9755 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9756 __isl_take isl_map *map2)
9758 isl_map *prod;
9760 prod = isl_map_range_product(map1, map2);
9761 prod = isl_map_flatten_range(prod);
9762 return prod;
9765 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9767 int i;
9768 uint32_t hash = isl_hash_init();
9769 unsigned total;
9771 if (!bmap)
9772 return 0;
9773 bmap = isl_basic_map_copy(bmap);
9774 bmap = isl_basic_map_normalize(bmap);
9775 if (!bmap)
9776 return 0;
9777 total = isl_basic_map_total_dim(bmap);
9778 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9779 for (i = 0; i < bmap->n_eq; ++i) {
9780 uint32_t c_hash;
9781 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9782 isl_hash_hash(hash, c_hash);
9784 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9785 for (i = 0; i < bmap->n_ineq; ++i) {
9786 uint32_t c_hash;
9787 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9788 isl_hash_hash(hash, c_hash);
9790 isl_hash_byte(hash, bmap->n_div & 0xFF);
9791 for (i = 0; i < bmap->n_div; ++i) {
9792 uint32_t c_hash;
9793 if (isl_int_is_zero(bmap->div[i][0]))
9794 continue;
9795 isl_hash_byte(hash, i & 0xFF);
9796 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9797 isl_hash_hash(hash, c_hash);
9799 isl_basic_map_free(bmap);
9800 return hash;
9803 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9805 return isl_basic_map_get_hash((isl_basic_map *)bset);
9808 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9810 int i;
9811 uint32_t hash;
9813 if (!map)
9814 return 0;
9815 map = isl_map_copy(map);
9816 map = isl_map_normalize(map);
9817 if (!map)
9818 return 0;
9820 hash = isl_hash_init();
9821 for (i = 0; i < map->n; ++i) {
9822 uint32_t bmap_hash;
9823 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9824 isl_hash_hash(hash, bmap_hash);
9827 isl_map_free(map);
9829 return hash;
9832 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9834 return isl_map_get_hash((isl_map *)set);
9837 /* Check if the value for dimension dim is completely determined
9838 * by the values of the other parameters and variables.
9839 * That is, check if dimension dim is involved in an equality.
9841 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9843 int i;
9844 unsigned nparam;
9846 if (!bset)
9847 return -1;
9848 nparam = isl_basic_set_n_param(bset);
9849 for (i = 0; i < bset->n_eq; ++i)
9850 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9851 return 1;
9852 return 0;
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
9858 * for each of the subsets.
9860 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9862 int i;
9864 if (!set)
9865 return -1;
9866 for (i = 0; i < set->n; ++i) {
9867 int unique;
9868 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9869 if (unique != 1)
9870 return unique;
9872 return 1;
9875 int isl_set_n_basic_set(__isl_keep isl_set *set)
9877 return set ? set->n : 0;
9880 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9881 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9883 int i;
9885 if (!map)
9886 return -1;
9888 for (i = 0; i < map->n; ++i)
9889 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9890 return -1;
9892 return 0;
9895 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9896 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9898 int i;
9900 if (!set)
9901 return -1;
9903 for (i = 0; i < set->n; ++i)
9904 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9905 return -1;
9907 return 0;
9910 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9912 isl_space *dim;
9914 if (!bset)
9915 return NULL;
9917 bset = isl_basic_set_cow(bset);
9918 if (!bset)
9919 return NULL;
9921 dim = isl_basic_set_get_space(bset);
9922 dim = isl_space_lift(dim, bset->n_div);
9923 if (!dim)
9924 goto error;
9925 isl_space_free(bset->dim);
9926 bset->dim = dim;
9927 bset->extra -= bset->n_div;
9928 bset->n_div = 0;
9930 bset = isl_basic_set_finalize(bset);
9932 return bset;
9933 error:
9934 isl_basic_set_free(bset);
9935 return NULL;
9938 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9940 int i;
9941 isl_space *dim;
9942 unsigned n_div;
9944 set = isl_set_align_divs(set);
9946 if (!set)
9947 return NULL;
9949 set = isl_set_cow(set);
9950 if (!set)
9951 return NULL;
9953 n_div = set->p[0]->n_div;
9954 dim = isl_set_get_space(set);
9955 dim = isl_space_lift(dim, n_div);
9956 if (!dim)
9957 goto error;
9958 isl_space_free(set->dim);
9959 set->dim = dim;
9961 for (i = 0; i < set->n; ++i) {
9962 set->p[i] = isl_basic_set_lift(set->p[i]);
9963 if (!set->p[i])
9964 goto error;
9967 return set;
9968 error:
9969 isl_set_free(set);
9970 return NULL;
9973 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9975 isl_space *dim;
9976 struct isl_basic_map *bmap;
9977 unsigned n_set;
9978 unsigned n_div;
9979 unsigned n_param;
9980 unsigned total;
9981 int i, k, l;
9983 set = isl_set_align_divs(set);
9985 if (!set)
9986 return NULL;
9988 dim = isl_set_get_space(set);
9989 if (set->n == 0 || set->p[0]->n_div == 0) {
9990 isl_set_free(set);
9991 return isl_map_identity(isl_space_map_from_set(dim));
9994 n_div = set->p[0]->n_div;
9995 dim = isl_space_map_from_set(dim);
9996 n_param = isl_space_dim(dim, isl_dim_param);
9997 n_set = isl_space_dim(dim, isl_dim_in);
9998 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9999 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10000 for (i = 0; i < n_set; ++i)
10001 bmap = var_equal(bmap, i);
10003 total = n_param + n_set + n_set + n_div;
10004 for (i = 0; i < n_div; ++i) {
10005 k = isl_basic_map_alloc_inequality(bmap);
10006 if (k < 0)
10007 goto error;
10008 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10009 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10010 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10011 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10012 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10013 set->p[0]->div[i][0]);
10015 l = isl_basic_map_alloc_inequality(bmap);
10016 if (l < 0)
10017 goto error;
10018 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10019 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10020 set->p[0]->div[i][0]);
10021 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10024 isl_set_free(set);
10025 bmap = isl_basic_map_simplify(bmap);
10026 bmap = isl_basic_map_finalize(bmap);
10027 return isl_map_from_basic_map(bmap);
10028 error:
10029 isl_set_free(set);
10030 isl_basic_map_free(bmap);
10031 return NULL;
10034 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10036 unsigned dim;
10037 int size = 0;
10039 if (!bset)
10040 return -1;
10042 dim = isl_basic_set_total_dim(bset);
10043 size += bset->n_eq * (1 + dim);
10044 size += bset->n_ineq * (1 + dim);
10045 size += bset->n_div * (2 + dim);
10047 return size;
10050 int isl_set_size(__isl_keep isl_set *set)
10052 int i;
10053 int size = 0;
10055 if (!set)
10056 return -1;
10058 for (i = 0; i < set->n; ++i)
10059 size += isl_basic_set_size(set->p[i]);
10061 return size;
10064 /* Check if there is any lower bound (if lower == 0) and/or upper
10065 * bound (if upper == 0) on the specified dim.
10067 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10068 enum isl_dim_type type, unsigned pos, int lower, int upper)
10070 int i;
10072 if (!bmap)
10073 return -1;
10075 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
10077 pos += isl_basic_map_offset(bmap, type);
10079 for (i = 0; i < bmap->n_div; ++i) {
10080 if (isl_int_is_zero(bmap->div[i][0]))
10081 continue;
10082 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10083 return 1;
10086 for (i = 0; i < bmap->n_eq; ++i)
10087 if (!isl_int_is_zero(bmap->eq[i][pos]))
10088 return 1;
10090 for (i = 0; i < bmap->n_ineq; ++i) {
10091 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10092 if (sgn > 0)
10093 lower = 1;
10094 if (sgn < 0)
10095 upper = 1;
10098 return lower && upper;
10101 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10102 enum isl_dim_type type, unsigned pos)
10104 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10107 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10108 enum isl_dim_type type, unsigned pos)
10110 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10113 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10114 enum isl_dim_type type, unsigned pos)
10116 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10119 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10120 enum isl_dim_type type, unsigned pos)
10122 int i;
10124 if (!map)
10125 return -1;
10127 for (i = 0; i < map->n; ++i) {
10128 int bounded;
10129 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10130 if (bounded < 0 || !bounded)
10131 return bounded;
10134 return 1;
10137 /* Return 1 if the specified dim is involved in both an upper bound
10138 * and a lower bound.
10140 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10141 enum isl_dim_type type, unsigned pos)
10143 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10146 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10148 static int has_any_bound(__isl_keep isl_map *map,
10149 enum isl_dim_type type, unsigned pos,
10150 int (*fn)(__isl_keep isl_basic_map *bmap,
10151 enum isl_dim_type type, unsigned pos))
10153 int i;
10155 if (!map)
10156 return -1;
10158 for (i = 0; i < map->n; ++i) {
10159 int bounded;
10160 bounded = fn(map->p[i], type, pos);
10161 if (bounded < 0 || bounded)
10162 return bounded;
10165 return 0;
10168 /* Return 1 if the specified dim is involved in any lower bound.
10170 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10171 enum isl_dim_type type, unsigned pos)
10173 return has_any_bound(set, type, pos,
10174 &isl_basic_map_dim_has_lower_bound);
10177 /* Return 1 if the specified dim is involved in any upper bound.
10179 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10180 enum isl_dim_type type, unsigned pos)
10182 return has_any_bound(set, type, pos,
10183 &isl_basic_map_dim_has_upper_bound);
10186 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10188 static int has_bound(__isl_keep isl_map *map,
10189 enum isl_dim_type type, unsigned pos,
10190 int (*fn)(__isl_keep isl_basic_map *bmap,
10191 enum isl_dim_type type, unsigned pos))
10193 int i;
10195 if (!map)
10196 return -1;
10198 for (i = 0; i < map->n; ++i) {
10199 int bounded;
10200 bounded = fn(map->p[i], type, pos);
10201 if (bounded < 0 || !bounded)
10202 return bounded;
10205 return 1;
10208 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10210 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10211 enum isl_dim_type type, unsigned pos)
10213 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10216 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10218 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10219 enum isl_dim_type type, unsigned pos)
10221 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10224 /* For each of the "n" variables starting at "first", determine
10225 * the sign of the variable and put the results in the first "n"
10226 * elements of the array "signs".
10227 * Sign
10228 * 1 means that the variable is non-negative
10229 * -1 means that the variable is non-positive
10230 * 0 means the variable attains both positive and negative values.
10232 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10233 unsigned first, unsigned n, int *signs)
10235 isl_vec *bound = NULL;
10236 struct isl_tab *tab = NULL;
10237 struct isl_tab_undo *snap;
10238 int i;
10240 if (!bset || !signs)
10241 return -1;
10243 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10244 tab = isl_tab_from_basic_set(bset, 0);
10245 if (!bound || !tab)
10246 goto error;
10248 isl_seq_clr(bound->el, bound->size);
10249 isl_int_set_si(bound->el[0], -1);
10251 snap = isl_tab_snap(tab);
10252 for (i = 0; i < n; ++i) {
10253 int empty;
10255 isl_int_set_si(bound->el[1 + first + i], -1);
10256 if (isl_tab_add_ineq(tab, bound->el) < 0)
10257 goto error;
10258 empty = tab->empty;
10259 isl_int_set_si(bound->el[1 + first + i], 0);
10260 if (isl_tab_rollback(tab, snap) < 0)
10261 goto error;
10263 if (empty) {
10264 signs[i] = 1;
10265 continue;
10268 isl_int_set_si(bound->el[1 + first + i], 1);
10269 if (isl_tab_add_ineq(tab, bound->el) < 0)
10270 goto error;
10271 empty = tab->empty;
10272 isl_int_set_si(bound->el[1 + first + i], 0);
10273 if (isl_tab_rollback(tab, snap) < 0)
10274 goto error;
10276 signs[i] = empty ? -1 : 0;
10279 isl_tab_free(tab);
10280 isl_vec_free(bound);
10281 return 0;
10282 error:
10283 isl_tab_free(tab);
10284 isl_vec_free(bound);
10285 return -1;
10288 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10289 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10291 if (!bset || !signs)
10292 return -1;
10293 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10294 return -1);
10296 first += pos(bset->dim, type) - 1;
10297 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10300 /* Is it possible for the integer division "div" to depend (possibly
10301 * indirectly) on any output dimensions?
10303 * If the div is undefined, then we conservatively assume that it
10304 * may depend on them.
10305 * Otherwise, we check if it actually depends on them or on any integer
10306 * divisions that may depend on them.
10308 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10310 int i;
10311 unsigned n_out, o_out;
10312 unsigned n_div, o_div;
10314 if (isl_int_is_zero(bmap->div[div][0]))
10315 return 1;
10317 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10318 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10320 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10321 return 1;
10323 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10324 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10326 for (i = 0; i < n_div; ++i) {
10327 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10328 continue;
10329 if (div_may_involve_output(bmap, i))
10330 return 1;
10333 return 0;
10336 /* Return the index of the equality of "bmap" that defines
10337 * the output dimension "pos" in terms of earlier dimensions.
10338 * The equality may also involve integer divisions, as long
10339 * as those integer divisions are defined in terms of
10340 * parameters or input dimensions.
10341 * Return bmap->n_eq if there is no such equality.
10342 * Return -1 on error.
10344 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10345 int pos)
10347 int j, k;
10348 unsigned n_out, o_out;
10349 unsigned n_div, o_div;
10351 if (!bmap)
10352 return -1;
10354 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10355 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10356 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10357 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10359 for (j = 0; j < bmap->n_eq; ++j) {
10360 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10361 continue;
10362 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10363 n_out - (pos + 1)) != -1)
10364 continue;
10365 for (k = 0; k < n_div; ++k) {
10366 if (isl_int_is_zero(bmap->eq[j][o_div + k]))
10367 continue;
10368 if (div_may_involve_output(bmap, k))
10369 break;
10371 if (k >= n_div)
10372 return j;
10375 return bmap->n_eq;
10378 /* Check if the given basic map is obviously single-valued.
10379 * In particular, for each output dimension, check that there is
10380 * an equality that defines the output dimension in terms of
10381 * earlier dimensions.
10383 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10385 int i;
10386 unsigned n_out;
10388 if (!bmap)
10389 return -1;
10391 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10393 for (i = 0; i < n_out; ++i) {
10394 int eq;
10396 eq = isl_basic_map_output_defining_equality(bmap, i);
10397 if (eq < 0)
10398 return -1;
10399 if (eq >= bmap->n_eq)
10400 return 0;
10403 return 1;
10406 /* Check if the given basic map is single-valued.
10407 * We simply compute
10409 * M \circ M^-1
10411 * and check if the result is a subset of the identity mapping.
10413 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10415 isl_space *space;
10416 isl_basic_map *test;
10417 isl_basic_map *id;
10418 int sv;
10420 sv = isl_basic_map_plain_is_single_valued(bmap);
10421 if (sv < 0 || sv)
10422 return sv;
10424 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10425 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10427 space = isl_basic_map_get_space(bmap);
10428 space = isl_space_map_from_set(isl_space_range(space));
10429 id = isl_basic_map_identity(space);
10431 sv = isl_basic_map_is_subset(test, id);
10433 isl_basic_map_free(test);
10434 isl_basic_map_free(id);
10436 return sv;
10439 /* Check if the given map is obviously single-valued.
10441 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10443 if (!map)
10444 return -1;
10445 if (map->n == 0)
10446 return 1;
10447 if (map->n >= 2)
10448 return 0;
10450 return isl_basic_map_plain_is_single_valued(map->p[0]);
10453 /* Check if the given map is single-valued.
10454 * We simply compute
10456 * M \circ M^-1
10458 * and check if the result is a subset of the identity mapping.
10460 int isl_map_is_single_valued(__isl_keep isl_map *map)
10462 isl_space *dim;
10463 isl_map *test;
10464 isl_map *id;
10465 int sv;
10467 sv = isl_map_plain_is_single_valued(map);
10468 if (sv < 0 || sv)
10469 return sv;
10471 test = isl_map_reverse(isl_map_copy(map));
10472 test = isl_map_apply_range(test, isl_map_copy(map));
10474 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10475 id = isl_map_identity(dim);
10477 sv = isl_map_is_subset(test, id);
10479 isl_map_free(test);
10480 isl_map_free(id);
10482 return sv;
10485 int isl_map_is_injective(__isl_keep isl_map *map)
10487 int in;
10489 map = isl_map_copy(map);
10490 map = isl_map_reverse(map);
10491 in = isl_map_is_single_valued(map);
10492 isl_map_free(map);
10494 return in;
10497 /* Check if the given map is obviously injective.
10499 int isl_map_plain_is_injective(__isl_keep isl_map *map)
10501 int in;
10503 map = isl_map_copy(map);
10504 map = isl_map_reverse(map);
10505 in = isl_map_plain_is_single_valued(map);
10506 isl_map_free(map);
10508 return in;
10511 int isl_map_is_bijective(__isl_keep isl_map *map)
10513 int sv;
10515 sv = isl_map_is_single_valued(map);
10516 if (sv < 0 || !sv)
10517 return sv;
10519 return isl_map_is_injective(map);
10522 int isl_set_is_singleton(__isl_keep isl_set *set)
10524 return isl_map_is_single_valued((isl_map *)set);
10527 int isl_map_is_translation(__isl_keep isl_map *map)
10529 int ok;
10530 isl_set *delta;
10532 delta = isl_map_deltas(isl_map_copy(map));
10533 ok = isl_set_is_singleton(delta);
10534 isl_set_free(delta);
10536 return ok;
10539 static int unique(isl_int *p, unsigned pos, unsigned len)
10541 if (isl_seq_first_non_zero(p, pos) != -1)
10542 return 0;
10543 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10544 return 0;
10545 return 1;
10548 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10550 int i, j;
10551 unsigned nvar;
10552 unsigned ovar;
10554 if (!bset)
10555 return -1;
10557 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10558 return 0;
10560 nvar = isl_basic_set_dim(bset, isl_dim_set);
10561 ovar = isl_space_offset(bset->dim, isl_dim_set);
10562 for (j = 0; j < nvar; ++j) {
10563 int lower = 0, upper = 0;
10564 for (i = 0; i < bset->n_eq; ++i) {
10565 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10566 continue;
10567 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10568 return 0;
10569 break;
10571 if (i < bset->n_eq)
10572 continue;
10573 for (i = 0; i < bset->n_ineq; ++i) {
10574 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10575 continue;
10576 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10577 return 0;
10578 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10579 lower = 1;
10580 else
10581 upper = 1;
10583 if (!lower || !upper)
10584 return 0;
10587 return 1;
10590 int isl_set_is_box(__isl_keep isl_set *set)
10592 if (!set)
10593 return -1;
10594 if (set->n != 1)
10595 return 0;
10597 return isl_basic_set_is_box(set->p[0]);
10600 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10602 if (!bset)
10603 return -1;
10605 return isl_space_is_wrapping(bset->dim);
10608 int isl_set_is_wrapping(__isl_keep isl_set *set)
10610 if (!set)
10611 return -1;
10613 return isl_space_is_wrapping(set->dim);
10616 /* Is the domain of "map" a wrapped relation?
10618 int isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10620 if (!map)
10621 return -1;
10623 return isl_space_domain_is_wrapping(map->dim);
10626 /* Is the range of "map" a wrapped relation?
10628 int isl_map_range_is_wrapping(__isl_keep isl_map *map)
10630 if (!map)
10631 return -1;
10633 return isl_space_range_is_wrapping(map->dim);
10636 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10638 bmap = isl_basic_map_cow(bmap);
10639 if (!bmap)
10640 return NULL;
10642 bmap->dim = isl_space_wrap(bmap->dim);
10643 if (!bmap->dim)
10644 goto error;
10646 bmap = isl_basic_map_finalize(bmap);
10648 return (isl_basic_set *)bmap;
10649 error:
10650 isl_basic_map_free(bmap);
10651 return NULL;
10654 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10656 int i;
10658 map = isl_map_cow(map);
10659 if (!map)
10660 return NULL;
10662 for (i = 0; i < map->n; ++i) {
10663 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10664 if (!map->p[i])
10665 goto error;
10667 map->dim = isl_space_wrap(map->dim);
10668 if (!map->dim)
10669 goto error;
10671 return (isl_set *)map;
10672 error:
10673 isl_map_free(map);
10674 return NULL;
10677 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10679 bset = isl_basic_set_cow(bset);
10680 if (!bset)
10681 return NULL;
10683 bset->dim = isl_space_unwrap(bset->dim);
10684 if (!bset->dim)
10685 goto error;
10687 bset = isl_basic_set_finalize(bset);
10689 return (isl_basic_map *)bset;
10690 error:
10691 isl_basic_set_free(bset);
10692 return NULL;
10695 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10697 int i;
10699 if (!set)
10700 return NULL;
10702 if (!isl_set_is_wrapping(set))
10703 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10704 goto error);
10706 set = isl_set_cow(set);
10707 if (!set)
10708 return NULL;
10710 for (i = 0; i < set->n; ++i) {
10711 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10712 if (!set->p[i])
10713 goto error;
10716 set->dim = isl_space_unwrap(set->dim);
10717 if (!set->dim)
10718 goto error;
10720 return (isl_map *)set;
10721 error:
10722 isl_set_free(set);
10723 return NULL;
10726 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10727 enum isl_dim_type type)
10729 if (!bmap)
10730 return NULL;
10732 if (!isl_space_is_named_or_nested(bmap->dim, type))
10733 return bmap;
10735 bmap = isl_basic_map_cow(bmap);
10736 if (!bmap)
10737 return NULL;
10739 bmap->dim = isl_space_reset(bmap->dim, type);
10740 if (!bmap->dim)
10741 goto error;
10743 bmap = isl_basic_map_finalize(bmap);
10745 return bmap;
10746 error:
10747 isl_basic_map_free(bmap);
10748 return NULL;
10751 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10752 enum isl_dim_type type)
10754 int i;
10756 if (!map)
10757 return NULL;
10759 if (!isl_space_is_named_or_nested(map->dim, type))
10760 return map;
10762 map = isl_map_cow(map);
10763 if (!map)
10764 return NULL;
10766 for (i = 0; i < map->n; ++i) {
10767 map->p[i] = isl_basic_map_reset(map->p[i], type);
10768 if (!map->p[i])
10769 goto error;
10771 map->dim = isl_space_reset(map->dim, type);
10772 if (!map->dim)
10773 goto error;
10775 return map;
10776 error:
10777 isl_map_free(map);
10778 return NULL;
10781 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10783 if (!bmap)
10784 return NULL;
10786 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10787 return bmap;
10789 bmap = isl_basic_map_cow(bmap);
10790 if (!bmap)
10791 return NULL;
10793 bmap->dim = isl_space_flatten(bmap->dim);
10794 if (!bmap->dim)
10795 goto error;
10797 bmap = isl_basic_map_finalize(bmap);
10799 return bmap;
10800 error:
10801 isl_basic_map_free(bmap);
10802 return NULL;
10805 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10807 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10810 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10811 __isl_take isl_basic_map *bmap)
10813 if (!bmap)
10814 return NULL;
10816 if (!bmap->dim->nested[0])
10817 return bmap;
10819 bmap = isl_basic_map_cow(bmap);
10820 if (!bmap)
10821 return NULL;
10823 bmap->dim = isl_space_flatten_domain(bmap->dim);
10824 if (!bmap->dim)
10825 goto error;
10827 bmap = isl_basic_map_finalize(bmap);
10829 return bmap;
10830 error:
10831 isl_basic_map_free(bmap);
10832 return NULL;
10835 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10836 __isl_take isl_basic_map *bmap)
10838 if (!bmap)
10839 return NULL;
10841 if (!bmap->dim->nested[1])
10842 return bmap;
10844 bmap = isl_basic_map_cow(bmap);
10845 if (!bmap)
10846 return NULL;
10848 bmap->dim = isl_space_flatten_range(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_map *isl_map_flatten(__isl_take isl_map *map)
10862 int i;
10864 if (!map)
10865 return NULL;
10867 if (!map->dim->nested[0] && !map->dim->nested[1])
10868 return map;
10870 map = isl_map_cow(map);
10871 if (!map)
10872 return NULL;
10874 for (i = 0; i < map->n; ++i) {
10875 map->p[i] = isl_basic_map_flatten(map->p[i]);
10876 if (!map->p[i])
10877 goto error;
10879 map->dim = isl_space_flatten(map->dim);
10880 if (!map->dim)
10881 goto error;
10883 return map;
10884 error:
10885 isl_map_free(map);
10886 return NULL;
10889 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10891 return (isl_set *)isl_map_flatten((isl_map *)set);
10894 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10896 isl_space *dim, *flat_dim;
10897 isl_map *map;
10899 dim = isl_set_get_space(set);
10900 flat_dim = isl_space_flatten(isl_space_copy(dim));
10901 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10902 map = isl_map_intersect_domain(map, set);
10904 return map;
10907 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10909 int i;
10911 if (!map)
10912 return NULL;
10914 if (!map->dim->nested[0])
10915 return map;
10917 map = isl_map_cow(map);
10918 if (!map)
10919 return NULL;
10921 for (i = 0; i < map->n; ++i) {
10922 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10923 if (!map->p[i])
10924 goto error;
10926 map->dim = isl_space_flatten_domain(map->dim);
10927 if (!map->dim)
10928 goto error;
10930 return map;
10931 error:
10932 isl_map_free(map);
10933 return NULL;
10936 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10938 int i;
10940 if (!map)
10941 return NULL;
10943 if (!map->dim->nested[1])
10944 return map;
10946 map = isl_map_cow(map);
10947 if (!map)
10948 return NULL;
10950 for (i = 0; i < map->n; ++i) {
10951 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10952 if (!map->p[i])
10953 goto error;
10955 map->dim = isl_space_flatten_range(map->dim);
10956 if (!map->dim)
10957 goto error;
10959 return map;
10960 error:
10961 isl_map_free(map);
10962 return NULL;
10965 /* Reorder the dimensions of "bmap" according to the given dim_map
10966 * and set the dimension specification to "dim".
10968 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10969 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10971 isl_basic_map *res;
10972 unsigned flags;
10974 bmap = isl_basic_map_cow(bmap);
10975 if (!bmap || !dim || !dim_map)
10976 goto error;
10978 flags = bmap->flags;
10979 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10980 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10981 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10982 res = isl_basic_map_alloc_space(dim,
10983 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10984 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10985 if (res)
10986 res->flags = flags;
10987 res = isl_basic_map_finalize(res);
10988 return res;
10989 error:
10990 free(dim_map);
10991 isl_basic_map_free(bmap);
10992 isl_space_free(dim);
10993 return NULL;
10996 /* Reorder the dimensions of "map" according to given reordering.
10998 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10999 __isl_take isl_reordering *r)
11001 int i;
11002 struct isl_dim_map *dim_map;
11004 map = isl_map_cow(map);
11005 dim_map = isl_dim_map_from_reordering(r);
11006 if (!map || !r || !dim_map)
11007 goto error;
11009 for (i = 0; i < map->n; ++i) {
11010 struct isl_dim_map *dim_map_i;
11012 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11014 map->p[i] = isl_basic_map_realign(map->p[i],
11015 isl_space_copy(r->dim), dim_map_i);
11017 if (!map->p[i])
11018 goto error;
11021 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11023 isl_reordering_free(r);
11024 free(dim_map);
11025 return map;
11026 error:
11027 free(dim_map);
11028 isl_map_free(map);
11029 isl_reordering_free(r);
11030 return NULL;
11033 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11034 __isl_take isl_reordering *r)
11036 return (isl_set *)isl_map_realign((isl_map *)set, r);
11039 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11040 __isl_take isl_space *model)
11042 isl_ctx *ctx;
11044 if (!map || !model)
11045 goto error;
11047 ctx = isl_space_get_ctx(model);
11048 if (!isl_space_has_named_params(model))
11049 isl_die(ctx, isl_error_invalid,
11050 "model has unnamed parameters", goto error);
11051 if (!isl_space_has_named_params(map->dim))
11052 isl_die(ctx, isl_error_invalid,
11053 "relation has unnamed parameters", goto error);
11054 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11055 isl_reordering *exp;
11057 model = isl_space_drop_dims(model, isl_dim_in,
11058 0, isl_space_dim(model, isl_dim_in));
11059 model = isl_space_drop_dims(model, isl_dim_out,
11060 0, isl_space_dim(model, isl_dim_out));
11061 exp = isl_parameter_alignment_reordering(map->dim, model);
11062 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11063 map = isl_map_realign(map, exp);
11066 isl_space_free(model);
11067 return map;
11068 error:
11069 isl_space_free(model);
11070 isl_map_free(map);
11071 return NULL;
11074 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11075 __isl_take isl_space *model)
11077 return isl_map_align_params(set, model);
11080 /* Align the parameters of "bmap" to those of "model", introducing
11081 * additional parameters if needed.
11083 __isl_give isl_basic_map *isl_basic_map_align_params(
11084 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11086 isl_ctx *ctx;
11088 if (!bmap || !model)
11089 goto error;
11091 ctx = isl_space_get_ctx(model);
11092 if (!isl_space_has_named_params(model))
11093 isl_die(ctx, isl_error_invalid,
11094 "model has unnamed parameters", goto error);
11095 if (!isl_space_has_named_params(bmap->dim))
11096 isl_die(ctx, isl_error_invalid,
11097 "relation has unnamed parameters", goto error);
11098 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11099 isl_reordering *exp;
11100 struct isl_dim_map *dim_map;
11102 model = isl_space_drop_dims(model, isl_dim_in,
11103 0, isl_space_dim(model, isl_dim_in));
11104 model = isl_space_drop_dims(model, isl_dim_out,
11105 0, isl_space_dim(model, isl_dim_out));
11106 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11107 exp = isl_reordering_extend_space(exp,
11108 isl_basic_map_get_space(bmap));
11109 dim_map = isl_dim_map_from_reordering(exp);
11110 bmap = isl_basic_map_realign(bmap,
11111 exp ? isl_space_copy(exp->dim) : NULL,
11112 isl_dim_map_extend(dim_map, bmap));
11113 isl_reordering_free(exp);
11114 free(dim_map);
11117 isl_space_free(model);
11118 return bmap;
11119 error:
11120 isl_space_free(model);
11121 isl_basic_map_free(bmap);
11122 return NULL;
11125 /* Align the parameters of "bset" to those of "model", introducing
11126 * additional parameters if needed.
11128 __isl_give isl_basic_set *isl_basic_set_align_params(
11129 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11131 return isl_basic_map_align_params(bset, model);
11134 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11135 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11136 enum isl_dim_type c2, enum isl_dim_type c3,
11137 enum isl_dim_type c4, enum isl_dim_type c5)
11139 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11140 struct isl_mat *mat;
11141 int i, j, k;
11142 int pos;
11144 if (!bmap)
11145 return NULL;
11146 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11147 isl_basic_map_total_dim(bmap) + 1);
11148 if (!mat)
11149 return NULL;
11150 for (i = 0; i < bmap->n_eq; ++i)
11151 for (j = 0, pos = 0; j < 5; ++j) {
11152 int off = isl_basic_map_offset(bmap, c[j]);
11153 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11154 isl_int_set(mat->row[i][pos],
11155 bmap->eq[i][off + k]);
11156 ++pos;
11160 return mat;
11163 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11164 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11165 enum isl_dim_type c2, enum isl_dim_type c3,
11166 enum isl_dim_type c4, enum isl_dim_type c5)
11168 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11169 struct isl_mat *mat;
11170 int i, j, k;
11171 int pos;
11173 if (!bmap)
11174 return NULL;
11175 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11176 isl_basic_map_total_dim(bmap) + 1);
11177 if (!mat)
11178 return NULL;
11179 for (i = 0; i < bmap->n_ineq; ++i)
11180 for (j = 0, pos = 0; j < 5; ++j) {
11181 int off = isl_basic_map_offset(bmap, c[j]);
11182 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11183 isl_int_set(mat->row[i][pos],
11184 bmap->ineq[i][off + k]);
11185 ++pos;
11189 return mat;
11192 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11193 __isl_take isl_space *dim,
11194 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11195 enum isl_dim_type c2, enum isl_dim_type c3,
11196 enum isl_dim_type c4, enum isl_dim_type c5)
11198 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11199 isl_basic_map *bmap;
11200 unsigned total;
11201 unsigned extra;
11202 int i, j, k, l;
11203 int pos;
11205 if (!dim || !eq || !ineq)
11206 goto error;
11208 if (eq->n_col != ineq->n_col)
11209 isl_die(dim->ctx, isl_error_invalid,
11210 "equalities and inequalities matrices should have "
11211 "same number of columns", goto error);
11213 total = 1 + isl_space_dim(dim, isl_dim_all);
11215 if (eq->n_col < total)
11216 isl_die(dim->ctx, isl_error_invalid,
11217 "number of columns too small", goto error);
11219 extra = eq->n_col - total;
11221 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11222 eq->n_row, ineq->n_row);
11223 if (!bmap)
11224 goto error;
11225 for (i = 0; i < extra; ++i) {
11226 k = isl_basic_map_alloc_div(bmap);
11227 if (k < 0)
11228 goto error;
11229 isl_int_set_si(bmap->div[k][0], 0);
11231 for (i = 0; i < eq->n_row; ++i) {
11232 l = isl_basic_map_alloc_equality(bmap);
11233 if (l < 0)
11234 goto error;
11235 for (j = 0, pos = 0; j < 5; ++j) {
11236 int off = isl_basic_map_offset(bmap, c[j]);
11237 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11238 isl_int_set(bmap->eq[l][off + k],
11239 eq->row[i][pos]);
11240 ++pos;
11244 for (i = 0; i < ineq->n_row; ++i) {
11245 l = isl_basic_map_alloc_inequality(bmap);
11246 if (l < 0)
11247 goto error;
11248 for (j = 0, pos = 0; j < 5; ++j) {
11249 int off = isl_basic_map_offset(bmap, c[j]);
11250 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11251 isl_int_set(bmap->ineq[l][off + k],
11252 ineq->row[i][pos]);
11253 ++pos;
11258 isl_space_free(dim);
11259 isl_mat_free(eq);
11260 isl_mat_free(ineq);
11262 bmap = isl_basic_map_simplify(bmap);
11263 return isl_basic_map_finalize(bmap);
11264 error:
11265 isl_space_free(dim);
11266 isl_mat_free(eq);
11267 isl_mat_free(ineq);
11268 return NULL;
11271 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11272 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11273 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11275 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11276 c1, c2, c3, c4, isl_dim_in);
11279 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11280 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11281 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11283 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11284 c1, c2, c3, c4, isl_dim_in);
11287 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11288 __isl_take isl_space *dim,
11289 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11290 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11292 return (isl_basic_set*)
11293 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11294 c1, c2, c3, c4, isl_dim_in);
11297 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11299 if (!bmap)
11300 return -1;
11302 return isl_space_can_zip(bmap->dim);
11305 int isl_map_can_zip(__isl_keep isl_map *map)
11307 if (!map)
11308 return -1;
11310 return isl_space_can_zip(map->dim);
11313 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11314 * (A -> C) -> (B -> D).
11316 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11318 unsigned pos;
11319 unsigned n1;
11320 unsigned n2;
11322 if (!bmap)
11323 return NULL;
11325 if (!isl_basic_map_can_zip(bmap))
11326 isl_die(bmap->ctx, isl_error_invalid,
11327 "basic map cannot be zipped", goto error);
11328 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11329 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11330 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11331 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11332 bmap = isl_basic_map_cow(bmap);
11333 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11334 if (!bmap)
11335 return NULL;
11336 bmap->dim = isl_space_zip(bmap->dim);
11337 if (!bmap->dim)
11338 goto error;
11339 return bmap;
11340 error:
11341 isl_basic_map_free(bmap);
11342 return NULL;
11345 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11346 * (A -> C) -> (B -> D).
11348 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11350 int i;
11352 if (!map)
11353 return NULL;
11355 if (!isl_map_can_zip(map))
11356 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11357 goto error);
11359 map = isl_map_cow(map);
11360 if (!map)
11361 return NULL;
11363 for (i = 0; i < map->n; ++i) {
11364 map->p[i] = isl_basic_map_zip(map->p[i]);
11365 if (!map->p[i])
11366 goto error;
11369 map->dim = isl_space_zip(map->dim);
11370 if (!map->dim)
11371 goto error;
11373 return map;
11374 error:
11375 isl_map_free(map);
11376 return NULL;
11379 /* Can we apply isl_basic_map_curry to "bmap"?
11380 * That is, does it have a nested relation in its domain?
11382 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11384 if (!bmap)
11385 return -1;
11387 return isl_space_can_curry(bmap->dim);
11390 /* Can we apply isl_map_curry to "map"?
11391 * That is, does it have a nested relation in its domain?
11393 int isl_map_can_curry(__isl_keep isl_map *map)
11395 if (!map)
11396 return -1;
11398 return isl_space_can_curry(map->dim);
11401 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11402 * A -> (B -> C).
11404 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11407 if (!bmap)
11408 return NULL;
11410 if (!isl_basic_map_can_curry(bmap))
11411 isl_die(bmap->ctx, isl_error_invalid,
11412 "basic map cannot be curried", goto error);
11413 bmap = isl_basic_map_cow(bmap);
11414 if (!bmap)
11415 return NULL;
11416 bmap->dim = isl_space_curry(bmap->dim);
11417 if (!bmap->dim)
11418 goto error;
11419 return bmap;
11420 error:
11421 isl_basic_map_free(bmap);
11422 return NULL;
11425 /* Given a map (A -> B) -> C, return the corresponding map
11426 * A -> (B -> C).
11428 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11430 int i;
11432 if (!map)
11433 return NULL;
11435 if (!isl_map_can_curry(map))
11436 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11437 goto error);
11439 map = isl_map_cow(map);
11440 if (!map)
11441 return NULL;
11443 for (i = 0; i < map->n; ++i) {
11444 map->p[i] = isl_basic_map_curry(map->p[i]);
11445 if (!map->p[i])
11446 goto error;
11449 map->dim = isl_space_curry(map->dim);
11450 if (!map->dim)
11451 goto error;
11453 return map;
11454 error:
11455 isl_map_free(map);
11456 return NULL;
11459 /* Can we apply isl_basic_map_uncurry to "bmap"?
11460 * That is, does it have a nested relation in its domain?
11462 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11464 if (!bmap)
11465 return -1;
11467 return isl_space_can_uncurry(bmap->dim);
11470 /* Can we apply isl_map_uncurry to "map"?
11471 * That is, does it have a nested relation in its domain?
11473 int isl_map_can_uncurry(__isl_keep isl_map *map)
11475 if (!map)
11476 return -1;
11478 return isl_space_can_uncurry(map->dim);
11481 /* Given a basic map A -> (B -> C), return the corresponding basic map
11482 * (A -> B) -> C.
11484 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11487 if (!bmap)
11488 return NULL;
11490 if (!isl_basic_map_can_uncurry(bmap))
11491 isl_die(bmap->ctx, isl_error_invalid,
11492 "basic map cannot be uncurried",
11493 return isl_basic_map_free(bmap));
11494 bmap = isl_basic_map_cow(bmap);
11495 if (!bmap)
11496 return NULL;
11497 bmap->dim = isl_space_uncurry(bmap->dim);
11498 if (!bmap->dim)
11499 return isl_basic_map_free(bmap);
11500 return bmap;
11503 /* Given a map A -> (B -> C), return the corresponding map
11504 * (A -> B) -> C.
11506 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11508 int i;
11510 if (!map)
11511 return NULL;
11513 if (!isl_map_can_uncurry(map))
11514 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11515 return isl_map_free(map));
11517 map = isl_map_cow(map);
11518 if (!map)
11519 return NULL;
11521 for (i = 0; i < map->n; ++i) {
11522 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11523 if (!map->p[i])
11524 return isl_map_free(map);
11527 map->dim = isl_space_uncurry(map->dim);
11528 if (!map->dim)
11529 return isl_map_free(map);
11531 return map;
11534 /* Construct a basic map mapping the domain of the affine expression
11535 * to a one-dimensional range prescribed by the affine expression.
11537 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11539 int k;
11540 int pos;
11541 isl_local_space *ls;
11542 isl_basic_map *bmap;
11544 if (!aff)
11545 return NULL;
11547 ls = isl_aff_get_local_space(aff);
11548 bmap = isl_basic_map_from_local_space(ls);
11549 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11550 k = isl_basic_map_alloc_equality(bmap);
11551 if (k < 0)
11552 goto error;
11554 pos = isl_basic_map_offset(bmap, isl_dim_out);
11555 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11556 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11557 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11558 aff->v->size - (pos + 1));
11560 isl_aff_free(aff);
11561 bmap = isl_basic_map_finalize(bmap);
11562 return bmap;
11563 error:
11564 isl_aff_free(aff);
11565 isl_basic_map_free(bmap);
11566 return NULL;
11569 /* Construct a map mapping the domain of the affine expression
11570 * to a one-dimensional range prescribed by the affine expression.
11572 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11574 isl_basic_map *bmap;
11576 bmap = isl_basic_map_from_aff(aff);
11577 return isl_map_from_basic_map(bmap);
11580 /* Construct a basic map mapping the domain the multi-affine expression
11581 * to its range, with each dimension in the range equated to the
11582 * corresponding affine expression.
11584 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11585 __isl_take isl_multi_aff *maff)
11587 int i;
11588 isl_space *space;
11589 isl_basic_map *bmap;
11591 if (!maff)
11592 return NULL;
11594 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11595 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11596 "invalid space", goto error);
11598 space = isl_space_domain(isl_multi_aff_get_space(maff));
11599 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11601 for (i = 0; i < maff->n; ++i) {
11602 isl_aff *aff;
11603 isl_basic_map *bmap_i;
11605 aff = isl_aff_copy(maff->p[i]);
11606 bmap_i = isl_basic_map_from_aff(aff);
11608 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11611 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11613 isl_multi_aff_free(maff);
11614 return bmap;
11615 error:
11616 isl_multi_aff_free(maff);
11617 return NULL;
11620 /* Construct a map mapping the domain the multi-affine expression
11621 * to its range, with each dimension in the range equated to the
11622 * corresponding affine expression.
11624 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11626 isl_basic_map *bmap;
11628 bmap = isl_basic_map_from_multi_aff(maff);
11629 return isl_map_from_basic_map(bmap);
11632 /* Construct a basic map mapping a domain in the given space to
11633 * to an n-dimensional range, with n the number of elements in the list,
11634 * where each coordinate in the range is prescribed by the
11635 * corresponding affine expression.
11636 * The domains of all affine expressions in the list are assumed to match
11637 * domain_dim.
11639 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11640 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11642 int i;
11643 isl_space *dim;
11644 isl_basic_map *bmap;
11646 if (!list)
11647 return NULL;
11649 dim = isl_space_from_domain(domain_dim);
11650 bmap = isl_basic_map_universe(dim);
11652 for (i = 0; i < list->n; ++i) {
11653 isl_aff *aff;
11654 isl_basic_map *bmap_i;
11656 aff = isl_aff_copy(list->p[i]);
11657 bmap_i = isl_basic_map_from_aff(aff);
11659 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11662 isl_aff_list_free(list);
11663 return bmap;
11666 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11667 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11669 return isl_map_equate(set, type1, pos1, type2, pos2);
11672 /* Construct a basic map where the given dimensions are equal to each other.
11674 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11675 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11677 isl_basic_map *bmap = NULL;
11678 int i;
11680 if (!space)
11681 return NULL;
11683 if (pos1 >= isl_space_dim(space, type1))
11684 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11685 "index out of bounds", goto error);
11686 if (pos2 >= isl_space_dim(space, type2))
11687 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11688 "index out of bounds", goto error);
11690 if (type1 == type2 && pos1 == pos2)
11691 return isl_basic_map_universe(space);
11693 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11694 i = isl_basic_map_alloc_equality(bmap);
11695 if (i < 0)
11696 goto error;
11697 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11698 pos1 += isl_basic_map_offset(bmap, type1);
11699 pos2 += isl_basic_map_offset(bmap, type2);
11700 isl_int_set_si(bmap->eq[i][pos1], -1);
11701 isl_int_set_si(bmap->eq[i][pos2], 1);
11702 bmap = isl_basic_map_finalize(bmap);
11703 isl_space_free(space);
11704 return bmap;
11705 error:
11706 isl_space_free(space);
11707 isl_basic_map_free(bmap);
11708 return NULL;
11711 /* Add a constraint imposing that the given two dimensions are equal.
11713 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11714 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11716 isl_basic_map *eq;
11718 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11720 bmap = isl_basic_map_intersect(bmap, eq);
11722 return bmap;
11725 /* Add a constraint imposing that the given two dimensions are equal.
11727 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11728 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11730 isl_basic_map *bmap;
11732 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11734 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11736 return map;
11739 /* Add a constraint imposing that the given two dimensions have opposite values.
11741 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11742 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11744 isl_basic_map *bmap = NULL;
11745 int i;
11747 if (!map)
11748 return NULL;
11750 if (pos1 >= isl_map_dim(map, type1))
11751 isl_die(map->ctx, isl_error_invalid,
11752 "index out of bounds", goto error);
11753 if (pos2 >= isl_map_dim(map, type2))
11754 isl_die(map->ctx, isl_error_invalid,
11755 "index out of bounds", goto error);
11757 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11758 i = isl_basic_map_alloc_equality(bmap);
11759 if (i < 0)
11760 goto error;
11761 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11762 pos1 += isl_basic_map_offset(bmap, type1);
11763 pos2 += isl_basic_map_offset(bmap, type2);
11764 isl_int_set_si(bmap->eq[i][pos1], 1);
11765 isl_int_set_si(bmap->eq[i][pos2], 1);
11766 bmap = isl_basic_map_finalize(bmap);
11768 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11770 return map;
11771 error:
11772 isl_basic_map_free(bmap);
11773 isl_map_free(map);
11774 return NULL;
11777 /* Construct a constraint imposing that the value of the first dimension is
11778 * greater than or equal to that of the second.
11780 static __isl_give isl_constraint *constraint_order_ge(
11781 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11782 enum isl_dim_type type2, int pos2)
11784 isl_constraint *c;
11786 if (!space)
11787 return NULL;
11789 c = isl_inequality_alloc(isl_local_space_from_space(space));
11791 if (pos1 >= isl_constraint_dim(c, type1))
11792 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11793 "index out of bounds", return isl_constraint_free(c));
11794 if (pos2 >= isl_constraint_dim(c, type2))
11795 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11796 "index out of bounds", return isl_constraint_free(c));
11798 if (type1 == type2 && pos1 == pos2)
11799 return c;
11801 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11802 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11804 return c;
11807 /* Add a constraint imposing that the value of the first dimension is
11808 * greater than or equal to that of the second.
11810 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11811 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11813 isl_constraint *c;
11814 isl_space *space;
11816 if (type1 == type2 && pos1 == pos2)
11817 return bmap;
11818 space = isl_basic_map_get_space(bmap);
11819 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11820 bmap = isl_basic_map_add_constraint(bmap, c);
11822 return bmap;
11825 /* Add a constraint imposing that the value of the first dimension is
11826 * greater than or equal to that of the second.
11828 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11829 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11831 isl_constraint *c;
11832 isl_space *space;
11834 if (type1 == type2 && pos1 == pos2)
11835 return map;
11836 space = isl_map_get_space(map);
11837 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11838 map = isl_map_add_constraint(map, c);
11840 return map;
11843 /* Add a constraint imposing that the value of the first dimension is
11844 * less than or equal to that of the second.
11846 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11847 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11849 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11852 /* Construct a basic map where the value of the first dimension is
11853 * greater than that of the second.
11855 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11856 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11858 isl_basic_map *bmap = NULL;
11859 int i;
11861 if (!space)
11862 return NULL;
11864 if (pos1 >= isl_space_dim(space, type1))
11865 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11866 "index out of bounds", goto error);
11867 if (pos2 >= isl_space_dim(space, type2))
11868 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11869 "index out of bounds", goto error);
11871 if (type1 == type2 && pos1 == pos2)
11872 return isl_basic_map_empty(space);
11874 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11875 i = isl_basic_map_alloc_inequality(bmap);
11876 if (i < 0)
11877 return isl_basic_map_free(bmap);
11878 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11879 pos1 += isl_basic_map_offset(bmap, type1);
11880 pos2 += isl_basic_map_offset(bmap, type2);
11881 isl_int_set_si(bmap->ineq[i][pos1], 1);
11882 isl_int_set_si(bmap->ineq[i][pos2], -1);
11883 isl_int_set_si(bmap->ineq[i][0], -1);
11884 bmap = isl_basic_map_finalize(bmap);
11886 return bmap;
11887 error:
11888 isl_space_free(space);
11889 isl_basic_map_free(bmap);
11890 return NULL;
11893 /* Add a constraint imposing that the value of the first dimension is
11894 * greater than that of the second.
11896 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11897 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11899 isl_basic_map *gt;
11901 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11903 bmap = isl_basic_map_intersect(bmap, gt);
11905 return bmap;
11908 /* Add a constraint imposing that the value of the first dimension is
11909 * greater than that of the second.
11911 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11912 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11914 isl_basic_map *bmap;
11916 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11918 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11920 return map;
11923 /* Add a constraint imposing that the value of the first dimension is
11924 * smaller than that of the second.
11926 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11927 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11929 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11932 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11933 int pos)
11935 isl_aff *div;
11936 isl_local_space *ls;
11938 if (!bmap)
11939 return NULL;
11941 if (!isl_basic_map_divs_known(bmap))
11942 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11943 "some divs are unknown", return NULL);
11945 ls = isl_basic_map_get_local_space(bmap);
11946 div = isl_local_space_get_div(ls, pos);
11947 isl_local_space_free(ls);
11949 return div;
11952 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11953 int pos)
11955 return isl_basic_map_get_div(bset, pos);
11958 /* Plug in "subs" for dimension "type", "pos" of "bset".
11960 * Let i be the dimension to replace and let "subs" be of the form
11962 * f/d
11964 * Any integer division with a non-zero coefficient for i,
11966 * floor((a i + g)/m)
11968 * is replaced by
11970 * floor((a f + d g)/(m d))
11972 * Constraints of the form
11974 * a i + g
11976 * are replaced by
11978 * a f + d g
11980 * We currently require that "subs" is an integral expression.
11981 * Handling rational expressions may require us to add stride constraints
11982 * as we do in isl_basic_set_preimage_multi_aff.
11984 __isl_give isl_basic_set *isl_basic_set_substitute(
11985 __isl_take isl_basic_set *bset,
11986 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11988 int i;
11989 isl_int v;
11990 isl_ctx *ctx;
11992 if (bset && isl_basic_set_plain_is_empty(bset))
11993 return bset;
11995 bset = isl_basic_set_cow(bset);
11996 if (!bset || !subs)
11997 goto error;
11999 ctx = isl_basic_set_get_ctx(bset);
12000 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12001 isl_die(ctx, isl_error_invalid,
12002 "spaces don't match", goto error);
12003 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12004 isl_die(ctx, isl_error_unsupported,
12005 "cannot handle divs yet", goto error);
12006 if (!isl_int_is_one(subs->v->el[0]))
12007 isl_die(ctx, isl_error_invalid,
12008 "can only substitute integer expressions", goto error);
12010 pos += isl_basic_set_offset(bset, type);
12012 isl_int_init(v);
12014 for (i = 0; i < bset->n_eq; ++i) {
12015 if (isl_int_is_zero(bset->eq[i][pos]))
12016 continue;
12017 isl_int_set(v, bset->eq[i][pos]);
12018 isl_int_set_si(bset->eq[i][pos], 0);
12019 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12020 v, subs->v->el + 1, subs->v->size - 1);
12023 for (i = 0; i < bset->n_ineq; ++i) {
12024 if (isl_int_is_zero(bset->ineq[i][pos]))
12025 continue;
12026 isl_int_set(v, bset->ineq[i][pos]);
12027 isl_int_set_si(bset->ineq[i][pos], 0);
12028 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12029 v, subs->v->el + 1, subs->v->size - 1);
12032 for (i = 0; i < bset->n_div; ++i) {
12033 if (isl_int_is_zero(bset->div[i][1 + pos]))
12034 continue;
12035 isl_int_set(v, bset->div[i][1 + pos]);
12036 isl_int_set_si(bset->div[i][1 + pos], 0);
12037 isl_seq_combine(bset->div[i] + 1,
12038 subs->v->el[0], bset->div[i] + 1,
12039 v, subs->v->el + 1, subs->v->size - 1);
12040 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12043 isl_int_clear(v);
12045 bset = isl_basic_set_simplify(bset);
12046 return isl_basic_set_finalize(bset);
12047 error:
12048 isl_basic_set_free(bset);
12049 return NULL;
12052 /* Plug in "subs" for dimension "type", "pos" of "set".
12054 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12055 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12057 int i;
12059 if (set && isl_set_plain_is_empty(set))
12060 return set;
12062 set = isl_set_cow(set);
12063 if (!set || !subs)
12064 goto error;
12066 for (i = set->n - 1; i >= 0; --i) {
12067 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12068 if (remove_if_empty(set, i) < 0)
12069 goto error;
12072 return set;
12073 error:
12074 isl_set_free(set);
12075 return NULL;
12078 /* Check if the range of "ma" is compatible with the domain or range
12079 * (depending on "type") of "bmap".
12080 * Return -1 if anything is wrong.
12082 static int check_basic_map_compatible_range_multi_aff(
12083 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12084 __isl_keep isl_multi_aff *ma)
12086 int m;
12087 isl_space *ma_space;
12089 ma_space = isl_multi_aff_get_space(ma);
12091 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12092 if (m < 0)
12093 goto error;
12094 if (!m)
12095 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12096 "parameters don't match", goto error);
12097 m = isl_space_tuple_match(bmap->dim, type, ma_space, isl_dim_out);
12098 if (m < 0)
12099 goto error;
12100 if (!m)
12101 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12102 "spaces don't match", goto error);
12104 isl_space_free(ma_space);
12105 return m;
12106 error:
12107 isl_space_free(ma_space);
12108 return -1;
12111 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12112 * coefficients before the transformed range of dimensions,
12113 * the "n_after" coefficients after the transformed range of dimensions
12114 * and the coefficients of the other divs in "bmap".
12116 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12117 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12119 int i;
12120 int n_param;
12121 int n_set;
12122 isl_local_space *ls;
12124 if (n_div == 0)
12125 return 0;
12127 ls = isl_aff_get_domain_local_space(ma->p[0]);
12128 if (!ls)
12129 return -1;
12131 n_param = isl_local_space_dim(ls, isl_dim_param);
12132 n_set = isl_local_space_dim(ls, isl_dim_set);
12133 for (i = 0; i < n_div; ++i) {
12134 int o_bmap = 0, o_ls = 0;
12136 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12137 o_bmap += 1 + 1 + n_param;
12138 o_ls += 1 + 1 + n_param;
12139 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12140 o_bmap += n_before;
12141 isl_seq_cpy(bmap->div[i] + o_bmap,
12142 ls->div->row[i] + o_ls, n_set);
12143 o_bmap += n_set;
12144 o_ls += n_set;
12145 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12146 o_bmap += n_after;
12147 isl_seq_cpy(bmap->div[i] + o_bmap,
12148 ls->div->row[i] + o_ls, n_div);
12149 o_bmap += n_div;
12150 o_ls += n_div;
12151 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12152 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12153 goto error;
12156 isl_local_space_free(ls);
12157 return 0;
12158 error:
12159 isl_local_space_free(ls);
12160 return -1;
12163 /* How many stride constraints does "ma" enforce?
12164 * That is, how many of the affine expressions have a denominator
12165 * different from one?
12167 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12169 int i;
12170 int strides = 0;
12172 for (i = 0; i < ma->n; ++i)
12173 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12174 strides++;
12176 return strides;
12179 /* For each affine expression in ma of the form
12181 * x_i = (f_i y + h_i)/m_i
12183 * with m_i different from one, add a constraint to "bmap"
12184 * of the form
12186 * f_i y + h_i = m_i alpha_i
12188 * with alpha_i an additional existentially quantified variable.
12190 static __isl_give isl_basic_map *add_ma_strides(
12191 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12192 int n_before, int n_after)
12194 int i, k;
12195 int div;
12196 int total;
12197 int n_param;
12198 int n_in;
12199 int n_div;
12201 total = isl_basic_map_total_dim(bmap);
12202 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12203 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12204 n_div = isl_multi_aff_dim(ma, isl_dim_div);
12205 for (i = 0; i < ma->n; ++i) {
12206 int o_bmap = 0, o_ma = 1;
12208 if (isl_int_is_one(ma->p[i]->v->el[0]))
12209 continue;
12210 div = isl_basic_map_alloc_div(bmap);
12211 k = isl_basic_map_alloc_equality(bmap);
12212 if (div < 0 || k < 0)
12213 goto error;
12214 isl_int_set_si(bmap->div[div][0], 0);
12215 isl_seq_cpy(bmap->eq[k] + o_bmap,
12216 ma->p[i]->v->el + o_ma, 1 + n_param);
12217 o_bmap += 1 + n_param;
12218 o_ma += 1 + n_param;
12219 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12220 o_bmap += n_before;
12221 isl_seq_cpy(bmap->eq[k] + o_bmap,
12222 ma->p[i]->v->el + o_ma, n_in);
12223 o_bmap += n_in;
12224 o_ma += n_in;
12225 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12226 o_bmap += n_after;
12227 isl_seq_cpy(bmap->eq[k] + o_bmap,
12228 ma->p[i]->v->el + o_ma, n_div);
12229 o_bmap += n_div;
12230 o_ma += n_div;
12231 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12232 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12233 total++;
12236 return bmap;
12237 error:
12238 isl_basic_map_free(bmap);
12239 return NULL;
12242 /* Replace the domain or range space (depending on "type) of "space" by "set".
12244 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12245 enum isl_dim_type type, __isl_take isl_space *set)
12247 if (type == isl_dim_in) {
12248 space = isl_space_range(space);
12249 space = isl_space_map_from_domain_and_range(set, space);
12250 } else {
12251 space = isl_space_domain(space);
12252 space = isl_space_map_from_domain_and_range(space, set);
12255 return space;
12258 /* Compute the preimage of the domain or range (depending on "type")
12259 * of "bmap" under the function represented by "ma".
12260 * In other words, plug in "ma" in the domain or range of "bmap".
12261 * The result is a basic map that lives in the same space as "bmap"
12262 * except that the domain or range has been replaced by
12263 * the domain space of "ma".
12265 * If bmap is represented by
12267 * A(p) + S u + B x + T v + C(divs) >= 0,
12269 * where u and x are input and output dimensions if type == isl_dim_out
12270 * while x and v are input and output dimensions if type == isl_dim_in,
12271 * and ma is represented by
12273 * x = D(p) + F(y) + G(divs')
12275 * then the result is
12277 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12279 * The divs in the input set are similarly adjusted.
12280 * In particular
12282 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12284 * becomes
12286 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12287 * B_i G(divs') + c_i(divs))/n_i)
12289 * If bmap is not a rational map and if F(y) involves any denominators
12291 * x_i = (f_i y + h_i)/m_i
12293 * then additional constraints are added to ensure that we only
12294 * map back integer points. That is we enforce
12296 * f_i y + h_i = m_i alpha_i
12298 * with alpha_i an additional existentially quantified variable.
12300 * We first copy over the divs from "ma".
12301 * Then we add the modified constraints and divs from "bmap".
12302 * Finally, we add the stride constraints, if needed.
12304 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12305 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12306 __isl_take isl_multi_aff *ma)
12308 int i, k;
12309 isl_space *space;
12310 isl_basic_map *res = NULL;
12311 int n_before, n_after, n_div_bmap, n_div_ma;
12312 isl_int f, c1, c2, g;
12313 int rational, strides;
12315 isl_int_init(f);
12316 isl_int_init(c1);
12317 isl_int_init(c2);
12318 isl_int_init(g);
12320 ma = isl_multi_aff_align_divs(ma);
12321 if (!bmap || !ma)
12322 goto error;
12323 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12324 goto error;
12326 if (type == isl_dim_in) {
12327 n_before = 0;
12328 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12329 } else {
12330 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12331 n_after = 0;
12333 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12334 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12336 space = isl_multi_aff_get_domain_space(ma);
12337 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12338 rational = isl_basic_map_is_rational(bmap);
12339 strides = rational ? 0 : multi_aff_strides(ma);
12340 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12341 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12342 if (rational)
12343 res = isl_basic_map_set_rational(res);
12345 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12346 if (isl_basic_map_alloc_div(res) < 0)
12347 goto error;
12349 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12350 goto error;
12352 for (i = 0; i < bmap->n_eq; ++i) {
12353 k = isl_basic_map_alloc_equality(res);
12354 if (k < 0)
12355 goto error;
12356 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12357 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12360 for (i = 0; i < bmap->n_ineq; ++i) {
12361 k = isl_basic_map_alloc_inequality(res);
12362 if (k < 0)
12363 goto error;
12364 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12365 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12368 for (i = 0; i < bmap->n_div; ++i) {
12369 if (isl_int_is_zero(bmap->div[i][0])) {
12370 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12371 continue;
12373 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12374 n_before, n_after, n_div_ma, n_div_bmap,
12375 f, c1, c2, g, 1);
12378 if (strides)
12379 res = add_ma_strides(res, ma, n_before, n_after);
12381 isl_int_clear(f);
12382 isl_int_clear(c1);
12383 isl_int_clear(c2);
12384 isl_int_clear(g);
12385 isl_basic_map_free(bmap);
12386 isl_multi_aff_free(ma);
12387 res = isl_basic_set_simplify(res);
12388 return isl_basic_map_finalize(res);
12389 error:
12390 isl_int_clear(f);
12391 isl_int_clear(c1);
12392 isl_int_clear(c2);
12393 isl_int_clear(g);
12394 isl_basic_map_free(bmap);
12395 isl_multi_aff_free(ma);
12396 isl_basic_map_free(res);
12397 return NULL;
12400 /* Compute the preimage of "bset" under the function represented by "ma".
12401 * In other words, plug in "ma" in "bset". The result is a basic set
12402 * that lives in the domain space of "ma".
12404 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12405 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12407 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12410 /* Compute the preimage of the domain of "bmap" under the function
12411 * represented by "ma".
12412 * In other words, plug in "ma" in the domain of "bmap".
12413 * The result is a basic map that lives in the same space as "bmap"
12414 * except that the domain has been replaced by the domain space of "ma".
12416 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12417 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12419 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12422 /* Compute the preimage of the range of "bmap" under the function
12423 * represented by "ma".
12424 * In other words, plug in "ma" in the range of "bmap".
12425 * The result is a basic map that lives in the same space as "bmap"
12426 * except that the range has been replaced by the domain space of "ma".
12428 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12429 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12431 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12434 /* Check if the range of "ma" is compatible with the domain or range
12435 * (depending on "type") of "map".
12436 * Return -1 if anything is wrong.
12438 static int check_map_compatible_range_multi_aff(
12439 __isl_keep isl_map *map, enum isl_dim_type type,
12440 __isl_keep isl_multi_aff *ma)
12442 int m;
12443 isl_space *ma_space;
12445 ma_space = isl_multi_aff_get_space(ma);
12446 m = isl_space_tuple_match(map->dim, type, ma_space, isl_dim_out);
12447 isl_space_free(ma_space);
12448 if (m >= 0 && !m)
12449 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12450 "spaces don't match", return -1);
12451 return m;
12454 /* Compute the preimage of the domain or range (depending on "type")
12455 * of "map" under the function represented by "ma".
12456 * In other words, plug in "ma" in the domain or range of "map".
12457 * The result is a map that lives in the same space as "map"
12458 * except that the domain or range has been replaced by
12459 * the domain space of "ma".
12461 * The parameters are assumed to have been aligned.
12463 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12464 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12466 int i;
12467 isl_space *space;
12469 map = isl_map_cow(map);
12470 ma = isl_multi_aff_align_divs(ma);
12471 if (!map || !ma)
12472 goto error;
12473 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12474 goto error;
12476 for (i = 0; i < map->n; ++i) {
12477 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12478 isl_multi_aff_copy(ma));
12479 if (!map->p[i])
12480 goto error;
12483 space = isl_multi_aff_get_domain_space(ma);
12484 space = isl_space_set(isl_map_get_space(map), type, space);
12486 isl_space_free(map->dim);
12487 map->dim = space;
12488 if (!map->dim)
12489 goto error;
12491 isl_multi_aff_free(ma);
12492 if (map->n > 1)
12493 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12494 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12495 return map;
12496 error:
12497 isl_multi_aff_free(ma);
12498 isl_map_free(map);
12499 return NULL;
12502 /* Compute the preimage of the domain or range (depending on "type")
12503 * of "map" under the function represented by "ma".
12504 * In other words, plug in "ma" in the domain or range of "map".
12505 * The result is a map that lives in the same space as "map"
12506 * except that the domain or range has been replaced by
12507 * the domain space of "ma".
12509 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12510 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12512 if (!map || !ma)
12513 goto error;
12515 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12516 return map_preimage_multi_aff(map, type, ma);
12518 if (!isl_space_has_named_params(map->dim) ||
12519 !isl_space_has_named_params(ma->space))
12520 isl_die(map->ctx, isl_error_invalid,
12521 "unaligned unnamed parameters", goto error);
12522 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12523 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12525 return map_preimage_multi_aff(map, type, ma);
12526 error:
12527 isl_multi_aff_free(ma);
12528 return isl_map_free(map);
12531 /* Compute the preimage of "set" under the function represented by "ma".
12532 * In other words, plug in "ma" in "set". The result is a set
12533 * that lives in the domain space of "ma".
12535 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12536 __isl_take isl_multi_aff *ma)
12538 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12541 /* Compute the preimage of the domain of "map" under the function
12542 * represented by "ma".
12543 * In other words, plug in "ma" in the domain of "map".
12544 * The result is a map that lives in the same space as "map"
12545 * except that the domain has been replaced by the domain space of "ma".
12547 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12548 __isl_take isl_multi_aff *ma)
12550 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12553 /* Compute the preimage of the range of "map" under the function
12554 * represented by "ma".
12555 * In other words, plug in "ma" in the range of "map".
12556 * The result is a map that lives in the same space as "map"
12557 * except that the range has been replaced by the domain space of "ma".
12559 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12560 __isl_take isl_multi_aff *ma)
12562 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12565 /* Compute the preimage of "map" under the function represented by "pma".
12566 * In other words, plug in "pma" in the domain or range of "map".
12567 * The result is a map that lives in the same space as "map",
12568 * except that the space of type "type" has been replaced by
12569 * the domain space of "pma".
12571 * The parameters of "map" and "pma" are assumed to have been aligned.
12573 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12574 __isl_take isl_map *map, enum isl_dim_type type,
12575 __isl_take isl_pw_multi_aff *pma)
12577 int i;
12578 isl_map *res;
12580 if (!pma)
12581 goto error;
12583 if (pma->n == 0) {
12584 isl_pw_multi_aff_free(pma);
12585 res = isl_map_empty(isl_map_get_space(map));
12586 isl_map_free(map);
12587 return res;
12590 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12591 isl_multi_aff_copy(pma->p[0].maff));
12592 if (type == isl_dim_in)
12593 res = isl_map_intersect_domain(res,
12594 isl_map_copy(pma->p[0].set));
12595 else
12596 res = isl_map_intersect_range(res,
12597 isl_map_copy(pma->p[0].set));
12599 for (i = 1; i < pma->n; ++i) {
12600 isl_map *res_i;
12602 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12603 isl_multi_aff_copy(pma->p[i].maff));
12604 if (type == isl_dim_in)
12605 res_i = isl_map_intersect_domain(res_i,
12606 isl_map_copy(pma->p[i].set));
12607 else
12608 res_i = isl_map_intersect_range(res_i,
12609 isl_map_copy(pma->p[i].set));
12610 res = isl_map_union(res, res_i);
12613 isl_pw_multi_aff_free(pma);
12614 isl_map_free(map);
12615 return res;
12616 error:
12617 isl_pw_multi_aff_free(pma);
12618 isl_map_free(map);
12619 return NULL;
12622 /* Compute the preimage of "map" under the function represented by "pma".
12623 * In other words, plug in "pma" in the domain or range of "map".
12624 * The result is a map that lives in the same space as "map",
12625 * except that the space of type "type" has been replaced by
12626 * the domain space of "pma".
12628 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12629 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12631 if (!map || !pma)
12632 goto error;
12634 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12635 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12637 if (!isl_space_has_named_params(map->dim) ||
12638 !isl_space_has_named_params(pma->dim))
12639 isl_die(map->ctx, isl_error_invalid,
12640 "unaligned unnamed parameters", goto error);
12641 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12642 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12644 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12645 error:
12646 isl_pw_multi_aff_free(pma);
12647 return isl_map_free(map);
12650 /* Compute the preimage of "set" under the function represented by "pma".
12651 * In other words, plug in "pma" in "set". The result is a set
12652 * that lives in the domain space of "pma".
12654 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12655 __isl_take isl_pw_multi_aff *pma)
12657 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12660 /* Compute the preimage of the domain of "map" under the function
12661 * represented by "pma".
12662 * In other words, plug in "pma" in the domain of "map".
12663 * The result is a map that lives in the same space as "map",
12664 * except that domain space has been replaced by the domain space of "pma".
12666 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12667 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12669 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12672 /* Compute the preimage of the range of "map" under the function
12673 * represented by "pma".
12674 * In other words, plug in "pma" in the range of "map".
12675 * The result is a map that lives in the same space as "map",
12676 * except that range space has been replaced by the domain space of "pma".
12678 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12679 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12681 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12684 /* Compute the preimage of "map" under the function represented by "mpa".
12685 * In other words, plug in "mpa" in the domain or range of "map".
12686 * The result is a map that lives in the same space as "map",
12687 * except that the space of type "type" has been replaced by
12688 * the domain space of "mpa".
12690 * If the map does not involve any constraints that refer to the
12691 * dimensions of the substituted space, then the only possible
12692 * effect of "mpa" on the map is to map the space to a different space.
12693 * We create a separate isl_multi_aff to effectuate this change
12694 * in order to avoid spurious splitting of the map along the pieces
12695 * of "mpa".
12697 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12698 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12700 int n;
12701 isl_pw_multi_aff *pma;
12703 if (!map || !mpa)
12704 goto error;
12706 n = isl_map_dim(map, type);
12707 if (!isl_map_involves_dims(map, type, 0, n)) {
12708 isl_space *space;
12709 isl_multi_aff *ma;
12711 space = isl_multi_pw_aff_get_space(mpa);
12712 isl_multi_pw_aff_free(mpa);
12713 ma = isl_multi_aff_zero(space);
12714 return isl_map_preimage_multi_aff(map, type, ma);
12717 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12718 return isl_map_preimage_pw_multi_aff(map, type, pma);
12719 error:
12720 isl_map_free(map);
12721 isl_multi_pw_aff_free(mpa);
12722 return NULL;
12725 /* Compute the preimage of "map" under the function represented by "mpa".
12726 * In other words, plug in "mpa" in the domain "map".
12727 * The result is a map that lives in the same space as "map",
12728 * except that domain space has been replaced by the domain space of "mpa".
12730 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12731 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12733 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12736 /* Compute the preimage of "set" by the function represented by "mpa".
12737 * In other words, plug in "mpa" in "set".
12739 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12740 __isl_take isl_multi_pw_aff *mpa)
12742 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);