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