add isl_seq_abs_max
[isl.git] / isl_map.c
blob8023814564b721d29b0591b168e469b981189f47
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <string.h>
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
16 #include <isl/blk.h>
17 #include "isl_space_private.h"
18 #include "isl_equalities.h"
19 #include <isl_list_private.h>
20 #include <isl/lp.h>
21 #include <isl/seq.h>
22 #include <isl/set.h>
23 #include <isl/map.h>
24 #include "isl_map_piplib.h"
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
27 #include "isl_tab.h"
28 #include <isl/vec.h>
29 #include <isl_mat_private.h>
30 #include <isl_dim_map.h>
31 #include <isl_local_space_private.h>
32 #include <isl_aff_private.h>
33 #include <isl_options_private.h>
35 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
37 switch (type) {
38 case isl_dim_param: return dim->nparam;
39 case isl_dim_in: return dim->n_in;
40 case isl_dim_out: return dim->n_out;
41 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
42 default: return 0;
46 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
48 switch (type) {
49 case isl_dim_param: return 1;
50 case isl_dim_in: return 1 + dim->nparam;
51 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
52 default: return 0;
56 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
57 enum isl_dim_type type)
59 if (!bmap)
60 return 0;
61 switch (type) {
62 case isl_dim_cst: return 1;
63 case isl_dim_param:
64 case isl_dim_in:
65 case isl_dim_out: return isl_space_dim(bmap->dim, type);
66 case isl_dim_div: return bmap->n_div;
67 case isl_dim_all: return isl_basic_map_total_dim(bmap);
68 default: return 0;
72 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
74 return map ? n(map->dim, type) : 0;
77 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
79 return set ? n(set->dim, type) : 0;
82 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
83 enum isl_dim_type type)
85 isl_space *dim = bmap->dim;
86 switch (type) {
87 case isl_dim_cst: return 0;
88 case isl_dim_param: return 1;
89 case isl_dim_in: return 1 + dim->nparam;
90 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
91 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
92 default: return 0;
96 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
97 enum isl_dim_type type)
99 return isl_basic_map_offset(bset, type);
102 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
104 return pos(map->dim, type);
107 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
108 enum isl_dim_type type)
110 return isl_basic_map_dim(bset, type);
113 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
115 return isl_basic_set_dim(bset, isl_dim_set);
118 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
120 return isl_basic_set_dim(bset, isl_dim_param);
123 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
125 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
128 unsigned isl_set_n_dim(__isl_keep isl_set *set)
130 return isl_set_dim(set, isl_dim_set);
133 unsigned isl_set_n_param(__isl_keep isl_set *set)
135 return isl_set_dim(set, isl_dim_param);
138 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
140 return bmap ? bmap->dim->n_in : 0;
143 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
145 return bmap ? bmap->dim->n_out : 0;
148 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
150 return bmap ? bmap->dim->nparam : 0;
153 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
155 return bmap ? bmap->n_div : 0;
158 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
160 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
163 unsigned isl_map_n_in(const struct isl_map *map)
165 return map ? map->dim->n_in : 0;
168 unsigned isl_map_n_out(const struct isl_map *map)
170 return map ? map->dim->n_out : 0;
173 unsigned isl_map_n_param(const struct isl_map *map)
175 return map ? map->dim->nparam : 0;
178 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
180 int m;
181 if (!map || !set)
182 return -1;
183 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
184 if (m < 0 || !m)
185 return m;
186 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
189 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
190 struct isl_basic_set *bset)
192 int m;
193 if (!bmap || !bset)
194 return -1;
195 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
196 if (m < 0 || !m)
197 return m;
198 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
201 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
203 int m;
204 if (!map || !set)
205 return -1;
206 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
207 if (m < 0 || !m)
208 return m;
209 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
212 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
213 struct isl_basic_set *bset)
215 int m;
216 if (!bmap || !bset)
217 return -1;
218 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
219 if (m < 0 || !m)
220 return m;
221 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
224 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
226 return bmap ? bmap->ctx : NULL;
229 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
231 return bset ? bset->ctx : NULL;
234 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
236 return map ? map->ctx : NULL;
239 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
241 return set ? set->ctx : NULL;
244 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
246 if (!bmap)
247 return NULL;
248 return isl_space_copy(bmap->dim);
251 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
253 if (!bset)
254 return NULL;
255 return isl_space_copy(bset->dim);
258 __isl_give isl_local_space *isl_basic_map_get_local_space(
259 __isl_keep isl_basic_map *bmap)
261 int i;
262 isl_local_space *ls;
263 unsigned total;
265 if (!bmap)
266 return NULL;
268 total = isl_basic_map_total_dim(bmap);
269 ls = isl_local_space_alloc(isl_space_copy(bmap->dim), bmap->n_div);
270 if (!ls)
271 return NULL;
273 for (i = 0; i < bmap->n_div; ++i)
274 isl_seq_cpy(ls->div->row[i], bmap->div[i], 2 + total);
276 return ls;
279 __isl_give isl_local_space *isl_basic_set_get_local_space(
280 __isl_keep isl_basic_set *bset)
282 return isl_basic_map_get_local_space(bset);
285 __isl_give isl_basic_map *isl_basic_map_from_local_space(
286 __isl_take isl_local_space *ls)
288 int i;
289 int n_div;
290 isl_basic_map *bmap;
292 if (!ls)
293 return NULL;
295 n_div = isl_local_space_dim(ls, isl_dim_div);
296 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
297 n_div, 0, 2 * n_div);
299 for (i = 0; i < n_div; ++i)
300 if (isl_basic_map_alloc_div(bmap) < 0)
301 goto error;
303 for (i = 0; i < n_div; ++i) {
304 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
305 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
306 goto error;
309 isl_local_space_free(ls);
310 return bmap;
311 error:
312 isl_local_space_free(ls);
313 isl_basic_map_free(bmap);
314 return NULL;
317 __isl_give isl_basic_set *isl_basic_set_from_local_space(
318 __isl_take isl_local_space *ls)
320 return isl_basic_map_from_local_space(ls);
323 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
325 if (!map)
326 return NULL;
327 return isl_space_copy(map->dim);
330 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
332 if (!set)
333 return NULL;
334 return isl_space_copy(set->dim);
337 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
338 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
340 bmap = isl_basic_map_cow(bmap);
341 if (!bmap)
342 return NULL;
343 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
344 if (!bmap->dim)
345 goto error;
346 bmap = isl_basic_map_finalize(bmap);
347 return bmap;
348 error:
349 isl_basic_map_free(bmap);
350 return NULL;
353 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
354 __isl_take isl_basic_set *bset, const char *s)
356 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
359 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
360 enum isl_dim_type type)
362 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
365 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
366 enum isl_dim_type type, const char *s)
368 int i;
370 map = isl_map_cow(map);
371 if (!map)
372 return NULL;
374 map->dim = isl_space_set_tuple_name(map->dim, type, s);
375 if (!map->dim)
376 goto error;
378 for (i = 0; i < map->n; ++i) {
379 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
380 if (!map->p[i])
381 goto error;
384 return map;
385 error:
386 isl_map_free(map);
387 return NULL;
390 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
391 enum isl_dim_type type)
393 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
396 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
397 const char *s)
399 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
402 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
403 enum isl_dim_type type, __isl_take isl_id *id)
405 map = isl_map_cow(map);
406 if (!map)
407 return isl_id_free(id);
409 map->dim = isl_space_set_tuple_id(map->dim, type, id);
411 return isl_map_reset_space(map, isl_space_copy(map->dim));
414 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
415 __isl_take isl_id *id)
417 return isl_map_set_tuple_id(set, isl_dim_set, id);
420 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
421 enum isl_dim_type type)
423 map = isl_map_cow(map);
424 if (!map)
425 return NULL;
427 map->dim = isl_space_reset_tuple_id(map->dim, type);
429 return isl_map_reset_space(map, isl_space_copy(map->dim));
432 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
434 return isl_map_reset_tuple_id(set, isl_dim_set);
437 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
439 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
442 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
443 enum isl_dim_type type)
445 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
448 int isl_set_has_tuple_id(__isl_keep isl_set *set)
450 return isl_map_has_tuple_id(set, isl_dim_set);
453 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
455 return isl_map_get_tuple_id(set, isl_dim_set);
458 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
460 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
463 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
465 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
468 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
469 enum isl_dim_type type, unsigned pos)
471 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
474 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
475 enum isl_dim_type type, unsigned pos)
477 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
480 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
481 enum isl_dim_type type, unsigned pos)
483 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
486 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
487 enum isl_dim_type type, unsigned pos)
489 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
492 /* Does the given dimension have a name?
494 int isl_set_has_dim_name(__isl_keep isl_set *set,
495 enum isl_dim_type type, unsigned pos)
497 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
500 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
501 __isl_take isl_basic_map *bmap,
502 enum isl_dim_type type, unsigned pos, const char *s)
504 bmap = isl_basic_map_cow(bmap);
505 if (!bmap)
506 return NULL;
507 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
508 if (!bmap->dim)
509 goto error;
510 return bmap;
511 error:
512 isl_basic_map_free(bmap);
513 return NULL;
516 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
517 enum isl_dim_type type, unsigned pos, const char *s)
519 int i;
521 map = isl_map_cow(map);
522 if (!map)
523 return NULL;
525 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
526 if (!map->dim)
527 goto error;
529 for (i = 0; i < map->n; ++i) {
530 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
531 if (!map->p[i])
532 goto error;
535 return map;
536 error:
537 isl_map_free(map);
538 return NULL;
541 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
542 __isl_take isl_basic_set *bset,
543 enum isl_dim_type type, unsigned pos, const char *s)
545 return (isl_basic_set *)isl_basic_map_set_dim_name(
546 (isl_basic_map *)bset, type, pos, s);
549 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
550 enum isl_dim_type type, unsigned pos, const char *s)
552 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
555 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
556 enum isl_dim_type type, unsigned pos)
558 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
561 int isl_map_has_dim_id(__isl_keep isl_map *map,
562 enum isl_dim_type type, unsigned pos)
564 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
567 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
568 enum isl_dim_type type, unsigned pos)
570 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
573 int isl_set_has_dim_id(__isl_keep isl_set *set,
574 enum isl_dim_type type, unsigned pos)
576 return isl_map_has_dim_id(set, type, pos);
579 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
580 enum isl_dim_type type, unsigned pos)
582 return isl_map_get_dim_id(set, type, pos);
585 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
586 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
588 map = isl_map_cow(map);
589 if (!map)
590 return isl_id_free(id);
592 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
594 return isl_map_reset_space(map, isl_space_copy(map->dim));
597 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
598 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
600 return isl_map_set_dim_id(set, type, pos, id);
603 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
604 __isl_keep isl_id *id)
606 if (!map)
607 return -1;
608 return isl_space_find_dim_by_id(map->dim, type, id);
611 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
612 __isl_keep isl_id *id)
614 return isl_map_find_dim_by_id(set, type, id);
617 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
618 const char *name)
620 if (!map)
621 return -1;
622 return isl_space_find_dim_by_name(map->dim, type, name);
625 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
626 const char *name)
628 return isl_map_find_dim_by_name(set, type, name);
631 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
633 if (!bmap)
634 return -1;
635 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
638 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
640 return isl_basic_map_is_rational(bset);
643 /* Is this basic set a parameter domain?
645 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
647 if (!bset)
648 return -1;
649 return isl_space_is_params(bset->dim);
652 /* Is this set a parameter domain?
654 int isl_set_is_params(__isl_keep isl_set *set)
656 if (!set)
657 return -1;
658 return isl_space_is_params(set->dim);
661 /* Is this map actually a parameter domain?
662 * Users should never call this function. Outside of isl,
663 * a map can never be a parameter domain.
665 int isl_map_is_params(__isl_keep isl_map *map)
667 if (!map)
668 return -1;
669 return isl_space_is_params(map->dim);
672 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
673 struct isl_basic_map *bmap, unsigned extra,
674 unsigned n_eq, unsigned n_ineq)
676 int i;
677 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
679 bmap->ctx = ctx;
680 isl_ctx_ref(ctx);
682 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
683 if (isl_blk_is_error(bmap->block))
684 goto error;
686 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
687 if (!bmap->ineq)
688 goto error;
690 if (extra == 0) {
691 bmap->block2 = isl_blk_empty();
692 bmap->div = NULL;
693 } else {
694 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
695 if (isl_blk_is_error(bmap->block2))
696 goto error;
698 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
699 if (!bmap->div)
700 goto error;
703 for (i = 0; i < n_ineq + n_eq; ++i)
704 bmap->ineq[i] = bmap->block.data + i * row_size;
706 for (i = 0; i < extra; ++i)
707 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
709 bmap->ref = 1;
710 bmap->flags = 0;
711 bmap->c_size = n_eq + n_ineq;
712 bmap->eq = bmap->ineq + n_ineq;
713 bmap->extra = extra;
714 bmap->n_eq = 0;
715 bmap->n_ineq = 0;
716 bmap->n_div = 0;
717 bmap->sample = NULL;
719 return bmap;
720 error:
721 isl_basic_map_free(bmap);
722 return NULL;
725 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
726 unsigned nparam, unsigned dim, unsigned extra,
727 unsigned n_eq, unsigned n_ineq)
729 struct isl_basic_map *bmap;
730 isl_space *space;
732 space = isl_space_set_alloc(ctx, nparam, dim);
733 if (!space)
734 return NULL;
736 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
737 return (struct isl_basic_set *)bmap;
740 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
741 unsigned extra, unsigned n_eq, unsigned n_ineq)
743 struct isl_basic_map *bmap;
744 if (!dim)
745 return NULL;
746 isl_assert(dim->ctx, dim->n_in == 0, goto error);
747 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
748 return (struct isl_basic_set *)bmap;
749 error:
750 isl_space_free(dim);
751 return NULL;
754 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
755 unsigned extra, unsigned n_eq, unsigned n_ineq)
757 struct isl_basic_map *bmap;
759 if (!dim)
760 return NULL;
761 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
762 if (!bmap)
763 goto error;
764 bmap->dim = dim;
766 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
767 error:
768 isl_space_free(dim);
769 return NULL;
772 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
773 unsigned nparam, unsigned in, unsigned out, unsigned extra,
774 unsigned n_eq, unsigned n_ineq)
776 struct isl_basic_map *bmap;
777 isl_space *dim;
779 dim = isl_space_alloc(ctx, nparam, in, out);
780 if (!dim)
781 return NULL;
783 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
784 return bmap;
787 static void dup_constraints(
788 struct isl_basic_map *dst, struct isl_basic_map *src)
790 int i;
791 unsigned total = isl_basic_map_total_dim(src);
793 for (i = 0; i < src->n_eq; ++i) {
794 int j = isl_basic_map_alloc_equality(dst);
795 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
798 for (i = 0; i < src->n_ineq; ++i) {
799 int j = isl_basic_map_alloc_inequality(dst);
800 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
803 for (i = 0; i < src->n_div; ++i) {
804 int j = isl_basic_map_alloc_div(dst);
805 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
807 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
810 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
812 struct isl_basic_map *dup;
814 if (!bmap)
815 return NULL;
816 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
817 bmap->n_div, bmap->n_eq, bmap->n_ineq);
818 if (!dup)
819 return NULL;
820 dup_constraints(dup, bmap);
821 dup->flags = bmap->flags;
822 dup->sample = isl_vec_copy(bmap->sample);
823 return dup;
826 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
828 struct isl_basic_map *dup;
830 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
831 return (struct isl_basic_set *)dup;
834 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
836 if (!bset)
837 return NULL;
839 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
840 bset->ref++;
841 return bset;
843 return isl_basic_set_dup(bset);
846 struct isl_set *isl_set_copy(struct isl_set *set)
848 if (!set)
849 return NULL;
851 set->ref++;
852 return set;
855 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
857 if (!bmap)
858 return NULL;
860 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
861 bmap->ref++;
862 return bmap;
864 bmap = isl_basic_map_dup(bmap);
865 if (bmap)
866 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
867 return bmap;
870 struct isl_map *isl_map_copy(struct isl_map *map)
872 if (!map)
873 return NULL;
875 map->ref++;
876 return map;
879 void isl_basic_map_free(struct isl_basic_map *bmap)
881 if (!bmap)
882 return;
884 if (--bmap->ref > 0)
885 return;
887 isl_ctx_deref(bmap->ctx);
888 free(bmap->div);
889 isl_blk_free(bmap->ctx, bmap->block2);
890 free(bmap->ineq);
891 isl_blk_free(bmap->ctx, bmap->block);
892 isl_vec_free(bmap->sample);
893 isl_space_free(bmap->dim);
894 free(bmap);
897 void isl_basic_set_free(struct isl_basic_set *bset)
899 isl_basic_map_free((struct isl_basic_map *)bset);
902 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
904 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
907 __isl_give isl_map *isl_map_align_params_map_map_and(
908 __isl_take isl_map *map1, __isl_take isl_map *map2,
909 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
910 __isl_take isl_map *map2))
912 if (!map1 || !map2)
913 goto error;
914 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
915 return fn(map1, map2);
916 if (!isl_space_has_named_params(map1->dim) ||
917 !isl_space_has_named_params(map2->dim))
918 isl_die(map1->ctx, isl_error_invalid,
919 "unaligned unnamed parameters", goto error);
920 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
921 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
922 return fn(map1, map2);
923 error:
924 isl_map_free(map1);
925 isl_map_free(map2);
926 return NULL;
929 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
930 __isl_keep isl_map *map2,
931 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
933 int r;
935 if (!map1 || !map2)
936 return -1;
937 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
938 return fn(map1, map2);
939 if (!isl_space_has_named_params(map1->dim) ||
940 !isl_space_has_named_params(map2->dim))
941 isl_die(map1->ctx, isl_error_invalid,
942 "unaligned unnamed parameters", return -1);
943 map1 = isl_map_copy(map1);
944 map2 = isl_map_copy(map2);
945 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
946 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
947 r = fn(map1, map2);
948 isl_map_free(map1);
949 isl_map_free(map2);
950 return r;
953 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
955 struct isl_ctx *ctx;
956 if (!bmap)
957 return -1;
958 ctx = bmap->ctx;
959 isl_assert(ctx, room_for_con(bmap, 1), return -1);
960 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
961 return -1);
962 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
963 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
964 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
965 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
966 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
967 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
968 isl_int *t;
969 int j = isl_basic_map_alloc_inequality(bmap);
970 if (j < 0)
971 return -1;
972 t = bmap->ineq[j];
973 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
974 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
975 bmap->eq[-1] = t;
976 bmap->n_eq++;
977 bmap->n_ineq--;
978 bmap->eq--;
979 return 0;
981 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
982 bmap->extra - bmap->n_div);
983 return bmap->n_eq++;
986 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
988 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
991 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
993 if (!bmap)
994 return -1;
995 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
996 bmap->n_eq -= n;
997 return 0;
1000 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1002 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1005 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1007 isl_int *t;
1008 if (!bmap)
1009 return -1;
1010 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1012 if (pos != bmap->n_eq - 1) {
1013 t = bmap->eq[pos];
1014 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1015 bmap->eq[bmap->n_eq - 1] = t;
1017 bmap->n_eq--;
1018 return 0;
1021 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1023 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1026 void isl_basic_map_inequality_to_equality(
1027 struct isl_basic_map *bmap, unsigned pos)
1029 isl_int *t;
1031 t = bmap->ineq[pos];
1032 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1033 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1034 bmap->eq[-1] = t;
1035 bmap->n_eq++;
1036 bmap->n_ineq--;
1037 bmap->eq--;
1038 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1039 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1040 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1041 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1044 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1046 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1049 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1051 struct isl_ctx *ctx;
1052 if (!bmap)
1053 return -1;
1054 ctx = bmap->ctx;
1055 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1056 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1057 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1058 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1059 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1060 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1061 1 + isl_basic_map_total_dim(bmap),
1062 bmap->extra - bmap->n_div);
1063 return bmap->n_ineq++;
1066 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1068 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1071 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1073 if (!bmap)
1074 return -1;
1075 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1076 bmap->n_ineq -= n;
1077 return 0;
1080 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1082 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1085 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1087 isl_int *t;
1088 if (!bmap)
1089 return -1;
1090 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1092 if (pos != bmap->n_ineq - 1) {
1093 t = bmap->ineq[pos];
1094 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1095 bmap->ineq[bmap->n_ineq - 1] = t;
1096 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1098 bmap->n_ineq--;
1099 return 0;
1102 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1104 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1107 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1108 isl_int *eq)
1110 int k;
1112 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1113 if (!bmap)
1114 return NULL;
1115 k = isl_basic_map_alloc_equality(bmap);
1116 if (k < 0)
1117 goto error;
1118 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1119 return bmap;
1120 error:
1121 isl_basic_map_free(bmap);
1122 return NULL;
1125 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1126 isl_int *eq)
1128 return (isl_basic_set *)
1129 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1132 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1133 isl_int *ineq)
1135 int k;
1137 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1138 if (!bmap)
1139 return NULL;
1140 k = isl_basic_map_alloc_inequality(bmap);
1141 if (k < 0)
1142 goto error;
1143 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1144 return bmap;
1145 error:
1146 isl_basic_map_free(bmap);
1147 return NULL;
1150 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1151 isl_int *ineq)
1153 return (isl_basic_set *)
1154 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1157 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1159 if (!bmap)
1160 return -1;
1161 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1162 isl_seq_clr(bmap->div[bmap->n_div] +
1163 1 + 1 + isl_basic_map_total_dim(bmap),
1164 bmap->extra - bmap->n_div);
1165 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1166 return bmap->n_div++;
1169 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1171 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1174 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1176 if (!bmap)
1177 return -1;
1178 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1179 bmap->n_div -= n;
1180 return 0;
1183 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1185 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1188 /* Copy constraint from src to dst, putting the vars of src at offset
1189 * dim_off in dst and the divs of src at offset div_off in dst.
1190 * If both sets are actually map, then dim_off applies to the input
1191 * variables.
1193 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1194 struct isl_basic_map *src_map, isl_int *src,
1195 unsigned in_off, unsigned out_off, unsigned div_off)
1197 unsigned src_nparam = isl_basic_map_n_param(src_map);
1198 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1199 unsigned src_in = isl_basic_map_n_in(src_map);
1200 unsigned dst_in = isl_basic_map_n_in(dst_map);
1201 unsigned src_out = isl_basic_map_n_out(src_map);
1202 unsigned dst_out = isl_basic_map_n_out(dst_map);
1203 isl_int_set(dst[0], src[0]);
1204 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1205 if (dst_nparam > src_nparam)
1206 isl_seq_clr(dst+1+src_nparam,
1207 dst_nparam - src_nparam);
1208 isl_seq_clr(dst+1+dst_nparam, in_off);
1209 isl_seq_cpy(dst+1+dst_nparam+in_off,
1210 src+1+src_nparam,
1211 isl_min(dst_in-in_off, src_in));
1212 if (dst_in-in_off > src_in)
1213 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1214 dst_in - in_off - src_in);
1215 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1216 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1217 src+1+src_nparam+src_in,
1218 isl_min(dst_out-out_off, src_out));
1219 if (dst_out-out_off > src_out)
1220 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1221 dst_out - out_off - src_out);
1222 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1223 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1224 src+1+src_nparam+src_in+src_out,
1225 isl_min(dst_map->extra-div_off, src_map->n_div));
1226 if (dst_map->n_div-div_off > src_map->n_div)
1227 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1228 div_off+src_map->n_div,
1229 dst_map->n_div - div_off - src_map->n_div);
1232 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1233 struct isl_basic_map *src_map, isl_int *src,
1234 unsigned in_off, unsigned out_off, unsigned div_off)
1236 isl_int_set(dst[0], src[0]);
1237 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1240 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1241 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1243 int i;
1244 unsigned div_off;
1246 if (!bmap1 || !bmap2)
1247 goto error;
1249 div_off = bmap1->n_div;
1251 for (i = 0; i < bmap2->n_eq; ++i) {
1252 int i1 = isl_basic_map_alloc_equality(bmap1);
1253 if (i1 < 0)
1254 goto error;
1255 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1256 i_pos, o_pos, div_off);
1259 for (i = 0; i < bmap2->n_ineq; ++i) {
1260 int i1 = isl_basic_map_alloc_inequality(bmap1);
1261 if (i1 < 0)
1262 goto error;
1263 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1264 i_pos, o_pos, div_off);
1267 for (i = 0; i < bmap2->n_div; ++i) {
1268 int i1 = isl_basic_map_alloc_div(bmap1);
1269 if (i1 < 0)
1270 goto error;
1271 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1272 i_pos, o_pos, div_off);
1275 isl_basic_map_free(bmap2);
1277 return bmap1;
1279 error:
1280 isl_basic_map_free(bmap1);
1281 isl_basic_map_free(bmap2);
1282 return NULL;
1285 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1286 struct isl_basic_set *bset2, unsigned pos)
1288 return (struct isl_basic_set *)
1289 add_constraints((struct isl_basic_map *)bset1,
1290 (struct isl_basic_map *)bset2, 0, pos);
1293 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1294 __isl_take isl_space *dim, unsigned extra,
1295 unsigned n_eq, unsigned n_ineq)
1297 struct isl_basic_map *ext;
1298 unsigned flags;
1299 int dims_ok;
1301 if (!dim)
1302 goto error;
1304 if (!base)
1305 goto error;
1307 dims_ok = isl_space_is_equal(base->dim, dim) &&
1308 base->extra >= base->n_div + extra;
1310 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1311 room_for_ineq(base, n_ineq)) {
1312 isl_space_free(dim);
1313 return base;
1316 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1317 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1318 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1319 extra += base->extra;
1320 n_eq += base->n_eq;
1321 n_ineq += base->n_ineq;
1323 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1324 dim = NULL;
1325 if (!ext)
1326 goto error;
1328 if (dims_ok)
1329 ext->sample = isl_vec_copy(base->sample);
1330 flags = base->flags;
1331 ext = add_constraints(ext, base, 0, 0);
1332 if (ext) {
1333 ext->flags = flags;
1334 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1337 return ext;
1339 error:
1340 isl_space_free(dim);
1341 isl_basic_map_free(base);
1342 return NULL;
1345 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1346 __isl_take isl_space *dim, unsigned extra,
1347 unsigned n_eq, unsigned n_ineq)
1349 return (struct isl_basic_set *)
1350 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1351 extra, n_eq, n_ineq);
1354 struct isl_basic_map *isl_basic_map_extend_constraints(
1355 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1357 if (!base)
1358 return NULL;
1359 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1360 0, n_eq, n_ineq);
1363 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1364 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1365 unsigned n_eq, unsigned n_ineq)
1367 struct isl_basic_map *bmap;
1368 isl_space *dim;
1370 if (!base)
1371 return NULL;
1372 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1373 if (!dim)
1374 goto error;
1376 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1377 return bmap;
1378 error:
1379 isl_basic_map_free(base);
1380 return NULL;
1383 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1384 unsigned nparam, unsigned dim, unsigned extra,
1385 unsigned n_eq, unsigned n_ineq)
1387 return (struct isl_basic_set *)
1388 isl_basic_map_extend((struct isl_basic_map *)base,
1389 nparam, 0, dim, extra, n_eq, n_ineq);
1392 struct isl_basic_set *isl_basic_set_extend_constraints(
1393 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1395 return (struct isl_basic_set *)
1396 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1397 n_eq, n_ineq);
1400 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1402 return (struct isl_basic_set *)
1403 isl_basic_map_cow((struct isl_basic_map *)bset);
1406 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1408 if (!bmap)
1409 return NULL;
1411 if (bmap->ref > 1) {
1412 bmap->ref--;
1413 bmap = isl_basic_map_dup(bmap);
1415 if (bmap)
1416 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1417 return bmap;
1420 struct isl_set *isl_set_cow(struct isl_set *set)
1422 if (!set)
1423 return NULL;
1425 if (set->ref == 1)
1426 return set;
1427 set->ref--;
1428 return isl_set_dup(set);
1431 struct isl_map *isl_map_cow(struct isl_map *map)
1433 if (!map)
1434 return NULL;
1436 if (map->ref == 1)
1437 return map;
1438 map->ref--;
1439 return isl_map_dup(map);
1442 static void swap_vars(struct isl_blk blk, isl_int *a,
1443 unsigned a_len, unsigned b_len)
1445 isl_seq_cpy(blk.data, a+a_len, b_len);
1446 isl_seq_cpy(blk.data+b_len, a, a_len);
1447 isl_seq_cpy(a, blk.data, b_len+a_len);
1450 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1451 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1453 int i;
1454 struct isl_blk blk;
1456 if (!bmap)
1457 goto error;
1459 isl_assert(bmap->ctx,
1460 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1462 if (n1 == 0 || n2 == 0)
1463 return bmap;
1465 bmap = isl_basic_map_cow(bmap);
1466 if (!bmap)
1467 return NULL;
1469 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1470 if (isl_blk_is_error(blk))
1471 goto error;
1473 for (i = 0; i < bmap->n_eq; ++i)
1474 swap_vars(blk,
1475 bmap->eq[i] + pos, n1, n2);
1477 for (i = 0; i < bmap->n_ineq; ++i)
1478 swap_vars(blk,
1479 bmap->ineq[i] + pos, n1, n2);
1481 for (i = 0; i < bmap->n_div; ++i)
1482 swap_vars(blk,
1483 bmap->div[i]+1 + pos, n1, n2);
1485 isl_blk_free(bmap->ctx, blk);
1487 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1488 bmap = isl_basic_map_gauss(bmap, NULL);
1489 return isl_basic_map_finalize(bmap);
1490 error:
1491 isl_basic_map_free(bmap);
1492 return NULL;
1495 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1496 __isl_take isl_basic_set *bset, unsigned n)
1498 unsigned dim;
1499 unsigned nparam;
1501 nparam = isl_basic_set_n_param(bset);
1502 dim = isl_basic_set_n_dim(bset);
1503 isl_assert(bset->ctx, n <= dim, goto error);
1505 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1506 error:
1507 isl_basic_set_free(bset);
1508 return NULL;
1511 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1513 int i = 0;
1514 unsigned total;
1515 if (!bmap)
1516 goto error;
1517 total = isl_basic_map_total_dim(bmap);
1518 isl_basic_map_free_div(bmap, bmap->n_div);
1519 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1520 if (bmap->n_eq > 0)
1521 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1522 else {
1523 i = isl_basic_map_alloc_equality(bmap);
1524 if (i < 0)
1525 goto error;
1527 isl_int_set_si(bmap->eq[i][0], 1);
1528 isl_seq_clr(bmap->eq[i]+1, total);
1529 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1530 isl_vec_free(bmap->sample);
1531 bmap->sample = NULL;
1532 return isl_basic_map_finalize(bmap);
1533 error:
1534 isl_basic_map_free(bmap);
1535 return NULL;
1538 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1540 return (struct isl_basic_set *)
1541 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1544 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1546 int i;
1547 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1548 isl_int *t = bmap->div[a];
1549 bmap->div[a] = bmap->div[b];
1550 bmap->div[b] = t;
1552 for (i = 0; i < bmap->n_eq; ++i)
1553 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1555 for (i = 0; i < bmap->n_ineq; ++i)
1556 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1558 for (i = 0; i < bmap->n_div; ++i)
1559 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1560 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1563 /* Eliminate the specified n dimensions starting at first from the
1564 * constraints using Fourier-Motzkin. The dimensions themselves
1565 * are not removed.
1567 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1568 enum isl_dim_type type, unsigned first, unsigned n)
1570 int i;
1572 if (!map)
1573 return NULL;
1574 if (n == 0)
1575 return map;
1577 map = isl_map_cow(map);
1578 if (!map)
1579 return NULL;
1581 for (i = 0; i < map->n; ++i) {
1582 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1583 if (!map->p[i])
1584 goto error;
1586 return map;
1587 error:
1588 isl_map_free(map);
1589 return NULL;
1592 /* Eliminate the specified n dimensions starting at first from the
1593 * constraints using Fourier-Motzkin. The dimensions themselves
1594 * are not removed.
1596 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1597 enum isl_dim_type type, unsigned first, unsigned n)
1599 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1602 /* Eliminate the specified n dimensions starting at first from the
1603 * constraints using Fourier-Motzkin. The dimensions themselves
1604 * are not removed.
1606 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1607 unsigned first, unsigned n)
1609 return isl_set_eliminate(set, isl_dim_set, first, n);
1612 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1613 __isl_take isl_basic_map *bmap)
1615 if (!bmap)
1616 return NULL;
1617 bmap = isl_basic_map_eliminate_vars(bmap,
1618 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1619 if (!bmap)
1620 return NULL;
1621 bmap->n_div = 0;
1622 return isl_basic_map_finalize(bmap);
1625 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1626 __isl_take isl_basic_set *bset)
1628 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1629 (struct isl_basic_map *)bset);
1632 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1634 int i;
1636 if (!map)
1637 return NULL;
1638 if (map->n == 0)
1639 return map;
1641 map = isl_map_cow(map);
1642 if (!map)
1643 return NULL;
1645 for (i = 0; i < map->n; ++i) {
1646 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1647 if (!map->p[i])
1648 goto error;
1650 return map;
1651 error:
1652 isl_map_free(map);
1653 return NULL;
1656 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1658 return isl_map_remove_divs(set);
1661 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1662 enum isl_dim_type type, unsigned first, unsigned n)
1664 if (!bmap)
1665 return NULL;
1666 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1667 goto error);
1668 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1669 return bmap;
1670 bmap = isl_basic_map_eliminate_vars(bmap,
1671 isl_basic_map_offset(bmap, type) - 1 + first, n);
1672 if (!bmap)
1673 return bmap;
1674 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1675 return bmap;
1676 bmap = isl_basic_map_drop(bmap, type, first, n);
1677 return bmap;
1678 error:
1679 isl_basic_map_free(bmap);
1680 return NULL;
1683 /* Return true if the definition of the given div (recursively) involves
1684 * any of the given variables.
1686 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1687 unsigned first, unsigned n)
1689 int i;
1690 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1692 if (isl_int_is_zero(bmap->div[div][0]))
1693 return 0;
1694 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1695 return 1;
1697 for (i = bmap->n_div - 1; i >= 0; --i) {
1698 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1699 continue;
1700 if (div_involves_vars(bmap, i, first, n))
1701 return 1;
1704 return 0;
1707 /* Remove all divs (recursively) involving any of the given dimensions
1708 * in their definitions.
1710 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1711 __isl_take isl_basic_map *bmap,
1712 enum isl_dim_type type, unsigned first, unsigned n)
1714 int i;
1716 if (!bmap)
1717 return NULL;
1718 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1719 goto error);
1720 first += isl_basic_map_offset(bmap, type);
1722 for (i = bmap->n_div - 1; i >= 0; --i) {
1723 if (!div_involves_vars(bmap, i, first, n))
1724 continue;
1725 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1726 if (!bmap)
1727 return NULL;
1728 i = bmap->n_div;
1731 return bmap;
1732 error:
1733 isl_basic_map_free(bmap);
1734 return NULL;
1737 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1738 enum isl_dim_type type, unsigned first, unsigned n)
1740 int i;
1742 if (!map)
1743 return NULL;
1744 if (map->n == 0)
1745 return map;
1747 map = isl_map_cow(map);
1748 if (!map)
1749 return NULL;
1751 for (i = 0; i < map->n; ++i) {
1752 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1753 type, first, n);
1754 if (!map->p[i])
1755 goto error;
1757 return map;
1758 error:
1759 isl_map_free(map);
1760 return NULL;
1763 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1764 enum isl_dim_type type, unsigned first, unsigned n)
1766 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1767 type, first, n);
1770 /* Does the desciption of "bmap" depend on the specified dimensions?
1771 * We also check whether the dimensions appear in any of the div definitions.
1772 * In principle there is no need for this check. If the dimensions appear
1773 * in a div definition, they also appear in the defining constraints of that
1774 * div.
1776 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1777 enum isl_dim_type type, unsigned first, unsigned n)
1779 int i;
1781 if (!bmap)
1782 return -1;
1784 if (first + n > isl_basic_map_dim(bmap, type))
1785 isl_die(bmap->ctx, isl_error_invalid,
1786 "index out of bounds", return -1);
1788 first += isl_basic_map_offset(bmap, type);
1789 for (i = 0; i < bmap->n_eq; ++i)
1790 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1791 return 1;
1792 for (i = 0; i < bmap->n_ineq; ++i)
1793 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1794 return 1;
1795 for (i = 0; i < bmap->n_div; ++i) {
1796 if (isl_int_is_zero(bmap->div[i][0]))
1797 continue;
1798 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
1799 return 1;
1802 return 0;
1805 int isl_map_involves_dims(__isl_keep isl_map *map,
1806 enum isl_dim_type type, unsigned first, unsigned n)
1808 int i;
1810 if (!map)
1811 return -1;
1813 if (first + n > isl_map_dim(map, type))
1814 isl_die(map->ctx, isl_error_invalid,
1815 "index out of bounds", return -1);
1817 for (i = 0; i < map->n; ++i) {
1818 int involves = isl_basic_map_involves_dims(map->p[i],
1819 type, first, n);
1820 if (involves < 0 || involves)
1821 return involves;
1824 return 0;
1827 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1828 enum isl_dim_type type, unsigned first, unsigned n)
1830 return isl_basic_map_involves_dims(bset, type, first, n);
1833 int isl_set_involves_dims(__isl_keep isl_set *set,
1834 enum isl_dim_type type, unsigned first, unsigned n)
1836 return isl_map_involves_dims(set, type, first, n);
1839 /* Return true if the definition of the given div is unknown or depends
1840 * on unknown divs.
1842 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1844 int i;
1845 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1847 if (isl_int_is_zero(bmap->div[div][0]))
1848 return 1;
1850 for (i = bmap->n_div - 1; i >= 0; --i) {
1851 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1852 continue;
1853 if (div_is_unknown(bmap, i))
1854 return 1;
1857 return 0;
1860 /* Remove all divs that are unknown or defined in terms of unknown divs.
1862 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1863 __isl_take isl_basic_map *bmap)
1865 int i;
1867 if (!bmap)
1868 return NULL;
1870 for (i = bmap->n_div - 1; i >= 0; --i) {
1871 if (!div_is_unknown(bmap, i))
1872 continue;
1873 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1874 if (!bmap)
1875 return NULL;
1876 i = bmap->n_div;
1879 return bmap;
1882 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1884 int i;
1886 if (!map)
1887 return NULL;
1888 if (map->n == 0)
1889 return map;
1891 map = isl_map_cow(map);
1892 if (!map)
1893 return NULL;
1895 for (i = 0; i < map->n; ++i) {
1896 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1897 if (!map->p[i])
1898 goto error;
1900 return map;
1901 error:
1902 isl_map_free(map);
1903 return NULL;
1906 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1908 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1911 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1912 __isl_take isl_basic_set *bset,
1913 enum isl_dim_type type, unsigned first, unsigned n)
1915 return (isl_basic_set *)
1916 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1919 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1920 enum isl_dim_type type, unsigned first, unsigned n)
1922 int i;
1924 if (n == 0)
1925 return map;
1927 map = isl_map_cow(map);
1928 if (!map)
1929 return NULL;
1930 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1932 for (i = 0; i < map->n; ++i) {
1933 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1934 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1935 if (!map->p[i])
1936 goto error;
1938 map = isl_map_drop(map, type, first, n);
1939 return map;
1940 error:
1941 isl_map_free(map);
1942 return NULL;
1945 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1946 enum isl_dim_type type, unsigned first, unsigned n)
1948 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1951 /* Project out n inputs starting at first using Fourier-Motzkin */
1952 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1953 unsigned first, unsigned n)
1955 return isl_map_remove_dims(map, isl_dim_in, first, n);
1958 static void dump_term(struct isl_basic_map *bmap,
1959 isl_int c, int pos, FILE *out)
1961 const char *name;
1962 unsigned in = isl_basic_map_n_in(bmap);
1963 unsigned dim = in + isl_basic_map_n_out(bmap);
1964 unsigned nparam = isl_basic_map_n_param(bmap);
1965 if (!pos)
1966 isl_int_print(out, c, 0);
1967 else {
1968 if (!isl_int_is_one(c))
1969 isl_int_print(out, c, 0);
1970 if (pos < 1 + nparam) {
1971 name = isl_space_get_dim_name(bmap->dim,
1972 isl_dim_param, pos - 1);
1973 if (name)
1974 fprintf(out, "%s", name);
1975 else
1976 fprintf(out, "p%d", pos - 1);
1977 } else if (pos < 1 + nparam + in)
1978 fprintf(out, "i%d", pos - 1 - nparam);
1979 else if (pos < 1 + nparam + dim)
1980 fprintf(out, "o%d", pos - 1 - nparam - in);
1981 else
1982 fprintf(out, "e%d", pos - 1 - nparam - dim);
1986 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1987 int sign, FILE *out)
1989 int i;
1990 int first;
1991 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1992 isl_int v;
1994 isl_int_init(v);
1995 for (i = 0, first = 1; i < len; ++i) {
1996 if (isl_int_sgn(c[i]) * sign <= 0)
1997 continue;
1998 if (!first)
1999 fprintf(out, " + ");
2000 first = 0;
2001 isl_int_abs(v, c[i]);
2002 dump_term(bmap, v, i, out);
2004 isl_int_clear(v);
2005 if (first)
2006 fprintf(out, "0");
2009 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2010 const char *op, FILE *out, int indent)
2012 int i;
2014 fprintf(out, "%*s", indent, "");
2016 dump_constraint_sign(bmap, c, 1, out);
2017 fprintf(out, " %s ", op);
2018 dump_constraint_sign(bmap, c, -1, out);
2020 fprintf(out, "\n");
2022 for (i = bmap->n_div; i < bmap->extra; ++i) {
2023 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2024 continue;
2025 fprintf(out, "%*s", indent, "");
2026 fprintf(out, "ERROR: unused div coefficient not zero\n");
2027 abort();
2031 static void dump_constraints(struct isl_basic_map *bmap,
2032 isl_int **c, unsigned n,
2033 const char *op, FILE *out, int indent)
2035 int i;
2037 for (i = 0; i < n; ++i)
2038 dump_constraint(bmap, c[i], op, out, indent);
2041 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2043 int j;
2044 int first = 1;
2045 unsigned total = isl_basic_map_total_dim(bmap);
2047 for (j = 0; j < 1 + total; ++j) {
2048 if (isl_int_is_zero(exp[j]))
2049 continue;
2050 if (!first && isl_int_is_pos(exp[j]))
2051 fprintf(out, "+");
2052 dump_term(bmap, exp[j], j, out);
2053 first = 0;
2057 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2059 int i;
2061 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2062 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2064 for (i = 0; i < bmap->n_div; ++i) {
2065 fprintf(out, "%*s", indent, "");
2066 fprintf(out, "e%d = [(", i);
2067 dump_affine(bmap, bmap->div[i]+1, out);
2068 fprintf(out, ")/");
2069 isl_int_print(out, bmap->div[i][0], 0);
2070 fprintf(out, "]\n");
2074 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2075 FILE *out, int indent)
2077 if (!bset) {
2078 fprintf(out, "null basic set\n");
2079 return;
2082 fprintf(out, "%*s", indent, "");
2083 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2084 bset->ref, bset->dim->nparam, bset->dim->n_out,
2085 bset->extra, bset->flags);
2086 dump((struct isl_basic_map *)bset, out, indent);
2089 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2090 FILE *out, int indent)
2092 if (!bmap) {
2093 fprintf(out, "null basic map\n");
2094 return;
2097 fprintf(out, "%*s", indent, "");
2098 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2099 "flags: %x, n_name: %d\n",
2100 bmap->ref,
2101 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2102 bmap->extra, bmap->flags, bmap->dim->n_id);
2103 dump(bmap, out, indent);
2106 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2108 unsigned total;
2109 if (!bmap)
2110 return -1;
2111 total = isl_basic_map_total_dim(bmap);
2112 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2113 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2114 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2115 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2116 return 0;
2119 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2120 unsigned flags)
2122 struct isl_set *set;
2124 if (!dim)
2125 return NULL;
2126 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2127 isl_assert(dim->ctx, n >= 0, goto error);
2128 set = isl_alloc(dim->ctx, struct isl_set,
2129 sizeof(struct isl_set) +
2130 (n - 1) * sizeof(struct isl_basic_set *));
2131 if (!set)
2132 goto error;
2134 set->ctx = dim->ctx;
2135 isl_ctx_ref(set->ctx);
2136 set->ref = 1;
2137 set->size = n;
2138 set->n = 0;
2139 set->dim = dim;
2140 set->flags = flags;
2141 return set;
2142 error:
2143 isl_space_free(dim);
2144 return NULL;
2147 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2148 unsigned nparam, unsigned dim, int n, unsigned flags)
2150 struct isl_set *set;
2151 isl_space *dims;
2153 dims = isl_space_alloc(ctx, nparam, 0, dim);
2154 if (!dims)
2155 return NULL;
2157 set = isl_set_alloc_space(dims, n, flags);
2158 return set;
2161 /* Make sure "map" has room for at least "n" more basic maps.
2163 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2165 int i;
2166 struct isl_map *grown = NULL;
2168 if (!map)
2169 return NULL;
2170 isl_assert(map->ctx, n >= 0, goto error);
2171 if (map->n + n <= map->size)
2172 return map;
2173 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2174 if (!grown)
2175 goto error;
2176 for (i = 0; i < map->n; ++i) {
2177 grown->p[i] = isl_basic_map_copy(map->p[i]);
2178 if (!grown->p[i])
2179 goto error;
2180 grown->n++;
2182 isl_map_free(map);
2183 return grown;
2184 error:
2185 isl_map_free(grown);
2186 isl_map_free(map);
2187 return NULL;
2190 /* Make sure "set" has room for at least "n" more basic sets.
2192 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2194 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2197 struct isl_set *isl_set_dup(struct isl_set *set)
2199 int i;
2200 struct isl_set *dup;
2202 if (!set)
2203 return NULL;
2205 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2206 if (!dup)
2207 return NULL;
2208 for (i = 0; i < set->n; ++i)
2209 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2210 return dup;
2213 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2215 return isl_map_from_basic_map(bset);
2218 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2220 struct isl_map *map;
2222 if (!bmap)
2223 return NULL;
2225 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2226 return isl_map_add_basic_map(map, bmap);
2229 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2230 __isl_take isl_basic_set *bset)
2232 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2233 (struct isl_basic_map *)bset);
2236 void isl_set_free(struct isl_set *set)
2238 int i;
2240 if (!set)
2241 return;
2243 if (--set->ref > 0)
2244 return;
2246 isl_ctx_deref(set->ctx);
2247 for (i = 0; i < set->n; ++i)
2248 isl_basic_set_free(set->p[i]);
2249 isl_space_free(set->dim);
2250 free(set);
2253 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2255 int i;
2257 if (!set) {
2258 fprintf(out, "null set\n");
2259 return;
2262 fprintf(out, "%*s", indent, "");
2263 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2264 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2265 set->flags);
2266 for (i = 0; i < set->n; ++i) {
2267 fprintf(out, "%*s", indent, "");
2268 fprintf(out, "basic set %d:\n", i);
2269 isl_basic_set_print_internal(set->p[i], out, indent+4);
2273 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2275 int i;
2277 if (!map) {
2278 fprintf(out, "null map\n");
2279 return;
2282 fprintf(out, "%*s", indent, "");
2283 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2284 "flags: %x, n_name: %d\n",
2285 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2286 map->dim->n_out, map->flags, map->dim->n_id);
2287 for (i = 0; i < map->n; ++i) {
2288 fprintf(out, "%*s", indent, "");
2289 fprintf(out, "basic map %d:\n", i);
2290 isl_basic_map_print_internal(map->p[i], out, indent+4);
2294 struct isl_basic_map *isl_basic_map_intersect_domain(
2295 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2297 struct isl_basic_map *bmap_domain;
2299 if (!bmap || !bset)
2300 goto error;
2302 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2303 bset->dim, isl_dim_param), goto error);
2305 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2306 isl_assert(bset->ctx,
2307 isl_basic_map_compatible_domain(bmap, bset), goto error);
2309 bmap = isl_basic_map_cow(bmap);
2310 if (!bmap)
2311 goto error;
2312 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2313 bset->n_div, bset->n_eq, bset->n_ineq);
2314 bmap_domain = isl_basic_map_from_domain(bset);
2315 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2317 bmap = isl_basic_map_simplify(bmap);
2318 return isl_basic_map_finalize(bmap);
2319 error:
2320 isl_basic_map_free(bmap);
2321 isl_basic_set_free(bset);
2322 return NULL;
2325 struct isl_basic_map *isl_basic_map_intersect_range(
2326 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2328 struct isl_basic_map *bmap_range;
2330 if (!bmap || !bset)
2331 goto error;
2333 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2334 bset->dim, isl_dim_param), goto error);
2336 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2337 isl_assert(bset->ctx,
2338 isl_basic_map_compatible_range(bmap, bset), goto error);
2340 if (isl_basic_set_is_universe(bset)) {
2341 isl_basic_set_free(bset);
2342 return bmap;
2345 bmap = isl_basic_map_cow(bmap);
2346 if (!bmap)
2347 goto error;
2348 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2349 bset->n_div, bset->n_eq, bset->n_ineq);
2350 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2351 bmap = add_constraints(bmap, bmap_range, 0, 0);
2353 bmap = isl_basic_map_simplify(bmap);
2354 return isl_basic_map_finalize(bmap);
2355 error:
2356 isl_basic_map_free(bmap);
2357 isl_basic_set_free(bset);
2358 return NULL;
2361 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2363 int i;
2364 unsigned total;
2365 isl_int s;
2367 total = 1 + isl_basic_map_total_dim(bmap);
2368 if (total != vec->size)
2369 return -1;
2371 isl_int_init(s);
2373 for (i = 0; i < bmap->n_eq; ++i) {
2374 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2375 if (!isl_int_is_zero(s)) {
2376 isl_int_clear(s);
2377 return 0;
2381 for (i = 0; i < bmap->n_ineq; ++i) {
2382 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2383 if (isl_int_is_neg(s)) {
2384 isl_int_clear(s);
2385 return 0;
2389 isl_int_clear(s);
2391 return 1;
2394 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2396 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2399 struct isl_basic_map *isl_basic_map_intersect(
2400 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2402 struct isl_vec *sample = NULL;
2404 if (!bmap1 || !bmap2)
2405 goto error;
2407 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2408 bmap2->dim, isl_dim_param), goto error);
2409 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2410 isl_space_dim(bmap1->dim, isl_dim_param) &&
2411 isl_space_dim(bmap2->dim, isl_dim_all) !=
2412 isl_space_dim(bmap2->dim, isl_dim_param))
2413 return isl_basic_map_intersect(bmap2, bmap1);
2415 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2416 isl_space_dim(bmap2->dim, isl_dim_param))
2417 isl_assert(bmap1->ctx,
2418 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2420 if (bmap1->sample &&
2421 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2422 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2423 sample = isl_vec_copy(bmap1->sample);
2424 else if (bmap2->sample &&
2425 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2426 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2427 sample = isl_vec_copy(bmap2->sample);
2429 bmap1 = isl_basic_map_cow(bmap1);
2430 if (!bmap1)
2431 goto error;
2432 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2433 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2434 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2436 if (!bmap1)
2437 isl_vec_free(sample);
2438 else if (sample) {
2439 isl_vec_free(bmap1->sample);
2440 bmap1->sample = sample;
2443 bmap1 = isl_basic_map_simplify(bmap1);
2444 return isl_basic_map_finalize(bmap1);
2445 error:
2446 if (sample)
2447 isl_vec_free(sample);
2448 isl_basic_map_free(bmap1);
2449 isl_basic_map_free(bmap2);
2450 return NULL;
2453 struct isl_basic_set *isl_basic_set_intersect(
2454 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2456 return (struct isl_basic_set *)
2457 isl_basic_map_intersect(
2458 (struct isl_basic_map *)bset1,
2459 (struct isl_basic_map *)bset2);
2462 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2463 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2465 return isl_basic_set_intersect(bset1, bset2);
2468 /* Special case of isl_map_intersect, where both map1 and map2
2469 * are convex, without any divs and such that either map1 or map2
2470 * contains a single constraint. This constraint is then simply
2471 * added to the other map.
2473 static __isl_give isl_map *map_intersect_add_constraint(
2474 __isl_take isl_map *map1, __isl_take isl_map *map2)
2476 isl_assert(map1->ctx, map1->n == 1, goto error);
2477 isl_assert(map2->ctx, map1->n == 1, goto error);
2478 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2479 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2481 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2482 return isl_map_intersect(map2, map1);
2484 isl_assert(map2->ctx,
2485 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2487 map1 = isl_map_cow(map1);
2488 if (!map1)
2489 goto error;
2490 if (isl_map_plain_is_empty(map1)) {
2491 isl_map_free(map2);
2492 return map1;
2494 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2495 if (map2->p[0]->n_eq == 1)
2496 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2497 else
2498 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2499 map2->p[0]->ineq[0]);
2501 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2502 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2503 if (!map1->p[0])
2504 goto error;
2506 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2507 isl_basic_map_free(map1->p[0]);
2508 map1->n = 0;
2511 isl_map_free(map2);
2513 return map1;
2514 error:
2515 isl_map_free(map1);
2516 isl_map_free(map2);
2517 return NULL;
2520 /* map2 may be either a parameter domain or a map living in the same
2521 * space as map1.
2523 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2524 __isl_take isl_map *map2)
2526 unsigned flags = 0;
2527 struct isl_map *result;
2528 int i, j;
2530 if (!map1 || !map2)
2531 goto error;
2533 if (isl_map_plain_is_empty(map1) &&
2534 isl_space_is_equal(map1->dim, map2->dim)) {
2535 isl_map_free(map2);
2536 return map1;
2538 if (isl_map_plain_is_empty(map2) &&
2539 isl_space_is_equal(map1->dim, map2->dim)) {
2540 isl_map_free(map1);
2541 return map2;
2544 if (map1->n == 1 && map2->n == 1 &&
2545 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2546 isl_space_is_equal(map1->dim, map2->dim) &&
2547 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2548 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2549 return map_intersect_add_constraint(map1, map2);
2551 if (isl_space_dim(map2->dim, isl_dim_all) !=
2552 isl_space_dim(map2->dim, isl_dim_param))
2553 isl_assert(map1->ctx,
2554 isl_space_is_equal(map1->dim, map2->dim), goto error);
2556 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2557 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2558 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2560 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2561 map1->n * map2->n, flags);
2562 if (!result)
2563 goto error;
2564 for (i = 0; i < map1->n; ++i)
2565 for (j = 0; j < map2->n; ++j) {
2566 struct isl_basic_map *part;
2567 part = isl_basic_map_intersect(
2568 isl_basic_map_copy(map1->p[i]),
2569 isl_basic_map_copy(map2->p[j]));
2570 if (isl_basic_map_is_empty(part))
2571 isl_basic_map_free(part);
2572 else
2573 result = isl_map_add_basic_map(result, part);
2574 if (!result)
2575 goto error;
2577 isl_map_free(map1);
2578 isl_map_free(map2);
2579 return result;
2580 error:
2581 isl_map_free(map1);
2582 isl_map_free(map2);
2583 return NULL;
2586 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2587 __isl_take isl_map *map2)
2589 if (!map1 || !map2)
2590 goto error;
2591 if (!isl_space_is_equal(map1->dim, map2->dim))
2592 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2593 "spaces don't match", goto error);
2594 return map_intersect_internal(map1, map2);
2595 error:
2596 isl_map_free(map1);
2597 isl_map_free(map2);
2598 return NULL;
2601 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2602 __isl_take isl_map *map2)
2604 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2607 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2609 return (struct isl_set *)
2610 isl_map_intersect((struct isl_map *)set1,
2611 (struct isl_map *)set2);
2614 /* map_intersect_internal accepts intersections
2615 * with parameter domains, so we can just call that function.
2617 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2618 __isl_take isl_set *params)
2620 return map_intersect_internal(map, params);
2623 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2624 __isl_take isl_map *map2)
2626 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2629 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2630 __isl_take isl_set *params)
2632 return isl_map_intersect_params(set, params);
2635 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2637 isl_space *dim;
2638 struct isl_basic_set *bset;
2639 unsigned in;
2641 if (!bmap)
2642 return NULL;
2643 bmap = isl_basic_map_cow(bmap);
2644 if (!bmap)
2645 return NULL;
2646 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2647 in = isl_basic_map_n_in(bmap);
2648 bset = isl_basic_set_from_basic_map(bmap);
2649 bset = isl_basic_set_swap_vars(bset, in);
2650 return isl_basic_map_from_basic_set(bset, dim);
2653 static __isl_give isl_basic_map *basic_map_space_reset(
2654 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2656 isl_space *space;
2658 if (!isl_space_is_named_or_nested(bmap->dim, type))
2659 return bmap;
2661 space = isl_basic_map_get_space(bmap);
2662 space = isl_space_reset(space, type);
2663 bmap = isl_basic_map_reset_space(bmap, space);
2664 return bmap;
2667 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2668 enum isl_dim_type type, unsigned pos, unsigned n)
2670 isl_space *res_dim;
2671 struct isl_basic_map *res;
2672 struct isl_dim_map *dim_map;
2673 unsigned total, off;
2674 enum isl_dim_type t;
2676 if (n == 0)
2677 return basic_map_space_reset(bmap, type);
2679 if (!bmap)
2680 return NULL;
2682 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2684 total = isl_basic_map_total_dim(bmap) + n;
2685 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2686 off = 0;
2687 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2688 if (t != type) {
2689 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2690 } else {
2691 unsigned size = isl_basic_map_dim(bmap, t);
2692 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2693 0, pos, off);
2694 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2695 pos, size - pos, off + pos + n);
2697 off += isl_space_dim(res_dim, t);
2699 isl_dim_map_div(dim_map, bmap, off);
2701 res = isl_basic_map_alloc_space(res_dim,
2702 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2703 if (isl_basic_map_is_rational(bmap))
2704 res = isl_basic_map_set_rational(res);
2705 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2706 return isl_basic_map_finalize(res);
2709 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2710 enum isl_dim_type type, unsigned n)
2712 if (!bmap)
2713 return NULL;
2714 return isl_basic_map_insert(bmap, type,
2715 isl_basic_map_dim(bmap, type), n);
2718 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2719 enum isl_dim_type type, unsigned n)
2721 if (!bset)
2722 return NULL;
2723 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2724 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2725 error:
2726 isl_basic_set_free(bset);
2727 return NULL;
2730 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2731 enum isl_dim_type type)
2733 isl_space *space;
2735 if (!map || !isl_space_is_named_or_nested(map->dim, type))
2736 return map;
2738 space = isl_map_get_space(map);
2739 space = isl_space_reset(space, type);
2740 map = isl_map_reset_space(map, space);
2741 return map;
2744 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2745 enum isl_dim_type type, unsigned pos, unsigned n)
2747 int i;
2749 if (n == 0)
2750 return map_space_reset(map, type);
2752 map = isl_map_cow(map);
2753 if (!map)
2754 return NULL;
2756 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
2757 if (!map->dim)
2758 goto error;
2760 for (i = 0; i < map->n; ++i) {
2761 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2762 if (!map->p[i])
2763 goto error;
2766 return map;
2767 error:
2768 isl_map_free(map);
2769 return NULL;
2772 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
2773 enum isl_dim_type type, unsigned pos, unsigned n)
2775 return isl_map_insert_dims(set, type, pos, n);
2778 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2779 enum isl_dim_type type, unsigned n)
2781 if (!map)
2782 return NULL;
2783 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
2786 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2787 enum isl_dim_type type, unsigned n)
2789 if (!set)
2790 return NULL;
2791 isl_assert(set->ctx, type != isl_dim_in, goto error);
2792 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2793 error:
2794 isl_set_free(set);
2795 return NULL;
2798 __isl_give isl_basic_map *isl_basic_map_move_dims(
2799 __isl_take isl_basic_map *bmap,
2800 enum isl_dim_type dst_type, unsigned dst_pos,
2801 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2803 struct isl_dim_map *dim_map;
2804 struct isl_basic_map *res;
2805 enum isl_dim_type t;
2806 unsigned total, off;
2808 if (!bmap)
2809 return NULL;
2810 if (n == 0)
2811 return bmap;
2813 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2814 goto error);
2816 if (dst_type == src_type && dst_pos == src_pos)
2817 return bmap;
2819 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2821 if (pos(bmap->dim, dst_type) + dst_pos ==
2822 pos(bmap->dim, src_type) + src_pos +
2823 ((src_type < dst_type) ? n : 0)) {
2824 bmap = isl_basic_map_cow(bmap);
2825 if (!bmap)
2826 return NULL;
2828 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2829 src_type, src_pos, n);
2830 if (!bmap->dim)
2831 goto error;
2833 bmap = isl_basic_map_finalize(bmap);
2835 return bmap;
2838 total = isl_basic_map_total_dim(bmap);
2839 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2841 off = 0;
2842 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2843 unsigned size = isl_space_dim(bmap->dim, t);
2844 if (t == dst_type) {
2845 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2846 0, dst_pos, off);
2847 off += dst_pos;
2848 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2849 src_pos, n, off);
2850 off += n;
2851 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2852 dst_pos, size - dst_pos, off);
2853 off += size - dst_pos;
2854 } else if (t == src_type) {
2855 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2856 0, src_pos, off);
2857 off += src_pos;
2858 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2859 src_pos + n, size - src_pos - n, off);
2860 off += size - src_pos - n;
2861 } else {
2862 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2863 off += size;
2866 isl_dim_map_div(dim_map, bmap, off);
2868 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2869 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2870 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2872 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2873 src_type, src_pos, n);
2874 if (!bmap->dim)
2875 goto error;
2877 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2878 bmap = isl_basic_map_gauss(bmap, NULL);
2879 bmap = isl_basic_map_finalize(bmap);
2881 return bmap;
2882 error:
2883 isl_basic_map_free(bmap);
2884 return NULL;
2887 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2888 enum isl_dim_type dst_type, unsigned dst_pos,
2889 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2891 return (isl_basic_set *)isl_basic_map_move_dims(
2892 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2895 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2896 enum isl_dim_type dst_type, unsigned dst_pos,
2897 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2899 if (!set)
2900 return NULL;
2901 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2902 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2903 src_type, src_pos, n);
2904 error:
2905 isl_set_free(set);
2906 return NULL;
2909 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2910 enum isl_dim_type dst_type, unsigned dst_pos,
2911 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2913 int i;
2915 if (!map)
2916 return NULL;
2917 if (n == 0)
2918 return map;
2920 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2921 goto error);
2923 if (dst_type == src_type && dst_pos == src_pos)
2924 return map;
2926 isl_assert(map->ctx, dst_type != src_type, goto error);
2928 map = isl_map_cow(map);
2929 if (!map)
2930 return NULL;
2932 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2933 if (!map->dim)
2934 goto error;
2936 for (i = 0; i < map->n; ++i) {
2937 map->p[i] = isl_basic_map_move_dims(map->p[i],
2938 dst_type, dst_pos,
2939 src_type, src_pos, n);
2940 if (!map->p[i])
2941 goto error;
2944 return map;
2945 error:
2946 isl_map_free(map);
2947 return NULL;
2950 /* Move the specified dimensions to the last columns right before
2951 * the divs. Don't change the dimension specification of bmap.
2952 * That's the responsibility of the caller.
2954 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2955 enum isl_dim_type type, unsigned first, unsigned n)
2957 struct isl_dim_map *dim_map;
2958 struct isl_basic_map *res;
2959 enum isl_dim_type t;
2960 unsigned total, off;
2962 if (!bmap)
2963 return NULL;
2964 if (pos(bmap->dim, type) + first + n ==
2965 1 + isl_space_dim(bmap->dim, isl_dim_all))
2966 return bmap;
2968 total = isl_basic_map_total_dim(bmap);
2969 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2971 off = 0;
2972 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2973 unsigned size = isl_space_dim(bmap->dim, t);
2974 if (t == type) {
2975 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2976 0, first, off);
2977 off += first;
2978 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2979 first, n, total - bmap->n_div - n);
2980 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2981 first + n, size - (first + n), off);
2982 off += size - (first + n);
2983 } else {
2984 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2985 off += size;
2988 isl_dim_map_div(dim_map, bmap, off + n);
2990 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2991 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2992 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2993 return res;
2996 /* Turn the n dimensions of type type, starting at first
2997 * into existentially quantified variables.
2999 __isl_give isl_basic_map *isl_basic_map_project_out(
3000 __isl_take isl_basic_map *bmap,
3001 enum isl_dim_type type, unsigned first, unsigned n)
3003 int i;
3004 size_t row_size;
3005 isl_int **new_div;
3006 isl_int *old;
3008 if (n == 0)
3009 return basic_map_space_reset(bmap, type);
3011 if (!bmap)
3012 return NULL;
3014 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3015 return isl_basic_map_remove_dims(bmap, type, first, n);
3017 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3018 goto error);
3020 bmap = move_last(bmap, type, first, n);
3021 bmap = isl_basic_map_cow(bmap);
3022 if (!bmap)
3023 return NULL;
3025 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3026 old = bmap->block2.data;
3027 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3028 (bmap->extra + n) * (1 + row_size));
3029 if (!bmap->block2.data)
3030 goto error;
3031 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3032 if (!new_div)
3033 goto error;
3034 for (i = 0; i < n; ++i) {
3035 new_div[i] = bmap->block2.data +
3036 (bmap->extra + i) * (1 + row_size);
3037 isl_seq_clr(new_div[i], 1 + row_size);
3039 for (i = 0; i < bmap->extra; ++i)
3040 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3041 free(bmap->div);
3042 bmap->div = new_div;
3043 bmap->n_div += n;
3044 bmap->extra += n;
3046 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3047 if (!bmap->dim)
3048 goto error;
3049 bmap = isl_basic_map_simplify(bmap);
3050 bmap = isl_basic_map_drop_redundant_divs(bmap);
3051 return isl_basic_map_finalize(bmap);
3052 error:
3053 isl_basic_map_free(bmap);
3054 return NULL;
3057 /* Turn the n dimensions of type type, starting at first
3058 * into existentially quantified variables.
3060 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3061 enum isl_dim_type type, unsigned first, unsigned n)
3063 return (isl_basic_set *)isl_basic_map_project_out(
3064 (isl_basic_map *)bset, type, first, n);
3067 /* Turn the n dimensions of type type, starting at first
3068 * into existentially quantified variables.
3070 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3071 enum isl_dim_type type, unsigned first, unsigned n)
3073 int i;
3075 if (!map)
3076 return NULL;
3078 if (n == 0)
3079 return map_space_reset(map, type);
3081 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3083 map = isl_map_cow(map);
3084 if (!map)
3085 return NULL;
3087 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3088 if (!map->dim)
3089 goto error;
3091 for (i = 0; i < map->n; ++i) {
3092 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3093 if (!map->p[i])
3094 goto error;
3097 return map;
3098 error:
3099 isl_map_free(map);
3100 return NULL;
3103 /* Turn the n dimensions of type type, starting at first
3104 * into existentially quantified variables.
3106 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3107 enum isl_dim_type type, unsigned first, unsigned n)
3109 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3112 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3114 int i, j;
3116 for (i = 0; i < n; ++i) {
3117 j = isl_basic_map_alloc_div(bmap);
3118 if (j < 0)
3119 goto error;
3120 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3122 return bmap;
3123 error:
3124 isl_basic_map_free(bmap);
3125 return NULL;
3128 struct isl_basic_map *isl_basic_map_apply_range(
3129 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3131 isl_space *dim_result = NULL;
3132 struct isl_basic_map *bmap;
3133 unsigned n_in, n_out, n, nparam, total, pos;
3134 struct isl_dim_map *dim_map1, *dim_map2;
3136 if (!bmap1 || !bmap2)
3137 goto error;
3139 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3140 isl_space_copy(bmap2->dim));
3142 n_in = isl_basic_map_n_in(bmap1);
3143 n_out = isl_basic_map_n_out(bmap2);
3144 n = isl_basic_map_n_out(bmap1);
3145 nparam = isl_basic_map_n_param(bmap1);
3147 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3148 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3149 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3150 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3151 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3152 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3153 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3154 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3155 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3156 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3157 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3159 bmap = isl_basic_map_alloc_space(dim_result,
3160 bmap1->n_div + bmap2->n_div + n,
3161 bmap1->n_eq + bmap2->n_eq,
3162 bmap1->n_ineq + bmap2->n_ineq);
3163 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3164 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3165 bmap = add_divs(bmap, n);
3166 bmap = isl_basic_map_simplify(bmap);
3167 bmap = isl_basic_map_drop_redundant_divs(bmap);
3168 return isl_basic_map_finalize(bmap);
3169 error:
3170 isl_basic_map_free(bmap1);
3171 isl_basic_map_free(bmap2);
3172 return NULL;
3175 struct isl_basic_set *isl_basic_set_apply(
3176 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3178 if (!bset || !bmap)
3179 goto error;
3181 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3182 goto error);
3184 return (struct isl_basic_set *)
3185 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3186 error:
3187 isl_basic_set_free(bset);
3188 isl_basic_map_free(bmap);
3189 return NULL;
3192 struct isl_basic_map *isl_basic_map_apply_domain(
3193 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3195 if (!bmap1 || !bmap2)
3196 goto error;
3198 isl_assert(bmap1->ctx,
3199 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3200 isl_assert(bmap1->ctx,
3201 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3202 goto error);
3204 bmap1 = isl_basic_map_reverse(bmap1);
3205 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3206 return isl_basic_map_reverse(bmap1);
3207 error:
3208 isl_basic_map_free(bmap1);
3209 isl_basic_map_free(bmap2);
3210 return NULL;
3213 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3214 * A \cap B -> f(A) + f(B)
3216 struct isl_basic_map *isl_basic_map_sum(
3217 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3219 unsigned n_in, n_out, nparam, total, pos;
3220 struct isl_basic_map *bmap = NULL;
3221 struct isl_dim_map *dim_map1, *dim_map2;
3222 int i;
3224 if (!bmap1 || !bmap2)
3225 goto error;
3227 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3228 goto error);
3230 nparam = isl_basic_map_n_param(bmap1);
3231 n_in = isl_basic_map_n_in(bmap1);
3232 n_out = isl_basic_map_n_out(bmap1);
3234 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3235 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3236 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3237 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3238 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3239 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3240 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3241 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3242 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3243 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3244 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3246 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3247 bmap1->n_div + bmap2->n_div + 2 * n_out,
3248 bmap1->n_eq + bmap2->n_eq + n_out,
3249 bmap1->n_ineq + bmap2->n_ineq);
3250 for (i = 0; i < n_out; ++i) {
3251 int j = isl_basic_map_alloc_equality(bmap);
3252 if (j < 0)
3253 goto error;
3254 isl_seq_clr(bmap->eq[j], 1+total);
3255 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3256 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3257 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3259 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3260 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3261 bmap = add_divs(bmap, 2 * n_out);
3263 bmap = isl_basic_map_simplify(bmap);
3264 return isl_basic_map_finalize(bmap);
3265 error:
3266 isl_basic_map_free(bmap);
3267 isl_basic_map_free(bmap1);
3268 isl_basic_map_free(bmap2);
3269 return NULL;
3272 /* Given two maps A -> f(A) and B -> g(B), construct a map
3273 * A \cap B -> f(A) + f(B)
3275 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3277 struct isl_map *result;
3278 int i, j;
3280 if (!map1 || !map2)
3281 goto error;
3283 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3285 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3286 map1->n * map2->n, 0);
3287 if (!result)
3288 goto error;
3289 for (i = 0; i < map1->n; ++i)
3290 for (j = 0; j < map2->n; ++j) {
3291 struct isl_basic_map *part;
3292 part = isl_basic_map_sum(
3293 isl_basic_map_copy(map1->p[i]),
3294 isl_basic_map_copy(map2->p[j]));
3295 if (isl_basic_map_is_empty(part))
3296 isl_basic_map_free(part);
3297 else
3298 result = isl_map_add_basic_map(result, part);
3299 if (!result)
3300 goto error;
3302 isl_map_free(map1);
3303 isl_map_free(map2);
3304 return result;
3305 error:
3306 isl_map_free(map1);
3307 isl_map_free(map2);
3308 return NULL;
3311 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3312 __isl_take isl_set *set2)
3314 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3317 /* Given a basic map A -> f(A), construct A -> -f(A).
3319 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3321 int i, j;
3322 unsigned off, n;
3324 bmap = isl_basic_map_cow(bmap);
3325 if (!bmap)
3326 return NULL;
3328 n = isl_basic_map_dim(bmap, isl_dim_out);
3329 off = isl_basic_map_offset(bmap, isl_dim_out);
3330 for (i = 0; i < bmap->n_eq; ++i)
3331 for (j = 0; j < n; ++j)
3332 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3333 for (i = 0; i < bmap->n_ineq; ++i)
3334 for (j = 0; j < n; ++j)
3335 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3336 for (i = 0; i < bmap->n_div; ++i)
3337 for (j = 0; j < n; ++j)
3338 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3339 bmap = isl_basic_map_gauss(bmap, NULL);
3340 return isl_basic_map_finalize(bmap);
3343 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3345 return isl_basic_map_neg(bset);
3348 /* Given a map A -> f(A), construct A -> -f(A).
3350 struct isl_map *isl_map_neg(struct isl_map *map)
3352 int i;
3354 map = isl_map_cow(map);
3355 if (!map)
3356 return NULL;
3358 for (i = 0; i < map->n; ++i) {
3359 map->p[i] = isl_basic_map_neg(map->p[i]);
3360 if (!map->p[i])
3361 goto error;
3364 return map;
3365 error:
3366 isl_map_free(map);
3367 return NULL;
3370 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3372 return (isl_set *)isl_map_neg((isl_map *)set);
3375 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3376 * A -> floor(f(A)/d).
3378 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3379 isl_int d)
3381 unsigned n_in, n_out, nparam, total, pos;
3382 struct isl_basic_map *result = NULL;
3383 struct isl_dim_map *dim_map;
3384 int i;
3386 if (!bmap)
3387 return NULL;
3389 nparam = isl_basic_map_n_param(bmap);
3390 n_in = isl_basic_map_n_in(bmap);
3391 n_out = isl_basic_map_n_out(bmap);
3393 total = nparam + n_in + n_out + bmap->n_div + n_out;
3394 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3395 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3396 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3397 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3398 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3400 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3401 bmap->n_div + n_out,
3402 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3403 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3404 result = add_divs(result, n_out);
3405 for (i = 0; i < n_out; ++i) {
3406 int j;
3407 j = isl_basic_map_alloc_inequality(result);
3408 if (j < 0)
3409 goto error;
3410 isl_seq_clr(result->ineq[j], 1+total);
3411 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3412 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3413 j = isl_basic_map_alloc_inequality(result);
3414 if (j < 0)
3415 goto error;
3416 isl_seq_clr(result->ineq[j], 1+total);
3417 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3418 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3419 isl_int_sub_ui(result->ineq[j][0], d, 1);
3422 result = isl_basic_map_simplify(result);
3423 return isl_basic_map_finalize(result);
3424 error:
3425 isl_basic_map_free(result);
3426 return NULL;
3429 /* Given a map A -> f(A) and an integer d, construct a map
3430 * A -> floor(f(A)/d).
3432 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3434 int i;
3436 map = isl_map_cow(map);
3437 if (!map)
3438 return NULL;
3440 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3441 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3442 for (i = 0; i < map->n; ++i) {
3443 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3444 if (!map->p[i])
3445 goto error;
3448 return map;
3449 error:
3450 isl_map_free(map);
3451 return NULL;
3454 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3456 int i;
3457 unsigned nparam;
3458 unsigned n_in;
3460 i = isl_basic_map_alloc_equality(bmap);
3461 if (i < 0)
3462 goto error;
3463 nparam = isl_basic_map_n_param(bmap);
3464 n_in = isl_basic_map_n_in(bmap);
3465 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3466 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3467 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3468 return isl_basic_map_finalize(bmap);
3469 error:
3470 isl_basic_map_free(bmap);
3471 return NULL;
3474 /* Add a constraints to "bmap" expressing i_pos < o_pos
3476 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3478 int i;
3479 unsigned nparam;
3480 unsigned n_in;
3482 i = isl_basic_map_alloc_inequality(bmap);
3483 if (i < 0)
3484 goto error;
3485 nparam = isl_basic_map_n_param(bmap);
3486 n_in = isl_basic_map_n_in(bmap);
3487 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3488 isl_int_set_si(bmap->ineq[i][0], -1);
3489 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3490 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3491 return isl_basic_map_finalize(bmap);
3492 error:
3493 isl_basic_map_free(bmap);
3494 return NULL;
3497 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3499 static __isl_give isl_basic_map *var_less_or_equal(
3500 __isl_take isl_basic_map *bmap, unsigned pos)
3502 int i;
3503 unsigned nparam;
3504 unsigned n_in;
3506 i = isl_basic_map_alloc_inequality(bmap);
3507 if (i < 0)
3508 goto error;
3509 nparam = isl_basic_map_n_param(bmap);
3510 n_in = isl_basic_map_n_in(bmap);
3511 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3512 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3513 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3514 return isl_basic_map_finalize(bmap);
3515 error:
3516 isl_basic_map_free(bmap);
3517 return NULL;
3520 /* Add a constraints to "bmap" expressing i_pos > o_pos
3522 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3524 int i;
3525 unsigned nparam;
3526 unsigned n_in;
3528 i = isl_basic_map_alloc_inequality(bmap);
3529 if (i < 0)
3530 goto error;
3531 nparam = isl_basic_map_n_param(bmap);
3532 n_in = isl_basic_map_n_in(bmap);
3533 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3534 isl_int_set_si(bmap->ineq[i][0], -1);
3535 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3536 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3537 return isl_basic_map_finalize(bmap);
3538 error:
3539 isl_basic_map_free(bmap);
3540 return NULL;
3543 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3545 static __isl_give isl_basic_map *var_more_or_equal(
3546 __isl_take isl_basic_map *bmap, unsigned pos)
3548 int i;
3549 unsigned nparam;
3550 unsigned n_in;
3552 i = isl_basic_map_alloc_inequality(bmap);
3553 if (i < 0)
3554 goto error;
3555 nparam = isl_basic_map_n_param(bmap);
3556 n_in = isl_basic_map_n_in(bmap);
3557 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3558 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3559 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3560 return isl_basic_map_finalize(bmap);
3561 error:
3562 isl_basic_map_free(bmap);
3563 return NULL;
3566 __isl_give isl_basic_map *isl_basic_map_equal(
3567 __isl_take isl_space *dim, unsigned n_equal)
3569 int i;
3570 struct isl_basic_map *bmap;
3571 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3572 if (!bmap)
3573 return NULL;
3574 for (i = 0; i < n_equal && bmap; ++i)
3575 bmap = var_equal(bmap, i);
3576 return isl_basic_map_finalize(bmap);
3579 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3581 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3582 unsigned pos)
3584 int i;
3585 struct isl_basic_map *bmap;
3586 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3587 if (!bmap)
3588 return NULL;
3589 for (i = 0; i < pos && bmap; ++i)
3590 bmap = var_equal(bmap, i);
3591 if (bmap)
3592 bmap = var_less(bmap, pos);
3593 return isl_basic_map_finalize(bmap);
3596 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3598 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3599 __isl_take isl_space *dim, unsigned pos)
3601 int i;
3602 isl_basic_map *bmap;
3604 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3605 for (i = 0; i < pos; ++i)
3606 bmap = var_equal(bmap, i);
3607 bmap = var_less_or_equal(bmap, pos);
3608 return isl_basic_map_finalize(bmap);
3611 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3613 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3614 unsigned pos)
3616 int i;
3617 struct isl_basic_map *bmap;
3618 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3619 if (!bmap)
3620 return NULL;
3621 for (i = 0; i < pos && bmap; ++i)
3622 bmap = var_equal(bmap, i);
3623 if (bmap)
3624 bmap = var_more(bmap, pos);
3625 return isl_basic_map_finalize(bmap);
3628 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3630 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3631 __isl_take isl_space *dim, unsigned pos)
3633 int i;
3634 isl_basic_map *bmap;
3636 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3637 for (i = 0; i < pos; ++i)
3638 bmap = var_equal(bmap, i);
3639 bmap = var_more_or_equal(bmap, pos);
3640 return isl_basic_map_finalize(bmap);
3643 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3644 unsigned n, int equal)
3646 struct isl_map *map;
3647 int i;
3649 if (n == 0 && equal)
3650 return isl_map_universe(dims);
3652 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3654 for (i = 0; i + 1 < n; ++i)
3655 map = isl_map_add_basic_map(map,
3656 isl_basic_map_less_at(isl_space_copy(dims), i));
3657 if (n > 0) {
3658 if (equal)
3659 map = isl_map_add_basic_map(map,
3660 isl_basic_map_less_or_equal_at(dims, n - 1));
3661 else
3662 map = isl_map_add_basic_map(map,
3663 isl_basic_map_less_at(dims, n - 1));
3664 } else
3665 isl_space_free(dims);
3667 return map;
3670 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3672 if (!dims)
3673 return NULL;
3674 return map_lex_lte_first(dims, dims->n_out, equal);
3677 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3679 return map_lex_lte_first(dim, n, 0);
3682 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3684 return map_lex_lte_first(dim, n, 1);
3687 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3689 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3692 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3694 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3697 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3698 unsigned n, int equal)
3700 struct isl_map *map;
3701 int i;
3703 if (n == 0 && equal)
3704 return isl_map_universe(dims);
3706 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3708 for (i = 0; i + 1 < n; ++i)
3709 map = isl_map_add_basic_map(map,
3710 isl_basic_map_more_at(isl_space_copy(dims), i));
3711 if (n > 0) {
3712 if (equal)
3713 map = isl_map_add_basic_map(map,
3714 isl_basic_map_more_or_equal_at(dims, n - 1));
3715 else
3716 map = isl_map_add_basic_map(map,
3717 isl_basic_map_more_at(dims, n - 1));
3718 } else
3719 isl_space_free(dims);
3721 return map;
3724 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3726 if (!dims)
3727 return NULL;
3728 return map_lex_gte_first(dims, dims->n_out, equal);
3731 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3733 return map_lex_gte_first(dim, n, 0);
3736 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3738 return map_lex_gte_first(dim, n, 1);
3741 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3743 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3746 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
3748 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
3751 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3752 __isl_take isl_set *set2)
3754 isl_map *map;
3755 map = isl_map_lex_le(isl_set_get_space(set1));
3756 map = isl_map_intersect_domain(map, set1);
3757 map = isl_map_intersect_range(map, set2);
3758 return map;
3761 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3762 __isl_take isl_set *set2)
3764 isl_map *map;
3765 map = isl_map_lex_lt(isl_set_get_space(set1));
3766 map = isl_map_intersect_domain(map, set1);
3767 map = isl_map_intersect_range(map, set2);
3768 return map;
3771 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3772 __isl_take isl_set *set2)
3774 isl_map *map;
3775 map = isl_map_lex_ge(isl_set_get_space(set1));
3776 map = isl_map_intersect_domain(map, set1);
3777 map = isl_map_intersect_range(map, set2);
3778 return map;
3781 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3782 __isl_take isl_set *set2)
3784 isl_map *map;
3785 map = isl_map_lex_gt(isl_set_get_space(set1));
3786 map = isl_map_intersect_domain(map, set1);
3787 map = isl_map_intersect_range(map, set2);
3788 return map;
3791 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3792 __isl_take isl_map *map2)
3794 isl_map *map;
3795 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
3796 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3797 map = isl_map_apply_range(map, isl_map_reverse(map2));
3798 return map;
3801 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3802 __isl_take isl_map *map2)
3804 isl_map *map;
3805 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
3806 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3807 map = isl_map_apply_range(map, isl_map_reverse(map2));
3808 return map;
3811 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3812 __isl_take isl_map *map2)
3814 isl_map *map;
3815 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
3816 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3817 map = isl_map_apply_range(map, isl_map_reverse(map2));
3818 return map;
3821 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3822 __isl_take isl_map *map2)
3824 isl_map *map;
3825 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
3826 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3827 map = isl_map_apply_range(map, isl_map_reverse(map2));
3828 return map;
3831 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
3832 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
3834 struct isl_basic_map *bmap;
3836 bset = isl_basic_set_cow(bset);
3837 if (!bset || !dim)
3838 goto error;
3840 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
3841 isl_space_free(bset->dim);
3842 bmap = (struct isl_basic_map *) bset;
3843 bmap->dim = dim;
3844 return isl_basic_map_finalize(bmap);
3845 error:
3846 isl_basic_set_free(bset);
3847 isl_space_free(dim);
3848 return NULL;
3851 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3853 if (!bmap)
3854 goto error;
3855 if (bmap->dim->n_in == 0)
3856 return (struct isl_basic_set *)bmap;
3857 bmap = isl_basic_map_cow(bmap);
3858 if (!bmap)
3859 goto error;
3860 bmap->dim = isl_space_as_set_space(bmap->dim);
3861 if (!bmap->dim)
3862 goto error;
3863 bmap = isl_basic_map_finalize(bmap);
3864 return (struct isl_basic_set *)bmap;
3865 error:
3866 isl_basic_map_free(bmap);
3867 return NULL;
3870 /* For a div d = floor(f/m), add the constraints
3872 * f - m d >= 0
3873 * -(f-(n-1)) + m d >= 0
3875 * Note that the second constraint is the negation of
3877 * f - m d >= n
3879 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3880 unsigned pos, isl_int *div)
3882 int i, j;
3883 unsigned total = isl_basic_map_total_dim(bmap);
3885 i = isl_basic_map_alloc_inequality(bmap);
3886 if (i < 0)
3887 return -1;
3888 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3889 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3891 j = isl_basic_map_alloc_inequality(bmap);
3892 if (j < 0)
3893 return -1;
3894 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3895 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3896 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3897 return j;
3900 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3901 unsigned pos, isl_int *div)
3903 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3904 pos, div);
3907 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3909 unsigned total = isl_basic_map_total_dim(bmap);
3910 unsigned div_pos = total - bmap->n_div + div;
3912 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3913 bmap->div[div]);
3916 struct isl_basic_set *isl_basic_map_underlying_set(
3917 struct isl_basic_map *bmap)
3919 if (!bmap)
3920 goto error;
3921 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3922 bmap->n_div == 0 &&
3923 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
3924 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
3925 return (struct isl_basic_set *)bmap;
3926 bmap = isl_basic_map_cow(bmap);
3927 if (!bmap)
3928 goto error;
3929 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
3930 if (!bmap->dim)
3931 goto error;
3932 bmap->extra -= bmap->n_div;
3933 bmap->n_div = 0;
3934 bmap = isl_basic_map_finalize(bmap);
3935 return (struct isl_basic_set *)bmap;
3936 error:
3937 return NULL;
3940 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3941 __isl_take isl_basic_set *bset)
3943 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3946 struct isl_basic_map *isl_basic_map_overlying_set(
3947 struct isl_basic_set *bset, struct isl_basic_map *like)
3949 struct isl_basic_map *bmap;
3950 struct isl_ctx *ctx;
3951 unsigned total;
3952 int i;
3954 if (!bset || !like)
3955 goto error;
3956 ctx = bset->ctx;
3957 isl_assert(ctx, bset->n_div == 0, goto error);
3958 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3959 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3960 goto error);
3961 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
3962 isl_basic_map_free(like);
3963 return (struct isl_basic_map *)bset;
3965 bset = isl_basic_set_cow(bset);
3966 if (!bset)
3967 goto error;
3968 total = bset->dim->n_out + bset->extra;
3969 bmap = (struct isl_basic_map *)bset;
3970 isl_space_free(bmap->dim);
3971 bmap->dim = isl_space_copy(like->dim);
3972 if (!bmap->dim)
3973 goto error;
3974 bmap->n_div = like->n_div;
3975 bmap->extra += like->n_div;
3976 if (bmap->extra) {
3977 unsigned ltotal;
3978 isl_int **div;
3979 ltotal = total - bmap->extra + like->extra;
3980 if (ltotal > total)
3981 ltotal = total;
3982 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3983 bmap->extra * (1 + 1 + total));
3984 if (isl_blk_is_error(bmap->block2))
3985 goto error;
3986 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3987 if (!div)
3988 goto error;
3989 bmap->div = div;
3990 for (i = 0; i < bmap->extra; ++i)
3991 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3992 for (i = 0; i < like->n_div; ++i) {
3993 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3994 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3996 bmap = isl_basic_map_extend_constraints(bmap,
3997 0, 2 * like->n_div);
3998 for (i = 0; i < like->n_div; ++i) {
3999 if (isl_int_is_zero(bmap->div[i][0]))
4000 continue;
4001 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4002 goto error;
4005 isl_basic_map_free(like);
4006 bmap = isl_basic_map_simplify(bmap);
4007 bmap = isl_basic_map_finalize(bmap);
4008 return bmap;
4009 error:
4010 isl_basic_map_free(like);
4011 isl_basic_set_free(bset);
4012 return NULL;
4015 struct isl_basic_set *isl_basic_set_from_underlying_set(
4016 struct isl_basic_set *bset, struct isl_basic_set *like)
4018 return (struct isl_basic_set *)
4019 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4022 struct isl_set *isl_set_from_underlying_set(
4023 struct isl_set *set, struct isl_basic_set *like)
4025 int i;
4027 if (!set || !like)
4028 goto error;
4029 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4030 goto error);
4031 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4032 isl_basic_set_free(like);
4033 return set;
4035 set = isl_set_cow(set);
4036 if (!set)
4037 goto error;
4038 for (i = 0; i < set->n; ++i) {
4039 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4040 isl_basic_set_copy(like));
4041 if (!set->p[i])
4042 goto error;
4044 isl_space_free(set->dim);
4045 set->dim = isl_space_copy(like->dim);
4046 if (!set->dim)
4047 goto error;
4048 isl_basic_set_free(like);
4049 return set;
4050 error:
4051 isl_basic_set_free(like);
4052 isl_set_free(set);
4053 return NULL;
4056 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4058 int i;
4060 map = isl_map_cow(map);
4061 if (!map)
4062 return NULL;
4063 map->dim = isl_space_cow(map->dim);
4064 if (!map->dim)
4065 goto error;
4067 for (i = 1; i < map->n; ++i)
4068 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4069 goto error);
4070 for (i = 0; i < map->n; ++i) {
4071 map->p[i] = (struct isl_basic_map *)
4072 isl_basic_map_underlying_set(map->p[i]);
4073 if (!map->p[i])
4074 goto error;
4076 if (map->n == 0)
4077 map->dim = isl_space_underlying(map->dim, 0);
4078 else {
4079 isl_space_free(map->dim);
4080 map->dim = isl_space_copy(map->p[0]->dim);
4082 if (!map->dim)
4083 goto error;
4084 return (struct isl_set *)map;
4085 error:
4086 isl_map_free(map);
4087 return NULL;
4090 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4092 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4095 __isl_give isl_basic_map *isl_basic_map_reset_space(
4096 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4098 bmap = isl_basic_map_cow(bmap);
4099 if (!bmap || !dim)
4100 goto error;
4102 isl_space_free(bmap->dim);
4103 bmap->dim = dim;
4105 bmap = isl_basic_map_finalize(bmap);
4107 return bmap;
4108 error:
4109 isl_basic_map_free(bmap);
4110 isl_space_free(dim);
4111 return NULL;
4114 __isl_give isl_basic_set *isl_basic_set_reset_space(
4115 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4117 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4118 dim);
4121 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4122 __isl_take isl_space *dim)
4124 int i;
4126 map = isl_map_cow(map);
4127 if (!map || !dim)
4128 goto error;
4130 for (i = 0; i < map->n; ++i) {
4131 map->p[i] = isl_basic_map_reset_space(map->p[i],
4132 isl_space_copy(dim));
4133 if (!map->p[i])
4134 goto error;
4136 isl_space_free(map->dim);
4137 map->dim = dim;
4139 return map;
4140 error:
4141 isl_map_free(map);
4142 isl_space_free(dim);
4143 return NULL;
4146 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4147 __isl_take isl_space *dim)
4149 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4152 /* Compute the parameter domain of the given basic set.
4154 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4156 isl_space *space;
4157 unsigned n;
4159 if (isl_basic_set_is_params(bset))
4160 return bset;
4162 n = isl_basic_set_dim(bset, isl_dim_set);
4163 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4164 space = isl_basic_set_get_space(bset);
4165 space = isl_space_params(space);
4166 bset = isl_basic_set_reset_space(bset, space);
4167 return bset;
4170 /* Compute the parameter domain of the given set.
4172 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4174 isl_space *space;
4175 unsigned n;
4177 if (isl_set_is_params(set))
4178 return set;
4180 n = isl_set_dim(set, isl_dim_set);
4181 set = isl_set_project_out(set, isl_dim_set, 0, n);
4182 space = isl_set_get_space(set);
4183 space = isl_space_params(space);
4184 set = isl_set_reset_space(set, space);
4185 return set;
4188 /* Construct a zero-dimensional set with the given parameter domain.
4190 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4192 isl_space *space;
4193 space = isl_set_get_space(set);
4194 space = isl_space_set_from_params(space);
4195 set = isl_set_reset_space(set, space);
4196 return set;
4199 /* Compute the parameter domain of the given map.
4201 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4203 isl_space *space;
4204 unsigned n;
4206 n = isl_map_dim(map, isl_dim_in);
4207 map = isl_map_project_out(map, isl_dim_in, 0, n);
4208 n = isl_map_dim(map, isl_dim_out);
4209 map = isl_map_project_out(map, isl_dim_out, 0, n);
4210 space = isl_map_get_space(map);
4211 space = isl_space_params(space);
4212 map = isl_map_reset_space(map, space);
4213 return map;
4216 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4218 isl_space *dim;
4219 struct isl_basic_set *domain;
4220 unsigned n_in;
4221 unsigned n_out;
4223 if (!bmap)
4224 return NULL;
4225 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4227 n_in = isl_basic_map_n_in(bmap);
4228 n_out = isl_basic_map_n_out(bmap);
4229 domain = isl_basic_set_from_basic_map(bmap);
4230 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4232 domain = isl_basic_set_reset_space(domain, dim);
4234 return domain;
4237 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4239 if (!bmap)
4240 return -1;
4241 return isl_space_may_be_set(bmap->dim);
4244 /* Is this basic map actually a set?
4245 * Users should never call this function. Outside of isl,
4246 * the type should indicate whether something is a set or a map.
4248 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4250 if (!bmap)
4251 return -1;
4252 return isl_space_is_set(bmap->dim);
4255 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4257 if (!bmap)
4258 return NULL;
4259 if (isl_basic_map_is_set(bmap))
4260 return bmap;
4261 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4264 __isl_give isl_basic_map *isl_basic_map_domain_map(
4265 __isl_take isl_basic_map *bmap)
4267 int i, k;
4268 isl_space *dim;
4269 isl_basic_map *domain;
4270 int nparam, n_in, n_out;
4271 unsigned total;
4273 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4274 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4275 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4277 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4278 domain = isl_basic_map_universe(dim);
4280 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4281 bmap = isl_basic_map_apply_range(bmap, domain);
4282 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4284 total = isl_basic_map_total_dim(bmap);
4286 for (i = 0; i < n_in; ++i) {
4287 k = isl_basic_map_alloc_equality(bmap);
4288 if (k < 0)
4289 goto error;
4290 isl_seq_clr(bmap->eq[k], 1 + total);
4291 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4292 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4295 bmap = isl_basic_map_gauss(bmap, NULL);
4296 return isl_basic_map_finalize(bmap);
4297 error:
4298 isl_basic_map_free(bmap);
4299 return NULL;
4302 __isl_give isl_basic_map *isl_basic_map_range_map(
4303 __isl_take isl_basic_map *bmap)
4305 int i, k;
4306 isl_space *dim;
4307 isl_basic_map *range;
4308 int nparam, n_in, n_out;
4309 unsigned total;
4311 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4312 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4313 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4315 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4316 range = isl_basic_map_universe(dim);
4318 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4319 bmap = isl_basic_map_apply_range(bmap, range);
4320 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4322 total = isl_basic_map_total_dim(bmap);
4324 for (i = 0; i < n_out; ++i) {
4325 k = isl_basic_map_alloc_equality(bmap);
4326 if (k < 0)
4327 goto error;
4328 isl_seq_clr(bmap->eq[k], 1 + total);
4329 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4330 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4333 bmap = isl_basic_map_gauss(bmap, NULL);
4334 return isl_basic_map_finalize(bmap);
4335 error:
4336 isl_basic_map_free(bmap);
4337 return NULL;
4340 int isl_map_may_be_set(__isl_keep isl_map *map)
4342 if (!map)
4343 return -1;
4344 return isl_space_may_be_set(map->dim);
4347 /* Is this map actually a set?
4348 * Users should never call this function. Outside of isl,
4349 * the type should indicate whether something is a set or a map.
4351 int isl_map_is_set(__isl_keep isl_map *map)
4353 if (!map)
4354 return -1;
4355 return isl_space_is_set(map->dim);
4358 struct isl_set *isl_map_range(struct isl_map *map)
4360 int i;
4361 struct isl_set *set;
4363 if (!map)
4364 goto error;
4365 if (isl_map_is_set(map))
4366 return (isl_set *)map;
4368 map = isl_map_cow(map);
4369 if (!map)
4370 goto error;
4372 set = (struct isl_set *) map;
4373 set->dim = isl_space_range(set->dim);
4374 if (!set->dim)
4375 goto error;
4376 for (i = 0; i < map->n; ++i) {
4377 set->p[i] = isl_basic_map_range(map->p[i]);
4378 if (!set->p[i])
4379 goto error;
4381 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4382 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4383 return set;
4384 error:
4385 isl_map_free(map);
4386 return NULL;
4389 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4391 int i;
4392 isl_space *domain_dim;
4394 map = isl_map_cow(map);
4395 if (!map)
4396 return NULL;
4398 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4399 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4400 map->dim = isl_space_join(map->dim, domain_dim);
4401 if (!map->dim)
4402 goto error;
4403 for (i = 0; i < map->n; ++i) {
4404 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4405 if (!map->p[i])
4406 goto error;
4408 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4409 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4410 return map;
4411 error:
4412 isl_map_free(map);
4413 return NULL;
4416 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4418 int i;
4419 isl_space *range_dim;
4421 map = isl_map_cow(map);
4422 if (!map)
4423 return NULL;
4425 range_dim = isl_space_range(isl_map_get_space(map));
4426 range_dim = isl_space_from_range(range_dim);
4427 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4428 map->dim = isl_space_join(map->dim, range_dim);
4429 if (!map->dim)
4430 goto error;
4431 for (i = 0; i < map->n; ++i) {
4432 map->p[i] = isl_basic_map_range_map(map->p[i]);
4433 if (!map->p[i])
4434 goto error;
4436 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4437 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4438 return map;
4439 error:
4440 isl_map_free(map);
4441 return NULL;
4444 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4445 __isl_take isl_space *dim)
4447 int i;
4448 struct isl_map *map = NULL;
4450 set = isl_set_cow(set);
4451 if (!set || !dim)
4452 goto error;
4453 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4454 map = (struct isl_map *)set;
4455 for (i = 0; i < set->n; ++i) {
4456 map->p[i] = isl_basic_map_from_basic_set(
4457 set->p[i], isl_space_copy(dim));
4458 if (!map->p[i])
4459 goto error;
4461 isl_space_free(map->dim);
4462 map->dim = dim;
4463 return map;
4464 error:
4465 isl_space_free(dim);
4466 isl_set_free(set);
4467 return NULL;
4470 __isl_give isl_basic_map *isl_basic_map_from_domain(
4471 __isl_take isl_basic_set *bset)
4473 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4476 __isl_give isl_basic_map *isl_basic_map_from_range(
4477 __isl_take isl_basic_set *bset)
4479 isl_space *space;
4480 space = isl_basic_set_get_space(bset);
4481 space = isl_space_from_range(space);
4482 bset = isl_basic_set_reset_space(bset, space);
4483 return (isl_basic_map *)bset;
4486 struct isl_map *isl_map_from_range(struct isl_set *set)
4488 isl_space *space;
4489 space = isl_set_get_space(set);
4490 space = isl_space_from_range(space);
4491 set = isl_set_reset_space(set, space);
4492 return (struct isl_map *)set;
4495 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4497 return isl_map_reverse(isl_map_from_range(set));
4500 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4501 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4503 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4506 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4507 __isl_take isl_set *range)
4509 return isl_map_apply_range(isl_map_reverse(domain), range);
4512 struct isl_set *isl_set_from_map(struct isl_map *map)
4514 int i;
4515 struct isl_set *set = NULL;
4517 if (!map)
4518 return NULL;
4519 map = isl_map_cow(map);
4520 if (!map)
4521 return NULL;
4522 map->dim = isl_space_as_set_space(map->dim);
4523 if (!map->dim)
4524 goto error;
4525 set = (struct isl_set *)map;
4526 for (i = 0; i < map->n; ++i) {
4527 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4528 if (!set->p[i])
4529 goto error;
4531 return set;
4532 error:
4533 isl_map_free(map);
4534 return NULL;
4537 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4538 unsigned flags)
4540 struct isl_map *map;
4542 if (!dim)
4543 return NULL;
4544 if (n < 0)
4545 isl_die(dim->ctx, isl_error_internal,
4546 "negative number of basic maps", goto error);
4547 map = isl_alloc(dim->ctx, struct isl_map,
4548 sizeof(struct isl_map) +
4549 (n - 1) * sizeof(struct isl_basic_map *));
4550 if (!map)
4551 goto error;
4553 map->ctx = dim->ctx;
4554 isl_ctx_ref(map->ctx);
4555 map->ref = 1;
4556 map->size = n;
4557 map->n = 0;
4558 map->dim = dim;
4559 map->flags = flags;
4560 return map;
4561 error:
4562 isl_space_free(dim);
4563 return NULL;
4566 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4567 unsigned nparam, unsigned in, unsigned out, int n,
4568 unsigned flags)
4570 struct isl_map *map;
4571 isl_space *dims;
4573 dims = isl_space_alloc(ctx, nparam, in, out);
4574 if (!dims)
4575 return NULL;
4577 map = isl_map_alloc_space(dims, n, flags);
4578 return map;
4581 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4583 struct isl_basic_map *bmap;
4584 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4585 bmap = isl_basic_map_set_to_empty(bmap);
4586 return bmap;
4589 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4591 struct isl_basic_set *bset;
4592 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4593 bset = isl_basic_set_set_to_empty(bset);
4594 return bset;
4597 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4599 struct isl_basic_map *bmap;
4600 if (!model)
4601 return NULL;
4602 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4603 bmap = isl_basic_map_set_to_empty(bmap);
4604 return bmap;
4607 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4609 struct isl_basic_map *bmap;
4610 if (!model)
4611 return NULL;
4612 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4613 bmap = isl_basic_map_set_to_empty(bmap);
4614 return bmap;
4617 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4619 struct isl_basic_set *bset;
4620 if (!model)
4621 return NULL;
4622 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4623 bset = isl_basic_set_set_to_empty(bset);
4624 return bset;
4627 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4629 struct isl_basic_map *bmap;
4630 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4631 bmap = isl_basic_map_finalize(bmap);
4632 return bmap;
4635 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4637 struct isl_basic_set *bset;
4638 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4639 bset = isl_basic_set_finalize(bset);
4640 return bset;
4643 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4645 int i;
4646 unsigned total = isl_space_dim(dim, isl_dim_all);
4647 isl_basic_map *bmap;
4649 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4650 for (i = 0; i < total; ++i) {
4651 int k = isl_basic_map_alloc_inequality(bmap);
4652 if (k < 0)
4653 goto error;
4654 isl_seq_clr(bmap->ineq[k], 1 + total);
4655 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4657 return bmap;
4658 error:
4659 isl_basic_map_free(bmap);
4660 return NULL;
4663 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4665 return isl_basic_map_nat_universe(dim);
4668 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4670 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4673 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4675 return isl_map_nat_universe(dim);
4678 __isl_give isl_basic_map *isl_basic_map_universe_like(
4679 __isl_keep isl_basic_map *model)
4681 if (!model)
4682 return NULL;
4683 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4686 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4688 if (!model)
4689 return NULL;
4690 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4693 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4694 __isl_keep isl_set *model)
4696 if (!model)
4697 return NULL;
4698 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4701 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4703 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4706 struct isl_map *isl_map_empty_like(struct isl_map *model)
4708 if (!model)
4709 return NULL;
4710 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4713 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4715 if (!model)
4716 return NULL;
4717 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4720 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4722 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4725 struct isl_set *isl_set_empty_like(struct isl_set *model)
4727 if (!model)
4728 return NULL;
4729 return isl_set_empty(isl_space_copy(model->dim));
4732 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4734 struct isl_map *map;
4735 if (!dim)
4736 return NULL;
4737 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4738 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4739 return map;
4742 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4744 struct isl_set *set;
4745 if (!dim)
4746 return NULL;
4747 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4748 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4749 return set;
4752 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4754 if (!model)
4755 return NULL;
4756 return isl_set_universe(isl_space_copy(model->dim));
4759 struct isl_map *isl_map_dup(struct isl_map *map)
4761 int i;
4762 struct isl_map *dup;
4764 if (!map)
4765 return NULL;
4766 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
4767 for (i = 0; i < map->n; ++i)
4768 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4769 return dup;
4772 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4773 __isl_take isl_basic_map *bmap)
4775 if (!bmap || !map)
4776 goto error;
4777 if (isl_basic_map_plain_is_empty(bmap)) {
4778 isl_basic_map_free(bmap);
4779 return map;
4781 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
4782 isl_assert(map->ctx, map->n < map->size, goto error);
4783 map->p[map->n] = bmap;
4784 map->n++;
4785 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4786 return map;
4787 error:
4788 if (map)
4789 isl_map_free(map);
4790 if (bmap)
4791 isl_basic_map_free(bmap);
4792 return NULL;
4795 void isl_map_free(struct isl_map *map)
4797 int i;
4799 if (!map)
4800 return;
4802 if (--map->ref > 0)
4803 return;
4805 isl_ctx_deref(map->ctx);
4806 for (i = 0; i < map->n; ++i)
4807 isl_basic_map_free(map->p[i]);
4808 isl_space_free(map->dim);
4809 free(map);
4812 struct isl_map *isl_map_extend(struct isl_map *base,
4813 unsigned nparam, unsigned n_in, unsigned n_out)
4815 int i;
4817 base = isl_map_cow(base);
4818 if (!base)
4819 return NULL;
4821 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
4822 if (!base->dim)
4823 goto error;
4824 for (i = 0; i < base->n; ++i) {
4825 base->p[i] = isl_basic_map_extend_space(base->p[i],
4826 isl_space_copy(base->dim), 0, 0, 0);
4827 if (!base->p[i])
4828 goto error;
4830 return base;
4831 error:
4832 isl_map_free(base);
4833 return NULL;
4836 struct isl_set *isl_set_extend(struct isl_set *base,
4837 unsigned nparam, unsigned dim)
4839 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4840 nparam, 0, dim);
4843 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4844 struct isl_basic_map *bmap, unsigned pos, int value)
4846 int j;
4848 bmap = isl_basic_map_cow(bmap);
4849 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4850 j = isl_basic_map_alloc_equality(bmap);
4851 if (j < 0)
4852 goto error;
4853 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4854 isl_int_set_si(bmap->eq[j][pos], -1);
4855 isl_int_set_si(bmap->eq[j][0], value);
4856 bmap = isl_basic_map_simplify(bmap);
4857 return isl_basic_map_finalize(bmap);
4858 error:
4859 isl_basic_map_free(bmap);
4860 return NULL;
4863 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4864 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4866 int j;
4868 bmap = isl_basic_map_cow(bmap);
4869 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4870 j = isl_basic_map_alloc_equality(bmap);
4871 if (j < 0)
4872 goto error;
4873 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4874 isl_int_set_si(bmap->eq[j][pos], -1);
4875 isl_int_set(bmap->eq[j][0], value);
4876 bmap = isl_basic_map_simplify(bmap);
4877 return isl_basic_map_finalize(bmap);
4878 error:
4879 isl_basic_map_free(bmap);
4880 return NULL;
4883 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4884 enum isl_dim_type type, unsigned pos, int value)
4886 if (!bmap)
4887 return NULL;
4888 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4889 return isl_basic_map_fix_pos_si(bmap,
4890 isl_basic_map_offset(bmap, type) + pos, value);
4891 error:
4892 isl_basic_map_free(bmap);
4893 return NULL;
4896 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4897 enum isl_dim_type type, unsigned pos, isl_int value)
4899 if (!bmap)
4900 return NULL;
4901 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4902 return isl_basic_map_fix_pos(bmap,
4903 isl_basic_map_offset(bmap, type) + pos, value);
4904 error:
4905 isl_basic_map_free(bmap);
4906 return NULL;
4909 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4910 enum isl_dim_type type, unsigned pos, int value)
4912 return (struct isl_basic_set *)
4913 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4914 type, pos, value);
4917 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4918 enum isl_dim_type type, unsigned pos, isl_int value)
4920 return (struct isl_basic_set *)
4921 isl_basic_map_fix((struct isl_basic_map *)bset,
4922 type, pos, value);
4925 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4926 unsigned input, int value)
4928 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4931 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4932 unsigned dim, int value)
4934 return (struct isl_basic_set *)
4935 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4936 isl_dim_set, dim, value);
4939 static int remove_if_empty(__isl_keep isl_map *map, int i)
4941 int empty = isl_basic_map_plain_is_empty(map->p[i]);
4943 if (empty < 0)
4944 return -1;
4945 if (!empty)
4946 return 0;
4948 isl_basic_map_free(map->p[i]);
4949 if (i != map->n - 1) {
4950 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4951 map->p[i] = map->p[map->n - 1];
4953 map->n--;
4955 return 0;
4958 struct isl_map *isl_map_fix_si(struct isl_map *map,
4959 enum isl_dim_type type, unsigned pos, int value)
4961 int i;
4963 map = isl_map_cow(map);
4964 if (!map)
4965 return NULL;
4967 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4968 for (i = map->n - 1; i >= 0; --i) {
4969 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4970 if (remove_if_empty(map, i) < 0)
4971 goto error;
4973 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4974 return map;
4975 error:
4976 isl_map_free(map);
4977 return NULL;
4980 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4981 enum isl_dim_type type, unsigned pos, int value)
4983 return (struct isl_set *)
4984 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4987 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4988 enum isl_dim_type type, unsigned pos, isl_int value)
4990 int i;
4992 map = isl_map_cow(map);
4993 if (!map)
4994 return NULL;
4996 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4997 for (i = 0; i < map->n; ++i) {
4998 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4999 if (!map->p[i])
5000 goto error;
5002 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5003 return map;
5004 error:
5005 isl_map_free(map);
5006 return NULL;
5009 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5010 enum isl_dim_type type, unsigned pos, isl_int value)
5012 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5015 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5016 unsigned input, int value)
5018 return isl_map_fix_si(map, isl_dim_in, input, value);
5021 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5023 return (struct isl_set *)
5024 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5027 static __isl_give isl_basic_map *basic_map_bound_si(
5028 __isl_take isl_basic_map *bmap,
5029 enum isl_dim_type type, unsigned pos, int value, int upper)
5031 int j;
5033 if (!bmap)
5034 return NULL;
5035 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5036 pos += isl_basic_map_offset(bmap, type);
5037 bmap = isl_basic_map_cow(bmap);
5038 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5039 j = isl_basic_map_alloc_inequality(bmap);
5040 if (j < 0)
5041 goto error;
5042 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5043 if (upper) {
5044 isl_int_set_si(bmap->ineq[j][pos], -1);
5045 isl_int_set_si(bmap->ineq[j][0], value);
5046 } else {
5047 isl_int_set_si(bmap->ineq[j][pos], 1);
5048 isl_int_set_si(bmap->ineq[j][0], -value);
5050 bmap = isl_basic_map_simplify(bmap);
5051 return isl_basic_map_finalize(bmap);
5052 error:
5053 isl_basic_map_free(bmap);
5054 return NULL;
5057 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5058 __isl_take isl_basic_map *bmap,
5059 enum isl_dim_type type, unsigned pos, int value)
5061 return basic_map_bound_si(bmap, type, pos, value, 0);
5064 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5065 unsigned dim, isl_int value)
5067 int j;
5069 bset = isl_basic_set_cow(bset);
5070 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5071 j = isl_basic_set_alloc_inequality(bset);
5072 if (j < 0)
5073 goto error;
5074 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5075 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5076 isl_int_neg(bset->ineq[j][0], value);
5077 bset = isl_basic_set_simplify(bset);
5078 return isl_basic_set_finalize(bset);
5079 error:
5080 isl_basic_set_free(bset);
5081 return NULL;
5084 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5085 enum isl_dim_type type, unsigned pos, int value, int upper)
5087 int i;
5089 map = isl_map_cow(map);
5090 if (!map)
5091 return NULL;
5093 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5094 for (i = 0; i < map->n; ++i) {
5095 map->p[i] = basic_map_bound_si(map->p[i],
5096 type, pos, value, upper);
5097 if (!map->p[i])
5098 goto error;
5100 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5101 return map;
5102 error:
5103 isl_map_free(map);
5104 return NULL;
5107 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5108 enum isl_dim_type type, unsigned pos, int value)
5110 return map_bound_si(map, type, pos, value, 0);
5113 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5114 enum isl_dim_type type, unsigned pos, int value)
5116 return map_bound_si(map, type, pos, value, 1);
5119 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5120 enum isl_dim_type type, unsigned pos, int value)
5122 return (struct isl_set *)
5123 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5126 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5127 enum isl_dim_type type, unsigned pos, int value)
5129 return isl_map_upper_bound_si(set, type, pos, value);
5132 /* Bound the given variable of "bmap" from below (or above is "upper"
5133 * is set) to "value".
5135 static __isl_give isl_basic_map *basic_map_bound(
5136 __isl_take isl_basic_map *bmap,
5137 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5139 int j;
5141 if (!bmap)
5142 return NULL;
5143 if (pos >= isl_basic_map_dim(bmap, type))
5144 isl_die(bmap->ctx, isl_error_invalid,
5145 "index out of bounds", goto error);
5146 pos += isl_basic_map_offset(bmap, type);
5147 bmap = isl_basic_map_cow(bmap);
5148 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5149 j = isl_basic_map_alloc_inequality(bmap);
5150 if (j < 0)
5151 goto error;
5152 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5153 if (upper) {
5154 isl_int_set_si(bmap->ineq[j][pos], -1);
5155 isl_int_set(bmap->ineq[j][0], value);
5156 } else {
5157 isl_int_set_si(bmap->ineq[j][pos], 1);
5158 isl_int_neg(bmap->ineq[j][0], value);
5160 bmap = isl_basic_map_simplify(bmap);
5161 return isl_basic_map_finalize(bmap);
5162 error:
5163 isl_basic_map_free(bmap);
5164 return NULL;
5167 /* Bound the given variable of "map" from below (or above is "upper"
5168 * is set) to "value".
5170 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5171 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5173 int i;
5175 map = isl_map_cow(map);
5176 if (!map)
5177 return NULL;
5179 if (pos >= isl_map_dim(map, type))
5180 isl_die(map->ctx, isl_error_invalid,
5181 "index out of bounds", goto error);
5182 for (i = map->n - 1; i >= 0; --i) {
5183 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5184 if (remove_if_empty(map, i) < 0)
5185 goto error;
5187 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5188 return map;
5189 error:
5190 isl_map_free(map);
5191 return NULL;
5194 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5195 enum isl_dim_type type, unsigned pos, isl_int value)
5197 return map_bound(map, type, pos, value, 0);
5200 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5201 enum isl_dim_type type, unsigned pos, isl_int value)
5203 return map_bound(map, type, pos, value, 1);
5206 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5207 enum isl_dim_type type, unsigned pos, isl_int value)
5209 return isl_map_lower_bound(set, type, pos, value);
5212 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5213 enum isl_dim_type type, unsigned pos, isl_int value)
5215 return isl_map_upper_bound(set, type, pos, value);
5218 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5219 isl_int value)
5221 int i;
5223 set = isl_set_cow(set);
5224 if (!set)
5225 return NULL;
5227 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5228 for (i = 0; i < set->n; ++i) {
5229 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5230 if (!set->p[i])
5231 goto error;
5233 return set;
5234 error:
5235 isl_set_free(set);
5236 return NULL;
5239 struct isl_map *isl_map_reverse(struct isl_map *map)
5241 int i;
5243 map = isl_map_cow(map);
5244 if (!map)
5245 return NULL;
5247 map->dim = isl_space_reverse(map->dim);
5248 if (!map->dim)
5249 goto error;
5250 for (i = 0; i < map->n; ++i) {
5251 map->p[i] = isl_basic_map_reverse(map->p[i]);
5252 if (!map->p[i])
5253 goto error;
5255 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5256 return map;
5257 error:
5258 isl_map_free(map);
5259 return NULL;
5262 static struct isl_map *isl_basic_map_partial_lexopt(
5263 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5264 struct isl_set **empty, int max)
5266 if (!bmap)
5267 goto error;
5268 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5269 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5270 else
5271 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5272 error:
5273 isl_basic_map_free(bmap);
5274 isl_basic_set_free(dom);
5275 if (empty)
5276 *empty = NULL;
5277 return NULL;
5280 struct isl_map *isl_basic_map_partial_lexmax(
5281 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5282 struct isl_set **empty)
5284 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5287 struct isl_map *isl_basic_map_partial_lexmin(
5288 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5289 struct isl_set **empty)
5291 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5294 struct isl_set *isl_basic_set_partial_lexmin(
5295 struct isl_basic_set *bset, struct isl_basic_set *dom,
5296 struct isl_set **empty)
5298 return (struct isl_set *)
5299 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5300 dom, empty);
5303 struct isl_set *isl_basic_set_partial_lexmax(
5304 struct isl_basic_set *bset, struct isl_basic_set *dom,
5305 struct isl_set **empty)
5307 return (struct isl_set *)
5308 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5309 dom, empty);
5312 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5313 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5314 __isl_give isl_set **empty)
5316 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5319 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5320 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5321 __isl_give isl_set **empty)
5323 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5326 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5327 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5328 __isl_give isl_set **empty)
5330 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5333 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5334 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5335 __isl_give isl_set **empty)
5337 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5340 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5341 __isl_take isl_basic_map *bmap, int max)
5343 isl_basic_set *dom = NULL;
5344 isl_space *dom_space;
5346 if (!bmap)
5347 goto error;
5348 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5349 dom = isl_basic_set_universe(dom_space);
5350 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5351 error:
5352 isl_basic_map_free(bmap);
5353 return NULL;
5356 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5357 __isl_take isl_basic_map *bmap)
5359 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5362 /* Given a basic map "bmap", compute the lexicographically minimal
5363 * (or maximal) image element for each domain element in dom.
5364 * Set *empty to those elements in dom that do not have an image element.
5366 * We first make sure the basic sets in dom are disjoint and then
5367 * simply collect the results over each of the basic sets separately.
5368 * We could probably improve the efficiency a bit by moving the union
5369 * domain down into the parametric integer programming.
5371 static __isl_give isl_map *basic_map_partial_lexopt(
5372 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
5373 __isl_give isl_set **empty, int max)
5375 int i;
5376 struct isl_map *res;
5378 dom = isl_set_make_disjoint(dom);
5379 if (!dom)
5380 goto error;
5382 if (isl_set_plain_is_empty(dom)) {
5383 res = isl_map_empty_like_basic_map(bmap);
5384 *empty = isl_set_empty_like(dom);
5385 isl_set_free(dom);
5386 isl_basic_map_free(bmap);
5387 return res;
5390 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5391 isl_basic_set_copy(dom->p[0]), empty, max);
5393 for (i = 1; i < dom->n; ++i) {
5394 struct isl_map *res_i;
5395 struct isl_set *empty_i;
5397 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5398 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5400 res = isl_map_union_disjoint(res, res_i);
5401 *empty = isl_set_union_disjoint(*empty, empty_i);
5404 isl_set_free(dom);
5405 isl_basic_map_free(bmap);
5406 return res;
5407 error:
5408 *empty = NULL;
5409 isl_set_free(dom);
5410 isl_basic_map_free(bmap);
5411 return NULL;
5414 /* Given a map "map", compute the lexicographically minimal
5415 * (or maximal) image element for each domain element in dom.
5416 * Set *empty to those elements in dom that do not have an image element.
5418 * We first compute the lexicographically minimal or maximal element
5419 * in the first basic map. This results in a partial solution "res"
5420 * and a subset "todo" of dom that still need to be handled.
5421 * We then consider each of the remaining maps in "map" and successively
5422 * improve both "res" and "todo".
5424 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5425 * Assume we are computing the lexicographical maximum.
5426 * We first compute the lexicographically maximal element in basic map i.
5427 * This results in a partial solution res_i and a subset todo_i.
5428 * Then we combine these results with those obtain for the first k basic maps
5429 * to obtain a result that is valid for the first k+1 basic maps.
5430 * In particular, the set where there is no solution is the set where
5431 * there is no solution for the first k basic maps and also no solution
5432 * for the ith basic map, i.e.,
5434 * todo^i = todo^k * todo_i
5436 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5437 * solutions, arbitrarily breaking ties in favor of res^k.
5438 * That is, when res^k(a) >= res_i(a), we pick res^k and
5439 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5440 * the lexicographic order.)
5441 * In practice, we compute
5443 * res^k * (res_i . "<=")
5445 * and
5447 * res_i * (res^k . "<")
5449 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5450 * where only one of res^k and res_i provides a solution and we simply pick
5451 * that one, i.e.,
5453 * res^k * todo_i
5454 * and
5455 * res_i * todo^k
5457 * Note that we only compute these intersections when dom(res^k) intersects
5458 * dom(res_i). Otherwise, the only effect of these intersections is to
5459 * potentially break up res^k and res_i into smaller pieces.
5460 * We want to avoid such splintering as much as possible.
5461 * In fact, an earlier implementation of this function would look for
5462 * better results in the domain of res^k and for extra results in todo^k,
5463 * but this would always result in a splintering according to todo^k,
5464 * even when the domain of basic map i is disjoint from the domains of
5465 * the previous basic maps.
5467 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5468 __isl_take isl_map *map, __isl_take isl_set *dom,
5469 __isl_give isl_set **empty, int max)
5471 int i;
5472 struct isl_map *res;
5473 struct isl_set *todo;
5475 if (!map || !dom)
5476 goto error;
5478 if (isl_map_plain_is_empty(map)) {
5479 if (empty)
5480 *empty = dom;
5481 else
5482 isl_set_free(dom);
5483 return map;
5486 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5487 isl_set_copy(dom), &todo, max);
5489 for (i = 1; i < map->n; ++i) {
5490 isl_map *lt, *le;
5491 isl_map *res_i;
5492 isl_set *todo_i;
5493 isl_space *dim = isl_space_range(isl_map_get_space(res));
5495 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5496 isl_set_copy(dom), &todo_i, max);
5498 if (max) {
5499 lt = isl_map_lex_lt(isl_space_copy(dim));
5500 le = isl_map_lex_le(dim);
5501 } else {
5502 lt = isl_map_lex_gt(isl_space_copy(dim));
5503 le = isl_map_lex_ge(dim);
5505 lt = isl_map_apply_range(isl_map_copy(res), lt);
5506 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5507 le = isl_map_apply_range(isl_map_copy(res_i), le);
5508 le = isl_map_intersect(le, isl_map_copy(res));
5510 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5511 res = isl_map_intersect_domain(res,
5512 isl_set_copy(todo_i));
5513 res_i = isl_map_intersect_domain(res_i,
5514 isl_set_copy(todo));
5517 res = isl_map_union_disjoint(res, res_i);
5518 res = isl_map_union_disjoint(res, lt);
5519 res = isl_map_union_disjoint(res, le);
5521 todo = isl_set_intersect(todo, todo_i);
5524 isl_set_free(dom);
5525 isl_map_free(map);
5527 if (empty)
5528 *empty = todo;
5529 else
5530 isl_set_free(todo);
5532 return res;
5533 error:
5534 if (empty)
5535 *empty = NULL;
5536 isl_set_free(dom);
5537 isl_map_free(map);
5538 return NULL;
5541 /* Given a map "map", compute the lexicographically minimal
5542 * (or maximal) image element for each domain element in dom.
5543 * Set *empty to those elements in dom that do not have an image element.
5545 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5547 static __isl_give isl_map *isl_map_partial_lexopt(
5548 __isl_take isl_map *map, __isl_take isl_set *dom,
5549 __isl_give isl_set **empty, int max)
5551 if (!map || !dom)
5552 goto error;
5553 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5554 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5555 if (!isl_space_has_named_params(map->dim) ||
5556 !isl_space_has_named_params(dom->dim))
5557 isl_die(map->ctx, isl_error_invalid,
5558 "unaligned unnamed parameters", goto error);
5559 map = isl_map_align_params(map, isl_map_get_space(dom));
5560 dom = isl_map_align_params(dom, isl_map_get_space(map));
5561 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5562 error:
5563 if (empty)
5564 *empty = NULL;
5565 isl_set_free(dom);
5566 isl_map_free(map);
5567 return NULL;
5570 __isl_give isl_map *isl_map_partial_lexmax(
5571 __isl_take isl_map *map, __isl_take isl_set *dom,
5572 __isl_give isl_set **empty)
5574 return isl_map_partial_lexopt(map, dom, empty, 1);
5577 __isl_give isl_map *isl_map_partial_lexmin(
5578 __isl_take isl_map *map, __isl_take isl_set *dom,
5579 __isl_give isl_set **empty)
5581 return isl_map_partial_lexopt(map, dom, empty, 0);
5584 __isl_give isl_set *isl_set_partial_lexmin(
5585 __isl_take isl_set *set, __isl_take isl_set *dom,
5586 __isl_give isl_set **empty)
5588 return (struct isl_set *)
5589 isl_map_partial_lexmin((struct isl_map *)set,
5590 dom, empty);
5593 __isl_give isl_set *isl_set_partial_lexmax(
5594 __isl_take isl_set *set, __isl_take isl_set *dom,
5595 __isl_give isl_set **empty)
5597 return (struct isl_set *)
5598 isl_map_partial_lexmax((struct isl_map *)set,
5599 dom, empty);
5602 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5604 struct isl_basic_set *dom = NULL;
5605 isl_space *dom_dim;
5607 if (!bmap)
5608 goto error;
5609 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5610 dom = isl_basic_set_universe(dom_dim);
5611 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5612 error:
5613 isl_basic_map_free(bmap);
5614 return NULL;
5617 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5619 return isl_basic_map_lexopt(bmap, 0);
5622 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5624 return isl_basic_map_lexopt(bmap, 1);
5627 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5629 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5632 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5634 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5637 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5639 struct isl_set *dom = NULL;
5640 isl_space *dom_dim;
5642 if (!map)
5643 goto error;
5644 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5645 dom = isl_set_universe(dom_dim);
5646 return isl_map_partial_lexopt(map, dom, NULL, max);
5647 error:
5648 isl_map_free(map);
5649 return NULL;
5652 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5654 return isl_map_lexopt(map, 0);
5657 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5659 return isl_map_lexopt(map, 1);
5662 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5664 return (isl_set *)isl_map_lexmin((isl_map *)set);
5667 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5669 return (isl_set *)isl_map_lexmax((isl_map *)set);
5672 /* Extract the first and only affine expression from list
5673 * and then add it to *pwaff with the given dom.
5674 * This domain is known to be disjoint from other domains
5675 * because of the way isl_basic_map_foreach_lexmax works.
5677 static int update_dim_opt(__isl_take isl_basic_set *dom,
5678 __isl_take isl_aff_list *list, void *user)
5680 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5681 isl_aff *aff;
5682 isl_pw_aff **pwaff = user;
5683 isl_pw_aff *pwaff_i;
5685 if (isl_aff_list_n_aff(list) != 1)
5686 isl_die(ctx, isl_error_internal,
5687 "expecting single element list", goto error);
5689 aff = isl_aff_list_get_aff(list, 0);
5690 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5692 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5694 isl_aff_list_free(list);
5696 return 0;
5697 error:
5698 isl_basic_set_free(dom);
5699 isl_aff_list_free(list);
5700 return -1;
5703 /* Given a basic map with one output dimension, compute the minimum or
5704 * maximum of that dimension as an isl_pw_aff.
5706 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5707 * call update_dim_opt on each leaf of the result.
5709 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
5710 int max)
5712 isl_space *dim = isl_basic_map_get_space(bmap);
5713 isl_pw_aff *pwaff;
5714 int r;
5716 dim = isl_space_from_domain(isl_space_domain(dim));
5717 dim = isl_space_add_dims(dim, isl_dim_out, 1);
5718 pwaff = isl_pw_aff_empty(dim);
5720 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
5721 if (r < 0)
5722 return isl_pw_aff_free(pwaff);
5724 return pwaff;
5727 /* Compute the minimum or maximum of the given output dimension
5728 * as a function of the parameters and the input dimensions,
5729 * but independently of the other output dimensions.
5731 * We first project out the other output dimension and then compute
5732 * the "lexicographic" maximum in each basic map, combining the results
5733 * using isl_pw_aff_union_max.
5735 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
5736 int max)
5738 int i;
5739 isl_pw_aff *pwaff;
5740 unsigned n_out;
5742 n_out = isl_map_dim(map, isl_dim_out);
5743 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
5744 map = isl_map_project_out(map, isl_dim_out, 0, pos);
5745 if (!map)
5746 return NULL;
5748 if (map->n == 0) {
5749 isl_space *dim = isl_map_get_space(map);
5750 dim = isl_space_domain(isl_space_from_range(dim));
5751 isl_map_free(map);
5752 return isl_pw_aff_empty(dim);
5755 pwaff = basic_map_dim_opt(map->p[0], max);
5756 for (i = 1; i < map->n; ++i) {
5757 isl_pw_aff *pwaff_i;
5759 pwaff_i = basic_map_dim_opt(map->p[i], max);
5760 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
5763 isl_map_free(map);
5765 return pwaff;
5768 /* Compute the maximum of the given output dimension as a function of the
5769 * parameters and input dimensions, but independently of
5770 * the other output dimensions.
5772 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
5774 return map_dim_opt(map, pos, 1);
5777 /* Compute the minimum or maximum of the given set dimension
5778 * as a function of the parameters,
5779 * but independently of the other set dimensions.
5781 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
5782 int max)
5784 return map_dim_opt(set, pos, max);
5787 /* Compute the maximum of the given set dimension as a function of the
5788 * parameters, but independently of the other set dimensions.
5790 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5792 return set_dim_opt(set, pos, 1);
5795 /* Compute the minimum of the given set dimension as a function of the
5796 * parameters, but independently of the other set dimensions.
5798 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
5800 return set_dim_opt(set, pos, 0);
5803 /* Apply a preimage specified by "mat" on the parameters of "bset".
5804 * bset is assumed to have only parameters and divs.
5806 static struct isl_basic_set *basic_set_parameter_preimage(
5807 struct isl_basic_set *bset, struct isl_mat *mat)
5809 unsigned nparam;
5811 if (!bset || !mat)
5812 goto error;
5814 bset->dim = isl_space_cow(bset->dim);
5815 if (!bset->dim)
5816 goto error;
5818 nparam = isl_basic_set_dim(bset, isl_dim_param);
5820 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5822 bset->dim->nparam = 0;
5823 bset->dim->n_out = nparam;
5824 bset = isl_basic_set_preimage(bset, mat);
5825 if (bset) {
5826 bset->dim->nparam = bset->dim->n_out;
5827 bset->dim->n_out = 0;
5829 return bset;
5830 error:
5831 isl_mat_free(mat);
5832 isl_basic_set_free(bset);
5833 return NULL;
5836 /* Apply a preimage specified by "mat" on the parameters of "set".
5837 * set is assumed to have only parameters and divs.
5839 static struct isl_set *set_parameter_preimage(
5840 struct isl_set *set, struct isl_mat *mat)
5842 isl_space *dim = NULL;
5843 unsigned nparam;
5845 if (!set || !mat)
5846 goto error;
5848 dim = isl_space_copy(set->dim);
5849 dim = isl_space_cow(dim);
5850 if (!dim)
5851 goto error;
5853 nparam = isl_set_dim(set, isl_dim_param);
5855 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5857 dim->nparam = 0;
5858 dim->n_out = nparam;
5859 isl_set_reset_space(set, dim);
5860 set = isl_set_preimage(set, mat);
5861 if (!set)
5862 goto error2;
5863 dim = isl_space_copy(set->dim);
5864 dim = isl_space_cow(dim);
5865 if (!dim)
5866 goto error2;
5867 dim->nparam = dim->n_out;
5868 dim->n_out = 0;
5869 isl_set_reset_space(set, dim);
5870 return set;
5871 error:
5872 isl_space_free(dim);
5873 isl_mat_free(mat);
5874 error2:
5875 isl_set_free(set);
5876 return NULL;
5879 /* Intersect the basic set "bset" with the affine space specified by the
5880 * equalities in "eq".
5882 static struct isl_basic_set *basic_set_append_equalities(
5883 struct isl_basic_set *bset, struct isl_mat *eq)
5885 int i, k;
5886 unsigned len;
5888 if (!bset || !eq)
5889 goto error;
5891 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
5892 eq->n_row, 0);
5893 if (!bset)
5894 goto error;
5896 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
5897 for (i = 0; i < eq->n_row; ++i) {
5898 k = isl_basic_set_alloc_equality(bset);
5899 if (k < 0)
5900 goto error;
5901 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5902 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5904 isl_mat_free(eq);
5906 bset = isl_basic_set_gauss(bset, NULL);
5907 bset = isl_basic_set_finalize(bset);
5909 return bset;
5910 error:
5911 isl_mat_free(eq);
5912 isl_basic_set_free(bset);
5913 return NULL;
5916 /* Intersect the set "set" with the affine space specified by the
5917 * equalities in "eq".
5919 static struct isl_set *set_append_equalities(struct isl_set *set,
5920 struct isl_mat *eq)
5922 int i;
5924 if (!set || !eq)
5925 goto error;
5927 for (i = 0; i < set->n; ++i) {
5928 set->p[i] = basic_set_append_equalities(set->p[i],
5929 isl_mat_copy(eq));
5930 if (!set->p[i])
5931 goto error;
5933 isl_mat_free(eq);
5934 return set;
5935 error:
5936 isl_mat_free(eq);
5937 isl_set_free(set);
5938 return NULL;
5941 /* Project the given basic set onto its parameter domain, possibly introducing
5942 * new, explicit, existential variables in the constraints.
5943 * The input has parameters and (possibly implicit) existential variables.
5944 * The output has the same parameters, but only
5945 * explicit existentially quantified variables.
5947 * The actual projection is performed by pip, but pip doesn't seem
5948 * to like equalities very much, so we first remove the equalities
5949 * among the parameters by performing a variable compression on
5950 * the parameters. Afterward, an inverse transformation is performed
5951 * and the equalities among the parameters are inserted back in.
5953 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5955 int i, j;
5956 struct isl_mat *eq;
5957 struct isl_mat *T, *T2;
5958 struct isl_set *set;
5959 unsigned nparam, n_div;
5961 bset = isl_basic_set_cow(bset);
5962 if (!bset)
5963 return NULL;
5965 if (bset->n_eq == 0)
5966 return isl_basic_set_lexmin(bset);
5968 isl_basic_set_gauss(bset, NULL);
5970 nparam = isl_basic_set_dim(bset, isl_dim_param);
5971 n_div = isl_basic_set_dim(bset, isl_dim_div);
5973 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5974 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5975 ++i;
5977 if (i == bset->n_eq)
5978 return isl_basic_set_lexmin(bset);
5980 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5981 0, 1 + nparam);
5982 eq = isl_mat_cow(eq);
5983 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5984 if (T && T->n_col == 0) {
5985 isl_mat_free(T);
5986 isl_mat_free(T2);
5987 isl_mat_free(eq);
5988 bset = isl_basic_set_set_to_empty(bset);
5989 return isl_set_from_basic_set(bset);
5991 bset = basic_set_parameter_preimage(bset, T);
5993 set = isl_basic_set_lexmin(bset);
5994 set = set_parameter_preimage(set, T2);
5995 set = set_append_equalities(set, eq);
5996 return set;
5999 /* Compute an explicit representation for all the existentially
6000 * quantified variables.
6001 * The input and output dimensions are first turned into parameters.
6002 * compute_divs then returns a map with the same parameters and
6003 * no input or output dimensions and the dimension specification
6004 * is reset to that of the input.
6006 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6008 struct isl_basic_set *bset;
6009 struct isl_set *set;
6010 struct isl_map *map;
6011 isl_space *dim, *orig_dim = NULL;
6012 unsigned nparam;
6013 unsigned n_in;
6014 unsigned n_out;
6016 bmap = isl_basic_map_cow(bmap);
6017 if (!bmap)
6018 return NULL;
6020 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6021 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6022 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6023 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
6024 if (!dim)
6025 goto error;
6027 orig_dim = bmap->dim;
6028 bmap->dim = dim;
6029 bset = (struct isl_basic_set *)bmap;
6031 set = parameter_compute_divs(bset);
6032 map = (struct isl_map *)set;
6033 map = isl_map_reset_space(map, orig_dim);
6035 return map;
6036 error:
6037 isl_basic_map_free(bmap);
6038 return NULL;
6041 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6043 int i;
6044 unsigned off;
6046 if (!bmap)
6047 return -1;
6049 off = isl_space_dim(bmap->dim, isl_dim_all);
6050 for (i = 0; i < bmap->n_div; ++i) {
6051 if (isl_int_is_zero(bmap->div[i][0]))
6052 return 0;
6053 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6054 return -1);
6056 return 1;
6059 static int map_divs_known(__isl_keep isl_map *map)
6061 int i;
6063 if (!map)
6064 return -1;
6066 for (i = 0; i < map->n; ++i) {
6067 int known = isl_basic_map_divs_known(map->p[i]);
6068 if (known <= 0)
6069 return known;
6072 return 1;
6075 /* If bmap contains any unknown divs, then compute explicit
6076 * expressions for them. However, this computation may be
6077 * quite expensive, so first try to remove divs that aren't
6078 * strictly needed.
6080 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6082 int known;
6083 struct isl_map *map;
6085 known = isl_basic_map_divs_known(bmap);
6086 if (known < 0)
6087 goto error;
6088 if (known)
6089 return isl_map_from_basic_map(bmap);
6091 bmap = isl_basic_map_drop_redundant_divs(bmap);
6093 known = isl_basic_map_divs_known(bmap);
6094 if (known < 0)
6095 goto error;
6096 if (known)
6097 return isl_map_from_basic_map(bmap);
6099 map = compute_divs(bmap);
6100 return map;
6101 error:
6102 isl_basic_map_free(bmap);
6103 return NULL;
6106 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6108 int i;
6109 int known;
6110 struct isl_map *res;
6112 if (!map)
6113 return NULL;
6114 if (map->n == 0)
6115 return map;
6117 known = map_divs_known(map);
6118 if (known < 0) {
6119 isl_map_free(map);
6120 return NULL;
6122 if (known)
6123 return map;
6125 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6126 for (i = 1 ; i < map->n; ++i) {
6127 struct isl_map *r2;
6128 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6129 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6130 res = isl_map_union_disjoint(res, r2);
6131 else
6132 res = isl_map_union(res, r2);
6134 isl_map_free(map);
6136 return res;
6139 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6141 return (struct isl_set *)
6142 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6145 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6147 return (struct isl_set *)
6148 isl_map_compute_divs((struct isl_map *)set);
6151 struct isl_set *isl_map_domain(struct isl_map *map)
6153 int i;
6154 struct isl_set *set;
6156 if (!map)
6157 goto error;
6159 map = isl_map_cow(map);
6160 if (!map)
6161 return NULL;
6163 set = (struct isl_set *)map;
6164 set->dim = isl_space_domain(set->dim);
6165 if (!set->dim)
6166 goto error;
6167 for (i = 0; i < map->n; ++i) {
6168 set->p[i] = isl_basic_map_domain(map->p[i]);
6169 if (!set->p[i])
6170 goto error;
6172 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6173 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6174 return set;
6175 error:
6176 isl_map_free(map);
6177 return NULL;
6180 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6181 __isl_take isl_map *map2)
6183 int i;
6184 unsigned flags = 0;
6185 struct isl_map *map = NULL;
6187 if (!map1 || !map2)
6188 goto error;
6190 if (map1->n == 0) {
6191 isl_map_free(map1);
6192 return map2;
6194 if (map2->n == 0) {
6195 isl_map_free(map2);
6196 return map1;
6199 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6201 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6202 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6203 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6205 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6206 map1->n + map2->n, flags);
6207 if (!map)
6208 goto error;
6209 for (i = 0; i < map1->n; ++i) {
6210 map = isl_map_add_basic_map(map,
6211 isl_basic_map_copy(map1->p[i]));
6212 if (!map)
6213 goto error;
6215 for (i = 0; i < map2->n; ++i) {
6216 map = isl_map_add_basic_map(map,
6217 isl_basic_map_copy(map2->p[i]));
6218 if (!map)
6219 goto error;
6221 isl_map_free(map1);
6222 isl_map_free(map2);
6223 return map;
6224 error:
6225 isl_map_free(map);
6226 isl_map_free(map1);
6227 isl_map_free(map2);
6228 return NULL;
6231 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6232 __isl_take isl_map *map2)
6234 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6237 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6239 map1 = isl_map_union_disjoint(map1, map2);
6240 if (!map1)
6241 return NULL;
6242 if (map1->n > 1)
6243 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6244 return map1;
6247 struct isl_set *isl_set_union_disjoint(
6248 struct isl_set *set1, struct isl_set *set2)
6250 return (struct isl_set *)
6251 isl_map_union_disjoint(
6252 (struct isl_map *)set1, (struct isl_map *)set2);
6255 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6257 return (struct isl_set *)
6258 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6261 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6262 __isl_take isl_set *set)
6264 unsigned flags = 0;
6265 struct isl_map *result;
6266 int i, j;
6268 if (!map || !set)
6269 goto error;
6271 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
6272 isl_die(set->ctx, isl_error_invalid,
6273 "parameters don't match", goto error);
6275 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
6276 !isl_map_compatible_range(map, set))
6277 isl_die(set->ctx, isl_error_invalid,
6278 "incompatible spaces", goto error);
6280 if (isl_set_plain_is_universe(set)) {
6281 isl_set_free(set);
6282 return map;
6285 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6286 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6287 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6289 result = isl_map_alloc_space(isl_space_copy(map->dim),
6290 map->n * set->n, flags);
6291 if (!result)
6292 goto error;
6293 for (i = 0; i < map->n; ++i)
6294 for (j = 0; j < set->n; ++j) {
6295 result = isl_map_add_basic_map(result,
6296 isl_basic_map_intersect_range(
6297 isl_basic_map_copy(map->p[i]),
6298 isl_basic_set_copy(set->p[j])));
6299 if (!result)
6300 goto error;
6302 isl_map_free(map);
6303 isl_set_free(set);
6304 return result;
6305 error:
6306 isl_map_free(map);
6307 isl_set_free(set);
6308 return NULL;
6311 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6312 __isl_take isl_set *set)
6314 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6317 struct isl_map *isl_map_intersect_domain(
6318 struct isl_map *map, struct isl_set *set)
6320 return isl_map_reverse(
6321 isl_map_intersect_range(isl_map_reverse(map), set));
6324 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6325 __isl_take isl_map *map2)
6327 if (!map1 || !map2)
6328 goto error;
6329 map1 = isl_map_reverse(map1);
6330 map1 = isl_map_apply_range(map1, map2);
6331 return isl_map_reverse(map1);
6332 error:
6333 isl_map_free(map1);
6334 isl_map_free(map2);
6335 return NULL;
6338 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6339 __isl_take isl_map *map2)
6341 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6344 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6345 __isl_take isl_map *map2)
6347 isl_space *dim_result;
6348 struct isl_map *result;
6349 int i, j;
6351 if (!map1 || !map2)
6352 goto error;
6354 dim_result = isl_space_join(isl_space_copy(map1->dim),
6355 isl_space_copy(map2->dim));
6357 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6358 if (!result)
6359 goto error;
6360 for (i = 0; i < map1->n; ++i)
6361 for (j = 0; j < map2->n; ++j) {
6362 result = isl_map_add_basic_map(result,
6363 isl_basic_map_apply_range(
6364 isl_basic_map_copy(map1->p[i]),
6365 isl_basic_map_copy(map2->p[j])));
6366 if (!result)
6367 goto error;
6369 isl_map_free(map1);
6370 isl_map_free(map2);
6371 if (result && result->n <= 1)
6372 ISL_F_SET(result, ISL_MAP_DISJOINT);
6373 return result;
6374 error:
6375 isl_map_free(map1);
6376 isl_map_free(map2);
6377 return NULL;
6380 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6381 __isl_take isl_map *map2)
6383 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6387 * returns range - domain
6389 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6391 isl_space *dims, *target_dim;
6392 struct isl_basic_set *bset;
6393 unsigned dim;
6394 unsigned nparam;
6395 int i;
6397 if (!bmap)
6398 goto error;
6399 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6400 bmap->dim, isl_dim_out),
6401 goto error);
6402 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6403 dim = isl_basic_map_n_in(bmap);
6404 nparam = isl_basic_map_n_param(bmap);
6405 bset = isl_basic_set_from_basic_map(bmap);
6406 bset = isl_basic_set_cow(bset);
6407 dims = isl_basic_set_get_space(bset);
6408 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6409 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6410 bset = isl_basic_set_swap_vars(bset, 2*dim);
6411 for (i = 0; i < dim; ++i) {
6412 int j = isl_basic_map_alloc_equality(
6413 (struct isl_basic_map *)bset);
6414 if (j < 0)
6415 goto error;
6416 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6417 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6418 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6419 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6421 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6422 bset = isl_basic_set_reset_space(bset, target_dim);
6423 return bset;
6424 error:
6425 isl_basic_map_free(bmap);
6426 return NULL;
6430 * returns range - domain
6432 struct isl_set *isl_map_deltas(struct isl_map *map)
6434 int i;
6435 isl_space *dim;
6436 struct isl_set *result;
6438 if (!map)
6439 return NULL;
6441 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6442 map->dim, isl_dim_out),
6443 goto error);
6444 dim = isl_map_get_space(map);
6445 dim = isl_space_domain(dim);
6446 result = isl_set_alloc_space(dim, map->n, 0);
6447 if (!result)
6448 goto error;
6449 for (i = 0; i < map->n; ++i)
6450 result = isl_set_add_basic_set(result,
6451 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6452 isl_map_free(map);
6453 return result;
6454 error:
6455 isl_map_free(map);
6456 return NULL;
6460 * returns [domain -> range] -> range - domain
6462 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6463 __isl_take isl_basic_map *bmap)
6465 int i, k;
6466 isl_space *dim;
6467 isl_basic_map *domain;
6468 int nparam, n;
6469 unsigned total;
6471 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6472 isl_die(bmap->ctx, isl_error_invalid,
6473 "domain and range don't match", goto error);
6475 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6476 n = isl_basic_map_dim(bmap, isl_dim_in);
6478 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6479 domain = isl_basic_map_universe(dim);
6481 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6482 bmap = isl_basic_map_apply_range(bmap, domain);
6483 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6485 total = isl_basic_map_total_dim(bmap);
6487 for (i = 0; i < n; ++i) {
6488 k = isl_basic_map_alloc_equality(bmap);
6489 if (k < 0)
6490 goto error;
6491 isl_seq_clr(bmap->eq[k], 1 + total);
6492 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6493 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6494 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6497 bmap = isl_basic_map_gauss(bmap, NULL);
6498 return isl_basic_map_finalize(bmap);
6499 error:
6500 isl_basic_map_free(bmap);
6501 return NULL;
6505 * returns [domain -> range] -> range - domain
6507 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6509 int i;
6510 isl_space *domain_dim;
6512 if (!map)
6513 return NULL;
6515 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6516 isl_die(map->ctx, isl_error_invalid,
6517 "domain and range don't match", goto error);
6519 map = isl_map_cow(map);
6520 if (!map)
6521 return NULL;
6523 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6524 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6525 map->dim = isl_space_join(map->dim, domain_dim);
6526 if (!map->dim)
6527 goto error;
6528 for (i = 0; i < map->n; ++i) {
6529 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6530 if (!map->p[i])
6531 goto error;
6533 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6534 return map;
6535 error:
6536 isl_map_free(map);
6537 return NULL;
6540 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6542 struct isl_basic_map *bmap;
6543 unsigned nparam;
6544 unsigned dim;
6545 int i;
6547 if (!dims)
6548 return NULL;
6550 nparam = dims->nparam;
6551 dim = dims->n_out;
6552 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6553 if (!bmap)
6554 goto error;
6556 for (i = 0; i < dim; ++i) {
6557 int j = isl_basic_map_alloc_equality(bmap);
6558 if (j < 0)
6559 goto error;
6560 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6561 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6562 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6564 return isl_basic_map_finalize(bmap);
6565 error:
6566 isl_basic_map_free(bmap);
6567 return NULL;
6570 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6572 if (!dim)
6573 return NULL;
6574 if (dim->n_in != dim->n_out)
6575 isl_die(dim->ctx, isl_error_invalid,
6576 "number of input and output dimensions needs to be "
6577 "the same", goto error);
6578 return basic_map_identity(dim);
6579 error:
6580 isl_space_free(dim);
6581 return NULL;
6584 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6586 if (!model || !model->dim)
6587 return NULL;
6588 return isl_basic_map_identity(isl_space_copy(model->dim));
6591 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6593 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6596 struct isl_map *isl_map_identity_like(struct isl_map *model)
6598 if (!model || !model->dim)
6599 return NULL;
6600 return isl_map_identity(isl_space_copy(model->dim));
6603 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6605 if (!model || !model->dim)
6606 return NULL;
6607 return isl_map_identity(isl_space_copy(model->dim));
6610 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6612 isl_space *dim = isl_set_get_space(set);
6613 isl_map *id;
6614 id = isl_map_identity(isl_space_map_from_set(dim));
6615 return isl_map_intersect_range(id, set);
6618 /* Construct a basic set with all set dimensions having only non-negative
6619 * values.
6621 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6622 __isl_take isl_space *space)
6624 int i;
6625 unsigned nparam;
6626 unsigned dim;
6627 struct isl_basic_set *bset;
6629 if (!space)
6630 return NULL;
6631 nparam = space->nparam;
6632 dim = space->n_out;
6633 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6634 if (!bset)
6635 return NULL;
6636 for (i = 0; i < dim; ++i) {
6637 int k = isl_basic_set_alloc_inequality(bset);
6638 if (k < 0)
6639 goto error;
6640 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6641 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6643 return bset;
6644 error:
6645 isl_basic_set_free(bset);
6646 return NULL;
6649 /* Construct the half-space x_pos >= 0.
6651 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6652 int pos)
6654 int k;
6655 isl_basic_set *nonneg;
6657 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6658 k = isl_basic_set_alloc_inequality(nonneg);
6659 if (k < 0)
6660 goto error;
6661 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6662 isl_int_set_si(nonneg->ineq[k][pos], 1);
6664 return isl_basic_set_finalize(nonneg);
6665 error:
6666 isl_basic_set_free(nonneg);
6667 return NULL;
6670 /* Construct the half-space x_pos <= -1.
6672 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6674 int k;
6675 isl_basic_set *neg;
6677 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6678 k = isl_basic_set_alloc_inequality(neg);
6679 if (k < 0)
6680 goto error;
6681 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6682 isl_int_set_si(neg->ineq[k][0], -1);
6683 isl_int_set_si(neg->ineq[k][pos], -1);
6685 return isl_basic_set_finalize(neg);
6686 error:
6687 isl_basic_set_free(neg);
6688 return NULL;
6691 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6692 enum isl_dim_type type, unsigned first, unsigned n)
6694 int i;
6695 isl_basic_set *nonneg;
6696 isl_basic_set *neg;
6698 if (!set)
6699 return NULL;
6700 if (n == 0)
6701 return set;
6703 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6705 for (i = 0; i < n; ++i) {
6706 nonneg = nonneg_halfspace(isl_set_get_space(set),
6707 pos(set->dim, type) + first + i);
6708 neg = neg_halfspace(isl_set_get_space(set),
6709 pos(set->dim, type) + first + i);
6711 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6714 return set;
6715 error:
6716 isl_set_free(set);
6717 return NULL;
6720 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6721 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6722 void *user)
6724 isl_set *half;
6726 if (!set)
6727 return -1;
6728 if (isl_set_plain_is_empty(set)) {
6729 isl_set_free(set);
6730 return 0;
6732 if (first == len)
6733 return fn(set, signs, user);
6735 signs[first] = 1;
6736 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6737 1 + first));
6738 half = isl_set_intersect(half, isl_set_copy(set));
6739 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6740 goto error;
6742 signs[first] = -1;
6743 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6744 1 + first));
6745 half = isl_set_intersect(half, set);
6746 return foreach_orthant(half, signs, first + 1, len, fn, user);
6747 error:
6748 isl_set_free(set);
6749 return -1;
6752 /* Call "fn" on the intersections of "set" with each of the orthants
6753 * (except for obviously empty intersections). The orthant is identified
6754 * by the signs array, with each entry having value 1 or -1 according
6755 * to the sign of the corresponding variable.
6757 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6758 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6759 void *user)
6761 unsigned nparam;
6762 unsigned nvar;
6763 int *signs;
6764 int r;
6766 if (!set)
6767 return -1;
6768 if (isl_set_plain_is_empty(set))
6769 return 0;
6771 nparam = isl_set_dim(set, isl_dim_param);
6772 nvar = isl_set_dim(set, isl_dim_set);
6774 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6776 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6777 fn, user);
6779 free(signs);
6781 return r;
6784 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6786 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6789 int isl_basic_map_is_subset(
6790 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6792 int is_subset;
6793 struct isl_map *map1;
6794 struct isl_map *map2;
6796 if (!bmap1 || !bmap2)
6797 return -1;
6799 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6800 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6802 is_subset = isl_map_is_subset(map1, map2);
6804 isl_map_free(map1);
6805 isl_map_free(map2);
6807 return is_subset;
6810 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
6811 __isl_keep isl_basic_set *bset2)
6813 return isl_basic_map_is_subset(bset1, bset2);
6816 int isl_basic_map_is_equal(
6817 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6819 int is_subset;
6821 if (!bmap1 || !bmap2)
6822 return -1;
6823 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6824 if (is_subset != 1)
6825 return is_subset;
6826 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6827 return is_subset;
6830 int isl_basic_set_is_equal(
6831 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6833 return isl_basic_map_is_equal(
6834 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6837 int isl_map_is_empty(struct isl_map *map)
6839 int i;
6840 int is_empty;
6842 if (!map)
6843 return -1;
6844 for (i = 0; i < map->n; ++i) {
6845 is_empty = isl_basic_map_is_empty(map->p[i]);
6846 if (is_empty < 0)
6847 return -1;
6848 if (!is_empty)
6849 return 0;
6851 return 1;
6854 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6856 return map ? map->n == 0 : -1;
6859 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6861 return isl_map_plain_is_empty(map);
6864 int isl_set_plain_is_empty(struct isl_set *set)
6866 return set ? set->n == 0 : -1;
6869 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6871 return isl_set_plain_is_empty(set);
6874 int isl_set_is_empty(struct isl_set *set)
6876 return isl_map_is_empty((struct isl_map *)set);
6879 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6881 if (!map1 || !map2)
6882 return -1;
6884 return isl_space_is_equal(map1->dim, map2->dim);
6887 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6889 if (!set1 || !set2)
6890 return -1;
6892 return isl_space_is_equal(set1->dim, set2->dim);
6895 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6897 int is_subset;
6899 if (!map1 || !map2)
6900 return -1;
6901 is_subset = isl_map_is_subset(map1, map2);
6902 if (is_subset != 1)
6903 return is_subset;
6904 is_subset = isl_map_is_subset(map2, map1);
6905 return is_subset;
6908 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6910 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
6913 int isl_basic_map_is_strict_subset(
6914 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6916 int is_subset;
6918 if (!bmap1 || !bmap2)
6919 return -1;
6920 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6921 if (is_subset != 1)
6922 return is_subset;
6923 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6924 if (is_subset == -1)
6925 return is_subset;
6926 return !is_subset;
6929 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6931 int is_subset;
6933 if (!map1 || !map2)
6934 return -1;
6935 is_subset = isl_map_is_subset(map1, map2);
6936 if (is_subset != 1)
6937 return is_subset;
6938 is_subset = isl_map_is_subset(map2, map1);
6939 if (is_subset == -1)
6940 return is_subset;
6941 return !is_subset;
6944 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6946 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6949 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6951 if (!bmap)
6952 return -1;
6953 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6956 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6958 if (!bset)
6959 return -1;
6960 return bset->n_eq == 0 && bset->n_ineq == 0;
6963 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6965 int i;
6967 if (!map)
6968 return -1;
6970 for (i = 0; i < map->n; ++i) {
6971 int r = isl_basic_map_is_universe(map->p[i]);
6972 if (r < 0 || r)
6973 return r;
6976 return 0;
6979 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6981 return isl_map_plain_is_universe((isl_map *) set);
6984 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6986 return isl_set_plain_is_universe(set);
6989 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6991 struct isl_basic_set *bset = NULL;
6992 struct isl_vec *sample = NULL;
6993 int empty;
6994 unsigned total;
6996 if (!bmap)
6997 return -1;
6999 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7000 return 1;
7002 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7003 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7004 copy = isl_basic_map_remove_redundancies(copy);
7005 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
7006 isl_basic_map_free(copy);
7007 return empty;
7010 total = 1 + isl_basic_map_total_dim(bmap);
7011 if (bmap->sample && bmap->sample->size == total) {
7012 int contains = isl_basic_map_contains(bmap, bmap->sample);
7013 if (contains < 0)
7014 return -1;
7015 if (contains)
7016 return 0;
7018 isl_vec_free(bmap->sample);
7019 bmap->sample = NULL;
7020 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7021 if (!bset)
7022 return -1;
7023 sample = isl_basic_set_sample_vec(bset);
7024 if (!sample)
7025 return -1;
7026 empty = sample->size == 0;
7027 isl_vec_free(bmap->sample);
7028 bmap->sample = sample;
7029 if (empty)
7030 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7032 return empty;
7035 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7037 if (!bmap)
7038 return -1;
7039 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7042 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7044 return isl_basic_map_plain_is_empty(bmap);
7047 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7049 if (!bset)
7050 return -1;
7051 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7054 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7056 return isl_basic_set_plain_is_empty(bset);
7059 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7061 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7064 struct isl_map *isl_basic_map_union(
7065 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7067 struct isl_map *map;
7068 if (!bmap1 || !bmap2)
7069 goto error;
7071 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7073 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7074 if (!map)
7075 goto error;
7076 map = isl_map_add_basic_map(map, bmap1);
7077 map = isl_map_add_basic_map(map, bmap2);
7078 return map;
7079 error:
7080 isl_basic_map_free(bmap1);
7081 isl_basic_map_free(bmap2);
7082 return NULL;
7085 struct isl_set *isl_basic_set_union(
7086 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7088 return (struct isl_set *)isl_basic_map_union(
7089 (struct isl_basic_map *)bset1,
7090 (struct isl_basic_map *)bset2);
7093 /* Order divs such that any div only depends on previous divs */
7094 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7096 int i;
7097 unsigned off;
7099 if (!bmap)
7100 return NULL;
7102 off = isl_space_dim(bmap->dim, isl_dim_all);
7104 for (i = 0; i < bmap->n_div; ++i) {
7105 int pos;
7106 if (isl_int_is_zero(bmap->div[i][0]))
7107 continue;
7108 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7109 bmap->n_div-i);
7110 if (pos == -1)
7111 continue;
7112 isl_basic_map_swap_div(bmap, i, i + pos);
7113 --i;
7115 return bmap;
7118 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7120 return (struct isl_basic_set *)
7121 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7124 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7126 int i;
7128 if (!map)
7129 return 0;
7131 for (i = 0; i < map->n; ++i) {
7132 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7133 if (!map->p[i])
7134 goto error;
7137 return map;
7138 error:
7139 isl_map_free(map);
7140 return NULL;
7143 /* Apply the expansion computed by isl_merge_divs.
7144 * The expansion itself is given by "exp" while the resulting
7145 * list of divs is given by "div".
7147 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7148 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7150 int i, j;
7151 int n_div;
7153 bset = isl_basic_set_cow(bset);
7154 if (!bset || !div)
7155 goto error;
7157 if (div->n_row < bset->n_div)
7158 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7159 "not an expansion", goto error);
7161 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
7162 div->n_row - bset->n_div, 0,
7163 2 * (div->n_row - bset->n_div));
7165 n_div = bset->n_div;
7166 for (i = n_div; i < div->n_row; ++i)
7167 if (isl_basic_set_alloc_div(bset) < 0)
7168 goto error;
7170 j = n_div - 1;
7171 for (i = div->n_row - 1; i >= 0; --i) {
7172 if (j >= 0 && exp[j] == i) {
7173 if (i != j)
7174 isl_basic_map_swap_div(bset, i, j);
7175 j--;
7176 } else {
7177 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
7178 if (isl_basic_map_add_div_constraints(bset, i) < 0)
7179 goto error;
7183 isl_mat_free(div);
7184 return bset;
7185 error:
7186 isl_basic_set_free(bset);
7187 isl_mat_free(div);
7188 return NULL;
7191 /* Look for a div in dst that corresponds to the div "div" in src.
7192 * The divs before "div" in src and dst are assumed to be the same.
7194 * Returns -1 if no corresponding div was found and the position
7195 * of the corresponding div in dst otherwise.
7197 static int find_div(struct isl_basic_map *dst,
7198 struct isl_basic_map *src, unsigned div)
7200 int i;
7202 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7204 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7205 for (i = div; i < dst->n_div; ++i)
7206 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7207 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7208 dst->n_div - div) == -1)
7209 return i;
7210 return -1;
7213 struct isl_basic_map *isl_basic_map_align_divs(
7214 struct isl_basic_map *dst, struct isl_basic_map *src)
7216 int i;
7217 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7219 if (!dst || !src)
7220 goto error;
7222 if (src->n_div == 0)
7223 return dst;
7225 for (i = 0; i < src->n_div; ++i)
7226 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7228 src = isl_basic_map_order_divs(src);
7229 dst = isl_basic_map_cow(dst);
7230 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7231 src->n_div, 0, 2 * src->n_div);
7232 if (!dst)
7233 return NULL;
7234 for (i = 0; i < src->n_div; ++i) {
7235 int j = find_div(dst, src, i);
7236 if (j < 0) {
7237 j = isl_basic_map_alloc_div(dst);
7238 if (j < 0)
7239 goto error;
7240 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7241 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7242 if (isl_basic_map_add_div_constraints(dst, j) < 0)
7243 goto error;
7245 if (j != i)
7246 isl_basic_map_swap_div(dst, i, j);
7248 return dst;
7249 error:
7250 isl_basic_map_free(dst);
7251 return NULL;
7254 struct isl_basic_set *isl_basic_set_align_divs(
7255 struct isl_basic_set *dst, struct isl_basic_set *src)
7257 return (struct isl_basic_set *)isl_basic_map_align_divs(
7258 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7261 struct isl_map *isl_map_align_divs(struct isl_map *map)
7263 int i;
7265 if (!map)
7266 return NULL;
7267 if (map->n == 0)
7268 return map;
7269 map = isl_map_compute_divs(map);
7270 map = isl_map_cow(map);
7271 if (!map)
7272 return NULL;
7274 for (i = 1; i < map->n; ++i)
7275 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7276 for (i = 1; i < map->n; ++i)
7277 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7279 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7280 return map;
7283 struct isl_set *isl_set_align_divs(struct isl_set *set)
7285 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7288 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7289 __isl_take isl_map *map)
7291 if (!set || !map)
7292 goto error;
7293 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7294 map = isl_map_intersect_domain(map, set);
7295 set = isl_map_range(map);
7296 return set;
7297 error:
7298 isl_set_free(set);
7299 isl_map_free(map);
7300 return NULL;
7303 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7304 __isl_take isl_map *map)
7306 return isl_map_align_params_map_map_and(set, map, &set_apply);
7309 /* There is no need to cow as removing empty parts doesn't change
7310 * the meaning of the set.
7312 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7314 int i;
7316 if (!map)
7317 return NULL;
7319 for (i = map->n - 1; i >= 0; --i)
7320 remove_if_empty(map, i);
7322 return map;
7325 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7327 return (struct isl_set *)
7328 isl_map_remove_empty_parts((struct isl_map *)set);
7331 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7333 struct isl_basic_map *bmap;
7334 if (!map || map->n == 0)
7335 return NULL;
7336 bmap = map->p[map->n-1];
7337 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7338 return isl_basic_map_copy(bmap);
7341 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7343 return (struct isl_basic_set *)
7344 isl_map_copy_basic_map((struct isl_map *)set);
7347 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7348 __isl_keep isl_basic_map *bmap)
7350 int i;
7352 if (!map || !bmap)
7353 goto error;
7354 for (i = map->n-1; i >= 0; --i) {
7355 if (map->p[i] != bmap)
7356 continue;
7357 map = isl_map_cow(map);
7358 if (!map)
7359 goto error;
7360 isl_basic_map_free(map->p[i]);
7361 if (i != map->n-1) {
7362 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7363 map->p[i] = map->p[map->n-1];
7365 map->n--;
7366 return map;
7368 return map;
7369 error:
7370 isl_map_free(map);
7371 return NULL;
7374 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7375 struct isl_basic_set *bset)
7377 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7378 (struct isl_basic_map *)bset);
7381 /* Given two basic sets bset1 and bset2, compute the maximal difference
7382 * between the values of dimension pos in bset1 and those in bset2
7383 * for any common value of the parameters and dimensions preceding pos.
7385 static enum isl_lp_result basic_set_maximal_difference_at(
7386 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7387 int pos, isl_int *opt)
7389 isl_space *dims;
7390 struct isl_basic_map *bmap1 = NULL;
7391 struct isl_basic_map *bmap2 = NULL;
7392 struct isl_ctx *ctx;
7393 struct isl_vec *obj;
7394 unsigned total;
7395 unsigned nparam;
7396 unsigned dim1, dim2;
7397 enum isl_lp_result res;
7399 if (!bset1 || !bset2)
7400 return isl_lp_error;
7402 nparam = isl_basic_set_n_param(bset1);
7403 dim1 = isl_basic_set_n_dim(bset1);
7404 dim2 = isl_basic_set_n_dim(bset2);
7405 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7406 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7407 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7408 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7409 if (!bmap1 || !bmap2)
7410 goto error;
7411 bmap1 = isl_basic_map_cow(bmap1);
7412 bmap1 = isl_basic_map_extend(bmap1, nparam,
7413 pos, (dim1 - pos) + (dim2 - pos),
7414 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7415 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7416 if (!bmap1)
7417 goto error;
7418 total = isl_basic_map_total_dim(bmap1);
7419 ctx = bmap1->ctx;
7420 obj = isl_vec_alloc(ctx, 1 + total);
7421 isl_seq_clr(obj->block.data, 1 + total);
7422 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7423 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7424 if (!obj)
7425 goto error;
7426 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7427 opt, NULL, NULL);
7428 isl_basic_map_free(bmap1);
7429 isl_vec_free(obj);
7430 return res;
7431 error:
7432 isl_basic_map_free(bmap1);
7433 isl_basic_map_free(bmap2);
7434 return isl_lp_error;
7437 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7438 * for any common value of the parameters and dimensions preceding pos
7439 * in both basic sets, the values of dimension pos in bset1 are
7440 * smaller or larger than those in bset2.
7442 * Returns
7443 * 1 if bset1 follows bset2
7444 * -1 if bset1 precedes bset2
7445 * 0 if bset1 and bset2 are incomparable
7446 * -2 if some error occurred.
7448 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7449 struct isl_basic_set *bset2, int pos)
7451 isl_int opt;
7452 enum isl_lp_result res;
7453 int cmp;
7455 isl_int_init(opt);
7457 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7459 if (res == isl_lp_empty)
7460 cmp = 0;
7461 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7462 res == isl_lp_unbounded)
7463 cmp = 1;
7464 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7465 cmp = -1;
7466 else
7467 cmp = -2;
7469 isl_int_clear(opt);
7470 return cmp;
7473 /* Given two basic sets bset1 and bset2, check whether
7474 * for any common value of the parameters and dimensions preceding pos
7475 * there is a value of dimension pos in bset1 that is larger
7476 * than a value of the same dimension in bset2.
7478 * Return
7479 * 1 if there exists such a pair
7480 * 0 if there is no such pair, but there is a pair of equal values
7481 * -1 otherwise
7482 * -2 if some error occurred.
7484 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7485 __isl_keep isl_basic_set *bset2, int pos)
7487 isl_int opt;
7488 enum isl_lp_result res;
7489 int cmp;
7491 isl_int_init(opt);
7493 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7495 if (res == isl_lp_empty)
7496 cmp = -1;
7497 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7498 res == isl_lp_unbounded)
7499 cmp = 1;
7500 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7501 cmp = -1;
7502 else if (res == isl_lp_ok)
7503 cmp = 0;
7504 else
7505 cmp = -2;
7507 isl_int_clear(opt);
7508 return cmp;
7511 /* Given two sets set1 and set2, check whether
7512 * for any common value of the parameters and dimensions preceding pos
7513 * there is a value of dimension pos in set1 that is larger
7514 * than a value of the same dimension in set2.
7516 * Return
7517 * 1 if there exists such a pair
7518 * 0 if there is no such pair, but there is a pair of equal values
7519 * -1 otherwise
7520 * -2 if some error occurred.
7522 int isl_set_follows_at(__isl_keep isl_set *set1,
7523 __isl_keep isl_set *set2, int pos)
7525 int i, j;
7526 int follows = -1;
7528 if (!set1 || !set2)
7529 return -2;
7531 for (i = 0; i < set1->n; ++i)
7532 for (j = 0; j < set2->n; ++j) {
7533 int f;
7534 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7535 if (f == 1 || f == -2)
7536 return f;
7537 if (f > follows)
7538 follows = f;
7541 return follows;
7544 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7545 unsigned pos, isl_int *val)
7547 int i;
7548 int d;
7549 unsigned total;
7551 if (!bmap)
7552 return -1;
7553 total = isl_basic_map_total_dim(bmap);
7554 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7555 for (; d+1 > pos; --d)
7556 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7557 break;
7558 if (d != pos)
7559 continue;
7560 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7561 return 0;
7562 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7563 return 0;
7564 if (!isl_int_is_one(bmap->eq[i][1+d]))
7565 return 0;
7566 if (val)
7567 isl_int_neg(*val, bmap->eq[i][0]);
7568 return 1;
7570 return 0;
7573 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7574 unsigned pos, isl_int *val)
7576 int i;
7577 isl_int v;
7578 isl_int tmp;
7579 int fixed;
7581 if (!map)
7582 return -1;
7583 if (map->n == 0)
7584 return 0;
7585 if (map->n == 1)
7586 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7587 isl_int_init(v);
7588 isl_int_init(tmp);
7589 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7590 for (i = 1; fixed == 1 && i < map->n; ++i) {
7591 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7592 if (fixed == 1 && isl_int_ne(tmp, v))
7593 fixed = 0;
7595 if (val)
7596 isl_int_set(*val, v);
7597 isl_int_clear(tmp);
7598 isl_int_clear(v);
7599 return fixed;
7602 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7603 unsigned pos, isl_int *val)
7605 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7606 pos, val);
7609 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7610 isl_int *val)
7612 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7615 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7616 enum isl_dim_type type, unsigned pos, isl_int *val)
7618 if (pos >= isl_basic_map_dim(bmap, type))
7619 return -1;
7620 return isl_basic_map_plain_has_fixed_var(bmap,
7621 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7624 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7625 enum isl_dim_type type, unsigned pos, isl_int *val)
7627 if (pos >= isl_map_dim(map, type))
7628 return -1;
7629 return isl_map_plain_has_fixed_var(map,
7630 map_offset(map, type) - 1 + pos, val);
7633 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
7634 enum isl_dim_type type, unsigned pos, isl_int *val)
7636 return isl_map_plain_is_fixed(set, type, pos, val);
7639 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7640 enum isl_dim_type type, unsigned pos, isl_int *val)
7642 return isl_map_plain_is_fixed(map, type, pos, val);
7645 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7646 * then return this fixed value in *val.
7648 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7649 unsigned dim, isl_int *val)
7651 return isl_basic_set_plain_has_fixed_var(bset,
7652 isl_basic_set_n_param(bset) + dim, val);
7655 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7656 * then return this fixed value in *val.
7658 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7659 unsigned dim, isl_int *val)
7661 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7664 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7665 unsigned dim, isl_int *val)
7667 return isl_set_plain_dim_is_fixed(set, dim, val);
7670 /* Check if input variable in has fixed value and if so and if val is not NULL,
7671 * then return this fixed value in *val.
7673 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7674 unsigned in, isl_int *val)
7676 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7679 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7680 * and if val is not NULL, then return this lower bound in *val.
7682 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7683 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7685 int i, i_eq = -1, i_ineq = -1;
7686 isl_int *c;
7687 unsigned total;
7688 unsigned nparam;
7690 if (!bset)
7691 return -1;
7692 total = isl_basic_set_total_dim(bset);
7693 nparam = isl_basic_set_n_param(bset);
7694 for (i = 0; i < bset->n_eq; ++i) {
7695 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7696 continue;
7697 if (i_eq != -1)
7698 return 0;
7699 i_eq = i;
7701 for (i = 0; i < bset->n_ineq; ++i) {
7702 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7703 continue;
7704 if (i_eq != -1 || i_ineq != -1)
7705 return 0;
7706 i_ineq = i;
7708 if (i_eq == -1 && i_ineq == -1)
7709 return 0;
7710 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7711 /* The coefficient should always be one due to normalization. */
7712 if (!isl_int_is_one(c[1+nparam+dim]))
7713 return 0;
7714 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7715 return 0;
7716 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7717 total - nparam - dim - 1) != -1)
7718 return 0;
7719 if (val)
7720 isl_int_neg(*val, c[0]);
7721 return 1;
7724 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7725 unsigned dim, isl_int *val)
7727 int i;
7728 isl_int v;
7729 isl_int tmp;
7730 int fixed;
7732 if (!set)
7733 return -1;
7734 if (set->n == 0)
7735 return 0;
7736 if (set->n == 1)
7737 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7738 dim, val);
7739 isl_int_init(v);
7740 isl_int_init(tmp);
7741 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7742 dim, &v);
7743 for (i = 1; fixed == 1 && i < set->n; ++i) {
7744 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7745 dim, &tmp);
7746 if (fixed == 1 && isl_int_ne(tmp, v))
7747 fixed = 0;
7749 if (val)
7750 isl_int_set(*val, v);
7751 isl_int_clear(tmp);
7752 isl_int_clear(v);
7753 return fixed;
7756 struct constraint {
7757 unsigned size;
7758 isl_int *c;
7761 /* uset_gist depends on constraints without existentially quantified
7762 * variables sorting first.
7764 static int qsort_constraint_cmp(const void *p1, const void *p2)
7766 const struct constraint *c1 = (const struct constraint *)p1;
7767 const struct constraint *c2 = (const struct constraint *)p2;
7768 int l1, l2;
7769 unsigned size = isl_min(c1->size, c2->size);
7771 l1 = isl_seq_last_non_zero(c1->c, size);
7772 l2 = isl_seq_last_non_zero(c2->c, size);
7774 if (l1 != l2)
7775 return l1 - l2;
7777 return isl_seq_cmp(c1->c, c2->c, size);
7780 static struct isl_basic_map *isl_basic_map_sort_constraints(
7781 struct isl_basic_map *bmap)
7783 int i;
7784 struct constraint *c;
7785 unsigned total;
7787 if (!bmap)
7788 return NULL;
7789 total = isl_basic_map_total_dim(bmap);
7790 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7791 if (!c)
7792 goto error;
7793 for (i = 0; i < bmap->n_ineq; ++i) {
7794 c[i].size = total;
7795 c[i].c = bmap->ineq[i];
7797 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7798 for (i = 0; i < bmap->n_ineq; ++i)
7799 bmap->ineq[i] = c[i].c;
7800 free(c);
7801 return bmap;
7802 error:
7803 isl_basic_map_free(bmap);
7804 return NULL;
7807 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7808 __isl_take isl_basic_set *bset)
7810 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7811 (struct isl_basic_map *)bset);
7814 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7816 if (!bmap)
7817 return NULL;
7818 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7819 return bmap;
7820 bmap = isl_basic_map_remove_redundancies(bmap);
7821 bmap = isl_basic_map_sort_constraints(bmap);
7822 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7823 return bmap;
7826 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7828 return (struct isl_basic_set *)isl_basic_map_normalize(
7829 (struct isl_basic_map *)bset);
7832 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7833 const __isl_keep isl_basic_map *bmap2)
7835 int i, cmp;
7836 unsigned total;
7838 if (bmap1 == bmap2)
7839 return 0;
7840 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7841 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7842 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7843 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7844 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7845 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7846 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7847 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7848 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7849 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7850 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7851 return 0;
7852 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7853 return 1;
7854 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7855 return -1;
7856 if (bmap1->n_eq != bmap2->n_eq)
7857 return bmap1->n_eq - bmap2->n_eq;
7858 if (bmap1->n_ineq != bmap2->n_ineq)
7859 return bmap1->n_ineq - bmap2->n_ineq;
7860 if (bmap1->n_div != bmap2->n_div)
7861 return bmap1->n_div - bmap2->n_div;
7862 total = isl_basic_map_total_dim(bmap1);
7863 for (i = 0; i < bmap1->n_eq; ++i) {
7864 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7865 if (cmp)
7866 return cmp;
7868 for (i = 0; i < bmap1->n_ineq; ++i) {
7869 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7870 if (cmp)
7871 return cmp;
7873 for (i = 0; i < bmap1->n_div; ++i) {
7874 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7875 if (cmp)
7876 return cmp;
7878 return 0;
7881 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
7882 const __isl_keep isl_basic_set *bset2)
7884 return isl_basic_map_plain_cmp(bset1, bset2);
7887 int isl_set_plain_cmp(const __isl_keep isl_set *set1,
7888 const __isl_keep isl_set *set2)
7890 int i, cmp;
7892 if (set1 == set2)
7893 return 0;
7894 if (set1->n != set2->n)
7895 return set1->n - set2->n;
7897 for (i = 0; i < set1->n; ++i) {
7898 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
7899 if (cmp)
7900 return cmp;
7903 return 0;
7906 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7907 __isl_keep isl_basic_map *bmap2)
7909 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7912 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7913 __isl_keep isl_basic_set *bset2)
7915 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7916 (isl_basic_map *)bset2);
7919 static int qsort_bmap_cmp(const void *p1, const void *p2)
7921 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7922 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7924 return isl_basic_map_plain_cmp(bmap1, bmap2);
7927 /* We normalize in place, but if anything goes wrong we need
7928 * to return NULL, so we need to make sure we don't change the
7929 * meaning of any possible other copies of map.
7931 struct isl_map *isl_map_normalize(struct isl_map *map)
7933 int i, j;
7934 struct isl_basic_map *bmap;
7936 if (!map)
7937 return NULL;
7938 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7939 return map;
7940 for (i = 0; i < map->n; ++i) {
7941 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7942 if (!bmap)
7943 goto error;
7944 isl_basic_map_free(map->p[i]);
7945 map->p[i] = bmap;
7947 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7948 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7949 map = isl_map_remove_empty_parts(map);
7950 if (!map)
7951 return NULL;
7952 for (i = map->n - 1; i >= 1; --i) {
7953 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7954 continue;
7955 isl_basic_map_free(map->p[i-1]);
7956 for (j = i; j < map->n; ++j)
7957 map->p[j-1] = map->p[j];
7958 map->n--;
7960 return map;
7961 error:
7962 isl_map_free(map);
7963 return NULL;
7967 struct isl_set *isl_set_normalize(struct isl_set *set)
7969 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7972 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7974 int i;
7975 int equal;
7977 if (!map1 || !map2)
7978 return -1;
7980 if (map1 == map2)
7981 return 1;
7982 if (!isl_space_is_equal(map1->dim, map2->dim))
7983 return 0;
7985 map1 = isl_map_copy(map1);
7986 map2 = isl_map_copy(map2);
7987 map1 = isl_map_normalize(map1);
7988 map2 = isl_map_normalize(map2);
7989 if (!map1 || !map2)
7990 goto error;
7991 equal = map1->n == map2->n;
7992 for (i = 0; equal && i < map1->n; ++i) {
7993 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7994 if (equal < 0)
7995 goto error;
7997 isl_map_free(map1);
7998 isl_map_free(map2);
7999 return equal;
8000 error:
8001 isl_map_free(map1);
8002 isl_map_free(map2);
8003 return -1;
8006 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8008 return isl_map_plain_is_equal(map1, map2);
8011 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8013 return isl_map_plain_is_equal((struct isl_map *)set1,
8014 (struct isl_map *)set2);
8017 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8019 return isl_set_plain_is_equal(set1, set2);
8022 /* Return an interval that ranges from min to max (inclusive)
8024 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8025 isl_int min, isl_int max)
8027 int k;
8028 struct isl_basic_set *bset = NULL;
8030 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8031 if (!bset)
8032 goto error;
8034 k = isl_basic_set_alloc_inequality(bset);
8035 if (k < 0)
8036 goto error;
8037 isl_int_set_si(bset->ineq[k][1], 1);
8038 isl_int_neg(bset->ineq[k][0], min);
8040 k = isl_basic_set_alloc_inequality(bset);
8041 if (k < 0)
8042 goto error;
8043 isl_int_set_si(bset->ineq[k][1], -1);
8044 isl_int_set(bset->ineq[k][0], max);
8046 return bset;
8047 error:
8048 isl_basic_set_free(bset);
8049 return NULL;
8052 /* Return the Cartesian product of the basic sets in list (in the given order).
8054 __isl_give isl_basic_set *isl_basic_set_list_product(
8055 __isl_take struct isl_basic_set_list *list)
8057 int i;
8058 unsigned dim;
8059 unsigned nparam;
8060 unsigned extra;
8061 unsigned n_eq;
8062 unsigned n_ineq;
8063 struct isl_basic_set *product = NULL;
8065 if (!list)
8066 goto error;
8067 isl_assert(list->ctx, list->n > 0, goto error);
8068 isl_assert(list->ctx, list->p[0], goto error);
8069 nparam = isl_basic_set_n_param(list->p[0]);
8070 dim = isl_basic_set_n_dim(list->p[0]);
8071 extra = list->p[0]->n_div;
8072 n_eq = list->p[0]->n_eq;
8073 n_ineq = list->p[0]->n_ineq;
8074 for (i = 1; i < list->n; ++i) {
8075 isl_assert(list->ctx, list->p[i], goto error);
8076 isl_assert(list->ctx,
8077 nparam == isl_basic_set_n_param(list->p[i]), goto error);
8078 dim += isl_basic_set_n_dim(list->p[i]);
8079 extra += list->p[i]->n_div;
8080 n_eq += list->p[i]->n_eq;
8081 n_ineq += list->p[i]->n_ineq;
8083 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
8084 n_eq, n_ineq);
8085 if (!product)
8086 goto error;
8087 dim = 0;
8088 for (i = 0; i < list->n; ++i) {
8089 isl_basic_set_add_constraints(product,
8090 isl_basic_set_copy(list->p[i]), dim);
8091 dim += isl_basic_set_n_dim(list->p[i]);
8093 isl_basic_set_list_free(list);
8094 return product;
8095 error:
8096 isl_basic_set_free(product);
8097 isl_basic_set_list_free(list);
8098 return NULL;
8101 struct isl_basic_map *isl_basic_map_product(
8102 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8104 isl_space *dim_result = NULL;
8105 struct isl_basic_map *bmap;
8106 unsigned in1, in2, out1, out2, nparam, total, pos;
8107 struct isl_dim_map *dim_map1, *dim_map2;
8109 if (!bmap1 || !bmap2)
8110 goto error;
8112 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
8113 bmap2->dim, isl_dim_param), goto error);
8114 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
8115 isl_space_copy(bmap2->dim));
8117 in1 = isl_basic_map_n_in(bmap1);
8118 in2 = isl_basic_map_n_in(bmap2);
8119 out1 = isl_basic_map_n_out(bmap1);
8120 out2 = isl_basic_map_n_out(bmap2);
8121 nparam = isl_basic_map_n_param(bmap1);
8123 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
8124 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8125 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8126 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8127 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8128 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8129 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8130 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8131 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8132 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8133 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8135 bmap = isl_basic_map_alloc_space(dim_result,
8136 bmap1->n_div + bmap2->n_div,
8137 bmap1->n_eq + bmap2->n_eq,
8138 bmap1->n_ineq + bmap2->n_ineq);
8139 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8140 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8141 bmap = isl_basic_map_simplify(bmap);
8142 return isl_basic_map_finalize(bmap);
8143 error:
8144 isl_basic_map_free(bmap1);
8145 isl_basic_map_free(bmap2);
8146 return NULL;
8149 __isl_give isl_basic_map *isl_basic_map_flat_product(
8150 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8152 isl_basic_map *prod;
8154 prod = isl_basic_map_product(bmap1, bmap2);
8155 prod = isl_basic_map_flatten(prod);
8156 return prod;
8159 __isl_give isl_basic_set *isl_basic_set_flat_product(
8160 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
8162 return isl_basic_map_flat_range_product(bset1, bset2);
8165 __isl_give isl_basic_map *isl_basic_map_domain_product(
8166 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8168 isl_space *space_result = NULL;
8169 isl_basic_map *bmap;
8170 unsigned in1, in2, out, nparam, total, pos;
8171 struct isl_dim_map *dim_map1, *dim_map2;
8173 if (!bmap1 || !bmap2)
8174 goto error;
8176 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8177 isl_space_copy(bmap2->dim));
8179 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8180 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8181 out = isl_basic_map_dim(bmap1, isl_dim_out);
8182 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8184 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8185 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8186 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8187 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8188 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8189 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8190 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8191 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8192 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8193 isl_dim_map_div(dim_map1, bmap1, pos += out);
8194 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8196 bmap = isl_basic_map_alloc_space(space_result,
8197 bmap1->n_div + bmap2->n_div,
8198 bmap1->n_eq + bmap2->n_eq,
8199 bmap1->n_ineq + bmap2->n_ineq);
8200 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8201 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8202 bmap = isl_basic_map_simplify(bmap);
8203 return isl_basic_map_finalize(bmap);
8204 error:
8205 isl_basic_map_free(bmap1);
8206 isl_basic_map_free(bmap2);
8207 return NULL;
8210 __isl_give isl_basic_map *isl_basic_map_range_product(
8211 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8213 isl_space *dim_result = NULL;
8214 isl_basic_map *bmap;
8215 unsigned in, out1, out2, nparam, total, pos;
8216 struct isl_dim_map *dim_map1, *dim_map2;
8218 if (!bmap1 || !bmap2)
8219 goto error;
8221 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8222 isl_space_copy(bmap2->dim));
8224 in = isl_basic_map_dim(bmap1, isl_dim_in);
8225 out1 = isl_basic_map_n_out(bmap1);
8226 out2 = isl_basic_map_n_out(bmap2);
8227 nparam = isl_basic_map_n_param(bmap1);
8229 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8230 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8231 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8232 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8233 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8234 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8235 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8236 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8237 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8238 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8239 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8241 bmap = isl_basic_map_alloc_space(dim_result,
8242 bmap1->n_div + bmap2->n_div,
8243 bmap1->n_eq + bmap2->n_eq,
8244 bmap1->n_ineq + bmap2->n_ineq);
8245 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8246 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8247 bmap = isl_basic_map_simplify(bmap);
8248 return isl_basic_map_finalize(bmap);
8249 error:
8250 isl_basic_map_free(bmap1);
8251 isl_basic_map_free(bmap2);
8252 return NULL;
8255 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8256 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8258 isl_basic_map *prod;
8260 prod = isl_basic_map_range_product(bmap1, bmap2);
8261 prod = isl_basic_map_flatten_range(prod);
8262 return prod;
8265 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8266 __isl_take isl_map *map2,
8267 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8268 __isl_take isl_space *right),
8269 __isl_give isl_basic_map *(*basic_map_product)(
8270 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8272 unsigned flags = 0;
8273 struct isl_map *result;
8274 int i, j;
8276 if (!map1 || !map2)
8277 goto error;
8279 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8280 map2->dim, isl_dim_param), goto error);
8282 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8283 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8284 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8286 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8287 isl_space_copy(map2->dim)),
8288 map1->n * map2->n, flags);
8289 if (!result)
8290 goto error;
8291 for (i = 0; i < map1->n; ++i)
8292 for (j = 0; j < map2->n; ++j) {
8293 struct isl_basic_map *part;
8294 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8295 isl_basic_map_copy(map2->p[j]));
8296 if (isl_basic_map_is_empty(part))
8297 isl_basic_map_free(part);
8298 else
8299 result = isl_map_add_basic_map(result, part);
8300 if (!result)
8301 goto error;
8303 isl_map_free(map1);
8304 isl_map_free(map2);
8305 return result;
8306 error:
8307 isl_map_free(map1);
8308 isl_map_free(map2);
8309 return NULL;
8312 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8314 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8315 __isl_take isl_map *map2)
8317 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8320 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8321 __isl_take isl_map *map2)
8323 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8326 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8328 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8329 __isl_take isl_map *map2)
8331 isl_map *prod;
8333 prod = isl_map_product(map1, map2);
8334 prod = isl_map_flatten(prod);
8335 return prod;
8338 /* Given two set A and B, construct its Cartesian product A x B.
8340 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8342 return isl_map_range_product(set1, set2);
8345 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8346 __isl_take isl_set *set2)
8348 return isl_map_flat_range_product(set1, set2);
8351 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8353 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8354 __isl_take isl_map *map2)
8356 return map_product(map1, map2, &isl_space_domain_product,
8357 &isl_basic_map_domain_product);
8360 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8362 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8363 __isl_take isl_map *map2)
8365 return map_product(map1, map2, &isl_space_range_product,
8366 &isl_basic_map_range_product);
8369 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8370 __isl_take isl_map *map2)
8372 return isl_map_align_params_map_map_and(map1, map2,
8373 &map_domain_product_aligned);
8376 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8377 __isl_take isl_map *map2)
8379 return isl_map_align_params_map_map_and(map1, map2,
8380 &map_range_product_aligned);
8383 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8385 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8386 __isl_take isl_map *map2)
8388 isl_map *prod;
8390 prod = isl_map_domain_product(map1, map2);
8391 prod = isl_map_flatten_domain(prod);
8392 return prod;
8395 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8397 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8398 __isl_take isl_map *map2)
8400 isl_map *prod;
8402 prod = isl_map_range_product(map1, map2);
8403 prod = isl_map_flatten_range(prod);
8404 return prod;
8407 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8409 int i;
8410 uint32_t hash = isl_hash_init();
8411 unsigned total;
8413 if (!bmap)
8414 return 0;
8415 bmap = isl_basic_map_copy(bmap);
8416 bmap = isl_basic_map_normalize(bmap);
8417 if (!bmap)
8418 return 0;
8419 total = isl_basic_map_total_dim(bmap);
8420 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8421 for (i = 0; i < bmap->n_eq; ++i) {
8422 uint32_t c_hash;
8423 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8424 isl_hash_hash(hash, c_hash);
8426 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8427 for (i = 0; i < bmap->n_ineq; ++i) {
8428 uint32_t c_hash;
8429 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8430 isl_hash_hash(hash, c_hash);
8432 isl_hash_byte(hash, bmap->n_div & 0xFF);
8433 for (i = 0; i < bmap->n_div; ++i) {
8434 uint32_t c_hash;
8435 if (isl_int_is_zero(bmap->div[i][0]))
8436 continue;
8437 isl_hash_byte(hash, i & 0xFF);
8438 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8439 isl_hash_hash(hash, c_hash);
8441 isl_basic_map_free(bmap);
8442 return hash;
8445 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8447 return isl_basic_map_get_hash((isl_basic_map *)bset);
8450 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8452 int i;
8453 uint32_t hash;
8455 if (!map)
8456 return 0;
8457 map = isl_map_copy(map);
8458 map = isl_map_normalize(map);
8459 if (!map)
8460 return 0;
8462 hash = isl_hash_init();
8463 for (i = 0; i < map->n; ++i) {
8464 uint32_t bmap_hash;
8465 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8466 isl_hash_hash(hash, bmap_hash);
8469 isl_map_free(map);
8471 return hash;
8474 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8476 return isl_map_get_hash((isl_map *)set);
8479 /* Check if the value for dimension dim is completely determined
8480 * by the values of the other parameters and variables.
8481 * That is, check if dimension dim is involved in an equality.
8483 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8485 int i;
8486 unsigned nparam;
8488 if (!bset)
8489 return -1;
8490 nparam = isl_basic_set_n_param(bset);
8491 for (i = 0; i < bset->n_eq; ++i)
8492 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8493 return 1;
8494 return 0;
8497 /* Check if the value for dimension dim is completely determined
8498 * by the values of the other parameters and variables.
8499 * That is, check if dimension dim is involved in an equality
8500 * for each of the subsets.
8502 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8504 int i;
8506 if (!set)
8507 return -1;
8508 for (i = 0; i < set->n; ++i) {
8509 int unique;
8510 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8511 if (unique != 1)
8512 return unique;
8514 return 1;
8517 int isl_set_n_basic_set(__isl_keep isl_set *set)
8519 return set ? set->n : 0;
8522 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8523 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8525 int i;
8527 if (!map)
8528 return -1;
8530 for (i = 0; i < map->n; ++i)
8531 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8532 return -1;
8534 return 0;
8537 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8538 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8540 int i;
8542 if (!set)
8543 return -1;
8545 for (i = 0; i < set->n; ++i)
8546 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8547 return -1;
8549 return 0;
8552 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8554 isl_space *dim;
8556 if (!bset)
8557 return NULL;
8559 bset = isl_basic_set_cow(bset);
8560 if (!bset)
8561 return NULL;
8563 dim = isl_basic_set_get_space(bset);
8564 dim = isl_space_lift(dim, bset->n_div);
8565 if (!dim)
8566 goto error;
8567 isl_space_free(bset->dim);
8568 bset->dim = dim;
8569 bset->extra -= bset->n_div;
8570 bset->n_div = 0;
8572 bset = isl_basic_set_finalize(bset);
8574 return bset;
8575 error:
8576 isl_basic_set_free(bset);
8577 return NULL;
8580 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8582 int i;
8583 isl_space *dim;
8584 unsigned n_div;
8586 set = isl_set_align_divs(set);
8588 if (!set)
8589 return NULL;
8591 set = isl_set_cow(set);
8592 if (!set)
8593 return NULL;
8595 n_div = set->p[0]->n_div;
8596 dim = isl_set_get_space(set);
8597 dim = isl_space_lift(dim, n_div);
8598 if (!dim)
8599 goto error;
8600 isl_space_free(set->dim);
8601 set->dim = dim;
8603 for (i = 0; i < set->n; ++i) {
8604 set->p[i] = isl_basic_set_lift(set->p[i]);
8605 if (!set->p[i])
8606 goto error;
8609 return set;
8610 error:
8611 isl_set_free(set);
8612 return NULL;
8615 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8617 isl_space *dim;
8618 struct isl_basic_map *bmap;
8619 unsigned n_set;
8620 unsigned n_div;
8621 unsigned n_param;
8622 unsigned total;
8623 int i, k, l;
8625 set = isl_set_align_divs(set);
8627 if (!set)
8628 return NULL;
8630 dim = isl_set_get_space(set);
8631 if (set->n == 0 || set->p[0]->n_div == 0) {
8632 isl_set_free(set);
8633 return isl_map_identity(isl_space_map_from_set(dim));
8636 n_div = set->p[0]->n_div;
8637 dim = isl_space_map_from_set(dim);
8638 n_param = isl_space_dim(dim, isl_dim_param);
8639 n_set = isl_space_dim(dim, isl_dim_in);
8640 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8641 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8642 for (i = 0; i < n_set; ++i)
8643 bmap = var_equal(bmap, i);
8645 total = n_param + n_set + n_set + n_div;
8646 for (i = 0; i < n_div; ++i) {
8647 k = isl_basic_map_alloc_inequality(bmap);
8648 if (k < 0)
8649 goto error;
8650 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8651 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8652 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8653 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8654 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8655 set->p[0]->div[i][0]);
8657 l = isl_basic_map_alloc_inequality(bmap);
8658 if (l < 0)
8659 goto error;
8660 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8661 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8662 set->p[0]->div[i][0]);
8663 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8666 isl_set_free(set);
8667 bmap = isl_basic_map_simplify(bmap);
8668 bmap = isl_basic_map_finalize(bmap);
8669 return isl_map_from_basic_map(bmap);
8670 error:
8671 isl_set_free(set);
8672 isl_basic_map_free(bmap);
8673 return NULL;
8676 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8678 unsigned dim;
8679 int size = 0;
8681 if (!bset)
8682 return -1;
8684 dim = isl_basic_set_total_dim(bset);
8685 size += bset->n_eq * (1 + dim);
8686 size += bset->n_ineq * (1 + dim);
8687 size += bset->n_div * (2 + dim);
8689 return size;
8692 int isl_set_size(__isl_keep isl_set *set)
8694 int i;
8695 int size = 0;
8697 if (!set)
8698 return -1;
8700 for (i = 0; i < set->n; ++i)
8701 size += isl_basic_set_size(set->p[i]);
8703 return size;
8706 /* Check if there is any lower bound (if lower == 0) and/or upper
8707 * bound (if upper == 0) on the specified dim.
8709 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8710 enum isl_dim_type type, unsigned pos, int lower, int upper)
8712 int i;
8714 if (!bmap)
8715 return -1;
8717 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8719 pos += isl_basic_map_offset(bmap, type);
8721 for (i = 0; i < bmap->n_div; ++i) {
8722 if (isl_int_is_zero(bmap->div[i][0]))
8723 continue;
8724 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8725 return 1;
8728 for (i = 0; i < bmap->n_eq; ++i)
8729 if (!isl_int_is_zero(bmap->eq[i][pos]))
8730 return 1;
8732 for (i = 0; i < bmap->n_ineq; ++i) {
8733 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8734 if (sgn > 0)
8735 lower = 1;
8736 if (sgn < 0)
8737 upper = 1;
8740 return lower && upper;
8743 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8744 enum isl_dim_type type, unsigned pos)
8746 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8749 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8750 enum isl_dim_type type, unsigned pos)
8752 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8755 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8756 enum isl_dim_type type, unsigned pos)
8758 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
8761 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8762 enum isl_dim_type type, unsigned pos)
8764 int i;
8766 if (!map)
8767 return -1;
8769 for (i = 0; i < map->n; ++i) {
8770 int bounded;
8771 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8772 if (bounded < 0 || !bounded)
8773 return bounded;
8776 return 1;
8779 /* Return 1 if the specified dim is involved in both an upper bound
8780 * and a lower bound.
8782 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8783 enum isl_dim_type type, unsigned pos)
8785 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8788 static int has_bound(__isl_keep isl_map *map,
8789 enum isl_dim_type type, unsigned pos,
8790 int (*fn)(__isl_keep isl_basic_map *bmap,
8791 enum isl_dim_type type, unsigned pos))
8793 int i;
8795 if (!map)
8796 return -1;
8798 for (i = 0; i < map->n; ++i) {
8799 int bounded;
8800 bounded = fn(map->p[i], type, pos);
8801 if (bounded < 0 || bounded)
8802 return bounded;
8805 return 0;
8808 /* Return 1 if the specified dim is involved in any lower bound.
8810 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
8811 enum isl_dim_type type, unsigned pos)
8813 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
8816 /* Return 1 if the specified dim is involved in any upper bound.
8818 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
8819 enum isl_dim_type type, unsigned pos)
8821 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
8824 /* For each of the "n" variables starting at "first", determine
8825 * the sign of the variable and put the results in the first "n"
8826 * elements of the array "signs".
8827 * Sign
8828 * 1 means that the variable is non-negative
8829 * -1 means that the variable is non-positive
8830 * 0 means the variable attains both positive and negative values.
8832 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8833 unsigned first, unsigned n, int *signs)
8835 isl_vec *bound = NULL;
8836 struct isl_tab *tab = NULL;
8837 struct isl_tab_undo *snap;
8838 int i;
8840 if (!bset || !signs)
8841 return -1;
8843 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8844 tab = isl_tab_from_basic_set(bset, 0);
8845 if (!bound || !tab)
8846 goto error;
8848 isl_seq_clr(bound->el, bound->size);
8849 isl_int_set_si(bound->el[0], -1);
8851 snap = isl_tab_snap(tab);
8852 for (i = 0; i < n; ++i) {
8853 int empty;
8855 isl_int_set_si(bound->el[1 + first + i], -1);
8856 if (isl_tab_add_ineq(tab, bound->el) < 0)
8857 goto error;
8858 empty = tab->empty;
8859 isl_int_set_si(bound->el[1 + first + i], 0);
8860 if (isl_tab_rollback(tab, snap) < 0)
8861 goto error;
8863 if (empty) {
8864 signs[i] = 1;
8865 continue;
8868 isl_int_set_si(bound->el[1 + first + i], 1);
8869 if (isl_tab_add_ineq(tab, bound->el) < 0)
8870 goto error;
8871 empty = tab->empty;
8872 isl_int_set_si(bound->el[1 + first + i], 0);
8873 if (isl_tab_rollback(tab, snap) < 0)
8874 goto error;
8876 signs[i] = empty ? -1 : 0;
8879 isl_tab_free(tab);
8880 isl_vec_free(bound);
8881 return 0;
8882 error:
8883 isl_tab_free(tab);
8884 isl_vec_free(bound);
8885 return -1;
8888 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8889 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8891 if (!bset || !signs)
8892 return -1;
8893 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8894 return -1);
8896 first += pos(bset->dim, type) - 1;
8897 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8900 /* Check if the given basic map is obviously single-valued.
8901 * In particular, for each output dimension, check that there is
8902 * an equality that defines the output dimension in terms of
8903 * earlier dimensions.
8905 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8907 int i, j;
8908 unsigned total;
8909 unsigned n_out;
8910 unsigned o_out;
8912 if (!bmap)
8913 return -1;
8915 total = 1 + isl_basic_map_total_dim(bmap);
8916 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8917 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8919 for (i = 0; i < n_out; ++i) {
8920 for (j = 0; j < bmap->n_eq; ++j) {
8921 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8922 continue;
8923 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8924 total - (o_out + i + 1)) == -1)
8925 break;
8927 if (j >= bmap->n_eq)
8928 return 0;
8931 return 1;
8934 /* Check if the given map is obviously single-valued.
8936 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8938 if (!map)
8939 return -1;
8940 if (map->n == 0)
8941 return 1;
8942 if (map->n >= 2)
8943 return 0;
8945 return isl_basic_map_plain_is_single_valued(map->p[0]);
8948 /* Check if the given map is single-valued.
8949 * We simply compute
8951 * M \circ M^-1
8953 * and check if the result is a subset of the identity mapping.
8955 int isl_map_is_single_valued(__isl_keep isl_map *map)
8957 isl_space *dim;
8958 isl_map *test;
8959 isl_map *id;
8960 int sv;
8962 sv = isl_map_plain_is_single_valued(map);
8963 if (sv < 0 || sv)
8964 return sv;
8966 test = isl_map_reverse(isl_map_copy(map));
8967 test = isl_map_apply_range(test, isl_map_copy(map));
8969 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
8970 id = isl_map_identity(dim);
8972 sv = isl_map_is_subset(test, id);
8974 isl_map_free(test);
8975 isl_map_free(id);
8977 return sv;
8980 int isl_map_is_injective(__isl_keep isl_map *map)
8982 int in;
8984 map = isl_map_copy(map);
8985 map = isl_map_reverse(map);
8986 in = isl_map_is_single_valued(map);
8987 isl_map_free(map);
8989 return in;
8992 /* Check if the given map is obviously injective.
8994 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8996 int in;
8998 map = isl_map_copy(map);
8999 map = isl_map_reverse(map);
9000 in = isl_map_plain_is_single_valued(map);
9001 isl_map_free(map);
9003 return in;
9006 int isl_map_is_bijective(__isl_keep isl_map *map)
9008 int sv;
9010 sv = isl_map_is_single_valued(map);
9011 if (sv < 0 || !sv)
9012 return sv;
9014 return isl_map_is_injective(map);
9017 int isl_set_is_singleton(__isl_keep isl_set *set)
9019 return isl_map_is_single_valued((isl_map *)set);
9022 int isl_map_is_translation(__isl_keep isl_map *map)
9024 int ok;
9025 isl_set *delta;
9027 delta = isl_map_deltas(isl_map_copy(map));
9028 ok = isl_set_is_singleton(delta);
9029 isl_set_free(delta);
9031 return ok;
9034 static int unique(isl_int *p, unsigned pos, unsigned len)
9036 if (isl_seq_first_non_zero(p, pos) != -1)
9037 return 0;
9038 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
9039 return 0;
9040 return 1;
9043 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
9045 int i, j;
9046 unsigned nvar;
9047 unsigned ovar;
9049 if (!bset)
9050 return -1;
9052 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
9053 return 0;
9055 nvar = isl_basic_set_dim(bset, isl_dim_set);
9056 ovar = isl_space_offset(bset->dim, isl_dim_set);
9057 for (j = 0; j < nvar; ++j) {
9058 int lower = 0, upper = 0;
9059 for (i = 0; i < bset->n_eq; ++i) {
9060 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
9061 continue;
9062 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
9063 return 0;
9064 break;
9066 if (i < bset->n_eq)
9067 continue;
9068 for (i = 0; i < bset->n_ineq; ++i) {
9069 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
9070 continue;
9071 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
9072 return 0;
9073 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
9074 lower = 1;
9075 else
9076 upper = 1;
9078 if (!lower || !upper)
9079 return 0;
9082 return 1;
9085 int isl_set_is_box(__isl_keep isl_set *set)
9087 if (!set)
9088 return -1;
9089 if (set->n != 1)
9090 return 0;
9092 return isl_basic_set_is_box(set->p[0]);
9095 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
9097 if (!bset)
9098 return -1;
9100 return isl_space_is_wrapping(bset->dim);
9103 int isl_set_is_wrapping(__isl_keep isl_set *set)
9105 if (!set)
9106 return -1;
9108 return isl_space_is_wrapping(set->dim);
9111 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
9113 bmap = isl_basic_map_cow(bmap);
9114 if (!bmap)
9115 return NULL;
9117 bmap->dim = isl_space_wrap(bmap->dim);
9118 if (!bmap->dim)
9119 goto error;
9121 bmap = isl_basic_map_finalize(bmap);
9123 return (isl_basic_set *)bmap;
9124 error:
9125 isl_basic_map_free(bmap);
9126 return NULL;
9129 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
9131 int i;
9133 map = isl_map_cow(map);
9134 if (!map)
9135 return NULL;
9137 for (i = 0; i < map->n; ++i) {
9138 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
9139 if (!map->p[i])
9140 goto error;
9142 map->dim = isl_space_wrap(map->dim);
9143 if (!map->dim)
9144 goto error;
9146 return (isl_set *)map;
9147 error:
9148 isl_map_free(map);
9149 return NULL;
9152 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
9154 bset = isl_basic_set_cow(bset);
9155 if (!bset)
9156 return NULL;
9158 bset->dim = isl_space_unwrap(bset->dim);
9159 if (!bset->dim)
9160 goto error;
9162 bset = isl_basic_set_finalize(bset);
9164 return (isl_basic_map *)bset;
9165 error:
9166 isl_basic_set_free(bset);
9167 return NULL;
9170 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
9172 int i;
9174 if (!set)
9175 return NULL;
9177 if (!isl_set_is_wrapping(set))
9178 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
9179 goto error);
9181 set = isl_set_cow(set);
9182 if (!set)
9183 return NULL;
9185 for (i = 0; i < set->n; ++i) {
9186 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9187 if (!set->p[i])
9188 goto error;
9191 set->dim = isl_space_unwrap(set->dim);
9192 if (!set->dim)
9193 goto error;
9195 return (isl_map *)set;
9196 error:
9197 isl_set_free(set);
9198 return NULL;
9201 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9202 enum isl_dim_type type)
9204 if (!bmap)
9205 return NULL;
9207 if (!isl_space_is_named_or_nested(bmap->dim, type))
9208 return bmap;
9210 bmap = isl_basic_map_cow(bmap);
9211 if (!bmap)
9212 return NULL;
9214 bmap->dim = isl_space_reset(bmap->dim, type);
9215 if (!bmap->dim)
9216 goto error;
9218 bmap = isl_basic_map_finalize(bmap);
9220 return bmap;
9221 error:
9222 isl_basic_map_free(bmap);
9223 return NULL;
9226 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
9227 enum isl_dim_type type)
9229 int i;
9231 if (!map)
9232 return NULL;
9234 if (!isl_space_is_named_or_nested(map->dim, type))
9235 return map;
9237 map = isl_map_cow(map);
9238 if (!map)
9239 return NULL;
9241 for (i = 0; i < map->n; ++i) {
9242 map->p[i] = isl_basic_map_reset(map->p[i], type);
9243 if (!map->p[i])
9244 goto error;
9246 map->dim = isl_space_reset(map->dim, type);
9247 if (!map->dim)
9248 goto error;
9250 return map;
9251 error:
9252 isl_map_free(map);
9253 return NULL;
9256 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
9258 if (!bmap)
9259 return NULL;
9261 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
9262 return bmap;
9264 bmap = isl_basic_map_cow(bmap);
9265 if (!bmap)
9266 return NULL;
9268 bmap->dim = isl_space_flatten(bmap->dim);
9269 if (!bmap->dim)
9270 goto error;
9272 bmap = isl_basic_map_finalize(bmap);
9274 return bmap;
9275 error:
9276 isl_basic_map_free(bmap);
9277 return NULL;
9280 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9282 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9285 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9286 __isl_take isl_basic_map *bmap)
9288 if (!bmap)
9289 return NULL;
9291 if (!bmap->dim->nested[0])
9292 return bmap;
9294 bmap = isl_basic_map_cow(bmap);
9295 if (!bmap)
9296 return NULL;
9298 bmap->dim = isl_space_flatten_domain(bmap->dim);
9299 if (!bmap->dim)
9300 goto error;
9302 bmap = isl_basic_map_finalize(bmap);
9304 return bmap;
9305 error:
9306 isl_basic_map_free(bmap);
9307 return NULL;
9310 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9311 __isl_take isl_basic_map *bmap)
9313 if (!bmap)
9314 return NULL;
9316 if (!bmap->dim->nested[1])
9317 return bmap;
9319 bmap = isl_basic_map_cow(bmap);
9320 if (!bmap)
9321 return NULL;
9323 bmap->dim = isl_space_flatten_range(bmap->dim);
9324 if (!bmap->dim)
9325 goto error;
9327 bmap = isl_basic_map_finalize(bmap);
9329 return bmap;
9330 error:
9331 isl_basic_map_free(bmap);
9332 return NULL;
9335 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9337 int i;
9339 if (!map)
9340 return NULL;
9342 if (!map->dim->nested[0] && !map->dim->nested[1])
9343 return map;
9345 map = isl_map_cow(map);
9346 if (!map)
9347 return NULL;
9349 for (i = 0; i < map->n; ++i) {
9350 map->p[i] = isl_basic_map_flatten(map->p[i]);
9351 if (!map->p[i])
9352 goto error;
9354 map->dim = isl_space_flatten(map->dim);
9355 if (!map->dim)
9356 goto error;
9358 return map;
9359 error:
9360 isl_map_free(map);
9361 return NULL;
9364 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9366 return (isl_set *)isl_map_flatten((isl_map *)set);
9369 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9371 isl_space *dim, *flat_dim;
9372 isl_map *map;
9374 dim = isl_set_get_space(set);
9375 flat_dim = isl_space_flatten(isl_space_copy(dim));
9376 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9377 map = isl_map_intersect_domain(map, set);
9379 return map;
9382 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9384 int i;
9386 if (!map)
9387 return NULL;
9389 if (!map->dim->nested[0])
9390 return map;
9392 map = isl_map_cow(map);
9393 if (!map)
9394 return NULL;
9396 for (i = 0; i < map->n; ++i) {
9397 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9398 if (!map->p[i])
9399 goto error;
9401 map->dim = isl_space_flatten_domain(map->dim);
9402 if (!map->dim)
9403 goto error;
9405 return map;
9406 error:
9407 isl_map_free(map);
9408 return NULL;
9411 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9413 int i;
9415 if (!map)
9416 return NULL;
9418 if (!map->dim->nested[1])
9419 return map;
9421 map = isl_map_cow(map);
9422 if (!map)
9423 return NULL;
9425 for (i = 0; i < map->n; ++i) {
9426 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9427 if (!map->p[i])
9428 goto error;
9430 map->dim = isl_space_flatten_range(map->dim);
9431 if (!map->dim)
9432 goto error;
9434 return map;
9435 error:
9436 isl_map_free(map);
9437 return NULL;
9440 /* Reorder the dimensions of "bmap" according to the given dim_map
9441 * and set the dimension specification to "dim".
9443 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9444 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9446 isl_basic_map *res;
9447 unsigned flags;
9449 bmap = isl_basic_map_cow(bmap);
9450 if (!bmap || !dim || !dim_map)
9451 goto error;
9453 flags = bmap->flags;
9454 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
9455 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
9456 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
9457 res = isl_basic_map_alloc_space(dim,
9458 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9459 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9460 if (res)
9461 res->flags = flags;
9462 res = isl_basic_map_finalize(res);
9463 return res;
9464 error:
9465 free(dim_map);
9466 isl_basic_map_free(bmap);
9467 isl_space_free(dim);
9468 return NULL;
9471 /* Reorder the dimensions of "map" according to given reordering.
9473 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9474 __isl_take isl_reordering *r)
9476 int i;
9477 struct isl_dim_map *dim_map;
9479 map = isl_map_cow(map);
9480 dim_map = isl_dim_map_from_reordering(r);
9481 if (!map || !r || !dim_map)
9482 goto error;
9484 for (i = 0; i < map->n; ++i) {
9485 struct isl_dim_map *dim_map_i;
9487 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9489 map->p[i] = isl_basic_map_realign(map->p[i],
9490 isl_space_copy(r->dim), dim_map_i);
9492 if (!map->p[i])
9493 goto error;
9496 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9498 isl_reordering_free(r);
9499 free(dim_map);
9500 return map;
9501 error:
9502 free(dim_map);
9503 isl_map_free(map);
9504 isl_reordering_free(r);
9505 return NULL;
9508 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9509 __isl_take isl_reordering *r)
9511 return (isl_set *)isl_map_realign((isl_map *)set, r);
9514 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9515 __isl_take isl_space *model)
9517 isl_ctx *ctx;
9519 if (!map || !model)
9520 goto error;
9522 ctx = isl_space_get_ctx(model);
9523 if (!isl_space_has_named_params(model))
9524 isl_die(ctx, isl_error_invalid,
9525 "model has unnamed parameters", goto error);
9526 if (!isl_space_has_named_params(map->dim))
9527 isl_die(ctx, isl_error_invalid,
9528 "relation has unnamed parameters", goto error);
9529 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9530 isl_reordering *exp;
9532 model = isl_space_drop_dims(model, isl_dim_in,
9533 0, isl_space_dim(model, isl_dim_in));
9534 model = isl_space_drop_dims(model, isl_dim_out,
9535 0, isl_space_dim(model, isl_dim_out));
9536 exp = isl_parameter_alignment_reordering(map->dim, model);
9537 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9538 map = isl_map_realign(map, exp);
9541 isl_space_free(model);
9542 return map;
9543 error:
9544 isl_space_free(model);
9545 isl_map_free(map);
9546 return NULL;
9549 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9550 __isl_take isl_space *model)
9552 return isl_map_align_params(set, model);
9555 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9556 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9557 enum isl_dim_type c2, enum isl_dim_type c3,
9558 enum isl_dim_type c4, enum isl_dim_type c5)
9560 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9561 struct isl_mat *mat;
9562 int i, j, k;
9563 int pos;
9565 if (!bmap)
9566 return NULL;
9567 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9568 isl_basic_map_total_dim(bmap) + 1);
9569 if (!mat)
9570 return NULL;
9571 for (i = 0; i < bmap->n_eq; ++i)
9572 for (j = 0, pos = 0; j < 5; ++j) {
9573 int off = isl_basic_map_offset(bmap, c[j]);
9574 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9575 isl_int_set(mat->row[i][pos],
9576 bmap->eq[i][off + k]);
9577 ++pos;
9581 return mat;
9584 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9585 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9586 enum isl_dim_type c2, enum isl_dim_type c3,
9587 enum isl_dim_type c4, enum isl_dim_type c5)
9589 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9590 struct isl_mat *mat;
9591 int i, j, k;
9592 int pos;
9594 if (!bmap)
9595 return NULL;
9596 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9597 isl_basic_map_total_dim(bmap) + 1);
9598 if (!mat)
9599 return NULL;
9600 for (i = 0; i < bmap->n_ineq; ++i)
9601 for (j = 0, pos = 0; j < 5; ++j) {
9602 int off = isl_basic_map_offset(bmap, c[j]);
9603 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9604 isl_int_set(mat->row[i][pos],
9605 bmap->ineq[i][off + k]);
9606 ++pos;
9610 return mat;
9613 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9614 __isl_take isl_space *dim,
9615 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9616 enum isl_dim_type c2, enum isl_dim_type c3,
9617 enum isl_dim_type c4, enum isl_dim_type c5)
9619 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9620 isl_basic_map *bmap;
9621 unsigned total;
9622 unsigned extra;
9623 int i, j, k, l;
9624 int pos;
9626 if (!dim || !eq || !ineq)
9627 goto error;
9629 if (eq->n_col != ineq->n_col)
9630 isl_die(dim->ctx, isl_error_invalid,
9631 "equalities and inequalities matrices should have "
9632 "same number of columns", goto error);
9634 total = 1 + isl_space_dim(dim, isl_dim_all);
9636 if (eq->n_col < total)
9637 isl_die(dim->ctx, isl_error_invalid,
9638 "number of columns too small", goto error);
9640 extra = eq->n_col - total;
9642 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9643 eq->n_row, ineq->n_row);
9644 if (!bmap)
9645 goto error;
9646 for (i = 0; i < extra; ++i) {
9647 k = isl_basic_map_alloc_div(bmap);
9648 if (k < 0)
9649 goto error;
9650 isl_int_set_si(bmap->div[k][0], 0);
9652 for (i = 0; i < eq->n_row; ++i) {
9653 l = isl_basic_map_alloc_equality(bmap);
9654 if (l < 0)
9655 goto error;
9656 for (j = 0, pos = 0; j < 5; ++j) {
9657 int off = isl_basic_map_offset(bmap, c[j]);
9658 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9659 isl_int_set(bmap->eq[l][off + k],
9660 eq->row[i][pos]);
9661 ++pos;
9665 for (i = 0; i < ineq->n_row; ++i) {
9666 l = isl_basic_map_alloc_inequality(bmap);
9667 if (l < 0)
9668 goto error;
9669 for (j = 0, pos = 0; j < 5; ++j) {
9670 int off = isl_basic_map_offset(bmap, c[j]);
9671 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9672 isl_int_set(bmap->ineq[l][off + k],
9673 ineq->row[i][pos]);
9674 ++pos;
9679 isl_space_free(dim);
9680 isl_mat_free(eq);
9681 isl_mat_free(ineq);
9683 return bmap;
9684 error:
9685 isl_space_free(dim);
9686 isl_mat_free(eq);
9687 isl_mat_free(ineq);
9688 return NULL;
9691 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9692 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9693 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9695 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9696 c1, c2, c3, c4, isl_dim_in);
9699 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9700 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9701 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9703 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9704 c1, c2, c3, c4, isl_dim_in);
9707 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9708 __isl_take isl_space *dim,
9709 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9710 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9712 return (isl_basic_set*)
9713 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9714 c1, c2, c3, c4, isl_dim_in);
9717 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9719 if (!bmap)
9720 return -1;
9722 return isl_space_can_zip(bmap->dim);
9725 int isl_map_can_zip(__isl_keep isl_map *map)
9727 if (!map)
9728 return -1;
9730 return isl_space_can_zip(map->dim);
9733 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9734 * (A -> C) -> (B -> D).
9736 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9738 unsigned pos;
9739 unsigned n1;
9740 unsigned n2;
9742 if (!bmap)
9743 return NULL;
9745 if (!isl_basic_map_can_zip(bmap))
9746 isl_die(bmap->ctx, isl_error_invalid,
9747 "basic map cannot be zipped", goto error);
9748 pos = isl_basic_map_offset(bmap, isl_dim_in) +
9749 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
9750 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
9751 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
9752 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9753 if (!bmap)
9754 return NULL;
9755 bmap->dim = isl_space_zip(bmap->dim);
9756 if (!bmap->dim)
9757 goto error;
9758 return bmap;
9759 error:
9760 isl_basic_map_free(bmap);
9761 return NULL;
9764 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9765 * (A -> C) -> (B -> D).
9767 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9769 int i;
9771 if (!map)
9772 return NULL;
9774 if (!isl_map_can_zip(map))
9775 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9776 goto error);
9778 map = isl_map_cow(map);
9779 if (!map)
9780 return NULL;
9782 for (i = 0; i < map->n; ++i) {
9783 map->p[i] = isl_basic_map_zip(map->p[i]);
9784 if (!map->p[i])
9785 goto error;
9788 map->dim = isl_space_zip(map->dim);
9789 if (!map->dim)
9790 goto error;
9792 return map;
9793 error:
9794 isl_map_free(map);
9795 return NULL;
9798 /* Construct a basic map mapping the domain of the affine expression
9799 * to a one-dimensional range prescribed by the affine expression.
9801 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9803 int k;
9804 int pos;
9805 isl_local_space *ls;
9806 isl_basic_map *bmap;
9808 if (!aff)
9809 return NULL;
9811 ls = isl_aff_get_local_space(aff);
9812 bmap = isl_basic_map_from_local_space(ls);
9813 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9814 k = isl_basic_map_alloc_equality(bmap);
9815 if (k < 0)
9816 goto error;
9818 pos = isl_basic_map_offset(bmap, isl_dim_out);
9819 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9820 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9821 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9822 aff->v->size - (pos + 1));
9824 isl_aff_free(aff);
9825 bmap = isl_basic_map_finalize(bmap);
9826 return bmap;
9827 error:
9828 isl_aff_free(aff);
9829 isl_basic_map_free(bmap);
9830 return NULL;
9833 /* Construct a basic map mapping the domain the multi-affine expression
9834 * to its range, with each dimension in the range equated to the
9835 * corresponding affine expression.
9837 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
9838 __isl_take isl_multi_aff *maff)
9840 int i;
9841 isl_space *space;
9842 isl_basic_map *bmap;
9844 if (!maff)
9845 return NULL;
9847 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
9848 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
9849 "invalid space", return isl_multi_aff_free(maff));
9851 space = isl_space_domain(isl_multi_aff_get_space(maff));
9852 bmap = isl_basic_map_universe(isl_space_from_domain(space));
9854 for (i = 0; i < maff->n; ++i) {
9855 isl_aff *aff;
9856 isl_basic_map *bmap_i;
9858 aff = isl_aff_copy(maff->p[i]);
9859 bmap_i = isl_basic_map_from_aff(aff);
9861 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9864 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
9866 isl_multi_aff_free(maff);
9867 return bmap;
9870 /* Construct a basic map mapping a domain in the given space to
9871 * to an n-dimensional range, with n the number of elements in the list,
9872 * where each coordinate in the range is prescribed by the
9873 * corresponding affine expression.
9874 * The domains of all affine expressions in the list are assumed to match
9875 * domain_dim.
9877 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
9878 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
9880 int i;
9881 isl_space *dim;
9882 isl_basic_map *bmap;
9884 if (!list)
9885 return NULL;
9887 dim = isl_space_from_domain(domain_dim);
9888 bmap = isl_basic_map_universe(dim);
9890 for (i = 0; i < list->n; ++i) {
9891 isl_aff *aff;
9892 isl_basic_map *bmap_i;
9894 aff = isl_aff_copy(list->p[i]);
9895 bmap_i = isl_basic_map_from_aff(aff);
9897 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9900 isl_aff_list_free(list);
9901 return bmap;
9904 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
9905 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9907 return isl_map_equate(set, type1, pos1, type2, pos2);
9910 /* Add a constraint imposing that the given two dimensions are equal.
9912 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
9913 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9915 isl_basic_map *bmap = NULL;
9916 int i;
9918 if (!map)
9919 return NULL;
9921 if (pos1 >= isl_map_dim(map, type1))
9922 isl_die(map->ctx, isl_error_invalid,
9923 "index out of bounds", goto error);
9924 if (pos2 >= isl_map_dim(map, type2))
9925 isl_die(map->ctx, isl_error_invalid,
9926 "index out of bounds", goto error);
9928 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9929 i = isl_basic_map_alloc_equality(bmap);
9930 if (i < 0)
9931 goto error;
9932 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9933 pos1 += isl_basic_map_offset(bmap, type1);
9934 pos2 += isl_basic_map_offset(bmap, type2);
9935 isl_int_set_si(bmap->eq[i][pos1], -1);
9936 isl_int_set_si(bmap->eq[i][pos2], 1);
9937 bmap = isl_basic_map_finalize(bmap);
9939 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9941 return map;
9942 error:
9943 isl_basic_map_free(bmap);
9944 isl_map_free(map);
9945 return NULL;
9948 /* Add a constraint imposing that the given two dimensions have opposite values.
9950 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
9951 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9953 isl_basic_map *bmap = NULL;
9954 int i;
9956 if (!map)
9957 return NULL;
9959 if (pos1 >= isl_map_dim(map, type1))
9960 isl_die(map->ctx, isl_error_invalid,
9961 "index out of bounds", goto error);
9962 if (pos2 >= isl_map_dim(map, type2))
9963 isl_die(map->ctx, isl_error_invalid,
9964 "index out of bounds", goto error);
9966 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9967 i = isl_basic_map_alloc_equality(bmap);
9968 if (i < 0)
9969 goto error;
9970 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9971 pos1 += isl_basic_map_offset(bmap, type1);
9972 pos2 += isl_basic_map_offset(bmap, type2);
9973 isl_int_set_si(bmap->eq[i][pos1], 1);
9974 isl_int_set_si(bmap->eq[i][pos2], 1);
9975 bmap = isl_basic_map_finalize(bmap);
9977 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9979 return map;
9980 error:
9981 isl_basic_map_free(bmap);
9982 isl_map_free(map);
9983 return NULL;
9986 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
9987 int pos)
9989 isl_aff *div;
9990 isl_local_space *ls;
9992 if (!bmap)
9993 return NULL;
9995 if (!isl_basic_map_divs_known(bmap))
9996 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
9997 "some divs are unknown", return NULL);
9999 ls = isl_basic_map_get_local_space(bmap);
10000 div = isl_local_space_get_div(ls, pos);
10001 isl_local_space_free(ls);
10003 return div;
10006 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
10007 int pos)
10009 return isl_basic_map_get_div(bset, pos);
10012 /* Plug in "subs" for dimension "type", "pos" of "bset".
10014 * Let i be the dimension to replace and let "subs" be of the form
10016 * f/d
10018 * Any integer division with a non-zero coefficient for i,
10020 * floor((a i + g)/m)
10022 * is replaced by
10024 * floor((a f + d g)/(m d))
10026 * Constraints of the form
10028 * a i + g
10030 * are replaced by
10032 * a f + d g
10034 __isl_give isl_basic_set *isl_basic_set_substitute(
10035 __isl_take isl_basic_set *bset,
10036 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10038 int i;
10039 isl_int v;
10040 isl_ctx *ctx;
10042 if (bset && isl_basic_set_plain_is_empty(bset))
10043 return bset;
10045 bset = isl_basic_set_cow(bset);
10046 if (!bset || !subs)
10047 goto error;
10049 ctx = isl_basic_set_get_ctx(bset);
10050 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
10051 isl_die(ctx, isl_error_invalid,
10052 "spaces don't match", goto error);
10053 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
10054 isl_die(ctx, isl_error_unsupported,
10055 "cannot handle divs yet", goto error);
10057 pos += isl_basic_set_offset(bset, type);
10059 isl_int_init(v);
10061 for (i = 0; i < bset->n_eq; ++i) {
10062 if (isl_int_is_zero(bset->eq[i][pos]))
10063 continue;
10064 isl_int_set(v, bset->eq[i][pos]);
10065 isl_int_set_si(bset->eq[i][pos], 0);
10066 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
10067 v, subs->v->el + 1, subs->v->size - 1);
10070 for (i = 0; i < bset->n_ineq; ++i) {
10071 if (isl_int_is_zero(bset->ineq[i][pos]))
10072 continue;
10073 isl_int_set(v, bset->ineq[i][pos]);
10074 isl_int_set_si(bset->ineq[i][pos], 0);
10075 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
10076 v, subs->v->el + 1, subs->v->size - 1);
10079 for (i = 0; i < bset->n_div; ++i) {
10080 if (isl_int_is_zero(bset->div[i][1 + pos]))
10081 continue;
10082 isl_int_set(v, bset->div[i][1 + pos]);
10083 isl_int_set_si(bset->div[i][1 + pos], 0);
10084 isl_seq_combine(bset->div[i] + 1,
10085 subs->v->el[0], bset->div[i] + 1,
10086 v, subs->v->el + 1, subs->v->size - 1);
10087 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
10090 isl_int_clear(v);
10092 bset = isl_basic_set_simplify(bset);
10093 return isl_basic_set_finalize(bset);
10094 error:
10095 isl_basic_set_free(bset);
10096 return NULL;
10099 /* Plug in "subs" for dimension "type", "pos" of "set".
10101 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
10102 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10104 int i;
10106 if (set && isl_set_plain_is_empty(set))
10107 return set;
10109 set = isl_set_cow(set);
10110 if (!set || !subs)
10111 goto error;
10113 for (i = set->n - 1; i >= 0; --i) {
10114 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
10115 if (remove_if_empty(set, i) < 0)
10116 goto error;
10119 return set;
10120 error:
10121 isl_set_free(set);
10122 return NULL;