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