isl_multi_*_gist: add missing isl_multi_*_cow
[isl.git] / isl_map.c
blobbbb86d8452d551f8923ed0540a09a5477ee9d246
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 MIT 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 /* Does the input or output tuple have a name?
413 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
415 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
418 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
419 enum isl_dim_type type)
421 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
424 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
425 const char *s)
427 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
430 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
431 enum isl_dim_type type, __isl_take isl_id *id)
433 map = isl_map_cow(map);
434 if (!map)
435 return isl_id_free(id);
437 map->dim = isl_space_set_tuple_id(map->dim, type, id);
439 return isl_map_reset_space(map, isl_space_copy(map->dim));
442 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
443 __isl_take isl_id *id)
445 return isl_map_set_tuple_id(set, isl_dim_set, id);
448 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
449 enum isl_dim_type type)
451 map = isl_map_cow(map);
452 if (!map)
453 return NULL;
455 map->dim = isl_space_reset_tuple_id(map->dim, type);
457 return isl_map_reset_space(map, isl_space_copy(map->dim));
460 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
462 return isl_map_reset_tuple_id(set, isl_dim_set);
465 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
467 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
470 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
471 enum isl_dim_type type)
473 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
476 int isl_set_has_tuple_id(__isl_keep isl_set *set)
478 return isl_map_has_tuple_id(set, isl_dim_set);
481 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
483 return isl_map_get_tuple_id(set, isl_dim_set);
486 /* Does the set tuple have a name?
488 int isl_set_has_tuple_name(__isl_keep isl_set *set)
490 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
494 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
496 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
499 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
501 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
504 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
505 enum isl_dim_type type, unsigned pos)
507 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
510 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
511 enum isl_dim_type type, unsigned pos)
513 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
516 /* Does the given dimension have a name?
518 int isl_map_has_dim_name(__isl_keep isl_map *map,
519 enum isl_dim_type type, unsigned pos)
521 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
524 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
525 enum isl_dim_type type, unsigned pos)
527 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
530 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
531 enum isl_dim_type type, unsigned pos)
533 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
536 /* Does the given dimension have a name?
538 int isl_set_has_dim_name(__isl_keep isl_set *set,
539 enum isl_dim_type type, unsigned pos)
541 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
544 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
545 __isl_take isl_basic_map *bmap,
546 enum isl_dim_type type, unsigned pos, const char *s)
548 bmap = isl_basic_map_cow(bmap);
549 if (!bmap)
550 return NULL;
551 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
552 if (!bmap->dim)
553 goto error;
554 return isl_basic_map_finalize(bmap);
555 error:
556 isl_basic_map_free(bmap);
557 return NULL;
560 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
561 enum isl_dim_type type, unsigned pos, const char *s)
563 int i;
565 map = isl_map_cow(map);
566 if (!map)
567 return NULL;
569 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
570 if (!map->dim)
571 goto error;
573 for (i = 0; i < map->n; ++i) {
574 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
575 if (!map->p[i])
576 goto error;
579 return map;
580 error:
581 isl_map_free(map);
582 return NULL;
585 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
586 __isl_take isl_basic_set *bset,
587 enum isl_dim_type type, unsigned pos, const char *s)
589 return (isl_basic_set *)isl_basic_map_set_dim_name(
590 (isl_basic_map *)bset, type, pos, s);
593 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
594 enum isl_dim_type type, unsigned pos, const char *s)
596 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
599 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
600 enum isl_dim_type type, unsigned pos)
602 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
605 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
606 enum isl_dim_type type, unsigned pos)
608 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
611 int isl_map_has_dim_id(__isl_keep isl_map *map,
612 enum isl_dim_type type, unsigned pos)
614 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
617 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
618 enum isl_dim_type type, unsigned pos)
620 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
623 int isl_set_has_dim_id(__isl_keep isl_set *set,
624 enum isl_dim_type type, unsigned pos)
626 return isl_map_has_dim_id(set, type, pos);
629 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
630 enum isl_dim_type type, unsigned pos)
632 return isl_map_get_dim_id(set, type, pos);
635 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
636 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
638 map = isl_map_cow(map);
639 if (!map)
640 return isl_id_free(id);
642 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
644 return isl_map_reset_space(map, isl_space_copy(map->dim));
647 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
648 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
650 return isl_map_set_dim_id(set, type, pos, id);
653 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
654 __isl_keep isl_id *id)
656 if (!map)
657 return -1;
658 return isl_space_find_dim_by_id(map->dim, type, id);
661 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
662 __isl_keep isl_id *id)
664 return isl_map_find_dim_by_id(set, type, id);
667 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
668 const char *name)
670 if (!map)
671 return -1;
672 return isl_space_find_dim_by_name(map->dim, type, name);
675 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
676 const char *name)
678 return isl_map_find_dim_by_name(set, type, name);
681 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
683 if (!bmap)
684 return -1;
685 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
688 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
690 return isl_basic_map_is_rational(bset);
693 /* Does "bmap" contain any rational points?
695 * If "bmap" has an equality for each dimension, equating the dimension
696 * to an integer constant, then it has no rational points, even if it
697 * is marked as rational.
699 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
701 int has_rational = 1;
702 unsigned total;
704 if (!bmap)
705 return -1;
706 if (isl_basic_map_plain_is_empty(bmap))
707 return 0;
708 if (!isl_basic_map_is_rational(bmap))
709 return 0;
710 bmap = isl_basic_map_copy(bmap);
711 bmap = isl_basic_map_implicit_equalities(bmap);
712 if (!bmap)
713 return -1;
714 total = isl_basic_map_total_dim(bmap);
715 if (bmap->n_eq == total) {
716 int i, j;
717 for (i = 0; i < bmap->n_eq; ++i) {
718 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
719 if (j < 0)
720 break;
721 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
722 !isl_int_is_negone(bmap->eq[i][1 + j]))
723 break;
724 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
725 total - j - 1);
726 if (j >= 0)
727 break;
729 if (i == bmap->n_eq)
730 has_rational = 0;
732 isl_basic_map_free(bmap);
734 return has_rational;
737 /* Does "map" contain any rational points?
739 int isl_map_has_rational(__isl_keep isl_map *map)
741 int i;
742 int has_rational;
744 if (!map)
745 return -1;
746 for (i = 0; i < map->n; ++i) {
747 has_rational = isl_basic_map_has_rational(map->p[i]);
748 if (has_rational < 0)
749 return -1;
750 if (has_rational)
751 return 1;
753 return 0;
756 /* Does "set" contain any rational points?
758 int isl_set_has_rational(__isl_keep isl_set *set)
760 return isl_map_has_rational(set);
763 /* Is this basic set a parameter domain?
765 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
767 if (!bset)
768 return -1;
769 return isl_space_is_params(bset->dim);
772 /* Is this set a parameter domain?
774 int isl_set_is_params(__isl_keep isl_set *set)
776 if (!set)
777 return -1;
778 return isl_space_is_params(set->dim);
781 /* Is this map actually a parameter domain?
782 * Users should never call this function. Outside of isl,
783 * a map can never be a parameter domain.
785 int isl_map_is_params(__isl_keep isl_map *map)
787 if (!map)
788 return -1;
789 return isl_space_is_params(map->dim);
792 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
793 struct isl_basic_map *bmap, unsigned extra,
794 unsigned n_eq, unsigned n_ineq)
796 int i;
797 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
799 bmap->ctx = ctx;
800 isl_ctx_ref(ctx);
802 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
803 if (isl_blk_is_error(bmap->block))
804 goto error;
806 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
807 if (!bmap->ineq)
808 goto error;
810 if (extra == 0) {
811 bmap->block2 = isl_blk_empty();
812 bmap->div = NULL;
813 } else {
814 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
815 if (isl_blk_is_error(bmap->block2))
816 goto error;
818 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
819 if (!bmap->div)
820 goto error;
823 for (i = 0; i < n_ineq + n_eq; ++i)
824 bmap->ineq[i] = bmap->block.data + i * row_size;
826 for (i = 0; i < extra; ++i)
827 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
829 bmap->ref = 1;
830 bmap->flags = 0;
831 bmap->c_size = n_eq + n_ineq;
832 bmap->eq = bmap->ineq + n_ineq;
833 bmap->extra = extra;
834 bmap->n_eq = 0;
835 bmap->n_ineq = 0;
836 bmap->n_div = 0;
837 bmap->sample = NULL;
839 return bmap;
840 error:
841 isl_basic_map_free(bmap);
842 return NULL;
845 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
846 unsigned nparam, unsigned dim, unsigned extra,
847 unsigned n_eq, unsigned n_ineq)
849 struct isl_basic_map *bmap;
850 isl_space *space;
852 space = isl_space_set_alloc(ctx, nparam, dim);
853 if (!space)
854 return NULL;
856 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
857 return (struct isl_basic_set *)bmap;
860 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
861 unsigned extra, unsigned n_eq, unsigned n_ineq)
863 struct isl_basic_map *bmap;
864 if (!dim)
865 return NULL;
866 isl_assert(dim->ctx, dim->n_in == 0, goto error);
867 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
868 return (struct isl_basic_set *)bmap;
869 error:
870 isl_space_free(dim);
871 return NULL;
874 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
875 unsigned extra, unsigned n_eq, unsigned n_ineq)
877 struct isl_basic_map *bmap;
879 if (!dim)
880 return NULL;
881 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
882 if (!bmap)
883 goto error;
884 bmap->dim = dim;
886 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
887 error:
888 isl_space_free(dim);
889 return NULL;
892 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
893 unsigned nparam, unsigned in, unsigned out, unsigned extra,
894 unsigned n_eq, unsigned n_ineq)
896 struct isl_basic_map *bmap;
897 isl_space *dim;
899 dim = isl_space_alloc(ctx, nparam, in, out);
900 if (!dim)
901 return NULL;
903 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
904 return bmap;
907 static void dup_constraints(
908 struct isl_basic_map *dst, struct isl_basic_map *src)
910 int i;
911 unsigned total = isl_basic_map_total_dim(src);
913 for (i = 0; i < src->n_eq; ++i) {
914 int j = isl_basic_map_alloc_equality(dst);
915 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
918 for (i = 0; i < src->n_ineq; ++i) {
919 int j = isl_basic_map_alloc_inequality(dst);
920 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
923 for (i = 0; i < src->n_div; ++i) {
924 int j = isl_basic_map_alloc_div(dst);
925 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
927 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
930 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
932 struct isl_basic_map *dup;
934 if (!bmap)
935 return NULL;
936 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
937 bmap->n_div, bmap->n_eq, bmap->n_ineq);
938 if (!dup)
939 return NULL;
940 dup_constraints(dup, bmap);
941 dup->flags = bmap->flags;
942 dup->sample = isl_vec_copy(bmap->sample);
943 return dup;
946 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
948 struct isl_basic_map *dup;
950 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
951 return (struct isl_basic_set *)dup;
954 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
956 if (!bset)
957 return NULL;
959 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
960 bset->ref++;
961 return bset;
963 return isl_basic_set_dup(bset);
966 struct isl_set *isl_set_copy(struct isl_set *set)
968 if (!set)
969 return NULL;
971 set->ref++;
972 return set;
975 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
977 if (!bmap)
978 return NULL;
980 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
981 bmap->ref++;
982 return bmap;
984 bmap = isl_basic_map_dup(bmap);
985 if (bmap)
986 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
987 return bmap;
990 struct isl_map *isl_map_copy(struct isl_map *map)
992 if (!map)
993 return NULL;
995 map->ref++;
996 return map;
999 void *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1001 if (!bmap)
1002 return NULL;
1004 if (--bmap->ref > 0)
1005 return NULL;
1007 isl_ctx_deref(bmap->ctx);
1008 free(bmap->div);
1009 isl_blk_free(bmap->ctx, bmap->block2);
1010 free(bmap->ineq);
1011 isl_blk_free(bmap->ctx, bmap->block);
1012 isl_vec_free(bmap->sample);
1013 isl_space_free(bmap->dim);
1014 free(bmap);
1016 return NULL;
1019 void *isl_basic_set_free(struct isl_basic_set *bset)
1021 return isl_basic_map_free((struct isl_basic_map *)bset);
1024 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1026 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1029 __isl_give isl_map *isl_map_align_params_map_map_and(
1030 __isl_take isl_map *map1, __isl_take isl_map *map2,
1031 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1032 __isl_take isl_map *map2))
1034 if (!map1 || !map2)
1035 goto error;
1036 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1037 return fn(map1, map2);
1038 if (!isl_space_has_named_params(map1->dim) ||
1039 !isl_space_has_named_params(map2->dim))
1040 isl_die(map1->ctx, isl_error_invalid,
1041 "unaligned unnamed parameters", goto error);
1042 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1043 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1044 return fn(map1, map2);
1045 error:
1046 isl_map_free(map1);
1047 isl_map_free(map2);
1048 return NULL;
1051 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1052 __isl_keep isl_map *map2,
1053 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1055 int r;
1057 if (!map1 || !map2)
1058 return -1;
1059 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1060 return fn(map1, map2);
1061 if (!isl_space_has_named_params(map1->dim) ||
1062 !isl_space_has_named_params(map2->dim))
1063 isl_die(map1->ctx, isl_error_invalid,
1064 "unaligned unnamed parameters", return -1);
1065 map1 = isl_map_copy(map1);
1066 map2 = isl_map_copy(map2);
1067 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1068 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1069 r = fn(map1, map2);
1070 isl_map_free(map1);
1071 isl_map_free(map2);
1072 return r;
1075 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1077 struct isl_ctx *ctx;
1078 if (!bmap)
1079 return -1;
1080 ctx = bmap->ctx;
1081 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1082 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1083 return -1);
1084 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1085 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1086 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1087 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1088 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1089 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1090 isl_int *t;
1091 int j = isl_basic_map_alloc_inequality(bmap);
1092 if (j < 0)
1093 return -1;
1094 t = bmap->ineq[j];
1095 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1096 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1097 bmap->eq[-1] = t;
1098 bmap->n_eq++;
1099 bmap->n_ineq--;
1100 bmap->eq--;
1101 return 0;
1103 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1104 bmap->extra - bmap->n_div);
1105 return bmap->n_eq++;
1108 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1110 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1113 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1115 if (!bmap)
1116 return -1;
1117 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1118 bmap->n_eq -= n;
1119 return 0;
1122 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1124 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1127 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1129 isl_int *t;
1130 if (!bmap)
1131 return -1;
1132 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1134 if (pos != bmap->n_eq - 1) {
1135 t = bmap->eq[pos];
1136 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1137 bmap->eq[bmap->n_eq - 1] = t;
1139 bmap->n_eq--;
1140 return 0;
1143 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1145 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1148 /* Turn inequality "pos" of "bmap" into an equality.
1150 * In particular, we move the inequality in front of the equalities
1151 * and move the last inequality in the position of the moved inequality.
1152 * Note that isl_tab_make_equalities_explicit depends on this particular
1153 * change in the ordering of the constraints.
1155 void isl_basic_map_inequality_to_equality(
1156 struct isl_basic_map *bmap, unsigned pos)
1158 isl_int *t;
1160 t = bmap->ineq[pos];
1161 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1162 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1163 bmap->eq[-1] = t;
1164 bmap->n_eq++;
1165 bmap->n_ineq--;
1166 bmap->eq--;
1167 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1168 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1169 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1170 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1173 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1175 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1178 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1180 struct isl_ctx *ctx;
1181 if (!bmap)
1182 return -1;
1183 ctx = bmap->ctx;
1184 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1185 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1186 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1187 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1188 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1189 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1190 1 + isl_basic_map_total_dim(bmap),
1191 bmap->extra - bmap->n_div);
1192 return bmap->n_ineq++;
1195 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1197 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1200 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1202 if (!bmap)
1203 return -1;
1204 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1205 bmap->n_ineq -= n;
1206 return 0;
1209 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1211 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1214 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1216 isl_int *t;
1217 if (!bmap)
1218 return -1;
1219 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1221 if (pos != bmap->n_ineq - 1) {
1222 t = bmap->ineq[pos];
1223 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1224 bmap->ineq[bmap->n_ineq - 1] = t;
1225 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1227 bmap->n_ineq--;
1228 return 0;
1231 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1233 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1236 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1237 isl_int *eq)
1239 int k;
1241 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1242 if (!bmap)
1243 return NULL;
1244 k = isl_basic_map_alloc_equality(bmap);
1245 if (k < 0)
1246 goto error;
1247 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1248 return bmap;
1249 error:
1250 isl_basic_map_free(bmap);
1251 return NULL;
1254 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1255 isl_int *eq)
1257 return (isl_basic_set *)
1258 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1261 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1262 isl_int *ineq)
1264 int k;
1266 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1267 if (!bmap)
1268 return NULL;
1269 k = isl_basic_map_alloc_inequality(bmap);
1270 if (k < 0)
1271 goto error;
1272 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1273 return bmap;
1274 error:
1275 isl_basic_map_free(bmap);
1276 return NULL;
1279 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1280 isl_int *ineq)
1282 return (isl_basic_set *)
1283 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1286 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1288 if (!bmap)
1289 return -1;
1290 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1291 isl_seq_clr(bmap->div[bmap->n_div] +
1292 1 + 1 + isl_basic_map_total_dim(bmap),
1293 bmap->extra - bmap->n_div);
1294 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1295 return bmap->n_div++;
1298 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1300 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1303 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1305 if (!bmap)
1306 return -1;
1307 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1308 bmap->n_div -= n;
1309 return 0;
1312 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1314 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1317 /* Copy constraint from src to dst, putting the vars of src at offset
1318 * dim_off in dst and the divs of src at offset div_off in dst.
1319 * If both sets are actually map, then dim_off applies to the input
1320 * variables.
1322 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1323 struct isl_basic_map *src_map, isl_int *src,
1324 unsigned in_off, unsigned out_off, unsigned div_off)
1326 unsigned src_nparam = isl_basic_map_n_param(src_map);
1327 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1328 unsigned src_in = isl_basic_map_n_in(src_map);
1329 unsigned dst_in = isl_basic_map_n_in(dst_map);
1330 unsigned src_out = isl_basic_map_n_out(src_map);
1331 unsigned dst_out = isl_basic_map_n_out(dst_map);
1332 isl_int_set(dst[0], src[0]);
1333 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1334 if (dst_nparam > src_nparam)
1335 isl_seq_clr(dst+1+src_nparam,
1336 dst_nparam - src_nparam);
1337 isl_seq_clr(dst+1+dst_nparam, in_off);
1338 isl_seq_cpy(dst+1+dst_nparam+in_off,
1339 src+1+src_nparam,
1340 isl_min(dst_in-in_off, src_in));
1341 if (dst_in-in_off > src_in)
1342 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1343 dst_in - in_off - src_in);
1344 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1345 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1346 src+1+src_nparam+src_in,
1347 isl_min(dst_out-out_off, src_out));
1348 if (dst_out-out_off > src_out)
1349 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1350 dst_out - out_off - src_out);
1351 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1352 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1353 src+1+src_nparam+src_in+src_out,
1354 isl_min(dst_map->extra-div_off, src_map->n_div));
1355 if (dst_map->n_div-div_off > src_map->n_div)
1356 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1357 div_off+src_map->n_div,
1358 dst_map->n_div - div_off - src_map->n_div);
1361 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1362 struct isl_basic_map *src_map, isl_int *src,
1363 unsigned in_off, unsigned out_off, unsigned div_off)
1365 isl_int_set(dst[0], src[0]);
1366 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1369 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1370 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1372 int i;
1373 unsigned div_off;
1375 if (!bmap1 || !bmap2)
1376 goto error;
1378 div_off = bmap1->n_div;
1380 for (i = 0; i < bmap2->n_eq; ++i) {
1381 int i1 = isl_basic_map_alloc_equality(bmap1);
1382 if (i1 < 0)
1383 goto error;
1384 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1385 i_pos, o_pos, div_off);
1388 for (i = 0; i < bmap2->n_ineq; ++i) {
1389 int i1 = isl_basic_map_alloc_inequality(bmap1);
1390 if (i1 < 0)
1391 goto error;
1392 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1393 i_pos, o_pos, div_off);
1396 for (i = 0; i < bmap2->n_div; ++i) {
1397 int i1 = isl_basic_map_alloc_div(bmap1);
1398 if (i1 < 0)
1399 goto error;
1400 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1401 i_pos, o_pos, div_off);
1404 isl_basic_map_free(bmap2);
1406 return bmap1;
1408 error:
1409 isl_basic_map_free(bmap1);
1410 isl_basic_map_free(bmap2);
1411 return NULL;
1414 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1415 struct isl_basic_set *bset2, unsigned pos)
1417 return (struct isl_basic_set *)
1418 add_constraints((struct isl_basic_map *)bset1,
1419 (struct isl_basic_map *)bset2, 0, pos);
1422 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1423 __isl_take isl_space *dim, unsigned extra,
1424 unsigned n_eq, unsigned n_ineq)
1426 struct isl_basic_map *ext;
1427 unsigned flags;
1428 int dims_ok;
1430 if (!dim)
1431 goto error;
1433 if (!base)
1434 goto error;
1436 dims_ok = isl_space_is_equal(base->dim, dim) &&
1437 base->extra >= base->n_div + extra;
1439 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1440 room_for_ineq(base, n_ineq)) {
1441 isl_space_free(dim);
1442 return base;
1445 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1446 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1447 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1448 extra += base->extra;
1449 n_eq += base->n_eq;
1450 n_ineq += base->n_ineq;
1452 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1453 dim = NULL;
1454 if (!ext)
1455 goto error;
1457 if (dims_ok)
1458 ext->sample = isl_vec_copy(base->sample);
1459 flags = base->flags;
1460 ext = add_constraints(ext, base, 0, 0);
1461 if (ext) {
1462 ext->flags = flags;
1463 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1466 return ext;
1468 error:
1469 isl_space_free(dim);
1470 isl_basic_map_free(base);
1471 return NULL;
1474 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1475 __isl_take isl_space *dim, unsigned extra,
1476 unsigned n_eq, unsigned n_ineq)
1478 return (struct isl_basic_set *)
1479 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1480 extra, n_eq, n_ineq);
1483 struct isl_basic_map *isl_basic_map_extend_constraints(
1484 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1486 if (!base)
1487 return NULL;
1488 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1489 0, n_eq, n_ineq);
1492 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1493 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1494 unsigned n_eq, unsigned n_ineq)
1496 struct isl_basic_map *bmap;
1497 isl_space *dim;
1499 if (!base)
1500 return NULL;
1501 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1502 if (!dim)
1503 goto error;
1505 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1506 return bmap;
1507 error:
1508 isl_basic_map_free(base);
1509 return NULL;
1512 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1513 unsigned nparam, unsigned dim, unsigned extra,
1514 unsigned n_eq, unsigned n_ineq)
1516 return (struct isl_basic_set *)
1517 isl_basic_map_extend((struct isl_basic_map *)base,
1518 nparam, 0, dim, extra, n_eq, n_ineq);
1521 struct isl_basic_set *isl_basic_set_extend_constraints(
1522 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1524 return (struct isl_basic_set *)
1525 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1526 n_eq, n_ineq);
1529 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1531 return (struct isl_basic_set *)
1532 isl_basic_map_cow((struct isl_basic_map *)bset);
1535 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1537 if (!bmap)
1538 return NULL;
1540 if (bmap->ref > 1) {
1541 bmap->ref--;
1542 bmap = isl_basic_map_dup(bmap);
1544 if (bmap)
1545 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1546 return bmap;
1549 struct isl_set *isl_set_cow(struct isl_set *set)
1551 if (!set)
1552 return NULL;
1554 if (set->ref == 1)
1555 return set;
1556 set->ref--;
1557 return isl_set_dup(set);
1560 struct isl_map *isl_map_cow(struct isl_map *map)
1562 if (!map)
1563 return NULL;
1565 if (map->ref == 1)
1566 return map;
1567 map->ref--;
1568 return isl_map_dup(map);
1571 static void swap_vars(struct isl_blk blk, isl_int *a,
1572 unsigned a_len, unsigned b_len)
1574 isl_seq_cpy(blk.data, a+a_len, b_len);
1575 isl_seq_cpy(blk.data+b_len, a, a_len);
1576 isl_seq_cpy(a, blk.data, b_len+a_len);
1579 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1580 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1582 int i;
1583 struct isl_blk blk;
1585 if (!bmap)
1586 goto error;
1588 isl_assert(bmap->ctx,
1589 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1591 if (n1 == 0 || n2 == 0)
1592 return bmap;
1594 bmap = isl_basic_map_cow(bmap);
1595 if (!bmap)
1596 return NULL;
1598 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1599 if (isl_blk_is_error(blk))
1600 goto error;
1602 for (i = 0; i < bmap->n_eq; ++i)
1603 swap_vars(blk,
1604 bmap->eq[i] + pos, n1, n2);
1606 for (i = 0; i < bmap->n_ineq; ++i)
1607 swap_vars(blk,
1608 bmap->ineq[i] + pos, n1, n2);
1610 for (i = 0; i < bmap->n_div; ++i)
1611 swap_vars(blk,
1612 bmap->div[i]+1 + pos, n1, n2);
1614 isl_blk_free(bmap->ctx, blk);
1616 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1617 bmap = isl_basic_map_gauss(bmap, NULL);
1618 return isl_basic_map_finalize(bmap);
1619 error:
1620 isl_basic_map_free(bmap);
1621 return NULL;
1624 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1625 __isl_take isl_basic_set *bset, unsigned n)
1627 unsigned dim;
1628 unsigned nparam;
1630 if (!bset)
1631 return NULL;
1633 nparam = isl_basic_set_n_param(bset);
1634 dim = isl_basic_set_n_dim(bset);
1635 isl_assert(bset->ctx, n <= dim, goto error);
1637 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1638 error:
1639 isl_basic_set_free(bset);
1640 return NULL;
1643 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1645 int i = 0;
1646 unsigned total;
1647 if (!bmap)
1648 goto error;
1649 total = isl_basic_map_total_dim(bmap);
1650 isl_basic_map_free_div(bmap, bmap->n_div);
1651 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1652 if (bmap->n_eq > 0)
1653 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1654 else {
1655 i = isl_basic_map_alloc_equality(bmap);
1656 if (i < 0)
1657 goto error;
1659 isl_int_set_si(bmap->eq[i][0], 1);
1660 isl_seq_clr(bmap->eq[i]+1, total);
1661 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1662 isl_vec_free(bmap->sample);
1663 bmap->sample = NULL;
1664 return isl_basic_map_finalize(bmap);
1665 error:
1666 isl_basic_map_free(bmap);
1667 return NULL;
1670 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1672 return (struct isl_basic_set *)
1673 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1676 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1678 int i;
1679 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1680 isl_int *t = bmap->div[a];
1681 bmap->div[a] = bmap->div[b];
1682 bmap->div[b] = t;
1684 for (i = 0; i < bmap->n_eq; ++i)
1685 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1687 for (i = 0; i < bmap->n_ineq; ++i)
1688 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1690 for (i = 0; i < bmap->n_div; ++i)
1691 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1692 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1695 /* Eliminate the specified n dimensions starting at first from the
1696 * constraints, without removing the dimensions from the space.
1697 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1699 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1700 enum isl_dim_type type, unsigned first, unsigned n)
1702 int i;
1704 if (!map)
1705 return NULL;
1706 if (n == 0)
1707 return map;
1709 if (first + n > isl_map_dim(map, type) || first + n < first)
1710 isl_die(map->ctx, isl_error_invalid,
1711 "index out of bounds", goto error);
1713 map = isl_map_cow(map);
1714 if (!map)
1715 return NULL;
1717 for (i = 0; i < map->n; ++i) {
1718 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1719 if (!map->p[i])
1720 goto error;
1722 return map;
1723 error:
1724 isl_map_free(map);
1725 return NULL;
1728 /* Eliminate the specified n dimensions starting at first from the
1729 * constraints, without removing the dimensions from the space.
1730 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1732 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1733 enum isl_dim_type type, unsigned first, unsigned n)
1735 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1738 /* Eliminate the specified n dimensions starting at first from the
1739 * constraints, without removing the dimensions from the space.
1740 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1742 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1743 unsigned first, unsigned n)
1745 return isl_set_eliminate(set, isl_dim_set, first, n);
1748 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1749 __isl_take isl_basic_map *bmap)
1751 if (!bmap)
1752 return NULL;
1753 bmap = isl_basic_map_eliminate_vars(bmap,
1754 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1755 if (!bmap)
1756 return NULL;
1757 bmap->n_div = 0;
1758 return isl_basic_map_finalize(bmap);
1761 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1762 __isl_take isl_basic_set *bset)
1764 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1765 (struct isl_basic_map *)bset);
1768 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1770 int i;
1772 if (!map)
1773 return NULL;
1774 if (map->n == 0)
1775 return map;
1777 map = isl_map_cow(map);
1778 if (!map)
1779 return NULL;
1781 for (i = 0; i < map->n; ++i) {
1782 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1783 if (!map->p[i])
1784 goto error;
1786 return map;
1787 error:
1788 isl_map_free(map);
1789 return NULL;
1792 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1794 return isl_map_remove_divs(set);
1797 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1798 enum isl_dim_type type, unsigned first, unsigned n)
1800 if (!bmap)
1801 return NULL;
1802 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1803 goto error);
1804 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1805 return bmap;
1806 bmap = isl_basic_map_eliminate_vars(bmap,
1807 isl_basic_map_offset(bmap, type) - 1 + first, n);
1808 if (!bmap)
1809 return bmap;
1810 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1811 return bmap;
1812 bmap = isl_basic_map_drop(bmap, type, first, n);
1813 return bmap;
1814 error:
1815 isl_basic_map_free(bmap);
1816 return NULL;
1819 /* Return true if the definition of the given div (recursively) involves
1820 * any of the given variables.
1822 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1823 unsigned first, unsigned n)
1825 int i;
1826 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1828 if (isl_int_is_zero(bmap->div[div][0]))
1829 return 0;
1830 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1831 return 1;
1833 for (i = bmap->n_div - 1; i >= 0; --i) {
1834 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1835 continue;
1836 if (div_involves_vars(bmap, i, first, n))
1837 return 1;
1840 return 0;
1843 /* Try and add a lower and/or upper bound on "div" to "bmap"
1844 * based on inequality "i".
1845 * "total" is the total number of variables (excluding the divs).
1846 * "v" is a temporary object that can be used during the calculations.
1847 * If "lb" is set, then a lower bound should be constructed.
1848 * If "ub" is set, then an upper bound should be constructed.
1850 * The calling function has already checked that the inequality does not
1851 * reference "div", but we still need to check that the inequality is
1852 * of the right form. We'll consider the case where we want to construct
1853 * a lower bound. The construction of upper bounds is similar.
1855 * Let "div" be of the form
1857 * q = floor((a + f(x))/d)
1859 * We essentially check if constraint "i" is of the form
1861 * b + f(x) >= 0
1863 * so that we can use it to derive a lower bound on "div".
1864 * However, we allow a slightly more general form
1866 * b + g(x) >= 0
1868 * with the condition that the coefficients of g(x) - f(x) are all
1869 * divisible by d.
1870 * Rewriting this constraint as
1872 * 0 >= -b - g(x)
1874 * adding a + f(x) to both sides and dividing by d, we obtain
1876 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1878 * Taking the floor on both sides, we obtain
1880 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1882 * or
1884 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1886 * In the case of an upper bound, we construct the constraint
1888 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1891 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1892 __isl_take isl_basic_map *bmap, int div, int i,
1893 unsigned total, isl_int v, int lb, int ub)
1895 int j;
1897 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1898 if (lb) {
1899 isl_int_sub(v, bmap->ineq[i][1 + j],
1900 bmap->div[div][1 + 1 + j]);
1901 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1903 if (ub) {
1904 isl_int_add(v, bmap->ineq[i][1 + j],
1905 bmap->div[div][1 + 1 + j]);
1906 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1909 if (!lb && !ub)
1910 return bmap;
1912 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1913 if (lb) {
1914 int k = isl_basic_map_alloc_inequality(bmap);
1915 if (k < 0)
1916 goto error;
1917 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1918 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
1919 bmap->div[div][1 + j]);
1920 isl_int_cdiv_q(bmap->ineq[k][j],
1921 bmap->ineq[k][j], bmap->div[div][0]);
1923 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
1925 if (ub) {
1926 int k = isl_basic_map_alloc_inequality(bmap);
1927 if (k < 0)
1928 goto error;
1929 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1930 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
1931 bmap->div[div][1 + j]);
1932 isl_int_fdiv_q(bmap->ineq[k][j],
1933 bmap->ineq[k][j], bmap->div[div][0]);
1935 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
1938 return bmap;
1939 error:
1940 isl_basic_map_free(bmap);
1941 return NULL;
1944 /* This function is called right before "div" is eliminated from "bmap"
1945 * using Fourier-Motzkin.
1946 * Look through the constraints of "bmap" for constraints on the argument
1947 * of the integer division and use them to construct constraints on the
1948 * integer division itself. These constraints can then be combined
1949 * during the Fourier-Motzkin elimination.
1950 * Note that it is only useful to introduce lower bounds on "div"
1951 * if "bmap" already contains upper bounds on "div" as the newly
1952 * introduce lower bounds can then be combined with the pre-existing
1953 * upper bounds. Similarly for upper bounds.
1954 * We therefore first check if "bmap" contains any lower and/or upper bounds
1955 * on "div".
1957 * It is interesting to note that the introduction of these constraints
1958 * can indeed lead to more accurate results, even when compared to
1959 * deriving constraints on the argument of "div" from constraints on "div".
1960 * Consider, for example, the set
1962 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
1964 * The second constraint can be rewritten as
1966 * 2 * [(-i-2j+3)/4] + k >= 0
1968 * from which we can derive
1970 * -i - 2j + 3 >= -2k
1972 * or
1974 * i + 2j <= 3 + 2k
1976 * Combined with the first constraint, we obtain
1978 * -3 <= 3 + 2k or k >= -3
1980 * If, on the other hand we derive a constraint on [(i+2j)/4] from
1981 * the first constraint, we obtain
1983 * [(i + 2j)/4] >= [-3/4] = -1
1985 * Combining this constraint with the second constraint, we obtain
1987 * k >= -2
1989 static __isl_give isl_basic_map *insert_bounds_on_div(
1990 __isl_take isl_basic_map *bmap, int div)
1992 int i;
1993 int check_lb, check_ub;
1994 isl_int v;
1995 unsigned total;
1997 if (!bmap)
1998 return NULL;
2000 if (isl_int_is_zero(bmap->div[div][0]))
2001 return bmap;
2003 total = isl_space_dim(bmap->dim, isl_dim_all);
2005 check_lb = 0;
2006 check_ub = 0;
2007 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2008 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2009 if (s > 0)
2010 check_ub = 1;
2011 if (s < 0)
2012 check_lb = 1;
2015 if (!check_lb && !check_ub)
2016 return bmap;
2018 isl_int_init(v);
2020 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2021 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2022 continue;
2024 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2025 check_lb, check_ub);
2028 isl_int_clear(v);
2030 return bmap;
2033 /* Remove all divs (recursively) involving any of the given dimensions
2034 * in their definitions.
2036 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2037 __isl_take isl_basic_map *bmap,
2038 enum isl_dim_type type, unsigned first, unsigned n)
2040 int i;
2042 if (!bmap)
2043 return NULL;
2044 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2045 goto error);
2046 first += isl_basic_map_offset(bmap, type);
2048 for (i = bmap->n_div - 1; i >= 0; --i) {
2049 if (!div_involves_vars(bmap, i, first, n))
2050 continue;
2051 bmap = insert_bounds_on_div(bmap, i);
2052 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2053 if (!bmap)
2054 return NULL;
2055 i = bmap->n_div;
2058 return bmap;
2059 error:
2060 isl_basic_map_free(bmap);
2061 return NULL;
2064 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2065 __isl_take isl_basic_set *bset,
2066 enum isl_dim_type type, unsigned first, unsigned n)
2068 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2071 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2072 enum isl_dim_type type, unsigned first, unsigned n)
2074 int i;
2076 if (!map)
2077 return NULL;
2078 if (map->n == 0)
2079 return map;
2081 map = isl_map_cow(map);
2082 if (!map)
2083 return NULL;
2085 for (i = 0; i < map->n; ++i) {
2086 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2087 type, first, n);
2088 if (!map->p[i])
2089 goto error;
2091 return map;
2092 error:
2093 isl_map_free(map);
2094 return NULL;
2097 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2098 enum isl_dim_type type, unsigned first, unsigned n)
2100 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2101 type, first, n);
2104 /* Does the desciption of "bmap" depend on the specified dimensions?
2105 * We also check whether the dimensions appear in any of the div definitions.
2106 * In principle there is no need for this check. If the dimensions appear
2107 * in a div definition, they also appear in the defining constraints of that
2108 * div.
2110 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2111 enum isl_dim_type type, unsigned first, unsigned n)
2113 int i;
2115 if (!bmap)
2116 return -1;
2118 if (first + n > isl_basic_map_dim(bmap, type))
2119 isl_die(bmap->ctx, isl_error_invalid,
2120 "index out of bounds", return -1);
2122 first += isl_basic_map_offset(bmap, type);
2123 for (i = 0; i < bmap->n_eq; ++i)
2124 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2125 return 1;
2126 for (i = 0; i < bmap->n_ineq; ++i)
2127 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2128 return 1;
2129 for (i = 0; i < bmap->n_div; ++i) {
2130 if (isl_int_is_zero(bmap->div[i][0]))
2131 continue;
2132 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2133 return 1;
2136 return 0;
2139 int isl_map_involves_dims(__isl_keep isl_map *map,
2140 enum isl_dim_type type, unsigned first, unsigned n)
2142 int i;
2144 if (!map)
2145 return -1;
2147 if (first + n > isl_map_dim(map, type))
2148 isl_die(map->ctx, isl_error_invalid,
2149 "index out of bounds", return -1);
2151 for (i = 0; i < map->n; ++i) {
2152 int involves = isl_basic_map_involves_dims(map->p[i],
2153 type, first, n);
2154 if (involves < 0 || involves)
2155 return involves;
2158 return 0;
2161 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2162 enum isl_dim_type type, unsigned first, unsigned n)
2164 return isl_basic_map_involves_dims(bset, type, first, n);
2167 int isl_set_involves_dims(__isl_keep isl_set *set,
2168 enum isl_dim_type type, unsigned first, unsigned n)
2170 return isl_map_involves_dims(set, type, first, n);
2173 /* Return true if the definition of the given div is unknown or depends
2174 * on unknown divs.
2176 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2178 int i;
2179 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2181 if (isl_int_is_zero(bmap->div[div][0]))
2182 return 1;
2184 for (i = bmap->n_div - 1; i >= 0; --i) {
2185 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2186 continue;
2187 if (div_is_unknown(bmap, i))
2188 return 1;
2191 return 0;
2194 /* Remove all divs that are unknown or defined in terms of unknown divs.
2196 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2197 __isl_take isl_basic_map *bmap)
2199 int i;
2201 if (!bmap)
2202 return NULL;
2204 for (i = bmap->n_div - 1; i >= 0; --i) {
2205 if (!div_is_unknown(bmap, i))
2206 continue;
2207 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2208 if (!bmap)
2209 return NULL;
2210 i = bmap->n_div;
2213 return bmap;
2216 /* Remove all divs that are unknown or defined in terms of unknown divs.
2218 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2219 __isl_take isl_basic_set *bset)
2221 return isl_basic_map_remove_unknown_divs(bset);
2224 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2226 int i;
2228 if (!map)
2229 return NULL;
2230 if (map->n == 0)
2231 return map;
2233 map = isl_map_cow(map);
2234 if (!map)
2235 return NULL;
2237 for (i = 0; i < map->n; ++i) {
2238 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2239 if (!map->p[i])
2240 goto error;
2242 return map;
2243 error:
2244 isl_map_free(map);
2245 return NULL;
2248 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2250 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2253 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2254 __isl_take isl_basic_set *bset,
2255 enum isl_dim_type type, unsigned first, unsigned n)
2257 return (isl_basic_set *)
2258 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2261 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2262 enum isl_dim_type type, unsigned first, unsigned n)
2264 int i;
2266 if (n == 0)
2267 return map;
2269 map = isl_map_cow(map);
2270 if (!map)
2271 return NULL;
2272 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2274 for (i = 0; i < map->n; ++i) {
2275 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2276 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2277 if (!map->p[i])
2278 goto error;
2280 map = isl_map_drop(map, type, first, n);
2281 return map;
2282 error:
2283 isl_map_free(map);
2284 return NULL;
2287 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2288 enum isl_dim_type type, unsigned first, unsigned n)
2290 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2293 /* Project out n inputs starting at first using Fourier-Motzkin */
2294 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2295 unsigned first, unsigned n)
2297 return isl_map_remove_dims(map, isl_dim_in, first, n);
2300 static void dump_term(struct isl_basic_map *bmap,
2301 isl_int c, int pos, FILE *out)
2303 const char *name;
2304 unsigned in = isl_basic_map_n_in(bmap);
2305 unsigned dim = in + isl_basic_map_n_out(bmap);
2306 unsigned nparam = isl_basic_map_n_param(bmap);
2307 if (!pos)
2308 isl_int_print(out, c, 0);
2309 else {
2310 if (!isl_int_is_one(c))
2311 isl_int_print(out, c, 0);
2312 if (pos < 1 + nparam) {
2313 name = isl_space_get_dim_name(bmap->dim,
2314 isl_dim_param, pos - 1);
2315 if (name)
2316 fprintf(out, "%s", name);
2317 else
2318 fprintf(out, "p%d", pos - 1);
2319 } else if (pos < 1 + nparam + in)
2320 fprintf(out, "i%d", pos - 1 - nparam);
2321 else if (pos < 1 + nparam + dim)
2322 fprintf(out, "o%d", pos - 1 - nparam - in);
2323 else
2324 fprintf(out, "e%d", pos - 1 - nparam - dim);
2328 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2329 int sign, FILE *out)
2331 int i;
2332 int first;
2333 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2334 isl_int v;
2336 isl_int_init(v);
2337 for (i = 0, first = 1; i < len; ++i) {
2338 if (isl_int_sgn(c[i]) * sign <= 0)
2339 continue;
2340 if (!first)
2341 fprintf(out, " + ");
2342 first = 0;
2343 isl_int_abs(v, c[i]);
2344 dump_term(bmap, v, i, out);
2346 isl_int_clear(v);
2347 if (first)
2348 fprintf(out, "0");
2351 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2352 const char *op, FILE *out, int indent)
2354 int i;
2356 fprintf(out, "%*s", indent, "");
2358 dump_constraint_sign(bmap, c, 1, out);
2359 fprintf(out, " %s ", op);
2360 dump_constraint_sign(bmap, c, -1, out);
2362 fprintf(out, "\n");
2364 for (i = bmap->n_div; i < bmap->extra; ++i) {
2365 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2366 continue;
2367 fprintf(out, "%*s", indent, "");
2368 fprintf(out, "ERROR: unused div coefficient not zero\n");
2369 abort();
2373 static void dump_constraints(struct isl_basic_map *bmap,
2374 isl_int **c, unsigned n,
2375 const char *op, FILE *out, int indent)
2377 int i;
2379 for (i = 0; i < n; ++i)
2380 dump_constraint(bmap, c[i], op, out, indent);
2383 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2385 int j;
2386 int first = 1;
2387 unsigned total = isl_basic_map_total_dim(bmap);
2389 for (j = 0; j < 1 + total; ++j) {
2390 if (isl_int_is_zero(exp[j]))
2391 continue;
2392 if (!first && isl_int_is_pos(exp[j]))
2393 fprintf(out, "+");
2394 dump_term(bmap, exp[j], j, out);
2395 first = 0;
2399 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2401 int i;
2403 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2404 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2406 for (i = 0; i < bmap->n_div; ++i) {
2407 fprintf(out, "%*s", indent, "");
2408 fprintf(out, "e%d = [(", i);
2409 dump_affine(bmap, bmap->div[i]+1, out);
2410 fprintf(out, ")/");
2411 isl_int_print(out, bmap->div[i][0], 0);
2412 fprintf(out, "]\n");
2416 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2417 FILE *out, int indent)
2419 if (!bset) {
2420 fprintf(out, "null basic set\n");
2421 return;
2424 fprintf(out, "%*s", indent, "");
2425 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2426 bset->ref, bset->dim->nparam, bset->dim->n_out,
2427 bset->extra, bset->flags);
2428 dump((struct isl_basic_map *)bset, out, indent);
2431 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2432 FILE *out, int indent)
2434 if (!bmap) {
2435 fprintf(out, "null basic map\n");
2436 return;
2439 fprintf(out, "%*s", indent, "");
2440 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2441 "flags: %x, n_name: %d\n",
2442 bmap->ref,
2443 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2444 bmap->extra, bmap->flags, bmap->dim->n_id);
2445 dump(bmap, out, indent);
2448 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2450 unsigned total;
2451 if (!bmap)
2452 return -1;
2453 total = isl_basic_map_total_dim(bmap);
2454 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2455 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2456 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2457 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2458 return 0;
2461 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2462 unsigned flags)
2464 struct isl_set *set;
2466 if (!dim)
2467 return NULL;
2468 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2469 isl_assert(dim->ctx, n >= 0, goto error);
2470 set = isl_alloc(dim->ctx, struct isl_set,
2471 sizeof(struct isl_set) +
2472 (n - 1) * sizeof(struct isl_basic_set *));
2473 if (!set)
2474 goto error;
2476 set->ctx = dim->ctx;
2477 isl_ctx_ref(set->ctx);
2478 set->ref = 1;
2479 set->size = n;
2480 set->n = 0;
2481 set->dim = dim;
2482 set->flags = flags;
2483 return set;
2484 error:
2485 isl_space_free(dim);
2486 return NULL;
2489 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2490 unsigned nparam, unsigned dim, int n, unsigned flags)
2492 struct isl_set *set;
2493 isl_space *dims;
2495 dims = isl_space_alloc(ctx, nparam, 0, dim);
2496 if (!dims)
2497 return NULL;
2499 set = isl_set_alloc_space(dims, n, flags);
2500 return set;
2503 /* Make sure "map" has room for at least "n" more basic maps.
2505 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2507 int i;
2508 struct isl_map *grown = NULL;
2510 if (!map)
2511 return NULL;
2512 isl_assert(map->ctx, n >= 0, goto error);
2513 if (map->n + n <= map->size)
2514 return map;
2515 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2516 if (!grown)
2517 goto error;
2518 for (i = 0; i < map->n; ++i) {
2519 grown->p[i] = isl_basic_map_copy(map->p[i]);
2520 if (!grown->p[i])
2521 goto error;
2522 grown->n++;
2524 isl_map_free(map);
2525 return grown;
2526 error:
2527 isl_map_free(grown);
2528 isl_map_free(map);
2529 return NULL;
2532 /* Make sure "set" has room for at least "n" more basic sets.
2534 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2536 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2539 struct isl_set *isl_set_dup(struct isl_set *set)
2541 int i;
2542 struct isl_set *dup;
2544 if (!set)
2545 return NULL;
2547 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2548 if (!dup)
2549 return NULL;
2550 for (i = 0; i < set->n; ++i)
2551 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2552 return dup;
2555 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2557 return isl_map_from_basic_map(bset);
2560 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2562 struct isl_map *map;
2564 if (!bmap)
2565 return NULL;
2567 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2568 return isl_map_add_basic_map(map, bmap);
2571 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2572 __isl_take isl_basic_set *bset)
2574 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2575 (struct isl_basic_map *)bset);
2578 void *isl_set_free(__isl_take isl_set *set)
2580 int i;
2582 if (!set)
2583 return NULL;
2585 if (--set->ref > 0)
2586 return NULL;
2588 isl_ctx_deref(set->ctx);
2589 for (i = 0; i < set->n; ++i)
2590 isl_basic_set_free(set->p[i]);
2591 isl_space_free(set->dim);
2592 free(set);
2594 return NULL;
2597 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2599 int i;
2601 if (!set) {
2602 fprintf(out, "null set\n");
2603 return;
2606 fprintf(out, "%*s", indent, "");
2607 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2608 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2609 set->flags);
2610 for (i = 0; i < set->n; ++i) {
2611 fprintf(out, "%*s", indent, "");
2612 fprintf(out, "basic set %d:\n", i);
2613 isl_basic_set_print_internal(set->p[i], out, indent+4);
2617 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2619 int i;
2621 if (!map) {
2622 fprintf(out, "null map\n");
2623 return;
2626 fprintf(out, "%*s", indent, "");
2627 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2628 "flags: %x, n_name: %d\n",
2629 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2630 map->dim->n_out, map->flags, map->dim->n_id);
2631 for (i = 0; i < map->n; ++i) {
2632 fprintf(out, "%*s", indent, "");
2633 fprintf(out, "basic map %d:\n", i);
2634 isl_basic_map_print_internal(map->p[i], out, indent+4);
2638 struct isl_basic_map *isl_basic_map_intersect_domain(
2639 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2641 struct isl_basic_map *bmap_domain;
2643 if (!bmap || !bset)
2644 goto error;
2646 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2647 bset->dim, isl_dim_param), goto error);
2649 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2650 isl_assert(bset->ctx,
2651 isl_basic_map_compatible_domain(bmap, bset), goto error);
2653 bmap = isl_basic_map_cow(bmap);
2654 if (!bmap)
2655 goto error;
2656 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2657 bset->n_div, bset->n_eq, bset->n_ineq);
2658 bmap_domain = isl_basic_map_from_domain(bset);
2659 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2661 bmap = isl_basic_map_simplify(bmap);
2662 return isl_basic_map_finalize(bmap);
2663 error:
2664 isl_basic_map_free(bmap);
2665 isl_basic_set_free(bset);
2666 return NULL;
2669 struct isl_basic_map *isl_basic_map_intersect_range(
2670 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2672 struct isl_basic_map *bmap_range;
2674 if (!bmap || !bset)
2675 goto error;
2677 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2678 bset->dim, isl_dim_param), goto error);
2680 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2681 isl_assert(bset->ctx,
2682 isl_basic_map_compatible_range(bmap, bset), goto error);
2684 if (isl_basic_set_is_universe(bset)) {
2685 isl_basic_set_free(bset);
2686 return bmap;
2689 bmap = isl_basic_map_cow(bmap);
2690 if (!bmap)
2691 goto error;
2692 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2693 bset->n_div, bset->n_eq, bset->n_ineq);
2694 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2695 bmap = add_constraints(bmap, bmap_range, 0, 0);
2697 bmap = isl_basic_map_simplify(bmap);
2698 return isl_basic_map_finalize(bmap);
2699 error:
2700 isl_basic_map_free(bmap);
2701 isl_basic_set_free(bset);
2702 return NULL;
2705 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2707 int i;
2708 unsigned total;
2709 isl_int s;
2711 if (!bmap || !vec)
2712 return -1;
2714 total = 1 + isl_basic_map_total_dim(bmap);
2715 if (total != vec->size)
2716 return -1;
2718 isl_int_init(s);
2720 for (i = 0; i < bmap->n_eq; ++i) {
2721 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2722 if (!isl_int_is_zero(s)) {
2723 isl_int_clear(s);
2724 return 0;
2728 for (i = 0; i < bmap->n_ineq; ++i) {
2729 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2730 if (isl_int_is_neg(s)) {
2731 isl_int_clear(s);
2732 return 0;
2736 isl_int_clear(s);
2738 return 1;
2741 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2743 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2746 struct isl_basic_map *isl_basic_map_intersect(
2747 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2749 struct isl_vec *sample = NULL;
2751 if (!bmap1 || !bmap2)
2752 goto error;
2754 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2755 bmap2->dim, isl_dim_param), goto error);
2756 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2757 isl_space_dim(bmap1->dim, isl_dim_param) &&
2758 isl_space_dim(bmap2->dim, isl_dim_all) !=
2759 isl_space_dim(bmap2->dim, isl_dim_param))
2760 return isl_basic_map_intersect(bmap2, bmap1);
2762 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2763 isl_space_dim(bmap2->dim, isl_dim_param))
2764 isl_assert(bmap1->ctx,
2765 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2767 if (bmap1->sample &&
2768 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2769 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2770 sample = isl_vec_copy(bmap1->sample);
2771 else if (bmap2->sample &&
2772 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2773 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2774 sample = isl_vec_copy(bmap2->sample);
2776 bmap1 = isl_basic_map_cow(bmap1);
2777 if (!bmap1)
2778 goto error;
2779 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2780 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2781 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2783 if (!bmap1)
2784 isl_vec_free(sample);
2785 else if (sample) {
2786 isl_vec_free(bmap1->sample);
2787 bmap1->sample = sample;
2790 bmap1 = isl_basic_map_simplify(bmap1);
2791 return isl_basic_map_finalize(bmap1);
2792 error:
2793 if (sample)
2794 isl_vec_free(sample);
2795 isl_basic_map_free(bmap1);
2796 isl_basic_map_free(bmap2);
2797 return NULL;
2800 struct isl_basic_set *isl_basic_set_intersect(
2801 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2803 return (struct isl_basic_set *)
2804 isl_basic_map_intersect(
2805 (struct isl_basic_map *)bset1,
2806 (struct isl_basic_map *)bset2);
2809 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2810 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2812 return isl_basic_set_intersect(bset1, bset2);
2815 /* Special case of isl_map_intersect, where both map1 and map2
2816 * are convex, without any divs and such that either map1 or map2
2817 * contains a single constraint. This constraint is then simply
2818 * added to the other map.
2820 static __isl_give isl_map *map_intersect_add_constraint(
2821 __isl_take isl_map *map1, __isl_take isl_map *map2)
2823 isl_assert(map1->ctx, map1->n == 1, goto error);
2824 isl_assert(map2->ctx, map1->n == 1, goto error);
2825 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2826 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2828 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2829 return isl_map_intersect(map2, map1);
2831 isl_assert(map2->ctx,
2832 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2834 map1 = isl_map_cow(map1);
2835 if (!map1)
2836 goto error;
2837 if (isl_map_plain_is_empty(map1)) {
2838 isl_map_free(map2);
2839 return map1;
2841 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2842 if (map2->p[0]->n_eq == 1)
2843 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2844 else
2845 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2846 map2->p[0]->ineq[0]);
2848 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2849 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2850 if (!map1->p[0])
2851 goto error;
2853 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2854 isl_basic_map_free(map1->p[0]);
2855 map1->n = 0;
2858 isl_map_free(map2);
2860 return map1;
2861 error:
2862 isl_map_free(map1);
2863 isl_map_free(map2);
2864 return NULL;
2867 /* map2 may be either a parameter domain or a map living in the same
2868 * space as map1.
2870 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2871 __isl_take isl_map *map2)
2873 unsigned flags = 0;
2874 isl_map *result;
2875 int i, j;
2877 if (!map1 || !map2)
2878 goto error;
2880 if ((isl_map_plain_is_empty(map1) ||
2881 isl_map_plain_is_universe(map2)) &&
2882 isl_space_is_equal(map1->dim, map2->dim)) {
2883 isl_map_free(map2);
2884 return map1;
2886 if ((isl_map_plain_is_empty(map2) ||
2887 isl_map_plain_is_universe(map1)) &&
2888 isl_space_is_equal(map1->dim, map2->dim)) {
2889 isl_map_free(map1);
2890 return map2;
2893 if (map1->n == 1 && map2->n == 1 &&
2894 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2895 isl_space_is_equal(map1->dim, map2->dim) &&
2896 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2897 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2898 return map_intersect_add_constraint(map1, map2);
2900 if (isl_space_dim(map2->dim, isl_dim_all) !=
2901 isl_space_dim(map2->dim, isl_dim_param))
2902 isl_assert(map1->ctx,
2903 isl_space_is_equal(map1->dim, map2->dim), goto error);
2905 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2906 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2907 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2909 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2910 map1->n * map2->n, flags);
2911 if (!result)
2912 goto error;
2913 for (i = 0; i < map1->n; ++i)
2914 for (j = 0; j < map2->n; ++j) {
2915 struct isl_basic_map *part;
2916 part = isl_basic_map_intersect(
2917 isl_basic_map_copy(map1->p[i]),
2918 isl_basic_map_copy(map2->p[j]));
2919 if (isl_basic_map_is_empty(part) < 0)
2920 part = isl_basic_map_free(part);
2921 result = isl_map_add_basic_map(result, part);
2922 if (!result)
2923 goto error;
2925 isl_map_free(map1);
2926 isl_map_free(map2);
2927 return result;
2928 error:
2929 isl_map_free(map1);
2930 isl_map_free(map2);
2931 return NULL;
2934 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2935 __isl_take isl_map *map2)
2937 if (!map1 || !map2)
2938 goto error;
2939 if (!isl_space_is_equal(map1->dim, map2->dim))
2940 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2941 "spaces don't match", goto error);
2942 return map_intersect_internal(map1, map2);
2943 error:
2944 isl_map_free(map1);
2945 isl_map_free(map2);
2946 return NULL;
2949 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2950 __isl_take isl_map *map2)
2952 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2955 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2957 return (struct isl_set *)
2958 isl_map_intersect((struct isl_map *)set1,
2959 (struct isl_map *)set2);
2962 /* map_intersect_internal accepts intersections
2963 * with parameter domains, so we can just call that function.
2965 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2966 __isl_take isl_set *params)
2968 return map_intersect_internal(map, params);
2971 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2972 __isl_take isl_map *map2)
2974 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2977 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2978 __isl_take isl_set *params)
2980 return isl_map_intersect_params(set, params);
2983 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2985 isl_space *dim;
2986 struct isl_basic_set *bset;
2987 unsigned in;
2989 if (!bmap)
2990 return NULL;
2991 bmap = isl_basic_map_cow(bmap);
2992 if (!bmap)
2993 return NULL;
2994 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2995 in = isl_basic_map_n_in(bmap);
2996 bset = isl_basic_set_from_basic_map(bmap);
2997 bset = isl_basic_set_swap_vars(bset, in);
2998 return isl_basic_map_from_basic_set(bset, dim);
3001 static __isl_give isl_basic_map *basic_map_space_reset(
3002 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3004 isl_space *space;
3006 if (!bmap)
3007 return NULL;
3008 if (!isl_space_is_named_or_nested(bmap->dim, type))
3009 return bmap;
3011 space = isl_basic_map_get_space(bmap);
3012 space = isl_space_reset(space, type);
3013 bmap = isl_basic_map_reset_space(bmap, space);
3014 return bmap;
3017 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3018 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3019 unsigned pos, unsigned n)
3021 isl_space *res_dim;
3022 struct isl_basic_map *res;
3023 struct isl_dim_map *dim_map;
3024 unsigned total, off;
3025 enum isl_dim_type t;
3027 if (n == 0)
3028 return basic_map_space_reset(bmap, type);
3030 if (!bmap)
3031 return NULL;
3033 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3035 total = isl_basic_map_total_dim(bmap) + n;
3036 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3037 off = 0;
3038 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3039 if (t != type) {
3040 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3041 } else {
3042 unsigned size = isl_basic_map_dim(bmap, t);
3043 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3044 0, pos, off);
3045 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3046 pos, size - pos, off + pos + n);
3048 off += isl_space_dim(res_dim, t);
3050 isl_dim_map_div(dim_map, bmap, off);
3052 res = isl_basic_map_alloc_space(res_dim,
3053 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3054 if (isl_basic_map_is_rational(bmap))
3055 res = isl_basic_map_set_rational(res);
3056 if (isl_basic_map_plain_is_empty(bmap)) {
3057 isl_basic_map_free(bmap);
3058 free(dim_map);
3059 return isl_basic_map_set_to_empty(res);
3061 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3062 return isl_basic_map_finalize(res);
3065 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3066 __isl_take isl_basic_set *bset,
3067 enum isl_dim_type type, unsigned pos, unsigned n)
3069 return isl_basic_map_insert_dims(bset, type, pos, n);
3072 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3073 enum isl_dim_type type, unsigned n)
3075 if (!bmap)
3076 return NULL;
3077 return isl_basic_map_insert_dims(bmap, type,
3078 isl_basic_map_dim(bmap, type), n);
3081 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3082 enum isl_dim_type type, unsigned n)
3084 if (!bset)
3085 return NULL;
3086 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3087 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3088 error:
3089 isl_basic_set_free(bset);
3090 return NULL;
3093 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3094 enum isl_dim_type type)
3096 isl_space *space;
3098 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3099 return map;
3101 space = isl_map_get_space(map);
3102 space = isl_space_reset(space, type);
3103 map = isl_map_reset_space(map, space);
3104 return map;
3107 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3108 enum isl_dim_type type, unsigned pos, unsigned n)
3110 int i;
3112 if (n == 0)
3113 return map_space_reset(map, type);
3115 map = isl_map_cow(map);
3116 if (!map)
3117 return NULL;
3119 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3120 if (!map->dim)
3121 goto error;
3123 for (i = 0; i < map->n; ++i) {
3124 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3125 if (!map->p[i])
3126 goto error;
3129 return map;
3130 error:
3131 isl_map_free(map);
3132 return NULL;
3135 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3136 enum isl_dim_type type, unsigned pos, unsigned n)
3138 return isl_map_insert_dims(set, type, pos, n);
3141 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3142 enum isl_dim_type type, unsigned n)
3144 if (!map)
3145 return NULL;
3146 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3149 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3150 enum isl_dim_type type, unsigned n)
3152 if (!set)
3153 return NULL;
3154 isl_assert(set->ctx, type != isl_dim_in, goto error);
3155 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3156 error:
3157 isl_set_free(set);
3158 return NULL;
3161 __isl_give isl_basic_map *isl_basic_map_move_dims(
3162 __isl_take isl_basic_map *bmap,
3163 enum isl_dim_type dst_type, unsigned dst_pos,
3164 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3166 struct isl_dim_map *dim_map;
3167 struct isl_basic_map *res;
3168 enum isl_dim_type t;
3169 unsigned total, off;
3171 if (!bmap)
3172 return NULL;
3173 if (n == 0)
3174 return bmap;
3176 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3177 goto error);
3179 if (dst_type == src_type && dst_pos == src_pos)
3180 return bmap;
3182 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3184 if (pos(bmap->dim, dst_type) + dst_pos ==
3185 pos(bmap->dim, src_type) + src_pos +
3186 ((src_type < dst_type) ? n : 0)) {
3187 bmap = isl_basic_map_cow(bmap);
3188 if (!bmap)
3189 return NULL;
3191 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3192 src_type, src_pos, n);
3193 if (!bmap->dim)
3194 goto error;
3196 bmap = isl_basic_map_finalize(bmap);
3198 return bmap;
3201 total = isl_basic_map_total_dim(bmap);
3202 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3204 off = 0;
3205 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3206 unsigned size = isl_space_dim(bmap->dim, t);
3207 if (t == dst_type) {
3208 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3209 0, dst_pos, off);
3210 off += dst_pos;
3211 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3212 src_pos, n, off);
3213 off += n;
3214 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3215 dst_pos, size - dst_pos, off);
3216 off += size - dst_pos;
3217 } else if (t == src_type) {
3218 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3219 0, src_pos, off);
3220 off += src_pos;
3221 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3222 src_pos + n, size - src_pos - n, off);
3223 off += size - src_pos - n;
3224 } else {
3225 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3226 off += size;
3229 isl_dim_map_div(dim_map, bmap, off);
3231 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3232 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3233 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3234 if (!bmap)
3235 goto error;
3237 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3238 src_type, src_pos, n);
3239 if (!bmap->dim)
3240 goto error;
3242 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3243 bmap = isl_basic_map_gauss(bmap, NULL);
3244 bmap = isl_basic_map_finalize(bmap);
3246 return bmap;
3247 error:
3248 isl_basic_map_free(bmap);
3249 return NULL;
3252 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3253 enum isl_dim_type dst_type, unsigned dst_pos,
3254 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3256 return (isl_basic_set *)isl_basic_map_move_dims(
3257 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3260 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3261 enum isl_dim_type dst_type, unsigned dst_pos,
3262 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3264 if (!set)
3265 return NULL;
3266 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3267 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3268 src_type, src_pos, n);
3269 error:
3270 isl_set_free(set);
3271 return NULL;
3274 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3275 enum isl_dim_type dst_type, unsigned dst_pos,
3276 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3278 int i;
3280 if (!map)
3281 return NULL;
3282 if (n == 0)
3283 return map;
3285 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3286 goto error);
3288 if (dst_type == src_type && dst_pos == src_pos)
3289 return map;
3291 isl_assert(map->ctx, dst_type != src_type, goto error);
3293 map = isl_map_cow(map);
3294 if (!map)
3295 return NULL;
3297 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3298 if (!map->dim)
3299 goto error;
3301 for (i = 0; i < map->n; ++i) {
3302 map->p[i] = isl_basic_map_move_dims(map->p[i],
3303 dst_type, dst_pos,
3304 src_type, src_pos, n);
3305 if (!map->p[i])
3306 goto error;
3309 return map;
3310 error:
3311 isl_map_free(map);
3312 return NULL;
3315 /* Move the specified dimensions to the last columns right before
3316 * the divs. Don't change the dimension specification of bmap.
3317 * That's the responsibility of the caller.
3319 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3320 enum isl_dim_type type, unsigned first, unsigned n)
3322 struct isl_dim_map *dim_map;
3323 struct isl_basic_map *res;
3324 enum isl_dim_type t;
3325 unsigned total, off;
3327 if (!bmap)
3328 return NULL;
3329 if (pos(bmap->dim, type) + first + n ==
3330 1 + isl_space_dim(bmap->dim, isl_dim_all))
3331 return bmap;
3333 total = isl_basic_map_total_dim(bmap);
3334 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3336 off = 0;
3337 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3338 unsigned size = isl_space_dim(bmap->dim, t);
3339 if (t == type) {
3340 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3341 0, first, off);
3342 off += first;
3343 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3344 first, n, total - bmap->n_div - n);
3345 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3346 first + n, size - (first + n), off);
3347 off += size - (first + n);
3348 } else {
3349 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3350 off += size;
3353 isl_dim_map_div(dim_map, bmap, off + n);
3355 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3356 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3357 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3358 return res;
3361 /* Turn the n dimensions of type type, starting at first
3362 * into existentially quantified variables.
3364 __isl_give isl_basic_map *isl_basic_map_project_out(
3365 __isl_take isl_basic_map *bmap,
3366 enum isl_dim_type type, unsigned first, unsigned n)
3368 int i;
3369 size_t row_size;
3370 isl_int **new_div;
3371 isl_int *old;
3373 if (n == 0)
3374 return basic_map_space_reset(bmap, type);
3376 if (!bmap)
3377 return NULL;
3379 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3380 return isl_basic_map_remove_dims(bmap, type, first, n);
3382 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3383 goto error);
3385 bmap = move_last(bmap, type, first, n);
3386 bmap = isl_basic_map_cow(bmap);
3387 if (!bmap)
3388 return NULL;
3390 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3391 old = bmap->block2.data;
3392 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3393 (bmap->extra + n) * (1 + row_size));
3394 if (!bmap->block2.data)
3395 goto error;
3396 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3397 if (!new_div)
3398 goto error;
3399 for (i = 0; i < n; ++i) {
3400 new_div[i] = bmap->block2.data +
3401 (bmap->extra + i) * (1 + row_size);
3402 isl_seq_clr(new_div[i], 1 + row_size);
3404 for (i = 0; i < bmap->extra; ++i)
3405 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3406 free(bmap->div);
3407 bmap->div = new_div;
3408 bmap->n_div += n;
3409 bmap->extra += n;
3411 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3412 if (!bmap->dim)
3413 goto error;
3414 bmap = isl_basic_map_simplify(bmap);
3415 bmap = isl_basic_map_drop_redundant_divs(bmap);
3416 return isl_basic_map_finalize(bmap);
3417 error:
3418 isl_basic_map_free(bmap);
3419 return NULL;
3422 /* Turn the n dimensions of type type, starting at first
3423 * into existentially quantified variables.
3425 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3426 enum isl_dim_type type, unsigned first, unsigned n)
3428 return (isl_basic_set *)isl_basic_map_project_out(
3429 (isl_basic_map *)bset, type, first, n);
3432 /* Turn the n dimensions of type type, starting at first
3433 * into existentially quantified variables.
3435 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3436 enum isl_dim_type type, unsigned first, unsigned n)
3438 int i;
3440 if (!map)
3441 return NULL;
3443 if (n == 0)
3444 return map_space_reset(map, type);
3446 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3448 map = isl_map_cow(map);
3449 if (!map)
3450 return NULL;
3452 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3453 if (!map->dim)
3454 goto error;
3456 for (i = 0; i < map->n; ++i) {
3457 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3458 if (!map->p[i])
3459 goto error;
3462 return map;
3463 error:
3464 isl_map_free(map);
3465 return NULL;
3468 /* Turn the n dimensions of type type, starting at first
3469 * into existentially quantified variables.
3471 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3472 enum isl_dim_type type, unsigned first, unsigned n)
3474 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3477 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3479 int i, j;
3481 for (i = 0; i < n; ++i) {
3482 j = isl_basic_map_alloc_div(bmap);
3483 if (j < 0)
3484 goto error;
3485 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3487 return bmap;
3488 error:
3489 isl_basic_map_free(bmap);
3490 return NULL;
3493 struct isl_basic_map *isl_basic_map_apply_range(
3494 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3496 isl_space *dim_result = NULL;
3497 struct isl_basic_map *bmap;
3498 unsigned n_in, n_out, n, nparam, total, pos;
3499 struct isl_dim_map *dim_map1, *dim_map2;
3501 if (!bmap1 || !bmap2)
3502 goto error;
3504 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3505 isl_space_copy(bmap2->dim));
3507 n_in = isl_basic_map_n_in(bmap1);
3508 n_out = isl_basic_map_n_out(bmap2);
3509 n = isl_basic_map_n_out(bmap1);
3510 nparam = isl_basic_map_n_param(bmap1);
3512 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3513 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3514 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3515 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3516 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3517 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3518 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3519 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3520 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3521 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3522 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3524 bmap = isl_basic_map_alloc_space(dim_result,
3525 bmap1->n_div + bmap2->n_div + n,
3526 bmap1->n_eq + bmap2->n_eq,
3527 bmap1->n_ineq + bmap2->n_ineq);
3528 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3529 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3530 bmap = add_divs(bmap, n);
3531 bmap = isl_basic_map_simplify(bmap);
3532 bmap = isl_basic_map_drop_redundant_divs(bmap);
3533 return isl_basic_map_finalize(bmap);
3534 error:
3535 isl_basic_map_free(bmap1);
3536 isl_basic_map_free(bmap2);
3537 return NULL;
3540 struct isl_basic_set *isl_basic_set_apply(
3541 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3543 if (!bset || !bmap)
3544 goto error;
3546 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3547 goto error);
3549 return (struct isl_basic_set *)
3550 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3551 error:
3552 isl_basic_set_free(bset);
3553 isl_basic_map_free(bmap);
3554 return NULL;
3557 struct isl_basic_map *isl_basic_map_apply_domain(
3558 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3560 if (!bmap1 || !bmap2)
3561 goto error;
3563 isl_assert(bmap1->ctx,
3564 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3565 isl_assert(bmap1->ctx,
3566 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3567 goto error);
3569 bmap1 = isl_basic_map_reverse(bmap1);
3570 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3571 return isl_basic_map_reverse(bmap1);
3572 error:
3573 isl_basic_map_free(bmap1);
3574 isl_basic_map_free(bmap2);
3575 return NULL;
3578 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3579 * A \cap B -> f(A) + f(B)
3581 struct isl_basic_map *isl_basic_map_sum(
3582 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3584 unsigned n_in, n_out, nparam, total, pos;
3585 struct isl_basic_map *bmap = NULL;
3586 struct isl_dim_map *dim_map1, *dim_map2;
3587 int i;
3589 if (!bmap1 || !bmap2)
3590 goto error;
3592 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3593 goto error);
3595 nparam = isl_basic_map_n_param(bmap1);
3596 n_in = isl_basic_map_n_in(bmap1);
3597 n_out = isl_basic_map_n_out(bmap1);
3599 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3600 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3601 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3602 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3603 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3604 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3605 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3606 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3607 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3608 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3609 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3611 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3612 bmap1->n_div + bmap2->n_div + 2 * n_out,
3613 bmap1->n_eq + bmap2->n_eq + n_out,
3614 bmap1->n_ineq + bmap2->n_ineq);
3615 for (i = 0; i < n_out; ++i) {
3616 int j = isl_basic_map_alloc_equality(bmap);
3617 if (j < 0)
3618 goto error;
3619 isl_seq_clr(bmap->eq[j], 1+total);
3620 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3621 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3622 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3624 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3625 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3626 bmap = add_divs(bmap, 2 * n_out);
3628 bmap = isl_basic_map_simplify(bmap);
3629 return isl_basic_map_finalize(bmap);
3630 error:
3631 isl_basic_map_free(bmap);
3632 isl_basic_map_free(bmap1);
3633 isl_basic_map_free(bmap2);
3634 return NULL;
3637 /* Given two maps A -> f(A) and B -> g(B), construct a map
3638 * A \cap B -> f(A) + f(B)
3640 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3642 struct isl_map *result;
3643 int i, j;
3645 if (!map1 || !map2)
3646 goto error;
3648 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3650 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3651 map1->n * map2->n, 0);
3652 if (!result)
3653 goto error;
3654 for (i = 0; i < map1->n; ++i)
3655 for (j = 0; j < map2->n; ++j) {
3656 struct isl_basic_map *part;
3657 part = isl_basic_map_sum(
3658 isl_basic_map_copy(map1->p[i]),
3659 isl_basic_map_copy(map2->p[j]));
3660 if (isl_basic_map_is_empty(part))
3661 isl_basic_map_free(part);
3662 else
3663 result = isl_map_add_basic_map(result, part);
3664 if (!result)
3665 goto error;
3667 isl_map_free(map1);
3668 isl_map_free(map2);
3669 return result;
3670 error:
3671 isl_map_free(map1);
3672 isl_map_free(map2);
3673 return NULL;
3676 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3677 __isl_take isl_set *set2)
3679 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3682 /* Given a basic map A -> f(A), construct A -> -f(A).
3684 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3686 int i, j;
3687 unsigned off, n;
3689 bmap = isl_basic_map_cow(bmap);
3690 if (!bmap)
3691 return NULL;
3693 n = isl_basic_map_dim(bmap, isl_dim_out);
3694 off = isl_basic_map_offset(bmap, isl_dim_out);
3695 for (i = 0; i < bmap->n_eq; ++i)
3696 for (j = 0; j < n; ++j)
3697 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3698 for (i = 0; i < bmap->n_ineq; ++i)
3699 for (j = 0; j < n; ++j)
3700 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3701 for (i = 0; i < bmap->n_div; ++i)
3702 for (j = 0; j < n; ++j)
3703 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3704 bmap = isl_basic_map_gauss(bmap, NULL);
3705 return isl_basic_map_finalize(bmap);
3708 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3710 return isl_basic_map_neg(bset);
3713 /* Given a map A -> f(A), construct A -> -f(A).
3715 struct isl_map *isl_map_neg(struct isl_map *map)
3717 int i;
3719 map = isl_map_cow(map);
3720 if (!map)
3721 return NULL;
3723 for (i = 0; i < map->n; ++i) {
3724 map->p[i] = isl_basic_map_neg(map->p[i]);
3725 if (!map->p[i])
3726 goto error;
3729 return map;
3730 error:
3731 isl_map_free(map);
3732 return NULL;
3735 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3737 return (isl_set *)isl_map_neg((isl_map *)set);
3740 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3741 * A -> floor(f(A)/d).
3743 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3744 isl_int d)
3746 unsigned n_in, n_out, nparam, total, pos;
3747 struct isl_basic_map *result = NULL;
3748 struct isl_dim_map *dim_map;
3749 int i;
3751 if (!bmap)
3752 return NULL;
3754 nparam = isl_basic_map_n_param(bmap);
3755 n_in = isl_basic_map_n_in(bmap);
3756 n_out = isl_basic_map_n_out(bmap);
3758 total = nparam + n_in + n_out + bmap->n_div + n_out;
3759 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3760 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3761 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3762 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3763 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3765 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3766 bmap->n_div + n_out,
3767 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3768 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3769 result = add_divs(result, n_out);
3770 for (i = 0; i < n_out; ++i) {
3771 int j;
3772 j = isl_basic_map_alloc_inequality(result);
3773 if (j < 0)
3774 goto error;
3775 isl_seq_clr(result->ineq[j], 1+total);
3776 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3777 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3778 j = isl_basic_map_alloc_inequality(result);
3779 if (j < 0)
3780 goto error;
3781 isl_seq_clr(result->ineq[j], 1+total);
3782 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3783 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3784 isl_int_sub_ui(result->ineq[j][0], d, 1);
3787 result = isl_basic_map_simplify(result);
3788 return isl_basic_map_finalize(result);
3789 error:
3790 isl_basic_map_free(result);
3791 return NULL;
3794 /* Given a map A -> f(A) and an integer d, construct a map
3795 * A -> floor(f(A)/d).
3797 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3799 int i;
3801 map = isl_map_cow(map);
3802 if (!map)
3803 return NULL;
3805 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3806 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3807 for (i = 0; i < map->n; ++i) {
3808 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3809 if (!map->p[i])
3810 goto error;
3813 return map;
3814 error:
3815 isl_map_free(map);
3816 return NULL;
3819 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3821 int i;
3822 unsigned nparam;
3823 unsigned n_in;
3825 i = isl_basic_map_alloc_equality(bmap);
3826 if (i < 0)
3827 goto error;
3828 nparam = isl_basic_map_n_param(bmap);
3829 n_in = isl_basic_map_n_in(bmap);
3830 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3831 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3832 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3833 return isl_basic_map_finalize(bmap);
3834 error:
3835 isl_basic_map_free(bmap);
3836 return NULL;
3839 /* Add a constraints to "bmap" expressing i_pos < o_pos
3841 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3843 int i;
3844 unsigned nparam;
3845 unsigned n_in;
3847 i = isl_basic_map_alloc_inequality(bmap);
3848 if (i < 0)
3849 goto error;
3850 nparam = isl_basic_map_n_param(bmap);
3851 n_in = isl_basic_map_n_in(bmap);
3852 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3853 isl_int_set_si(bmap->ineq[i][0], -1);
3854 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3855 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3856 return isl_basic_map_finalize(bmap);
3857 error:
3858 isl_basic_map_free(bmap);
3859 return NULL;
3862 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3864 static __isl_give isl_basic_map *var_less_or_equal(
3865 __isl_take isl_basic_map *bmap, unsigned pos)
3867 int i;
3868 unsigned nparam;
3869 unsigned n_in;
3871 i = isl_basic_map_alloc_inequality(bmap);
3872 if (i < 0)
3873 goto error;
3874 nparam = isl_basic_map_n_param(bmap);
3875 n_in = isl_basic_map_n_in(bmap);
3876 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3877 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3878 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3879 return isl_basic_map_finalize(bmap);
3880 error:
3881 isl_basic_map_free(bmap);
3882 return NULL;
3885 /* Add a constraints to "bmap" expressing i_pos > o_pos
3887 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3889 int i;
3890 unsigned nparam;
3891 unsigned n_in;
3893 i = isl_basic_map_alloc_inequality(bmap);
3894 if (i < 0)
3895 goto error;
3896 nparam = isl_basic_map_n_param(bmap);
3897 n_in = isl_basic_map_n_in(bmap);
3898 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3899 isl_int_set_si(bmap->ineq[i][0], -1);
3900 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3901 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3902 return isl_basic_map_finalize(bmap);
3903 error:
3904 isl_basic_map_free(bmap);
3905 return NULL;
3908 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3910 static __isl_give isl_basic_map *var_more_or_equal(
3911 __isl_take isl_basic_map *bmap, unsigned pos)
3913 int i;
3914 unsigned nparam;
3915 unsigned n_in;
3917 i = isl_basic_map_alloc_inequality(bmap);
3918 if (i < 0)
3919 goto error;
3920 nparam = isl_basic_map_n_param(bmap);
3921 n_in = isl_basic_map_n_in(bmap);
3922 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3923 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3924 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3925 return isl_basic_map_finalize(bmap);
3926 error:
3927 isl_basic_map_free(bmap);
3928 return NULL;
3931 __isl_give isl_basic_map *isl_basic_map_equal(
3932 __isl_take isl_space *dim, unsigned n_equal)
3934 int i;
3935 struct isl_basic_map *bmap;
3936 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3937 if (!bmap)
3938 return NULL;
3939 for (i = 0; i < n_equal && bmap; ++i)
3940 bmap = var_equal(bmap, i);
3941 return isl_basic_map_finalize(bmap);
3944 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3946 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3947 unsigned pos)
3949 int i;
3950 struct isl_basic_map *bmap;
3951 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3952 if (!bmap)
3953 return NULL;
3954 for (i = 0; i < pos && bmap; ++i)
3955 bmap = var_equal(bmap, i);
3956 if (bmap)
3957 bmap = var_less(bmap, pos);
3958 return isl_basic_map_finalize(bmap);
3961 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3963 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3964 __isl_take isl_space *dim, unsigned pos)
3966 int i;
3967 isl_basic_map *bmap;
3969 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3970 for (i = 0; i < pos; ++i)
3971 bmap = var_equal(bmap, i);
3972 bmap = var_less_or_equal(bmap, pos);
3973 return isl_basic_map_finalize(bmap);
3976 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3978 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3979 unsigned pos)
3981 int i;
3982 struct isl_basic_map *bmap;
3983 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3984 if (!bmap)
3985 return NULL;
3986 for (i = 0; i < pos && bmap; ++i)
3987 bmap = var_equal(bmap, i);
3988 if (bmap)
3989 bmap = var_more(bmap, pos);
3990 return isl_basic_map_finalize(bmap);
3993 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3995 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3996 __isl_take isl_space *dim, unsigned pos)
3998 int i;
3999 isl_basic_map *bmap;
4001 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4002 for (i = 0; i < pos; ++i)
4003 bmap = var_equal(bmap, i);
4004 bmap = var_more_or_equal(bmap, pos);
4005 return isl_basic_map_finalize(bmap);
4008 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4009 unsigned n, int equal)
4011 struct isl_map *map;
4012 int i;
4014 if (n == 0 && equal)
4015 return isl_map_universe(dims);
4017 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4019 for (i = 0; i + 1 < n; ++i)
4020 map = isl_map_add_basic_map(map,
4021 isl_basic_map_less_at(isl_space_copy(dims), i));
4022 if (n > 0) {
4023 if (equal)
4024 map = isl_map_add_basic_map(map,
4025 isl_basic_map_less_or_equal_at(dims, n - 1));
4026 else
4027 map = isl_map_add_basic_map(map,
4028 isl_basic_map_less_at(dims, n - 1));
4029 } else
4030 isl_space_free(dims);
4032 return map;
4035 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4037 if (!dims)
4038 return NULL;
4039 return map_lex_lte_first(dims, dims->n_out, equal);
4042 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4044 return map_lex_lte_first(dim, n, 0);
4047 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4049 return map_lex_lte_first(dim, n, 1);
4052 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4054 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4057 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4059 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4062 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4063 unsigned n, int equal)
4065 struct isl_map *map;
4066 int i;
4068 if (n == 0 && equal)
4069 return isl_map_universe(dims);
4071 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4073 for (i = 0; i + 1 < n; ++i)
4074 map = isl_map_add_basic_map(map,
4075 isl_basic_map_more_at(isl_space_copy(dims), i));
4076 if (n > 0) {
4077 if (equal)
4078 map = isl_map_add_basic_map(map,
4079 isl_basic_map_more_or_equal_at(dims, n - 1));
4080 else
4081 map = isl_map_add_basic_map(map,
4082 isl_basic_map_more_at(dims, n - 1));
4083 } else
4084 isl_space_free(dims);
4086 return map;
4089 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4091 if (!dims)
4092 return NULL;
4093 return map_lex_gte_first(dims, dims->n_out, equal);
4096 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4098 return map_lex_gte_first(dim, n, 0);
4101 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4103 return map_lex_gte_first(dim, n, 1);
4106 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4108 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4111 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4113 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4116 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4117 __isl_take isl_set *set2)
4119 isl_map *map;
4120 map = isl_map_lex_le(isl_set_get_space(set1));
4121 map = isl_map_intersect_domain(map, set1);
4122 map = isl_map_intersect_range(map, set2);
4123 return map;
4126 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4127 __isl_take isl_set *set2)
4129 isl_map *map;
4130 map = isl_map_lex_lt(isl_set_get_space(set1));
4131 map = isl_map_intersect_domain(map, set1);
4132 map = isl_map_intersect_range(map, set2);
4133 return map;
4136 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4137 __isl_take isl_set *set2)
4139 isl_map *map;
4140 map = isl_map_lex_ge(isl_set_get_space(set1));
4141 map = isl_map_intersect_domain(map, set1);
4142 map = isl_map_intersect_range(map, set2);
4143 return map;
4146 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4147 __isl_take isl_set *set2)
4149 isl_map *map;
4150 map = isl_map_lex_gt(isl_set_get_space(set1));
4151 map = isl_map_intersect_domain(map, set1);
4152 map = isl_map_intersect_range(map, set2);
4153 return map;
4156 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4157 __isl_take isl_map *map2)
4159 isl_map *map;
4160 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4161 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4162 map = isl_map_apply_range(map, isl_map_reverse(map2));
4163 return map;
4166 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4167 __isl_take isl_map *map2)
4169 isl_map *map;
4170 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4171 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4172 map = isl_map_apply_range(map, isl_map_reverse(map2));
4173 return map;
4176 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4177 __isl_take isl_map *map2)
4179 isl_map *map;
4180 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4181 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4182 map = isl_map_apply_range(map, isl_map_reverse(map2));
4183 return map;
4186 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4187 __isl_take isl_map *map2)
4189 isl_map *map;
4190 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4191 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4192 map = isl_map_apply_range(map, isl_map_reverse(map2));
4193 return map;
4196 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4197 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4199 struct isl_basic_map *bmap;
4201 bset = isl_basic_set_cow(bset);
4202 if (!bset || !dim)
4203 goto error;
4205 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4206 isl_space_free(bset->dim);
4207 bmap = (struct isl_basic_map *) bset;
4208 bmap->dim = dim;
4209 return isl_basic_map_finalize(bmap);
4210 error:
4211 isl_basic_set_free(bset);
4212 isl_space_free(dim);
4213 return NULL;
4216 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4218 if (!bmap)
4219 goto error;
4220 if (bmap->dim->n_in == 0)
4221 return (struct isl_basic_set *)bmap;
4222 bmap = isl_basic_map_cow(bmap);
4223 if (!bmap)
4224 goto error;
4225 bmap->dim = isl_space_as_set_space(bmap->dim);
4226 if (!bmap->dim)
4227 goto error;
4228 bmap = isl_basic_map_finalize(bmap);
4229 return (struct isl_basic_set *)bmap;
4230 error:
4231 isl_basic_map_free(bmap);
4232 return NULL;
4235 /* For a div d = floor(f/m), add the constraints
4237 * f - m d >= 0
4238 * -(f-(n-1)) + m d >= 0
4240 * Note that the second constraint is the negation of
4242 * f - m d >= n
4244 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4245 unsigned pos, isl_int *div)
4247 int i, j;
4248 unsigned total = isl_basic_map_total_dim(bmap);
4250 i = isl_basic_map_alloc_inequality(bmap);
4251 if (i < 0)
4252 return -1;
4253 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4254 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4256 j = isl_basic_map_alloc_inequality(bmap);
4257 if (j < 0)
4258 return -1;
4259 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
4260 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
4261 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
4262 return j;
4265 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4266 unsigned pos, isl_int *div)
4268 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4269 pos, div);
4272 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4274 unsigned total = isl_basic_map_total_dim(bmap);
4275 unsigned div_pos = total - bmap->n_div + div;
4277 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4278 bmap->div[div]);
4281 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4283 return isl_basic_map_add_div_constraints(bset, div);
4286 struct isl_basic_set *isl_basic_map_underlying_set(
4287 struct isl_basic_map *bmap)
4289 if (!bmap)
4290 goto error;
4291 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4292 bmap->n_div == 0 &&
4293 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4294 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4295 return (struct isl_basic_set *)bmap;
4296 bmap = isl_basic_map_cow(bmap);
4297 if (!bmap)
4298 goto error;
4299 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4300 if (!bmap->dim)
4301 goto error;
4302 bmap->extra -= bmap->n_div;
4303 bmap->n_div = 0;
4304 bmap = isl_basic_map_finalize(bmap);
4305 return (struct isl_basic_set *)bmap;
4306 error:
4307 isl_basic_map_free(bmap);
4308 return NULL;
4311 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4312 __isl_take isl_basic_set *bset)
4314 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4317 struct isl_basic_map *isl_basic_map_overlying_set(
4318 struct isl_basic_set *bset, struct isl_basic_map *like)
4320 struct isl_basic_map *bmap;
4321 struct isl_ctx *ctx;
4322 unsigned total;
4323 int i;
4325 if (!bset || !like)
4326 goto error;
4327 ctx = bset->ctx;
4328 isl_assert(ctx, bset->n_div == 0, goto error);
4329 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4330 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4331 goto error);
4332 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4333 isl_basic_map_free(like);
4334 return (struct isl_basic_map *)bset;
4336 bset = isl_basic_set_cow(bset);
4337 if (!bset)
4338 goto error;
4339 total = bset->dim->n_out + bset->extra;
4340 bmap = (struct isl_basic_map *)bset;
4341 isl_space_free(bmap->dim);
4342 bmap->dim = isl_space_copy(like->dim);
4343 if (!bmap->dim)
4344 goto error;
4345 bmap->n_div = like->n_div;
4346 bmap->extra += like->n_div;
4347 if (bmap->extra) {
4348 unsigned ltotal;
4349 isl_int **div;
4350 ltotal = total - bmap->extra + like->extra;
4351 if (ltotal > total)
4352 ltotal = total;
4353 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4354 bmap->extra * (1 + 1 + total));
4355 if (isl_blk_is_error(bmap->block2))
4356 goto error;
4357 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4358 if (!div)
4359 goto error;
4360 bmap->div = div;
4361 for (i = 0; i < bmap->extra; ++i)
4362 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4363 for (i = 0; i < like->n_div; ++i) {
4364 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4365 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4367 bmap = isl_basic_map_extend_constraints(bmap,
4368 0, 2 * like->n_div);
4369 for (i = 0; i < like->n_div; ++i) {
4370 if (!bmap)
4371 break;
4372 if (isl_int_is_zero(bmap->div[i][0]))
4373 continue;
4374 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4375 bmap = isl_basic_map_free(bmap);
4378 isl_basic_map_free(like);
4379 bmap = isl_basic_map_simplify(bmap);
4380 bmap = isl_basic_map_finalize(bmap);
4381 return bmap;
4382 error:
4383 isl_basic_map_free(like);
4384 isl_basic_set_free(bset);
4385 return NULL;
4388 struct isl_basic_set *isl_basic_set_from_underlying_set(
4389 struct isl_basic_set *bset, struct isl_basic_set *like)
4391 return (struct isl_basic_set *)
4392 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4395 struct isl_set *isl_set_from_underlying_set(
4396 struct isl_set *set, struct isl_basic_set *like)
4398 int i;
4400 if (!set || !like)
4401 goto error;
4402 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4403 goto error);
4404 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4405 isl_basic_set_free(like);
4406 return set;
4408 set = isl_set_cow(set);
4409 if (!set)
4410 goto error;
4411 for (i = 0; i < set->n; ++i) {
4412 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4413 isl_basic_set_copy(like));
4414 if (!set->p[i])
4415 goto error;
4417 isl_space_free(set->dim);
4418 set->dim = isl_space_copy(like->dim);
4419 if (!set->dim)
4420 goto error;
4421 isl_basic_set_free(like);
4422 return set;
4423 error:
4424 isl_basic_set_free(like);
4425 isl_set_free(set);
4426 return NULL;
4429 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4431 int i;
4433 map = isl_map_cow(map);
4434 if (!map)
4435 return NULL;
4436 map->dim = isl_space_cow(map->dim);
4437 if (!map->dim)
4438 goto error;
4440 for (i = 1; i < map->n; ++i)
4441 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4442 goto error);
4443 for (i = 0; i < map->n; ++i) {
4444 map->p[i] = (struct isl_basic_map *)
4445 isl_basic_map_underlying_set(map->p[i]);
4446 if (!map->p[i])
4447 goto error;
4449 if (map->n == 0)
4450 map->dim = isl_space_underlying(map->dim, 0);
4451 else {
4452 isl_space_free(map->dim);
4453 map->dim = isl_space_copy(map->p[0]->dim);
4455 if (!map->dim)
4456 goto error;
4457 return (struct isl_set *)map;
4458 error:
4459 isl_map_free(map);
4460 return NULL;
4463 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4465 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4468 __isl_give isl_basic_map *isl_basic_map_reset_space(
4469 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4471 bmap = isl_basic_map_cow(bmap);
4472 if (!bmap || !dim)
4473 goto error;
4475 isl_space_free(bmap->dim);
4476 bmap->dim = dim;
4478 bmap = isl_basic_map_finalize(bmap);
4480 return bmap;
4481 error:
4482 isl_basic_map_free(bmap);
4483 isl_space_free(dim);
4484 return NULL;
4487 __isl_give isl_basic_set *isl_basic_set_reset_space(
4488 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4490 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4491 dim);
4494 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4495 __isl_take isl_space *dim)
4497 int i;
4499 map = isl_map_cow(map);
4500 if (!map || !dim)
4501 goto error;
4503 for (i = 0; i < map->n; ++i) {
4504 map->p[i] = isl_basic_map_reset_space(map->p[i],
4505 isl_space_copy(dim));
4506 if (!map->p[i])
4507 goto error;
4509 isl_space_free(map->dim);
4510 map->dim = dim;
4512 return map;
4513 error:
4514 isl_map_free(map);
4515 isl_space_free(dim);
4516 return NULL;
4519 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4520 __isl_take isl_space *dim)
4522 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4525 /* Compute the parameter domain of the given basic set.
4527 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4529 isl_space *space;
4530 unsigned n;
4532 if (isl_basic_set_is_params(bset))
4533 return bset;
4535 n = isl_basic_set_dim(bset, isl_dim_set);
4536 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4537 space = isl_basic_set_get_space(bset);
4538 space = isl_space_params(space);
4539 bset = isl_basic_set_reset_space(bset, space);
4540 return bset;
4543 /* Construct a zero-dimensional basic set with the given parameter domain.
4545 __isl_give isl_basic_set *isl_basic_set_from_params(
4546 __isl_take isl_basic_set *bset)
4548 isl_space *space;
4549 space = isl_basic_set_get_space(bset);
4550 space = isl_space_set_from_params(space);
4551 bset = isl_basic_set_reset_space(bset, space);
4552 return bset;
4555 /* Compute the parameter domain of the given set.
4557 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4559 isl_space *space;
4560 unsigned n;
4562 if (isl_set_is_params(set))
4563 return set;
4565 n = isl_set_dim(set, isl_dim_set);
4566 set = isl_set_project_out(set, isl_dim_set, 0, n);
4567 space = isl_set_get_space(set);
4568 space = isl_space_params(space);
4569 set = isl_set_reset_space(set, space);
4570 return set;
4573 /* Construct a zero-dimensional set with the given parameter domain.
4575 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4577 isl_space *space;
4578 space = isl_set_get_space(set);
4579 space = isl_space_set_from_params(space);
4580 set = isl_set_reset_space(set, space);
4581 return set;
4584 /* Compute the parameter domain of the given map.
4586 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4588 isl_space *space;
4589 unsigned n;
4591 n = isl_map_dim(map, isl_dim_in);
4592 map = isl_map_project_out(map, isl_dim_in, 0, n);
4593 n = isl_map_dim(map, isl_dim_out);
4594 map = isl_map_project_out(map, isl_dim_out, 0, n);
4595 space = isl_map_get_space(map);
4596 space = isl_space_params(space);
4597 map = isl_map_reset_space(map, space);
4598 return map;
4601 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4603 isl_space *dim;
4604 struct isl_basic_set *domain;
4605 unsigned n_in;
4606 unsigned n_out;
4608 if (!bmap)
4609 return NULL;
4610 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4612 n_in = isl_basic_map_n_in(bmap);
4613 n_out = isl_basic_map_n_out(bmap);
4614 domain = isl_basic_set_from_basic_map(bmap);
4615 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4617 domain = isl_basic_set_reset_space(domain, dim);
4619 return domain;
4622 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4624 if (!bmap)
4625 return -1;
4626 return isl_space_may_be_set(bmap->dim);
4629 /* Is this basic map actually a set?
4630 * Users should never call this function. Outside of isl,
4631 * the type should indicate whether something is a set or a map.
4633 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4635 if (!bmap)
4636 return -1;
4637 return isl_space_is_set(bmap->dim);
4640 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4642 if (!bmap)
4643 return NULL;
4644 if (isl_basic_map_is_set(bmap))
4645 return bmap;
4646 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4649 __isl_give isl_basic_map *isl_basic_map_domain_map(
4650 __isl_take isl_basic_map *bmap)
4652 int i, k;
4653 isl_space *dim;
4654 isl_basic_map *domain;
4655 int nparam, n_in, n_out;
4656 unsigned total;
4658 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4659 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4660 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4662 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4663 domain = isl_basic_map_universe(dim);
4665 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4666 bmap = isl_basic_map_apply_range(bmap, domain);
4667 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4669 total = isl_basic_map_total_dim(bmap);
4671 for (i = 0; i < n_in; ++i) {
4672 k = isl_basic_map_alloc_equality(bmap);
4673 if (k < 0)
4674 goto error;
4675 isl_seq_clr(bmap->eq[k], 1 + total);
4676 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4677 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4680 bmap = isl_basic_map_gauss(bmap, NULL);
4681 return isl_basic_map_finalize(bmap);
4682 error:
4683 isl_basic_map_free(bmap);
4684 return NULL;
4687 __isl_give isl_basic_map *isl_basic_map_range_map(
4688 __isl_take isl_basic_map *bmap)
4690 int i, k;
4691 isl_space *dim;
4692 isl_basic_map *range;
4693 int nparam, n_in, n_out;
4694 unsigned total;
4696 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4697 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4698 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4700 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4701 range = isl_basic_map_universe(dim);
4703 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4704 bmap = isl_basic_map_apply_range(bmap, range);
4705 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4707 total = isl_basic_map_total_dim(bmap);
4709 for (i = 0; i < n_out; ++i) {
4710 k = isl_basic_map_alloc_equality(bmap);
4711 if (k < 0)
4712 goto error;
4713 isl_seq_clr(bmap->eq[k], 1 + total);
4714 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4715 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4718 bmap = isl_basic_map_gauss(bmap, NULL);
4719 return isl_basic_map_finalize(bmap);
4720 error:
4721 isl_basic_map_free(bmap);
4722 return NULL;
4725 int isl_map_may_be_set(__isl_keep isl_map *map)
4727 if (!map)
4728 return -1;
4729 return isl_space_may_be_set(map->dim);
4732 /* Is this map actually a set?
4733 * Users should never call this function. Outside of isl,
4734 * the type should indicate whether something is a set or a map.
4736 int isl_map_is_set(__isl_keep isl_map *map)
4738 if (!map)
4739 return -1;
4740 return isl_space_is_set(map->dim);
4743 struct isl_set *isl_map_range(struct isl_map *map)
4745 int i;
4746 struct isl_set *set;
4748 if (!map)
4749 goto error;
4750 if (isl_map_is_set(map))
4751 return (isl_set *)map;
4753 map = isl_map_cow(map);
4754 if (!map)
4755 goto error;
4757 set = (struct isl_set *) map;
4758 set->dim = isl_space_range(set->dim);
4759 if (!set->dim)
4760 goto error;
4761 for (i = 0; i < map->n; ++i) {
4762 set->p[i] = isl_basic_map_range(map->p[i]);
4763 if (!set->p[i])
4764 goto error;
4766 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4767 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4768 return set;
4769 error:
4770 isl_map_free(map);
4771 return NULL;
4774 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4776 int i;
4777 isl_space *domain_dim;
4779 map = isl_map_cow(map);
4780 if (!map)
4781 return NULL;
4783 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4784 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4785 map->dim = isl_space_join(map->dim, domain_dim);
4786 if (!map->dim)
4787 goto error;
4788 for (i = 0; i < map->n; ++i) {
4789 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4790 if (!map->p[i])
4791 goto error;
4793 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4794 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4795 return map;
4796 error:
4797 isl_map_free(map);
4798 return NULL;
4801 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4803 int i;
4804 isl_space *range_dim;
4806 map = isl_map_cow(map);
4807 if (!map)
4808 return NULL;
4810 range_dim = isl_space_range(isl_map_get_space(map));
4811 range_dim = isl_space_from_range(range_dim);
4812 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4813 map->dim = isl_space_join(map->dim, range_dim);
4814 if (!map->dim)
4815 goto error;
4816 for (i = 0; i < map->n; ++i) {
4817 map->p[i] = isl_basic_map_range_map(map->p[i]);
4818 if (!map->p[i])
4819 goto error;
4821 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4822 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4823 return map;
4824 error:
4825 isl_map_free(map);
4826 return NULL;
4829 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4830 __isl_take isl_space *dim)
4832 int i;
4833 struct isl_map *map = NULL;
4835 set = isl_set_cow(set);
4836 if (!set || !dim)
4837 goto error;
4838 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4839 map = (struct isl_map *)set;
4840 for (i = 0; i < set->n; ++i) {
4841 map->p[i] = isl_basic_map_from_basic_set(
4842 set->p[i], isl_space_copy(dim));
4843 if (!map->p[i])
4844 goto error;
4846 isl_space_free(map->dim);
4847 map->dim = dim;
4848 return map;
4849 error:
4850 isl_space_free(dim);
4851 isl_set_free(set);
4852 return NULL;
4855 __isl_give isl_basic_map *isl_basic_map_from_domain(
4856 __isl_take isl_basic_set *bset)
4858 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4861 __isl_give isl_basic_map *isl_basic_map_from_range(
4862 __isl_take isl_basic_set *bset)
4864 isl_space *space;
4865 space = isl_basic_set_get_space(bset);
4866 space = isl_space_from_range(space);
4867 bset = isl_basic_set_reset_space(bset, space);
4868 return (isl_basic_map *)bset;
4871 struct isl_map *isl_map_from_range(struct isl_set *set)
4873 isl_space *space;
4874 space = isl_set_get_space(set);
4875 space = isl_space_from_range(space);
4876 set = isl_set_reset_space(set, space);
4877 return (struct isl_map *)set;
4880 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4882 return isl_map_reverse(isl_map_from_range(set));
4885 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4886 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4888 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4891 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4892 __isl_take isl_set *range)
4894 return isl_map_apply_range(isl_map_reverse(domain), range);
4897 struct isl_set *isl_set_from_map(struct isl_map *map)
4899 int i;
4900 struct isl_set *set = NULL;
4902 if (!map)
4903 return NULL;
4904 map = isl_map_cow(map);
4905 if (!map)
4906 return NULL;
4907 map->dim = isl_space_as_set_space(map->dim);
4908 if (!map->dim)
4909 goto error;
4910 set = (struct isl_set *)map;
4911 for (i = 0; i < map->n; ++i) {
4912 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4913 if (!set->p[i])
4914 goto error;
4916 return set;
4917 error:
4918 isl_map_free(map);
4919 return NULL;
4922 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4923 unsigned flags)
4925 struct isl_map *map;
4927 if (!dim)
4928 return NULL;
4929 if (n < 0)
4930 isl_die(dim->ctx, isl_error_internal,
4931 "negative number of basic maps", goto error);
4932 map = isl_alloc(dim->ctx, struct isl_map,
4933 sizeof(struct isl_map) +
4934 (n - 1) * sizeof(struct isl_basic_map *));
4935 if (!map)
4936 goto error;
4938 map->ctx = dim->ctx;
4939 isl_ctx_ref(map->ctx);
4940 map->ref = 1;
4941 map->size = n;
4942 map->n = 0;
4943 map->dim = dim;
4944 map->flags = flags;
4945 return map;
4946 error:
4947 isl_space_free(dim);
4948 return NULL;
4951 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4952 unsigned nparam, unsigned in, unsigned out, int n,
4953 unsigned flags)
4955 struct isl_map *map;
4956 isl_space *dims;
4958 dims = isl_space_alloc(ctx, nparam, in, out);
4959 if (!dims)
4960 return NULL;
4962 map = isl_map_alloc_space(dims, n, flags);
4963 return map;
4966 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4968 struct isl_basic_map *bmap;
4969 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4970 bmap = isl_basic_map_set_to_empty(bmap);
4971 return bmap;
4974 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4976 struct isl_basic_set *bset;
4977 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4978 bset = isl_basic_set_set_to_empty(bset);
4979 return bset;
4982 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4984 struct isl_basic_map *bmap;
4985 if (!model)
4986 return NULL;
4987 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4988 bmap = isl_basic_map_set_to_empty(bmap);
4989 return bmap;
4992 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4994 struct isl_basic_map *bmap;
4995 if (!model)
4996 return NULL;
4997 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4998 bmap = isl_basic_map_set_to_empty(bmap);
4999 return bmap;
5002 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5004 struct isl_basic_set *bset;
5005 if (!model)
5006 return NULL;
5007 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5008 bset = isl_basic_set_set_to_empty(bset);
5009 return bset;
5012 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5014 struct isl_basic_map *bmap;
5015 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5016 bmap = isl_basic_map_finalize(bmap);
5017 return bmap;
5020 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5022 struct isl_basic_set *bset;
5023 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5024 bset = isl_basic_set_finalize(bset);
5025 return bset;
5028 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5030 int i;
5031 unsigned total = isl_space_dim(dim, isl_dim_all);
5032 isl_basic_map *bmap;
5034 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5035 for (i = 0; i < total; ++i) {
5036 int k = isl_basic_map_alloc_inequality(bmap);
5037 if (k < 0)
5038 goto error;
5039 isl_seq_clr(bmap->ineq[k], 1 + total);
5040 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5042 return bmap;
5043 error:
5044 isl_basic_map_free(bmap);
5045 return NULL;
5048 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5050 return isl_basic_map_nat_universe(dim);
5053 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5055 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5058 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5060 return isl_map_nat_universe(dim);
5063 __isl_give isl_basic_map *isl_basic_map_universe_like(
5064 __isl_keep isl_basic_map *model)
5066 if (!model)
5067 return NULL;
5068 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5071 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5073 if (!model)
5074 return NULL;
5075 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5078 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5079 __isl_keep isl_set *model)
5081 if (!model)
5082 return NULL;
5083 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5086 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5088 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5091 struct isl_map *isl_map_empty_like(struct isl_map *model)
5093 if (!model)
5094 return NULL;
5095 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5098 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5100 if (!model)
5101 return NULL;
5102 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5105 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5107 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5110 struct isl_set *isl_set_empty_like(struct isl_set *model)
5112 if (!model)
5113 return NULL;
5114 return isl_set_empty(isl_space_copy(model->dim));
5117 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5119 struct isl_map *map;
5120 if (!dim)
5121 return NULL;
5122 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5123 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5124 return map;
5127 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5129 struct isl_set *set;
5130 if (!dim)
5131 return NULL;
5132 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5133 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5134 return set;
5137 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5139 if (!model)
5140 return NULL;
5141 return isl_set_universe(isl_space_copy(model->dim));
5144 struct isl_map *isl_map_dup(struct isl_map *map)
5146 int i;
5147 struct isl_map *dup;
5149 if (!map)
5150 return NULL;
5151 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5152 for (i = 0; i < map->n; ++i)
5153 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5154 return dup;
5157 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5158 __isl_take isl_basic_map *bmap)
5160 if (!bmap || !map)
5161 goto error;
5162 if (isl_basic_map_plain_is_empty(bmap)) {
5163 isl_basic_map_free(bmap);
5164 return map;
5166 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5167 isl_assert(map->ctx, map->n < map->size, goto error);
5168 map->p[map->n] = bmap;
5169 map->n++;
5170 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5171 return map;
5172 error:
5173 if (map)
5174 isl_map_free(map);
5175 if (bmap)
5176 isl_basic_map_free(bmap);
5177 return NULL;
5180 void *isl_map_free(struct isl_map *map)
5182 int i;
5184 if (!map)
5185 return NULL;
5187 if (--map->ref > 0)
5188 return NULL;
5190 isl_ctx_deref(map->ctx);
5191 for (i = 0; i < map->n; ++i)
5192 isl_basic_map_free(map->p[i]);
5193 isl_space_free(map->dim);
5194 free(map);
5196 return NULL;
5199 struct isl_map *isl_map_extend(struct isl_map *base,
5200 unsigned nparam, unsigned n_in, unsigned n_out)
5202 int i;
5204 base = isl_map_cow(base);
5205 if (!base)
5206 return NULL;
5208 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5209 if (!base->dim)
5210 goto error;
5211 for (i = 0; i < base->n; ++i) {
5212 base->p[i] = isl_basic_map_extend_space(base->p[i],
5213 isl_space_copy(base->dim), 0, 0, 0);
5214 if (!base->p[i])
5215 goto error;
5217 return base;
5218 error:
5219 isl_map_free(base);
5220 return NULL;
5223 struct isl_set *isl_set_extend(struct isl_set *base,
5224 unsigned nparam, unsigned dim)
5226 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5227 nparam, 0, dim);
5230 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5231 struct isl_basic_map *bmap, unsigned pos, int value)
5233 int j;
5235 bmap = isl_basic_map_cow(bmap);
5236 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5237 j = isl_basic_map_alloc_equality(bmap);
5238 if (j < 0)
5239 goto error;
5240 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5241 isl_int_set_si(bmap->eq[j][pos], -1);
5242 isl_int_set_si(bmap->eq[j][0], value);
5243 bmap = isl_basic_map_simplify(bmap);
5244 return isl_basic_map_finalize(bmap);
5245 error:
5246 isl_basic_map_free(bmap);
5247 return NULL;
5250 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5251 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5253 int j;
5255 bmap = isl_basic_map_cow(bmap);
5256 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5257 j = isl_basic_map_alloc_equality(bmap);
5258 if (j < 0)
5259 goto error;
5260 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5261 isl_int_set_si(bmap->eq[j][pos], -1);
5262 isl_int_set(bmap->eq[j][0], value);
5263 bmap = isl_basic_map_simplify(bmap);
5264 return isl_basic_map_finalize(bmap);
5265 error:
5266 isl_basic_map_free(bmap);
5267 return NULL;
5270 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5271 enum isl_dim_type type, unsigned pos, int value)
5273 if (!bmap)
5274 return NULL;
5275 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5276 return isl_basic_map_fix_pos_si(bmap,
5277 isl_basic_map_offset(bmap, type) + pos, value);
5278 error:
5279 isl_basic_map_free(bmap);
5280 return NULL;
5283 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5284 enum isl_dim_type type, unsigned pos, isl_int value)
5286 if (!bmap)
5287 return NULL;
5288 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5289 return isl_basic_map_fix_pos(bmap,
5290 isl_basic_map_offset(bmap, type) + pos, value);
5291 error:
5292 isl_basic_map_free(bmap);
5293 return NULL;
5296 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5297 enum isl_dim_type type, unsigned pos, int value)
5299 return (struct isl_basic_set *)
5300 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5301 type, pos, value);
5304 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5305 enum isl_dim_type type, unsigned pos, isl_int value)
5307 return (struct isl_basic_set *)
5308 isl_basic_map_fix((struct isl_basic_map *)bset,
5309 type, pos, value);
5312 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5313 unsigned input, int value)
5315 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5318 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5319 unsigned dim, int value)
5321 return (struct isl_basic_set *)
5322 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5323 isl_dim_set, dim, value);
5326 static int remove_if_empty(__isl_keep isl_map *map, int i)
5328 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5330 if (empty < 0)
5331 return -1;
5332 if (!empty)
5333 return 0;
5335 isl_basic_map_free(map->p[i]);
5336 if (i != map->n - 1) {
5337 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5338 map->p[i] = map->p[map->n - 1];
5340 map->n--;
5342 return 0;
5345 struct isl_map *isl_map_fix_si(struct isl_map *map,
5346 enum isl_dim_type type, unsigned pos, int value)
5348 int i;
5350 map = isl_map_cow(map);
5351 if (!map)
5352 return NULL;
5354 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5355 for (i = map->n - 1; i >= 0; --i) {
5356 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5357 if (remove_if_empty(map, i) < 0)
5358 goto error;
5360 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5361 return map;
5362 error:
5363 isl_map_free(map);
5364 return NULL;
5367 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5368 enum isl_dim_type type, unsigned pos, int value)
5370 return (struct isl_set *)
5371 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5374 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5375 enum isl_dim_type type, unsigned pos, isl_int value)
5377 int i;
5379 map = isl_map_cow(map);
5380 if (!map)
5381 return NULL;
5383 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5384 for (i = 0; i < map->n; ++i) {
5385 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5386 if (!map->p[i])
5387 goto error;
5389 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5390 return map;
5391 error:
5392 isl_map_free(map);
5393 return NULL;
5396 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5397 enum isl_dim_type type, unsigned pos, isl_int value)
5399 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5402 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5403 unsigned input, int value)
5405 return isl_map_fix_si(map, isl_dim_in, input, value);
5408 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5410 return (struct isl_set *)
5411 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5414 static __isl_give isl_basic_map *basic_map_bound_si(
5415 __isl_take isl_basic_map *bmap,
5416 enum isl_dim_type type, unsigned pos, int value, int upper)
5418 int j;
5420 if (!bmap)
5421 return NULL;
5422 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5423 pos += isl_basic_map_offset(bmap, type);
5424 bmap = isl_basic_map_cow(bmap);
5425 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5426 j = isl_basic_map_alloc_inequality(bmap);
5427 if (j < 0)
5428 goto error;
5429 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5430 if (upper) {
5431 isl_int_set_si(bmap->ineq[j][pos], -1);
5432 isl_int_set_si(bmap->ineq[j][0], value);
5433 } else {
5434 isl_int_set_si(bmap->ineq[j][pos], 1);
5435 isl_int_set_si(bmap->ineq[j][0], -value);
5437 bmap = isl_basic_map_simplify(bmap);
5438 return isl_basic_map_finalize(bmap);
5439 error:
5440 isl_basic_map_free(bmap);
5441 return NULL;
5444 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5445 __isl_take isl_basic_map *bmap,
5446 enum isl_dim_type type, unsigned pos, int value)
5448 return basic_map_bound_si(bmap, type, pos, value, 0);
5451 /* Constrain the values of the given dimension to be no greater than "value".
5453 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5454 __isl_take isl_basic_map *bmap,
5455 enum isl_dim_type type, unsigned pos, int value)
5457 return basic_map_bound_si(bmap, type, pos, value, 1);
5460 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5461 unsigned dim, isl_int value)
5463 int j;
5465 bset = isl_basic_set_cow(bset);
5466 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5467 j = isl_basic_set_alloc_inequality(bset);
5468 if (j < 0)
5469 goto error;
5470 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5471 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5472 isl_int_neg(bset->ineq[j][0], value);
5473 bset = isl_basic_set_simplify(bset);
5474 return isl_basic_set_finalize(bset);
5475 error:
5476 isl_basic_set_free(bset);
5477 return NULL;
5480 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5481 enum isl_dim_type type, unsigned pos, int value, int upper)
5483 int i;
5485 map = isl_map_cow(map);
5486 if (!map)
5487 return NULL;
5489 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5490 for (i = 0; i < map->n; ++i) {
5491 map->p[i] = basic_map_bound_si(map->p[i],
5492 type, pos, value, upper);
5493 if (!map->p[i])
5494 goto error;
5496 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5497 return map;
5498 error:
5499 isl_map_free(map);
5500 return NULL;
5503 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5504 enum isl_dim_type type, unsigned pos, int value)
5506 return map_bound_si(map, type, pos, value, 0);
5509 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5510 enum isl_dim_type type, unsigned pos, int value)
5512 return map_bound_si(map, type, pos, value, 1);
5515 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5516 enum isl_dim_type type, unsigned pos, int value)
5518 return (struct isl_set *)
5519 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5522 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5523 enum isl_dim_type type, unsigned pos, int value)
5525 return isl_map_upper_bound_si(set, type, pos, value);
5528 /* Bound the given variable of "bmap" from below (or above is "upper"
5529 * is set) to "value".
5531 static __isl_give isl_basic_map *basic_map_bound(
5532 __isl_take isl_basic_map *bmap,
5533 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5535 int j;
5537 if (!bmap)
5538 return NULL;
5539 if (pos >= isl_basic_map_dim(bmap, type))
5540 isl_die(bmap->ctx, isl_error_invalid,
5541 "index out of bounds", goto error);
5542 pos += isl_basic_map_offset(bmap, type);
5543 bmap = isl_basic_map_cow(bmap);
5544 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5545 j = isl_basic_map_alloc_inequality(bmap);
5546 if (j < 0)
5547 goto error;
5548 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5549 if (upper) {
5550 isl_int_set_si(bmap->ineq[j][pos], -1);
5551 isl_int_set(bmap->ineq[j][0], value);
5552 } else {
5553 isl_int_set_si(bmap->ineq[j][pos], 1);
5554 isl_int_neg(bmap->ineq[j][0], value);
5556 bmap = isl_basic_map_simplify(bmap);
5557 return isl_basic_map_finalize(bmap);
5558 error:
5559 isl_basic_map_free(bmap);
5560 return NULL;
5563 /* Bound the given variable of "map" from below (or above is "upper"
5564 * is set) to "value".
5566 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5567 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5569 int i;
5571 map = isl_map_cow(map);
5572 if (!map)
5573 return NULL;
5575 if (pos >= isl_map_dim(map, type))
5576 isl_die(map->ctx, isl_error_invalid,
5577 "index out of bounds", goto error);
5578 for (i = map->n - 1; i >= 0; --i) {
5579 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5580 if (remove_if_empty(map, i) < 0)
5581 goto error;
5583 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5584 return map;
5585 error:
5586 isl_map_free(map);
5587 return NULL;
5590 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5591 enum isl_dim_type type, unsigned pos, isl_int value)
5593 return map_bound(map, type, pos, value, 0);
5596 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5597 enum isl_dim_type type, unsigned pos, isl_int value)
5599 return map_bound(map, type, pos, value, 1);
5602 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5603 enum isl_dim_type type, unsigned pos, isl_int value)
5605 return isl_map_lower_bound(set, type, pos, value);
5608 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5609 enum isl_dim_type type, unsigned pos, isl_int value)
5611 return isl_map_upper_bound(set, type, pos, value);
5614 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5615 isl_int value)
5617 int i;
5619 set = isl_set_cow(set);
5620 if (!set)
5621 return NULL;
5623 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5624 for (i = 0; i < set->n; ++i) {
5625 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5626 if (!set->p[i])
5627 goto error;
5629 return set;
5630 error:
5631 isl_set_free(set);
5632 return NULL;
5635 struct isl_map *isl_map_reverse(struct isl_map *map)
5637 int i;
5639 map = isl_map_cow(map);
5640 if (!map)
5641 return NULL;
5643 map->dim = isl_space_reverse(map->dim);
5644 if (!map->dim)
5645 goto error;
5646 for (i = 0; i < map->n; ++i) {
5647 map->p[i] = isl_basic_map_reverse(map->p[i]);
5648 if (!map->p[i])
5649 goto error;
5651 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5652 return map;
5653 error:
5654 isl_map_free(map);
5655 return NULL;
5658 static struct isl_map *isl_basic_map_partial_lexopt(
5659 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5660 struct isl_set **empty, int max)
5662 if (!bmap)
5663 goto error;
5664 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5665 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5666 else
5667 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5668 error:
5669 isl_basic_map_free(bmap);
5670 isl_basic_set_free(dom);
5671 if (empty)
5672 *empty = NULL;
5673 return NULL;
5676 struct isl_map *isl_basic_map_partial_lexmax(
5677 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5678 struct isl_set **empty)
5680 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5683 struct isl_map *isl_basic_map_partial_lexmin(
5684 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5685 struct isl_set **empty)
5687 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5690 struct isl_set *isl_basic_set_partial_lexmin(
5691 struct isl_basic_set *bset, struct isl_basic_set *dom,
5692 struct isl_set **empty)
5694 return (struct isl_set *)
5695 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5696 dom, empty);
5699 struct isl_set *isl_basic_set_partial_lexmax(
5700 struct isl_basic_set *bset, struct isl_basic_set *dom,
5701 struct isl_set **empty)
5703 return (struct isl_set *)
5704 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5705 dom, empty);
5708 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5709 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5710 __isl_give isl_set **empty)
5712 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5715 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5716 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5717 __isl_give isl_set **empty)
5719 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5722 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5723 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5724 __isl_give isl_set **empty)
5726 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5729 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5730 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5731 __isl_give isl_set **empty)
5733 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5736 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5737 __isl_take isl_basic_map *bmap, int max)
5739 isl_basic_set *dom = NULL;
5740 isl_space *dom_space;
5742 if (!bmap)
5743 goto error;
5744 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5745 dom = isl_basic_set_universe(dom_space);
5746 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5747 error:
5748 isl_basic_map_free(bmap);
5749 return NULL;
5752 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5753 __isl_take isl_basic_map *bmap)
5755 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5758 #undef TYPE
5759 #define TYPE isl_pw_multi_aff
5760 #undef SUFFIX
5761 #define SUFFIX _pw_multi_aff
5762 #undef EMPTY
5763 #define EMPTY isl_pw_multi_aff_empty
5764 #undef ADD
5765 #define ADD isl_pw_multi_aff_union_add
5766 #include "isl_map_lexopt_templ.c"
5768 /* Given a map "map", compute the lexicographically minimal
5769 * (or maximal) image element for each domain element in dom,
5770 * in the form of an isl_pw_multi_aff.
5771 * Set *empty to those elements in dom that do not have an image element.
5773 * We first compute the lexicographically minimal or maximal element
5774 * in the first basic map. This results in a partial solution "res"
5775 * and a subset "todo" of dom that still need to be handled.
5776 * We then consider each of the remaining maps in "map" and successively
5777 * update both "res" and "todo".
5779 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
5780 __isl_take isl_map *map, __isl_take isl_set *dom,
5781 __isl_give isl_set **empty, int max)
5783 int i;
5784 isl_pw_multi_aff *res;
5785 isl_set *todo;
5787 if (!map || !dom)
5788 goto error;
5790 if (isl_map_plain_is_empty(map)) {
5791 if (empty)
5792 *empty = dom;
5793 else
5794 isl_set_free(dom);
5795 return isl_pw_multi_aff_from_map(map);
5798 res = basic_map_partial_lexopt_pw_multi_aff(
5799 isl_basic_map_copy(map->p[0]),
5800 isl_set_copy(dom), &todo, max);
5802 for (i = 1; i < map->n; ++i) {
5803 isl_pw_multi_aff *res_i;
5804 isl_set *todo_i;
5806 res_i = basic_map_partial_lexopt_pw_multi_aff(
5807 isl_basic_map_copy(map->p[i]),
5808 isl_set_copy(dom), &todo_i, max);
5810 if (max)
5811 res = isl_pw_multi_aff_union_lexmax(res, res_i);
5812 else
5813 res = isl_pw_multi_aff_union_lexmin(res, res_i);
5815 todo = isl_set_intersect(todo, todo_i);
5818 isl_set_free(dom);
5819 isl_map_free(map);
5821 if (empty)
5822 *empty = todo;
5823 else
5824 isl_set_free(todo);
5826 return res;
5827 error:
5828 if (empty)
5829 *empty = NULL;
5830 isl_set_free(dom);
5831 isl_map_free(map);
5832 return NULL;
5835 #undef TYPE
5836 #define TYPE isl_map
5837 #undef SUFFIX
5838 #define SUFFIX
5839 #undef EMPTY
5840 #define EMPTY isl_map_empty
5841 #undef ADD
5842 #define ADD isl_map_union_disjoint
5843 #include "isl_map_lexopt_templ.c"
5845 /* Given a map "map", compute the lexicographically minimal
5846 * (or maximal) image element for each domain element in dom.
5847 * Set *empty to those elements in dom that do not have an image element.
5849 * We first compute the lexicographically minimal or maximal element
5850 * in the first basic map. This results in a partial solution "res"
5851 * and a subset "todo" of dom that still need to be handled.
5852 * We then consider each of the remaining maps in "map" and successively
5853 * update both "res" and "todo".
5855 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5856 * Assume we are computing the lexicographical maximum.
5857 * We first compute the lexicographically maximal element in basic map i.
5858 * This results in a partial solution res_i and a subset todo_i.
5859 * Then we combine these results with those obtain for the first k basic maps
5860 * to obtain a result that is valid for the first k+1 basic maps.
5861 * In particular, the set where there is no solution is the set where
5862 * there is no solution for the first k basic maps and also no solution
5863 * for the ith basic map, i.e.,
5865 * todo^i = todo^k * todo_i
5867 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5868 * solutions, arbitrarily breaking ties in favor of res^k.
5869 * That is, when res^k(a) >= res_i(a), we pick res^k and
5870 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5871 * the lexicographic order.)
5872 * In practice, we compute
5874 * res^k * (res_i . "<=")
5876 * and
5878 * res_i * (res^k . "<")
5880 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5881 * where only one of res^k and res_i provides a solution and we simply pick
5882 * that one, i.e.,
5884 * res^k * todo_i
5885 * and
5886 * res_i * todo^k
5888 * Note that we only compute these intersections when dom(res^k) intersects
5889 * dom(res_i). Otherwise, the only effect of these intersections is to
5890 * potentially break up res^k and res_i into smaller pieces.
5891 * We want to avoid such splintering as much as possible.
5892 * In fact, an earlier implementation of this function would look for
5893 * better results in the domain of res^k and for extra results in todo^k,
5894 * but this would always result in a splintering according to todo^k,
5895 * even when the domain of basic map i is disjoint from the domains of
5896 * the previous basic maps.
5898 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5899 __isl_take isl_map *map, __isl_take isl_set *dom,
5900 __isl_give isl_set **empty, int max)
5902 int i;
5903 struct isl_map *res;
5904 struct isl_set *todo;
5906 if (!map || !dom)
5907 goto error;
5909 if (isl_map_plain_is_empty(map)) {
5910 if (empty)
5911 *empty = dom;
5912 else
5913 isl_set_free(dom);
5914 return map;
5917 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5918 isl_set_copy(dom), &todo, max);
5920 for (i = 1; i < map->n; ++i) {
5921 isl_map *lt, *le;
5922 isl_map *res_i;
5923 isl_set *todo_i;
5924 isl_space *dim = isl_space_range(isl_map_get_space(res));
5926 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5927 isl_set_copy(dom), &todo_i, max);
5929 if (max) {
5930 lt = isl_map_lex_lt(isl_space_copy(dim));
5931 le = isl_map_lex_le(dim);
5932 } else {
5933 lt = isl_map_lex_gt(isl_space_copy(dim));
5934 le = isl_map_lex_ge(dim);
5936 lt = isl_map_apply_range(isl_map_copy(res), lt);
5937 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5938 le = isl_map_apply_range(isl_map_copy(res_i), le);
5939 le = isl_map_intersect(le, isl_map_copy(res));
5941 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5942 res = isl_map_intersect_domain(res,
5943 isl_set_copy(todo_i));
5944 res_i = isl_map_intersect_domain(res_i,
5945 isl_set_copy(todo));
5948 res = isl_map_union_disjoint(res, res_i);
5949 res = isl_map_union_disjoint(res, lt);
5950 res = isl_map_union_disjoint(res, le);
5952 todo = isl_set_intersect(todo, todo_i);
5955 isl_set_free(dom);
5956 isl_map_free(map);
5958 if (empty)
5959 *empty = todo;
5960 else
5961 isl_set_free(todo);
5963 return res;
5964 error:
5965 if (empty)
5966 *empty = NULL;
5967 isl_set_free(dom);
5968 isl_map_free(map);
5969 return NULL;
5972 __isl_give isl_map *isl_map_partial_lexmax(
5973 __isl_take isl_map *map, __isl_take isl_set *dom,
5974 __isl_give isl_set **empty)
5976 return isl_map_partial_lexopt(map, dom, empty, 1);
5979 __isl_give isl_map *isl_map_partial_lexmin(
5980 __isl_take isl_map *map, __isl_take isl_set *dom,
5981 __isl_give isl_set **empty)
5983 return isl_map_partial_lexopt(map, dom, empty, 0);
5986 __isl_give isl_set *isl_set_partial_lexmin(
5987 __isl_take isl_set *set, __isl_take isl_set *dom,
5988 __isl_give isl_set **empty)
5990 return (struct isl_set *)
5991 isl_map_partial_lexmin((struct isl_map *)set,
5992 dom, empty);
5995 __isl_give isl_set *isl_set_partial_lexmax(
5996 __isl_take isl_set *set, __isl_take isl_set *dom,
5997 __isl_give isl_set **empty)
5999 return (struct isl_set *)
6000 isl_map_partial_lexmax((struct isl_map *)set,
6001 dom, empty);
6004 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6006 struct isl_basic_set *dom = NULL;
6007 isl_space *dom_dim;
6009 if (!bmap)
6010 goto error;
6011 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
6012 dom = isl_basic_set_universe(dom_dim);
6013 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6014 error:
6015 isl_basic_map_free(bmap);
6016 return NULL;
6019 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6021 return isl_basic_map_lexopt(bmap, 0);
6024 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6026 return isl_basic_map_lexopt(bmap, 1);
6029 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6031 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6034 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6036 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6039 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
6041 return (isl_set *)isl_map_lexmin((isl_map *)set);
6044 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
6046 return (isl_set *)isl_map_lexmax((isl_map *)set);
6049 /* Extract the first and only affine expression from list
6050 * and then add it to *pwaff with the given dom.
6051 * This domain is known to be disjoint from other domains
6052 * because of the way isl_basic_map_foreach_lexmax works.
6054 static int update_dim_opt(__isl_take isl_basic_set *dom,
6055 __isl_take isl_aff_list *list, void *user)
6057 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6058 isl_aff *aff;
6059 isl_pw_aff **pwaff = user;
6060 isl_pw_aff *pwaff_i;
6062 if (!list)
6063 goto error;
6064 if (isl_aff_list_n_aff(list) != 1)
6065 isl_die(ctx, isl_error_internal,
6066 "expecting single element list", goto error);
6068 aff = isl_aff_list_get_aff(list, 0);
6069 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6071 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6073 isl_aff_list_free(list);
6075 return 0;
6076 error:
6077 isl_basic_set_free(dom);
6078 isl_aff_list_free(list);
6079 return -1;
6082 /* Given a basic map with one output dimension, compute the minimum or
6083 * maximum of that dimension as an isl_pw_aff.
6085 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6086 * call update_dim_opt on each leaf of the result.
6088 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6089 int max)
6091 isl_space *dim = isl_basic_map_get_space(bmap);
6092 isl_pw_aff *pwaff;
6093 int r;
6095 dim = isl_space_from_domain(isl_space_domain(dim));
6096 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6097 pwaff = isl_pw_aff_empty(dim);
6099 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6100 if (r < 0)
6101 return isl_pw_aff_free(pwaff);
6103 return pwaff;
6106 /* Compute the minimum or maximum of the given output dimension
6107 * as a function of the parameters and the input dimensions,
6108 * but independently of the other output dimensions.
6110 * We first project out the other output dimension and then compute
6111 * the "lexicographic" maximum in each basic map, combining the results
6112 * using isl_pw_aff_union_max.
6114 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6115 int max)
6117 int i;
6118 isl_pw_aff *pwaff;
6119 unsigned n_out;
6121 n_out = isl_map_dim(map, isl_dim_out);
6122 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6123 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6124 if (!map)
6125 return NULL;
6127 if (map->n == 0) {
6128 isl_space *dim = isl_map_get_space(map);
6129 dim = isl_space_domain(isl_space_from_range(dim));
6130 isl_map_free(map);
6131 return isl_pw_aff_empty(dim);
6134 pwaff = basic_map_dim_opt(map->p[0], max);
6135 for (i = 1; i < map->n; ++i) {
6136 isl_pw_aff *pwaff_i;
6138 pwaff_i = basic_map_dim_opt(map->p[i], max);
6139 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6142 isl_map_free(map);
6144 return pwaff;
6147 /* Compute the maximum of the given output dimension as a function of the
6148 * parameters and input dimensions, but independently of
6149 * the other output dimensions.
6151 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6153 return map_dim_opt(map, pos, 1);
6156 /* Compute the minimum or maximum of the given set dimension
6157 * as a function of the parameters,
6158 * but independently of the other set dimensions.
6160 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6161 int max)
6163 return map_dim_opt(set, pos, max);
6166 /* Compute the maximum of the given set dimension as a function of the
6167 * parameters, but independently of the other set dimensions.
6169 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6171 return set_dim_opt(set, pos, 1);
6174 /* Compute the minimum of the given set dimension as a function of the
6175 * parameters, but independently of the other set dimensions.
6177 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6179 return set_dim_opt(set, pos, 0);
6182 /* Apply a preimage specified by "mat" on the parameters of "bset".
6183 * bset is assumed to have only parameters and divs.
6185 static struct isl_basic_set *basic_set_parameter_preimage(
6186 struct isl_basic_set *bset, struct isl_mat *mat)
6188 unsigned nparam;
6190 if (!bset || !mat)
6191 goto error;
6193 bset->dim = isl_space_cow(bset->dim);
6194 if (!bset->dim)
6195 goto error;
6197 nparam = isl_basic_set_dim(bset, isl_dim_param);
6199 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6201 bset->dim->nparam = 0;
6202 bset->dim->n_out = nparam;
6203 bset = isl_basic_set_preimage(bset, mat);
6204 if (bset) {
6205 bset->dim->nparam = bset->dim->n_out;
6206 bset->dim->n_out = 0;
6208 return bset;
6209 error:
6210 isl_mat_free(mat);
6211 isl_basic_set_free(bset);
6212 return NULL;
6215 /* Apply a preimage specified by "mat" on the parameters of "set".
6216 * set is assumed to have only parameters and divs.
6218 static struct isl_set *set_parameter_preimage(
6219 struct isl_set *set, struct isl_mat *mat)
6221 isl_space *dim = NULL;
6222 unsigned nparam;
6224 if (!set || !mat)
6225 goto error;
6227 dim = isl_space_copy(set->dim);
6228 dim = isl_space_cow(dim);
6229 if (!dim)
6230 goto error;
6232 nparam = isl_set_dim(set, isl_dim_param);
6234 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6236 dim->nparam = 0;
6237 dim->n_out = nparam;
6238 isl_set_reset_space(set, dim);
6239 set = isl_set_preimage(set, mat);
6240 if (!set)
6241 goto error2;
6242 dim = isl_space_copy(set->dim);
6243 dim = isl_space_cow(dim);
6244 if (!dim)
6245 goto error2;
6246 dim->nparam = dim->n_out;
6247 dim->n_out = 0;
6248 isl_set_reset_space(set, dim);
6249 return set;
6250 error:
6251 isl_space_free(dim);
6252 isl_mat_free(mat);
6253 error2:
6254 isl_set_free(set);
6255 return NULL;
6258 /* Intersect the basic set "bset" with the affine space specified by the
6259 * equalities in "eq".
6261 static struct isl_basic_set *basic_set_append_equalities(
6262 struct isl_basic_set *bset, struct isl_mat *eq)
6264 int i, k;
6265 unsigned len;
6267 if (!bset || !eq)
6268 goto error;
6270 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6271 eq->n_row, 0);
6272 if (!bset)
6273 goto error;
6275 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6276 for (i = 0; i < eq->n_row; ++i) {
6277 k = isl_basic_set_alloc_equality(bset);
6278 if (k < 0)
6279 goto error;
6280 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6281 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6283 isl_mat_free(eq);
6285 bset = isl_basic_set_gauss(bset, NULL);
6286 bset = isl_basic_set_finalize(bset);
6288 return bset;
6289 error:
6290 isl_mat_free(eq);
6291 isl_basic_set_free(bset);
6292 return NULL;
6295 /* Intersect the set "set" with the affine space specified by the
6296 * equalities in "eq".
6298 static struct isl_set *set_append_equalities(struct isl_set *set,
6299 struct isl_mat *eq)
6301 int i;
6303 if (!set || !eq)
6304 goto error;
6306 for (i = 0; i < set->n; ++i) {
6307 set->p[i] = basic_set_append_equalities(set->p[i],
6308 isl_mat_copy(eq));
6309 if (!set->p[i])
6310 goto error;
6312 isl_mat_free(eq);
6313 return set;
6314 error:
6315 isl_mat_free(eq);
6316 isl_set_free(set);
6317 return NULL;
6320 /* Project the given basic set onto its parameter domain, possibly introducing
6321 * new, explicit, existential variables in the constraints.
6322 * The input has parameters and (possibly implicit) existential variables.
6323 * The output has the same parameters, but only
6324 * explicit existentially quantified variables.
6326 * The actual projection is performed by pip, but pip doesn't seem
6327 * to like equalities very much, so we first remove the equalities
6328 * among the parameters by performing a variable compression on
6329 * the parameters. Afterward, an inverse transformation is performed
6330 * and the equalities among the parameters are inserted back in.
6332 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6334 int i, j;
6335 struct isl_mat *eq;
6336 struct isl_mat *T, *T2;
6337 struct isl_set *set;
6338 unsigned nparam, n_div;
6340 bset = isl_basic_set_cow(bset);
6341 if (!bset)
6342 return NULL;
6344 if (bset->n_eq == 0)
6345 return isl_basic_set_lexmin(bset);
6347 bset = isl_basic_set_gauss(bset, NULL);
6348 if (!bset)
6349 return NULL;
6350 if (isl_basic_set_plain_is_empty(bset))
6351 return isl_set_from_basic_set(bset);
6353 nparam = isl_basic_set_dim(bset, isl_dim_param);
6354 n_div = isl_basic_set_dim(bset, isl_dim_div);
6356 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6357 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6358 ++i;
6360 if (i == bset->n_eq)
6361 return isl_basic_set_lexmin(bset);
6363 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6364 0, 1 + nparam);
6365 eq = isl_mat_cow(eq);
6366 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6367 if (T && T->n_col == 0) {
6368 isl_mat_free(T);
6369 isl_mat_free(T2);
6370 isl_mat_free(eq);
6371 bset = isl_basic_set_set_to_empty(bset);
6372 return isl_set_from_basic_set(bset);
6374 bset = basic_set_parameter_preimage(bset, T);
6376 set = isl_basic_set_lexmin(bset);
6377 set = set_parameter_preimage(set, T2);
6378 set = set_append_equalities(set, eq);
6379 return set;
6382 /* Compute an explicit representation for all the existentially
6383 * quantified variables.
6384 * The input and output dimensions are first turned into parameters.
6385 * compute_divs then returns a map with the same parameters and
6386 * no input or output dimensions and the dimension specification
6387 * is reset to that of the input.
6389 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6391 struct isl_basic_set *bset;
6392 struct isl_set *set;
6393 struct isl_map *map;
6394 isl_space *dim, *orig_dim = NULL;
6395 unsigned nparam;
6396 unsigned n_in;
6397 unsigned n_out;
6399 bmap = isl_basic_map_cow(bmap);
6400 if (!bmap)
6401 return NULL;
6403 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6404 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6405 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6406 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
6407 if (!dim)
6408 goto error;
6410 orig_dim = bmap->dim;
6411 bmap->dim = dim;
6412 bset = (struct isl_basic_set *)bmap;
6414 set = parameter_compute_divs(bset);
6415 map = (struct isl_map *)set;
6416 map = isl_map_reset_space(map, orig_dim);
6418 return map;
6419 error:
6420 isl_basic_map_free(bmap);
6421 return NULL;
6424 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6426 int i;
6427 unsigned off;
6429 if (!bmap)
6430 return -1;
6432 off = isl_space_dim(bmap->dim, isl_dim_all);
6433 for (i = 0; i < bmap->n_div; ++i) {
6434 if (isl_int_is_zero(bmap->div[i][0]))
6435 return 0;
6436 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6437 return -1);
6439 return 1;
6442 static int map_divs_known(__isl_keep isl_map *map)
6444 int i;
6446 if (!map)
6447 return -1;
6449 for (i = 0; i < map->n; ++i) {
6450 int known = isl_basic_map_divs_known(map->p[i]);
6451 if (known <= 0)
6452 return known;
6455 return 1;
6458 /* If bmap contains any unknown divs, then compute explicit
6459 * expressions for them. However, this computation may be
6460 * quite expensive, so first try to remove divs that aren't
6461 * strictly needed.
6463 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6465 int known;
6466 struct isl_map *map;
6468 known = isl_basic_map_divs_known(bmap);
6469 if (known < 0)
6470 goto error;
6471 if (known)
6472 return isl_map_from_basic_map(bmap);
6474 bmap = isl_basic_map_drop_redundant_divs(bmap);
6476 known = isl_basic_map_divs_known(bmap);
6477 if (known < 0)
6478 goto error;
6479 if (known)
6480 return isl_map_from_basic_map(bmap);
6482 map = compute_divs(bmap);
6483 return map;
6484 error:
6485 isl_basic_map_free(bmap);
6486 return NULL;
6489 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6491 int i;
6492 int known;
6493 struct isl_map *res;
6495 if (!map)
6496 return NULL;
6497 if (map->n == 0)
6498 return map;
6500 known = map_divs_known(map);
6501 if (known < 0) {
6502 isl_map_free(map);
6503 return NULL;
6505 if (known)
6506 return map;
6508 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6509 for (i = 1 ; i < map->n; ++i) {
6510 struct isl_map *r2;
6511 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6512 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6513 res = isl_map_union_disjoint(res, r2);
6514 else
6515 res = isl_map_union(res, r2);
6517 isl_map_free(map);
6519 return res;
6522 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6524 return (struct isl_set *)
6525 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6528 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6530 return (struct isl_set *)
6531 isl_map_compute_divs((struct isl_map *)set);
6534 struct isl_set *isl_map_domain(struct isl_map *map)
6536 int i;
6537 struct isl_set *set;
6539 if (!map)
6540 goto error;
6542 map = isl_map_cow(map);
6543 if (!map)
6544 return NULL;
6546 set = (struct isl_set *)map;
6547 set->dim = isl_space_domain(set->dim);
6548 if (!set->dim)
6549 goto error;
6550 for (i = 0; i < map->n; ++i) {
6551 set->p[i] = isl_basic_map_domain(map->p[i]);
6552 if (!set->p[i])
6553 goto error;
6555 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6556 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6557 return set;
6558 error:
6559 isl_map_free(map);
6560 return NULL;
6563 /* Return the union of "map1" and "map2", where we assume for now that
6564 * "map1" and "map2" are disjoint. Note that the basic maps inside
6565 * "map1" or "map2" may not be disjoint from each other.
6566 * Also note that this function is also called from isl_map_union,
6567 * which takes care of handling the situation where "map1" and "map2"
6568 * may not be disjoint.
6570 * If one of the inputs is empty, we can simply return the other input.
6571 * Similarly, if one of the inputs is universal, then it is equal to the union.
6573 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6574 __isl_take isl_map *map2)
6576 int i;
6577 unsigned flags = 0;
6578 struct isl_map *map = NULL;
6579 int is_universe;
6581 if (!map1 || !map2)
6582 goto error;
6584 if (map1->n == 0) {
6585 isl_map_free(map1);
6586 return map2;
6588 if (map2->n == 0) {
6589 isl_map_free(map2);
6590 return map1;
6593 is_universe = isl_map_plain_is_universe(map1);
6594 if (is_universe < 0)
6595 goto error;
6596 if (is_universe) {
6597 isl_map_free(map2);
6598 return map1;
6601 is_universe = isl_map_plain_is_universe(map2);
6602 if (is_universe < 0)
6603 goto error;
6604 if (is_universe) {
6605 isl_map_free(map1);
6606 return map2;
6609 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6611 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6612 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6613 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6615 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6616 map1->n + map2->n, flags);
6617 if (!map)
6618 goto error;
6619 for (i = 0; i < map1->n; ++i) {
6620 map = isl_map_add_basic_map(map,
6621 isl_basic_map_copy(map1->p[i]));
6622 if (!map)
6623 goto error;
6625 for (i = 0; i < map2->n; ++i) {
6626 map = isl_map_add_basic_map(map,
6627 isl_basic_map_copy(map2->p[i]));
6628 if (!map)
6629 goto error;
6631 isl_map_free(map1);
6632 isl_map_free(map2);
6633 return map;
6634 error:
6635 isl_map_free(map);
6636 isl_map_free(map1);
6637 isl_map_free(map2);
6638 return NULL;
6641 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6642 __isl_take isl_map *map2)
6644 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6647 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6649 map1 = isl_map_union_disjoint(map1, map2);
6650 if (!map1)
6651 return NULL;
6652 if (map1->n > 1)
6653 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6654 return map1;
6657 struct isl_set *isl_set_union_disjoint(
6658 struct isl_set *set1, struct isl_set *set2)
6660 return (struct isl_set *)
6661 isl_map_union_disjoint(
6662 (struct isl_map *)set1, (struct isl_map *)set2);
6665 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6667 return (struct isl_set *)
6668 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6671 /* Apply "fn" to pairs of elements from "map" and "set" and collect
6672 * the results.
6674 * "map" and "set" are assumed to be compatible and non-NULL.
6676 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
6677 __isl_take isl_set *set,
6678 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
6679 __isl_take isl_basic_set *bset))
6681 unsigned flags = 0;
6682 struct isl_map *result;
6683 int i, j;
6685 if (isl_set_plain_is_universe(set)) {
6686 isl_set_free(set);
6687 return map;
6690 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6691 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6692 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6694 result = isl_map_alloc_space(isl_space_copy(map->dim),
6695 map->n * set->n, flags);
6696 for (i = 0; result && i < map->n; ++i)
6697 for (j = 0; j < set->n; ++j) {
6698 result = isl_map_add_basic_map(result,
6699 fn(isl_basic_map_copy(map->p[i]),
6700 isl_basic_set_copy(set->p[j])));
6701 if (!result)
6702 break;
6705 isl_map_free(map);
6706 isl_set_free(set);
6707 return result;
6710 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6711 __isl_take isl_set *set)
6713 if (!map || !set)
6714 goto error;
6716 if (!isl_map_compatible_range(map, set))
6717 isl_die(set->ctx, isl_error_invalid,
6718 "incompatible spaces", goto error);
6720 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
6721 error:
6722 isl_map_free(map);
6723 isl_set_free(set);
6724 return NULL;
6727 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6728 __isl_take isl_set *set)
6730 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6733 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
6734 __isl_take isl_set *set)
6736 if (!map || !set)
6737 goto error;
6739 if (!isl_map_compatible_domain(map, set))
6740 isl_die(set->ctx, isl_error_invalid,
6741 "incompatible spaces", goto error);
6743 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
6744 error:
6745 isl_map_free(map);
6746 isl_set_free(set);
6747 return NULL;
6750 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
6751 __isl_take isl_set *set)
6753 return isl_map_align_params_map_map_and(map, set,
6754 &map_intersect_domain);
6757 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6758 __isl_take isl_map *map2)
6760 if (!map1 || !map2)
6761 goto error;
6762 map1 = isl_map_reverse(map1);
6763 map1 = isl_map_apply_range(map1, map2);
6764 return isl_map_reverse(map1);
6765 error:
6766 isl_map_free(map1);
6767 isl_map_free(map2);
6768 return NULL;
6771 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6772 __isl_take isl_map *map2)
6774 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6777 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6778 __isl_take isl_map *map2)
6780 isl_space *dim_result;
6781 struct isl_map *result;
6782 int i, j;
6784 if (!map1 || !map2)
6785 goto error;
6787 dim_result = isl_space_join(isl_space_copy(map1->dim),
6788 isl_space_copy(map2->dim));
6790 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6791 if (!result)
6792 goto error;
6793 for (i = 0; i < map1->n; ++i)
6794 for (j = 0; j < map2->n; ++j) {
6795 result = isl_map_add_basic_map(result,
6796 isl_basic_map_apply_range(
6797 isl_basic_map_copy(map1->p[i]),
6798 isl_basic_map_copy(map2->p[j])));
6799 if (!result)
6800 goto error;
6802 isl_map_free(map1);
6803 isl_map_free(map2);
6804 if (result && result->n <= 1)
6805 ISL_F_SET(result, ISL_MAP_DISJOINT);
6806 return result;
6807 error:
6808 isl_map_free(map1);
6809 isl_map_free(map2);
6810 return NULL;
6813 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6814 __isl_take isl_map *map2)
6816 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6820 * returns range - domain
6822 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6824 isl_space *dims, *target_dim;
6825 struct isl_basic_set *bset;
6826 unsigned dim;
6827 unsigned nparam;
6828 int i;
6830 if (!bmap)
6831 goto error;
6832 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6833 bmap->dim, isl_dim_out),
6834 goto error);
6835 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6836 dim = isl_basic_map_n_in(bmap);
6837 nparam = isl_basic_map_n_param(bmap);
6838 bset = isl_basic_set_from_basic_map(bmap);
6839 bset = isl_basic_set_cow(bset);
6840 dims = isl_basic_set_get_space(bset);
6841 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6842 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6843 bset = isl_basic_set_swap_vars(bset, 2*dim);
6844 for (i = 0; i < dim; ++i) {
6845 int j = isl_basic_map_alloc_equality(
6846 (struct isl_basic_map *)bset);
6847 if (j < 0) {
6848 bset = isl_basic_set_free(bset);
6849 break;
6851 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6852 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6853 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6854 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6856 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6857 bset = isl_basic_set_reset_space(bset, target_dim);
6858 return bset;
6859 error:
6860 isl_basic_map_free(bmap);
6861 return NULL;
6865 * returns range - domain
6867 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
6869 int i;
6870 isl_space *dim;
6871 struct isl_set *result;
6873 if (!map)
6874 return NULL;
6876 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6877 map->dim, isl_dim_out),
6878 goto error);
6879 dim = isl_map_get_space(map);
6880 dim = isl_space_domain(dim);
6881 result = isl_set_alloc_space(dim, map->n, 0);
6882 if (!result)
6883 goto error;
6884 for (i = 0; i < map->n; ++i)
6885 result = isl_set_add_basic_set(result,
6886 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6887 isl_map_free(map);
6888 return result;
6889 error:
6890 isl_map_free(map);
6891 return NULL;
6895 * returns [domain -> range] -> range - domain
6897 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6898 __isl_take isl_basic_map *bmap)
6900 int i, k;
6901 isl_space *dim;
6902 isl_basic_map *domain;
6903 int nparam, n;
6904 unsigned total;
6906 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6907 isl_die(bmap->ctx, isl_error_invalid,
6908 "domain and range don't match", goto error);
6910 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6911 n = isl_basic_map_dim(bmap, isl_dim_in);
6913 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6914 domain = isl_basic_map_universe(dim);
6916 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6917 bmap = isl_basic_map_apply_range(bmap, domain);
6918 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6920 total = isl_basic_map_total_dim(bmap);
6922 for (i = 0; i < n; ++i) {
6923 k = isl_basic_map_alloc_equality(bmap);
6924 if (k < 0)
6925 goto error;
6926 isl_seq_clr(bmap->eq[k], 1 + total);
6927 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6928 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6929 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6932 bmap = isl_basic_map_gauss(bmap, NULL);
6933 return isl_basic_map_finalize(bmap);
6934 error:
6935 isl_basic_map_free(bmap);
6936 return NULL;
6940 * returns [domain -> range] -> range - domain
6942 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6944 int i;
6945 isl_space *domain_dim;
6947 if (!map)
6948 return NULL;
6950 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6951 isl_die(map->ctx, isl_error_invalid,
6952 "domain and range don't match", goto error);
6954 map = isl_map_cow(map);
6955 if (!map)
6956 return NULL;
6958 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6959 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6960 map->dim = isl_space_join(map->dim, domain_dim);
6961 if (!map->dim)
6962 goto error;
6963 for (i = 0; i < map->n; ++i) {
6964 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6965 if (!map->p[i])
6966 goto error;
6968 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6969 return map;
6970 error:
6971 isl_map_free(map);
6972 return NULL;
6975 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6977 struct isl_basic_map *bmap;
6978 unsigned nparam;
6979 unsigned dim;
6980 int i;
6982 if (!dims)
6983 return NULL;
6985 nparam = dims->nparam;
6986 dim = dims->n_out;
6987 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6988 if (!bmap)
6989 goto error;
6991 for (i = 0; i < dim; ++i) {
6992 int j = isl_basic_map_alloc_equality(bmap);
6993 if (j < 0)
6994 goto error;
6995 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6996 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6997 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6999 return isl_basic_map_finalize(bmap);
7000 error:
7001 isl_basic_map_free(bmap);
7002 return NULL;
7005 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7007 if (!dim)
7008 return NULL;
7009 if (dim->n_in != dim->n_out)
7010 isl_die(dim->ctx, isl_error_invalid,
7011 "number of input and output dimensions needs to be "
7012 "the same", goto error);
7013 return basic_map_identity(dim);
7014 error:
7015 isl_space_free(dim);
7016 return NULL;
7019 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7021 if (!model || !model->dim)
7022 return NULL;
7023 return isl_basic_map_identity(isl_space_copy(model->dim));
7026 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7028 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7031 struct isl_map *isl_map_identity_like(struct isl_map *model)
7033 if (!model || !model->dim)
7034 return NULL;
7035 return isl_map_identity(isl_space_copy(model->dim));
7038 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7040 if (!model || !model->dim)
7041 return NULL;
7042 return isl_map_identity(isl_space_copy(model->dim));
7045 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7047 isl_space *dim = isl_set_get_space(set);
7048 isl_map *id;
7049 id = isl_map_identity(isl_space_map_from_set(dim));
7050 return isl_map_intersect_range(id, set);
7053 /* Construct a basic set with all set dimensions having only non-negative
7054 * values.
7056 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7057 __isl_take isl_space *space)
7059 int i;
7060 unsigned nparam;
7061 unsigned dim;
7062 struct isl_basic_set *bset;
7064 if (!space)
7065 return NULL;
7066 nparam = space->nparam;
7067 dim = space->n_out;
7068 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7069 if (!bset)
7070 return NULL;
7071 for (i = 0; i < dim; ++i) {
7072 int k = isl_basic_set_alloc_inequality(bset);
7073 if (k < 0)
7074 goto error;
7075 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7076 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7078 return bset;
7079 error:
7080 isl_basic_set_free(bset);
7081 return NULL;
7084 /* Construct the half-space x_pos >= 0.
7086 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7087 int pos)
7089 int k;
7090 isl_basic_set *nonneg;
7092 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7093 k = isl_basic_set_alloc_inequality(nonneg);
7094 if (k < 0)
7095 goto error;
7096 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7097 isl_int_set_si(nonneg->ineq[k][pos], 1);
7099 return isl_basic_set_finalize(nonneg);
7100 error:
7101 isl_basic_set_free(nonneg);
7102 return NULL;
7105 /* Construct the half-space x_pos <= -1.
7107 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7109 int k;
7110 isl_basic_set *neg;
7112 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7113 k = isl_basic_set_alloc_inequality(neg);
7114 if (k < 0)
7115 goto error;
7116 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7117 isl_int_set_si(neg->ineq[k][0], -1);
7118 isl_int_set_si(neg->ineq[k][pos], -1);
7120 return isl_basic_set_finalize(neg);
7121 error:
7122 isl_basic_set_free(neg);
7123 return NULL;
7126 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7127 enum isl_dim_type type, unsigned first, unsigned n)
7129 int i;
7130 isl_basic_set *nonneg;
7131 isl_basic_set *neg;
7133 if (!set)
7134 return NULL;
7135 if (n == 0)
7136 return set;
7138 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7140 for (i = 0; i < n; ++i) {
7141 nonneg = nonneg_halfspace(isl_set_get_space(set),
7142 pos(set->dim, type) + first + i);
7143 neg = neg_halfspace(isl_set_get_space(set),
7144 pos(set->dim, type) + first + i);
7146 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7149 return set;
7150 error:
7151 isl_set_free(set);
7152 return NULL;
7155 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7156 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7157 void *user)
7159 isl_set *half;
7161 if (!set)
7162 return -1;
7163 if (isl_set_plain_is_empty(set)) {
7164 isl_set_free(set);
7165 return 0;
7167 if (first == len)
7168 return fn(set, signs, user);
7170 signs[first] = 1;
7171 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7172 1 + first));
7173 half = isl_set_intersect(half, isl_set_copy(set));
7174 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7175 goto error;
7177 signs[first] = -1;
7178 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7179 1 + first));
7180 half = isl_set_intersect(half, set);
7181 return foreach_orthant(half, signs, first + 1, len, fn, user);
7182 error:
7183 isl_set_free(set);
7184 return -1;
7187 /* Call "fn" on the intersections of "set" with each of the orthants
7188 * (except for obviously empty intersections). The orthant is identified
7189 * by the signs array, with each entry having value 1 or -1 according
7190 * to the sign of the corresponding variable.
7192 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7193 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7194 void *user)
7196 unsigned nparam;
7197 unsigned nvar;
7198 int *signs;
7199 int r;
7201 if (!set)
7202 return -1;
7203 if (isl_set_plain_is_empty(set))
7204 return 0;
7206 nparam = isl_set_dim(set, isl_dim_param);
7207 nvar = isl_set_dim(set, isl_dim_set);
7209 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7211 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7212 fn, user);
7214 free(signs);
7216 return r;
7219 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7221 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7224 int isl_basic_map_is_subset(
7225 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7227 int is_subset;
7228 struct isl_map *map1;
7229 struct isl_map *map2;
7231 if (!bmap1 || !bmap2)
7232 return -1;
7234 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7235 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7237 is_subset = isl_map_is_subset(map1, map2);
7239 isl_map_free(map1);
7240 isl_map_free(map2);
7242 return is_subset;
7245 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7246 __isl_keep isl_basic_set *bset2)
7248 return isl_basic_map_is_subset(bset1, bset2);
7251 int isl_basic_map_is_equal(
7252 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7254 int is_subset;
7256 if (!bmap1 || !bmap2)
7257 return -1;
7258 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7259 if (is_subset != 1)
7260 return is_subset;
7261 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7262 return is_subset;
7265 int isl_basic_set_is_equal(
7266 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7268 return isl_basic_map_is_equal(
7269 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7272 int isl_map_is_empty(struct isl_map *map)
7274 int i;
7275 int is_empty;
7277 if (!map)
7278 return -1;
7279 for (i = 0; i < map->n; ++i) {
7280 is_empty = isl_basic_map_is_empty(map->p[i]);
7281 if (is_empty < 0)
7282 return -1;
7283 if (!is_empty)
7284 return 0;
7286 return 1;
7289 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7291 return map ? map->n == 0 : -1;
7294 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7296 return isl_map_plain_is_empty(map);
7299 int isl_set_plain_is_empty(struct isl_set *set)
7301 return set ? set->n == 0 : -1;
7304 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7306 return isl_set_plain_is_empty(set);
7309 int isl_set_is_empty(struct isl_set *set)
7311 return isl_map_is_empty((struct isl_map *)set);
7314 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7316 if (!map1 || !map2)
7317 return -1;
7319 return isl_space_is_equal(map1->dim, map2->dim);
7322 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7324 if (!set1 || !set2)
7325 return -1;
7327 return isl_space_is_equal(set1->dim, set2->dim);
7330 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7332 int is_subset;
7334 if (!map1 || !map2)
7335 return -1;
7336 is_subset = isl_map_is_subset(map1, map2);
7337 if (is_subset != 1)
7338 return is_subset;
7339 is_subset = isl_map_is_subset(map2, map1);
7340 return is_subset;
7343 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7345 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7348 int isl_basic_map_is_strict_subset(
7349 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7351 int is_subset;
7353 if (!bmap1 || !bmap2)
7354 return -1;
7355 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7356 if (is_subset != 1)
7357 return is_subset;
7358 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7359 if (is_subset == -1)
7360 return is_subset;
7361 return !is_subset;
7364 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7366 int is_subset;
7368 if (!map1 || !map2)
7369 return -1;
7370 is_subset = isl_map_is_subset(map1, map2);
7371 if (is_subset != 1)
7372 return is_subset;
7373 is_subset = isl_map_is_subset(map2, map1);
7374 if (is_subset == -1)
7375 return is_subset;
7376 return !is_subset;
7379 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7381 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7384 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7386 if (!bmap)
7387 return -1;
7388 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7391 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7393 if (!bset)
7394 return -1;
7395 return bset->n_eq == 0 && bset->n_ineq == 0;
7398 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7400 int i;
7402 if (!map)
7403 return -1;
7405 for (i = 0; i < map->n; ++i) {
7406 int r = isl_basic_map_is_universe(map->p[i]);
7407 if (r < 0 || r)
7408 return r;
7411 return 0;
7414 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7416 return isl_map_plain_is_universe((isl_map *) set);
7419 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7421 return isl_set_plain_is_universe(set);
7424 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7426 struct isl_basic_set *bset = NULL;
7427 struct isl_vec *sample = NULL;
7428 int empty;
7429 unsigned total;
7431 if (!bmap)
7432 return -1;
7434 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7435 return 1;
7437 if (isl_basic_map_is_universe(bmap))
7438 return 0;
7440 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7441 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7442 copy = isl_basic_map_remove_redundancies(copy);
7443 empty = isl_basic_map_plain_is_empty(copy);
7444 isl_basic_map_free(copy);
7445 return empty;
7448 total = 1 + isl_basic_map_total_dim(bmap);
7449 if (bmap->sample && bmap->sample->size == total) {
7450 int contains = isl_basic_map_contains(bmap, bmap->sample);
7451 if (contains < 0)
7452 return -1;
7453 if (contains)
7454 return 0;
7456 isl_vec_free(bmap->sample);
7457 bmap->sample = NULL;
7458 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7459 if (!bset)
7460 return -1;
7461 sample = isl_basic_set_sample_vec(bset);
7462 if (!sample)
7463 return -1;
7464 empty = sample->size == 0;
7465 isl_vec_free(bmap->sample);
7466 bmap->sample = sample;
7467 if (empty)
7468 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7470 return empty;
7473 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7475 if (!bmap)
7476 return -1;
7477 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7480 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7482 return isl_basic_map_plain_is_empty(bmap);
7485 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7487 if (!bset)
7488 return -1;
7489 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7492 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7494 return isl_basic_set_plain_is_empty(bset);
7497 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7499 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7502 struct isl_map *isl_basic_map_union(
7503 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7505 struct isl_map *map;
7506 if (!bmap1 || !bmap2)
7507 goto error;
7509 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7511 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7512 if (!map)
7513 goto error;
7514 map = isl_map_add_basic_map(map, bmap1);
7515 map = isl_map_add_basic_map(map, bmap2);
7516 return map;
7517 error:
7518 isl_basic_map_free(bmap1);
7519 isl_basic_map_free(bmap2);
7520 return NULL;
7523 struct isl_set *isl_basic_set_union(
7524 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7526 return (struct isl_set *)isl_basic_map_union(
7527 (struct isl_basic_map *)bset1,
7528 (struct isl_basic_map *)bset2);
7531 /* Order divs such that any div only depends on previous divs */
7532 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7534 int i;
7535 unsigned off;
7537 if (!bmap)
7538 return NULL;
7540 off = isl_space_dim(bmap->dim, isl_dim_all);
7542 for (i = 0; i < bmap->n_div; ++i) {
7543 int pos;
7544 if (isl_int_is_zero(bmap->div[i][0]))
7545 continue;
7546 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7547 bmap->n_div-i);
7548 if (pos == -1)
7549 continue;
7550 isl_basic_map_swap_div(bmap, i, i + pos);
7551 --i;
7553 return bmap;
7556 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7558 return (struct isl_basic_set *)
7559 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7562 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7564 int i;
7566 if (!map)
7567 return 0;
7569 for (i = 0; i < map->n; ++i) {
7570 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7571 if (!map->p[i])
7572 goto error;
7575 return map;
7576 error:
7577 isl_map_free(map);
7578 return NULL;
7581 /* Apply the expansion computed by isl_merge_divs.
7582 * The expansion itself is given by "exp" while the resulting
7583 * list of divs is given by "div".
7585 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7586 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7588 int i, j;
7589 int n_div;
7591 bset = isl_basic_set_cow(bset);
7592 if (!bset || !div)
7593 goto error;
7595 if (div->n_row < bset->n_div)
7596 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7597 "not an expansion", goto error);
7599 n_div = bset->n_div;
7600 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
7601 div->n_row - n_div, 0,
7602 2 * (div->n_row - n_div));
7604 for (i = n_div; i < div->n_row; ++i)
7605 if (isl_basic_set_alloc_div(bset) < 0)
7606 goto error;
7608 j = n_div - 1;
7609 for (i = div->n_row - 1; i >= 0; --i) {
7610 if (j >= 0 && exp[j] == i) {
7611 if (i != j)
7612 isl_basic_map_swap_div(bset, i, j);
7613 j--;
7614 } else {
7615 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
7616 if (isl_basic_map_add_div_constraints(bset, i) < 0)
7617 goto error;
7621 isl_mat_free(div);
7622 return bset;
7623 error:
7624 isl_basic_set_free(bset);
7625 isl_mat_free(div);
7626 return NULL;
7629 /* Look for a div in dst that corresponds to the div "div" in src.
7630 * The divs before "div" in src and dst are assumed to be the same.
7632 * Returns -1 if no corresponding div was found and the position
7633 * of the corresponding div in dst otherwise.
7635 static int find_div(struct isl_basic_map *dst,
7636 struct isl_basic_map *src, unsigned div)
7638 int i;
7640 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7642 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7643 for (i = div; i < dst->n_div; ++i)
7644 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7645 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7646 dst->n_div - div) == -1)
7647 return i;
7648 return -1;
7651 struct isl_basic_map *isl_basic_map_align_divs(
7652 struct isl_basic_map *dst, struct isl_basic_map *src)
7654 int i;
7655 unsigned total;
7657 if (!dst || !src)
7658 goto error;
7660 if (src->n_div == 0)
7661 return dst;
7663 for (i = 0; i < src->n_div; ++i)
7664 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7666 src = isl_basic_map_order_divs(src);
7667 dst = isl_basic_map_cow(dst);
7668 if (!dst)
7669 return NULL;
7670 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7671 src->n_div, 0, 2 * src->n_div);
7672 if (!dst)
7673 return NULL;
7674 total = isl_space_dim(src->dim, isl_dim_all);
7675 for (i = 0; i < src->n_div; ++i) {
7676 int j = find_div(dst, src, i);
7677 if (j < 0) {
7678 j = isl_basic_map_alloc_div(dst);
7679 if (j < 0)
7680 goto error;
7681 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7682 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7683 if (isl_basic_map_add_div_constraints(dst, j) < 0)
7684 goto error;
7686 if (j != i)
7687 isl_basic_map_swap_div(dst, i, j);
7689 return dst;
7690 error:
7691 isl_basic_map_free(dst);
7692 return NULL;
7695 struct isl_basic_set *isl_basic_set_align_divs(
7696 struct isl_basic_set *dst, struct isl_basic_set *src)
7698 return (struct isl_basic_set *)isl_basic_map_align_divs(
7699 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7702 struct isl_map *isl_map_align_divs(struct isl_map *map)
7704 int i;
7706 if (!map)
7707 return NULL;
7708 if (map->n == 0)
7709 return map;
7710 map = isl_map_compute_divs(map);
7711 map = isl_map_cow(map);
7712 if (!map)
7713 return NULL;
7715 for (i = 1; i < map->n; ++i)
7716 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7717 for (i = 1; i < map->n; ++i) {
7718 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7719 if (!map->p[i])
7720 return isl_map_free(map);
7723 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7724 return map;
7727 struct isl_set *isl_set_align_divs(struct isl_set *set)
7729 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7732 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7733 __isl_take isl_map *map)
7735 if (!set || !map)
7736 goto error;
7737 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7738 map = isl_map_intersect_domain(map, set);
7739 set = isl_map_range(map);
7740 return set;
7741 error:
7742 isl_set_free(set);
7743 isl_map_free(map);
7744 return NULL;
7747 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7748 __isl_take isl_map *map)
7750 return isl_map_align_params_map_map_and(set, map, &set_apply);
7753 /* There is no need to cow as removing empty parts doesn't change
7754 * the meaning of the set.
7756 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7758 int i;
7760 if (!map)
7761 return NULL;
7763 for (i = map->n - 1; i >= 0; --i)
7764 remove_if_empty(map, i);
7766 return map;
7769 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7771 return (struct isl_set *)
7772 isl_map_remove_empty_parts((struct isl_map *)set);
7775 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7777 struct isl_basic_map *bmap;
7778 if (!map || map->n == 0)
7779 return NULL;
7780 bmap = map->p[map->n-1];
7781 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7782 return isl_basic_map_copy(bmap);
7785 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7787 return (struct isl_basic_set *)
7788 isl_map_copy_basic_map((struct isl_map *)set);
7791 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7792 __isl_keep isl_basic_map *bmap)
7794 int i;
7796 if (!map || !bmap)
7797 goto error;
7798 for (i = map->n-1; i >= 0; --i) {
7799 if (map->p[i] != bmap)
7800 continue;
7801 map = isl_map_cow(map);
7802 if (!map)
7803 goto error;
7804 isl_basic_map_free(map->p[i]);
7805 if (i != map->n-1) {
7806 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7807 map->p[i] = map->p[map->n-1];
7809 map->n--;
7810 return map;
7812 return map;
7813 error:
7814 isl_map_free(map);
7815 return NULL;
7818 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7819 struct isl_basic_set *bset)
7821 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7822 (struct isl_basic_map *)bset);
7825 /* Given two basic sets bset1 and bset2, compute the maximal difference
7826 * between the values of dimension pos in bset1 and those in bset2
7827 * for any common value of the parameters and dimensions preceding pos.
7829 static enum isl_lp_result basic_set_maximal_difference_at(
7830 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7831 int pos, isl_int *opt)
7833 isl_space *dims;
7834 struct isl_basic_map *bmap1 = NULL;
7835 struct isl_basic_map *bmap2 = NULL;
7836 struct isl_ctx *ctx;
7837 struct isl_vec *obj;
7838 unsigned total;
7839 unsigned nparam;
7840 unsigned dim1, dim2;
7841 enum isl_lp_result res;
7843 if (!bset1 || !bset2)
7844 return isl_lp_error;
7846 nparam = isl_basic_set_n_param(bset1);
7847 dim1 = isl_basic_set_n_dim(bset1);
7848 dim2 = isl_basic_set_n_dim(bset2);
7849 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7850 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7851 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7852 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7853 if (!bmap1 || !bmap2)
7854 goto error;
7855 bmap1 = isl_basic_map_cow(bmap1);
7856 bmap1 = isl_basic_map_extend(bmap1, nparam,
7857 pos, (dim1 - pos) + (dim2 - pos),
7858 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7859 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7860 if (!bmap1)
7861 goto error;
7862 total = isl_basic_map_total_dim(bmap1);
7863 ctx = bmap1->ctx;
7864 obj = isl_vec_alloc(ctx, 1 + total);
7865 if (!obj)
7866 goto error2;
7867 isl_seq_clr(obj->block.data, 1 + total);
7868 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7869 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7870 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7871 opt, NULL, NULL);
7872 isl_basic_map_free(bmap1);
7873 isl_vec_free(obj);
7874 return res;
7875 error:
7876 isl_basic_map_free(bmap2);
7877 error2:
7878 isl_basic_map_free(bmap1);
7879 return isl_lp_error;
7882 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7883 * for any common value of the parameters and dimensions preceding pos
7884 * in both basic sets, the values of dimension pos in bset1 are
7885 * smaller or larger than those in bset2.
7887 * Returns
7888 * 1 if bset1 follows bset2
7889 * -1 if bset1 precedes bset2
7890 * 0 if bset1 and bset2 are incomparable
7891 * -2 if some error occurred.
7893 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7894 struct isl_basic_set *bset2, int pos)
7896 isl_int opt;
7897 enum isl_lp_result res;
7898 int cmp;
7900 isl_int_init(opt);
7902 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7904 if (res == isl_lp_empty)
7905 cmp = 0;
7906 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7907 res == isl_lp_unbounded)
7908 cmp = 1;
7909 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7910 cmp = -1;
7911 else
7912 cmp = -2;
7914 isl_int_clear(opt);
7915 return cmp;
7918 /* Given two basic sets bset1 and bset2, check whether
7919 * for any common value of the parameters and dimensions preceding pos
7920 * there is a value of dimension pos in bset1 that is larger
7921 * than a value of the same dimension in bset2.
7923 * Return
7924 * 1 if there exists such a pair
7925 * 0 if there is no such pair, but there is a pair of equal values
7926 * -1 otherwise
7927 * -2 if some error occurred.
7929 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7930 __isl_keep isl_basic_set *bset2, int pos)
7932 isl_int opt;
7933 enum isl_lp_result res;
7934 int cmp;
7936 isl_int_init(opt);
7938 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7940 if (res == isl_lp_empty)
7941 cmp = -1;
7942 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7943 res == isl_lp_unbounded)
7944 cmp = 1;
7945 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7946 cmp = -1;
7947 else if (res == isl_lp_ok)
7948 cmp = 0;
7949 else
7950 cmp = -2;
7952 isl_int_clear(opt);
7953 return cmp;
7956 /* Given two sets set1 and set2, check whether
7957 * for any common value of the parameters and dimensions preceding pos
7958 * there is a value of dimension pos in set1 that is larger
7959 * than a value of the same dimension in set2.
7961 * Return
7962 * 1 if there exists such a pair
7963 * 0 if there is no such pair, but there is a pair of equal values
7964 * -1 otherwise
7965 * -2 if some error occurred.
7967 int isl_set_follows_at(__isl_keep isl_set *set1,
7968 __isl_keep isl_set *set2, int pos)
7970 int i, j;
7971 int follows = -1;
7973 if (!set1 || !set2)
7974 return -2;
7976 for (i = 0; i < set1->n; ++i)
7977 for (j = 0; j < set2->n; ++j) {
7978 int f;
7979 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7980 if (f == 1 || f == -2)
7981 return f;
7982 if (f > follows)
7983 follows = f;
7986 return follows;
7989 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7990 unsigned pos, isl_int *val)
7992 int i;
7993 int d;
7994 unsigned total;
7996 if (!bmap)
7997 return -1;
7998 total = isl_basic_map_total_dim(bmap);
7999 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8000 for (; d+1 > pos; --d)
8001 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8002 break;
8003 if (d != pos)
8004 continue;
8005 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8006 return 0;
8007 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8008 return 0;
8009 if (!isl_int_is_one(bmap->eq[i][1+d]))
8010 return 0;
8011 if (val)
8012 isl_int_neg(*val, bmap->eq[i][0]);
8013 return 1;
8015 return 0;
8018 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8019 unsigned pos, isl_int *val)
8021 int i;
8022 isl_int v;
8023 isl_int tmp;
8024 int fixed;
8026 if (!map)
8027 return -1;
8028 if (map->n == 0)
8029 return 0;
8030 if (map->n == 1)
8031 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8032 isl_int_init(v);
8033 isl_int_init(tmp);
8034 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8035 for (i = 1; fixed == 1 && i < map->n; ++i) {
8036 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8037 if (fixed == 1 && isl_int_ne(tmp, v))
8038 fixed = 0;
8040 if (val)
8041 isl_int_set(*val, v);
8042 isl_int_clear(tmp);
8043 isl_int_clear(v);
8044 return fixed;
8047 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8048 unsigned pos, isl_int *val)
8050 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8051 pos, val);
8054 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8055 isl_int *val)
8057 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8060 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8061 enum isl_dim_type type, unsigned pos, isl_int *val)
8063 if (pos >= isl_basic_map_dim(bmap, type))
8064 return -1;
8065 return isl_basic_map_plain_has_fixed_var(bmap,
8066 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8069 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8070 enum isl_dim_type type, unsigned pos, isl_int *val)
8072 if (pos >= isl_map_dim(map, type))
8073 return -1;
8074 return isl_map_plain_has_fixed_var(map,
8075 map_offset(map, type) - 1 + pos, val);
8078 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8079 enum isl_dim_type type, unsigned pos, isl_int *val)
8081 return isl_map_plain_is_fixed(set, type, pos, val);
8084 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8085 enum isl_dim_type type, unsigned pos, isl_int *val)
8087 return isl_map_plain_is_fixed(map, type, pos, val);
8090 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8091 * then return this fixed value in *val.
8093 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8094 unsigned dim, isl_int *val)
8096 return isl_basic_set_plain_has_fixed_var(bset,
8097 isl_basic_set_n_param(bset) + dim, val);
8100 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8101 * then return this fixed value in *val.
8103 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8104 unsigned dim, isl_int *val)
8106 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8109 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8110 unsigned dim, isl_int *val)
8112 return isl_set_plain_dim_is_fixed(set, dim, val);
8115 /* Check if input variable in has fixed value and if so and if val is not NULL,
8116 * then return this fixed value in *val.
8118 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8119 unsigned in, isl_int *val)
8121 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8124 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8125 * and if val is not NULL, then return this lower bound in *val.
8127 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8128 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8130 int i, i_eq = -1, i_ineq = -1;
8131 isl_int *c;
8132 unsigned total;
8133 unsigned nparam;
8135 if (!bset)
8136 return -1;
8137 total = isl_basic_set_total_dim(bset);
8138 nparam = isl_basic_set_n_param(bset);
8139 for (i = 0; i < bset->n_eq; ++i) {
8140 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8141 continue;
8142 if (i_eq != -1)
8143 return 0;
8144 i_eq = i;
8146 for (i = 0; i < bset->n_ineq; ++i) {
8147 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8148 continue;
8149 if (i_eq != -1 || i_ineq != -1)
8150 return 0;
8151 i_ineq = i;
8153 if (i_eq == -1 && i_ineq == -1)
8154 return 0;
8155 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8156 /* The coefficient should always be one due to normalization. */
8157 if (!isl_int_is_one(c[1+nparam+dim]))
8158 return 0;
8159 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8160 return 0;
8161 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8162 total - nparam - dim - 1) != -1)
8163 return 0;
8164 if (val)
8165 isl_int_neg(*val, c[0]);
8166 return 1;
8169 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8170 unsigned dim, isl_int *val)
8172 int i;
8173 isl_int v;
8174 isl_int tmp;
8175 int fixed;
8177 if (!set)
8178 return -1;
8179 if (set->n == 0)
8180 return 0;
8181 if (set->n == 1)
8182 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8183 dim, val);
8184 isl_int_init(v);
8185 isl_int_init(tmp);
8186 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8187 dim, &v);
8188 for (i = 1; fixed == 1 && i < set->n; ++i) {
8189 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8190 dim, &tmp);
8191 if (fixed == 1 && isl_int_ne(tmp, v))
8192 fixed = 0;
8194 if (val)
8195 isl_int_set(*val, v);
8196 isl_int_clear(tmp);
8197 isl_int_clear(v);
8198 return fixed;
8201 struct constraint {
8202 unsigned size;
8203 isl_int *c;
8206 /* uset_gist depends on constraints without existentially quantified
8207 * variables sorting first.
8209 static int qsort_constraint_cmp(const void *p1, const void *p2)
8211 const struct constraint *c1 = (const struct constraint *)p1;
8212 const struct constraint *c2 = (const struct constraint *)p2;
8213 int l1, l2;
8214 unsigned size = isl_min(c1->size, c2->size);
8216 l1 = isl_seq_last_non_zero(c1->c + 1, size);
8217 l2 = isl_seq_last_non_zero(c2->c + 1, size);
8219 if (l1 != l2)
8220 return l1 - l2;
8222 return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8225 static struct isl_basic_map *isl_basic_map_sort_constraints(
8226 struct isl_basic_map *bmap)
8228 int i;
8229 struct constraint *c;
8230 unsigned total;
8232 if (!bmap)
8233 return NULL;
8234 total = isl_basic_map_total_dim(bmap);
8235 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8236 if (!c)
8237 goto error;
8238 for (i = 0; i < bmap->n_ineq; ++i) {
8239 c[i].size = total;
8240 c[i].c = bmap->ineq[i];
8242 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8243 for (i = 0; i < bmap->n_ineq; ++i)
8244 bmap->ineq[i] = c[i].c;
8245 free(c);
8246 return bmap;
8247 error:
8248 isl_basic_map_free(bmap);
8249 return NULL;
8252 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8253 __isl_take isl_basic_set *bset)
8255 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8256 (struct isl_basic_map *)bset);
8259 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8261 if (!bmap)
8262 return NULL;
8263 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8264 return bmap;
8265 bmap = isl_basic_map_remove_redundancies(bmap);
8266 bmap = isl_basic_map_sort_constraints(bmap);
8267 if (bmap)
8268 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8269 return bmap;
8272 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8274 return (struct isl_basic_set *)isl_basic_map_normalize(
8275 (struct isl_basic_map *)bset);
8278 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8279 const __isl_keep isl_basic_map *bmap2)
8281 int i, cmp;
8282 unsigned total;
8284 if (bmap1 == bmap2)
8285 return 0;
8286 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8287 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8288 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8289 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8290 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8291 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8292 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8293 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8294 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8295 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8296 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8297 return 0;
8298 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8299 return 1;
8300 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8301 return -1;
8302 if (bmap1->n_eq != bmap2->n_eq)
8303 return bmap1->n_eq - bmap2->n_eq;
8304 if (bmap1->n_ineq != bmap2->n_ineq)
8305 return bmap1->n_ineq - bmap2->n_ineq;
8306 if (bmap1->n_div != bmap2->n_div)
8307 return bmap1->n_div - bmap2->n_div;
8308 total = isl_basic_map_total_dim(bmap1);
8309 for (i = 0; i < bmap1->n_eq; ++i) {
8310 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8311 if (cmp)
8312 return cmp;
8314 for (i = 0; i < bmap1->n_ineq; ++i) {
8315 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8316 if (cmp)
8317 return cmp;
8319 for (i = 0; i < bmap1->n_div; ++i) {
8320 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8321 if (cmp)
8322 return cmp;
8324 return 0;
8327 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8328 const __isl_keep isl_basic_set *bset2)
8330 return isl_basic_map_plain_cmp(bset1, bset2);
8333 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8335 int i, cmp;
8337 if (set1 == set2)
8338 return 0;
8339 if (set1->n != set2->n)
8340 return set1->n - set2->n;
8342 for (i = 0; i < set1->n; ++i) {
8343 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8344 if (cmp)
8345 return cmp;
8348 return 0;
8351 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8352 __isl_keep isl_basic_map *bmap2)
8354 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8357 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8358 __isl_keep isl_basic_set *bset2)
8360 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8361 (isl_basic_map *)bset2);
8364 static int qsort_bmap_cmp(const void *p1, const void *p2)
8366 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8367 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8369 return isl_basic_map_plain_cmp(bmap1, bmap2);
8372 /* We normalize in place, but if anything goes wrong we need
8373 * to return NULL, so we need to make sure we don't change the
8374 * meaning of any possible other copies of map.
8376 struct isl_map *isl_map_normalize(struct isl_map *map)
8378 int i, j;
8379 struct isl_basic_map *bmap;
8381 if (!map)
8382 return NULL;
8383 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8384 return map;
8385 for (i = 0; i < map->n; ++i) {
8386 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8387 if (!bmap)
8388 goto error;
8389 isl_basic_map_free(map->p[i]);
8390 map->p[i] = bmap;
8392 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8393 ISL_F_SET(map, ISL_MAP_NORMALIZED);
8394 map = isl_map_remove_empty_parts(map);
8395 if (!map)
8396 return NULL;
8397 for (i = map->n - 1; i >= 1; --i) {
8398 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8399 continue;
8400 isl_basic_map_free(map->p[i-1]);
8401 for (j = i; j < map->n; ++j)
8402 map->p[j-1] = map->p[j];
8403 map->n--;
8405 return map;
8406 error:
8407 isl_map_free(map);
8408 return NULL;
8412 struct isl_set *isl_set_normalize(struct isl_set *set)
8414 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8417 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8419 int i;
8420 int equal;
8422 if (!map1 || !map2)
8423 return -1;
8425 if (map1 == map2)
8426 return 1;
8427 if (!isl_space_is_equal(map1->dim, map2->dim))
8428 return 0;
8430 map1 = isl_map_copy(map1);
8431 map2 = isl_map_copy(map2);
8432 map1 = isl_map_normalize(map1);
8433 map2 = isl_map_normalize(map2);
8434 if (!map1 || !map2)
8435 goto error;
8436 equal = map1->n == map2->n;
8437 for (i = 0; equal && i < map1->n; ++i) {
8438 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8439 if (equal < 0)
8440 goto error;
8442 isl_map_free(map1);
8443 isl_map_free(map2);
8444 return equal;
8445 error:
8446 isl_map_free(map1);
8447 isl_map_free(map2);
8448 return -1;
8451 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8453 return isl_map_plain_is_equal(map1, map2);
8456 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8458 return isl_map_plain_is_equal((struct isl_map *)set1,
8459 (struct isl_map *)set2);
8462 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8464 return isl_set_plain_is_equal(set1, set2);
8467 /* Return an interval that ranges from min to max (inclusive)
8469 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8470 isl_int min, isl_int max)
8472 int k;
8473 struct isl_basic_set *bset = NULL;
8475 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8476 if (!bset)
8477 goto error;
8479 k = isl_basic_set_alloc_inequality(bset);
8480 if (k < 0)
8481 goto error;
8482 isl_int_set_si(bset->ineq[k][1], 1);
8483 isl_int_neg(bset->ineq[k][0], min);
8485 k = isl_basic_set_alloc_inequality(bset);
8486 if (k < 0)
8487 goto error;
8488 isl_int_set_si(bset->ineq[k][1], -1);
8489 isl_int_set(bset->ineq[k][0], max);
8491 return bset;
8492 error:
8493 isl_basic_set_free(bset);
8494 return NULL;
8497 /* Return the Cartesian product of the basic sets in list (in the given order).
8499 __isl_give isl_basic_set *isl_basic_set_list_product(
8500 __isl_take struct isl_basic_set_list *list)
8502 int i;
8503 unsigned dim;
8504 unsigned nparam;
8505 unsigned extra;
8506 unsigned n_eq;
8507 unsigned n_ineq;
8508 struct isl_basic_set *product = NULL;
8510 if (!list)
8511 goto error;
8512 isl_assert(list->ctx, list->n > 0, goto error);
8513 isl_assert(list->ctx, list->p[0], goto error);
8514 nparam = isl_basic_set_n_param(list->p[0]);
8515 dim = isl_basic_set_n_dim(list->p[0]);
8516 extra = list->p[0]->n_div;
8517 n_eq = list->p[0]->n_eq;
8518 n_ineq = list->p[0]->n_ineq;
8519 for (i = 1; i < list->n; ++i) {
8520 isl_assert(list->ctx, list->p[i], goto error);
8521 isl_assert(list->ctx,
8522 nparam == isl_basic_set_n_param(list->p[i]), goto error);
8523 dim += isl_basic_set_n_dim(list->p[i]);
8524 extra += list->p[i]->n_div;
8525 n_eq += list->p[i]->n_eq;
8526 n_ineq += list->p[i]->n_ineq;
8528 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
8529 n_eq, n_ineq);
8530 if (!product)
8531 goto error;
8532 dim = 0;
8533 for (i = 0; i < list->n; ++i) {
8534 isl_basic_set_add_constraints(product,
8535 isl_basic_set_copy(list->p[i]), dim);
8536 dim += isl_basic_set_n_dim(list->p[i]);
8538 isl_basic_set_list_free(list);
8539 return product;
8540 error:
8541 isl_basic_set_free(product);
8542 isl_basic_set_list_free(list);
8543 return NULL;
8546 struct isl_basic_map *isl_basic_map_product(
8547 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8549 isl_space *dim_result = NULL;
8550 struct isl_basic_map *bmap;
8551 unsigned in1, in2, out1, out2, nparam, total, pos;
8552 struct isl_dim_map *dim_map1, *dim_map2;
8554 if (!bmap1 || !bmap2)
8555 goto error;
8557 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
8558 bmap2->dim, isl_dim_param), goto error);
8559 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
8560 isl_space_copy(bmap2->dim));
8562 in1 = isl_basic_map_n_in(bmap1);
8563 in2 = isl_basic_map_n_in(bmap2);
8564 out1 = isl_basic_map_n_out(bmap1);
8565 out2 = isl_basic_map_n_out(bmap2);
8566 nparam = isl_basic_map_n_param(bmap1);
8568 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
8569 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8570 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8571 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8572 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8573 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8574 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8575 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8576 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8577 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8578 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8580 bmap = isl_basic_map_alloc_space(dim_result,
8581 bmap1->n_div + bmap2->n_div,
8582 bmap1->n_eq + bmap2->n_eq,
8583 bmap1->n_ineq + bmap2->n_ineq);
8584 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8585 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8586 bmap = isl_basic_map_simplify(bmap);
8587 return isl_basic_map_finalize(bmap);
8588 error:
8589 isl_basic_map_free(bmap1);
8590 isl_basic_map_free(bmap2);
8591 return NULL;
8594 __isl_give isl_basic_map *isl_basic_map_flat_product(
8595 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8597 isl_basic_map *prod;
8599 prod = isl_basic_map_product(bmap1, bmap2);
8600 prod = isl_basic_map_flatten(prod);
8601 return prod;
8604 __isl_give isl_basic_set *isl_basic_set_flat_product(
8605 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
8607 return isl_basic_map_flat_range_product(bset1, bset2);
8610 __isl_give isl_basic_map *isl_basic_map_domain_product(
8611 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8613 isl_space *space_result = NULL;
8614 isl_basic_map *bmap;
8615 unsigned in1, in2, out, nparam, total, pos;
8616 struct isl_dim_map *dim_map1, *dim_map2;
8618 if (!bmap1 || !bmap2)
8619 goto error;
8621 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8622 isl_space_copy(bmap2->dim));
8624 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8625 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8626 out = isl_basic_map_dim(bmap1, isl_dim_out);
8627 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8629 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8630 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8631 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8632 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8633 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8634 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8635 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8636 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8637 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8638 isl_dim_map_div(dim_map1, bmap1, pos += out);
8639 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8641 bmap = isl_basic_map_alloc_space(space_result,
8642 bmap1->n_div + bmap2->n_div,
8643 bmap1->n_eq + bmap2->n_eq,
8644 bmap1->n_ineq + bmap2->n_ineq);
8645 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8646 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8647 bmap = isl_basic_map_simplify(bmap);
8648 return isl_basic_map_finalize(bmap);
8649 error:
8650 isl_basic_map_free(bmap1);
8651 isl_basic_map_free(bmap2);
8652 return NULL;
8655 __isl_give isl_basic_map *isl_basic_map_range_product(
8656 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8658 isl_space *dim_result = NULL;
8659 isl_basic_map *bmap;
8660 unsigned in, out1, out2, nparam, total, pos;
8661 struct isl_dim_map *dim_map1, *dim_map2;
8663 if (!bmap1 || !bmap2)
8664 goto error;
8666 if (!isl_space_match(bmap1->dim, isl_dim_param,
8667 bmap2->dim, isl_dim_param))
8668 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
8669 "parameters don't match", goto error);
8671 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8672 isl_space_copy(bmap2->dim));
8674 in = isl_basic_map_dim(bmap1, isl_dim_in);
8675 out1 = isl_basic_map_n_out(bmap1);
8676 out2 = isl_basic_map_n_out(bmap2);
8677 nparam = isl_basic_map_n_param(bmap1);
8679 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8680 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8681 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8682 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8683 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8684 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8685 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8686 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8687 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8688 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8689 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8691 bmap = isl_basic_map_alloc_space(dim_result,
8692 bmap1->n_div + bmap2->n_div,
8693 bmap1->n_eq + bmap2->n_eq,
8694 bmap1->n_ineq + bmap2->n_ineq);
8695 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8696 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8697 bmap = isl_basic_map_simplify(bmap);
8698 return isl_basic_map_finalize(bmap);
8699 error:
8700 isl_basic_map_free(bmap1);
8701 isl_basic_map_free(bmap2);
8702 return NULL;
8705 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8706 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8708 isl_basic_map *prod;
8710 prod = isl_basic_map_range_product(bmap1, bmap2);
8711 prod = isl_basic_map_flatten_range(prod);
8712 return prod;
8715 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8716 __isl_take isl_map *map2,
8717 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8718 __isl_take isl_space *right),
8719 __isl_give isl_basic_map *(*basic_map_product)(
8720 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8722 unsigned flags = 0;
8723 struct isl_map *result;
8724 int i, j;
8726 if (!map1 || !map2)
8727 goto error;
8729 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8730 map2->dim, isl_dim_param), goto error);
8732 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8733 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8734 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8736 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8737 isl_space_copy(map2->dim)),
8738 map1->n * map2->n, flags);
8739 if (!result)
8740 goto error;
8741 for (i = 0; i < map1->n; ++i)
8742 for (j = 0; j < map2->n; ++j) {
8743 struct isl_basic_map *part;
8744 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8745 isl_basic_map_copy(map2->p[j]));
8746 if (isl_basic_map_is_empty(part))
8747 isl_basic_map_free(part);
8748 else
8749 result = isl_map_add_basic_map(result, part);
8750 if (!result)
8751 goto error;
8753 isl_map_free(map1);
8754 isl_map_free(map2);
8755 return result;
8756 error:
8757 isl_map_free(map1);
8758 isl_map_free(map2);
8759 return NULL;
8762 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8764 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8765 __isl_take isl_map *map2)
8767 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8770 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8771 __isl_take isl_map *map2)
8773 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8776 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8778 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8779 __isl_take isl_map *map2)
8781 isl_map *prod;
8783 prod = isl_map_product(map1, map2);
8784 prod = isl_map_flatten(prod);
8785 return prod;
8788 /* Given two set A and B, construct its Cartesian product A x B.
8790 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8792 return isl_map_range_product(set1, set2);
8795 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8796 __isl_take isl_set *set2)
8798 return isl_map_flat_range_product(set1, set2);
8801 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8803 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8804 __isl_take isl_map *map2)
8806 return map_product(map1, map2, &isl_space_domain_product,
8807 &isl_basic_map_domain_product);
8810 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8812 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8813 __isl_take isl_map *map2)
8815 return map_product(map1, map2, &isl_space_range_product,
8816 &isl_basic_map_range_product);
8819 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8820 __isl_take isl_map *map2)
8822 return isl_map_align_params_map_map_and(map1, map2,
8823 &map_domain_product_aligned);
8826 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8827 __isl_take isl_map *map2)
8829 return isl_map_align_params_map_map_and(map1, map2,
8830 &map_range_product_aligned);
8833 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8835 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8836 __isl_take isl_map *map2)
8838 isl_map *prod;
8840 prod = isl_map_domain_product(map1, map2);
8841 prod = isl_map_flatten_domain(prod);
8842 return prod;
8845 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8847 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8848 __isl_take isl_map *map2)
8850 isl_map *prod;
8852 prod = isl_map_range_product(map1, map2);
8853 prod = isl_map_flatten_range(prod);
8854 return prod;
8857 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8859 int i;
8860 uint32_t hash = isl_hash_init();
8861 unsigned total;
8863 if (!bmap)
8864 return 0;
8865 bmap = isl_basic_map_copy(bmap);
8866 bmap = isl_basic_map_normalize(bmap);
8867 if (!bmap)
8868 return 0;
8869 total = isl_basic_map_total_dim(bmap);
8870 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8871 for (i = 0; i < bmap->n_eq; ++i) {
8872 uint32_t c_hash;
8873 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8874 isl_hash_hash(hash, c_hash);
8876 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8877 for (i = 0; i < bmap->n_ineq; ++i) {
8878 uint32_t c_hash;
8879 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8880 isl_hash_hash(hash, c_hash);
8882 isl_hash_byte(hash, bmap->n_div & 0xFF);
8883 for (i = 0; i < bmap->n_div; ++i) {
8884 uint32_t c_hash;
8885 if (isl_int_is_zero(bmap->div[i][0]))
8886 continue;
8887 isl_hash_byte(hash, i & 0xFF);
8888 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8889 isl_hash_hash(hash, c_hash);
8891 isl_basic_map_free(bmap);
8892 return hash;
8895 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8897 return isl_basic_map_get_hash((isl_basic_map *)bset);
8900 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8902 int i;
8903 uint32_t hash;
8905 if (!map)
8906 return 0;
8907 map = isl_map_copy(map);
8908 map = isl_map_normalize(map);
8909 if (!map)
8910 return 0;
8912 hash = isl_hash_init();
8913 for (i = 0; i < map->n; ++i) {
8914 uint32_t bmap_hash;
8915 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8916 isl_hash_hash(hash, bmap_hash);
8919 isl_map_free(map);
8921 return hash;
8924 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8926 return isl_map_get_hash((isl_map *)set);
8929 /* Check if the value for dimension dim is completely determined
8930 * by the values of the other parameters and variables.
8931 * That is, check if dimension dim is involved in an equality.
8933 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8935 int i;
8936 unsigned nparam;
8938 if (!bset)
8939 return -1;
8940 nparam = isl_basic_set_n_param(bset);
8941 for (i = 0; i < bset->n_eq; ++i)
8942 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8943 return 1;
8944 return 0;
8947 /* Check if the value for dimension dim is completely determined
8948 * by the values of the other parameters and variables.
8949 * That is, check if dimension dim is involved in an equality
8950 * for each of the subsets.
8952 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8954 int i;
8956 if (!set)
8957 return -1;
8958 for (i = 0; i < set->n; ++i) {
8959 int unique;
8960 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8961 if (unique != 1)
8962 return unique;
8964 return 1;
8967 int isl_set_n_basic_set(__isl_keep isl_set *set)
8969 return set ? set->n : 0;
8972 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8973 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8975 int i;
8977 if (!map)
8978 return -1;
8980 for (i = 0; i < map->n; ++i)
8981 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8982 return -1;
8984 return 0;
8987 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8988 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8990 int i;
8992 if (!set)
8993 return -1;
8995 for (i = 0; i < set->n; ++i)
8996 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8997 return -1;
8999 return 0;
9002 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9004 isl_space *dim;
9006 if (!bset)
9007 return NULL;
9009 bset = isl_basic_set_cow(bset);
9010 if (!bset)
9011 return NULL;
9013 dim = isl_basic_set_get_space(bset);
9014 dim = isl_space_lift(dim, bset->n_div);
9015 if (!dim)
9016 goto error;
9017 isl_space_free(bset->dim);
9018 bset->dim = dim;
9019 bset->extra -= bset->n_div;
9020 bset->n_div = 0;
9022 bset = isl_basic_set_finalize(bset);
9024 return bset;
9025 error:
9026 isl_basic_set_free(bset);
9027 return NULL;
9030 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9032 int i;
9033 isl_space *dim;
9034 unsigned n_div;
9036 set = isl_set_align_divs(set);
9038 if (!set)
9039 return NULL;
9041 set = isl_set_cow(set);
9042 if (!set)
9043 return NULL;
9045 n_div = set->p[0]->n_div;
9046 dim = isl_set_get_space(set);
9047 dim = isl_space_lift(dim, n_div);
9048 if (!dim)
9049 goto error;
9050 isl_space_free(set->dim);
9051 set->dim = dim;
9053 for (i = 0; i < set->n; ++i) {
9054 set->p[i] = isl_basic_set_lift(set->p[i]);
9055 if (!set->p[i])
9056 goto error;
9059 return set;
9060 error:
9061 isl_set_free(set);
9062 return NULL;
9065 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9067 isl_space *dim;
9068 struct isl_basic_map *bmap;
9069 unsigned n_set;
9070 unsigned n_div;
9071 unsigned n_param;
9072 unsigned total;
9073 int i, k, l;
9075 set = isl_set_align_divs(set);
9077 if (!set)
9078 return NULL;
9080 dim = isl_set_get_space(set);
9081 if (set->n == 0 || set->p[0]->n_div == 0) {
9082 isl_set_free(set);
9083 return isl_map_identity(isl_space_map_from_set(dim));
9086 n_div = set->p[0]->n_div;
9087 dim = isl_space_map_from_set(dim);
9088 n_param = isl_space_dim(dim, isl_dim_param);
9089 n_set = isl_space_dim(dim, isl_dim_in);
9090 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9091 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9092 for (i = 0; i < n_set; ++i)
9093 bmap = var_equal(bmap, i);
9095 total = n_param + n_set + n_set + n_div;
9096 for (i = 0; i < n_div; ++i) {
9097 k = isl_basic_map_alloc_inequality(bmap);
9098 if (k < 0)
9099 goto error;
9100 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9101 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9102 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9103 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9104 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9105 set->p[0]->div[i][0]);
9107 l = isl_basic_map_alloc_inequality(bmap);
9108 if (l < 0)
9109 goto error;
9110 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9111 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9112 set->p[0]->div[i][0]);
9113 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9116 isl_set_free(set);
9117 bmap = isl_basic_map_simplify(bmap);
9118 bmap = isl_basic_map_finalize(bmap);
9119 return isl_map_from_basic_map(bmap);
9120 error:
9121 isl_set_free(set);
9122 isl_basic_map_free(bmap);
9123 return NULL;
9126 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9128 unsigned dim;
9129 int size = 0;
9131 if (!bset)
9132 return -1;
9134 dim = isl_basic_set_total_dim(bset);
9135 size += bset->n_eq * (1 + dim);
9136 size += bset->n_ineq * (1 + dim);
9137 size += bset->n_div * (2 + dim);
9139 return size;
9142 int isl_set_size(__isl_keep isl_set *set)
9144 int i;
9145 int size = 0;
9147 if (!set)
9148 return -1;
9150 for (i = 0; i < set->n; ++i)
9151 size += isl_basic_set_size(set->p[i]);
9153 return size;
9156 /* Check if there is any lower bound (if lower == 0) and/or upper
9157 * bound (if upper == 0) on the specified dim.
9159 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9160 enum isl_dim_type type, unsigned pos, int lower, int upper)
9162 int i;
9164 if (!bmap)
9165 return -1;
9167 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9169 pos += isl_basic_map_offset(bmap, type);
9171 for (i = 0; i < bmap->n_div; ++i) {
9172 if (isl_int_is_zero(bmap->div[i][0]))
9173 continue;
9174 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9175 return 1;
9178 for (i = 0; i < bmap->n_eq; ++i)
9179 if (!isl_int_is_zero(bmap->eq[i][pos]))
9180 return 1;
9182 for (i = 0; i < bmap->n_ineq; ++i) {
9183 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
9184 if (sgn > 0)
9185 lower = 1;
9186 if (sgn < 0)
9187 upper = 1;
9190 return lower && upper;
9193 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9194 enum isl_dim_type type, unsigned pos)
9196 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9199 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9200 enum isl_dim_type type, unsigned pos)
9202 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9205 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9206 enum isl_dim_type type, unsigned pos)
9208 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9211 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9212 enum isl_dim_type type, unsigned pos)
9214 int i;
9216 if (!map)
9217 return -1;
9219 for (i = 0; i < map->n; ++i) {
9220 int bounded;
9221 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9222 if (bounded < 0 || !bounded)
9223 return bounded;
9226 return 1;
9229 /* Return 1 if the specified dim is involved in both an upper bound
9230 * and a lower bound.
9232 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9233 enum isl_dim_type type, unsigned pos)
9235 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9238 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9240 static int has_any_bound(__isl_keep isl_map *map,
9241 enum isl_dim_type type, unsigned pos,
9242 int (*fn)(__isl_keep isl_basic_map *bmap,
9243 enum isl_dim_type type, unsigned pos))
9245 int i;
9247 if (!map)
9248 return -1;
9250 for (i = 0; i < map->n; ++i) {
9251 int bounded;
9252 bounded = fn(map->p[i], type, pos);
9253 if (bounded < 0 || bounded)
9254 return bounded;
9257 return 0;
9260 /* Return 1 if the specified dim is involved in any lower bound.
9262 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
9263 enum isl_dim_type type, unsigned pos)
9265 return has_any_bound(set, type, pos,
9266 &isl_basic_map_dim_has_lower_bound);
9269 /* Return 1 if the specified dim is involved in any upper bound.
9271 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
9272 enum isl_dim_type type, unsigned pos)
9274 return has_any_bound(set, type, pos,
9275 &isl_basic_map_dim_has_upper_bound);
9278 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9280 static int has_bound(__isl_keep isl_map *map,
9281 enum isl_dim_type type, unsigned pos,
9282 int (*fn)(__isl_keep isl_basic_map *bmap,
9283 enum isl_dim_type type, unsigned pos))
9285 int i;
9287 if (!map)
9288 return -1;
9290 for (i = 0; i < map->n; ++i) {
9291 int bounded;
9292 bounded = fn(map->p[i], type, pos);
9293 if (bounded < 0 || !bounded)
9294 return bounded;
9297 return 1;
9300 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9302 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9303 enum isl_dim_type type, unsigned pos)
9305 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9308 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9310 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9311 enum isl_dim_type type, unsigned pos)
9313 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9316 /* For each of the "n" variables starting at "first", determine
9317 * the sign of the variable and put the results in the first "n"
9318 * elements of the array "signs".
9319 * Sign
9320 * 1 means that the variable is non-negative
9321 * -1 means that the variable is non-positive
9322 * 0 means the variable attains both positive and negative values.
9324 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9325 unsigned first, unsigned n, int *signs)
9327 isl_vec *bound = NULL;
9328 struct isl_tab *tab = NULL;
9329 struct isl_tab_undo *snap;
9330 int i;
9332 if (!bset || !signs)
9333 return -1;
9335 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9336 tab = isl_tab_from_basic_set(bset, 0);
9337 if (!bound || !tab)
9338 goto error;
9340 isl_seq_clr(bound->el, bound->size);
9341 isl_int_set_si(bound->el[0], -1);
9343 snap = isl_tab_snap(tab);
9344 for (i = 0; i < n; ++i) {
9345 int empty;
9347 isl_int_set_si(bound->el[1 + first + i], -1);
9348 if (isl_tab_add_ineq(tab, bound->el) < 0)
9349 goto error;
9350 empty = tab->empty;
9351 isl_int_set_si(bound->el[1 + first + i], 0);
9352 if (isl_tab_rollback(tab, snap) < 0)
9353 goto error;
9355 if (empty) {
9356 signs[i] = 1;
9357 continue;
9360 isl_int_set_si(bound->el[1 + first + i], 1);
9361 if (isl_tab_add_ineq(tab, bound->el) < 0)
9362 goto error;
9363 empty = tab->empty;
9364 isl_int_set_si(bound->el[1 + first + i], 0);
9365 if (isl_tab_rollback(tab, snap) < 0)
9366 goto error;
9368 signs[i] = empty ? -1 : 0;
9371 isl_tab_free(tab);
9372 isl_vec_free(bound);
9373 return 0;
9374 error:
9375 isl_tab_free(tab);
9376 isl_vec_free(bound);
9377 return -1;
9380 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
9381 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
9383 if (!bset || !signs)
9384 return -1;
9385 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
9386 return -1);
9388 first += pos(bset->dim, type) - 1;
9389 return isl_basic_set_vars_get_sign(bset, first, n, signs);
9392 /* Check if the given basic map is obviously single-valued.
9393 * In particular, for each output dimension, check that there is
9394 * an equality that defines the output dimension in terms of
9395 * earlier dimensions.
9397 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
9399 int i, j;
9400 unsigned total;
9401 unsigned n_out;
9402 unsigned o_out;
9404 if (!bmap)
9405 return -1;
9407 total = 1 + isl_basic_map_total_dim(bmap);
9408 n_out = isl_basic_map_dim(bmap, isl_dim_out);
9409 o_out = isl_basic_map_offset(bmap, isl_dim_out);
9411 for (i = 0; i < n_out; ++i) {
9412 for (j = 0; j < bmap->n_eq; ++j) {
9413 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
9414 continue;
9415 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
9416 total - (o_out + i + 1)) == -1)
9417 break;
9419 if (j >= bmap->n_eq)
9420 return 0;
9423 return 1;
9426 /* Check if the given basic map is single-valued.
9427 * We simply compute
9429 * M \circ M^-1
9431 * and check if the result is a subset of the identity mapping.
9433 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
9435 isl_space *space;
9436 isl_basic_map *test;
9437 isl_basic_map *id;
9438 int sv;
9440 sv = isl_basic_map_plain_is_single_valued(bmap);
9441 if (sv < 0 || sv)
9442 return sv;
9444 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
9445 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
9447 space = isl_basic_map_get_space(bmap);
9448 space = isl_space_map_from_set(isl_space_range(space));
9449 id = isl_basic_map_identity(space);
9451 sv = isl_basic_map_is_subset(test, id);
9453 isl_basic_map_free(test);
9454 isl_basic_map_free(id);
9456 return sv;
9459 /* Check if the given map is obviously single-valued.
9461 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
9463 if (!map)
9464 return -1;
9465 if (map->n == 0)
9466 return 1;
9467 if (map->n >= 2)
9468 return 0;
9470 return isl_basic_map_plain_is_single_valued(map->p[0]);
9473 /* Check if the given map is single-valued.
9474 * We simply compute
9476 * M \circ M^-1
9478 * and check if the result is a subset of the identity mapping.
9480 int isl_map_is_single_valued(__isl_keep isl_map *map)
9482 isl_space *dim;
9483 isl_map *test;
9484 isl_map *id;
9485 int sv;
9487 sv = isl_map_plain_is_single_valued(map);
9488 if (sv < 0 || sv)
9489 return sv;
9491 test = isl_map_reverse(isl_map_copy(map));
9492 test = isl_map_apply_range(test, isl_map_copy(map));
9494 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
9495 id = isl_map_identity(dim);
9497 sv = isl_map_is_subset(test, id);
9499 isl_map_free(test);
9500 isl_map_free(id);
9502 return sv;
9505 int isl_map_is_injective(__isl_keep isl_map *map)
9507 int in;
9509 map = isl_map_copy(map);
9510 map = isl_map_reverse(map);
9511 in = isl_map_is_single_valued(map);
9512 isl_map_free(map);
9514 return in;
9517 /* Check if the given map is obviously injective.
9519 int isl_map_plain_is_injective(__isl_keep isl_map *map)
9521 int in;
9523 map = isl_map_copy(map);
9524 map = isl_map_reverse(map);
9525 in = isl_map_plain_is_single_valued(map);
9526 isl_map_free(map);
9528 return in;
9531 int isl_map_is_bijective(__isl_keep isl_map *map)
9533 int sv;
9535 sv = isl_map_is_single_valued(map);
9536 if (sv < 0 || !sv)
9537 return sv;
9539 return isl_map_is_injective(map);
9542 int isl_set_is_singleton(__isl_keep isl_set *set)
9544 return isl_map_is_single_valued((isl_map *)set);
9547 int isl_map_is_translation(__isl_keep isl_map *map)
9549 int ok;
9550 isl_set *delta;
9552 delta = isl_map_deltas(isl_map_copy(map));
9553 ok = isl_set_is_singleton(delta);
9554 isl_set_free(delta);
9556 return ok;
9559 static int unique(isl_int *p, unsigned pos, unsigned len)
9561 if (isl_seq_first_non_zero(p, pos) != -1)
9562 return 0;
9563 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
9564 return 0;
9565 return 1;
9568 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
9570 int i, j;
9571 unsigned nvar;
9572 unsigned ovar;
9574 if (!bset)
9575 return -1;
9577 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
9578 return 0;
9580 nvar = isl_basic_set_dim(bset, isl_dim_set);
9581 ovar = isl_space_offset(bset->dim, isl_dim_set);
9582 for (j = 0; j < nvar; ++j) {
9583 int lower = 0, upper = 0;
9584 for (i = 0; i < bset->n_eq; ++i) {
9585 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
9586 continue;
9587 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
9588 return 0;
9589 break;
9591 if (i < bset->n_eq)
9592 continue;
9593 for (i = 0; i < bset->n_ineq; ++i) {
9594 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
9595 continue;
9596 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
9597 return 0;
9598 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
9599 lower = 1;
9600 else
9601 upper = 1;
9603 if (!lower || !upper)
9604 return 0;
9607 return 1;
9610 int isl_set_is_box(__isl_keep isl_set *set)
9612 if (!set)
9613 return -1;
9614 if (set->n != 1)
9615 return 0;
9617 return isl_basic_set_is_box(set->p[0]);
9620 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
9622 if (!bset)
9623 return -1;
9625 return isl_space_is_wrapping(bset->dim);
9628 int isl_set_is_wrapping(__isl_keep isl_set *set)
9630 if (!set)
9631 return -1;
9633 return isl_space_is_wrapping(set->dim);
9636 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
9638 bmap = isl_basic_map_cow(bmap);
9639 if (!bmap)
9640 return NULL;
9642 bmap->dim = isl_space_wrap(bmap->dim);
9643 if (!bmap->dim)
9644 goto error;
9646 bmap = isl_basic_map_finalize(bmap);
9648 return (isl_basic_set *)bmap;
9649 error:
9650 isl_basic_map_free(bmap);
9651 return NULL;
9654 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
9656 int i;
9658 map = isl_map_cow(map);
9659 if (!map)
9660 return NULL;
9662 for (i = 0; i < map->n; ++i) {
9663 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
9664 if (!map->p[i])
9665 goto error;
9667 map->dim = isl_space_wrap(map->dim);
9668 if (!map->dim)
9669 goto error;
9671 return (isl_set *)map;
9672 error:
9673 isl_map_free(map);
9674 return NULL;
9677 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
9679 bset = isl_basic_set_cow(bset);
9680 if (!bset)
9681 return NULL;
9683 bset->dim = isl_space_unwrap(bset->dim);
9684 if (!bset->dim)
9685 goto error;
9687 bset = isl_basic_set_finalize(bset);
9689 return (isl_basic_map *)bset;
9690 error:
9691 isl_basic_set_free(bset);
9692 return NULL;
9695 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
9697 int i;
9699 if (!set)
9700 return NULL;
9702 if (!isl_set_is_wrapping(set))
9703 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
9704 goto error);
9706 set = isl_set_cow(set);
9707 if (!set)
9708 return NULL;
9710 for (i = 0; i < set->n; ++i) {
9711 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9712 if (!set->p[i])
9713 goto error;
9716 set->dim = isl_space_unwrap(set->dim);
9717 if (!set->dim)
9718 goto error;
9720 return (isl_map *)set;
9721 error:
9722 isl_set_free(set);
9723 return NULL;
9726 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9727 enum isl_dim_type type)
9729 if (!bmap)
9730 return NULL;
9732 if (!isl_space_is_named_or_nested(bmap->dim, type))
9733 return bmap;
9735 bmap = isl_basic_map_cow(bmap);
9736 if (!bmap)
9737 return NULL;
9739 bmap->dim = isl_space_reset(bmap->dim, type);
9740 if (!bmap->dim)
9741 goto error;
9743 bmap = isl_basic_map_finalize(bmap);
9745 return bmap;
9746 error:
9747 isl_basic_map_free(bmap);
9748 return NULL;
9751 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
9752 enum isl_dim_type type)
9754 int i;
9756 if (!map)
9757 return NULL;
9759 if (!isl_space_is_named_or_nested(map->dim, type))
9760 return map;
9762 map = isl_map_cow(map);
9763 if (!map)
9764 return NULL;
9766 for (i = 0; i < map->n; ++i) {
9767 map->p[i] = isl_basic_map_reset(map->p[i], type);
9768 if (!map->p[i])
9769 goto error;
9771 map->dim = isl_space_reset(map->dim, type);
9772 if (!map->dim)
9773 goto error;
9775 return map;
9776 error:
9777 isl_map_free(map);
9778 return NULL;
9781 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
9783 if (!bmap)
9784 return NULL;
9786 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
9787 return bmap;
9789 bmap = isl_basic_map_cow(bmap);
9790 if (!bmap)
9791 return NULL;
9793 bmap->dim = isl_space_flatten(bmap->dim);
9794 if (!bmap->dim)
9795 goto error;
9797 bmap = isl_basic_map_finalize(bmap);
9799 return bmap;
9800 error:
9801 isl_basic_map_free(bmap);
9802 return NULL;
9805 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9807 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9810 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9811 __isl_take isl_basic_map *bmap)
9813 if (!bmap)
9814 return NULL;
9816 if (!bmap->dim->nested[0])
9817 return bmap;
9819 bmap = isl_basic_map_cow(bmap);
9820 if (!bmap)
9821 return NULL;
9823 bmap->dim = isl_space_flatten_domain(bmap->dim);
9824 if (!bmap->dim)
9825 goto error;
9827 bmap = isl_basic_map_finalize(bmap);
9829 return bmap;
9830 error:
9831 isl_basic_map_free(bmap);
9832 return NULL;
9835 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9836 __isl_take isl_basic_map *bmap)
9838 if (!bmap)
9839 return NULL;
9841 if (!bmap->dim->nested[1])
9842 return bmap;
9844 bmap = isl_basic_map_cow(bmap);
9845 if (!bmap)
9846 return NULL;
9848 bmap->dim = isl_space_flatten_range(bmap->dim);
9849 if (!bmap->dim)
9850 goto error;
9852 bmap = isl_basic_map_finalize(bmap);
9854 return bmap;
9855 error:
9856 isl_basic_map_free(bmap);
9857 return NULL;
9860 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9862 int i;
9864 if (!map)
9865 return NULL;
9867 if (!map->dim->nested[0] && !map->dim->nested[1])
9868 return map;
9870 map = isl_map_cow(map);
9871 if (!map)
9872 return NULL;
9874 for (i = 0; i < map->n; ++i) {
9875 map->p[i] = isl_basic_map_flatten(map->p[i]);
9876 if (!map->p[i])
9877 goto error;
9879 map->dim = isl_space_flatten(map->dim);
9880 if (!map->dim)
9881 goto error;
9883 return map;
9884 error:
9885 isl_map_free(map);
9886 return NULL;
9889 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9891 return (isl_set *)isl_map_flatten((isl_map *)set);
9894 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9896 isl_space *dim, *flat_dim;
9897 isl_map *map;
9899 dim = isl_set_get_space(set);
9900 flat_dim = isl_space_flatten(isl_space_copy(dim));
9901 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9902 map = isl_map_intersect_domain(map, set);
9904 return map;
9907 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9909 int i;
9911 if (!map)
9912 return NULL;
9914 if (!map->dim->nested[0])
9915 return map;
9917 map = isl_map_cow(map);
9918 if (!map)
9919 return NULL;
9921 for (i = 0; i < map->n; ++i) {
9922 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9923 if (!map->p[i])
9924 goto error;
9926 map->dim = isl_space_flatten_domain(map->dim);
9927 if (!map->dim)
9928 goto error;
9930 return map;
9931 error:
9932 isl_map_free(map);
9933 return NULL;
9936 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9938 int i;
9940 if (!map)
9941 return NULL;
9943 if (!map->dim->nested[1])
9944 return map;
9946 map = isl_map_cow(map);
9947 if (!map)
9948 return NULL;
9950 for (i = 0; i < map->n; ++i) {
9951 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9952 if (!map->p[i])
9953 goto error;
9955 map->dim = isl_space_flatten_range(map->dim);
9956 if (!map->dim)
9957 goto error;
9959 return map;
9960 error:
9961 isl_map_free(map);
9962 return NULL;
9965 /* Reorder the dimensions of "bmap" according to the given dim_map
9966 * and set the dimension specification to "dim".
9968 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9969 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9971 isl_basic_map *res;
9972 unsigned flags;
9974 bmap = isl_basic_map_cow(bmap);
9975 if (!bmap || !dim || !dim_map)
9976 goto error;
9978 flags = bmap->flags;
9979 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
9980 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
9981 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
9982 res = isl_basic_map_alloc_space(dim,
9983 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9984 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9985 if (res)
9986 res->flags = flags;
9987 res = isl_basic_map_finalize(res);
9988 return res;
9989 error:
9990 free(dim_map);
9991 isl_basic_map_free(bmap);
9992 isl_space_free(dim);
9993 return NULL;
9996 /* Reorder the dimensions of "map" according to given reordering.
9998 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9999 __isl_take isl_reordering *r)
10001 int i;
10002 struct isl_dim_map *dim_map;
10004 map = isl_map_cow(map);
10005 dim_map = isl_dim_map_from_reordering(r);
10006 if (!map || !r || !dim_map)
10007 goto error;
10009 for (i = 0; i < map->n; ++i) {
10010 struct isl_dim_map *dim_map_i;
10012 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10014 map->p[i] = isl_basic_map_realign(map->p[i],
10015 isl_space_copy(r->dim), dim_map_i);
10017 if (!map->p[i])
10018 goto error;
10021 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10023 isl_reordering_free(r);
10024 free(dim_map);
10025 return map;
10026 error:
10027 free(dim_map);
10028 isl_map_free(map);
10029 isl_reordering_free(r);
10030 return NULL;
10033 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10034 __isl_take isl_reordering *r)
10036 return (isl_set *)isl_map_realign((isl_map *)set, r);
10039 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10040 __isl_take isl_space *model)
10042 isl_ctx *ctx;
10044 if (!map || !model)
10045 goto error;
10047 ctx = isl_space_get_ctx(model);
10048 if (!isl_space_has_named_params(model))
10049 isl_die(ctx, isl_error_invalid,
10050 "model has unnamed parameters", goto error);
10051 if (!isl_space_has_named_params(map->dim))
10052 isl_die(ctx, isl_error_invalid,
10053 "relation has unnamed parameters", goto error);
10054 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10055 isl_reordering *exp;
10057 model = isl_space_drop_dims(model, isl_dim_in,
10058 0, isl_space_dim(model, isl_dim_in));
10059 model = isl_space_drop_dims(model, isl_dim_out,
10060 0, isl_space_dim(model, isl_dim_out));
10061 exp = isl_parameter_alignment_reordering(map->dim, model);
10062 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10063 map = isl_map_realign(map, exp);
10066 isl_space_free(model);
10067 return map;
10068 error:
10069 isl_space_free(model);
10070 isl_map_free(map);
10071 return NULL;
10074 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10075 __isl_take isl_space *model)
10077 return isl_map_align_params(set, model);
10080 /* Align the parameters of "bmap" to those of "model", introducing
10081 * additional parameters if needed.
10083 __isl_give isl_basic_map *isl_basic_map_align_params(
10084 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10086 isl_ctx *ctx;
10088 if (!bmap || !model)
10089 goto error;
10091 ctx = isl_space_get_ctx(model);
10092 if (!isl_space_has_named_params(model))
10093 isl_die(ctx, isl_error_invalid,
10094 "model has unnamed parameters", goto error);
10095 if (!isl_space_has_named_params(bmap->dim))
10096 isl_die(ctx, isl_error_invalid,
10097 "relation has unnamed parameters", goto error);
10098 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10099 isl_reordering *exp;
10100 struct isl_dim_map *dim_map;
10102 model = isl_space_drop_dims(model, isl_dim_in,
10103 0, isl_space_dim(model, isl_dim_in));
10104 model = isl_space_drop_dims(model, isl_dim_out,
10105 0, isl_space_dim(model, isl_dim_out));
10106 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10107 exp = isl_reordering_extend_space(exp,
10108 isl_basic_map_get_space(bmap));
10109 dim_map = isl_dim_map_from_reordering(exp);
10110 bmap = isl_basic_map_realign(bmap,
10111 exp ? isl_space_copy(exp->dim) : NULL,
10112 isl_dim_map_extend(dim_map, bmap));
10113 isl_reordering_free(exp);
10114 free(dim_map);
10117 isl_space_free(model);
10118 return bmap;
10119 error:
10120 isl_space_free(model);
10121 isl_basic_map_free(bmap);
10122 return NULL;
10125 /* Align the parameters of "bset" to those of "model", introducing
10126 * additional parameters if needed.
10128 __isl_give isl_basic_set *isl_basic_set_align_params(
10129 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10131 return isl_basic_map_align_params(bset, model);
10134 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10135 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10136 enum isl_dim_type c2, enum isl_dim_type c3,
10137 enum isl_dim_type c4, enum isl_dim_type c5)
10139 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10140 struct isl_mat *mat;
10141 int i, j, k;
10142 int pos;
10144 if (!bmap)
10145 return NULL;
10146 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
10147 isl_basic_map_total_dim(bmap) + 1);
10148 if (!mat)
10149 return NULL;
10150 for (i = 0; i < bmap->n_eq; ++i)
10151 for (j = 0, pos = 0; j < 5; ++j) {
10152 int off = isl_basic_map_offset(bmap, c[j]);
10153 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10154 isl_int_set(mat->row[i][pos],
10155 bmap->eq[i][off + k]);
10156 ++pos;
10160 return mat;
10163 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
10164 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10165 enum isl_dim_type c2, enum isl_dim_type c3,
10166 enum isl_dim_type c4, enum isl_dim_type c5)
10168 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10169 struct isl_mat *mat;
10170 int i, j, k;
10171 int pos;
10173 if (!bmap)
10174 return NULL;
10175 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
10176 isl_basic_map_total_dim(bmap) + 1);
10177 if (!mat)
10178 return NULL;
10179 for (i = 0; i < bmap->n_ineq; ++i)
10180 for (j = 0, pos = 0; j < 5; ++j) {
10181 int off = isl_basic_map_offset(bmap, c[j]);
10182 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10183 isl_int_set(mat->row[i][pos],
10184 bmap->ineq[i][off + k]);
10185 ++pos;
10189 return mat;
10192 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
10193 __isl_take isl_space *dim,
10194 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10195 enum isl_dim_type c2, enum isl_dim_type c3,
10196 enum isl_dim_type c4, enum isl_dim_type c5)
10198 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10199 isl_basic_map *bmap;
10200 unsigned total;
10201 unsigned extra;
10202 int i, j, k, l;
10203 int pos;
10205 if (!dim || !eq || !ineq)
10206 goto error;
10208 if (eq->n_col != ineq->n_col)
10209 isl_die(dim->ctx, isl_error_invalid,
10210 "equalities and inequalities matrices should have "
10211 "same number of columns", goto error);
10213 total = 1 + isl_space_dim(dim, isl_dim_all);
10215 if (eq->n_col < total)
10216 isl_die(dim->ctx, isl_error_invalid,
10217 "number of columns too small", goto error);
10219 extra = eq->n_col - total;
10221 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
10222 eq->n_row, ineq->n_row);
10223 if (!bmap)
10224 goto error;
10225 for (i = 0; i < extra; ++i) {
10226 k = isl_basic_map_alloc_div(bmap);
10227 if (k < 0)
10228 goto error;
10229 isl_int_set_si(bmap->div[k][0], 0);
10231 for (i = 0; i < eq->n_row; ++i) {
10232 l = isl_basic_map_alloc_equality(bmap);
10233 if (l < 0)
10234 goto error;
10235 for (j = 0, pos = 0; j < 5; ++j) {
10236 int off = isl_basic_map_offset(bmap, c[j]);
10237 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10238 isl_int_set(bmap->eq[l][off + k],
10239 eq->row[i][pos]);
10240 ++pos;
10244 for (i = 0; i < ineq->n_row; ++i) {
10245 l = isl_basic_map_alloc_inequality(bmap);
10246 if (l < 0)
10247 goto error;
10248 for (j = 0, pos = 0; j < 5; ++j) {
10249 int off = isl_basic_map_offset(bmap, c[j]);
10250 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10251 isl_int_set(bmap->ineq[l][off + k],
10252 ineq->row[i][pos]);
10253 ++pos;
10258 isl_space_free(dim);
10259 isl_mat_free(eq);
10260 isl_mat_free(ineq);
10262 return bmap;
10263 error:
10264 isl_space_free(dim);
10265 isl_mat_free(eq);
10266 isl_mat_free(ineq);
10267 return NULL;
10270 __isl_give isl_mat *isl_basic_set_equalities_matrix(
10271 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10272 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10274 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
10275 c1, c2, c3, c4, isl_dim_in);
10278 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
10279 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10280 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10282 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
10283 c1, c2, c3, c4, isl_dim_in);
10286 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
10287 __isl_take isl_space *dim,
10288 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10289 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10291 return (isl_basic_set*)
10292 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
10293 c1, c2, c3, c4, isl_dim_in);
10296 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
10298 if (!bmap)
10299 return -1;
10301 return isl_space_can_zip(bmap->dim);
10304 int isl_map_can_zip(__isl_keep isl_map *map)
10306 if (!map)
10307 return -1;
10309 return isl_space_can_zip(map->dim);
10312 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10313 * (A -> C) -> (B -> D).
10315 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10317 unsigned pos;
10318 unsigned n1;
10319 unsigned n2;
10321 if (!bmap)
10322 return NULL;
10324 if (!isl_basic_map_can_zip(bmap))
10325 isl_die(bmap->ctx, isl_error_invalid,
10326 "basic map cannot be zipped", goto error);
10327 pos = isl_basic_map_offset(bmap, isl_dim_in) +
10328 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10329 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10330 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10331 bmap = isl_basic_map_cow(bmap);
10332 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10333 if (!bmap)
10334 return NULL;
10335 bmap->dim = isl_space_zip(bmap->dim);
10336 if (!bmap->dim)
10337 goto error;
10338 return bmap;
10339 error:
10340 isl_basic_map_free(bmap);
10341 return NULL;
10344 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10345 * (A -> C) -> (B -> D).
10347 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
10349 int i;
10351 if (!map)
10352 return NULL;
10354 if (!isl_map_can_zip(map))
10355 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
10356 goto error);
10358 map = isl_map_cow(map);
10359 if (!map)
10360 return NULL;
10362 for (i = 0; i < map->n; ++i) {
10363 map->p[i] = isl_basic_map_zip(map->p[i]);
10364 if (!map->p[i])
10365 goto error;
10368 map->dim = isl_space_zip(map->dim);
10369 if (!map->dim)
10370 goto error;
10372 return map;
10373 error:
10374 isl_map_free(map);
10375 return NULL;
10378 /* Can we apply isl_basic_map_curry to "bmap"?
10379 * That is, does it have a nested relation in its domain?
10381 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
10383 if (!bmap)
10384 return -1;
10386 return isl_space_can_curry(bmap->dim);
10389 /* Can we apply isl_map_curry to "map"?
10390 * That is, does it have a nested relation in its domain?
10392 int isl_map_can_curry(__isl_keep isl_map *map)
10394 if (!map)
10395 return -1;
10397 return isl_space_can_curry(map->dim);
10400 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10401 * A -> (B -> C).
10403 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
10406 if (!bmap)
10407 return NULL;
10409 if (!isl_basic_map_can_curry(bmap))
10410 isl_die(bmap->ctx, isl_error_invalid,
10411 "basic map cannot be curried", goto error);
10412 bmap = isl_basic_map_cow(bmap);
10413 if (!bmap)
10414 return NULL;
10415 bmap->dim = isl_space_curry(bmap->dim);
10416 if (!bmap->dim)
10417 goto error;
10418 return bmap;
10419 error:
10420 isl_basic_map_free(bmap);
10421 return NULL;
10424 /* Given a map (A -> B) -> C, return the corresponding map
10425 * A -> (B -> C).
10427 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
10429 int i;
10431 if (!map)
10432 return NULL;
10434 if (!isl_map_can_curry(map))
10435 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
10436 goto error);
10438 map = isl_map_cow(map);
10439 if (!map)
10440 return NULL;
10442 for (i = 0; i < map->n; ++i) {
10443 map->p[i] = isl_basic_map_curry(map->p[i]);
10444 if (!map->p[i])
10445 goto error;
10448 map->dim = isl_space_curry(map->dim);
10449 if (!map->dim)
10450 goto error;
10452 return map;
10453 error:
10454 isl_map_free(map);
10455 return NULL;
10458 /* Can we apply isl_basic_map_uncurry to "bmap"?
10459 * That is, does it have a nested relation in its domain?
10461 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
10463 if (!bmap)
10464 return -1;
10466 return isl_space_can_uncurry(bmap->dim);
10469 /* Can we apply isl_map_uncurry to "map"?
10470 * That is, does it have a nested relation in its domain?
10472 int isl_map_can_uncurry(__isl_keep isl_map *map)
10474 if (!map)
10475 return -1;
10477 return isl_space_can_uncurry(map->dim);
10480 /* Given a basic map A -> (B -> C), return the corresponding basic map
10481 * (A -> B) -> C.
10483 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
10486 if (!bmap)
10487 return NULL;
10489 if (!isl_basic_map_can_uncurry(bmap))
10490 isl_die(bmap->ctx, isl_error_invalid,
10491 "basic map cannot be uncurried",
10492 return isl_basic_map_free(bmap));
10493 bmap = isl_basic_map_cow(bmap);
10494 if (!bmap)
10495 return NULL;
10496 bmap->dim = isl_space_uncurry(bmap->dim);
10497 if (!bmap->dim)
10498 return isl_basic_map_free(bmap);
10499 return bmap;
10502 /* Given a map A -> (B -> C), return the corresponding map
10503 * (A -> B) -> C.
10505 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
10507 int i;
10509 if (!map)
10510 return NULL;
10512 if (!isl_map_can_uncurry(map))
10513 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
10514 return isl_map_free(map));
10516 map = isl_map_cow(map);
10517 if (!map)
10518 return NULL;
10520 for (i = 0; i < map->n; ++i) {
10521 map->p[i] = isl_basic_map_uncurry(map->p[i]);
10522 if (!map->p[i])
10523 return isl_map_free(map);
10526 map->dim = isl_space_uncurry(map->dim);
10527 if (!map->dim)
10528 return isl_map_free(map);
10530 return map;
10533 /* Construct a basic map mapping the domain of the affine expression
10534 * to a one-dimensional range prescribed by the affine expression.
10536 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
10538 int k;
10539 int pos;
10540 isl_local_space *ls;
10541 isl_basic_map *bmap;
10543 if (!aff)
10544 return NULL;
10546 ls = isl_aff_get_local_space(aff);
10547 bmap = isl_basic_map_from_local_space(ls);
10548 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
10549 k = isl_basic_map_alloc_equality(bmap);
10550 if (k < 0)
10551 goto error;
10553 pos = isl_basic_map_offset(bmap, isl_dim_out);
10554 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
10555 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
10556 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
10557 aff->v->size - (pos + 1));
10559 isl_aff_free(aff);
10560 bmap = isl_basic_map_finalize(bmap);
10561 return bmap;
10562 error:
10563 isl_aff_free(aff);
10564 isl_basic_map_free(bmap);
10565 return NULL;
10568 /* Construct a map mapping the domain of the affine expression
10569 * to a one-dimensional range prescribed by the affine expression.
10571 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
10573 isl_basic_map *bmap;
10575 bmap = isl_basic_map_from_aff(aff);
10576 return isl_map_from_basic_map(bmap);
10579 /* Construct a basic map mapping the domain the multi-affine expression
10580 * to its range, with each dimension in the range equated to the
10581 * corresponding affine expression.
10583 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
10584 __isl_take isl_multi_aff *maff)
10586 int i;
10587 isl_space *space;
10588 isl_basic_map *bmap;
10590 if (!maff)
10591 return NULL;
10593 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
10594 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
10595 "invalid space", return isl_multi_aff_free(maff));
10597 space = isl_space_domain(isl_multi_aff_get_space(maff));
10598 bmap = isl_basic_map_universe(isl_space_from_domain(space));
10600 for (i = 0; i < maff->n; ++i) {
10601 isl_aff *aff;
10602 isl_basic_map *bmap_i;
10604 aff = isl_aff_copy(maff->p[i]);
10605 bmap_i = isl_basic_map_from_aff(aff);
10607 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10610 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
10612 isl_multi_aff_free(maff);
10613 return bmap;
10616 /* Construct a map mapping the domain the multi-affine expression
10617 * to its range, with each dimension in the range equated to the
10618 * corresponding affine expression.
10620 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
10622 isl_basic_map *bmap;
10624 bmap = isl_basic_map_from_multi_aff(maff);
10625 return isl_map_from_basic_map(bmap);
10628 /* Construct a basic map mapping a domain in the given space to
10629 * to an n-dimensional range, with n the number of elements in the list,
10630 * where each coordinate in the range is prescribed by the
10631 * corresponding affine expression.
10632 * The domains of all affine expressions in the list are assumed to match
10633 * domain_dim.
10635 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
10636 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
10638 int i;
10639 isl_space *dim;
10640 isl_basic_map *bmap;
10642 if (!list)
10643 return NULL;
10645 dim = isl_space_from_domain(domain_dim);
10646 bmap = isl_basic_map_universe(dim);
10648 for (i = 0; i < list->n; ++i) {
10649 isl_aff *aff;
10650 isl_basic_map *bmap_i;
10652 aff = isl_aff_copy(list->p[i]);
10653 bmap_i = isl_basic_map_from_aff(aff);
10655 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10658 isl_aff_list_free(list);
10659 return bmap;
10662 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
10663 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10665 return isl_map_equate(set, type1, pos1, type2, pos2);
10668 /* Construct a basic map where the given dimensions are equal to each other.
10670 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
10671 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10673 isl_basic_map *bmap = NULL;
10674 int i;
10676 if (!space)
10677 return NULL;
10679 if (pos1 >= isl_space_dim(space, type1))
10680 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10681 "index out of bounds", goto error);
10682 if (pos2 >= isl_space_dim(space, type2))
10683 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10684 "index out of bounds", goto error);
10686 if (type1 == type2 && pos1 == pos2)
10687 return isl_basic_map_universe(space);
10689 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
10690 i = isl_basic_map_alloc_equality(bmap);
10691 if (i < 0)
10692 goto error;
10693 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10694 pos1 += isl_basic_map_offset(bmap, type1);
10695 pos2 += isl_basic_map_offset(bmap, type2);
10696 isl_int_set_si(bmap->eq[i][pos1], -1);
10697 isl_int_set_si(bmap->eq[i][pos2], 1);
10698 bmap = isl_basic_map_finalize(bmap);
10699 isl_space_free(space);
10700 return bmap;
10701 error:
10702 isl_space_free(space);
10703 isl_basic_map_free(bmap);
10704 return NULL;
10707 /* Add a constraint imposing that the given two dimensions are equal.
10709 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
10710 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10712 isl_basic_map *eq;
10714 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
10716 bmap = isl_basic_map_intersect(bmap, eq);
10718 return bmap;
10721 /* Add a constraint imposing that the given two dimensions are equal.
10723 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
10724 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10726 isl_basic_map *bmap;
10728 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
10730 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10732 return map;
10735 /* Add a constraint imposing that the given two dimensions have opposite values.
10737 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
10738 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10740 isl_basic_map *bmap = NULL;
10741 int i;
10743 if (!map)
10744 return NULL;
10746 if (pos1 >= isl_map_dim(map, type1))
10747 isl_die(map->ctx, isl_error_invalid,
10748 "index out of bounds", goto error);
10749 if (pos2 >= isl_map_dim(map, type2))
10750 isl_die(map->ctx, isl_error_invalid,
10751 "index out of bounds", goto error);
10753 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
10754 i = isl_basic_map_alloc_equality(bmap);
10755 if (i < 0)
10756 goto error;
10757 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10758 pos1 += isl_basic_map_offset(bmap, type1);
10759 pos2 += isl_basic_map_offset(bmap, type2);
10760 isl_int_set_si(bmap->eq[i][pos1], 1);
10761 isl_int_set_si(bmap->eq[i][pos2], 1);
10762 bmap = isl_basic_map_finalize(bmap);
10764 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10766 return map;
10767 error:
10768 isl_basic_map_free(bmap);
10769 isl_map_free(map);
10770 return NULL;
10773 /* Add a constraint imposing that the value of the first dimension is
10774 * greater than or equal to that of the second.
10776 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
10777 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10779 isl_constraint *c;
10780 isl_local_space *ls;
10782 if (!bmap)
10783 return NULL;
10785 if (pos1 >= isl_basic_map_dim(bmap, type1))
10786 isl_die(bmap->ctx, isl_error_invalid,
10787 "index out of bounds", return isl_basic_map_free(bmap));
10788 if (pos2 >= isl_basic_map_dim(bmap, type2))
10789 isl_die(bmap->ctx, isl_error_invalid,
10790 "index out of bounds", return isl_basic_map_free(bmap));
10792 if (type1 == type2 && pos1 == pos2)
10793 return bmap;
10795 ls = isl_local_space_from_space(isl_basic_map_get_space(bmap));
10796 c = isl_inequality_alloc(ls);
10797 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
10798 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
10799 bmap = isl_basic_map_add_constraint(bmap, c);
10801 return bmap;
10804 /* Add a constraint imposing that the value of the first dimension is
10805 * greater than that of the second.
10807 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
10808 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10810 isl_basic_map *bmap = NULL;
10811 int i;
10813 if (!map)
10814 return NULL;
10816 if (pos1 >= isl_map_dim(map, type1))
10817 isl_die(map->ctx, isl_error_invalid,
10818 "index out of bounds", goto error);
10819 if (pos2 >= isl_map_dim(map, type2))
10820 isl_die(map->ctx, isl_error_invalid,
10821 "index out of bounds", goto error);
10823 if (type1 == type2 && pos1 == pos2) {
10824 isl_space *space = isl_map_get_space(map);
10825 isl_map_free(map);
10826 return isl_map_empty(space);
10829 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 0, 1);
10830 i = isl_basic_map_alloc_inequality(bmap);
10831 if (i < 0)
10832 goto error;
10833 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
10834 pos1 += isl_basic_map_offset(bmap, type1);
10835 pos2 += isl_basic_map_offset(bmap, type2);
10836 isl_int_set_si(bmap->ineq[i][pos1], 1);
10837 isl_int_set_si(bmap->ineq[i][pos2], -1);
10838 isl_int_set_si(bmap->ineq[i][0], -1);
10839 bmap = isl_basic_map_finalize(bmap);
10841 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10843 return map;
10844 error:
10845 isl_basic_map_free(bmap);
10846 isl_map_free(map);
10847 return NULL;
10850 /* Add a constraint imposing that the value of the first dimension is
10851 * smaller than that of the second.
10853 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
10854 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10856 return isl_map_order_gt(map, type2, pos2, type1, pos1);
10859 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
10860 int pos)
10862 isl_aff *div;
10863 isl_local_space *ls;
10865 if (!bmap)
10866 return NULL;
10868 if (!isl_basic_map_divs_known(bmap))
10869 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
10870 "some divs are unknown", return NULL);
10872 ls = isl_basic_map_get_local_space(bmap);
10873 div = isl_local_space_get_div(ls, pos);
10874 isl_local_space_free(ls);
10876 return div;
10879 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
10880 int pos)
10882 return isl_basic_map_get_div(bset, pos);
10885 /* Plug in "subs" for dimension "type", "pos" of "bset".
10887 * Let i be the dimension to replace and let "subs" be of the form
10889 * f/d
10891 * Any integer division with a non-zero coefficient for i,
10893 * floor((a i + g)/m)
10895 * is replaced by
10897 * floor((a f + d g)/(m d))
10899 * Constraints of the form
10901 * a i + g
10903 * are replaced by
10905 * a f + d g
10907 * We currently require that "subs" is an integral expression.
10908 * Handling rational expressions may require us to add stride constraints
10909 * as we do in isl_basic_set_preimage_multi_aff.
10911 __isl_give isl_basic_set *isl_basic_set_substitute(
10912 __isl_take isl_basic_set *bset,
10913 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10915 int i;
10916 isl_int v;
10917 isl_ctx *ctx;
10919 if (bset && isl_basic_set_plain_is_empty(bset))
10920 return bset;
10922 bset = isl_basic_set_cow(bset);
10923 if (!bset || !subs)
10924 goto error;
10926 ctx = isl_basic_set_get_ctx(bset);
10927 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
10928 isl_die(ctx, isl_error_invalid,
10929 "spaces don't match", goto error);
10930 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
10931 isl_die(ctx, isl_error_unsupported,
10932 "cannot handle divs yet", goto error);
10933 if (!isl_int_is_one(subs->v->el[0]))
10934 isl_die(ctx, isl_error_invalid,
10935 "can only substitute integer expressions", goto error);
10937 pos += isl_basic_set_offset(bset, type);
10939 isl_int_init(v);
10941 for (i = 0; i < bset->n_eq; ++i) {
10942 if (isl_int_is_zero(bset->eq[i][pos]))
10943 continue;
10944 isl_int_set(v, bset->eq[i][pos]);
10945 isl_int_set_si(bset->eq[i][pos], 0);
10946 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
10947 v, subs->v->el + 1, subs->v->size - 1);
10950 for (i = 0; i < bset->n_ineq; ++i) {
10951 if (isl_int_is_zero(bset->ineq[i][pos]))
10952 continue;
10953 isl_int_set(v, bset->ineq[i][pos]);
10954 isl_int_set_si(bset->ineq[i][pos], 0);
10955 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
10956 v, subs->v->el + 1, subs->v->size - 1);
10959 for (i = 0; i < bset->n_div; ++i) {
10960 if (isl_int_is_zero(bset->div[i][1 + pos]))
10961 continue;
10962 isl_int_set(v, bset->div[i][1 + pos]);
10963 isl_int_set_si(bset->div[i][1 + pos], 0);
10964 isl_seq_combine(bset->div[i] + 1,
10965 subs->v->el[0], bset->div[i] + 1,
10966 v, subs->v->el + 1, subs->v->size - 1);
10967 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
10970 isl_int_clear(v);
10972 bset = isl_basic_set_simplify(bset);
10973 return isl_basic_set_finalize(bset);
10974 error:
10975 isl_basic_set_free(bset);
10976 return NULL;
10979 /* Plug in "subs" for dimension "type", "pos" of "set".
10981 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
10982 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10984 int i;
10986 if (set && isl_set_plain_is_empty(set))
10987 return set;
10989 set = isl_set_cow(set);
10990 if (!set || !subs)
10991 goto error;
10993 for (i = set->n - 1; i >= 0; --i) {
10994 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
10995 if (remove_if_empty(set, i) < 0)
10996 goto error;
10999 return set;
11000 error:
11001 isl_set_free(set);
11002 return NULL;
11005 /* Check if the range of "ma" is compatible with "space".
11006 * Return -1 if anything is wrong.
11008 static int check_space_compatible_range_multi_aff(
11009 __isl_keep isl_space *space, __isl_keep isl_multi_aff *ma)
11011 int m;
11012 isl_space *ma_space;
11014 ma_space = isl_multi_aff_get_space(ma);
11015 m = isl_space_is_range_internal(space, ma_space);
11016 isl_space_free(ma_space);
11017 if (m >= 0 && !m)
11018 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11019 "spaces don't match", return -1);
11020 return m;
11023 /* Check if the range of "ma" is compatible with "bset".
11024 * Return -1 if anything is wrong.
11026 static int check_basic_set_compatible_range_multi_aff(
11027 __isl_keep isl_basic_set *bset, __isl_keep isl_multi_aff *ma)
11029 return check_space_compatible_range_multi_aff(bset->dim, ma);
11032 /* Copy the divs from "ma" to "bset", adding zeros for the coefficients
11033 * of the other divs in "bset".
11035 static int set_ma_divs(__isl_keep isl_basic_set *bset,
11036 __isl_keep isl_multi_aff *ma, int n_div)
11038 int i;
11039 isl_local_space *ls;
11041 if (n_div == 0)
11042 return 0;
11044 ls = isl_aff_get_domain_local_space(ma->p[0]);
11045 if (!ls)
11046 return -1;
11048 for (i = 0; i < n_div; ++i) {
11049 isl_seq_cpy(bset->div[i], ls->div->row[i], ls->div->n_col);
11050 isl_seq_clr(bset->div[i] + ls->div->n_col, bset->n_div - n_div);
11051 if (isl_basic_set_add_div_constraints(bset, i) < 0)
11052 goto error;
11055 isl_local_space_free(ls);
11056 return 0;
11057 error:
11058 isl_local_space_free(ls);
11059 return -1;
11062 /* How many stride constraints does "ma" enforce?
11063 * That is, how many of the affine expressions have a denominator
11064 * different from one?
11066 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
11068 int i;
11069 int strides = 0;
11071 for (i = 0; i < ma->n; ++i)
11072 if (!isl_int_is_one(ma->p[i]->v->el[0]))
11073 strides++;
11075 return strides;
11078 /* For each affine expression in ma of the form
11080 * x_i = (f_i y + h_i)/m_i
11082 * with m_i different from one, add a constraint to "bset"
11083 * of the form
11085 * f_i y + h_i = m_i alpha_i
11087 * with alpha_i an additional existentially quantified variable.
11089 static __isl_give isl_basic_set *add_ma_strides(
11090 __isl_take isl_basic_set *bset, __isl_keep isl_multi_aff *ma)
11092 int i, k;
11093 int div;
11094 int total;
11096 total = isl_basic_set_total_dim(bset);
11097 for (i = 0; i < ma->n; ++i) {
11098 int len;
11100 if (isl_int_is_one(ma->p[i]->v->el[0]))
11101 continue;
11102 div = isl_basic_set_alloc_div(bset);
11103 k = isl_basic_set_alloc_equality(bset);
11104 if (div < 0 || k < 0)
11105 goto error;
11106 isl_int_set_si(bset->div[div][0], 0);
11107 len = ma->p[i]->v->size;
11108 isl_seq_cpy(bset->eq[k], ma->p[i]->v->el + 1, len - 1);
11109 isl_seq_clr(bset->eq[k] + len - 1, 1 + total - (len - 1));
11110 isl_int_neg(bset->eq[k][1 + total], ma->p[i]->v->el[0]);
11111 total++;
11114 return bset;
11115 error:
11116 isl_basic_set_free(bset);
11117 return NULL;
11120 /* Compute the preimage of "bset" under the function represented by "ma".
11121 * In other words, plug in "ma" in "bset". The result is a basic set
11122 * that lives in the domain space of "ma".
11124 * If bset is represented by
11126 * A(p) + B x + C(divs) >= 0
11128 * and ma is represented by
11130 * x = D(p) + F(y) + G(divs')
11132 * then the result is
11134 * A(p) + B D(p) + B F(y) + B G(divs') + C(divs) >= 0
11136 * The divs in the input set are similarly adjusted.
11137 * In particular
11139 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
11141 * becomes
11143 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
11145 * If bset is not a rational set and if F(y) involves any denominators
11147 * x_i = (f_i y + h_i)/m_i
11149 * the additional constraints are added to ensure that we only
11150 * map back integer points. That is we enforce
11152 * f_i y + h_i = m_i alpha_i
11154 * with alpha_i an additional existentially quantified variable.
11156 * We first copy over the divs from "ma".
11157 * Then we add the modified constraints and divs from "bset".
11158 * Finally, we add the stride constraints, if needed.
11160 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
11161 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
11163 int i, k;
11164 isl_space *space;
11165 isl_basic_set *res = NULL;
11166 int n_div_bset, n_div_ma;
11167 isl_int f, c1, c2, g;
11168 int rational, strides;
11170 isl_int_init(f);
11171 isl_int_init(c1);
11172 isl_int_init(c2);
11173 isl_int_init(g);
11175 ma = isl_multi_aff_align_divs(ma);
11176 if (!bset || !ma)
11177 goto error;
11178 if (check_basic_set_compatible_range_multi_aff(bset, ma) < 0)
11179 goto error;
11181 n_div_bset = isl_basic_set_dim(bset, isl_dim_div);
11182 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
11184 space = isl_space_domain(isl_multi_aff_get_space(ma));
11185 rational = isl_basic_set_is_rational(bset);
11186 strides = rational ? 0 : multi_aff_strides(ma);
11187 res = isl_basic_set_alloc_space(space, n_div_ma + n_div_bset + strides,
11188 bset->n_eq + strides, bset->n_ineq + 2 * n_div_ma);
11189 if (rational)
11190 res = isl_basic_set_set_rational(res);
11192 for (i = 0; i < n_div_ma + n_div_bset; ++i)
11193 if (isl_basic_set_alloc_div(res) < 0)
11194 goto error;
11196 if (set_ma_divs(res, ma, n_div_ma) < 0)
11197 goto error;
11199 for (i = 0; i < bset->n_eq; ++i) {
11200 k = isl_basic_set_alloc_equality(res);
11201 if (k < 0)
11202 goto error;
11203 isl_seq_preimage(res->eq[k], bset->eq[i], ma, n_div_ma,
11204 n_div_bset, f, c1, c2, g, 0);
11207 for (i = 0; i < bset->n_ineq; ++i) {
11208 k = isl_basic_set_alloc_inequality(res);
11209 if (k < 0)
11210 goto error;
11211 isl_seq_preimage(res->ineq[k], bset->ineq[i], ma, n_div_ma,
11212 n_div_bset, f, c1, c2, g, 0);
11215 for (i = 0; i < bset->n_div; ++i) {
11216 if (isl_int_is_zero(bset->div[i][0])) {
11217 isl_int_set_si(res->div[n_div_ma + i][0], 0);
11218 continue;
11220 isl_seq_preimage(res->div[n_div_ma + i], bset->div[i],
11221 ma, n_div_ma, n_div_bset, f, c1, c2, g, 1);
11224 if (strides)
11225 res = add_ma_strides(res, ma);
11227 isl_int_clear(f);
11228 isl_int_clear(c1);
11229 isl_int_clear(c2);
11230 isl_int_clear(g);
11231 isl_basic_set_free(bset);
11232 isl_multi_aff_free(ma);
11233 res = isl_basic_set_simplify(res);
11234 return isl_basic_set_finalize(res);
11235 error:
11236 isl_int_clear(f);
11237 isl_int_clear(c1);
11238 isl_int_clear(c2);
11239 isl_int_clear(g);
11240 isl_basic_set_free(bset);
11241 isl_multi_aff_free(ma);
11242 isl_basic_set_free(res);
11243 return NULL;
11246 /* Check if the range of "ma" is compatible with "set".
11247 * Return -1 if anything is wrong.
11249 static int check_set_compatible_range_multi_aff(
11250 __isl_keep isl_set *set, __isl_keep isl_multi_aff *ma)
11252 return check_space_compatible_range_multi_aff(set->dim, ma);
11255 /* Compute the preimage of "set" under the function represented by "ma".
11256 * In other words, plug in "ma" in "set. The result is a set
11257 * that lives in the domain space of "ma".
11259 static __isl_give isl_set *set_preimage_multi_aff(__isl_take isl_set *set,
11260 __isl_take isl_multi_aff *ma)
11262 int i;
11264 set = isl_set_cow(set);
11265 ma = isl_multi_aff_align_divs(ma);
11266 if (!set || !ma)
11267 goto error;
11268 if (check_set_compatible_range_multi_aff(set, ma) < 0)
11269 goto error;
11271 for (i = 0; i < set->n; ++i) {
11272 set->p[i] = isl_basic_set_preimage_multi_aff(set->p[i],
11273 isl_multi_aff_copy(ma));
11274 if (!set->p[i])
11275 goto error;
11278 isl_space_free(set->dim);
11279 set->dim = isl_multi_aff_get_domain_space(ma);
11280 if (!set->dim)
11281 goto error;
11283 isl_multi_aff_free(ma);
11284 if (set->n > 1)
11285 ISL_F_CLR(set, ISL_MAP_DISJOINT);
11286 ISL_F_CLR(set, ISL_SET_NORMALIZED);
11287 return set;
11288 error:
11289 isl_multi_aff_free(ma);
11290 isl_set_free(set);
11291 return NULL;
11294 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
11295 __isl_take isl_multi_aff *ma)
11297 if (!set || !ma)
11298 goto error;
11300 if (isl_space_match(set->dim, isl_dim_param, ma->space, isl_dim_param))
11301 return set_preimage_multi_aff(set, ma);
11303 if (!isl_space_has_named_params(set->dim) ||
11304 !isl_space_has_named_params(ma->space))
11305 isl_die(set->ctx, isl_error_invalid,
11306 "unaligned unnamed parameters", goto error);
11307 set = isl_set_align_params(set, isl_multi_aff_get_space(ma));
11308 ma = isl_multi_aff_align_params(ma, isl_set_get_space(set));
11310 return set_preimage_multi_aff(set, ma);
11311 error:
11312 isl_multi_aff_free(ma);
11313 return isl_set_free(set);
11316 /* Compute the preimage of "set" under the function represented by "pma".
11317 * In other words, plug in "pma" in "set. The result is a set
11318 * that lives in the domain space of "pma".
11320 static __isl_give isl_set *set_preimage_pw_multi_aff(__isl_take isl_set *set,
11321 __isl_take isl_pw_multi_aff *pma)
11323 int i;
11324 isl_set *res;
11326 if (!pma)
11327 goto error;
11329 if (pma->n == 0) {
11330 isl_pw_multi_aff_free(pma);
11331 res = isl_set_empty(isl_set_get_space(set));
11332 isl_set_free(set);
11333 return res;
11336 res = isl_set_preimage_multi_aff(isl_set_copy(set),
11337 isl_multi_aff_copy(pma->p[0].maff));
11338 res = isl_set_intersect(res, isl_set_copy(pma->p[0].set));
11340 for (i = 1; i < pma->n; ++i) {
11341 isl_set *res_i;
11343 res_i = isl_set_preimage_multi_aff(isl_set_copy(set),
11344 isl_multi_aff_copy(pma->p[i].maff));
11345 res_i = isl_set_intersect(res_i, isl_set_copy(pma->p[i].set));
11346 res = isl_set_union(res, res_i);
11349 isl_pw_multi_aff_free(pma);
11350 isl_set_free(set);
11351 return res;
11352 error:
11353 isl_pw_multi_aff_free(pma);
11354 isl_set_free(set);
11355 return NULL;
11358 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
11359 __isl_take isl_pw_multi_aff *pma)
11361 if (!set || !pma)
11362 goto error;
11364 if (isl_space_match(set->dim, isl_dim_param, pma->dim, isl_dim_param))
11365 return set_preimage_pw_multi_aff(set, pma);
11367 if (!isl_space_has_named_params(set->dim) ||
11368 !isl_space_has_named_params(pma->dim))
11369 isl_die(set->ctx, isl_error_invalid,
11370 "unaligned unnamed parameters", goto error);
11371 set = isl_set_align_params(set, isl_pw_multi_aff_get_space(pma));
11372 pma = isl_pw_multi_aff_align_params(pma, isl_set_get_space(set));
11374 return set_preimage_pw_multi_aff(set, pma);
11375 error:
11376 isl_pw_multi_aff_free(pma);
11377 return isl_set_free(set);