add isl_basic_{set,map}_insert_dims
[isl.git] / isl_map.c
blobec96252d5f9237f4b2a99714385ac3c98f0f8fbb
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012 Ecole Normale Superieure
6 * Use of this software is governed by the GNU LGPLv2.1 license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <string.h>
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include <isl/blk.h>
19 #include "isl_space_private.h"
20 #include "isl_equalities.h"
21 #include <isl_list_private.h>
22 #include <isl/lp.h>
23 #include <isl/seq.h>
24 #include <isl/set.h>
25 #include <isl/map.h>
26 #include "isl_map_piplib.h"
27 #include <isl_reordering.h>
28 #include "isl_sample.h"
29 #include "isl_tab.h"
30 #include <isl/vec.h>
31 #include <isl_mat_private.h>
32 #include <isl_dim_map.h>
33 #include <isl_local_space_private.h>
34 #include <isl_aff_private.h>
35 #include <isl_options_private.h>
37 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
39 switch (type) {
40 case isl_dim_param: return dim->nparam;
41 case isl_dim_in: return dim->n_in;
42 case isl_dim_out: return dim->n_out;
43 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
44 default: return 0;
48 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
50 switch (type) {
51 case isl_dim_param: return 1;
52 case isl_dim_in: return 1 + dim->nparam;
53 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
54 default: return 0;
58 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
59 enum isl_dim_type type)
61 if (!bmap)
62 return 0;
63 switch (type) {
64 case isl_dim_cst: return 1;
65 case isl_dim_param:
66 case isl_dim_in:
67 case isl_dim_out: return isl_space_dim(bmap->dim, type);
68 case isl_dim_div: return bmap->n_div;
69 case isl_dim_all: return isl_basic_map_total_dim(bmap);
70 default: return 0;
74 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
76 return map ? n(map->dim, type) : 0;
79 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
81 return set ? n(set->dim, type) : 0;
84 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
85 enum isl_dim_type type)
87 isl_space *dim = bmap->dim;
88 switch (type) {
89 case isl_dim_cst: return 0;
90 case isl_dim_param: return 1;
91 case isl_dim_in: return 1 + dim->nparam;
92 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
93 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
94 default: return 0;
98 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
99 enum isl_dim_type type)
101 return isl_basic_map_offset(bset, type);
104 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
106 return pos(map->dim, type);
109 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
110 enum isl_dim_type type)
112 return isl_basic_map_dim(bset, type);
115 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
117 return isl_basic_set_dim(bset, isl_dim_set);
120 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
122 return isl_basic_set_dim(bset, isl_dim_param);
125 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
127 if (!bset)
128 return 0;
129 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
132 unsigned isl_set_n_dim(__isl_keep isl_set *set)
134 return isl_set_dim(set, isl_dim_set);
137 unsigned isl_set_n_param(__isl_keep isl_set *set)
139 return isl_set_dim(set, isl_dim_param);
142 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
144 return bmap ? bmap->dim->n_in : 0;
147 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
149 return bmap ? bmap->dim->n_out : 0;
152 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
154 return bmap ? bmap->dim->nparam : 0;
157 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
159 return bmap ? bmap->n_div : 0;
162 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
164 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
167 unsigned isl_map_n_in(const struct isl_map *map)
169 return map ? map->dim->n_in : 0;
172 unsigned isl_map_n_out(const struct isl_map *map)
174 return map ? map->dim->n_out : 0;
177 unsigned isl_map_n_param(const struct isl_map *map)
179 return map ? map->dim->nparam : 0;
182 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
184 int m;
185 if (!map || !set)
186 return -1;
187 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
188 if (m < 0 || !m)
189 return m;
190 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
193 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
194 struct isl_basic_set *bset)
196 int m;
197 if (!bmap || !bset)
198 return -1;
199 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
200 if (m < 0 || !m)
201 return m;
202 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
205 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
207 int m;
208 if (!map || !set)
209 return -1;
210 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
211 if (m < 0 || !m)
212 return m;
213 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
216 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
217 struct isl_basic_set *bset)
219 int m;
220 if (!bmap || !bset)
221 return -1;
222 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
223 if (m < 0 || !m)
224 return m;
225 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
228 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
230 return bmap ? bmap->ctx : NULL;
233 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
235 return bset ? bset->ctx : NULL;
238 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
240 return map ? map->ctx : NULL;
243 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
245 return set ? set->ctx : NULL;
248 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
250 if (!bmap)
251 return NULL;
252 return isl_space_copy(bmap->dim);
255 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
257 if (!bset)
258 return NULL;
259 return isl_space_copy(bset->dim);
262 /* Extract the divs in "bmap" as a matrix.
264 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
266 int i;
267 isl_ctx *ctx;
268 isl_mat *div;
269 unsigned total;
270 unsigned cols;
272 if (!bmap)
273 return NULL;
275 ctx = isl_basic_map_get_ctx(bmap);
276 total = isl_space_dim(bmap->dim, isl_dim_all);
277 cols = 1 + 1 + total + bmap->n_div;
278 div = isl_mat_alloc(ctx, bmap->n_div, cols);
279 if (!div)
280 return NULL;
282 for (i = 0; i < bmap->n_div; ++i)
283 isl_seq_cpy(div->row[i], bmap->div[i], cols);
285 return div;
288 __isl_give isl_local_space *isl_basic_map_get_local_space(
289 __isl_keep isl_basic_map *bmap)
291 isl_mat *div;
293 if (!bmap)
294 return NULL;
296 div = isl_basic_map_get_divs(bmap);
297 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
300 __isl_give isl_local_space *isl_basic_set_get_local_space(
301 __isl_keep isl_basic_set *bset)
303 return isl_basic_map_get_local_space(bset);
306 __isl_give isl_basic_map *isl_basic_map_from_local_space(
307 __isl_take isl_local_space *ls)
309 int i;
310 int n_div;
311 isl_basic_map *bmap;
313 if (!ls)
314 return NULL;
316 n_div = isl_local_space_dim(ls, isl_dim_div);
317 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
318 n_div, 0, 2 * n_div);
320 for (i = 0; i < n_div; ++i)
321 if (isl_basic_map_alloc_div(bmap) < 0)
322 goto error;
324 for (i = 0; i < n_div; ++i) {
325 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
326 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
327 goto error;
330 isl_local_space_free(ls);
331 return bmap;
332 error:
333 isl_local_space_free(ls);
334 isl_basic_map_free(bmap);
335 return NULL;
338 __isl_give isl_basic_set *isl_basic_set_from_local_space(
339 __isl_take isl_local_space *ls)
341 return isl_basic_map_from_local_space(ls);
344 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
346 if (!map)
347 return NULL;
348 return isl_space_copy(map->dim);
351 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
353 if (!set)
354 return NULL;
355 return isl_space_copy(set->dim);
358 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
359 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
361 bmap = isl_basic_map_cow(bmap);
362 if (!bmap)
363 return NULL;
364 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
365 if (!bmap->dim)
366 goto error;
367 bmap = isl_basic_map_finalize(bmap);
368 return bmap;
369 error:
370 isl_basic_map_free(bmap);
371 return NULL;
374 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
375 __isl_take isl_basic_set *bset, const char *s)
377 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
380 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
381 enum isl_dim_type type)
383 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
386 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
387 enum isl_dim_type type, const char *s)
389 int i;
391 map = isl_map_cow(map);
392 if (!map)
393 return NULL;
395 map->dim = isl_space_set_tuple_name(map->dim, type, s);
396 if (!map->dim)
397 goto error;
399 for (i = 0; i < map->n; ++i) {
400 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
401 if (!map->p[i])
402 goto error;
405 return map;
406 error:
407 isl_map_free(map);
408 return NULL;
411 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
412 enum isl_dim_type type)
414 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
417 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
418 const char *s)
420 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
423 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
424 enum isl_dim_type type, __isl_take isl_id *id)
426 map = isl_map_cow(map);
427 if (!map)
428 return isl_id_free(id);
430 map->dim = isl_space_set_tuple_id(map->dim, type, id);
432 return isl_map_reset_space(map, isl_space_copy(map->dim));
435 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
436 __isl_take isl_id *id)
438 return isl_map_set_tuple_id(set, isl_dim_set, id);
441 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
442 enum isl_dim_type type)
444 map = isl_map_cow(map);
445 if (!map)
446 return NULL;
448 map->dim = isl_space_reset_tuple_id(map->dim, type);
450 return isl_map_reset_space(map, isl_space_copy(map->dim));
453 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
455 return isl_map_reset_tuple_id(set, isl_dim_set);
458 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
460 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
463 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
464 enum isl_dim_type type)
466 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
469 int isl_set_has_tuple_id(__isl_keep isl_set *set)
471 return isl_map_has_tuple_id(set, isl_dim_set);
474 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
476 return isl_map_get_tuple_id(set, isl_dim_set);
479 /* Does the set tuple have a name?
481 int isl_set_has_tuple_name(__isl_keep isl_set *set)
483 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
487 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
489 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
492 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
494 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
497 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
498 enum isl_dim_type type, unsigned pos)
500 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
503 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
504 enum isl_dim_type type, unsigned pos)
506 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
509 /* Does the given dimension have a name?
511 int isl_map_has_dim_name(__isl_keep isl_map *map,
512 enum isl_dim_type type, unsigned pos)
514 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
517 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
518 enum isl_dim_type type, unsigned pos)
520 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
523 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
524 enum isl_dim_type type, unsigned pos)
526 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
529 /* Does the given dimension have a name?
531 int isl_set_has_dim_name(__isl_keep isl_set *set,
532 enum isl_dim_type type, unsigned pos)
534 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
537 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
538 __isl_take isl_basic_map *bmap,
539 enum isl_dim_type type, unsigned pos, const char *s)
541 bmap = isl_basic_map_cow(bmap);
542 if (!bmap)
543 return NULL;
544 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
545 if (!bmap->dim)
546 goto error;
547 return isl_basic_map_finalize(bmap);
548 error:
549 isl_basic_map_free(bmap);
550 return NULL;
553 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
554 enum isl_dim_type type, unsigned pos, const char *s)
556 int i;
558 map = isl_map_cow(map);
559 if (!map)
560 return NULL;
562 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
563 if (!map->dim)
564 goto error;
566 for (i = 0; i < map->n; ++i) {
567 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
568 if (!map->p[i])
569 goto error;
572 return map;
573 error:
574 isl_map_free(map);
575 return NULL;
578 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
579 __isl_take isl_basic_set *bset,
580 enum isl_dim_type type, unsigned pos, const char *s)
582 return (isl_basic_set *)isl_basic_map_set_dim_name(
583 (isl_basic_map *)bset, type, pos, s);
586 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
587 enum isl_dim_type type, unsigned pos, const char *s)
589 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
592 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
593 enum isl_dim_type type, unsigned pos)
595 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
598 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
599 enum isl_dim_type type, unsigned pos)
601 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
604 int isl_map_has_dim_id(__isl_keep isl_map *map,
605 enum isl_dim_type type, unsigned pos)
607 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
610 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
611 enum isl_dim_type type, unsigned pos)
613 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
616 int isl_set_has_dim_id(__isl_keep isl_set *set,
617 enum isl_dim_type type, unsigned pos)
619 return isl_map_has_dim_id(set, type, pos);
622 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
623 enum isl_dim_type type, unsigned pos)
625 return isl_map_get_dim_id(set, type, pos);
628 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
629 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
631 map = isl_map_cow(map);
632 if (!map)
633 return isl_id_free(id);
635 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
637 return isl_map_reset_space(map, isl_space_copy(map->dim));
640 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
641 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
643 return isl_map_set_dim_id(set, type, pos, id);
646 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
647 __isl_keep isl_id *id)
649 if (!map)
650 return -1;
651 return isl_space_find_dim_by_id(map->dim, type, id);
654 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
655 __isl_keep isl_id *id)
657 return isl_map_find_dim_by_id(set, type, id);
660 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
661 const char *name)
663 if (!map)
664 return -1;
665 return isl_space_find_dim_by_name(map->dim, type, name);
668 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
669 const char *name)
671 return isl_map_find_dim_by_name(set, type, name);
674 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
676 if (!bmap)
677 return -1;
678 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
681 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
683 return isl_basic_map_is_rational(bset);
686 /* Is this basic set a parameter domain?
688 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
690 if (!bset)
691 return -1;
692 return isl_space_is_params(bset->dim);
695 /* Is this set a parameter domain?
697 int isl_set_is_params(__isl_keep isl_set *set)
699 if (!set)
700 return -1;
701 return isl_space_is_params(set->dim);
704 /* Is this map actually a parameter domain?
705 * Users should never call this function. Outside of isl,
706 * a map can never be a parameter domain.
708 int isl_map_is_params(__isl_keep isl_map *map)
710 if (!map)
711 return -1;
712 return isl_space_is_params(map->dim);
715 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
716 struct isl_basic_map *bmap, unsigned extra,
717 unsigned n_eq, unsigned n_ineq)
719 int i;
720 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
722 bmap->ctx = ctx;
723 isl_ctx_ref(ctx);
725 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
726 if (isl_blk_is_error(bmap->block))
727 goto error;
729 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
730 if (!bmap->ineq)
731 goto error;
733 if (extra == 0) {
734 bmap->block2 = isl_blk_empty();
735 bmap->div = NULL;
736 } else {
737 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
738 if (isl_blk_is_error(bmap->block2))
739 goto error;
741 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
742 if (!bmap->div)
743 goto error;
746 for (i = 0; i < n_ineq + n_eq; ++i)
747 bmap->ineq[i] = bmap->block.data + i * row_size;
749 for (i = 0; i < extra; ++i)
750 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
752 bmap->ref = 1;
753 bmap->flags = 0;
754 bmap->c_size = n_eq + n_ineq;
755 bmap->eq = bmap->ineq + n_ineq;
756 bmap->extra = extra;
757 bmap->n_eq = 0;
758 bmap->n_ineq = 0;
759 bmap->n_div = 0;
760 bmap->sample = NULL;
762 return bmap;
763 error:
764 isl_basic_map_free(bmap);
765 return NULL;
768 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
769 unsigned nparam, unsigned dim, unsigned extra,
770 unsigned n_eq, unsigned n_ineq)
772 struct isl_basic_map *bmap;
773 isl_space *space;
775 space = isl_space_set_alloc(ctx, nparam, dim);
776 if (!space)
777 return NULL;
779 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
780 return (struct isl_basic_set *)bmap;
783 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
784 unsigned extra, unsigned n_eq, unsigned n_ineq)
786 struct isl_basic_map *bmap;
787 if (!dim)
788 return NULL;
789 isl_assert(dim->ctx, dim->n_in == 0, goto error);
790 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
791 return (struct isl_basic_set *)bmap;
792 error:
793 isl_space_free(dim);
794 return NULL;
797 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
798 unsigned extra, unsigned n_eq, unsigned n_ineq)
800 struct isl_basic_map *bmap;
802 if (!dim)
803 return NULL;
804 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
805 if (!bmap)
806 goto error;
807 bmap->dim = dim;
809 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
810 error:
811 isl_space_free(dim);
812 return NULL;
815 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
816 unsigned nparam, unsigned in, unsigned out, unsigned extra,
817 unsigned n_eq, unsigned n_ineq)
819 struct isl_basic_map *bmap;
820 isl_space *dim;
822 dim = isl_space_alloc(ctx, nparam, in, out);
823 if (!dim)
824 return NULL;
826 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
827 return bmap;
830 static void dup_constraints(
831 struct isl_basic_map *dst, struct isl_basic_map *src)
833 int i;
834 unsigned total = isl_basic_map_total_dim(src);
836 for (i = 0; i < src->n_eq; ++i) {
837 int j = isl_basic_map_alloc_equality(dst);
838 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
841 for (i = 0; i < src->n_ineq; ++i) {
842 int j = isl_basic_map_alloc_inequality(dst);
843 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
846 for (i = 0; i < src->n_div; ++i) {
847 int j = isl_basic_map_alloc_div(dst);
848 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
850 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
853 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
855 struct isl_basic_map *dup;
857 if (!bmap)
858 return NULL;
859 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
860 bmap->n_div, bmap->n_eq, bmap->n_ineq);
861 if (!dup)
862 return NULL;
863 dup_constraints(dup, bmap);
864 dup->flags = bmap->flags;
865 dup->sample = isl_vec_copy(bmap->sample);
866 return dup;
869 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
871 struct isl_basic_map *dup;
873 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
874 return (struct isl_basic_set *)dup;
877 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
879 if (!bset)
880 return NULL;
882 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
883 bset->ref++;
884 return bset;
886 return isl_basic_set_dup(bset);
889 struct isl_set *isl_set_copy(struct isl_set *set)
891 if (!set)
892 return NULL;
894 set->ref++;
895 return set;
898 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
900 if (!bmap)
901 return NULL;
903 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
904 bmap->ref++;
905 return bmap;
907 bmap = isl_basic_map_dup(bmap);
908 if (bmap)
909 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
910 return bmap;
913 struct isl_map *isl_map_copy(struct isl_map *map)
915 if (!map)
916 return NULL;
918 map->ref++;
919 return map;
922 void isl_basic_map_free(struct isl_basic_map *bmap)
924 if (!bmap)
925 return;
927 if (--bmap->ref > 0)
928 return;
930 isl_ctx_deref(bmap->ctx);
931 free(bmap->div);
932 isl_blk_free(bmap->ctx, bmap->block2);
933 free(bmap->ineq);
934 isl_blk_free(bmap->ctx, bmap->block);
935 isl_vec_free(bmap->sample);
936 isl_space_free(bmap->dim);
937 free(bmap);
940 void isl_basic_set_free(struct isl_basic_set *bset)
942 isl_basic_map_free((struct isl_basic_map *)bset);
945 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
947 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
950 __isl_give isl_map *isl_map_align_params_map_map_and(
951 __isl_take isl_map *map1, __isl_take isl_map *map2,
952 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
953 __isl_take isl_map *map2))
955 if (!map1 || !map2)
956 goto error;
957 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
958 return fn(map1, map2);
959 if (!isl_space_has_named_params(map1->dim) ||
960 !isl_space_has_named_params(map2->dim))
961 isl_die(map1->ctx, isl_error_invalid,
962 "unaligned unnamed parameters", goto error);
963 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
964 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
965 return fn(map1, map2);
966 error:
967 isl_map_free(map1);
968 isl_map_free(map2);
969 return NULL;
972 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
973 __isl_keep isl_map *map2,
974 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
976 int r;
978 if (!map1 || !map2)
979 return -1;
980 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
981 return fn(map1, map2);
982 if (!isl_space_has_named_params(map1->dim) ||
983 !isl_space_has_named_params(map2->dim))
984 isl_die(map1->ctx, isl_error_invalid,
985 "unaligned unnamed parameters", return -1);
986 map1 = isl_map_copy(map1);
987 map2 = isl_map_copy(map2);
988 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
989 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
990 r = fn(map1, map2);
991 isl_map_free(map1);
992 isl_map_free(map2);
993 return r;
996 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
998 struct isl_ctx *ctx;
999 if (!bmap)
1000 return -1;
1001 ctx = bmap->ctx;
1002 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1003 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1004 return -1);
1005 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1006 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1007 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1008 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1009 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1010 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1011 isl_int *t;
1012 int j = isl_basic_map_alloc_inequality(bmap);
1013 if (j < 0)
1014 return -1;
1015 t = bmap->ineq[j];
1016 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1017 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1018 bmap->eq[-1] = t;
1019 bmap->n_eq++;
1020 bmap->n_ineq--;
1021 bmap->eq--;
1022 return 0;
1024 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1025 bmap->extra - bmap->n_div);
1026 return bmap->n_eq++;
1029 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1031 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1034 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1036 if (!bmap)
1037 return -1;
1038 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1039 bmap->n_eq -= n;
1040 return 0;
1043 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1045 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1048 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1050 isl_int *t;
1051 if (!bmap)
1052 return -1;
1053 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1055 if (pos != bmap->n_eq - 1) {
1056 t = bmap->eq[pos];
1057 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1058 bmap->eq[bmap->n_eq - 1] = t;
1060 bmap->n_eq--;
1061 return 0;
1064 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1066 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1069 void isl_basic_map_inequality_to_equality(
1070 struct isl_basic_map *bmap, unsigned pos)
1072 isl_int *t;
1074 t = bmap->ineq[pos];
1075 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1076 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1077 bmap->eq[-1] = t;
1078 bmap->n_eq++;
1079 bmap->n_ineq--;
1080 bmap->eq--;
1081 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1082 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1083 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1084 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1087 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1089 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1092 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1094 struct isl_ctx *ctx;
1095 if (!bmap)
1096 return -1;
1097 ctx = bmap->ctx;
1098 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1099 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1100 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1101 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1102 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1103 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1104 1 + isl_basic_map_total_dim(bmap),
1105 bmap->extra - bmap->n_div);
1106 return bmap->n_ineq++;
1109 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1111 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1114 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1116 if (!bmap)
1117 return -1;
1118 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1119 bmap->n_ineq -= n;
1120 return 0;
1123 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1125 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1128 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1130 isl_int *t;
1131 if (!bmap)
1132 return -1;
1133 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1135 if (pos != bmap->n_ineq - 1) {
1136 t = bmap->ineq[pos];
1137 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1138 bmap->ineq[bmap->n_ineq - 1] = t;
1139 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1141 bmap->n_ineq--;
1142 return 0;
1145 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1147 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1150 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1151 isl_int *eq)
1153 int k;
1155 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1156 if (!bmap)
1157 return NULL;
1158 k = isl_basic_map_alloc_equality(bmap);
1159 if (k < 0)
1160 goto error;
1161 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1162 return bmap;
1163 error:
1164 isl_basic_map_free(bmap);
1165 return NULL;
1168 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1169 isl_int *eq)
1171 return (isl_basic_set *)
1172 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1175 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1176 isl_int *ineq)
1178 int k;
1180 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1181 if (!bmap)
1182 return NULL;
1183 k = isl_basic_map_alloc_inequality(bmap);
1184 if (k < 0)
1185 goto error;
1186 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1187 return bmap;
1188 error:
1189 isl_basic_map_free(bmap);
1190 return NULL;
1193 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1194 isl_int *ineq)
1196 return (isl_basic_set *)
1197 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1200 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1202 if (!bmap)
1203 return -1;
1204 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1205 isl_seq_clr(bmap->div[bmap->n_div] +
1206 1 + 1 + isl_basic_map_total_dim(bmap),
1207 bmap->extra - bmap->n_div);
1208 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1209 return bmap->n_div++;
1212 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1214 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1217 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1219 if (!bmap)
1220 return -1;
1221 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1222 bmap->n_div -= n;
1223 return 0;
1226 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1228 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1231 /* Copy constraint from src to dst, putting the vars of src at offset
1232 * dim_off in dst and the divs of src at offset div_off in dst.
1233 * If both sets are actually map, then dim_off applies to the input
1234 * variables.
1236 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1237 struct isl_basic_map *src_map, isl_int *src,
1238 unsigned in_off, unsigned out_off, unsigned div_off)
1240 unsigned src_nparam = isl_basic_map_n_param(src_map);
1241 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1242 unsigned src_in = isl_basic_map_n_in(src_map);
1243 unsigned dst_in = isl_basic_map_n_in(dst_map);
1244 unsigned src_out = isl_basic_map_n_out(src_map);
1245 unsigned dst_out = isl_basic_map_n_out(dst_map);
1246 isl_int_set(dst[0], src[0]);
1247 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1248 if (dst_nparam > src_nparam)
1249 isl_seq_clr(dst+1+src_nparam,
1250 dst_nparam - src_nparam);
1251 isl_seq_clr(dst+1+dst_nparam, in_off);
1252 isl_seq_cpy(dst+1+dst_nparam+in_off,
1253 src+1+src_nparam,
1254 isl_min(dst_in-in_off, src_in));
1255 if (dst_in-in_off > src_in)
1256 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1257 dst_in - in_off - src_in);
1258 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1259 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1260 src+1+src_nparam+src_in,
1261 isl_min(dst_out-out_off, src_out));
1262 if (dst_out-out_off > src_out)
1263 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1264 dst_out - out_off - src_out);
1265 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1266 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1267 src+1+src_nparam+src_in+src_out,
1268 isl_min(dst_map->extra-div_off, src_map->n_div));
1269 if (dst_map->n_div-div_off > src_map->n_div)
1270 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1271 div_off+src_map->n_div,
1272 dst_map->n_div - div_off - src_map->n_div);
1275 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1276 struct isl_basic_map *src_map, isl_int *src,
1277 unsigned in_off, unsigned out_off, unsigned div_off)
1279 isl_int_set(dst[0], src[0]);
1280 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1283 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1284 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1286 int i;
1287 unsigned div_off;
1289 if (!bmap1 || !bmap2)
1290 goto error;
1292 div_off = bmap1->n_div;
1294 for (i = 0; i < bmap2->n_eq; ++i) {
1295 int i1 = isl_basic_map_alloc_equality(bmap1);
1296 if (i1 < 0)
1297 goto error;
1298 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1299 i_pos, o_pos, div_off);
1302 for (i = 0; i < bmap2->n_ineq; ++i) {
1303 int i1 = isl_basic_map_alloc_inequality(bmap1);
1304 if (i1 < 0)
1305 goto error;
1306 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1307 i_pos, o_pos, div_off);
1310 for (i = 0; i < bmap2->n_div; ++i) {
1311 int i1 = isl_basic_map_alloc_div(bmap1);
1312 if (i1 < 0)
1313 goto error;
1314 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1315 i_pos, o_pos, div_off);
1318 isl_basic_map_free(bmap2);
1320 return bmap1;
1322 error:
1323 isl_basic_map_free(bmap1);
1324 isl_basic_map_free(bmap2);
1325 return NULL;
1328 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1329 struct isl_basic_set *bset2, unsigned pos)
1331 return (struct isl_basic_set *)
1332 add_constraints((struct isl_basic_map *)bset1,
1333 (struct isl_basic_map *)bset2, 0, pos);
1336 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1337 __isl_take isl_space *dim, unsigned extra,
1338 unsigned n_eq, unsigned n_ineq)
1340 struct isl_basic_map *ext;
1341 unsigned flags;
1342 int dims_ok;
1344 if (!dim)
1345 goto error;
1347 if (!base)
1348 goto error;
1350 dims_ok = isl_space_is_equal(base->dim, dim) &&
1351 base->extra >= base->n_div + extra;
1353 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1354 room_for_ineq(base, n_ineq)) {
1355 isl_space_free(dim);
1356 return base;
1359 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1360 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1361 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1362 extra += base->extra;
1363 n_eq += base->n_eq;
1364 n_ineq += base->n_ineq;
1366 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1367 dim = NULL;
1368 if (!ext)
1369 goto error;
1371 if (dims_ok)
1372 ext->sample = isl_vec_copy(base->sample);
1373 flags = base->flags;
1374 ext = add_constraints(ext, base, 0, 0);
1375 if (ext) {
1376 ext->flags = flags;
1377 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1380 return ext;
1382 error:
1383 isl_space_free(dim);
1384 isl_basic_map_free(base);
1385 return NULL;
1388 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1389 __isl_take isl_space *dim, unsigned extra,
1390 unsigned n_eq, unsigned n_ineq)
1392 return (struct isl_basic_set *)
1393 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1394 extra, n_eq, n_ineq);
1397 struct isl_basic_map *isl_basic_map_extend_constraints(
1398 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1400 if (!base)
1401 return NULL;
1402 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1403 0, n_eq, n_ineq);
1406 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1407 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1408 unsigned n_eq, unsigned n_ineq)
1410 struct isl_basic_map *bmap;
1411 isl_space *dim;
1413 if (!base)
1414 return NULL;
1415 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1416 if (!dim)
1417 goto error;
1419 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1420 return bmap;
1421 error:
1422 isl_basic_map_free(base);
1423 return NULL;
1426 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1427 unsigned nparam, unsigned dim, unsigned extra,
1428 unsigned n_eq, unsigned n_ineq)
1430 return (struct isl_basic_set *)
1431 isl_basic_map_extend((struct isl_basic_map *)base,
1432 nparam, 0, dim, extra, n_eq, n_ineq);
1435 struct isl_basic_set *isl_basic_set_extend_constraints(
1436 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1438 return (struct isl_basic_set *)
1439 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1440 n_eq, n_ineq);
1443 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1445 return (struct isl_basic_set *)
1446 isl_basic_map_cow((struct isl_basic_map *)bset);
1449 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1451 if (!bmap)
1452 return NULL;
1454 if (bmap->ref > 1) {
1455 bmap->ref--;
1456 bmap = isl_basic_map_dup(bmap);
1458 if (bmap)
1459 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1460 return bmap;
1463 struct isl_set *isl_set_cow(struct isl_set *set)
1465 if (!set)
1466 return NULL;
1468 if (set->ref == 1)
1469 return set;
1470 set->ref--;
1471 return isl_set_dup(set);
1474 struct isl_map *isl_map_cow(struct isl_map *map)
1476 if (!map)
1477 return NULL;
1479 if (map->ref == 1)
1480 return map;
1481 map->ref--;
1482 return isl_map_dup(map);
1485 static void swap_vars(struct isl_blk blk, isl_int *a,
1486 unsigned a_len, unsigned b_len)
1488 isl_seq_cpy(blk.data, a+a_len, b_len);
1489 isl_seq_cpy(blk.data+b_len, a, a_len);
1490 isl_seq_cpy(a, blk.data, b_len+a_len);
1493 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1494 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1496 int i;
1497 struct isl_blk blk;
1499 if (!bmap)
1500 goto error;
1502 isl_assert(bmap->ctx,
1503 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1505 if (n1 == 0 || n2 == 0)
1506 return bmap;
1508 bmap = isl_basic_map_cow(bmap);
1509 if (!bmap)
1510 return NULL;
1512 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1513 if (isl_blk_is_error(blk))
1514 goto error;
1516 for (i = 0; i < bmap->n_eq; ++i)
1517 swap_vars(blk,
1518 bmap->eq[i] + pos, n1, n2);
1520 for (i = 0; i < bmap->n_ineq; ++i)
1521 swap_vars(blk,
1522 bmap->ineq[i] + pos, n1, n2);
1524 for (i = 0; i < bmap->n_div; ++i)
1525 swap_vars(blk,
1526 bmap->div[i]+1 + pos, n1, n2);
1528 isl_blk_free(bmap->ctx, blk);
1530 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1531 bmap = isl_basic_map_gauss(bmap, NULL);
1532 return isl_basic_map_finalize(bmap);
1533 error:
1534 isl_basic_map_free(bmap);
1535 return NULL;
1538 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1539 __isl_take isl_basic_set *bset, unsigned n)
1541 unsigned dim;
1542 unsigned nparam;
1544 nparam = isl_basic_set_n_param(bset);
1545 dim = isl_basic_set_n_dim(bset);
1546 isl_assert(bset->ctx, n <= dim, goto error);
1548 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1549 error:
1550 isl_basic_set_free(bset);
1551 return NULL;
1554 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1556 int i = 0;
1557 unsigned total;
1558 if (!bmap)
1559 goto error;
1560 total = isl_basic_map_total_dim(bmap);
1561 isl_basic_map_free_div(bmap, bmap->n_div);
1562 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1563 if (bmap->n_eq > 0)
1564 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1565 else {
1566 i = isl_basic_map_alloc_equality(bmap);
1567 if (i < 0)
1568 goto error;
1570 isl_int_set_si(bmap->eq[i][0], 1);
1571 isl_seq_clr(bmap->eq[i]+1, total);
1572 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1573 isl_vec_free(bmap->sample);
1574 bmap->sample = NULL;
1575 return isl_basic_map_finalize(bmap);
1576 error:
1577 isl_basic_map_free(bmap);
1578 return NULL;
1581 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1583 return (struct isl_basic_set *)
1584 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1587 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1589 int i;
1590 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1591 isl_int *t = bmap->div[a];
1592 bmap->div[a] = bmap->div[b];
1593 bmap->div[b] = t;
1595 for (i = 0; i < bmap->n_eq; ++i)
1596 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1598 for (i = 0; i < bmap->n_ineq; ++i)
1599 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1601 for (i = 0; i < bmap->n_div; ++i)
1602 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1603 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1606 /* Eliminate the specified n dimensions starting at first from the
1607 * constraints, without removing the dimensions from the space.
1608 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1610 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1611 enum isl_dim_type type, unsigned first, unsigned n)
1613 int i;
1615 if (!map)
1616 return NULL;
1617 if (n == 0)
1618 return map;
1620 if (first + n > isl_map_dim(map, type) || first + n < first)
1621 isl_die(map->ctx, isl_error_invalid,
1622 "index out of bounds", goto error);
1624 map = isl_map_cow(map);
1625 if (!map)
1626 return NULL;
1628 for (i = 0; i < map->n; ++i) {
1629 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1630 if (!map->p[i])
1631 goto error;
1633 return map;
1634 error:
1635 isl_map_free(map);
1636 return NULL;
1639 /* Eliminate the specified n dimensions starting at first from the
1640 * constraints, without removing the dimensions from the space.
1641 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1643 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1644 enum isl_dim_type type, unsigned first, unsigned n)
1646 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1649 /* Eliminate the specified n dimensions starting at first from the
1650 * constraints, without removing the dimensions from the space.
1651 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1653 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1654 unsigned first, unsigned n)
1656 return isl_set_eliminate(set, isl_dim_set, first, n);
1659 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1660 __isl_take isl_basic_map *bmap)
1662 if (!bmap)
1663 return NULL;
1664 bmap = isl_basic_map_eliminate_vars(bmap,
1665 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1666 if (!bmap)
1667 return NULL;
1668 bmap->n_div = 0;
1669 return isl_basic_map_finalize(bmap);
1672 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1673 __isl_take isl_basic_set *bset)
1675 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1676 (struct isl_basic_map *)bset);
1679 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1681 int i;
1683 if (!map)
1684 return NULL;
1685 if (map->n == 0)
1686 return map;
1688 map = isl_map_cow(map);
1689 if (!map)
1690 return NULL;
1692 for (i = 0; i < map->n; ++i) {
1693 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1694 if (!map->p[i])
1695 goto error;
1697 return map;
1698 error:
1699 isl_map_free(map);
1700 return NULL;
1703 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1705 return isl_map_remove_divs(set);
1708 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1709 enum isl_dim_type type, unsigned first, unsigned n)
1711 if (!bmap)
1712 return NULL;
1713 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1714 goto error);
1715 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1716 return bmap;
1717 bmap = isl_basic_map_eliminate_vars(bmap,
1718 isl_basic_map_offset(bmap, type) - 1 + first, n);
1719 if (!bmap)
1720 return bmap;
1721 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1722 return bmap;
1723 bmap = isl_basic_map_drop(bmap, type, first, n);
1724 return bmap;
1725 error:
1726 isl_basic_map_free(bmap);
1727 return NULL;
1730 /* Return true if the definition of the given div (recursively) involves
1731 * any of the given variables.
1733 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1734 unsigned first, unsigned n)
1736 int i;
1737 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1739 if (isl_int_is_zero(bmap->div[div][0]))
1740 return 0;
1741 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1742 return 1;
1744 for (i = bmap->n_div - 1; i >= 0; --i) {
1745 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1746 continue;
1747 if (div_involves_vars(bmap, i, first, n))
1748 return 1;
1751 return 0;
1754 /* Try and add a lower and/or upper bound on "div" to "bmap"
1755 * based on inequality "i".
1756 * "total" is the total number of variables (excluding the divs).
1757 * "v" is a temporary object that can be used during the calculations.
1758 * If "lb" is set, then a lower bound should be constructed.
1759 * If "ub" is set, then an upper bound should be constructed.
1761 * The calling function has already checked that the inequality does not
1762 * reference "div", but we still need to check that the inequality is
1763 * of the right form. We'll consider the case where we want to construct
1764 * a lower bound. The construction of upper bounds is similar.
1766 * Let "div" be of the form
1768 * q = floor((a + f(x))/d)
1770 * We essentially check if constraint "i" is of the form
1772 * b + f(x) >= 0
1774 * so that we can use it to derive a lower bound on "div".
1775 * However, we allow a slightly more general form
1777 * b + g(x) >= 0
1779 * with the condition that the coefficients of g(x) - f(x) are all
1780 * divisible by d.
1781 * Rewriting this constraint as
1783 * 0 >= -b - g(x)
1785 * adding a + f(x) to both sides and dividing by d, we obtain
1787 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1789 * Taking the floor on both sides, we obtain
1791 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1793 * or
1795 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1797 * In the case of an upper bound, we construct the constraint
1799 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1802 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1803 __isl_take isl_basic_map *bmap, int div, int i,
1804 unsigned total, isl_int v, int lb, int ub)
1806 int j;
1808 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1809 if (lb) {
1810 isl_int_sub(v, bmap->ineq[i][1 + j],
1811 bmap->div[div][1 + 1 + j]);
1812 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1814 if (ub) {
1815 isl_int_add(v, bmap->ineq[i][1 + j],
1816 bmap->div[div][1 + 1 + j]);
1817 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1820 if (!lb && !ub)
1821 return bmap;
1823 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1824 if (lb) {
1825 int k = isl_basic_map_alloc_inequality(bmap);
1826 if (k < 0)
1827 goto error;
1828 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1829 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
1830 bmap->div[div][1 + j]);
1831 isl_int_cdiv_q(bmap->ineq[k][j],
1832 bmap->ineq[k][j], bmap->div[div][0]);
1834 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
1836 if (ub) {
1837 int k = isl_basic_map_alloc_inequality(bmap);
1838 if (k < 0)
1839 goto error;
1840 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1841 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
1842 bmap->div[div][1 + j]);
1843 isl_int_fdiv_q(bmap->ineq[k][j],
1844 bmap->ineq[k][j], bmap->div[div][0]);
1846 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
1849 return bmap;
1850 error:
1851 isl_basic_map_free(bmap);
1852 return NULL;
1855 /* This function is called right before "div" is eliminated from "bmap"
1856 * using Fourier-Motzkin.
1857 * Look through the constraints of "bmap" for constraints on the argument
1858 * of the integer division and use them to construct constraints on the
1859 * integer division itself. These constraints can then be combined
1860 * during the Fourier-Motzkin elimination.
1861 * Note that it is only useful to introduce lower bounds on "div"
1862 * if "bmap" already contains upper bounds on "div" as the newly
1863 * introduce lower bounds can then be combined with the pre-existing
1864 * upper bounds. Similarly for upper bounds.
1865 * We therefore first check if "bmap" contains any lower and/or upper bounds
1866 * on "div".
1868 * It is interesting to note that the introduction of these constraints
1869 * can indeed lead to more accurate results, even when compared to
1870 * deriving constraints on the argument of "div" from constraints on "div".
1871 * Consider, for example, the set
1873 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
1875 * The second constraint can be rewritten as
1877 * 2 * [(-i-2j+3)/4] + k >= 0
1879 * from which we can derive
1881 * -i - 2j + 3 >= -2k
1883 * or
1885 * i + 2j <= 3 + 2k
1887 * Combined with the first constraint, we obtain
1889 * -3 <= 3 + 2k or k >= -3
1891 * If, on the other hand we derive a constraint on [(i+2j)/4] from
1892 * the first constraint, we obtain
1894 * [(i + 2j)/4] >= [-3/4] = -1
1896 * Combining this constraint with the second constraint, we obtain
1898 * k >= -2
1900 static __isl_give isl_basic_map *insert_bounds_on_div(
1901 __isl_take isl_basic_map *bmap, int div)
1903 int i;
1904 int check_lb, check_ub;
1905 isl_int v;
1906 unsigned total;
1908 if (!bmap)
1909 return NULL;
1911 if (isl_int_is_zero(bmap->div[div][0]))
1912 return bmap;
1914 total = isl_space_dim(bmap->dim, isl_dim_all);
1916 check_lb = 0;
1917 check_ub = 0;
1918 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
1919 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
1920 if (s > 0)
1921 check_ub = 1;
1922 if (s < 0)
1923 check_lb = 1;
1926 if (!check_lb && !check_ub)
1927 return bmap;
1929 isl_int_init(v);
1931 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
1932 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
1933 continue;
1935 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
1936 check_lb, check_ub);
1939 isl_int_clear(v);
1941 return bmap;
1944 /* Remove all divs (recursively) involving any of the given dimensions
1945 * in their definitions.
1947 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1948 __isl_take isl_basic_map *bmap,
1949 enum isl_dim_type type, unsigned first, unsigned n)
1951 int i;
1953 if (!bmap)
1954 return NULL;
1955 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1956 goto error);
1957 first += isl_basic_map_offset(bmap, type);
1959 for (i = bmap->n_div - 1; i >= 0; --i) {
1960 if (!div_involves_vars(bmap, i, first, n))
1961 continue;
1962 bmap = insert_bounds_on_div(bmap, i);
1963 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1964 if (!bmap)
1965 return NULL;
1966 i = bmap->n_div;
1969 return bmap;
1970 error:
1971 isl_basic_map_free(bmap);
1972 return NULL;
1975 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1976 enum isl_dim_type type, unsigned first, unsigned n)
1978 int i;
1980 if (!map)
1981 return NULL;
1982 if (map->n == 0)
1983 return map;
1985 map = isl_map_cow(map);
1986 if (!map)
1987 return NULL;
1989 for (i = 0; i < map->n; ++i) {
1990 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1991 type, first, n);
1992 if (!map->p[i])
1993 goto error;
1995 return map;
1996 error:
1997 isl_map_free(map);
1998 return NULL;
2001 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2002 enum isl_dim_type type, unsigned first, unsigned n)
2004 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2005 type, first, n);
2008 /* Does the desciption of "bmap" depend on the specified dimensions?
2009 * We also check whether the dimensions appear in any of the div definitions.
2010 * In principle there is no need for this check. If the dimensions appear
2011 * in a div definition, they also appear in the defining constraints of that
2012 * div.
2014 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2015 enum isl_dim_type type, unsigned first, unsigned n)
2017 int i;
2019 if (!bmap)
2020 return -1;
2022 if (first + n > isl_basic_map_dim(bmap, type))
2023 isl_die(bmap->ctx, isl_error_invalid,
2024 "index out of bounds", return -1);
2026 first += isl_basic_map_offset(bmap, type);
2027 for (i = 0; i < bmap->n_eq; ++i)
2028 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2029 return 1;
2030 for (i = 0; i < bmap->n_ineq; ++i)
2031 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2032 return 1;
2033 for (i = 0; i < bmap->n_div; ++i) {
2034 if (isl_int_is_zero(bmap->div[i][0]))
2035 continue;
2036 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2037 return 1;
2040 return 0;
2043 int isl_map_involves_dims(__isl_keep isl_map *map,
2044 enum isl_dim_type type, unsigned first, unsigned n)
2046 int i;
2048 if (!map)
2049 return -1;
2051 if (first + n > isl_map_dim(map, type))
2052 isl_die(map->ctx, isl_error_invalid,
2053 "index out of bounds", return -1);
2055 for (i = 0; i < map->n; ++i) {
2056 int involves = isl_basic_map_involves_dims(map->p[i],
2057 type, first, n);
2058 if (involves < 0 || involves)
2059 return involves;
2062 return 0;
2065 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2066 enum isl_dim_type type, unsigned first, unsigned n)
2068 return isl_basic_map_involves_dims(bset, type, first, n);
2071 int isl_set_involves_dims(__isl_keep isl_set *set,
2072 enum isl_dim_type type, unsigned first, unsigned n)
2074 return isl_map_involves_dims(set, type, first, n);
2077 /* Return true if the definition of the given div is unknown or depends
2078 * on unknown divs.
2080 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2082 int i;
2083 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2085 if (isl_int_is_zero(bmap->div[div][0]))
2086 return 1;
2088 for (i = bmap->n_div - 1; i >= 0; --i) {
2089 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2090 continue;
2091 if (div_is_unknown(bmap, i))
2092 return 1;
2095 return 0;
2098 /* Remove all divs that are unknown or defined in terms of unknown divs.
2100 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2101 __isl_take isl_basic_map *bmap)
2103 int i;
2105 if (!bmap)
2106 return NULL;
2108 for (i = bmap->n_div - 1; i >= 0; --i) {
2109 if (!div_is_unknown(bmap, i))
2110 continue;
2111 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2112 if (!bmap)
2113 return NULL;
2114 i = bmap->n_div;
2117 return bmap;
2120 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2122 int i;
2124 if (!map)
2125 return NULL;
2126 if (map->n == 0)
2127 return map;
2129 map = isl_map_cow(map);
2130 if (!map)
2131 return NULL;
2133 for (i = 0; i < map->n; ++i) {
2134 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2135 if (!map->p[i])
2136 goto error;
2138 return map;
2139 error:
2140 isl_map_free(map);
2141 return NULL;
2144 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2146 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2149 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2150 __isl_take isl_basic_set *bset,
2151 enum isl_dim_type type, unsigned first, unsigned n)
2153 return (isl_basic_set *)
2154 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2157 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2158 enum isl_dim_type type, unsigned first, unsigned n)
2160 int i;
2162 if (n == 0)
2163 return map;
2165 map = isl_map_cow(map);
2166 if (!map)
2167 return NULL;
2168 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2170 for (i = 0; i < map->n; ++i) {
2171 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2172 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2173 if (!map->p[i])
2174 goto error;
2176 map = isl_map_drop(map, type, first, n);
2177 return map;
2178 error:
2179 isl_map_free(map);
2180 return NULL;
2183 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2184 enum isl_dim_type type, unsigned first, unsigned n)
2186 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2189 /* Project out n inputs starting at first using Fourier-Motzkin */
2190 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2191 unsigned first, unsigned n)
2193 return isl_map_remove_dims(map, isl_dim_in, first, n);
2196 static void dump_term(struct isl_basic_map *bmap,
2197 isl_int c, int pos, FILE *out)
2199 const char *name;
2200 unsigned in = isl_basic_map_n_in(bmap);
2201 unsigned dim = in + isl_basic_map_n_out(bmap);
2202 unsigned nparam = isl_basic_map_n_param(bmap);
2203 if (!pos)
2204 isl_int_print(out, c, 0);
2205 else {
2206 if (!isl_int_is_one(c))
2207 isl_int_print(out, c, 0);
2208 if (pos < 1 + nparam) {
2209 name = isl_space_get_dim_name(bmap->dim,
2210 isl_dim_param, pos - 1);
2211 if (name)
2212 fprintf(out, "%s", name);
2213 else
2214 fprintf(out, "p%d", pos - 1);
2215 } else if (pos < 1 + nparam + in)
2216 fprintf(out, "i%d", pos - 1 - nparam);
2217 else if (pos < 1 + nparam + dim)
2218 fprintf(out, "o%d", pos - 1 - nparam - in);
2219 else
2220 fprintf(out, "e%d", pos - 1 - nparam - dim);
2224 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2225 int sign, FILE *out)
2227 int i;
2228 int first;
2229 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2230 isl_int v;
2232 isl_int_init(v);
2233 for (i = 0, first = 1; i < len; ++i) {
2234 if (isl_int_sgn(c[i]) * sign <= 0)
2235 continue;
2236 if (!first)
2237 fprintf(out, " + ");
2238 first = 0;
2239 isl_int_abs(v, c[i]);
2240 dump_term(bmap, v, i, out);
2242 isl_int_clear(v);
2243 if (first)
2244 fprintf(out, "0");
2247 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2248 const char *op, FILE *out, int indent)
2250 int i;
2252 fprintf(out, "%*s", indent, "");
2254 dump_constraint_sign(bmap, c, 1, out);
2255 fprintf(out, " %s ", op);
2256 dump_constraint_sign(bmap, c, -1, out);
2258 fprintf(out, "\n");
2260 for (i = bmap->n_div; i < bmap->extra; ++i) {
2261 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2262 continue;
2263 fprintf(out, "%*s", indent, "");
2264 fprintf(out, "ERROR: unused div coefficient not zero\n");
2265 abort();
2269 static void dump_constraints(struct isl_basic_map *bmap,
2270 isl_int **c, unsigned n,
2271 const char *op, FILE *out, int indent)
2273 int i;
2275 for (i = 0; i < n; ++i)
2276 dump_constraint(bmap, c[i], op, out, indent);
2279 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2281 int j;
2282 int first = 1;
2283 unsigned total = isl_basic_map_total_dim(bmap);
2285 for (j = 0; j < 1 + total; ++j) {
2286 if (isl_int_is_zero(exp[j]))
2287 continue;
2288 if (!first && isl_int_is_pos(exp[j]))
2289 fprintf(out, "+");
2290 dump_term(bmap, exp[j], j, out);
2291 first = 0;
2295 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2297 int i;
2299 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2300 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2302 for (i = 0; i < bmap->n_div; ++i) {
2303 fprintf(out, "%*s", indent, "");
2304 fprintf(out, "e%d = [(", i);
2305 dump_affine(bmap, bmap->div[i]+1, out);
2306 fprintf(out, ")/");
2307 isl_int_print(out, bmap->div[i][0], 0);
2308 fprintf(out, "]\n");
2312 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2313 FILE *out, int indent)
2315 if (!bset) {
2316 fprintf(out, "null basic set\n");
2317 return;
2320 fprintf(out, "%*s", indent, "");
2321 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2322 bset->ref, bset->dim->nparam, bset->dim->n_out,
2323 bset->extra, bset->flags);
2324 dump((struct isl_basic_map *)bset, out, indent);
2327 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2328 FILE *out, int indent)
2330 if (!bmap) {
2331 fprintf(out, "null basic map\n");
2332 return;
2335 fprintf(out, "%*s", indent, "");
2336 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2337 "flags: %x, n_name: %d\n",
2338 bmap->ref,
2339 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2340 bmap->extra, bmap->flags, bmap->dim->n_id);
2341 dump(bmap, out, indent);
2344 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2346 unsigned total;
2347 if (!bmap)
2348 return -1;
2349 total = isl_basic_map_total_dim(bmap);
2350 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2351 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2352 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2353 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2354 return 0;
2357 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2358 unsigned flags)
2360 struct isl_set *set;
2362 if (!dim)
2363 return NULL;
2364 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2365 isl_assert(dim->ctx, n >= 0, goto error);
2366 set = isl_alloc(dim->ctx, struct isl_set,
2367 sizeof(struct isl_set) +
2368 (n - 1) * sizeof(struct isl_basic_set *));
2369 if (!set)
2370 goto error;
2372 set->ctx = dim->ctx;
2373 isl_ctx_ref(set->ctx);
2374 set->ref = 1;
2375 set->size = n;
2376 set->n = 0;
2377 set->dim = dim;
2378 set->flags = flags;
2379 return set;
2380 error:
2381 isl_space_free(dim);
2382 return NULL;
2385 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2386 unsigned nparam, unsigned dim, int n, unsigned flags)
2388 struct isl_set *set;
2389 isl_space *dims;
2391 dims = isl_space_alloc(ctx, nparam, 0, dim);
2392 if (!dims)
2393 return NULL;
2395 set = isl_set_alloc_space(dims, n, flags);
2396 return set;
2399 /* Make sure "map" has room for at least "n" more basic maps.
2401 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2403 int i;
2404 struct isl_map *grown = NULL;
2406 if (!map)
2407 return NULL;
2408 isl_assert(map->ctx, n >= 0, goto error);
2409 if (map->n + n <= map->size)
2410 return map;
2411 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2412 if (!grown)
2413 goto error;
2414 for (i = 0; i < map->n; ++i) {
2415 grown->p[i] = isl_basic_map_copy(map->p[i]);
2416 if (!grown->p[i])
2417 goto error;
2418 grown->n++;
2420 isl_map_free(map);
2421 return grown;
2422 error:
2423 isl_map_free(grown);
2424 isl_map_free(map);
2425 return NULL;
2428 /* Make sure "set" has room for at least "n" more basic sets.
2430 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2432 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2435 struct isl_set *isl_set_dup(struct isl_set *set)
2437 int i;
2438 struct isl_set *dup;
2440 if (!set)
2441 return NULL;
2443 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2444 if (!dup)
2445 return NULL;
2446 for (i = 0; i < set->n; ++i)
2447 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2448 return dup;
2451 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2453 return isl_map_from_basic_map(bset);
2456 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2458 struct isl_map *map;
2460 if (!bmap)
2461 return NULL;
2463 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2464 return isl_map_add_basic_map(map, bmap);
2467 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2468 __isl_take isl_basic_set *bset)
2470 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2471 (struct isl_basic_map *)bset);
2474 void *isl_set_free(__isl_take isl_set *set)
2476 int i;
2478 if (!set)
2479 return NULL;
2481 if (--set->ref > 0)
2482 return NULL;
2484 isl_ctx_deref(set->ctx);
2485 for (i = 0; i < set->n; ++i)
2486 isl_basic_set_free(set->p[i]);
2487 isl_space_free(set->dim);
2488 free(set);
2490 return NULL;
2493 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2495 int i;
2497 if (!set) {
2498 fprintf(out, "null set\n");
2499 return;
2502 fprintf(out, "%*s", indent, "");
2503 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2504 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2505 set->flags);
2506 for (i = 0; i < set->n; ++i) {
2507 fprintf(out, "%*s", indent, "");
2508 fprintf(out, "basic set %d:\n", i);
2509 isl_basic_set_print_internal(set->p[i], out, indent+4);
2513 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2515 int i;
2517 if (!map) {
2518 fprintf(out, "null map\n");
2519 return;
2522 fprintf(out, "%*s", indent, "");
2523 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2524 "flags: %x, n_name: %d\n",
2525 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2526 map->dim->n_out, map->flags, map->dim->n_id);
2527 for (i = 0; i < map->n; ++i) {
2528 fprintf(out, "%*s", indent, "");
2529 fprintf(out, "basic map %d:\n", i);
2530 isl_basic_map_print_internal(map->p[i], out, indent+4);
2534 struct isl_basic_map *isl_basic_map_intersect_domain(
2535 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2537 struct isl_basic_map *bmap_domain;
2539 if (!bmap || !bset)
2540 goto error;
2542 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2543 bset->dim, isl_dim_param), goto error);
2545 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2546 isl_assert(bset->ctx,
2547 isl_basic_map_compatible_domain(bmap, bset), goto error);
2549 bmap = isl_basic_map_cow(bmap);
2550 if (!bmap)
2551 goto error;
2552 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2553 bset->n_div, bset->n_eq, bset->n_ineq);
2554 bmap_domain = isl_basic_map_from_domain(bset);
2555 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2557 bmap = isl_basic_map_simplify(bmap);
2558 return isl_basic_map_finalize(bmap);
2559 error:
2560 isl_basic_map_free(bmap);
2561 isl_basic_set_free(bset);
2562 return NULL;
2565 struct isl_basic_map *isl_basic_map_intersect_range(
2566 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2568 struct isl_basic_map *bmap_range;
2570 if (!bmap || !bset)
2571 goto error;
2573 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2574 bset->dim, isl_dim_param), goto error);
2576 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2577 isl_assert(bset->ctx,
2578 isl_basic_map_compatible_range(bmap, bset), goto error);
2580 if (isl_basic_set_is_universe(bset)) {
2581 isl_basic_set_free(bset);
2582 return bmap;
2585 bmap = isl_basic_map_cow(bmap);
2586 if (!bmap)
2587 goto error;
2588 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2589 bset->n_div, bset->n_eq, bset->n_ineq);
2590 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2591 bmap = add_constraints(bmap, bmap_range, 0, 0);
2593 bmap = isl_basic_map_simplify(bmap);
2594 return isl_basic_map_finalize(bmap);
2595 error:
2596 isl_basic_map_free(bmap);
2597 isl_basic_set_free(bset);
2598 return NULL;
2601 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2603 int i;
2604 unsigned total;
2605 isl_int s;
2607 total = 1 + isl_basic_map_total_dim(bmap);
2608 if (total != vec->size)
2609 return -1;
2611 isl_int_init(s);
2613 for (i = 0; i < bmap->n_eq; ++i) {
2614 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2615 if (!isl_int_is_zero(s)) {
2616 isl_int_clear(s);
2617 return 0;
2621 for (i = 0; i < bmap->n_ineq; ++i) {
2622 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2623 if (isl_int_is_neg(s)) {
2624 isl_int_clear(s);
2625 return 0;
2629 isl_int_clear(s);
2631 return 1;
2634 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2636 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2639 struct isl_basic_map *isl_basic_map_intersect(
2640 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2642 struct isl_vec *sample = NULL;
2644 if (!bmap1 || !bmap2)
2645 goto error;
2647 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2648 bmap2->dim, isl_dim_param), goto error);
2649 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2650 isl_space_dim(bmap1->dim, isl_dim_param) &&
2651 isl_space_dim(bmap2->dim, isl_dim_all) !=
2652 isl_space_dim(bmap2->dim, isl_dim_param))
2653 return isl_basic_map_intersect(bmap2, bmap1);
2655 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2656 isl_space_dim(bmap2->dim, isl_dim_param))
2657 isl_assert(bmap1->ctx,
2658 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2660 if (bmap1->sample &&
2661 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2662 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2663 sample = isl_vec_copy(bmap1->sample);
2664 else if (bmap2->sample &&
2665 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2666 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2667 sample = isl_vec_copy(bmap2->sample);
2669 bmap1 = isl_basic_map_cow(bmap1);
2670 if (!bmap1)
2671 goto error;
2672 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2673 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2674 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2676 if (!bmap1)
2677 isl_vec_free(sample);
2678 else if (sample) {
2679 isl_vec_free(bmap1->sample);
2680 bmap1->sample = sample;
2683 bmap1 = isl_basic_map_simplify(bmap1);
2684 return isl_basic_map_finalize(bmap1);
2685 error:
2686 if (sample)
2687 isl_vec_free(sample);
2688 isl_basic_map_free(bmap1);
2689 isl_basic_map_free(bmap2);
2690 return NULL;
2693 struct isl_basic_set *isl_basic_set_intersect(
2694 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2696 return (struct isl_basic_set *)
2697 isl_basic_map_intersect(
2698 (struct isl_basic_map *)bset1,
2699 (struct isl_basic_map *)bset2);
2702 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2703 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2705 return isl_basic_set_intersect(bset1, bset2);
2708 /* Special case of isl_map_intersect, where both map1 and map2
2709 * are convex, without any divs and such that either map1 or map2
2710 * contains a single constraint. This constraint is then simply
2711 * added to the other map.
2713 static __isl_give isl_map *map_intersect_add_constraint(
2714 __isl_take isl_map *map1, __isl_take isl_map *map2)
2716 isl_assert(map1->ctx, map1->n == 1, goto error);
2717 isl_assert(map2->ctx, map1->n == 1, goto error);
2718 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2719 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2721 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2722 return isl_map_intersect(map2, map1);
2724 isl_assert(map2->ctx,
2725 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2727 map1 = isl_map_cow(map1);
2728 if (!map1)
2729 goto error;
2730 if (isl_map_plain_is_empty(map1)) {
2731 isl_map_free(map2);
2732 return map1;
2734 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2735 if (map2->p[0]->n_eq == 1)
2736 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2737 else
2738 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2739 map2->p[0]->ineq[0]);
2741 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2742 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2743 if (!map1->p[0])
2744 goto error;
2746 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2747 isl_basic_map_free(map1->p[0]);
2748 map1->n = 0;
2751 isl_map_free(map2);
2753 return map1;
2754 error:
2755 isl_map_free(map1);
2756 isl_map_free(map2);
2757 return NULL;
2760 /* map2 may be either a parameter domain or a map living in the same
2761 * space as map1.
2763 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2764 __isl_take isl_map *map2)
2766 unsigned flags = 0;
2767 struct isl_map *result;
2768 int i, j;
2770 if (!map1 || !map2)
2771 goto error;
2773 if (isl_map_plain_is_empty(map1) &&
2774 isl_space_is_equal(map1->dim, map2->dim)) {
2775 isl_map_free(map2);
2776 return map1;
2778 if (isl_map_plain_is_empty(map2) &&
2779 isl_space_is_equal(map1->dim, map2->dim)) {
2780 isl_map_free(map1);
2781 return map2;
2784 if (map1->n == 1 && map2->n == 1 &&
2785 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2786 isl_space_is_equal(map1->dim, map2->dim) &&
2787 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2788 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2789 return map_intersect_add_constraint(map1, map2);
2791 if (isl_space_dim(map2->dim, isl_dim_all) !=
2792 isl_space_dim(map2->dim, isl_dim_param))
2793 isl_assert(map1->ctx,
2794 isl_space_is_equal(map1->dim, map2->dim), goto error);
2796 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2797 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2798 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2800 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2801 map1->n * map2->n, flags);
2802 if (!result)
2803 goto error;
2804 for (i = 0; i < map1->n; ++i)
2805 for (j = 0; j < map2->n; ++j) {
2806 struct isl_basic_map *part;
2807 part = isl_basic_map_intersect(
2808 isl_basic_map_copy(map1->p[i]),
2809 isl_basic_map_copy(map2->p[j]));
2810 if (isl_basic_map_is_empty(part))
2811 isl_basic_map_free(part);
2812 else
2813 result = isl_map_add_basic_map(result, part);
2814 if (!result)
2815 goto error;
2817 isl_map_free(map1);
2818 isl_map_free(map2);
2819 return result;
2820 error:
2821 isl_map_free(map1);
2822 isl_map_free(map2);
2823 return NULL;
2826 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2827 __isl_take isl_map *map2)
2829 if (!map1 || !map2)
2830 goto error;
2831 if (!isl_space_is_equal(map1->dim, map2->dim))
2832 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2833 "spaces don't match", goto error);
2834 return map_intersect_internal(map1, map2);
2835 error:
2836 isl_map_free(map1);
2837 isl_map_free(map2);
2838 return NULL;
2841 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2842 __isl_take isl_map *map2)
2844 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2847 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2849 return (struct isl_set *)
2850 isl_map_intersect((struct isl_map *)set1,
2851 (struct isl_map *)set2);
2854 /* map_intersect_internal accepts intersections
2855 * with parameter domains, so we can just call that function.
2857 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2858 __isl_take isl_set *params)
2860 return map_intersect_internal(map, params);
2863 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2864 __isl_take isl_map *map2)
2866 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2869 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2870 __isl_take isl_set *params)
2872 return isl_map_intersect_params(set, params);
2875 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2877 isl_space *dim;
2878 struct isl_basic_set *bset;
2879 unsigned in;
2881 if (!bmap)
2882 return NULL;
2883 bmap = isl_basic_map_cow(bmap);
2884 if (!bmap)
2885 return NULL;
2886 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2887 in = isl_basic_map_n_in(bmap);
2888 bset = isl_basic_set_from_basic_map(bmap);
2889 bset = isl_basic_set_swap_vars(bset, in);
2890 return isl_basic_map_from_basic_set(bset, dim);
2893 static __isl_give isl_basic_map *basic_map_space_reset(
2894 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2896 isl_space *space;
2898 if (!isl_space_is_named_or_nested(bmap->dim, type))
2899 return bmap;
2901 space = isl_basic_map_get_space(bmap);
2902 space = isl_space_reset(space, type);
2903 bmap = isl_basic_map_reset_space(bmap, space);
2904 return bmap;
2907 __isl_give isl_basic_map *isl_basic_map_insert_dims(
2908 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2909 unsigned pos, unsigned n)
2911 isl_space *res_dim;
2912 struct isl_basic_map *res;
2913 struct isl_dim_map *dim_map;
2914 unsigned total, off;
2915 enum isl_dim_type t;
2917 if (n == 0)
2918 return basic_map_space_reset(bmap, type);
2920 if (!bmap)
2921 return NULL;
2923 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2925 total = isl_basic_map_total_dim(bmap) + n;
2926 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2927 off = 0;
2928 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2929 if (t != type) {
2930 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2931 } else {
2932 unsigned size = isl_basic_map_dim(bmap, t);
2933 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2934 0, pos, off);
2935 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2936 pos, size - pos, off + pos + n);
2938 off += isl_space_dim(res_dim, t);
2940 isl_dim_map_div(dim_map, bmap, off);
2942 res = isl_basic_map_alloc_space(res_dim,
2943 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2944 if (isl_basic_map_is_rational(bmap))
2945 res = isl_basic_map_set_rational(res);
2946 if (isl_basic_map_plain_is_empty(bmap)) {
2947 isl_basic_map_free(bmap);
2948 return isl_basic_map_set_to_empty(res);
2950 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2951 return isl_basic_map_finalize(res);
2954 __isl_give isl_basic_set *isl_basic_set_insert_dims(
2955 __isl_take isl_basic_set *bset,
2956 enum isl_dim_type type, unsigned pos, unsigned n)
2958 return isl_basic_map_insert_dims(bset, type, pos, n);
2961 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2962 enum isl_dim_type type, unsigned n)
2964 if (!bmap)
2965 return NULL;
2966 return isl_basic_map_insert_dims(bmap, type,
2967 isl_basic_map_dim(bmap, type), n);
2970 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2971 enum isl_dim_type type, unsigned n)
2973 if (!bset)
2974 return NULL;
2975 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2976 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2977 error:
2978 isl_basic_set_free(bset);
2979 return NULL;
2982 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2983 enum isl_dim_type type)
2985 isl_space *space;
2987 if (!map || !isl_space_is_named_or_nested(map->dim, type))
2988 return map;
2990 space = isl_map_get_space(map);
2991 space = isl_space_reset(space, type);
2992 map = isl_map_reset_space(map, space);
2993 return map;
2996 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2997 enum isl_dim_type type, unsigned pos, unsigned n)
2999 int i;
3001 if (n == 0)
3002 return map_space_reset(map, type);
3004 map = isl_map_cow(map);
3005 if (!map)
3006 return NULL;
3008 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3009 if (!map->dim)
3010 goto error;
3012 for (i = 0; i < map->n; ++i) {
3013 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3014 if (!map->p[i])
3015 goto error;
3018 return map;
3019 error:
3020 isl_map_free(map);
3021 return NULL;
3024 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3025 enum isl_dim_type type, unsigned pos, unsigned n)
3027 return isl_map_insert_dims(set, type, pos, n);
3030 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3031 enum isl_dim_type type, unsigned n)
3033 if (!map)
3034 return NULL;
3035 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3038 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3039 enum isl_dim_type type, unsigned n)
3041 if (!set)
3042 return NULL;
3043 isl_assert(set->ctx, type != isl_dim_in, goto error);
3044 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3045 error:
3046 isl_set_free(set);
3047 return NULL;
3050 __isl_give isl_basic_map *isl_basic_map_move_dims(
3051 __isl_take isl_basic_map *bmap,
3052 enum isl_dim_type dst_type, unsigned dst_pos,
3053 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3055 struct isl_dim_map *dim_map;
3056 struct isl_basic_map *res;
3057 enum isl_dim_type t;
3058 unsigned total, off;
3060 if (!bmap)
3061 return NULL;
3062 if (n == 0)
3063 return bmap;
3065 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3066 goto error);
3068 if (dst_type == src_type && dst_pos == src_pos)
3069 return bmap;
3071 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3073 if (pos(bmap->dim, dst_type) + dst_pos ==
3074 pos(bmap->dim, src_type) + src_pos +
3075 ((src_type < dst_type) ? n : 0)) {
3076 bmap = isl_basic_map_cow(bmap);
3077 if (!bmap)
3078 return NULL;
3080 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3081 src_type, src_pos, n);
3082 if (!bmap->dim)
3083 goto error;
3085 bmap = isl_basic_map_finalize(bmap);
3087 return bmap;
3090 total = isl_basic_map_total_dim(bmap);
3091 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3093 off = 0;
3094 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3095 unsigned size = isl_space_dim(bmap->dim, t);
3096 if (t == dst_type) {
3097 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3098 0, dst_pos, off);
3099 off += dst_pos;
3100 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3101 src_pos, n, off);
3102 off += n;
3103 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3104 dst_pos, size - dst_pos, off);
3105 off += size - dst_pos;
3106 } else if (t == src_type) {
3107 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3108 0, src_pos, off);
3109 off += src_pos;
3110 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3111 src_pos + n, size - src_pos - n, off);
3112 off += size - src_pos - n;
3113 } else {
3114 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3115 off += size;
3118 isl_dim_map_div(dim_map, bmap, off);
3120 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3121 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3122 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3124 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3125 src_type, src_pos, n);
3126 if (!bmap->dim)
3127 goto error;
3129 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3130 bmap = isl_basic_map_gauss(bmap, NULL);
3131 bmap = isl_basic_map_finalize(bmap);
3133 return bmap;
3134 error:
3135 isl_basic_map_free(bmap);
3136 return NULL;
3139 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3140 enum isl_dim_type dst_type, unsigned dst_pos,
3141 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3143 return (isl_basic_set *)isl_basic_map_move_dims(
3144 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3147 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3148 enum isl_dim_type dst_type, unsigned dst_pos,
3149 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3151 if (!set)
3152 return NULL;
3153 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3154 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3155 src_type, src_pos, n);
3156 error:
3157 isl_set_free(set);
3158 return NULL;
3161 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3162 enum isl_dim_type dst_type, unsigned dst_pos,
3163 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3165 int i;
3167 if (!map)
3168 return NULL;
3169 if (n == 0)
3170 return map;
3172 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3173 goto error);
3175 if (dst_type == src_type && dst_pos == src_pos)
3176 return map;
3178 isl_assert(map->ctx, dst_type != src_type, goto error);
3180 map = isl_map_cow(map);
3181 if (!map)
3182 return NULL;
3184 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3185 if (!map->dim)
3186 goto error;
3188 for (i = 0; i < map->n; ++i) {
3189 map->p[i] = isl_basic_map_move_dims(map->p[i],
3190 dst_type, dst_pos,
3191 src_type, src_pos, n);
3192 if (!map->p[i])
3193 goto error;
3196 return map;
3197 error:
3198 isl_map_free(map);
3199 return NULL;
3202 /* Move the specified dimensions to the last columns right before
3203 * the divs. Don't change the dimension specification of bmap.
3204 * That's the responsibility of the caller.
3206 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3207 enum isl_dim_type type, unsigned first, unsigned n)
3209 struct isl_dim_map *dim_map;
3210 struct isl_basic_map *res;
3211 enum isl_dim_type t;
3212 unsigned total, off;
3214 if (!bmap)
3215 return NULL;
3216 if (pos(bmap->dim, type) + first + n ==
3217 1 + isl_space_dim(bmap->dim, isl_dim_all))
3218 return bmap;
3220 total = isl_basic_map_total_dim(bmap);
3221 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3223 off = 0;
3224 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3225 unsigned size = isl_space_dim(bmap->dim, t);
3226 if (t == type) {
3227 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3228 0, first, off);
3229 off += first;
3230 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3231 first, n, total - bmap->n_div - n);
3232 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3233 first + n, size - (first + n), off);
3234 off += size - (first + n);
3235 } else {
3236 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3237 off += size;
3240 isl_dim_map_div(dim_map, bmap, off + n);
3242 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3243 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3244 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3245 return res;
3248 /* Turn the n dimensions of type type, starting at first
3249 * into existentially quantified variables.
3251 __isl_give isl_basic_map *isl_basic_map_project_out(
3252 __isl_take isl_basic_map *bmap,
3253 enum isl_dim_type type, unsigned first, unsigned n)
3255 int i;
3256 size_t row_size;
3257 isl_int **new_div;
3258 isl_int *old;
3260 if (n == 0)
3261 return basic_map_space_reset(bmap, type);
3263 if (!bmap)
3264 return NULL;
3266 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3267 return isl_basic_map_remove_dims(bmap, type, first, n);
3269 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3270 goto error);
3272 bmap = move_last(bmap, type, first, n);
3273 bmap = isl_basic_map_cow(bmap);
3274 if (!bmap)
3275 return NULL;
3277 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3278 old = bmap->block2.data;
3279 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3280 (bmap->extra + n) * (1 + row_size));
3281 if (!bmap->block2.data)
3282 goto error;
3283 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3284 if (!new_div)
3285 goto error;
3286 for (i = 0; i < n; ++i) {
3287 new_div[i] = bmap->block2.data +
3288 (bmap->extra + i) * (1 + row_size);
3289 isl_seq_clr(new_div[i], 1 + row_size);
3291 for (i = 0; i < bmap->extra; ++i)
3292 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3293 free(bmap->div);
3294 bmap->div = new_div;
3295 bmap->n_div += n;
3296 bmap->extra += n;
3298 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3299 if (!bmap->dim)
3300 goto error;
3301 bmap = isl_basic_map_simplify(bmap);
3302 bmap = isl_basic_map_drop_redundant_divs(bmap);
3303 return isl_basic_map_finalize(bmap);
3304 error:
3305 isl_basic_map_free(bmap);
3306 return NULL;
3309 /* Turn the n dimensions of type type, starting at first
3310 * into existentially quantified variables.
3312 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3313 enum isl_dim_type type, unsigned first, unsigned n)
3315 return (isl_basic_set *)isl_basic_map_project_out(
3316 (isl_basic_map *)bset, type, first, n);
3319 /* Turn the n dimensions of type type, starting at first
3320 * into existentially quantified variables.
3322 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3323 enum isl_dim_type type, unsigned first, unsigned n)
3325 int i;
3327 if (!map)
3328 return NULL;
3330 if (n == 0)
3331 return map_space_reset(map, type);
3333 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3335 map = isl_map_cow(map);
3336 if (!map)
3337 return NULL;
3339 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3340 if (!map->dim)
3341 goto error;
3343 for (i = 0; i < map->n; ++i) {
3344 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3345 if (!map->p[i])
3346 goto error;
3349 return map;
3350 error:
3351 isl_map_free(map);
3352 return NULL;
3355 /* Turn the n dimensions of type type, starting at first
3356 * into existentially quantified variables.
3358 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3359 enum isl_dim_type type, unsigned first, unsigned n)
3361 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3364 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3366 int i, j;
3368 for (i = 0; i < n; ++i) {
3369 j = isl_basic_map_alloc_div(bmap);
3370 if (j < 0)
3371 goto error;
3372 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3374 return bmap;
3375 error:
3376 isl_basic_map_free(bmap);
3377 return NULL;
3380 struct isl_basic_map *isl_basic_map_apply_range(
3381 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3383 isl_space *dim_result = NULL;
3384 struct isl_basic_map *bmap;
3385 unsigned n_in, n_out, n, nparam, total, pos;
3386 struct isl_dim_map *dim_map1, *dim_map2;
3388 if (!bmap1 || !bmap2)
3389 goto error;
3391 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3392 isl_space_copy(bmap2->dim));
3394 n_in = isl_basic_map_n_in(bmap1);
3395 n_out = isl_basic_map_n_out(bmap2);
3396 n = isl_basic_map_n_out(bmap1);
3397 nparam = isl_basic_map_n_param(bmap1);
3399 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3400 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3401 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3402 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3403 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3404 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3405 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3406 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3407 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3408 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3409 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3411 bmap = isl_basic_map_alloc_space(dim_result,
3412 bmap1->n_div + bmap2->n_div + n,
3413 bmap1->n_eq + bmap2->n_eq,
3414 bmap1->n_ineq + bmap2->n_ineq);
3415 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3416 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3417 bmap = add_divs(bmap, n);
3418 bmap = isl_basic_map_simplify(bmap);
3419 bmap = isl_basic_map_drop_redundant_divs(bmap);
3420 return isl_basic_map_finalize(bmap);
3421 error:
3422 isl_basic_map_free(bmap1);
3423 isl_basic_map_free(bmap2);
3424 return NULL;
3427 struct isl_basic_set *isl_basic_set_apply(
3428 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3430 if (!bset || !bmap)
3431 goto error;
3433 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3434 goto error);
3436 return (struct isl_basic_set *)
3437 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3438 error:
3439 isl_basic_set_free(bset);
3440 isl_basic_map_free(bmap);
3441 return NULL;
3444 struct isl_basic_map *isl_basic_map_apply_domain(
3445 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3447 if (!bmap1 || !bmap2)
3448 goto error;
3450 isl_assert(bmap1->ctx,
3451 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3452 isl_assert(bmap1->ctx,
3453 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3454 goto error);
3456 bmap1 = isl_basic_map_reverse(bmap1);
3457 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3458 return isl_basic_map_reverse(bmap1);
3459 error:
3460 isl_basic_map_free(bmap1);
3461 isl_basic_map_free(bmap2);
3462 return NULL;
3465 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3466 * A \cap B -> f(A) + f(B)
3468 struct isl_basic_map *isl_basic_map_sum(
3469 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3471 unsigned n_in, n_out, nparam, total, pos;
3472 struct isl_basic_map *bmap = NULL;
3473 struct isl_dim_map *dim_map1, *dim_map2;
3474 int i;
3476 if (!bmap1 || !bmap2)
3477 goto error;
3479 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3480 goto error);
3482 nparam = isl_basic_map_n_param(bmap1);
3483 n_in = isl_basic_map_n_in(bmap1);
3484 n_out = isl_basic_map_n_out(bmap1);
3486 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3487 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3488 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3489 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3490 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3491 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3492 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3493 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3494 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3495 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3496 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3498 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3499 bmap1->n_div + bmap2->n_div + 2 * n_out,
3500 bmap1->n_eq + bmap2->n_eq + n_out,
3501 bmap1->n_ineq + bmap2->n_ineq);
3502 for (i = 0; i < n_out; ++i) {
3503 int j = isl_basic_map_alloc_equality(bmap);
3504 if (j < 0)
3505 goto error;
3506 isl_seq_clr(bmap->eq[j], 1+total);
3507 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3508 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3509 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3511 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3512 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3513 bmap = add_divs(bmap, 2 * n_out);
3515 bmap = isl_basic_map_simplify(bmap);
3516 return isl_basic_map_finalize(bmap);
3517 error:
3518 isl_basic_map_free(bmap);
3519 isl_basic_map_free(bmap1);
3520 isl_basic_map_free(bmap2);
3521 return NULL;
3524 /* Given two maps A -> f(A) and B -> g(B), construct a map
3525 * A \cap B -> f(A) + f(B)
3527 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3529 struct isl_map *result;
3530 int i, j;
3532 if (!map1 || !map2)
3533 goto error;
3535 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3537 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3538 map1->n * map2->n, 0);
3539 if (!result)
3540 goto error;
3541 for (i = 0; i < map1->n; ++i)
3542 for (j = 0; j < map2->n; ++j) {
3543 struct isl_basic_map *part;
3544 part = isl_basic_map_sum(
3545 isl_basic_map_copy(map1->p[i]),
3546 isl_basic_map_copy(map2->p[j]));
3547 if (isl_basic_map_is_empty(part))
3548 isl_basic_map_free(part);
3549 else
3550 result = isl_map_add_basic_map(result, part);
3551 if (!result)
3552 goto error;
3554 isl_map_free(map1);
3555 isl_map_free(map2);
3556 return result;
3557 error:
3558 isl_map_free(map1);
3559 isl_map_free(map2);
3560 return NULL;
3563 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3564 __isl_take isl_set *set2)
3566 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3569 /* Given a basic map A -> f(A), construct A -> -f(A).
3571 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3573 int i, j;
3574 unsigned off, n;
3576 bmap = isl_basic_map_cow(bmap);
3577 if (!bmap)
3578 return NULL;
3580 n = isl_basic_map_dim(bmap, isl_dim_out);
3581 off = isl_basic_map_offset(bmap, isl_dim_out);
3582 for (i = 0; i < bmap->n_eq; ++i)
3583 for (j = 0; j < n; ++j)
3584 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3585 for (i = 0; i < bmap->n_ineq; ++i)
3586 for (j = 0; j < n; ++j)
3587 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3588 for (i = 0; i < bmap->n_div; ++i)
3589 for (j = 0; j < n; ++j)
3590 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3591 bmap = isl_basic_map_gauss(bmap, NULL);
3592 return isl_basic_map_finalize(bmap);
3595 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3597 return isl_basic_map_neg(bset);
3600 /* Given a map A -> f(A), construct A -> -f(A).
3602 struct isl_map *isl_map_neg(struct isl_map *map)
3604 int i;
3606 map = isl_map_cow(map);
3607 if (!map)
3608 return NULL;
3610 for (i = 0; i < map->n; ++i) {
3611 map->p[i] = isl_basic_map_neg(map->p[i]);
3612 if (!map->p[i])
3613 goto error;
3616 return map;
3617 error:
3618 isl_map_free(map);
3619 return NULL;
3622 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3624 return (isl_set *)isl_map_neg((isl_map *)set);
3627 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3628 * A -> floor(f(A)/d).
3630 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3631 isl_int d)
3633 unsigned n_in, n_out, nparam, total, pos;
3634 struct isl_basic_map *result = NULL;
3635 struct isl_dim_map *dim_map;
3636 int i;
3638 if (!bmap)
3639 return NULL;
3641 nparam = isl_basic_map_n_param(bmap);
3642 n_in = isl_basic_map_n_in(bmap);
3643 n_out = isl_basic_map_n_out(bmap);
3645 total = nparam + n_in + n_out + bmap->n_div + n_out;
3646 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3647 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3648 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3649 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3650 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3652 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3653 bmap->n_div + n_out,
3654 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3655 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3656 result = add_divs(result, n_out);
3657 for (i = 0; i < n_out; ++i) {
3658 int j;
3659 j = isl_basic_map_alloc_inequality(result);
3660 if (j < 0)
3661 goto error;
3662 isl_seq_clr(result->ineq[j], 1+total);
3663 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3664 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3665 j = isl_basic_map_alloc_inequality(result);
3666 if (j < 0)
3667 goto error;
3668 isl_seq_clr(result->ineq[j], 1+total);
3669 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3670 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3671 isl_int_sub_ui(result->ineq[j][0], d, 1);
3674 result = isl_basic_map_simplify(result);
3675 return isl_basic_map_finalize(result);
3676 error:
3677 isl_basic_map_free(result);
3678 return NULL;
3681 /* Given a map A -> f(A) and an integer d, construct a map
3682 * A -> floor(f(A)/d).
3684 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3686 int i;
3688 map = isl_map_cow(map);
3689 if (!map)
3690 return NULL;
3692 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3693 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3694 for (i = 0; i < map->n; ++i) {
3695 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3696 if (!map->p[i])
3697 goto error;
3700 return map;
3701 error:
3702 isl_map_free(map);
3703 return NULL;
3706 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3708 int i;
3709 unsigned nparam;
3710 unsigned n_in;
3712 i = isl_basic_map_alloc_equality(bmap);
3713 if (i < 0)
3714 goto error;
3715 nparam = isl_basic_map_n_param(bmap);
3716 n_in = isl_basic_map_n_in(bmap);
3717 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3718 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3719 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3720 return isl_basic_map_finalize(bmap);
3721 error:
3722 isl_basic_map_free(bmap);
3723 return NULL;
3726 /* Add a constraints to "bmap" expressing i_pos < o_pos
3728 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3730 int i;
3731 unsigned nparam;
3732 unsigned n_in;
3734 i = isl_basic_map_alloc_inequality(bmap);
3735 if (i < 0)
3736 goto error;
3737 nparam = isl_basic_map_n_param(bmap);
3738 n_in = isl_basic_map_n_in(bmap);
3739 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3740 isl_int_set_si(bmap->ineq[i][0], -1);
3741 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3742 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3743 return isl_basic_map_finalize(bmap);
3744 error:
3745 isl_basic_map_free(bmap);
3746 return NULL;
3749 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3751 static __isl_give isl_basic_map *var_less_or_equal(
3752 __isl_take isl_basic_map *bmap, unsigned pos)
3754 int i;
3755 unsigned nparam;
3756 unsigned n_in;
3758 i = isl_basic_map_alloc_inequality(bmap);
3759 if (i < 0)
3760 goto error;
3761 nparam = isl_basic_map_n_param(bmap);
3762 n_in = isl_basic_map_n_in(bmap);
3763 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3764 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3765 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3766 return isl_basic_map_finalize(bmap);
3767 error:
3768 isl_basic_map_free(bmap);
3769 return NULL;
3772 /* Add a constraints to "bmap" expressing i_pos > o_pos
3774 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3776 int i;
3777 unsigned nparam;
3778 unsigned n_in;
3780 i = isl_basic_map_alloc_inequality(bmap);
3781 if (i < 0)
3782 goto error;
3783 nparam = isl_basic_map_n_param(bmap);
3784 n_in = isl_basic_map_n_in(bmap);
3785 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3786 isl_int_set_si(bmap->ineq[i][0], -1);
3787 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3788 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3789 return isl_basic_map_finalize(bmap);
3790 error:
3791 isl_basic_map_free(bmap);
3792 return NULL;
3795 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3797 static __isl_give isl_basic_map *var_more_or_equal(
3798 __isl_take isl_basic_map *bmap, unsigned pos)
3800 int i;
3801 unsigned nparam;
3802 unsigned n_in;
3804 i = isl_basic_map_alloc_inequality(bmap);
3805 if (i < 0)
3806 goto error;
3807 nparam = isl_basic_map_n_param(bmap);
3808 n_in = isl_basic_map_n_in(bmap);
3809 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3810 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3811 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3812 return isl_basic_map_finalize(bmap);
3813 error:
3814 isl_basic_map_free(bmap);
3815 return NULL;
3818 __isl_give isl_basic_map *isl_basic_map_equal(
3819 __isl_take isl_space *dim, unsigned n_equal)
3821 int i;
3822 struct isl_basic_map *bmap;
3823 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3824 if (!bmap)
3825 return NULL;
3826 for (i = 0; i < n_equal && bmap; ++i)
3827 bmap = var_equal(bmap, i);
3828 return isl_basic_map_finalize(bmap);
3831 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3833 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3834 unsigned pos)
3836 int i;
3837 struct isl_basic_map *bmap;
3838 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3839 if (!bmap)
3840 return NULL;
3841 for (i = 0; i < pos && bmap; ++i)
3842 bmap = var_equal(bmap, i);
3843 if (bmap)
3844 bmap = var_less(bmap, pos);
3845 return isl_basic_map_finalize(bmap);
3848 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3850 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3851 __isl_take isl_space *dim, unsigned pos)
3853 int i;
3854 isl_basic_map *bmap;
3856 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3857 for (i = 0; i < pos; ++i)
3858 bmap = var_equal(bmap, i);
3859 bmap = var_less_or_equal(bmap, pos);
3860 return isl_basic_map_finalize(bmap);
3863 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3865 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3866 unsigned pos)
3868 int i;
3869 struct isl_basic_map *bmap;
3870 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3871 if (!bmap)
3872 return NULL;
3873 for (i = 0; i < pos && bmap; ++i)
3874 bmap = var_equal(bmap, i);
3875 if (bmap)
3876 bmap = var_more(bmap, pos);
3877 return isl_basic_map_finalize(bmap);
3880 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3882 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3883 __isl_take isl_space *dim, unsigned pos)
3885 int i;
3886 isl_basic_map *bmap;
3888 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3889 for (i = 0; i < pos; ++i)
3890 bmap = var_equal(bmap, i);
3891 bmap = var_more_or_equal(bmap, pos);
3892 return isl_basic_map_finalize(bmap);
3895 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3896 unsigned n, int equal)
3898 struct isl_map *map;
3899 int i;
3901 if (n == 0 && equal)
3902 return isl_map_universe(dims);
3904 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3906 for (i = 0; i + 1 < n; ++i)
3907 map = isl_map_add_basic_map(map,
3908 isl_basic_map_less_at(isl_space_copy(dims), i));
3909 if (n > 0) {
3910 if (equal)
3911 map = isl_map_add_basic_map(map,
3912 isl_basic_map_less_or_equal_at(dims, n - 1));
3913 else
3914 map = isl_map_add_basic_map(map,
3915 isl_basic_map_less_at(dims, n - 1));
3916 } else
3917 isl_space_free(dims);
3919 return map;
3922 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3924 if (!dims)
3925 return NULL;
3926 return map_lex_lte_first(dims, dims->n_out, equal);
3929 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3931 return map_lex_lte_first(dim, n, 0);
3934 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3936 return map_lex_lte_first(dim, n, 1);
3939 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3941 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3944 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3946 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3949 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3950 unsigned n, int equal)
3952 struct isl_map *map;
3953 int i;
3955 if (n == 0 && equal)
3956 return isl_map_universe(dims);
3958 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3960 for (i = 0; i + 1 < n; ++i)
3961 map = isl_map_add_basic_map(map,
3962 isl_basic_map_more_at(isl_space_copy(dims), i));
3963 if (n > 0) {
3964 if (equal)
3965 map = isl_map_add_basic_map(map,
3966 isl_basic_map_more_or_equal_at(dims, n - 1));
3967 else
3968 map = isl_map_add_basic_map(map,
3969 isl_basic_map_more_at(dims, n - 1));
3970 } else
3971 isl_space_free(dims);
3973 return map;
3976 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3978 if (!dims)
3979 return NULL;
3980 return map_lex_gte_first(dims, dims->n_out, equal);
3983 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3985 return map_lex_gte_first(dim, n, 0);
3988 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3990 return map_lex_gte_first(dim, n, 1);
3993 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3995 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3998 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4000 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4003 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4004 __isl_take isl_set *set2)
4006 isl_map *map;
4007 map = isl_map_lex_le(isl_set_get_space(set1));
4008 map = isl_map_intersect_domain(map, set1);
4009 map = isl_map_intersect_range(map, set2);
4010 return map;
4013 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4014 __isl_take isl_set *set2)
4016 isl_map *map;
4017 map = isl_map_lex_lt(isl_set_get_space(set1));
4018 map = isl_map_intersect_domain(map, set1);
4019 map = isl_map_intersect_range(map, set2);
4020 return map;
4023 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4024 __isl_take isl_set *set2)
4026 isl_map *map;
4027 map = isl_map_lex_ge(isl_set_get_space(set1));
4028 map = isl_map_intersect_domain(map, set1);
4029 map = isl_map_intersect_range(map, set2);
4030 return map;
4033 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4034 __isl_take isl_set *set2)
4036 isl_map *map;
4037 map = isl_map_lex_gt(isl_set_get_space(set1));
4038 map = isl_map_intersect_domain(map, set1);
4039 map = isl_map_intersect_range(map, set2);
4040 return map;
4043 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4044 __isl_take isl_map *map2)
4046 isl_map *map;
4047 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4048 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4049 map = isl_map_apply_range(map, isl_map_reverse(map2));
4050 return map;
4053 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4054 __isl_take isl_map *map2)
4056 isl_map *map;
4057 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4058 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4059 map = isl_map_apply_range(map, isl_map_reverse(map2));
4060 return map;
4063 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4064 __isl_take isl_map *map2)
4066 isl_map *map;
4067 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4068 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4069 map = isl_map_apply_range(map, isl_map_reverse(map2));
4070 return map;
4073 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4074 __isl_take isl_map *map2)
4076 isl_map *map;
4077 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4078 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4079 map = isl_map_apply_range(map, isl_map_reverse(map2));
4080 return map;
4083 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4084 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4086 struct isl_basic_map *bmap;
4088 bset = isl_basic_set_cow(bset);
4089 if (!bset || !dim)
4090 goto error;
4092 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4093 isl_space_free(bset->dim);
4094 bmap = (struct isl_basic_map *) bset;
4095 bmap->dim = dim;
4096 return isl_basic_map_finalize(bmap);
4097 error:
4098 isl_basic_set_free(bset);
4099 isl_space_free(dim);
4100 return NULL;
4103 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4105 if (!bmap)
4106 goto error;
4107 if (bmap->dim->n_in == 0)
4108 return (struct isl_basic_set *)bmap;
4109 bmap = isl_basic_map_cow(bmap);
4110 if (!bmap)
4111 goto error;
4112 bmap->dim = isl_space_as_set_space(bmap->dim);
4113 if (!bmap->dim)
4114 goto error;
4115 bmap = isl_basic_map_finalize(bmap);
4116 return (struct isl_basic_set *)bmap;
4117 error:
4118 isl_basic_map_free(bmap);
4119 return NULL;
4122 /* For a div d = floor(f/m), add the constraints
4124 * f - m d >= 0
4125 * -(f-(n-1)) + m d >= 0
4127 * Note that the second constraint is the negation of
4129 * f - m d >= n
4131 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4132 unsigned pos, isl_int *div)
4134 int i, j;
4135 unsigned total = isl_basic_map_total_dim(bmap);
4137 i = isl_basic_map_alloc_inequality(bmap);
4138 if (i < 0)
4139 return -1;
4140 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4141 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4143 j = isl_basic_map_alloc_inequality(bmap);
4144 if (j < 0)
4145 return -1;
4146 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
4147 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
4148 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
4149 return j;
4152 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4153 unsigned pos, isl_int *div)
4155 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4156 pos, div);
4159 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4161 unsigned total = isl_basic_map_total_dim(bmap);
4162 unsigned div_pos = total - bmap->n_div + div;
4164 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4165 bmap->div[div]);
4168 struct isl_basic_set *isl_basic_map_underlying_set(
4169 struct isl_basic_map *bmap)
4171 if (!bmap)
4172 goto error;
4173 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4174 bmap->n_div == 0 &&
4175 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4176 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4177 return (struct isl_basic_set *)bmap;
4178 bmap = isl_basic_map_cow(bmap);
4179 if (!bmap)
4180 goto error;
4181 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4182 if (!bmap->dim)
4183 goto error;
4184 bmap->extra -= bmap->n_div;
4185 bmap->n_div = 0;
4186 bmap = isl_basic_map_finalize(bmap);
4187 return (struct isl_basic_set *)bmap;
4188 error:
4189 return NULL;
4192 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4193 __isl_take isl_basic_set *bset)
4195 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4198 struct isl_basic_map *isl_basic_map_overlying_set(
4199 struct isl_basic_set *bset, struct isl_basic_map *like)
4201 struct isl_basic_map *bmap;
4202 struct isl_ctx *ctx;
4203 unsigned total;
4204 int i;
4206 if (!bset || !like)
4207 goto error;
4208 ctx = bset->ctx;
4209 isl_assert(ctx, bset->n_div == 0, goto error);
4210 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4211 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4212 goto error);
4213 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4214 isl_basic_map_free(like);
4215 return (struct isl_basic_map *)bset;
4217 bset = isl_basic_set_cow(bset);
4218 if (!bset)
4219 goto error;
4220 total = bset->dim->n_out + bset->extra;
4221 bmap = (struct isl_basic_map *)bset;
4222 isl_space_free(bmap->dim);
4223 bmap->dim = isl_space_copy(like->dim);
4224 if (!bmap->dim)
4225 goto error;
4226 bmap->n_div = like->n_div;
4227 bmap->extra += like->n_div;
4228 if (bmap->extra) {
4229 unsigned ltotal;
4230 isl_int **div;
4231 ltotal = total - bmap->extra + like->extra;
4232 if (ltotal > total)
4233 ltotal = total;
4234 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4235 bmap->extra * (1 + 1 + total));
4236 if (isl_blk_is_error(bmap->block2))
4237 goto error;
4238 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4239 if (!div)
4240 goto error;
4241 bmap->div = div;
4242 for (i = 0; i < bmap->extra; ++i)
4243 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4244 for (i = 0; i < like->n_div; ++i) {
4245 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4246 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4248 bmap = isl_basic_map_extend_constraints(bmap,
4249 0, 2 * like->n_div);
4250 for (i = 0; i < like->n_div; ++i) {
4251 if (isl_int_is_zero(bmap->div[i][0]))
4252 continue;
4253 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4254 goto error;
4257 isl_basic_map_free(like);
4258 bmap = isl_basic_map_simplify(bmap);
4259 bmap = isl_basic_map_finalize(bmap);
4260 return bmap;
4261 error:
4262 isl_basic_map_free(like);
4263 isl_basic_set_free(bset);
4264 return NULL;
4267 struct isl_basic_set *isl_basic_set_from_underlying_set(
4268 struct isl_basic_set *bset, struct isl_basic_set *like)
4270 return (struct isl_basic_set *)
4271 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4274 struct isl_set *isl_set_from_underlying_set(
4275 struct isl_set *set, struct isl_basic_set *like)
4277 int i;
4279 if (!set || !like)
4280 goto error;
4281 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4282 goto error);
4283 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4284 isl_basic_set_free(like);
4285 return set;
4287 set = isl_set_cow(set);
4288 if (!set)
4289 goto error;
4290 for (i = 0; i < set->n; ++i) {
4291 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4292 isl_basic_set_copy(like));
4293 if (!set->p[i])
4294 goto error;
4296 isl_space_free(set->dim);
4297 set->dim = isl_space_copy(like->dim);
4298 if (!set->dim)
4299 goto error;
4300 isl_basic_set_free(like);
4301 return set;
4302 error:
4303 isl_basic_set_free(like);
4304 isl_set_free(set);
4305 return NULL;
4308 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4310 int i;
4312 map = isl_map_cow(map);
4313 if (!map)
4314 return NULL;
4315 map->dim = isl_space_cow(map->dim);
4316 if (!map->dim)
4317 goto error;
4319 for (i = 1; i < map->n; ++i)
4320 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4321 goto error);
4322 for (i = 0; i < map->n; ++i) {
4323 map->p[i] = (struct isl_basic_map *)
4324 isl_basic_map_underlying_set(map->p[i]);
4325 if (!map->p[i])
4326 goto error;
4328 if (map->n == 0)
4329 map->dim = isl_space_underlying(map->dim, 0);
4330 else {
4331 isl_space_free(map->dim);
4332 map->dim = isl_space_copy(map->p[0]->dim);
4334 if (!map->dim)
4335 goto error;
4336 return (struct isl_set *)map;
4337 error:
4338 isl_map_free(map);
4339 return NULL;
4342 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4344 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4347 __isl_give isl_basic_map *isl_basic_map_reset_space(
4348 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4350 bmap = isl_basic_map_cow(bmap);
4351 if (!bmap || !dim)
4352 goto error;
4354 isl_space_free(bmap->dim);
4355 bmap->dim = dim;
4357 bmap = isl_basic_map_finalize(bmap);
4359 return bmap;
4360 error:
4361 isl_basic_map_free(bmap);
4362 isl_space_free(dim);
4363 return NULL;
4366 __isl_give isl_basic_set *isl_basic_set_reset_space(
4367 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4369 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4370 dim);
4373 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4374 __isl_take isl_space *dim)
4376 int i;
4378 map = isl_map_cow(map);
4379 if (!map || !dim)
4380 goto error;
4382 for (i = 0; i < map->n; ++i) {
4383 map->p[i] = isl_basic_map_reset_space(map->p[i],
4384 isl_space_copy(dim));
4385 if (!map->p[i])
4386 goto error;
4388 isl_space_free(map->dim);
4389 map->dim = dim;
4391 return map;
4392 error:
4393 isl_map_free(map);
4394 isl_space_free(dim);
4395 return NULL;
4398 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4399 __isl_take isl_space *dim)
4401 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4404 /* Compute the parameter domain of the given basic set.
4406 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4408 isl_space *space;
4409 unsigned n;
4411 if (isl_basic_set_is_params(bset))
4412 return bset;
4414 n = isl_basic_set_dim(bset, isl_dim_set);
4415 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4416 space = isl_basic_set_get_space(bset);
4417 space = isl_space_params(space);
4418 bset = isl_basic_set_reset_space(bset, space);
4419 return bset;
4422 /* Compute the parameter domain of the given set.
4424 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4426 isl_space *space;
4427 unsigned n;
4429 if (isl_set_is_params(set))
4430 return set;
4432 n = isl_set_dim(set, isl_dim_set);
4433 set = isl_set_project_out(set, isl_dim_set, 0, n);
4434 space = isl_set_get_space(set);
4435 space = isl_space_params(space);
4436 set = isl_set_reset_space(set, space);
4437 return set;
4440 /* Construct a zero-dimensional set with the given parameter domain.
4442 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4444 isl_space *space;
4445 space = isl_set_get_space(set);
4446 space = isl_space_set_from_params(space);
4447 set = isl_set_reset_space(set, space);
4448 return set;
4451 /* Compute the parameter domain of the given map.
4453 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4455 isl_space *space;
4456 unsigned n;
4458 n = isl_map_dim(map, isl_dim_in);
4459 map = isl_map_project_out(map, isl_dim_in, 0, n);
4460 n = isl_map_dim(map, isl_dim_out);
4461 map = isl_map_project_out(map, isl_dim_out, 0, n);
4462 space = isl_map_get_space(map);
4463 space = isl_space_params(space);
4464 map = isl_map_reset_space(map, space);
4465 return map;
4468 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4470 isl_space *dim;
4471 struct isl_basic_set *domain;
4472 unsigned n_in;
4473 unsigned n_out;
4475 if (!bmap)
4476 return NULL;
4477 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4479 n_in = isl_basic_map_n_in(bmap);
4480 n_out = isl_basic_map_n_out(bmap);
4481 domain = isl_basic_set_from_basic_map(bmap);
4482 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4484 domain = isl_basic_set_reset_space(domain, dim);
4486 return domain;
4489 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4491 if (!bmap)
4492 return -1;
4493 return isl_space_may_be_set(bmap->dim);
4496 /* Is this basic map actually a set?
4497 * Users should never call this function. Outside of isl,
4498 * the type should indicate whether something is a set or a map.
4500 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4502 if (!bmap)
4503 return -1;
4504 return isl_space_is_set(bmap->dim);
4507 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4509 if (!bmap)
4510 return NULL;
4511 if (isl_basic_map_is_set(bmap))
4512 return bmap;
4513 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4516 __isl_give isl_basic_map *isl_basic_map_domain_map(
4517 __isl_take isl_basic_map *bmap)
4519 int i, k;
4520 isl_space *dim;
4521 isl_basic_map *domain;
4522 int nparam, n_in, n_out;
4523 unsigned total;
4525 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4526 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4527 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4529 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4530 domain = isl_basic_map_universe(dim);
4532 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4533 bmap = isl_basic_map_apply_range(bmap, domain);
4534 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4536 total = isl_basic_map_total_dim(bmap);
4538 for (i = 0; i < n_in; ++i) {
4539 k = isl_basic_map_alloc_equality(bmap);
4540 if (k < 0)
4541 goto error;
4542 isl_seq_clr(bmap->eq[k], 1 + total);
4543 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4544 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4547 bmap = isl_basic_map_gauss(bmap, NULL);
4548 return isl_basic_map_finalize(bmap);
4549 error:
4550 isl_basic_map_free(bmap);
4551 return NULL;
4554 __isl_give isl_basic_map *isl_basic_map_range_map(
4555 __isl_take isl_basic_map *bmap)
4557 int i, k;
4558 isl_space *dim;
4559 isl_basic_map *range;
4560 int nparam, n_in, n_out;
4561 unsigned total;
4563 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4564 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4565 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4567 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4568 range = isl_basic_map_universe(dim);
4570 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4571 bmap = isl_basic_map_apply_range(bmap, range);
4572 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4574 total = isl_basic_map_total_dim(bmap);
4576 for (i = 0; i < n_out; ++i) {
4577 k = isl_basic_map_alloc_equality(bmap);
4578 if (k < 0)
4579 goto error;
4580 isl_seq_clr(bmap->eq[k], 1 + total);
4581 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4582 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4585 bmap = isl_basic_map_gauss(bmap, NULL);
4586 return isl_basic_map_finalize(bmap);
4587 error:
4588 isl_basic_map_free(bmap);
4589 return NULL;
4592 int isl_map_may_be_set(__isl_keep isl_map *map)
4594 if (!map)
4595 return -1;
4596 return isl_space_may_be_set(map->dim);
4599 /* Is this map actually a set?
4600 * Users should never call this function. Outside of isl,
4601 * the type should indicate whether something is a set or a map.
4603 int isl_map_is_set(__isl_keep isl_map *map)
4605 if (!map)
4606 return -1;
4607 return isl_space_is_set(map->dim);
4610 struct isl_set *isl_map_range(struct isl_map *map)
4612 int i;
4613 struct isl_set *set;
4615 if (!map)
4616 goto error;
4617 if (isl_map_is_set(map))
4618 return (isl_set *)map;
4620 map = isl_map_cow(map);
4621 if (!map)
4622 goto error;
4624 set = (struct isl_set *) map;
4625 set->dim = isl_space_range(set->dim);
4626 if (!set->dim)
4627 goto error;
4628 for (i = 0; i < map->n; ++i) {
4629 set->p[i] = isl_basic_map_range(map->p[i]);
4630 if (!set->p[i])
4631 goto error;
4633 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4634 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4635 return set;
4636 error:
4637 isl_map_free(map);
4638 return NULL;
4641 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4643 int i;
4644 isl_space *domain_dim;
4646 map = isl_map_cow(map);
4647 if (!map)
4648 return NULL;
4650 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4651 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4652 map->dim = isl_space_join(map->dim, domain_dim);
4653 if (!map->dim)
4654 goto error;
4655 for (i = 0; i < map->n; ++i) {
4656 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4657 if (!map->p[i])
4658 goto error;
4660 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4661 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4662 return map;
4663 error:
4664 isl_map_free(map);
4665 return NULL;
4668 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4670 int i;
4671 isl_space *range_dim;
4673 map = isl_map_cow(map);
4674 if (!map)
4675 return NULL;
4677 range_dim = isl_space_range(isl_map_get_space(map));
4678 range_dim = isl_space_from_range(range_dim);
4679 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4680 map->dim = isl_space_join(map->dim, range_dim);
4681 if (!map->dim)
4682 goto error;
4683 for (i = 0; i < map->n; ++i) {
4684 map->p[i] = isl_basic_map_range_map(map->p[i]);
4685 if (!map->p[i])
4686 goto error;
4688 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4689 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4690 return map;
4691 error:
4692 isl_map_free(map);
4693 return NULL;
4696 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4697 __isl_take isl_space *dim)
4699 int i;
4700 struct isl_map *map = NULL;
4702 set = isl_set_cow(set);
4703 if (!set || !dim)
4704 goto error;
4705 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4706 map = (struct isl_map *)set;
4707 for (i = 0; i < set->n; ++i) {
4708 map->p[i] = isl_basic_map_from_basic_set(
4709 set->p[i], isl_space_copy(dim));
4710 if (!map->p[i])
4711 goto error;
4713 isl_space_free(map->dim);
4714 map->dim = dim;
4715 return map;
4716 error:
4717 isl_space_free(dim);
4718 isl_set_free(set);
4719 return NULL;
4722 __isl_give isl_basic_map *isl_basic_map_from_domain(
4723 __isl_take isl_basic_set *bset)
4725 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4728 __isl_give isl_basic_map *isl_basic_map_from_range(
4729 __isl_take isl_basic_set *bset)
4731 isl_space *space;
4732 space = isl_basic_set_get_space(bset);
4733 space = isl_space_from_range(space);
4734 bset = isl_basic_set_reset_space(bset, space);
4735 return (isl_basic_map *)bset;
4738 struct isl_map *isl_map_from_range(struct isl_set *set)
4740 isl_space *space;
4741 space = isl_set_get_space(set);
4742 space = isl_space_from_range(space);
4743 set = isl_set_reset_space(set, space);
4744 return (struct isl_map *)set;
4747 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4749 return isl_map_reverse(isl_map_from_range(set));
4752 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4753 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4755 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4758 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4759 __isl_take isl_set *range)
4761 return isl_map_apply_range(isl_map_reverse(domain), range);
4764 struct isl_set *isl_set_from_map(struct isl_map *map)
4766 int i;
4767 struct isl_set *set = NULL;
4769 if (!map)
4770 return NULL;
4771 map = isl_map_cow(map);
4772 if (!map)
4773 return NULL;
4774 map->dim = isl_space_as_set_space(map->dim);
4775 if (!map->dim)
4776 goto error;
4777 set = (struct isl_set *)map;
4778 for (i = 0; i < map->n; ++i) {
4779 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4780 if (!set->p[i])
4781 goto error;
4783 return set;
4784 error:
4785 isl_map_free(map);
4786 return NULL;
4789 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4790 unsigned flags)
4792 struct isl_map *map;
4794 if (!dim)
4795 return NULL;
4796 if (n < 0)
4797 isl_die(dim->ctx, isl_error_internal,
4798 "negative number of basic maps", goto error);
4799 map = isl_alloc(dim->ctx, struct isl_map,
4800 sizeof(struct isl_map) +
4801 (n - 1) * sizeof(struct isl_basic_map *));
4802 if (!map)
4803 goto error;
4805 map->ctx = dim->ctx;
4806 isl_ctx_ref(map->ctx);
4807 map->ref = 1;
4808 map->size = n;
4809 map->n = 0;
4810 map->dim = dim;
4811 map->flags = flags;
4812 return map;
4813 error:
4814 isl_space_free(dim);
4815 return NULL;
4818 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4819 unsigned nparam, unsigned in, unsigned out, int n,
4820 unsigned flags)
4822 struct isl_map *map;
4823 isl_space *dims;
4825 dims = isl_space_alloc(ctx, nparam, in, out);
4826 if (!dims)
4827 return NULL;
4829 map = isl_map_alloc_space(dims, n, flags);
4830 return map;
4833 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4835 struct isl_basic_map *bmap;
4836 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4837 bmap = isl_basic_map_set_to_empty(bmap);
4838 return bmap;
4841 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4843 struct isl_basic_set *bset;
4844 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4845 bset = isl_basic_set_set_to_empty(bset);
4846 return bset;
4849 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4851 struct isl_basic_map *bmap;
4852 if (!model)
4853 return NULL;
4854 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4855 bmap = isl_basic_map_set_to_empty(bmap);
4856 return bmap;
4859 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4861 struct isl_basic_map *bmap;
4862 if (!model)
4863 return NULL;
4864 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4865 bmap = isl_basic_map_set_to_empty(bmap);
4866 return bmap;
4869 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4871 struct isl_basic_set *bset;
4872 if (!model)
4873 return NULL;
4874 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4875 bset = isl_basic_set_set_to_empty(bset);
4876 return bset;
4879 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4881 struct isl_basic_map *bmap;
4882 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4883 bmap = isl_basic_map_finalize(bmap);
4884 return bmap;
4887 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4889 struct isl_basic_set *bset;
4890 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4891 bset = isl_basic_set_finalize(bset);
4892 return bset;
4895 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4897 int i;
4898 unsigned total = isl_space_dim(dim, isl_dim_all);
4899 isl_basic_map *bmap;
4901 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4902 for (i = 0; i < total; ++i) {
4903 int k = isl_basic_map_alloc_inequality(bmap);
4904 if (k < 0)
4905 goto error;
4906 isl_seq_clr(bmap->ineq[k], 1 + total);
4907 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4909 return bmap;
4910 error:
4911 isl_basic_map_free(bmap);
4912 return NULL;
4915 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4917 return isl_basic_map_nat_universe(dim);
4920 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4922 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4925 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4927 return isl_map_nat_universe(dim);
4930 __isl_give isl_basic_map *isl_basic_map_universe_like(
4931 __isl_keep isl_basic_map *model)
4933 if (!model)
4934 return NULL;
4935 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4938 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4940 if (!model)
4941 return NULL;
4942 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4945 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4946 __isl_keep isl_set *model)
4948 if (!model)
4949 return NULL;
4950 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4953 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4955 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4958 struct isl_map *isl_map_empty_like(struct isl_map *model)
4960 if (!model)
4961 return NULL;
4962 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4965 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4967 if (!model)
4968 return NULL;
4969 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4972 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4974 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4977 struct isl_set *isl_set_empty_like(struct isl_set *model)
4979 if (!model)
4980 return NULL;
4981 return isl_set_empty(isl_space_copy(model->dim));
4984 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4986 struct isl_map *map;
4987 if (!dim)
4988 return NULL;
4989 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4990 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4991 return map;
4994 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4996 struct isl_set *set;
4997 if (!dim)
4998 return NULL;
4999 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5000 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5001 return set;
5004 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5006 if (!model)
5007 return NULL;
5008 return isl_set_universe(isl_space_copy(model->dim));
5011 struct isl_map *isl_map_dup(struct isl_map *map)
5013 int i;
5014 struct isl_map *dup;
5016 if (!map)
5017 return NULL;
5018 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5019 for (i = 0; i < map->n; ++i)
5020 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5021 return dup;
5024 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5025 __isl_take isl_basic_map *bmap)
5027 if (!bmap || !map)
5028 goto error;
5029 if (isl_basic_map_plain_is_empty(bmap)) {
5030 isl_basic_map_free(bmap);
5031 return map;
5033 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5034 isl_assert(map->ctx, map->n < map->size, goto error);
5035 map->p[map->n] = bmap;
5036 map->n++;
5037 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5038 return map;
5039 error:
5040 if (map)
5041 isl_map_free(map);
5042 if (bmap)
5043 isl_basic_map_free(bmap);
5044 return NULL;
5047 void isl_map_free(struct isl_map *map)
5049 int i;
5051 if (!map)
5052 return;
5054 if (--map->ref > 0)
5055 return;
5057 isl_ctx_deref(map->ctx);
5058 for (i = 0; i < map->n; ++i)
5059 isl_basic_map_free(map->p[i]);
5060 isl_space_free(map->dim);
5061 free(map);
5064 struct isl_map *isl_map_extend(struct isl_map *base,
5065 unsigned nparam, unsigned n_in, unsigned n_out)
5067 int i;
5069 base = isl_map_cow(base);
5070 if (!base)
5071 return NULL;
5073 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5074 if (!base->dim)
5075 goto error;
5076 for (i = 0; i < base->n; ++i) {
5077 base->p[i] = isl_basic_map_extend_space(base->p[i],
5078 isl_space_copy(base->dim), 0, 0, 0);
5079 if (!base->p[i])
5080 goto error;
5082 return base;
5083 error:
5084 isl_map_free(base);
5085 return NULL;
5088 struct isl_set *isl_set_extend(struct isl_set *base,
5089 unsigned nparam, unsigned dim)
5091 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5092 nparam, 0, dim);
5095 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5096 struct isl_basic_map *bmap, unsigned pos, int value)
5098 int j;
5100 bmap = isl_basic_map_cow(bmap);
5101 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5102 j = isl_basic_map_alloc_equality(bmap);
5103 if (j < 0)
5104 goto error;
5105 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5106 isl_int_set_si(bmap->eq[j][pos], -1);
5107 isl_int_set_si(bmap->eq[j][0], value);
5108 bmap = isl_basic_map_simplify(bmap);
5109 return isl_basic_map_finalize(bmap);
5110 error:
5111 isl_basic_map_free(bmap);
5112 return NULL;
5115 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5116 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5118 int j;
5120 bmap = isl_basic_map_cow(bmap);
5121 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5122 j = isl_basic_map_alloc_equality(bmap);
5123 if (j < 0)
5124 goto error;
5125 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5126 isl_int_set_si(bmap->eq[j][pos], -1);
5127 isl_int_set(bmap->eq[j][0], value);
5128 bmap = isl_basic_map_simplify(bmap);
5129 return isl_basic_map_finalize(bmap);
5130 error:
5131 isl_basic_map_free(bmap);
5132 return NULL;
5135 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5136 enum isl_dim_type type, unsigned pos, int value)
5138 if (!bmap)
5139 return NULL;
5140 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5141 return isl_basic_map_fix_pos_si(bmap,
5142 isl_basic_map_offset(bmap, type) + pos, value);
5143 error:
5144 isl_basic_map_free(bmap);
5145 return NULL;
5148 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5149 enum isl_dim_type type, unsigned pos, isl_int value)
5151 if (!bmap)
5152 return NULL;
5153 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5154 return isl_basic_map_fix_pos(bmap,
5155 isl_basic_map_offset(bmap, type) + pos, value);
5156 error:
5157 isl_basic_map_free(bmap);
5158 return NULL;
5161 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5162 enum isl_dim_type type, unsigned pos, int value)
5164 return (struct isl_basic_set *)
5165 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5166 type, pos, value);
5169 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5170 enum isl_dim_type type, unsigned pos, isl_int value)
5172 return (struct isl_basic_set *)
5173 isl_basic_map_fix((struct isl_basic_map *)bset,
5174 type, pos, value);
5177 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5178 unsigned input, int value)
5180 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5183 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5184 unsigned dim, int value)
5186 return (struct isl_basic_set *)
5187 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5188 isl_dim_set, dim, value);
5191 static int remove_if_empty(__isl_keep isl_map *map, int i)
5193 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5195 if (empty < 0)
5196 return -1;
5197 if (!empty)
5198 return 0;
5200 isl_basic_map_free(map->p[i]);
5201 if (i != map->n - 1) {
5202 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5203 map->p[i] = map->p[map->n - 1];
5205 map->n--;
5207 return 0;
5210 struct isl_map *isl_map_fix_si(struct isl_map *map,
5211 enum isl_dim_type type, unsigned pos, int value)
5213 int i;
5215 map = isl_map_cow(map);
5216 if (!map)
5217 return NULL;
5219 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5220 for (i = map->n - 1; i >= 0; --i) {
5221 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5222 if (remove_if_empty(map, i) < 0)
5223 goto error;
5225 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5226 return map;
5227 error:
5228 isl_map_free(map);
5229 return NULL;
5232 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5233 enum isl_dim_type type, unsigned pos, int value)
5235 return (struct isl_set *)
5236 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5239 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5240 enum isl_dim_type type, unsigned pos, isl_int value)
5242 int i;
5244 map = isl_map_cow(map);
5245 if (!map)
5246 return NULL;
5248 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5249 for (i = 0; i < map->n; ++i) {
5250 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5251 if (!map->p[i])
5252 goto error;
5254 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5255 return map;
5256 error:
5257 isl_map_free(map);
5258 return NULL;
5261 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5262 enum isl_dim_type type, unsigned pos, isl_int value)
5264 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5267 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5268 unsigned input, int value)
5270 return isl_map_fix_si(map, isl_dim_in, input, value);
5273 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5275 return (struct isl_set *)
5276 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5279 static __isl_give isl_basic_map *basic_map_bound_si(
5280 __isl_take isl_basic_map *bmap,
5281 enum isl_dim_type type, unsigned pos, int value, int upper)
5283 int j;
5285 if (!bmap)
5286 return NULL;
5287 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5288 pos += isl_basic_map_offset(bmap, type);
5289 bmap = isl_basic_map_cow(bmap);
5290 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5291 j = isl_basic_map_alloc_inequality(bmap);
5292 if (j < 0)
5293 goto error;
5294 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5295 if (upper) {
5296 isl_int_set_si(bmap->ineq[j][pos], -1);
5297 isl_int_set_si(bmap->ineq[j][0], value);
5298 } else {
5299 isl_int_set_si(bmap->ineq[j][pos], 1);
5300 isl_int_set_si(bmap->ineq[j][0], -value);
5302 bmap = isl_basic_map_simplify(bmap);
5303 return isl_basic_map_finalize(bmap);
5304 error:
5305 isl_basic_map_free(bmap);
5306 return NULL;
5309 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5310 __isl_take isl_basic_map *bmap,
5311 enum isl_dim_type type, unsigned pos, int value)
5313 return basic_map_bound_si(bmap, type, pos, value, 0);
5316 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5317 unsigned dim, isl_int value)
5319 int j;
5321 bset = isl_basic_set_cow(bset);
5322 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5323 j = isl_basic_set_alloc_inequality(bset);
5324 if (j < 0)
5325 goto error;
5326 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5327 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5328 isl_int_neg(bset->ineq[j][0], value);
5329 bset = isl_basic_set_simplify(bset);
5330 return isl_basic_set_finalize(bset);
5331 error:
5332 isl_basic_set_free(bset);
5333 return NULL;
5336 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5337 enum isl_dim_type type, unsigned pos, int value, int upper)
5339 int i;
5341 map = isl_map_cow(map);
5342 if (!map)
5343 return NULL;
5345 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5346 for (i = 0; i < map->n; ++i) {
5347 map->p[i] = basic_map_bound_si(map->p[i],
5348 type, pos, value, upper);
5349 if (!map->p[i])
5350 goto error;
5352 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5353 return map;
5354 error:
5355 isl_map_free(map);
5356 return NULL;
5359 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5360 enum isl_dim_type type, unsigned pos, int value)
5362 return map_bound_si(map, type, pos, value, 0);
5365 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5366 enum isl_dim_type type, unsigned pos, int value)
5368 return map_bound_si(map, type, pos, value, 1);
5371 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5372 enum isl_dim_type type, unsigned pos, int value)
5374 return (struct isl_set *)
5375 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5378 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5379 enum isl_dim_type type, unsigned pos, int value)
5381 return isl_map_upper_bound_si(set, type, pos, value);
5384 /* Bound the given variable of "bmap" from below (or above is "upper"
5385 * is set) to "value".
5387 static __isl_give isl_basic_map *basic_map_bound(
5388 __isl_take isl_basic_map *bmap,
5389 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5391 int j;
5393 if (!bmap)
5394 return NULL;
5395 if (pos >= isl_basic_map_dim(bmap, type))
5396 isl_die(bmap->ctx, isl_error_invalid,
5397 "index out of bounds", goto error);
5398 pos += isl_basic_map_offset(bmap, type);
5399 bmap = isl_basic_map_cow(bmap);
5400 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5401 j = isl_basic_map_alloc_inequality(bmap);
5402 if (j < 0)
5403 goto error;
5404 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5405 if (upper) {
5406 isl_int_set_si(bmap->ineq[j][pos], -1);
5407 isl_int_set(bmap->ineq[j][0], value);
5408 } else {
5409 isl_int_set_si(bmap->ineq[j][pos], 1);
5410 isl_int_neg(bmap->ineq[j][0], value);
5412 bmap = isl_basic_map_simplify(bmap);
5413 return isl_basic_map_finalize(bmap);
5414 error:
5415 isl_basic_map_free(bmap);
5416 return NULL;
5419 /* Bound the given variable of "map" from below (or above is "upper"
5420 * is set) to "value".
5422 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5423 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5425 int i;
5427 map = isl_map_cow(map);
5428 if (!map)
5429 return NULL;
5431 if (pos >= isl_map_dim(map, type))
5432 isl_die(map->ctx, isl_error_invalid,
5433 "index out of bounds", goto error);
5434 for (i = map->n - 1; i >= 0; --i) {
5435 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5436 if (remove_if_empty(map, i) < 0)
5437 goto error;
5439 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5440 return map;
5441 error:
5442 isl_map_free(map);
5443 return NULL;
5446 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5447 enum isl_dim_type type, unsigned pos, isl_int value)
5449 return map_bound(map, type, pos, value, 0);
5452 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5453 enum isl_dim_type type, unsigned pos, isl_int value)
5455 return map_bound(map, type, pos, value, 1);
5458 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5459 enum isl_dim_type type, unsigned pos, isl_int value)
5461 return isl_map_lower_bound(set, type, pos, value);
5464 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5465 enum isl_dim_type type, unsigned pos, isl_int value)
5467 return isl_map_upper_bound(set, type, pos, value);
5470 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5471 isl_int value)
5473 int i;
5475 set = isl_set_cow(set);
5476 if (!set)
5477 return NULL;
5479 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5480 for (i = 0; i < set->n; ++i) {
5481 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5482 if (!set->p[i])
5483 goto error;
5485 return set;
5486 error:
5487 isl_set_free(set);
5488 return NULL;
5491 struct isl_map *isl_map_reverse(struct isl_map *map)
5493 int i;
5495 map = isl_map_cow(map);
5496 if (!map)
5497 return NULL;
5499 map->dim = isl_space_reverse(map->dim);
5500 if (!map->dim)
5501 goto error;
5502 for (i = 0; i < map->n; ++i) {
5503 map->p[i] = isl_basic_map_reverse(map->p[i]);
5504 if (!map->p[i])
5505 goto error;
5507 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5508 return map;
5509 error:
5510 isl_map_free(map);
5511 return NULL;
5514 static struct isl_map *isl_basic_map_partial_lexopt(
5515 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5516 struct isl_set **empty, int max)
5518 if (!bmap)
5519 goto error;
5520 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5521 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5522 else
5523 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5524 error:
5525 isl_basic_map_free(bmap);
5526 isl_basic_set_free(dom);
5527 if (empty)
5528 *empty = NULL;
5529 return NULL;
5532 struct isl_map *isl_basic_map_partial_lexmax(
5533 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5534 struct isl_set **empty)
5536 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5539 struct isl_map *isl_basic_map_partial_lexmin(
5540 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5541 struct isl_set **empty)
5543 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5546 struct isl_set *isl_basic_set_partial_lexmin(
5547 struct isl_basic_set *bset, struct isl_basic_set *dom,
5548 struct isl_set **empty)
5550 return (struct isl_set *)
5551 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5552 dom, empty);
5555 struct isl_set *isl_basic_set_partial_lexmax(
5556 struct isl_basic_set *bset, struct isl_basic_set *dom,
5557 struct isl_set **empty)
5559 return (struct isl_set *)
5560 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5561 dom, empty);
5564 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5565 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5566 __isl_give isl_set **empty)
5568 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5571 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5572 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5573 __isl_give isl_set **empty)
5575 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5578 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5579 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5580 __isl_give isl_set **empty)
5582 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5585 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5586 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5587 __isl_give isl_set **empty)
5589 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5592 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5593 __isl_take isl_basic_map *bmap, int max)
5595 isl_basic_set *dom = NULL;
5596 isl_space *dom_space;
5598 if (!bmap)
5599 goto error;
5600 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5601 dom = isl_basic_set_universe(dom_space);
5602 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5603 error:
5604 isl_basic_map_free(bmap);
5605 return NULL;
5608 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5609 __isl_take isl_basic_map *bmap)
5611 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5614 /* Given a basic map "bmap", compute the lexicographically minimal
5615 * (or maximal) image element for each domain element in dom.
5616 * Set *empty to those elements in dom that do not have an image element.
5618 * We first make sure the basic sets in dom are disjoint and then
5619 * simply collect the results over each of the basic sets separately.
5620 * We could probably improve the efficiency a bit by moving the union
5621 * domain down into the parametric integer programming.
5623 static __isl_give isl_map *basic_map_partial_lexopt(
5624 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
5625 __isl_give isl_set **empty, int max)
5627 int i;
5628 struct isl_map *res;
5630 dom = isl_set_make_disjoint(dom);
5631 if (!dom)
5632 goto error;
5634 if (isl_set_plain_is_empty(dom)) {
5635 res = isl_map_empty_like_basic_map(bmap);
5636 *empty = isl_set_empty_like(dom);
5637 isl_set_free(dom);
5638 isl_basic_map_free(bmap);
5639 return res;
5642 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5643 isl_basic_set_copy(dom->p[0]), empty, max);
5645 for (i = 1; i < dom->n; ++i) {
5646 struct isl_map *res_i;
5647 struct isl_set *empty_i;
5649 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5650 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5652 res = isl_map_union_disjoint(res, res_i);
5653 *empty = isl_set_union_disjoint(*empty, empty_i);
5656 isl_set_free(dom);
5657 isl_basic_map_free(bmap);
5658 return res;
5659 error:
5660 *empty = NULL;
5661 isl_set_free(dom);
5662 isl_basic_map_free(bmap);
5663 return NULL;
5666 /* Given a map "map", compute the lexicographically minimal
5667 * (or maximal) image element for each domain element in dom.
5668 * Set *empty to those elements in dom that do not have an image element.
5670 * We first compute the lexicographically minimal or maximal element
5671 * in the first basic map. This results in a partial solution "res"
5672 * and a subset "todo" of dom that still need to be handled.
5673 * We then consider each of the remaining maps in "map" and successively
5674 * improve both "res" and "todo".
5676 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5677 * Assume we are computing the lexicographical maximum.
5678 * We first compute the lexicographically maximal element in basic map i.
5679 * This results in a partial solution res_i and a subset todo_i.
5680 * Then we combine these results with those obtain for the first k basic maps
5681 * to obtain a result that is valid for the first k+1 basic maps.
5682 * In particular, the set where there is no solution is the set where
5683 * there is no solution for the first k basic maps and also no solution
5684 * for the ith basic map, i.e.,
5686 * todo^i = todo^k * todo_i
5688 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5689 * solutions, arbitrarily breaking ties in favor of res^k.
5690 * That is, when res^k(a) >= res_i(a), we pick res^k and
5691 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5692 * the lexicographic order.)
5693 * In practice, we compute
5695 * res^k * (res_i . "<=")
5697 * and
5699 * res_i * (res^k . "<")
5701 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5702 * where only one of res^k and res_i provides a solution and we simply pick
5703 * that one, i.e.,
5705 * res^k * todo_i
5706 * and
5707 * res_i * todo^k
5709 * Note that we only compute these intersections when dom(res^k) intersects
5710 * dom(res_i). Otherwise, the only effect of these intersections is to
5711 * potentially break up res^k and res_i into smaller pieces.
5712 * We want to avoid such splintering as much as possible.
5713 * In fact, an earlier implementation of this function would look for
5714 * better results in the domain of res^k and for extra results in todo^k,
5715 * but this would always result in a splintering according to todo^k,
5716 * even when the domain of basic map i is disjoint from the domains of
5717 * the previous basic maps.
5719 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5720 __isl_take isl_map *map, __isl_take isl_set *dom,
5721 __isl_give isl_set **empty, int max)
5723 int i;
5724 struct isl_map *res;
5725 struct isl_set *todo;
5727 if (!map || !dom)
5728 goto error;
5730 if (isl_map_plain_is_empty(map)) {
5731 if (empty)
5732 *empty = dom;
5733 else
5734 isl_set_free(dom);
5735 return map;
5738 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5739 isl_set_copy(dom), &todo, max);
5741 for (i = 1; i < map->n; ++i) {
5742 isl_map *lt, *le;
5743 isl_map *res_i;
5744 isl_set *todo_i;
5745 isl_space *dim = isl_space_range(isl_map_get_space(res));
5747 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5748 isl_set_copy(dom), &todo_i, max);
5750 if (max) {
5751 lt = isl_map_lex_lt(isl_space_copy(dim));
5752 le = isl_map_lex_le(dim);
5753 } else {
5754 lt = isl_map_lex_gt(isl_space_copy(dim));
5755 le = isl_map_lex_ge(dim);
5757 lt = isl_map_apply_range(isl_map_copy(res), lt);
5758 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5759 le = isl_map_apply_range(isl_map_copy(res_i), le);
5760 le = isl_map_intersect(le, isl_map_copy(res));
5762 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5763 res = isl_map_intersect_domain(res,
5764 isl_set_copy(todo_i));
5765 res_i = isl_map_intersect_domain(res_i,
5766 isl_set_copy(todo));
5769 res = isl_map_union_disjoint(res, res_i);
5770 res = isl_map_union_disjoint(res, lt);
5771 res = isl_map_union_disjoint(res, le);
5773 todo = isl_set_intersect(todo, todo_i);
5776 isl_set_free(dom);
5777 isl_map_free(map);
5779 if (empty)
5780 *empty = todo;
5781 else
5782 isl_set_free(todo);
5784 return res;
5785 error:
5786 if (empty)
5787 *empty = NULL;
5788 isl_set_free(dom);
5789 isl_map_free(map);
5790 return NULL;
5793 /* Given a map "map", compute the lexicographically minimal
5794 * (or maximal) image element for each domain element in dom.
5795 * Set *empty to those elements in dom that do not have an image element.
5797 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5799 static __isl_give isl_map *isl_map_partial_lexopt(
5800 __isl_take isl_map *map, __isl_take isl_set *dom,
5801 __isl_give isl_set **empty, int max)
5803 if (!map || !dom)
5804 goto error;
5805 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5806 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5807 if (!isl_space_has_named_params(map->dim) ||
5808 !isl_space_has_named_params(dom->dim))
5809 isl_die(map->ctx, isl_error_invalid,
5810 "unaligned unnamed parameters", goto error);
5811 map = isl_map_align_params(map, isl_map_get_space(dom));
5812 dom = isl_map_align_params(dom, isl_map_get_space(map));
5813 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5814 error:
5815 if (empty)
5816 *empty = NULL;
5817 isl_set_free(dom);
5818 isl_map_free(map);
5819 return NULL;
5822 __isl_give isl_map *isl_map_partial_lexmax(
5823 __isl_take isl_map *map, __isl_take isl_set *dom,
5824 __isl_give isl_set **empty)
5826 return isl_map_partial_lexopt(map, dom, empty, 1);
5829 __isl_give isl_map *isl_map_partial_lexmin(
5830 __isl_take isl_map *map, __isl_take isl_set *dom,
5831 __isl_give isl_set **empty)
5833 return isl_map_partial_lexopt(map, dom, empty, 0);
5836 __isl_give isl_set *isl_set_partial_lexmin(
5837 __isl_take isl_set *set, __isl_take isl_set *dom,
5838 __isl_give isl_set **empty)
5840 return (struct isl_set *)
5841 isl_map_partial_lexmin((struct isl_map *)set,
5842 dom, empty);
5845 __isl_give isl_set *isl_set_partial_lexmax(
5846 __isl_take isl_set *set, __isl_take isl_set *dom,
5847 __isl_give isl_set **empty)
5849 return (struct isl_set *)
5850 isl_map_partial_lexmax((struct isl_map *)set,
5851 dom, empty);
5854 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5856 struct isl_basic_set *dom = NULL;
5857 isl_space *dom_dim;
5859 if (!bmap)
5860 goto error;
5861 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5862 dom = isl_basic_set_universe(dom_dim);
5863 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5864 error:
5865 isl_basic_map_free(bmap);
5866 return NULL;
5869 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5871 return isl_basic_map_lexopt(bmap, 0);
5874 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5876 return isl_basic_map_lexopt(bmap, 1);
5879 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5881 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5884 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5886 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5889 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5891 struct isl_set *dom = NULL;
5892 isl_space *dom_dim;
5894 if (!map)
5895 goto error;
5896 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5897 dom = isl_set_universe(dom_dim);
5898 return isl_map_partial_lexopt(map, dom, NULL, max);
5899 error:
5900 isl_map_free(map);
5901 return NULL;
5904 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5906 return isl_map_lexopt(map, 0);
5909 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5911 return isl_map_lexopt(map, 1);
5914 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5916 return (isl_set *)isl_map_lexmin((isl_map *)set);
5919 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5921 return (isl_set *)isl_map_lexmax((isl_map *)set);
5924 /* Extract the first and only affine expression from list
5925 * and then add it to *pwaff with the given dom.
5926 * This domain is known to be disjoint from other domains
5927 * because of the way isl_basic_map_foreach_lexmax works.
5929 static int update_dim_opt(__isl_take isl_basic_set *dom,
5930 __isl_take isl_aff_list *list, void *user)
5932 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5933 isl_aff *aff;
5934 isl_pw_aff **pwaff = user;
5935 isl_pw_aff *pwaff_i;
5937 if (isl_aff_list_n_aff(list) != 1)
5938 isl_die(ctx, isl_error_internal,
5939 "expecting single element list", goto error);
5941 aff = isl_aff_list_get_aff(list, 0);
5942 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5944 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5946 isl_aff_list_free(list);
5948 return 0;
5949 error:
5950 isl_basic_set_free(dom);
5951 isl_aff_list_free(list);
5952 return -1;
5955 /* Given a basic map with one output dimension, compute the minimum or
5956 * maximum of that dimension as an isl_pw_aff.
5958 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5959 * call update_dim_opt on each leaf of the result.
5961 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
5962 int max)
5964 isl_space *dim = isl_basic_map_get_space(bmap);
5965 isl_pw_aff *pwaff;
5966 int r;
5968 dim = isl_space_from_domain(isl_space_domain(dim));
5969 dim = isl_space_add_dims(dim, isl_dim_out, 1);
5970 pwaff = isl_pw_aff_empty(dim);
5972 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
5973 if (r < 0)
5974 return isl_pw_aff_free(pwaff);
5976 return pwaff;
5979 /* Compute the minimum or maximum of the given output dimension
5980 * as a function of the parameters and the input dimensions,
5981 * but independently of the other output dimensions.
5983 * We first project out the other output dimension and then compute
5984 * the "lexicographic" maximum in each basic map, combining the results
5985 * using isl_pw_aff_union_max.
5987 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
5988 int max)
5990 int i;
5991 isl_pw_aff *pwaff;
5992 unsigned n_out;
5994 n_out = isl_map_dim(map, isl_dim_out);
5995 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
5996 map = isl_map_project_out(map, isl_dim_out, 0, pos);
5997 if (!map)
5998 return NULL;
6000 if (map->n == 0) {
6001 isl_space *dim = isl_map_get_space(map);
6002 dim = isl_space_domain(isl_space_from_range(dim));
6003 isl_map_free(map);
6004 return isl_pw_aff_empty(dim);
6007 pwaff = basic_map_dim_opt(map->p[0], max);
6008 for (i = 1; i < map->n; ++i) {
6009 isl_pw_aff *pwaff_i;
6011 pwaff_i = basic_map_dim_opt(map->p[i], max);
6012 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6015 isl_map_free(map);
6017 return pwaff;
6020 /* Compute the maximum of the given output dimension as a function of the
6021 * parameters and input dimensions, but independently of
6022 * the other output dimensions.
6024 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6026 return map_dim_opt(map, pos, 1);
6029 /* Compute the minimum or maximum of the given set dimension
6030 * as a function of the parameters,
6031 * but independently of the other set dimensions.
6033 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6034 int max)
6036 return map_dim_opt(set, pos, max);
6039 /* Compute the maximum of the given set dimension as a function of the
6040 * parameters, but independently of the other set dimensions.
6042 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6044 return set_dim_opt(set, pos, 1);
6047 /* Compute the minimum of the given set dimension as a function of the
6048 * parameters, but independently of the other set dimensions.
6050 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6052 return set_dim_opt(set, pos, 0);
6055 /* Apply a preimage specified by "mat" on the parameters of "bset".
6056 * bset is assumed to have only parameters and divs.
6058 static struct isl_basic_set *basic_set_parameter_preimage(
6059 struct isl_basic_set *bset, struct isl_mat *mat)
6061 unsigned nparam;
6063 if (!bset || !mat)
6064 goto error;
6066 bset->dim = isl_space_cow(bset->dim);
6067 if (!bset->dim)
6068 goto error;
6070 nparam = isl_basic_set_dim(bset, isl_dim_param);
6072 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6074 bset->dim->nparam = 0;
6075 bset->dim->n_out = nparam;
6076 bset = isl_basic_set_preimage(bset, mat);
6077 if (bset) {
6078 bset->dim->nparam = bset->dim->n_out;
6079 bset->dim->n_out = 0;
6081 return bset;
6082 error:
6083 isl_mat_free(mat);
6084 isl_basic_set_free(bset);
6085 return NULL;
6088 /* Apply a preimage specified by "mat" on the parameters of "set".
6089 * set is assumed to have only parameters and divs.
6091 static struct isl_set *set_parameter_preimage(
6092 struct isl_set *set, struct isl_mat *mat)
6094 isl_space *dim = NULL;
6095 unsigned nparam;
6097 if (!set || !mat)
6098 goto error;
6100 dim = isl_space_copy(set->dim);
6101 dim = isl_space_cow(dim);
6102 if (!dim)
6103 goto error;
6105 nparam = isl_set_dim(set, isl_dim_param);
6107 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6109 dim->nparam = 0;
6110 dim->n_out = nparam;
6111 isl_set_reset_space(set, dim);
6112 set = isl_set_preimage(set, mat);
6113 if (!set)
6114 goto error2;
6115 dim = isl_space_copy(set->dim);
6116 dim = isl_space_cow(dim);
6117 if (!dim)
6118 goto error2;
6119 dim->nparam = dim->n_out;
6120 dim->n_out = 0;
6121 isl_set_reset_space(set, dim);
6122 return set;
6123 error:
6124 isl_space_free(dim);
6125 isl_mat_free(mat);
6126 error2:
6127 isl_set_free(set);
6128 return NULL;
6131 /* Intersect the basic set "bset" with the affine space specified by the
6132 * equalities in "eq".
6134 static struct isl_basic_set *basic_set_append_equalities(
6135 struct isl_basic_set *bset, struct isl_mat *eq)
6137 int i, k;
6138 unsigned len;
6140 if (!bset || !eq)
6141 goto error;
6143 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6144 eq->n_row, 0);
6145 if (!bset)
6146 goto error;
6148 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6149 for (i = 0; i < eq->n_row; ++i) {
6150 k = isl_basic_set_alloc_equality(bset);
6151 if (k < 0)
6152 goto error;
6153 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6154 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6156 isl_mat_free(eq);
6158 bset = isl_basic_set_gauss(bset, NULL);
6159 bset = isl_basic_set_finalize(bset);
6161 return bset;
6162 error:
6163 isl_mat_free(eq);
6164 isl_basic_set_free(bset);
6165 return NULL;
6168 /* Intersect the set "set" with the affine space specified by the
6169 * equalities in "eq".
6171 static struct isl_set *set_append_equalities(struct isl_set *set,
6172 struct isl_mat *eq)
6174 int i;
6176 if (!set || !eq)
6177 goto error;
6179 for (i = 0; i < set->n; ++i) {
6180 set->p[i] = basic_set_append_equalities(set->p[i],
6181 isl_mat_copy(eq));
6182 if (!set->p[i])
6183 goto error;
6185 isl_mat_free(eq);
6186 return set;
6187 error:
6188 isl_mat_free(eq);
6189 isl_set_free(set);
6190 return NULL;
6193 /* Project the given basic set onto its parameter domain, possibly introducing
6194 * new, explicit, existential variables in the constraints.
6195 * The input has parameters and (possibly implicit) existential variables.
6196 * The output has the same parameters, but only
6197 * explicit existentially quantified variables.
6199 * The actual projection is performed by pip, but pip doesn't seem
6200 * to like equalities very much, so we first remove the equalities
6201 * among the parameters by performing a variable compression on
6202 * the parameters. Afterward, an inverse transformation is performed
6203 * and the equalities among the parameters are inserted back in.
6205 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6207 int i, j;
6208 struct isl_mat *eq;
6209 struct isl_mat *T, *T2;
6210 struct isl_set *set;
6211 unsigned nparam, n_div;
6213 bset = isl_basic_set_cow(bset);
6214 if (!bset)
6215 return NULL;
6217 if (bset->n_eq == 0)
6218 return isl_basic_set_lexmin(bset);
6220 isl_basic_set_gauss(bset, NULL);
6222 nparam = isl_basic_set_dim(bset, isl_dim_param);
6223 n_div = isl_basic_set_dim(bset, isl_dim_div);
6225 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6226 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6227 ++i;
6229 if (i == bset->n_eq)
6230 return isl_basic_set_lexmin(bset);
6232 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6233 0, 1 + nparam);
6234 eq = isl_mat_cow(eq);
6235 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6236 if (T && T->n_col == 0) {
6237 isl_mat_free(T);
6238 isl_mat_free(T2);
6239 isl_mat_free(eq);
6240 bset = isl_basic_set_set_to_empty(bset);
6241 return isl_set_from_basic_set(bset);
6243 bset = basic_set_parameter_preimage(bset, T);
6245 set = isl_basic_set_lexmin(bset);
6246 set = set_parameter_preimage(set, T2);
6247 set = set_append_equalities(set, eq);
6248 return set;
6251 /* Compute an explicit representation for all the existentially
6252 * quantified variables.
6253 * The input and output dimensions are first turned into parameters.
6254 * compute_divs then returns a map with the same parameters and
6255 * no input or output dimensions and the dimension specification
6256 * is reset to that of the input.
6258 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6260 struct isl_basic_set *bset;
6261 struct isl_set *set;
6262 struct isl_map *map;
6263 isl_space *dim, *orig_dim = NULL;
6264 unsigned nparam;
6265 unsigned n_in;
6266 unsigned n_out;
6268 bmap = isl_basic_map_cow(bmap);
6269 if (!bmap)
6270 return NULL;
6272 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6273 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6274 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6275 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
6276 if (!dim)
6277 goto error;
6279 orig_dim = bmap->dim;
6280 bmap->dim = dim;
6281 bset = (struct isl_basic_set *)bmap;
6283 set = parameter_compute_divs(bset);
6284 map = (struct isl_map *)set;
6285 map = isl_map_reset_space(map, orig_dim);
6287 return map;
6288 error:
6289 isl_basic_map_free(bmap);
6290 return NULL;
6293 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6295 int i;
6296 unsigned off;
6298 if (!bmap)
6299 return -1;
6301 off = isl_space_dim(bmap->dim, isl_dim_all);
6302 for (i = 0; i < bmap->n_div; ++i) {
6303 if (isl_int_is_zero(bmap->div[i][0]))
6304 return 0;
6305 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6306 return -1);
6308 return 1;
6311 static int map_divs_known(__isl_keep isl_map *map)
6313 int i;
6315 if (!map)
6316 return -1;
6318 for (i = 0; i < map->n; ++i) {
6319 int known = isl_basic_map_divs_known(map->p[i]);
6320 if (known <= 0)
6321 return known;
6324 return 1;
6327 /* If bmap contains any unknown divs, then compute explicit
6328 * expressions for them. However, this computation may be
6329 * quite expensive, so first try to remove divs that aren't
6330 * strictly needed.
6332 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6334 int known;
6335 struct isl_map *map;
6337 known = isl_basic_map_divs_known(bmap);
6338 if (known < 0)
6339 goto error;
6340 if (known)
6341 return isl_map_from_basic_map(bmap);
6343 bmap = isl_basic_map_drop_redundant_divs(bmap);
6345 known = isl_basic_map_divs_known(bmap);
6346 if (known < 0)
6347 goto error;
6348 if (known)
6349 return isl_map_from_basic_map(bmap);
6351 map = compute_divs(bmap);
6352 return map;
6353 error:
6354 isl_basic_map_free(bmap);
6355 return NULL;
6358 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6360 int i;
6361 int known;
6362 struct isl_map *res;
6364 if (!map)
6365 return NULL;
6366 if (map->n == 0)
6367 return map;
6369 known = map_divs_known(map);
6370 if (known < 0) {
6371 isl_map_free(map);
6372 return NULL;
6374 if (known)
6375 return map;
6377 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6378 for (i = 1 ; i < map->n; ++i) {
6379 struct isl_map *r2;
6380 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6381 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6382 res = isl_map_union_disjoint(res, r2);
6383 else
6384 res = isl_map_union(res, r2);
6386 isl_map_free(map);
6388 return res;
6391 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6393 return (struct isl_set *)
6394 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6397 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6399 return (struct isl_set *)
6400 isl_map_compute_divs((struct isl_map *)set);
6403 struct isl_set *isl_map_domain(struct isl_map *map)
6405 int i;
6406 struct isl_set *set;
6408 if (!map)
6409 goto error;
6411 map = isl_map_cow(map);
6412 if (!map)
6413 return NULL;
6415 set = (struct isl_set *)map;
6416 set->dim = isl_space_domain(set->dim);
6417 if (!set->dim)
6418 goto error;
6419 for (i = 0; i < map->n; ++i) {
6420 set->p[i] = isl_basic_map_domain(map->p[i]);
6421 if (!set->p[i])
6422 goto error;
6424 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6425 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6426 return set;
6427 error:
6428 isl_map_free(map);
6429 return NULL;
6432 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6433 __isl_take isl_map *map2)
6435 int i;
6436 unsigned flags = 0;
6437 struct isl_map *map = NULL;
6439 if (!map1 || !map2)
6440 goto error;
6442 if (map1->n == 0) {
6443 isl_map_free(map1);
6444 return map2;
6446 if (map2->n == 0) {
6447 isl_map_free(map2);
6448 return map1;
6451 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6453 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6454 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6455 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6457 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6458 map1->n + map2->n, flags);
6459 if (!map)
6460 goto error;
6461 for (i = 0; i < map1->n; ++i) {
6462 map = isl_map_add_basic_map(map,
6463 isl_basic_map_copy(map1->p[i]));
6464 if (!map)
6465 goto error;
6467 for (i = 0; i < map2->n; ++i) {
6468 map = isl_map_add_basic_map(map,
6469 isl_basic_map_copy(map2->p[i]));
6470 if (!map)
6471 goto error;
6473 isl_map_free(map1);
6474 isl_map_free(map2);
6475 return map;
6476 error:
6477 isl_map_free(map);
6478 isl_map_free(map1);
6479 isl_map_free(map2);
6480 return NULL;
6483 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6484 __isl_take isl_map *map2)
6486 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6489 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6491 map1 = isl_map_union_disjoint(map1, map2);
6492 if (!map1)
6493 return NULL;
6494 if (map1->n > 1)
6495 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6496 return map1;
6499 struct isl_set *isl_set_union_disjoint(
6500 struct isl_set *set1, struct isl_set *set2)
6502 return (struct isl_set *)
6503 isl_map_union_disjoint(
6504 (struct isl_map *)set1, (struct isl_map *)set2);
6507 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6509 return (struct isl_set *)
6510 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6513 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6514 __isl_take isl_set *set)
6516 unsigned flags = 0;
6517 struct isl_map *result;
6518 int i, j;
6520 if (!map || !set)
6521 goto error;
6523 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
6524 isl_die(set->ctx, isl_error_invalid,
6525 "parameters don't match", goto error);
6527 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
6528 !isl_map_compatible_range(map, set))
6529 isl_die(set->ctx, isl_error_invalid,
6530 "incompatible spaces", goto error);
6532 if (isl_set_plain_is_universe(set)) {
6533 isl_set_free(set);
6534 return map;
6537 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6538 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6539 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6541 result = isl_map_alloc_space(isl_space_copy(map->dim),
6542 map->n * set->n, flags);
6543 if (!result)
6544 goto error;
6545 for (i = 0; i < map->n; ++i)
6546 for (j = 0; j < set->n; ++j) {
6547 result = isl_map_add_basic_map(result,
6548 isl_basic_map_intersect_range(
6549 isl_basic_map_copy(map->p[i]),
6550 isl_basic_set_copy(set->p[j])));
6551 if (!result)
6552 goto error;
6554 isl_map_free(map);
6555 isl_set_free(set);
6556 return result;
6557 error:
6558 isl_map_free(map);
6559 isl_set_free(set);
6560 return NULL;
6563 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6564 __isl_take isl_set *set)
6566 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6569 struct isl_map *isl_map_intersect_domain(
6570 struct isl_map *map, struct isl_set *set)
6572 return isl_map_reverse(
6573 isl_map_intersect_range(isl_map_reverse(map), set));
6576 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6577 __isl_take isl_map *map2)
6579 if (!map1 || !map2)
6580 goto error;
6581 map1 = isl_map_reverse(map1);
6582 map1 = isl_map_apply_range(map1, map2);
6583 return isl_map_reverse(map1);
6584 error:
6585 isl_map_free(map1);
6586 isl_map_free(map2);
6587 return NULL;
6590 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6591 __isl_take isl_map *map2)
6593 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6596 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6597 __isl_take isl_map *map2)
6599 isl_space *dim_result;
6600 struct isl_map *result;
6601 int i, j;
6603 if (!map1 || !map2)
6604 goto error;
6606 dim_result = isl_space_join(isl_space_copy(map1->dim),
6607 isl_space_copy(map2->dim));
6609 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6610 if (!result)
6611 goto error;
6612 for (i = 0; i < map1->n; ++i)
6613 for (j = 0; j < map2->n; ++j) {
6614 result = isl_map_add_basic_map(result,
6615 isl_basic_map_apply_range(
6616 isl_basic_map_copy(map1->p[i]),
6617 isl_basic_map_copy(map2->p[j])));
6618 if (!result)
6619 goto error;
6621 isl_map_free(map1);
6622 isl_map_free(map2);
6623 if (result && result->n <= 1)
6624 ISL_F_SET(result, ISL_MAP_DISJOINT);
6625 return result;
6626 error:
6627 isl_map_free(map1);
6628 isl_map_free(map2);
6629 return NULL;
6632 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6633 __isl_take isl_map *map2)
6635 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6639 * returns range - domain
6641 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6643 isl_space *dims, *target_dim;
6644 struct isl_basic_set *bset;
6645 unsigned dim;
6646 unsigned nparam;
6647 int i;
6649 if (!bmap)
6650 goto error;
6651 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6652 bmap->dim, isl_dim_out),
6653 goto error);
6654 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6655 dim = isl_basic_map_n_in(bmap);
6656 nparam = isl_basic_map_n_param(bmap);
6657 bset = isl_basic_set_from_basic_map(bmap);
6658 bset = isl_basic_set_cow(bset);
6659 dims = isl_basic_set_get_space(bset);
6660 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6661 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6662 bset = isl_basic_set_swap_vars(bset, 2*dim);
6663 for (i = 0; i < dim; ++i) {
6664 int j = isl_basic_map_alloc_equality(
6665 (struct isl_basic_map *)bset);
6666 if (j < 0)
6667 goto error;
6668 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6669 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6670 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6671 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6673 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6674 bset = isl_basic_set_reset_space(bset, target_dim);
6675 return bset;
6676 error:
6677 isl_basic_map_free(bmap);
6678 return NULL;
6682 * returns range - domain
6684 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
6686 int i;
6687 isl_space *dim;
6688 struct isl_set *result;
6690 if (!map)
6691 return NULL;
6693 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6694 map->dim, isl_dim_out),
6695 goto error);
6696 dim = isl_map_get_space(map);
6697 dim = isl_space_domain(dim);
6698 result = isl_set_alloc_space(dim, map->n, 0);
6699 if (!result)
6700 goto error;
6701 for (i = 0; i < map->n; ++i)
6702 result = isl_set_add_basic_set(result,
6703 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6704 isl_map_free(map);
6705 return result;
6706 error:
6707 isl_map_free(map);
6708 return NULL;
6712 * returns [domain -> range] -> range - domain
6714 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6715 __isl_take isl_basic_map *bmap)
6717 int i, k;
6718 isl_space *dim;
6719 isl_basic_map *domain;
6720 int nparam, n;
6721 unsigned total;
6723 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6724 isl_die(bmap->ctx, isl_error_invalid,
6725 "domain and range don't match", goto error);
6727 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6728 n = isl_basic_map_dim(bmap, isl_dim_in);
6730 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6731 domain = isl_basic_map_universe(dim);
6733 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6734 bmap = isl_basic_map_apply_range(bmap, domain);
6735 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6737 total = isl_basic_map_total_dim(bmap);
6739 for (i = 0; i < n; ++i) {
6740 k = isl_basic_map_alloc_equality(bmap);
6741 if (k < 0)
6742 goto error;
6743 isl_seq_clr(bmap->eq[k], 1 + total);
6744 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6745 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6746 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6749 bmap = isl_basic_map_gauss(bmap, NULL);
6750 return isl_basic_map_finalize(bmap);
6751 error:
6752 isl_basic_map_free(bmap);
6753 return NULL;
6757 * returns [domain -> range] -> range - domain
6759 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6761 int i;
6762 isl_space *domain_dim;
6764 if (!map)
6765 return NULL;
6767 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6768 isl_die(map->ctx, isl_error_invalid,
6769 "domain and range don't match", goto error);
6771 map = isl_map_cow(map);
6772 if (!map)
6773 return NULL;
6775 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6776 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6777 map->dim = isl_space_join(map->dim, domain_dim);
6778 if (!map->dim)
6779 goto error;
6780 for (i = 0; i < map->n; ++i) {
6781 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6782 if (!map->p[i])
6783 goto error;
6785 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6786 return map;
6787 error:
6788 isl_map_free(map);
6789 return NULL;
6792 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6794 struct isl_basic_map *bmap;
6795 unsigned nparam;
6796 unsigned dim;
6797 int i;
6799 if (!dims)
6800 return NULL;
6802 nparam = dims->nparam;
6803 dim = dims->n_out;
6804 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6805 if (!bmap)
6806 goto error;
6808 for (i = 0; i < dim; ++i) {
6809 int j = isl_basic_map_alloc_equality(bmap);
6810 if (j < 0)
6811 goto error;
6812 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6813 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6814 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6816 return isl_basic_map_finalize(bmap);
6817 error:
6818 isl_basic_map_free(bmap);
6819 return NULL;
6822 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6824 if (!dim)
6825 return NULL;
6826 if (dim->n_in != dim->n_out)
6827 isl_die(dim->ctx, isl_error_invalid,
6828 "number of input and output dimensions needs to be "
6829 "the same", goto error);
6830 return basic_map_identity(dim);
6831 error:
6832 isl_space_free(dim);
6833 return NULL;
6836 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6838 if (!model || !model->dim)
6839 return NULL;
6840 return isl_basic_map_identity(isl_space_copy(model->dim));
6843 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6845 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6848 struct isl_map *isl_map_identity_like(struct isl_map *model)
6850 if (!model || !model->dim)
6851 return NULL;
6852 return isl_map_identity(isl_space_copy(model->dim));
6855 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6857 if (!model || !model->dim)
6858 return NULL;
6859 return isl_map_identity(isl_space_copy(model->dim));
6862 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6864 isl_space *dim = isl_set_get_space(set);
6865 isl_map *id;
6866 id = isl_map_identity(isl_space_map_from_set(dim));
6867 return isl_map_intersect_range(id, set);
6870 /* Construct a basic set with all set dimensions having only non-negative
6871 * values.
6873 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6874 __isl_take isl_space *space)
6876 int i;
6877 unsigned nparam;
6878 unsigned dim;
6879 struct isl_basic_set *bset;
6881 if (!space)
6882 return NULL;
6883 nparam = space->nparam;
6884 dim = space->n_out;
6885 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6886 if (!bset)
6887 return NULL;
6888 for (i = 0; i < dim; ++i) {
6889 int k = isl_basic_set_alloc_inequality(bset);
6890 if (k < 0)
6891 goto error;
6892 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6893 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6895 return bset;
6896 error:
6897 isl_basic_set_free(bset);
6898 return NULL;
6901 /* Construct the half-space x_pos >= 0.
6903 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6904 int pos)
6906 int k;
6907 isl_basic_set *nonneg;
6909 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6910 k = isl_basic_set_alloc_inequality(nonneg);
6911 if (k < 0)
6912 goto error;
6913 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6914 isl_int_set_si(nonneg->ineq[k][pos], 1);
6916 return isl_basic_set_finalize(nonneg);
6917 error:
6918 isl_basic_set_free(nonneg);
6919 return NULL;
6922 /* Construct the half-space x_pos <= -1.
6924 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6926 int k;
6927 isl_basic_set *neg;
6929 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6930 k = isl_basic_set_alloc_inequality(neg);
6931 if (k < 0)
6932 goto error;
6933 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6934 isl_int_set_si(neg->ineq[k][0], -1);
6935 isl_int_set_si(neg->ineq[k][pos], -1);
6937 return isl_basic_set_finalize(neg);
6938 error:
6939 isl_basic_set_free(neg);
6940 return NULL;
6943 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6944 enum isl_dim_type type, unsigned first, unsigned n)
6946 int i;
6947 isl_basic_set *nonneg;
6948 isl_basic_set *neg;
6950 if (!set)
6951 return NULL;
6952 if (n == 0)
6953 return set;
6955 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6957 for (i = 0; i < n; ++i) {
6958 nonneg = nonneg_halfspace(isl_set_get_space(set),
6959 pos(set->dim, type) + first + i);
6960 neg = neg_halfspace(isl_set_get_space(set),
6961 pos(set->dim, type) + first + i);
6963 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6966 return set;
6967 error:
6968 isl_set_free(set);
6969 return NULL;
6972 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6973 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6974 void *user)
6976 isl_set *half;
6978 if (!set)
6979 return -1;
6980 if (isl_set_plain_is_empty(set)) {
6981 isl_set_free(set);
6982 return 0;
6984 if (first == len)
6985 return fn(set, signs, user);
6987 signs[first] = 1;
6988 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6989 1 + first));
6990 half = isl_set_intersect(half, isl_set_copy(set));
6991 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6992 goto error;
6994 signs[first] = -1;
6995 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6996 1 + first));
6997 half = isl_set_intersect(half, set);
6998 return foreach_orthant(half, signs, first + 1, len, fn, user);
6999 error:
7000 isl_set_free(set);
7001 return -1;
7004 /* Call "fn" on the intersections of "set" with each of the orthants
7005 * (except for obviously empty intersections). The orthant is identified
7006 * by the signs array, with each entry having value 1 or -1 according
7007 * to the sign of the corresponding variable.
7009 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7010 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7011 void *user)
7013 unsigned nparam;
7014 unsigned nvar;
7015 int *signs;
7016 int r;
7018 if (!set)
7019 return -1;
7020 if (isl_set_plain_is_empty(set))
7021 return 0;
7023 nparam = isl_set_dim(set, isl_dim_param);
7024 nvar = isl_set_dim(set, isl_dim_set);
7026 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7028 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7029 fn, user);
7031 free(signs);
7033 return r;
7036 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7038 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7041 int isl_basic_map_is_subset(
7042 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7044 int is_subset;
7045 struct isl_map *map1;
7046 struct isl_map *map2;
7048 if (!bmap1 || !bmap2)
7049 return -1;
7051 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7052 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7054 is_subset = isl_map_is_subset(map1, map2);
7056 isl_map_free(map1);
7057 isl_map_free(map2);
7059 return is_subset;
7062 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7063 __isl_keep isl_basic_set *bset2)
7065 return isl_basic_map_is_subset(bset1, bset2);
7068 int isl_basic_map_is_equal(
7069 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7071 int is_subset;
7073 if (!bmap1 || !bmap2)
7074 return -1;
7075 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7076 if (is_subset != 1)
7077 return is_subset;
7078 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7079 return is_subset;
7082 int isl_basic_set_is_equal(
7083 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7085 return isl_basic_map_is_equal(
7086 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7089 int isl_map_is_empty(struct isl_map *map)
7091 int i;
7092 int is_empty;
7094 if (!map)
7095 return -1;
7096 for (i = 0; i < map->n; ++i) {
7097 is_empty = isl_basic_map_is_empty(map->p[i]);
7098 if (is_empty < 0)
7099 return -1;
7100 if (!is_empty)
7101 return 0;
7103 return 1;
7106 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7108 return map ? map->n == 0 : -1;
7111 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7113 return isl_map_plain_is_empty(map);
7116 int isl_set_plain_is_empty(struct isl_set *set)
7118 return set ? set->n == 0 : -1;
7121 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7123 return isl_set_plain_is_empty(set);
7126 int isl_set_is_empty(struct isl_set *set)
7128 return isl_map_is_empty((struct isl_map *)set);
7131 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7133 if (!map1 || !map2)
7134 return -1;
7136 return isl_space_is_equal(map1->dim, map2->dim);
7139 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7141 if (!set1 || !set2)
7142 return -1;
7144 return isl_space_is_equal(set1->dim, set2->dim);
7147 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7149 int is_subset;
7151 if (!map1 || !map2)
7152 return -1;
7153 is_subset = isl_map_is_subset(map1, map2);
7154 if (is_subset != 1)
7155 return is_subset;
7156 is_subset = isl_map_is_subset(map2, map1);
7157 return is_subset;
7160 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7162 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7165 int isl_basic_map_is_strict_subset(
7166 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7168 int is_subset;
7170 if (!bmap1 || !bmap2)
7171 return -1;
7172 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7173 if (is_subset != 1)
7174 return is_subset;
7175 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7176 if (is_subset == -1)
7177 return is_subset;
7178 return !is_subset;
7181 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7183 int is_subset;
7185 if (!map1 || !map2)
7186 return -1;
7187 is_subset = isl_map_is_subset(map1, map2);
7188 if (is_subset != 1)
7189 return is_subset;
7190 is_subset = isl_map_is_subset(map2, map1);
7191 if (is_subset == -1)
7192 return is_subset;
7193 return !is_subset;
7196 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7198 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7201 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7203 if (!bmap)
7204 return -1;
7205 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7208 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7210 if (!bset)
7211 return -1;
7212 return bset->n_eq == 0 && bset->n_ineq == 0;
7215 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7217 int i;
7219 if (!map)
7220 return -1;
7222 for (i = 0; i < map->n; ++i) {
7223 int r = isl_basic_map_is_universe(map->p[i]);
7224 if (r < 0 || r)
7225 return r;
7228 return 0;
7231 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7233 return isl_map_plain_is_universe((isl_map *) set);
7236 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7238 return isl_set_plain_is_universe(set);
7241 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7243 struct isl_basic_set *bset = NULL;
7244 struct isl_vec *sample = NULL;
7245 int empty;
7246 unsigned total;
7248 if (!bmap)
7249 return -1;
7251 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7252 return 1;
7254 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7255 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7256 copy = isl_basic_map_remove_redundancies(copy);
7257 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
7258 isl_basic_map_free(copy);
7259 return empty;
7262 total = 1 + isl_basic_map_total_dim(bmap);
7263 if (bmap->sample && bmap->sample->size == total) {
7264 int contains = isl_basic_map_contains(bmap, bmap->sample);
7265 if (contains < 0)
7266 return -1;
7267 if (contains)
7268 return 0;
7270 isl_vec_free(bmap->sample);
7271 bmap->sample = NULL;
7272 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7273 if (!bset)
7274 return -1;
7275 sample = isl_basic_set_sample_vec(bset);
7276 if (!sample)
7277 return -1;
7278 empty = sample->size == 0;
7279 isl_vec_free(bmap->sample);
7280 bmap->sample = sample;
7281 if (empty)
7282 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7284 return empty;
7287 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7289 if (!bmap)
7290 return -1;
7291 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7294 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7296 return isl_basic_map_plain_is_empty(bmap);
7299 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7301 if (!bset)
7302 return -1;
7303 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7306 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7308 return isl_basic_set_plain_is_empty(bset);
7311 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7313 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7316 struct isl_map *isl_basic_map_union(
7317 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7319 struct isl_map *map;
7320 if (!bmap1 || !bmap2)
7321 goto error;
7323 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7325 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7326 if (!map)
7327 goto error;
7328 map = isl_map_add_basic_map(map, bmap1);
7329 map = isl_map_add_basic_map(map, bmap2);
7330 return map;
7331 error:
7332 isl_basic_map_free(bmap1);
7333 isl_basic_map_free(bmap2);
7334 return NULL;
7337 struct isl_set *isl_basic_set_union(
7338 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7340 return (struct isl_set *)isl_basic_map_union(
7341 (struct isl_basic_map *)bset1,
7342 (struct isl_basic_map *)bset2);
7345 /* Order divs such that any div only depends on previous divs */
7346 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7348 int i;
7349 unsigned off;
7351 if (!bmap)
7352 return NULL;
7354 off = isl_space_dim(bmap->dim, isl_dim_all);
7356 for (i = 0; i < bmap->n_div; ++i) {
7357 int pos;
7358 if (isl_int_is_zero(bmap->div[i][0]))
7359 continue;
7360 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7361 bmap->n_div-i);
7362 if (pos == -1)
7363 continue;
7364 isl_basic_map_swap_div(bmap, i, i + pos);
7365 --i;
7367 return bmap;
7370 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7372 return (struct isl_basic_set *)
7373 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7376 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7378 int i;
7380 if (!map)
7381 return 0;
7383 for (i = 0; i < map->n; ++i) {
7384 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7385 if (!map->p[i])
7386 goto error;
7389 return map;
7390 error:
7391 isl_map_free(map);
7392 return NULL;
7395 /* Apply the expansion computed by isl_merge_divs.
7396 * The expansion itself is given by "exp" while the resulting
7397 * list of divs is given by "div".
7399 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7400 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7402 int i, j;
7403 int n_div;
7405 bset = isl_basic_set_cow(bset);
7406 if (!bset || !div)
7407 goto error;
7409 if (div->n_row < bset->n_div)
7410 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7411 "not an expansion", goto error);
7413 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
7414 div->n_row - bset->n_div, 0,
7415 2 * (div->n_row - bset->n_div));
7417 n_div = bset->n_div;
7418 for (i = n_div; i < div->n_row; ++i)
7419 if (isl_basic_set_alloc_div(bset) < 0)
7420 goto error;
7422 j = n_div - 1;
7423 for (i = div->n_row - 1; i >= 0; --i) {
7424 if (j >= 0 && exp[j] == i) {
7425 if (i != j)
7426 isl_basic_map_swap_div(bset, i, j);
7427 j--;
7428 } else {
7429 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
7430 if (isl_basic_map_add_div_constraints(bset, i) < 0)
7431 goto error;
7435 isl_mat_free(div);
7436 return bset;
7437 error:
7438 isl_basic_set_free(bset);
7439 isl_mat_free(div);
7440 return NULL;
7443 /* Look for a div in dst that corresponds to the div "div" in src.
7444 * The divs before "div" in src and dst are assumed to be the same.
7446 * Returns -1 if no corresponding div was found and the position
7447 * of the corresponding div in dst otherwise.
7449 static int find_div(struct isl_basic_map *dst,
7450 struct isl_basic_map *src, unsigned div)
7452 int i;
7454 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7456 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7457 for (i = div; i < dst->n_div; ++i)
7458 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7459 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7460 dst->n_div - div) == -1)
7461 return i;
7462 return -1;
7465 struct isl_basic_map *isl_basic_map_align_divs(
7466 struct isl_basic_map *dst, struct isl_basic_map *src)
7468 int i;
7469 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7471 if (!dst || !src)
7472 goto error;
7474 if (src->n_div == 0)
7475 return dst;
7477 for (i = 0; i < src->n_div; ++i)
7478 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7480 src = isl_basic_map_order_divs(src);
7481 dst = isl_basic_map_cow(dst);
7482 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7483 src->n_div, 0, 2 * src->n_div);
7484 if (!dst)
7485 return NULL;
7486 for (i = 0; i < src->n_div; ++i) {
7487 int j = find_div(dst, src, i);
7488 if (j < 0) {
7489 j = isl_basic_map_alloc_div(dst);
7490 if (j < 0)
7491 goto error;
7492 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7493 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7494 if (isl_basic_map_add_div_constraints(dst, j) < 0)
7495 goto error;
7497 if (j != i)
7498 isl_basic_map_swap_div(dst, i, j);
7500 return dst;
7501 error:
7502 isl_basic_map_free(dst);
7503 return NULL;
7506 struct isl_basic_set *isl_basic_set_align_divs(
7507 struct isl_basic_set *dst, struct isl_basic_set *src)
7509 return (struct isl_basic_set *)isl_basic_map_align_divs(
7510 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7513 struct isl_map *isl_map_align_divs(struct isl_map *map)
7515 int i;
7517 if (!map)
7518 return NULL;
7519 if (map->n == 0)
7520 return map;
7521 map = isl_map_compute_divs(map);
7522 map = isl_map_cow(map);
7523 if (!map)
7524 return NULL;
7526 for (i = 1; i < map->n; ++i)
7527 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7528 for (i = 1; i < map->n; ++i)
7529 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7531 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7532 return map;
7535 struct isl_set *isl_set_align_divs(struct isl_set *set)
7537 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7540 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7541 __isl_take isl_map *map)
7543 if (!set || !map)
7544 goto error;
7545 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7546 map = isl_map_intersect_domain(map, set);
7547 set = isl_map_range(map);
7548 return set;
7549 error:
7550 isl_set_free(set);
7551 isl_map_free(map);
7552 return NULL;
7555 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7556 __isl_take isl_map *map)
7558 return isl_map_align_params_map_map_and(set, map, &set_apply);
7561 /* There is no need to cow as removing empty parts doesn't change
7562 * the meaning of the set.
7564 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7566 int i;
7568 if (!map)
7569 return NULL;
7571 for (i = map->n - 1; i >= 0; --i)
7572 remove_if_empty(map, i);
7574 return map;
7577 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7579 return (struct isl_set *)
7580 isl_map_remove_empty_parts((struct isl_map *)set);
7583 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7585 struct isl_basic_map *bmap;
7586 if (!map || map->n == 0)
7587 return NULL;
7588 bmap = map->p[map->n-1];
7589 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7590 return isl_basic_map_copy(bmap);
7593 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7595 return (struct isl_basic_set *)
7596 isl_map_copy_basic_map((struct isl_map *)set);
7599 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7600 __isl_keep isl_basic_map *bmap)
7602 int i;
7604 if (!map || !bmap)
7605 goto error;
7606 for (i = map->n-1; i >= 0; --i) {
7607 if (map->p[i] != bmap)
7608 continue;
7609 map = isl_map_cow(map);
7610 if (!map)
7611 goto error;
7612 isl_basic_map_free(map->p[i]);
7613 if (i != map->n-1) {
7614 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7615 map->p[i] = map->p[map->n-1];
7617 map->n--;
7618 return map;
7620 return map;
7621 error:
7622 isl_map_free(map);
7623 return NULL;
7626 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7627 struct isl_basic_set *bset)
7629 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7630 (struct isl_basic_map *)bset);
7633 /* Given two basic sets bset1 and bset2, compute the maximal difference
7634 * between the values of dimension pos in bset1 and those in bset2
7635 * for any common value of the parameters and dimensions preceding pos.
7637 static enum isl_lp_result basic_set_maximal_difference_at(
7638 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7639 int pos, isl_int *opt)
7641 isl_space *dims;
7642 struct isl_basic_map *bmap1 = NULL;
7643 struct isl_basic_map *bmap2 = NULL;
7644 struct isl_ctx *ctx;
7645 struct isl_vec *obj;
7646 unsigned total;
7647 unsigned nparam;
7648 unsigned dim1, dim2;
7649 enum isl_lp_result res;
7651 if (!bset1 || !bset2)
7652 return isl_lp_error;
7654 nparam = isl_basic_set_n_param(bset1);
7655 dim1 = isl_basic_set_n_dim(bset1);
7656 dim2 = isl_basic_set_n_dim(bset2);
7657 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7658 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7659 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7660 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7661 if (!bmap1 || !bmap2)
7662 goto error;
7663 bmap1 = isl_basic_map_cow(bmap1);
7664 bmap1 = isl_basic_map_extend(bmap1, nparam,
7665 pos, (dim1 - pos) + (dim2 - pos),
7666 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7667 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7668 if (!bmap1)
7669 goto error;
7670 total = isl_basic_map_total_dim(bmap1);
7671 ctx = bmap1->ctx;
7672 obj = isl_vec_alloc(ctx, 1 + total);
7673 isl_seq_clr(obj->block.data, 1 + total);
7674 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7675 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7676 if (!obj)
7677 goto error;
7678 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7679 opt, NULL, NULL);
7680 isl_basic_map_free(bmap1);
7681 isl_vec_free(obj);
7682 return res;
7683 error:
7684 isl_basic_map_free(bmap1);
7685 isl_basic_map_free(bmap2);
7686 return isl_lp_error;
7689 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7690 * for any common value of the parameters and dimensions preceding pos
7691 * in both basic sets, the values of dimension pos in bset1 are
7692 * smaller or larger than those in bset2.
7694 * Returns
7695 * 1 if bset1 follows bset2
7696 * -1 if bset1 precedes bset2
7697 * 0 if bset1 and bset2 are incomparable
7698 * -2 if some error occurred.
7700 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7701 struct isl_basic_set *bset2, int pos)
7703 isl_int opt;
7704 enum isl_lp_result res;
7705 int cmp;
7707 isl_int_init(opt);
7709 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7711 if (res == isl_lp_empty)
7712 cmp = 0;
7713 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7714 res == isl_lp_unbounded)
7715 cmp = 1;
7716 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7717 cmp = -1;
7718 else
7719 cmp = -2;
7721 isl_int_clear(opt);
7722 return cmp;
7725 /* Given two basic sets bset1 and bset2, check whether
7726 * for any common value of the parameters and dimensions preceding pos
7727 * there is a value of dimension pos in bset1 that is larger
7728 * than a value of the same dimension in bset2.
7730 * Return
7731 * 1 if there exists such a pair
7732 * 0 if there is no such pair, but there is a pair of equal values
7733 * -1 otherwise
7734 * -2 if some error occurred.
7736 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7737 __isl_keep isl_basic_set *bset2, int pos)
7739 isl_int opt;
7740 enum isl_lp_result res;
7741 int cmp;
7743 isl_int_init(opt);
7745 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7747 if (res == isl_lp_empty)
7748 cmp = -1;
7749 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7750 res == isl_lp_unbounded)
7751 cmp = 1;
7752 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7753 cmp = -1;
7754 else if (res == isl_lp_ok)
7755 cmp = 0;
7756 else
7757 cmp = -2;
7759 isl_int_clear(opt);
7760 return cmp;
7763 /* Given two sets set1 and set2, check whether
7764 * for any common value of the parameters and dimensions preceding pos
7765 * there is a value of dimension pos in set1 that is larger
7766 * than a value of the same dimension in set2.
7768 * Return
7769 * 1 if there exists such a pair
7770 * 0 if there is no such pair, but there is a pair of equal values
7771 * -1 otherwise
7772 * -2 if some error occurred.
7774 int isl_set_follows_at(__isl_keep isl_set *set1,
7775 __isl_keep isl_set *set2, int pos)
7777 int i, j;
7778 int follows = -1;
7780 if (!set1 || !set2)
7781 return -2;
7783 for (i = 0; i < set1->n; ++i)
7784 for (j = 0; j < set2->n; ++j) {
7785 int f;
7786 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7787 if (f == 1 || f == -2)
7788 return f;
7789 if (f > follows)
7790 follows = f;
7793 return follows;
7796 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7797 unsigned pos, isl_int *val)
7799 int i;
7800 int d;
7801 unsigned total;
7803 if (!bmap)
7804 return -1;
7805 total = isl_basic_map_total_dim(bmap);
7806 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7807 for (; d+1 > pos; --d)
7808 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7809 break;
7810 if (d != pos)
7811 continue;
7812 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7813 return 0;
7814 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7815 return 0;
7816 if (!isl_int_is_one(bmap->eq[i][1+d]))
7817 return 0;
7818 if (val)
7819 isl_int_neg(*val, bmap->eq[i][0]);
7820 return 1;
7822 return 0;
7825 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7826 unsigned pos, isl_int *val)
7828 int i;
7829 isl_int v;
7830 isl_int tmp;
7831 int fixed;
7833 if (!map)
7834 return -1;
7835 if (map->n == 0)
7836 return 0;
7837 if (map->n == 1)
7838 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7839 isl_int_init(v);
7840 isl_int_init(tmp);
7841 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7842 for (i = 1; fixed == 1 && i < map->n; ++i) {
7843 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7844 if (fixed == 1 && isl_int_ne(tmp, v))
7845 fixed = 0;
7847 if (val)
7848 isl_int_set(*val, v);
7849 isl_int_clear(tmp);
7850 isl_int_clear(v);
7851 return fixed;
7854 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7855 unsigned pos, isl_int *val)
7857 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7858 pos, val);
7861 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7862 isl_int *val)
7864 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7867 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7868 enum isl_dim_type type, unsigned pos, isl_int *val)
7870 if (pos >= isl_basic_map_dim(bmap, type))
7871 return -1;
7872 return isl_basic_map_plain_has_fixed_var(bmap,
7873 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7876 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7877 enum isl_dim_type type, unsigned pos, isl_int *val)
7879 if (pos >= isl_map_dim(map, type))
7880 return -1;
7881 return isl_map_plain_has_fixed_var(map,
7882 map_offset(map, type) - 1 + pos, val);
7885 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
7886 enum isl_dim_type type, unsigned pos, isl_int *val)
7888 return isl_map_plain_is_fixed(set, type, pos, val);
7891 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7892 enum isl_dim_type type, unsigned pos, isl_int *val)
7894 return isl_map_plain_is_fixed(map, type, pos, val);
7897 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7898 * then return this fixed value in *val.
7900 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7901 unsigned dim, isl_int *val)
7903 return isl_basic_set_plain_has_fixed_var(bset,
7904 isl_basic_set_n_param(bset) + dim, val);
7907 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7908 * then return this fixed value in *val.
7910 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7911 unsigned dim, isl_int *val)
7913 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7916 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7917 unsigned dim, isl_int *val)
7919 return isl_set_plain_dim_is_fixed(set, dim, val);
7922 /* Check if input variable in has fixed value and if so and if val is not NULL,
7923 * then return this fixed value in *val.
7925 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7926 unsigned in, isl_int *val)
7928 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7931 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7932 * and if val is not NULL, then return this lower bound in *val.
7934 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7935 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7937 int i, i_eq = -1, i_ineq = -1;
7938 isl_int *c;
7939 unsigned total;
7940 unsigned nparam;
7942 if (!bset)
7943 return -1;
7944 total = isl_basic_set_total_dim(bset);
7945 nparam = isl_basic_set_n_param(bset);
7946 for (i = 0; i < bset->n_eq; ++i) {
7947 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7948 continue;
7949 if (i_eq != -1)
7950 return 0;
7951 i_eq = i;
7953 for (i = 0; i < bset->n_ineq; ++i) {
7954 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7955 continue;
7956 if (i_eq != -1 || i_ineq != -1)
7957 return 0;
7958 i_ineq = i;
7960 if (i_eq == -1 && i_ineq == -1)
7961 return 0;
7962 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7963 /* The coefficient should always be one due to normalization. */
7964 if (!isl_int_is_one(c[1+nparam+dim]))
7965 return 0;
7966 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7967 return 0;
7968 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7969 total - nparam - dim - 1) != -1)
7970 return 0;
7971 if (val)
7972 isl_int_neg(*val, c[0]);
7973 return 1;
7976 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7977 unsigned dim, isl_int *val)
7979 int i;
7980 isl_int v;
7981 isl_int tmp;
7982 int fixed;
7984 if (!set)
7985 return -1;
7986 if (set->n == 0)
7987 return 0;
7988 if (set->n == 1)
7989 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7990 dim, val);
7991 isl_int_init(v);
7992 isl_int_init(tmp);
7993 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7994 dim, &v);
7995 for (i = 1; fixed == 1 && i < set->n; ++i) {
7996 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7997 dim, &tmp);
7998 if (fixed == 1 && isl_int_ne(tmp, v))
7999 fixed = 0;
8001 if (val)
8002 isl_int_set(*val, v);
8003 isl_int_clear(tmp);
8004 isl_int_clear(v);
8005 return fixed;
8008 struct constraint {
8009 unsigned size;
8010 isl_int *c;
8013 /* uset_gist depends on constraints without existentially quantified
8014 * variables sorting first.
8016 static int qsort_constraint_cmp(const void *p1, const void *p2)
8018 const struct constraint *c1 = (const struct constraint *)p1;
8019 const struct constraint *c2 = (const struct constraint *)p2;
8020 int l1, l2;
8021 unsigned size = isl_min(c1->size, c2->size);
8023 l1 = isl_seq_last_non_zero(c1->c, size);
8024 l2 = isl_seq_last_non_zero(c2->c, size);
8026 if (l1 != l2)
8027 return l1 - l2;
8029 return isl_seq_cmp(c1->c, c2->c, size);
8032 static struct isl_basic_map *isl_basic_map_sort_constraints(
8033 struct isl_basic_map *bmap)
8035 int i;
8036 struct constraint *c;
8037 unsigned total;
8039 if (!bmap)
8040 return NULL;
8041 total = isl_basic_map_total_dim(bmap);
8042 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8043 if (!c)
8044 goto error;
8045 for (i = 0; i < bmap->n_ineq; ++i) {
8046 c[i].size = total;
8047 c[i].c = bmap->ineq[i];
8049 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8050 for (i = 0; i < bmap->n_ineq; ++i)
8051 bmap->ineq[i] = c[i].c;
8052 free(c);
8053 return bmap;
8054 error:
8055 isl_basic_map_free(bmap);
8056 return NULL;
8059 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8060 __isl_take isl_basic_set *bset)
8062 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8063 (struct isl_basic_map *)bset);
8066 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8068 if (!bmap)
8069 return NULL;
8070 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8071 return bmap;
8072 bmap = isl_basic_map_remove_redundancies(bmap);
8073 bmap = isl_basic_map_sort_constraints(bmap);
8074 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8075 return bmap;
8078 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8080 return (struct isl_basic_set *)isl_basic_map_normalize(
8081 (struct isl_basic_map *)bset);
8084 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8085 const __isl_keep isl_basic_map *bmap2)
8087 int i, cmp;
8088 unsigned total;
8090 if (bmap1 == bmap2)
8091 return 0;
8092 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8093 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8094 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8095 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8096 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8097 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8098 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8099 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8100 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8101 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8102 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8103 return 0;
8104 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8105 return 1;
8106 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8107 return -1;
8108 if (bmap1->n_eq != bmap2->n_eq)
8109 return bmap1->n_eq - bmap2->n_eq;
8110 if (bmap1->n_ineq != bmap2->n_ineq)
8111 return bmap1->n_ineq - bmap2->n_ineq;
8112 if (bmap1->n_div != bmap2->n_div)
8113 return bmap1->n_div - bmap2->n_div;
8114 total = isl_basic_map_total_dim(bmap1);
8115 for (i = 0; i < bmap1->n_eq; ++i) {
8116 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8117 if (cmp)
8118 return cmp;
8120 for (i = 0; i < bmap1->n_ineq; ++i) {
8121 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8122 if (cmp)
8123 return cmp;
8125 for (i = 0; i < bmap1->n_div; ++i) {
8126 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8127 if (cmp)
8128 return cmp;
8130 return 0;
8133 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8134 const __isl_keep isl_basic_set *bset2)
8136 return isl_basic_map_plain_cmp(bset1, bset2);
8139 int isl_set_plain_cmp(const __isl_keep isl_set *set1,
8140 const __isl_keep isl_set *set2)
8142 int i, cmp;
8144 if (set1 == set2)
8145 return 0;
8146 if (set1->n != set2->n)
8147 return set1->n - set2->n;
8149 for (i = 0; i < set1->n; ++i) {
8150 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8151 if (cmp)
8152 return cmp;
8155 return 0;
8158 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8159 __isl_keep isl_basic_map *bmap2)
8161 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8164 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8165 __isl_keep isl_basic_set *bset2)
8167 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8168 (isl_basic_map *)bset2);
8171 static int qsort_bmap_cmp(const void *p1, const void *p2)
8173 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8174 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8176 return isl_basic_map_plain_cmp(bmap1, bmap2);
8179 /* We normalize in place, but if anything goes wrong we need
8180 * to return NULL, so we need to make sure we don't change the
8181 * meaning of any possible other copies of map.
8183 struct isl_map *isl_map_normalize(struct isl_map *map)
8185 int i, j;
8186 struct isl_basic_map *bmap;
8188 if (!map)
8189 return NULL;
8190 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8191 return map;
8192 for (i = 0; i < map->n; ++i) {
8193 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8194 if (!bmap)
8195 goto error;
8196 isl_basic_map_free(map->p[i]);
8197 map->p[i] = bmap;
8199 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8200 ISL_F_SET(map, ISL_MAP_NORMALIZED);
8201 map = isl_map_remove_empty_parts(map);
8202 if (!map)
8203 return NULL;
8204 for (i = map->n - 1; i >= 1; --i) {
8205 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8206 continue;
8207 isl_basic_map_free(map->p[i-1]);
8208 for (j = i; j < map->n; ++j)
8209 map->p[j-1] = map->p[j];
8210 map->n--;
8212 return map;
8213 error:
8214 isl_map_free(map);
8215 return NULL;
8219 struct isl_set *isl_set_normalize(struct isl_set *set)
8221 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8224 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8226 int i;
8227 int equal;
8229 if (!map1 || !map2)
8230 return -1;
8232 if (map1 == map2)
8233 return 1;
8234 if (!isl_space_is_equal(map1->dim, map2->dim))
8235 return 0;
8237 map1 = isl_map_copy(map1);
8238 map2 = isl_map_copy(map2);
8239 map1 = isl_map_normalize(map1);
8240 map2 = isl_map_normalize(map2);
8241 if (!map1 || !map2)
8242 goto error;
8243 equal = map1->n == map2->n;
8244 for (i = 0; equal && i < map1->n; ++i) {
8245 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8246 if (equal < 0)
8247 goto error;
8249 isl_map_free(map1);
8250 isl_map_free(map2);
8251 return equal;
8252 error:
8253 isl_map_free(map1);
8254 isl_map_free(map2);
8255 return -1;
8258 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8260 return isl_map_plain_is_equal(map1, map2);
8263 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8265 return isl_map_plain_is_equal((struct isl_map *)set1,
8266 (struct isl_map *)set2);
8269 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8271 return isl_set_plain_is_equal(set1, set2);
8274 /* Return an interval that ranges from min to max (inclusive)
8276 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8277 isl_int min, isl_int max)
8279 int k;
8280 struct isl_basic_set *bset = NULL;
8282 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8283 if (!bset)
8284 goto error;
8286 k = isl_basic_set_alloc_inequality(bset);
8287 if (k < 0)
8288 goto error;
8289 isl_int_set_si(bset->ineq[k][1], 1);
8290 isl_int_neg(bset->ineq[k][0], min);
8292 k = isl_basic_set_alloc_inequality(bset);
8293 if (k < 0)
8294 goto error;
8295 isl_int_set_si(bset->ineq[k][1], -1);
8296 isl_int_set(bset->ineq[k][0], max);
8298 return bset;
8299 error:
8300 isl_basic_set_free(bset);
8301 return NULL;
8304 /* Return the Cartesian product of the basic sets in list (in the given order).
8306 __isl_give isl_basic_set *isl_basic_set_list_product(
8307 __isl_take struct isl_basic_set_list *list)
8309 int i;
8310 unsigned dim;
8311 unsigned nparam;
8312 unsigned extra;
8313 unsigned n_eq;
8314 unsigned n_ineq;
8315 struct isl_basic_set *product = NULL;
8317 if (!list)
8318 goto error;
8319 isl_assert(list->ctx, list->n > 0, goto error);
8320 isl_assert(list->ctx, list->p[0], goto error);
8321 nparam = isl_basic_set_n_param(list->p[0]);
8322 dim = isl_basic_set_n_dim(list->p[0]);
8323 extra = list->p[0]->n_div;
8324 n_eq = list->p[0]->n_eq;
8325 n_ineq = list->p[0]->n_ineq;
8326 for (i = 1; i < list->n; ++i) {
8327 isl_assert(list->ctx, list->p[i], goto error);
8328 isl_assert(list->ctx,
8329 nparam == isl_basic_set_n_param(list->p[i]), goto error);
8330 dim += isl_basic_set_n_dim(list->p[i]);
8331 extra += list->p[i]->n_div;
8332 n_eq += list->p[i]->n_eq;
8333 n_ineq += list->p[i]->n_ineq;
8335 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
8336 n_eq, n_ineq);
8337 if (!product)
8338 goto error;
8339 dim = 0;
8340 for (i = 0; i < list->n; ++i) {
8341 isl_basic_set_add_constraints(product,
8342 isl_basic_set_copy(list->p[i]), dim);
8343 dim += isl_basic_set_n_dim(list->p[i]);
8345 isl_basic_set_list_free(list);
8346 return product;
8347 error:
8348 isl_basic_set_free(product);
8349 isl_basic_set_list_free(list);
8350 return NULL;
8353 struct isl_basic_map *isl_basic_map_product(
8354 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8356 isl_space *dim_result = NULL;
8357 struct isl_basic_map *bmap;
8358 unsigned in1, in2, out1, out2, nparam, total, pos;
8359 struct isl_dim_map *dim_map1, *dim_map2;
8361 if (!bmap1 || !bmap2)
8362 goto error;
8364 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
8365 bmap2->dim, isl_dim_param), goto error);
8366 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
8367 isl_space_copy(bmap2->dim));
8369 in1 = isl_basic_map_n_in(bmap1);
8370 in2 = isl_basic_map_n_in(bmap2);
8371 out1 = isl_basic_map_n_out(bmap1);
8372 out2 = isl_basic_map_n_out(bmap2);
8373 nparam = isl_basic_map_n_param(bmap1);
8375 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
8376 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8377 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8378 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8379 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8380 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8381 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8382 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8383 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8384 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8385 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8387 bmap = isl_basic_map_alloc_space(dim_result,
8388 bmap1->n_div + bmap2->n_div,
8389 bmap1->n_eq + bmap2->n_eq,
8390 bmap1->n_ineq + bmap2->n_ineq);
8391 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8392 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8393 bmap = isl_basic_map_simplify(bmap);
8394 return isl_basic_map_finalize(bmap);
8395 error:
8396 isl_basic_map_free(bmap1);
8397 isl_basic_map_free(bmap2);
8398 return NULL;
8401 __isl_give isl_basic_map *isl_basic_map_flat_product(
8402 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8404 isl_basic_map *prod;
8406 prod = isl_basic_map_product(bmap1, bmap2);
8407 prod = isl_basic_map_flatten(prod);
8408 return prod;
8411 __isl_give isl_basic_set *isl_basic_set_flat_product(
8412 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
8414 return isl_basic_map_flat_range_product(bset1, bset2);
8417 __isl_give isl_basic_map *isl_basic_map_domain_product(
8418 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8420 isl_space *space_result = NULL;
8421 isl_basic_map *bmap;
8422 unsigned in1, in2, out, nparam, total, pos;
8423 struct isl_dim_map *dim_map1, *dim_map2;
8425 if (!bmap1 || !bmap2)
8426 goto error;
8428 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8429 isl_space_copy(bmap2->dim));
8431 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8432 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8433 out = isl_basic_map_dim(bmap1, isl_dim_out);
8434 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8436 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8437 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8438 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8439 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8440 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8441 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8442 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8443 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8444 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8445 isl_dim_map_div(dim_map1, bmap1, pos += out);
8446 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8448 bmap = isl_basic_map_alloc_space(space_result,
8449 bmap1->n_div + bmap2->n_div,
8450 bmap1->n_eq + bmap2->n_eq,
8451 bmap1->n_ineq + bmap2->n_ineq);
8452 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8453 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8454 bmap = isl_basic_map_simplify(bmap);
8455 return isl_basic_map_finalize(bmap);
8456 error:
8457 isl_basic_map_free(bmap1);
8458 isl_basic_map_free(bmap2);
8459 return NULL;
8462 __isl_give isl_basic_map *isl_basic_map_range_product(
8463 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8465 isl_space *dim_result = NULL;
8466 isl_basic_map *bmap;
8467 unsigned in, out1, out2, nparam, total, pos;
8468 struct isl_dim_map *dim_map1, *dim_map2;
8470 if (!bmap1 || !bmap2)
8471 goto error;
8473 if (!isl_space_match(bmap1->dim, isl_dim_param,
8474 bmap2->dim, isl_dim_param))
8475 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
8476 "parameters don't match", goto error);
8478 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8479 isl_space_copy(bmap2->dim));
8481 in = isl_basic_map_dim(bmap1, isl_dim_in);
8482 out1 = isl_basic_map_n_out(bmap1);
8483 out2 = isl_basic_map_n_out(bmap2);
8484 nparam = isl_basic_map_n_param(bmap1);
8486 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8487 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8488 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8489 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8490 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8491 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8492 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8493 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8494 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8495 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8496 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8498 bmap = isl_basic_map_alloc_space(dim_result,
8499 bmap1->n_div + bmap2->n_div,
8500 bmap1->n_eq + bmap2->n_eq,
8501 bmap1->n_ineq + bmap2->n_ineq);
8502 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8503 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8504 bmap = isl_basic_map_simplify(bmap);
8505 return isl_basic_map_finalize(bmap);
8506 error:
8507 isl_basic_map_free(bmap1);
8508 isl_basic_map_free(bmap2);
8509 return NULL;
8512 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8513 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8515 isl_basic_map *prod;
8517 prod = isl_basic_map_range_product(bmap1, bmap2);
8518 prod = isl_basic_map_flatten_range(prod);
8519 return prod;
8522 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8523 __isl_take isl_map *map2,
8524 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8525 __isl_take isl_space *right),
8526 __isl_give isl_basic_map *(*basic_map_product)(
8527 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8529 unsigned flags = 0;
8530 struct isl_map *result;
8531 int i, j;
8533 if (!map1 || !map2)
8534 goto error;
8536 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8537 map2->dim, isl_dim_param), goto error);
8539 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8540 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8541 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8543 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8544 isl_space_copy(map2->dim)),
8545 map1->n * map2->n, flags);
8546 if (!result)
8547 goto error;
8548 for (i = 0; i < map1->n; ++i)
8549 for (j = 0; j < map2->n; ++j) {
8550 struct isl_basic_map *part;
8551 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8552 isl_basic_map_copy(map2->p[j]));
8553 if (isl_basic_map_is_empty(part))
8554 isl_basic_map_free(part);
8555 else
8556 result = isl_map_add_basic_map(result, part);
8557 if (!result)
8558 goto error;
8560 isl_map_free(map1);
8561 isl_map_free(map2);
8562 return result;
8563 error:
8564 isl_map_free(map1);
8565 isl_map_free(map2);
8566 return NULL;
8569 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8571 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8572 __isl_take isl_map *map2)
8574 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8577 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8578 __isl_take isl_map *map2)
8580 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8583 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8585 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8586 __isl_take isl_map *map2)
8588 isl_map *prod;
8590 prod = isl_map_product(map1, map2);
8591 prod = isl_map_flatten(prod);
8592 return prod;
8595 /* Given two set A and B, construct its Cartesian product A x B.
8597 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8599 return isl_map_range_product(set1, set2);
8602 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8603 __isl_take isl_set *set2)
8605 return isl_map_flat_range_product(set1, set2);
8608 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8610 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8611 __isl_take isl_map *map2)
8613 return map_product(map1, map2, &isl_space_domain_product,
8614 &isl_basic_map_domain_product);
8617 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8619 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8620 __isl_take isl_map *map2)
8622 return map_product(map1, map2, &isl_space_range_product,
8623 &isl_basic_map_range_product);
8626 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8627 __isl_take isl_map *map2)
8629 return isl_map_align_params_map_map_and(map1, map2,
8630 &map_domain_product_aligned);
8633 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8634 __isl_take isl_map *map2)
8636 return isl_map_align_params_map_map_and(map1, map2,
8637 &map_range_product_aligned);
8640 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8642 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8643 __isl_take isl_map *map2)
8645 isl_map *prod;
8647 prod = isl_map_domain_product(map1, map2);
8648 prod = isl_map_flatten_domain(prod);
8649 return prod;
8652 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8654 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8655 __isl_take isl_map *map2)
8657 isl_map *prod;
8659 prod = isl_map_range_product(map1, map2);
8660 prod = isl_map_flatten_range(prod);
8661 return prod;
8664 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8666 int i;
8667 uint32_t hash = isl_hash_init();
8668 unsigned total;
8670 if (!bmap)
8671 return 0;
8672 bmap = isl_basic_map_copy(bmap);
8673 bmap = isl_basic_map_normalize(bmap);
8674 if (!bmap)
8675 return 0;
8676 total = isl_basic_map_total_dim(bmap);
8677 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8678 for (i = 0; i < bmap->n_eq; ++i) {
8679 uint32_t c_hash;
8680 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8681 isl_hash_hash(hash, c_hash);
8683 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8684 for (i = 0; i < bmap->n_ineq; ++i) {
8685 uint32_t c_hash;
8686 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8687 isl_hash_hash(hash, c_hash);
8689 isl_hash_byte(hash, bmap->n_div & 0xFF);
8690 for (i = 0; i < bmap->n_div; ++i) {
8691 uint32_t c_hash;
8692 if (isl_int_is_zero(bmap->div[i][0]))
8693 continue;
8694 isl_hash_byte(hash, i & 0xFF);
8695 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8696 isl_hash_hash(hash, c_hash);
8698 isl_basic_map_free(bmap);
8699 return hash;
8702 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8704 return isl_basic_map_get_hash((isl_basic_map *)bset);
8707 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8709 int i;
8710 uint32_t hash;
8712 if (!map)
8713 return 0;
8714 map = isl_map_copy(map);
8715 map = isl_map_normalize(map);
8716 if (!map)
8717 return 0;
8719 hash = isl_hash_init();
8720 for (i = 0; i < map->n; ++i) {
8721 uint32_t bmap_hash;
8722 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8723 isl_hash_hash(hash, bmap_hash);
8726 isl_map_free(map);
8728 return hash;
8731 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8733 return isl_map_get_hash((isl_map *)set);
8736 /* Check if the value for dimension dim is completely determined
8737 * by the values of the other parameters and variables.
8738 * That is, check if dimension dim is involved in an equality.
8740 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8742 int i;
8743 unsigned nparam;
8745 if (!bset)
8746 return -1;
8747 nparam = isl_basic_set_n_param(bset);
8748 for (i = 0; i < bset->n_eq; ++i)
8749 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8750 return 1;
8751 return 0;
8754 /* Check if the value for dimension dim is completely determined
8755 * by the values of the other parameters and variables.
8756 * That is, check if dimension dim is involved in an equality
8757 * for each of the subsets.
8759 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8761 int i;
8763 if (!set)
8764 return -1;
8765 for (i = 0; i < set->n; ++i) {
8766 int unique;
8767 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8768 if (unique != 1)
8769 return unique;
8771 return 1;
8774 int isl_set_n_basic_set(__isl_keep isl_set *set)
8776 return set ? set->n : 0;
8779 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8780 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8782 int i;
8784 if (!map)
8785 return -1;
8787 for (i = 0; i < map->n; ++i)
8788 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8789 return -1;
8791 return 0;
8794 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8795 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8797 int i;
8799 if (!set)
8800 return -1;
8802 for (i = 0; i < set->n; ++i)
8803 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8804 return -1;
8806 return 0;
8809 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8811 isl_space *dim;
8813 if (!bset)
8814 return NULL;
8816 bset = isl_basic_set_cow(bset);
8817 if (!bset)
8818 return NULL;
8820 dim = isl_basic_set_get_space(bset);
8821 dim = isl_space_lift(dim, bset->n_div);
8822 if (!dim)
8823 goto error;
8824 isl_space_free(bset->dim);
8825 bset->dim = dim;
8826 bset->extra -= bset->n_div;
8827 bset->n_div = 0;
8829 bset = isl_basic_set_finalize(bset);
8831 return bset;
8832 error:
8833 isl_basic_set_free(bset);
8834 return NULL;
8837 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8839 int i;
8840 isl_space *dim;
8841 unsigned n_div;
8843 set = isl_set_align_divs(set);
8845 if (!set)
8846 return NULL;
8848 set = isl_set_cow(set);
8849 if (!set)
8850 return NULL;
8852 n_div = set->p[0]->n_div;
8853 dim = isl_set_get_space(set);
8854 dim = isl_space_lift(dim, n_div);
8855 if (!dim)
8856 goto error;
8857 isl_space_free(set->dim);
8858 set->dim = dim;
8860 for (i = 0; i < set->n; ++i) {
8861 set->p[i] = isl_basic_set_lift(set->p[i]);
8862 if (!set->p[i])
8863 goto error;
8866 return set;
8867 error:
8868 isl_set_free(set);
8869 return NULL;
8872 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8874 isl_space *dim;
8875 struct isl_basic_map *bmap;
8876 unsigned n_set;
8877 unsigned n_div;
8878 unsigned n_param;
8879 unsigned total;
8880 int i, k, l;
8882 set = isl_set_align_divs(set);
8884 if (!set)
8885 return NULL;
8887 dim = isl_set_get_space(set);
8888 if (set->n == 0 || set->p[0]->n_div == 0) {
8889 isl_set_free(set);
8890 return isl_map_identity(isl_space_map_from_set(dim));
8893 n_div = set->p[0]->n_div;
8894 dim = isl_space_map_from_set(dim);
8895 n_param = isl_space_dim(dim, isl_dim_param);
8896 n_set = isl_space_dim(dim, isl_dim_in);
8897 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8898 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8899 for (i = 0; i < n_set; ++i)
8900 bmap = var_equal(bmap, i);
8902 total = n_param + n_set + n_set + n_div;
8903 for (i = 0; i < n_div; ++i) {
8904 k = isl_basic_map_alloc_inequality(bmap);
8905 if (k < 0)
8906 goto error;
8907 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8908 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8909 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8910 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8911 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8912 set->p[0]->div[i][0]);
8914 l = isl_basic_map_alloc_inequality(bmap);
8915 if (l < 0)
8916 goto error;
8917 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8918 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8919 set->p[0]->div[i][0]);
8920 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8923 isl_set_free(set);
8924 bmap = isl_basic_map_simplify(bmap);
8925 bmap = isl_basic_map_finalize(bmap);
8926 return isl_map_from_basic_map(bmap);
8927 error:
8928 isl_set_free(set);
8929 isl_basic_map_free(bmap);
8930 return NULL;
8933 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8935 unsigned dim;
8936 int size = 0;
8938 if (!bset)
8939 return -1;
8941 dim = isl_basic_set_total_dim(bset);
8942 size += bset->n_eq * (1 + dim);
8943 size += bset->n_ineq * (1 + dim);
8944 size += bset->n_div * (2 + dim);
8946 return size;
8949 int isl_set_size(__isl_keep isl_set *set)
8951 int i;
8952 int size = 0;
8954 if (!set)
8955 return -1;
8957 for (i = 0; i < set->n; ++i)
8958 size += isl_basic_set_size(set->p[i]);
8960 return size;
8963 /* Check if there is any lower bound (if lower == 0) and/or upper
8964 * bound (if upper == 0) on the specified dim.
8966 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8967 enum isl_dim_type type, unsigned pos, int lower, int upper)
8969 int i;
8971 if (!bmap)
8972 return -1;
8974 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8976 pos += isl_basic_map_offset(bmap, type);
8978 for (i = 0; i < bmap->n_div; ++i) {
8979 if (isl_int_is_zero(bmap->div[i][0]))
8980 continue;
8981 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8982 return 1;
8985 for (i = 0; i < bmap->n_eq; ++i)
8986 if (!isl_int_is_zero(bmap->eq[i][pos]))
8987 return 1;
8989 for (i = 0; i < bmap->n_ineq; ++i) {
8990 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8991 if (sgn > 0)
8992 lower = 1;
8993 if (sgn < 0)
8994 upper = 1;
8997 return lower && upper;
9000 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9001 enum isl_dim_type type, unsigned pos)
9003 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9006 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9007 enum isl_dim_type type, unsigned pos)
9009 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9012 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9013 enum isl_dim_type type, unsigned pos)
9015 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9018 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9019 enum isl_dim_type type, unsigned pos)
9021 int i;
9023 if (!map)
9024 return -1;
9026 for (i = 0; i < map->n; ++i) {
9027 int bounded;
9028 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9029 if (bounded < 0 || !bounded)
9030 return bounded;
9033 return 1;
9036 /* Return 1 if the specified dim is involved in both an upper bound
9037 * and a lower bound.
9039 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9040 enum isl_dim_type type, unsigned pos)
9042 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9045 static int has_bound(__isl_keep isl_map *map,
9046 enum isl_dim_type type, unsigned pos,
9047 int (*fn)(__isl_keep isl_basic_map *bmap,
9048 enum isl_dim_type type, unsigned pos))
9050 int i;
9052 if (!map)
9053 return -1;
9055 for (i = 0; i < map->n; ++i) {
9056 int bounded;
9057 bounded = fn(map->p[i], type, pos);
9058 if (bounded < 0 || bounded)
9059 return bounded;
9062 return 0;
9065 /* Return 1 if the specified dim is involved in any lower bound.
9067 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9068 enum isl_dim_type type, unsigned pos)
9070 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9073 /* Return 1 if the specified dim is involved in any upper bound.
9075 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9076 enum isl_dim_type type, unsigned pos)
9078 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9081 /* For each of the "n" variables starting at "first", determine
9082 * the sign of the variable and put the results in the first "n"
9083 * elements of the array "signs".
9084 * Sign
9085 * 1 means that the variable is non-negative
9086 * -1 means that the variable is non-positive
9087 * 0 means the variable attains both positive and negative values.
9089 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9090 unsigned first, unsigned n, int *signs)
9092 isl_vec *bound = NULL;
9093 struct isl_tab *tab = NULL;
9094 struct isl_tab_undo *snap;
9095 int i;
9097 if (!bset || !signs)
9098 return -1;
9100 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9101 tab = isl_tab_from_basic_set(bset, 0);
9102 if (!bound || !tab)
9103 goto error;
9105 isl_seq_clr(bound->el, bound->size);
9106 isl_int_set_si(bound->el[0], -1);
9108 snap = isl_tab_snap(tab);
9109 for (i = 0; i < n; ++i) {
9110 int empty;
9112 isl_int_set_si(bound->el[1 + first + i], -1);
9113 if (isl_tab_add_ineq(tab, bound->el) < 0)
9114 goto error;
9115 empty = tab->empty;
9116 isl_int_set_si(bound->el[1 + first + i], 0);
9117 if (isl_tab_rollback(tab, snap) < 0)
9118 goto error;
9120 if (empty) {
9121 signs[i] = 1;
9122 continue;
9125 isl_int_set_si(bound->el[1 + first + i], 1);
9126 if (isl_tab_add_ineq(tab, bound->el) < 0)
9127 goto error;
9128 empty = tab->empty;
9129 isl_int_set_si(bound->el[1 + first + i], 0);
9130 if (isl_tab_rollback(tab, snap) < 0)
9131 goto error;
9133 signs[i] = empty ? -1 : 0;
9136 isl_tab_free(tab);
9137 isl_vec_free(bound);
9138 return 0;
9139 error:
9140 isl_tab_free(tab);
9141 isl_vec_free(bound);
9142 return -1;
9145 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
9146 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
9148 if (!bset || !signs)
9149 return -1;
9150 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
9151 return -1);
9153 first += pos(bset->dim, type) - 1;
9154 return isl_basic_set_vars_get_sign(bset, first, n, signs);
9157 /* Check if the given basic map is obviously single-valued.
9158 * In particular, for each output dimension, check that there is
9159 * an equality that defines the output dimension in terms of
9160 * earlier dimensions.
9162 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
9164 int i, j;
9165 unsigned total;
9166 unsigned n_out;
9167 unsigned o_out;
9169 if (!bmap)
9170 return -1;
9172 total = 1 + isl_basic_map_total_dim(bmap);
9173 n_out = isl_basic_map_dim(bmap, isl_dim_out);
9174 o_out = isl_basic_map_offset(bmap, isl_dim_out);
9176 for (i = 0; i < n_out; ++i) {
9177 for (j = 0; j < bmap->n_eq; ++j) {
9178 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
9179 continue;
9180 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
9181 total - (o_out + i + 1)) == -1)
9182 break;
9184 if (j >= bmap->n_eq)
9185 return 0;
9188 return 1;
9191 /* Check if the given basic map is single-valued.
9192 * We simply compute
9194 * M \circ M^-1
9196 * and check if the result is a subset of the identity mapping.
9198 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
9200 isl_space *space;
9201 isl_basic_map *test;
9202 isl_basic_map *id;
9203 int sv;
9205 sv = isl_basic_map_plain_is_single_valued(bmap);
9206 if (sv < 0 || sv)
9207 return sv;
9209 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
9210 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
9212 space = isl_basic_map_get_space(bmap);
9213 space = isl_space_map_from_set(isl_space_range(space));
9214 id = isl_basic_map_identity(space);
9216 sv = isl_basic_map_is_subset(test, id);
9218 isl_basic_map_free(test);
9219 isl_basic_map_free(id);
9221 return sv;
9224 /* Check if the given map is obviously single-valued.
9226 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
9228 if (!map)
9229 return -1;
9230 if (map->n == 0)
9231 return 1;
9232 if (map->n >= 2)
9233 return 0;
9235 return isl_basic_map_plain_is_single_valued(map->p[0]);
9238 /* Check if the given map is single-valued.
9239 * We simply compute
9241 * M \circ M^-1
9243 * and check if the result is a subset of the identity mapping.
9245 int isl_map_is_single_valued(__isl_keep isl_map *map)
9247 isl_space *dim;
9248 isl_map *test;
9249 isl_map *id;
9250 int sv;
9252 sv = isl_map_plain_is_single_valued(map);
9253 if (sv < 0 || sv)
9254 return sv;
9256 test = isl_map_reverse(isl_map_copy(map));
9257 test = isl_map_apply_range(test, isl_map_copy(map));
9259 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
9260 id = isl_map_identity(dim);
9262 sv = isl_map_is_subset(test, id);
9264 isl_map_free(test);
9265 isl_map_free(id);
9267 return sv;
9270 int isl_map_is_injective(__isl_keep isl_map *map)
9272 int in;
9274 map = isl_map_copy(map);
9275 map = isl_map_reverse(map);
9276 in = isl_map_is_single_valued(map);
9277 isl_map_free(map);
9279 return in;
9282 /* Check if the given map is obviously injective.
9284 int isl_map_plain_is_injective(__isl_keep isl_map *map)
9286 int in;
9288 map = isl_map_copy(map);
9289 map = isl_map_reverse(map);
9290 in = isl_map_plain_is_single_valued(map);
9291 isl_map_free(map);
9293 return in;
9296 int isl_map_is_bijective(__isl_keep isl_map *map)
9298 int sv;
9300 sv = isl_map_is_single_valued(map);
9301 if (sv < 0 || !sv)
9302 return sv;
9304 return isl_map_is_injective(map);
9307 int isl_set_is_singleton(__isl_keep isl_set *set)
9309 return isl_map_is_single_valued((isl_map *)set);
9312 int isl_map_is_translation(__isl_keep isl_map *map)
9314 int ok;
9315 isl_set *delta;
9317 delta = isl_map_deltas(isl_map_copy(map));
9318 ok = isl_set_is_singleton(delta);
9319 isl_set_free(delta);
9321 return ok;
9324 static int unique(isl_int *p, unsigned pos, unsigned len)
9326 if (isl_seq_first_non_zero(p, pos) != -1)
9327 return 0;
9328 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
9329 return 0;
9330 return 1;
9333 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
9335 int i, j;
9336 unsigned nvar;
9337 unsigned ovar;
9339 if (!bset)
9340 return -1;
9342 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
9343 return 0;
9345 nvar = isl_basic_set_dim(bset, isl_dim_set);
9346 ovar = isl_space_offset(bset->dim, isl_dim_set);
9347 for (j = 0; j < nvar; ++j) {
9348 int lower = 0, upper = 0;
9349 for (i = 0; i < bset->n_eq; ++i) {
9350 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
9351 continue;
9352 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
9353 return 0;
9354 break;
9356 if (i < bset->n_eq)
9357 continue;
9358 for (i = 0; i < bset->n_ineq; ++i) {
9359 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
9360 continue;
9361 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
9362 return 0;
9363 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
9364 lower = 1;
9365 else
9366 upper = 1;
9368 if (!lower || !upper)
9369 return 0;
9372 return 1;
9375 int isl_set_is_box(__isl_keep isl_set *set)
9377 if (!set)
9378 return -1;
9379 if (set->n != 1)
9380 return 0;
9382 return isl_basic_set_is_box(set->p[0]);
9385 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
9387 if (!bset)
9388 return -1;
9390 return isl_space_is_wrapping(bset->dim);
9393 int isl_set_is_wrapping(__isl_keep isl_set *set)
9395 if (!set)
9396 return -1;
9398 return isl_space_is_wrapping(set->dim);
9401 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
9403 bmap = isl_basic_map_cow(bmap);
9404 if (!bmap)
9405 return NULL;
9407 bmap->dim = isl_space_wrap(bmap->dim);
9408 if (!bmap->dim)
9409 goto error;
9411 bmap = isl_basic_map_finalize(bmap);
9413 return (isl_basic_set *)bmap;
9414 error:
9415 isl_basic_map_free(bmap);
9416 return NULL;
9419 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
9421 int i;
9423 map = isl_map_cow(map);
9424 if (!map)
9425 return NULL;
9427 for (i = 0; i < map->n; ++i) {
9428 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
9429 if (!map->p[i])
9430 goto error;
9432 map->dim = isl_space_wrap(map->dim);
9433 if (!map->dim)
9434 goto error;
9436 return (isl_set *)map;
9437 error:
9438 isl_map_free(map);
9439 return NULL;
9442 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
9444 bset = isl_basic_set_cow(bset);
9445 if (!bset)
9446 return NULL;
9448 bset->dim = isl_space_unwrap(bset->dim);
9449 if (!bset->dim)
9450 goto error;
9452 bset = isl_basic_set_finalize(bset);
9454 return (isl_basic_map *)bset;
9455 error:
9456 isl_basic_set_free(bset);
9457 return NULL;
9460 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
9462 int i;
9464 if (!set)
9465 return NULL;
9467 if (!isl_set_is_wrapping(set))
9468 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
9469 goto error);
9471 set = isl_set_cow(set);
9472 if (!set)
9473 return NULL;
9475 for (i = 0; i < set->n; ++i) {
9476 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9477 if (!set->p[i])
9478 goto error;
9481 set->dim = isl_space_unwrap(set->dim);
9482 if (!set->dim)
9483 goto error;
9485 return (isl_map *)set;
9486 error:
9487 isl_set_free(set);
9488 return NULL;
9491 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9492 enum isl_dim_type type)
9494 if (!bmap)
9495 return NULL;
9497 if (!isl_space_is_named_or_nested(bmap->dim, type))
9498 return bmap;
9500 bmap = isl_basic_map_cow(bmap);
9501 if (!bmap)
9502 return NULL;
9504 bmap->dim = isl_space_reset(bmap->dim, type);
9505 if (!bmap->dim)
9506 goto error;
9508 bmap = isl_basic_map_finalize(bmap);
9510 return bmap;
9511 error:
9512 isl_basic_map_free(bmap);
9513 return NULL;
9516 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
9517 enum isl_dim_type type)
9519 int i;
9521 if (!map)
9522 return NULL;
9524 if (!isl_space_is_named_or_nested(map->dim, type))
9525 return map;
9527 map = isl_map_cow(map);
9528 if (!map)
9529 return NULL;
9531 for (i = 0; i < map->n; ++i) {
9532 map->p[i] = isl_basic_map_reset(map->p[i], type);
9533 if (!map->p[i])
9534 goto error;
9536 map->dim = isl_space_reset(map->dim, type);
9537 if (!map->dim)
9538 goto error;
9540 return map;
9541 error:
9542 isl_map_free(map);
9543 return NULL;
9546 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
9548 if (!bmap)
9549 return NULL;
9551 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
9552 return bmap;
9554 bmap = isl_basic_map_cow(bmap);
9555 if (!bmap)
9556 return NULL;
9558 bmap->dim = isl_space_flatten(bmap->dim);
9559 if (!bmap->dim)
9560 goto error;
9562 bmap = isl_basic_map_finalize(bmap);
9564 return bmap;
9565 error:
9566 isl_basic_map_free(bmap);
9567 return NULL;
9570 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9572 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9575 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9576 __isl_take isl_basic_map *bmap)
9578 if (!bmap)
9579 return NULL;
9581 if (!bmap->dim->nested[0])
9582 return bmap;
9584 bmap = isl_basic_map_cow(bmap);
9585 if (!bmap)
9586 return NULL;
9588 bmap->dim = isl_space_flatten_domain(bmap->dim);
9589 if (!bmap->dim)
9590 goto error;
9592 bmap = isl_basic_map_finalize(bmap);
9594 return bmap;
9595 error:
9596 isl_basic_map_free(bmap);
9597 return NULL;
9600 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9601 __isl_take isl_basic_map *bmap)
9603 if (!bmap)
9604 return NULL;
9606 if (!bmap->dim->nested[1])
9607 return bmap;
9609 bmap = isl_basic_map_cow(bmap);
9610 if (!bmap)
9611 return NULL;
9613 bmap->dim = isl_space_flatten_range(bmap->dim);
9614 if (!bmap->dim)
9615 goto error;
9617 bmap = isl_basic_map_finalize(bmap);
9619 return bmap;
9620 error:
9621 isl_basic_map_free(bmap);
9622 return NULL;
9625 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9627 int i;
9629 if (!map)
9630 return NULL;
9632 if (!map->dim->nested[0] && !map->dim->nested[1])
9633 return map;
9635 map = isl_map_cow(map);
9636 if (!map)
9637 return NULL;
9639 for (i = 0; i < map->n; ++i) {
9640 map->p[i] = isl_basic_map_flatten(map->p[i]);
9641 if (!map->p[i])
9642 goto error;
9644 map->dim = isl_space_flatten(map->dim);
9645 if (!map->dim)
9646 goto error;
9648 return map;
9649 error:
9650 isl_map_free(map);
9651 return NULL;
9654 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9656 return (isl_set *)isl_map_flatten((isl_map *)set);
9659 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9661 isl_space *dim, *flat_dim;
9662 isl_map *map;
9664 dim = isl_set_get_space(set);
9665 flat_dim = isl_space_flatten(isl_space_copy(dim));
9666 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9667 map = isl_map_intersect_domain(map, set);
9669 return map;
9672 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9674 int i;
9676 if (!map)
9677 return NULL;
9679 if (!map->dim->nested[0])
9680 return map;
9682 map = isl_map_cow(map);
9683 if (!map)
9684 return NULL;
9686 for (i = 0; i < map->n; ++i) {
9687 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9688 if (!map->p[i])
9689 goto error;
9691 map->dim = isl_space_flatten_domain(map->dim);
9692 if (!map->dim)
9693 goto error;
9695 return map;
9696 error:
9697 isl_map_free(map);
9698 return NULL;
9701 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9703 int i;
9705 if (!map)
9706 return NULL;
9708 if (!map->dim->nested[1])
9709 return map;
9711 map = isl_map_cow(map);
9712 if (!map)
9713 return NULL;
9715 for (i = 0; i < map->n; ++i) {
9716 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9717 if (!map->p[i])
9718 goto error;
9720 map->dim = isl_space_flatten_range(map->dim);
9721 if (!map->dim)
9722 goto error;
9724 return map;
9725 error:
9726 isl_map_free(map);
9727 return NULL;
9730 /* Reorder the dimensions of "bmap" according to the given dim_map
9731 * and set the dimension specification to "dim".
9733 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9734 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9736 isl_basic_map *res;
9737 unsigned flags;
9739 bmap = isl_basic_map_cow(bmap);
9740 if (!bmap || !dim || !dim_map)
9741 goto error;
9743 flags = bmap->flags;
9744 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
9745 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
9746 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
9747 res = isl_basic_map_alloc_space(dim,
9748 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9749 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9750 if (res)
9751 res->flags = flags;
9752 res = isl_basic_map_finalize(res);
9753 return res;
9754 error:
9755 free(dim_map);
9756 isl_basic_map_free(bmap);
9757 isl_space_free(dim);
9758 return NULL;
9761 /* Reorder the dimensions of "map" according to given reordering.
9763 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9764 __isl_take isl_reordering *r)
9766 int i;
9767 struct isl_dim_map *dim_map;
9769 map = isl_map_cow(map);
9770 dim_map = isl_dim_map_from_reordering(r);
9771 if (!map || !r || !dim_map)
9772 goto error;
9774 for (i = 0; i < map->n; ++i) {
9775 struct isl_dim_map *dim_map_i;
9777 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9779 map->p[i] = isl_basic_map_realign(map->p[i],
9780 isl_space_copy(r->dim), dim_map_i);
9782 if (!map->p[i])
9783 goto error;
9786 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9788 isl_reordering_free(r);
9789 free(dim_map);
9790 return map;
9791 error:
9792 free(dim_map);
9793 isl_map_free(map);
9794 isl_reordering_free(r);
9795 return NULL;
9798 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9799 __isl_take isl_reordering *r)
9801 return (isl_set *)isl_map_realign((isl_map *)set, r);
9804 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9805 __isl_take isl_space *model)
9807 isl_ctx *ctx;
9809 if (!map || !model)
9810 goto error;
9812 ctx = isl_space_get_ctx(model);
9813 if (!isl_space_has_named_params(model))
9814 isl_die(ctx, isl_error_invalid,
9815 "model has unnamed parameters", goto error);
9816 if (!isl_space_has_named_params(map->dim))
9817 isl_die(ctx, isl_error_invalid,
9818 "relation has unnamed parameters", goto error);
9819 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9820 isl_reordering *exp;
9822 model = isl_space_drop_dims(model, isl_dim_in,
9823 0, isl_space_dim(model, isl_dim_in));
9824 model = isl_space_drop_dims(model, isl_dim_out,
9825 0, isl_space_dim(model, isl_dim_out));
9826 exp = isl_parameter_alignment_reordering(map->dim, model);
9827 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9828 map = isl_map_realign(map, exp);
9831 isl_space_free(model);
9832 return map;
9833 error:
9834 isl_space_free(model);
9835 isl_map_free(map);
9836 return NULL;
9839 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9840 __isl_take isl_space *model)
9842 return isl_map_align_params(set, model);
9845 /* Align the parameters of "bmap" to those of "model", introducing
9846 * additional parameters if needed.
9848 __isl_give isl_basic_map *isl_basic_map_align_params(
9849 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
9851 isl_ctx *ctx;
9853 if (!bmap || !model)
9854 goto error;
9856 ctx = isl_space_get_ctx(model);
9857 if (!isl_space_has_named_params(model))
9858 isl_die(ctx, isl_error_invalid,
9859 "model has unnamed parameters", goto error);
9860 if (!isl_space_has_named_params(bmap->dim))
9861 isl_die(ctx, isl_error_invalid,
9862 "relation has unnamed parameters", goto error);
9863 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
9864 isl_reordering *exp;
9865 struct isl_dim_map *dim_map;
9867 model = isl_space_drop_dims(model, isl_dim_in,
9868 0, isl_space_dim(model, isl_dim_in));
9869 model = isl_space_drop_dims(model, isl_dim_out,
9870 0, isl_space_dim(model, isl_dim_out));
9871 exp = isl_parameter_alignment_reordering(bmap->dim, model);
9872 exp = isl_reordering_extend_space(exp,
9873 isl_basic_map_get_space(bmap));
9874 dim_map = isl_dim_map_from_reordering(exp);
9875 bmap = isl_basic_map_realign(bmap,
9876 exp ? isl_space_copy(exp->dim) : NULL,
9877 isl_dim_map_extend(dim_map, bmap));
9878 isl_reordering_free(exp);
9879 free(dim_map);
9882 isl_space_free(model);
9883 return bmap;
9884 error:
9885 isl_space_free(model);
9886 isl_basic_map_free(bmap);
9887 return NULL;
9890 /* Align the parameters of "bset" to those of "model", introducing
9891 * additional parameters if needed.
9893 __isl_give isl_basic_set *isl_basic_set_align_params(
9894 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
9896 return isl_basic_map_align_params(bset, model);
9899 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9900 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9901 enum isl_dim_type c2, enum isl_dim_type c3,
9902 enum isl_dim_type c4, enum isl_dim_type c5)
9904 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9905 struct isl_mat *mat;
9906 int i, j, k;
9907 int pos;
9909 if (!bmap)
9910 return NULL;
9911 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9912 isl_basic_map_total_dim(bmap) + 1);
9913 if (!mat)
9914 return NULL;
9915 for (i = 0; i < bmap->n_eq; ++i)
9916 for (j = 0, pos = 0; j < 5; ++j) {
9917 int off = isl_basic_map_offset(bmap, c[j]);
9918 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9919 isl_int_set(mat->row[i][pos],
9920 bmap->eq[i][off + k]);
9921 ++pos;
9925 return mat;
9928 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9929 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9930 enum isl_dim_type c2, enum isl_dim_type c3,
9931 enum isl_dim_type c4, enum isl_dim_type c5)
9933 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9934 struct isl_mat *mat;
9935 int i, j, k;
9936 int pos;
9938 if (!bmap)
9939 return NULL;
9940 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9941 isl_basic_map_total_dim(bmap) + 1);
9942 if (!mat)
9943 return NULL;
9944 for (i = 0; i < bmap->n_ineq; ++i)
9945 for (j = 0, pos = 0; j < 5; ++j) {
9946 int off = isl_basic_map_offset(bmap, c[j]);
9947 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9948 isl_int_set(mat->row[i][pos],
9949 bmap->ineq[i][off + k]);
9950 ++pos;
9954 return mat;
9957 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9958 __isl_take isl_space *dim,
9959 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9960 enum isl_dim_type c2, enum isl_dim_type c3,
9961 enum isl_dim_type c4, enum isl_dim_type c5)
9963 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9964 isl_basic_map *bmap;
9965 unsigned total;
9966 unsigned extra;
9967 int i, j, k, l;
9968 int pos;
9970 if (!dim || !eq || !ineq)
9971 goto error;
9973 if (eq->n_col != ineq->n_col)
9974 isl_die(dim->ctx, isl_error_invalid,
9975 "equalities and inequalities matrices should have "
9976 "same number of columns", goto error);
9978 total = 1 + isl_space_dim(dim, isl_dim_all);
9980 if (eq->n_col < total)
9981 isl_die(dim->ctx, isl_error_invalid,
9982 "number of columns too small", goto error);
9984 extra = eq->n_col - total;
9986 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9987 eq->n_row, ineq->n_row);
9988 if (!bmap)
9989 goto error;
9990 for (i = 0; i < extra; ++i) {
9991 k = isl_basic_map_alloc_div(bmap);
9992 if (k < 0)
9993 goto error;
9994 isl_int_set_si(bmap->div[k][0], 0);
9996 for (i = 0; i < eq->n_row; ++i) {
9997 l = isl_basic_map_alloc_equality(bmap);
9998 if (l < 0)
9999 goto error;
10000 for (j = 0, pos = 0; j < 5; ++j) {
10001 int off = isl_basic_map_offset(bmap, c[j]);
10002 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10003 isl_int_set(bmap->eq[l][off + k],
10004 eq->row[i][pos]);
10005 ++pos;
10009 for (i = 0; i < ineq->n_row; ++i) {
10010 l = isl_basic_map_alloc_inequality(bmap);
10011 if (l < 0)
10012 goto error;
10013 for (j = 0, pos = 0; j < 5; ++j) {
10014 int off = isl_basic_map_offset(bmap, c[j]);
10015 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10016 isl_int_set(bmap->ineq[l][off + k],
10017 ineq->row[i][pos]);
10018 ++pos;
10023 isl_space_free(dim);
10024 isl_mat_free(eq);
10025 isl_mat_free(ineq);
10027 return bmap;
10028 error:
10029 isl_space_free(dim);
10030 isl_mat_free(eq);
10031 isl_mat_free(ineq);
10032 return NULL;
10035 __isl_give isl_mat *isl_basic_set_equalities_matrix(
10036 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10037 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10039 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
10040 c1, c2, c3, c4, isl_dim_in);
10043 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
10044 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10045 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10047 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
10048 c1, c2, c3, c4, isl_dim_in);
10051 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
10052 __isl_take isl_space *dim,
10053 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10054 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10056 return (isl_basic_set*)
10057 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
10058 c1, c2, c3, c4, isl_dim_in);
10061 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
10063 if (!bmap)
10064 return -1;
10066 return isl_space_can_zip(bmap->dim);
10069 int isl_map_can_zip(__isl_keep isl_map *map)
10071 if (!map)
10072 return -1;
10074 return isl_space_can_zip(map->dim);
10077 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10078 * (A -> C) -> (B -> D).
10080 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10082 unsigned pos;
10083 unsigned n1;
10084 unsigned n2;
10086 if (!bmap)
10087 return NULL;
10089 if (!isl_basic_map_can_zip(bmap))
10090 isl_die(bmap->ctx, isl_error_invalid,
10091 "basic map cannot be zipped", goto error);
10092 pos = isl_basic_map_offset(bmap, isl_dim_in) +
10093 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10094 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10095 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10096 bmap = isl_basic_map_cow(bmap);
10097 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10098 if (!bmap)
10099 return NULL;
10100 bmap->dim = isl_space_zip(bmap->dim);
10101 if (!bmap->dim)
10102 goto error;
10103 return bmap;
10104 error:
10105 isl_basic_map_free(bmap);
10106 return NULL;
10109 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10110 * (A -> C) -> (B -> D).
10112 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
10114 int i;
10116 if (!map)
10117 return NULL;
10119 if (!isl_map_can_zip(map))
10120 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
10121 goto error);
10123 map = isl_map_cow(map);
10124 if (!map)
10125 return NULL;
10127 for (i = 0; i < map->n; ++i) {
10128 map->p[i] = isl_basic_map_zip(map->p[i]);
10129 if (!map->p[i])
10130 goto error;
10133 map->dim = isl_space_zip(map->dim);
10134 if (!map->dim)
10135 goto error;
10137 return map;
10138 error:
10139 isl_map_free(map);
10140 return NULL;
10143 /* Can we apply isl_basic_map_curry to "bmap"?
10144 * That is, does it have a nested relation in its domain?
10146 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
10148 if (!bmap)
10149 return -1;
10151 return isl_space_can_curry(bmap->dim);
10154 /* Can we apply isl_map_curry to "map"?
10155 * That is, does it have a nested relation in its domain?
10157 int isl_map_can_curry(__isl_keep isl_map *map)
10159 if (!map)
10160 return -1;
10162 return isl_space_can_curry(map->dim);
10165 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10166 * A -> (B -> C).
10168 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
10171 if (!bmap)
10172 return NULL;
10174 if (!isl_basic_map_can_curry(bmap))
10175 isl_die(bmap->ctx, isl_error_invalid,
10176 "basic map cannot be curried", goto error);
10177 bmap->dim = isl_space_curry(bmap->dim);
10178 if (!bmap->dim)
10179 goto error;
10180 return bmap;
10181 error:
10182 isl_basic_map_free(bmap);
10183 return NULL;
10186 /* Given a map (A -> B) -> C, return the corresponding map
10187 * A -> (B -> C).
10189 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
10191 int i;
10193 if (!map)
10194 return NULL;
10196 if (!isl_map_can_curry(map))
10197 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
10198 goto error);
10200 map = isl_map_cow(map);
10201 if (!map)
10202 return NULL;
10204 for (i = 0; i < map->n; ++i) {
10205 map->p[i] = isl_basic_map_curry(map->p[i]);
10206 if (!map->p[i])
10207 goto error;
10210 map->dim = isl_space_curry(map->dim);
10211 if (!map->dim)
10212 goto error;
10214 return map;
10215 error:
10216 isl_map_free(map);
10217 return NULL;
10220 /* Construct a basic map mapping the domain of the affine expression
10221 * to a one-dimensional range prescribed by the affine expression.
10223 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
10225 int k;
10226 int pos;
10227 isl_local_space *ls;
10228 isl_basic_map *bmap;
10230 if (!aff)
10231 return NULL;
10233 ls = isl_aff_get_local_space(aff);
10234 bmap = isl_basic_map_from_local_space(ls);
10235 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
10236 k = isl_basic_map_alloc_equality(bmap);
10237 if (k < 0)
10238 goto error;
10240 pos = isl_basic_map_offset(bmap, isl_dim_out);
10241 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
10242 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
10243 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
10244 aff->v->size - (pos + 1));
10246 isl_aff_free(aff);
10247 bmap = isl_basic_map_finalize(bmap);
10248 return bmap;
10249 error:
10250 isl_aff_free(aff);
10251 isl_basic_map_free(bmap);
10252 return NULL;
10255 /* Construct a map mapping the domain of the affine expression
10256 * to a one-dimensional range prescribed by the affine expression.
10258 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
10260 isl_basic_map *bmap;
10262 bmap = isl_basic_map_from_aff(aff);
10263 return isl_map_from_basic_map(bmap);
10266 /* Construct a basic map mapping the domain the multi-affine expression
10267 * to its range, with each dimension in the range equated to the
10268 * corresponding affine expression.
10270 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
10271 __isl_take isl_multi_aff *maff)
10273 int i;
10274 isl_space *space;
10275 isl_basic_map *bmap;
10277 if (!maff)
10278 return NULL;
10280 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
10281 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
10282 "invalid space", return isl_multi_aff_free(maff));
10284 space = isl_space_domain(isl_multi_aff_get_space(maff));
10285 bmap = isl_basic_map_universe(isl_space_from_domain(space));
10287 for (i = 0; i < maff->n; ++i) {
10288 isl_aff *aff;
10289 isl_basic_map *bmap_i;
10291 aff = isl_aff_copy(maff->p[i]);
10292 bmap_i = isl_basic_map_from_aff(aff);
10294 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10297 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
10299 isl_multi_aff_free(maff);
10300 return bmap;
10303 /* Construct a map mapping the domain the multi-affine expression
10304 * to its range, with each dimension in the range equated to the
10305 * corresponding affine expression.
10307 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
10309 isl_basic_map *bmap;
10311 bmap = isl_basic_map_from_multi_aff(maff);
10312 return isl_map_from_basic_map(bmap);
10315 /* Construct a basic map mapping a domain in the given space to
10316 * to an n-dimensional range, with n the number of elements in the list,
10317 * where each coordinate in the range is prescribed by the
10318 * corresponding affine expression.
10319 * The domains of all affine expressions in the list are assumed to match
10320 * domain_dim.
10322 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
10323 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
10325 int i;
10326 isl_space *dim;
10327 isl_basic_map *bmap;
10329 if (!list)
10330 return NULL;
10332 dim = isl_space_from_domain(domain_dim);
10333 bmap = isl_basic_map_universe(dim);
10335 for (i = 0; i < list->n; ++i) {
10336 isl_aff *aff;
10337 isl_basic_map *bmap_i;
10339 aff = isl_aff_copy(list->p[i]);
10340 bmap_i = isl_basic_map_from_aff(aff);
10342 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10345 isl_aff_list_free(list);
10346 return bmap;
10349 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
10350 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10352 return isl_map_equate(set, type1, pos1, type2, pos2);
10355 /* Construct a basic map where the given dimensions are equal to each other.
10357 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
10358 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10360 isl_basic_map *bmap = NULL;
10361 int i;
10363 if (!space)
10364 return NULL;
10366 if (pos1 >= isl_space_dim(space, type1))
10367 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10368 "index out of bounds", goto error);
10369 if (pos2 >= isl_space_dim(space, type2))
10370 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10371 "index out of bounds", goto error);
10373 if (type1 == type2 && pos1 == pos2)
10374 return isl_basic_map_universe(space);
10376 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
10377 i = isl_basic_map_alloc_equality(bmap);
10378 if (i < 0)
10379 goto error;
10380 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10381 pos1 += isl_basic_map_offset(bmap, type1);
10382 pos2 += isl_basic_map_offset(bmap, type2);
10383 isl_int_set_si(bmap->eq[i][pos1], -1);
10384 isl_int_set_si(bmap->eq[i][pos2], 1);
10385 bmap = isl_basic_map_finalize(bmap);
10386 isl_space_free(space);
10387 return bmap;
10388 error:
10389 isl_space_free(space);
10390 isl_basic_map_free(bmap);
10391 return NULL;
10394 /* Add a constraint imposing that the given two dimensions are equal.
10396 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
10397 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10399 isl_basic_map *eq;
10401 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
10403 bmap = isl_basic_map_intersect(bmap, eq);
10405 return bmap;
10408 /* Add a constraint imposing that the given two dimensions are equal.
10410 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
10411 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10413 isl_basic_map *bmap;
10415 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
10417 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10419 return map;
10422 /* Add a constraint imposing that the given two dimensions have opposite values.
10424 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
10425 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10427 isl_basic_map *bmap = NULL;
10428 int i;
10430 if (!map)
10431 return NULL;
10433 if (pos1 >= isl_map_dim(map, type1))
10434 isl_die(map->ctx, isl_error_invalid,
10435 "index out of bounds", goto error);
10436 if (pos2 >= isl_map_dim(map, type2))
10437 isl_die(map->ctx, isl_error_invalid,
10438 "index out of bounds", goto error);
10440 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
10441 i = isl_basic_map_alloc_equality(bmap);
10442 if (i < 0)
10443 goto error;
10444 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10445 pos1 += isl_basic_map_offset(bmap, type1);
10446 pos2 += isl_basic_map_offset(bmap, type2);
10447 isl_int_set_si(bmap->eq[i][pos1], 1);
10448 isl_int_set_si(bmap->eq[i][pos2], 1);
10449 bmap = isl_basic_map_finalize(bmap);
10451 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10453 return map;
10454 error:
10455 isl_basic_map_free(bmap);
10456 isl_map_free(map);
10457 return NULL;
10460 /* Add a constraint imposing that the value of the first dimension is
10461 * greater than that of the second.
10463 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
10464 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10466 isl_basic_map *bmap = NULL;
10467 int i;
10469 if (!map)
10470 return NULL;
10472 if (pos1 >= isl_map_dim(map, type1))
10473 isl_die(map->ctx, isl_error_invalid,
10474 "index out of bounds", goto error);
10475 if (pos2 >= isl_map_dim(map, type2))
10476 isl_die(map->ctx, isl_error_invalid,
10477 "index out of bounds", goto error);
10479 if (type1 == type2 && pos1 == pos2) {
10480 isl_space *space = isl_map_get_space(map);
10481 isl_map_free(map);
10482 return isl_map_empty(space);
10485 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 0, 1);
10486 i = isl_basic_map_alloc_inequality(bmap);
10487 if (i < 0)
10488 goto error;
10489 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
10490 pos1 += isl_basic_map_offset(bmap, type1);
10491 pos2 += isl_basic_map_offset(bmap, type2);
10492 isl_int_set_si(bmap->ineq[i][pos1], 1);
10493 isl_int_set_si(bmap->ineq[i][pos2], -1);
10494 isl_int_set_si(bmap->ineq[i][0], -1);
10495 bmap = isl_basic_map_finalize(bmap);
10497 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10499 return map;
10500 error:
10501 isl_basic_map_free(bmap);
10502 isl_map_free(map);
10503 return NULL;
10506 /* Add a constraint imposing that the value of the first dimension is
10507 * smaller than that of the second.
10509 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
10510 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10512 return isl_map_order_gt(map, type2, pos2, type1, pos1);
10515 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
10516 int pos)
10518 isl_aff *div;
10519 isl_local_space *ls;
10521 if (!bmap)
10522 return NULL;
10524 if (!isl_basic_map_divs_known(bmap))
10525 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
10526 "some divs are unknown", return NULL);
10528 ls = isl_basic_map_get_local_space(bmap);
10529 div = isl_local_space_get_div(ls, pos);
10530 isl_local_space_free(ls);
10532 return div;
10535 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
10536 int pos)
10538 return isl_basic_map_get_div(bset, pos);
10541 /* Plug in "subs" for dimension "type", "pos" of "bset".
10543 * Let i be the dimension to replace and let "subs" be of the form
10545 * f/d
10547 * Any integer division with a non-zero coefficient for i,
10549 * floor((a i + g)/m)
10551 * is replaced by
10553 * floor((a f + d g)/(m d))
10555 * Constraints of the form
10557 * a i + g
10559 * are replaced by
10561 * a f + d g
10563 __isl_give isl_basic_set *isl_basic_set_substitute(
10564 __isl_take isl_basic_set *bset,
10565 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10567 int i;
10568 isl_int v;
10569 isl_ctx *ctx;
10571 if (bset && isl_basic_set_plain_is_empty(bset))
10572 return bset;
10574 bset = isl_basic_set_cow(bset);
10575 if (!bset || !subs)
10576 goto error;
10578 ctx = isl_basic_set_get_ctx(bset);
10579 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
10580 isl_die(ctx, isl_error_invalid,
10581 "spaces don't match", goto error);
10582 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
10583 isl_die(ctx, isl_error_unsupported,
10584 "cannot handle divs yet", goto error);
10586 pos += isl_basic_set_offset(bset, type);
10588 isl_int_init(v);
10590 for (i = 0; i < bset->n_eq; ++i) {
10591 if (isl_int_is_zero(bset->eq[i][pos]))
10592 continue;
10593 isl_int_set(v, bset->eq[i][pos]);
10594 isl_int_set_si(bset->eq[i][pos], 0);
10595 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
10596 v, subs->v->el + 1, subs->v->size - 1);
10599 for (i = 0; i < bset->n_ineq; ++i) {
10600 if (isl_int_is_zero(bset->ineq[i][pos]))
10601 continue;
10602 isl_int_set(v, bset->ineq[i][pos]);
10603 isl_int_set_si(bset->ineq[i][pos], 0);
10604 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
10605 v, subs->v->el + 1, subs->v->size - 1);
10608 for (i = 0; i < bset->n_div; ++i) {
10609 if (isl_int_is_zero(bset->div[i][1 + pos]))
10610 continue;
10611 isl_int_set(v, bset->div[i][1 + pos]);
10612 isl_int_set_si(bset->div[i][1 + pos], 0);
10613 isl_seq_combine(bset->div[i] + 1,
10614 subs->v->el[0], bset->div[i] + 1,
10615 v, subs->v->el + 1, subs->v->size - 1);
10616 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
10619 isl_int_clear(v);
10621 bset = isl_basic_set_simplify(bset);
10622 return isl_basic_set_finalize(bset);
10623 error:
10624 isl_basic_set_free(bset);
10625 return NULL;
10628 /* Plug in "subs" for dimension "type", "pos" of "set".
10630 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
10631 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10633 int i;
10635 if (set && isl_set_plain_is_empty(set))
10636 return set;
10638 set = isl_set_cow(set);
10639 if (!set || !subs)
10640 goto error;
10642 for (i = set->n - 1; i >= 0; --i) {
10643 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
10644 if (remove_if_empty(set, i) < 0)
10645 goto error;
10648 return set;
10649 error:
10650 isl_set_free(set);
10651 return NULL;