add isl_qpolynomial_gist_params
[isl.git] / isl_map.c
blob4446106a7e1709de47c419a8078c3b0821450bcf
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>
34 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
36 switch (type) {
37 case isl_dim_param: return dim->nparam;
38 case isl_dim_in: return dim->n_in;
39 case isl_dim_out: return dim->n_out;
40 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
41 default: return 0;
45 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
47 switch (type) {
48 case isl_dim_param: return 1;
49 case isl_dim_in: return 1 + dim->nparam;
50 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
51 default: return 0;
55 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
56 enum isl_dim_type type)
58 if (!bmap)
59 return 0;
60 switch (type) {
61 case isl_dim_cst: return 1;
62 case isl_dim_param:
63 case isl_dim_in:
64 case isl_dim_out: return isl_space_dim(bmap->dim, type);
65 case isl_dim_div: return bmap->n_div;
66 case isl_dim_all: return isl_basic_map_total_dim(bmap);
67 default: return 0;
71 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
73 return map ? n(map->dim, type) : 0;
76 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
78 return set ? n(set->dim, type) : 0;
81 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
82 enum isl_dim_type type)
84 isl_space *dim = bmap->dim;
85 switch (type) {
86 case isl_dim_cst: return 0;
87 case isl_dim_param: return 1;
88 case isl_dim_in: return 1 + dim->nparam;
89 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
90 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
91 default: return 0;
95 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
96 enum isl_dim_type type)
98 return isl_basic_map_offset(bset, type);
101 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
103 return pos(map->dim, type);
106 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
107 enum isl_dim_type type)
109 return isl_basic_map_dim(bset, type);
112 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
114 return isl_basic_set_dim(bset, isl_dim_set);
117 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
119 return isl_basic_set_dim(bset, isl_dim_param);
122 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
124 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
127 unsigned isl_set_n_dim(__isl_keep isl_set *set)
129 return isl_set_dim(set, isl_dim_set);
132 unsigned isl_set_n_param(__isl_keep isl_set *set)
134 return isl_set_dim(set, isl_dim_param);
137 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
139 return bmap ? bmap->dim->n_in : 0;
142 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
144 return bmap ? bmap->dim->n_out : 0;
147 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
149 return bmap ? bmap->dim->nparam : 0;
152 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
154 return bmap ? bmap->n_div : 0;
157 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
159 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
162 unsigned isl_map_n_in(const struct isl_map *map)
164 return map ? map->dim->n_in : 0;
167 unsigned isl_map_n_out(const struct isl_map *map)
169 return map ? map->dim->n_out : 0;
172 unsigned isl_map_n_param(const struct isl_map *map)
174 return map ? map->dim->nparam : 0;
177 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
179 int m;
180 if (!map || !set)
181 return -1;
182 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
183 if (m < 0 || !m)
184 return m;
185 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
188 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
189 struct isl_basic_set *bset)
191 int m;
192 if (!bmap || !bset)
193 return -1;
194 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
195 if (m < 0 || !m)
196 return m;
197 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
200 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
202 int m;
203 if (!map || !set)
204 return -1;
205 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
206 if (m < 0 || !m)
207 return m;
208 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
211 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
212 struct isl_basic_set *bset)
214 int m;
215 if (!bmap || !bset)
216 return -1;
217 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
218 if (m < 0 || !m)
219 return m;
220 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
223 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
225 return bmap ? bmap->ctx : NULL;
228 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
230 return bset ? bset->ctx : NULL;
233 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
235 return map ? map->ctx : NULL;
238 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
240 return set ? set->ctx : NULL;
243 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
245 if (!bmap)
246 return NULL;
247 return isl_space_copy(bmap->dim);
250 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
252 if (!bset)
253 return NULL;
254 return isl_space_copy(bset->dim);
257 __isl_give isl_local_space *isl_basic_map_get_local_space(
258 __isl_keep isl_basic_map *bmap)
260 int i;
261 isl_local_space *ls;
262 unsigned total;
264 if (!bmap)
265 return NULL;
267 total = isl_basic_map_total_dim(bmap);
268 ls = isl_local_space_alloc(isl_space_copy(bmap->dim), bmap->n_div);
269 if (!ls)
270 return NULL;
272 for (i = 0; i < bmap->n_div; ++i)
273 isl_seq_cpy(ls->div->row[i], bmap->div[i], 2 + total);
275 return ls;
278 __isl_give isl_local_space *isl_basic_set_get_local_space(
279 __isl_keep isl_basic_set *bset)
281 return isl_basic_map_get_local_space(bset);
284 __isl_give isl_basic_map *isl_basic_map_from_local_space(
285 __isl_take isl_local_space *ls)
287 int i;
288 int n_div;
289 isl_basic_map *bmap;
291 if (!ls)
292 return NULL;
294 n_div = isl_local_space_dim(ls, isl_dim_div);
295 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
296 n_div, 0, 2 * n_div);
298 for (i = 0; i < n_div; ++i)
299 if (isl_basic_map_alloc_div(bmap) < 0)
300 goto error;
302 for (i = 0; i < n_div; ++i) {
303 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
304 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
305 goto error;
308 isl_local_space_free(ls);
309 return bmap;
310 error:
311 isl_local_space_free(ls);
312 isl_basic_map_free(bmap);
313 return NULL;
316 __isl_give isl_basic_set *isl_basic_set_from_local_space(
317 __isl_take isl_local_space *ls)
319 return isl_basic_map_from_local_space(ls);
322 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
324 if (!map)
325 return NULL;
326 return isl_space_copy(map->dim);
329 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
331 if (!set)
332 return NULL;
333 return isl_space_copy(set->dim);
336 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
337 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
339 bmap = isl_basic_map_cow(bmap);
340 if (!bmap)
341 return NULL;
342 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
343 if (!bmap->dim)
344 goto error;
345 bmap = isl_basic_map_finalize(bmap);
346 return bmap;
347 error:
348 isl_basic_map_free(bmap);
349 return NULL;
352 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
353 __isl_take isl_basic_set *bset, const char *s)
355 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
358 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
359 enum isl_dim_type type)
361 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
364 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
365 enum isl_dim_type type, const char *s)
367 int i;
369 map = isl_map_cow(map);
370 if (!map)
371 return NULL;
373 map->dim = isl_space_set_tuple_name(map->dim, type, s);
374 if (!map->dim)
375 goto error;
377 for (i = 0; i < map->n; ++i) {
378 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
379 if (!map->p[i])
380 goto error;
383 return map;
384 error:
385 isl_map_free(map);
386 return NULL;
389 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
390 enum isl_dim_type type)
392 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
395 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
396 const char *s)
398 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
401 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
402 enum isl_dim_type type, __isl_take isl_id *id)
404 map = isl_map_cow(map);
405 if (!map)
406 return isl_id_free(id);
408 map->dim = isl_space_set_tuple_id(map->dim, type, id);
410 return isl_map_reset_space(map, isl_space_copy(map->dim));
413 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
414 __isl_take isl_id *id)
416 return isl_map_set_tuple_id(set, isl_dim_set, id);
419 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
420 enum isl_dim_type type)
422 map = isl_map_cow(map);
423 if (!map)
424 return NULL;
426 map->dim = isl_space_reset_tuple_id(map->dim, type);
428 return isl_map_reset_space(map, isl_space_copy(map->dim));
431 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
433 return isl_map_reset_tuple_id(set, isl_dim_set);
436 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
438 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
441 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
442 enum isl_dim_type type)
444 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
447 int isl_set_has_tuple_id(__isl_keep isl_set *set)
449 return isl_map_has_tuple_id(set, isl_dim_set);
452 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
454 return isl_map_get_tuple_id(set, isl_dim_set);
457 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
459 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
462 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
464 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
467 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
468 enum isl_dim_type type, unsigned pos)
470 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
473 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
474 enum isl_dim_type type, unsigned pos)
476 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
479 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
480 enum isl_dim_type type, unsigned pos)
482 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
485 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
486 enum isl_dim_type type, unsigned pos)
488 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
491 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
492 __isl_take isl_basic_map *bmap,
493 enum isl_dim_type type, unsigned pos, const char *s)
495 bmap = isl_basic_map_cow(bmap);
496 if (!bmap)
497 return NULL;
498 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
499 if (!bmap->dim)
500 goto error;
501 return bmap;
502 error:
503 isl_basic_map_free(bmap);
504 return NULL;
507 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
508 enum isl_dim_type type, unsigned pos, const char *s)
510 int i;
512 map = isl_map_cow(map);
513 if (!map)
514 return NULL;
516 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
517 if (!map->dim)
518 goto error;
520 for (i = 0; i < map->n; ++i) {
521 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
522 if (!map->p[i])
523 goto error;
526 return map;
527 error:
528 isl_map_free(map);
529 return NULL;
532 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
533 __isl_take isl_basic_set *bset,
534 enum isl_dim_type type, unsigned pos, const char *s)
536 return (isl_basic_set *)isl_basic_map_set_dim_name(
537 (isl_basic_map *)bset, type, pos, s);
540 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
541 enum isl_dim_type type, unsigned pos, const char *s)
543 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
546 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
547 enum isl_dim_type type, unsigned pos)
549 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
552 int isl_map_has_dim_id(__isl_keep isl_map *map,
553 enum isl_dim_type type, unsigned pos)
555 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
558 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
559 enum isl_dim_type type, unsigned pos)
561 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
564 int isl_set_has_dim_id(__isl_keep isl_set *set,
565 enum isl_dim_type type, unsigned pos)
567 return isl_map_has_dim_id(set, type, pos);
570 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
571 enum isl_dim_type type, unsigned pos)
573 return isl_map_get_dim_id(set, type, pos);
576 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
577 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
579 map = isl_map_cow(map);
580 if (!map)
581 return isl_id_free(id);
583 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
585 return isl_map_reset_space(map, isl_space_copy(map->dim));
588 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
589 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
591 return isl_map_set_dim_id(set, type, pos, id);
594 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
595 __isl_keep isl_id *id)
597 if (!map)
598 return -1;
599 return isl_space_find_dim_by_id(map->dim, type, id);
602 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
603 __isl_keep isl_id *id)
605 return isl_map_find_dim_by_id(set, type, id);
608 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
609 const char *name)
611 if (!map)
612 return -1;
613 return isl_space_find_dim_by_name(map->dim, type, name);
616 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
618 if (!bmap)
619 return -1;
620 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
623 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
625 return isl_basic_map_is_rational(bset);
628 /* Is this basic set a parameter domain?
630 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
632 if (!bset)
633 return -1;
634 return isl_space_is_params(bset->dim);
637 /* Is this set a parameter domain?
639 int isl_set_is_params(__isl_keep isl_set *set)
641 if (!set)
642 return -1;
643 return isl_space_is_params(set->dim);
646 /* Is this map actually a parameter domain?
647 * Users should never call this function. Outside of isl,
648 * a map can never be a parameter domain.
650 int isl_map_is_params(__isl_keep isl_map *map)
652 if (!map)
653 return -1;
654 return isl_space_is_params(map->dim);
657 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
658 struct isl_basic_map *bmap, unsigned extra,
659 unsigned n_eq, unsigned n_ineq)
661 int i;
662 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
664 bmap->ctx = ctx;
665 isl_ctx_ref(ctx);
667 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
668 if (isl_blk_is_error(bmap->block))
669 goto error;
671 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
672 if (!bmap->ineq)
673 goto error;
675 if (extra == 0) {
676 bmap->block2 = isl_blk_empty();
677 bmap->div = NULL;
678 } else {
679 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
680 if (isl_blk_is_error(bmap->block2))
681 goto error;
683 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
684 if (!bmap->div)
685 goto error;
688 for (i = 0; i < n_ineq + n_eq; ++i)
689 bmap->ineq[i] = bmap->block.data + i * row_size;
691 for (i = 0; i < extra; ++i)
692 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
694 bmap->ref = 1;
695 bmap->flags = 0;
696 bmap->c_size = n_eq + n_ineq;
697 bmap->eq = bmap->ineq + n_ineq;
698 bmap->extra = extra;
699 bmap->n_eq = 0;
700 bmap->n_ineq = 0;
701 bmap->n_div = 0;
702 bmap->sample = NULL;
704 return bmap;
705 error:
706 isl_basic_map_free(bmap);
707 return NULL;
710 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
711 unsigned nparam, unsigned dim, unsigned extra,
712 unsigned n_eq, unsigned n_ineq)
714 struct isl_basic_map *bmap;
715 isl_space *space;
717 space = isl_space_set_alloc(ctx, nparam, dim);
718 if (!space)
719 return NULL;
721 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
722 return (struct isl_basic_set *)bmap;
725 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
726 unsigned extra, unsigned n_eq, unsigned n_ineq)
728 struct isl_basic_map *bmap;
729 if (!dim)
730 return NULL;
731 isl_assert(dim->ctx, dim->n_in == 0, goto error);
732 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
733 return (struct isl_basic_set *)bmap;
734 error:
735 isl_space_free(dim);
736 return NULL;
739 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
740 unsigned extra, unsigned n_eq, unsigned n_ineq)
742 struct isl_basic_map *bmap;
744 if (!dim)
745 return NULL;
746 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
747 if (!bmap)
748 goto error;
749 bmap->dim = dim;
751 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
752 error:
753 isl_space_free(dim);
754 return NULL;
757 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
758 unsigned nparam, unsigned in, unsigned out, unsigned extra,
759 unsigned n_eq, unsigned n_ineq)
761 struct isl_basic_map *bmap;
762 isl_space *dim;
764 dim = isl_space_alloc(ctx, nparam, in, out);
765 if (!dim)
766 return NULL;
768 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
769 return bmap;
772 static void dup_constraints(
773 struct isl_basic_map *dst, struct isl_basic_map *src)
775 int i;
776 unsigned total = isl_basic_map_total_dim(src);
778 for (i = 0; i < src->n_eq; ++i) {
779 int j = isl_basic_map_alloc_equality(dst);
780 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
783 for (i = 0; i < src->n_ineq; ++i) {
784 int j = isl_basic_map_alloc_inequality(dst);
785 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
788 for (i = 0; i < src->n_div; ++i) {
789 int j = isl_basic_map_alloc_div(dst);
790 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
792 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
795 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
797 struct isl_basic_map *dup;
799 if (!bmap)
800 return NULL;
801 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
802 bmap->n_div, bmap->n_eq, bmap->n_ineq);
803 if (!dup)
804 return NULL;
805 dup_constraints(dup, bmap);
806 dup->flags = bmap->flags;
807 dup->sample = isl_vec_copy(bmap->sample);
808 return dup;
811 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
813 struct isl_basic_map *dup;
815 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
816 return (struct isl_basic_set *)dup;
819 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
821 if (!bset)
822 return NULL;
824 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
825 bset->ref++;
826 return bset;
828 return isl_basic_set_dup(bset);
831 struct isl_set *isl_set_copy(struct isl_set *set)
833 if (!set)
834 return NULL;
836 set->ref++;
837 return set;
840 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
842 if (!bmap)
843 return NULL;
845 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
846 bmap->ref++;
847 return bmap;
849 bmap = isl_basic_map_dup(bmap);
850 if (bmap)
851 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
852 return bmap;
855 struct isl_map *isl_map_copy(struct isl_map *map)
857 if (!map)
858 return NULL;
860 map->ref++;
861 return map;
864 void isl_basic_map_free(struct isl_basic_map *bmap)
866 if (!bmap)
867 return;
869 if (--bmap->ref > 0)
870 return;
872 isl_ctx_deref(bmap->ctx);
873 free(bmap->div);
874 isl_blk_free(bmap->ctx, bmap->block2);
875 free(bmap->ineq);
876 isl_blk_free(bmap->ctx, bmap->block);
877 isl_vec_free(bmap->sample);
878 isl_space_free(bmap->dim);
879 free(bmap);
882 void isl_basic_set_free(struct isl_basic_set *bset)
884 isl_basic_map_free((struct isl_basic_map *)bset);
887 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
889 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
892 __isl_give isl_map *isl_map_align_params_map_map_and(
893 __isl_take isl_map *map1, __isl_take isl_map *map2,
894 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
895 __isl_take isl_map *map2))
897 if (!map1 || !map2)
898 goto error;
899 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
900 return fn(map1, map2);
901 if (!isl_space_has_named_params(map1->dim) ||
902 !isl_space_has_named_params(map2->dim))
903 isl_die(map1->ctx, isl_error_invalid,
904 "unaligned unnamed parameters", goto error);
905 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
906 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
907 return fn(map1, map2);
908 error:
909 isl_map_free(map1);
910 isl_map_free(map2);
911 return NULL;
914 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
915 __isl_keep isl_map *map2,
916 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
918 int r;
920 if (!map1 || !map2)
921 return -1;
922 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
923 return fn(map1, map2);
924 if (!isl_space_has_named_params(map1->dim) ||
925 !isl_space_has_named_params(map2->dim))
926 isl_die(map1->ctx, isl_error_invalid,
927 "unaligned unnamed parameters", return -1);
928 map1 = isl_map_copy(map1);
929 map2 = isl_map_copy(map2);
930 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
931 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
932 r = fn(map1, map2);
933 isl_map_free(map1);
934 isl_map_free(map2);
935 return r;
938 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
940 struct isl_ctx *ctx;
941 if (!bmap)
942 return -1;
943 ctx = bmap->ctx;
944 isl_assert(ctx, room_for_con(bmap, 1), return -1);
945 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
946 return -1);
947 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
948 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
949 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
950 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
951 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
952 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
953 isl_int *t;
954 int j = isl_basic_map_alloc_inequality(bmap);
955 if (j < 0)
956 return -1;
957 t = bmap->ineq[j];
958 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
959 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
960 bmap->eq[-1] = t;
961 bmap->n_eq++;
962 bmap->n_ineq--;
963 bmap->eq--;
964 return 0;
966 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
967 bmap->extra - bmap->n_div);
968 return bmap->n_eq++;
971 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
973 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
976 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
978 if (!bmap)
979 return -1;
980 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
981 bmap->n_eq -= n;
982 return 0;
985 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
987 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
990 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
992 isl_int *t;
993 if (!bmap)
994 return -1;
995 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
997 if (pos != bmap->n_eq - 1) {
998 t = bmap->eq[pos];
999 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1000 bmap->eq[bmap->n_eq - 1] = t;
1002 bmap->n_eq--;
1003 return 0;
1006 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1008 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1011 void isl_basic_map_inequality_to_equality(
1012 struct isl_basic_map *bmap, unsigned pos)
1014 isl_int *t;
1016 t = bmap->ineq[pos];
1017 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1018 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1019 bmap->eq[-1] = t;
1020 bmap->n_eq++;
1021 bmap->n_ineq--;
1022 bmap->eq--;
1023 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1024 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1025 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1026 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1029 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1031 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1034 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1036 struct isl_ctx *ctx;
1037 if (!bmap)
1038 return -1;
1039 ctx = bmap->ctx;
1040 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1041 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1042 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1043 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1044 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1045 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1046 1 + isl_basic_map_total_dim(bmap),
1047 bmap->extra - bmap->n_div);
1048 return bmap->n_ineq++;
1051 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1053 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1056 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1058 if (!bmap)
1059 return -1;
1060 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1061 bmap->n_ineq -= n;
1062 return 0;
1065 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1067 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1070 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1072 isl_int *t;
1073 if (!bmap)
1074 return -1;
1075 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1077 if (pos != bmap->n_ineq - 1) {
1078 t = bmap->ineq[pos];
1079 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1080 bmap->ineq[bmap->n_ineq - 1] = t;
1081 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1083 bmap->n_ineq--;
1084 return 0;
1087 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1089 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1092 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1093 isl_int *eq)
1095 int k;
1097 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1098 if (!bmap)
1099 return NULL;
1100 k = isl_basic_map_alloc_equality(bmap);
1101 if (k < 0)
1102 goto error;
1103 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1104 return bmap;
1105 error:
1106 isl_basic_map_free(bmap);
1107 return NULL;
1110 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1111 isl_int *eq)
1113 return (isl_basic_set *)
1114 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1117 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1118 isl_int *ineq)
1120 int k;
1122 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1123 if (!bmap)
1124 return NULL;
1125 k = isl_basic_map_alloc_inequality(bmap);
1126 if (k < 0)
1127 goto error;
1128 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1129 return bmap;
1130 error:
1131 isl_basic_map_free(bmap);
1132 return NULL;
1135 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1136 isl_int *ineq)
1138 return (isl_basic_set *)
1139 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1142 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1144 if (!bmap)
1145 return -1;
1146 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1147 isl_seq_clr(bmap->div[bmap->n_div] +
1148 1 + 1 + isl_basic_map_total_dim(bmap),
1149 bmap->extra - bmap->n_div);
1150 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1151 return bmap->n_div++;
1154 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1156 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1159 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1161 if (!bmap)
1162 return -1;
1163 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1164 bmap->n_div -= n;
1165 return 0;
1168 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1170 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1173 /* Copy constraint from src to dst, putting the vars of src at offset
1174 * dim_off in dst and the divs of src at offset div_off in dst.
1175 * If both sets are actually map, then dim_off applies to the input
1176 * variables.
1178 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1179 struct isl_basic_map *src_map, isl_int *src,
1180 unsigned in_off, unsigned out_off, unsigned div_off)
1182 unsigned src_nparam = isl_basic_map_n_param(src_map);
1183 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1184 unsigned src_in = isl_basic_map_n_in(src_map);
1185 unsigned dst_in = isl_basic_map_n_in(dst_map);
1186 unsigned src_out = isl_basic_map_n_out(src_map);
1187 unsigned dst_out = isl_basic_map_n_out(dst_map);
1188 isl_int_set(dst[0], src[0]);
1189 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1190 if (dst_nparam > src_nparam)
1191 isl_seq_clr(dst+1+src_nparam,
1192 dst_nparam - src_nparam);
1193 isl_seq_clr(dst+1+dst_nparam, in_off);
1194 isl_seq_cpy(dst+1+dst_nparam+in_off,
1195 src+1+src_nparam,
1196 isl_min(dst_in-in_off, src_in));
1197 if (dst_in-in_off > src_in)
1198 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1199 dst_in - in_off - src_in);
1200 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1201 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1202 src+1+src_nparam+src_in,
1203 isl_min(dst_out-out_off, src_out));
1204 if (dst_out-out_off > src_out)
1205 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1206 dst_out - out_off - src_out);
1207 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1208 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1209 src+1+src_nparam+src_in+src_out,
1210 isl_min(dst_map->extra-div_off, src_map->n_div));
1211 if (dst_map->n_div-div_off > src_map->n_div)
1212 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1213 div_off+src_map->n_div,
1214 dst_map->n_div - div_off - src_map->n_div);
1217 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1218 struct isl_basic_map *src_map, isl_int *src,
1219 unsigned in_off, unsigned out_off, unsigned div_off)
1221 isl_int_set(dst[0], src[0]);
1222 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1225 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1226 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1228 int i;
1229 unsigned div_off;
1231 if (!bmap1 || !bmap2)
1232 goto error;
1234 div_off = bmap1->n_div;
1236 for (i = 0; i < bmap2->n_eq; ++i) {
1237 int i1 = isl_basic_map_alloc_equality(bmap1);
1238 if (i1 < 0)
1239 goto error;
1240 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1241 i_pos, o_pos, div_off);
1244 for (i = 0; i < bmap2->n_ineq; ++i) {
1245 int i1 = isl_basic_map_alloc_inequality(bmap1);
1246 if (i1 < 0)
1247 goto error;
1248 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1249 i_pos, o_pos, div_off);
1252 for (i = 0; i < bmap2->n_div; ++i) {
1253 int i1 = isl_basic_map_alloc_div(bmap1);
1254 if (i1 < 0)
1255 goto error;
1256 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1257 i_pos, o_pos, div_off);
1260 isl_basic_map_free(bmap2);
1262 return bmap1;
1264 error:
1265 isl_basic_map_free(bmap1);
1266 isl_basic_map_free(bmap2);
1267 return NULL;
1270 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1271 struct isl_basic_set *bset2, unsigned pos)
1273 return (struct isl_basic_set *)
1274 add_constraints((struct isl_basic_map *)bset1,
1275 (struct isl_basic_map *)bset2, 0, pos);
1278 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1279 __isl_take isl_space *dim, unsigned extra,
1280 unsigned n_eq, unsigned n_ineq)
1282 struct isl_basic_map *ext;
1283 unsigned flags;
1284 int dims_ok;
1286 if (!dim)
1287 goto error;
1289 if (!base)
1290 goto error;
1292 dims_ok = isl_space_is_equal(base->dim, dim) &&
1293 base->extra >= base->n_div + extra;
1295 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1296 room_for_ineq(base, n_ineq)) {
1297 isl_space_free(dim);
1298 return base;
1301 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1302 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1303 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1304 extra += base->extra;
1305 n_eq += base->n_eq;
1306 n_ineq += base->n_ineq;
1308 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1309 dim = NULL;
1310 if (!ext)
1311 goto error;
1313 if (dims_ok)
1314 ext->sample = isl_vec_copy(base->sample);
1315 flags = base->flags;
1316 ext = add_constraints(ext, base, 0, 0);
1317 if (ext) {
1318 ext->flags = flags;
1319 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1322 return ext;
1324 error:
1325 isl_space_free(dim);
1326 isl_basic_map_free(base);
1327 return NULL;
1330 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1331 __isl_take isl_space *dim, unsigned extra,
1332 unsigned n_eq, unsigned n_ineq)
1334 return (struct isl_basic_set *)
1335 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1336 extra, n_eq, n_ineq);
1339 struct isl_basic_map *isl_basic_map_extend_constraints(
1340 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1342 if (!base)
1343 return NULL;
1344 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1345 0, n_eq, n_ineq);
1348 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1349 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1350 unsigned n_eq, unsigned n_ineq)
1352 struct isl_basic_map *bmap;
1353 isl_space *dim;
1355 if (!base)
1356 return NULL;
1357 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1358 if (!dim)
1359 goto error;
1361 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1362 return bmap;
1363 error:
1364 isl_basic_map_free(base);
1365 return NULL;
1368 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1369 unsigned nparam, unsigned dim, unsigned extra,
1370 unsigned n_eq, unsigned n_ineq)
1372 return (struct isl_basic_set *)
1373 isl_basic_map_extend((struct isl_basic_map *)base,
1374 nparam, 0, dim, extra, n_eq, n_ineq);
1377 struct isl_basic_set *isl_basic_set_extend_constraints(
1378 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1380 return (struct isl_basic_set *)
1381 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1382 n_eq, n_ineq);
1385 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1387 return (struct isl_basic_set *)
1388 isl_basic_map_cow((struct isl_basic_map *)bset);
1391 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1393 if (!bmap)
1394 return NULL;
1396 if (bmap->ref > 1) {
1397 bmap->ref--;
1398 bmap = isl_basic_map_dup(bmap);
1400 if (bmap)
1401 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1402 return bmap;
1405 struct isl_set *isl_set_cow(struct isl_set *set)
1407 if (!set)
1408 return NULL;
1410 if (set->ref == 1)
1411 return set;
1412 set->ref--;
1413 return isl_set_dup(set);
1416 struct isl_map *isl_map_cow(struct isl_map *map)
1418 if (!map)
1419 return NULL;
1421 if (map->ref == 1)
1422 return map;
1423 map->ref--;
1424 return isl_map_dup(map);
1427 static void swap_vars(struct isl_blk blk, isl_int *a,
1428 unsigned a_len, unsigned b_len)
1430 isl_seq_cpy(blk.data, a+a_len, b_len);
1431 isl_seq_cpy(blk.data+b_len, a, a_len);
1432 isl_seq_cpy(a, blk.data, b_len+a_len);
1435 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1436 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1438 int i;
1439 struct isl_blk blk;
1441 if (!bmap)
1442 goto error;
1444 isl_assert(bmap->ctx,
1445 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1447 if (n1 == 0 || n2 == 0)
1448 return bmap;
1450 bmap = isl_basic_map_cow(bmap);
1451 if (!bmap)
1452 return NULL;
1454 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1455 if (isl_blk_is_error(blk))
1456 goto error;
1458 for (i = 0; i < bmap->n_eq; ++i)
1459 swap_vars(blk,
1460 bmap->eq[i] + pos, n1, n2);
1462 for (i = 0; i < bmap->n_ineq; ++i)
1463 swap_vars(blk,
1464 bmap->ineq[i] + pos, n1, n2);
1466 for (i = 0; i < bmap->n_div; ++i)
1467 swap_vars(blk,
1468 bmap->div[i]+1 + pos, n1, n2);
1470 isl_blk_free(bmap->ctx, blk);
1472 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1473 bmap = isl_basic_map_gauss(bmap, NULL);
1474 return isl_basic_map_finalize(bmap);
1475 error:
1476 isl_basic_map_free(bmap);
1477 return NULL;
1480 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1481 __isl_take isl_basic_set *bset, unsigned n)
1483 unsigned dim;
1484 unsigned nparam;
1486 nparam = isl_basic_set_n_param(bset);
1487 dim = isl_basic_set_n_dim(bset);
1488 isl_assert(bset->ctx, n <= dim, goto error);
1490 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1491 error:
1492 isl_basic_set_free(bset);
1493 return NULL;
1496 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1498 int i = 0;
1499 unsigned total;
1500 if (!bmap)
1501 goto error;
1502 total = isl_basic_map_total_dim(bmap);
1503 isl_basic_map_free_div(bmap, bmap->n_div);
1504 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1505 if (bmap->n_eq > 0)
1506 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1507 else {
1508 i = isl_basic_map_alloc_equality(bmap);
1509 if (i < 0)
1510 goto error;
1512 isl_int_set_si(bmap->eq[i][0], 1);
1513 isl_seq_clr(bmap->eq[i]+1, total);
1514 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1515 isl_vec_free(bmap->sample);
1516 bmap->sample = NULL;
1517 return isl_basic_map_finalize(bmap);
1518 error:
1519 isl_basic_map_free(bmap);
1520 return NULL;
1523 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1525 return (struct isl_basic_set *)
1526 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1529 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1531 int i;
1532 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1533 isl_int *t = bmap->div[a];
1534 bmap->div[a] = bmap->div[b];
1535 bmap->div[b] = t;
1537 for (i = 0; i < bmap->n_eq; ++i)
1538 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1540 for (i = 0; i < bmap->n_ineq; ++i)
1541 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1543 for (i = 0; i < bmap->n_div; ++i)
1544 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1545 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1548 /* Eliminate the specified n dimensions starting at first from the
1549 * constraints using Fourier-Motzkin. The dimensions themselves
1550 * are not removed.
1552 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1553 enum isl_dim_type type, unsigned first, unsigned n)
1555 int i;
1557 if (!map)
1558 return NULL;
1559 if (n == 0)
1560 return map;
1562 map = isl_map_cow(map);
1563 if (!map)
1564 return NULL;
1566 for (i = 0; i < map->n; ++i) {
1567 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1568 if (!map->p[i])
1569 goto error;
1571 return map;
1572 error:
1573 isl_map_free(map);
1574 return NULL;
1577 /* Eliminate the specified n dimensions starting at first from the
1578 * constraints using Fourier-Motzkin. The dimensions themselves
1579 * are not removed.
1581 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1582 enum isl_dim_type type, unsigned first, unsigned n)
1584 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1587 /* Eliminate the specified n dimensions starting at first from the
1588 * constraints using Fourier-Motzkin. The dimensions themselves
1589 * are not removed.
1591 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1592 unsigned first, unsigned n)
1594 return isl_set_eliminate(set, isl_dim_set, first, n);
1597 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1598 __isl_take isl_basic_map *bmap)
1600 if (!bmap)
1601 return NULL;
1602 bmap = isl_basic_map_eliminate_vars(bmap,
1603 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1604 if (!bmap)
1605 return NULL;
1606 bmap->n_div = 0;
1607 return isl_basic_map_finalize(bmap);
1610 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1611 __isl_take isl_basic_set *bset)
1613 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1614 (struct isl_basic_map *)bset);
1617 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1619 int i;
1621 if (!map)
1622 return NULL;
1623 if (map->n == 0)
1624 return map;
1626 map = isl_map_cow(map);
1627 if (!map)
1628 return NULL;
1630 for (i = 0; i < map->n; ++i) {
1631 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1632 if (!map->p[i])
1633 goto error;
1635 return map;
1636 error:
1637 isl_map_free(map);
1638 return NULL;
1641 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1643 return isl_map_remove_divs(set);
1646 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1647 enum isl_dim_type type, unsigned first, unsigned n)
1649 if (!bmap)
1650 return NULL;
1651 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1652 goto error);
1653 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1654 return bmap;
1655 bmap = isl_basic_map_eliminate_vars(bmap,
1656 isl_basic_map_offset(bmap, type) - 1 + first, n);
1657 if (!bmap)
1658 return bmap;
1659 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1660 return bmap;
1661 bmap = isl_basic_map_drop(bmap, type, first, n);
1662 return bmap;
1663 error:
1664 isl_basic_map_free(bmap);
1665 return NULL;
1668 /* Return true if the definition of the given div (recursively) involves
1669 * any of the given variables.
1671 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1672 unsigned first, unsigned n)
1674 int i;
1675 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1677 if (isl_int_is_zero(bmap->div[div][0]))
1678 return 0;
1679 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1680 return 1;
1682 for (i = bmap->n_div - 1; i >= 0; --i) {
1683 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1684 continue;
1685 if (div_involves_vars(bmap, i, first, n))
1686 return 1;
1689 return 0;
1692 /* Remove all divs (recursively) involving any of the given dimensions
1693 * in their definitions.
1695 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1696 __isl_take isl_basic_map *bmap,
1697 enum isl_dim_type type, unsigned first, unsigned n)
1699 int i;
1701 if (!bmap)
1702 return NULL;
1703 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1704 goto error);
1705 first += isl_basic_map_offset(bmap, type);
1707 for (i = bmap->n_div - 1; i >= 0; --i) {
1708 if (!div_involves_vars(bmap, i, first, n))
1709 continue;
1710 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1711 if (!bmap)
1712 return NULL;
1713 i = bmap->n_div;
1716 return bmap;
1717 error:
1718 isl_basic_map_free(bmap);
1719 return NULL;
1722 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1723 enum isl_dim_type type, unsigned first, unsigned n)
1725 int i;
1727 if (!map)
1728 return NULL;
1729 if (map->n == 0)
1730 return map;
1732 map = isl_map_cow(map);
1733 if (!map)
1734 return NULL;
1736 for (i = 0; i < map->n; ++i) {
1737 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1738 type, first, n);
1739 if (!map->p[i])
1740 goto error;
1742 return map;
1743 error:
1744 isl_map_free(map);
1745 return NULL;
1748 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1749 enum isl_dim_type type, unsigned first, unsigned n)
1751 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1752 type, first, n);
1755 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1756 enum isl_dim_type type, unsigned first, unsigned n)
1758 int i;
1760 if (!bmap)
1761 return -1;
1763 if (first + n > isl_basic_map_dim(bmap, type))
1764 isl_die(bmap->ctx, isl_error_invalid,
1765 "index out of bounds", return -1);
1767 first += isl_basic_map_offset(bmap, type);
1768 for (i = 0; i < bmap->n_eq; ++i)
1769 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1770 return 1;
1771 for (i = 0; i < bmap->n_ineq; ++i)
1772 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1773 return 1;
1775 return 0;
1778 int isl_map_involves_dims(__isl_keep isl_map *map,
1779 enum isl_dim_type type, unsigned first, unsigned n)
1781 int i;
1783 if (!map)
1784 return -1;
1786 if (first + n > isl_map_dim(map, type))
1787 isl_die(map->ctx, isl_error_invalid,
1788 "index out of bounds", return -1);
1790 for (i = 0; i < map->n; ++i) {
1791 int involves = isl_basic_map_involves_dims(map->p[i],
1792 type, first, n);
1793 if (involves < 0 || involves)
1794 return involves;
1797 return 0;
1800 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1801 enum isl_dim_type type, unsigned first, unsigned n)
1803 return isl_basic_map_involves_dims(bset, type, first, n);
1806 int isl_set_involves_dims(__isl_keep isl_set *set,
1807 enum isl_dim_type type, unsigned first, unsigned n)
1809 return isl_map_involves_dims(set, type, first, n);
1812 /* Return true if the definition of the given div is unknown or depends
1813 * on unknown divs.
1815 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1817 int i;
1818 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1820 if (isl_int_is_zero(bmap->div[div][0]))
1821 return 1;
1823 for (i = bmap->n_div - 1; i >= 0; --i) {
1824 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1825 continue;
1826 if (div_is_unknown(bmap, i))
1827 return 1;
1830 return 0;
1833 /* Remove all divs that are unknown or defined in terms of unknown divs.
1835 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1836 __isl_take isl_basic_map *bmap)
1838 int i;
1840 if (!bmap)
1841 return NULL;
1843 for (i = bmap->n_div - 1; i >= 0; --i) {
1844 if (!div_is_unknown(bmap, i))
1845 continue;
1846 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1849 return bmap;
1852 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1854 int i;
1856 if (!map)
1857 return NULL;
1858 if (map->n == 0)
1859 return map;
1861 map = isl_map_cow(map);
1862 if (!map)
1863 return NULL;
1865 for (i = 0; i < map->n; ++i) {
1866 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1867 if (!map->p[i])
1868 goto error;
1870 return map;
1871 error:
1872 isl_map_free(map);
1873 return NULL;
1876 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1878 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1881 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1882 __isl_take isl_basic_set *bset,
1883 enum isl_dim_type type, unsigned first, unsigned n)
1885 return (isl_basic_set *)
1886 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1889 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1890 enum isl_dim_type type, unsigned first, unsigned n)
1892 int i;
1894 if (n == 0)
1895 return map;
1897 map = isl_map_cow(map);
1898 if (!map)
1899 return NULL;
1900 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1902 for (i = 0; i < map->n; ++i) {
1903 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1904 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1905 if (!map->p[i])
1906 goto error;
1908 map = isl_map_drop(map, type, first, n);
1909 return map;
1910 error:
1911 isl_map_free(map);
1912 return NULL;
1915 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1916 enum isl_dim_type type, unsigned first, unsigned n)
1918 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1921 /* Project out n inputs starting at first using Fourier-Motzkin */
1922 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1923 unsigned first, unsigned n)
1925 return isl_map_remove_dims(map, isl_dim_in, first, n);
1928 static void dump_term(struct isl_basic_map *bmap,
1929 isl_int c, int pos, FILE *out)
1931 const char *name;
1932 unsigned in = isl_basic_map_n_in(bmap);
1933 unsigned dim = in + isl_basic_map_n_out(bmap);
1934 unsigned nparam = isl_basic_map_n_param(bmap);
1935 if (!pos)
1936 isl_int_print(out, c, 0);
1937 else {
1938 if (!isl_int_is_one(c))
1939 isl_int_print(out, c, 0);
1940 if (pos < 1 + nparam) {
1941 name = isl_space_get_dim_name(bmap->dim,
1942 isl_dim_param, pos - 1);
1943 if (name)
1944 fprintf(out, "%s", name);
1945 else
1946 fprintf(out, "p%d", pos - 1);
1947 } else if (pos < 1 + nparam + in)
1948 fprintf(out, "i%d", pos - 1 - nparam);
1949 else if (pos < 1 + nparam + dim)
1950 fprintf(out, "o%d", pos - 1 - nparam - in);
1951 else
1952 fprintf(out, "e%d", pos - 1 - nparam - dim);
1956 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1957 int sign, FILE *out)
1959 int i;
1960 int first;
1961 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1962 isl_int v;
1964 isl_int_init(v);
1965 for (i = 0, first = 1; i < len; ++i) {
1966 if (isl_int_sgn(c[i]) * sign <= 0)
1967 continue;
1968 if (!first)
1969 fprintf(out, " + ");
1970 first = 0;
1971 isl_int_abs(v, c[i]);
1972 dump_term(bmap, v, i, out);
1974 isl_int_clear(v);
1975 if (first)
1976 fprintf(out, "0");
1979 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1980 const char *op, FILE *out, int indent)
1982 int i;
1984 fprintf(out, "%*s", indent, "");
1986 dump_constraint_sign(bmap, c, 1, out);
1987 fprintf(out, " %s ", op);
1988 dump_constraint_sign(bmap, c, -1, out);
1990 fprintf(out, "\n");
1992 for (i = bmap->n_div; i < bmap->extra; ++i) {
1993 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
1994 continue;
1995 fprintf(out, "%*s", indent, "");
1996 fprintf(out, "ERROR: unused div coefficient not zero\n");
1997 abort();
2001 static void dump_constraints(struct isl_basic_map *bmap,
2002 isl_int **c, unsigned n,
2003 const char *op, FILE *out, int indent)
2005 int i;
2007 for (i = 0; i < n; ++i)
2008 dump_constraint(bmap, c[i], op, out, indent);
2011 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2013 int j;
2014 int first = 1;
2015 unsigned total = isl_basic_map_total_dim(bmap);
2017 for (j = 0; j < 1 + total; ++j) {
2018 if (isl_int_is_zero(exp[j]))
2019 continue;
2020 if (!first && isl_int_is_pos(exp[j]))
2021 fprintf(out, "+");
2022 dump_term(bmap, exp[j], j, out);
2023 first = 0;
2027 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2029 int i;
2031 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2032 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2034 for (i = 0; i < bmap->n_div; ++i) {
2035 fprintf(out, "%*s", indent, "");
2036 fprintf(out, "e%d = [(", i);
2037 dump_affine(bmap, bmap->div[i]+1, out);
2038 fprintf(out, ")/");
2039 isl_int_print(out, bmap->div[i][0], 0);
2040 fprintf(out, "]\n");
2044 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2045 FILE *out, int indent)
2047 if (!bset) {
2048 fprintf(out, "null basic set\n");
2049 return;
2052 fprintf(out, "%*s", indent, "");
2053 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2054 bset->ref, bset->dim->nparam, bset->dim->n_out,
2055 bset->extra, bset->flags);
2056 dump((struct isl_basic_map *)bset, out, indent);
2059 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2060 FILE *out, int indent)
2062 if (!bmap) {
2063 fprintf(out, "null basic map\n");
2064 return;
2067 fprintf(out, "%*s", indent, "");
2068 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2069 "flags: %x, n_name: %d\n",
2070 bmap->ref,
2071 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2072 bmap->extra, bmap->flags, bmap->dim->n_id);
2073 dump(bmap, out, indent);
2076 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2078 unsigned total;
2079 if (!bmap)
2080 return -1;
2081 total = isl_basic_map_total_dim(bmap);
2082 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2083 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2084 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2085 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2086 return 0;
2089 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2090 unsigned flags)
2092 struct isl_set *set;
2094 if (!dim)
2095 return NULL;
2096 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2097 isl_assert(dim->ctx, n >= 0, goto error);
2098 set = isl_alloc(dim->ctx, struct isl_set,
2099 sizeof(struct isl_set) +
2100 (n - 1) * sizeof(struct isl_basic_set *));
2101 if (!set)
2102 goto error;
2104 set->ctx = dim->ctx;
2105 isl_ctx_ref(set->ctx);
2106 set->ref = 1;
2107 set->size = n;
2108 set->n = 0;
2109 set->dim = dim;
2110 set->flags = flags;
2111 return set;
2112 error:
2113 isl_space_free(dim);
2114 return NULL;
2117 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2118 unsigned nparam, unsigned dim, int n, unsigned flags)
2120 struct isl_set *set;
2121 isl_space *dims;
2123 dims = isl_space_alloc(ctx, nparam, 0, dim);
2124 if (!dims)
2125 return NULL;
2127 set = isl_set_alloc_space(dims, n, flags);
2128 return set;
2131 /* Make sure "map" has room for at least "n" more basic maps.
2133 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2135 int i;
2136 struct isl_map *grown = NULL;
2138 if (!map)
2139 return NULL;
2140 isl_assert(map->ctx, n >= 0, goto error);
2141 if (map->n + n <= map->size)
2142 return map;
2143 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2144 if (!grown)
2145 goto error;
2146 for (i = 0; i < map->n; ++i) {
2147 grown->p[i] = isl_basic_map_copy(map->p[i]);
2148 if (!grown->p[i])
2149 goto error;
2150 grown->n++;
2152 isl_map_free(map);
2153 return grown;
2154 error:
2155 isl_map_free(grown);
2156 isl_map_free(map);
2157 return NULL;
2160 /* Make sure "set" has room for at least "n" more basic sets.
2162 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2164 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2167 struct isl_set *isl_set_dup(struct isl_set *set)
2169 int i;
2170 struct isl_set *dup;
2172 if (!set)
2173 return NULL;
2175 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2176 if (!dup)
2177 return NULL;
2178 for (i = 0; i < set->n; ++i)
2179 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2180 return dup;
2183 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2185 return isl_map_from_basic_map(bset);
2188 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2190 struct isl_map *map;
2192 if (!bmap)
2193 return NULL;
2195 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2196 return isl_map_add_basic_map(map, bmap);
2199 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2200 __isl_take isl_basic_set *bset)
2202 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2203 (struct isl_basic_map *)bset);
2206 void isl_set_free(struct isl_set *set)
2208 int i;
2210 if (!set)
2211 return;
2213 if (--set->ref > 0)
2214 return;
2216 isl_ctx_deref(set->ctx);
2217 for (i = 0; i < set->n; ++i)
2218 isl_basic_set_free(set->p[i]);
2219 isl_space_free(set->dim);
2220 free(set);
2223 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2225 int i;
2227 if (!set) {
2228 fprintf(out, "null set\n");
2229 return;
2232 fprintf(out, "%*s", indent, "");
2233 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2234 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2235 set->flags);
2236 for (i = 0; i < set->n; ++i) {
2237 fprintf(out, "%*s", indent, "");
2238 fprintf(out, "basic set %d:\n", i);
2239 isl_basic_set_print_internal(set->p[i], out, indent+4);
2243 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2245 int i;
2247 if (!map) {
2248 fprintf(out, "null map\n");
2249 return;
2252 fprintf(out, "%*s", indent, "");
2253 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2254 "flags: %x, n_name: %d\n",
2255 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2256 map->dim->n_out, map->flags, map->dim->n_id);
2257 for (i = 0; i < map->n; ++i) {
2258 fprintf(out, "%*s", indent, "");
2259 fprintf(out, "basic map %d:\n", i);
2260 isl_basic_map_print_internal(map->p[i], out, indent+4);
2264 struct isl_basic_map *isl_basic_map_intersect_domain(
2265 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2267 struct isl_basic_map *bmap_domain;
2269 if (!bmap || !bset)
2270 goto error;
2272 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2273 bset->dim, isl_dim_param), goto error);
2275 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2276 isl_assert(bset->ctx,
2277 isl_basic_map_compatible_domain(bmap, bset), goto error);
2279 bmap = isl_basic_map_cow(bmap);
2280 if (!bmap)
2281 goto error;
2282 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2283 bset->n_div, bset->n_eq, bset->n_ineq);
2284 bmap_domain = isl_basic_map_from_domain(bset);
2285 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2287 bmap = isl_basic_map_simplify(bmap);
2288 return isl_basic_map_finalize(bmap);
2289 error:
2290 isl_basic_map_free(bmap);
2291 isl_basic_set_free(bset);
2292 return NULL;
2295 struct isl_basic_map *isl_basic_map_intersect_range(
2296 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2298 struct isl_basic_map *bmap_range;
2300 if (!bmap || !bset)
2301 goto error;
2303 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2304 bset->dim, isl_dim_param), goto error);
2306 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2307 isl_assert(bset->ctx,
2308 isl_basic_map_compatible_range(bmap, bset), goto error);
2310 if (isl_basic_set_is_universe(bset)) {
2311 isl_basic_set_free(bset);
2312 return bmap;
2315 bmap = isl_basic_map_cow(bmap);
2316 if (!bmap)
2317 goto error;
2318 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2319 bset->n_div, bset->n_eq, bset->n_ineq);
2320 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2321 bmap = add_constraints(bmap, bmap_range, 0, 0);
2323 bmap = isl_basic_map_simplify(bmap);
2324 return isl_basic_map_finalize(bmap);
2325 error:
2326 isl_basic_map_free(bmap);
2327 isl_basic_set_free(bset);
2328 return NULL;
2331 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2333 int i;
2334 unsigned total;
2335 isl_int s;
2337 total = 1 + isl_basic_map_total_dim(bmap);
2338 if (total != vec->size)
2339 return -1;
2341 isl_int_init(s);
2343 for (i = 0; i < bmap->n_eq; ++i) {
2344 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2345 if (!isl_int_is_zero(s)) {
2346 isl_int_clear(s);
2347 return 0;
2351 for (i = 0; i < bmap->n_ineq; ++i) {
2352 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2353 if (isl_int_is_neg(s)) {
2354 isl_int_clear(s);
2355 return 0;
2359 isl_int_clear(s);
2361 return 1;
2364 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2366 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2369 struct isl_basic_map *isl_basic_map_intersect(
2370 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2372 struct isl_vec *sample = NULL;
2374 if (!bmap1 || !bmap2)
2375 goto error;
2377 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2378 bmap2->dim, isl_dim_param), goto error);
2379 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2380 isl_space_dim(bmap1->dim, isl_dim_param) &&
2381 isl_space_dim(bmap2->dim, isl_dim_all) !=
2382 isl_space_dim(bmap2->dim, isl_dim_param))
2383 return isl_basic_map_intersect(bmap2, bmap1);
2385 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2386 isl_space_dim(bmap2->dim, isl_dim_param))
2387 isl_assert(bmap1->ctx,
2388 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2390 if (bmap1->sample &&
2391 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2392 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2393 sample = isl_vec_copy(bmap1->sample);
2394 else if (bmap2->sample &&
2395 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2396 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2397 sample = isl_vec_copy(bmap2->sample);
2399 bmap1 = isl_basic_map_cow(bmap1);
2400 if (!bmap1)
2401 goto error;
2402 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2403 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2404 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2406 if (!bmap1)
2407 isl_vec_free(sample);
2408 else if (sample) {
2409 isl_vec_free(bmap1->sample);
2410 bmap1->sample = sample;
2413 bmap1 = isl_basic_map_simplify(bmap1);
2414 return isl_basic_map_finalize(bmap1);
2415 error:
2416 if (sample)
2417 isl_vec_free(sample);
2418 isl_basic_map_free(bmap1);
2419 isl_basic_map_free(bmap2);
2420 return NULL;
2423 struct isl_basic_set *isl_basic_set_intersect(
2424 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2426 return (struct isl_basic_set *)
2427 isl_basic_map_intersect(
2428 (struct isl_basic_map *)bset1,
2429 (struct isl_basic_map *)bset2);
2432 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2433 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2435 return isl_basic_set_intersect(bset1, bset2);
2438 /* Special case of isl_map_intersect, where both map1 and map2
2439 * are convex, without any divs and such that either map1 or map2
2440 * contains a single constraint. This constraint is then simply
2441 * added to the other map.
2443 static __isl_give isl_map *map_intersect_add_constraint(
2444 __isl_take isl_map *map1, __isl_take isl_map *map2)
2446 isl_assert(map1->ctx, map1->n == 1, goto error);
2447 isl_assert(map2->ctx, map1->n == 1, goto error);
2448 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2449 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2451 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2452 return isl_map_intersect(map2, map1);
2454 isl_assert(map2->ctx,
2455 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2457 map1 = isl_map_cow(map1);
2458 if (!map1)
2459 goto error;
2460 if (isl_map_plain_is_empty(map1)) {
2461 isl_map_free(map2);
2462 return map1;
2464 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2465 if (map2->p[0]->n_eq == 1)
2466 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2467 else
2468 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2469 map2->p[0]->ineq[0]);
2471 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2472 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2473 if (!map1->p[0])
2474 goto error;
2476 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2477 isl_basic_map_free(map1->p[0]);
2478 map1->n = 0;
2481 isl_map_free(map2);
2483 return map1;
2484 error:
2485 isl_map_free(map1);
2486 isl_map_free(map2);
2487 return NULL;
2490 /* map2 may be either a parameter domain or a map living in the same
2491 * space as map1.
2493 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2494 __isl_take isl_map *map2)
2496 unsigned flags = 0;
2497 struct isl_map *result;
2498 int i, j;
2500 if (!map1 || !map2)
2501 goto error;
2503 if (isl_map_plain_is_empty(map1) &&
2504 isl_space_is_equal(map1->dim, map2->dim)) {
2505 isl_map_free(map2);
2506 return map1;
2508 if (isl_map_plain_is_empty(map2) &&
2509 isl_space_is_equal(map1->dim, map2->dim)) {
2510 isl_map_free(map1);
2511 return map2;
2514 if (map1->n == 1 && map2->n == 1 &&
2515 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2516 isl_space_is_equal(map1->dim, map2->dim) &&
2517 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2518 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2519 return map_intersect_add_constraint(map1, map2);
2521 if (isl_space_dim(map2->dim, isl_dim_all) !=
2522 isl_space_dim(map2->dim, isl_dim_param))
2523 isl_assert(map1->ctx,
2524 isl_space_is_equal(map1->dim, map2->dim), goto error);
2526 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2527 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2528 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2530 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2531 map1->n * map2->n, flags);
2532 if (!result)
2533 goto error;
2534 for (i = 0; i < map1->n; ++i)
2535 for (j = 0; j < map2->n; ++j) {
2536 struct isl_basic_map *part;
2537 part = isl_basic_map_intersect(
2538 isl_basic_map_copy(map1->p[i]),
2539 isl_basic_map_copy(map2->p[j]));
2540 if (isl_basic_map_is_empty(part))
2541 isl_basic_map_free(part);
2542 else
2543 result = isl_map_add_basic_map(result, part);
2544 if (!result)
2545 goto error;
2547 isl_map_free(map1);
2548 isl_map_free(map2);
2549 return result;
2550 error:
2551 isl_map_free(map1);
2552 isl_map_free(map2);
2553 return NULL;
2556 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2557 __isl_take isl_map *map2)
2559 if (!map1 || !map2)
2560 goto error;
2561 if (!isl_space_is_equal(map1->dim, map2->dim))
2562 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2563 "spaces don't match", goto error);
2564 return map_intersect_internal(map1, map2);
2565 error:
2566 isl_map_free(map1);
2567 isl_map_free(map2);
2568 return NULL;
2571 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2572 __isl_take isl_map *map2)
2574 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2577 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2579 return (struct isl_set *)
2580 isl_map_intersect((struct isl_map *)set1,
2581 (struct isl_map *)set2);
2584 /* map_intersect_internal accepts intersections
2585 * with parameter domains, so we can just call that function.
2587 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2588 __isl_take isl_set *params)
2590 return map_intersect_internal(map, params);
2593 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2594 __isl_take isl_map *map2)
2596 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2599 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2600 __isl_take isl_set *params)
2602 return isl_map_intersect_params(set, params);
2605 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2607 isl_space *dim;
2608 struct isl_basic_set *bset;
2609 unsigned in;
2611 if (!bmap)
2612 return NULL;
2613 bmap = isl_basic_map_cow(bmap);
2614 if (!bmap)
2615 return NULL;
2616 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2617 in = isl_basic_map_n_in(bmap);
2618 bset = isl_basic_set_from_basic_map(bmap);
2619 bset = isl_basic_set_swap_vars(bset, in);
2620 return isl_basic_map_from_basic_set(bset, dim);
2623 static __isl_give isl_basic_map *basic_map_space_reset(
2624 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2626 isl_space *space;
2628 if (!isl_space_is_named_or_nested(bmap->dim, type))
2629 return bmap;
2631 space = isl_basic_map_get_space(bmap);
2632 space = isl_space_reset(space, type);
2633 bmap = isl_basic_map_reset_space(bmap, space);
2634 return bmap;
2637 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2638 enum isl_dim_type type, unsigned pos, unsigned n)
2640 isl_space *res_dim;
2641 struct isl_basic_map *res;
2642 struct isl_dim_map *dim_map;
2643 unsigned total, off;
2644 enum isl_dim_type t;
2646 if (n == 0)
2647 return basic_map_space_reset(bmap, type);
2649 if (!bmap)
2650 return NULL;
2652 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2654 total = isl_basic_map_total_dim(bmap) + n;
2655 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2656 off = 0;
2657 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2658 if (t != type) {
2659 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2660 } else {
2661 unsigned size = isl_basic_map_dim(bmap, t);
2662 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2663 0, pos, off);
2664 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2665 pos, size - pos, off + pos + n);
2667 off += isl_space_dim(res_dim, t);
2669 isl_dim_map_div(dim_map, bmap, off);
2671 res = isl_basic_map_alloc_space(res_dim,
2672 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2673 if (isl_basic_map_is_rational(bmap))
2674 res = isl_basic_map_set_rational(res);
2675 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2676 return isl_basic_map_finalize(res);
2679 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2680 enum isl_dim_type type, unsigned n)
2682 if (!bmap)
2683 return NULL;
2684 return isl_basic_map_insert(bmap, type,
2685 isl_basic_map_dim(bmap, type), n);
2688 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2689 enum isl_dim_type type, unsigned n)
2691 if (!bset)
2692 return NULL;
2693 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2694 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2695 error:
2696 isl_basic_set_free(bset);
2697 return NULL;
2700 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2701 enum isl_dim_type type)
2703 isl_space *space;
2705 if (!isl_space_is_named_or_nested(map->dim, type))
2706 return map;
2708 space = isl_map_get_space(map);
2709 space = isl_space_reset(space, type);
2710 map = isl_map_reset_space(map, space);
2711 return map;
2714 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2715 enum isl_dim_type type, unsigned pos, unsigned n)
2717 int i;
2719 if (n == 0)
2720 return map_space_reset(map, type);
2722 map = isl_map_cow(map);
2723 if (!map)
2724 return NULL;
2726 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
2727 if (!map->dim)
2728 goto error;
2730 for (i = 0; i < map->n; ++i) {
2731 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2732 if (!map->p[i])
2733 goto error;
2736 return map;
2737 error:
2738 isl_map_free(map);
2739 return NULL;
2742 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
2743 enum isl_dim_type type, unsigned pos, unsigned n)
2745 return isl_map_insert_dims(set, type, pos, n);
2748 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2749 enum isl_dim_type type, unsigned n)
2751 if (!map)
2752 return NULL;
2753 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
2756 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2757 enum isl_dim_type type, unsigned n)
2759 if (!set)
2760 return NULL;
2761 isl_assert(set->ctx, type != isl_dim_in, goto error);
2762 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2763 error:
2764 isl_set_free(set);
2765 return NULL;
2768 __isl_give isl_basic_map *isl_basic_map_move_dims(
2769 __isl_take isl_basic_map *bmap,
2770 enum isl_dim_type dst_type, unsigned dst_pos,
2771 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2773 struct isl_dim_map *dim_map;
2774 struct isl_basic_map *res;
2775 enum isl_dim_type t;
2776 unsigned total, off;
2778 if (!bmap)
2779 return NULL;
2780 if (n == 0)
2781 return bmap;
2783 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2784 goto error);
2786 if (dst_type == src_type && dst_pos == src_pos)
2787 return bmap;
2789 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2791 if (pos(bmap->dim, dst_type) + dst_pos ==
2792 pos(bmap->dim, src_type) + src_pos +
2793 ((src_type < dst_type) ? n : 0)) {
2794 bmap = isl_basic_map_cow(bmap);
2795 if (!bmap)
2796 return NULL;
2798 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2799 src_type, src_pos, n);
2800 if (!bmap->dim)
2801 goto error;
2803 bmap = isl_basic_map_finalize(bmap);
2805 return bmap;
2808 total = isl_basic_map_total_dim(bmap);
2809 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2811 off = 0;
2812 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2813 unsigned size = isl_space_dim(bmap->dim, t);
2814 if (t == dst_type) {
2815 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2816 0, dst_pos, off);
2817 off += dst_pos;
2818 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2819 src_pos, n, off);
2820 off += n;
2821 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2822 dst_pos, size - dst_pos, off);
2823 off += size - dst_pos;
2824 } else if (t == src_type) {
2825 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2826 0, src_pos, off);
2827 off += src_pos;
2828 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2829 src_pos + n, size - src_pos - n, off);
2830 off += size - src_pos - n;
2831 } else {
2832 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2833 off += size;
2836 isl_dim_map_div(dim_map, bmap, off);
2838 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2839 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2840 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2842 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2843 src_type, src_pos, n);
2844 if (!bmap->dim)
2845 goto error;
2847 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2848 bmap = isl_basic_map_gauss(bmap, NULL);
2849 bmap = isl_basic_map_finalize(bmap);
2851 return bmap;
2852 error:
2853 isl_basic_map_free(bmap);
2854 return NULL;
2857 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2858 enum isl_dim_type dst_type, unsigned dst_pos,
2859 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2861 return (isl_basic_set *)isl_basic_map_move_dims(
2862 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2865 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2866 enum isl_dim_type dst_type, unsigned dst_pos,
2867 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2869 if (!set)
2870 return NULL;
2871 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2872 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2873 src_type, src_pos, n);
2874 error:
2875 isl_set_free(set);
2876 return NULL;
2879 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2880 enum isl_dim_type dst_type, unsigned dst_pos,
2881 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2883 int i;
2885 if (!map)
2886 return NULL;
2887 if (n == 0)
2888 return map;
2890 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2891 goto error);
2893 if (dst_type == src_type && dst_pos == src_pos)
2894 return map;
2896 isl_assert(map->ctx, dst_type != src_type, goto error);
2898 map = isl_map_cow(map);
2899 if (!map)
2900 return NULL;
2902 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2903 if (!map->dim)
2904 goto error;
2906 for (i = 0; i < map->n; ++i) {
2907 map->p[i] = isl_basic_map_move_dims(map->p[i],
2908 dst_type, dst_pos,
2909 src_type, src_pos, n);
2910 if (!map->p[i])
2911 goto error;
2914 return map;
2915 error:
2916 isl_map_free(map);
2917 return NULL;
2920 /* Move the specified dimensions to the last columns right before
2921 * the divs. Don't change the dimension specification of bmap.
2922 * That's the responsibility of the caller.
2924 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2925 enum isl_dim_type type, unsigned first, unsigned n)
2927 struct isl_dim_map *dim_map;
2928 struct isl_basic_map *res;
2929 enum isl_dim_type t;
2930 unsigned total, off;
2932 if (!bmap)
2933 return NULL;
2934 if (pos(bmap->dim, type) + first + n ==
2935 1 + isl_space_dim(bmap->dim, isl_dim_all))
2936 return bmap;
2938 total = isl_basic_map_total_dim(bmap);
2939 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2941 off = 0;
2942 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2943 unsigned size = isl_space_dim(bmap->dim, t);
2944 if (t == type) {
2945 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2946 0, first, off);
2947 off += first;
2948 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2949 first, n, total - bmap->n_div - n);
2950 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2951 first + n, size - (first + n), off);
2952 off += size - (first + n);
2953 } else {
2954 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2955 off += size;
2958 isl_dim_map_div(dim_map, bmap, off + n);
2960 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2961 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2962 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2963 return res;
2966 /* Turn the n dimensions of type type, starting at first
2967 * into existentially quantified variables.
2969 __isl_give isl_basic_map *isl_basic_map_project_out(
2970 __isl_take isl_basic_map *bmap,
2971 enum isl_dim_type type, unsigned first, unsigned n)
2973 int i;
2974 size_t row_size;
2975 isl_int **new_div;
2976 isl_int *old;
2978 if (n == 0)
2979 return bmap;
2981 if (!bmap)
2982 return NULL;
2984 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2985 return isl_basic_map_remove_dims(bmap, type, first, n);
2987 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2988 goto error);
2990 bmap = move_last(bmap, type, first, n);
2991 bmap = isl_basic_map_cow(bmap);
2992 if (!bmap)
2993 return NULL;
2995 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
2996 old = bmap->block2.data;
2997 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2998 (bmap->extra + n) * (1 + row_size));
2999 if (!bmap->block2.data)
3000 goto error;
3001 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3002 if (!new_div)
3003 goto error;
3004 for (i = 0; i < n; ++i) {
3005 new_div[i] = bmap->block2.data +
3006 (bmap->extra + i) * (1 + row_size);
3007 isl_seq_clr(new_div[i], 1 + row_size);
3009 for (i = 0; i < bmap->extra; ++i)
3010 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3011 free(bmap->div);
3012 bmap->div = new_div;
3013 bmap->n_div += n;
3014 bmap->extra += n;
3016 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3017 if (!bmap->dim)
3018 goto error;
3019 bmap = isl_basic_map_simplify(bmap);
3020 bmap = isl_basic_map_drop_redundant_divs(bmap);
3021 return isl_basic_map_finalize(bmap);
3022 error:
3023 isl_basic_map_free(bmap);
3024 return NULL;
3027 /* Turn the n dimensions of type type, starting at first
3028 * into existentially quantified variables.
3030 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3031 enum isl_dim_type type, unsigned first, unsigned n)
3033 return (isl_basic_set *)isl_basic_map_project_out(
3034 (isl_basic_map *)bset, type, first, n);
3037 /* Turn the n dimensions of type type, starting at first
3038 * into existentially quantified variables.
3040 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3041 enum isl_dim_type type, unsigned first, unsigned n)
3043 int i;
3045 if (!map)
3046 return NULL;
3048 if (n == 0)
3049 return map;
3051 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3053 map = isl_map_cow(map);
3054 if (!map)
3055 return NULL;
3057 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3058 if (!map->dim)
3059 goto error;
3061 for (i = 0; i < map->n; ++i) {
3062 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3063 if (!map->p[i])
3064 goto error;
3067 return map;
3068 error:
3069 isl_map_free(map);
3070 return NULL;
3073 /* Turn the n dimensions of type type, starting at first
3074 * into existentially quantified variables.
3076 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3077 enum isl_dim_type type, unsigned first, unsigned n)
3079 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3082 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3084 int i, j;
3086 for (i = 0; i < n; ++i) {
3087 j = isl_basic_map_alloc_div(bmap);
3088 if (j < 0)
3089 goto error;
3090 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3092 return bmap;
3093 error:
3094 isl_basic_map_free(bmap);
3095 return NULL;
3098 struct isl_basic_map *isl_basic_map_apply_range(
3099 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3101 isl_space *dim_result = NULL;
3102 struct isl_basic_map *bmap;
3103 unsigned n_in, n_out, n, nparam, total, pos;
3104 struct isl_dim_map *dim_map1, *dim_map2;
3106 if (!bmap1 || !bmap2)
3107 goto error;
3109 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3110 isl_space_copy(bmap2->dim));
3112 n_in = isl_basic_map_n_in(bmap1);
3113 n_out = isl_basic_map_n_out(bmap2);
3114 n = isl_basic_map_n_out(bmap1);
3115 nparam = isl_basic_map_n_param(bmap1);
3117 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3118 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3119 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3120 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3121 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3122 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3123 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3124 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3125 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3126 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3127 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3129 bmap = isl_basic_map_alloc_space(dim_result,
3130 bmap1->n_div + bmap2->n_div + n,
3131 bmap1->n_eq + bmap2->n_eq,
3132 bmap1->n_ineq + bmap2->n_ineq);
3133 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3134 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3135 bmap = add_divs(bmap, n);
3136 bmap = isl_basic_map_simplify(bmap);
3137 bmap = isl_basic_map_drop_redundant_divs(bmap);
3138 return isl_basic_map_finalize(bmap);
3139 error:
3140 isl_basic_map_free(bmap1);
3141 isl_basic_map_free(bmap2);
3142 return NULL;
3145 struct isl_basic_set *isl_basic_set_apply(
3146 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3148 if (!bset || !bmap)
3149 goto error;
3151 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3152 goto error);
3154 return (struct isl_basic_set *)
3155 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3156 error:
3157 isl_basic_set_free(bset);
3158 isl_basic_map_free(bmap);
3159 return NULL;
3162 struct isl_basic_map *isl_basic_map_apply_domain(
3163 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3165 if (!bmap1 || !bmap2)
3166 goto error;
3168 isl_assert(bmap1->ctx,
3169 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3170 isl_assert(bmap1->ctx,
3171 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3172 goto error);
3174 bmap1 = isl_basic_map_reverse(bmap1);
3175 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3176 return isl_basic_map_reverse(bmap1);
3177 error:
3178 isl_basic_map_free(bmap1);
3179 isl_basic_map_free(bmap2);
3180 return NULL;
3183 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3184 * A \cap B -> f(A) + f(B)
3186 struct isl_basic_map *isl_basic_map_sum(
3187 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3189 unsigned n_in, n_out, nparam, total, pos;
3190 struct isl_basic_map *bmap = NULL;
3191 struct isl_dim_map *dim_map1, *dim_map2;
3192 int i;
3194 if (!bmap1 || !bmap2)
3195 goto error;
3197 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3198 goto error);
3200 nparam = isl_basic_map_n_param(bmap1);
3201 n_in = isl_basic_map_n_in(bmap1);
3202 n_out = isl_basic_map_n_out(bmap1);
3204 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3205 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3206 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3207 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3208 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3209 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3210 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3211 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3212 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3213 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3214 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3216 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3217 bmap1->n_div + bmap2->n_div + 2 * n_out,
3218 bmap1->n_eq + bmap2->n_eq + n_out,
3219 bmap1->n_ineq + bmap2->n_ineq);
3220 for (i = 0; i < n_out; ++i) {
3221 int j = isl_basic_map_alloc_equality(bmap);
3222 if (j < 0)
3223 goto error;
3224 isl_seq_clr(bmap->eq[j], 1+total);
3225 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3226 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3227 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3229 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3230 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3231 bmap = add_divs(bmap, 2 * n_out);
3233 bmap = isl_basic_map_simplify(bmap);
3234 return isl_basic_map_finalize(bmap);
3235 error:
3236 isl_basic_map_free(bmap);
3237 isl_basic_map_free(bmap1);
3238 isl_basic_map_free(bmap2);
3239 return NULL;
3242 /* Given two maps A -> f(A) and B -> g(B), construct a map
3243 * A \cap B -> f(A) + f(B)
3245 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3247 struct isl_map *result;
3248 int i, j;
3250 if (!map1 || !map2)
3251 goto error;
3253 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3255 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3256 map1->n * map2->n, 0);
3257 if (!result)
3258 goto error;
3259 for (i = 0; i < map1->n; ++i)
3260 for (j = 0; j < map2->n; ++j) {
3261 struct isl_basic_map *part;
3262 part = isl_basic_map_sum(
3263 isl_basic_map_copy(map1->p[i]),
3264 isl_basic_map_copy(map2->p[j]));
3265 if (isl_basic_map_is_empty(part))
3266 isl_basic_map_free(part);
3267 else
3268 result = isl_map_add_basic_map(result, part);
3269 if (!result)
3270 goto error;
3272 isl_map_free(map1);
3273 isl_map_free(map2);
3274 return result;
3275 error:
3276 isl_map_free(map1);
3277 isl_map_free(map2);
3278 return NULL;
3281 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3282 __isl_take isl_set *set2)
3284 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3287 /* Given a basic map A -> f(A), construct A -> -f(A).
3289 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3291 int i, j;
3292 unsigned off, n;
3294 bmap = isl_basic_map_cow(bmap);
3295 if (!bmap)
3296 return NULL;
3298 n = isl_basic_map_dim(bmap, isl_dim_out);
3299 off = isl_basic_map_offset(bmap, isl_dim_out);
3300 for (i = 0; i < bmap->n_eq; ++i)
3301 for (j = 0; j < n; ++j)
3302 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3303 for (i = 0; i < bmap->n_ineq; ++i)
3304 for (j = 0; j < n; ++j)
3305 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3306 for (i = 0; i < bmap->n_div; ++i)
3307 for (j = 0; j < n; ++j)
3308 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3309 bmap = isl_basic_map_gauss(bmap, NULL);
3310 return isl_basic_map_finalize(bmap);
3313 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3315 return isl_basic_map_neg(bset);
3318 /* Given a map A -> f(A), construct A -> -f(A).
3320 struct isl_map *isl_map_neg(struct isl_map *map)
3322 int i;
3324 map = isl_map_cow(map);
3325 if (!map)
3326 return NULL;
3328 for (i = 0; i < map->n; ++i) {
3329 map->p[i] = isl_basic_map_neg(map->p[i]);
3330 if (!map->p[i])
3331 goto error;
3334 return map;
3335 error:
3336 isl_map_free(map);
3337 return NULL;
3340 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3342 return (isl_set *)isl_map_neg((isl_map *)set);
3345 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3346 * A -> floor(f(A)/d).
3348 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3349 isl_int d)
3351 unsigned n_in, n_out, nparam, total, pos;
3352 struct isl_basic_map *result = NULL;
3353 struct isl_dim_map *dim_map;
3354 int i;
3356 if (!bmap)
3357 return NULL;
3359 nparam = isl_basic_map_n_param(bmap);
3360 n_in = isl_basic_map_n_in(bmap);
3361 n_out = isl_basic_map_n_out(bmap);
3363 total = nparam + n_in + n_out + bmap->n_div + n_out;
3364 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3365 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3366 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3367 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3368 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3370 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3371 bmap->n_div + n_out,
3372 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3373 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3374 result = add_divs(result, n_out);
3375 for (i = 0; i < n_out; ++i) {
3376 int j;
3377 j = isl_basic_map_alloc_inequality(result);
3378 if (j < 0)
3379 goto error;
3380 isl_seq_clr(result->ineq[j], 1+total);
3381 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3382 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3383 j = isl_basic_map_alloc_inequality(result);
3384 if (j < 0)
3385 goto error;
3386 isl_seq_clr(result->ineq[j], 1+total);
3387 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3388 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3389 isl_int_sub_ui(result->ineq[j][0], d, 1);
3392 result = isl_basic_map_simplify(result);
3393 return isl_basic_map_finalize(result);
3394 error:
3395 isl_basic_map_free(result);
3396 return NULL;
3399 /* Given a map A -> f(A) and an integer d, construct a map
3400 * A -> floor(f(A)/d).
3402 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3404 int i;
3406 map = isl_map_cow(map);
3407 if (!map)
3408 return NULL;
3410 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3411 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3412 for (i = 0; i < map->n; ++i) {
3413 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3414 if (!map->p[i])
3415 goto error;
3418 return map;
3419 error:
3420 isl_map_free(map);
3421 return NULL;
3424 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3426 int i;
3427 unsigned nparam;
3428 unsigned n_in;
3430 i = isl_basic_map_alloc_equality(bmap);
3431 if (i < 0)
3432 goto error;
3433 nparam = isl_basic_map_n_param(bmap);
3434 n_in = isl_basic_map_n_in(bmap);
3435 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3436 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3437 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3438 return isl_basic_map_finalize(bmap);
3439 error:
3440 isl_basic_map_free(bmap);
3441 return NULL;
3444 /* Add a constraints to "bmap" expressing i_pos < o_pos
3446 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3448 int i;
3449 unsigned nparam;
3450 unsigned n_in;
3452 i = isl_basic_map_alloc_inequality(bmap);
3453 if (i < 0)
3454 goto error;
3455 nparam = isl_basic_map_n_param(bmap);
3456 n_in = isl_basic_map_n_in(bmap);
3457 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3458 isl_int_set_si(bmap->ineq[i][0], -1);
3459 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3460 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3461 return isl_basic_map_finalize(bmap);
3462 error:
3463 isl_basic_map_free(bmap);
3464 return NULL;
3467 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3469 static __isl_give isl_basic_map *var_less_or_equal(
3470 __isl_take isl_basic_map *bmap, unsigned pos)
3472 int i;
3473 unsigned nparam;
3474 unsigned n_in;
3476 i = isl_basic_map_alloc_inequality(bmap);
3477 if (i < 0)
3478 goto error;
3479 nparam = isl_basic_map_n_param(bmap);
3480 n_in = isl_basic_map_n_in(bmap);
3481 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3482 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3483 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3484 return isl_basic_map_finalize(bmap);
3485 error:
3486 isl_basic_map_free(bmap);
3487 return NULL;
3490 /* Add a constraints to "bmap" expressing i_pos > o_pos
3492 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3494 int i;
3495 unsigned nparam;
3496 unsigned n_in;
3498 i = isl_basic_map_alloc_inequality(bmap);
3499 if (i < 0)
3500 goto error;
3501 nparam = isl_basic_map_n_param(bmap);
3502 n_in = isl_basic_map_n_in(bmap);
3503 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3504 isl_int_set_si(bmap->ineq[i][0], -1);
3505 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3506 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3507 return isl_basic_map_finalize(bmap);
3508 error:
3509 isl_basic_map_free(bmap);
3510 return NULL;
3513 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3515 static __isl_give isl_basic_map *var_more_or_equal(
3516 __isl_take isl_basic_map *bmap, unsigned pos)
3518 int i;
3519 unsigned nparam;
3520 unsigned n_in;
3522 i = isl_basic_map_alloc_inequality(bmap);
3523 if (i < 0)
3524 goto error;
3525 nparam = isl_basic_map_n_param(bmap);
3526 n_in = isl_basic_map_n_in(bmap);
3527 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3528 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3529 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3530 return isl_basic_map_finalize(bmap);
3531 error:
3532 isl_basic_map_free(bmap);
3533 return NULL;
3536 __isl_give isl_basic_map *isl_basic_map_equal(
3537 __isl_take isl_space *dim, unsigned n_equal)
3539 int i;
3540 struct isl_basic_map *bmap;
3541 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3542 if (!bmap)
3543 return NULL;
3544 for (i = 0; i < n_equal && bmap; ++i)
3545 bmap = var_equal(bmap, i);
3546 return isl_basic_map_finalize(bmap);
3549 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3551 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3552 unsigned pos)
3554 int i;
3555 struct isl_basic_map *bmap;
3556 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3557 if (!bmap)
3558 return NULL;
3559 for (i = 0; i < pos && bmap; ++i)
3560 bmap = var_equal(bmap, i);
3561 if (bmap)
3562 bmap = var_less(bmap, pos);
3563 return isl_basic_map_finalize(bmap);
3566 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3568 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3569 __isl_take isl_space *dim, unsigned pos)
3571 int i;
3572 isl_basic_map *bmap;
3574 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3575 for (i = 0; i < pos; ++i)
3576 bmap = var_equal(bmap, i);
3577 bmap = var_less_or_equal(bmap, pos);
3578 return isl_basic_map_finalize(bmap);
3581 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3583 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3584 unsigned pos)
3586 int i;
3587 struct isl_basic_map *bmap;
3588 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3589 if (!bmap)
3590 return NULL;
3591 for (i = 0; i < pos && bmap; ++i)
3592 bmap = var_equal(bmap, i);
3593 if (bmap)
3594 bmap = var_more(bmap, pos);
3595 return isl_basic_map_finalize(bmap);
3598 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3600 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3601 __isl_take isl_space *dim, unsigned pos)
3603 int i;
3604 isl_basic_map *bmap;
3606 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3607 for (i = 0; i < pos; ++i)
3608 bmap = var_equal(bmap, i);
3609 bmap = var_more_or_equal(bmap, pos);
3610 return isl_basic_map_finalize(bmap);
3613 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3614 unsigned n, int equal)
3616 struct isl_map *map;
3617 int i;
3619 if (n == 0 && equal)
3620 return isl_map_universe(dims);
3622 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3624 for (i = 0; i + 1 < n; ++i)
3625 map = isl_map_add_basic_map(map,
3626 isl_basic_map_less_at(isl_space_copy(dims), i));
3627 if (n > 0) {
3628 if (equal)
3629 map = isl_map_add_basic_map(map,
3630 isl_basic_map_less_or_equal_at(dims, n - 1));
3631 else
3632 map = isl_map_add_basic_map(map,
3633 isl_basic_map_less_at(dims, n - 1));
3634 } else
3635 isl_space_free(dims);
3637 return map;
3640 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3642 if (!dims)
3643 return NULL;
3644 return map_lex_lte_first(dims, dims->n_out, equal);
3647 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3649 return map_lex_lte_first(dim, n, 0);
3652 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3654 return map_lex_lte_first(dim, n, 1);
3657 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3659 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3662 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3664 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3667 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3668 unsigned n, int equal)
3670 struct isl_map *map;
3671 int i;
3673 if (n == 0 && equal)
3674 return isl_map_universe(dims);
3676 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3678 for (i = 0; i + 1 < n; ++i)
3679 map = isl_map_add_basic_map(map,
3680 isl_basic_map_more_at(isl_space_copy(dims), i));
3681 if (n > 0) {
3682 if (equal)
3683 map = isl_map_add_basic_map(map,
3684 isl_basic_map_more_or_equal_at(dims, n - 1));
3685 else
3686 map = isl_map_add_basic_map(map,
3687 isl_basic_map_more_at(dims, n - 1));
3688 } else
3689 isl_space_free(dims);
3691 return map;
3694 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3696 if (!dims)
3697 return NULL;
3698 return map_lex_gte_first(dims, dims->n_out, equal);
3701 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3703 return map_lex_gte_first(dim, n, 0);
3706 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3708 return map_lex_gte_first(dim, n, 1);
3711 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3713 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3716 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
3718 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
3721 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3722 __isl_take isl_set *set2)
3724 isl_map *map;
3725 map = isl_map_lex_le(isl_set_get_space(set1));
3726 map = isl_map_intersect_domain(map, set1);
3727 map = isl_map_intersect_range(map, set2);
3728 return map;
3731 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3732 __isl_take isl_set *set2)
3734 isl_map *map;
3735 map = isl_map_lex_lt(isl_set_get_space(set1));
3736 map = isl_map_intersect_domain(map, set1);
3737 map = isl_map_intersect_range(map, set2);
3738 return map;
3741 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3742 __isl_take isl_set *set2)
3744 isl_map *map;
3745 map = isl_map_lex_ge(isl_set_get_space(set1));
3746 map = isl_map_intersect_domain(map, set1);
3747 map = isl_map_intersect_range(map, set2);
3748 return map;
3751 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3752 __isl_take isl_set *set2)
3754 isl_map *map;
3755 map = isl_map_lex_gt(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_map_lex_le_map(__isl_take isl_map *map1,
3762 __isl_take isl_map *map2)
3764 isl_map *map;
3765 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
3766 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3767 map = isl_map_apply_range(map, isl_map_reverse(map2));
3768 return map;
3771 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3772 __isl_take isl_map *map2)
3774 isl_map *map;
3775 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
3776 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3777 map = isl_map_apply_range(map, isl_map_reverse(map2));
3778 return map;
3781 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3782 __isl_take isl_map *map2)
3784 isl_map *map;
3785 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
3786 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3787 map = isl_map_apply_range(map, isl_map_reverse(map2));
3788 return map;
3791 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3792 __isl_take isl_map *map2)
3794 isl_map *map;
3795 map = isl_map_lex_gt(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_basic_map *isl_basic_map_from_basic_set(
3802 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
3804 struct isl_basic_map *bmap;
3806 bset = isl_basic_set_cow(bset);
3807 if (!bset || !dim)
3808 goto error;
3810 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
3811 isl_space_free(bset->dim);
3812 bmap = (struct isl_basic_map *) bset;
3813 bmap->dim = dim;
3814 return isl_basic_map_finalize(bmap);
3815 error:
3816 isl_basic_set_free(bset);
3817 isl_space_free(dim);
3818 return NULL;
3821 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3823 if (!bmap)
3824 goto error;
3825 if (bmap->dim->n_in == 0)
3826 return (struct isl_basic_set *)bmap;
3827 bmap = isl_basic_map_cow(bmap);
3828 if (!bmap)
3829 goto error;
3830 bmap->dim = isl_space_as_set_space(bmap->dim);
3831 if (!bmap->dim)
3832 goto error;
3833 bmap = isl_basic_map_finalize(bmap);
3834 return (struct isl_basic_set *)bmap;
3835 error:
3836 isl_basic_map_free(bmap);
3837 return NULL;
3840 /* For a div d = floor(f/m), add the constraints
3842 * f - m d >= 0
3843 * -(f-(n-1)) + m d >= 0
3845 * Note that the second constraint is the negation of
3847 * f - m d >= n
3849 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3850 unsigned pos, isl_int *div)
3852 int i, j;
3853 unsigned total = isl_basic_map_total_dim(bmap);
3855 i = isl_basic_map_alloc_inequality(bmap);
3856 if (i < 0)
3857 return -1;
3858 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3859 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3861 j = isl_basic_map_alloc_inequality(bmap);
3862 if (j < 0)
3863 return -1;
3864 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3865 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3866 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3867 return j;
3870 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3871 unsigned pos, isl_int *div)
3873 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3874 pos, div);
3877 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3879 unsigned total = isl_basic_map_total_dim(bmap);
3880 unsigned div_pos = total - bmap->n_div + div;
3882 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3883 bmap->div[div]);
3886 struct isl_basic_set *isl_basic_map_underlying_set(
3887 struct isl_basic_map *bmap)
3889 if (!bmap)
3890 goto error;
3891 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3892 bmap->n_div == 0 &&
3893 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
3894 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
3895 return (struct isl_basic_set *)bmap;
3896 bmap = isl_basic_map_cow(bmap);
3897 if (!bmap)
3898 goto error;
3899 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
3900 if (!bmap->dim)
3901 goto error;
3902 bmap->extra -= bmap->n_div;
3903 bmap->n_div = 0;
3904 bmap = isl_basic_map_finalize(bmap);
3905 return (struct isl_basic_set *)bmap;
3906 error:
3907 return NULL;
3910 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3911 __isl_take isl_basic_set *bset)
3913 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3916 struct isl_basic_map *isl_basic_map_overlying_set(
3917 struct isl_basic_set *bset, struct isl_basic_map *like)
3919 struct isl_basic_map *bmap;
3920 struct isl_ctx *ctx;
3921 unsigned total;
3922 int i;
3924 if (!bset || !like)
3925 goto error;
3926 ctx = bset->ctx;
3927 isl_assert(ctx, bset->n_div == 0, goto error);
3928 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3929 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3930 goto error);
3931 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
3932 isl_basic_map_free(like);
3933 return (struct isl_basic_map *)bset;
3935 bset = isl_basic_set_cow(bset);
3936 if (!bset)
3937 goto error;
3938 total = bset->dim->n_out + bset->extra;
3939 bmap = (struct isl_basic_map *)bset;
3940 isl_space_free(bmap->dim);
3941 bmap->dim = isl_space_copy(like->dim);
3942 if (!bmap->dim)
3943 goto error;
3944 bmap->n_div = like->n_div;
3945 bmap->extra += like->n_div;
3946 if (bmap->extra) {
3947 unsigned ltotal;
3948 isl_int **div;
3949 ltotal = total - bmap->extra + like->extra;
3950 if (ltotal > total)
3951 ltotal = total;
3952 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3953 bmap->extra * (1 + 1 + total));
3954 if (isl_blk_is_error(bmap->block2))
3955 goto error;
3956 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3957 if (!div)
3958 goto error;
3959 bmap->div = div;
3960 for (i = 0; i < bmap->extra; ++i)
3961 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3962 for (i = 0; i < like->n_div; ++i) {
3963 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3964 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3966 bmap = isl_basic_map_extend_constraints(bmap,
3967 0, 2 * like->n_div);
3968 for (i = 0; i < like->n_div; ++i) {
3969 if (isl_int_is_zero(bmap->div[i][0]))
3970 continue;
3971 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3972 goto error;
3975 isl_basic_map_free(like);
3976 bmap = isl_basic_map_simplify(bmap);
3977 bmap = isl_basic_map_finalize(bmap);
3978 return bmap;
3979 error:
3980 isl_basic_map_free(like);
3981 isl_basic_set_free(bset);
3982 return NULL;
3985 struct isl_basic_set *isl_basic_set_from_underlying_set(
3986 struct isl_basic_set *bset, struct isl_basic_set *like)
3988 return (struct isl_basic_set *)
3989 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3992 struct isl_set *isl_set_from_underlying_set(
3993 struct isl_set *set, struct isl_basic_set *like)
3995 int i;
3997 if (!set || !like)
3998 goto error;
3999 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4000 goto error);
4001 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4002 isl_basic_set_free(like);
4003 return set;
4005 set = isl_set_cow(set);
4006 if (!set)
4007 goto error;
4008 for (i = 0; i < set->n; ++i) {
4009 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4010 isl_basic_set_copy(like));
4011 if (!set->p[i])
4012 goto error;
4014 isl_space_free(set->dim);
4015 set->dim = isl_space_copy(like->dim);
4016 if (!set->dim)
4017 goto error;
4018 isl_basic_set_free(like);
4019 return set;
4020 error:
4021 isl_basic_set_free(like);
4022 isl_set_free(set);
4023 return NULL;
4026 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4028 int i;
4030 map = isl_map_cow(map);
4031 if (!map)
4032 return NULL;
4033 map->dim = isl_space_cow(map->dim);
4034 if (!map->dim)
4035 goto error;
4037 for (i = 1; i < map->n; ++i)
4038 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4039 goto error);
4040 for (i = 0; i < map->n; ++i) {
4041 map->p[i] = (struct isl_basic_map *)
4042 isl_basic_map_underlying_set(map->p[i]);
4043 if (!map->p[i])
4044 goto error;
4046 if (map->n == 0)
4047 map->dim = isl_space_underlying(map->dim, 0);
4048 else {
4049 isl_space_free(map->dim);
4050 map->dim = isl_space_copy(map->p[0]->dim);
4052 if (!map->dim)
4053 goto error;
4054 return (struct isl_set *)map;
4055 error:
4056 isl_map_free(map);
4057 return NULL;
4060 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4062 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4065 __isl_give isl_basic_map *isl_basic_map_reset_space(
4066 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4068 bmap = isl_basic_map_cow(bmap);
4069 if (!bmap || !dim)
4070 goto error;
4072 isl_space_free(bmap->dim);
4073 bmap->dim = dim;
4075 bmap = isl_basic_map_finalize(bmap);
4077 return bmap;
4078 error:
4079 isl_basic_map_free(bmap);
4080 isl_space_free(dim);
4081 return NULL;
4084 __isl_give isl_basic_set *isl_basic_set_reset_space(
4085 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4087 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4088 dim);
4091 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4092 __isl_take isl_space *dim)
4094 int i;
4096 map = isl_map_cow(map);
4097 if (!map || !dim)
4098 goto error;
4100 for (i = 0; i < map->n; ++i) {
4101 map->p[i] = isl_basic_map_reset_space(map->p[i],
4102 isl_space_copy(dim));
4103 if (!map->p[i])
4104 goto error;
4106 isl_space_free(map->dim);
4107 map->dim = dim;
4109 return map;
4110 error:
4111 isl_map_free(map);
4112 isl_space_free(dim);
4113 return NULL;
4116 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4117 __isl_take isl_space *dim)
4119 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4122 /* Compute the parameter domain of the given basic set.
4124 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4126 isl_space *space;
4127 unsigned n;
4129 if (isl_basic_set_is_params(bset))
4130 return bset;
4132 n = isl_basic_set_dim(bset, isl_dim_set);
4133 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4134 space = isl_basic_set_get_space(bset);
4135 space = isl_space_params(space);
4136 bset = isl_basic_set_reset_space(bset, space);
4137 return bset;
4140 /* Compute the parameter domain of the given set.
4142 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4144 isl_space *space;
4145 unsigned n;
4147 if (isl_set_is_params(set))
4148 return set;
4150 n = isl_set_dim(set, isl_dim_set);
4151 set = isl_set_project_out(set, isl_dim_set, 0, n);
4152 space = isl_set_get_space(set);
4153 space = isl_space_params(space);
4154 set = isl_set_reset_space(set, space);
4155 return set;
4158 /* Construct a zero-dimensional set with the given parameter domain.
4160 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4162 isl_space *space;
4163 space = isl_set_get_space(set);
4164 space = isl_space_set_from_params(space);
4165 set = isl_set_reset_space(set, space);
4166 return set;
4169 /* Compute the parameter domain of the given map.
4171 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4173 isl_space *space;
4174 unsigned n;
4176 n = isl_map_dim(map, isl_dim_in);
4177 map = isl_map_project_out(map, isl_dim_in, 0, n);
4178 n = isl_map_dim(map, isl_dim_out);
4179 map = isl_map_project_out(map, isl_dim_out, 0, n);
4180 space = isl_map_get_space(map);
4181 space = isl_space_params(space);
4182 map = isl_map_reset_space(map, space);
4183 return map;
4186 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4188 isl_space *dim;
4189 struct isl_basic_set *domain;
4190 unsigned n_in;
4191 unsigned n_out;
4193 if (!bmap)
4194 return NULL;
4195 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4197 n_in = isl_basic_map_n_in(bmap);
4198 n_out = isl_basic_map_n_out(bmap);
4199 domain = isl_basic_set_from_basic_map(bmap);
4200 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4202 domain = isl_basic_set_reset_space(domain, dim);
4204 return domain;
4207 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4209 if (!bmap)
4210 return -1;
4211 return isl_space_may_be_set(bmap->dim);
4214 /* Is this basic map actually a set?
4215 * Users should never call this function. Outside of isl,
4216 * the type should indicate whether something is a set or a map.
4218 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4220 if (!bmap)
4221 return -1;
4222 return isl_space_is_set(bmap->dim);
4225 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4227 if (!bmap)
4228 return NULL;
4229 if (isl_basic_map_is_set(bmap))
4230 return bmap;
4231 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4234 __isl_give isl_basic_map *isl_basic_map_domain_map(
4235 __isl_take isl_basic_map *bmap)
4237 int i, k;
4238 isl_space *dim;
4239 isl_basic_map *domain;
4240 int nparam, n_in, n_out;
4241 unsigned total;
4243 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4244 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4245 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4247 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4248 domain = isl_basic_map_universe(dim);
4250 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4251 bmap = isl_basic_map_apply_range(bmap, domain);
4252 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4254 total = isl_basic_map_total_dim(bmap);
4256 for (i = 0; i < n_in; ++i) {
4257 k = isl_basic_map_alloc_equality(bmap);
4258 if (k < 0)
4259 goto error;
4260 isl_seq_clr(bmap->eq[k], 1 + total);
4261 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4262 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4265 bmap = isl_basic_map_gauss(bmap, NULL);
4266 return isl_basic_map_finalize(bmap);
4267 error:
4268 isl_basic_map_free(bmap);
4269 return NULL;
4272 __isl_give isl_basic_map *isl_basic_map_range_map(
4273 __isl_take isl_basic_map *bmap)
4275 int i, k;
4276 isl_space *dim;
4277 isl_basic_map *range;
4278 int nparam, n_in, n_out;
4279 unsigned total;
4281 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4282 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4283 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4285 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4286 range = isl_basic_map_universe(dim);
4288 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4289 bmap = isl_basic_map_apply_range(bmap, range);
4290 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4292 total = isl_basic_map_total_dim(bmap);
4294 for (i = 0; i < n_out; ++i) {
4295 k = isl_basic_map_alloc_equality(bmap);
4296 if (k < 0)
4297 goto error;
4298 isl_seq_clr(bmap->eq[k], 1 + total);
4299 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4300 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4303 bmap = isl_basic_map_gauss(bmap, NULL);
4304 return isl_basic_map_finalize(bmap);
4305 error:
4306 isl_basic_map_free(bmap);
4307 return NULL;
4310 int isl_map_may_be_set(__isl_keep isl_map *map)
4312 if (!map)
4313 return -1;
4314 return isl_space_may_be_set(map->dim);
4317 /* Is this map actually a set?
4318 * Users should never call this function. Outside of isl,
4319 * the type should indicate whether something is a set or a map.
4321 int isl_map_is_set(__isl_keep isl_map *map)
4323 if (!map)
4324 return -1;
4325 return isl_space_is_set(map->dim);
4328 struct isl_set *isl_map_range(struct isl_map *map)
4330 int i;
4331 struct isl_set *set;
4333 if (!map)
4334 goto error;
4335 if (isl_map_is_set(map))
4336 return (isl_set *)map;
4338 map = isl_map_cow(map);
4339 if (!map)
4340 goto error;
4342 set = (struct isl_set *) map;
4343 set->dim = isl_space_range(set->dim);
4344 if (!set->dim)
4345 goto error;
4346 for (i = 0; i < map->n; ++i) {
4347 set->p[i] = isl_basic_map_range(map->p[i]);
4348 if (!set->p[i])
4349 goto error;
4351 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4352 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4353 return set;
4354 error:
4355 isl_map_free(map);
4356 return NULL;
4359 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4361 int i;
4362 isl_space *domain_dim;
4364 map = isl_map_cow(map);
4365 if (!map)
4366 return NULL;
4368 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4369 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4370 map->dim = isl_space_join(map->dim, domain_dim);
4371 if (!map->dim)
4372 goto error;
4373 for (i = 0; i < map->n; ++i) {
4374 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4375 if (!map->p[i])
4376 goto error;
4378 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4379 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4380 return map;
4381 error:
4382 isl_map_free(map);
4383 return NULL;
4386 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4388 int i;
4389 isl_space *range_dim;
4391 map = isl_map_cow(map);
4392 if (!map)
4393 return NULL;
4395 range_dim = isl_space_range(isl_map_get_space(map));
4396 range_dim = isl_space_from_range(range_dim);
4397 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4398 map->dim = isl_space_join(map->dim, range_dim);
4399 if (!map->dim)
4400 goto error;
4401 for (i = 0; i < map->n; ++i) {
4402 map->p[i] = isl_basic_map_range_map(map->p[i]);
4403 if (!map->p[i])
4404 goto error;
4406 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4407 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4408 return map;
4409 error:
4410 isl_map_free(map);
4411 return NULL;
4414 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4415 __isl_take isl_space *dim)
4417 int i;
4418 struct isl_map *map = NULL;
4420 set = isl_set_cow(set);
4421 if (!set || !dim)
4422 goto error;
4423 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4424 map = (struct isl_map *)set;
4425 for (i = 0; i < set->n; ++i) {
4426 map->p[i] = isl_basic_map_from_basic_set(
4427 set->p[i], isl_space_copy(dim));
4428 if (!map->p[i])
4429 goto error;
4431 isl_space_free(map->dim);
4432 map->dim = dim;
4433 return map;
4434 error:
4435 isl_space_free(dim);
4436 isl_set_free(set);
4437 return NULL;
4440 __isl_give isl_basic_map *isl_basic_map_from_domain(
4441 __isl_take isl_basic_set *bset)
4443 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4446 __isl_give isl_basic_map *isl_basic_map_from_range(
4447 __isl_take isl_basic_set *bset)
4449 isl_space *space;
4450 space = isl_basic_set_get_space(bset);
4451 space = isl_space_from_range(space);
4452 bset = isl_basic_set_reset_space(bset, space);
4453 return (isl_basic_map *)bset;
4456 struct isl_map *isl_map_from_range(struct isl_set *set)
4458 isl_space *space;
4459 space = isl_set_get_space(set);
4460 space = isl_space_from_range(space);
4461 set = isl_set_reset_space(set, space);
4462 return (struct isl_map *)set;
4465 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4467 return isl_map_reverse(isl_map_from_range(set));
4470 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4471 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4473 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4476 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4477 __isl_take isl_set *range)
4479 return isl_map_apply_range(isl_map_reverse(domain), range);
4482 struct isl_set *isl_set_from_map(struct isl_map *map)
4484 int i;
4485 struct isl_set *set = NULL;
4487 if (!map)
4488 return NULL;
4489 map = isl_map_cow(map);
4490 if (!map)
4491 return NULL;
4492 map->dim = isl_space_as_set_space(map->dim);
4493 if (!map->dim)
4494 goto error;
4495 set = (struct isl_set *)map;
4496 for (i = 0; i < map->n; ++i) {
4497 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4498 if (!set->p[i])
4499 goto error;
4501 return set;
4502 error:
4503 isl_map_free(map);
4504 return NULL;
4507 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4508 unsigned flags)
4510 struct isl_map *map;
4512 if (!dim)
4513 return NULL;
4514 if (n < 0)
4515 isl_die(dim->ctx, isl_error_internal,
4516 "negative number of basic maps", goto error);
4517 map = isl_alloc(dim->ctx, struct isl_map,
4518 sizeof(struct isl_map) +
4519 (n - 1) * sizeof(struct isl_basic_map *));
4520 if (!map)
4521 goto error;
4523 map->ctx = dim->ctx;
4524 isl_ctx_ref(map->ctx);
4525 map->ref = 1;
4526 map->size = n;
4527 map->n = 0;
4528 map->dim = dim;
4529 map->flags = flags;
4530 return map;
4531 error:
4532 isl_space_free(dim);
4533 return NULL;
4536 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4537 unsigned nparam, unsigned in, unsigned out, int n,
4538 unsigned flags)
4540 struct isl_map *map;
4541 isl_space *dims;
4543 dims = isl_space_alloc(ctx, nparam, in, out);
4544 if (!dims)
4545 return NULL;
4547 map = isl_map_alloc_space(dims, n, flags);
4548 return map;
4551 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4553 struct isl_basic_map *bmap;
4554 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4555 bmap = isl_basic_map_set_to_empty(bmap);
4556 return bmap;
4559 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4561 struct isl_basic_set *bset;
4562 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4563 bset = isl_basic_set_set_to_empty(bset);
4564 return bset;
4567 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4569 struct isl_basic_map *bmap;
4570 if (!model)
4571 return NULL;
4572 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4573 bmap = isl_basic_map_set_to_empty(bmap);
4574 return bmap;
4577 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4579 struct isl_basic_map *bmap;
4580 if (!model)
4581 return NULL;
4582 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4583 bmap = isl_basic_map_set_to_empty(bmap);
4584 return bmap;
4587 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4589 struct isl_basic_set *bset;
4590 if (!model)
4591 return NULL;
4592 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4593 bset = isl_basic_set_set_to_empty(bset);
4594 return bset;
4597 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4599 struct isl_basic_map *bmap;
4600 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4601 bmap = isl_basic_map_finalize(bmap);
4602 return bmap;
4605 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4607 struct isl_basic_set *bset;
4608 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4609 bset = isl_basic_set_finalize(bset);
4610 return bset;
4613 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4615 int i;
4616 unsigned total = isl_space_dim(dim, isl_dim_all);
4617 isl_basic_map *bmap;
4619 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4620 for (i = 0; i < total; ++i) {
4621 int k = isl_basic_map_alloc_inequality(bmap);
4622 if (k < 0)
4623 goto error;
4624 isl_seq_clr(bmap->ineq[k], 1 + total);
4625 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4627 return bmap;
4628 error:
4629 isl_basic_map_free(bmap);
4630 return NULL;
4633 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4635 return isl_basic_map_nat_universe(dim);
4638 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4640 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4643 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4645 return isl_map_nat_universe(dim);
4648 __isl_give isl_basic_map *isl_basic_map_universe_like(
4649 __isl_keep isl_basic_map *model)
4651 if (!model)
4652 return NULL;
4653 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4656 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4658 if (!model)
4659 return NULL;
4660 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4663 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4664 __isl_keep isl_set *model)
4666 if (!model)
4667 return NULL;
4668 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4671 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4673 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4676 struct isl_map *isl_map_empty_like(struct isl_map *model)
4678 if (!model)
4679 return NULL;
4680 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4683 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4685 if (!model)
4686 return NULL;
4687 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4690 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4692 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4695 struct isl_set *isl_set_empty_like(struct isl_set *model)
4697 if (!model)
4698 return NULL;
4699 return isl_set_empty(isl_space_copy(model->dim));
4702 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4704 struct isl_map *map;
4705 if (!dim)
4706 return NULL;
4707 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4708 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4709 return map;
4712 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4714 struct isl_set *set;
4715 if (!dim)
4716 return NULL;
4717 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4718 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4719 return set;
4722 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4724 if (!model)
4725 return NULL;
4726 return isl_set_universe(isl_space_copy(model->dim));
4729 struct isl_map *isl_map_dup(struct isl_map *map)
4731 int i;
4732 struct isl_map *dup;
4734 if (!map)
4735 return NULL;
4736 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
4737 for (i = 0; i < map->n; ++i)
4738 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4739 return dup;
4742 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4743 __isl_take isl_basic_map *bmap)
4745 if (!bmap || !map)
4746 goto error;
4747 if (isl_basic_map_plain_is_empty(bmap)) {
4748 isl_basic_map_free(bmap);
4749 return map;
4751 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
4752 isl_assert(map->ctx, map->n < map->size, goto error);
4753 map->p[map->n] = bmap;
4754 map->n++;
4755 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4756 return map;
4757 error:
4758 if (map)
4759 isl_map_free(map);
4760 if (bmap)
4761 isl_basic_map_free(bmap);
4762 return NULL;
4765 void isl_map_free(struct isl_map *map)
4767 int i;
4769 if (!map)
4770 return;
4772 if (--map->ref > 0)
4773 return;
4775 isl_ctx_deref(map->ctx);
4776 for (i = 0; i < map->n; ++i)
4777 isl_basic_map_free(map->p[i]);
4778 isl_space_free(map->dim);
4779 free(map);
4782 struct isl_map *isl_map_extend(struct isl_map *base,
4783 unsigned nparam, unsigned n_in, unsigned n_out)
4785 int i;
4787 base = isl_map_cow(base);
4788 if (!base)
4789 return NULL;
4791 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
4792 if (!base->dim)
4793 goto error;
4794 for (i = 0; i < base->n; ++i) {
4795 base->p[i] = isl_basic_map_extend_space(base->p[i],
4796 isl_space_copy(base->dim), 0, 0, 0);
4797 if (!base->p[i])
4798 goto error;
4800 return base;
4801 error:
4802 isl_map_free(base);
4803 return NULL;
4806 struct isl_set *isl_set_extend(struct isl_set *base,
4807 unsigned nparam, unsigned dim)
4809 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4810 nparam, 0, dim);
4813 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4814 struct isl_basic_map *bmap, unsigned pos, int value)
4816 int j;
4818 bmap = isl_basic_map_cow(bmap);
4819 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4820 j = isl_basic_map_alloc_equality(bmap);
4821 if (j < 0)
4822 goto error;
4823 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4824 isl_int_set_si(bmap->eq[j][pos], -1);
4825 isl_int_set_si(bmap->eq[j][0], value);
4826 bmap = isl_basic_map_simplify(bmap);
4827 return isl_basic_map_finalize(bmap);
4828 error:
4829 isl_basic_map_free(bmap);
4830 return NULL;
4833 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4834 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4836 int j;
4838 bmap = isl_basic_map_cow(bmap);
4839 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4840 j = isl_basic_map_alloc_equality(bmap);
4841 if (j < 0)
4842 goto error;
4843 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4844 isl_int_set_si(bmap->eq[j][pos], -1);
4845 isl_int_set(bmap->eq[j][0], value);
4846 bmap = isl_basic_map_simplify(bmap);
4847 return isl_basic_map_finalize(bmap);
4848 error:
4849 isl_basic_map_free(bmap);
4850 return NULL;
4853 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4854 enum isl_dim_type type, unsigned pos, int value)
4856 if (!bmap)
4857 return NULL;
4858 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4859 return isl_basic_map_fix_pos_si(bmap,
4860 isl_basic_map_offset(bmap, type) + pos, value);
4861 error:
4862 isl_basic_map_free(bmap);
4863 return NULL;
4866 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4867 enum isl_dim_type type, unsigned pos, isl_int value)
4869 if (!bmap)
4870 return NULL;
4871 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4872 return isl_basic_map_fix_pos(bmap,
4873 isl_basic_map_offset(bmap, type) + pos, value);
4874 error:
4875 isl_basic_map_free(bmap);
4876 return NULL;
4879 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4880 enum isl_dim_type type, unsigned pos, int value)
4882 return (struct isl_basic_set *)
4883 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4884 type, pos, value);
4887 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4888 enum isl_dim_type type, unsigned pos, isl_int value)
4890 return (struct isl_basic_set *)
4891 isl_basic_map_fix((struct isl_basic_map *)bset,
4892 type, pos, value);
4895 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4896 unsigned input, int value)
4898 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4901 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4902 unsigned dim, int value)
4904 return (struct isl_basic_set *)
4905 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4906 isl_dim_set, dim, value);
4909 struct isl_map *isl_map_fix_si(struct isl_map *map,
4910 enum isl_dim_type type, unsigned pos, int value)
4912 int i;
4914 map = isl_map_cow(map);
4915 if (!map)
4916 return NULL;
4918 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4919 for (i = 0; i < map->n; ++i) {
4920 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4921 if (!map->p[i])
4922 goto error;
4924 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4925 return map;
4926 error:
4927 isl_map_free(map);
4928 return NULL;
4931 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4932 enum isl_dim_type type, unsigned pos, int value)
4934 return (struct isl_set *)
4935 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4938 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4939 enum isl_dim_type type, unsigned pos, isl_int value)
4941 int i;
4943 map = isl_map_cow(map);
4944 if (!map)
4945 return NULL;
4947 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4948 for (i = 0; i < map->n; ++i) {
4949 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4950 if (!map->p[i])
4951 goto error;
4953 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4954 return map;
4955 error:
4956 isl_map_free(map);
4957 return NULL;
4960 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4961 enum isl_dim_type type, unsigned pos, isl_int value)
4963 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4966 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4967 unsigned input, int value)
4969 return isl_map_fix_si(map, isl_dim_in, input, value);
4972 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4974 return (struct isl_set *)
4975 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4978 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4979 __isl_take isl_basic_map *bmap,
4980 enum isl_dim_type type, unsigned pos, int value)
4982 int j;
4984 if (!bmap)
4985 return NULL;
4986 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4987 pos += isl_basic_map_offset(bmap, type);
4988 bmap = isl_basic_map_cow(bmap);
4989 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4990 j = isl_basic_map_alloc_inequality(bmap);
4991 if (j < 0)
4992 goto error;
4993 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4994 isl_int_set_si(bmap->ineq[j][pos], 1);
4995 isl_int_set_si(bmap->ineq[j][0], -value);
4996 bmap = isl_basic_map_simplify(bmap);
4997 return isl_basic_map_finalize(bmap);
4998 error:
4999 isl_basic_map_free(bmap);
5000 return NULL;
5003 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5004 unsigned dim, isl_int value)
5006 int j;
5008 bset = isl_basic_set_cow(bset);
5009 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5010 j = isl_basic_set_alloc_inequality(bset);
5011 if (j < 0)
5012 goto error;
5013 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5014 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5015 isl_int_neg(bset->ineq[j][0], value);
5016 bset = isl_basic_set_simplify(bset);
5017 return isl_basic_set_finalize(bset);
5018 error:
5019 isl_basic_set_free(bset);
5020 return NULL;
5023 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5024 enum isl_dim_type type, unsigned pos, int value)
5026 int i;
5028 map = isl_map_cow(map);
5029 if (!map)
5030 return NULL;
5032 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5033 for (i = 0; i < map->n; ++i) {
5034 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
5035 type, pos, value);
5036 if (!map->p[i])
5037 goto error;
5039 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5040 return map;
5041 error:
5042 isl_map_free(map);
5043 return NULL;
5046 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5047 enum isl_dim_type type, unsigned pos, int value)
5049 return (struct isl_set *)
5050 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5053 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5054 isl_int value)
5056 int i;
5058 set = isl_set_cow(set);
5059 if (!set)
5060 return NULL;
5062 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5063 for (i = 0; i < set->n; ++i) {
5064 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5065 if (!set->p[i])
5066 goto error;
5068 return set;
5069 error:
5070 isl_set_free(set);
5071 return NULL;
5074 struct isl_map *isl_map_reverse(struct isl_map *map)
5076 int i;
5078 map = isl_map_cow(map);
5079 if (!map)
5080 return NULL;
5082 map->dim = isl_space_reverse(map->dim);
5083 if (!map->dim)
5084 goto error;
5085 for (i = 0; i < map->n; ++i) {
5086 map->p[i] = isl_basic_map_reverse(map->p[i]);
5087 if (!map->p[i])
5088 goto error;
5090 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5091 return map;
5092 error:
5093 isl_map_free(map);
5094 return NULL;
5097 static struct isl_map *isl_basic_map_partial_lexopt(
5098 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5099 struct isl_set **empty, int max)
5101 if (!bmap)
5102 goto error;
5103 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5104 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5105 else
5106 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5107 error:
5108 isl_basic_map_free(bmap);
5109 isl_basic_set_free(dom);
5110 if (empty)
5111 *empty = NULL;
5112 return NULL;
5115 struct isl_map *isl_basic_map_partial_lexmax(
5116 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5117 struct isl_set **empty)
5119 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5122 struct isl_map *isl_basic_map_partial_lexmin(
5123 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5124 struct isl_set **empty)
5126 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5129 struct isl_set *isl_basic_set_partial_lexmin(
5130 struct isl_basic_set *bset, struct isl_basic_set *dom,
5131 struct isl_set **empty)
5133 return (struct isl_set *)
5134 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5135 dom, empty);
5138 struct isl_set *isl_basic_set_partial_lexmax(
5139 struct isl_basic_set *bset, struct isl_basic_set *dom,
5140 struct isl_set **empty)
5142 return (struct isl_set *)
5143 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5144 dom, empty);
5147 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5148 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5149 __isl_give isl_set **empty)
5151 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5154 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5155 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5156 __isl_give isl_set **empty)
5158 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5161 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5162 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5163 __isl_give isl_set **empty)
5165 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5168 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5169 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5170 __isl_give isl_set **empty)
5172 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5175 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5176 __isl_take isl_basic_map *bmap, int max)
5178 isl_basic_set *dom = NULL;
5179 isl_space *dom_space;
5181 if (!bmap)
5182 goto error;
5183 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5184 dom = isl_basic_set_universe(dom_space);
5185 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5186 error:
5187 isl_basic_map_free(bmap);
5188 return NULL;
5191 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5192 __isl_take isl_basic_map *bmap)
5194 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5197 /* Given a basic map "bmap", compute the lexicographically minimal
5198 * (or maximal) image element for each domain element in dom.
5199 * Set *empty to those elements in dom that do not have an image element.
5201 * We first make sure the basic sets in dom are disjoint and then
5202 * simply collect the results over each of the basic sets separately.
5203 * We could probably improve the efficiency a bit by moving the union
5204 * domain down into the parametric integer programming.
5206 static __isl_give isl_map *basic_map_partial_lexopt(
5207 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
5208 __isl_give isl_set **empty, int max)
5210 int i;
5211 struct isl_map *res;
5213 dom = isl_set_make_disjoint(dom);
5214 if (!dom)
5215 goto error;
5217 if (isl_set_plain_is_empty(dom)) {
5218 res = isl_map_empty_like_basic_map(bmap);
5219 *empty = isl_set_empty_like(dom);
5220 isl_set_free(dom);
5221 isl_basic_map_free(bmap);
5222 return res;
5225 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5226 isl_basic_set_copy(dom->p[0]), empty, max);
5228 for (i = 1; i < dom->n; ++i) {
5229 struct isl_map *res_i;
5230 struct isl_set *empty_i;
5232 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5233 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5235 res = isl_map_union_disjoint(res, res_i);
5236 *empty = isl_set_union_disjoint(*empty, empty_i);
5239 isl_set_free(dom);
5240 isl_basic_map_free(bmap);
5241 return res;
5242 error:
5243 *empty = NULL;
5244 isl_set_free(dom);
5245 isl_basic_map_free(bmap);
5246 return NULL;
5249 /* Given a map "map", compute the lexicographically minimal
5250 * (or maximal) image element for each domain element in dom.
5251 * Set *empty to those elements in dom that do not have an image element.
5253 * We first compute the lexicographically minimal or maximal element
5254 * in the first basic map. This results in a partial solution "res"
5255 * and a subset "todo" of dom that still need to be handled.
5256 * We then consider each of the remaining maps in "map" and successively
5257 * improve both "res" and "todo".
5259 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5260 * Assume we are computing the lexicographical maximum.
5261 * We first compute the lexicographically maximal element in basic map i.
5262 * This results in a partial solution res_i and a subset todo_i.
5263 * Then we combine these results with those obtain for the first k basic maps
5264 * to obtain a result that is valid for the first k+1 basic maps.
5265 * In particular, the set where there is no solution is the set where
5266 * there is no solution for the first k basic maps and also no solution
5267 * for the ith basic map, i.e.,
5269 * todo^i = todo^k * todo_i
5271 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5272 * solutions, arbitrarily breaking ties in favor of res^k.
5273 * That is, when res^k(a) >= res_i(a), we pick res^k and
5274 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5275 * the lexicographic order.)
5276 * In practice, we compute
5278 * res^k * (res_i . "<=")
5280 * and
5282 * res_i * (res^k . "<")
5284 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5285 * where only one of res^k and res_i provides a solution and we simply pick
5286 * that one, i.e.,
5288 * res^k * todo_i
5289 * and
5290 * res_i * todo^k
5292 * Note that we only compute these intersections when dom(res^k) intersects
5293 * dom(res_i). Otherwise, the only effect of these intersections is to
5294 * potentially break up res^k and res_i into smaller pieces.
5295 * We want to avoid such splintering as much as possible.
5296 * In fact, an earlier implementation of this function would look for
5297 * better results in the domain of res^k and for extra results in todo^k,
5298 * but this would always result in a splintering according to todo^k,
5299 * even when the domain of basic map i is disjoint from the domains of
5300 * the previous basic maps.
5302 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5303 __isl_take isl_map *map, __isl_take isl_set *dom,
5304 __isl_give isl_set **empty, int max)
5306 int i;
5307 struct isl_map *res;
5308 struct isl_set *todo;
5310 if (!map || !dom)
5311 goto error;
5313 if (isl_map_plain_is_empty(map)) {
5314 if (empty)
5315 *empty = dom;
5316 else
5317 isl_set_free(dom);
5318 return map;
5321 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5322 isl_set_copy(dom), &todo, max);
5324 for (i = 1; i < map->n; ++i) {
5325 isl_map *lt, *le;
5326 isl_map *res_i;
5327 isl_set *todo_i;
5328 isl_space *dim = isl_space_range(isl_map_get_space(res));
5330 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5331 isl_set_copy(dom), &todo_i, max);
5333 if (max) {
5334 lt = isl_map_lex_lt(isl_space_copy(dim));
5335 le = isl_map_lex_le(dim);
5336 } else {
5337 lt = isl_map_lex_gt(isl_space_copy(dim));
5338 le = isl_map_lex_ge(dim);
5340 lt = isl_map_apply_range(isl_map_copy(res), lt);
5341 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5342 le = isl_map_apply_range(isl_map_copy(res_i), le);
5343 le = isl_map_intersect(le, isl_map_copy(res));
5345 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5346 res = isl_map_intersect_domain(res,
5347 isl_set_copy(todo_i));
5348 res_i = isl_map_intersect_domain(res_i,
5349 isl_set_copy(todo));
5352 res = isl_map_union_disjoint(res, res_i);
5353 res = isl_map_union_disjoint(res, lt);
5354 res = isl_map_union_disjoint(res, le);
5356 todo = isl_set_intersect(todo, todo_i);
5359 isl_set_free(dom);
5360 isl_map_free(map);
5362 if (empty)
5363 *empty = todo;
5364 else
5365 isl_set_free(todo);
5367 return res;
5368 error:
5369 if (empty)
5370 *empty = NULL;
5371 isl_set_free(dom);
5372 isl_map_free(map);
5373 return NULL;
5376 /* Given a map "map", compute the lexicographically minimal
5377 * (or maximal) image element for each domain element in dom.
5378 * Set *empty to those elements in dom that do not have an image element.
5380 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5382 static __isl_give isl_map *isl_map_partial_lexopt(
5383 __isl_take isl_map *map, __isl_take isl_set *dom,
5384 __isl_give isl_set **empty, int max)
5386 if (!map || !dom)
5387 goto error;
5388 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5389 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5390 if (!isl_space_has_named_params(map->dim) ||
5391 !isl_space_has_named_params(dom->dim))
5392 isl_die(map->ctx, isl_error_invalid,
5393 "unaligned unnamed parameters", goto error);
5394 map = isl_map_align_params(map, isl_map_get_space(dom));
5395 dom = isl_map_align_params(dom, isl_map_get_space(map));
5396 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5397 error:
5398 if (empty)
5399 *empty = NULL;
5400 isl_set_free(dom);
5401 isl_map_free(map);
5402 return NULL;
5405 __isl_give isl_map *isl_map_partial_lexmax(
5406 __isl_take isl_map *map, __isl_take isl_set *dom,
5407 __isl_give isl_set **empty)
5409 return isl_map_partial_lexopt(map, dom, empty, 1);
5412 __isl_give isl_map *isl_map_partial_lexmin(
5413 __isl_take isl_map *map, __isl_take isl_set *dom,
5414 __isl_give isl_set **empty)
5416 return isl_map_partial_lexopt(map, dom, empty, 0);
5419 __isl_give isl_set *isl_set_partial_lexmin(
5420 __isl_take isl_set *set, __isl_take isl_set *dom,
5421 __isl_give isl_set **empty)
5423 return (struct isl_set *)
5424 isl_map_partial_lexmin((struct isl_map *)set,
5425 dom, empty);
5428 __isl_give isl_set *isl_set_partial_lexmax(
5429 __isl_take isl_set *set, __isl_take isl_set *dom,
5430 __isl_give isl_set **empty)
5432 return (struct isl_set *)
5433 isl_map_partial_lexmax((struct isl_map *)set,
5434 dom, empty);
5437 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5439 struct isl_basic_set *dom = NULL;
5440 isl_space *dom_dim;
5442 if (!bmap)
5443 goto error;
5444 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5445 dom = isl_basic_set_universe(dom_dim);
5446 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5447 error:
5448 isl_basic_map_free(bmap);
5449 return NULL;
5452 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5454 return isl_basic_map_lexopt(bmap, 0);
5457 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5459 return isl_basic_map_lexopt(bmap, 1);
5462 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5464 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5467 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5469 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5472 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5474 struct isl_set *dom = NULL;
5475 isl_space *dom_dim;
5477 if (!map)
5478 goto error;
5479 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5480 dom = isl_set_universe(dom_dim);
5481 return isl_map_partial_lexopt(map, dom, NULL, max);
5482 error:
5483 isl_map_free(map);
5484 return NULL;
5487 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5489 return isl_map_lexopt(map, 0);
5492 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5494 return isl_map_lexopt(map, 1);
5497 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5499 return (isl_set *)isl_map_lexmin((isl_map *)set);
5502 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5504 return (isl_set *)isl_map_lexmax((isl_map *)set);
5507 /* Extract the first and only affine expression from list
5508 * and then add it to *pwaff with the given dom.
5509 * This domain is known to be disjoint from other domains
5510 * because of the way isl_basic_map_foreach_lexmax works.
5512 static int update_dim_opt(__isl_take isl_basic_set *dom,
5513 __isl_take isl_aff_list *list, void *user)
5515 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5516 isl_aff *aff;
5517 isl_pw_aff **pwaff = user;
5518 isl_pw_aff *pwaff_i;
5520 if (isl_aff_list_n_aff(list) != 1)
5521 isl_die(ctx, isl_error_internal,
5522 "expecting single element list", goto error);
5524 aff = isl_aff_list_get_aff(list, 0);
5525 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5527 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5529 isl_aff_list_free(list);
5531 return 0;
5532 error:
5533 isl_basic_set_free(dom);
5534 isl_aff_list_free(list);
5535 return -1;
5538 /* Given a basic map with one output dimension, compute the minimum or
5539 * maximum of that dimension as an isl_pw_aff.
5541 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5542 * call update_dim_opt on each leaf of the result.
5544 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
5545 int max)
5547 isl_space *dim = isl_basic_map_get_space(bmap);
5548 isl_pw_aff *pwaff;
5549 int r;
5551 dim = isl_space_from_domain(isl_space_domain(dim));
5552 dim = isl_space_add_dims(dim, isl_dim_out, 1);
5553 pwaff = isl_pw_aff_empty(dim);
5555 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
5556 if (r < 0)
5557 return isl_pw_aff_free(pwaff);
5559 return pwaff;
5562 /* Compute the minimum or maximum of the given output dimension
5563 * as a function of the parameters and the input dimensions,
5564 * but independently of the other output dimensions.
5566 * We first project out the other output dimension and then compute
5567 * the "lexicographic" maximum in each basic map, combining the results
5568 * using isl_pw_aff_union_max.
5570 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
5571 int max)
5573 int i;
5574 isl_pw_aff *pwaff;
5575 unsigned n_out;
5577 n_out = isl_map_dim(map, isl_dim_out);
5578 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
5579 map = isl_map_project_out(map, isl_dim_out, 0, pos);
5580 if (!map)
5581 return NULL;
5583 if (map->n == 0) {
5584 isl_space *dim = isl_map_get_space(map);
5585 dim = isl_space_domain(isl_space_from_range(dim));
5586 isl_map_free(map);
5587 return isl_pw_aff_empty(dim);
5590 pwaff = basic_map_dim_opt(map->p[0], max);
5591 for (i = 1; i < map->n; ++i) {
5592 isl_pw_aff *pwaff_i;
5594 pwaff_i = basic_map_dim_opt(map->p[i], max);
5595 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
5598 isl_map_free(map);
5600 return pwaff;
5603 /* Compute the maximum of the given output dimension as a function of the
5604 * parameters and input dimensions, but independently of
5605 * the other output dimensions.
5607 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
5609 return map_dim_opt(map, pos, 1);
5612 /* Compute the minimum or maximum of the given set dimension
5613 * as a function of the parameters,
5614 * but independently of the other set dimensions.
5616 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
5617 int max)
5619 return map_dim_opt(set, pos, max);
5622 /* Compute the maximum of the given set dimension as a function of the
5623 * parameters, but independently of the other set dimensions.
5625 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5627 return set_dim_opt(set, pos, 1);
5630 /* Compute the minimum of the given set dimension as a function of the
5631 * parameters, but independently of the other set dimensions.
5633 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
5635 return set_dim_opt(set, pos, 0);
5638 /* Apply a preimage specified by "mat" on the parameters of "bset".
5639 * bset is assumed to have only parameters and divs.
5641 static struct isl_basic_set *basic_set_parameter_preimage(
5642 struct isl_basic_set *bset, struct isl_mat *mat)
5644 unsigned nparam;
5646 if (!bset || !mat)
5647 goto error;
5649 bset->dim = isl_space_cow(bset->dim);
5650 if (!bset->dim)
5651 goto error;
5653 nparam = isl_basic_set_dim(bset, isl_dim_param);
5655 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5657 bset->dim->nparam = 0;
5658 bset->dim->n_out = nparam;
5659 bset = isl_basic_set_preimage(bset, mat);
5660 if (bset) {
5661 bset->dim->nparam = bset->dim->n_out;
5662 bset->dim->n_out = 0;
5664 return bset;
5665 error:
5666 isl_mat_free(mat);
5667 isl_basic_set_free(bset);
5668 return NULL;
5671 /* Apply a preimage specified by "mat" on the parameters of "set".
5672 * set is assumed to have only parameters and divs.
5674 static struct isl_set *set_parameter_preimage(
5675 struct isl_set *set, struct isl_mat *mat)
5677 isl_space *dim = NULL;
5678 unsigned nparam;
5680 if (!set || !mat)
5681 goto error;
5683 dim = isl_space_copy(set->dim);
5684 dim = isl_space_cow(dim);
5685 if (!dim)
5686 goto error;
5688 nparam = isl_set_dim(set, isl_dim_param);
5690 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5692 dim->nparam = 0;
5693 dim->n_out = nparam;
5694 isl_set_reset_space(set, dim);
5695 set = isl_set_preimage(set, mat);
5696 if (!set)
5697 goto error2;
5698 dim = isl_space_copy(set->dim);
5699 dim = isl_space_cow(dim);
5700 if (!dim)
5701 goto error2;
5702 dim->nparam = dim->n_out;
5703 dim->n_out = 0;
5704 isl_set_reset_space(set, dim);
5705 return set;
5706 error:
5707 isl_space_free(dim);
5708 isl_mat_free(mat);
5709 error2:
5710 isl_set_free(set);
5711 return NULL;
5714 /* Intersect the basic set "bset" with the affine space specified by the
5715 * equalities in "eq".
5717 static struct isl_basic_set *basic_set_append_equalities(
5718 struct isl_basic_set *bset, struct isl_mat *eq)
5720 int i, k;
5721 unsigned len;
5723 if (!bset || !eq)
5724 goto error;
5726 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
5727 eq->n_row, 0);
5728 if (!bset)
5729 goto error;
5731 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
5732 for (i = 0; i < eq->n_row; ++i) {
5733 k = isl_basic_set_alloc_equality(bset);
5734 if (k < 0)
5735 goto error;
5736 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5737 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5739 isl_mat_free(eq);
5741 bset = isl_basic_set_gauss(bset, NULL);
5742 bset = isl_basic_set_finalize(bset);
5744 return bset;
5745 error:
5746 isl_mat_free(eq);
5747 isl_basic_set_free(bset);
5748 return NULL;
5751 /* Intersect the set "set" with the affine space specified by the
5752 * equalities in "eq".
5754 static struct isl_set *set_append_equalities(struct isl_set *set,
5755 struct isl_mat *eq)
5757 int i;
5759 if (!set || !eq)
5760 goto error;
5762 for (i = 0; i < set->n; ++i) {
5763 set->p[i] = basic_set_append_equalities(set->p[i],
5764 isl_mat_copy(eq));
5765 if (!set->p[i])
5766 goto error;
5768 isl_mat_free(eq);
5769 return set;
5770 error:
5771 isl_mat_free(eq);
5772 isl_set_free(set);
5773 return NULL;
5776 /* Project the given basic set onto its parameter domain, possibly introducing
5777 * new, explicit, existential variables in the constraints.
5778 * The input has parameters and (possibly implicit) existential variables.
5779 * The output has the same parameters, but only
5780 * explicit existentially quantified variables.
5782 * The actual projection is performed by pip, but pip doesn't seem
5783 * to like equalities very much, so we first remove the equalities
5784 * among the parameters by performing a variable compression on
5785 * the parameters. Afterward, an inverse transformation is performed
5786 * and the equalities among the parameters are inserted back in.
5788 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5790 int i, j;
5791 struct isl_mat *eq;
5792 struct isl_mat *T, *T2;
5793 struct isl_set *set;
5794 unsigned nparam, n_div;
5796 bset = isl_basic_set_cow(bset);
5797 if (!bset)
5798 return NULL;
5800 if (bset->n_eq == 0)
5801 return isl_basic_set_lexmin(bset);
5803 isl_basic_set_gauss(bset, NULL);
5805 nparam = isl_basic_set_dim(bset, isl_dim_param);
5806 n_div = isl_basic_set_dim(bset, isl_dim_div);
5808 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5809 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5810 ++i;
5812 if (i == bset->n_eq)
5813 return isl_basic_set_lexmin(bset);
5815 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5816 0, 1 + nparam);
5817 eq = isl_mat_cow(eq);
5818 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5819 if (T && T->n_col == 0) {
5820 isl_mat_free(T);
5821 isl_mat_free(T2);
5822 isl_mat_free(eq);
5823 bset = isl_basic_set_set_to_empty(bset);
5824 return isl_set_from_basic_set(bset);
5826 bset = basic_set_parameter_preimage(bset, T);
5828 set = isl_basic_set_lexmin(bset);
5829 set = set_parameter_preimage(set, T2);
5830 set = set_append_equalities(set, eq);
5831 return set;
5834 /* Compute an explicit representation for all the existentially
5835 * quantified variables.
5836 * The input and output dimensions are first turned into parameters.
5837 * compute_divs then returns a map with the same parameters and
5838 * no input or output dimensions and the dimension specification
5839 * is reset to that of the input.
5841 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5843 struct isl_basic_set *bset;
5844 struct isl_set *set;
5845 struct isl_map *map;
5846 isl_space *dim, *orig_dim = NULL;
5847 unsigned nparam;
5848 unsigned n_in;
5849 unsigned n_out;
5851 bmap = isl_basic_map_cow(bmap);
5852 if (!bmap)
5853 return NULL;
5855 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5856 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5857 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5858 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5859 if (!dim)
5860 goto error;
5862 orig_dim = bmap->dim;
5863 bmap->dim = dim;
5864 bset = (struct isl_basic_set *)bmap;
5866 set = parameter_compute_divs(bset);
5867 map = (struct isl_map *)set;
5868 map = isl_map_reset_space(map, orig_dim);
5870 return map;
5871 error:
5872 isl_basic_map_free(bmap);
5873 return NULL;
5876 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5878 int i;
5879 unsigned off;
5881 if (!bmap)
5882 return -1;
5884 off = isl_space_dim(bmap->dim, isl_dim_all);
5885 for (i = 0; i < bmap->n_div; ++i) {
5886 if (isl_int_is_zero(bmap->div[i][0]))
5887 return 0;
5888 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5889 return -1);
5891 return 1;
5894 static int map_divs_known(__isl_keep isl_map *map)
5896 int i;
5898 if (!map)
5899 return -1;
5901 for (i = 0; i < map->n; ++i) {
5902 int known = isl_basic_map_divs_known(map->p[i]);
5903 if (known <= 0)
5904 return known;
5907 return 1;
5910 /* If bmap contains any unknown divs, then compute explicit
5911 * expressions for them. However, this computation may be
5912 * quite expensive, so first try to remove divs that aren't
5913 * strictly needed.
5915 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5917 int known;
5918 struct isl_map *map;
5920 known = isl_basic_map_divs_known(bmap);
5921 if (known < 0)
5922 goto error;
5923 if (known)
5924 return isl_map_from_basic_map(bmap);
5926 bmap = isl_basic_map_drop_redundant_divs(bmap);
5928 known = isl_basic_map_divs_known(bmap);
5929 if (known < 0)
5930 goto error;
5931 if (known)
5932 return isl_map_from_basic_map(bmap);
5934 map = compute_divs(bmap);
5935 return map;
5936 error:
5937 isl_basic_map_free(bmap);
5938 return NULL;
5941 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5943 int i;
5944 int known;
5945 struct isl_map *res;
5947 if (!map)
5948 return NULL;
5949 if (map->n == 0)
5950 return map;
5952 known = map_divs_known(map);
5953 if (known < 0) {
5954 isl_map_free(map);
5955 return NULL;
5957 if (known)
5958 return map;
5960 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5961 for (i = 1 ; i < map->n; ++i) {
5962 struct isl_map *r2;
5963 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5964 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5965 res = isl_map_union_disjoint(res, r2);
5966 else
5967 res = isl_map_union(res, r2);
5969 isl_map_free(map);
5971 return res;
5974 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5976 return (struct isl_set *)
5977 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5980 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5982 return (struct isl_set *)
5983 isl_map_compute_divs((struct isl_map *)set);
5986 struct isl_set *isl_map_domain(struct isl_map *map)
5988 int i;
5989 struct isl_set *set;
5991 if (!map)
5992 goto error;
5994 map = isl_map_cow(map);
5995 if (!map)
5996 return NULL;
5998 set = (struct isl_set *)map;
5999 set->dim = isl_space_domain(set->dim);
6000 if (!set->dim)
6001 goto error;
6002 for (i = 0; i < map->n; ++i) {
6003 set->p[i] = isl_basic_map_domain(map->p[i]);
6004 if (!set->p[i])
6005 goto error;
6007 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6008 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6009 return set;
6010 error:
6011 isl_map_free(map);
6012 return NULL;
6015 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6016 __isl_take isl_map *map2)
6018 int i;
6019 unsigned flags = 0;
6020 struct isl_map *map = NULL;
6022 if (!map1 || !map2)
6023 goto error;
6025 if (map1->n == 0) {
6026 isl_map_free(map1);
6027 return map2;
6029 if (map2->n == 0) {
6030 isl_map_free(map2);
6031 return map1;
6034 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6036 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6037 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6038 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6040 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6041 map1->n + map2->n, flags);
6042 if (!map)
6043 goto error;
6044 for (i = 0; i < map1->n; ++i) {
6045 map = isl_map_add_basic_map(map,
6046 isl_basic_map_copy(map1->p[i]));
6047 if (!map)
6048 goto error;
6050 for (i = 0; i < map2->n; ++i) {
6051 map = isl_map_add_basic_map(map,
6052 isl_basic_map_copy(map2->p[i]));
6053 if (!map)
6054 goto error;
6056 isl_map_free(map1);
6057 isl_map_free(map2);
6058 return map;
6059 error:
6060 isl_map_free(map);
6061 isl_map_free(map1);
6062 isl_map_free(map2);
6063 return NULL;
6066 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6067 __isl_take isl_map *map2)
6069 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6072 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6074 map1 = isl_map_union_disjoint(map1, map2);
6075 if (!map1)
6076 return NULL;
6077 if (map1->n > 1)
6078 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6079 return map1;
6082 struct isl_set *isl_set_union_disjoint(
6083 struct isl_set *set1, struct isl_set *set2)
6085 return (struct isl_set *)
6086 isl_map_union_disjoint(
6087 (struct isl_map *)set1, (struct isl_map *)set2);
6090 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6092 return (struct isl_set *)
6093 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6096 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6097 __isl_take isl_set *set)
6099 unsigned flags = 0;
6100 struct isl_map *result;
6101 int i, j;
6103 if (!map || !set)
6104 goto error;
6106 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
6107 isl_die(set->ctx, isl_error_invalid,
6108 "parameters don't match", goto error);
6110 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
6111 !isl_map_compatible_range(map, set))
6112 isl_die(set->ctx, isl_error_invalid,
6113 "incompatible spaces", goto error);
6115 if (isl_set_plain_is_universe(set)) {
6116 isl_set_free(set);
6117 return map;
6120 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6121 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6122 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6124 result = isl_map_alloc_space(isl_space_copy(map->dim),
6125 map->n * set->n, flags);
6126 if (!result)
6127 goto error;
6128 for (i = 0; i < map->n; ++i)
6129 for (j = 0; j < set->n; ++j) {
6130 result = isl_map_add_basic_map(result,
6131 isl_basic_map_intersect_range(
6132 isl_basic_map_copy(map->p[i]),
6133 isl_basic_set_copy(set->p[j])));
6134 if (!result)
6135 goto error;
6137 isl_map_free(map);
6138 isl_set_free(set);
6139 return result;
6140 error:
6141 isl_map_free(map);
6142 isl_set_free(set);
6143 return NULL;
6146 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6147 __isl_take isl_set *set)
6149 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6152 struct isl_map *isl_map_intersect_domain(
6153 struct isl_map *map, struct isl_set *set)
6155 return isl_map_reverse(
6156 isl_map_intersect_range(isl_map_reverse(map), set));
6159 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6160 __isl_take isl_map *map2)
6162 if (!map1 || !map2)
6163 goto error;
6164 map1 = isl_map_reverse(map1);
6165 map1 = isl_map_apply_range(map1, map2);
6166 return isl_map_reverse(map1);
6167 error:
6168 isl_map_free(map1);
6169 isl_map_free(map2);
6170 return NULL;
6173 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6174 __isl_take isl_map *map2)
6176 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6179 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6180 __isl_take isl_map *map2)
6182 isl_space *dim_result;
6183 struct isl_map *result;
6184 int i, j;
6186 if (!map1 || !map2)
6187 goto error;
6189 dim_result = isl_space_join(isl_space_copy(map1->dim),
6190 isl_space_copy(map2->dim));
6192 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6193 if (!result)
6194 goto error;
6195 for (i = 0; i < map1->n; ++i)
6196 for (j = 0; j < map2->n; ++j) {
6197 result = isl_map_add_basic_map(result,
6198 isl_basic_map_apply_range(
6199 isl_basic_map_copy(map1->p[i]),
6200 isl_basic_map_copy(map2->p[j])));
6201 if (!result)
6202 goto error;
6204 isl_map_free(map1);
6205 isl_map_free(map2);
6206 if (result && result->n <= 1)
6207 ISL_F_SET(result, ISL_MAP_DISJOINT);
6208 return result;
6209 error:
6210 isl_map_free(map1);
6211 isl_map_free(map2);
6212 return NULL;
6215 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6216 __isl_take isl_map *map2)
6218 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6222 * returns range - domain
6224 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6226 isl_space *dims, *target_dim;
6227 struct isl_basic_set *bset;
6228 unsigned dim;
6229 unsigned nparam;
6230 int i;
6232 if (!bmap)
6233 goto error;
6234 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6235 bmap->dim, isl_dim_out),
6236 goto error);
6237 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6238 dim = isl_basic_map_n_in(bmap);
6239 nparam = isl_basic_map_n_param(bmap);
6240 bset = isl_basic_set_from_basic_map(bmap);
6241 bset = isl_basic_set_cow(bset);
6242 dims = isl_basic_set_get_space(bset);
6243 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6244 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6245 bset = isl_basic_set_swap_vars(bset, 2*dim);
6246 for (i = 0; i < dim; ++i) {
6247 int j = isl_basic_map_alloc_equality(
6248 (struct isl_basic_map *)bset);
6249 if (j < 0)
6250 goto error;
6251 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6252 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6253 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6254 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6256 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6257 bset = isl_basic_set_reset_space(bset, target_dim);
6258 return bset;
6259 error:
6260 isl_basic_map_free(bmap);
6261 return NULL;
6265 * returns range - domain
6267 struct isl_set *isl_map_deltas(struct isl_map *map)
6269 int i;
6270 isl_space *dim;
6271 struct isl_set *result;
6273 if (!map)
6274 return NULL;
6276 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6277 map->dim, isl_dim_out),
6278 goto error);
6279 dim = isl_map_get_space(map);
6280 dim = isl_space_domain(dim);
6281 result = isl_set_alloc_space(dim, map->n, 0);
6282 if (!result)
6283 goto error;
6284 for (i = 0; i < map->n; ++i)
6285 result = isl_set_add_basic_set(result,
6286 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6287 isl_map_free(map);
6288 return result;
6289 error:
6290 isl_map_free(map);
6291 return NULL;
6295 * returns [domain -> range] -> range - domain
6297 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6298 __isl_take isl_basic_map *bmap)
6300 int i, k;
6301 isl_space *dim;
6302 isl_basic_map *domain;
6303 int nparam, n;
6304 unsigned total;
6306 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6307 isl_die(bmap->ctx, isl_error_invalid,
6308 "domain and range don't match", goto error);
6310 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6311 n = isl_basic_map_dim(bmap, isl_dim_in);
6313 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6314 domain = isl_basic_map_universe(dim);
6316 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6317 bmap = isl_basic_map_apply_range(bmap, domain);
6318 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6320 total = isl_basic_map_total_dim(bmap);
6322 for (i = 0; i < n; ++i) {
6323 k = isl_basic_map_alloc_equality(bmap);
6324 if (k < 0)
6325 goto error;
6326 isl_seq_clr(bmap->eq[k], 1 + total);
6327 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6328 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6329 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6332 bmap = isl_basic_map_gauss(bmap, NULL);
6333 return isl_basic_map_finalize(bmap);
6334 error:
6335 isl_basic_map_free(bmap);
6336 return NULL;
6340 * returns [domain -> range] -> range - domain
6342 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6344 int i;
6345 isl_space *domain_dim;
6347 if (!map)
6348 return NULL;
6350 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6351 isl_die(map->ctx, isl_error_invalid,
6352 "domain and range don't match", goto error);
6354 map = isl_map_cow(map);
6355 if (!map)
6356 return NULL;
6358 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6359 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6360 map->dim = isl_space_join(map->dim, domain_dim);
6361 if (!map->dim)
6362 goto error;
6363 for (i = 0; i < map->n; ++i) {
6364 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6365 if (!map->p[i])
6366 goto error;
6368 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6369 return map;
6370 error:
6371 isl_map_free(map);
6372 return NULL;
6375 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6377 struct isl_basic_map *bmap;
6378 unsigned nparam;
6379 unsigned dim;
6380 int i;
6382 if (!dims)
6383 return NULL;
6385 nparam = dims->nparam;
6386 dim = dims->n_out;
6387 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6388 if (!bmap)
6389 goto error;
6391 for (i = 0; i < dim; ++i) {
6392 int j = isl_basic_map_alloc_equality(bmap);
6393 if (j < 0)
6394 goto error;
6395 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6396 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6397 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6399 return isl_basic_map_finalize(bmap);
6400 error:
6401 isl_basic_map_free(bmap);
6402 return NULL;
6405 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6407 if (!dim)
6408 return NULL;
6409 if (dim->n_in != dim->n_out)
6410 isl_die(dim->ctx, isl_error_invalid,
6411 "number of input and output dimensions needs to be "
6412 "the same", goto error);
6413 return basic_map_identity(dim);
6414 error:
6415 isl_space_free(dim);
6416 return NULL;
6419 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6421 if (!model || !model->dim)
6422 return NULL;
6423 return isl_basic_map_identity(isl_space_copy(model->dim));
6426 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6428 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6431 struct isl_map *isl_map_identity_like(struct isl_map *model)
6433 if (!model || !model->dim)
6434 return NULL;
6435 return isl_map_identity(isl_space_copy(model->dim));
6438 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6440 if (!model || !model->dim)
6441 return NULL;
6442 return isl_map_identity(isl_space_copy(model->dim));
6445 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6447 isl_space *dim = isl_set_get_space(set);
6448 isl_map *id;
6449 id = isl_map_identity(isl_space_map_from_set(dim));
6450 return isl_map_intersect_range(id, set);
6453 /* Construct a basic set with all set dimensions having only non-negative
6454 * values.
6456 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6457 __isl_take isl_space *space)
6459 int i;
6460 unsigned nparam;
6461 unsigned dim;
6462 struct isl_basic_set *bset;
6464 if (!space)
6465 return NULL;
6466 nparam = space->nparam;
6467 dim = space->n_out;
6468 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6469 if (!bset)
6470 return NULL;
6471 for (i = 0; i < dim; ++i) {
6472 int k = isl_basic_set_alloc_inequality(bset);
6473 if (k < 0)
6474 goto error;
6475 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6476 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6478 return bset;
6479 error:
6480 isl_basic_set_free(bset);
6481 return NULL;
6484 /* Construct the half-space x_pos >= 0.
6486 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6487 int pos)
6489 int k;
6490 isl_basic_set *nonneg;
6492 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6493 k = isl_basic_set_alloc_inequality(nonneg);
6494 if (k < 0)
6495 goto error;
6496 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6497 isl_int_set_si(nonneg->ineq[k][pos], 1);
6499 return isl_basic_set_finalize(nonneg);
6500 error:
6501 isl_basic_set_free(nonneg);
6502 return NULL;
6505 /* Construct the half-space x_pos <= -1.
6507 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6509 int k;
6510 isl_basic_set *neg;
6512 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6513 k = isl_basic_set_alloc_inequality(neg);
6514 if (k < 0)
6515 goto error;
6516 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6517 isl_int_set_si(neg->ineq[k][0], -1);
6518 isl_int_set_si(neg->ineq[k][pos], -1);
6520 return isl_basic_set_finalize(neg);
6521 error:
6522 isl_basic_set_free(neg);
6523 return NULL;
6526 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6527 enum isl_dim_type type, unsigned first, unsigned n)
6529 int i;
6530 isl_basic_set *nonneg;
6531 isl_basic_set *neg;
6533 if (!set)
6534 return NULL;
6535 if (n == 0)
6536 return set;
6538 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6540 for (i = 0; i < n; ++i) {
6541 nonneg = nonneg_halfspace(isl_set_get_space(set),
6542 pos(set->dim, type) + first + i);
6543 neg = neg_halfspace(isl_set_get_space(set),
6544 pos(set->dim, type) + first + i);
6546 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6549 return set;
6550 error:
6551 isl_set_free(set);
6552 return NULL;
6555 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6556 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6557 void *user)
6559 isl_set *half;
6561 if (!set)
6562 return -1;
6563 if (isl_set_plain_is_empty(set)) {
6564 isl_set_free(set);
6565 return 0;
6567 if (first == len)
6568 return fn(set, signs, user);
6570 signs[first] = 1;
6571 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6572 1 + first));
6573 half = isl_set_intersect(half, isl_set_copy(set));
6574 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6575 goto error;
6577 signs[first] = -1;
6578 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6579 1 + first));
6580 half = isl_set_intersect(half, set);
6581 return foreach_orthant(half, signs, first + 1, len, fn, user);
6582 error:
6583 isl_set_free(set);
6584 return -1;
6587 /* Call "fn" on the intersections of "set" with each of the orthants
6588 * (except for obviously empty intersections). The orthant is identified
6589 * by the signs array, with each entry having value 1 or -1 according
6590 * to the sign of the corresponding variable.
6592 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6593 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6594 void *user)
6596 unsigned nparam;
6597 unsigned nvar;
6598 int *signs;
6599 int r;
6601 if (!set)
6602 return -1;
6603 if (isl_set_plain_is_empty(set))
6604 return 0;
6606 nparam = isl_set_dim(set, isl_dim_param);
6607 nvar = isl_set_dim(set, isl_dim_set);
6609 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6611 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6612 fn, user);
6614 free(signs);
6616 return r;
6619 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6621 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6624 int isl_basic_map_is_subset(
6625 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6627 int is_subset;
6628 struct isl_map *map1;
6629 struct isl_map *map2;
6631 if (!bmap1 || !bmap2)
6632 return -1;
6634 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6635 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6637 is_subset = isl_map_is_subset(map1, map2);
6639 isl_map_free(map1);
6640 isl_map_free(map2);
6642 return is_subset;
6645 int isl_basic_map_is_equal(
6646 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6648 int is_subset;
6650 if (!bmap1 || !bmap2)
6651 return -1;
6652 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6653 if (is_subset != 1)
6654 return is_subset;
6655 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6656 return is_subset;
6659 int isl_basic_set_is_equal(
6660 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6662 return isl_basic_map_is_equal(
6663 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6666 int isl_map_is_empty(struct isl_map *map)
6668 int i;
6669 int is_empty;
6671 if (!map)
6672 return -1;
6673 for (i = 0; i < map->n; ++i) {
6674 is_empty = isl_basic_map_is_empty(map->p[i]);
6675 if (is_empty < 0)
6676 return -1;
6677 if (!is_empty)
6678 return 0;
6680 return 1;
6683 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6685 return map ? map->n == 0 : -1;
6688 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6690 return isl_map_plain_is_empty(map);
6693 int isl_set_plain_is_empty(struct isl_set *set)
6695 return set ? set->n == 0 : -1;
6698 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6700 return isl_set_plain_is_empty(set);
6703 int isl_set_is_empty(struct isl_set *set)
6705 return isl_map_is_empty((struct isl_map *)set);
6708 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6710 if (!map1 || !map2)
6711 return -1;
6713 return isl_space_is_equal(map1->dim, map2->dim);
6716 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6718 if (!set1 || !set2)
6719 return -1;
6721 return isl_space_is_equal(set1->dim, set2->dim);
6724 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6726 int is_subset;
6728 if (!map1 || !map2)
6729 return -1;
6730 is_subset = isl_map_is_subset(map1, map2);
6731 if (is_subset != 1)
6732 return is_subset;
6733 is_subset = isl_map_is_subset(map2, map1);
6734 return is_subset;
6737 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6739 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
6742 int isl_basic_map_is_strict_subset(
6743 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6745 int is_subset;
6747 if (!bmap1 || !bmap2)
6748 return -1;
6749 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6750 if (is_subset != 1)
6751 return is_subset;
6752 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6753 if (is_subset == -1)
6754 return is_subset;
6755 return !is_subset;
6758 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6760 int is_subset;
6762 if (!map1 || !map2)
6763 return -1;
6764 is_subset = isl_map_is_subset(map1, map2);
6765 if (is_subset != 1)
6766 return is_subset;
6767 is_subset = isl_map_is_subset(map2, map1);
6768 if (is_subset == -1)
6769 return is_subset;
6770 return !is_subset;
6773 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6775 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6778 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6780 if (!bmap)
6781 return -1;
6782 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6785 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6787 if (!bset)
6788 return -1;
6789 return bset->n_eq == 0 && bset->n_ineq == 0;
6792 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6794 int i;
6796 if (!map)
6797 return -1;
6799 for (i = 0; i < map->n; ++i) {
6800 int r = isl_basic_map_is_universe(map->p[i]);
6801 if (r < 0 || r)
6802 return r;
6805 return 0;
6808 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6810 return isl_map_plain_is_universe((isl_map *) set);
6813 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6815 return isl_set_plain_is_universe(set);
6818 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6820 struct isl_basic_set *bset = NULL;
6821 struct isl_vec *sample = NULL;
6822 int empty;
6823 unsigned total;
6825 if (!bmap)
6826 return -1;
6828 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6829 return 1;
6831 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6832 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6833 copy = isl_basic_map_remove_redundancies(copy);
6834 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6835 isl_basic_map_free(copy);
6836 return empty;
6839 total = 1 + isl_basic_map_total_dim(bmap);
6840 if (bmap->sample && bmap->sample->size == total) {
6841 int contains = isl_basic_map_contains(bmap, bmap->sample);
6842 if (contains < 0)
6843 return -1;
6844 if (contains)
6845 return 0;
6847 isl_vec_free(bmap->sample);
6848 bmap->sample = NULL;
6849 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6850 if (!bset)
6851 return -1;
6852 sample = isl_basic_set_sample_vec(bset);
6853 if (!sample)
6854 return -1;
6855 empty = sample->size == 0;
6856 isl_vec_free(bmap->sample);
6857 bmap->sample = sample;
6858 if (empty)
6859 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6861 return empty;
6864 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6866 if (!bmap)
6867 return -1;
6868 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6871 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6873 return isl_basic_map_plain_is_empty(bmap);
6876 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6878 if (!bset)
6879 return -1;
6880 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6883 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6885 return isl_basic_set_plain_is_empty(bset);
6888 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6890 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6893 struct isl_map *isl_basic_map_union(
6894 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6896 struct isl_map *map;
6897 if (!bmap1 || !bmap2)
6898 return NULL;
6900 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
6902 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
6903 if (!map)
6904 goto error;
6905 map = isl_map_add_basic_map(map, bmap1);
6906 map = isl_map_add_basic_map(map, bmap2);
6907 return map;
6908 error:
6909 isl_basic_map_free(bmap1);
6910 isl_basic_map_free(bmap2);
6911 return NULL;
6914 struct isl_set *isl_basic_set_union(
6915 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6917 return (struct isl_set *)isl_basic_map_union(
6918 (struct isl_basic_map *)bset1,
6919 (struct isl_basic_map *)bset2);
6922 /* Order divs such that any div only depends on previous divs */
6923 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6925 int i;
6926 unsigned off;
6928 if (!bmap)
6929 return NULL;
6931 off = isl_space_dim(bmap->dim, isl_dim_all);
6933 for (i = 0; i < bmap->n_div; ++i) {
6934 int pos;
6935 if (isl_int_is_zero(bmap->div[i][0]))
6936 continue;
6937 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6938 bmap->n_div-i);
6939 if (pos == -1)
6940 continue;
6941 isl_basic_map_swap_div(bmap, i, i + pos);
6942 --i;
6944 return bmap;
6947 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6949 return (struct isl_basic_set *)
6950 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6953 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6955 int i;
6957 if (!map)
6958 return 0;
6960 for (i = 0; i < map->n; ++i) {
6961 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6962 if (!map->p[i])
6963 goto error;
6966 return map;
6967 error:
6968 isl_map_free(map);
6969 return NULL;
6972 /* Apply the expansion computed by isl_merge_divs.
6973 * The expansion itself is given by "exp" while the resulting
6974 * list of divs is given by "div".
6976 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6977 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6979 int i, j;
6980 int n_div;
6982 bset = isl_basic_set_cow(bset);
6983 if (!bset || !div)
6984 goto error;
6986 if (div->n_row < bset->n_div)
6987 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6988 "not an expansion", goto error);
6990 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
6991 div->n_row - bset->n_div, 0,
6992 2 * (div->n_row - bset->n_div));
6994 n_div = bset->n_div;
6995 for (i = n_div; i < div->n_row; ++i)
6996 if (isl_basic_set_alloc_div(bset) < 0)
6997 goto error;
6999 j = n_div - 1;
7000 for (i = div->n_row - 1; i >= 0; --i) {
7001 if (j >= 0 && exp[j] == i) {
7002 if (i != j)
7003 isl_basic_map_swap_div(bset, i, j);
7004 j--;
7005 } else {
7006 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
7007 if (isl_basic_map_add_div_constraints(bset, i) < 0)
7008 goto error;
7012 isl_mat_free(div);
7013 return bset;
7014 error:
7015 isl_basic_set_free(bset);
7016 isl_mat_free(div);
7017 return NULL;
7020 /* Look for a div in dst that corresponds to the div "div" in src.
7021 * The divs before "div" in src and dst are assumed to be the same.
7023 * Returns -1 if no corresponding div was found and the position
7024 * of the corresponding div in dst otherwise.
7026 static int find_div(struct isl_basic_map *dst,
7027 struct isl_basic_map *src, unsigned div)
7029 int i;
7031 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7033 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7034 for (i = div; i < dst->n_div; ++i)
7035 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7036 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7037 dst->n_div - div) == -1)
7038 return i;
7039 return -1;
7042 struct isl_basic_map *isl_basic_map_align_divs(
7043 struct isl_basic_map *dst, struct isl_basic_map *src)
7045 int i;
7046 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7048 if (!dst || !src)
7049 goto error;
7051 if (src->n_div == 0)
7052 return dst;
7054 for (i = 0; i < src->n_div; ++i)
7055 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7057 src = isl_basic_map_order_divs(src);
7058 dst = isl_basic_map_cow(dst);
7059 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7060 src->n_div, 0, 2 * src->n_div);
7061 if (!dst)
7062 return NULL;
7063 for (i = 0; i < src->n_div; ++i) {
7064 int j = find_div(dst, src, i);
7065 if (j < 0) {
7066 j = isl_basic_map_alloc_div(dst);
7067 if (j < 0)
7068 goto error;
7069 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7070 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7071 if (isl_basic_map_add_div_constraints(dst, j) < 0)
7072 goto error;
7074 if (j != i)
7075 isl_basic_map_swap_div(dst, i, j);
7077 return dst;
7078 error:
7079 isl_basic_map_free(dst);
7080 return NULL;
7083 struct isl_basic_set *isl_basic_set_align_divs(
7084 struct isl_basic_set *dst, struct isl_basic_set *src)
7086 return (struct isl_basic_set *)isl_basic_map_align_divs(
7087 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7090 struct isl_map *isl_map_align_divs(struct isl_map *map)
7092 int i;
7094 if (!map)
7095 return NULL;
7096 if (map->n == 0)
7097 return map;
7098 map = isl_map_compute_divs(map);
7099 map = isl_map_cow(map);
7100 if (!map)
7101 return NULL;
7103 for (i = 1; i < map->n; ++i)
7104 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7105 for (i = 1; i < map->n; ++i)
7106 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7108 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7109 return map;
7112 struct isl_set *isl_set_align_divs(struct isl_set *set)
7114 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7117 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7118 __isl_take isl_map *map)
7120 if (!set || !map)
7121 goto error;
7122 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7123 map = isl_map_intersect_domain(map, set);
7124 set = isl_map_range(map);
7125 return set;
7126 error:
7127 isl_set_free(set);
7128 isl_map_free(map);
7129 return NULL;
7132 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7133 __isl_take isl_map *map)
7135 return isl_map_align_params_map_map_and(set, map, &set_apply);
7138 /* There is no need to cow as removing empty parts doesn't change
7139 * the meaning of the set.
7141 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7143 int i;
7145 if (!map)
7146 return NULL;
7148 for (i = map->n-1; i >= 0; --i) {
7149 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
7150 continue;
7151 isl_basic_map_free(map->p[i]);
7152 if (i != map->n-1) {
7153 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7154 map->p[i] = map->p[map->n-1];
7156 map->n--;
7159 return map;
7162 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7164 return (struct isl_set *)
7165 isl_map_remove_empty_parts((struct isl_map *)set);
7168 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7170 struct isl_basic_map *bmap;
7171 if (!map || map->n == 0)
7172 return NULL;
7173 bmap = map->p[map->n-1];
7174 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7175 return isl_basic_map_copy(bmap);
7178 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7180 return (struct isl_basic_set *)
7181 isl_map_copy_basic_map((struct isl_map *)set);
7184 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7185 __isl_keep isl_basic_map *bmap)
7187 int i;
7189 if (!map || !bmap)
7190 goto error;
7191 for (i = map->n-1; i >= 0; --i) {
7192 if (map->p[i] != bmap)
7193 continue;
7194 map = isl_map_cow(map);
7195 if (!map)
7196 goto error;
7197 isl_basic_map_free(map->p[i]);
7198 if (i != map->n-1) {
7199 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7200 map->p[i] = map->p[map->n-1];
7202 map->n--;
7203 return map;
7205 return map;
7206 error:
7207 isl_map_free(map);
7208 return NULL;
7211 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7212 struct isl_basic_set *bset)
7214 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7215 (struct isl_basic_map *)bset);
7218 /* Given two basic sets bset1 and bset2, compute the maximal difference
7219 * between the values of dimension pos in bset1 and those in bset2
7220 * for any common value of the parameters and dimensions preceding pos.
7222 static enum isl_lp_result basic_set_maximal_difference_at(
7223 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7224 int pos, isl_int *opt)
7226 isl_space *dims;
7227 struct isl_basic_map *bmap1 = NULL;
7228 struct isl_basic_map *bmap2 = NULL;
7229 struct isl_ctx *ctx;
7230 struct isl_vec *obj;
7231 unsigned total;
7232 unsigned nparam;
7233 unsigned dim1, dim2;
7234 enum isl_lp_result res;
7236 if (!bset1 || !bset2)
7237 return isl_lp_error;
7239 nparam = isl_basic_set_n_param(bset1);
7240 dim1 = isl_basic_set_n_dim(bset1);
7241 dim2 = isl_basic_set_n_dim(bset2);
7242 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7243 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7244 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7245 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7246 if (!bmap1 || !bmap2)
7247 goto error;
7248 bmap1 = isl_basic_map_cow(bmap1);
7249 bmap1 = isl_basic_map_extend(bmap1, nparam,
7250 pos, (dim1 - pos) + (dim2 - pos),
7251 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7252 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7253 if (!bmap1)
7254 goto error;
7255 total = isl_basic_map_total_dim(bmap1);
7256 ctx = bmap1->ctx;
7257 obj = isl_vec_alloc(ctx, 1 + total);
7258 isl_seq_clr(obj->block.data, 1 + total);
7259 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7260 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7261 if (!obj)
7262 goto error;
7263 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7264 opt, NULL, NULL);
7265 isl_basic_map_free(bmap1);
7266 isl_vec_free(obj);
7267 return res;
7268 error:
7269 isl_basic_map_free(bmap1);
7270 isl_basic_map_free(bmap2);
7271 return isl_lp_error;
7274 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7275 * for any common value of the parameters and dimensions preceding pos
7276 * in both basic sets, the values of dimension pos in bset1 are
7277 * smaller or larger than those in bset2.
7279 * Returns
7280 * 1 if bset1 follows bset2
7281 * -1 if bset1 precedes bset2
7282 * 0 if bset1 and bset2 are incomparable
7283 * -2 if some error occurred.
7285 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7286 struct isl_basic_set *bset2, int pos)
7288 isl_int opt;
7289 enum isl_lp_result res;
7290 int cmp;
7292 isl_int_init(opt);
7294 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7296 if (res == isl_lp_empty)
7297 cmp = 0;
7298 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7299 res == isl_lp_unbounded)
7300 cmp = 1;
7301 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7302 cmp = -1;
7303 else
7304 cmp = -2;
7306 isl_int_clear(opt);
7307 return cmp;
7310 /* Given two basic sets bset1 and bset2, check whether
7311 * for any common value of the parameters and dimensions preceding pos
7312 * there is a value of dimension pos in bset1 that is larger
7313 * than a value of the same dimension in bset2.
7315 * Return
7316 * 1 if there exists such a pair
7317 * 0 if there is no such pair, but there is a pair of equal values
7318 * -1 otherwise
7319 * -2 if some error occurred.
7321 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7322 __isl_keep isl_basic_set *bset2, int pos)
7324 isl_int opt;
7325 enum isl_lp_result res;
7326 int cmp;
7328 isl_int_init(opt);
7330 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7332 if (res == isl_lp_empty)
7333 cmp = -1;
7334 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7335 res == isl_lp_unbounded)
7336 cmp = 1;
7337 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7338 cmp = -1;
7339 else if (res == isl_lp_ok)
7340 cmp = 0;
7341 else
7342 cmp = -2;
7344 isl_int_clear(opt);
7345 return cmp;
7348 /* Given two sets set1 and set2, check whether
7349 * for any common value of the parameters and dimensions preceding pos
7350 * there is a value of dimension pos in set1 that is larger
7351 * than a value of the same dimension in set2.
7353 * Return
7354 * 1 if there exists such a pair
7355 * 0 if there is no such pair, but there is a pair of equal values
7356 * -1 otherwise
7357 * -2 if some error occurred.
7359 int isl_set_follows_at(__isl_keep isl_set *set1,
7360 __isl_keep isl_set *set2, int pos)
7362 int i, j;
7363 int follows = -1;
7365 if (!set1 || !set2)
7366 return -2;
7368 for (i = 0; i < set1->n; ++i)
7369 for (j = 0; j < set2->n; ++j) {
7370 int f;
7371 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7372 if (f == 1 || f == -2)
7373 return f;
7374 if (f > follows)
7375 follows = f;
7378 return follows;
7381 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7382 unsigned pos, isl_int *val)
7384 int i;
7385 int d;
7386 unsigned total;
7388 if (!bmap)
7389 return -1;
7390 total = isl_basic_map_total_dim(bmap);
7391 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7392 for (; d+1 > pos; --d)
7393 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7394 break;
7395 if (d != pos)
7396 continue;
7397 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7398 return 0;
7399 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7400 return 0;
7401 if (!isl_int_is_one(bmap->eq[i][1+d]))
7402 return 0;
7403 if (val)
7404 isl_int_neg(*val, bmap->eq[i][0]);
7405 return 1;
7407 return 0;
7410 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7411 unsigned pos, isl_int *val)
7413 int i;
7414 isl_int v;
7415 isl_int tmp;
7416 int fixed;
7418 if (!map)
7419 return -1;
7420 if (map->n == 0)
7421 return 0;
7422 if (map->n == 1)
7423 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7424 isl_int_init(v);
7425 isl_int_init(tmp);
7426 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7427 for (i = 1; fixed == 1 && i < map->n; ++i) {
7428 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7429 if (fixed == 1 && isl_int_ne(tmp, v))
7430 fixed = 0;
7432 if (val)
7433 isl_int_set(*val, v);
7434 isl_int_clear(tmp);
7435 isl_int_clear(v);
7436 return fixed;
7439 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7440 unsigned pos, isl_int *val)
7442 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7443 pos, val);
7446 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7447 isl_int *val)
7449 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7452 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7453 enum isl_dim_type type, unsigned pos, isl_int *val)
7455 if (pos >= isl_basic_map_dim(bmap, type))
7456 return -1;
7457 return isl_basic_map_plain_has_fixed_var(bmap,
7458 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7461 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7462 enum isl_dim_type type, unsigned pos, isl_int *val)
7464 if (pos >= isl_map_dim(map, type))
7465 return -1;
7466 return isl_map_plain_has_fixed_var(map,
7467 map_offset(map, type) - 1 + pos, val);
7470 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
7471 enum isl_dim_type type, unsigned pos, isl_int *val)
7473 return isl_map_plain_is_fixed(set, type, pos, val);
7476 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7477 enum isl_dim_type type, unsigned pos, isl_int *val)
7479 return isl_map_plain_is_fixed(map, type, pos, val);
7482 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7483 * then return this fixed value in *val.
7485 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7486 unsigned dim, isl_int *val)
7488 return isl_basic_set_plain_has_fixed_var(bset,
7489 isl_basic_set_n_param(bset) + dim, val);
7492 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7493 * then return this fixed value in *val.
7495 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7496 unsigned dim, isl_int *val)
7498 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7501 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7502 unsigned dim, isl_int *val)
7504 return isl_set_plain_dim_is_fixed(set, dim, val);
7507 /* Check if input variable in has fixed value and if so and if val is not NULL,
7508 * then return this fixed value in *val.
7510 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7511 unsigned in, isl_int *val)
7513 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7516 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7517 * and if val is not NULL, then return this lower bound in *val.
7519 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7520 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7522 int i, i_eq = -1, i_ineq = -1;
7523 isl_int *c;
7524 unsigned total;
7525 unsigned nparam;
7527 if (!bset)
7528 return -1;
7529 total = isl_basic_set_total_dim(bset);
7530 nparam = isl_basic_set_n_param(bset);
7531 for (i = 0; i < bset->n_eq; ++i) {
7532 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7533 continue;
7534 if (i_eq != -1)
7535 return 0;
7536 i_eq = i;
7538 for (i = 0; i < bset->n_ineq; ++i) {
7539 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7540 continue;
7541 if (i_eq != -1 || i_ineq != -1)
7542 return 0;
7543 i_ineq = i;
7545 if (i_eq == -1 && i_ineq == -1)
7546 return 0;
7547 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7548 /* The coefficient should always be one due to normalization. */
7549 if (!isl_int_is_one(c[1+nparam+dim]))
7550 return 0;
7551 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7552 return 0;
7553 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7554 total - nparam - dim - 1) != -1)
7555 return 0;
7556 if (val)
7557 isl_int_neg(*val, c[0]);
7558 return 1;
7561 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7562 unsigned dim, isl_int *val)
7564 int i;
7565 isl_int v;
7566 isl_int tmp;
7567 int fixed;
7569 if (!set)
7570 return -1;
7571 if (set->n == 0)
7572 return 0;
7573 if (set->n == 1)
7574 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7575 dim, val);
7576 isl_int_init(v);
7577 isl_int_init(tmp);
7578 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7579 dim, &v);
7580 for (i = 1; fixed == 1 && i < set->n; ++i) {
7581 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7582 dim, &tmp);
7583 if (fixed == 1 && isl_int_ne(tmp, v))
7584 fixed = 0;
7586 if (val)
7587 isl_int_set(*val, v);
7588 isl_int_clear(tmp);
7589 isl_int_clear(v);
7590 return fixed;
7593 struct constraint {
7594 unsigned size;
7595 isl_int *c;
7598 /* uset_gist depends on constraints without existentially quantified
7599 * variables sorting first.
7601 static int qsort_constraint_cmp(const void *p1, const void *p2)
7603 const struct constraint *c1 = (const struct constraint *)p1;
7604 const struct constraint *c2 = (const struct constraint *)p2;
7605 int l1, l2;
7606 unsigned size = isl_min(c1->size, c2->size);
7608 l1 = isl_seq_last_non_zero(c1->c, size);
7609 l2 = isl_seq_last_non_zero(c2->c, size);
7611 if (l1 != l2)
7612 return l1 - l2;
7614 return isl_seq_cmp(c1->c, c2->c, size);
7617 static struct isl_basic_map *isl_basic_map_sort_constraints(
7618 struct isl_basic_map *bmap)
7620 int i;
7621 struct constraint *c;
7622 unsigned total;
7624 if (!bmap)
7625 return NULL;
7626 total = isl_basic_map_total_dim(bmap);
7627 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7628 if (!c)
7629 goto error;
7630 for (i = 0; i < bmap->n_ineq; ++i) {
7631 c[i].size = total;
7632 c[i].c = bmap->ineq[i];
7634 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7635 for (i = 0; i < bmap->n_ineq; ++i)
7636 bmap->ineq[i] = c[i].c;
7637 free(c);
7638 return bmap;
7639 error:
7640 isl_basic_map_free(bmap);
7641 return NULL;
7644 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7645 __isl_take isl_basic_set *bset)
7647 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7648 (struct isl_basic_map *)bset);
7651 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7653 if (!bmap)
7654 return NULL;
7655 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7656 return bmap;
7657 bmap = isl_basic_map_remove_redundancies(bmap);
7658 bmap = isl_basic_map_sort_constraints(bmap);
7659 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7660 return bmap;
7663 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7665 return (struct isl_basic_set *)isl_basic_map_normalize(
7666 (struct isl_basic_map *)bset);
7669 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7670 const __isl_keep isl_basic_map *bmap2)
7672 int i, cmp;
7673 unsigned total;
7675 if (bmap1 == bmap2)
7676 return 0;
7677 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7678 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7679 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7680 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7681 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7682 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7683 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7684 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7685 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7686 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7687 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7688 return 0;
7689 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7690 return 1;
7691 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7692 return -1;
7693 if (bmap1->n_eq != bmap2->n_eq)
7694 return bmap1->n_eq - bmap2->n_eq;
7695 if (bmap1->n_ineq != bmap2->n_ineq)
7696 return bmap1->n_ineq - bmap2->n_ineq;
7697 if (bmap1->n_div != bmap2->n_div)
7698 return bmap1->n_div - bmap2->n_div;
7699 total = isl_basic_map_total_dim(bmap1);
7700 for (i = 0; i < bmap1->n_eq; ++i) {
7701 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7702 if (cmp)
7703 return cmp;
7705 for (i = 0; i < bmap1->n_ineq; ++i) {
7706 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7707 if (cmp)
7708 return cmp;
7710 for (i = 0; i < bmap1->n_div; ++i) {
7711 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7712 if (cmp)
7713 return cmp;
7715 return 0;
7718 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
7719 const __isl_keep isl_basic_set *bset2)
7721 return isl_basic_map_plain_cmp(bset1, bset2);
7724 int isl_set_plain_cmp(const __isl_keep isl_set *set1,
7725 const __isl_keep isl_set *set2)
7727 int i, cmp;
7729 if (set1 == set2)
7730 return 0;
7731 if (set1->n != set2->n)
7732 return set1->n - set2->n;
7734 for (i = 0; i < set1->n; ++i) {
7735 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
7736 if (cmp)
7737 return cmp;
7740 return 0;
7743 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7744 __isl_keep isl_basic_map *bmap2)
7746 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7749 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7750 __isl_keep isl_basic_set *bset2)
7752 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7753 (isl_basic_map *)bset2);
7756 static int qsort_bmap_cmp(const void *p1, const void *p2)
7758 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7759 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7761 return isl_basic_map_plain_cmp(bmap1, bmap2);
7764 /* We normalize in place, but if anything goes wrong we need
7765 * to return NULL, so we need to make sure we don't change the
7766 * meaning of any possible other copies of map.
7768 struct isl_map *isl_map_normalize(struct isl_map *map)
7770 int i, j;
7771 struct isl_basic_map *bmap;
7773 if (!map)
7774 return NULL;
7775 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7776 return map;
7777 for (i = 0; i < map->n; ++i) {
7778 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7779 if (!bmap)
7780 goto error;
7781 isl_basic_map_free(map->p[i]);
7782 map->p[i] = bmap;
7784 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7785 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7786 map = isl_map_remove_empty_parts(map);
7787 if (!map)
7788 return NULL;
7789 for (i = map->n - 1; i >= 1; --i) {
7790 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7791 continue;
7792 isl_basic_map_free(map->p[i-1]);
7793 for (j = i; j < map->n; ++j)
7794 map->p[j-1] = map->p[j];
7795 map->n--;
7797 return map;
7798 error:
7799 isl_map_free(map);
7800 return NULL;
7804 struct isl_set *isl_set_normalize(struct isl_set *set)
7806 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7809 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7811 int i;
7812 int equal;
7814 if (!map1 || !map2)
7815 return -1;
7817 if (map1 == map2)
7818 return 1;
7819 if (!isl_space_is_equal(map1->dim, map2->dim))
7820 return 0;
7822 map1 = isl_map_copy(map1);
7823 map2 = isl_map_copy(map2);
7824 map1 = isl_map_normalize(map1);
7825 map2 = isl_map_normalize(map2);
7826 if (!map1 || !map2)
7827 goto error;
7828 equal = map1->n == map2->n;
7829 for (i = 0; equal && i < map1->n; ++i) {
7830 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7831 if (equal < 0)
7832 goto error;
7834 isl_map_free(map1);
7835 isl_map_free(map2);
7836 return equal;
7837 error:
7838 isl_map_free(map1);
7839 isl_map_free(map2);
7840 return -1;
7843 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7845 return isl_map_plain_is_equal(map1, map2);
7848 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7850 return isl_map_plain_is_equal((struct isl_map *)set1,
7851 (struct isl_map *)set2);
7854 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7856 return isl_set_plain_is_equal(set1, set2);
7859 /* Return an interval that ranges from min to max (inclusive)
7861 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7862 isl_int min, isl_int max)
7864 int k;
7865 struct isl_basic_set *bset = NULL;
7867 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7868 if (!bset)
7869 goto error;
7871 k = isl_basic_set_alloc_inequality(bset);
7872 if (k < 0)
7873 goto error;
7874 isl_int_set_si(bset->ineq[k][1], 1);
7875 isl_int_neg(bset->ineq[k][0], min);
7877 k = isl_basic_set_alloc_inequality(bset);
7878 if (k < 0)
7879 goto error;
7880 isl_int_set_si(bset->ineq[k][1], -1);
7881 isl_int_set(bset->ineq[k][0], max);
7883 return bset;
7884 error:
7885 isl_basic_set_free(bset);
7886 return NULL;
7889 /* Return the Cartesian product of the basic sets in list (in the given order).
7891 __isl_give isl_basic_set *isl_basic_set_list_product(
7892 __isl_take struct isl_basic_set_list *list)
7894 int i;
7895 unsigned dim;
7896 unsigned nparam;
7897 unsigned extra;
7898 unsigned n_eq;
7899 unsigned n_ineq;
7900 struct isl_basic_set *product = NULL;
7902 if (!list)
7903 goto error;
7904 isl_assert(list->ctx, list->n > 0, goto error);
7905 isl_assert(list->ctx, list->p[0], goto error);
7906 nparam = isl_basic_set_n_param(list->p[0]);
7907 dim = isl_basic_set_n_dim(list->p[0]);
7908 extra = list->p[0]->n_div;
7909 n_eq = list->p[0]->n_eq;
7910 n_ineq = list->p[0]->n_ineq;
7911 for (i = 1; i < list->n; ++i) {
7912 isl_assert(list->ctx, list->p[i], goto error);
7913 isl_assert(list->ctx,
7914 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7915 dim += isl_basic_set_n_dim(list->p[i]);
7916 extra += list->p[i]->n_div;
7917 n_eq += list->p[i]->n_eq;
7918 n_ineq += list->p[i]->n_ineq;
7920 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7921 n_eq, n_ineq);
7922 if (!product)
7923 goto error;
7924 dim = 0;
7925 for (i = 0; i < list->n; ++i) {
7926 isl_basic_set_add_constraints(product,
7927 isl_basic_set_copy(list->p[i]), dim);
7928 dim += isl_basic_set_n_dim(list->p[i]);
7930 isl_basic_set_list_free(list);
7931 return product;
7932 error:
7933 isl_basic_set_free(product);
7934 isl_basic_set_list_free(list);
7935 return NULL;
7938 struct isl_basic_map *isl_basic_map_product(
7939 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7941 isl_space *dim_result = NULL;
7942 struct isl_basic_map *bmap;
7943 unsigned in1, in2, out1, out2, nparam, total, pos;
7944 struct isl_dim_map *dim_map1, *dim_map2;
7946 if (!bmap1 || !bmap2)
7947 goto error;
7949 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
7950 bmap2->dim, isl_dim_param), goto error);
7951 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
7952 isl_space_copy(bmap2->dim));
7954 in1 = isl_basic_map_n_in(bmap1);
7955 in2 = isl_basic_map_n_in(bmap2);
7956 out1 = isl_basic_map_n_out(bmap1);
7957 out2 = isl_basic_map_n_out(bmap2);
7958 nparam = isl_basic_map_n_param(bmap1);
7960 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7961 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7962 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7963 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7964 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7965 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7966 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7967 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7968 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7969 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7970 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7972 bmap = isl_basic_map_alloc_space(dim_result,
7973 bmap1->n_div + bmap2->n_div,
7974 bmap1->n_eq + bmap2->n_eq,
7975 bmap1->n_ineq + bmap2->n_ineq);
7976 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7977 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7978 bmap = isl_basic_map_simplify(bmap);
7979 return isl_basic_map_finalize(bmap);
7980 error:
7981 isl_basic_map_free(bmap1);
7982 isl_basic_map_free(bmap2);
7983 return NULL;
7986 __isl_give isl_basic_map *isl_basic_map_flat_product(
7987 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7989 isl_basic_map *prod;
7991 prod = isl_basic_map_product(bmap1, bmap2);
7992 prod = isl_basic_map_flatten(prod);
7993 return prod;
7996 __isl_give isl_basic_set *isl_basic_set_flat_product(
7997 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7999 return isl_basic_map_flat_range_product(bset1, bset2);
8002 __isl_give isl_basic_map *isl_basic_map_domain_product(
8003 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8005 isl_space *space_result = NULL;
8006 isl_basic_map *bmap;
8007 unsigned in1, in2, out, nparam, total, pos;
8008 struct isl_dim_map *dim_map1, *dim_map2;
8010 if (!bmap1 || !bmap2)
8011 goto error;
8013 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8014 isl_space_copy(bmap2->dim));
8016 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8017 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8018 out = isl_basic_map_dim(bmap1, isl_dim_out);
8019 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8021 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8022 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8023 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8024 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8025 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8026 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8027 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8028 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8029 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8030 isl_dim_map_div(dim_map1, bmap1, pos += out);
8031 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8033 bmap = isl_basic_map_alloc_space(space_result,
8034 bmap1->n_div + bmap2->n_div,
8035 bmap1->n_eq + bmap2->n_eq,
8036 bmap1->n_ineq + bmap2->n_ineq);
8037 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8038 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8039 bmap = isl_basic_map_simplify(bmap);
8040 return isl_basic_map_finalize(bmap);
8041 error:
8042 isl_basic_map_free(bmap1);
8043 isl_basic_map_free(bmap2);
8044 return NULL;
8047 __isl_give isl_basic_map *isl_basic_map_range_product(
8048 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8050 isl_space *dim_result = NULL;
8051 isl_basic_map *bmap;
8052 unsigned in, out1, out2, nparam, total, pos;
8053 struct isl_dim_map *dim_map1, *dim_map2;
8055 if (!bmap1 || !bmap2)
8056 goto error;
8058 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8059 isl_space_copy(bmap2->dim));
8061 in = isl_basic_map_dim(bmap1, isl_dim_in);
8062 out1 = isl_basic_map_n_out(bmap1);
8063 out2 = isl_basic_map_n_out(bmap2);
8064 nparam = isl_basic_map_n_param(bmap1);
8066 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8067 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8068 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8069 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8070 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8071 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8072 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8073 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8074 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8075 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8076 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8078 bmap = isl_basic_map_alloc_space(dim_result,
8079 bmap1->n_div + bmap2->n_div,
8080 bmap1->n_eq + bmap2->n_eq,
8081 bmap1->n_ineq + bmap2->n_ineq);
8082 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8083 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8084 bmap = isl_basic_map_simplify(bmap);
8085 return isl_basic_map_finalize(bmap);
8086 error:
8087 isl_basic_map_free(bmap1);
8088 isl_basic_map_free(bmap2);
8089 return NULL;
8092 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8093 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8095 isl_basic_map *prod;
8097 prod = isl_basic_map_range_product(bmap1, bmap2);
8098 prod = isl_basic_map_flatten_range(prod);
8099 return prod;
8102 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8103 __isl_take isl_map *map2,
8104 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8105 __isl_take isl_space *right),
8106 __isl_give isl_basic_map *(*basic_map_product)(
8107 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8109 unsigned flags = 0;
8110 struct isl_map *result;
8111 int i, j;
8113 if (!map1 || !map2)
8114 goto error;
8116 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8117 map2->dim, isl_dim_param), goto error);
8119 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8120 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8121 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8123 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8124 isl_space_copy(map2->dim)),
8125 map1->n * map2->n, flags);
8126 if (!result)
8127 goto error;
8128 for (i = 0; i < map1->n; ++i)
8129 for (j = 0; j < map2->n; ++j) {
8130 struct isl_basic_map *part;
8131 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8132 isl_basic_map_copy(map2->p[j]));
8133 if (isl_basic_map_is_empty(part))
8134 isl_basic_map_free(part);
8135 else
8136 result = isl_map_add_basic_map(result, part);
8137 if (!result)
8138 goto error;
8140 isl_map_free(map1);
8141 isl_map_free(map2);
8142 return result;
8143 error:
8144 isl_map_free(map1);
8145 isl_map_free(map2);
8146 return NULL;
8149 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8151 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8152 __isl_take isl_map *map2)
8154 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8157 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8158 __isl_take isl_map *map2)
8160 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8163 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8165 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8166 __isl_take isl_map *map2)
8168 isl_map *prod;
8170 prod = isl_map_product(map1, map2);
8171 prod = isl_map_flatten(prod);
8172 return prod;
8175 /* Given two set A and B, construct its Cartesian product A x B.
8177 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8179 return isl_map_range_product(set1, set2);
8182 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8183 __isl_take isl_set *set2)
8185 return isl_map_flat_range_product(set1, set2);
8188 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8190 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8191 __isl_take isl_map *map2)
8193 return map_product(map1, map2, &isl_space_domain_product,
8194 &isl_basic_map_domain_product);
8197 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8199 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8200 __isl_take isl_map *map2)
8202 return map_product(map1, map2, &isl_space_range_product,
8203 &isl_basic_map_range_product);
8206 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8207 __isl_take isl_map *map2)
8209 return isl_map_align_params_map_map_and(map1, map2,
8210 &map_domain_product_aligned);
8213 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8214 __isl_take isl_map *map2)
8216 return isl_map_align_params_map_map_and(map1, map2,
8217 &map_range_product_aligned);
8220 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8222 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8223 __isl_take isl_map *map2)
8225 isl_map *prod;
8227 prod = isl_map_domain_product(map1, map2);
8228 prod = isl_map_flatten_domain(prod);
8229 return prod;
8232 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8234 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8235 __isl_take isl_map *map2)
8237 isl_map *prod;
8239 prod = isl_map_range_product(map1, map2);
8240 prod = isl_map_flatten_range(prod);
8241 return prod;
8244 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8246 int i;
8247 uint32_t hash = isl_hash_init();
8248 unsigned total;
8250 if (!bmap)
8251 return 0;
8252 bmap = isl_basic_map_copy(bmap);
8253 bmap = isl_basic_map_normalize(bmap);
8254 if (!bmap)
8255 return 0;
8256 total = isl_basic_map_total_dim(bmap);
8257 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8258 for (i = 0; i < bmap->n_eq; ++i) {
8259 uint32_t c_hash;
8260 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8261 isl_hash_hash(hash, c_hash);
8263 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8264 for (i = 0; i < bmap->n_ineq; ++i) {
8265 uint32_t c_hash;
8266 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8267 isl_hash_hash(hash, c_hash);
8269 isl_hash_byte(hash, bmap->n_div & 0xFF);
8270 for (i = 0; i < bmap->n_div; ++i) {
8271 uint32_t c_hash;
8272 if (isl_int_is_zero(bmap->div[i][0]))
8273 continue;
8274 isl_hash_byte(hash, i & 0xFF);
8275 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8276 isl_hash_hash(hash, c_hash);
8278 isl_basic_map_free(bmap);
8279 return hash;
8282 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8284 return isl_basic_map_get_hash((isl_basic_map *)bset);
8287 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8289 int i;
8290 uint32_t hash;
8292 if (!map)
8293 return 0;
8294 map = isl_map_copy(map);
8295 map = isl_map_normalize(map);
8296 if (!map)
8297 return 0;
8299 hash = isl_hash_init();
8300 for (i = 0; i < map->n; ++i) {
8301 uint32_t bmap_hash;
8302 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8303 isl_hash_hash(hash, bmap_hash);
8306 isl_map_free(map);
8308 return hash;
8311 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8313 return isl_map_get_hash((isl_map *)set);
8316 /* Check if the value for dimension dim is completely determined
8317 * by the values of the other parameters and variables.
8318 * That is, check if dimension dim is involved in an equality.
8320 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8322 int i;
8323 unsigned nparam;
8325 if (!bset)
8326 return -1;
8327 nparam = isl_basic_set_n_param(bset);
8328 for (i = 0; i < bset->n_eq; ++i)
8329 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8330 return 1;
8331 return 0;
8334 /* Check if the value for dimension dim is completely determined
8335 * by the values of the other parameters and variables.
8336 * That is, check if dimension dim is involved in an equality
8337 * for each of the subsets.
8339 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8341 int i;
8343 if (!set)
8344 return -1;
8345 for (i = 0; i < set->n; ++i) {
8346 int unique;
8347 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8348 if (unique != 1)
8349 return unique;
8351 return 1;
8354 int isl_set_n_basic_set(__isl_keep isl_set *set)
8356 return set ? set->n : 0;
8359 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8360 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8362 int i;
8364 if (!map)
8365 return -1;
8367 for (i = 0; i < map->n; ++i)
8368 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8369 return -1;
8371 return 0;
8374 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8375 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8377 int i;
8379 if (!set)
8380 return -1;
8382 for (i = 0; i < set->n; ++i)
8383 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8384 return -1;
8386 return 0;
8389 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8391 isl_space *dim;
8393 if (!bset)
8394 return NULL;
8396 bset = isl_basic_set_cow(bset);
8397 if (!bset)
8398 return NULL;
8400 dim = isl_basic_set_get_space(bset);
8401 dim = isl_space_lift(dim, bset->n_div);
8402 if (!dim)
8403 goto error;
8404 isl_space_free(bset->dim);
8405 bset->dim = dim;
8406 bset->extra -= bset->n_div;
8407 bset->n_div = 0;
8409 bset = isl_basic_set_finalize(bset);
8411 return bset;
8412 error:
8413 isl_basic_set_free(bset);
8414 return NULL;
8417 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8419 int i;
8420 isl_space *dim;
8421 unsigned n_div;
8423 set = isl_set_align_divs(set);
8425 if (!set)
8426 return NULL;
8428 set = isl_set_cow(set);
8429 if (!set)
8430 return NULL;
8432 n_div = set->p[0]->n_div;
8433 dim = isl_set_get_space(set);
8434 dim = isl_space_lift(dim, n_div);
8435 if (!dim)
8436 goto error;
8437 isl_space_free(set->dim);
8438 set->dim = dim;
8440 for (i = 0; i < set->n; ++i) {
8441 set->p[i] = isl_basic_set_lift(set->p[i]);
8442 if (!set->p[i])
8443 goto error;
8446 return set;
8447 error:
8448 isl_set_free(set);
8449 return NULL;
8452 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8454 isl_space *dim;
8455 struct isl_basic_map *bmap;
8456 unsigned n_set;
8457 unsigned n_div;
8458 unsigned n_param;
8459 unsigned total;
8460 int i, k, l;
8462 set = isl_set_align_divs(set);
8464 if (!set)
8465 return NULL;
8467 dim = isl_set_get_space(set);
8468 if (set->n == 0 || set->p[0]->n_div == 0) {
8469 isl_set_free(set);
8470 return isl_map_identity(isl_space_map_from_set(dim));
8473 n_div = set->p[0]->n_div;
8474 dim = isl_space_map_from_set(dim);
8475 n_param = isl_space_dim(dim, isl_dim_param);
8476 n_set = isl_space_dim(dim, isl_dim_in);
8477 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8478 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8479 for (i = 0; i < n_set; ++i)
8480 bmap = var_equal(bmap, i);
8482 total = n_param + n_set + n_set + n_div;
8483 for (i = 0; i < n_div; ++i) {
8484 k = isl_basic_map_alloc_inequality(bmap);
8485 if (k < 0)
8486 goto error;
8487 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8488 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8489 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8490 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8491 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8492 set->p[0]->div[i][0]);
8494 l = isl_basic_map_alloc_inequality(bmap);
8495 if (l < 0)
8496 goto error;
8497 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8498 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8499 set->p[0]->div[i][0]);
8500 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8503 isl_set_free(set);
8504 bmap = isl_basic_map_simplify(bmap);
8505 bmap = isl_basic_map_finalize(bmap);
8506 return isl_map_from_basic_map(bmap);
8507 error:
8508 isl_set_free(set);
8509 isl_basic_map_free(bmap);
8510 return NULL;
8513 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8515 unsigned dim;
8516 int size = 0;
8518 if (!bset)
8519 return -1;
8521 dim = isl_basic_set_total_dim(bset);
8522 size += bset->n_eq * (1 + dim);
8523 size += bset->n_ineq * (1 + dim);
8524 size += bset->n_div * (2 + dim);
8526 return size;
8529 int isl_set_size(__isl_keep isl_set *set)
8531 int i;
8532 int size = 0;
8534 if (!set)
8535 return -1;
8537 for (i = 0; i < set->n; ++i)
8538 size += isl_basic_set_size(set->p[i]);
8540 return size;
8543 /* Check if there is any lower bound (if lower == 0) and/or upper
8544 * bound (if upper == 0) on the specified dim.
8546 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8547 enum isl_dim_type type, unsigned pos, int lower, int upper)
8549 int i;
8551 if (!bmap)
8552 return -1;
8554 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8556 pos += isl_basic_map_offset(bmap, type);
8558 for (i = 0; i < bmap->n_div; ++i) {
8559 if (isl_int_is_zero(bmap->div[i][0]))
8560 continue;
8561 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8562 return 1;
8565 for (i = 0; i < bmap->n_eq; ++i)
8566 if (!isl_int_is_zero(bmap->eq[i][pos]))
8567 return 1;
8569 for (i = 0; i < bmap->n_ineq; ++i) {
8570 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8571 if (sgn > 0)
8572 lower = 1;
8573 if (sgn < 0)
8574 upper = 1;
8577 return lower && upper;
8580 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8581 enum isl_dim_type type, unsigned pos)
8583 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8586 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8587 enum isl_dim_type type, unsigned pos)
8589 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8592 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8593 enum isl_dim_type type, unsigned pos)
8595 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
8598 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8599 enum isl_dim_type type, unsigned pos)
8601 int i;
8603 if (!map)
8604 return -1;
8606 for (i = 0; i < map->n; ++i) {
8607 int bounded;
8608 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8609 if (bounded < 0 || !bounded)
8610 return bounded;
8613 return 1;
8616 /* Return 1 if the specified dim is involved in both an upper bound
8617 * and a lower bound.
8619 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8620 enum isl_dim_type type, unsigned pos)
8622 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8625 static int has_bound(__isl_keep isl_map *map,
8626 enum isl_dim_type type, unsigned pos,
8627 int (*fn)(__isl_keep isl_basic_map *bmap,
8628 enum isl_dim_type type, unsigned pos))
8630 int i;
8632 if (!map)
8633 return -1;
8635 for (i = 0; i < map->n; ++i) {
8636 int bounded;
8637 bounded = fn(map->p[i], type, pos);
8638 if (bounded < 0 || bounded)
8639 return bounded;
8642 return 0;
8645 /* Return 1 if the specified dim is involved in any lower bound.
8647 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
8648 enum isl_dim_type type, unsigned pos)
8650 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
8653 /* Return 1 if the specified dim is involved in any upper bound.
8655 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
8656 enum isl_dim_type type, unsigned pos)
8658 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
8661 /* For each of the "n" variables starting at "first", determine
8662 * the sign of the variable and put the results in the first "n"
8663 * elements of the array "signs".
8664 * Sign
8665 * 1 means that the variable is non-negative
8666 * -1 means that the variable is non-positive
8667 * 0 means the variable attains both positive and negative values.
8669 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8670 unsigned first, unsigned n, int *signs)
8672 isl_vec *bound = NULL;
8673 struct isl_tab *tab = NULL;
8674 struct isl_tab_undo *snap;
8675 int i;
8677 if (!bset || !signs)
8678 return -1;
8680 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8681 tab = isl_tab_from_basic_set(bset);
8682 if (!bound || !tab)
8683 goto error;
8685 isl_seq_clr(bound->el, bound->size);
8686 isl_int_set_si(bound->el[0], -1);
8688 snap = isl_tab_snap(tab);
8689 for (i = 0; i < n; ++i) {
8690 int empty;
8692 isl_int_set_si(bound->el[1 + first + i], -1);
8693 if (isl_tab_add_ineq(tab, bound->el) < 0)
8694 goto error;
8695 empty = tab->empty;
8696 isl_int_set_si(bound->el[1 + first + i], 0);
8697 if (isl_tab_rollback(tab, snap) < 0)
8698 goto error;
8700 if (empty) {
8701 signs[i] = 1;
8702 continue;
8705 isl_int_set_si(bound->el[1 + first + i], 1);
8706 if (isl_tab_add_ineq(tab, bound->el) < 0)
8707 goto error;
8708 empty = tab->empty;
8709 isl_int_set_si(bound->el[1 + first + i], 0);
8710 if (isl_tab_rollback(tab, snap) < 0)
8711 goto error;
8713 signs[i] = empty ? -1 : 0;
8716 isl_tab_free(tab);
8717 isl_vec_free(bound);
8718 return 0;
8719 error:
8720 isl_tab_free(tab);
8721 isl_vec_free(bound);
8722 return -1;
8725 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8726 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8728 if (!bset || !signs)
8729 return -1;
8730 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8731 return -1);
8733 first += pos(bset->dim, type) - 1;
8734 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8737 /* Check if the given basic map is obviously single-valued.
8738 * In particular, for each output dimension, check that there is
8739 * an equality that defines the output dimension in terms of
8740 * earlier dimensions.
8742 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8744 int i, j;
8745 unsigned total;
8746 unsigned n_out;
8747 unsigned o_out;
8749 if (!bmap)
8750 return -1;
8752 total = 1 + isl_basic_map_total_dim(bmap);
8753 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8754 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8756 for (i = 0; i < n_out; ++i) {
8757 for (j = 0; j < bmap->n_eq; ++j) {
8758 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8759 continue;
8760 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8761 total - (o_out + i + 1)) == -1)
8762 break;
8764 if (j >= bmap->n_eq)
8765 return 0;
8768 return 1;
8771 /* Check if the given map is obviously single-valued.
8773 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8775 if (!map)
8776 return -1;
8777 if (map->n == 0)
8778 return 1;
8779 if (map->n >= 2)
8780 return 0;
8782 return isl_basic_map_plain_is_single_valued(map->p[0]);
8785 /* Check if the given map is single-valued.
8786 * We simply compute
8788 * M \circ M^-1
8790 * and check if the result is a subset of the identity mapping.
8792 int isl_map_is_single_valued(__isl_keep isl_map *map)
8794 isl_space *dim;
8795 isl_map *test;
8796 isl_map *id;
8797 int sv;
8799 sv = isl_map_plain_is_single_valued(map);
8800 if (sv < 0 || sv)
8801 return sv;
8803 test = isl_map_reverse(isl_map_copy(map));
8804 test = isl_map_apply_range(test, isl_map_copy(map));
8806 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
8807 id = isl_map_identity(dim);
8809 sv = isl_map_is_subset(test, id);
8811 isl_map_free(test);
8812 isl_map_free(id);
8814 return sv;
8817 int isl_map_is_injective(__isl_keep isl_map *map)
8819 int in;
8821 map = isl_map_copy(map);
8822 map = isl_map_reverse(map);
8823 in = isl_map_is_single_valued(map);
8824 isl_map_free(map);
8826 return in;
8829 /* Check if the given map is obviously injective.
8831 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8833 int in;
8835 map = isl_map_copy(map);
8836 map = isl_map_reverse(map);
8837 in = isl_map_plain_is_single_valued(map);
8838 isl_map_free(map);
8840 return in;
8843 int isl_map_is_bijective(__isl_keep isl_map *map)
8845 int sv;
8847 sv = isl_map_is_single_valued(map);
8848 if (sv < 0 || !sv)
8849 return sv;
8851 return isl_map_is_injective(map);
8854 int isl_set_is_singleton(__isl_keep isl_set *set)
8856 return isl_map_is_single_valued((isl_map *)set);
8859 int isl_map_is_translation(__isl_keep isl_map *map)
8861 int ok;
8862 isl_set *delta;
8864 delta = isl_map_deltas(isl_map_copy(map));
8865 ok = isl_set_is_singleton(delta);
8866 isl_set_free(delta);
8868 return ok;
8871 static int unique(isl_int *p, unsigned pos, unsigned len)
8873 if (isl_seq_first_non_zero(p, pos) != -1)
8874 return 0;
8875 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8876 return 0;
8877 return 1;
8880 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8882 int i, j;
8883 unsigned nvar;
8884 unsigned ovar;
8886 if (!bset)
8887 return -1;
8889 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8890 return 0;
8892 nvar = isl_basic_set_dim(bset, isl_dim_set);
8893 ovar = isl_space_offset(bset->dim, isl_dim_set);
8894 for (j = 0; j < nvar; ++j) {
8895 int lower = 0, upper = 0;
8896 for (i = 0; i < bset->n_eq; ++i) {
8897 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8898 continue;
8899 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8900 return 0;
8901 break;
8903 if (i < bset->n_eq)
8904 continue;
8905 for (i = 0; i < bset->n_ineq; ++i) {
8906 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8907 continue;
8908 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8909 return 0;
8910 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8911 lower = 1;
8912 else
8913 upper = 1;
8915 if (!lower || !upper)
8916 return 0;
8919 return 1;
8922 int isl_set_is_box(__isl_keep isl_set *set)
8924 if (!set)
8925 return -1;
8926 if (set->n != 1)
8927 return 0;
8929 return isl_basic_set_is_box(set->p[0]);
8932 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8934 if (!bset)
8935 return -1;
8937 return isl_space_is_wrapping(bset->dim);
8940 int isl_set_is_wrapping(__isl_keep isl_set *set)
8942 if (!set)
8943 return -1;
8945 return isl_space_is_wrapping(set->dim);
8948 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8950 bmap = isl_basic_map_cow(bmap);
8951 if (!bmap)
8952 return NULL;
8954 bmap->dim = isl_space_wrap(bmap->dim);
8955 if (!bmap->dim)
8956 goto error;
8958 bmap = isl_basic_map_finalize(bmap);
8960 return (isl_basic_set *)bmap;
8961 error:
8962 isl_basic_map_free(bmap);
8963 return NULL;
8966 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8968 int i;
8970 map = isl_map_cow(map);
8971 if (!map)
8972 return NULL;
8974 for (i = 0; i < map->n; ++i) {
8975 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8976 if (!map->p[i])
8977 goto error;
8979 map->dim = isl_space_wrap(map->dim);
8980 if (!map->dim)
8981 goto error;
8983 return (isl_set *)map;
8984 error:
8985 isl_map_free(map);
8986 return NULL;
8989 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8991 bset = isl_basic_set_cow(bset);
8992 if (!bset)
8993 return NULL;
8995 bset->dim = isl_space_unwrap(bset->dim);
8996 if (!bset->dim)
8997 goto error;
8999 bset = isl_basic_set_finalize(bset);
9001 return (isl_basic_map *)bset;
9002 error:
9003 isl_basic_set_free(bset);
9004 return NULL;
9007 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
9009 int i;
9011 if (!set)
9012 return NULL;
9014 if (!isl_set_is_wrapping(set))
9015 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
9016 goto error);
9018 set = isl_set_cow(set);
9019 if (!set)
9020 return NULL;
9022 for (i = 0; i < set->n; ++i) {
9023 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9024 if (!set->p[i])
9025 goto error;
9028 set->dim = isl_space_unwrap(set->dim);
9029 if (!set->dim)
9030 goto error;
9032 return (isl_map *)set;
9033 error:
9034 isl_set_free(set);
9035 return NULL;
9038 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9039 enum isl_dim_type type)
9041 if (!bmap)
9042 return NULL;
9044 if (!isl_space_is_named_or_nested(bmap->dim, type))
9045 return bmap;
9047 bmap = isl_basic_map_cow(bmap);
9048 if (!bmap)
9049 return NULL;
9051 bmap->dim = isl_space_reset(bmap->dim, type);
9052 if (!bmap->dim)
9053 goto error;
9055 bmap = isl_basic_map_finalize(bmap);
9057 return bmap;
9058 error:
9059 isl_basic_map_free(bmap);
9060 return NULL;
9063 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
9064 enum isl_dim_type type)
9066 int i;
9068 if (!map)
9069 return NULL;
9071 if (!isl_space_is_named_or_nested(map->dim, type))
9072 return map;
9074 map = isl_map_cow(map);
9075 if (!map)
9076 return NULL;
9078 for (i = 0; i < map->n; ++i) {
9079 map->p[i] = isl_basic_map_reset(map->p[i], type);
9080 if (!map->p[i])
9081 goto error;
9083 map->dim = isl_space_reset(map->dim, type);
9084 if (!map->dim)
9085 goto error;
9087 return map;
9088 error:
9089 isl_map_free(map);
9090 return NULL;
9093 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
9095 if (!bmap)
9096 return NULL;
9098 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
9099 return bmap;
9101 bmap = isl_basic_map_cow(bmap);
9102 if (!bmap)
9103 return NULL;
9105 bmap->dim = isl_space_flatten(bmap->dim);
9106 if (!bmap->dim)
9107 goto error;
9109 bmap = isl_basic_map_finalize(bmap);
9111 return bmap;
9112 error:
9113 isl_basic_map_free(bmap);
9114 return NULL;
9117 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9119 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9122 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9123 __isl_take isl_basic_map *bmap)
9125 if (!bmap)
9126 return NULL;
9128 if (!bmap->dim->nested[0])
9129 return bmap;
9131 bmap = isl_basic_map_cow(bmap);
9132 if (!bmap)
9133 return NULL;
9135 bmap->dim = isl_space_flatten_domain(bmap->dim);
9136 if (!bmap->dim)
9137 goto error;
9139 bmap = isl_basic_map_finalize(bmap);
9141 return bmap;
9142 error:
9143 isl_basic_map_free(bmap);
9144 return NULL;
9147 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9148 __isl_take isl_basic_map *bmap)
9150 if (!bmap)
9151 return NULL;
9153 if (!bmap->dim->nested[1])
9154 return bmap;
9156 bmap = isl_basic_map_cow(bmap);
9157 if (!bmap)
9158 return NULL;
9160 bmap->dim = isl_space_flatten_range(bmap->dim);
9161 if (!bmap->dim)
9162 goto error;
9164 bmap = isl_basic_map_finalize(bmap);
9166 return bmap;
9167 error:
9168 isl_basic_map_free(bmap);
9169 return NULL;
9172 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9174 int i;
9176 if (!map)
9177 return NULL;
9179 if (!map->dim->nested[0] && !map->dim->nested[1])
9180 return map;
9182 map = isl_map_cow(map);
9183 if (!map)
9184 return NULL;
9186 for (i = 0; i < map->n; ++i) {
9187 map->p[i] = isl_basic_map_flatten(map->p[i]);
9188 if (!map->p[i])
9189 goto error;
9191 map->dim = isl_space_flatten(map->dim);
9192 if (!map->dim)
9193 goto error;
9195 return map;
9196 error:
9197 isl_map_free(map);
9198 return NULL;
9201 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9203 return (isl_set *)isl_map_flatten((isl_map *)set);
9206 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9208 isl_space *dim, *flat_dim;
9209 isl_map *map;
9211 dim = isl_set_get_space(set);
9212 flat_dim = isl_space_flatten(isl_space_copy(dim));
9213 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9214 map = isl_map_intersect_domain(map, set);
9216 return map;
9219 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9221 int i;
9223 if (!map)
9224 return NULL;
9226 if (!map->dim->nested[0])
9227 return map;
9229 map = isl_map_cow(map);
9230 if (!map)
9231 return NULL;
9233 for (i = 0; i < map->n; ++i) {
9234 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9235 if (!map->p[i])
9236 goto error;
9238 map->dim = isl_space_flatten_domain(map->dim);
9239 if (!map->dim)
9240 goto error;
9242 return map;
9243 error:
9244 isl_map_free(map);
9245 return NULL;
9248 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9250 int i;
9252 if (!map)
9253 return NULL;
9255 if (!map->dim->nested[1])
9256 return map;
9258 map = isl_map_cow(map);
9259 if (!map)
9260 return NULL;
9262 for (i = 0; i < map->n; ++i) {
9263 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9264 if (!map->p[i])
9265 goto error;
9267 map->dim = isl_space_flatten_range(map->dim);
9268 if (!map->dim)
9269 goto error;
9271 return map;
9272 error:
9273 isl_map_free(map);
9274 return NULL;
9277 /* Reorder the dimensions of "bmap" according to the given dim_map
9278 * and set the dimension specification to "dim".
9280 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9281 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9283 isl_basic_map *res;
9285 bmap = isl_basic_map_cow(bmap);
9286 if (!bmap || !dim || !dim_map)
9287 goto error;
9289 res = isl_basic_map_alloc_space(dim,
9290 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9291 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9292 res = isl_basic_map_finalize(res);
9293 return res;
9294 error:
9295 free(dim_map);
9296 isl_basic_map_free(bmap);
9297 isl_space_free(dim);
9298 return NULL;
9301 /* Reorder the dimensions of "map" according to given reordering.
9303 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9304 __isl_take isl_reordering *r)
9306 int i;
9307 struct isl_dim_map *dim_map;
9309 map = isl_map_cow(map);
9310 dim_map = isl_dim_map_from_reordering(r);
9311 if (!map || !r || !dim_map)
9312 goto error;
9314 for (i = 0; i < map->n; ++i) {
9315 struct isl_dim_map *dim_map_i;
9317 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9319 map->p[i] = isl_basic_map_realign(map->p[i],
9320 isl_space_copy(r->dim), dim_map_i);
9322 if (!map->p[i])
9323 goto error;
9326 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9328 isl_reordering_free(r);
9329 free(dim_map);
9330 return map;
9331 error:
9332 free(dim_map);
9333 isl_map_free(map);
9334 isl_reordering_free(r);
9335 return NULL;
9338 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9339 __isl_take isl_reordering *r)
9341 return (isl_set *)isl_map_realign((isl_map *)set, r);
9344 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9345 __isl_take isl_space *model)
9347 isl_ctx *ctx;
9349 if (!map || !model)
9350 goto error;
9352 ctx = isl_space_get_ctx(model);
9353 if (!isl_space_has_named_params(model))
9354 isl_die(ctx, isl_error_invalid,
9355 "model has unnamed parameters", goto error);
9356 if (!isl_space_has_named_params(map->dim))
9357 isl_die(ctx, isl_error_invalid,
9358 "relation has unnamed parameters", goto error);
9359 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9360 isl_reordering *exp;
9362 model = isl_space_drop_dims(model, isl_dim_in,
9363 0, isl_space_dim(model, isl_dim_in));
9364 model = isl_space_drop_dims(model, isl_dim_out,
9365 0, isl_space_dim(model, isl_dim_out));
9366 exp = isl_parameter_alignment_reordering(map->dim, model);
9367 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9368 map = isl_map_realign(map, exp);
9371 isl_space_free(model);
9372 return map;
9373 error:
9374 isl_space_free(model);
9375 isl_map_free(map);
9376 return NULL;
9379 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9380 __isl_take isl_space *model)
9382 return isl_map_align_params(set, model);
9385 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9386 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9387 enum isl_dim_type c2, enum isl_dim_type c3,
9388 enum isl_dim_type c4, enum isl_dim_type c5)
9390 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9391 struct isl_mat *mat;
9392 int i, j, k;
9393 int pos;
9395 if (!bmap)
9396 return NULL;
9397 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9398 isl_basic_map_total_dim(bmap) + 1);
9399 if (!mat)
9400 return NULL;
9401 for (i = 0; i < bmap->n_eq; ++i)
9402 for (j = 0, pos = 0; j < 5; ++j) {
9403 int off = isl_basic_map_offset(bmap, c[j]);
9404 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9405 isl_int_set(mat->row[i][pos],
9406 bmap->eq[i][off + k]);
9407 ++pos;
9411 return mat;
9414 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9415 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9416 enum isl_dim_type c2, enum isl_dim_type c3,
9417 enum isl_dim_type c4, enum isl_dim_type c5)
9419 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9420 struct isl_mat *mat;
9421 int i, j, k;
9422 int pos;
9424 if (!bmap)
9425 return NULL;
9426 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9427 isl_basic_map_total_dim(bmap) + 1);
9428 if (!mat)
9429 return NULL;
9430 for (i = 0; i < bmap->n_ineq; ++i)
9431 for (j = 0, pos = 0; j < 5; ++j) {
9432 int off = isl_basic_map_offset(bmap, c[j]);
9433 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9434 isl_int_set(mat->row[i][pos],
9435 bmap->ineq[i][off + k]);
9436 ++pos;
9440 return mat;
9443 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9444 __isl_take isl_space *dim,
9445 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9446 enum isl_dim_type c2, enum isl_dim_type c3,
9447 enum isl_dim_type c4, enum isl_dim_type c5)
9449 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9450 isl_basic_map *bmap;
9451 unsigned total;
9452 unsigned extra;
9453 int i, j, k, l;
9454 int pos;
9456 if (!dim || !eq || !ineq)
9457 goto error;
9459 if (eq->n_col != ineq->n_col)
9460 isl_die(dim->ctx, isl_error_invalid,
9461 "equalities and inequalities matrices should have "
9462 "same number of columns", goto error);
9464 total = 1 + isl_space_dim(dim, isl_dim_all);
9466 if (eq->n_col < total)
9467 isl_die(dim->ctx, isl_error_invalid,
9468 "number of columns too small", goto error);
9470 extra = eq->n_col - total;
9472 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9473 eq->n_row, ineq->n_row);
9474 if (!bmap)
9475 goto error;
9476 for (i = 0; i < extra; ++i) {
9477 k = isl_basic_map_alloc_div(bmap);
9478 if (k < 0)
9479 goto error;
9480 isl_int_set_si(bmap->div[k][0], 0);
9482 for (i = 0; i < eq->n_row; ++i) {
9483 l = isl_basic_map_alloc_equality(bmap);
9484 if (l < 0)
9485 goto error;
9486 for (j = 0, pos = 0; j < 5; ++j) {
9487 int off = isl_basic_map_offset(bmap, c[j]);
9488 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9489 isl_int_set(bmap->eq[l][off + k],
9490 eq->row[i][pos]);
9491 ++pos;
9495 for (i = 0; i < ineq->n_row; ++i) {
9496 l = isl_basic_map_alloc_inequality(bmap);
9497 if (l < 0)
9498 goto error;
9499 for (j = 0, pos = 0; j < 5; ++j) {
9500 int off = isl_basic_map_offset(bmap, c[j]);
9501 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9502 isl_int_set(bmap->ineq[l][off + k],
9503 ineq->row[i][pos]);
9504 ++pos;
9509 isl_space_free(dim);
9510 isl_mat_free(eq);
9511 isl_mat_free(ineq);
9513 return bmap;
9514 error:
9515 isl_space_free(dim);
9516 isl_mat_free(eq);
9517 isl_mat_free(ineq);
9518 return NULL;
9521 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9522 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9523 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9525 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9526 c1, c2, c3, c4, isl_dim_in);
9529 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9530 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9531 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9533 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9534 c1, c2, c3, c4, isl_dim_in);
9537 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9538 __isl_take isl_space *dim,
9539 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9540 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9542 return (isl_basic_set*)
9543 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9544 c1, c2, c3, c4, isl_dim_in);
9547 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9549 if (!bmap)
9550 return -1;
9552 return isl_space_can_zip(bmap->dim);
9555 int isl_map_can_zip(__isl_keep isl_map *map)
9557 if (!map)
9558 return -1;
9560 return isl_space_can_zip(map->dim);
9563 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9564 * (A -> C) -> (B -> D).
9566 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9568 unsigned pos;
9569 unsigned n1;
9570 unsigned n2;
9572 if (!bmap)
9573 return NULL;
9575 if (!isl_basic_map_can_zip(bmap))
9576 isl_die(bmap->ctx, isl_error_invalid,
9577 "basic map cannot be zipped", goto error);
9578 pos = isl_basic_map_offset(bmap, isl_dim_in) +
9579 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
9580 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
9581 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
9582 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9583 if (!bmap)
9584 return NULL;
9585 bmap->dim = isl_space_zip(bmap->dim);
9586 if (!bmap->dim)
9587 goto error;
9588 return bmap;
9589 error:
9590 isl_basic_map_free(bmap);
9591 return NULL;
9594 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9595 * (A -> C) -> (B -> D).
9597 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9599 int i;
9601 if (!map)
9602 return NULL;
9604 if (!isl_map_can_zip(map))
9605 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9606 goto error);
9608 map = isl_map_cow(map);
9609 if (!map)
9610 return NULL;
9612 for (i = 0; i < map->n; ++i) {
9613 map->p[i] = isl_basic_map_zip(map->p[i]);
9614 if (!map->p[i])
9615 goto error;
9618 map->dim = isl_space_zip(map->dim);
9619 if (!map->dim)
9620 goto error;
9622 return map;
9623 error:
9624 isl_map_free(map);
9625 return NULL;
9628 /* Construct a basic map mapping the domain of the affine expression
9629 * to a one-dimensional range prescribed by the affine expression.
9631 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9633 int k;
9634 int pos;
9635 isl_local_space *ls;
9636 isl_basic_map *bmap;
9638 if (!aff)
9639 return NULL;
9641 ls = isl_aff_get_local_space(aff);
9642 bmap = isl_basic_map_from_local_space(ls);
9643 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9644 k = isl_basic_map_alloc_equality(bmap);
9645 if (k < 0)
9646 goto error;
9648 pos = isl_basic_map_offset(bmap, isl_dim_out);
9649 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9650 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9651 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9652 aff->v->size - (pos + 1));
9654 isl_aff_free(aff);
9655 bmap = isl_basic_map_finalize(bmap);
9656 return bmap;
9657 error:
9658 isl_aff_free(aff);
9659 isl_basic_map_free(bmap);
9660 return NULL;
9663 /* Construct a basic map mapping the domain the multi-affine expression
9664 * to its range, with each dimension in the range equated to the
9665 * corresponding affine expression.
9667 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
9668 __isl_take isl_multi_aff *maff)
9670 int i;
9671 isl_space *space;
9672 isl_basic_map *bmap;
9674 if (!maff)
9675 return NULL;
9677 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
9678 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
9679 "invalid space", return isl_multi_aff_free(maff));
9681 space = isl_space_domain(isl_multi_aff_get_space(maff));
9682 bmap = isl_basic_map_universe(isl_space_from_domain(space));
9684 for (i = 0; i < maff->n; ++i) {
9685 isl_aff *aff;
9686 isl_basic_map *bmap_i;
9688 aff = isl_aff_copy(maff->p[i]);
9689 bmap_i = isl_basic_map_from_aff(aff);
9691 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9694 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
9696 isl_multi_aff_free(maff);
9697 return bmap;
9700 /* Construct a basic map mapping a domain in the given space to
9701 * to an n-dimensional range, with n the number of elements in the list,
9702 * where each coordinate in the range is prescribed by the
9703 * corresponding affine expression.
9704 * The domains of all affine expressions in the list are assumed to match
9705 * domain_dim.
9707 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
9708 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
9710 int i;
9711 isl_space *dim;
9712 isl_basic_map *bmap;
9714 if (!list)
9715 return NULL;
9717 dim = isl_space_from_domain(domain_dim);
9718 bmap = isl_basic_map_universe(dim);
9720 for (i = 0; i < list->n; ++i) {
9721 isl_aff *aff;
9722 isl_basic_map *bmap_i;
9724 aff = isl_aff_copy(list->p[i]);
9725 bmap_i = isl_basic_map_from_aff(aff);
9727 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9730 isl_aff_list_free(list);
9731 return bmap;
9734 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
9735 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9737 return isl_map_equate(set, type1, pos1, type2, pos2);
9740 /* Add a constraint imposing that the given two dimensions are equal.
9742 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
9743 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9745 isl_basic_map *bmap = NULL;
9746 int i;
9748 if (!map)
9749 return NULL;
9751 if (pos1 >= isl_map_dim(map, type1))
9752 isl_die(map->ctx, isl_error_invalid,
9753 "index out of bounds", goto error);
9754 if (pos2 >= isl_map_dim(map, type2))
9755 isl_die(map->ctx, isl_error_invalid,
9756 "index out of bounds", goto error);
9758 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9759 i = isl_basic_map_alloc_equality(bmap);
9760 if (i < 0)
9761 goto error;
9762 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9763 pos1 += isl_basic_map_offset(bmap, type1);
9764 pos2 += isl_basic_map_offset(bmap, type2);
9765 isl_int_set_si(bmap->eq[i][pos1], -1);
9766 isl_int_set_si(bmap->eq[i][pos2], 1);
9767 bmap = isl_basic_map_finalize(bmap);
9769 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9771 return map;
9772 error:
9773 isl_basic_map_free(bmap);
9774 isl_map_free(map);
9775 return NULL;
9778 /* Add a constraint imposing that the given two dimensions have opposite values.
9780 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
9781 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9783 isl_basic_map *bmap = NULL;
9784 int i;
9786 if (!map)
9787 return NULL;
9789 if (pos1 >= isl_map_dim(map, type1))
9790 isl_die(map->ctx, isl_error_invalid,
9791 "index out of bounds", goto error);
9792 if (pos2 >= isl_map_dim(map, type2))
9793 isl_die(map->ctx, isl_error_invalid,
9794 "index out of bounds", goto error);
9796 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9797 i = isl_basic_map_alloc_equality(bmap);
9798 if (i < 0)
9799 goto error;
9800 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9801 pos1 += isl_basic_map_offset(bmap, type1);
9802 pos2 += isl_basic_map_offset(bmap, type2);
9803 isl_int_set_si(bmap->eq[i][pos1], 1);
9804 isl_int_set_si(bmap->eq[i][pos2], 1);
9805 bmap = isl_basic_map_finalize(bmap);
9807 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9809 return map;
9810 error:
9811 isl_basic_map_free(bmap);
9812 isl_map_free(map);
9813 return NULL;
9816 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
9817 int pos)
9819 isl_aff *div;
9820 isl_local_space *ls;
9822 if (!bmap)
9823 return NULL;
9825 if (!isl_basic_map_divs_known(bmap))
9826 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
9827 "some divs are unknown", return NULL);
9829 ls = isl_basic_map_get_local_space(bmap);
9830 div = isl_local_space_get_div(ls, pos);
9831 isl_local_space_free(ls);
9833 return div;
9836 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
9837 int pos)
9839 return isl_basic_map_get_div(bset, pos);
9842 /* Plug in "subs" for dimension "type", "pos" of "bset".
9844 * Let i be the dimension to replace and let "subs" be of the form
9846 * f/d
9848 * Any integer division with a non-zero coefficient for i,
9850 * floor((a i + g)/m)
9852 * is replaced by
9854 * floor((a f + d g)/(m d))
9856 * Constraints of the form
9858 * a i + g
9860 * are replaced by
9862 * a f + d g
9864 __isl_give isl_basic_set *isl_basic_set_substitute(
9865 __isl_take isl_basic_set *bset,
9866 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
9868 int i;
9869 isl_int v;
9870 isl_ctx *ctx;
9872 if (bset && isl_basic_set_plain_is_empty(bset))
9873 return bset;
9875 bset = isl_basic_set_cow(bset);
9876 if (!bset || !subs)
9877 goto error;
9879 ctx = isl_basic_set_get_ctx(bset);
9880 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
9881 isl_die(ctx, isl_error_invalid,
9882 "spaces don't match", goto error);
9883 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
9884 isl_die(ctx, isl_error_unsupported,
9885 "cannot handle divs yet", goto error);
9887 pos += isl_basic_set_offset(bset, type);
9889 isl_int_init(v);
9891 for (i = 0; i < bset->n_eq; ++i) {
9892 if (isl_int_is_zero(bset->eq[i][pos]))
9893 continue;
9894 isl_int_set(v, bset->eq[i][pos]);
9895 isl_int_set_si(bset->eq[i][pos], 0);
9896 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
9897 v, subs->v->el + 1, subs->v->size - 1);
9900 for (i = 0; i < bset->n_ineq; ++i) {
9901 if (isl_int_is_zero(bset->ineq[i][pos]))
9902 continue;
9903 isl_int_set(v, bset->ineq[i][pos]);
9904 isl_int_set_si(bset->ineq[i][pos], 0);
9905 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
9906 v, subs->v->el + 1, subs->v->size - 1);
9909 for (i = 0; i < bset->n_div; ++i) {
9910 if (isl_int_is_zero(bset->div[i][1 + pos]))
9911 continue;
9912 isl_int_set(v, bset->div[i][1 + pos]);
9913 isl_int_set_si(bset->div[i][1 + pos], 0);
9914 isl_seq_combine(bset->div[i] + 1,
9915 subs->v->el[0], bset->div[i] + 1,
9916 v, subs->v->el + 1, subs->v->size - 1);
9917 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
9920 isl_int_clear(v);
9922 bset = isl_basic_set_simplify(bset);
9923 return isl_basic_set_finalize(bset);
9924 error:
9925 isl_basic_set_free(bset);
9926 return NULL;
9929 /* Plug in "subs" for dimension "type", "pos" of "set".
9931 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
9932 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
9934 int i;
9936 if (set && isl_set_plain_is_empty(set))
9937 return set;
9939 set = isl_set_cow(set);
9940 if (!set || !subs)
9941 goto error;
9943 for (i = set->n - 1; i >= 0; --i) {
9944 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
9945 if (!set->p[i])
9946 goto error;
9947 if (isl_basic_set_plain_is_empty(set->p[i])) {
9948 isl_basic_set_free(set->p[i]);
9949 if (i != set->n - 1)
9950 set->p[i] = set->p[set->n - 1];
9951 set->n--;
9955 return set;
9956 error:
9957 isl_set_free(set);
9958 return NULL;