isl_test: generalize coalesce tests with unbounded wrapping
[isl.git] / isl_map.c
blobd17cf26f577a5516529fb209569483f9539809cb
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <string.h>
19 #include <isl_ctx_private.h>
20 #include <isl_map_private.h>
21 #include <isl_blk.h>
22 #include "isl_space_private.h"
23 #include "isl_equalities.h"
24 #include <isl_lp_private.h>
25 #include <isl_seq.h>
26 #include <isl/set.h>
27 #include <isl/map.h>
28 #include <isl_reordering.h>
29 #include "isl_sample.h"
30 #include <isl_sort.h>
31 #include "isl_tab.h"
32 #include <isl/vec.h>
33 #include <isl_mat_private.h>
34 #include <isl_vec_private.h>
35 #include <isl_dim_map.h>
36 #include <isl_local_space_private.h>
37 #include <isl_aff_private.h>
38 #include <isl_options_private.h>
39 #include <isl_morph.h>
40 #include <isl_val_private.h>
41 #include <isl/deprecated/map_int.h>
42 #include <isl/deprecated/set_int.h>
44 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
46 switch (type) {
47 case isl_dim_param: return dim->nparam;
48 case isl_dim_in: return dim->n_in;
49 case isl_dim_out: return dim->n_out;
50 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
51 default: return 0;
55 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
57 switch (type) {
58 case isl_dim_param: return 1;
59 case isl_dim_in: return 1 + dim->nparam;
60 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
61 default: return 0;
65 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
66 enum isl_dim_type type)
68 if (!bmap)
69 return 0;
70 switch (type) {
71 case isl_dim_cst: return 1;
72 case isl_dim_param:
73 case isl_dim_in:
74 case isl_dim_out: return isl_space_dim(bmap->dim, type);
75 case isl_dim_div: return bmap->n_div;
76 case isl_dim_all: return isl_basic_map_total_dim(bmap);
77 default: return 0;
81 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
83 return map ? n(map->dim, type) : 0;
86 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
88 return set ? n(set->dim, type) : 0;
91 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
92 enum isl_dim_type type)
94 isl_space *dim = bmap->dim;
95 switch (type) {
96 case isl_dim_cst: return 0;
97 case isl_dim_param: return 1;
98 case isl_dim_in: return 1 + dim->nparam;
99 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
100 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
101 default: return 0;
105 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
106 enum isl_dim_type type)
108 return isl_basic_map_offset(bset, type);
111 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
113 return pos(map->dim, type);
116 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
117 enum isl_dim_type type)
119 return isl_basic_map_dim(bset, type);
122 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
124 return isl_basic_set_dim(bset, isl_dim_set);
127 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
129 return isl_basic_set_dim(bset, isl_dim_param);
132 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
134 if (!bset)
135 return 0;
136 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
139 unsigned isl_set_n_dim(__isl_keep isl_set *set)
141 return isl_set_dim(set, isl_dim_set);
144 unsigned isl_set_n_param(__isl_keep isl_set *set)
146 return isl_set_dim(set, isl_dim_param);
149 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
151 return bmap ? bmap->dim->n_in : 0;
154 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
156 return bmap ? bmap->dim->n_out : 0;
159 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
161 return bmap ? bmap->dim->nparam : 0;
164 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
166 return bmap ? bmap->n_div : 0;
169 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
171 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
174 unsigned isl_map_n_in(const struct isl_map *map)
176 return map ? map->dim->n_in : 0;
179 unsigned isl_map_n_out(const struct isl_map *map)
181 return map ? map->dim->n_out : 0;
184 unsigned isl_map_n_param(const struct isl_map *map)
186 return map ? map->dim->nparam : 0;
189 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
191 int m;
192 if (!map || !set)
193 return -1;
194 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
195 if (m < 0 || !m)
196 return m;
197 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
198 set->dim, isl_dim_set);
201 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
202 struct isl_basic_set *bset)
204 int m;
205 if (!bmap || !bset)
206 return -1;
207 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
208 if (m < 0 || !m)
209 return m;
210 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
211 bset->dim, isl_dim_set);
214 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
216 int m;
217 if (!map || !set)
218 return -1;
219 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
220 if (m < 0 || !m)
221 return m;
222 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
223 set->dim, isl_dim_set);
226 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
227 struct isl_basic_set *bset)
229 int m;
230 if (!bmap || !bset)
231 return -1;
232 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
233 if (m < 0 || !m)
234 return m;
235 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
236 bset->dim, isl_dim_set);
239 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
241 return bmap ? bmap->ctx : NULL;
244 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
246 return bset ? bset->ctx : NULL;
249 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
251 return map ? map->ctx : NULL;
254 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
256 return set ? set->ctx : NULL;
259 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
261 if (!bmap)
262 return NULL;
263 return isl_space_copy(bmap->dim);
266 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
268 if (!bset)
269 return NULL;
270 return isl_space_copy(bset->dim);
273 /* Extract the divs in "bmap" as a matrix.
275 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
277 int i;
278 isl_ctx *ctx;
279 isl_mat *div;
280 unsigned total;
281 unsigned cols;
283 if (!bmap)
284 return NULL;
286 ctx = isl_basic_map_get_ctx(bmap);
287 total = isl_space_dim(bmap->dim, isl_dim_all);
288 cols = 1 + 1 + total + bmap->n_div;
289 div = isl_mat_alloc(ctx, bmap->n_div, cols);
290 if (!div)
291 return NULL;
293 for (i = 0; i < bmap->n_div; ++i)
294 isl_seq_cpy(div->row[i], bmap->div[i], cols);
296 return div;
299 /* Extract the divs in "bset" as a matrix.
301 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
303 return isl_basic_map_get_divs(bset);
306 __isl_give isl_local_space *isl_basic_map_get_local_space(
307 __isl_keep isl_basic_map *bmap)
309 isl_mat *div;
311 if (!bmap)
312 return NULL;
314 div = isl_basic_map_get_divs(bmap);
315 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
318 __isl_give isl_local_space *isl_basic_set_get_local_space(
319 __isl_keep isl_basic_set *bset)
321 return isl_basic_map_get_local_space(bset);
324 __isl_give isl_basic_map *isl_basic_map_from_local_space(
325 __isl_take isl_local_space *ls)
327 int i;
328 int n_div;
329 isl_basic_map *bmap;
331 if (!ls)
332 return NULL;
334 n_div = isl_local_space_dim(ls, isl_dim_div);
335 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
336 n_div, 0, 2 * n_div);
338 for (i = 0; i < n_div; ++i)
339 if (isl_basic_map_alloc_div(bmap) < 0)
340 goto error;
342 for (i = 0; i < n_div; ++i) {
343 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
344 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
345 goto error;
348 isl_local_space_free(ls);
349 return bmap;
350 error:
351 isl_local_space_free(ls);
352 isl_basic_map_free(bmap);
353 return NULL;
356 __isl_give isl_basic_set *isl_basic_set_from_local_space(
357 __isl_take isl_local_space *ls)
359 return isl_basic_map_from_local_space(ls);
362 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
364 if (!map)
365 return NULL;
366 return isl_space_copy(map->dim);
369 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
371 if (!set)
372 return NULL;
373 return isl_space_copy(set->dim);
376 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
377 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
379 bmap = isl_basic_map_cow(bmap);
380 if (!bmap)
381 return NULL;
382 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
383 if (!bmap->dim)
384 goto error;
385 bmap = isl_basic_map_finalize(bmap);
386 return bmap;
387 error:
388 isl_basic_map_free(bmap);
389 return NULL;
392 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
393 __isl_take isl_basic_set *bset, const char *s)
395 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
398 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
399 enum isl_dim_type type)
401 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
404 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
405 enum isl_dim_type type, const char *s)
407 int i;
409 map = isl_map_cow(map);
410 if (!map)
411 return NULL;
413 map->dim = isl_space_set_tuple_name(map->dim, type, s);
414 if (!map->dim)
415 goto error;
417 for (i = 0; i < map->n; ++i) {
418 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
419 if (!map->p[i])
420 goto error;
423 return map;
424 error:
425 isl_map_free(map);
426 return NULL;
429 /* Replace the identifier of the tuple of type "type" by "id".
431 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
432 __isl_take isl_basic_map *bmap,
433 enum isl_dim_type type, __isl_take isl_id *id)
435 bmap = isl_basic_map_cow(bmap);
436 if (!bmap)
437 goto error;
438 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
439 if (!bmap->dim)
440 return isl_basic_map_free(bmap);
441 bmap = isl_basic_map_finalize(bmap);
442 return bmap;
443 error:
444 isl_id_free(id);
445 return NULL;
448 /* Replace the identifier of the tuple by "id".
450 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
451 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
453 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
456 /* Does the input or output tuple have a name?
458 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
460 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
463 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
464 enum isl_dim_type type)
466 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
469 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
470 const char *s)
472 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
475 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
476 enum isl_dim_type type, __isl_take isl_id *id)
478 map = isl_map_cow(map);
479 if (!map)
480 goto error;
482 map->dim = isl_space_set_tuple_id(map->dim, type, id);
484 return isl_map_reset_space(map, isl_space_copy(map->dim));
485 error:
486 isl_id_free(id);
487 return NULL;
490 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
491 __isl_take isl_id *id)
493 return isl_map_set_tuple_id(set, isl_dim_set, id);
496 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
497 enum isl_dim_type type)
499 map = isl_map_cow(map);
500 if (!map)
501 return NULL;
503 map->dim = isl_space_reset_tuple_id(map->dim, type);
505 return isl_map_reset_space(map, isl_space_copy(map->dim));
508 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
510 return isl_map_reset_tuple_id(set, isl_dim_set);
513 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
515 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
518 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
519 enum isl_dim_type type)
521 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
524 int isl_set_has_tuple_id(__isl_keep isl_set *set)
526 return isl_map_has_tuple_id(set, isl_dim_set);
529 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
531 return isl_map_get_tuple_id(set, isl_dim_set);
534 /* Does the set tuple have a name?
536 int isl_set_has_tuple_name(__isl_keep isl_set *set)
538 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
542 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
544 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
547 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
549 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
552 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
553 enum isl_dim_type type, unsigned pos)
555 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
558 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
559 enum isl_dim_type type, unsigned pos)
561 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
564 /* Does the given dimension have a name?
566 int isl_map_has_dim_name(__isl_keep isl_map *map,
567 enum isl_dim_type type, unsigned pos)
569 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
572 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
573 enum isl_dim_type type, unsigned pos)
575 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
578 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
579 enum isl_dim_type type, unsigned pos)
581 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
584 /* Does the given dimension have a name?
586 int isl_set_has_dim_name(__isl_keep isl_set *set,
587 enum isl_dim_type type, unsigned pos)
589 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
592 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
593 __isl_take isl_basic_map *bmap,
594 enum isl_dim_type type, unsigned pos, const char *s)
596 bmap = isl_basic_map_cow(bmap);
597 if (!bmap)
598 return NULL;
599 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
600 if (!bmap->dim)
601 goto error;
602 return isl_basic_map_finalize(bmap);
603 error:
604 isl_basic_map_free(bmap);
605 return NULL;
608 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
609 enum isl_dim_type type, unsigned pos, const char *s)
611 int i;
613 map = isl_map_cow(map);
614 if (!map)
615 return NULL;
617 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
618 if (!map->dim)
619 goto error;
621 for (i = 0; i < map->n; ++i) {
622 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
623 if (!map->p[i])
624 goto error;
627 return map;
628 error:
629 isl_map_free(map);
630 return NULL;
633 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
634 __isl_take isl_basic_set *bset,
635 enum isl_dim_type type, unsigned pos, const char *s)
637 return (isl_basic_set *)isl_basic_map_set_dim_name(
638 (isl_basic_map *)bset, type, pos, s);
641 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
642 enum isl_dim_type type, unsigned pos, const char *s)
644 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
647 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
648 enum isl_dim_type type, unsigned pos)
650 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
653 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
654 enum isl_dim_type type, unsigned pos)
656 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
659 int isl_map_has_dim_id(__isl_keep isl_map *map,
660 enum isl_dim_type type, unsigned pos)
662 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
665 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
666 enum isl_dim_type type, unsigned pos)
668 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
671 int isl_set_has_dim_id(__isl_keep isl_set *set,
672 enum isl_dim_type type, unsigned pos)
674 return isl_map_has_dim_id(set, type, pos);
677 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
678 enum isl_dim_type type, unsigned pos)
680 return isl_map_get_dim_id(set, type, pos);
683 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
684 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
686 map = isl_map_cow(map);
687 if (!map)
688 goto error;
690 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
692 return isl_map_reset_space(map, isl_space_copy(map->dim));
693 error:
694 isl_id_free(id);
695 return NULL;
698 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
699 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
701 return isl_map_set_dim_id(set, type, pos, id);
704 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
705 __isl_keep isl_id *id)
707 if (!map)
708 return -1;
709 return isl_space_find_dim_by_id(map->dim, type, id);
712 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
713 __isl_keep isl_id *id)
715 return isl_map_find_dim_by_id(set, type, id);
718 /* Return the position of the dimension of the given type and name
719 * in "bmap".
720 * Return -1 if no such dimension can be found.
722 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
723 enum isl_dim_type type, const char *name)
725 if (!bmap)
726 return -1;
727 return isl_space_find_dim_by_name(bmap->dim, type, name);
730 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
731 const char *name)
733 if (!map)
734 return -1;
735 return isl_space_find_dim_by_name(map->dim, type, name);
738 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
739 const char *name)
741 return isl_map_find_dim_by_name(set, type, name);
744 /* Reset the user pointer on all identifiers of parameters and tuples
745 * of the space of "map".
747 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
749 isl_space *space;
751 space = isl_map_get_space(map);
752 space = isl_space_reset_user(space);
753 map = isl_map_reset_space(map, space);
755 return map;
758 /* Reset the user pointer on all identifiers of parameters and tuples
759 * of the space of "set".
761 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
763 return isl_map_reset_user(set);
766 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
768 if (!bmap)
769 return -1;
770 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
773 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
775 return isl_basic_map_is_rational(bset);
778 /* Does "bmap" contain any rational points?
780 * If "bmap" has an equality for each dimension, equating the dimension
781 * to an integer constant, then it has no rational points, even if it
782 * is marked as rational.
784 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
786 int has_rational = 1;
787 unsigned total;
789 if (!bmap)
790 return -1;
791 if (isl_basic_map_plain_is_empty(bmap))
792 return 0;
793 if (!isl_basic_map_is_rational(bmap))
794 return 0;
795 bmap = isl_basic_map_copy(bmap);
796 bmap = isl_basic_map_implicit_equalities(bmap);
797 if (!bmap)
798 return -1;
799 total = isl_basic_map_total_dim(bmap);
800 if (bmap->n_eq == total) {
801 int i, j;
802 for (i = 0; i < bmap->n_eq; ++i) {
803 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
804 if (j < 0)
805 break;
806 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
807 !isl_int_is_negone(bmap->eq[i][1 + j]))
808 break;
809 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
810 total - j - 1);
811 if (j >= 0)
812 break;
814 if (i == bmap->n_eq)
815 has_rational = 0;
817 isl_basic_map_free(bmap);
819 return has_rational;
822 /* Does "map" contain any rational points?
824 int isl_map_has_rational(__isl_keep isl_map *map)
826 int i;
827 int has_rational;
829 if (!map)
830 return -1;
831 for (i = 0; i < map->n; ++i) {
832 has_rational = isl_basic_map_has_rational(map->p[i]);
833 if (has_rational < 0)
834 return -1;
835 if (has_rational)
836 return 1;
838 return 0;
841 /* Does "set" contain any rational points?
843 int isl_set_has_rational(__isl_keep isl_set *set)
845 return isl_map_has_rational(set);
848 /* Is this basic set a parameter domain?
850 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
852 if (!bset)
853 return -1;
854 return isl_space_is_params(bset->dim);
857 /* Is this set a parameter domain?
859 int isl_set_is_params(__isl_keep isl_set *set)
861 if (!set)
862 return -1;
863 return isl_space_is_params(set->dim);
866 /* Is this map actually a parameter domain?
867 * Users should never call this function. Outside of isl,
868 * a map can never be a parameter domain.
870 int isl_map_is_params(__isl_keep isl_map *map)
872 if (!map)
873 return -1;
874 return isl_space_is_params(map->dim);
877 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
878 struct isl_basic_map *bmap, unsigned extra,
879 unsigned n_eq, unsigned n_ineq)
881 int i;
882 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
884 bmap->ctx = ctx;
885 isl_ctx_ref(ctx);
887 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
888 if (isl_blk_is_error(bmap->block))
889 goto error;
891 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
892 if ((n_ineq + n_eq) && !bmap->ineq)
893 goto error;
895 if (extra == 0) {
896 bmap->block2 = isl_blk_empty();
897 bmap->div = NULL;
898 } else {
899 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
900 if (isl_blk_is_error(bmap->block2))
901 goto error;
903 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
904 if (!bmap->div)
905 goto error;
908 for (i = 0; i < n_ineq + n_eq; ++i)
909 bmap->ineq[i] = bmap->block.data + i * row_size;
911 for (i = 0; i < extra; ++i)
912 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
914 bmap->ref = 1;
915 bmap->flags = 0;
916 bmap->c_size = n_eq + n_ineq;
917 bmap->eq = bmap->ineq + n_ineq;
918 bmap->extra = extra;
919 bmap->n_eq = 0;
920 bmap->n_ineq = 0;
921 bmap->n_div = 0;
922 bmap->sample = NULL;
924 return bmap;
925 error:
926 isl_basic_map_free(bmap);
927 return NULL;
930 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
931 unsigned nparam, unsigned dim, unsigned extra,
932 unsigned n_eq, unsigned n_ineq)
934 struct isl_basic_map *bmap;
935 isl_space *space;
937 space = isl_space_set_alloc(ctx, nparam, dim);
938 if (!space)
939 return NULL;
941 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
942 return (struct isl_basic_set *)bmap;
945 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
946 unsigned extra, unsigned n_eq, unsigned n_ineq)
948 struct isl_basic_map *bmap;
949 if (!dim)
950 return NULL;
951 isl_assert(dim->ctx, dim->n_in == 0, goto error);
952 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
953 return (struct isl_basic_set *)bmap;
954 error:
955 isl_space_free(dim);
956 return NULL;
959 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
960 unsigned extra, unsigned n_eq, unsigned n_ineq)
962 struct isl_basic_map *bmap;
964 if (!dim)
965 return NULL;
966 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
967 if (!bmap)
968 goto error;
969 bmap->dim = dim;
971 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
972 error:
973 isl_space_free(dim);
974 return NULL;
977 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
978 unsigned nparam, unsigned in, unsigned out, unsigned extra,
979 unsigned n_eq, unsigned n_ineq)
981 struct isl_basic_map *bmap;
982 isl_space *dim;
984 dim = isl_space_alloc(ctx, nparam, in, out);
985 if (!dim)
986 return NULL;
988 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
989 return bmap;
992 static void dup_constraints(
993 struct isl_basic_map *dst, struct isl_basic_map *src)
995 int i;
996 unsigned total = isl_basic_map_total_dim(src);
998 for (i = 0; i < src->n_eq; ++i) {
999 int j = isl_basic_map_alloc_equality(dst);
1000 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1003 for (i = 0; i < src->n_ineq; ++i) {
1004 int j = isl_basic_map_alloc_inequality(dst);
1005 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1008 for (i = 0; i < src->n_div; ++i) {
1009 int j = isl_basic_map_alloc_div(dst);
1010 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1012 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1015 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1017 struct isl_basic_map *dup;
1019 if (!bmap)
1020 return NULL;
1021 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1022 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1023 if (!dup)
1024 return NULL;
1025 dup_constraints(dup, bmap);
1026 dup->flags = bmap->flags;
1027 dup->sample = isl_vec_copy(bmap->sample);
1028 return dup;
1031 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1033 struct isl_basic_map *dup;
1035 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1036 return (struct isl_basic_set *)dup;
1039 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1041 if (!bset)
1042 return NULL;
1044 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1045 bset->ref++;
1046 return bset;
1048 return isl_basic_set_dup(bset);
1051 struct isl_set *isl_set_copy(struct isl_set *set)
1053 if (!set)
1054 return NULL;
1056 set->ref++;
1057 return set;
1060 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1062 if (!bmap)
1063 return NULL;
1065 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1066 bmap->ref++;
1067 return bmap;
1069 bmap = isl_basic_map_dup(bmap);
1070 if (bmap)
1071 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1072 return bmap;
1075 struct isl_map *isl_map_copy(struct isl_map *map)
1077 if (!map)
1078 return NULL;
1080 map->ref++;
1081 return map;
1084 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1086 if (!bmap)
1087 return NULL;
1089 if (--bmap->ref > 0)
1090 return NULL;
1092 isl_ctx_deref(bmap->ctx);
1093 free(bmap->div);
1094 isl_blk_free(bmap->ctx, bmap->block2);
1095 free(bmap->ineq);
1096 isl_blk_free(bmap->ctx, bmap->block);
1097 isl_vec_free(bmap->sample);
1098 isl_space_free(bmap->dim);
1099 free(bmap);
1101 return NULL;
1104 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1106 return isl_basic_map_free((struct isl_basic_map *)bset);
1109 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1111 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1114 __isl_give isl_map *isl_map_align_params_map_map_and(
1115 __isl_take isl_map *map1, __isl_take isl_map *map2,
1116 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1117 __isl_take isl_map *map2))
1119 if (!map1 || !map2)
1120 goto error;
1121 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1122 return fn(map1, map2);
1123 if (!isl_space_has_named_params(map1->dim) ||
1124 !isl_space_has_named_params(map2->dim))
1125 isl_die(map1->ctx, isl_error_invalid,
1126 "unaligned unnamed parameters", goto error);
1127 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1128 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1129 return fn(map1, map2);
1130 error:
1131 isl_map_free(map1);
1132 isl_map_free(map2);
1133 return NULL;
1136 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1137 __isl_keep isl_map *map2,
1138 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1140 int r;
1142 if (!map1 || !map2)
1143 return -1;
1144 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1145 return fn(map1, map2);
1146 if (!isl_space_has_named_params(map1->dim) ||
1147 !isl_space_has_named_params(map2->dim))
1148 isl_die(map1->ctx, isl_error_invalid,
1149 "unaligned unnamed parameters", return -1);
1150 map1 = isl_map_copy(map1);
1151 map2 = isl_map_copy(map2);
1152 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1153 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1154 r = fn(map1, map2);
1155 isl_map_free(map1);
1156 isl_map_free(map2);
1157 return r;
1160 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1162 struct isl_ctx *ctx;
1163 if (!bmap)
1164 return -1;
1165 ctx = bmap->ctx;
1166 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1167 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1168 return -1);
1169 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1170 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1171 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1172 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1173 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1174 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1175 isl_int *t;
1176 int j = isl_basic_map_alloc_inequality(bmap);
1177 if (j < 0)
1178 return -1;
1179 t = bmap->ineq[j];
1180 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1181 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1182 bmap->eq[-1] = t;
1183 bmap->n_eq++;
1184 bmap->n_ineq--;
1185 bmap->eq--;
1186 return 0;
1188 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1189 bmap->extra - bmap->n_div);
1190 return bmap->n_eq++;
1193 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1195 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1198 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1200 if (!bmap)
1201 return -1;
1202 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1203 bmap->n_eq -= n;
1204 return 0;
1207 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1209 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1212 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1214 isl_int *t;
1215 if (!bmap)
1216 return -1;
1217 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1219 if (pos != bmap->n_eq - 1) {
1220 t = bmap->eq[pos];
1221 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1222 bmap->eq[bmap->n_eq - 1] = t;
1224 bmap->n_eq--;
1225 return 0;
1228 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1230 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1233 /* Turn inequality "pos" of "bmap" into an equality.
1235 * In particular, we move the inequality in front of the equalities
1236 * and move the last inequality in the position of the moved inequality.
1237 * Note that isl_tab_make_equalities_explicit depends on this particular
1238 * change in the ordering of the constraints.
1240 void isl_basic_map_inequality_to_equality(
1241 struct isl_basic_map *bmap, unsigned pos)
1243 isl_int *t;
1245 t = bmap->ineq[pos];
1246 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1247 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1248 bmap->eq[-1] = t;
1249 bmap->n_eq++;
1250 bmap->n_ineq--;
1251 bmap->eq--;
1252 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1253 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1254 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1255 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1258 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1260 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1263 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1265 struct isl_ctx *ctx;
1266 if (!bmap)
1267 return -1;
1268 ctx = bmap->ctx;
1269 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1270 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1272 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1273 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1274 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1275 1 + isl_basic_map_total_dim(bmap),
1276 bmap->extra - bmap->n_div);
1277 return bmap->n_ineq++;
1280 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1282 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1285 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1287 if (!bmap)
1288 return -1;
1289 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1290 bmap->n_ineq -= n;
1291 return 0;
1294 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1296 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1299 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1301 isl_int *t;
1302 if (!bmap)
1303 return -1;
1304 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1306 if (pos != bmap->n_ineq - 1) {
1307 t = bmap->ineq[pos];
1308 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1309 bmap->ineq[bmap->n_ineq - 1] = t;
1310 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1312 bmap->n_ineq--;
1313 return 0;
1316 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1318 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1321 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1322 isl_int *eq)
1324 int k;
1326 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1327 if (!bmap)
1328 return NULL;
1329 k = isl_basic_map_alloc_equality(bmap);
1330 if (k < 0)
1331 goto error;
1332 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1333 return bmap;
1334 error:
1335 isl_basic_map_free(bmap);
1336 return NULL;
1339 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1340 isl_int *eq)
1342 return (isl_basic_set *)
1343 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1346 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1347 isl_int *ineq)
1349 int k;
1351 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1352 if (!bmap)
1353 return NULL;
1354 k = isl_basic_map_alloc_inequality(bmap);
1355 if (k < 0)
1356 goto error;
1357 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1358 return bmap;
1359 error:
1360 isl_basic_map_free(bmap);
1361 return NULL;
1364 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1365 isl_int *ineq)
1367 return (isl_basic_set *)
1368 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1371 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1373 if (!bmap)
1374 return -1;
1375 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1376 isl_seq_clr(bmap->div[bmap->n_div] +
1377 1 + 1 + isl_basic_map_total_dim(bmap),
1378 bmap->extra - bmap->n_div);
1379 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1380 return bmap->n_div++;
1383 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1385 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1388 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1390 if (!bmap)
1391 return -1;
1392 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1393 bmap->n_div -= n;
1394 return 0;
1397 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1399 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1402 /* Copy constraint from src to dst, putting the vars of src at offset
1403 * dim_off in dst and the divs of src at offset div_off in dst.
1404 * If both sets are actually map, then dim_off applies to the input
1405 * variables.
1407 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1408 struct isl_basic_map *src_map, isl_int *src,
1409 unsigned in_off, unsigned out_off, unsigned div_off)
1411 unsigned src_nparam = isl_basic_map_n_param(src_map);
1412 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1413 unsigned src_in = isl_basic_map_n_in(src_map);
1414 unsigned dst_in = isl_basic_map_n_in(dst_map);
1415 unsigned src_out = isl_basic_map_n_out(src_map);
1416 unsigned dst_out = isl_basic_map_n_out(dst_map);
1417 isl_int_set(dst[0], src[0]);
1418 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1419 if (dst_nparam > src_nparam)
1420 isl_seq_clr(dst+1+src_nparam,
1421 dst_nparam - src_nparam);
1422 isl_seq_clr(dst+1+dst_nparam, in_off);
1423 isl_seq_cpy(dst+1+dst_nparam+in_off,
1424 src+1+src_nparam,
1425 isl_min(dst_in-in_off, src_in));
1426 if (dst_in-in_off > src_in)
1427 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1428 dst_in - in_off - src_in);
1429 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1430 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1431 src+1+src_nparam+src_in,
1432 isl_min(dst_out-out_off, src_out));
1433 if (dst_out-out_off > src_out)
1434 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1435 dst_out - out_off - src_out);
1436 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1437 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1438 src+1+src_nparam+src_in+src_out,
1439 isl_min(dst_map->extra-div_off, src_map->n_div));
1440 if (dst_map->n_div-div_off > src_map->n_div)
1441 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1442 div_off+src_map->n_div,
1443 dst_map->n_div - div_off - src_map->n_div);
1446 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1447 struct isl_basic_map *src_map, isl_int *src,
1448 unsigned in_off, unsigned out_off, unsigned div_off)
1450 isl_int_set(dst[0], src[0]);
1451 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1454 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1455 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1457 int i;
1458 unsigned div_off;
1460 if (!bmap1 || !bmap2)
1461 goto error;
1463 div_off = bmap1->n_div;
1465 for (i = 0; i < bmap2->n_eq; ++i) {
1466 int i1 = isl_basic_map_alloc_equality(bmap1);
1467 if (i1 < 0)
1468 goto error;
1469 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1470 i_pos, o_pos, div_off);
1473 for (i = 0; i < bmap2->n_ineq; ++i) {
1474 int i1 = isl_basic_map_alloc_inequality(bmap1);
1475 if (i1 < 0)
1476 goto error;
1477 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1478 i_pos, o_pos, div_off);
1481 for (i = 0; i < bmap2->n_div; ++i) {
1482 int i1 = isl_basic_map_alloc_div(bmap1);
1483 if (i1 < 0)
1484 goto error;
1485 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1486 i_pos, o_pos, div_off);
1489 isl_basic_map_free(bmap2);
1491 return bmap1;
1493 error:
1494 isl_basic_map_free(bmap1);
1495 isl_basic_map_free(bmap2);
1496 return NULL;
1499 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1500 struct isl_basic_set *bset2, unsigned pos)
1502 return (struct isl_basic_set *)
1503 add_constraints((struct isl_basic_map *)bset1,
1504 (struct isl_basic_map *)bset2, 0, pos);
1507 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1508 __isl_take isl_space *dim, unsigned extra,
1509 unsigned n_eq, unsigned n_ineq)
1511 struct isl_basic_map *ext;
1512 unsigned flags;
1513 int dims_ok;
1515 if (!dim)
1516 goto error;
1518 if (!base)
1519 goto error;
1521 dims_ok = isl_space_is_equal(base->dim, dim) &&
1522 base->extra >= base->n_div + extra;
1524 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1525 room_for_ineq(base, n_ineq)) {
1526 isl_space_free(dim);
1527 return base;
1530 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1531 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1532 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1533 extra += base->extra;
1534 n_eq += base->n_eq;
1535 n_ineq += base->n_ineq;
1537 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1538 dim = NULL;
1539 if (!ext)
1540 goto error;
1542 if (dims_ok)
1543 ext->sample = isl_vec_copy(base->sample);
1544 flags = base->flags;
1545 ext = add_constraints(ext, base, 0, 0);
1546 if (ext) {
1547 ext->flags = flags;
1548 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1551 return ext;
1553 error:
1554 isl_space_free(dim);
1555 isl_basic_map_free(base);
1556 return NULL;
1559 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1560 __isl_take isl_space *dim, unsigned extra,
1561 unsigned n_eq, unsigned n_ineq)
1563 return (struct isl_basic_set *)
1564 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1565 extra, n_eq, n_ineq);
1568 struct isl_basic_map *isl_basic_map_extend_constraints(
1569 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1571 if (!base)
1572 return NULL;
1573 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1574 0, n_eq, n_ineq);
1577 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1578 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1579 unsigned n_eq, unsigned n_ineq)
1581 struct isl_basic_map *bmap;
1582 isl_space *dim;
1584 if (!base)
1585 return NULL;
1586 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1587 if (!dim)
1588 goto error;
1590 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1591 return bmap;
1592 error:
1593 isl_basic_map_free(base);
1594 return NULL;
1597 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1598 unsigned nparam, unsigned dim, unsigned extra,
1599 unsigned n_eq, unsigned n_ineq)
1601 return (struct isl_basic_set *)
1602 isl_basic_map_extend((struct isl_basic_map *)base,
1603 nparam, 0, dim, extra, n_eq, n_ineq);
1606 struct isl_basic_set *isl_basic_set_extend_constraints(
1607 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1609 return (struct isl_basic_set *)
1610 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1611 n_eq, n_ineq);
1614 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1616 return (struct isl_basic_set *)
1617 isl_basic_map_cow((struct isl_basic_map *)bset);
1620 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1622 if (!bmap)
1623 return NULL;
1625 if (bmap->ref > 1) {
1626 bmap->ref--;
1627 bmap = isl_basic_map_dup(bmap);
1629 if (bmap)
1630 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1631 return bmap;
1634 struct isl_set *isl_set_cow(struct isl_set *set)
1636 if (!set)
1637 return NULL;
1639 if (set->ref == 1)
1640 return set;
1641 set->ref--;
1642 return isl_set_dup(set);
1645 struct isl_map *isl_map_cow(struct isl_map *map)
1647 if (!map)
1648 return NULL;
1650 if (map->ref == 1)
1651 return map;
1652 map->ref--;
1653 return isl_map_dup(map);
1656 static void swap_vars(struct isl_blk blk, isl_int *a,
1657 unsigned a_len, unsigned b_len)
1659 isl_seq_cpy(blk.data, a+a_len, b_len);
1660 isl_seq_cpy(blk.data+b_len, a, a_len);
1661 isl_seq_cpy(a, blk.data, b_len+a_len);
1664 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1665 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1667 int i;
1668 struct isl_blk blk;
1670 if (!bmap)
1671 goto error;
1673 isl_assert(bmap->ctx,
1674 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1676 if (n1 == 0 || n2 == 0)
1677 return bmap;
1679 bmap = isl_basic_map_cow(bmap);
1680 if (!bmap)
1681 return NULL;
1683 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1684 if (isl_blk_is_error(blk))
1685 goto error;
1687 for (i = 0; i < bmap->n_eq; ++i)
1688 swap_vars(blk,
1689 bmap->eq[i] + pos, n1, n2);
1691 for (i = 0; i < bmap->n_ineq; ++i)
1692 swap_vars(blk,
1693 bmap->ineq[i] + pos, n1, n2);
1695 for (i = 0; i < bmap->n_div; ++i)
1696 swap_vars(blk,
1697 bmap->div[i]+1 + pos, n1, n2);
1699 isl_blk_free(bmap->ctx, blk);
1701 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1702 bmap = isl_basic_map_gauss(bmap, NULL);
1703 return isl_basic_map_finalize(bmap);
1704 error:
1705 isl_basic_map_free(bmap);
1706 return NULL;
1709 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1710 __isl_take isl_basic_set *bset, unsigned n)
1712 unsigned dim;
1713 unsigned nparam;
1715 if (!bset)
1716 return NULL;
1718 nparam = isl_basic_set_n_param(bset);
1719 dim = isl_basic_set_n_dim(bset);
1720 isl_assert(bset->ctx, n <= dim, goto error);
1722 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1723 error:
1724 isl_basic_set_free(bset);
1725 return NULL;
1728 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1730 int i = 0;
1731 unsigned total;
1732 if (!bmap)
1733 goto error;
1734 total = isl_basic_map_total_dim(bmap);
1735 isl_basic_map_free_div(bmap, bmap->n_div);
1736 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1737 if (bmap->n_eq > 0)
1738 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1739 else {
1740 i = isl_basic_map_alloc_equality(bmap);
1741 if (i < 0)
1742 goto error;
1744 isl_int_set_si(bmap->eq[i][0], 1);
1745 isl_seq_clr(bmap->eq[i]+1, total);
1746 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1747 isl_vec_free(bmap->sample);
1748 bmap->sample = NULL;
1749 return isl_basic_map_finalize(bmap);
1750 error:
1751 isl_basic_map_free(bmap);
1752 return NULL;
1755 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1757 return (struct isl_basic_set *)
1758 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1761 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1762 * of "bmap").
1764 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1766 isl_int *t = bmap->div[a];
1767 bmap->div[a] = bmap->div[b];
1768 bmap->div[b] = t;
1771 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1772 * div definitions accordingly.
1774 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1776 int i;
1777 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1779 swap_div(bmap, a, b);
1781 for (i = 0; i < bmap->n_eq; ++i)
1782 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1784 for (i = 0; i < bmap->n_ineq; ++i)
1785 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1787 for (i = 0; i < bmap->n_div; ++i)
1788 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1789 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1792 /* Eliminate the specified n dimensions starting at first from the
1793 * constraints, without removing the dimensions from the space.
1794 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1796 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1797 enum isl_dim_type type, unsigned first, unsigned n)
1799 int i;
1801 if (!map)
1802 return NULL;
1803 if (n == 0)
1804 return map;
1806 if (first + n > isl_map_dim(map, type) || first + n < first)
1807 isl_die(map->ctx, isl_error_invalid,
1808 "index out of bounds", goto error);
1810 map = isl_map_cow(map);
1811 if (!map)
1812 return NULL;
1814 for (i = 0; i < map->n; ++i) {
1815 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1816 if (!map->p[i])
1817 goto error;
1819 return map;
1820 error:
1821 isl_map_free(map);
1822 return NULL;
1825 /* Eliminate the specified n dimensions starting at first from the
1826 * constraints, without removing the dimensions from the space.
1827 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1829 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1830 enum isl_dim_type type, unsigned first, unsigned n)
1832 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1835 /* Eliminate the specified n dimensions starting at first from the
1836 * constraints, without removing the dimensions from the space.
1837 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1839 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1840 unsigned first, unsigned n)
1842 return isl_set_eliminate(set, isl_dim_set, first, n);
1845 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1846 __isl_take isl_basic_map *bmap)
1848 if (!bmap)
1849 return NULL;
1850 bmap = isl_basic_map_eliminate_vars(bmap,
1851 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1852 if (!bmap)
1853 return NULL;
1854 bmap->n_div = 0;
1855 return isl_basic_map_finalize(bmap);
1858 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1859 __isl_take isl_basic_set *bset)
1861 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1862 (struct isl_basic_map *)bset);
1865 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1867 int i;
1869 if (!map)
1870 return NULL;
1871 if (map->n == 0)
1872 return map;
1874 map = isl_map_cow(map);
1875 if (!map)
1876 return NULL;
1878 for (i = 0; i < map->n; ++i) {
1879 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1880 if (!map->p[i])
1881 goto error;
1883 return map;
1884 error:
1885 isl_map_free(map);
1886 return NULL;
1889 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1891 return isl_map_remove_divs(set);
1894 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1895 enum isl_dim_type type, unsigned first, unsigned n)
1897 if (!bmap)
1898 return NULL;
1899 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1900 goto error);
1901 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1902 return bmap;
1903 bmap = isl_basic_map_eliminate_vars(bmap,
1904 isl_basic_map_offset(bmap, type) - 1 + first, n);
1905 if (!bmap)
1906 return bmap;
1907 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1908 return bmap;
1909 bmap = isl_basic_map_drop(bmap, type, first, n);
1910 return bmap;
1911 error:
1912 isl_basic_map_free(bmap);
1913 return NULL;
1916 /* Return true if the definition of the given div (recursively) involves
1917 * any of the given variables.
1919 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1920 unsigned first, unsigned n)
1922 int i;
1923 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1925 if (isl_int_is_zero(bmap->div[div][0]))
1926 return 0;
1927 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1928 return 1;
1930 for (i = bmap->n_div - 1; i >= 0; --i) {
1931 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1932 continue;
1933 if (div_involves_vars(bmap, i, first, n))
1934 return 1;
1937 return 0;
1940 /* Try and add a lower and/or upper bound on "div" to "bmap"
1941 * based on inequality "i".
1942 * "total" is the total number of variables (excluding the divs).
1943 * "v" is a temporary object that can be used during the calculations.
1944 * If "lb" is set, then a lower bound should be constructed.
1945 * If "ub" is set, then an upper bound should be constructed.
1947 * The calling function has already checked that the inequality does not
1948 * reference "div", but we still need to check that the inequality is
1949 * of the right form. We'll consider the case where we want to construct
1950 * a lower bound. The construction of upper bounds is similar.
1952 * Let "div" be of the form
1954 * q = floor((a + f(x))/d)
1956 * We essentially check if constraint "i" is of the form
1958 * b + f(x) >= 0
1960 * so that we can use it to derive a lower bound on "div".
1961 * However, we allow a slightly more general form
1963 * b + g(x) >= 0
1965 * with the condition that the coefficients of g(x) - f(x) are all
1966 * divisible by d.
1967 * Rewriting this constraint as
1969 * 0 >= -b - g(x)
1971 * adding a + f(x) to both sides and dividing by d, we obtain
1973 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1975 * Taking the floor on both sides, we obtain
1977 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1979 * or
1981 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1983 * In the case of an upper bound, we construct the constraint
1985 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1988 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1989 __isl_take isl_basic_map *bmap, int div, int i,
1990 unsigned total, isl_int v, int lb, int ub)
1992 int j;
1994 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1995 if (lb) {
1996 isl_int_sub(v, bmap->ineq[i][1 + j],
1997 bmap->div[div][1 + 1 + j]);
1998 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2000 if (ub) {
2001 isl_int_add(v, bmap->ineq[i][1 + j],
2002 bmap->div[div][1 + 1 + j]);
2003 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2006 if (!lb && !ub)
2007 return bmap;
2009 bmap = isl_basic_map_cow(bmap);
2010 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2011 if (lb) {
2012 int k = isl_basic_map_alloc_inequality(bmap);
2013 if (k < 0)
2014 goto error;
2015 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2016 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2017 bmap->div[div][1 + j]);
2018 isl_int_cdiv_q(bmap->ineq[k][j],
2019 bmap->ineq[k][j], bmap->div[div][0]);
2021 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2023 if (ub) {
2024 int k = isl_basic_map_alloc_inequality(bmap);
2025 if (k < 0)
2026 goto error;
2027 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2028 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2029 bmap->div[div][1 + j]);
2030 isl_int_fdiv_q(bmap->ineq[k][j],
2031 bmap->ineq[k][j], bmap->div[div][0]);
2033 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2036 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2037 return bmap;
2038 error:
2039 isl_basic_map_free(bmap);
2040 return NULL;
2043 /* This function is called right before "div" is eliminated from "bmap"
2044 * using Fourier-Motzkin.
2045 * Look through the constraints of "bmap" for constraints on the argument
2046 * of the integer division and use them to construct constraints on the
2047 * integer division itself. These constraints can then be combined
2048 * during the Fourier-Motzkin elimination.
2049 * Note that it is only useful to introduce lower bounds on "div"
2050 * if "bmap" already contains upper bounds on "div" as the newly
2051 * introduce lower bounds can then be combined with the pre-existing
2052 * upper bounds. Similarly for upper bounds.
2053 * We therefore first check if "bmap" contains any lower and/or upper bounds
2054 * on "div".
2056 * It is interesting to note that the introduction of these constraints
2057 * can indeed lead to more accurate results, even when compared to
2058 * deriving constraints on the argument of "div" from constraints on "div".
2059 * Consider, for example, the set
2061 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2063 * The second constraint can be rewritten as
2065 * 2 * [(-i-2j+3)/4] + k >= 0
2067 * from which we can derive
2069 * -i - 2j + 3 >= -2k
2071 * or
2073 * i + 2j <= 3 + 2k
2075 * Combined with the first constraint, we obtain
2077 * -3 <= 3 + 2k or k >= -3
2079 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2080 * the first constraint, we obtain
2082 * [(i + 2j)/4] >= [-3/4] = -1
2084 * Combining this constraint with the second constraint, we obtain
2086 * k >= -2
2088 static __isl_give isl_basic_map *insert_bounds_on_div(
2089 __isl_take isl_basic_map *bmap, int div)
2091 int i;
2092 int check_lb, check_ub;
2093 isl_int v;
2094 unsigned total;
2096 if (!bmap)
2097 return NULL;
2099 if (isl_int_is_zero(bmap->div[div][0]))
2100 return bmap;
2102 total = isl_space_dim(bmap->dim, isl_dim_all);
2104 check_lb = 0;
2105 check_ub = 0;
2106 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2107 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2108 if (s > 0)
2109 check_ub = 1;
2110 if (s < 0)
2111 check_lb = 1;
2114 if (!check_lb && !check_ub)
2115 return bmap;
2117 isl_int_init(v);
2119 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2120 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2121 continue;
2123 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2124 check_lb, check_ub);
2127 isl_int_clear(v);
2129 return bmap;
2132 /* Remove all divs (recursively) involving any of the given dimensions
2133 * in their definitions.
2135 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2136 __isl_take isl_basic_map *bmap,
2137 enum isl_dim_type type, unsigned first, unsigned n)
2139 int i;
2141 if (!bmap)
2142 return NULL;
2143 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2144 goto error);
2145 first += isl_basic_map_offset(bmap, type);
2147 for (i = bmap->n_div - 1; i >= 0; --i) {
2148 if (!div_involves_vars(bmap, i, first, n))
2149 continue;
2150 bmap = insert_bounds_on_div(bmap, i);
2151 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2152 if (!bmap)
2153 return NULL;
2154 i = bmap->n_div;
2157 return bmap;
2158 error:
2159 isl_basic_map_free(bmap);
2160 return NULL;
2163 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2164 __isl_take isl_basic_set *bset,
2165 enum isl_dim_type type, unsigned first, unsigned n)
2167 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2170 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2171 enum isl_dim_type type, unsigned first, unsigned n)
2173 int i;
2175 if (!map)
2176 return NULL;
2177 if (map->n == 0)
2178 return map;
2180 map = isl_map_cow(map);
2181 if (!map)
2182 return NULL;
2184 for (i = 0; i < map->n; ++i) {
2185 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2186 type, first, n);
2187 if (!map->p[i])
2188 goto error;
2190 return map;
2191 error:
2192 isl_map_free(map);
2193 return NULL;
2196 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2197 enum isl_dim_type type, unsigned first, unsigned n)
2199 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2200 type, first, n);
2203 /* Does the desciption of "bmap" depend on the specified dimensions?
2204 * We also check whether the dimensions appear in any of the div definitions.
2205 * In principle there is no need for this check. If the dimensions appear
2206 * in a div definition, they also appear in the defining constraints of that
2207 * div.
2209 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2210 enum isl_dim_type type, unsigned first, unsigned n)
2212 int i;
2214 if (!bmap)
2215 return -1;
2217 if (first + n > isl_basic_map_dim(bmap, type))
2218 isl_die(bmap->ctx, isl_error_invalid,
2219 "index out of bounds", return -1);
2221 first += isl_basic_map_offset(bmap, type);
2222 for (i = 0; i < bmap->n_eq; ++i)
2223 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2224 return 1;
2225 for (i = 0; i < bmap->n_ineq; ++i)
2226 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2227 return 1;
2228 for (i = 0; i < bmap->n_div; ++i) {
2229 if (isl_int_is_zero(bmap->div[i][0]))
2230 continue;
2231 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2232 return 1;
2235 return 0;
2238 int isl_map_involves_dims(__isl_keep isl_map *map,
2239 enum isl_dim_type type, unsigned first, unsigned n)
2241 int i;
2243 if (!map)
2244 return -1;
2246 if (first + n > isl_map_dim(map, type))
2247 isl_die(map->ctx, isl_error_invalid,
2248 "index out of bounds", return -1);
2250 for (i = 0; i < map->n; ++i) {
2251 int involves = isl_basic_map_involves_dims(map->p[i],
2252 type, first, n);
2253 if (involves < 0 || involves)
2254 return involves;
2257 return 0;
2260 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2261 enum isl_dim_type type, unsigned first, unsigned n)
2263 return isl_basic_map_involves_dims(bset, type, first, n);
2266 int isl_set_involves_dims(__isl_keep isl_set *set,
2267 enum isl_dim_type type, unsigned first, unsigned n)
2269 return isl_map_involves_dims(set, type, first, n);
2272 /* Return true if the definition of the given div is unknown or depends
2273 * on unknown divs.
2275 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2277 int i;
2278 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2280 if (isl_int_is_zero(bmap->div[div][0]))
2281 return 1;
2283 for (i = bmap->n_div - 1; i >= 0; --i) {
2284 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2285 continue;
2286 if (div_is_unknown(bmap, i))
2287 return 1;
2290 return 0;
2293 /* Remove all divs that are unknown or defined in terms of unknown divs.
2295 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2296 __isl_take isl_basic_map *bmap)
2298 int i;
2300 if (!bmap)
2301 return NULL;
2303 for (i = bmap->n_div - 1; i >= 0; --i) {
2304 if (!div_is_unknown(bmap, i))
2305 continue;
2306 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2307 if (!bmap)
2308 return NULL;
2309 i = bmap->n_div;
2312 return bmap;
2315 /* Remove all divs that are unknown or defined in terms of unknown divs.
2317 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2318 __isl_take isl_basic_set *bset)
2320 return isl_basic_map_remove_unknown_divs(bset);
2323 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2325 int i;
2327 if (!map)
2328 return NULL;
2329 if (map->n == 0)
2330 return map;
2332 map = isl_map_cow(map);
2333 if (!map)
2334 return NULL;
2336 for (i = 0; i < map->n; ++i) {
2337 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2338 if (!map->p[i])
2339 goto error;
2341 return map;
2342 error:
2343 isl_map_free(map);
2344 return NULL;
2347 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2349 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2352 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2353 __isl_take isl_basic_set *bset,
2354 enum isl_dim_type type, unsigned first, unsigned n)
2356 return (isl_basic_set *)
2357 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2360 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2361 enum isl_dim_type type, unsigned first, unsigned n)
2363 int i;
2365 if (n == 0)
2366 return map;
2368 map = isl_map_cow(map);
2369 if (!map)
2370 return NULL;
2371 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2373 for (i = 0; i < map->n; ++i) {
2374 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2375 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2376 if (!map->p[i])
2377 goto error;
2379 map = isl_map_drop(map, type, first, n);
2380 return map;
2381 error:
2382 isl_map_free(map);
2383 return NULL;
2386 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2387 enum isl_dim_type type, unsigned first, unsigned n)
2389 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2392 /* Project out n inputs starting at first using Fourier-Motzkin */
2393 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2394 unsigned first, unsigned n)
2396 return isl_map_remove_dims(map, isl_dim_in, first, n);
2399 static void dump_term(struct isl_basic_map *bmap,
2400 isl_int c, int pos, FILE *out)
2402 const char *name;
2403 unsigned in = isl_basic_map_n_in(bmap);
2404 unsigned dim = in + isl_basic_map_n_out(bmap);
2405 unsigned nparam = isl_basic_map_n_param(bmap);
2406 if (!pos)
2407 isl_int_print(out, c, 0);
2408 else {
2409 if (!isl_int_is_one(c))
2410 isl_int_print(out, c, 0);
2411 if (pos < 1 + nparam) {
2412 name = isl_space_get_dim_name(bmap->dim,
2413 isl_dim_param, pos - 1);
2414 if (name)
2415 fprintf(out, "%s", name);
2416 else
2417 fprintf(out, "p%d", pos - 1);
2418 } else if (pos < 1 + nparam + in)
2419 fprintf(out, "i%d", pos - 1 - nparam);
2420 else if (pos < 1 + nparam + dim)
2421 fprintf(out, "o%d", pos - 1 - nparam - in);
2422 else
2423 fprintf(out, "e%d", pos - 1 - nparam - dim);
2427 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2428 int sign, FILE *out)
2430 int i;
2431 int first;
2432 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2433 isl_int v;
2435 isl_int_init(v);
2436 for (i = 0, first = 1; i < len; ++i) {
2437 if (isl_int_sgn(c[i]) * sign <= 0)
2438 continue;
2439 if (!first)
2440 fprintf(out, " + ");
2441 first = 0;
2442 isl_int_abs(v, c[i]);
2443 dump_term(bmap, v, i, out);
2445 isl_int_clear(v);
2446 if (first)
2447 fprintf(out, "0");
2450 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2451 const char *op, FILE *out, int indent)
2453 int i;
2455 fprintf(out, "%*s", indent, "");
2457 dump_constraint_sign(bmap, c, 1, out);
2458 fprintf(out, " %s ", op);
2459 dump_constraint_sign(bmap, c, -1, out);
2461 fprintf(out, "\n");
2463 for (i = bmap->n_div; i < bmap->extra; ++i) {
2464 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2465 continue;
2466 fprintf(out, "%*s", indent, "");
2467 fprintf(out, "ERROR: unused div coefficient not zero\n");
2468 abort();
2472 static void dump_constraints(struct isl_basic_map *bmap,
2473 isl_int **c, unsigned n,
2474 const char *op, FILE *out, int indent)
2476 int i;
2478 for (i = 0; i < n; ++i)
2479 dump_constraint(bmap, c[i], op, out, indent);
2482 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2484 int j;
2485 int first = 1;
2486 unsigned total = isl_basic_map_total_dim(bmap);
2488 for (j = 0; j < 1 + total; ++j) {
2489 if (isl_int_is_zero(exp[j]))
2490 continue;
2491 if (!first && isl_int_is_pos(exp[j]))
2492 fprintf(out, "+");
2493 dump_term(bmap, exp[j], j, out);
2494 first = 0;
2498 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2500 int i;
2502 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2503 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2505 for (i = 0; i < bmap->n_div; ++i) {
2506 fprintf(out, "%*s", indent, "");
2507 fprintf(out, "e%d = [(", i);
2508 dump_affine(bmap, bmap->div[i]+1, out);
2509 fprintf(out, ")/");
2510 isl_int_print(out, bmap->div[i][0], 0);
2511 fprintf(out, "]\n");
2515 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2516 FILE *out, int indent)
2518 if (!bset) {
2519 fprintf(out, "null basic set\n");
2520 return;
2523 fprintf(out, "%*s", indent, "");
2524 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2525 bset->ref, bset->dim->nparam, bset->dim->n_out,
2526 bset->extra, bset->flags);
2527 dump((struct isl_basic_map *)bset, out, indent);
2530 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2531 FILE *out, int indent)
2533 if (!bmap) {
2534 fprintf(out, "null basic map\n");
2535 return;
2538 fprintf(out, "%*s", indent, "");
2539 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2540 "flags: %x, n_name: %d\n",
2541 bmap->ref,
2542 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2543 bmap->extra, bmap->flags, bmap->dim->n_id);
2544 dump(bmap, out, indent);
2547 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2549 unsigned total;
2550 if (!bmap)
2551 return -1;
2552 total = isl_basic_map_total_dim(bmap);
2553 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2554 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2555 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2556 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2557 return 0;
2560 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2561 unsigned flags)
2563 struct isl_set *set;
2565 if (!dim)
2566 return NULL;
2567 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2568 isl_assert(dim->ctx, n >= 0, goto error);
2569 set = isl_alloc(dim->ctx, struct isl_set,
2570 sizeof(struct isl_set) +
2571 (n - 1) * sizeof(struct isl_basic_set *));
2572 if (!set)
2573 goto error;
2575 set->ctx = dim->ctx;
2576 isl_ctx_ref(set->ctx);
2577 set->ref = 1;
2578 set->size = n;
2579 set->n = 0;
2580 set->dim = dim;
2581 set->flags = flags;
2582 return set;
2583 error:
2584 isl_space_free(dim);
2585 return NULL;
2588 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2589 unsigned nparam, unsigned dim, int n, unsigned flags)
2591 struct isl_set *set;
2592 isl_space *dims;
2594 dims = isl_space_alloc(ctx, nparam, 0, dim);
2595 if (!dims)
2596 return NULL;
2598 set = isl_set_alloc_space(dims, n, flags);
2599 return set;
2602 /* Make sure "map" has room for at least "n" more basic maps.
2604 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2606 int i;
2607 struct isl_map *grown = NULL;
2609 if (!map)
2610 return NULL;
2611 isl_assert(map->ctx, n >= 0, goto error);
2612 if (map->n + n <= map->size)
2613 return map;
2614 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2615 if (!grown)
2616 goto error;
2617 for (i = 0; i < map->n; ++i) {
2618 grown->p[i] = isl_basic_map_copy(map->p[i]);
2619 if (!grown->p[i])
2620 goto error;
2621 grown->n++;
2623 isl_map_free(map);
2624 return grown;
2625 error:
2626 isl_map_free(grown);
2627 isl_map_free(map);
2628 return NULL;
2631 /* Make sure "set" has room for at least "n" more basic sets.
2633 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2635 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2638 struct isl_set *isl_set_dup(struct isl_set *set)
2640 int i;
2641 struct isl_set *dup;
2643 if (!set)
2644 return NULL;
2646 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2647 if (!dup)
2648 return NULL;
2649 for (i = 0; i < set->n; ++i)
2650 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2651 return dup;
2654 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2656 return isl_map_from_basic_map(bset);
2659 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2661 struct isl_map *map;
2663 if (!bmap)
2664 return NULL;
2666 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2667 return isl_map_add_basic_map(map, bmap);
2670 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2671 __isl_take isl_basic_set *bset)
2673 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2674 (struct isl_basic_map *)bset);
2677 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2679 int i;
2681 if (!set)
2682 return NULL;
2684 if (--set->ref > 0)
2685 return NULL;
2687 isl_ctx_deref(set->ctx);
2688 for (i = 0; i < set->n; ++i)
2689 isl_basic_set_free(set->p[i]);
2690 isl_space_free(set->dim);
2691 free(set);
2693 return NULL;
2696 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2698 int i;
2700 if (!set) {
2701 fprintf(out, "null set\n");
2702 return;
2705 fprintf(out, "%*s", indent, "");
2706 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2707 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2708 set->flags);
2709 for (i = 0; i < set->n; ++i) {
2710 fprintf(out, "%*s", indent, "");
2711 fprintf(out, "basic set %d:\n", i);
2712 isl_basic_set_print_internal(set->p[i], out, indent+4);
2716 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2718 int i;
2720 if (!map) {
2721 fprintf(out, "null map\n");
2722 return;
2725 fprintf(out, "%*s", indent, "");
2726 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2727 "flags: %x, n_name: %d\n",
2728 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2729 map->dim->n_out, map->flags, map->dim->n_id);
2730 for (i = 0; i < map->n; ++i) {
2731 fprintf(out, "%*s", indent, "");
2732 fprintf(out, "basic map %d:\n", i);
2733 isl_basic_map_print_internal(map->p[i], out, indent+4);
2737 struct isl_basic_map *isl_basic_map_intersect_domain(
2738 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2740 struct isl_basic_map *bmap_domain;
2742 if (!bmap || !bset)
2743 goto error;
2745 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2746 bset->dim, isl_dim_param), goto error);
2748 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2749 isl_assert(bset->ctx,
2750 isl_basic_map_compatible_domain(bmap, bset), goto error);
2752 bmap = isl_basic_map_cow(bmap);
2753 if (!bmap)
2754 goto error;
2755 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2756 bset->n_div, bset->n_eq, bset->n_ineq);
2757 bmap_domain = isl_basic_map_from_domain(bset);
2758 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2760 bmap = isl_basic_map_simplify(bmap);
2761 return isl_basic_map_finalize(bmap);
2762 error:
2763 isl_basic_map_free(bmap);
2764 isl_basic_set_free(bset);
2765 return NULL;
2768 struct isl_basic_map *isl_basic_map_intersect_range(
2769 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2771 struct isl_basic_map *bmap_range;
2773 if (!bmap || !bset)
2774 goto error;
2776 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2777 bset->dim, isl_dim_param), goto error);
2779 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2780 isl_assert(bset->ctx,
2781 isl_basic_map_compatible_range(bmap, bset), goto error);
2783 if (isl_basic_set_is_universe(bset)) {
2784 isl_basic_set_free(bset);
2785 return bmap;
2788 bmap = isl_basic_map_cow(bmap);
2789 if (!bmap)
2790 goto error;
2791 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2792 bset->n_div, bset->n_eq, bset->n_ineq);
2793 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2794 bmap = add_constraints(bmap, bmap_range, 0, 0);
2796 bmap = isl_basic_map_simplify(bmap);
2797 return isl_basic_map_finalize(bmap);
2798 error:
2799 isl_basic_map_free(bmap);
2800 isl_basic_set_free(bset);
2801 return NULL;
2804 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2806 int i;
2807 unsigned total;
2808 isl_int s;
2810 if (!bmap || !vec)
2811 return -1;
2813 total = 1 + isl_basic_map_total_dim(bmap);
2814 if (total != vec->size)
2815 return -1;
2817 isl_int_init(s);
2819 for (i = 0; i < bmap->n_eq; ++i) {
2820 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2821 if (!isl_int_is_zero(s)) {
2822 isl_int_clear(s);
2823 return 0;
2827 for (i = 0; i < bmap->n_ineq; ++i) {
2828 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2829 if (isl_int_is_neg(s)) {
2830 isl_int_clear(s);
2831 return 0;
2835 isl_int_clear(s);
2837 return 1;
2840 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2842 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2845 struct isl_basic_map *isl_basic_map_intersect(
2846 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2848 struct isl_vec *sample = NULL;
2850 if (!bmap1 || !bmap2)
2851 goto error;
2853 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2854 bmap2->dim, isl_dim_param), goto error);
2855 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2856 isl_space_dim(bmap1->dim, isl_dim_param) &&
2857 isl_space_dim(bmap2->dim, isl_dim_all) !=
2858 isl_space_dim(bmap2->dim, isl_dim_param))
2859 return isl_basic_map_intersect(bmap2, bmap1);
2861 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2862 isl_space_dim(bmap2->dim, isl_dim_param))
2863 isl_assert(bmap1->ctx,
2864 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2866 if (bmap1->sample &&
2867 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2868 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2869 sample = isl_vec_copy(bmap1->sample);
2870 else if (bmap2->sample &&
2871 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2872 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2873 sample = isl_vec_copy(bmap2->sample);
2875 bmap1 = isl_basic_map_cow(bmap1);
2876 if (!bmap1)
2877 goto error;
2878 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2879 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2880 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2882 if (!bmap1)
2883 isl_vec_free(sample);
2884 else if (sample) {
2885 isl_vec_free(bmap1->sample);
2886 bmap1->sample = sample;
2889 bmap1 = isl_basic_map_simplify(bmap1);
2890 return isl_basic_map_finalize(bmap1);
2891 error:
2892 if (sample)
2893 isl_vec_free(sample);
2894 isl_basic_map_free(bmap1);
2895 isl_basic_map_free(bmap2);
2896 return NULL;
2899 struct isl_basic_set *isl_basic_set_intersect(
2900 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2902 return (struct isl_basic_set *)
2903 isl_basic_map_intersect(
2904 (struct isl_basic_map *)bset1,
2905 (struct isl_basic_map *)bset2);
2908 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2909 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2911 return isl_basic_set_intersect(bset1, bset2);
2914 /* Special case of isl_map_intersect, where both map1 and map2
2915 * are convex, without any divs and such that either map1 or map2
2916 * contains a single constraint. This constraint is then simply
2917 * added to the other map.
2919 static __isl_give isl_map *map_intersect_add_constraint(
2920 __isl_take isl_map *map1, __isl_take isl_map *map2)
2922 isl_assert(map1->ctx, map1->n == 1, goto error);
2923 isl_assert(map2->ctx, map1->n == 1, goto error);
2924 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2925 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2927 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2928 return isl_map_intersect(map2, map1);
2930 isl_assert(map2->ctx,
2931 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2933 map1 = isl_map_cow(map1);
2934 if (!map1)
2935 goto error;
2936 if (isl_map_plain_is_empty(map1)) {
2937 isl_map_free(map2);
2938 return map1;
2940 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2941 if (map2->p[0]->n_eq == 1)
2942 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2943 else
2944 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2945 map2->p[0]->ineq[0]);
2947 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2948 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2949 if (!map1->p[0])
2950 goto error;
2952 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2953 isl_basic_map_free(map1->p[0]);
2954 map1->n = 0;
2957 isl_map_free(map2);
2959 return map1;
2960 error:
2961 isl_map_free(map1);
2962 isl_map_free(map2);
2963 return NULL;
2966 /* map2 may be either a parameter domain or a map living in the same
2967 * space as map1.
2969 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2970 __isl_take isl_map *map2)
2972 unsigned flags = 0;
2973 isl_map *result;
2974 int i, j;
2976 if (!map1 || !map2)
2977 goto error;
2979 if ((isl_map_plain_is_empty(map1) ||
2980 isl_map_plain_is_universe(map2)) &&
2981 isl_space_is_equal(map1->dim, map2->dim)) {
2982 isl_map_free(map2);
2983 return map1;
2985 if ((isl_map_plain_is_empty(map2) ||
2986 isl_map_plain_is_universe(map1)) &&
2987 isl_space_is_equal(map1->dim, map2->dim)) {
2988 isl_map_free(map1);
2989 return map2;
2992 if (map1->n == 1 && map2->n == 1 &&
2993 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2994 isl_space_is_equal(map1->dim, map2->dim) &&
2995 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2996 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2997 return map_intersect_add_constraint(map1, map2);
2999 if (isl_space_dim(map2->dim, isl_dim_all) !=
3000 isl_space_dim(map2->dim, isl_dim_param))
3001 isl_assert(map1->ctx,
3002 isl_space_is_equal(map1->dim, map2->dim), goto error);
3004 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3005 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3006 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3008 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3009 map1->n * map2->n, flags);
3010 if (!result)
3011 goto error;
3012 for (i = 0; i < map1->n; ++i)
3013 for (j = 0; j < map2->n; ++j) {
3014 struct isl_basic_map *part;
3015 part = isl_basic_map_intersect(
3016 isl_basic_map_copy(map1->p[i]),
3017 isl_basic_map_copy(map2->p[j]));
3018 if (isl_basic_map_is_empty(part) < 0)
3019 part = isl_basic_map_free(part);
3020 result = isl_map_add_basic_map(result, part);
3021 if (!result)
3022 goto error;
3024 isl_map_free(map1);
3025 isl_map_free(map2);
3026 return result;
3027 error:
3028 isl_map_free(map1);
3029 isl_map_free(map2);
3030 return NULL;
3033 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3034 __isl_take isl_map *map2)
3036 if (!map1 || !map2)
3037 goto error;
3038 if (!isl_space_is_equal(map1->dim, map2->dim))
3039 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3040 "spaces don't match", goto error);
3041 return map_intersect_internal(map1, map2);
3042 error:
3043 isl_map_free(map1);
3044 isl_map_free(map2);
3045 return NULL;
3048 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3049 __isl_take isl_map *map2)
3051 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3054 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3056 return (struct isl_set *)
3057 isl_map_intersect((struct isl_map *)set1,
3058 (struct isl_map *)set2);
3061 /* map_intersect_internal accepts intersections
3062 * with parameter domains, so we can just call that function.
3064 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3065 __isl_take isl_set *params)
3067 return map_intersect_internal(map, params);
3070 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3071 __isl_take isl_map *map2)
3073 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3076 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3077 __isl_take isl_set *params)
3079 return isl_map_intersect_params(set, params);
3082 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3084 isl_space *dim;
3085 struct isl_basic_set *bset;
3086 unsigned in;
3088 if (!bmap)
3089 return NULL;
3090 bmap = isl_basic_map_cow(bmap);
3091 if (!bmap)
3092 return NULL;
3093 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3094 in = isl_basic_map_n_in(bmap);
3095 bset = isl_basic_set_from_basic_map(bmap);
3096 bset = isl_basic_set_swap_vars(bset, in);
3097 return isl_basic_map_from_basic_set(bset, dim);
3100 static __isl_give isl_basic_map *basic_map_space_reset(
3101 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3103 isl_space *space;
3105 if (!bmap)
3106 return NULL;
3107 if (!isl_space_is_named_or_nested(bmap->dim, type))
3108 return bmap;
3110 space = isl_basic_map_get_space(bmap);
3111 space = isl_space_reset(space, type);
3112 bmap = isl_basic_map_reset_space(bmap, space);
3113 return bmap;
3116 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3117 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3118 unsigned pos, unsigned n)
3120 isl_space *res_dim;
3121 struct isl_basic_map *res;
3122 struct isl_dim_map *dim_map;
3123 unsigned total, off;
3124 enum isl_dim_type t;
3126 if (n == 0)
3127 return basic_map_space_reset(bmap, type);
3129 if (!bmap)
3130 return NULL;
3132 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3134 total = isl_basic_map_total_dim(bmap) + n;
3135 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3136 off = 0;
3137 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3138 if (t != type) {
3139 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3140 } else {
3141 unsigned size = isl_basic_map_dim(bmap, t);
3142 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3143 0, pos, off);
3144 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3145 pos, size - pos, off + pos + n);
3147 off += isl_space_dim(res_dim, t);
3149 isl_dim_map_div(dim_map, bmap, off);
3151 res = isl_basic_map_alloc_space(res_dim,
3152 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3153 if (isl_basic_map_is_rational(bmap))
3154 res = isl_basic_map_set_rational(res);
3155 if (isl_basic_map_plain_is_empty(bmap)) {
3156 isl_basic_map_free(bmap);
3157 free(dim_map);
3158 return isl_basic_map_set_to_empty(res);
3160 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3161 return isl_basic_map_finalize(res);
3164 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3165 __isl_take isl_basic_set *bset,
3166 enum isl_dim_type type, unsigned pos, unsigned n)
3168 return isl_basic_map_insert_dims(bset, type, pos, n);
3171 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3172 enum isl_dim_type type, unsigned n)
3174 if (!bmap)
3175 return NULL;
3176 return isl_basic_map_insert_dims(bmap, type,
3177 isl_basic_map_dim(bmap, type), n);
3180 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3181 enum isl_dim_type type, unsigned n)
3183 if (!bset)
3184 return NULL;
3185 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3186 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3187 error:
3188 isl_basic_set_free(bset);
3189 return NULL;
3192 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3193 enum isl_dim_type type)
3195 isl_space *space;
3197 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3198 return map;
3200 space = isl_map_get_space(map);
3201 space = isl_space_reset(space, type);
3202 map = isl_map_reset_space(map, space);
3203 return map;
3206 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3207 enum isl_dim_type type, unsigned pos, unsigned n)
3209 int i;
3211 if (n == 0)
3212 return map_space_reset(map, type);
3214 map = isl_map_cow(map);
3215 if (!map)
3216 return NULL;
3218 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3219 if (!map->dim)
3220 goto error;
3222 for (i = 0; i < map->n; ++i) {
3223 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3224 if (!map->p[i])
3225 goto error;
3228 return map;
3229 error:
3230 isl_map_free(map);
3231 return NULL;
3234 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3235 enum isl_dim_type type, unsigned pos, unsigned n)
3237 return isl_map_insert_dims(set, type, pos, n);
3240 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3241 enum isl_dim_type type, unsigned n)
3243 if (!map)
3244 return NULL;
3245 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3248 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3249 enum isl_dim_type type, unsigned n)
3251 if (!set)
3252 return NULL;
3253 isl_assert(set->ctx, type != isl_dim_in, goto error);
3254 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3255 error:
3256 isl_set_free(set);
3257 return NULL;
3260 __isl_give isl_basic_map *isl_basic_map_move_dims(
3261 __isl_take isl_basic_map *bmap,
3262 enum isl_dim_type dst_type, unsigned dst_pos,
3263 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3265 struct isl_dim_map *dim_map;
3266 struct isl_basic_map *res;
3267 enum isl_dim_type t;
3268 unsigned total, off;
3270 if (!bmap)
3271 return NULL;
3272 if (n == 0)
3273 return bmap;
3275 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3276 goto error);
3278 if (dst_type == src_type && dst_pos == src_pos)
3279 return bmap;
3281 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3283 if (pos(bmap->dim, dst_type) + dst_pos ==
3284 pos(bmap->dim, src_type) + src_pos +
3285 ((src_type < dst_type) ? n : 0)) {
3286 bmap = isl_basic_map_cow(bmap);
3287 if (!bmap)
3288 return NULL;
3290 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3291 src_type, src_pos, n);
3292 if (!bmap->dim)
3293 goto error;
3295 bmap = isl_basic_map_finalize(bmap);
3297 return bmap;
3300 total = isl_basic_map_total_dim(bmap);
3301 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3303 off = 0;
3304 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3305 unsigned size = isl_space_dim(bmap->dim, t);
3306 if (t == dst_type) {
3307 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3308 0, dst_pos, off);
3309 off += dst_pos;
3310 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3311 src_pos, n, off);
3312 off += n;
3313 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3314 dst_pos, size - dst_pos, off);
3315 off += size - dst_pos;
3316 } else if (t == src_type) {
3317 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3318 0, src_pos, off);
3319 off += src_pos;
3320 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3321 src_pos + n, size - src_pos - n, off);
3322 off += size - src_pos - n;
3323 } else {
3324 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3325 off += size;
3328 isl_dim_map_div(dim_map, bmap, off);
3330 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3331 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3332 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3333 if (!bmap)
3334 goto error;
3336 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3337 src_type, src_pos, n);
3338 if (!bmap->dim)
3339 goto error;
3341 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3342 bmap = isl_basic_map_gauss(bmap, NULL);
3343 bmap = isl_basic_map_finalize(bmap);
3345 return bmap;
3346 error:
3347 isl_basic_map_free(bmap);
3348 return NULL;
3351 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3352 enum isl_dim_type dst_type, unsigned dst_pos,
3353 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3355 return (isl_basic_set *)isl_basic_map_move_dims(
3356 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3359 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3360 enum isl_dim_type dst_type, unsigned dst_pos,
3361 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3363 if (!set)
3364 return NULL;
3365 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3366 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3367 src_type, src_pos, n);
3368 error:
3369 isl_set_free(set);
3370 return NULL;
3373 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3374 enum isl_dim_type dst_type, unsigned dst_pos,
3375 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3377 int i;
3379 if (!map)
3380 return NULL;
3381 if (n == 0)
3382 return map;
3384 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3385 goto error);
3387 if (dst_type == src_type && dst_pos == src_pos)
3388 return map;
3390 isl_assert(map->ctx, dst_type != src_type, goto error);
3392 map = isl_map_cow(map);
3393 if (!map)
3394 return NULL;
3396 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3397 if (!map->dim)
3398 goto error;
3400 for (i = 0; i < map->n; ++i) {
3401 map->p[i] = isl_basic_map_move_dims(map->p[i],
3402 dst_type, dst_pos,
3403 src_type, src_pos, n);
3404 if (!map->p[i])
3405 goto error;
3408 return map;
3409 error:
3410 isl_map_free(map);
3411 return NULL;
3414 /* Move the specified dimensions to the last columns right before
3415 * the divs. Don't change the dimension specification of bmap.
3416 * That's the responsibility of the caller.
3418 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3419 enum isl_dim_type type, unsigned first, unsigned n)
3421 struct isl_dim_map *dim_map;
3422 struct isl_basic_map *res;
3423 enum isl_dim_type t;
3424 unsigned total, off;
3426 if (!bmap)
3427 return NULL;
3428 if (pos(bmap->dim, type) + first + n ==
3429 1 + isl_space_dim(bmap->dim, isl_dim_all))
3430 return bmap;
3432 total = isl_basic_map_total_dim(bmap);
3433 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3435 off = 0;
3436 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3437 unsigned size = isl_space_dim(bmap->dim, t);
3438 if (t == type) {
3439 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3440 0, first, off);
3441 off += first;
3442 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3443 first, n, total - bmap->n_div - n);
3444 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3445 first + n, size - (first + n), off);
3446 off += size - (first + n);
3447 } else {
3448 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3449 off += size;
3452 isl_dim_map_div(dim_map, bmap, off + n);
3454 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3455 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3456 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3457 return res;
3460 /* Insert "n" rows in the divs of "bmap".
3462 * The number of columns is not changed, which means that the last
3463 * dimensions of "bmap" are being reintepreted as the new divs.
3464 * The space of "bmap" is not adjusted, however, which means
3465 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3466 * from the space of "bmap" is the responsibility of the caller.
3468 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3469 int n)
3471 int i;
3472 size_t row_size;
3473 isl_int **new_div;
3474 isl_int *old;
3476 bmap = isl_basic_map_cow(bmap);
3477 if (!bmap)
3478 return NULL;
3480 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3481 old = bmap->block2.data;
3482 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3483 (bmap->extra + n) * (1 + row_size));
3484 if (!bmap->block2.data)
3485 return isl_basic_map_free(bmap);
3486 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3487 if (!new_div)
3488 return isl_basic_map_free(bmap);
3489 for (i = 0; i < n; ++i) {
3490 new_div[i] = bmap->block2.data +
3491 (bmap->extra + i) * (1 + row_size);
3492 isl_seq_clr(new_div[i], 1 + row_size);
3494 for (i = 0; i < bmap->extra; ++i)
3495 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3496 free(bmap->div);
3497 bmap->div = new_div;
3498 bmap->n_div += n;
3499 bmap->extra += n;
3501 return bmap;
3504 /* Turn the n dimensions of type type, starting at first
3505 * into existentially quantified variables.
3507 __isl_give isl_basic_map *isl_basic_map_project_out(
3508 __isl_take isl_basic_map *bmap,
3509 enum isl_dim_type type, unsigned first, unsigned n)
3511 if (n == 0)
3512 return basic_map_space_reset(bmap, type);
3514 if (!bmap)
3515 return NULL;
3517 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3518 return isl_basic_map_remove_dims(bmap, type, first, n);
3520 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3521 goto error);
3523 bmap = move_last(bmap, type, first, n);
3524 bmap = isl_basic_map_cow(bmap);
3525 bmap = insert_div_rows(bmap, n);
3526 if (!bmap)
3527 return NULL;
3529 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3530 if (!bmap->dim)
3531 goto error;
3532 bmap = isl_basic_map_simplify(bmap);
3533 bmap = isl_basic_map_drop_redundant_divs(bmap);
3534 return isl_basic_map_finalize(bmap);
3535 error:
3536 isl_basic_map_free(bmap);
3537 return NULL;
3540 /* Turn the n dimensions of type type, starting at first
3541 * into existentially quantified variables.
3543 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3544 enum isl_dim_type type, unsigned first, unsigned n)
3546 return (isl_basic_set *)isl_basic_map_project_out(
3547 (isl_basic_map *)bset, type, first, n);
3550 /* Turn the n dimensions of type type, starting at first
3551 * into existentially quantified variables.
3553 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3554 enum isl_dim_type type, unsigned first, unsigned n)
3556 int i;
3558 if (!map)
3559 return NULL;
3561 if (n == 0)
3562 return map_space_reset(map, type);
3564 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3566 map = isl_map_cow(map);
3567 if (!map)
3568 return NULL;
3570 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3571 if (!map->dim)
3572 goto error;
3574 for (i = 0; i < map->n; ++i) {
3575 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3576 if (!map->p[i])
3577 goto error;
3580 return map;
3581 error:
3582 isl_map_free(map);
3583 return NULL;
3586 /* Turn the n dimensions of type type, starting at first
3587 * into existentially quantified variables.
3589 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3590 enum isl_dim_type type, unsigned first, unsigned n)
3592 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3595 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3597 int i, j;
3599 for (i = 0; i < n; ++i) {
3600 j = isl_basic_map_alloc_div(bmap);
3601 if (j < 0)
3602 goto error;
3603 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3605 return bmap;
3606 error:
3607 isl_basic_map_free(bmap);
3608 return NULL;
3611 struct isl_basic_map *isl_basic_map_apply_range(
3612 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3614 isl_space *dim_result = NULL;
3615 struct isl_basic_map *bmap;
3616 unsigned n_in, n_out, n, nparam, total, pos;
3617 struct isl_dim_map *dim_map1, *dim_map2;
3619 if (!bmap1 || !bmap2)
3620 goto error;
3621 if (!isl_space_match(bmap1->dim, isl_dim_param,
3622 bmap2->dim, isl_dim_param))
3623 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3624 "parameters don't match", goto error);
3625 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3626 bmap2->dim, isl_dim_in))
3627 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3628 "spaces don't match", goto error);
3630 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3631 isl_space_copy(bmap2->dim));
3633 n_in = isl_basic_map_n_in(bmap1);
3634 n_out = isl_basic_map_n_out(bmap2);
3635 n = isl_basic_map_n_out(bmap1);
3636 nparam = isl_basic_map_n_param(bmap1);
3638 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3639 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3640 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3641 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3642 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3643 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3644 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3645 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3646 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3647 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3648 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3650 bmap = isl_basic_map_alloc_space(dim_result,
3651 bmap1->n_div + bmap2->n_div + n,
3652 bmap1->n_eq + bmap2->n_eq,
3653 bmap1->n_ineq + bmap2->n_ineq);
3654 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3655 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3656 bmap = add_divs(bmap, n);
3657 bmap = isl_basic_map_simplify(bmap);
3658 bmap = isl_basic_map_drop_redundant_divs(bmap);
3659 return isl_basic_map_finalize(bmap);
3660 error:
3661 isl_basic_map_free(bmap1);
3662 isl_basic_map_free(bmap2);
3663 return NULL;
3666 struct isl_basic_set *isl_basic_set_apply(
3667 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3669 if (!bset || !bmap)
3670 goto error;
3672 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3673 goto error);
3675 return (struct isl_basic_set *)
3676 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3677 error:
3678 isl_basic_set_free(bset);
3679 isl_basic_map_free(bmap);
3680 return NULL;
3683 struct isl_basic_map *isl_basic_map_apply_domain(
3684 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3686 if (!bmap1 || !bmap2)
3687 goto error;
3689 isl_assert(bmap1->ctx,
3690 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3691 isl_assert(bmap1->ctx,
3692 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3693 goto error);
3695 bmap1 = isl_basic_map_reverse(bmap1);
3696 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3697 return isl_basic_map_reverse(bmap1);
3698 error:
3699 isl_basic_map_free(bmap1);
3700 isl_basic_map_free(bmap2);
3701 return NULL;
3704 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3705 * A \cap B -> f(A) + f(B)
3707 struct isl_basic_map *isl_basic_map_sum(
3708 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3710 unsigned n_in, n_out, nparam, total, pos;
3711 struct isl_basic_map *bmap = NULL;
3712 struct isl_dim_map *dim_map1, *dim_map2;
3713 int i;
3715 if (!bmap1 || !bmap2)
3716 goto error;
3718 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3719 goto error);
3721 nparam = isl_basic_map_n_param(bmap1);
3722 n_in = isl_basic_map_n_in(bmap1);
3723 n_out = isl_basic_map_n_out(bmap1);
3725 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3726 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3727 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3728 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3729 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3730 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3731 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3732 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3733 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3734 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3735 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3737 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3738 bmap1->n_div + bmap2->n_div + 2 * n_out,
3739 bmap1->n_eq + bmap2->n_eq + n_out,
3740 bmap1->n_ineq + bmap2->n_ineq);
3741 for (i = 0; i < n_out; ++i) {
3742 int j = isl_basic_map_alloc_equality(bmap);
3743 if (j < 0)
3744 goto error;
3745 isl_seq_clr(bmap->eq[j], 1+total);
3746 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3747 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3748 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3750 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3751 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3752 bmap = add_divs(bmap, 2 * n_out);
3754 bmap = isl_basic_map_simplify(bmap);
3755 return isl_basic_map_finalize(bmap);
3756 error:
3757 isl_basic_map_free(bmap);
3758 isl_basic_map_free(bmap1);
3759 isl_basic_map_free(bmap2);
3760 return NULL;
3763 /* Given two maps A -> f(A) and B -> g(B), construct a map
3764 * A \cap B -> f(A) + f(B)
3766 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3768 struct isl_map *result;
3769 int i, j;
3771 if (!map1 || !map2)
3772 goto error;
3774 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3776 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3777 map1->n * map2->n, 0);
3778 if (!result)
3779 goto error;
3780 for (i = 0; i < map1->n; ++i)
3781 for (j = 0; j < map2->n; ++j) {
3782 struct isl_basic_map *part;
3783 part = isl_basic_map_sum(
3784 isl_basic_map_copy(map1->p[i]),
3785 isl_basic_map_copy(map2->p[j]));
3786 if (isl_basic_map_is_empty(part))
3787 isl_basic_map_free(part);
3788 else
3789 result = isl_map_add_basic_map(result, part);
3790 if (!result)
3791 goto error;
3793 isl_map_free(map1);
3794 isl_map_free(map2);
3795 return result;
3796 error:
3797 isl_map_free(map1);
3798 isl_map_free(map2);
3799 return NULL;
3802 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3803 __isl_take isl_set *set2)
3805 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3808 /* Given a basic map A -> f(A), construct A -> -f(A).
3810 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3812 int i, j;
3813 unsigned off, n;
3815 bmap = isl_basic_map_cow(bmap);
3816 if (!bmap)
3817 return NULL;
3819 n = isl_basic_map_dim(bmap, isl_dim_out);
3820 off = isl_basic_map_offset(bmap, isl_dim_out);
3821 for (i = 0; i < bmap->n_eq; ++i)
3822 for (j = 0; j < n; ++j)
3823 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3824 for (i = 0; i < bmap->n_ineq; ++i)
3825 for (j = 0; j < n; ++j)
3826 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3827 for (i = 0; i < bmap->n_div; ++i)
3828 for (j = 0; j < n; ++j)
3829 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3830 bmap = isl_basic_map_gauss(bmap, NULL);
3831 return isl_basic_map_finalize(bmap);
3834 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3836 return isl_basic_map_neg(bset);
3839 /* Given a map A -> f(A), construct A -> -f(A).
3841 struct isl_map *isl_map_neg(struct isl_map *map)
3843 int i;
3845 map = isl_map_cow(map);
3846 if (!map)
3847 return NULL;
3849 for (i = 0; i < map->n; ++i) {
3850 map->p[i] = isl_basic_map_neg(map->p[i]);
3851 if (!map->p[i])
3852 goto error;
3855 return map;
3856 error:
3857 isl_map_free(map);
3858 return NULL;
3861 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3863 return (isl_set *)isl_map_neg((isl_map *)set);
3866 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3867 * A -> floor(f(A)/d).
3869 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3870 isl_int d)
3872 unsigned n_in, n_out, nparam, total, pos;
3873 struct isl_basic_map *result = NULL;
3874 struct isl_dim_map *dim_map;
3875 int i;
3877 if (!bmap)
3878 return NULL;
3880 nparam = isl_basic_map_n_param(bmap);
3881 n_in = isl_basic_map_n_in(bmap);
3882 n_out = isl_basic_map_n_out(bmap);
3884 total = nparam + n_in + n_out + bmap->n_div + n_out;
3885 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3886 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3887 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3888 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3889 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3891 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3892 bmap->n_div + n_out,
3893 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3894 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3895 result = add_divs(result, n_out);
3896 for (i = 0; i < n_out; ++i) {
3897 int j;
3898 j = isl_basic_map_alloc_inequality(result);
3899 if (j < 0)
3900 goto error;
3901 isl_seq_clr(result->ineq[j], 1+total);
3902 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3903 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3904 j = isl_basic_map_alloc_inequality(result);
3905 if (j < 0)
3906 goto error;
3907 isl_seq_clr(result->ineq[j], 1+total);
3908 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3909 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3910 isl_int_sub_ui(result->ineq[j][0], d, 1);
3913 result = isl_basic_map_simplify(result);
3914 return isl_basic_map_finalize(result);
3915 error:
3916 isl_basic_map_free(result);
3917 return NULL;
3920 /* Given a map A -> f(A) and an integer d, construct a map
3921 * A -> floor(f(A)/d).
3923 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3925 int i;
3927 map = isl_map_cow(map);
3928 if (!map)
3929 return NULL;
3931 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3932 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3933 for (i = 0; i < map->n; ++i) {
3934 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3935 if (!map->p[i])
3936 goto error;
3939 return map;
3940 error:
3941 isl_map_free(map);
3942 return NULL;
3945 /* Given a map A -> f(A) and an integer d, construct a map
3946 * A -> floor(f(A)/d).
3948 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3949 __isl_take isl_val *d)
3951 if (!map || !d)
3952 goto error;
3953 if (!isl_val_is_int(d))
3954 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3955 "expecting integer denominator", goto error);
3956 map = isl_map_floordiv(map, d->n);
3957 isl_val_free(d);
3958 return map;
3959 error:
3960 isl_map_free(map);
3961 isl_val_free(d);
3962 return NULL;
3965 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3967 int i;
3968 unsigned nparam;
3969 unsigned n_in;
3971 i = isl_basic_map_alloc_equality(bmap);
3972 if (i < 0)
3973 goto error;
3974 nparam = isl_basic_map_n_param(bmap);
3975 n_in = isl_basic_map_n_in(bmap);
3976 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3977 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3978 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3979 return isl_basic_map_finalize(bmap);
3980 error:
3981 isl_basic_map_free(bmap);
3982 return NULL;
3985 /* Add a constraints to "bmap" expressing i_pos < o_pos
3987 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3989 int i;
3990 unsigned nparam;
3991 unsigned n_in;
3993 i = isl_basic_map_alloc_inequality(bmap);
3994 if (i < 0)
3995 goto error;
3996 nparam = isl_basic_map_n_param(bmap);
3997 n_in = isl_basic_map_n_in(bmap);
3998 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3999 isl_int_set_si(bmap->ineq[i][0], -1);
4000 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4001 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4002 return isl_basic_map_finalize(bmap);
4003 error:
4004 isl_basic_map_free(bmap);
4005 return NULL;
4008 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4010 static __isl_give isl_basic_map *var_less_or_equal(
4011 __isl_take isl_basic_map *bmap, unsigned pos)
4013 int i;
4014 unsigned nparam;
4015 unsigned n_in;
4017 i = isl_basic_map_alloc_inequality(bmap);
4018 if (i < 0)
4019 goto error;
4020 nparam = isl_basic_map_n_param(bmap);
4021 n_in = isl_basic_map_n_in(bmap);
4022 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4023 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4024 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4025 return isl_basic_map_finalize(bmap);
4026 error:
4027 isl_basic_map_free(bmap);
4028 return NULL;
4031 /* Add a constraints to "bmap" expressing i_pos > o_pos
4033 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4035 int i;
4036 unsigned nparam;
4037 unsigned n_in;
4039 i = isl_basic_map_alloc_inequality(bmap);
4040 if (i < 0)
4041 goto error;
4042 nparam = isl_basic_map_n_param(bmap);
4043 n_in = isl_basic_map_n_in(bmap);
4044 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4045 isl_int_set_si(bmap->ineq[i][0], -1);
4046 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4047 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4048 return isl_basic_map_finalize(bmap);
4049 error:
4050 isl_basic_map_free(bmap);
4051 return NULL;
4054 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4056 static __isl_give isl_basic_map *var_more_or_equal(
4057 __isl_take isl_basic_map *bmap, unsigned pos)
4059 int i;
4060 unsigned nparam;
4061 unsigned n_in;
4063 i = isl_basic_map_alloc_inequality(bmap);
4064 if (i < 0)
4065 goto error;
4066 nparam = isl_basic_map_n_param(bmap);
4067 n_in = isl_basic_map_n_in(bmap);
4068 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4069 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4070 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4071 return isl_basic_map_finalize(bmap);
4072 error:
4073 isl_basic_map_free(bmap);
4074 return NULL;
4077 __isl_give isl_basic_map *isl_basic_map_equal(
4078 __isl_take isl_space *dim, unsigned n_equal)
4080 int i;
4081 struct isl_basic_map *bmap;
4082 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4083 if (!bmap)
4084 return NULL;
4085 for (i = 0; i < n_equal && bmap; ++i)
4086 bmap = var_equal(bmap, i);
4087 return isl_basic_map_finalize(bmap);
4090 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4092 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4093 unsigned pos)
4095 int i;
4096 struct isl_basic_map *bmap;
4097 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4098 if (!bmap)
4099 return NULL;
4100 for (i = 0; i < pos && bmap; ++i)
4101 bmap = var_equal(bmap, i);
4102 if (bmap)
4103 bmap = var_less(bmap, pos);
4104 return isl_basic_map_finalize(bmap);
4107 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4109 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4110 __isl_take isl_space *dim, unsigned pos)
4112 int i;
4113 isl_basic_map *bmap;
4115 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4116 for (i = 0; i < pos; ++i)
4117 bmap = var_equal(bmap, i);
4118 bmap = var_less_or_equal(bmap, pos);
4119 return isl_basic_map_finalize(bmap);
4122 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4124 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4125 unsigned pos)
4127 int i;
4128 struct isl_basic_map *bmap;
4129 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4130 if (!bmap)
4131 return NULL;
4132 for (i = 0; i < pos && bmap; ++i)
4133 bmap = var_equal(bmap, i);
4134 if (bmap)
4135 bmap = var_more(bmap, pos);
4136 return isl_basic_map_finalize(bmap);
4139 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4141 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4142 __isl_take isl_space *dim, unsigned pos)
4144 int i;
4145 isl_basic_map *bmap;
4147 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4148 for (i = 0; i < pos; ++i)
4149 bmap = var_equal(bmap, i);
4150 bmap = var_more_or_equal(bmap, pos);
4151 return isl_basic_map_finalize(bmap);
4154 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4155 unsigned n, int equal)
4157 struct isl_map *map;
4158 int i;
4160 if (n == 0 && equal)
4161 return isl_map_universe(dims);
4163 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4165 for (i = 0; i + 1 < n; ++i)
4166 map = isl_map_add_basic_map(map,
4167 isl_basic_map_less_at(isl_space_copy(dims), i));
4168 if (n > 0) {
4169 if (equal)
4170 map = isl_map_add_basic_map(map,
4171 isl_basic_map_less_or_equal_at(dims, n - 1));
4172 else
4173 map = isl_map_add_basic_map(map,
4174 isl_basic_map_less_at(dims, n - 1));
4175 } else
4176 isl_space_free(dims);
4178 return map;
4181 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4183 if (!dims)
4184 return NULL;
4185 return map_lex_lte_first(dims, dims->n_out, equal);
4188 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4190 return map_lex_lte_first(dim, n, 0);
4193 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4195 return map_lex_lte_first(dim, n, 1);
4198 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4200 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4203 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4205 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4208 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4209 unsigned n, int equal)
4211 struct isl_map *map;
4212 int i;
4214 if (n == 0 && equal)
4215 return isl_map_universe(dims);
4217 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4219 for (i = 0; i + 1 < n; ++i)
4220 map = isl_map_add_basic_map(map,
4221 isl_basic_map_more_at(isl_space_copy(dims), i));
4222 if (n > 0) {
4223 if (equal)
4224 map = isl_map_add_basic_map(map,
4225 isl_basic_map_more_or_equal_at(dims, n - 1));
4226 else
4227 map = isl_map_add_basic_map(map,
4228 isl_basic_map_more_at(dims, n - 1));
4229 } else
4230 isl_space_free(dims);
4232 return map;
4235 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4237 if (!dims)
4238 return NULL;
4239 return map_lex_gte_first(dims, dims->n_out, equal);
4242 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4244 return map_lex_gte_first(dim, n, 0);
4247 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4249 return map_lex_gte_first(dim, n, 1);
4252 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4254 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4257 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4259 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4262 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4263 __isl_take isl_set *set2)
4265 isl_map *map;
4266 map = isl_map_lex_le(isl_set_get_space(set1));
4267 map = isl_map_intersect_domain(map, set1);
4268 map = isl_map_intersect_range(map, set2);
4269 return map;
4272 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4273 __isl_take isl_set *set2)
4275 isl_map *map;
4276 map = isl_map_lex_lt(isl_set_get_space(set1));
4277 map = isl_map_intersect_domain(map, set1);
4278 map = isl_map_intersect_range(map, set2);
4279 return map;
4282 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4283 __isl_take isl_set *set2)
4285 isl_map *map;
4286 map = isl_map_lex_ge(isl_set_get_space(set1));
4287 map = isl_map_intersect_domain(map, set1);
4288 map = isl_map_intersect_range(map, set2);
4289 return map;
4292 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4293 __isl_take isl_set *set2)
4295 isl_map *map;
4296 map = isl_map_lex_gt(isl_set_get_space(set1));
4297 map = isl_map_intersect_domain(map, set1);
4298 map = isl_map_intersect_range(map, set2);
4299 return map;
4302 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4303 __isl_take isl_map *map2)
4305 isl_map *map;
4306 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4307 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4308 map = isl_map_apply_range(map, isl_map_reverse(map2));
4309 return map;
4312 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4313 __isl_take isl_map *map2)
4315 isl_map *map;
4316 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4317 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4318 map = isl_map_apply_range(map, isl_map_reverse(map2));
4319 return map;
4322 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4323 __isl_take isl_map *map2)
4325 isl_map *map;
4326 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4327 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4328 map = isl_map_apply_range(map, isl_map_reverse(map2));
4329 return map;
4332 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4333 __isl_take isl_map *map2)
4335 isl_map *map;
4336 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4337 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4338 map = isl_map_apply_range(map, isl_map_reverse(map2));
4339 return map;
4342 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4343 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4345 struct isl_basic_map *bmap;
4347 bset = isl_basic_set_cow(bset);
4348 if (!bset || !dim)
4349 goto error;
4351 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4352 isl_space_free(bset->dim);
4353 bmap = (struct isl_basic_map *) bset;
4354 bmap->dim = dim;
4355 return isl_basic_map_finalize(bmap);
4356 error:
4357 isl_basic_set_free(bset);
4358 isl_space_free(dim);
4359 return NULL;
4362 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4364 if (!bmap)
4365 goto error;
4366 if (bmap->dim->n_in == 0)
4367 return (struct isl_basic_set *)bmap;
4368 bmap = isl_basic_map_cow(bmap);
4369 if (!bmap)
4370 goto error;
4371 bmap->dim = isl_space_as_set_space(bmap->dim);
4372 if (!bmap->dim)
4373 goto error;
4374 bmap = isl_basic_map_finalize(bmap);
4375 return (struct isl_basic_set *)bmap;
4376 error:
4377 isl_basic_map_free(bmap);
4378 return NULL;
4381 /* For a div d = floor(f/m), add the constraint
4383 * f - m d >= 0
4385 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4386 unsigned pos, isl_int *div)
4388 int i;
4389 unsigned total = isl_basic_map_total_dim(bmap);
4391 i = isl_basic_map_alloc_inequality(bmap);
4392 if (i < 0)
4393 return -1;
4394 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4395 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4397 return 0;
4400 /* For a div d = floor(f/m), add the constraint
4402 * -(f-(n-1)) + m d >= 0
4404 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4405 unsigned pos, isl_int *div)
4407 int i;
4408 unsigned total = isl_basic_map_total_dim(bmap);
4410 i = isl_basic_map_alloc_inequality(bmap);
4411 if (i < 0)
4412 return -1;
4413 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4414 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4415 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4416 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4418 return 0;
4421 /* For a div d = floor(f/m), add the constraints
4423 * f - m d >= 0
4424 * -(f-(n-1)) + m d >= 0
4426 * Note that the second constraint is the negation of
4428 * f - m d >= n
4430 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4431 unsigned pos, isl_int *div)
4433 if (add_upper_div_constraint(bmap, pos, div) < 0)
4434 return -1;
4435 if (add_lower_div_constraint(bmap, pos, div) < 0)
4436 return -1;
4437 return 0;
4440 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4441 unsigned pos, isl_int *div)
4443 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4444 pos, div);
4447 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4449 unsigned total = isl_basic_map_total_dim(bmap);
4450 unsigned div_pos = total - bmap->n_div + div;
4452 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4453 bmap->div[div]);
4456 /* For each known div d = floor(f/m), add the constraints
4458 * f - m d >= 0
4459 * -(f-(n-1)) + m d >= 0
4461 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4462 __isl_take isl_basic_map *bmap)
4464 int i;
4465 unsigned n_div;
4467 if (!bmap)
4468 return NULL;
4469 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4470 if (n_div == 0)
4471 return bmap;
4472 bmap = isl_basic_map_cow(bmap);
4473 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
4474 if (!bmap)
4475 return NULL;
4476 for (i = 0; i < n_div; ++i) {
4477 if (isl_int_is_zero(bmap->div[i][0]))
4478 continue;
4479 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4480 return isl_basic_map_free(bmap);
4483 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4484 bmap = isl_basic_map_finalize(bmap);
4485 return bmap;
4488 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4490 * In particular, if this div is of the form d = floor(f/m),
4491 * then add the constraint
4493 * f - m d >= 0
4495 * if sign < 0 or the constraint
4497 * -(f-(n-1)) + m d >= 0
4499 * if sign > 0.
4501 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4502 unsigned div, int sign)
4504 unsigned total;
4505 unsigned div_pos;
4507 if (!bmap)
4508 return -1;
4510 total = isl_basic_map_total_dim(bmap);
4511 div_pos = total - bmap->n_div + div;
4513 if (sign < 0)
4514 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4515 else
4516 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4519 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4521 return isl_basic_map_add_div_constraints(bset, div);
4524 struct isl_basic_set *isl_basic_map_underlying_set(
4525 struct isl_basic_map *bmap)
4527 if (!bmap)
4528 goto error;
4529 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4530 bmap->n_div == 0 &&
4531 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4532 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4533 return (struct isl_basic_set *)bmap;
4534 bmap = isl_basic_map_cow(bmap);
4535 if (!bmap)
4536 goto error;
4537 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4538 if (!bmap->dim)
4539 goto error;
4540 bmap->extra -= bmap->n_div;
4541 bmap->n_div = 0;
4542 bmap = isl_basic_map_finalize(bmap);
4543 return (struct isl_basic_set *)bmap;
4544 error:
4545 isl_basic_map_free(bmap);
4546 return NULL;
4549 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4550 __isl_take isl_basic_set *bset)
4552 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4555 /* Replace each element in "list" by the result of applying
4556 * isl_basic_map_underlying_set to the element.
4558 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4559 __isl_take isl_basic_map_list *list)
4561 int i, n;
4563 if (!list)
4564 return NULL;
4566 n = isl_basic_map_list_n_basic_map(list);
4567 for (i = 0; i < n; ++i) {
4568 isl_basic_map *bmap;
4569 isl_basic_set *bset;
4571 bmap = isl_basic_map_list_get_basic_map(list, i);
4572 bset = isl_basic_set_underlying_set(bmap);
4573 list = isl_basic_set_list_set_basic_set(list, i, bset);
4576 return list;
4579 struct isl_basic_map *isl_basic_map_overlying_set(
4580 struct isl_basic_set *bset, struct isl_basic_map *like)
4582 struct isl_basic_map *bmap;
4583 struct isl_ctx *ctx;
4584 unsigned total;
4585 int i;
4587 if (!bset || !like)
4588 goto error;
4589 ctx = bset->ctx;
4590 isl_assert(ctx, bset->n_div == 0, goto error);
4591 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4592 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4593 goto error);
4594 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4595 isl_basic_map_free(like);
4596 return (struct isl_basic_map *)bset;
4598 bset = isl_basic_set_cow(bset);
4599 if (!bset)
4600 goto error;
4601 total = bset->dim->n_out + bset->extra;
4602 bmap = (struct isl_basic_map *)bset;
4603 isl_space_free(bmap->dim);
4604 bmap->dim = isl_space_copy(like->dim);
4605 if (!bmap->dim)
4606 goto error;
4607 bmap->n_div = like->n_div;
4608 bmap->extra += like->n_div;
4609 if (bmap->extra) {
4610 unsigned ltotal;
4611 isl_int **div;
4612 ltotal = total - bmap->extra + like->extra;
4613 if (ltotal > total)
4614 ltotal = total;
4615 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4616 bmap->extra * (1 + 1 + total));
4617 if (isl_blk_is_error(bmap->block2))
4618 goto error;
4619 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4620 if (!div)
4621 goto error;
4622 bmap->div = div;
4623 for (i = 0; i < bmap->extra; ++i)
4624 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4625 for (i = 0; i < like->n_div; ++i) {
4626 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4627 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4629 bmap = isl_basic_map_add_known_div_constraints(bmap);
4631 isl_basic_map_free(like);
4632 bmap = isl_basic_map_simplify(bmap);
4633 bmap = isl_basic_map_finalize(bmap);
4634 return bmap;
4635 error:
4636 isl_basic_map_free(like);
4637 isl_basic_set_free(bset);
4638 return NULL;
4641 struct isl_basic_set *isl_basic_set_from_underlying_set(
4642 struct isl_basic_set *bset, struct isl_basic_set *like)
4644 return (struct isl_basic_set *)
4645 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4648 struct isl_set *isl_set_from_underlying_set(
4649 struct isl_set *set, struct isl_basic_set *like)
4651 int i;
4653 if (!set || !like)
4654 goto error;
4655 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4656 goto error);
4657 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4658 isl_basic_set_free(like);
4659 return set;
4661 set = isl_set_cow(set);
4662 if (!set)
4663 goto error;
4664 for (i = 0; i < set->n; ++i) {
4665 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4666 isl_basic_set_copy(like));
4667 if (!set->p[i])
4668 goto error;
4670 isl_space_free(set->dim);
4671 set->dim = isl_space_copy(like->dim);
4672 if (!set->dim)
4673 goto error;
4674 isl_basic_set_free(like);
4675 return set;
4676 error:
4677 isl_basic_set_free(like);
4678 isl_set_free(set);
4679 return NULL;
4682 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4684 int i;
4686 map = isl_map_cow(map);
4687 if (!map)
4688 return NULL;
4689 map->dim = isl_space_cow(map->dim);
4690 if (!map->dim)
4691 goto error;
4693 for (i = 1; i < map->n; ++i)
4694 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4695 goto error);
4696 for (i = 0; i < map->n; ++i) {
4697 map->p[i] = (struct isl_basic_map *)
4698 isl_basic_map_underlying_set(map->p[i]);
4699 if (!map->p[i])
4700 goto error;
4702 if (map->n == 0)
4703 map->dim = isl_space_underlying(map->dim, 0);
4704 else {
4705 isl_space_free(map->dim);
4706 map->dim = isl_space_copy(map->p[0]->dim);
4708 if (!map->dim)
4709 goto error;
4710 return (struct isl_set *)map;
4711 error:
4712 isl_map_free(map);
4713 return NULL;
4716 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4718 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4721 __isl_give isl_basic_map *isl_basic_map_reset_space(
4722 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4724 bmap = isl_basic_map_cow(bmap);
4725 if (!bmap || !dim)
4726 goto error;
4728 isl_space_free(bmap->dim);
4729 bmap->dim = dim;
4731 bmap = isl_basic_map_finalize(bmap);
4733 return bmap;
4734 error:
4735 isl_basic_map_free(bmap);
4736 isl_space_free(dim);
4737 return NULL;
4740 __isl_give isl_basic_set *isl_basic_set_reset_space(
4741 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4743 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4744 dim);
4747 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4748 __isl_take isl_space *dim)
4750 int i;
4752 map = isl_map_cow(map);
4753 if (!map || !dim)
4754 goto error;
4756 for (i = 0; i < map->n; ++i) {
4757 map->p[i] = isl_basic_map_reset_space(map->p[i],
4758 isl_space_copy(dim));
4759 if (!map->p[i])
4760 goto error;
4762 isl_space_free(map->dim);
4763 map->dim = dim;
4765 return map;
4766 error:
4767 isl_map_free(map);
4768 isl_space_free(dim);
4769 return NULL;
4772 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4773 __isl_take isl_space *dim)
4775 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4778 /* Compute the parameter domain of the given basic set.
4780 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4782 isl_space *space;
4783 unsigned n;
4785 if (isl_basic_set_is_params(bset))
4786 return bset;
4788 n = isl_basic_set_dim(bset, isl_dim_set);
4789 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4790 space = isl_basic_set_get_space(bset);
4791 space = isl_space_params(space);
4792 bset = isl_basic_set_reset_space(bset, space);
4793 return bset;
4796 /* Construct a zero-dimensional basic set with the given parameter domain.
4798 __isl_give isl_basic_set *isl_basic_set_from_params(
4799 __isl_take isl_basic_set *bset)
4801 isl_space *space;
4802 space = isl_basic_set_get_space(bset);
4803 space = isl_space_set_from_params(space);
4804 bset = isl_basic_set_reset_space(bset, space);
4805 return bset;
4808 /* Compute the parameter domain of the given set.
4810 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4812 isl_space *space;
4813 unsigned n;
4815 if (isl_set_is_params(set))
4816 return set;
4818 n = isl_set_dim(set, isl_dim_set);
4819 set = isl_set_project_out(set, isl_dim_set, 0, n);
4820 space = isl_set_get_space(set);
4821 space = isl_space_params(space);
4822 set = isl_set_reset_space(set, space);
4823 return set;
4826 /* Construct a zero-dimensional set with the given parameter domain.
4828 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4830 isl_space *space;
4831 space = isl_set_get_space(set);
4832 space = isl_space_set_from_params(space);
4833 set = isl_set_reset_space(set, space);
4834 return set;
4837 /* Compute the parameter domain of the given map.
4839 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4841 isl_space *space;
4842 unsigned n;
4844 n = isl_map_dim(map, isl_dim_in);
4845 map = isl_map_project_out(map, isl_dim_in, 0, n);
4846 n = isl_map_dim(map, isl_dim_out);
4847 map = isl_map_project_out(map, isl_dim_out, 0, n);
4848 space = isl_map_get_space(map);
4849 space = isl_space_params(space);
4850 map = isl_map_reset_space(map, space);
4851 return map;
4854 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4856 isl_space *dim;
4857 struct isl_basic_set *domain;
4858 unsigned n_in;
4859 unsigned n_out;
4861 if (!bmap)
4862 return NULL;
4863 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4865 n_in = isl_basic_map_n_in(bmap);
4866 n_out = isl_basic_map_n_out(bmap);
4867 domain = isl_basic_set_from_basic_map(bmap);
4868 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4870 domain = isl_basic_set_reset_space(domain, dim);
4872 return domain;
4875 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4877 if (!bmap)
4878 return -1;
4879 return isl_space_may_be_set(bmap->dim);
4882 /* Is this basic map actually a set?
4883 * Users should never call this function. Outside of isl,
4884 * the type should indicate whether something is a set or a map.
4886 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4888 if (!bmap)
4889 return -1;
4890 return isl_space_is_set(bmap->dim);
4893 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4895 if (!bmap)
4896 return NULL;
4897 if (isl_basic_map_is_set(bmap))
4898 return bmap;
4899 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4902 __isl_give isl_basic_map *isl_basic_map_domain_map(
4903 __isl_take isl_basic_map *bmap)
4905 int i, k;
4906 isl_space *dim;
4907 isl_basic_map *domain;
4908 int nparam, n_in, n_out;
4909 unsigned total;
4911 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4912 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4913 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4915 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4916 domain = isl_basic_map_universe(dim);
4918 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4919 bmap = isl_basic_map_apply_range(bmap, domain);
4920 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4922 total = isl_basic_map_total_dim(bmap);
4924 for (i = 0; i < n_in; ++i) {
4925 k = isl_basic_map_alloc_equality(bmap);
4926 if (k < 0)
4927 goto error;
4928 isl_seq_clr(bmap->eq[k], 1 + total);
4929 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4930 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4933 bmap = isl_basic_map_gauss(bmap, NULL);
4934 return isl_basic_map_finalize(bmap);
4935 error:
4936 isl_basic_map_free(bmap);
4937 return NULL;
4940 __isl_give isl_basic_map *isl_basic_map_range_map(
4941 __isl_take isl_basic_map *bmap)
4943 int i, k;
4944 isl_space *dim;
4945 isl_basic_map *range;
4946 int nparam, n_in, n_out;
4947 unsigned total;
4949 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4950 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4951 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4953 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4954 range = isl_basic_map_universe(dim);
4956 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4957 bmap = isl_basic_map_apply_range(bmap, range);
4958 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4960 total = isl_basic_map_total_dim(bmap);
4962 for (i = 0; i < n_out; ++i) {
4963 k = isl_basic_map_alloc_equality(bmap);
4964 if (k < 0)
4965 goto error;
4966 isl_seq_clr(bmap->eq[k], 1 + total);
4967 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4968 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4971 bmap = isl_basic_map_gauss(bmap, NULL);
4972 return isl_basic_map_finalize(bmap);
4973 error:
4974 isl_basic_map_free(bmap);
4975 return NULL;
4978 int isl_map_may_be_set(__isl_keep isl_map *map)
4980 if (!map)
4981 return -1;
4982 return isl_space_may_be_set(map->dim);
4985 /* Is this map actually a set?
4986 * Users should never call this function. Outside of isl,
4987 * the type should indicate whether something is a set or a map.
4989 int isl_map_is_set(__isl_keep isl_map *map)
4991 if (!map)
4992 return -1;
4993 return isl_space_is_set(map->dim);
4996 struct isl_set *isl_map_range(struct isl_map *map)
4998 int i;
4999 struct isl_set *set;
5001 if (!map)
5002 goto error;
5003 if (isl_map_is_set(map))
5004 return (isl_set *)map;
5006 map = isl_map_cow(map);
5007 if (!map)
5008 goto error;
5010 set = (struct isl_set *) map;
5011 set->dim = isl_space_range(set->dim);
5012 if (!set->dim)
5013 goto error;
5014 for (i = 0; i < map->n; ++i) {
5015 set->p[i] = isl_basic_map_range(map->p[i]);
5016 if (!set->p[i])
5017 goto error;
5019 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5020 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5021 return set;
5022 error:
5023 isl_map_free(map);
5024 return NULL;
5027 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5029 int i;
5031 map = isl_map_cow(map);
5032 if (!map)
5033 return NULL;
5035 map->dim = isl_space_domain_map(map->dim);
5036 if (!map->dim)
5037 goto error;
5038 for (i = 0; i < map->n; ++i) {
5039 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5040 if (!map->p[i])
5041 goto error;
5043 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5044 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5045 return map;
5046 error:
5047 isl_map_free(map);
5048 return NULL;
5051 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5053 int i;
5054 isl_space *range_dim;
5056 map = isl_map_cow(map);
5057 if (!map)
5058 return NULL;
5060 range_dim = isl_space_range(isl_map_get_space(map));
5061 range_dim = isl_space_from_range(range_dim);
5062 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5063 map->dim = isl_space_join(map->dim, range_dim);
5064 if (!map->dim)
5065 goto error;
5066 for (i = 0; i < map->n; ++i) {
5067 map->p[i] = isl_basic_map_range_map(map->p[i]);
5068 if (!map->p[i])
5069 goto error;
5071 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5072 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5073 return map;
5074 error:
5075 isl_map_free(map);
5076 return NULL;
5079 /* Given a wrapped map of the form A[B -> C],
5080 * return the map A[B -> C] -> B.
5082 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5084 isl_id *id;
5085 isl_map *map;
5087 if (!set)
5088 return NULL;
5089 if (!isl_set_has_tuple_id(set))
5090 return isl_map_domain_map(isl_set_unwrap(set));
5092 id = isl_set_get_tuple_id(set);
5093 map = isl_map_domain_map(isl_set_unwrap(set));
5094 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5096 return map;
5099 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5100 __isl_take isl_space *dim)
5102 int i;
5103 struct isl_map *map = NULL;
5105 set = isl_set_cow(set);
5106 if (!set || !dim)
5107 goto error;
5108 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5109 map = (struct isl_map *)set;
5110 for (i = 0; i < set->n; ++i) {
5111 map->p[i] = isl_basic_map_from_basic_set(
5112 set->p[i], isl_space_copy(dim));
5113 if (!map->p[i])
5114 goto error;
5116 isl_space_free(map->dim);
5117 map->dim = dim;
5118 return map;
5119 error:
5120 isl_space_free(dim);
5121 isl_set_free(set);
5122 return NULL;
5125 __isl_give isl_basic_map *isl_basic_map_from_domain(
5126 __isl_take isl_basic_set *bset)
5128 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5131 __isl_give isl_basic_map *isl_basic_map_from_range(
5132 __isl_take isl_basic_set *bset)
5134 isl_space *space;
5135 space = isl_basic_set_get_space(bset);
5136 space = isl_space_from_range(space);
5137 bset = isl_basic_set_reset_space(bset, space);
5138 return (isl_basic_map *)bset;
5141 /* Create a relation with the given set as range.
5142 * The domain of the created relation is a zero-dimensional
5143 * flat anonymous space.
5145 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5147 isl_space *space;
5148 space = isl_set_get_space(set);
5149 space = isl_space_from_range(space);
5150 set = isl_set_reset_space(set, space);
5151 return (struct isl_map *)set;
5154 /* Create a relation with the given set as domain.
5155 * The range of the created relation is a zero-dimensional
5156 * flat anonymous space.
5158 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5160 return isl_map_reverse(isl_map_from_range(set));
5163 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5164 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5166 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5169 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5170 __isl_take isl_set *range)
5172 return isl_map_apply_range(isl_map_reverse(domain), range);
5175 struct isl_set *isl_set_from_map(struct isl_map *map)
5177 int i;
5178 struct isl_set *set = NULL;
5180 if (!map)
5181 return NULL;
5182 map = isl_map_cow(map);
5183 if (!map)
5184 return NULL;
5185 map->dim = isl_space_as_set_space(map->dim);
5186 if (!map->dim)
5187 goto error;
5188 set = (struct isl_set *)map;
5189 for (i = 0; i < map->n; ++i) {
5190 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
5191 if (!set->p[i])
5192 goto error;
5194 return set;
5195 error:
5196 isl_map_free(map);
5197 return NULL;
5200 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5201 unsigned flags)
5203 struct isl_map *map;
5205 if (!dim)
5206 return NULL;
5207 if (n < 0)
5208 isl_die(dim->ctx, isl_error_internal,
5209 "negative number of basic maps", goto error);
5210 map = isl_alloc(dim->ctx, struct isl_map,
5211 sizeof(struct isl_map) +
5212 (n - 1) * sizeof(struct isl_basic_map *));
5213 if (!map)
5214 goto error;
5216 map->ctx = dim->ctx;
5217 isl_ctx_ref(map->ctx);
5218 map->ref = 1;
5219 map->size = n;
5220 map->n = 0;
5221 map->dim = dim;
5222 map->flags = flags;
5223 return map;
5224 error:
5225 isl_space_free(dim);
5226 return NULL;
5229 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5230 unsigned nparam, unsigned in, unsigned out, int n,
5231 unsigned flags)
5233 struct isl_map *map;
5234 isl_space *dims;
5236 dims = isl_space_alloc(ctx, nparam, in, out);
5237 if (!dims)
5238 return NULL;
5240 map = isl_map_alloc_space(dims, n, flags);
5241 return map;
5244 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5246 struct isl_basic_map *bmap;
5247 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5248 bmap = isl_basic_map_set_to_empty(bmap);
5249 return bmap;
5252 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5254 struct isl_basic_set *bset;
5255 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5256 bset = isl_basic_set_set_to_empty(bset);
5257 return bset;
5260 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5262 struct isl_basic_map *bmap;
5263 if (!model)
5264 return NULL;
5265 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5266 bmap = isl_basic_map_set_to_empty(bmap);
5267 return bmap;
5270 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5272 struct isl_basic_map *bmap;
5273 if (!model)
5274 return NULL;
5275 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5276 bmap = isl_basic_map_set_to_empty(bmap);
5277 return bmap;
5280 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5282 struct isl_basic_set *bset;
5283 if (!model)
5284 return NULL;
5285 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5286 bset = isl_basic_set_set_to_empty(bset);
5287 return bset;
5290 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5292 struct isl_basic_map *bmap;
5293 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5294 bmap = isl_basic_map_finalize(bmap);
5295 return bmap;
5298 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5300 struct isl_basic_set *bset;
5301 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5302 bset = isl_basic_set_finalize(bset);
5303 return bset;
5306 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5308 int i;
5309 unsigned total = isl_space_dim(dim, isl_dim_all);
5310 isl_basic_map *bmap;
5312 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5313 for (i = 0; i < total; ++i) {
5314 int k = isl_basic_map_alloc_inequality(bmap);
5315 if (k < 0)
5316 goto error;
5317 isl_seq_clr(bmap->ineq[k], 1 + total);
5318 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5320 return bmap;
5321 error:
5322 isl_basic_map_free(bmap);
5323 return NULL;
5326 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5328 return isl_basic_map_nat_universe(dim);
5331 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5333 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5336 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5338 return isl_map_nat_universe(dim);
5341 __isl_give isl_basic_map *isl_basic_map_universe_like(
5342 __isl_keep isl_basic_map *model)
5344 if (!model)
5345 return NULL;
5346 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5349 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5351 if (!model)
5352 return NULL;
5353 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5356 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5357 __isl_keep isl_set *model)
5359 if (!model)
5360 return NULL;
5361 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5364 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5366 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5369 struct isl_map *isl_map_empty_like(struct isl_map *model)
5371 if (!model)
5372 return NULL;
5373 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5376 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5378 if (!model)
5379 return NULL;
5380 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5383 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5385 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5388 struct isl_set *isl_set_empty_like(struct isl_set *model)
5390 if (!model)
5391 return NULL;
5392 return isl_set_empty(isl_space_copy(model->dim));
5395 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5397 struct isl_map *map;
5398 if (!dim)
5399 return NULL;
5400 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5401 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5402 return map;
5405 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5407 struct isl_set *set;
5408 if (!dim)
5409 return NULL;
5410 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5411 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5412 return set;
5415 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5417 if (!model)
5418 return NULL;
5419 return isl_set_universe(isl_space_copy(model->dim));
5422 struct isl_map *isl_map_dup(struct isl_map *map)
5424 int i;
5425 struct isl_map *dup;
5427 if (!map)
5428 return NULL;
5429 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5430 for (i = 0; i < map->n; ++i)
5431 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5432 return dup;
5435 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5436 __isl_take isl_basic_map *bmap)
5438 if (!bmap || !map)
5439 goto error;
5440 if (isl_basic_map_plain_is_empty(bmap)) {
5441 isl_basic_map_free(bmap);
5442 return map;
5444 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5445 isl_assert(map->ctx, map->n < map->size, goto error);
5446 map->p[map->n] = bmap;
5447 map->n++;
5448 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5449 return map;
5450 error:
5451 if (map)
5452 isl_map_free(map);
5453 if (bmap)
5454 isl_basic_map_free(bmap);
5455 return NULL;
5458 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5460 int i;
5462 if (!map)
5463 return NULL;
5465 if (--map->ref > 0)
5466 return NULL;
5468 isl_ctx_deref(map->ctx);
5469 for (i = 0; i < map->n; ++i)
5470 isl_basic_map_free(map->p[i]);
5471 isl_space_free(map->dim);
5472 free(map);
5474 return NULL;
5477 struct isl_map *isl_map_extend(struct isl_map *base,
5478 unsigned nparam, unsigned n_in, unsigned n_out)
5480 int i;
5482 base = isl_map_cow(base);
5483 if (!base)
5484 return NULL;
5486 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5487 if (!base->dim)
5488 goto error;
5489 for (i = 0; i < base->n; ++i) {
5490 base->p[i] = isl_basic_map_extend_space(base->p[i],
5491 isl_space_copy(base->dim), 0, 0, 0);
5492 if (!base->p[i])
5493 goto error;
5495 return base;
5496 error:
5497 isl_map_free(base);
5498 return NULL;
5501 struct isl_set *isl_set_extend(struct isl_set *base,
5502 unsigned nparam, unsigned dim)
5504 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5505 nparam, 0, dim);
5508 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5509 struct isl_basic_map *bmap, unsigned pos, int value)
5511 int j;
5513 bmap = isl_basic_map_cow(bmap);
5514 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5515 j = isl_basic_map_alloc_equality(bmap);
5516 if (j < 0)
5517 goto error;
5518 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5519 isl_int_set_si(bmap->eq[j][pos], -1);
5520 isl_int_set_si(bmap->eq[j][0], value);
5521 bmap = isl_basic_map_simplify(bmap);
5522 return isl_basic_map_finalize(bmap);
5523 error:
5524 isl_basic_map_free(bmap);
5525 return NULL;
5528 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5529 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5531 int j;
5533 bmap = isl_basic_map_cow(bmap);
5534 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5535 j = isl_basic_map_alloc_equality(bmap);
5536 if (j < 0)
5537 goto error;
5538 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5539 isl_int_set_si(bmap->eq[j][pos], -1);
5540 isl_int_set(bmap->eq[j][0], value);
5541 bmap = isl_basic_map_simplify(bmap);
5542 return isl_basic_map_finalize(bmap);
5543 error:
5544 isl_basic_map_free(bmap);
5545 return NULL;
5548 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5549 enum isl_dim_type type, unsigned pos, int value)
5551 if (!bmap)
5552 return NULL;
5553 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5554 return isl_basic_map_fix_pos_si(bmap,
5555 isl_basic_map_offset(bmap, type) + pos, value);
5556 error:
5557 isl_basic_map_free(bmap);
5558 return NULL;
5561 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5562 enum isl_dim_type type, unsigned pos, isl_int value)
5564 if (!bmap)
5565 return NULL;
5566 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5567 return isl_basic_map_fix_pos(bmap,
5568 isl_basic_map_offset(bmap, type) + pos, value);
5569 error:
5570 isl_basic_map_free(bmap);
5571 return NULL;
5574 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5575 * to be equal to "v".
5577 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5578 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5580 if (!bmap || !v)
5581 goto error;
5582 if (!isl_val_is_int(v))
5583 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5584 "expecting integer value", goto error);
5585 if (pos >= isl_basic_map_dim(bmap, type))
5586 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5587 "index out of bounds", goto error);
5588 pos += isl_basic_map_offset(bmap, type);
5589 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5590 isl_val_free(v);
5591 return bmap;
5592 error:
5593 isl_basic_map_free(bmap);
5594 isl_val_free(v);
5595 return NULL;
5598 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5599 * to be equal to "v".
5601 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5602 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5604 return isl_basic_map_fix_val(bset, type, pos, v);
5607 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5608 enum isl_dim_type type, unsigned pos, int value)
5610 return (struct isl_basic_set *)
5611 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5612 type, pos, value);
5615 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5616 enum isl_dim_type type, unsigned pos, isl_int value)
5618 return (struct isl_basic_set *)
5619 isl_basic_map_fix((struct isl_basic_map *)bset,
5620 type, pos, value);
5623 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5624 unsigned input, int value)
5626 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5629 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5630 unsigned dim, int value)
5632 return (struct isl_basic_set *)
5633 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5634 isl_dim_set, dim, value);
5637 static int remove_if_empty(__isl_keep isl_map *map, int i)
5639 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5641 if (empty < 0)
5642 return -1;
5643 if (!empty)
5644 return 0;
5646 isl_basic_map_free(map->p[i]);
5647 if (i != map->n - 1) {
5648 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5649 map->p[i] = map->p[map->n - 1];
5651 map->n--;
5653 return 0;
5656 /* Perform "fn" on each basic map of "map", where we may not be holding
5657 * the only reference to "map".
5658 * In particular, "fn" should be a semantics preserving operation
5659 * that we want to apply to all copies of "map". We therefore need
5660 * to be careful not to modify "map" in a way that breaks "map"
5661 * in case anything goes wrong.
5663 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5664 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5666 struct isl_basic_map *bmap;
5667 int i;
5669 if (!map)
5670 return NULL;
5672 for (i = map->n - 1; i >= 0; --i) {
5673 bmap = isl_basic_map_copy(map->p[i]);
5674 bmap = fn(bmap);
5675 if (!bmap)
5676 goto error;
5677 isl_basic_map_free(map->p[i]);
5678 map->p[i] = bmap;
5679 if (remove_if_empty(map, i) < 0)
5680 goto error;
5683 return map;
5684 error:
5685 isl_map_free(map);
5686 return NULL;
5689 struct isl_map *isl_map_fix_si(struct isl_map *map,
5690 enum isl_dim_type type, unsigned pos, int value)
5692 int i;
5694 map = isl_map_cow(map);
5695 if (!map)
5696 return NULL;
5698 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5699 for (i = map->n - 1; i >= 0; --i) {
5700 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5701 if (remove_if_empty(map, i) < 0)
5702 goto error;
5704 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5705 return map;
5706 error:
5707 isl_map_free(map);
5708 return NULL;
5711 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5712 enum isl_dim_type type, unsigned pos, int value)
5714 return (struct isl_set *)
5715 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5718 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5719 enum isl_dim_type type, unsigned pos, isl_int value)
5721 int i;
5723 map = isl_map_cow(map);
5724 if (!map)
5725 return NULL;
5727 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5728 for (i = 0; i < map->n; ++i) {
5729 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5730 if (!map->p[i])
5731 goto error;
5733 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5734 return map;
5735 error:
5736 isl_map_free(map);
5737 return NULL;
5740 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5741 enum isl_dim_type type, unsigned pos, isl_int value)
5743 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5746 /* Fix the value of the variable at position "pos" of type "type" of "map"
5747 * to be equal to "v".
5749 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5750 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5752 int i;
5754 map = isl_map_cow(map);
5755 if (!map || !v)
5756 goto error;
5758 if (!isl_val_is_int(v))
5759 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5760 "expecting integer value", goto error);
5761 if (pos >= isl_map_dim(map, type))
5762 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5763 "index out of bounds", goto error);
5764 for (i = map->n - 1; i >= 0; --i) {
5765 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5766 isl_val_copy(v));
5767 if (remove_if_empty(map, i) < 0)
5768 goto error;
5770 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5771 isl_val_free(v);
5772 return map;
5773 error:
5774 isl_map_free(map);
5775 isl_val_free(v);
5776 return NULL;
5779 /* Fix the value of the variable at position "pos" of type "type" of "set"
5780 * to be equal to "v".
5782 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5783 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5785 return isl_map_fix_val(set, type, pos, v);
5788 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5789 unsigned input, int value)
5791 return isl_map_fix_si(map, isl_dim_in, input, value);
5794 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5796 return (struct isl_set *)
5797 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5800 static __isl_give isl_basic_map *basic_map_bound_si(
5801 __isl_take isl_basic_map *bmap,
5802 enum isl_dim_type type, unsigned pos, int value, int upper)
5804 int j;
5806 if (!bmap)
5807 return NULL;
5808 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5809 pos += isl_basic_map_offset(bmap, type);
5810 bmap = isl_basic_map_cow(bmap);
5811 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5812 j = isl_basic_map_alloc_inequality(bmap);
5813 if (j < 0)
5814 goto error;
5815 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5816 if (upper) {
5817 isl_int_set_si(bmap->ineq[j][pos], -1);
5818 isl_int_set_si(bmap->ineq[j][0], value);
5819 } else {
5820 isl_int_set_si(bmap->ineq[j][pos], 1);
5821 isl_int_set_si(bmap->ineq[j][0], -value);
5823 bmap = isl_basic_map_simplify(bmap);
5824 return isl_basic_map_finalize(bmap);
5825 error:
5826 isl_basic_map_free(bmap);
5827 return NULL;
5830 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5831 __isl_take isl_basic_map *bmap,
5832 enum isl_dim_type type, unsigned pos, int value)
5834 return basic_map_bound_si(bmap, type, pos, value, 0);
5837 /* Constrain the values of the given dimension to be no greater than "value".
5839 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5840 __isl_take isl_basic_map *bmap,
5841 enum isl_dim_type type, unsigned pos, int value)
5843 return basic_map_bound_si(bmap, type, pos, value, 1);
5846 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5847 unsigned dim, isl_int value)
5849 int j;
5851 bset = isl_basic_set_cow(bset);
5852 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5853 j = isl_basic_set_alloc_inequality(bset);
5854 if (j < 0)
5855 goto error;
5856 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5857 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5858 isl_int_neg(bset->ineq[j][0], value);
5859 bset = isl_basic_set_simplify(bset);
5860 return isl_basic_set_finalize(bset);
5861 error:
5862 isl_basic_set_free(bset);
5863 return NULL;
5866 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5867 enum isl_dim_type type, unsigned pos, int value, int upper)
5869 int i;
5871 map = isl_map_cow(map);
5872 if (!map)
5873 return NULL;
5875 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5876 for (i = 0; i < map->n; ++i) {
5877 map->p[i] = basic_map_bound_si(map->p[i],
5878 type, pos, value, upper);
5879 if (!map->p[i])
5880 goto error;
5882 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5883 return map;
5884 error:
5885 isl_map_free(map);
5886 return NULL;
5889 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5890 enum isl_dim_type type, unsigned pos, int value)
5892 return map_bound_si(map, type, pos, value, 0);
5895 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5896 enum isl_dim_type type, unsigned pos, int value)
5898 return map_bound_si(map, type, pos, value, 1);
5901 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5902 enum isl_dim_type type, unsigned pos, int value)
5904 return (struct isl_set *)
5905 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5908 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5909 enum isl_dim_type type, unsigned pos, int value)
5911 return isl_map_upper_bound_si(set, type, pos, value);
5914 /* Bound the given variable of "bmap" from below (or above is "upper"
5915 * is set) to "value".
5917 static __isl_give isl_basic_map *basic_map_bound(
5918 __isl_take isl_basic_map *bmap,
5919 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5921 int j;
5923 if (!bmap)
5924 return NULL;
5925 if (pos >= isl_basic_map_dim(bmap, type))
5926 isl_die(bmap->ctx, isl_error_invalid,
5927 "index out of bounds", goto error);
5928 pos += isl_basic_map_offset(bmap, type);
5929 bmap = isl_basic_map_cow(bmap);
5930 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5931 j = isl_basic_map_alloc_inequality(bmap);
5932 if (j < 0)
5933 goto error;
5934 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5935 if (upper) {
5936 isl_int_set_si(bmap->ineq[j][pos], -1);
5937 isl_int_set(bmap->ineq[j][0], value);
5938 } else {
5939 isl_int_set_si(bmap->ineq[j][pos], 1);
5940 isl_int_neg(bmap->ineq[j][0], value);
5942 bmap = isl_basic_map_simplify(bmap);
5943 return isl_basic_map_finalize(bmap);
5944 error:
5945 isl_basic_map_free(bmap);
5946 return NULL;
5949 /* Bound the given variable of "map" from below (or above is "upper"
5950 * is set) to "value".
5952 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5953 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5955 int i;
5957 map = isl_map_cow(map);
5958 if (!map)
5959 return NULL;
5961 if (pos >= isl_map_dim(map, type))
5962 isl_die(map->ctx, isl_error_invalid,
5963 "index out of bounds", goto error);
5964 for (i = map->n - 1; i >= 0; --i) {
5965 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5966 if (remove_if_empty(map, i) < 0)
5967 goto error;
5969 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5970 return map;
5971 error:
5972 isl_map_free(map);
5973 return NULL;
5976 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5977 enum isl_dim_type type, unsigned pos, isl_int value)
5979 return map_bound(map, type, pos, value, 0);
5982 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5983 enum isl_dim_type type, unsigned pos, isl_int value)
5985 return map_bound(map, type, pos, value, 1);
5988 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5989 enum isl_dim_type type, unsigned pos, isl_int value)
5991 return isl_map_lower_bound(set, type, pos, value);
5994 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5995 enum isl_dim_type type, unsigned pos, isl_int value)
5997 return isl_map_upper_bound(set, type, pos, value);
6000 /* Force the values of the variable at position "pos" of type "type" of "set"
6001 * to be no smaller than "value".
6003 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6004 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6006 if (!value)
6007 goto error;
6008 if (!isl_val_is_int(value))
6009 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6010 "expecting integer value", goto error);
6011 set = isl_set_lower_bound(set, type, pos, value->n);
6012 isl_val_free(value);
6013 return set;
6014 error:
6015 isl_val_free(value);
6016 isl_set_free(set);
6017 return NULL;
6020 /* Force the values of the variable at position "pos" of type "type" of "set"
6021 * to be no greater than "value".
6023 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6024 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6026 if (!value)
6027 goto error;
6028 if (!isl_val_is_int(value))
6029 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6030 "expecting integer value", goto error);
6031 set = isl_set_upper_bound(set, type, pos, value->n);
6032 isl_val_free(value);
6033 return set;
6034 error:
6035 isl_val_free(value);
6036 isl_set_free(set);
6037 return NULL;
6040 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6041 isl_int value)
6043 int i;
6045 set = isl_set_cow(set);
6046 if (!set)
6047 return NULL;
6049 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6050 for (i = 0; i < set->n; ++i) {
6051 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6052 if (!set->p[i])
6053 goto error;
6055 return set;
6056 error:
6057 isl_set_free(set);
6058 return NULL;
6061 struct isl_map *isl_map_reverse(struct isl_map *map)
6063 int i;
6065 map = isl_map_cow(map);
6066 if (!map)
6067 return NULL;
6069 map->dim = isl_space_reverse(map->dim);
6070 if (!map->dim)
6071 goto error;
6072 for (i = 0; i < map->n; ++i) {
6073 map->p[i] = isl_basic_map_reverse(map->p[i]);
6074 if (!map->p[i])
6075 goto error;
6077 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6078 return map;
6079 error:
6080 isl_map_free(map);
6081 return NULL;
6084 static struct isl_map *isl_basic_map_partial_lexopt(
6085 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6086 struct isl_set **empty, int max)
6088 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6091 struct isl_map *isl_basic_map_partial_lexmax(
6092 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6093 struct isl_set **empty)
6095 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6098 struct isl_map *isl_basic_map_partial_lexmin(
6099 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6100 struct isl_set **empty)
6102 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6105 struct isl_set *isl_basic_set_partial_lexmin(
6106 struct isl_basic_set *bset, struct isl_basic_set *dom,
6107 struct isl_set **empty)
6109 return (struct isl_set *)
6110 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6111 dom, empty);
6114 struct isl_set *isl_basic_set_partial_lexmax(
6115 struct isl_basic_set *bset, struct isl_basic_set *dom,
6116 struct isl_set **empty)
6118 return (struct isl_set *)
6119 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6120 dom, empty);
6123 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6124 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6125 __isl_give isl_set **empty)
6127 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6130 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6131 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6132 __isl_give isl_set **empty)
6134 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6137 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6138 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6139 __isl_give isl_set **empty)
6141 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6144 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6145 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6146 __isl_give isl_set **empty)
6148 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6151 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6152 __isl_take isl_basic_map *bmap, int max)
6154 isl_basic_set *dom = NULL;
6155 isl_space *dom_space;
6157 if (!bmap)
6158 goto error;
6159 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6160 dom = isl_basic_set_universe(dom_space);
6161 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6162 error:
6163 isl_basic_map_free(bmap);
6164 return NULL;
6167 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6168 __isl_take isl_basic_map *bmap)
6170 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6173 #undef TYPE
6174 #define TYPE isl_pw_multi_aff
6175 #undef SUFFIX
6176 #define SUFFIX _pw_multi_aff
6177 #undef EMPTY
6178 #define EMPTY isl_pw_multi_aff_empty
6179 #undef ADD
6180 #define ADD isl_pw_multi_aff_union_add
6181 #include "isl_map_lexopt_templ.c"
6183 /* Given a map "map", compute the lexicographically minimal
6184 * (or maximal) image element for each domain element in dom,
6185 * in the form of an isl_pw_multi_aff.
6186 * Set *empty to those elements in dom that do not have an image element.
6188 * We first compute the lexicographically minimal or maximal element
6189 * in the first basic map. This results in a partial solution "res"
6190 * and a subset "todo" of dom that still need to be handled.
6191 * We then consider each of the remaining maps in "map" and successively
6192 * update both "res" and "todo".
6194 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6195 __isl_take isl_map *map, __isl_take isl_set *dom,
6196 __isl_give isl_set **empty, int max)
6198 int i;
6199 isl_pw_multi_aff *res;
6200 isl_set *todo;
6202 if (!map || !dom)
6203 goto error;
6205 if (isl_map_plain_is_empty(map)) {
6206 if (empty)
6207 *empty = dom;
6208 else
6209 isl_set_free(dom);
6210 return isl_pw_multi_aff_from_map(map);
6213 res = basic_map_partial_lexopt_pw_multi_aff(
6214 isl_basic_map_copy(map->p[0]),
6215 isl_set_copy(dom), &todo, max);
6217 for (i = 1; i < map->n; ++i) {
6218 isl_pw_multi_aff *res_i;
6219 isl_set *todo_i;
6221 res_i = basic_map_partial_lexopt_pw_multi_aff(
6222 isl_basic_map_copy(map->p[i]),
6223 isl_set_copy(dom), &todo_i, max);
6225 if (max)
6226 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6227 else
6228 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6230 todo = isl_set_intersect(todo, todo_i);
6233 isl_set_free(dom);
6234 isl_map_free(map);
6236 if (empty)
6237 *empty = todo;
6238 else
6239 isl_set_free(todo);
6241 return res;
6242 error:
6243 if (empty)
6244 *empty = NULL;
6245 isl_set_free(dom);
6246 isl_map_free(map);
6247 return NULL;
6250 #undef TYPE
6251 #define TYPE isl_map
6252 #undef SUFFIX
6253 #define SUFFIX
6254 #undef EMPTY
6255 #define EMPTY isl_map_empty
6256 #undef ADD
6257 #define ADD isl_map_union_disjoint
6258 #include "isl_map_lexopt_templ.c"
6260 /* Given a map "map", compute the lexicographically minimal
6261 * (or maximal) image element for each domain element in dom.
6262 * Set *empty to those elements in dom that do not have an image element.
6264 * We first compute the lexicographically minimal or maximal element
6265 * in the first basic map. This results in a partial solution "res"
6266 * and a subset "todo" of dom that still need to be handled.
6267 * We then consider each of the remaining maps in "map" and successively
6268 * update both "res" and "todo".
6270 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6271 * Assume we are computing the lexicographical maximum.
6272 * We first compute the lexicographically maximal element in basic map i.
6273 * This results in a partial solution res_i and a subset todo_i.
6274 * Then we combine these results with those obtain for the first k basic maps
6275 * to obtain a result that is valid for the first k+1 basic maps.
6276 * In particular, the set where there is no solution is the set where
6277 * there is no solution for the first k basic maps and also no solution
6278 * for the ith basic map, i.e.,
6280 * todo^i = todo^k * todo_i
6282 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6283 * solutions, arbitrarily breaking ties in favor of res^k.
6284 * That is, when res^k(a) >= res_i(a), we pick res^k and
6285 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6286 * the lexicographic order.)
6287 * In practice, we compute
6289 * res^k * (res_i . "<=")
6291 * and
6293 * res_i * (res^k . "<")
6295 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6296 * where only one of res^k and res_i provides a solution and we simply pick
6297 * that one, i.e.,
6299 * res^k * todo_i
6300 * and
6301 * res_i * todo^k
6303 * Note that we only compute these intersections when dom(res^k) intersects
6304 * dom(res_i). Otherwise, the only effect of these intersections is to
6305 * potentially break up res^k and res_i into smaller pieces.
6306 * We want to avoid such splintering as much as possible.
6307 * In fact, an earlier implementation of this function would look for
6308 * better results in the domain of res^k and for extra results in todo^k,
6309 * but this would always result in a splintering according to todo^k,
6310 * even when the domain of basic map i is disjoint from the domains of
6311 * the previous basic maps.
6313 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6314 __isl_take isl_map *map, __isl_take isl_set *dom,
6315 __isl_give isl_set **empty, int max)
6317 int i;
6318 struct isl_map *res;
6319 struct isl_set *todo;
6321 if (!map || !dom)
6322 goto error;
6324 if (isl_map_plain_is_empty(map)) {
6325 if (empty)
6326 *empty = dom;
6327 else
6328 isl_set_free(dom);
6329 return map;
6332 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6333 isl_set_copy(dom), &todo, max);
6335 for (i = 1; i < map->n; ++i) {
6336 isl_map *lt, *le;
6337 isl_map *res_i;
6338 isl_set *todo_i;
6339 isl_space *dim = isl_space_range(isl_map_get_space(res));
6341 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6342 isl_set_copy(dom), &todo_i, max);
6344 if (max) {
6345 lt = isl_map_lex_lt(isl_space_copy(dim));
6346 le = isl_map_lex_le(dim);
6347 } else {
6348 lt = isl_map_lex_gt(isl_space_copy(dim));
6349 le = isl_map_lex_ge(dim);
6351 lt = isl_map_apply_range(isl_map_copy(res), lt);
6352 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6353 le = isl_map_apply_range(isl_map_copy(res_i), le);
6354 le = isl_map_intersect(le, isl_map_copy(res));
6356 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6357 res = isl_map_intersect_domain(res,
6358 isl_set_copy(todo_i));
6359 res_i = isl_map_intersect_domain(res_i,
6360 isl_set_copy(todo));
6363 res = isl_map_union_disjoint(res, res_i);
6364 res = isl_map_union_disjoint(res, lt);
6365 res = isl_map_union_disjoint(res, le);
6367 todo = isl_set_intersect(todo, todo_i);
6370 isl_set_free(dom);
6371 isl_map_free(map);
6373 if (empty)
6374 *empty = todo;
6375 else
6376 isl_set_free(todo);
6378 return res;
6379 error:
6380 if (empty)
6381 *empty = NULL;
6382 isl_set_free(dom);
6383 isl_map_free(map);
6384 return NULL;
6387 __isl_give isl_map *isl_map_partial_lexmax(
6388 __isl_take isl_map *map, __isl_take isl_set *dom,
6389 __isl_give isl_set **empty)
6391 return isl_map_partial_lexopt(map, dom, empty, 1);
6394 __isl_give isl_map *isl_map_partial_lexmin(
6395 __isl_take isl_map *map, __isl_take isl_set *dom,
6396 __isl_give isl_set **empty)
6398 return isl_map_partial_lexopt(map, dom, empty, 0);
6401 __isl_give isl_set *isl_set_partial_lexmin(
6402 __isl_take isl_set *set, __isl_take isl_set *dom,
6403 __isl_give isl_set **empty)
6405 return (struct isl_set *)
6406 isl_map_partial_lexmin((struct isl_map *)set,
6407 dom, empty);
6410 __isl_give isl_set *isl_set_partial_lexmax(
6411 __isl_take isl_set *set, __isl_take isl_set *dom,
6412 __isl_give isl_set **empty)
6414 return (struct isl_set *)
6415 isl_map_partial_lexmax((struct isl_map *)set,
6416 dom, empty);
6419 /* Compute the lexicographic minimum (or maximum if "max" is set)
6420 * of "bmap" over its domain.
6422 * Since we are not interested in the part of the domain space where
6423 * there is no solution, we initialize the domain to those constraints
6424 * of "bmap" that only involve the parameters and the input dimensions.
6425 * This relieves the parametric programming engine from detecting those
6426 * inequalities and transferring them to the context. More importantly,
6427 * it ensures that those inequalities are transferred first and not
6428 * intermixed with inequalities that actually split the domain.
6430 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6432 int n_div;
6433 int n_out;
6434 isl_basic_map *copy;
6435 isl_basic_set *dom;
6437 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6438 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6439 copy = isl_basic_map_copy(bmap);
6440 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6441 isl_dim_div, 0, n_div);
6442 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6443 isl_dim_out, 0, n_out);
6444 dom = isl_basic_map_domain(copy);
6445 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6448 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6450 return isl_basic_map_lexopt(bmap, 0);
6453 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6455 return isl_basic_map_lexopt(bmap, 1);
6458 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6460 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6463 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6465 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6468 /* Extract the first and only affine expression from list
6469 * and then add it to *pwaff with the given dom.
6470 * This domain is known to be disjoint from other domains
6471 * because of the way isl_basic_map_foreach_lexmax works.
6473 static int update_dim_opt(__isl_take isl_basic_set *dom,
6474 __isl_take isl_aff_list *list, void *user)
6476 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6477 isl_aff *aff;
6478 isl_pw_aff **pwaff = user;
6479 isl_pw_aff *pwaff_i;
6481 if (!list)
6482 goto error;
6483 if (isl_aff_list_n_aff(list) != 1)
6484 isl_die(ctx, isl_error_internal,
6485 "expecting single element list", goto error);
6487 aff = isl_aff_list_get_aff(list, 0);
6488 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6490 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6492 isl_aff_list_free(list);
6494 return 0;
6495 error:
6496 isl_basic_set_free(dom);
6497 isl_aff_list_free(list);
6498 return -1;
6501 /* Given a basic map with one output dimension, compute the minimum or
6502 * maximum of that dimension as an isl_pw_aff.
6504 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6505 * call update_dim_opt on each leaf of the result.
6507 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6508 int max)
6510 isl_space *dim = isl_basic_map_get_space(bmap);
6511 isl_pw_aff *pwaff;
6512 int r;
6514 dim = isl_space_from_domain(isl_space_domain(dim));
6515 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6516 pwaff = isl_pw_aff_empty(dim);
6518 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6519 if (r < 0)
6520 return isl_pw_aff_free(pwaff);
6522 return pwaff;
6525 /* Compute the minimum or maximum of the given output dimension
6526 * as a function of the parameters and the input dimensions,
6527 * but independently of the other output dimensions.
6529 * We first project out the other output dimension and then compute
6530 * the "lexicographic" maximum in each basic map, combining the results
6531 * using isl_pw_aff_union_max.
6533 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6534 int max)
6536 int i;
6537 isl_pw_aff *pwaff;
6538 unsigned n_out;
6540 n_out = isl_map_dim(map, isl_dim_out);
6541 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6542 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6543 if (!map)
6544 return NULL;
6546 if (map->n == 0) {
6547 isl_space *dim = isl_map_get_space(map);
6548 isl_map_free(map);
6549 return isl_pw_aff_empty(dim);
6552 pwaff = basic_map_dim_opt(map->p[0], max);
6553 for (i = 1; i < map->n; ++i) {
6554 isl_pw_aff *pwaff_i;
6556 pwaff_i = basic_map_dim_opt(map->p[i], max);
6557 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6560 isl_map_free(map);
6562 return pwaff;
6565 /* Compute the maximum of the given output dimension as a function of the
6566 * parameters and input dimensions, but independently of
6567 * the other output dimensions.
6569 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6571 return map_dim_opt(map, pos, 1);
6574 /* Compute the minimum or maximum of the given set dimension
6575 * as a function of the parameters,
6576 * but independently of the other set dimensions.
6578 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6579 int max)
6581 return map_dim_opt(set, pos, max);
6584 /* Compute the maximum of the given set dimension as a function of the
6585 * parameters, but independently of the other set dimensions.
6587 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6589 return set_dim_opt(set, pos, 1);
6592 /* Compute the minimum of the given set dimension as a function of the
6593 * parameters, but independently of the other set dimensions.
6595 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6597 return set_dim_opt(set, pos, 0);
6600 /* Apply a preimage specified by "mat" on the parameters of "bset".
6601 * bset is assumed to have only parameters and divs.
6603 static struct isl_basic_set *basic_set_parameter_preimage(
6604 struct isl_basic_set *bset, struct isl_mat *mat)
6606 unsigned nparam;
6608 if (!bset || !mat)
6609 goto error;
6611 bset->dim = isl_space_cow(bset->dim);
6612 if (!bset->dim)
6613 goto error;
6615 nparam = isl_basic_set_dim(bset, isl_dim_param);
6617 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6619 bset->dim->nparam = 0;
6620 bset->dim->n_out = nparam;
6621 bset = isl_basic_set_preimage(bset, mat);
6622 if (bset) {
6623 bset->dim->nparam = bset->dim->n_out;
6624 bset->dim->n_out = 0;
6626 return bset;
6627 error:
6628 isl_mat_free(mat);
6629 isl_basic_set_free(bset);
6630 return NULL;
6633 /* Apply a preimage specified by "mat" on the parameters of "set".
6634 * set is assumed to have only parameters and divs.
6636 static struct isl_set *set_parameter_preimage(
6637 struct isl_set *set, struct isl_mat *mat)
6639 isl_space *dim = NULL;
6640 unsigned nparam;
6642 if (!set || !mat)
6643 goto error;
6645 dim = isl_space_copy(set->dim);
6646 dim = isl_space_cow(dim);
6647 if (!dim)
6648 goto error;
6650 nparam = isl_set_dim(set, isl_dim_param);
6652 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6654 dim->nparam = 0;
6655 dim->n_out = nparam;
6656 isl_set_reset_space(set, dim);
6657 set = isl_set_preimage(set, mat);
6658 if (!set)
6659 goto error2;
6660 dim = isl_space_copy(set->dim);
6661 dim = isl_space_cow(dim);
6662 if (!dim)
6663 goto error2;
6664 dim->nparam = dim->n_out;
6665 dim->n_out = 0;
6666 isl_set_reset_space(set, dim);
6667 return set;
6668 error:
6669 isl_space_free(dim);
6670 isl_mat_free(mat);
6671 error2:
6672 isl_set_free(set);
6673 return NULL;
6676 /* Intersect the basic set "bset" with the affine space specified by the
6677 * equalities in "eq".
6679 static struct isl_basic_set *basic_set_append_equalities(
6680 struct isl_basic_set *bset, struct isl_mat *eq)
6682 int i, k;
6683 unsigned len;
6685 if (!bset || !eq)
6686 goto error;
6688 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6689 eq->n_row, 0);
6690 if (!bset)
6691 goto error;
6693 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6694 for (i = 0; i < eq->n_row; ++i) {
6695 k = isl_basic_set_alloc_equality(bset);
6696 if (k < 0)
6697 goto error;
6698 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6699 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6701 isl_mat_free(eq);
6703 bset = isl_basic_set_gauss(bset, NULL);
6704 bset = isl_basic_set_finalize(bset);
6706 return bset;
6707 error:
6708 isl_mat_free(eq);
6709 isl_basic_set_free(bset);
6710 return NULL;
6713 /* Intersect the set "set" with the affine space specified by the
6714 * equalities in "eq".
6716 static struct isl_set *set_append_equalities(struct isl_set *set,
6717 struct isl_mat *eq)
6719 int i;
6721 if (!set || !eq)
6722 goto error;
6724 for (i = 0; i < set->n; ++i) {
6725 set->p[i] = basic_set_append_equalities(set->p[i],
6726 isl_mat_copy(eq));
6727 if (!set->p[i])
6728 goto error;
6730 isl_mat_free(eq);
6731 return set;
6732 error:
6733 isl_mat_free(eq);
6734 isl_set_free(set);
6735 return NULL;
6738 /* Given a basic set "bset" that only involves parameters and existentially
6739 * quantified variables, return the index of the first equality
6740 * that only involves parameters. If there is no such equality then
6741 * return bset->n_eq.
6743 * This function assumes that isl_basic_set_gauss has been called on "bset".
6745 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6747 int i, j;
6748 unsigned nparam, n_div;
6750 if (!bset)
6751 return -1;
6753 nparam = isl_basic_set_dim(bset, isl_dim_param);
6754 n_div = isl_basic_set_dim(bset, isl_dim_div);
6756 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6757 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6758 ++i;
6761 return i;
6764 /* Compute an explicit representation for the existentially quantified
6765 * variables in "bset" by computing the "minimal value" of the set
6766 * variables. Since there are no set variables, the computation of
6767 * the minimal value essentially computes an explicit representation
6768 * of the non-empty part(s) of "bset".
6770 * The input only involves parameters and existentially quantified variables.
6771 * All equalities among parameters have been removed.
6773 * Since the existentially quantified variables in the result are in general
6774 * going to be different from those in the input, we first replace
6775 * them by the minimal number of variables based on their equalities.
6776 * This should simplify the parametric integer programming.
6778 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6780 isl_morph *morph1, *morph2;
6781 isl_set *set;
6782 unsigned n;
6784 if (!bset)
6785 return NULL;
6786 if (bset->n_eq == 0)
6787 return isl_basic_set_lexmin(bset);
6789 morph1 = isl_basic_set_parameter_compression(bset);
6790 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6791 bset = isl_basic_set_lift(bset);
6792 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6793 bset = isl_morph_basic_set(morph2, bset);
6794 n = isl_basic_set_dim(bset, isl_dim_set);
6795 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6797 set = isl_basic_set_lexmin(bset);
6799 set = isl_morph_set(isl_morph_inverse(morph1), set);
6801 return set;
6804 /* Project the given basic set onto its parameter domain, possibly introducing
6805 * new, explicit, existential variables in the constraints.
6806 * The input has parameters and (possibly implicit) existential variables.
6807 * The output has the same parameters, but only
6808 * explicit existentially quantified variables.
6810 * The actual projection is performed by pip, but pip doesn't seem
6811 * to like equalities very much, so we first remove the equalities
6812 * among the parameters by performing a variable compression on
6813 * the parameters. Afterward, an inverse transformation is performed
6814 * and the equalities among the parameters are inserted back in.
6816 * The variable compression on the parameters may uncover additional
6817 * equalities that were only implicit before. We therefore check
6818 * if there are any new parameter equalities in the result and
6819 * if so recurse. The removal of parameter equalities is required
6820 * for the parameter compression performed by base_compute_divs.
6822 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6824 int i;
6825 struct isl_mat *eq;
6826 struct isl_mat *T, *T2;
6827 struct isl_set *set;
6828 unsigned nparam;
6830 bset = isl_basic_set_cow(bset);
6831 if (!bset)
6832 return NULL;
6834 if (bset->n_eq == 0)
6835 return base_compute_divs(bset);
6837 bset = isl_basic_set_gauss(bset, NULL);
6838 if (!bset)
6839 return NULL;
6840 if (isl_basic_set_plain_is_empty(bset))
6841 return isl_set_from_basic_set(bset);
6843 i = first_parameter_equality(bset);
6844 if (i == bset->n_eq)
6845 return base_compute_divs(bset);
6847 nparam = isl_basic_set_dim(bset, isl_dim_param);
6848 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6849 0, 1 + nparam);
6850 eq = isl_mat_cow(eq);
6851 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6852 if (T && T->n_col == 0) {
6853 isl_mat_free(T);
6854 isl_mat_free(T2);
6855 isl_mat_free(eq);
6856 bset = isl_basic_set_set_to_empty(bset);
6857 return isl_set_from_basic_set(bset);
6859 bset = basic_set_parameter_preimage(bset, T);
6861 i = first_parameter_equality(bset);
6862 if (!bset)
6863 set = NULL;
6864 else if (i == bset->n_eq)
6865 set = base_compute_divs(bset);
6866 else
6867 set = parameter_compute_divs(bset);
6868 set = set_parameter_preimage(set, T2);
6869 set = set_append_equalities(set, eq);
6870 return set;
6873 /* Insert the divs from "ls" before those of "bmap".
6875 * The number of columns is not changed, which means that the last
6876 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6877 * The caller is responsible for removing the same number of dimensions
6878 * from the space of "bmap".
6880 static __isl_give isl_basic_map *insert_divs_from_local_space(
6881 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6883 int i;
6884 int n_div;
6885 int old_n_div;
6887 n_div = isl_local_space_dim(ls, isl_dim_div);
6888 if (n_div == 0)
6889 return bmap;
6891 old_n_div = bmap->n_div;
6892 bmap = insert_div_rows(bmap, n_div);
6893 if (!bmap)
6894 return NULL;
6896 for (i = 0; i < n_div; ++i) {
6897 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6898 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6901 return bmap;
6904 /* Replace the space of "bmap" by the space and divs of "ls".
6906 * If "ls" has any divs, then we simplify the result since we may
6907 * have discovered some additional equalities that could simplify
6908 * the div expressions.
6910 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6911 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6913 int n_div;
6915 bmap = isl_basic_map_cow(bmap);
6916 if (!bmap || !ls)
6917 goto error;
6919 n_div = isl_local_space_dim(ls, isl_dim_div);
6920 bmap = insert_divs_from_local_space(bmap, ls);
6921 if (!bmap)
6922 goto error;
6924 isl_space_free(bmap->dim);
6925 bmap->dim = isl_local_space_get_space(ls);
6926 if (!bmap->dim)
6927 goto error;
6929 isl_local_space_free(ls);
6930 if (n_div > 0)
6931 bmap = isl_basic_map_simplify(bmap);
6932 bmap = isl_basic_map_finalize(bmap);
6933 return bmap;
6934 error:
6935 isl_basic_map_free(bmap);
6936 isl_local_space_free(ls);
6937 return NULL;
6940 /* Replace the space of "map" by the space and divs of "ls".
6942 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6943 __isl_take isl_local_space *ls)
6945 int i;
6947 map = isl_map_cow(map);
6948 if (!map || !ls)
6949 goto error;
6951 for (i = 0; i < map->n; ++i) {
6952 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6953 isl_local_space_copy(ls));
6954 if (!map->p[i])
6955 goto error;
6957 isl_space_free(map->dim);
6958 map->dim = isl_local_space_get_space(ls);
6959 if (!map->dim)
6960 goto error;
6962 isl_local_space_free(ls);
6963 return map;
6964 error:
6965 isl_local_space_free(ls);
6966 isl_map_free(map);
6967 return NULL;
6970 /* Compute an explicit representation for the existentially
6971 * quantified variables for which do not know any explicit representation yet.
6973 * We first sort the existentially quantified variables so that the
6974 * existentially quantified variables for which we already have an explicit
6975 * representation are placed before those for which we do not.
6976 * The input dimensions, the output dimensions and the existentially
6977 * quantified variables for which we already have an explicit
6978 * representation are then turned into parameters.
6979 * compute_divs returns a map with the same parameters and
6980 * no input or output dimensions and the dimension specification
6981 * is reset to that of the input, including the existentially quantified
6982 * variables for which we already had an explicit representation.
6984 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6986 struct isl_basic_set *bset;
6987 struct isl_set *set;
6988 struct isl_map *map;
6989 isl_space *dim;
6990 isl_local_space *ls;
6991 unsigned nparam;
6992 unsigned n_in;
6993 unsigned n_out;
6994 unsigned n_known;
6995 int i;
6997 bmap = isl_basic_map_sort_divs(bmap);
6998 bmap = isl_basic_map_cow(bmap);
6999 if (!bmap)
7000 return NULL;
7002 for (n_known = 0; n_known < bmap->n_div; ++n_known)
7003 if (isl_int_is_zero(bmap->div[n_known][0]))
7004 break;
7006 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7007 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7008 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7009 dim = isl_space_set_alloc(bmap->ctx,
7010 nparam + n_in + n_out + n_known, 0);
7011 if (!dim)
7012 goto error;
7014 ls = isl_basic_map_get_local_space(bmap);
7015 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7016 n_known, bmap->n_div - n_known);
7017 if (n_known > 0) {
7018 for (i = n_known; i < bmap->n_div; ++i)
7019 swap_div(bmap, i - n_known, i);
7020 bmap->n_div -= n_known;
7021 bmap->extra -= n_known;
7023 bmap = isl_basic_map_reset_space(bmap, dim);
7024 bset = (struct isl_basic_set *)bmap;
7026 set = parameter_compute_divs(bset);
7027 map = (struct isl_map *)set;
7028 map = replace_space_by_local_space(map, ls);
7030 return map;
7031 error:
7032 isl_basic_map_free(bmap);
7033 return NULL;
7036 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7038 int i;
7039 unsigned off;
7041 if (!bmap)
7042 return -1;
7044 off = isl_space_dim(bmap->dim, isl_dim_all);
7045 for (i = 0; i < bmap->n_div; ++i) {
7046 if (isl_int_is_zero(bmap->div[i][0]))
7047 return 0;
7048 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
7049 return -1);
7051 return 1;
7054 static int map_divs_known(__isl_keep isl_map *map)
7056 int i;
7058 if (!map)
7059 return -1;
7061 for (i = 0; i < map->n; ++i) {
7062 int known = isl_basic_map_divs_known(map->p[i]);
7063 if (known <= 0)
7064 return known;
7067 return 1;
7070 /* If bmap contains any unknown divs, then compute explicit
7071 * expressions for them. However, this computation may be
7072 * quite expensive, so first try to remove divs that aren't
7073 * strictly needed.
7075 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7077 int known;
7078 struct isl_map *map;
7080 known = isl_basic_map_divs_known(bmap);
7081 if (known < 0)
7082 goto error;
7083 if (known)
7084 return isl_map_from_basic_map(bmap);
7086 bmap = isl_basic_map_drop_redundant_divs(bmap);
7088 known = isl_basic_map_divs_known(bmap);
7089 if (known < 0)
7090 goto error;
7091 if (known)
7092 return isl_map_from_basic_map(bmap);
7094 map = compute_divs(bmap);
7095 return map;
7096 error:
7097 isl_basic_map_free(bmap);
7098 return NULL;
7101 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7103 int i;
7104 int known;
7105 struct isl_map *res;
7107 if (!map)
7108 return NULL;
7109 if (map->n == 0)
7110 return map;
7112 known = map_divs_known(map);
7113 if (known < 0) {
7114 isl_map_free(map);
7115 return NULL;
7117 if (known)
7118 return map;
7120 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7121 for (i = 1 ; i < map->n; ++i) {
7122 struct isl_map *r2;
7123 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7124 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7125 res = isl_map_union_disjoint(res, r2);
7126 else
7127 res = isl_map_union(res, r2);
7129 isl_map_free(map);
7131 return res;
7134 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7136 return (struct isl_set *)
7137 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7140 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7142 return (struct isl_set *)
7143 isl_map_compute_divs((struct isl_map *)set);
7146 struct isl_set *isl_map_domain(struct isl_map *map)
7148 int i;
7149 struct isl_set *set;
7151 if (!map)
7152 goto error;
7154 map = isl_map_cow(map);
7155 if (!map)
7156 return NULL;
7158 set = (struct isl_set *)map;
7159 set->dim = isl_space_domain(set->dim);
7160 if (!set->dim)
7161 goto error;
7162 for (i = 0; i < map->n; ++i) {
7163 set->p[i] = isl_basic_map_domain(map->p[i]);
7164 if (!set->p[i])
7165 goto error;
7167 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7168 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7169 return set;
7170 error:
7171 isl_map_free(map);
7172 return NULL;
7175 /* Return the union of "map1" and "map2", where we assume for now that
7176 * "map1" and "map2" are disjoint. Note that the basic maps inside
7177 * "map1" or "map2" may not be disjoint from each other.
7178 * Also note that this function is also called from isl_map_union,
7179 * which takes care of handling the situation where "map1" and "map2"
7180 * may not be disjoint.
7182 * If one of the inputs is empty, we can simply return the other input.
7183 * Similarly, if one of the inputs is universal, then it is equal to the union.
7185 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7186 __isl_take isl_map *map2)
7188 int i;
7189 unsigned flags = 0;
7190 struct isl_map *map = NULL;
7191 int is_universe;
7193 if (!map1 || !map2)
7194 goto error;
7196 if (!isl_space_is_equal(map1->dim, map2->dim))
7197 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7198 "spaces don't match", goto error);
7200 if (map1->n == 0) {
7201 isl_map_free(map1);
7202 return map2;
7204 if (map2->n == 0) {
7205 isl_map_free(map2);
7206 return map1;
7209 is_universe = isl_map_plain_is_universe(map1);
7210 if (is_universe < 0)
7211 goto error;
7212 if (is_universe) {
7213 isl_map_free(map2);
7214 return map1;
7217 is_universe = isl_map_plain_is_universe(map2);
7218 if (is_universe < 0)
7219 goto error;
7220 if (is_universe) {
7221 isl_map_free(map1);
7222 return map2;
7225 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7226 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7227 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7229 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7230 map1->n + map2->n, flags);
7231 if (!map)
7232 goto error;
7233 for (i = 0; i < map1->n; ++i) {
7234 map = isl_map_add_basic_map(map,
7235 isl_basic_map_copy(map1->p[i]));
7236 if (!map)
7237 goto error;
7239 for (i = 0; i < map2->n; ++i) {
7240 map = isl_map_add_basic_map(map,
7241 isl_basic_map_copy(map2->p[i]));
7242 if (!map)
7243 goto error;
7245 isl_map_free(map1);
7246 isl_map_free(map2);
7247 return map;
7248 error:
7249 isl_map_free(map);
7250 isl_map_free(map1);
7251 isl_map_free(map2);
7252 return NULL;
7255 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7256 * guaranteed to be disjoint by the caller.
7258 * Note that this functions is called from within isl_map_make_disjoint,
7259 * so we have to be careful not to touch the constraints of the inputs
7260 * in any way.
7262 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7263 __isl_take isl_map *map2)
7265 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7268 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7269 * not be disjoint. The parameters are assumed to have been aligned.
7271 * We currently simply call map_union_disjoint, the internal operation
7272 * of which does not really depend on the inputs being disjoint.
7273 * If the result contains more than one basic map, then we clear
7274 * the disjoint flag since the result may contain basic maps from
7275 * both inputs and these are not guaranteed to be disjoint.
7277 * As a special case, if "map1" and "map2" are obviously equal,
7278 * then we simply return "map1".
7280 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7281 __isl_take isl_map *map2)
7283 int equal;
7285 if (!map1 || !map2)
7286 goto error;
7288 equal = isl_map_plain_is_equal(map1, map2);
7289 if (equal < 0)
7290 goto error;
7291 if (equal) {
7292 isl_map_free(map2);
7293 return map1;
7296 map1 = map_union_disjoint(map1, map2);
7297 if (!map1)
7298 return NULL;
7299 if (map1->n > 1)
7300 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7301 return map1;
7302 error:
7303 isl_map_free(map1);
7304 isl_map_free(map2);
7305 return NULL;
7308 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7309 * not be disjoint.
7311 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7312 __isl_take isl_map *map2)
7314 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7317 struct isl_set *isl_set_union_disjoint(
7318 struct isl_set *set1, struct isl_set *set2)
7320 return (struct isl_set *)
7321 isl_map_union_disjoint(
7322 (struct isl_map *)set1, (struct isl_map *)set2);
7325 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7327 return (struct isl_set *)
7328 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7331 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7332 * the results.
7334 * "map" and "set" are assumed to be compatible and non-NULL.
7336 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7337 __isl_take isl_set *set,
7338 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7339 __isl_take isl_basic_set *bset))
7341 unsigned flags = 0;
7342 struct isl_map *result;
7343 int i, j;
7345 if (isl_set_plain_is_universe(set)) {
7346 isl_set_free(set);
7347 return map;
7350 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7351 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7352 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7354 result = isl_map_alloc_space(isl_space_copy(map->dim),
7355 map->n * set->n, flags);
7356 for (i = 0; result && i < map->n; ++i)
7357 for (j = 0; j < set->n; ++j) {
7358 result = isl_map_add_basic_map(result,
7359 fn(isl_basic_map_copy(map->p[i]),
7360 isl_basic_set_copy(set->p[j])));
7361 if (!result)
7362 break;
7365 isl_map_free(map);
7366 isl_set_free(set);
7367 return result;
7370 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7371 __isl_take isl_set *set)
7373 if (!map || !set)
7374 goto error;
7376 if (!isl_map_compatible_range(map, set))
7377 isl_die(set->ctx, isl_error_invalid,
7378 "incompatible spaces", goto error);
7380 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7381 error:
7382 isl_map_free(map);
7383 isl_set_free(set);
7384 return NULL;
7387 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7388 __isl_take isl_set *set)
7390 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7393 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7394 __isl_take isl_set *set)
7396 if (!map || !set)
7397 goto error;
7399 if (!isl_map_compatible_domain(map, set))
7400 isl_die(set->ctx, isl_error_invalid,
7401 "incompatible spaces", goto error);
7403 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7404 error:
7405 isl_map_free(map);
7406 isl_set_free(set);
7407 return NULL;
7410 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7411 __isl_take isl_set *set)
7413 return isl_map_align_params_map_map_and(map, set,
7414 &map_intersect_domain);
7417 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7418 __isl_take isl_map *map2)
7420 if (!map1 || !map2)
7421 goto error;
7422 map1 = isl_map_reverse(map1);
7423 map1 = isl_map_apply_range(map1, map2);
7424 return isl_map_reverse(map1);
7425 error:
7426 isl_map_free(map1);
7427 isl_map_free(map2);
7428 return NULL;
7431 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7432 __isl_take isl_map *map2)
7434 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7437 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7438 __isl_take isl_map *map2)
7440 isl_space *dim_result;
7441 struct isl_map *result;
7442 int i, j;
7444 if (!map1 || !map2)
7445 goto error;
7447 dim_result = isl_space_join(isl_space_copy(map1->dim),
7448 isl_space_copy(map2->dim));
7450 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7451 if (!result)
7452 goto error;
7453 for (i = 0; i < map1->n; ++i)
7454 for (j = 0; j < map2->n; ++j) {
7455 result = isl_map_add_basic_map(result,
7456 isl_basic_map_apply_range(
7457 isl_basic_map_copy(map1->p[i]),
7458 isl_basic_map_copy(map2->p[j])));
7459 if (!result)
7460 goto error;
7462 isl_map_free(map1);
7463 isl_map_free(map2);
7464 if (result && result->n <= 1)
7465 ISL_F_SET(result, ISL_MAP_DISJOINT);
7466 return result;
7467 error:
7468 isl_map_free(map1);
7469 isl_map_free(map2);
7470 return NULL;
7473 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7474 __isl_take isl_map *map2)
7476 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7480 * returns range - domain
7482 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7484 isl_space *dims, *target_dim;
7485 struct isl_basic_set *bset;
7486 unsigned dim;
7487 unsigned nparam;
7488 int i;
7490 if (!bmap)
7491 goto error;
7492 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7493 bmap->dim, isl_dim_out),
7494 goto error);
7495 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7496 dim = isl_basic_map_n_in(bmap);
7497 nparam = isl_basic_map_n_param(bmap);
7498 bset = isl_basic_set_from_basic_map(bmap);
7499 bset = isl_basic_set_cow(bset);
7500 dims = isl_basic_set_get_space(bset);
7501 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7502 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7503 bset = isl_basic_set_swap_vars(bset, 2*dim);
7504 for (i = 0; i < dim; ++i) {
7505 int j = isl_basic_map_alloc_equality(
7506 (struct isl_basic_map *)bset);
7507 if (j < 0) {
7508 bset = isl_basic_set_free(bset);
7509 break;
7511 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7512 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7513 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7514 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7516 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7517 bset = isl_basic_set_reset_space(bset, target_dim);
7518 return bset;
7519 error:
7520 isl_basic_map_free(bmap);
7521 return NULL;
7525 * returns range - domain
7527 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7529 int i;
7530 isl_space *dim;
7531 struct isl_set *result;
7533 if (!map)
7534 return NULL;
7536 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7537 map->dim, isl_dim_out),
7538 goto error);
7539 dim = isl_map_get_space(map);
7540 dim = isl_space_domain(dim);
7541 result = isl_set_alloc_space(dim, map->n, 0);
7542 if (!result)
7543 goto error;
7544 for (i = 0; i < map->n; ++i)
7545 result = isl_set_add_basic_set(result,
7546 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7547 isl_map_free(map);
7548 return result;
7549 error:
7550 isl_map_free(map);
7551 return NULL;
7555 * returns [domain -> range] -> range - domain
7557 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7558 __isl_take isl_basic_map *bmap)
7560 int i, k;
7561 isl_space *dim;
7562 isl_basic_map *domain;
7563 int nparam, n;
7564 unsigned total;
7566 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7567 bmap->dim, isl_dim_out))
7568 isl_die(bmap->ctx, isl_error_invalid,
7569 "domain and range don't match", goto error);
7571 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7572 n = isl_basic_map_dim(bmap, isl_dim_in);
7574 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7575 domain = isl_basic_map_universe(dim);
7577 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7578 bmap = isl_basic_map_apply_range(bmap, domain);
7579 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7581 total = isl_basic_map_total_dim(bmap);
7583 for (i = 0; i < n; ++i) {
7584 k = isl_basic_map_alloc_equality(bmap);
7585 if (k < 0)
7586 goto error;
7587 isl_seq_clr(bmap->eq[k], 1 + total);
7588 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7589 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7590 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7593 bmap = isl_basic_map_gauss(bmap, NULL);
7594 return isl_basic_map_finalize(bmap);
7595 error:
7596 isl_basic_map_free(bmap);
7597 return NULL;
7601 * returns [domain -> range] -> range - domain
7603 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7605 int i;
7606 isl_space *domain_dim;
7608 if (!map)
7609 return NULL;
7611 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7612 map->dim, isl_dim_out))
7613 isl_die(map->ctx, isl_error_invalid,
7614 "domain and range don't match", goto error);
7616 map = isl_map_cow(map);
7617 if (!map)
7618 return NULL;
7620 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7621 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7622 map->dim = isl_space_join(map->dim, domain_dim);
7623 if (!map->dim)
7624 goto error;
7625 for (i = 0; i < map->n; ++i) {
7626 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7627 if (!map->p[i])
7628 goto error;
7630 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7631 return map;
7632 error:
7633 isl_map_free(map);
7634 return NULL;
7637 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7639 struct isl_basic_map *bmap;
7640 unsigned nparam;
7641 unsigned dim;
7642 int i;
7644 if (!dims)
7645 return NULL;
7647 nparam = dims->nparam;
7648 dim = dims->n_out;
7649 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7650 if (!bmap)
7651 goto error;
7653 for (i = 0; i < dim; ++i) {
7654 int j = isl_basic_map_alloc_equality(bmap);
7655 if (j < 0)
7656 goto error;
7657 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7658 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7659 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7661 return isl_basic_map_finalize(bmap);
7662 error:
7663 isl_basic_map_free(bmap);
7664 return NULL;
7667 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7669 if (!dim)
7670 return NULL;
7671 if (dim->n_in != dim->n_out)
7672 isl_die(dim->ctx, isl_error_invalid,
7673 "number of input and output dimensions needs to be "
7674 "the same", goto error);
7675 return basic_map_identity(dim);
7676 error:
7677 isl_space_free(dim);
7678 return NULL;
7681 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7683 if (!model || !model->dim)
7684 return NULL;
7685 return isl_basic_map_identity(isl_space_copy(model->dim));
7688 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7690 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7693 struct isl_map *isl_map_identity_like(struct isl_map *model)
7695 if (!model || !model->dim)
7696 return NULL;
7697 return isl_map_identity(isl_space_copy(model->dim));
7700 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7702 if (!model || !model->dim)
7703 return NULL;
7704 return isl_map_identity(isl_space_copy(model->dim));
7707 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7709 isl_space *dim = isl_set_get_space(set);
7710 isl_map *id;
7711 id = isl_map_identity(isl_space_map_from_set(dim));
7712 return isl_map_intersect_range(id, set);
7715 /* Construct a basic set with all set dimensions having only non-negative
7716 * values.
7718 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7719 __isl_take isl_space *space)
7721 int i;
7722 unsigned nparam;
7723 unsigned dim;
7724 struct isl_basic_set *bset;
7726 if (!space)
7727 return NULL;
7728 nparam = space->nparam;
7729 dim = space->n_out;
7730 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7731 if (!bset)
7732 return NULL;
7733 for (i = 0; i < dim; ++i) {
7734 int k = isl_basic_set_alloc_inequality(bset);
7735 if (k < 0)
7736 goto error;
7737 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7738 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7740 return bset;
7741 error:
7742 isl_basic_set_free(bset);
7743 return NULL;
7746 /* Construct the half-space x_pos >= 0.
7748 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7749 int pos)
7751 int k;
7752 isl_basic_set *nonneg;
7754 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7755 k = isl_basic_set_alloc_inequality(nonneg);
7756 if (k < 0)
7757 goto error;
7758 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7759 isl_int_set_si(nonneg->ineq[k][pos], 1);
7761 return isl_basic_set_finalize(nonneg);
7762 error:
7763 isl_basic_set_free(nonneg);
7764 return NULL;
7767 /* Construct the half-space x_pos <= -1.
7769 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7771 int k;
7772 isl_basic_set *neg;
7774 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7775 k = isl_basic_set_alloc_inequality(neg);
7776 if (k < 0)
7777 goto error;
7778 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7779 isl_int_set_si(neg->ineq[k][0], -1);
7780 isl_int_set_si(neg->ineq[k][pos], -1);
7782 return isl_basic_set_finalize(neg);
7783 error:
7784 isl_basic_set_free(neg);
7785 return NULL;
7788 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7789 enum isl_dim_type type, unsigned first, unsigned n)
7791 int i;
7792 isl_basic_set *nonneg;
7793 isl_basic_set *neg;
7795 if (!set)
7796 return NULL;
7797 if (n == 0)
7798 return set;
7800 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7802 for (i = 0; i < n; ++i) {
7803 nonneg = nonneg_halfspace(isl_set_get_space(set),
7804 pos(set->dim, type) + first + i);
7805 neg = neg_halfspace(isl_set_get_space(set),
7806 pos(set->dim, type) + first + i);
7808 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7811 return set;
7812 error:
7813 isl_set_free(set);
7814 return NULL;
7817 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7818 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7819 void *user)
7821 isl_set *half;
7823 if (!set)
7824 return -1;
7825 if (isl_set_plain_is_empty(set)) {
7826 isl_set_free(set);
7827 return 0;
7829 if (first == len)
7830 return fn(set, signs, user);
7832 signs[first] = 1;
7833 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7834 1 + first));
7835 half = isl_set_intersect(half, isl_set_copy(set));
7836 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7837 goto error;
7839 signs[first] = -1;
7840 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7841 1 + first));
7842 half = isl_set_intersect(half, set);
7843 return foreach_orthant(half, signs, first + 1, len, fn, user);
7844 error:
7845 isl_set_free(set);
7846 return -1;
7849 /* Call "fn" on the intersections of "set" with each of the orthants
7850 * (except for obviously empty intersections). The orthant is identified
7851 * by the signs array, with each entry having value 1 or -1 according
7852 * to the sign of the corresponding variable.
7854 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7855 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7856 void *user)
7858 unsigned nparam;
7859 unsigned nvar;
7860 int *signs;
7861 int r;
7863 if (!set)
7864 return -1;
7865 if (isl_set_plain_is_empty(set))
7866 return 0;
7868 nparam = isl_set_dim(set, isl_dim_param);
7869 nvar = isl_set_dim(set, isl_dim_set);
7871 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7873 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7874 fn, user);
7876 free(signs);
7878 return r;
7881 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7883 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7886 int isl_basic_map_is_subset(
7887 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7889 int is_subset;
7890 struct isl_map *map1;
7891 struct isl_map *map2;
7893 if (!bmap1 || !bmap2)
7894 return -1;
7896 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7897 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7899 is_subset = isl_map_is_subset(map1, map2);
7901 isl_map_free(map1);
7902 isl_map_free(map2);
7904 return is_subset;
7907 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7908 __isl_keep isl_basic_set *bset2)
7910 return isl_basic_map_is_subset(bset1, bset2);
7913 int isl_basic_map_is_equal(
7914 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7916 int is_subset;
7918 if (!bmap1 || !bmap2)
7919 return -1;
7920 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7921 if (is_subset != 1)
7922 return is_subset;
7923 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7924 return is_subset;
7927 int isl_basic_set_is_equal(
7928 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7930 return isl_basic_map_is_equal(
7931 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7934 int isl_map_is_empty(struct isl_map *map)
7936 int i;
7937 int is_empty;
7939 if (!map)
7940 return -1;
7941 for (i = 0; i < map->n; ++i) {
7942 is_empty = isl_basic_map_is_empty(map->p[i]);
7943 if (is_empty < 0)
7944 return -1;
7945 if (!is_empty)
7946 return 0;
7948 return 1;
7951 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7953 return map ? map->n == 0 : -1;
7956 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7958 return isl_map_plain_is_empty(map);
7961 int isl_set_plain_is_empty(struct isl_set *set)
7963 return set ? set->n == 0 : -1;
7966 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7968 return isl_set_plain_is_empty(set);
7971 int isl_set_is_empty(struct isl_set *set)
7973 return isl_map_is_empty((struct isl_map *)set);
7976 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7978 if (!map1 || !map2)
7979 return -1;
7981 return isl_space_is_equal(map1->dim, map2->dim);
7984 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7986 if (!set1 || !set2)
7987 return -1;
7989 return isl_space_is_equal(set1->dim, set2->dim);
7992 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7994 int is_subset;
7996 if (!map1 || !map2)
7997 return -1;
7998 is_subset = isl_map_is_subset(map1, map2);
7999 if (is_subset != 1)
8000 return is_subset;
8001 is_subset = isl_map_is_subset(map2, map1);
8002 return is_subset;
8005 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8007 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8010 int isl_basic_map_is_strict_subset(
8011 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8013 int is_subset;
8015 if (!bmap1 || !bmap2)
8016 return -1;
8017 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8018 if (is_subset != 1)
8019 return is_subset;
8020 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8021 if (is_subset == -1)
8022 return is_subset;
8023 return !is_subset;
8026 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
8028 int is_subset;
8030 if (!map1 || !map2)
8031 return -1;
8032 is_subset = isl_map_is_subset(map1, map2);
8033 if (is_subset != 1)
8034 return is_subset;
8035 is_subset = isl_map_is_subset(map2, map1);
8036 if (is_subset == -1)
8037 return is_subset;
8038 return !is_subset;
8041 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8043 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
8046 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
8048 if (!bmap)
8049 return -1;
8050 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8053 int isl_basic_set_is_universe(struct isl_basic_set *bset)
8055 if (!bset)
8056 return -1;
8057 return bset->n_eq == 0 && bset->n_ineq == 0;
8060 int isl_map_plain_is_universe(__isl_keep isl_map *map)
8062 int i;
8064 if (!map)
8065 return -1;
8067 for (i = 0; i < map->n; ++i) {
8068 int r = isl_basic_map_is_universe(map->p[i]);
8069 if (r < 0 || r)
8070 return r;
8073 return 0;
8076 int isl_set_plain_is_universe(__isl_keep isl_set *set)
8078 return isl_map_plain_is_universe((isl_map *) set);
8081 int isl_set_fast_is_universe(__isl_keep isl_set *set)
8083 return isl_set_plain_is_universe(set);
8086 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
8088 struct isl_basic_set *bset = NULL;
8089 struct isl_vec *sample = NULL;
8090 int empty;
8091 unsigned total;
8093 if (!bmap)
8094 return -1;
8096 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8097 return 1;
8099 if (isl_basic_map_is_universe(bmap))
8100 return 0;
8102 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8103 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8104 copy = isl_basic_map_remove_redundancies(copy);
8105 empty = isl_basic_map_plain_is_empty(copy);
8106 isl_basic_map_free(copy);
8107 return empty;
8110 total = 1 + isl_basic_map_total_dim(bmap);
8111 if (bmap->sample && bmap->sample->size == total) {
8112 int contains = isl_basic_map_contains(bmap, bmap->sample);
8113 if (contains < 0)
8114 return -1;
8115 if (contains)
8116 return 0;
8118 isl_vec_free(bmap->sample);
8119 bmap->sample = NULL;
8120 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8121 if (!bset)
8122 return -1;
8123 sample = isl_basic_set_sample_vec(bset);
8124 if (!sample)
8125 return -1;
8126 empty = sample->size == 0;
8127 isl_vec_free(bmap->sample);
8128 bmap->sample = sample;
8129 if (empty)
8130 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8132 return empty;
8135 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8137 if (!bmap)
8138 return -1;
8139 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8142 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
8144 return isl_basic_map_plain_is_empty(bmap);
8147 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8149 if (!bset)
8150 return -1;
8151 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8154 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
8156 return isl_basic_set_plain_is_empty(bset);
8159 int isl_basic_set_is_empty(struct isl_basic_set *bset)
8161 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8164 struct isl_map *isl_basic_map_union(
8165 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8167 struct isl_map *map;
8168 if (!bmap1 || !bmap2)
8169 goto error;
8171 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8173 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8174 if (!map)
8175 goto error;
8176 map = isl_map_add_basic_map(map, bmap1);
8177 map = isl_map_add_basic_map(map, bmap2);
8178 return map;
8179 error:
8180 isl_basic_map_free(bmap1);
8181 isl_basic_map_free(bmap2);
8182 return NULL;
8185 struct isl_set *isl_basic_set_union(
8186 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8188 return (struct isl_set *)isl_basic_map_union(
8189 (struct isl_basic_map *)bset1,
8190 (struct isl_basic_map *)bset2);
8193 /* Order divs such that any div only depends on previous divs */
8194 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8196 int i;
8197 unsigned off;
8199 if (!bmap)
8200 return NULL;
8202 off = isl_space_dim(bmap->dim, isl_dim_all);
8204 for (i = 0; i < bmap->n_div; ++i) {
8205 int pos;
8206 if (isl_int_is_zero(bmap->div[i][0]))
8207 continue;
8208 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8209 bmap->n_div-i);
8210 if (pos == -1)
8211 continue;
8212 isl_basic_map_swap_div(bmap, i, i + pos);
8213 --i;
8215 return bmap;
8218 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8220 return (struct isl_basic_set *)
8221 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8224 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8226 int i;
8228 if (!map)
8229 return 0;
8231 for (i = 0; i < map->n; ++i) {
8232 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8233 if (!map->p[i])
8234 goto error;
8237 return map;
8238 error:
8239 isl_map_free(map);
8240 return NULL;
8243 /* Apply the expansion computed by isl_merge_divs.
8244 * The expansion itself is given by "exp" while the resulting
8245 * list of divs is given by "div".
8247 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8248 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8250 int i, j;
8251 int n_div;
8253 bset = isl_basic_set_cow(bset);
8254 if (!bset || !div)
8255 goto error;
8257 if (div->n_row < bset->n_div)
8258 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8259 "not an expansion", goto error);
8261 n_div = bset->n_div;
8262 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8263 div->n_row - n_div, 0,
8264 2 * (div->n_row - n_div));
8266 for (i = n_div; i < div->n_row; ++i)
8267 if (isl_basic_set_alloc_div(bset) < 0)
8268 goto error;
8270 j = n_div - 1;
8271 for (i = div->n_row - 1; i >= 0; --i) {
8272 if (j >= 0 && exp[j] == i) {
8273 if (i != j)
8274 isl_basic_map_swap_div(bset, i, j);
8275 j--;
8276 } else {
8277 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8278 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8279 goto error;
8283 isl_mat_free(div);
8284 return bset;
8285 error:
8286 isl_basic_set_free(bset);
8287 isl_mat_free(div);
8288 return NULL;
8291 /* Look for a div in dst that corresponds to the div "div" in src.
8292 * The divs before "div" in src and dst are assumed to be the same.
8294 * Returns -1 if no corresponding div was found and the position
8295 * of the corresponding div in dst otherwise.
8297 static int find_div(struct isl_basic_map *dst,
8298 struct isl_basic_map *src, unsigned div)
8300 int i;
8302 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8304 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8305 for (i = div; i < dst->n_div; ++i)
8306 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8307 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8308 dst->n_div - div) == -1)
8309 return i;
8310 return -1;
8313 /* Align the divs of "dst" to those of "src", adding divs from "src"
8314 * if needed. That is, make sure that the first src->n_div divs
8315 * of the result are equal to those of src.
8317 * The result is not finalized as by design it will have redundant
8318 * divs if any divs from "src" were copied.
8320 __isl_give isl_basic_map *isl_basic_map_align_divs(
8321 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8323 int i;
8324 int known, extended;
8325 unsigned total;
8327 if (!dst || !src)
8328 return isl_basic_map_free(dst);
8330 if (src->n_div == 0)
8331 return dst;
8333 known = isl_basic_map_divs_known(src);
8334 if (known < 0)
8335 return isl_basic_map_free(dst);
8336 if (!known)
8337 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8338 "some src divs are unknown",
8339 return isl_basic_map_free(dst));
8341 src = isl_basic_map_order_divs(src);
8343 extended = 0;
8344 total = isl_space_dim(src->dim, isl_dim_all);
8345 for (i = 0; i < src->n_div; ++i) {
8346 int j = find_div(dst, src, i);
8347 if (j < 0) {
8348 if (!extended) {
8349 int extra = src->n_div - i;
8350 dst = isl_basic_map_cow(dst);
8351 if (!dst)
8352 return NULL;
8353 dst = isl_basic_map_extend_space(dst,
8354 isl_space_copy(dst->dim),
8355 extra, 0, 2 * extra);
8356 extended = 1;
8358 j = isl_basic_map_alloc_div(dst);
8359 if (j < 0)
8360 return isl_basic_map_free(dst);
8361 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8362 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8363 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8364 return isl_basic_map_free(dst);
8366 if (j != i)
8367 isl_basic_map_swap_div(dst, i, j);
8369 return dst;
8372 struct isl_basic_set *isl_basic_set_align_divs(
8373 struct isl_basic_set *dst, struct isl_basic_set *src)
8375 return (struct isl_basic_set *)isl_basic_map_align_divs(
8376 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8379 struct isl_map *isl_map_align_divs(struct isl_map *map)
8381 int i;
8383 if (!map)
8384 return NULL;
8385 if (map->n == 0)
8386 return map;
8387 map = isl_map_compute_divs(map);
8388 map = isl_map_cow(map);
8389 if (!map)
8390 return NULL;
8392 for (i = 1; i < map->n; ++i)
8393 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8394 for (i = 1; i < map->n; ++i) {
8395 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8396 if (!map->p[i])
8397 return isl_map_free(map);
8400 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8401 return map;
8404 struct isl_set *isl_set_align_divs(struct isl_set *set)
8406 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8409 /* Align the divs of the basic maps in "map" to those
8410 * of the basic maps in "list", as well as to the other basic maps in "map".
8411 * The elements in "list" are assumed to have known divs.
8413 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8414 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8416 int i, n;
8418 map = isl_map_compute_divs(map);
8419 map = isl_map_cow(map);
8420 if (!map || !list)
8421 return isl_map_free(map);
8422 if (map->n == 0)
8423 return map;
8425 n = isl_basic_map_list_n_basic_map(list);
8426 for (i = 0; i < n; ++i) {
8427 isl_basic_map *bmap;
8429 bmap = isl_basic_map_list_get_basic_map(list, i);
8430 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8431 isl_basic_map_free(bmap);
8433 if (!map->p[0])
8434 return isl_map_free(map);
8436 return isl_map_align_divs(map);
8439 /* Align the divs of each element of "list" to those of "bmap".
8440 * Both "bmap" and the elements of "list" are assumed to have known divs.
8442 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8443 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8445 int i, n;
8447 if (!list || !bmap)
8448 return isl_basic_map_list_free(list);
8450 n = isl_basic_map_list_n_basic_map(list);
8451 for (i = 0; i < n; ++i) {
8452 isl_basic_map *bmap_i;
8454 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8455 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8456 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8459 return list;
8462 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8463 __isl_take isl_map *map)
8465 if (!set || !map)
8466 goto error;
8467 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8468 map = isl_map_intersect_domain(map, set);
8469 set = isl_map_range(map);
8470 return set;
8471 error:
8472 isl_set_free(set);
8473 isl_map_free(map);
8474 return NULL;
8477 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8478 __isl_take isl_map *map)
8480 return isl_map_align_params_map_map_and(set, map, &set_apply);
8483 /* There is no need to cow as removing empty parts doesn't change
8484 * the meaning of the set.
8486 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8488 int i;
8490 if (!map)
8491 return NULL;
8493 for (i = map->n - 1; i >= 0; --i)
8494 remove_if_empty(map, i);
8496 return map;
8499 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8501 return (struct isl_set *)
8502 isl_map_remove_empty_parts((struct isl_map *)set);
8505 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8507 struct isl_basic_map *bmap;
8508 if (!map || map->n == 0)
8509 return NULL;
8510 bmap = map->p[map->n-1];
8511 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8512 return isl_basic_map_copy(bmap);
8515 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8517 return (struct isl_basic_set *)
8518 isl_map_copy_basic_map((struct isl_map *)set);
8521 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8522 __isl_keep isl_basic_map *bmap)
8524 int i;
8526 if (!map || !bmap)
8527 goto error;
8528 for (i = map->n-1; i >= 0; --i) {
8529 if (map->p[i] != bmap)
8530 continue;
8531 map = isl_map_cow(map);
8532 if (!map)
8533 goto error;
8534 isl_basic_map_free(map->p[i]);
8535 if (i != map->n-1) {
8536 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8537 map->p[i] = map->p[map->n-1];
8539 map->n--;
8540 return map;
8542 return map;
8543 error:
8544 isl_map_free(map);
8545 return NULL;
8548 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8549 struct isl_basic_set *bset)
8551 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8552 (struct isl_basic_map *)bset);
8555 /* Given two basic sets bset1 and bset2, compute the maximal difference
8556 * between the values of dimension pos in bset1 and those in bset2
8557 * for any common value of the parameters and dimensions preceding pos.
8559 static enum isl_lp_result basic_set_maximal_difference_at(
8560 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8561 int pos, isl_int *opt)
8563 isl_space *dims;
8564 struct isl_basic_map *bmap1 = NULL;
8565 struct isl_basic_map *bmap2 = NULL;
8566 struct isl_ctx *ctx;
8567 struct isl_vec *obj;
8568 unsigned total;
8569 unsigned nparam;
8570 unsigned dim1, dim2;
8571 enum isl_lp_result res;
8573 if (!bset1 || !bset2)
8574 return isl_lp_error;
8576 nparam = isl_basic_set_n_param(bset1);
8577 dim1 = isl_basic_set_n_dim(bset1);
8578 dim2 = isl_basic_set_n_dim(bset2);
8579 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8580 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8581 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8582 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8583 if (!bmap1 || !bmap2)
8584 goto error;
8585 bmap1 = isl_basic_map_cow(bmap1);
8586 bmap1 = isl_basic_map_extend(bmap1, nparam,
8587 pos, (dim1 - pos) + (dim2 - pos),
8588 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8589 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8590 if (!bmap1)
8591 goto error2;
8592 total = isl_basic_map_total_dim(bmap1);
8593 ctx = bmap1->ctx;
8594 obj = isl_vec_alloc(ctx, 1 + total);
8595 if (!obj)
8596 goto error2;
8597 isl_seq_clr(obj->block.data, 1 + total);
8598 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8599 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8600 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8601 opt, NULL, NULL);
8602 isl_basic_map_free(bmap1);
8603 isl_vec_free(obj);
8604 return res;
8605 error:
8606 isl_basic_map_free(bmap2);
8607 error2:
8608 isl_basic_map_free(bmap1);
8609 return isl_lp_error;
8612 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8613 * for any common value of the parameters and dimensions preceding pos
8614 * in both basic sets, the values of dimension pos in bset1 are
8615 * smaller or larger than those in bset2.
8617 * Returns
8618 * 1 if bset1 follows bset2
8619 * -1 if bset1 precedes bset2
8620 * 0 if bset1 and bset2 are incomparable
8621 * -2 if some error occurred.
8623 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8624 struct isl_basic_set *bset2, int pos)
8626 isl_int opt;
8627 enum isl_lp_result res;
8628 int cmp;
8630 isl_int_init(opt);
8632 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8634 if (res == isl_lp_empty)
8635 cmp = 0;
8636 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8637 res == isl_lp_unbounded)
8638 cmp = 1;
8639 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8640 cmp = -1;
8641 else
8642 cmp = -2;
8644 isl_int_clear(opt);
8645 return cmp;
8648 /* Given two basic sets bset1 and bset2, check whether
8649 * for any common value of the parameters and dimensions preceding pos
8650 * there is a value of dimension pos in bset1 that is larger
8651 * than a value of the same dimension in bset2.
8653 * Return
8654 * 1 if there exists such a pair
8655 * 0 if there is no such pair, but there is a pair of equal values
8656 * -1 otherwise
8657 * -2 if some error occurred.
8659 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8660 __isl_keep isl_basic_set *bset2, int pos)
8662 isl_int opt;
8663 enum isl_lp_result res;
8664 int cmp;
8666 isl_int_init(opt);
8668 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8670 if (res == isl_lp_empty)
8671 cmp = -1;
8672 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8673 res == isl_lp_unbounded)
8674 cmp = 1;
8675 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8676 cmp = -1;
8677 else if (res == isl_lp_ok)
8678 cmp = 0;
8679 else
8680 cmp = -2;
8682 isl_int_clear(opt);
8683 return cmp;
8686 /* Given two sets set1 and set2, check whether
8687 * for any common value of the parameters and dimensions preceding pos
8688 * there is a value of dimension pos in set1 that is larger
8689 * than a value of the same dimension in set2.
8691 * Return
8692 * 1 if there exists such a pair
8693 * 0 if there is no such pair, but there is a pair of equal values
8694 * -1 otherwise
8695 * -2 if some error occurred.
8697 int isl_set_follows_at(__isl_keep isl_set *set1,
8698 __isl_keep isl_set *set2, int pos)
8700 int i, j;
8701 int follows = -1;
8703 if (!set1 || !set2)
8704 return -2;
8706 for (i = 0; i < set1->n; ++i)
8707 for (j = 0; j < set2->n; ++j) {
8708 int f;
8709 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8710 if (f == 1 || f == -2)
8711 return f;
8712 if (f > follows)
8713 follows = f;
8716 return follows;
8719 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8720 unsigned pos, isl_int *val)
8722 int i;
8723 int d;
8724 unsigned total;
8726 if (!bmap)
8727 return -1;
8728 total = isl_basic_map_total_dim(bmap);
8729 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8730 for (; d+1 > pos; --d)
8731 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8732 break;
8733 if (d != pos)
8734 continue;
8735 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8736 return 0;
8737 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8738 return 0;
8739 if (!isl_int_is_one(bmap->eq[i][1+d]))
8740 return 0;
8741 if (val)
8742 isl_int_neg(*val, bmap->eq[i][0]);
8743 return 1;
8745 return 0;
8748 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8749 unsigned pos, isl_int *val)
8751 int i;
8752 isl_int v;
8753 isl_int tmp;
8754 int fixed;
8756 if (!map)
8757 return -1;
8758 if (map->n == 0)
8759 return 0;
8760 if (map->n == 1)
8761 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8762 isl_int_init(v);
8763 isl_int_init(tmp);
8764 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8765 for (i = 1; fixed == 1 && i < map->n; ++i) {
8766 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8767 if (fixed == 1 && isl_int_ne(tmp, v))
8768 fixed = 0;
8770 if (val)
8771 isl_int_set(*val, v);
8772 isl_int_clear(tmp);
8773 isl_int_clear(v);
8774 return fixed;
8777 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8778 unsigned pos, isl_int *val)
8780 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8781 pos, val);
8784 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8785 isl_int *val)
8787 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8790 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8791 enum isl_dim_type type, unsigned pos, isl_int *val)
8793 if (pos >= isl_basic_map_dim(bmap, type))
8794 return -1;
8795 return isl_basic_map_plain_has_fixed_var(bmap,
8796 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8799 /* If "bmap" obviously lies on a hyperplane where the given dimension
8800 * has a fixed value, then return that value.
8801 * Otherwise return NaN.
8803 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8804 __isl_keep isl_basic_map *bmap,
8805 enum isl_dim_type type, unsigned pos)
8807 isl_ctx *ctx;
8808 isl_val *v;
8809 int fixed;
8811 if (!bmap)
8812 return NULL;
8813 ctx = isl_basic_map_get_ctx(bmap);
8814 v = isl_val_alloc(ctx);
8815 if (!v)
8816 return NULL;
8817 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8818 if (fixed < 0)
8819 return isl_val_free(v);
8820 if (fixed) {
8821 isl_int_set_si(v->d, 1);
8822 return v;
8824 isl_val_free(v);
8825 return isl_val_nan(ctx);
8828 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8829 enum isl_dim_type type, unsigned pos, isl_int *val)
8831 if (pos >= isl_map_dim(map, type))
8832 return -1;
8833 return isl_map_plain_has_fixed_var(map,
8834 map_offset(map, type) - 1 + pos, val);
8837 /* If "map" obviously lies on a hyperplane where the given dimension
8838 * has a fixed value, then return that value.
8839 * Otherwise return NaN.
8841 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8842 enum isl_dim_type type, unsigned pos)
8844 isl_ctx *ctx;
8845 isl_val *v;
8846 int fixed;
8848 if (!map)
8849 return NULL;
8850 ctx = isl_map_get_ctx(map);
8851 v = isl_val_alloc(ctx);
8852 if (!v)
8853 return NULL;
8854 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8855 if (fixed < 0)
8856 return isl_val_free(v);
8857 if (fixed) {
8858 isl_int_set_si(v->d, 1);
8859 return v;
8861 isl_val_free(v);
8862 return isl_val_nan(ctx);
8865 /* If "set" obviously lies on a hyperplane where the given dimension
8866 * has a fixed value, then return that value.
8867 * Otherwise return NaN.
8869 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8870 enum isl_dim_type type, unsigned pos)
8872 return isl_map_plain_get_val_if_fixed(set, type, pos);
8875 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8876 enum isl_dim_type type, unsigned pos, isl_int *val)
8878 return isl_map_plain_is_fixed(set, type, pos, val);
8881 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8882 enum isl_dim_type type, unsigned pos, isl_int *val)
8884 return isl_map_plain_is_fixed(map, type, pos, val);
8887 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8888 * then return this fixed value in *val.
8890 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8891 unsigned dim, isl_int *val)
8893 return isl_basic_set_plain_has_fixed_var(bset,
8894 isl_basic_set_n_param(bset) + dim, val);
8897 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8898 * then return this fixed value in *val.
8900 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8901 unsigned dim, isl_int *val)
8903 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8906 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8907 unsigned dim, isl_int *val)
8909 return isl_set_plain_dim_is_fixed(set, dim, val);
8912 /* Check if input variable in has fixed value and if so and if val is not NULL,
8913 * then return this fixed value in *val.
8915 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8916 unsigned in, isl_int *val)
8918 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8921 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8922 * and if val is not NULL, then return this lower bound in *val.
8924 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8925 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8927 int i, i_eq = -1, i_ineq = -1;
8928 isl_int *c;
8929 unsigned total;
8930 unsigned nparam;
8932 if (!bset)
8933 return -1;
8934 total = isl_basic_set_total_dim(bset);
8935 nparam = isl_basic_set_n_param(bset);
8936 for (i = 0; i < bset->n_eq; ++i) {
8937 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8938 continue;
8939 if (i_eq != -1)
8940 return 0;
8941 i_eq = i;
8943 for (i = 0; i < bset->n_ineq; ++i) {
8944 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8945 continue;
8946 if (i_eq != -1 || i_ineq != -1)
8947 return 0;
8948 i_ineq = i;
8950 if (i_eq == -1 && i_ineq == -1)
8951 return 0;
8952 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8953 /* The coefficient should always be one due to normalization. */
8954 if (!isl_int_is_one(c[1+nparam+dim]))
8955 return 0;
8956 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8957 return 0;
8958 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8959 total - nparam - dim - 1) != -1)
8960 return 0;
8961 if (val)
8962 isl_int_neg(*val, c[0]);
8963 return 1;
8966 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8967 unsigned dim, isl_int *val)
8969 int i;
8970 isl_int v;
8971 isl_int tmp;
8972 int fixed;
8974 if (!set)
8975 return -1;
8976 if (set->n == 0)
8977 return 0;
8978 if (set->n == 1)
8979 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8980 dim, val);
8981 isl_int_init(v);
8982 isl_int_init(tmp);
8983 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8984 dim, &v);
8985 for (i = 1; fixed == 1 && i < set->n; ++i) {
8986 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8987 dim, &tmp);
8988 if (fixed == 1 && isl_int_ne(tmp, v))
8989 fixed = 0;
8991 if (val)
8992 isl_int_set(*val, v);
8993 isl_int_clear(tmp);
8994 isl_int_clear(v);
8995 return fixed;
8998 /* uset_gist depends on constraints without existentially quantified
8999 * variables sorting first.
9001 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9003 isl_int **c1 = (isl_int **) p1;
9004 isl_int **c2 = (isl_int **) p2;
9005 int l1, l2;
9006 unsigned size = *(unsigned *) arg;
9008 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9009 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9011 if (l1 != l2)
9012 return l1 - l2;
9014 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9017 static struct isl_basic_map *isl_basic_map_sort_constraints(
9018 struct isl_basic_map *bmap)
9020 unsigned total;
9022 if (!bmap)
9023 return NULL;
9024 if (bmap->n_ineq == 0)
9025 return bmap;
9026 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9027 return bmap;
9028 total = isl_basic_map_total_dim(bmap);
9029 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9030 &sort_constraint_cmp, &total) < 0)
9031 return isl_basic_map_free(bmap);
9032 return bmap;
9035 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9036 __isl_take isl_basic_set *bset)
9038 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9039 (struct isl_basic_map *)bset);
9042 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9044 if (!bmap)
9045 return NULL;
9046 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9047 return bmap;
9048 bmap = isl_basic_map_remove_redundancies(bmap);
9049 bmap = isl_basic_map_sort_constraints(bmap);
9050 if (bmap)
9051 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9052 return bmap;
9055 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9057 return (struct isl_basic_set *)isl_basic_map_normalize(
9058 (struct isl_basic_map *)bset);
9061 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9062 const __isl_keep isl_basic_map *bmap2)
9064 int i, cmp;
9065 unsigned total;
9067 if (bmap1 == bmap2)
9068 return 0;
9069 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9070 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9071 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9072 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9073 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9074 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9075 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9076 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9077 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9078 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9079 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9080 return 0;
9081 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9082 return 1;
9083 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9084 return -1;
9085 if (bmap1->n_eq != bmap2->n_eq)
9086 return bmap1->n_eq - bmap2->n_eq;
9087 if (bmap1->n_ineq != bmap2->n_ineq)
9088 return bmap1->n_ineq - bmap2->n_ineq;
9089 if (bmap1->n_div != bmap2->n_div)
9090 return bmap1->n_div - bmap2->n_div;
9091 total = isl_basic_map_total_dim(bmap1);
9092 for (i = 0; i < bmap1->n_eq; ++i) {
9093 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9094 if (cmp)
9095 return cmp;
9097 for (i = 0; i < bmap1->n_ineq; ++i) {
9098 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9099 if (cmp)
9100 return cmp;
9102 for (i = 0; i < bmap1->n_div; ++i) {
9103 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9104 if (cmp)
9105 return cmp;
9107 return 0;
9110 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9111 const __isl_keep isl_basic_set *bset2)
9113 return isl_basic_map_plain_cmp(bset1, bset2);
9116 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9118 int i, cmp;
9120 if (set1 == set2)
9121 return 0;
9122 if (set1->n != set2->n)
9123 return set1->n - set2->n;
9125 for (i = 0; i < set1->n; ++i) {
9126 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9127 if (cmp)
9128 return cmp;
9131 return 0;
9134 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9135 __isl_keep isl_basic_map *bmap2)
9137 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9140 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9141 __isl_keep isl_basic_set *bset2)
9143 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9144 (isl_basic_map *)bset2);
9147 static int qsort_bmap_cmp(const void *p1, const void *p2)
9149 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9150 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9152 return isl_basic_map_plain_cmp(bmap1, bmap2);
9155 /* Sort the basic maps of "map" and remove duplicate basic maps.
9157 * While removing basic maps, we make sure that the basic maps remain
9158 * sorted because isl_map_normalize expects the basic maps of the result
9159 * to be sorted.
9161 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9163 int i, j;
9165 map = isl_map_remove_empty_parts(map);
9166 if (!map)
9167 return NULL;
9168 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9169 for (i = map->n - 1; i >= 1; --i) {
9170 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9171 continue;
9172 isl_basic_map_free(map->p[i-1]);
9173 for (j = i; j < map->n; ++j)
9174 map->p[j - 1] = map->p[j];
9175 map->n--;
9178 return map;
9181 /* Remove obvious duplicates among the basic maps of "map".
9183 * Unlike isl_map_normalize, this function does not remove redundant
9184 * constraints and only removes duplicates that have exactly the same
9185 * constraints in the input. It does sort the constraints and
9186 * the basic maps to ease the detection of duplicates.
9188 * If "map" has already been normalized or if the basic maps are
9189 * disjoint, then there can be no duplicates.
9191 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9193 int i;
9194 isl_basic_map *bmap;
9196 if (!map)
9197 return NULL;
9198 if (map->n <= 1)
9199 return map;
9200 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9201 return map;
9202 for (i = 0; i < map->n; ++i) {
9203 bmap = isl_basic_map_copy(map->p[i]);
9204 bmap = isl_basic_map_sort_constraints(bmap);
9205 if (!bmap)
9206 return isl_map_free(map);
9207 isl_basic_map_free(map->p[i]);
9208 map->p[i] = bmap;
9211 map = sort_and_remove_duplicates(map);
9212 return map;
9215 /* We normalize in place, but if anything goes wrong we need
9216 * to return NULL, so we need to make sure we don't change the
9217 * meaning of any possible other copies of map.
9219 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9221 int i;
9222 struct isl_basic_map *bmap;
9224 if (!map)
9225 return NULL;
9226 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9227 return map;
9228 for (i = 0; i < map->n; ++i) {
9229 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9230 if (!bmap)
9231 goto error;
9232 isl_basic_map_free(map->p[i]);
9233 map->p[i] = bmap;
9236 map = sort_and_remove_duplicates(map);
9237 if (map)
9238 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9239 return map;
9240 error:
9241 isl_map_free(map);
9242 return NULL;
9245 struct isl_set *isl_set_normalize(struct isl_set *set)
9247 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9250 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9252 int i;
9253 int equal;
9255 if (!map1 || !map2)
9256 return -1;
9258 if (map1 == map2)
9259 return 1;
9260 if (!isl_space_is_equal(map1->dim, map2->dim))
9261 return 0;
9263 map1 = isl_map_copy(map1);
9264 map2 = isl_map_copy(map2);
9265 map1 = isl_map_normalize(map1);
9266 map2 = isl_map_normalize(map2);
9267 if (!map1 || !map2)
9268 goto error;
9269 equal = map1->n == map2->n;
9270 for (i = 0; equal && i < map1->n; ++i) {
9271 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9272 if (equal < 0)
9273 goto error;
9275 isl_map_free(map1);
9276 isl_map_free(map2);
9277 return equal;
9278 error:
9279 isl_map_free(map1);
9280 isl_map_free(map2);
9281 return -1;
9284 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9286 return isl_map_plain_is_equal(map1, map2);
9289 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9291 return isl_map_plain_is_equal((struct isl_map *)set1,
9292 (struct isl_map *)set2);
9295 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9297 return isl_set_plain_is_equal(set1, set2);
9300 /* Return an interval that ranges from min to max (inclusive)
9302 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9303 isl_int min, isl_int max)
9305 int k;
9306 struct isl_basic_set *bset = NULL;
9308 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9309 if (!bset)
9310 goto error;
9312 k = isl_basic_set_alloc_inequality(bset);
9313 if (k < 0)
9314 goto error;
9315 isl_int_set_si(bset->ineq[k][1], 1);
9316 isl_int_neg(bset->ineq[k][0], min);
9318 k = isl_basic_set_alloc_inequality(bset);
9319 if (k < 0)
9320 goto error;
9321 isl_int_set_si(bset->ineq[k][1], -1);
9322 isl_int_set(bset->ineq[k][0], max);
9324 return bset;
9325 error:
9326 isl_basic_set_free(bset);
9327 return NULL;
9330 /* Return the basic maps in "map" as a list.
9332 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9333 __isl_keep isl_map *map)
9335 int i;
9336 isl_ctx *ctx;
9337 isl_basic_map_list *list;
9339 if (!map)
9340 return NULL;
9341 ctx = isl_map_get_ctx(map);
9342 list = isl_basic_map_list_alloc(ctx, map->n);
9344 for (i = 0; i < map->n; ++i) {
9345 isl_basic_map *bmap;
9347 bmap = isl_basic_map_copy(map->p[i]);
9348 list = isl_basic_map_list_add(list, bmap);
9351 return list;
9354 /* Return the intersection of the elements in the non-empty list "list".
9355 * All elements are assumed to live in the same space.
9357 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9358 __isl_take isl_basic_map_list *list)
9360 int i, n;
9361 isl_basic_map *bmap;
9363 if (!list)
9364 return NULL;
9365 n = isl_basic_map_list_n_basic_map(list);
9366 if (n < 1)
9367 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9368 "expecting non-empty list", goto error);
9370 bmap = isl_basic_map_list_get_basic_map(list, 0);
9371 for (i = 1; i < n; ++i) {
9372 isl_basic_map *bmap_i;
9374 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9375 bmap = isl_basic_map_intersect(bmap, bmap_i);
9378 isl_basic_map_list_free(list);
9379 return bmap;
9380 error:
9381 isl_basic_map_list_free(list);
9382 return NULL;
9385 /* Return the intersection of the elements in the non-empty list "list".
9386 * All elements are assumed to live in the same space.
9388 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9389 __isl_take isl_basic_set_list *list)
9391 return isl_basic_map_list_intersect(list);
9394 /* Return the Cartesian product of the basic sets in list (in the given order).
9396 __isl_give isl_basic_set *isl_basic_set_list_product(
9397 __isl_take struct isl_basic_set_list *list)
9399 int i;
9400 unsigned dim;
9401 unsigned nparam;
9402 unsigned extra;
9403 unsigned n_eq;
9404 unsigned n_ineq;
9405 struct isl_basic_set *product = NULL;
9407 if (!list)
9408 goto error;
9409 isl_assert(list->ctx, list->n > 0, goto error);
9410 isl_assert(list->ctx, list->p[0], goto error);
9411 nparam = isl_basic_set_n_param(list->p[0]);
9412 dim = isl_basic_set_n_dim(list->p[0]);
9413 extra = list->p[0]->n_div;
9414 n_eq = list->p[0]->n_eq;
9415 n_ineq = list->p[0]->n_ineq;
9416 for (i = 1; i < list->n; ++i) {
9417 isl_assert(list->ctx, list->p[i], goto error);
9418 isl_assert(list->ctx,
9419 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9420 dim += isl_basic_set_n_dim(list->p[i]);
9421 extra += list->p[i]->n_div;
9422 n_eq += list->p[i]->n_eq;
9423 n_ineq += list->p[i]->n_ineq;
9425 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9426 n_eq, n_ineq);
9427 if (!product)
9428 goto error;
9429 dim = 0;
9430 for (i = 0; i < list->n; ++i) {
9431 isl_basic_set_add_constraints(product,
9432 isl_basic_set_copy(list->p[i]), dim);
9433 dim += isl_basic_set_n_dim(list->p[i]);
9435 isl_basic_set_list_free(list);
9436 return product;
9437 error:
9438 isl_basic_set_free(product);
9439 isl_basic_set_list_free(list);
9440 return NULL;
9443 struct isl_basic_map *isl_basic_map_product(
9444 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9446 isl_space *dim_result = NULL;
9447 struct isl_basic_map *bmap;
9448 unsigned in1, in2, out1, out2, nparam, total, pos;
9449 struct isl_dim_map *dim_map1, *dim_map2;
9451 if (!bmap1 || !bmap2)
9452 goto error;
9454 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9455 bmap2->dim, isl_dim_param), goto error);
9456 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9457 isl_space_copy(bmap2->dim));
9459 in1 = isl_basic_map_n_in(bmap1);
9460 in2 = isl_basic_map_n_in(bmap2);
9461 out1 = isl_basic_map_n_out(bmap1);
9462 out2 = isl_basic_map_n_out(bmap2);
9463 nparam = isl_basic_map_n_param(bmap1);
9465 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9466 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9467 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9468 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9469 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9470 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9471 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9472 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9473 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9474 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9475 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9477 bmap = isl_basic_map_alloc_space(dim_result,
9478 bmap1->n_div + bmap2->n_div,
9479 bmap1->n_eq + bmap2->n_eq,
9480 bmap1->n_ineq + bmap2->n_ineq);
9481 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9482 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9483 bmap = isl_basic_map_simplify(bmap);
9484 return isl_basic_map_finalize(bmap);
9485 error:
9486 isl_basic_map_free(bmap1);
9487 isl_basic_map_free(bmap2);
9488 return NULL;
9491 __isl_give isl_basic_map *isl_basic_map_flat_product(
9492 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9494 isl_basic_map *prod;
9496 prod = isl_basic_map_product(bmap1, bmap2);
9497 prod = isl_basic_map_flatten(prod);
9498 return prod;
9501 __isl_give isl_basic_set *isl_basic_set_flat_product(
9502 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9504 return isl_basic_map_flat_range_product(bset1, bset2);
9507 __isl_give isl_basic_map *isl_basic_map_domain_product(
9508 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9510 isl_space *space_result = NULL;
9511 isl_basic_map *bmap;
9512 unsigned in1, in2, out, nparam, total, pos;
9513 struct isl_dim_map *dim_map1, *dim_map2;
9515 if (!bmap1 || !bmap2)
9516 goto error;
9518 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9519 isl_space_copy(bmap2->dim));
9521 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9522 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9523 out = isl_basic_map_dim(bmap1, isl_dim_out);
9524 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9526 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9527 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9528 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9529 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9530 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9531 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9532 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9533 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9534 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9535 isl_dim_map_div(dim_map1, bmap1, pos += out);
9536 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9538 bmap = isl_basic_map_alloc_space(space_result,
9539 bmap1->n_div + bmap2->n_div,
9540 bmap1->n_eq + bmap2->n_eq,
9541 bmap1->n_ineq + bmap2->n_ineq);
9542 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9543 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9544 bmap = isl_basic_map_simplify(bmap);
9545 return isl_basic_map_finalize(bmap);
9546 error:
9547 isl_basic_map_free(bmap1);
9548 isl_basic_map_free(bmap2);
9549 return NULL;
9552 __isl_give isl_basic_map *isl_basic_map_range_product(
9553 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9555 isl_space *dim_result = NULL;
9556 isl_basic_map *bmap;
9557 unsigned in, out1, out2, nparam, total, pos;
9558 struct isl_dim_map *dim_map1, *dim_map2;
9560 if (!bmap1 || !bmap2)
9561 goto error;
9563 if (!isl_space_match(bmap1->dim, isl_dim_param,
9564 bmap2->dim, isl_dim_param))
9565 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9566 "parameters don't match", goto error);
9568 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9569 isl_space_copy(bmap2->dim));
9571 in = isl_basic_map_dim(bmap1, isl_dim_in);
9572 out1 = isl_basic_map_n_out(bmap1);
9573 out2 = isl_basic_map_n_out(bmap2);
9574 nparam = isl_basic_map_n_param(bmap1);
9576 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9577 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9578 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9579 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9580 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9581 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9582 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9583 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9584 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9585 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9586 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9588 bmap = isl_basic_map_alloc_space(dim_result,
9589 bmap1->n_div + bmap2->n_div,
9590 bmap1->n_eq + bmap2->n_eq,
9591 bmap1->n_ineq + bmap2->n_ineq);
9592 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9593 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9594 bmap = isl_basic_map_simplify(bmap);
9595 return isl_basic_map_finalize(bmap);
9596 error:
9597 isl_basic_map_free(bmap1);
9598 isl_basic_map_free(bmap2);
9599 return NULL;
9602 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9603 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9605 isl_basic_map *prod;
9607 prod = isl_basic_map_range_product(bmap1, bmap2);
9608 prod = isl_basic_map_flatten_range(prod);
9609 return prod;
9612 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9613 * and collect the results.
9614 * The result live in the space obtained by calling "space_product"
9615 * on the spaces of "map1" and "map2".
9616 * If "remove_duplicates" is set then the result may contain duplicates
9617 * (even if the inputs do not) and so we try and remove the obvious
9618 * duplicates.
9620 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9621 __isl_take isl_map *map2,
9622 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9623 __isl_take isl_space *right),
9624 __isl_give isl_basic_map *(*basic_map_product)(
9625 __isl_take isl_basic_map *left,
9626 __isl_take isl_basic_map *right),
9627 int remove_duplicates)
9629 unsigned flags = 0;
9630 struct isl_map *result;
9631 int i, j;
9633 if (!map1 || !map2)
9634 goto error;
9636 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9637 map2->dim, isl_dim_param), goto error);
9639 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9640 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9641 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9643 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9644 isl_space_copy(map2->dim)),
9645 map1->n * map2->n, flags);
9646 if (!result)
9647 goto error;
9648 for (i = 0; i < map1->n; ++i)
9649 for (j = 0; j < map2->n; ++j) {
9650 struct isl_basic_map *part;
9651 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9652 isl_basic_map_copy(map2->p[j]));
9653 if (isl_basic_map_is_empty(part))
9654 isl_basic_map_free(part);
9655 else
9656 result = isl_map_add_basic_map(result, part);
9657 if (!result)
9658 goto error;
9660 if (remove_duplicates)
9661 result = isl_map_remove_obvious_duplicates(result);
9662 isl_map_free(map1);
9663 isl_map_free(map2);
9664 return result;
9665 error:
9666 isl_map_free(map1);
9667 isl_map_free(map2);
9668 return NULL;
9671 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9673 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9674 __isl_take isl_map *map2)
9676 return map_product(map1, map2, &isl_space_product,
9677 &isl_basic_map_product, 0);
9680 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9681 __isl_take isl_map *map2)
9683 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9686 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9688 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9689 __isl_take isl_map *map2)
9691 isl_map *prod;
9693 prod = isl_map_product(map1, map2);
9694 prod = isl_map_flatten(prod);
9695 return prod;
9698 /* Given two set A and B, construct its Cartesian product A x B.
9700 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9702 return isl_map_range_product(set1, set2);
9705 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9706 __isl_take isl_set *set2)
9708 return isl_map_flat_range_product(set1, set2);
9711 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9713 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9714 __isl_take isl_map *map2)
9716 return map_product(map1, map2, &isl_space_domain_product,
9717 &isl_basic_map_domain_product, 1);
9720 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9722 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9723 __isl_take isl_map *map2)
9725 return map_product(map1, map2, &isl_space_range_product,
9726 &isl_basic_map_range_product, 1);
9729 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9730 __isl_take isl_map *map2)
9732 return isl_map_align_params_map_map_and(map1, map2,
9733 &map_domain_product_aligned);
9736 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9737 __isl_take isl_map *map2)
9739 return isl_map_align_params_map_map_and(map1, map2,
9740 &map_range_product_aligned);
9743 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9745 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9747 isl_space *space;
9748 int total1, keep1, total2, keep2;
9750 if (!map)
9751 return NULL;
9752 if (!isl_space_domain_is_wrapping(map->dim) ||
9753 !isl_space_range_is_wrapping(map->dim))
9754 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9755 "not a product", return isl_map_free(map));
9757 space = isl_map_get_space(map);
9758 total1 = isl_space_dim(space, isl_dim_in);
9759 total2 = isl_space_dim(space, isl_dim_out);
9760 space = isl_space_factor_domain(space);
9761 keep1 = isl_space_dim(space, isl_dim_in);
9762 keep2 = isl_space_dim(space, isl_dim_out);
9763 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9764 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9765 map = isl_map_reset_space(map, space);
9767 return map;
9770 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9772 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9774 isl_space *space;
9775 int total1, keep1, total2, keep2;
9777 if (!map)
9778 return NULL;
9779 if (!isl_space_domain_is_wrapping(map->dim) ||
9780 !isl_space_range_is_wrapping(map->dim))
9781 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9782 "not a product", return isl_map_free(map));
9784 space = isl_map_get_space(map);
9785 total1 = isl_space_dim(space, isl_dim_in);
9786 total2 = isl_space_dim(space, isl_dim_out);
9787 space = isl_space_factor_range(space);
9788 keep1 = isl_space_dim(space, isl_dim_in);
9789 keep2 = isl_space_dim(space, isl_dim_out);
9790 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9791 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9792 map = isl_map_reset_space(map, space);
9794 return map;
9797 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9799 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9801 isl_space *space;
9802 int total, keep;
9804 if (!map)
9805 return NULL;
9806 if (!isl_space_domain_is_wrapping(map->dim))
9807 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9808 "domain is not a product", return isl_map_free(map));
9810 space = isl_map_get_space(map);
9811 total = isl_space_dim(space, isl_dim_in);
9812 space = isl_space_domain_factor_domain(space);
9813 keep = isl_space_dim(space, isl_dim_in);
9814 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9815 map = isl_map_reset_space(map, space);
9817 return map;
9820 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9822 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9824 isl_space *space;
9825 int total, keep;
9827 if (!map)
9828 return NULL;
9829 if (!isl_space_domain_is_wrapping(map->dim))
9830 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9831 "domain is not a product", return isl_map_free(map));
9833 space = isl_map_get_space(map);
9834 total = isl_space_dim(space, isl_dim_in);
9835 space = isl_space_domain_factor_range(space);
9836 keep = isl_space_dim(space, isl_dim_in);
9837 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9838 map = isl_map_reset_space(map, space);
9840 return map;
9843 /* Given a map A -> [B -> C], extract the map A -> B.
9845 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9847 isl_space *space;
9848 int total, keep;
9850 if (!map)
9851 return NULL;
9852 if (!isl_space_range_is_wrapping(map->dim))
9853 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9854 "range is not a product", return isl_map_free(map));
9856 space = isl_map_get_space(map);
9857 total = isl_space_dim(space, isl_dim_out);
9858 space = isl_space_range_factor_domain(space);
9859 keep = isl_space_dim(space, isl_dim_out);
9860 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9861 map = isl_map_reset_space(map, space);
9863 return map;
9866 /* Given a map A -> [B -> C], extract the map A -> C.
9868 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9870 isl_space *space;
9871 int total, keep;
9873 if (!map)
9874 return NULL;
9875 if (!isl_space_range_is_wrapping(map->dim))
9876 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9877 "range is not a product", return isl_map_free(map));
9879 space = isl_map_get_space(map);
9880 total = isl_space_dim(space, isl_dim_out);
9881 space = isl_space_range_factor_range(space);
9882 keep = isl_space_dim(space, isl_dim_out);
9883 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9884 map = isl_map_reset_space(map, space);
9886 return map;
9889 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9891 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9892 __isl_take isl_map *map2)
9894 isl_map *prod;
9896 prod = isl_map_domain_product(map1, map2);
9897 prod = isl_map_flatten_domain(prod);
9898 return prod;
9901 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9903 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9904 __isl_take isl_map *map2)
9906 isl_map *prod;
9908 prod = isl_map_range_product(map1, map2);
9909 prod = isl_map_flatten_range(prod);
9910 return prod;
9913 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9915 int i;
9916 uint32_t hash = isl_hash_init();
9917 unsigned total;
9919 if (!bmap)
9920 return 0;
9921 bmap = isl_basic_map_copy(bmap);
9922 bmap = isl_basic_map_normalize(bmap);
9923 if (!bmap)
9924 return 0;
9925 total = isl_basic_map_total_dim(bmap);
9926 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9927 for (i = 0; i < bmap->n_eq; ++i) {
9928 uint32_t c_hash;
9929 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9930 isl_hash_hash(hash, c_hash);
9932 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9933 for (i = 0; i < bmap->n_ineq; ++i) {
9934 uint32_t c_hash;
9935 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9936 isl_hash_hash(hash, c_hash);
9938 isl_hash_byte(hash, bmap->n_div & 0xFF);
9939 for (i = 0; i < bmap->n_div; ++i) {
9940 uint32_t c_hash;
9941 if (isl_int_is_zero(bmap->div[i][0]))
9942 continue;
9943 isl_hash_byte(hash, i & 0xFF);
9944 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9945 isl_hash_hash(hash, c_hash);
9947 isl_basic_map_free(bmap);
9948 return hash;
9951 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9953 return isl_basic_map_get_hash((isl_basic_map *)bset);
9956 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9958 int i;
9959 uint32_t hash;
9961 if (!map)
9962 return 0;
9963 map = isl_map_copy(map);
9964 map = isl_map_normalize(map);
9965 if (!map)
9966 return 0;
9968 hash = isl_hash_init();
9969 for (i = 0; i < map->n; ++i) {
9970 uint32_t bmap_hash;
9971 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9972 isl_hash_hash(hash, bmap_hash);
9975 isl_map_free(map);
9977 return hash;
9980 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9982 return isl_map_get_hash((isl_map *)set);
9985 /* Check if the value for dimension dim is completely determined
9986 * by the values of the other parameters and variables.
9987 * That is, check if dimension dim is involved in an equality.
9989 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9991 int i;
9992 unsigned nparam;
9994 if (!bset)
9995 return -1;
9996 nparam = isl_basic_set_n_param(bset);
9997 for (i = 0; i < bset->n_eq; ++i)
9998 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9999 return 1;
10000 return 0;
10003 /* Check if the value for dimension dim is completely determined
10004 * by the values of the other parameters and variables.
10005 * That is, check if dimension dim is involved in an equality
10006 * for each of the subsets.
10008 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10010 int i;
10012 if (!set)
10013 return -1;
10014 for (i = 0; i < set->n; ++i) {
10015 int unique;
10016 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10017 if (unique != 1)
10018 return unique;
10020 return 1;
10023 /* Return the number of basic maps in the (current) representation of "map".
10025 int isl_map_n_basic_map(__isl_keep isl_map *map)
10027 return map ? map->n : 0;
10030 int isl_set_n_basic_set(__isl_keep isl_set *set)
10032 return set ? set->n : 0;
10035 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
10036 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10038 int i;
10040 if (!map)
10041 return -1;
10043 for (i = 0; i < map->n; ++i)
10044 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10045 return -1;
10047 return 0;
10050 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
10051 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10053 int i;
10055 if (!set)
10056 return -1;
10058 for (i = 0; i < set->n; ++i)
10059 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10060 return -1;
10062 return 0;
10065 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10067 isl_space *dim;
10069 if (!bset)
10070 return NULL;
10072 bset = isl_basic_set_cow(bset);
10073 if (!bset)
10074 return NULL;
10076 dim = isl_basic_set_get_space(bset);
10077 dim = isl_space_lift(dim, bset->n_div);
10078 if (!dim)
10079 goto error;
10080 isl_space_free(bset->dim);
10081 bset->dim = dim;
10082 bset->extra -= bset->n_div;
10083 bset->n_div = 0;
10085 bset = isl_basic_set_finalize(bset);
10087 return bset;
10088 error:
10089 isl_basic_set_free(bset);
10090 return NULL;
10093 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10095 int i;
10096 isl_space *dim;
10097 unsigned n_div;
10099 set = isl_set_align_divs(set);
10101 if (!set)
10102 return NULL;
10104 set = isl_set_cow(set);
10105 if (!set)
10106 return NULL;
10108 n_div = set->p[0]->n_div;
10109 dim = isl_set_get_space(set);
10110 dim = isl_space_lift(dim, n_div);
10111 if (!dim)
10112 goto error;
10113 isl_space_free(set->dim);
10114 set->dim = dim;
10116 for (i = 0; i < set->n; ++i) {
10117 set->p[i] = isl_basic_set_lift(set->p[i]);
10118 if (!set->p[i])
10119 goto error;
10122 return set;
10123 error:
10124 isl_set_free(set);
10125 return NULL;
10128 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10130 isl_space *dim;
10131 struct isl_basic_map *bmap;
10132 unsigned n_set;
10133 unsigned n_div;
10134 unsigned n_param;
10135 unsigned total;
10136 int i, k, l;
10138 set = isl_set_align_divs(set);
10140 if (!set)
10141 return NULL;
10143 dim = isl_set_get_space(set);
10144 if (set->n == 0 || set->p[0]->n_div == 0) {
10145 isl_set_free(set);
10146 return isl_map_identity(isl_space_map_from_set(dim));
10149 n_div = set->p[0]->n_div;
10150 dim = isl_space_map_from_set(dim);
10151 n_param = isl_space_dim(dim, isl_dim_param);
10152 n_set = isl_space_dim(dim, isl_dim_in);
10153 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10154 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10155 for (i = 0; i < n_set; ++i)
10156 bmap = var_equal(bmap, i);
10158 total = n_param + n_set + n_set + n_div;
10159 for (i = 0; i < n_div; ++i) {
10160 k = isl_basic_map_alloc_inequality(bmap);
10161 if (k < 0)
10162 goto error;
10163 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10164 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10165 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10166 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10167 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10168 set->p[0]->div[i][0]);
10170 l = isl_basic_map_alloc_inequality(bmap);
10171 if (l < 0)
10172 goto error;
10173 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10174 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10175 set->p[0]->div[i][0]);
10176 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10179 isl_set_free(set);
10180 bmap = isl_basic_map_simplify(bmap);
10181 bmap = isl_basic_map_finalize(bmap);
10182 return isl_map_from_basic_map(bmap);
10183 error:
10184 isl_set_free(set);
10185 isl_basic_map_free(bmap);
10186 return NULL;
10189 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10191 unsigned dim;
10192 int size = 0;
10194 if (!bset)
10195 return -1;
10197 dim = isl_basic_set_total_dim(bset);
10198 size += bset->n_eq * (1 + dim);
10199 size += bset->n_ineq * (1 + dim);
10200 size += bset->n_div * (2 + dim);
10202 return size;
10205 int isl_set_size(__isl_keep isl_set *set)
10207 int i;
10208 int size = 0;
10210 if (!set)
10211 return -1;
10213 for (i = 0; i < set->n; ++i)
10214 size += isl_basic_set_size(set->p[i]);
10216 return size;
10219 /* Check if there is any lower bound (if lower == 0) and/or upper
10220 * bound (if upper == 0) on the specified dim.
10222 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10223 enum isl_dim_type type, unsigned pos, int lower, int upper)
10225 int i;
10227 if (!bmap)
10228 return -1;
10230 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
10232 pos += isl_basic_map_offset(bmap, type);
10234 for (i = 0; i < bmap->n_div; ++i) {
10235 if (isl_int_is_zero(bmap->div[i][0]))
10236 continue;
10237 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10238 return 1;
10241 for (i = 0; i < bmap->n_eq; ++i)
10242 if (!isl_int_is_zero(bmap->eq[i][pos]))
10243 return 1;
10245 for (i = 0; i < bmap->n_ineq; ++i) {
10246 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10247 if (sgn > 0)
10248 lower = 1;
10249 if (sgn < 0)
10250 upper = 1;
10253 return lower && upper;
10256 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10257 enum isl_dim_type type, unsigned pos)
10259 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10262 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10263 enum isl_dim_type type, unsigned pos)
10265 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10268 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10269 enum isl_dim_type type, unsigned pos)
10271 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10274 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10275 enum isl_dim_type type, unsigned pos)
10277 int i;
10279 if (!map)
10280 return -1;
10282 for (i = 0; i < map->n; ++i) {
10283 int bounded;
10284 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10285 if (bounded < 0 || !bounded)
10286 return bounded;
10289 return 1;
10292 /* Return 1 if the specified dim is involved in both an upper bound
10293 * and a lower bound.
10295 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10296 enum isl_dim_type type, unsigned pos)
10298 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10301 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10303 static int has_any_bound(__isl_keep isl_map *map,
10304 enum isl_dim_type type, unsigned pos,
10305 int (*fn)(__isl_keep isl_basic_map *bmap,
10306 enum isl_dim_type type, unsigned pos))
10308 int i;
10310 if (!map)
10311 return -1;
10313 for (i = 0; i < map->n; ++i) {
10314 int bounded;
10315 bounded = fn(map->p[i], type, pos);
10316 if (bounded < 0 || bounded)
10317 return bounded;
10320 return 0;
10323 /* Return 1 if the specified dim is involved in any lower bound.
10325 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10326 enum isl_dim_type type, unsigned pos)
10328 return has_any_bound(set, type, pos,
10329 &isl_basic_map_dim_has_lower_bound);
10332 /* Return 1 if the specified dim is involved in any upper bound.
10334 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10335 enum isl_dim_type type, unsigned pos)
10337 return has_any_bound(set, type, pos,
10338 &isl_basic_map_dim_has_upper_bound);
10341 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10343 static int has_bound(__isl_keep isl_map *map,
10344 enum isl_dim_type type, unsigned pos,
10345 int (*fn)(__isl_keep isl_basic_map *bmap,
10346 enum isl_dim_type type, unsigned pos))
10348 int i;
10350 if (!map)
10351 return -1;
10353 for (i = 0; i < map->n; ++i) {
10354 int bounded;
10355 bounded = fn(map->p[i], type, pos);
10356 if (bounded < 0 || !bounded)
10357 return bounded;
10360 return 1;
10363 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10365 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10366 enum isl_dim_type type, unsigned pos)
10368 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10371 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10373 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10374 enum isl_dim_type type, unsigned pos)
10376 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10379 /* For each of the "n" variables starting at "first", determine
10380 * the sign of the variable and put the results in the first "n"
10381 * elements of the array "signs".
10382 * Sign
10383 * 1 means that the variable is non-negative
10384 * -1 means that the variable is non-positive
10385 * 0 means the variable attains both positive and negative values.
10387 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10388 unsigned first, unsigned n, int *signs)
10390 isl_vec *bound = NULL;
10391 struct isl_tab *tab = NULL;
10392 struct isl_tab_undo *snap;
10393 int i;
10395 if (!bset || !signs)
10396 return -1;
10398 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10399 tab = isl_tab_from_basic_set(bset, 0);
10400 if (!bound || !tab)
10401 goto error;
10403 isl_seq_clr(bound->el, bound->size);
10404 isl_int_set_si(bound->el[0], -1);
10406 snap = isl_tab_snap(tab);
10407 for (i = 0; i < n; ++i) {
10408 int empty;
10410 isl_int_set_si(bound->el[1 + first + i], -1);
10411 if (isl_tab_add_ineq(tab, bound->el) < 0)
10412 goto error;
10413 empty = tab->empty;
10414 isl_int_set_si(bound->el[1 + first + i], 0);
10415 if (isl_tab_rollback(tab, snap) < 0)
10416 goto error;
10418 if (empty) {
10419 signs[i] = 1;
10420 continue;
10423 isl_int_set_si(bound->el[1 + first + i], 1);
10424 if (isl_tab_add_ineq(tab, bound->el) < 0)
10425 goto error;
10426 empty = tab->empty;
10427 isl_int_set_si(bound->el[1 + first + i], 0);
10428 if (isl_tab_rollback(tab, snap) < 0)
10429 goto error;
10431 signs[i] = empty ? -1 : 0;
10434 isl_tab_free(tab);
10435 isl_vec_free(bound);
10436 return 0;
10437 error:
10438 isl_tab_free(tab);
10439 isl_vec_free(bound);
10440 return -1;
10443 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10444 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10446 if (!bset || !signs)
10447 return -1;
10448 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10449 return -1);
10451 first += pos(bset->dim, type) - 1;
10452 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10455 /* Is it possible for the integer division "div" to depend (possibly
10456 * indirectly) on any output dimensions?
10458 * If the div is undefined, then we conservatively assume that it
10459 * may depend on them.
10460 * Otherwise, we check if it actually depends on them or on any integer
10461 * divisions that may depend on them.
10463 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10465 int i;
10466 unsigned n_out, o_out;
10467 unsigned n_div, o_div;
10469 if (isl_int_is_zero(bmap->div[div][0]))
10470 return 1;
10472 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10473 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10475 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10476 return 1;
10478 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10479 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10481 for (i = 0; i < n_div; ++i) {
10482 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10483 continue;
10484 if (div_may_involve_output(bmap, i))
10485 return 1;
10488 return 0;
10491 /* Return the index of the equality of "bmap" that defines
10492 * the output dimension "pos" in terms of earlier dimensions.
10493 * The equality may also involve integer divisions, as long
10494 * as those integer divisions are defined in terms of
10495 * parameters or input dimensions.
10496 * Return bmap->n_eq if there is no such equality.
10497 * Return -1 on error.
10499 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10500 int pos)
10502 int j, k;
10503 unsigned n_out, o_out;
10504 unsigned n_div, o_div;
10506 if (!bmap)
10507 return -1;
10509 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10510 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10511 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10512 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10514 for (j = 0; j < bmap->n_eq; ++j) {
10515 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10516 continue;
10517 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10518 n_out - (pos + 1)) != -1)
10519 continue;
10520 for (k = 0; k < n_div; ++k) {
10521 if (isl_int_is_zero(bmap->eq[j][o_div + k]))
10522 continue;
10523 if (div_may_involve_output(bmap, k))
10524 break;
10526 if (k >= n_div)
10527 return j;
10530 return bmap->n_eq;
10533 /* Check if the given basic map is obviously single-valued.
10534 * In particular, for each output dimension, check that there is
10535 * an equality that defines the output dimension in terms of
10536 * earlier dimensions.
10538 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10540 int i;
10541 unsigned n_out;
10543 if (!bmap)
10544 return -1;
10546 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10548 for (i = 0; i < n_out; ++i) {
10549 int eq;
10551 eq = isl_basic_map_output_defining_equality(bmap, i);
10552 if (eq < 0)
10553 return -1;
10554 if (eq >= bmap->n_eq)
10555 return 0;
10558 return 1;
10561 /* Check if the given basic map is single-valued.
10562 * We simply compute
10564 * M \circ M^-1
10566 * and check if the result is a subset of the identity mapping.
10568 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10570 isl_space *space;
10571 isl_basic_map *test;
10572 isl_basic_map *id;
10573 int sv;
10575 sv = isl_basic_map_plain_is_single_valued(bmap);
10576 if (sv < 0 || sv)
10577 return sv;
10579 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10580 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10582 space = isl_basic_map_get_space(bmap);
10583 space = isl_space_map_from_set(isl_space_range(space));
10584 id = isl_basic_map_identity(space);
10586 sv = isl_basic_map_is_subset(test, id);
10588 isl_basic_map_free(test);
10589 isl_basic_map_free(id);
10591 return sv;
10594 /* Check if the given map is obviously single-valued.
10596 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10598 if (!map)
10599 return -1;
10600 if (map->n == 0)
10601 return 1;
10602 if (map->n >= 2)
10603 return 0;
10605 return isl_basic_map_plain_is_single_valued(map->p[0]);
10608 /* Check if the given map is single-valued.
10609 * We simply compute
10611 * M \circ M^-1
10613 * and check if the result is a subset of the identity mapping.
10615 int isl_map_is_single_valued(__isl_keep isl_map *map)
10617 isl_space *dim;
10618 isl_map *test;
10619 isl_map *id;
10620 int sv;
10622 sv = isl_map_plain_is_single_valued(map);
10623 if (sv < 0 || sv)
10624 return sv;
10626 test = isl_map_reverse(isl_map_copy(map));
10627 test = isl_map_apply_range(test, isl_map_copy(map));
10629 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10630 id = isl_map_identity(dim);
10632 sv = isl_map_is_subset(test, id);
10634 isl_map_free(test);
10635 isl_map_free(id);
10637 return sv;
10640 int isl_map_is_injective(__isl_keep isl_map *map)
10642 int in;
10644 map = isl_map_copy(map);
10645 map = isl_map_reverse(map);
10646 in = isl_map_is_single_valued(map);
10647 isl_map_free(map);
10649 return in;
10652 /* Check if the given map is obviously injective.
10654 int isl_map_plain_is_injective(__isl_keep isl_map *map)
10656 int in;
10658 map = isl_map_copy(map);
10659 map = isl_map_reverse(map);
10660 in = isl_map_plain_is_single_valued(map);
10661 isl_map_free(map);
10663 return in;
10666 int isl_map_is_bijective(__isl_keep isl_map *map)
10668 int sv;
10670 sv = isl_map_is_single_valued(map);
10671 if (sv < 0 || !sv)
10672 return sv;
10674 return isl_map_is_injective(map);
10677 int isl_set_is_singleton(__isl_keep isl_set *set)
10679 return isl_map_is_single_valued((isl_map *)set);
10682 int isl_map_is_translation(__isl_keep isl_map *map)
10684 int ok;
10685 isl_set *delta;
10687 delta = isl_map_deltas(isl_map_copy(map));
10688 ok = isl_set_is_singleton(delta);
10689 isl_set_free(delta);
10691 return ok;
10694 static int unique(isl_int *p, unsigned pos, unsigned len)
10696 if (isl_seq_first_non_zero(p, pos) != -1)
10697 return 0;
10698 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10699 return 0;
10700 return 1;
10703 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10705 int i, j;
10706 unsigned nvar;
10707 unsigned ovar;
10709 if (!bset)
10710 return -1;
10712 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10713 return 0;
10715 nvar = isl_basic_set_dim(bset, isl_dim_set);
10716 ovar = isl_space_offset(bset->dim, isl_dim_set);
10717 for (j = 0; j < nvar; ++j) {
10718 int lower = 0, upper = 0;
10719 for (i = 0; i < bset->n_eq; ++i) {
10720 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10721 continue;
10722 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10723 return 0;
10724 break;
10726 if (i < bset->n_eq)
10727 continue;
10728 for (i = 0; i < bset->n_ineq; ++i) {
10729 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10730 continue;
10731 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10732 return 0;
10733 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10734 lower = 1;
10735 else
10736 upper = 1;
10738 if (!lower || !upper)
10739 return 0;
10742 return 1;
10745 int isl_set_is_box(__isl_keep isl_set *set)
10747 if (!set)
10748 return -1;
10749 if (set->n != 1)
10750 return 0;
10752 return isl_basic_set_is_box(set->p[0]);
10755 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10757 if (!bset)
10758 return -1;
10760 return isl_space_is_wrapping(bset->dim);
10763 int isl_set_is_wrapping(__isl_keep isl_set *set)
10765 if (!set)
10766 return -1;
10768 return isl_space_is_wrapping(set->dim);
10771 /* Is the domain of "map" a wrapped relation?
10773 int isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10775 if (!map)
10776 return -1;
10778 return isl_space_domain_is_wrapping(map->dim);
10781 /* Is the range of "map" a wrapped relation?
10783 int isl_map_range_is_wrapping(__isl_keep isl_map *map)
10785 if (!map)
10786 return -1;
10788 return isl_space_range_is_wrapping(map->dim);
10791 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10793 bmap = isl_basic_map_cow(bmap);
10794 if (!bmap)
10795 return NULL;
10797 bmap->dim = isl_space_wrap(bmap->dim);
10798 if (!bmap->dim)
10799 goto error;
10801 bmap = isl_basic_map_finalize(bmap);
10803 return (isl_basic_set *)bmap;
10804 error:
10805 isl_basic_map_free(bmap);
10806 return NULL;
10809 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10811 int i;
10813 map = isl_map_cow(map);
10814 if (!map)
10815 return NULL;
10817 for (i = 0; i < map->n; ++i) {
10818 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10819 if (!map->p[i])
10820 goto error;
10822 map->dim = isl_space_wrap(map->dim);
10823 if (!map->dim)
10824 goto error;
10826 return (isl_set *)map;
10827 error:
10828 isl_map_free(map);
10829 return NULL;
10832 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10834 bset = isl_basic_set_cow(bset);
10835 if (!bset)
10836 return NULL;
10838 bset->dim = isl_space_unwrap(bset->dim);
10839 if (!bset->dim)
10840 goto error;
10842 bset = isl_basic_set_finalize(bset);
10844 return (isl_basic_map *)bset;
10845 error:
10846 isl_basic_set_free(bset);
10847 return NULL;
10850 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10852 int i;
10854 if (!set)
10855 return NULL;
10857 if (!isl_set_is_wrapping(set))
10858 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10859 goto error);
10861 set = isl_set_cow(set);
10862 if (!set)
10863 return NULL;
10865 for (i = 0; i < set->n; ++i) {
10866 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10867 if (!set->p[i])
10868 goto error;
10871 set->dim = isl_space_unwrap(set->dim);
10872 if (!set->dim)
10873 goto error;
10875 return (isl_map *)set;
10876 error:
10877 isl_set_free(set);
10878 return NULL;
10881 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10882 enum isl_dim_type type)
10884 if (!bmap)
10885 return NULL;
10887 if (!isl_space_is_named_or_nested(bmap->dim, type))
10888 return bmap;
10890 bmap = isl_basic_map_cow(bmap);
10891 if (!bmap)
10892 return NULL;
10894 bmap->dim = isl_space_reset(bmap->dim, type);
10895 if (!bmap->dim)
10896 goto error;
10898 bmap = isl_basic_map_finalize(bmap);
10900 return bmap;
10901 error:
10902 isl_basic_map_free(bmap);
10903 return NULL;
10906 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10907 enum isl_dim_type type)
10909 int i;
10911 if (!map)
10912 return NULL;
10914 if (!isl_space_is_named_or_nested(map->dim, type))
10915 return map;
10917 map = isl_map_cow(map);
10918 if (!map)
10919 return NULL;
10921 for (i = 0; i < map->n; ++i) {
10922 map->p[i] = isl_basic_map_reset(map->p[i], type);
10923 if (!map->p[i])
10924 goto error;
10926 map->dim = isl_space_reset(map->dim, type);
10927 if (!map->dim)
10928 goto error;
10930 return map;
10931 error:
10932 isl_map_free(map);
10933 return NULL;
10936 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10938 if (!bmap)
10939 return NULL;
10941 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10942 return bmap;
10944 bmap = isl_basic_map_cow(bmap);
10945 if (!bmap)
10946 return NULL;
10948 bmap->dim = isl_space_flatten(bmap->dim);
10949 if (!bmap->dim)
10950 goto error;
10952 bmap = isl_basic_map_finalize(bmap);
10954 return bmap;
10955 error:
10956 isl_basic_map_free(bmap);
10957 return NULL;
10960 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10962 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10965 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10966 __isl_take isl_basic_map *bmap)
10968 if (!bmap)
10969 return NULL;
10971 if (!bmap->dim->nested[0])
10972 return bmap;
10974 bmap = isl_basic_map_cow(bmap);
10975 if (!bmap)
10976 return NULL;
10978 bmap->dim = isl_space_flatten_domain(bmap->dim);
10979 if (!bmap->dim)
10980 goto error;
10982 bmap = isl_basic_map_finalize(bmap);
10984 return bmap;
10985 error:
10986 isl_basic_map_free(bmap);
10987 return NULL;
10990 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10991 __isl_take isl_basic_map *bmap)
10993 if (!bmap)
10994 return NULL;
10996 if (!bmap->dim->nested[1])
10997 return bmap;
10999 bmap = isl_basic_map_cow(bmap);
11000 if (!bmap)
11001 return NULL;
11003 bmap->dim = isl_space_flatten_range(bmap->dim);
11004 if (!bmap->dim)
11005 goto error;
11007 bmap = isl_basic_map_finalize(bmap);
11009 return bmap;
11010 error:
11011 isl_basic_map_free(bmap);
11012 return NULL;
11015 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11017 int i;
11019 if (!map)
11020 return NULL;
11022 if (!map->dim->nested[0] && !map->dim->nested[1])
11023 return map;
11025 map = isl_map_cow(map);
11026 if (!map)
11027 return NULL;
11029 for (i = 0; i < map->n; ++i) {
11030 map->p[i] = isl_basic_map_flatten(map->p[i]);
11031 if (!map->p[i])
11032 goto error;
11034 map->dim = isl_space_flatten(map->dim);
11035 if (!map->dim)
11036 goto error;
11038 return map;
11039 error:
11040 isl_map_free(map);
11041 return NULL;
11044 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11046 return (isl_set *)isl_map_flatten((isl_map *)set);
11049 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11051 isl_space *dim, *flat_dim;
11052 isl_map *map;
11054 dim = isl_set_get_space(set);
11055 flat_dim = isl_space_flatten(isl_space_copy(dim));
11056 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11057 map = isl_map_intersect_domain(map, set);
11059 return map;
11062 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11064 int i;
11066 if (!map)
11067 return NULL;
11069 if (!map->dim->nested[0])
11070 return map;
11072 map = isl_map_cow(map);
11073 if (!map)
11074 return NULL;
11076 for (i = 0; i < map->n; ++i) {
11077 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
11078 if (!map->p[i])
11079 goto error;
11081 map->dim = isl_space_flatten_domain(map->dim);
11082 if (!map->dim)
11083 goto error;
11085 return map;
11086 error:
11087 isl_map_free(map);
11088 return NULL;
11091 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11093 int i;
11095 if (!map)
11096 return NULL;
11098 if (!map->dim->nested[1])
11099 return map;
11101 map = isl_map_cow(map);
11102 if (!map)
11103 return NULL;
11105 for (i = 0; i < map->n; ++i) {
11106 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
11107 if (!map->p[i])
11108 goto error;
11110 map->dim = isl_space_flatten_range(map->dim);
11111 if (!map->dim)
11112 goto error;
11114 return map;
11115 error:
11116 isl_map_free(map);
11117 return NULL;
11120 /* Reorder the dimensions of "bmap" according to the given dim_map
11121 * and set the dimension specification to "dim".
11123 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11124 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11126 isl_basic_map *res;
11127 unsigned flags;
11129 bmap = isl_basic_map_cow(bmap);
11130 if (!bmap || !dim || !dim_map)
11131 goto error;
11133 flags = bmap->flags;
11134 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11135 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11136 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11137 res = isl_basic_map_alloc_space(dim,
11138 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11139 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11140 if (res)
11141 res->flags = flags;
11142 res = isl_basic_map_finalize(res);
11143 return res;
11144 error:
11145 free(dim_map);
11146 isl_basic_map_free(bmap);
11147 isl_space_free(dim);
11148 return NULL;
11151 /* Reorder the dimensions of "map" according to given reordering.
11153 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11154 __isl_take isl_reordering *r)
11156 int i;
11157 struct isl_dim_map *dim_map;
11159 map = isl_map_cow(map);
11160 dim_map = isl_dim_map_from_reordering(r);
11161 if (!map || !r || !dim_map)
11162 goto error;
11164 for (i = 0; i < map->n; ++i) {
11165 struct isl_dim_map *dim_map_i;
11167 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11169 map->p[i] = isl_basic_map_realign(map->p[i],
11170 isl_space_copy(r->dim), dim_map_i);
11172 if (!map->p[i])
11173 goto error;
11176 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11178 isl_reordering_free(r);
11179 free(dim_map);
11180 return map;
11181 error:
11182 free(dim_map);
11183 isl_map_free(map);
11184 isl_reordering_free(r);
11185 return NULL;
11188 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11189 __isl_take isl_reordering *r)
11191 return (isl_set *)isl_map_realign((isl_map *)set, r);
11194 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11195 __isl_take isl_space *model)
11197 isl_ctx *ctx;
11199 if (!map || !model)
11200 goto error;
11202 ctx = isl_space_get_ctx(model);
11203 if (!isl_space_has_named_params(model))
11204 isl_die(ctx, isl_error_invalid,
11205 "model has unnamed parameters", goto error);
11206 if (!isl_space_has_named_params(map->dim))
11207 isl_die(ctx, isl_error_invalid,
11208 "relation has unnamed parameters", goto error);
11209 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11210 isl_reordering *exp;
11212 model = isl_space_drop_dims(model, isl_dim_in,
11213 0, isl_space_dim(model, isl_dim_in));
11214 model = isl_space_drop_dims(model, isl_dim_out,
11215 0, isl_space_dim(model, isl_dim_out));
11216 exp = isl_parameter_alignment_reordering(map->dim, model);
11217 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11218 map = isl_map_realign(map, exp);
11221 isl_space_free(model);
11222 return map;
11223 error:
11224 isl_space_free(model);
11225 isl_map_free(map);
11226 return NULL;
11229 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11230 __isl_take isl_space *model)
11232 return isl_map_align_params(set, model);
11235 /* Align the parameters of "bmap" to those of "model", introducing
11236 * additional parameters if needed.
11238 __isl_give isl_basic_map *isl_basic_map_align_params(
11239 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11241 isl_ctx *ctx;
11243 if (!bmap || !model)
11244 goto error;
11246 ctx = isl_space_get_ctx(model);
11247 if (!isl_space_has_named_params(model))
11248 isl_die(ctx, isl_error_invalid,
11249 "model has unnamed parameters", goto error);
11250 if (!isl_space_has_named_params(bmap->dim))
11251 isl_die(ctx, isl_error_invalid,
11252 "relation has unnamed parameters", goto error);
11253 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11254 isl_reordering *exp;
11255 struct isl_dim_map *dim_map;
11257 model = isl_space_drop_dims(model, isl_dim_in,
11258 0, isl_space_dim(model, isl_dim_in));
11259 model = isl_space_drop_dims(model, isl_dim_out,
11260 0, isl_space_dim(model, isl_dim_out));
11261 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11262 exp = isl_reordering_extend_space(exp,
11263 isl_basic_map_get_space(bmap));
11264 dim_map = isl_dim_map_from_reordering(exp);
11265 bmap = isl_basic_map_realign(bmap,
11266 exp ? isl_space_copy(exp->dim) : NULL,
11267 isl_dim_map_extend(dim_map, bmap));
11268 isl_reordering_free(exp);
11269 free(dim_map);
11272 isl_space_free(model);
11273 return bmap;
11274 error:
11275 isl_space_free(model);
11276 isl_basic_map_free(bmap);
11277 return NULL;
11280 /* Align the parameters of "bset" to those of "model", introducing
11281 * additional parameters if needed.
11283 __isl_give isl_basic_set *isl_basic_set_align_params(
11284 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11286 return isl_basic_map_align_params(bset, model);
11289 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11290 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11291 enum isl_dim_type c2, enum isl_dim_type c3,
11292 enum isl_dim_type c4, enum isl_dim_type c5)
11294 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11295 struct isl_mat *mat;
11296 int i, j, k;
11297 int pos;
11299 if (!bmap)
11300 return NULL;
11301 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11302 isl_basic_map_total_dim(bmap) + 1);
11303 if (!mat)
11304 return NULL;
11305 for (i = 0; i < bmap->n_eq; ++i)
11306 for (j = 0, pos = 0; j < 5; ++j) {
11307 int off = isl_basic_map_offset(bmap, c[j]);
11308 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11309 isl_int_set(mat->row[i][pos],
11310 bmap->eq[i][off + k]);
11311 ++pos;
11315 return mat;
11318 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11319 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11320 enum isl_dim_type c2, enum isl_dim_type c3,
11321 enum isl_dim_type c4, enum isl_dim_type c5)
11323 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11324 struct isl_mat *mat;
11325 int i, j, k;
11326 int pos;
11328 if (!bmap)
11329 return NULL;
11330 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11331 isl_basic_map_total_dim(bmap) + 1);
11332 if (!mat)
11333 return NULL;
11334 for (i = 0; i < bmap->n_ineq; ++i)
11335 for (j = 0, pos = 0; j < 5; ++j) {
11336 int off = isl_basic_map_offset(bmap, c[j]);
11337 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11338 isl_int_set(mat->row[i][pos],
11339 bmap->ineq[i][off + k]);
11340 ++pos;
11344 return mat;
11347 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11348 __isl_take isl_space *dim,
11349 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11350 enum isl_dim_type c2, enum isl_dim_type c3,
11351 enum isl_dim_type c4, enum isl_dim_type c5)
11353 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11354 isl_basic_map *bmap;
11355 unsigned total;
11356 unsigned extra;
11357 int i, j, k, l;
11358 int pos;
11360 if (!dim || !eq || !ineq)
11361 goto error;
11363 if (eq->n_col != ineq->n_col)
11364 isl_die(dim->ctx, isl_error_invalid,
11365 "equalities and inequalities matrices should have "
11366 "same number of columns", goto error);
11368 total = 1 + isl_space_dim(dim, isl_dim_all);
11370 if (eq->n_col < total)
11371 isl_die(dim->ctx, isl_error_invalid,
11372 "number of columns too small", goto error);
11374 extra = eq->n_col - total;
11376 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11377 eq->n_row, ineq->n_row);
11378 if (!bmap)
11379 goto error;
11380 for (i = 0; i < extra; ++i) {
11381 k = isl_basic_map_alloc_div(bmap);
11382 if (k < 0)
11383 goto error;
11384 isl_int_set_si(bmap->div[k][0], 0);
11386 for (i = 0; i < eq->n_row; ++i) {
11387 l = isl_basic_map_alloc_equality(bmap);
11388 if (l < 0)
11389 goto error;
11390 for (j = 0, pos = 0; j < 5; ++j) {
11391 int off = isl_basic_map_offset(bmap, c[j]);
11392 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11393 isl_int_set(bmap->eq[l][off + k],
11394 eq->row[i][pos]);
11395 ++pos;
11399 for (i = 0; i < ineq->n_row; ++i) {
11400 l = isl_basic_map_alloc_inequality(bmap);
11401 if (l < 0)
11402 goto error;
11403 for (j = 0, pos = 0; j < 5; ++j) {
11404 int off = isl_basic_map_offset(bmap, c[j]);
11405 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11406 isl_int_set(bmap->ineq[l][off + k],
11407 ineq->row[i][pos]);
11408 ++pos;
11413 isl_space_free(dim);
11414 isl_mat_free(eq);
11415 isl_mat_free(ineq);
11417 bmap = isl_basic_map_simplify(bmap);
11418 return isl_basic_map_finalize(bmap);
11419 error:
11420 isl_space_free(dim);
11421 isl_mat_free(eq);
11422 isl_mat_free(ineq);
11423 return NULL;
11426 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11427 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11428 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11430 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11431 c1, c2, c3, c4, isl_dim_in);
11434 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11435 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11436 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11438 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11439 c1, c2, c3, c4, isl_dim_in);
11442 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11443 __isl_take isl_space *dim,
11444 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11445 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11447 return (isl_basic_set*)
11448 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11449 c1, c2, c3, c4, isl_dim_in);
11452 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11454 if (!bmap)
11455 return -1;
11457 return isl_space_can_zip(bmap->dim);
11460 int isl_map_can_zip(__isl_keep isl_map *map)
11462 if (!map)
11463 return -1;
11465 return isl_space_can_zip(map->dim);
11468 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11469 * (A -> C) -> (B -> D).
11471 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11473 unsigned pos;
11474 unsigned n1;
11475 unsigned n2;
11477 if (!bmap)
11478 return NULL;
11480 if (!isl_basic_map_can_zip(bmap))
11481 isl_die(bmap->ctx, isl_error_invalid,
11482 "basic map cannot be zipped", goto error);
11483 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11484 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11485 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11486 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11487 bmap = isl_basic_map_cow(bmap);
11488 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11489 if (!bmap)
11490 return NULL;
11491 bmap->dim = isl_space_zip(bmap->dim);
11492 if (!bmap->dim)
11493 goto error;
11494 return bmap;
11495 error:
11496 isl_basic_map_free(bmap);
11497 return NULL;
11500 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11501 * (A -> C) -> (B -> D).
11503 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11505 int i;
11507 if (!map)
11508 return NULL;
11510 if (!isl_map_can_zip(map))
11511 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11512 goto error);
11514 map = isl_map_cow(map);
11515 if (!map)
11516 return NULL;
11518 for (i = 0; i < map->n; ++i) {
11519 map->p[i] = isl_basic_map_zip(map->p[i]);
11520 if (!map->p[i])
11521 goto error;
11524 map->dim = isl_space_zip(map->dim);
11525 if (!map->dim)
11526 goto error;
11528 return map;
11529 error:
11530 isl_map_free(map);
11531 return NULL;
11534 /* Can we apply isl_basic_map_curry to "bmap"?
11535 * That is, does it have a nested relation in its domain?
11537 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11539 if (!bmap)
11540 return -1;
11542 return isl_space_can_curry(bmap->dim);
11545 /* Can we apply isl_map_curry to "map"?
11546 * That is, does it have a nested relation in its domain?
11548 int isl_map_can_curry(__isl_keep isl_map *map)
11550 if (!map)
11551 return -1;
11553 return isl_space_can_curry(map->dim);
11556 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11557 * A -> (B -> C).
11559 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11562 if (!bmap)
11563 return NULL;
11565 if (!isl_basic_map_can_curry(bmap))
11566 isl_die(bmap->ctx, isl_error_invalid,
11567 "basic map cannot be curried", goto error);
11568 bmap = isl_basic_map_cow(bmap);
11569 if (!bmap)
11570 return NULL;
11571 bmap->dim = isl_space_curry(bmap->dim);
11572 if (!bmap->dim)
11573 goto error;
11574 return bmap;
11575 error:
11576 isl_basic_map_free(bmap);
11577 return NULL;
11580 /* Given a map (A -> B) -> C, return the corresponding map
11581 * A -> (B -> C).
11583 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11585 int i;
11587 if (!map)
11588 return NULL;
11590 if (!isl_map_can_curry(map))
11591 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11592 goto error);
11594 map = isl_map_cow(map);
11595 if (!map)
11596 return NULL;
11598 for (i = 0; i < map->n; ++i) {
11599 map->p[i] = isl_basic_map_curry(map->p[i]);
11600 if (!map->p[i])
11601 goto error;
11604 map->dim = isl_space_curry(map->dim);
11605 if (!map->dim)
11606 goto error;
11608 return map;
11609 error:
11610 isl_map_free(map);
11611 return NULL;
11614 /* Can we apply isl_basic_map_uncurry to "bmap"?
11615 * That is, does it have a nested relation in its domain?
11617 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11619 if (!bmap)
11620 return -1;
11622 return isl_space_can_uncurry(bmap->dim);
11625 /* Can we apply isl_map_uncurry to "map"?
11626 * That is, does it have a nested relation in its domain?
11628 int isl_map_can_uncurry(__isl_keep isl_map *map)
11630 if (!map)
11631 return -1;
11633 return isl_space_can_uncurry(map->dim);
11636 /* Given a basic map A -> (B -> C), return the corresponding basic map
11637 * (A -> B) -> C.
11639 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11642 if (!bmap)
11643 return NULL;
11645 if (!isl_basic_map_can_uncurry(bmap))
11646 isl_die(bmap->ctx, isl_error_invalid,
11647 "basic map cannot be uncurried",
11648 return isl_basic_map_free(bmap));
11649 bmap = isl_basic_map_cow(bmap);
11650 if (!bmap)
11651 return NULL;
11652 bmap->dim = isl_space_uncurry(bmap->dim);
11653 if (!bmap->dim)
11654 return isl_basic_map_free(bmap);
11655 return bmap;
11658 /* Given a map A -> (B -> C), return the corresponding map
11659 * (A -> B) -> C.
11661 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11663 int i;
11665 if (!map)
11666 return NULL;
11668 if (!isl_map_can_uncurry(map))
11669 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11670 return isl_map_free(map));
11672 map = isl_map_cow(map);
11673 if (!map)
11674 return NULL;
11676 for (i = 0; i < map->n; ++i) {
11677 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11678 if (!map->p[i])
11679 return isl_map_free(map);
11682 map->dim = isl_space_uncurry(map->dim);
11683 if (!map->dim)
11684 return isl_map_free(map);
11686 return map;
11689 /* Construct a basic map mapping the domain of the affine expression
11690 * to a one-dimensional range prescribed by the affine expression.
11692 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11694 int k;
11695 int pos;
11696 isl_local_space *ls;
11697 isl_basic_map *bmap;
11699 if (!aff)
11700 return NULL;
11702 ls = isl_aff_get_local_space(aff);
11703 bmap = isl_basic_map_from_local_space(ls);
11704 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11705 k = isl_basic_map_alloc_equality(bmap);
11706 if (k < 0)
11707 goto error;
11709 pos = isl_basic_map_offset(bmap, isl_dim_out);
11710 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11711 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11712 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11713 aff->v->size - (pos + 1));
11715 isl_aff_free(aff);
11716 bmap = isl_basic_map_finalize(bmap);
11717 return bmap;
11718 error:
11719 isl_aff_free(aff);
11720 isl_basic_map_free(bmap);
11721 return NULL;
11724 /* Construct a map mapping the domain of the affine expression
11725 * to a one-dimensional range prescribed by the affine expression.
11727 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11729 isl_basic_map *bmap;
11731 bmap = isl_basic_map_from_aff(aff);
11732 return isl_map_from_basic_map(bmap);
11735 /* Construct a basic map mapping the domain the multi-affine expression
11736 * to its range, with each dimension in the range equated to the
11737 * corresponding affine expression.
11739 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11740 __isl_take isl_multi_aff *maff)
11742 int i;
11743 isl_space *space;
11744 isl_basic_map *bmap;
11746 if (!maff)
11747 return NULL;
11749 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11750 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11751 "invalid space", goto error);
11753 space = isl_space_domain(isl_multi_aff_get_space(maff));
11754 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11756 for (i = 0; i < maff->n; ++i) {
11757 isl_aff *aff;
11758 isl_basic_map *bmap_i;
11760 aff = isl_aff_copy(maff->p[i]);
11761 bmap_i = isl_basic_map_from_aff(aff);
11763 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11766 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11768 isl_multi_aff_free(maff);
11769 return bmap;
11770 error:
11771 isl_multi_aff_free(maff);
11772 return NULL;
11775 /* Construct a map mapping the domain the multi-affine expression
11776 * to its range, with each dimension in the range equated to the
11777 * corresponding affine expression.
11779 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11781 isl_basic_map *bmap;
11783 bmap = isl_basic_map_from_multi_aff(maff);
11784 return isl_map_from_basic_map(bmap);
11787 /* Construct a basic map mapping a domain in the given space to
11788 * to an n-dimensional range, with n the number of elements in the list,
11789 * where each coordinate in the range is prescribed by the
11790 * corresponding affine expression.
11791 * The domains of all affine expressions in the list are assumed to match
11792 * domain_dim.
11794 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11795 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11797 int i;
11798 isl_space *dim;
11799 isl_basic_map *bmap;
11801 if (!list)
11802 return NULL;
11804 dim = isl_space_from_domain(domain_dim);
11805 bmap = isl_basic_map_universe(dim);
11807 for (i = 0; i < list->n; ++i) {
11808 isl_aff *aff;
11809 isl_basic_map *bmap_i;
11811 aff = isl_aff_copy(list->p[i]);
11812 bmap_i = isl_basic_map_from_aff(aff);
11814 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11817 isl_aff_list_free(list);
11818 return bmap;
11821 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11822 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11824 return isl_map_equate(set, type1, pos1, type2, pos2);
11827 /* Construct a basic map where the given dimensions are equal to each other.
11829 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11830 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11832 isl_basic_map *bmap = NULL;
11833 int i;
11835 if (!space)
11836 return NULL;
11838 if (pos1 >= isl_space_dim(space, type1))
11839 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11840 "index out of bounds", goto error);
11841 if (pos2 >= isl_space_dim(space, type2))
11842 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11843 "index out of bounds", goto error);
11845 if (type1 == type2 && pos1 == pos2)
11846 return isl_basic_map_universe(space);
11848 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11849 i = isl_basic_map_alloc_equality(bmap);
11850 if (i < 0)
11851 goto error;
11852 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11853 pos1 += isl_basic_map_offset(bmap, type1);
11854 pos2 += isl_basic_map_offset(bmap, type2);
11855 isl_int_set_si(bmap->eq[i][pos1], -1);
11856 isl_int_set_si(bmap->eq[i][pos2], 1);
11857 bmap = isl_basic_map_finalize(bmap);
11858 isl_space_free(space);
11859 return bmap;
11860 error:
11861 isl_space_free(space);
11862 isl_basic_map_free(bmap);
11863 return NULL;
11866 /* Add a constraint imposing that the given two dimensions are equal.
11868 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11869 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11871 isl_basic_map *eq;
11873 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11875 bmap = isl_basic_map_intersect(bmap, eq);
11877 return bmap;
11880 /* Add a constraint imposing that the given two dimensions are equal.
11882 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11883 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11885 isl_basic_map *bmap;
11887 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11889 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11891 return map;
11894 /* Add a constraint imposing that the given two dimensions have opposite values.
11896 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11897 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11899 isl_basic_map *bmap = NULL;
11900 int i;
11902 if (!map)
11903 return NULL;
11905 if (pos1 >= isl_map_dim(map, type1))
11906 isl_die(map->ctx, isl_error_invalid,
11907 "index out of bounds", goto error);
11908 if (pos2 >= isl_map_dim(map, type2))
11909 isl_die(map->ctx, isl_error_invalid,
11910 "index out of bounds", goto error);
11912 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11913 i = isl_basic_map_alloc_equality(bmap);
11914 if (i < 0)
11915 goto error;
11916 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11917 pos1 += isl_basic_map_offset(bmap, type1);
11918 pos2 += isl_basic_map_offset(bmap, type2);
11919 isl_int_set_si(bmap->eq[i][pos1], 1);
11920 isl_int_set_si(bmap->eq[i][pos2], 1);
11921 bmap = isl_basic_map_finalize(bmap);
11923 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11925 return map;
11926 error:
11927 isl_basic_map_free(bmap);
11928 isl_map_free(map);
11929 return NULL;
11932 /* Construct a constraint imposing that the value of the first dimension is
11933 * greater than or equal to that of the second.
11935 static __isl_give isl_constraint *constraint_order_ge(
11936 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11937 enum isl_dim_type type2, int pos2)
11939 isl_constraint *c;
11941 if (!space)
11942 return NULL;
11944 c = isl_inequality_alloc(isl_local_space_from_space(space));
11946 if (pos1 >= isl_constraint_dim(c, type1))
11947 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11948 "index out of bounds", return isl_constraint_free(c));
11949 if (pos2 >= isl_constraint_dim(c, type2))
11950 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11951 "index out of bounds", return isl_constraint_free(c));
11953 if (type1 == type2 && pos1 == pos2)
11954 return c;
11956 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11957 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11959 return c;
11962 /* Add a constraint imposing that the value of the first dimension is
11963 * greater than or equal to that of the second.
11965 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11966 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11968 isl_constraint *c;
11969 isl_space *space;
11971 if (type1 == type2 && pos1 == pos2)
11972 return bmap;
11973 space = isl_basic_map_get_space(bmap);
11974 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11975 bmap = isl_basic_map_add_constraint(bmap, c);
11977 return bmap;
11980 /* Add a constraint imposing that the value of the first dimension is
11981 * greater than or equal to that of the second.
11983 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11984 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11986 isl_constraint *c;
11987 isl_space *space;
11989 if (type1 == type2 && pos1 == pos2)
11990 return map;
11991 space = isl_map_get_space(map);
11992 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11993 map = isl_map_add_constraint(map, c);
11995 return map;
11998 /* Add a constraint imposing that the value of the first dimension is
11999 * less than or equal to that of the second.
12001 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12002 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12004 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12007 /* Construct a basic map where the value of the first dimension is
12008 * greater than that of the second.
12010 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12011 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12013 isl_basic_map *bmap = NULL;
12014 int i;
12016 if (!space)
12017 return NULL;
12019 if (pos1 >= isl_space_dim(space, type1))
12020 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12021 "index out of bounds", goto error);
12022 if (pos2 >= isl_space_dim(space, type2))
12023 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12024 "index out of bounds", goto error);
12026 if (type1 == type2 && pos1 == pos2)
12027 return isl_basic_map_empty(space);
12029 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12030 i = isl_basic_map_alloc_inequality(bmap);
12031 if (i < 0)
12032 return isl_basic_map_free(bmap);
12033 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12034 pos1 += isl_basic_map_offset(bmap, type1);
12035 pos2 += isl_basic_map_offset(bmap, type2);
12036 isl_int_set_si(bmap->ineq[i][pos1], 1);
12037 isl_int_set_si(bmap->ineq[i][pos2], -1);
12038 isl_int_set_si(bmap->ineq[i][0], -1);
12039 bmap = isl_basic_map_finalize(bmap);
12041 return bmap;
12042 error:
12043 isl_space_free(space);
12044 isl_basic_map_free(bmap);
12045 return NULL;
12048 /* Add a constraint imposing that the value of the first dimension is
12049 * greater than that of the second.
12051 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12052 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12054 isl_basic_map *gt;
12056 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12058 bmap = isl_basic_map_intersect(bmap, gt);
12060 return bmap;
12063 /* Add a constraint imposing that the value of the first dimension is
12064 * greater than that of the second.
12066 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12067 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12069 isl_basic_map *bmap;
12071 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12073 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12075 return map;
12078 /* Add a constraint imposing that the value of the first dimension is
12079 * smaller than that of the second.
12081 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12082 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12084 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12087 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12088 int pos)
12090 isl_aff *div;
12091 isl_local_space *ls;
12093 if (!bmap)
12094 return NULL;
12096 if (!isl_basic_map_divs_known(bmap))
12097 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12098 "some divs are unknown", return NULL);
12100 ls = isl_basic_map_get_local_space(bmap);
12101 div = isl_local_space_get_div(ls, pos);
12102 isl_local_space_free(ls);
12104 return div;
12107 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12108 int pos)
12110 return isl_basic_map_get_div(bset, pos);
12113 /* Plug in "subs" for dimension "type", "pos" of "bset".
12115 * Let i be the dimension to replace and let "subs" be of the form
12117 * f/d
12119 * Any integer division with a non-zero coefficient for i,
12121 * floor((a i + g)/m)
12123 * is replaced by
12125 * floor((a f + d g)/(m d))
12127 * Constraints of the form
12129 * a i + g
12131 * are replaced by
12133 * a f + d g
12135 * We currently require that "subs" is an integral expression.
12136 * Handling rational expressions may require us to add stride constraints
12137 * as we do in isl_basic_set_preimage_multi_aff.
12139 __isl_give isl_basic_set *isl_basic_set_substitute(
12140 __isl_take isl_basic_set *bset,
12141 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12143 int i;
12144 isl_int v;
12145 isl_ctx *ctx;
12147 if (bset && isl_basic_set_plain_is_empty(bset))
12148 return bset;
12150 bset = isl_basic_set_cow(bset);
12151 if (!bset || !subs)
12152 goto error;
12154 ctx = isl_basic_set_get_ctx(bset);
12155 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12156 isl_die(ctx, isl_error_invalid,
12157 "spaces don't match", goto error);
12158 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12159 isl_die(ctx, isl_error_unsupported,
12160 "cannot handle divs yet", goto error);
12161 if (!isl_int_is_one(subs->v->el[0]))
12162 isl_die(ctx, isl_error_invalid,
12163 "can only substitute integer expressions", goto error);
12165 pos += isl_basic_set_offset(bset, type);
12167 isl_int_init(v);
12169 for (i = 0; i < bset->n_eq; ++i) {
12170 if (isl_int_is_zero(bset->eq[i][pos]))
12171 continue;
12172 isl_int_set(v, bset->eq[i][pos]);
12173 isl_int_set_si(bset->eq[i][pos], 0);
12174 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12175 v, subs->v->el + 1, subs->v->size - 1);
12178 for (i = 0; i < bset->n_ineq; ++i) {
12179 if (isl_int_is_zero(bset->ineq[i][pos]))
12180 continue;
12181 isl_int_set(v, bset->ineq[i][pos]);
12182 isl_int_set_si(bset->ineq[i][pos], 0);
12183 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12184 v, subs->v->el + 1, subs->v->size - 1);
12187 for (i = 0; i < bset->n_div; ++i) {
12188 if (isl_int_is_zero(bset->div[i][1 + pos]))
12189 continue;
12190 isl_int_set(v, bset->div[i][1 + pos]);
12191 isl_int_set_si(bset->div[i][1 + pos], 0);
12192 isl_seq_combine(bset->div[i] + 1,
12193 subs->v->el[0], bset->div[i] + 1,
12194 v, subs->v->el + 1, subs->v->size - 1);
12195 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12198 isl_int_clear(v);
12200 bset = isl_basic_set_simplify(bset);
12201 return isl_basic_set_finalize(bset);
12202 error:
12203 isl_basic_set_free(bset);
12204 return NULL;
12207 /* Plug in "subs" for dimension "type", "pos" of "set".
12209 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12210 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12212 int i;
12214 if (set && isl_set_plain_is_empty(set))
12215 return set;
12217 set = isl_set_cow(set);
12218 if (!set || !subs)
12219 goto error;
12221 for (i = set->n - 1; i >= 0; --i) {
12222 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12223 if (remove_if_empty(set, i) < 0)
12224 goto error;
12227 return set;
12228 error:
12229 isl_set_free(set);
12230 return NULL;
12233 /* Check if the range of "ma" is compatible with the domain or range
12234 * (depending on "type") of "bmap".
12235 * Return -1 if anything is wrong.
12237 static int check_basic_map_compatible_range_multi_aff(
12238 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12239 __isl_keep isl_multi_aff *ma)
12241 int m;
12242 isl_space *ma_space;
12244 ma_space = isl_multi_aff_get_space(ma);
12246 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12247 if (m < 0)
12248 goto error;
12249 if (!m)
12250 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12251 "parameters don't match", goto error);
12252 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12253 if (m < 0)
12254 goto error;
12255 if (!m)
12256 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12257 "spaces don't match", goto error);
12259 isl_space_free(ma_space);
12260 return m;
12261 error:
12262 isl_space_free(ma_space);
12263 return -1;
12266 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12267 * coefficients before the transformed range of dimensions,
12268 * the "n_after" coefficients after the transformed range of dimensions
12269 * and the coefficients of the other divs in "bmap".
12271 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12272 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12274 int i;
12275 int n_param;
12276 int n_set;
12277 isl_local_space *ls;
12279 if (n_div == 0)
12280 return 0;
12282 ls = isl_aff_get_domain_local_space(ma->p[0]);
12283 if (!ls)
12284 return -1;
12286 n_param = isl_local_space_dim(ls, isl_dim_param);
12287 n_set = isl_local_space_dim(ls, isl_dim_set);
12288 for (i = 0; i < n_div; ++i) {
12289 int o_bmap = 0, o_ls = 0;
12291 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12292 o_bmap += 1 + 1 + n_param;
12293 o_ls += 1 + 1 + n_param;
12294 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12295 o_bmap += n_before;
12296 isl_seq_cpy(bmap->div[i] + o_bmap,
12297 ls->div->row[i] + o_ls, n_set);
12298 o_bmap += n_set;
12299 o_ls += n_set;
12300 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12301 o_bmap += n_after;
12302 isl_seq_cpy(bmap->div[i] + o_bmap,
12303 ls->div->row[i] + o_ls, n_div);
12304 o_bmap += n_div;
12305 o_ls += n_div;
12306 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12307 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12308 goto error;
12311 isl_local_space_free(ls);
12312 return 0;
12313 error:
12314 isl_local_space_free(ls);
12315 return -1;
12318 /* How many stride constraints does "ma" enforce?
12319 * That is, how many of the affine expressions have a denominator
12320 * different from one?
12322 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12324 int i;
12325 int strides = 0;
12327 for (i = 0; i < ma->n; ++i)
12328 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12329 strides++;
12331 return strides;
12334 /* For each affine expression in ma of the form
12336 * x_i = (f_i y + h_i)/m_i
12338 * with m_i different from one, add a constraint to "bmap"
12339 * of the form
12341 * f_i y + h_i = m_i alpha_i
12343 * with alpha_i an additional existentially quantified variable.
12345 static __isl_give isl_basic_map *add_ma_strides(
12346 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12347 int n_before, int n_after)
12349 int i, k;
12350 int div;
12351 int total;
12352 int n_param;
12353 int n_in;
12354 int n_div;
12356 total = isl_basic_map_total_dim(bmap);
12357 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12358 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12359 n_div = isl_multi_aff_dim(ma, isl_dim_div);
12360 for (i = 0; i < ma->n; ++i) {
12361 int o_bmap = 0, o_ma = 1;
12363 if (isl_int_is_one(ma->p[i]->v->el[0]))
12364 continue;
12365 div = isl_basic_map_alloc_div(bmap);
12366 k = isl_basic_map_alloc_equality(bmap);
12367 if (div < 0 || k < 0)
12368 goto error;
12369 isl_int_set_si(bmap->div[div][0], 0);
12370 isl_seq_cpy(bmap->eq[k] + o_bmap,
12371 ma->p[i]->v->el + o_ma, 1 + n_param);
12372 o_bmap += 1 + n_param;
12373 o_ma += 1 + n_param;
12374 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12375 o_bmap += n_before;
12376 isl_seq_cpy(bmap->eq[k] + o_bmap,
12377 ma->p[i]->v->el + o_ma, n_in);
12378 o_bmap += n_in;
12379 o_ma += n_in;
12380 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12381 o_bmap += n_after;
12382 isl_seq_cpy(bmap->eq[k] + o_bmap,
12383 ma->p[i]->v->el + o_ma, n_div);
12384 o_bmap += n_div;
12385 o_ma += n_div;
12386 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12387 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12388 total++;
12391 return bmap;
12392 error:
12393 isl_basic_map_free(bmap);
12394 return NULL;
12397 /* Replace the domain or range space (depending on "type) of "space" by "set".
12399 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12400 enum isl_dim_type type, __isl_take isl_space *set)
12402 if (type == isl_dim_in) {
12403 space = isl_space_range(space);
12404 space = isl_space_map_from_domain_and_range(set, space);
12405 } else {
12406 space = isl_space_domain(space);
12407 space = isl_space_map_from_domain_and_range(space, set);
12410 return space;
12413 /* Compute the preimage of the domain or range (depending on "type")
12414 * of "bmap" under the function represented by "ma".
12415 * In other words, plug in "ma" in the domain or range of "bmap".
12416 * The result is a basic map that lives in the same space as "bmap"
12417 * except that the domain or range has been replaced by
12418 * the domain space of "ma".
12420 * If bmap is represented by
12422 * A(p) + S u + B x + T v + C(divs) >= 0,
12424 * where u and x are input and output dimensions if type == isl_dim_out
12425 * while x and v are input and output dimensions if type == isl_dim_in,
12426 * and ma is represented by
12428 * x = D(p) + F(y) + G(divs')
12430 * then the result is
12432 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12434 * The divs in the input set are similarly adjusted.
12435 * In particular
12437 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12439 * becomes
12441 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12442 * B_i G(divs') + c_i(divs))/n_i)
12444 * If bmap is not a rational map and if F(y) involves any denominators
12446 * x_i = (f_i y + h_i)/m_i
12448 * then additional constraints are added to ensure that we only
12449 * map back integer points. That is we enforce
12451 * f_i y + h_i = m_i alpha_i
12453 * with alpha_i an additional existentially quantified variable.
12455 * We first copy over the divs from "ma".
12456 * Then we add the modified constraints and divs from "bmap".
12457 * Finally, we add the stride constraints, if needed.
12459 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12460 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12461 __isl_take isl_multi_aff *ma)
12463 int i, k;
12464 isl_space *space;
12465 isl_basic_map *res = NULL;
12466 int n_before, n_after, n_div_bmap, n_div_ma;
12467 isl_int f, c1, c2, g;
12468 int rational, strides;
12470 isl_int_init(f);
12471 isl_int_init(c1);
12472 isl_int_init(c2);
12473 isl_int_init(g);
12475 ma = isl_multi_aff_align_divs(ma);
12476 if (!bmap || !ma)
12477 goto error;
12478 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12479 goto error;
12481 if (type == isl_dim_in) {
12482 n_before = 0;
12483 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12484 } else {
12485 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12486 n_after = 0;
12488 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12489 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12491 space = isl_multi_aff_get_domain_space(ma);
12492 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12493 rational = isl_basic_map_is_rational(bmap);
12494 strides = rational ? 0 : multi_aff_strides(ma);
12495 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12496 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12497 if (rational)
12498 res = isl_basic_map_set_rational(res);
12500 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12501 if (isl_basic_map_alloc_div(res) < 0)
12502 goto error;
12504 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12505 goto error;
12507 for (i = 0; i < bmap->n_eq; ++i) {
12508 k = isl_basic_map_alloc_equality(res);
12509 if (k < 0)
12510 goto error;
12511 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12512 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12515 for (i = 0; i < bmap->n_ineq; ++i) {
12516 k = isl_basic_map_alloc_inequality(res);
12517 if (k < 0)
12518 goto error;
12519 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12520 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12523 for (i = 0; i < bmap->n_div; ++i) {
12524 if (isl_int_is_zero(bmap->div[i][0])) {
12525 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12526 continue;
12528 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12529 n_before, n_after, n_div_ma, n_div_bmap,
12530 f, c1, c2, g, 1);
12533 if (strides)
12534 res = add_ma_strides(res, ma, n_before, n_after);
12536 isl_int_clear(f);
12537 isl_int_clear(c1);
12538 isl_int_clear(c2);
12539 isl_int_clear(g);
12540 isl_basic_map_free(bmap);
12541 isl_multi_aff_free(ma);
12542 res = isl_basic_set_simplify(res);
12543 return isl_basic_map_finalize(res);
12544 error:
12545 isl_int_clear(f);
12546 isl_int_clear(c1);
12547 isl_int_clear(c2);
12548 isl_int_clear(g);
12549 isl_basic_map_free(bmap);
12550 isl_multi_aff_free(ma);
12551 isl_basic_map_free(res);
12552 return NULL;
12555 /* Compute the preimage of "bset" under the function represented by "ma".
12556 * In other words, plug in "ma" in "bset". The result is a basic set
12557 * that lives in the domain space of "ma".
12559 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12560 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12562 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12565 /* Compute the preimage of the domain of "bmap" under the function
12566 * represented by "ma".
12567 * In other words, plug in "ma" in the domain of "bmap".
12568 * The result is a basic map that lives in the same space as "bmap"
12569 * except that the domain has been replaced by the domain space of "ma".
12571 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12572 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12574 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12577 /* Compute the preimage of the range of "bmap" under the function
12578 * represented by "ma".
12579 * In other words, plug in "ma" in the range of "bmap".
12580 * The result is a basic map that lives in the same space as "bmap"
12581 * except that the range has been replaced by the domain space of "ma".
12583 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12584 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12586 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12589 /* Check if the range of "ma" is compatible with the domain or range
12590 * (depending on "type") of "map".
12591 * Return -1 if anything is wrong.
12593 static int check_map_compatible_range_multi_aff(
12594 __isl_keep isl_map *map, enum isl_dim_type type,
12595 __isl_keep isl_multi_aff *ma)
12597 int m;
12598 isl_space *ma_space;
12600 ma_space = isl_multi_aff_get_space(ma);
12601 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12602 isl_space_free(ma_space);
12603 if (m >= 0 && !m)
12604 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12605 "spaces don't match", return -1);
12606 return m;
12609 /* Compute the preimage of the domain or range (depending on "type")
12610 * of "map" under the function represented by "ma".
12611 * In other words, plug in "ma" in the domain or range of "map".
12612 * The result is a map that lives in the same space as "map"
12613 * except that the domain or range has been replaced by
12614 * the domain space of "ma".
12616 * The parameters are assumed to have been aligned.
12618 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12619 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12621 int i;
12622 isl_space *space;
12624 map = isl_map_cow(map);
12625 ma = isl_multi_aff_align_divs(ma);
12626 if (!map || !ma)
12627 goto error;
12628 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12629 goto error;
12631 for (i = 0; i < map->n; ++i) {
12632 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12633 isl_multi_aff_copy(ma));
12634 if (!map->p[i])
12635 goto error;
12638 space = isl_multi_aff_get_domain_space(ma);
12639 space = isl_space_set(isl_map_get_space(map), type, space);
12641 isl_space_free(map->dim);
12642 map->dim = space;
12643 if (!map->dim)
12644 goto error;
12646 isl_multi_aff_free(ma);
12647 if (map->n > 1)
12648 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12649 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12650 return map;
12651 error:
12652 isl_multi_aff_free(ma);
12653 isl_map_free(map);
12654 return NULL;
12657 /* Compute the preimage of the domain or range (depending on "type")
12658 * of "map" under the function represented by "ma".
12659 * In other words, plug in "ma" in the domain or range of "map".
12660 * The result is a map that lives in the same space as "map"
12661 * except that the domain or range has been replaced by
12662 * the domain space of "ma".
12664 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12665 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12667 if (!map || !ma)
12668 goto error;
12670 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12671 return map_preimage_multi_aff(map, type, ma);
12673 if (!isl_space_has_named_params(map->dim) ||
12674 !isl_space_has_named_params(ma->space))
12675 isl_die(map->ctx, isl_error_invalid,
12676 "unaligned unnamed parameters", goto error);
12677 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12678 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12680 return map_preimage_multi_aff(map, type, ma);
12681 error:
12682 isl_multi_aff_free(ma);
12683 return isl_map_free(map);
12686 /* Compute the preimage of "set" under the function represented by "ma".
12687 * In other words, plug in "ma" in "set". The result is a set
12688 * that lives in the domain space of "ma".
12690 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12691 __isl_take isl_multi_aff *ma)
12693 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12696 /* Compute the preimage of the domain of "map" under the function
12697 * represented by "ma".
12698 * In other words, plug in "ma" in the domain of "map".
12699 * The result is a map that lives in the same space as "map"
12700 * except that the domain has been replaced by the domain space of "ma".
12702 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12703 __isl_take isl_multi_aff *ma)
12705 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12708 /* Compute the preimage of the range of "map" under the function
12709 * represented by "ma".
12710 * In other words, plug in "ma" in the range of "map".
12711 * The result is a map that lives in the same space as "map"
12712 * except that the range has been replaced by the domain space of "ma".
12714 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12715 __isl_take isl_multi_aff *ma)
12717 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12720 /* Compute the preimage of "map" under the function represented by "pma".
12721 * In other words, plug in "pma" in the domain or range of "map".
12722 * The result is a map that lives in the same space as "map",
12723 * except that the space of type "type" has been replaced by
12724 * the domain space of "pma".
12726 * The parameters of "map" and "pma" are assumed to have been aligned.
12728 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12729 __isl_take isl_map *map, enum isl_dim_type type,
12730 __isl_take isl_pw_multi_aff *pma)
12732 int i;
12733 isl_map *res;
12735 if (!pma)
12736 goto error;
12738 if (pma->n == 0) {
12739 isl_pw_multi_aff_free(pma);
12740 res = isl_map_empty(isl_map_get_space(map));
12741 isl_map_free(map);
12742 return res;
12745 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12746 isl_multi_aff_copy(pma->p[0].maff));
12747 if (type == isl_dim_in)
12748 res = isl_map_intersect_domain(res,
12749 isl_map_copy(pma->p[0].set));
12750 else
12751 res = isl_map_intersect_range(res,
12752 isl_map_copy(pma->p[0].set));
12754 for (i = 1; i < pma->n; ++i) {
12755 isl_map *res_i;
12757 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12758 isl_multi_aff_copy(pma->p[i].maff));
12759 if (type == isl_dim_in)
12760 res_i = isl_map_intersect_domain(res_i,
12761 isl_map_copy(pma->p[i].set));
12762 else
12763 res_i = isl_map_intersect_range(res_i,
12764 isl_map_copy(pma->p[i].set));
12765 res = isl_map_union(res, res_i);
12768 isl_pw_multi_aff_free(pma);
12769 isl_map_free(map);
12770 return res;
12771 error:
12772 isl_pw_multi_aff_free(pma);
12773 isl_map_free(map);
12774 return NULL;
12777 /* Compute the preimage of "map" under the function represented by "pma".
12778 * In other words, plug in "pma" in the domain or range of "map".
12779 * The result is a map that lives in the same space as "map",
12780 * except that the space of type "type" has been replaced by
12781 * the domain space of "pma".
12783 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12784 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12786 if (!map || !pma)
12787 goto error;
12789 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12790 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12792 if (!isl_space_has_named_params(map->dim) ||
12793 !isl_space_has_named_params(pma->dim))
12794 isl_die(map->ctx, isl_error_invalid,
12795 "unaligned unnamed parameters", goto error);
12796 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12797 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12799 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12800 error:
12801 isl_pw_multi_aff_free(pma);
12802 return isl_map_free(map);
12805 /* Compute the preimage of "set" under the function represented by "pma".
12806 * In other words, plug in "pma" in "set". The result is a set
12807 * that lives in the domain space of "pma".
12809 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12810 __isl_take isl_pw_multi_aff *pma)
12812 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12815 /* Compute the preimage of the domain of "map" under the function
12816 * represented by "pma".
12817 * In other words, plug in "pma" in the domain of "map".
12818 * The result is a map that lives in the same space as "map",
12819 * except that domain space has been replaced by the domain space of "pma".
12821 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12822 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12824 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12827 /* Compute the preimage of the range of "map" under the function
12828 * represented by "pma".
12829 * In other words, plug in "pma" in the range of "map".
12830 * The result is a map that lives in the same space as "map",
12831 * except that range space has been replaced by the domain space of "pma".
12833 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12834 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12836 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12839 /* Compute the preimage of "map" under the function represented by "mpa".
12840 * In other words, plug in "mpa" in the domain or range of "map".
12841 * The result is a map that lives in the same space as "map",
12842 * except that the space of type "type" has been replaced by
12843 * the domain space of "mpa".
12845 * If the map does not involve any constraints that refer to the
12846 * dimensions of the substituted space, then the only possible
12847 * effect of "mpa" on the map is to map the space to a different space.
12848 * We create a separate isl_multi_aff to effectuate this change
12849 * in order to avoid spurious splitting of the map along the pieces
12850 * of "mpa".
12852 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12853 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12855 int n;
12856 isl_pw_multi_aff *pma;
12858 if (!map || !mpa)
12859 goto error;
12861 n = isl_map_dim(map, type);
12862 if (!isl_map_involves_dims(map, type, 0, n)) {
12863 isl_space *space;
12864 isl_multi_aff *ma;
12866 space = isl_multi_pw_aff_get_space(mpa);
12867 isl_multi_pw_aff_free(mpa);
12868 ma = isl_multi_aff_zero(space);
12869 return isl_map_preimage_multi_aff(map, type, ma);
12872 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12873 return isl_map_preimage_pw_multi_aff(map, type, pma);
12874 error:
12875 isl_map_free(map);
12876 isl_multi_pw_aff_free(mpa);
12877 return NULL;
12880 /* Compute the preimage of "map" under the function represented by "mpa".
12881 * In other words, plug in "mpa" in the domain "map".
12882 * The result is a map that lives in the same space as "map",
12883 * except that domain space has been replaced by the domain space of "mpa".
12885 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12886 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12888 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12891 /* Compute the preimage of "set" by the function represented by "mpa".
12892 * In other words, plug in "mpa" in "set".
12894 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12895 __isl_take isl_multi_pw_aff *mpa)
12897 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);