add private isl_basic_set_get_divs
[isl.git] / isl_map.c
blobd70f3f32c1517df650b548b817afab6b7991e826
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 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/lp.h>
22 #include <isl/seq.h>
23 #include <isl/set.h>
24 #include <isl/map.h>
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
28 #include "isl_tab.h"
29 #include <isl/vec.h>
30 #include <isl_mat_private.h>
31 #include <isl_dim_map.h>
32 #include <isl_local_space_private.h>
33 #include <isl_aff_private.h>
34 #include <isl_options_private.h>
35 #include <isl_morph.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 /* Extract the divs in "bset" as a matrix.
290 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
292 return isl_basic_map_get_divs(bset);
295 __isl_give isl_local_space *isl_basic_map_get_local_space(
296 __isl_keep isl_basic_map *bmap)
298 isl_mat *div;
300 if (!bmap)
301 return NULL;
303 div = isl_basic_map_get_divs(bmap);
304 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
307 __isl_give isl_local_space *isl_basic_set_get_local_space(
308 __isl_keep isl_basic_set *bset)
310 return isl_basic_map_get_local_space(bset);
313 __isl_give isl_basic_map *isl_basic_map_from_local_space(
314 __isl_take isl_local_space *ls)
316 int i;
317 int n_div;
318 isl_basic_map *bmap;
320 if (!ls)
321 return NULL;
323 n_div = isl_local_space_dim(ls, isl_dim_div);
324 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
325 n_div, 0, 2 * n_div);
327 for (i = 0; i < n_div; ++i)
328 if (isl_basic_map_alloc_div(bmap) < 0)
329 goto error;
331 for (i = 0; i < n_div; ++i) {
332 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
333 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
334 goto error;
337 isl_local_space_free(ls);
338 return bmap;
339 error:
340 isl_local_space_free(ls);
341 isl_basic_map_free(bmap);
342 return NULL;
345 __isl_give isl_basic_set *isl_basic_set_from_local_space(
346 __isl_take isl_local_space *ls)
348 return isl_basic_map_from_local_space(ls);
351 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
353 if (!map)
354 return NULL;
355 return isl_space_copy(map->dim);
358 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
360 if (!set)
361 return NULL;
362 return isl_space_copy(set->dim);
365 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
366 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
368 bmap = isl_basic_map_cow(bmap);
369 if (!bmap)
370 return NULL;
371 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
372 if (!bmap->dim)
373 goto error;
374 bmap = isl_basic_map_finalize(bmap);
375 return bmap;
376 error:
377 isl_basic_map_free(bmap);
378 return NULL;
381 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
382 __isl_take isl_basic_set *bset, const char *s)
384 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
387 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
388 enum isl_dim_type type)
390 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
393 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
394 enum isl_dim_type type, const char *s)
396 int i;
398 map = isl_map_cow(map);
399 if (!map)
400 return NULL;
402 map->dim = isl_space_set_tuple_name(map->dim, type, s);
403 if (!map->dim)
404 goto error;
406 for (i = 0; i < map->n; ++i) {
407 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
408 if (!map->p[i])
409 goto error;
412 return map;
413 error:
414 isl_map_free(map);
415 return NULL;
418 /* Does the input or output tuple have a name?
420 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
422 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
425 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
426 enum isl_dim_type type)
428 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
431 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
432 const char *s)
434 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
437 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
438 enum isl_dim_type type, __isl_take isl_id *id)
440 map = isl_map_cow(map);
441 if (!map)
442 return isl_id_free(id);
444 map->dim = isl_space_set_tuple_id(map->dim, type, id);
446 return isl_map_reset_space(map, isl_space_copy(map->dim));
449 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
450 __isl_take isl_id *id)
452 return isl_map_set_tuple_id(set, isl_dim_set, id);
455 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
456 enum isl_dim_type type)
458 map = isl_map_cow(map);
459 if (!map)
460 return NULL;
462 map->dim = isl_space_reset_tuple_id(map->dim, type);
464 return isl_map_reset_space(map, isl_space_copy(map->dim));
467 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
469 return isl_map_reset_tuple_id(set, isl_dim_set);
472 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
474 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
477 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
478 enum isl_dim_type type)
480 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
483 int isl_set_has_tuple_id(__isl_keep isl_set *set)
485 return isl_map_has_tuple_id(set, isl_dim_set);
488 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
490 return isl_map_get_tuple_id(set, isl_dim_set);
493 /* Does the set tuple have a name?
495 int isl_set_has_tuple_name(__isl_keep isl_set *set)
497 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
501 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
503 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
506 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
508 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
511 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
512 enum isl_dim_type type, unsigned pos)
514 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
517 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
518 enum isl_dim_type type, unsigned pos)
520 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
523 /* Does the given dimension have a name?
525 int isl_map_has_dim_name(__isl_keep isl_map *map,
526 enum isl_dim_type type, unsigned pos)
528 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
531 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
532 enum isl_dim_type type, unsigned pos)
534 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
537 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
538 enum isl_dim_type type, unsigned pos)
540 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
543 /* Does the given dimension have a name?
545 int isl_set_has_dim_name(__isl_keep isl_set *set,
546 enum isl_dim_type type, unsigned pos)
548 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
551 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
552 __isl_take isl_basic_map *bmap,
553 enum isl_dim_type type, unsigned pos, const char *s)
555 bmap = isl_basic_map_cow(bmap);
556 if (!bmap)
557 return NULL;
558 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
559 if (!bmap->dim)
560 goto error;
561 return isl_basic_map_finalize(bmap);
562 error:
563 isl_basic_map_free(bmap);
564 return NULL;
567 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
568 enum isl_dim_type type, unsigned pos, const char *s)
570 int i;
572 map = isl_map_cow(map);
573 if (!map)
574 return NULL;
576 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
577 if (!map->dim)
578 goto error;
580 for (i = 0; i < map->n; ++i) {
581 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
582 if (!map->p[i])
583 goto error;
586 return map;
587 error:
588 isl_map_free(map);
589 return NULL;
592 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
593 __isl_take isl_basic_set *bset,
594 enum isl_dim_type type, unsigned pos, const char *s)
596 return (isl_basic_set *)isl_basic_map_set_dim_name(
597 (isl_basic_map *)bset, type, pos, s);
600 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
601 enum isl_dim_type type, unsigned pos, const char *s)
603 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
606 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
607 enum isl_dim_type type, unsigned pos)
609 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
612 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
613 enum isl_dim_type type, unsigned pos)
615 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
618 int isl_map_has_dim_id(__isl_keep isl_map *map,
619 enum isl_dim_type type, unsigned pos)
621 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
624 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
625 enum isl_dim_type type, unsigned pos)
627 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
630 int isl_set_has_dim_id(__isl_keep isl_set *set,
631 enum isl_dim_type type, unsigned pos)
633 return isl_map_has_dim_id(set, type, pos);
636 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
637 enum isl_dim_type type, unsigned pos)
639 return isl_map_get_dim_id(set, type, pos);
642 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
643 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
645 map = isl_map_cow(map);
646 if (!map)
647 return isl_id_free(id);
649 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
651 return isl_map_reset_space(map, isl_space_copy(map->dim));
654 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
655 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
657 return isl_map_set_dim_id(set, type, pos, id);
660 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
661 __isl_keep isl_id *id)
663 if (!map)
664 return -1;
665 return isl_space_find_dim_by_id(map->dim, type, id);
668 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
669 __isl_keep isl_id *id)
671 return isl_map_find_dim_by_id(set, type, id);
674 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
675 const char *name)
677 if (!map)
678 return -1;
679 return isl_space_find_dim_by_name(map->dim, type, name);
682 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
683 const char *name)
685 return isl_map_find_dim_by_name(set, type, name);
688 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
690 if (!bmap)
691 return -1;
692 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
695 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
697 return isl_basic_map_is_rational(bset);
700 /* Does "bmap" contain any rational points?
702 * If "bmap" has an equality for each dimension, equating the dimension
703 * to an integer constant, then it has no rational points, even if it
704 * is marked as rational.
706 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
708 int has_rational = 1;
709 unsigned total;
711 if (!bmap)
712 return -1;
713 if (isl_basic_map_plain_is_empty(bmap))
714 return 0;
715 if (!isl_basic_map_is_rational(bmap))
716 return 0;
717 bmap = isl_basic_map_copy(bmap);
718 bmap = isl_basic_map_implicit_equalities(bmap);
719 if (!bmap)
720 return -1;
721 total = isl_basic_map_total_dim(bmap);
722 if (bmap->n_eq == total) {
723 int i, j;
724 for (i = 0; i < bmap->n_eq; ++i) {
725 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
726 if (j < 0)
727 break;
728 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
729 !isl_int_is_negone(bmap->eq[i][1 + j]))
730 break;
731 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
732 total - j - 1);
733 if (j >= 0)
734 break;
736 if (i == bmap->n_eq)
737 has_rational = 0;
739 isl_basic_map_free(bmap);
741 return has_rational;
744 /* Does "map" contain any rational points?
746 int isl_map_has_rational(__isl_keep isl_map *map)
748 int i;
749 int has_rational;
751 if (!map)
752 return -1;
753 for (i = 0; i < map->n; ++i) {
754 has_rational = isl_basic_map_has_rational(map->p[i]);
755 if (has_rational < 0)
756 return -1;
757 if (has_rational)
758 return 1;
760 return 0;
763 /* Does "set" contain any rational points?
765 int isl_set_has_rational(__isl_keep isl_set *set)
767 return isl_map_has_rational(set);
770 /* Is this basic set a parameter domain?
772 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
774 if (!bset)
775 return -1;
776 return isl_space_is_params(bset->dim);
779 /* Is this set a parameter domain?
781 int isl_set_is_params(__isl_keep isl_set *set)
783 if (!set)
784 return -1;
785 return isl_space_is_params(set->dim);
788 /* Is this map actually a parameter domain?
789 * Users should never call this function. Outside of isl,
790 * a map can never be a parameter domain.
792 int isl_map_is_params(__isl_keep isl_map *map)
794 if (!map)
795 return -1;
796 return isl_space_is_params(map->dim);
799 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
800 struct isl_basic_map *bmap, unsigned extra,
801 unsigned n_eq, unsigned n_ineq)
803 int i;
804 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
806 bmap->ctx = ctx;
807 isl_ctx_ref(ctx);
809 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
810 if (isl_blk_is_error(bmap->block))
811 goto error;
813 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
814 if (!bmap->ineq)
815 goto error;
817 if (extra == 0) {
818 bmap->block2 = isl_blk_empty();
819 bmap->div = NULL;
820 } else {
821 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
822 if (isl_blk_is_error(bmap->block2))
823 goto error;
825 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
826 if (!bmap->div)
827 goto error;
830 for (i = 0; i < n_ineq + n_eq; ++i)
831 bmap->ineq[i] = bmap->block.data + i * row_size;
833 for (i = 0; i < extra; ++i)
834 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
836 bmap->ref = 1;
837 bmap->flags = 0;
838 bmap->c_size = n_eq + n_ineq;
839 bmap->eq = bmap->ineq + n_ineq;
840 bmap->extra = extra;
841 bmap->n_eq = 0;
842 bmap->n_ineq = 0;
843 bmap->n_div = 0;
844 bmap->sample = NULL;
846 return bmap;
847 error:
848 isl_basic_map_free(bmap);
849 return NULL;
852 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
853 unsigned nparam, unsigned dim, unsigned extra,
854 unsigned n_eq, unsigned n_ineq)
856 struct isl_basic_map *bmap;
857 isl_space *space;
859 space = isl_space_set_alloc(ctx, nparam, dim);
860 if (!space)
861 return NULL;
863 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
864 return (struct isl_basic_set *)bmap;
867 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
868 unsigned extra, unsigned n_eq, unsigned n_ineq)
870 struct isl_basic_map *bmap;
871 if (!dim)
872 return NULL;
873 isl_assert(dim->ctx, dim->n_in == 0, goto error);
874 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
875 return (struct isl_basic_set *)bmap;
876 error:
877 isl_space_free(dim);
878 return NULL;
881 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
882 unsigned extra, unsigned n_eq, unsigned n_ineq)
884 struct isl_basic_map *bmap;
886 if (!dim)
887 return NULL;
888 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
889 if (!bmap)
890 goto error;
891 bmap->dim = dim;
893 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
894 error:
895 isl_space_free(dim);
896 return NULL;
899 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
900 unsigned nparam, unsigned in, unsigned out, unsigned extra,
901 unsigned n_eq, unsigned n_ineq)
903 struct isl_basic_map *bmap;
904 isl_space *dim;
906 dim = isl_space_alloc(ctx, nparam, in, out);
907 if (!dim)
908 return NULL;
910 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
911 return bmap;
914 static void dup_constraints(
915 struct isl_basic_map *dst, struct isl_basic_map *src)
917 int i;
918 unsigned total = isl_basic_map_total_dim(src);
920 for (i = 0; i < src->n_eq; ++i) {
921 int j = isl_basic_map_alloc_equality(dst);
922 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
925 for (i = 0; i < src->n_ineq; ++i) {
926 int j = isl_basic_map_alloc_inequality(dst);
927 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
930 for (i = 0; i < src->n_div; ++i) {
931 int j = isl_basic_map_alloc_div(dst);
932 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
934 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
937 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
939 struct isl_basic_map *dup;
941 if (!bmap)
942 return NULL;
943 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
944 bmap->n_div, bmap->n_eq, bmap->n_ineq);
945 if (!dup)
946 return NULL;
947 dup_constraints(dup, bmap);
948 dup->flags = bmap->flags;
949 dup->sample = isl_vec_copy(bmap->sample);
950 return dup;
953 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
955 struct isl_basic_map *dup;
957 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
958 return (struct isl_basic_set *)dup;
961 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
963 if (!bset)
964 return NULL;
966 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
967 bset->ref++;
968 return bset;
970 return isl_basic_set_dup(bset);
973 struct isl_set *isl_set_copy(struct isl_set *set)
975 if (!set)
976 return NULL;
978 set->ref++;
979 return set;
982 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
984 if (!bmap)
985 return NULL;
987 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
988 bmap->ref++;
989 return bmap;
991 bmap = isl_basic_map_dup(bmap);
992 if (bmap)
993 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
994 return bmap;
997 struct isl_map *isl_map_copy(struct isl_map *map)
999 if (!map)
1000 return NULL;
1002 map->ref++;
1003 return map;
1006 void *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1008 if (!bmap)
1009 return NULL;
1011 if (--bmap->ref > 0)
1012 return NULL;
1014 isl_ctx_deref(bmap->ctx);
1015 free(bmap->div);
1016 isl_blk_free(bmap->ctx, bmap->block2);
1017 free(bmap->ineq);
1018 isl_blk_free(bmap->ctx, bmap->block);
1019 isl_vec_free(bmap->sample);
1020 isl_space_free(bmap->dim);
1021 free(bmap);
1023 return NULL;
1026 void *isl_basic_set_free(struct isl_basic_set *bset)
1028 return isl_basic_map_free((struct isl_basic_map *)bset);
1031 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1033 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1036 __isl_give isl_map *isl_map_align_params_map_map_and(
1037 __isl_take isl_map *map1, __isl_take isl_map *map2,
1038 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1039 __isl_take isl_map *map2))
1041 if (!map1 || !map2)
1042 goto error;
1043 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1044 return fn(map1, map2);
1045 if (!isl_space_has_named_params(map1->dim) ||
1046 !isl_space_has_named_params(map2->dim))
1047 isl_die(map1->ctx, isl_error_invalid,
1048 "unaligned unnamed parameters", goto error);
1049 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1050 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1051 return fn(map1, map2);
1052 error:
1053 isl_map_free(map1);
1054 isl_map_free(map2);
1055 return NULL;
1058 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1059 __isl_keep isl_map *map2,
1060 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1062 int r;
1064 if (!map1 || !map2)
1065 return -1;
1066 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1067 return fn(map1, map2);
1068 if (!isl_space_has_named_params(map1->dim) ||
1069 !isl_space_has_named_params(map2->dim))
1070 isl_die(map1->ctx, isl_error_invalid,
1071 "unaligned unnamed parameters", return -1);
1072 map1 = isl_map_copy(map1);
1073 map2 = isl_map_copy(map2);
1074 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1075 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1076 r = fn(map1, map2);
1077 isl_map_free(map1);
1078 isl_map_free(map2);
1079 return r;
1082 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1084 struct isl_ctx *ctx;
1085 if (!bmap)
1086 return -1;
1087 ctx = bmap->ctx;
1088 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1089 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1090 return -1);
1091 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1092 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1093 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1094 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1095 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1096 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1097 isl_int *t;
1098 int j = isl_basic_map_alloc_inequality(bmap);
1099 if (j < 0)
1100 return -1;
1101 t = bmap->ineq[j];
1102 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1103 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1104 bmap->eq[-1] = t;
1105 bmap->n_eq++;
1106 bmap->n_ineq--;
1107 bmap->eq--;
1108 return 0;
1110 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1111 bmap->extra - bmap->n_div);
1112 return bmap->n_eq++;
1115 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1117 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1120 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1122 if (!bmap)
1123 return -1;
1124 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1125 bmap->n_eq -= n;
1126 return 0;
1129 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1131 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1134 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1136 isl_int *t;
1137 if (!bmap)
1138 return -1;
1139 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1141 if (pos != bmap->n_eq - 1) {
1142 t = bmap->eq[pos];
1143 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1144 bmap->eq[bmap->n_eq - 1] = t;
1146 bmap->n_eq--;
1147 return 0;
1150 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1152 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1155 /* Turn inequality "pos" of "bmap" into an equality.
1157 * In particular, we move the inequality in front of the equalities
1158 * and move the last inequality in the position of the moved inequality.
1159 * Note that isl_tab_make_equalities_explicit depends on this particular
1160 * change in the ordering of the constraints.
1162 void isl_basic_map_inequality_to_equality(
1163 struct isl_basic_map *bmap, unsigned pos)
1165 isl_int *t;
1167 t = bmap->ineq[pos];
1168 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1169 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1170 bmap->eq[-1] = t;
1171 bmap->n_eq++;
1172 bmap->n_ineq--;
1173 bmap->eq--;
1174 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1175 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1176 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1177 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1180 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1182 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1185 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1187 struct isl_ctx *ctx;
1188 if (!bmap)
1189 return -1;
1190 ctx = bmap->ctx;
1191 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1192 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1193 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1194 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1195 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1196 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1197 1 + isl_basic_map_total_dim(bmap),
1198 bmap->extra - bmap->n_div);
1199 return bmap->n_ineq++;
1202 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1204 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1207 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1209 if (!bmap)
1210 return -1;
1211 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1212 bmap->n_ineq -= n;
1213 return 0;
1216 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1218 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1221 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1223 isl_int *t;
1224 if (!bmap)
1225 return -1;
1226 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1228 if (pos != bmap->n_ineq - 1) {
1229 t = bmap->ineq[pos];
1230 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1231 bmap->ineq[bmap->n_ineq - 1] = t;
1232 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1234 bmap->n_ineq--;
1235 return 0;
1238 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1240 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1243 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1244 isl_int *eq)
1246 int k;
1248 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1249 if (!bmap)
1250 return NULL;
1251 k = isl_basic_map_alloc_equality(bmap);
1252 if (k < 0)
1253 goto error;
1254 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1255 return bmap;
1256 error:
1257 isl_basic_map_free(bmap);
1258 return NULL;
1261 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1262 isl_int *eq)
1264 return (isl_basic_set *)
1265 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1268 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1269 isl_int *ineq)
1271 int k;
1273 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1274 if (!bmap)
1275 return NULL;
1276 k = isl_basic_map_alloc_inequality(bmap);
1277 if (k < 0)
1278 goto error;
1279 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1280 return bmap;
1281 error:
1282 isl_basic_map_free(bmap);
1283 return NULL;
1286 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1287 isl_int *ineq)
1289 return (isl_basic_set *)
1290 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1293 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1295 if (!bmap)
1296 return -1;
1297 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1298 isl_seq_clr(bmap->div[bmap->n_div] +
1299 1 + 1 + isl_basic_map_total_dim(bmap),
1300 bmap->extra - bmap->n_div);
1301 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1302 return bmap->n_div++;
1305 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1307 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1310 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1312 if (!bmap)
1313 return -1;
1314 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1315 bmap->n_div -= n;
1316 return 0;
1319 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1321 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1324 /* Copy constraint from src to dst, putting the vars of src at offset
1325 * dim_off in dst and the divs of src at offset div_off in dst.
1326 * If both sets are actually map, then dim_off applies to the input
1327 * variables.
1329 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1330 struct isl_basic_map *src_map, isl_int *src,
1331 unsigned in_off, unsigned out_off, unsigned div_off)
1333 unsigned src_nparam = isl_basic_map_n_param(src_map);
1334 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1335 unsigned src_in = isl_basic_map_n_in(src_map);
1336 unsigned dst_in = isl_basic_map_n_in(dst_map);
1337 unsigned src_out = isl_basic_map_n_out(src_map);
1338 unsigned dst_out = isl_basic_map_n_out(dst_map);
1339 isl_int_set(dst[0], src[0]);
1340 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1341 if (dst_nparam > src_nparam)
1342 isl_seq_clr(dst+1+src_nparam,
1343 dst_nparam - src_nparam);
1344 isl_seq_clr(dst+1+dst_nparam, in_off);
1345 isl_seq_cpy(dst+1+dst_nparam+in_off,
1346 src+1+src_nparam,
1347 isl_min(dst_in-in_off, src_in));
1348 if (dst_in-in_off > src_in)
1349 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1350 dst_in - in_off - src_in);
1351 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1352 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1353 src+1+src_nparam+src_in,
1354 isl_min(dst_out-out_off, src_out));
1355 if (dst_out-out_off > src_out)
1356 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1357 dst_out - out_off - src_out);
1358 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1359 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1360 src+1+src_nparam+src_in+src_out,
1361 isl_min(dst_map->extra-div_off, src_map->n_div));
1362 if (dst_map->n_div-div_off > src_map->n_div)
1363 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1364 div_off+src_map->n_div,
1365 dst_map->n_div - div_off - src_map->n_div);
1368 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1369 struct isl_basic_map *src_map, isl_int *src,
1370 unsigned in_off, unsigned out_off, unsigned div_off)
1372 isl_int_set(dst[0], src[0]);
1373 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1376 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1377 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1379 int i;
1380 unsigned div_off;
1382 if (!bmap1 || !bmap2)
1383 goto error;
1385 div_off = bmap1->n_div;
1387 for (i = 0; i < bmap2->n_eq; ++i) {
1388 int i1 = isl_basic_map_alloc_equality(bmap1);
1389 if (i1 < 0)
1390 goto error;
1391 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1392 i_pos, o_pos, div_off);
1395 for (i = 0; i < bmap2->n_ineq; ++i) {
1396 int i1 = isl_basic_map_alloc_inequality(bmap1);
1397 if (i1 < 0)
1398 goto error;
1399 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1400 i_pos, o_pos, div_off);
1403 for (i = 0; i < bmap2->n_div; ++i) {
1404 int i1 = isl_basic_map_alloc_div(bmap1);
1405 if (i1 < 0)
1406 goto error;
1407 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1408 i_pos, o_pos, div_off);
1411 isl_basic_map_free(bmap2);
1413 return bmap1;
1415 error:
1416 isl_basic_map_free(bmap1);
1417 isl_basic_map_free(bmap2);
1418 return NULL;
1421 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1422 struct isl_basic_set *bset2, unsigned pos)
1424 return (struct isl_basic_set *)
1425 add_constraints((struct isl_basic_map *)bset1,
1426 (struct isl_basic_map *)bset2, 0, pos);
1429 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1430 __isl_take isl_space *dim, unsigned extra,
1431 unsigned n_eq, unsigned n_ineq)
1433 struct isl_basic_map *ext;
1434 unsigned flags;
1435 int dims_ok;
1437 if (!dim)
1438 goto error;
1440 if (!base)
1441 goto error;
1443 dims_ok = isl_space_is_equal(base->dim, dim) &&
1444 base->extra >= base->n_div + extra;
1446 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1447 room_for_ineq(base, n_ineq)) {
1448 isl_space_free(dim);
1449 return base;
1452 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1453 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1454 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1455 extra += base->extra;
1456 n_eq += base->n_eq;
1457 n_ineq += base->n_ineq;
1459 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1460 dim = NULL;
1461 if (!ext)
1462 goto error;
1464 if (dims_ok)
1465 ext->sample = isl_vec_copy(base->sample);
1466 flags = base->flags;
1467 ext = add_constraints(ext, base, 0, 0);
1468 if (ext) {
1469 ext->flags = flags;
1470 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1473 return ext;
1475 error:
1476 isl_space_free(dim);
1477 isl_basic_map_free(base);
1478 return NULL;
1481 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1482 __isl_take isl_space *dim, unsigned extra,
1483 unsigned n_eq, unsigned n_ineq)
1485 return (struct isl_basic_set *)
1486 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1487 extra, n_eq, n_ineq);
1490 struct isl_basic_map *isl_basic_map_extend_constraints(
1491 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1493 if (!base)
1494 return NULL;
1495 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1496 0, n_eq, n_ineq);
1499 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1500 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1501 unsigned n_eq, unsigned n_ineq)
1503 struct isl_basic_map *bmap;
1504 isl_space *dim;
1506 if (!base)
1507 return NULL;
1508 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1509 if (!dim)
1510 goto error;
1512 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1513 return bmap;
1514 error:
1515 isl_basic_map_free(base);
1516 return NULL;
1519 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1520 unsigned nparam, unsigned dim, unsigned extra,
1521 unsigned n_eq, unsigned n_ineq)
1523 return (struct isl_basic_set *)
1524 isl_basic_map_extend((struct isl_basic_map *)base,
1525 nparam, 0, dim, extra, n_eq, n_ineq);
1528 struct isl_basic_set *isl_basic_set_extend_constraints(
1529 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1531 return (struct isl_basic_set *)
1532 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1533 n_eq, n_ineq);
1536 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1538 return (struct isl_basic_set *)
1539 isl_basic_map_cow((struct isl_basic_map *)bset);
1542 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1544 if (!bmap)
1545 return NULL;
1547 if (bmap->ref > 1) {
1548 bmap->ref--;
1549 bmap = isl_basic_map_dup(bmap);
1551 if (bmap)
1552 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1553 return bmap;
1556 struct isl_set *isl_set_cow(struct isl_set *set)
1558 if (!set)
1559 return NULL;
1561 if (set->ref == 1)
1562 return set;
1563 set->ref--;
1564 return isl_set_dup(set);
1567 struct isl_map *isl_map_cow(struct isl_map *map)
1569 if (!map)
1570 return NULL;
1572 if (map->ref == 1)
1573 return map;
1574 map->ref--;
1575 return isl_map_dup(map);
1578 static void swap_vars(struct isl_blk blk, isl_int *a,
1579 unsigned a_len, unsigned b_len)
1581 isl_seq_cpy(blk.data, a+a_len, b_len);
1582 isl_seq_cpy(blk.data+b_len, a, a_len);
1583 isl_seq_cpy(a, blk.data, b_len+a_len);
1586 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1587 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1589 int i;
1590 struct isl_blk blk;
1592 if (!bmap)
1593 goto error;
1595 isl_assert(bmap->ctx,
1596 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1598 if (n1 == 0 || n2 == 0)
1599 return bmap;
1601 bmap = isl_basic_map_cow(bmap);
1602 if (!bmap)
1603 return NULL;
1605 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1606 if (isl_blk_is_error(blk))
1607 goto error;
1609 for (i = 0; i < bmap->n_eq; ++i)
1610 swap_vars(blk,
1611 bmap->eq[i] + pos, n1, n2);
1613 for (i = 0; i < bmap->n_ineq; ++i)
1614 swap_vars(blk,
1615 bmap->ineq[i] + pos, n1, n2);
1617 for (i = 0; i < bmap->n_div; ++i)
1618 swap_vars(blk,
1619 bmap->div[i]+1 + pos, n1, n2);
1621 isl_blk_free(bmap->ctx, blk);
1623 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1624 bmap = isl_basic_map_gauss(bmap, NULL);
1625 return isl_basic_map_finalize(bmap);
1626 error:
1627 isl_basic_map_free(bmap);
1628 return NULL;
1631 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1632 __isl_take isl_basic_set *bset, unsigned n)
1634 unsigned dim;
1635 unsigned nparam;
1637 if (!bset)
1638 return NULL;
1640 nparam = isl_basic_set_n_param(bset);
1641 dim = isl_basic_set_n_dim(bset);
1642 isl_assert(bset->ctx, n <= dim, goto error);
1644 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1645 error:
1646 isl_basic_set_free(bset);
1647 return NULL;
1650 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1652 int i = 0;
1653 unsigned total;
1654 if (!bmap)
1655 goto error;
1656 total = isl_basic_map_total_dim(bmap);
1657 isl_basic_map_free_div(bmap, bmap->n_div);
1658 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1659 if (bmap->n_eq > 0)
1660 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1661 else {
1662 i = isl_basic_map_alloc_equality(bmap);
1663 if (i < 0)
1664 goto error;
1666 isl_int_set_si(bmap->eq[i][0], 1);
1667 isl_seq_clr(bmap->eq[i]+1, total);
1668 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1669 isl_vec_free(bmap->sample);
1670 bmap->sample = NULL;
1671 return isl_basic_map_finalize(bmap);
1672 error:
1673 isl_basic_map_free(bmap);
1674 return NULL;
1677 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1679 return (struct isl_basic_set *)
1680 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1683 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1684 * of "bmap").
1686 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1688 isl_int *t = bmap->div[a];
1689 bmap->div[a] = bmap->div[b];
1690 bmap->div[b] = t;
1693 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1694 * div definitions accordingly.
1696 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1698 int i;
1699 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1701 swap_div(bmap, a, b);
1703 for (i = 0; i < bmap->n_eq; ++i)
1704 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1706 for (i = 0; i < bmap->n_ineq; ++i)
1707 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1709 for (i = 0; i < bmap->n_div; ++i)
1710 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1711 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1714 /* Eliminate the specified n dimensions starting at first from the
1715 * constraints, without removing the dimensions from the space.
1716 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1718 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1719 enum isl_dim_type type, unsigned first, unsigned n)
1721 int i;
1723 if (!map)
1724 return NULL;
1725 if (n == 0)
1726 return map;
1728 if (first + n > isl_map_dim(map, type) || first + n < first)
1729 isl_die(map->ctx, isl_error_invalid,
1730 "index out of bounds", goto error);
1732 map = isl_map_cow(map);
1733 if (!map)
1734 return NULL;
1736 for (i = 0; i < map->n; ++i) {
1737 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1738 if (!map->p[i])
1739 goto error;
1741 return map;
1742 error:
1743 isl_map_free(map);
1744 return NULL;
1747 /* Eliminate the specified n dimensions starting at first from the
1748 * constraints, without removing the dimensions from the space.
1749 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1751 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1752 enum isl_dim_type type, unsigned first, unsigned n)
1754 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1757 /* Eliminate the specified n dimensions starting at first from the
1758 * constraints, without removing the dimensions from the space.
1759 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1761 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1762 unsigned first, unsigned n)
1764 return isl_set_eliminate(set, isl_dim_set, first, n);
1767 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1768 __isl_take isl_basic_map *bmap)
1770 if (!bmap)
1771 return NULL;
1772 bmap = isl_basic_map_eliminate_vars(bmap,
1773 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1774 if (!bmap)
1775 return NULL;
1776 bmap->n_div = 0;
1777 return isl_basic_map_finalize(bmap);
1780 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1781 __isl_take isl_basic_set *bset)
1783 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1784 (struct isl_basic_map *)bset);
1787 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1789 int i;
1791 if (!map)
1792 return NULL;
1793 if (map->n == 0)
1794 return map;
1796 map = isl_map_cow(map);
1797 if (!map)
1798 return NULL;
1800 for (i = 0; i < map->n; ++i) {
1801 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1802 if (!map->p[i])
1803 goto error;
1805 return map;
1806 error:
1807 isl_map_free(map);
1808 return NULL;
1811 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1813 return isl_map_remove_divs(set);
1816 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1817 enum isl_dim_type type, unsigned first, unsigned n)
1819 if (!bmap)
1820 return NULL;
1821 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1822 goto error);
1823 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1824 return bmap;
1825 bmap = isl_basic_map_eliminate_vars(bmap,
1826 isl_basic_map_offset(bmap, type) - 1 + first, n);
1827 if (!bmap)
1828 return bmap;
1829 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1830 return bmap;
1831 bmap = isl_basic_map_drop(bmap, type, first, n);
1832 return bmap;
1833 error:
1834 isl_basic_map_free(bmap);
1835 return NULL;
1838 /* Return true if the definition of the given div (recursively) involves
1839 * any of the given variables.
1841 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1842 unsigned first, unsigned n)
1844 int i;
1845 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1847 if (isl_int_is_zero(bmap->div[div][0]))
1848 return 0;
1849 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1850 return 1;
1852 for (i = bmap->n_div - 1; i >= 0; --i) {
1853 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1854 continue;
1855 if (div_involves_vars(bmap, i, first, n))
1856 return 1;
1859 return 0;
1862 /* Try and add a lower and/or upper bound on "div" to "bmap"
1863 * based on inequality "i".
1864 * "total" is the total number of variables (excluding the divs).
1865 * "v" is a temporary object that can be used during the calculations.
1866 * If "lb" is set, then a lower bound should be constructed.
1867 * If "ub" is set, then an upper bound should be constructed.
1869 * The calling function has already checked that the inequality does not
1870 * reference "div", but we still need to check that the inequality is
1871 * of the right form. We'll consider the case where we want to construct
1872 * a lower bound. The construction of upper bounds is similar.
1874 * Let "div" be of the form
1876 * q = floor((a + f(x))/d)
1878 * We essentially check if constraint "i" is of the form
1880 * b + f(x) >= 0
1882 * so that we can use it to derive a lower bound on "div".
1883 * However, we allow a slightly more general form
1885 * b + g(x) >= 0
1887 * with the condition that the coefficients of g(x) - f(x) are all
1888 * divisible by d.
1889 * Rewriting this constraint as
1891 * 0 >= -b - g(x)
1893 * adding a + f(x) to both sides and dividing by d, we obtain
1895 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1897 * Taking the floor on both sides, we obtain
1899 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1901 * or
1903 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1905 * In the case of an upper bound, we construct the constraint
1907 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1910 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1911 __isl_take isl_basic_map *bmap, int div, int i,
1912 unsigned total, isl_int v, int lb, int ub)
1914 int j;
1916 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1917 if (lb) {
1918 isl_int_sub(v, bmap->ineq[i][1 + j],
1919 bmap->div[div][1 + 1 + j]);
1920 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1922 if (ub) {
1923 isl_int_add(v, bmap->ineq[i][1 + j],
1924 bmap->div[div][1 + 1 + j]);
1925 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1928 if (!lb && !ub)
1929 return bmap;
1931 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1932 if (lb) {
1933 int k = isl_basic_map_alloc_inequality(bmap);
1934 if (k < 0)
1935 goto error;
1936 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1937 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
1938 bmap->div[div][1 + j]);
1939 isl_int_cdiv_q(bmap->ineq[k][j],
1940 bmap->ineq[k][j], bmap->div[div][0]);
1942 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
1944 if (ub) {
1945 int k = isl_basic_map_alloc_inequality(bmap);
1946 if (k < 0)
1947 goto error;
1948 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1949 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
1950 bmap->div[div][1 + j]);
1951 isl_int_fdiv_q(bmap->ineq[k][j],
1952 bmap->ineq[k][j], bmap->div[div][0]);
1954 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
1957 return bmap;
1958 error:
1959 isl_basic_map_free(bmap);
1960 return NULL;
1963 /* This function is called right before "div" is eliminated from "bmap"
1964 * using Fourier-Motzkin.
1965 * Look through the constraints of "bmap" for constraints on the argument
1966 * of the integer division and use them to construct constraints on the
1967 * integer division itself. These constraints can then be combined
1968 * during the Fourier-Motzkin elimination.
1969 * Note that it is only useful to introduce lower bounds on "div"
1970 * if "bmap" already contains upper bounds on "div" as the newly
1971 * introduce lower bounds can then be combined with the pre-existing
1972 * upper bounds. Similarly for upper bounds.
1973 * We therefore first check if "bmap" contains any lower and/or upper bounds
1974 * on "div".
1976 * It is interesting to note that the introduction of these constraints
1977 * can indeed lead to more accurate results, even when compared to
1978 * deriving constraints on the argument of "div" from constraints on "div".
1979 * Consider, for example, the set
1981 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
1983 * The second constraint can be rewritten as
1985 * 2 * [(-i-2j+3)/4] + k >= 0
1987 * from which we can derive
1989 * -i - 2j + 3 >= -2k
1991 * or
1993 * i + 2j <= 3 + 2k
1995 * Combined with the first constraint, we obtain
1997 * -3 <= 3 + 2k or k >= -3
1999 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2000 * the first constraint, we obtain
2002 * [(i + 2j)/4] >= [-3/4] = -1
2004 * Combining this constraint with the second constraint, we obtain
2006 * k >= -2
2008 static __isl_give isl_basic_map *insert_bounds_on_div(
2009 __isl_take isl_basic_map *bmap, int div)
2011 int i;
2012 int check_lb, check_ub;
2013 isl_int v;
2014 unsigned total;
2016 if (!bmap)
2017 return NULL;
2019 if (isl_int_is_zero(bmap->div[div][0]))
2020 return bmap;
2022 total = isl_space_dim(bmap->dim, isl_dim_all);
2024 check_lb = 0;
2025 check_ub = 0;
2026 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2027 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2028 if (s > 0)
2029 check_ub = 1;
2030 if (s < 0)
2031 check_lb = 1;
2034 if (!check_lb && !check_ub)
2035 return bmap;
2037 isl_int_init(v);
2039 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2040 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2041 continue;
2043 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2044 check_lb, check_ub);
2047 isl_int_clear(v);
2049 return bmap;
2052 /* Remove all divs (recursively) involving any of the given dimensions
2053 * in their definitions.
2055 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2056 __isl_take isl_basic_map *bmap,
2057 enum isl_dim_type type, unsigned first, unsigned n)
2059 int i;
2061 if (!bmap)
2062 return NULL;
2063 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2064 goto error);
2065 first += isl_basic_map_offset(bmap, type);
2067 for (i = bmap->n_div - 1; i >= 0; --i) {
2068 if (!div_involves_vars(bmap, i, first, n))
2069 continue;
2070 bmap = insert_bounds_on_div(bmap, i);
2071 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2072 if (!bmap)
2073 return NULL;
2074 i = bmap->n_div;
2077 return bmap;
2078 error:
2079 isl_basic_map_free(bmap);
2080 return NULL;
2083 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2084 __isl_take isl_basic_set *bset,
2085 enum isl_dim_type type, unsigned first, unsigned n)
2087 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2090 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2091 enum isl_dim_type type, unsigned first, unsigned n)
2093 int i;
2095 if (!map)
2096 return NULL;
2097 if (map->n == 0)
2098 return map;
2100 map = isl_map_cow(map);
2101 if (!map)
2102 return NULL;
2104 for (i = 0; i < map->n; ++i) {
2105 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2106 type, first, n);
2107 if (!map->p[i])
2108 goto error;
2110 return map;
2111 error:
2112 isl_map_free(map);
2113 return NULL;
2116 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2117 enum isl_dim_type type, unsigned first, unsigned n)
2119 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2120 type, first, n);
2123 /* Does the desciption of "bmap" depend on the specified dimensions?
2124 * We also check whether the dimensions appear in any of the div definitions.
2125 * In principle there is no need for this check. If the dimensions appear
2126 * in a div definition, they also appear in the defining constraints of that
2127 * div.
2129 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2130 enum isl_dim_type type, unsigned first, unsigned n)
2132 int i;
2134 if (!bmap)
2135 return -1;
2137 if (first + n > isl_basic_map_dim(bmap, type))
2138 isl_die(bmap->ctx, isl_error_invalid,
2139 "index out of bounds", return -1);
2141 first += isl_basic_map_offset(bmap, type);
2142 for (i = 0; i < bmap->n_eq; ++i)
2143 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2144 return 1;
2145 for (i = 0; i < bmap->n_ineq; ++i)
2146 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2147 return 1;
2148 for (i = 0; i < bmap->n_div; ++i) {
2149 if (isl_int_is_zero(bmap->div[i][0]))
2150 continue;
2151 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2152 return 1;
2155 return 0;
2158 int isl_map_involves_dims(__isl_keep isl_map *map,
2159 enum isl_dim_type type, unsigned first, unsigned n)
2161 int i;
2163 if (!map)
2164 return -1;
2166 if (first + n > isl_map_dim(map, type))
2167 isl_die(map->ctx, isl_error_invalid,
2168 "index out of bounds", return -1);
2170 for (i = 0; i < map->n; ++i) {
2171 int involves = isl_basic_map_involves_dims(map->p[i],
2172 type, first, n);
2173 if (involves < 0 || involves)
2174 return involves;
2177 return 0;
2180 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2181 enum isl_dim_type type, unsigned first, unsigned n)
2183 return isl_basic_map_involves_dims(bset, type, first, n);
2186 int isl_set_involves_dims(__isl_keep isl_set *set,
2187 enum isl_dim_type type, unsigned first, unsigned n)
2189 return isl_map_involves_dims(set, type, first, n);
2192 /* Return true if the definition of the given div is unknown or depends
2193 * on unknown divs.
2195 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2197 int i;
2198 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2200 if (isl_int_is_zero(bmap->div[div][0]))
2201 return 1;
2203 for (i = bmap->n_div - 1; i >= 0; --i) {
2204 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2205 continue;
2206 if (div_is_unknown(bmap, i))
2207 return 1;
2210 return 0;
2213 /* Remove all divs that are unknown or defined in terms of unknown divs.
2215 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2216 __isl_take isl_basic_map *bmap)
2218 int i;
2220 if (!bmap)
2221 return NULL;
2223 for (i = bmap->n_div - 1; i >= 0; --i) {
2224 if (!div_is_unknown(bmap, i))
2225 continue;
2226 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2227 if (!bmap)
2228 return NULL;
2229 i = bmap->n_div;
2232 return bmap;
2235 /* Remove all divs that are unknown or defined in terms of unknown divs.
2237 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2238 __isl_take isl_basic_set *bset)
2240 return isl_basic_map_remove_unknown_divs(bset);
2243 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2245 int i;
2247 if (!map)
2248 return NULL;
2249 if (map->n == 0)
2250 return map;
2252 map = isl_map_cow(map);
2253 if (!map)
2254 return NULL;
2256 for (i = 0; i < map->n; ++i) {
2257 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2258 if (!map->p[i])
2259 goto error;
2261 return map;
2262 error:
2263 isl_map_free(map);
2264 return NULL;
2267 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2269 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2272 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2273 __isl_take isl_basic_set *bset,
2274 enum isl_dim_type type, unsigned first, unsigned n)
2276 return (isl_basic_set *)
2277 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2280 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2281 enum isl_dim_type type, unsigned first, unsigned n)
2283 int i;
2285 if (n == 0)
2286 return map;
2288 map = isl_map_cow(map);
2289 if (!map)
2290 return NULL;
2291 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2293 for (i = 0; i < map->n; ++i) {
2294 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2295 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2296 if (!map->p[i])
2297 goto error;
2299 map = isl_map_drop(map, type, first, n);
2300 return map;
2301 error:
2302 isl_map_free(map);
2303 return NULL;
2306 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2307 enum isl_dim_type type, unsigned first, unsigned n)
2309 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2312 /* Project out n inputs starting at first using Fourier-Motzkin */
2313 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2314 unsigned first, unsigned n)
2316 return isl_map_remove_dims(map, isl_dim_in, first, n);
2319 static void dump_term(struct isl_basic_map *bmap,
2320 isl_int c, int pos, FILE *out)
2322 const char *name;
2323 unsigned in = isl_basic_map_n_in(bmap);
2324 unsigned dim = in + isl_basic_map_n_out(bmap);
2325 unsigned nparam = isl_basic_map_n_param(bmap);
2326 if (!pos)
2327 isl_int_print(out, c, 0);
2328 else {
2329 if (!isl_int_is_one(c))
2330 isl_int_print(out, c, 0);
2331 if (pos < 1 + nparam) {
2332 name = isl_space_get_dim_name(bmap->dim,
2333 isl_dim_param, pos - 1);
2334 if (name)
2335 fprintf(out, "%s", name);
2336 else
2337 fprintf(out, "p%d", pos - 1);
2338 } else if (pos < 1 + nparam + in)
2339 fprintf(out, "i%d", pos - 1 - nparam);
2340 else if (pos < 1 + nparam + dim)
2341 fprintf(out, "o%d", pos - 1 - nparam - in);
2342 else
2343 fprintf(out, "e%d", pos - 1 - nparam - dim);
2347 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2348 int sign, FILE *out)
2350 int i;
2351 int first;
2352 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2353 isl_int v;
2355 isl_int_init(v);
2356 for (i = 0, first = 1; i < len; ++i) {
2357 if (isl_int_sgn(c[i]) * sign <= 0)
2358 continue;
2359 if (!first)
2360 fprintf(out, " + ");
2361 first = 0;
2362 isl_int_abs(v, c[i]);
2363 dump_term(bmap, v, i, out);
2365 isl_int_clear(v);
2366 if (first)
2367 fprintf(out, "0");
2370 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2371 const char *op, FILE *out, int indent)
2373 int i;
2375 fprintf(out, "%*s", indent, "");
2377 dump_constraint_sign(bmap, c, 1, out);
2378 fprintf(out, " %s ", op);
2379 dump_constraint_sign(bmap, c, -1, out);
2381 fprintf(out, "\n");
2383 for (i = bmap->n_div; i < bmap->extra; ++i) {
2384 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2385 continue;
2386 fprintf(out, "%*s", indent, "");
2387 fprintf(out, "ERROR: unused div coefficient not zero\n");
2388 abort();
2392 static void dump_constraints(struct isl_basic_map *bmap,
2393 isl_int **c, unsigned n,
2394 const char *op, FILE *out, int indent)
2396 int i;
2398 for (i = 0; i < n; ++i)
2399 dump_constraint(bmap, c[i], op, out, indent);
2402 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2404 int j;
2405 int first = 1;
2406 unsigned total = isl_basic_map_total_dim(bmap);
2408 for (j = 0; j < 1 + total; ++j) {
2409 if (isl_int_is_zero(exp[j]))
2410 continue;
2411 if (!first && isl_int_is_pos(exp[j]))
2412 fprintf(out, "+");
2413 dump_term(bmap, exp[j], j, out);
2414 first = 0;
2418 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2420 int i;
2422 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2423 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2425 for (i = 0; i < bmap->n_div; ++i) {
2426 fprintf(out, "%*s", indent, "");
2427 fprintf(out, "e%d = [(", i);
2428 dump_affine(bmap, bmap->div[i]+1, out);
2429 fprintf(out, ")/");
2430 isl_int_print(out, bmap->div[i][0], 0);
2431 fprintf(out, "]\n");
2435 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2436 FILE *out, int indent)
2438 if (!bset) {
2439 fprintf(out, "null basic set\n");
2440 return;
2443 fprintf(out, "%*s", indent, "");
2444 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2445 bset->ref, bset->dim->nparam, bset->dim->n_out,
2446 bset->extra, bset->flags);
2447 dump((struct isl_basic_map *)bset, out, indent);
2450 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2451 FILE *out, int indent)
2453 if (!bmap) {
2454 fprintf(out, "null basic map\n");
2455 return;
2458 fprintf(out, "%*s", indent, "");
2459 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2460 "flags: %x, n_name: %d\n",
2461 bmap->ref,
2462 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2463 bmap->extra, bmap->flags, bmap->dim->n_id);
2464 dump(bmap, out, indent);
2467 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2469 unsigned total;
2470 if (!bmap)
2471 return -1;
2472 total = isl_basic_map_total_dim(bmap);
2473 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2474 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2475 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2476 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2477 return 0;
2480 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2481 unsigned flags)
2483 struct isl_set *set;
2485 if (!dim)
2486 return NULL;
2487 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2488 isl_assert(dim->ctx, n >= 0, goto error);
2489 set = isl_alloc(dim->ctx, struct isl_set,
2490 sizeof(struct isl_set) +
2491 (n - 1) * sizeof(struct isl_basic_set *));
2492 if (!set)
2493 goto error;
2495 set->ctx = dim->ctx;
2496 isl_ctx_ref(set->ctx);
2497 set->ref = 1;
2498 set->size = n;
2499 set->n = 0;
2500 set->dim = dim;
2501 set->flags = flags;
2502 return set;
2503 error:
2504 isl_space_free(dim);
2505 return NULL;
2508 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2509 unsigned nparam, unsigned dim, int n, unsigned flags)
2511 struct isl_set *set;
2512 isl_space *dims;
2514 dims = isl_space_alloc(ctx, nparam, 0, dim);
2515 if (!dims)
2516 return NULL;
2518 set = isl_set_alloc_space(dims, n, flags);
2519 return set;
2522 /* Make sure "map" has room for at least "n" more basic maps.
2524 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2526 int i;
2527 struct isl_map *grown = NULL;
2529 if (!map)
2530 return NULL;
2531 isl_assert(map->ctx, n >= 0, goto error);
2532 if (map->n + n <= map->size)
2533 return map;
2534 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2535 if (!grown)
2536 goto error;
2537 for (i = 0; i < map->n; ++i) {
2538 grown->p[i] = isl_basic_map_copy(map->p[i]);
2539 if (!grown->p[i])
2540 goto error;
2541 grown->n++;
2543 isl_map_free(map);
2544 return grown;
2545 error:
2546 isl_map_free(grown);
2547 isl_map_free(map);
2548 return NULL;
2551 /* Make sure "set" has room for at least "n" more basic sets.
2553 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2555 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2558 struct isl_set *isl_set_dup(struct isl_set *set)
2560 int i;
2561 struct isl_set *dup;
2563 if (!set)
2564 return NULL;
2566 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2567 if (!dup)
2568 return NULL;
2569 for (i = 0; i < set->n; ++i)
2570 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2571 return dup;
2574 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2576 return isl_map_from_basic_map(bset);
2579 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2581 struct isl_map *map;
2583 if (!bmap)
2584 return NULL;
2586 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2587 return isl_map_add_basic_map(map, bmap);
2590 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2591 __isl_take isl_basic_set *bset)
2593 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2594 (struct isl_basic_map *)bset);
2597 void *isl_set_free(__isl_take isl_set *set)
2599 int i;
2601 if (!set)
2602 return NULL;
2604 if (--set->ref > 0)
2605 return NULL;
2607 isl_ctx_deref(set->ctx);
2608 for (i = 0; i < set->n; ++i)
2609 isl_basic_set_free(set->p[i]);
2610 isl_space_free(set->dim);
2611 free(set);
2613 return NULL;
2616 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2618 int i;
2620 if (!set) {
2621 fprintf(out, "null set\n");
2622 return;
2625 fprintf(out, "%*s", indent, "");
2626 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2627 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2628 set->flags);
2629 for (i = 0; i < set->n; ++i) {
2630 fprintf(out, "%*s", indent, "");
2631 fprintf(out, "basic set %d:\n", i);
2632 isl_basic_set_print_internal(set->p[i], out, indent+4);
2636 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2638 int i;
2640 if (!map) {
2641 fprintf(out, "null map\n");
2642 return;
2645 fprintf(out, "%*s", indent, "");
2646 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2647 "flags: %x, n_name: %d\n",
2648 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2649 map->dim->n_out, map->flags, map->dim->n_id);
2650 for (i = 0; i < map->n; ++i) {
2651 fprintf(out, "%*s", indent, "");
2652 fprintf(out, "basic map %d:\n", i);
2653 isl_basic_map_print_internal(map->p[i], out, indent+4);
2657 struct isl_basic_map *isl_basic_map_intersect_domain(
2658 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2660 struct isl_basic_map *bmap_domain;
2662 if (!bmap || !bset)
2663 goto error;
2665 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2666 bset->dim, isl_dim_param), goto error);
2668 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2669 isl_assert(bset->ctx,
2670 isl_basic_map_compatible_domain(bmap, bset), goto error);
2672 bmap = isl_basic_map_cow(bmap);
2673 if (!bmap)
2674 goto error;
2675 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2676 bset->n_div, bset->n_eq, bset->n_ineq);
2677 bmap_domain = isl_basic_map_from_domain(bset);
2678 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2680 bmap = isl_basic_map_simplify(bmap);
2681 return isl_basic_map_finalize(bmap);
2682 error:
2683 isl_basic_map_free(bmap);
2684 isl_basic_set_free(bset);
2685 return NULL;
2688 struct isl_basic_map *isl_basic_map_intersect_range(
2689 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2691 struct isl_basic_map *bmap_range;
2693 if (!bmap || !bset)
2694 goto error;
2696 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2697 bset->dim, isl_dim_param), goto error);
2699 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2700 isl_assert(bset->ctx,
2701 isl_basic_map_compatible_range(bmap, bset), goto error);
2703 if (isl_basic_set_is_universe(bset)) {
2704 isl_basic_set_free(bset);
2705 return bmap;
2708 bmap = isl_basic_map_cow(bmap);
2709 if (!bmap)
2710 goto error;
2711 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2712 bset->n_div, bset->n_eq, bset->n_ineq);
2713 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2714 bmap = add_constraints(bmap, bmap_range, 0, 0);
2716 bmap = isl_basic_map_simplify(bmap);
2717 return isl_basic_map_finalize(bmap);
2718 error:
2719 isl_basic_map_free(bmap);
2720 isl_basic_set_free(bset);
2721 return NULL;
2724 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2726 int i;
2727 unsigned total;
2728 isl_int s;
2730 total = 1 + isl_basic_map_total_dim(bmap);
2731 if (total != vec->size)
2732 return -1;
2734 isl_int_init(s);
2736 for (i = 0; i < bmap->n_eq; ++i) {
2737 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2738 if (!isl_int_is_zero(s)) {
2739 isl_int_clear(s);
2740 return 0;
2744 for (i = 0; i < bmap->n_ineq; ++i) {
2745 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2746 if (isl_int_is_neg(s)) {
2747 isl_int_clear(s);
2748 return 0;
2752 isl_int_clear(s);
2754 return 1;
2757 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2759 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2762 struct isl_basic_map *isl_basic_map_intersect(
2763 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2765 struct isl_vec *sample = NULL;
2767 if (!bmap1 || !bmap2)
2768 goto error;
2770 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2771 bmap2->dim, isl_dim_param), goto error);
2772 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2773 isl_space_dim(bmap1->dim, isl_dim_param) &&
2774 isl_space_dim(bmap2->dim, isl_dim_all) !=
2775 isl_space_dim(bmap2->dim, isl_dim_param))
2776 return isl_basic_map_intersect(bmap2, bmap1);
2778 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2779 isl_space_dim(bmap2->dim, isl_dim_param))
2780 isl_assert(bmap1->ctx,
2781 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2783 if (bmap1->sample &&
2784 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2785 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2786 sample = isl_vec_copy(bmap1->sample);
2787 else if (bmap2->sample &&
2788 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2789 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2790 sample = isl_vec_copy(bmap2->sample);
2792 bmap1 = isl_basic_map_cow(bmap1);
2793 if (!bmap1)
2794 goto error;
2795 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2796 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2797 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2799 if (!bmap1)
2800 isl_vec_free(sample);
2801 else if (sample) {
2802 isl_vec_free(bmap1->sample);
2803 bmap1->sample = sample;
2806 bmap1 = isl_basic_map_simplify(bmap1);
2807 return isl_basic_map_finalize(bmap1);
2808 error:
2809 if (sample)
2810 isl_vec_free(sample);
2811 isl_basic_map_free(bmap1);
2812 isl_basic_map_free(bmap2);
2813 return NULL;
2816 struct isl_basic_set *isl_basic_set_intersect(
2817 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2819 return (struct isl_basic_set *)
2820 isl_basic_map_intersect(
2821 (struct isl_basic_map *)bset1,
2822 (struct isl_basic_map *)bset2);
2825 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2826 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2828 return isl_basic_set_intersect(bset1, bset2);
2831 /* Special case of isl_map_intersect, where both map1 and map2
2832 * are convex, without any divs and such that either map1 or map2
2833 * contains a single constraint. This constraint is then simply
2834 * added to the other map.
2836 static __isl_give isl_map *map_intersect_add_constraint(
2837 __isl_take isl_map *map1, __isl_take isl_map *map2)
2839 isl_assert(map1->ctx, map1->n == 1, goto error);
2840 isl_assert(map2->ctx, map1->n == 1, goto error);
2841 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2842 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2844 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2845 return isl_map_intersect(map2, map1);
2847 isl_assert(map2->ctx,
2848 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2850 map1 = isl_map_cow(map1);
2851 if (!map1)
2852 goto error;
2853 if (isl_map_plain_is_empty(map1)) {
2854 isl_map_free(map2);
2855 return map1;
2857 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2858 if (map2->p[0]->n_eq == 1)
2859 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2860 else
2861 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2862 map2->p[0]->ineq[0]);
2864 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2865 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2866 if (!map1->p[0])
2867 goto error;
2869 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2870 isl_basic_map_free(map1->p[0]);
2871 map1->n = 0;
2874 isl_map_free(map2);
2876 return map1;
2877 error:
2878 isl_map_free(map1);
2879 isl_map_free(map2);
2880 return NULL;
2883 /* map2 may be either a parameter domain or a map living in the same
2884 * space as map1.
2886 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2887 __isl_take isl_map *map2)
2889 unsigned flags = 0;
2890 isl_map *result;
2891 int i, j;
2893 if (!map1 || !map2)
2894 goto error;
2896 if ((isl_map_plain_is_empty(map1) ||
2897 isl_map_plain_is_universe(map2)) &&
2898 isl_space_is_equal(map1->dim, map2->dim)) {
2899 isl_map_free(map2);
2900 return map1;
2902 if ((isl_map_plain_is_empty(map2) ||
2903 isl_map_plain_is_universe(map1)) &&
2904 isl_space_is_equal(map1->dim, map2->dim)) {
2905 isl_map_free(map1);
2906 return map2;
2909 if (map1->n == 1 && map2->n == 1 &&
2910 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2911 isl_space_is_equal(map1->dim, map2->dim) &&
2912 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2913 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2914 return map_intersect_add_constraint(map1, map2);
2916 if (isl_space_dim(map2->dim, isl_dim_all) !=
2917 isl_space_dim(map2->dim, isl_dim_param))
2918 isl_assert(map1->ctx,
2919 isl_space_is_equal(map1->dim, map2->dim), goto error);
2921 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2922 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2923 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2925 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2926 map1->n * map2->n, flags);
2927 if (!result)
2928 goto error;
2929 for (i = 0; i < map1->n; ++i)
2930 for (j = 0; j < map2->n; ++j) {
2931 struct isl_basic_map *part;
2932 part = isl_basic_map_intersect(
2933 isl_basic_map_copy(map1->p[i]),
2934 isl_basic_map_copy(map2->p[j]));
2935 if (isl_basic_map_is_empty(part) < 0)
2936 part = isl_basic_map_free(part);
2937 result = isl_map_add_basic_map(result, part);
2938 if (!result)
2939 goto error;
2941 isl_map_free(map1);
2942 isl_map_free(map2);
2943 return result;
2944 error:
2945 isl_map_free(map1);
2946 isl_map_free(map2);
2947 return NULL;
2950 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2951 __isl_take isl_map *map2)
2953 if (!map1 || !map2)
2954 goto error;
2955 if (!isl_space_is_equal(map1->dim, map2->dim))
2956 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2957 "spaces don't match", goto error);
2958 return map_intersect_internal(map1, map2);
2959 error:
2960 isl_map_free(map1);
2961 isl_map_free(map2);
2962 return NULL;
2965 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2966 __isl_take isl_map *map2)
2968 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2971 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2973 return (struct isl_set *)
2974 isl_map_intersect((struct isl_map *)set1,
2975 (struct isl_map *)set2);
2978 /* map_intersect_internal accepts intersections
2979 * with parameter domains, so we can just call that function.
2981 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2982 __isl_take isl_set *params)
2984 return map_intersect_internal(map, params);
2987 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2988 __isl_take isl_map *map2)
2990 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2993 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2994 __isl_take isl_set *params)
2996 return isl_map_intersect_params(set, params);
2999 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3001 isl_space *dim;
3002 struct isl_basic_set *bset;
3003 unsigned in;
3005 if (!bmap)
3006 return NULL;
3007 bmap = isl_basic_map_cow(bmap);
3008 if (!bmap)
3009 return NULL;
3010 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3011 in = isl_basic_map_n_in(bmap);
3012 bset = isl_basic_set_from_basic_map(bmap);
3013 bset = isl_basic_set_swap_vars(bset, in);
3014 return isl_basic_map_from_basic_set(bset, dim);
3017 static __isl_give isl_basic_map *basic_map_space_reset(
3018 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3020 isl_space *space;
3022 if (!bmap)
3023 return NULL;
3024 if (!isl_space_is_named_or_nested(bmap->dim, type))
3025 return bmap;
3027 space = isl_basic_map_get_space(bmap);
3028 space = isl_space_reset(space, type);
3029 bmap = isl_basic_map_reset_space(bmap, space);
3030 return bmap;
3033 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3034 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3035 unsigned pos, unsigned n)
3037 isl_space *res_dim;
3038 struct isl_basic_map *res;
3039 struct isl_dim_map *dim_map;
3040 unsigned total, off;
3041 enum isl_dim_type t;
3043 if (n == 0)
3044 return basic_map_space_reset(bmap, type);
3046 if (!bmap)
3047 return NULL;
3049 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3051 total = isl_basic_map_total_dim(bmap) + n;
3052 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3053 off = 0;
3054 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3055 if (t != type) {
3056 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3057 } else {
3058 unsigned size = isl_basic_map_dim(bmap, t);
3059 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3060 0, pos, off);
3061 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3062 pos, size - pos, off + pos + n);
3064 off += isl_space_dim(res_dim, t);
3066 isl_dim_map_div(dim_map, bmap, off);
3068 res = isl_basic_map_alloc_space(res_dim,
3069 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3070 if (isl_basic_map_is_rational(bmap))
3071 res = isl_basic_map_set_rational(res);
3072 if (isl_basic_map_plain_is_empty(bmap)) {
3073 isl_basic_map_free(bmap);
3074 free(dim_map);
3075 return isl_basic_map_set_to_empty(res);
3077 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3078 return isl_basic_map_finalize(res);
3081 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3082 __isl_take isl_basic_set *bset,
3083 enum isl_dim_type type, unsigned pos, unsigned n)
3085 return isl_basic_map_insert_dims(bset, type, pos, n);
3088 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3089 enum isl_dim_type type, unsigned n)
3091 if (!bmap)
3092 return NULL;
3093 return isl_basic_map_insert_dims(bmap, type,
3094 isl_basic_map_dim(bmap, type), n);
3097 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3098 enum isl_dim_type type, unsigned n)
3100 if (!bset)
3101 return NULL;
3102 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3103 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3104 error:
3105 isl_basic_set_free(bset);
3106 return NULL;
3109 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3110 enum isl_dim_type type)
3112 isl_space *space;
3114 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3115 return map;
3117 space = isl_map_get_space(map);
3118 space = isl_space_reset(space, type);
3119 map = isl_map_reset_space(map, space);
3120 return map;
3123 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3124 enum isl_dim_type type, unsigned pos, unsigned n)
3126 int i;
3128 if (n == 0)
3129 return map_space_reset(map, type);
3131 map = isl_map_cow(map);
3132 if (!map)
3133 return NULL;
3135 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3136 if (!map->dim)
3137 goto error;
3139 for (i = 0; i < map->n; ++i) {
3140 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3141 if (!map->p[i])
3142 goto error;
3145 return map;
3146 error:
3147 isl_map_free(map);
3148 return NULL;
3151 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3152 enum isl_dim_type type, unsigned pos, unsigned n)
3154 return isl_map_insert_dims(set, type, pos, n);
3157 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3158 enum isl_dim_type type, unsigned n)
3160 if (!map)
3161 return NULL;
3162 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3165 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3166 enum isl_dim_type type, unsigned n)
3168 if (!set)
3169 return NULL;
3170 isl_assert(set->ctx, type != isl_dim_in, goto error);
3171 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3172 error:
3173 isl_set_free(set);
3174 return NULL;
3177 __isl_give isl_basic_map *isl_basic_map_move_dims(
3178 __isl_take isl_basic_map *bmap,
3179 enum isl_dim_type dst_type, unsigned dst_pos,
3180 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3182 struct isl_dim_map *dim_map;
3183 struct isl_basic_map *res;
3184 enum isl_dim_type t;
3185 unsigned total, off;
3187 if (!bmap)
3188 return NULL;
3189 if (n == 0)
3190 return bmap;
3192 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3193 goto error);
3195 if (dst_type == src_type && dst_pos == src_pos)
3196 return bmap;
3198 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3200 if (pos(bmap->dim, dst_type) + dst_pos ==
3201 pos(bmap->dim, src_type) + src_pos +
3202 ((src_type < dst_type) ? n : 0)) {
3203 bmap = isl_basic_map_cow(bmap);
3204 if (!bmap)
3205 return NULL;
3207 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3208 src_type, src_pos, n);
3209 if (!bmap->dim)
3210 goto error;
3212 bmap = isl_basic_map_finalize(bmap);
3214 return bmap;
3217 total = isl_basic_map_total_dim(bmap);
3218 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3220 off = 0;
3221 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3222 unsigned size = isl_space_dim(bmap->dim, t);
3223 if (t == dst_type) {
3224 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3225 0, dst_pos, off);
3226 off += dst_pos;
3227 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3228 src_pos, n, off);
3229 off += n;
3230 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3231 dst_pos, size - dst_pos, off);
3232 off += size - dst_pos;
3233 } else if (t == src_type) {
3234 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3235 0, src_pos, off);
3236 off += src_pos;
3237 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3238 src_pos + n, size - src_pos - n, off);
3239 off += size - src_pos - n;
3240 } else {
3241 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3242 off += size;
3245 isl_dim_map_div(dim_map, bmap, off);
3247 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3248 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3249 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3250 if (!bmap)
3251 goto error;
3253 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3254 src_type, src_pos, n);
3255 if (!bmap->dim)
3256 goto error;
3258 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3259 bmap = isl_basic_map_gauss(bmap, NULL);
3260 bmap = isl_basic_map_finalize(bmap);
3262 return bmap;
3263 error:
3264 isl_basic_map_free(bmap);
3265 return NULL;
3268 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3269 enum isl_dim_type dst_type, unsigned dst_pos,
3270 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3272 return (isl_basic_set *)isl_basic_map_move_dims(
3273 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3276 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3277 enum isl_dim_type dst_type, unsigned dst_pos,
3278 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3280 if (!set)
3281 return NULL;
3282 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3283 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3284 src_type, src_pos, n);
3285 error:
3286 isl_set_free(set);
3287 return NULL;
3290 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3291 enum isl_dim_type dst_type, unsigned dst_pos,
3292 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3294 int i;
3296 if (!map)
3297 return NULL;
3298 if (n == 0)
3299 return map;
3301 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3302 goto error);
3304 if (dst_type == src_type && dst_pos == src_pos)
3305 return map;
3307 isl_assert(map->ctx, dst_type != src_type, goto error);
3309 map = isl_map_cow(map);
3310 if (!map)
3311 return NULL;
3313 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3314 if (!map->dim)
3315 goto error;
3317 for (i = 0; i < map->n; ++i) {
3318 map->p[i] = isl_basic_map_move_dims(map->p[i],
3319 dst_type, dst_pos,
3320 src_type, src_pos, n);
3321 if (!map->p[i])
3322 goto error;
3325 return map;
3326 error:
3327 isl_map_free(map);
3328 return NULL;
3331 /* Move the specified dimensions to the last columns right before
3332 * the divs. Don't change the dimension specification of bmap.
3333 * That's the responsibility of the caller.
3335 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3336 enum isl_dim_type type, unsigned first, unsigned n)
3338 struct isl_dim_map *dim_map;
3339 struct isl_basic_map *res;
3340 enum isl_dim_type t;
3341 unsigned total, off;
3343 if (!bmap)
3344 return NULL;
3345 if (pos(bmap->dim, type) + first + n ==
3346 1 + isl_space_dim(bmap->dim, isl_dim_all))
3347 return bmap;
3349 total = isl_basic_map_total_dim(bmap);
3350 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3352 off = 0;
3353 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3354 unsigned size = isl_space_dim(bmap->dim, t);
3355 if (t == type) {
3356 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3357 0, first, off);
3358 off += first;
3359 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3360 first, n, total - bmap->n_div - n);
3361 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3362 first + n, size - (first + n), off);
3363 off += size - (first + n);
3364 } else {
3365 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3366 off += size;
3369 isl_dim_map_div(dim_map, bmap, off + n);
3371 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3372 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3373 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3374 return res;
3377 /* Insert "n" rows in the divs of "bmap".
3379 * The number of columns is not changed, which means that the last
3380 * dimensions of "bmap" are being reintepreted as the new divs.
3381 * The space of "bmap" is not adjusted, however, which means
3382 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3383 * from the space of "bmap" is the responsibility of the caller.
3385 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3386 int n)
3388 int i;
3389 size_t row_size;
3390 isl_int **new_div;
3391 isl_int *old;
3393 bmap = isl_basic_map_cow(bmap);
3394 if (!bmap)
3395 return NULL;
3397 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3398 old = bmap->block2.data;
3399 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3400 (bmap->extra + n) * (1 + row_size));
3401 if (!bmap->block2.data)
3402 return isl_basic_map_free(bmap);
3403 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3404 if (!new_div)
3405 return isl_basic_map_free(bmap);
3406 for (i = 0; i < n; ++i) {
3407 new_div[i] = bmap->block2.data +
3408 (bmap->extra + i) * (1 + row_size);
3409 isl_seq_clr(new_div[i], 1 + row_size);
3411 for (i = 0; i < bmap->extra; ++i)
3412 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3413 free(bmap->div);
3414 bmap->div = new_div;
3415 bmap->n_div += n;
3416 bmap->extra += n;
3418 return bmap;
3421 /* Turn the n dimensions of type type, starting at first
3422 * into existentially quantified variables.
3424 __isl_give isl_basic_map *isl_basic_map_project_out(
3425 __isl_take isl_basic_map *bmap,
3426 enum isl_dim_type type, unsigned first, unsigned n)
3428 if (n == 0)
3429 return basic_map_space_reset(bmap, type);
3431 if (!bmap)
3432 return NULL;
3434 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3435 return isl_basic_map_remove_dims(bmap, type, first, n);
3437 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3438 goto error);
3440 bmap = move_last(bmap, type, first, n);
3441 bmap = isl_basic_map_cow(bmap);
3442 bmap = insert_div_rows(bmap, n);
3443 if (!bmap)
3444 return NULL;
3446 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3447 if (!bmap->dim)
3448 goto error;
3449 bmap = isl_basic_map_simplify(bmap);
3450 bmap = isl_basic_map_drop_redundant_divs(bmap);
3451 return isl_basic_map_finalize(bmap);
3452 error:
3453 isl_basic_map_free(bmap);
3454 return NULL;
3457 /* Turn the n dimensions of type type, starting at first
3458 * into existentially quantified variables.
3460 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3461 enum isl_dim_type type, unsigned first, unsigned n)
3463 return (isl_basic_set *)isl_basic_map_project_out(
3464 (isl_basic_map *)bset, type, first, n);
3467 /* Turn the n dimensions of type type, starting at first
3468 * into existentially quantified variables.
3470 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3471 enum isl_dim_type type, unsigned first, unsigned n)
3473 int i;
3475 if (!map)
3476 return NULL;
3478 if (n == 0)
3479 return map_space_reset(map, type);
3481 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3483 map = isl_map_cow(map);
3484 if (!map)
3485 return NULL;
3487 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3488 if (!map->dim)
3489 goto error;
3491 for (i = 0; i < map->n; ++i) {
3492 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3493 if (!map->p[i])
3494 goto error;
3497 return map;
3498 error:
3499 isl_map_free(map);
3500 return NULL;
3503 /* Turn the n dimensions of type type, starting at first
3504 * into existentially quantified variables.
3506 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3507 enum isl_dim_type type, unsigned first, unsigned n)
3509 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3512 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3514 int i, j;
3516 for (i = 0; i < n; ++i) {
3517 j = isl_basic_map_alloc_div(bmap);
3518 if (j < 0)
3519 goto error;
3520 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3522 return bmap;
3523 error:
3524 isl_basic_map_free(bmap);
3525 return NULL;
3528 struct isl_basic_map *isl_basic_map_apply_range(
3529 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3531 isl_space *dim_result = NULL;
3532 struct isl_basic_map *bmap;
3533 unsigned n_in, n_out, n, nparam, total, pos;
3534 struct isl_dim_map *dim_map1, *dim_map2;
3536 if (!bmap1 || !bmap2)
3537 goto error;
3539 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3540 isl_space_copy(bmap2->dim));
3542 n_in = isl_basic_map_n_in(bmap1);
3543 n_out = isl_basic_map_n_out(bmap2);
3544 n = isl_basic_map_n_out(bmap1);
3545 nparam = isl_basic_map_n_param(bmap1);
3547 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3548 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3549 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3550 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3551 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3552 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3553 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3554 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3555 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3556 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3557 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3559 bmap = isl_basic_map_alloc_space(dim_result,
3560 bmap1->n_div + bmap2->n_div + n,
3561 bmap1->n_eq + bmap2->n_eq,
3562 bmap1->n_ineq + bmap2->n_ineq);
3563 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3564 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3565 bmap = add_divs(bmap, n);
3566 bmap = isl_basic_map_simplify(bmap);
3567 bmap = isl_basic_map_drop_redundant_divs(bmap);
3568 return isl_basic_map_finalize(bmap);
3569 error:
3570 isl_basic_map_free(bmap1);
3571 isl_basic_map_free(bmap2);
3572 return NULL;
3575 struct isl_basic_set *isl_basic_set_apply(
3576 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3578 if (!bset || !bmap)
3579 goto error;
3581 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3582 goto error);
3584 return (struct isl_basic_set *)
3585 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3586 error:
3587 isl_basic_set_free(bset);
3588 isl_basic_map_free(bmap);
3589 return NULL;
3592 struct isl_basic_map *isl_basic_map_apply_domain(
3593 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3595 if (!bmap1 || !bmap2)
3596 goto error;
3598 isl_assert(bmap1->ctx,
3599 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3600 isl_assert(bmap1->ctx,
3601 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3602 goto error);
3604 bmap1 = isl_basic_map_reverse(bmap1);
3605 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3606 return isl_basic_map_reverse(bmap1);
3607 error:
3608 isl_basic_map_free(bmap1);
3609 isl_basic_map_free(bmap2);
3610 return NULL;
3613 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3614 * A \cap B -> f(A) + f(B)
3616 struct isl_basic_map *isl_basic_map_sum(
3617 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3619 unsigned n_in, n_out, nparam, total, pos;
3620 struct isl_basic_map *bmap = NULL;
3621 struct isl_dim_map *dim_map1, *dim_map2;
3622 int i;
3624 if (!bmap1 || !bmap2)
3625 goto error;
3627 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3628 goto error);
3630 nparam = isl_basic_map_n_param(bmap1);
3631 n_in = isl_basic_map_n_in(bmap1);
3632 n_out = isl_basic_map_n_out(bmap1);
3634 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3635 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3636 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3637 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3638 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3639 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3640 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3641 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3642 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3643 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3644 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3646 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3647 bmap1->n_div + bmap2->n_div + 2 * n_out,
3648 bmap1->n_eq + bmap2->n_eq + n_out,
3649 bmap1->n_ineq + bmap2->n_ineq);
3650 for (i = 0; i < n_out; ++i) {
3651 int j = isl_basic_map_alloc_equality(bmap);
3652 if (j < 0)
3653 goto error;
3654 isl_seq_clr(bmap->eq[j], 1+total);
3655 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3656 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3657 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3659 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3660 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3661 bmap = add_divs(bmap, 2 * n_out);
3663 bmap = isl_basic_map_simplify(bmap);
3664 return isl_basic_map_finalize(bmap);
3665 error:
3666 isl_basic_map_free(bmap);
3667 isl_basic_map_free(bmap1);
3668 isl_basic_map_free(bmap2);
3669 return NULL;
3672 /* Given two maps A -> f(A) and B -> g(B), construct a map
3673 * A \cap B -> f(A) + f(B)
3675 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3677 struct isl_map *result;
3678 int i, j;
3680 if (!map1 || !map2)
3681 goto error;
3683 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3685 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3686 map1->n * map2->n, 0);
3687 if (!result)
3688 goto error;
3689 for (i = 0; i < map1->n; ++i)
3690 for (j = 0; j < map2->n; ++j) {
3691 struct isl_basic_map *part;
3692 part = isl_basic_map_sum(
3693 isl_basic_map_copy(map1->p[i]),
3694 isl_basic_map_copy(map2->p[j]));
3695 if (isl_basic_map_is_empty(part))
3696 isl_basic_map_free(part);
3697 else
3698 result = isl_map_add_basic_map(result, part);
3699 if (!result)
3700 goto error;
3702 isl_map_free(map1);
3703 isl_map_free(map2);
3704 return result;
3705 error:
3706 isl_map_free(map1);
3707 isl_map_free(map2);
3708 return NULL;
3711 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3712 __isl_take isl_set *set2)
3714 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3717 /* Given a basic map A -> f(A), construct A -> -f(A).
3719 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3721 int i, j;
3722 unsigned off, n;
3724 bmap = isl_basic_map_cow(bmap);
3725 if (!bmap)
3726 return NULL;
3728 n = isl_basic_map_dim(bmap, isl_dim_out);
3729 off = isl_basic_map_offset(bmap, isl_dim_out);
3730 for (i = 0; i < bmap->n_eq; ++i)
3731 for (j = 0; j < n; ++j)
3732 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3733 for (i = 0; i < bmap->n_ineq; ++i)
3734 for (j = 0; j < n; ++j)
3735 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3736 for (i = 0; i < bmap->n_div; ++i)
3737 for (j = 0; j < n; ++j)
3738 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3739 bmap = isl_basic_map_gauss(bmap, NULL);
3740 return isl_basic_map_finalize(bmap);
3743 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3745 return isl_basic_map_neg(bset);
3748 /* Given a map A -> f(A), construct A -> -f(A).
3750 struct isl_map *isl_map_neg(struct isl_map *map)
3752 int i;
3754 map = isl_map_cow(map);
3755 if (!map)
3756 return NULL;
3758 for (i = 0; i < map->n; ++i) {
3759 map->p[i] = isl_basic_map_neg(map->p[i]);
3760 if (!map->p[i])
3761 goto error;
3764 return map;
3765 error:
3766 isl_map_free(map);
3767 return NULL;
3770 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3772 return (isl_set *)isl_map_neg((isl_map *)set);
3775 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3776 * A -> floor(f(A)/d).
3778 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3779 isl_int d)
3781 unsigned n_in, n_out, nparam, total, pos;
3782 struct isl_basic_map *result = NULL;
3783 struct isl_dim_map *dim_map;
3784 int i;
3786 if (!bmap)
3787 return NULL;
3789 nparam = isl_basic_map_n_param(bmap);
3790 n_in = isl_basic_map_n_in(bmap);
3791 n_out = isl_basic_map_n_out(bmap);
3793 total = nparam + n_in + n_out + bmap->n_div + n_out;
3794 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3795 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3796 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3797 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3798 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3800 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3801 bmap->n_div + n_out,
3802 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3803 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3804 result = add_divs(result, n_out);
3805 for (i = 0; i < n_out; ++i) {
3806 int j;
3807 j = isl_basic_map_alloc_inequality(result);
3808 if (j < 0)
3809 goto error;
3810 isl_seq_clr(result->ineq[j], 1+total);
3811 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3812 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3813 j = isl_basic_map_alloc_inequality(result);
3814 if (j < 0)
3815 goto error;
3816 isl_seq_clr(result->ineq[j], 1+total);
3817 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3818 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3819 isl_int_sub_ui(result->ineq[j][0], d, 1);
3822 result = isl_basic_map_simplify(result);
3823 return isl_basic_map_finalize(result);
3824 error:
3825 isl_basic_map_free(result);
3826 return NULL;
3829 /* Given a map A -> f(A) and an integer d, construct a map
3830 * A -> floor(f(A)/d).
3832 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3834 int i;
3836 map = isl_map_cow(map);
3837 if (!map)
3838 return NULL;
3840 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3841 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3842 for (i = 0; i < map->n; ++i) {
3843 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3844 if (!map->p[i])
3845 goto error;
3848 return map;
3849 error:
3850 isl_map_free(map);
3851 return NULL;
3854 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3856 int i;
3857 unsigned nparam;
3858 unsigned n_in;
3860 i = isl_basic_map_alloc_equality(bmap);
3861 if (i < 0)
3862 goto error;
3863 nparam = isl_basic_map_n_param(bmap);
3864 n_in = isl_basic_map_n_in(bmap);
3865 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3866 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3867 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3868 return isl_basic_map_finalize(bmap);
3869 error:
3870 isl_basic_map_free(bmap);
3871 return NULL;
3874 /* Add a constraints to "bmap" expressing i_pos < o_pos
3876 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3878 int i;
3879 unsigned nparam;
3880 unsigned n_in;
3882 i = isl_basic_map_alloc_inequality(bmap);
3883 if (i < 0)
3884 goto error;
3885 nparam = isl_basic_map_n_param(bmap);
3886 n_in = isl_basic_map_n_in(bmap);
3887 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3888 isl_int_set_si(bmap->ineq[i][0], -1);
3889 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3890 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3891 return isl_basic_map_finalize(bmap);
3892 error:
3893 isl_basic_map_free(bmap);
3894 return NULL;
3897 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3899 static __isl_give isl_basic_map *var_less_or_equal(
3900 __isl_take isl_basic_map *bmap, unsigned pos)
3902 int i;
3903 unsigned nparam;
3904 unsigned n_in;
3906 i = isl_basic_map_alloc_inequality(bmap);
3907 if (i < 0)
3908 goto error;
3909 nparam = isl_basic_map_n_param(bmap);
3910 n_in = isl_basic_map_n_in(bmap);
3911 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3912 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3913 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3914 return isl_basic_map_finalize(bmap);
3915 error:
3916 isl_basic_map_free(bmap);
3917 return NULL;
3920 /* Add a constraints to "bmap" expressing i_pos > o_pos
3922 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3924 int i;
3925 unsigned nparam;
3926 unsigned n_in;
3928 i = isl_basic_map_alloc_inequality(bmap);
3929 if (i < 0)
3930 goto error;
3931 nparam = isl_basic_map_n_param(bmap);
3932 n_in = isl_basic_map_n_in(bmap);
3933 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3934 isl_int_set_si(bmap->ineq[i][0], -1);
3935 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3936 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3937 return isl_basic_map_finalize(bmap);
3938 error:
3939 isl_basic_map_free(bmap);
3940 return NULL;
3943 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3945 static __isl_give isl_basic_map *var_more_or_equal(
3946 __isl_take isl_basic_map *bmap, unsigned pos)
3948 int i;
3949 unsigned nparam;
3950 unsigned n_in;
3952 i = isl_basic_map_alloc_inequality(bmap);
3953 if (i < 0)
3954 goto error;
3955 nparam = isl_basic_map_n_param(bmap);
3956 n_in = isl_basic_map_n_in(bmap);
3957 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3958 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3959 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3960 return isl_basic_map_finalize(bmap);
3961 error:
3962 isl_basic_map_free(bmap);
3963 return NULL;
3966 __isl_give isl_basic_map *isl_basic_map_equal(
3967 __isl_take isl_space *dim, unsigned n_equal)
3969 int i;
3970 struct isl_basic_map *bmap;
3971 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3972 if (!bmap)
3973 return NULL;
3974 for (i = 0; i < n_equal && bmap; ++i)
3975 bmap = var_equal(bmap, i);
3976 return isl_basic_map_finalize(bmap);
3979 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3981 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3982 unsigned pos)
3984 int i;
3985 struct isl_basic_map *bmap;
3986 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3987 if (!bmap)
3988 return NULL;
3989 for (i = 0; i < pos && bmap; ++i)
3990 bmap = var_equal(bmap, i);
3991 if (bmap)
3992 bmap = var_less(bmap, pos);
3993 return isl_basic_map_finalize(bmap);
3996 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3998 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3999 __isl_take isl_space *dim, unsigned pos)
4001 int i;
4002 isl_basic_map *bmap;
4004 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4005 for (i = 0; i < pos; ++i)
4006 bmap = var_equal(bmap, i);
4007 bmap = var_less_or_equal(bmap, pos);
4008 return isl_basic_map_finalize(bmap);
4011 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4013 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4014 unsigned pos)
4016 int i;
4017 struct isl_basic_map *bmap;
4018 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4019 if (!bmap)
4020 return NULL;
4021 for (i = 0; i < pos && bmap; ++i)
4022 bmap = var_equal(bmap, i);
4023 if (bmap)
4024 bmap = var_more(bmap, pos);
4025 return isl_basic_map_finalize(bmap);
4028 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4030 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4031 __isl_take isl_space *dim, unsigned pos)
4033 int i;
4034 isl_basic_map *bmap;
4036 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4037 for (i = 0; i < pos; ++i)
4038 bmap = var_equal(bmap, i);
4039 bmap = var_more_or_equal(bmap, pos);
4040 return isl_basic_map_finalize(bmap);
4043 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4044 unsigned n, int equal)
4046 struct isl_map *map;
4047 int i;
4049 if (n == 0 && equal)
4050 return isl_map_universe(dims);
4052 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4054 for (i = 0; i + 1 < n; ++i)
4055 map = isl_map_add_basic_map(map,
4056 isl_basic_map_less_at(isl_space_copy(dims), i));
4057 if (n > 0) {
4058 if (equal)
4059 map = isl_map_add_basic_map(map,
4060 isl_basic_map_less_or_equal_at(dims, n - 1));
4061 else
4062 map = isl_map_add_basic_map(map,
4063 isl_basic_map_less_at(dims, n - 1));
4064 } else
4065 isl_space_free(dims);
4067 return map;
4070 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4072 if (!dims)
4073 return NULL;
4074 return map_lex_lte_first(dims, dims->n_out, equal);
4077 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4079 return map_lex_lte_first(dim, n, 0);
4082 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4084 return map_lex_lte_first(dim, n, 1);
4087 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4089 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4092 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4094 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4097 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4098 unsigned n, int equal)
4100 struct isl_map *map;
4101 int i;
4103 if (n == 0 && equal)
4104 return isl_map_universe(dims);
4106 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4108 for (i = 0; i + 1 < n; ++i)
4109 map = isl_map_add_basic_map(map,
4110 isl_basic_map_more_at(isl_space_copy(dims), i));
4111 if (n > 0) {
4112 if (equal)
4113 map = isl_map_add_basic_map(map,
4114 isl_basic_map_more_or_equal_at(dims, n - 1));
4115 else
4116 map = isl_map_add_basic_map(map,
4117 isl_basic_map_more_at(dims, n - 1));
4118 } else
4119 isl_space_free(dims);
4121 return map;
4124 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4126 if (!dims)
4127 return NULL;
4128 return map_lex_gte_first(dims, dims->n_out, equal);
4131 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4133 return map_lex_gte_first(dim, n, 0);
4136 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4138 return map_lex_gte_first(dim, n, 1);
4141 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4143 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4146 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4148 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4151 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4152 __isl_take isl_set *set2)
4154 isl_map *map;
4155 map = isl_map_lex_le(isl_set_get_space(set1));
4156 map = isl_map_intersect_domain(map, set1);
4157 map = isl_map_intersect_range(map, set2);
4158 return map;
4161 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4162 __isl_take isl_set *set2)
4164 isl_map *map;
4165 map = isl_map_lex_lt(isl_set_get_space(set1));
4166 map = isl_map_intersect_domain(map, set1);
4167 map = isl_map_intersect_range(map, set2);
4168 return map;
4171 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4172 __isl_take isl_set *set2)
4174 isl_map *map;
4175 map = isl_map_lex_ge(isl_set_get_space(set1));
4176 map = isl_map_intersect_domain(map, set1);
4177 map = isl_map_intersect_range(map, set2);
4178 return map;
4181 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4182 __isl_take isl_set *set2)
4184 isl_map *map;
4185 map = isl_map_lex_gt(isl_set_get_space(set1));
4186 map = isl_map_intersect_domain(map, set1);
4187 map = isl_map_intersect_range(map, set2);
4188 return map;
4191 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4192 __isl_take isl_map *map2)
4194 isl_map *map;
4195 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4196 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4197 map = isl_map_apply_range(map, isl_map_reverse(map2));
4198 return map;
4201 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4202 __isl_take isl_map *map2)
4204 isl_map *map;
4205 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4206 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4207 map = isl_map_apply_range(map, isl_map_reverse(map2));
4208 return map;
4211 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4212 __isl_take isl_map *map2)
4214 isl_map *map;
4215 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4216 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4217 map = isl_map_apply_range(map, isl_map_reverse(map2));
4218 return map;
4221 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4222 __isl_take isl_map *map2)
4224 isl_map *map;
4225 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4226 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4227 map = isl_map_apply_range(map, isl_map_reverse(map2));
4228 return map;
4231 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4232 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4234 struct isl_basic_map *bmap;
4236 bset = isl_basic_set_cow(bset);
4237 if (!bset || !dim)
4238 goto error;
4240 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4241 isl_space_free(bset->dim);
4242 bmap = (struct isl_basic_map *) bset;
4243 bmap->dim = dim;
4244 return isl_basic_map_finalize(bmap);
4245 error:
4246 isl_basic_set_free(bset);
4247 isl_space_free(dim);
4248 return NULL;
4251 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4253 if (!bmap)
4254 goto error;
4255 if (bmap->dim->n_in == 0)
4256 return (struct isl_basic_set *)bmap;
4257 bmap = isl_basic_map_cow(bmap);
4258 if (!bmap)
4259 goto error;
4260 bmap->dim = isl_space_as_set_space(bmap->dim);
4261 if (!bmap->dim)
4262 goto error;
4263 bmap = isl_basic_map_finalize(bmap);
4264 return (struct isl_basic_set *)bmap;
4265 error:
4266 isl_basic_map_free(bmap);
4267 return NULL;
4270 /* For a div d = floor(f/m), add the constraints
4272 * f - m d >= 0
4273 * -(f-(n-1)) + m d >= 0
4275 * Note that the second constraint is the negation of
4277 * f - m d >= n
4279 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4280 unsigned pos, isl_int *div)
4282 int i, j;
4283 unsigned total = isl_basic_map_total_dim(bmap);
4285 i = isl_basic_map_alloc_inequality(bmap);
4286 if (i < 0)
4287 return -1;
4288 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4289 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4291 j = isl_basic_map_alloc_inequality(bmap);
4292 if (j < 0)
4293 return -1;
4294 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
4295 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
4296 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
4297 return j;
4300 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4301 unsigned pos, isl_int *div)
4303 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4304 pos, div);
4307 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4309 unsigned total = isl_basic_map_total_dim(bmap);
4310 unsigned div_pos = total - bmap->n_div + div;
4312 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4313 bmap->div[div]);
4316 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4318 return isl_basic_map_add_div_constraints(bset, div);
4321 struct isl_basic_set *isl_basic_map_underlying_set(
4322 struct isl_basic_map *bmap)
4324 if (!bmap)
4325 goto error;
4326 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4327 bmap->n_div == 0 &&
4328 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4329 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4330 return (struct isl_basic_set *)bmap;
4331 bmap = isl_basic_map_cow(bmap);
4332 if (!bmap)
4333 goto error;
4334 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4335 if (!bmap->dim)
4336 goto error;
4337 bmap->extra -= bmap->n_div;
4338 bmap->n_div = 0;
4339 bmap = isl_basic_map_finalize(bmap);
4340 return (struct isl_basic_set *)bmap;
4341 error:
4342 isl_basic_map_free(bmap);
4343 return NULL;
4346 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4347 __isl_take isl_basic_set *bset)
4349 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4352 struct isl_basic_map *isl_basic_map_overlying_set(
4353 struct isl_basic_set *bset, struct isl_basic_map *like)
4355 struct isl_basic_map *bmap;
4356 struct isl_ctx *ctx;
4357 unsigned total;
4358 int i;
4360 if (!bset || !like)
4361 goto error;
4362 ctx = bset->ctx;
4363 isl_assert(ctx, bset->n_div == 0, goto error);
4364 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4365 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4366 goto error);
4367 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4368 isl_basic_map_free(like);
4369 return (struct isl_basic_map *)bset;
4371 bset = isl_basic_set_cow(bset);
4372 if (!bset)
4373 goto error;
4374 total = bset->dim->n_out + bset->extra;
4375 bmap = (struct isl_basic_map *)bset;
4376 isl_space_free(bmap->dim);
4377 bmap->dim = isl_space_copy(like->dim);
4378 if (!bmap->dim)
4379 goto error;
4380 bmap->n_div = like->n_div;
4381 bmap->extra += like->n_div;
4382 if (bmap->extra) {
4383 unsigned ltotal;
4384 isl_int **div;
4385 ltotal = total - bmap->extra + like->extra;
4386 if (ltotal > total)
4387 ltotal = total;
4388 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4389 bmap->extra * (1 + 1 + total));
4390 if (isl_blk_is_error(bmap->block2))
4391 goto error;
4392 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4393 if (!div)
4394 goto error;
4395 bmap->div = div;
4396 for (i = 0; i < bmap->extra; ++i)
4397 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4398 for (i = 0; i < like->n_div; ++i) {
4399 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4400 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4402 bmap = isl_basic_map_extend_constraints(bmap,
4403 0, 2 * like->n_div);
4404 for (i = 0; i < like->n_div; ++i) {
4405 if (!bmap)
4406 break;
4407 if (isl_int_is_zero(bmap->div[i][0]))
4408 continue;
4409 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4410 bmap = isl_basic_map_free(bmap);
4413 isl_basic_map_free(like);
4414 bmap = isl_basic_map_simplify(bmap);
4415 bmap = isl_basic_map_finalize(bmap);
4416 return bmap;
4417 error:
4418 isl_basic_map_free(like);
4419 isl_basic_set_free(bset);
4420 return NULL;
4423 struct isl_basic_set *isl_basic_set_from_underlying_set(
4424 struct isl_basic_set *bset, struct isl_basic_set *like)
4426 return (struct isl_basic_set *)
4427 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4430 struct isl_set *isl_set_from_underlying_set(
4431 struct isl_set *set, struct isl_basic_set *like)
4433 int i;
4435 if (!set || !like)
4436 goto error;
4437 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4438 goto error);
4439 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4440 isl_basic_set_free(like);
4441 return set;
4443 set = isl_set_cow(set);
4444 if (!set)
4445 goto error;
4446 for (i = 0; i < set->n; ++i) {
4447 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4448 isl_basic_set_copy(like));
4449 if (!set->p[i])
4450 goto error;
4452 isl_space_free(set->dim);
4453 set->dim = isl_space_copy(like->dim);
4454 if (!set->dim)
4455 goto error;
4456 isl_basic_set_free(like);
4457 return set;
4458 error:
4459 isl_basic_set_free(like);
4460 isl_set_free(set);
4461 return NULL;
4464 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4466 int i;
4468 map = isl_map_cow(map);
4469 if (!map)
4470 return NULL;
4471 map->dim = isl_space_cow(map->dim);
4472 if (!map->dim)
4473 goto error;
4475 for (i = 1; i < map->n; ++i)
4476 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4477 goto error);
4478 for (i = 0; i < map->n; ++i) {
4479 map->p[i] = (struct isl_basic_map *)
4480 isl_basic_map_underlying_set(map->p[i]);
4481 if (!map->p[i])
4482 goto error;
4484 if (map->n == 0)
4485 map->dim = isl_space_underlying(map->dim, 0);
4486 else {
4487 isl_space_free(map->dim);
4488 map->dim = isl_space_copy(map->p[0]->dim);
4490 if (!map->dim)
4491 goto error;
4492 return (struct isl_set *)map;
4493 error:
4494 isl_map_free(map);
4495 return NULL;
4498 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4500 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4503 __isl_give isl_basic_map *isl_basic_map_reset_space(
4504 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4506 bmap = isl_basic_map_cow(bmap);
4507 if (!bmap || !dim)
4508 goto error;
4510 isl_space_free(bmap->dim);
4511 bmap->dim = dim;
4513 bmap = isl_basic_map_finalize(bmap);
4515 return bmap;
4516 error:
4517 isl_basic_map_free(bmap);
4518 isl_space_free(dim);
4519 return NULL;
4522 __isl_give isl_basic_set *isl_basic_set_reset_space(
4523 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4525 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4526 dim);
4529 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4530 __isl_take isl_space *dim)
4532 int i;
4534 map = isl_map_cow(map);
4535 if (!map || !dim)
4536 goto error;
4538 for (i = 0; i < map->n; ++i) {
4539 map->p[i] = isl_basic_map_reset_space(map->p[i],
4540 isl_space_copy(dim));
4541 if (!map->p[i])
4542 goto error;
4544 isl_space_free(map->dim);
4545 map->dim = dim;
4547 return map;
4548 error:
4549 isl_map_free(map);
4550 isl_space_free(dim);
4551 return NULL;
4554 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4555 __isl_take isl_space *dim)
4557 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4560 /* Compute the parameter domain of the given basic set.
4562 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4564 isl_space *space;
4565 unsigned n;
4567 if (isl_basic_set_is_params(bset))
4568 return bset;
4570 n = isl_basic_set_dim(bset, isl_dim_set);
4571 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4572 space = isl_basic_set_get_space(bset);
4573 space = isl_space_params(space);
4574 bset = isl_basic_set_reset_space(bset, space);
4575 return bset;
4578 /* Construct a zero-dimensional basic set with the given parameter domain.
4580 __isl_give isl_basic_set *isl_basic_set_from_params(
4581 __isl_take isl_basic_set *bset)
4583 isl_space *space;
4584 space = isl_basic_set_get_space(bset);
4585 space = isl_space_set_from_params(space);
4586 bset = isl_basic_set_reset_space(bset, space);
4587 return bset;
4590 /* Compute the parameter domain of the given set.
4592 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4594 isl_space *space;
4595 unsigned n;
4597 if (isl_set_is_params(set))
4598 return set;
4600 n = isl_set_dim(set, isl_dim_set);
4601 set = isl_set_project_out(set, isl_dim_set, 0, n);
4602 space = isl_set_get_space(set);
4603 space = isl_space_params(space);
4604 set = isl_set_reset_space(set, space);
4605 return set;
4608 /* Construct a zero-dimensional set with the given parameter domain.
4610 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4612 isl_space *space;
4613 space = isl_set_get_space(set);
4614 space = isl_space_set_from_params(space);
4615 set = isl_set_reset_space(set, space);
4616 return set;
4619 /* Compute the parameter domain of the given map.
4621 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4623 isl_space *space;
4624 unsigned n;
4626 n = isl_map_dim(map, isl_dim_in);
4627 map = isl_map_project_out(map, isl_dim_in, 0, n);
4628 n = isl_map_dim(map, isl_dim_out);
4629 map = isl_map_project_out(map, isl_dim_out, 0, n);
4630 space = isl_map_get_space(map);
4631 space = isl_space_params(space);
4632 map = isl_map_reset_space(map, space);
4633 return map;
4636 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4638 isl_space *dim;
4639 struct isl_basic_set *domain;
4640 unsigned n_in;
4641 unsigned n_out;
4643 if (!bmap)
4644 return NULL;
4645 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4647 n_in = isl_basic_map_n_in(bmap);
4648 n_out = isl_basic_map_n_out(bmap);
4649 domain = isl_basic_set_from_basic_map(bmap);
4650 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4652 domain = isl_basic_set_reset_space(domain, dim);
4654 return domain;
4657 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4659 if (!bmap)
4660 return -1;
4661 return isl_space_may_be_set(bmap->dim);
4664 /* Is this basic map actually a set?
4665 * Users should never call this function. Outside of isl,
4666 * the type should indicate whether something is a set or a map.
4668 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4670 if (!bmap)
4671 return -1;
4672 return isl_space_is_set(bmap->dim);
4675 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4677 if (!bmap)
4678 return NULL;
4679 if (isl_basic_map_is_set(bmap))
4680 return bmap;
4681 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4684 __isl_give isl_basic_map *isl_basic_map_domain_map(
4685 __isl_take isl_basic_map *bmap)
4687 int i, k;
4688 isl_space *dim;
4689 isl_basic_map *domain;
4690 int nparam, n_in, n_out;
4691 unsigned total;
4693 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4694 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4695 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4697 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4698 domain = isl_basic_map_universe(dim);
4700 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4701 bmap = isl_basic_map_apply_range(bmap, domain);
4702 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4704 total = isl_basic_map_total_dim(bmap);
4706 for (i = 0; i < n_in; ++i) {
4707 k = isl_basic_map_alloc_equality(bmap);
4708 if (k < 0)
4709 goto error;
4710 isl_seq_clr(bmap->eq[k], 1 + total);
4711 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4712 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4715 bmap = isl_basic_map_gauss(bmap, NULL);
4716 return isl_basic_map_finalize(bmap);
4717 error:
4718 isl_basic_map_free(bmap);
4719 return NULL;
4722 __isl_give isl_basic_map *isl_basic_map_range_map(
4723 __isl_take isl_basic_map *bmap)
4725 int i, k;
4726 isl_space *dim;
4727 isl_basic_map *range;
4728 int nparam, n_in, n_out;
4729 unsigned total;
4731 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4732 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4733 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4735 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4736 range = isl_basic_map_universe(dim);
4738 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4739 bmap = isl_basic_map_apply_range(bmap, range);
4740 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4742 total = isl_basic_map_total_dim(bmap);
4744 for (i = 0; i < n_out; ++i) {
4745 k = isl_basic_map_alloc_equality(bmap);
4746 if (k < 0)
4747 goto error;
4748 isl_seq_clr(bmap->eq[k], 1 + total);
4749 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4750 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4753 bmap = isl_basic_map_gauss(bmap, NULL);
4754 return isl_basic_map_finalize(bmap);
4755 error:
4756 isl_basic_map_free(bmap);
4757 return NULL;
4760 int isl_map_may_be_set(__isl_keep isl_map *map)
4762 if (!map)
4763 return -1;
4764 return isl_space_may_be_set(map->dim);
4767 /* Is this map actually a set?
4768 * Users should never call this function. Outside of isl,
4769 * the type should indicate whether something is a set or a map.
4771 int isl_map_is_set(__isl_keep isl_map *map)
4773 if (!map)
4774 return -1;
4775 return isl_space_is_set(map->dim);
4778 struct isl_set *isl_map_range(struct isl_map *map)
4780 int i;
4781 struct isl_set *set;
4783 if (!map)
4784 goto error;
4785 if (isl_map_is_set(map))
4786 return (isl_set *)map;
4788 map = isl_map_cow(map);
4789 if (!map)
4790 goto error;
4792 set = (struct isl_set *) map;
4793 set->dim = isl_space_range(set->dim);
4794 if (!set->dim)
4795 goto error;
4796 for (i = 0; i < map->n; ++i) {
4797 set->p[i] = isl_basic_map_range(map->p[i]);
4798 if (!set->p[i])
4799 goto error;
4801 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4802 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4803 return set;
4804 error:
4805 isl_map_free(map);
4806 return NULL;
4809 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4811 int i;
4812 isl_space *domain_dim;
4814 map = isl_map_cow(map);
4815 if (!map)
4816 return NULL;
4818 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4819 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4820 map->dim = isl_space_join(map->dim, domain_dim);
4821 if (!map->dim)
4822 goto error;
4823 for (i = 0; i < map->n; ++i) {
4824 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4825 if (!map->p[i])
4826 goto error;
4828 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4829 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4830 return map;
4831 error:
4832 isl_map_free(map);
4833 return NULL;
4836 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4838 int i;
4839 isl_space *range_dim;
4841 map = isl_map_cow(map);
4842 if (!map)
4843 return NULL;
4845 range_dim = isl_space_range(isl_map_get_space(map));
4846 range_dim = isl_space_from_range(range_dim);
4847 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4848 map->dim = isl_space_join(map->dim, range_dim);
4849 if (!map->dim)
4850 goto error;
4851 for (i = 0; i < map->n; ++i) {
4852 map->p[i] = isl_basic_map_range_map(map->p[i]);
4853 if (!map->p[i])
4854 goto error;
4856 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4857 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4858 return map;
4859 error:
4860 isl_map_free(map);
4861 return NULL;
4864 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4865 __isl_take isl_space *dim)
4867 int i;
4868 struct isl_map *map = NULL;
4870 set = isl_set_cow(set);
4871 if (!set || !dim)
4872 goto error;
4873 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4874 map = (struct isl_map *)set;
4875 for (i = 0; i < set->n; ++i) {
4876 map->p[i] = isl_basic_map_from_basic_set(
4877 set->p[i], isl_space_copy(dim));
4878 if (!map->p[i])
4879 goto error;
4881 isl_space_free(map->dim);
4882 map->dim = dim;
4883 return map;
4884 error:
4885 isl_space_free(dim);
4886 isl_set_free(set);
4887 return NULL;
4890 __isl_give isl_basic_map *isl_basic_map_from_domain(
4891 __isl_take isl_basic_set *bset)
4893 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4896 __isl_give isl_basic_map *isl_basic_map_from_range(
4897 __isl_take isl_basic_set *bset)
4899 isl_space *space;
4900 space = isl_basic_set_get_space(bset);
4901 space = isl_space_from_range(space);
4902 bset = isl_basic_set_reset_space(bset, space);
4903 return (isl_basic_map *)bset;
4906 struct isl_map *isl_map_from_range(struct isl_set *set)
4908 isl_space *space;
4909 space = isl_set_get_space(set);
4910 space = isl_space_from_range(space);
4911 set = isl_set_reset_space(set, space);
4912 return (struct isl_map *)set;
4915 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4917 return isl_map_reverse(isl_map_from_range(set));
4920 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4921 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4923 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4926 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4927 __isl_take isl_set *range)
4929 return isl_map_apply_range(isl_map_reverse(domain), range);
4932 struct isl_set *isl_set_from_map(struct isl_map *map)
4934 int i;
4935 struct isl_set *set = NULL;
4937 if (!map)
4938 return NULL;
4939 map = isl_map_cow(map);
4940 if (!map)
4941 return NULL;
4942 map->dim = isl_space_as_set_space(map->dim);
4943 if (!map->dim)
4944 goto error;
4945 set = (struct isl_set *)map;
4946 for (i = 0; i < map->n; ++i) {
4947 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4948 if (!set->p[i])
4949 goto error;
4951 return set;
4952 error:
4953 isl_map_free(map);
4954 return NULL;
4957 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4958 unsigned flags)
4960 struct isl_map *map;
4962 if (!dim)
4963 return NULL;
4964 if (n < 0)
4965 isl_die(dim->ctx, isl_error_internal,
4966 "negative number of basic maps", goto error);
4967 map = isl_alloc(dim->ctx, struct isl_map,
4968 sizeof(struct isl_map) +
4969 (n - 1) * sizeof(struct isl_basic_map *));
4970 if (!map)
4971 goto error;
4973 map->ctx = dim->ctx;
4974 isl_ctx_ref(map->ctx);
4975 map->ref = 1;
4976 map->size = n;
4977 map->n = 0;
4978 map->dim = dim;
4979 map->flags = flags;
4980 return map;
4981 error:
4982 isl_space_free(dim);
4983 return NULL;
4986 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4987 unsigned nparam, unsigned in, unsigned out, int n,
4988 unsigned flags)
4990 struct isl_map *map;
4991 isl_space *dims;
4993 dims = isl_space_alloc(ctx, nparam, in, out);
4994 if (!dims)
4995 return NULL;
4997 map = isl_map_alloc_space(dims, n, flags);
4998 return map;
5001 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5003 struct isl_basic_map *bmap;
5004 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5005 bmap = isl_basic_map_set_to_empty(bmap);
5006 return bmap;
5009 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5011 struct isl_basic_set *bset;
5012 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5013 bset = isl_basic_set_set_to_empty(bset);
5014 return bset;
5017 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5019 struct isl_basic_map *bmap;
5020 if (!model)
5021 return NULL;
5022 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5023 bmap = isl_basic_map_set_to_empty(bmap);
5024 return bmap;
5027 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5029 struct isl_basic_map *bmap;
5030 if (!model)
5031 return NULL;
5032 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5033 bmap = isl_basic_map_set_to_empty(bmap);
5034 return bmap;
5037 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5039 struct isl_basic_set *bset;
5040 if (!model)
5041 return NULL;
5042 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5043 bset = isl_basic_set_set_to_empty(bset);
5044 return bset;
5047 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5049 struct isl_basic_map *bmap;
5050 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5051 bmap = isl_basic_map_finalize(bmap);
5052 return bmap;
5055 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5057 struct isl_basic_set *bset;
5058 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5059 bset = isl_basic_set_finalize(bset);
5060 return bset;
5063 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5065 int i;
5066 unsigned total = isl_space_dim(dim, isl_dim_all);
5067 isl_basic_map *bmap;
5069 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5070 for (i = 0; i < total; ++i) {
5071 int k = isl_basic_map_alloc_inequality(bmap);
5072 if (k < 0)
5073 goto error;
5074 isl_seq_clr(bmap->ineq[k], 1 + total);
5075 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5077 return bmap;
5078 error:
5079 isl_basic_map_free(bmap);
5080 return NULL;
5083 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5085 return isl_basic_map_nat_universe(dim);
5088 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5090 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5093 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5095 return isl_map_nat_universe(dim);
5098 __isl_give isl_basic_map *isl_basic_map_universe_like(
5099 __isl_keep isl_basic_map *model)
5101 if (!model)
5102 return NULL;
5103 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5106 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5108 if (!model)
5109 return NULL;
5110 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5113 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5114 __isl_keep isl_set *model)
5116 if (!model)
5117 return NULL;
5118 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5121 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5123 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5126 struct isl_map *isl_map_empty_like(struct isl_map *model)
5128 if (!model)
5129 return NULL;
5130 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5133 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5135 if (!model)
5136 return NULL;
5137 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5140 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5142 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5145 struct isl_set *isl_set_empty_like(struct isl_set *model)
5147 if (!model)
5148 return NULL;
5149 return isl_set_empty(isl_space_copy(model->dim));
5152 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5154 struct isl_map *map;
5155 if (!dim)
5156 return NULL;
5157 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5158 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5159 return map;
5162 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5164 struct isl_set *set;
5165 if (!dim)
5166 return NULL;
5167 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5168 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5169 return set;
5172 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5174 if (!model)
5175 return NULL;
5176 return isl_set_universe(isl_space_copy(model->dim));
5179 struct isl_map *isl_map_dup(struct isl_map *map)
5181 int i;
5182 struct isl_map *dup;
5184 if (!map)
5185 return NULL;
5186 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5187 for (i = 0; i < map->n; ++i)
5188 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5189 return dup;
5192 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5193 __isl_take isl_basic_map *bmap)
5195 if (!bmap || !map)
5196 goto error;
5197 if (isl_basic_map_plain_is_empty(bmap)) {
5198 isl_basic_map_free(bmap);
5199 return map;
5201 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5202 isl_assert(map->ctx, map->n < map->size, goto error);
5203 map->p[map->n] = bmap;
5204 map->n++;
5205 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5206 return map;
5207 error:
5208 if (map)
5209 isl_map_free(map);
5210 if (bmap)
5211 isl_basic_map_free(bmap);
5212 return NULL;
5215 void *isl_map_free(struct isl_map *map)
5217 int i;
5219 if (!map)
5220 return NULL;
5222 if (--map->ref > 0)
5223 return NULL;
5225 isl_ctx_deref(map->ctx);
5226 for (i = 0; i < map->n; ++i)
5227 isl_basic_map_free(map->p[i]);
5228 isl_space_free(map->dim);
5229 free(map);
5231 return NULL;
5234 struct isl_map *isl_map_extend(struct isl_map *base,
5235 unsigned nparam, unsigned n_in, unsigned n_out)
5237 int i;
5239 base = isl_map_cow(base);
5240 if (!base)
5241 return NULL;
5243 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5244 if (!base->dim)
5245 goto error;
5246 for (i = 0; i < base->n; ++i) {
5247 base->p[i] = isl_basic_map_extend_space(base->p[i],
5248 isl_space_copy(base->dim), 0, 0, 0);
5249 if (!base->p[i])
5250 goto error;
5252 return base;
5253 error:
5254 isl_map_free(base);
5255 return NULL;
5258 struct isl_set *isl_set_extend(struct isl_set *base,
5259 unsigned nparam, unsigned dim)
5261 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5262 nparam, 0, dim);
5265 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5266 struct isl_basic_map *bmap, unsigned pos, int value)
5268 int j;
5270 bmap = isl_basic_map_cow(bmap);
5271 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5272 j = isl_basic_map_alloc_equality(bmap);
5273 if (j < 0)
5274 goto error;
5275 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5276 isl_int_set_si(bmap->eq[j][pos], -1);
5277 isl_int_set_si(bmap->eq[j][0], value);
5278 bmap = isl_basic_map_simplify(bmap);
5279 return isl_basic_map_finalize(bmap);
5280 error:
5281 isl_basic_map_free(bmap);
5282 return NULL;
5285 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5286 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5288 int j;
5290 bmap = isl_basic_map_cow(bmap);
5291 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5292 j = isl_basic_map_alloc_equality(bmap);
5293 if (j < 0)
5294 goto error;
5295 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5296 isl_int_set_si(bmap->eq[j][pos], -1);
5297 isl_int_set(bmap->eq[j][0], value);
5298 bmap = isl_basic_map_simplify(bmap);
5299 return isl_basic_map_finalize(bmap);
5300 error:
5301 isl_basic_map_free(bmap);
5302 return NULL;
5305 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5306 enum isl_dim_type type, unsigned pos, int value)
5308 if (!bmap)
5309 return NULL;
5310 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5311 return isl_basic_map_fix_pos_si(bmap,
5312 isl_basic_map_offset(bmap, type) + pos, value);
5313 error:
5314 isl_basic_map_free(bmap);
5315 return NULL;
5318 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5319 enum isl_dim_type type, unsigned pos, isl_int value)
5321 if (!bmap)
5322 return NULL;
5323 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5324 return isl_basic_map_fix_pos(bmap,
5325 isl_basic_map_offset(bmap, type) + pos, value);
5326 error:
5327 isl_basic_map_free(bmap);
5328 return NULL;
5331 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5332 enum isl_dim_type type, unsigned pos, int value)
5334 return (struct isl_basic_set *)
5335 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5336 type, pos, value);
5339 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5340 enum isl_dim_type type, unsigned pos, isl_int value)
5342 return (struct isl_basic_set *)
5343 isl_basic_map_fix((struct isl_basic_map *)bset,
5344 type, pos, value);
5347 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5348 unsigned input, int value)
5350 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5353 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5354 unsigned dim, int value)
5356 return (struct isl_basic_set *)
5357 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5358 isl_dim_set, dim, value);
5361 static int remove_if_empty(__isl_keep isl_map *map, int i)
5363 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5365 if (empty < 0)
5366 return -1;
5367 if (!empty)
5368 return 0;
5370 isl_basic_map_free(map->p[i]);
5371 if (i != map->n - 1) {
5372 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5373 map->p[i] = map->p[map->n - 1];
5375 map->n--;
5377 return 0;
5380 /* Perform "fn" on each basic map of "map", where we may not be holding
5381 * the only reference to "map".
5382 * In particular, "fn" should be a semantics preserving operation
5383 * that we want to apply to all copies of "map". We therefore need
5384 * to be careful not to modify "map" in a way that breaks "map"
5385 * in case anything goes wrong.
5387 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5388 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5390 struct isl_basic_map *bmap;
5391 int i;
5393 if (!map)
5394 return NULL;
5396 for (i = map->n - 1; i >= 0; --i) {
5397 bmap = isl_basic_map_copy(map->p[i]);
5398 bmap = fn(bmap);
5399 if (!bmap)
5400 goto error;
5401 isl_basic_map_free(map->p[i]);
5402 map->p[i] = bmap;
5403 if (remove_if_empty(map, i) < 0)
5404 goto error;
5407 return map;
5408 error:
5409 isl_map_free(map);
5410 return NULL;
5413 struct isl_map *isl_map_fix_si(struct isl_map *map,
5414 enum isl_dim_type type, unsigned pos, int value)
5416 int i;
5418 map = isl_map_cow(map);
5419 if (!map)
5420 return NULL;
5422 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5423 for (i = map->n - 1; i >= 0; --i) {
5424 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5425 if (remove_if_empty(map, i) < 0)
5426 goto error;
5428 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5429 return map;
5430 error:
5431 isl_map_free(map);
5432 return NULL;
5435 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5436 enum isl_dim_type type, unsigned pos, int value)
5438 return (struct isl_set *)
5439 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5442 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5443 enum isl_dim_type type, unsigned pos, isl_int value)
5445 int i;
5447 map = isl_map_cow(map);
5448 if (!map)
5449 return NULL;
5451 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5452 for (i = 0; i < map->n; ++i) {
5453 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5454 if (!map->p[i])
5455 goto error;
5457 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5458 return map;
5459 error:
5460 isl_map_free(map);
5461 return NULL;
5464 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5465 enum isl_dim_type type, unsigned pos, isl_int value)
5467 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5470 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5471 unsigned input, int value)
5473 return isl_map_fix_si(map, isl_dim_in, input, value);
5476 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5478 return (struct isl_set *)
5479 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5482 static __isl_give isl_basic_map *basic_map_bound_si(
5483 __isl_take isl_basic_map *bmap,
5484 enum isl_dim_type type, unsigned pos, int value, int upper)
5486 int j;
5488 if (!bmap)
5489 return NULL;
5490 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5491 pos += isl_basic_map_offset(bmap, type);
5492 bmap = isl_basic_map_cow(bmap);
5493 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5494 j = isl_basic_map_alloc_inequality(bmap);
5495 if (j < 0)
5496 goto error;
5497 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5498 if (upper) {
5499 isl_int_set_si(bmap->ineq[j][pos], -1);
5500 isl_int_set_si(bmap->ineq[j][0], value);
5501 } else {
5502 isl_int_set_si(bmap->ineq[j][pos], 1);
5503 isl_int_set_si(bmap->ineq[j][0], -value);
5505 bmap = isl_basic_map_simplify(bmap);
5506 return isl_basic_map_finalize(bmap);
5507 error:
5508 isl_basic_map_free(bmap);
5509 return NULL;
5512 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5513 __isl_take isl_basic_map *bmap,
5514 enum isl_dim_type type, unsigned pos, int value)
5516 return basic_map_bound_si(bmap, type, pos, value, 0);
5519 /* Constrain the values of the given dimension to be no greater than "value".
5521 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5522 __isl_take isl_basic_map *bmap,
5523 enum isl_dim_type type, unsigned pos, int value)
5525 return basic_map_bound_si(bmap, type, pos, value, 1);
5528 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5529 unsigned dim, isl_int value)
5531 int j;
5533 bset = isl_basic_set_cow(bset);
5534 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5535 j = isl_basic_set_alloc_inequality(bset);
5536 if (j < 0)
5537 goto error;
5538 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5539 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5540 isl_int_neg(bset->ineq[j][0], value);
5541 bset = isl_basic_set_simplify(bset);
5542 return isl_basic_set_finalize(bset);
5543 error:
5544 isl_basic_set_free(bset);
5545 return NULL;
5548 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5549 enum isl_dim_type type, unsigned pos, int value, int upper)
5551 int i;
5553 map = isl_map_cow(map);
5554 if (!map)
5555 return NULL;
5557 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5558 for (i = 0; i < map->n; ++i) {
5559 map->p[i] = basic_map_bound_si(map->p[i],
5560 type, pos, value, upper);
5561 if (!map->p[i])
5562 goto error;
5564 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5565 return map;
5566 error:
5567 isl_map_free(map);
5568 return NULL;
5571 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5572 enum isl_dim_type type, unsigned pos, int value)
5574 return map_bound_si(map, type, pos, value, 0);
5577 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5578 enum isl_dim_type type, unsigned pos, int value)
5580 return map_bound_si(map, type, pos, value, 1);
5583 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5584 enum isl_dim_type type, unsigned pos, int value)
5586 return (struct isl_set *)
5587 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5590 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5591 enum isl_dim_type type, unsigned pos, int value)
5593 return isl_map_upper_bound_si(set, type, pos, value);
5596 /* Bound the given variable of "bmap" from below (or above is "upper"
5597 * is set) to "value".
5599 static __isl_give isl_basic_map *basic_map_bound(
5600 __isl_take isl_basic_map *bmap,
5601 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5603 int j;
5605 if (!bmap)
5606 return NULL;
5607 if (pos >= isl_basic_map_dim(bmap, type))
5608 isl_die(bmap->ctx, isl_error_invalid,
5609 "index out of bounds", goto error);
5610 pos += isl_basic_map_offset(bmap, type);
5611 bmap = isl_basic_map_cow(bmap);
5612 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5613 j = isl_basic_map_alloc_inequality(bmap);
5614 if (j < 0)
5615 goto error;
5616 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5617 if (upper) {
5618 isl_int_set_si(bmap->ineq[j][pos], -1);
5619 isl_int_set(bmap->ineq[j][0], value);
5620 } else {
5621 isl_int_set_si(bmap->ineq[j][pos], 1);
5622 isl_int_neg(bmap->ineq[j][0], value);
5624 bmap = isl_basic_map_simplify(bmap);
5625 return isl_basic_map_finalize(bmap);
5626 error:
5627 isl_basic_map_free(bmap);
5628 return NULL;
5631 /* Bound the given variable of "map" from below (or above is "upper"
5632 * is set) to "value".
5634 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5635 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5637 int i;
5639 map = isl_map_cow(map);
5640 if (!map)
5641 return NULL;
5643 if (pos >= isl_map_dim(map, type))
5644 isl_die(map->ctx, isl_error_invalid,
5645 "index out of bounds", goto error);
5646 for (i = map->n - 1; i >= 0; --i) {
5647 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5648 if (remove_if_empty(map, i) < 0)
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 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5659 enum isl_dim_type type, unsigned pos, isl_int value)
5661 return map_bound(map, type, pos, value, 0);
5664 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5665 enum isl_dim_type type, unsigned pos, isl_int value)
5667 return map_bound(map, type, pos, value, 1);
5670 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5671 enum isl_dim_type type, unsigned pos, isl_int value)
5673 return isl_map_lower_bound(set, type, pos, value);
5676 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5677 enum isl_dim_type type, unsigned pos, isl_int value)
5679 return isl_map_upper_bound(set, type, pos, value);
5682 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5683 isl_int value)
5685 int i;
5687 set = isl_set_cow(set);
5688 if (!set)
5689 return NULL;
5691 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5692 for (i = 0; i < set->n; ++i) {
5693 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5694 if (!set->p[i])
5695 goto error;
5697 return set;
5698 error:
5699 isl_set_free(set);
5700 return NULL;
5703 struct isl_map *isl_map_reverse(struct isl_map *map)
5705 int i;
5707 map = isl_map_cow(map);
5708 if (!map)
5709 return NULL;
5711 map->dim = isl_space_reverse(map->dim);
5712 if (!map->dim)
5713 goto error;
5714 for (i = 0; i < map->n; ++i) {
5715 map->p[i] = isl_basic_map_reverse(map->p[i]);
5716 if (!map->p[i])
5717 goto error;
5719 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5720 return map;
5721 error:
5722 isl_map_free(map);
5723 return NULL;
5726 static struct isl_map *isl_basic_map_partial_lexopt(
5727 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5728 struct isl_set **empty, int max)
5730 if (!bmap)
5731 goto error;
5732 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5733 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5734 else
5735 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5736 error:
5737 isl_basic_map_free(bmap);
5738 isl_basic_set_free(dom);
5739 if (empty)
5740 *empty = NULL;
5741 return NULL;
5744 struct isl_map *isl_basic_map_partial_lexmax(
5745 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5746 struct isl_set **empty)
5748 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5751 struct isl_map *isl_basic_map_partial_lexmin(
5752 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5753 struct isl_set **empty)
5755 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5758 struct isl_set *isl_basic_set_partial_lexmin(
5759 struct isl_basic_set *bset, struct isl_basic_set *dom,
5760 struct isl_set **empty)
5762 return (struct isl_set *)
5763 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5764 dom, empty);
5767 struct isl_set *isl_basic_set_partial_lexmax(
5768 struct isl_basic_set *bset, struct isl_basic_set *dom,
5769 struct isl_set **empty)
5771 return (struct isl_set *)
5772 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5773 dom, empty);
5776 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5777 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5778 __isl_give isl_set **empty)
5780 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5783 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5784 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5785 __isl_give isl_set **empty)
5787 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5790 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5791 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5792 __isl_give isl_set **empty)
5794 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5797 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5798 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5799 __isl_give isl_set **empty)
5801 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5804 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5805 __isl_take isl_basic_map *bmap, int max)
5807 isl_basic_set *dom = NULL;
5808 isl_space *dom_space;
5810 if (!bmap)
5811 goto error;
5812 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5813 dom = isl_basic_set_universe(dom_space);
5814 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5815 error:
5816 isl_basic_map_free(bmap);
5817 return NULL;
5820 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5821 __isl_take isl_basic_map *bmap)
5823 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5826 #undef TYPE
5827 #define TYPE isl_pw_multi_aff
5828 #undef SUFFIX
5829 #define SUFFIX _pw_multi_aff
5830 #undef EMPTY
5831 #define EMPTY isl_pw_multi_aff_empty
5832 #undef ADD
5833 #define ADD isl_pw_multi_aff_union_add
5834 #include "isl_map_lexopt_templ.c"
5836 /* Given a map "map", compute the lexicographically minimal
5837 * (or maximal) image element for each domain element in dom,
5838 * in the form of an isl_pw_multi_aff.
5839 * Set *empty to those elements in dom that do not have an image element.
5841 * We first compute the lexicographically minimal or maximal element
5842 * in the first basic map. This results in a partial solution "res"
5843 * and a subset "todo" of dom that still need to be handled.
5844 * We then consider each of the remaining maps in "map" and successively
5845 * update both "res" and "todo".
5847 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
5848 __isl_take isl_map *map, __isl_take isl_set *dom,
5849 __isl_give isl_set **empty, int max)
5851 int i;
5852 isl_pw_multi_aff *res;
5853 isl_set *todo;
5855 if (!map || !dom)
5856 goto error;
5858 if (isl_map_plain_is_empty(map)) {
5859 if (empty)
5860 *empty = dom;
5861 else
5862 isl_set_free(dom);
5863 return isl_pw_multi_aff_from_map(map);
5866 res = basic_map_partial_lexopt_pw_multi_aff(
5867 isl_basic_map_copy(map->p[0]),
5868 isl_set_copy(dom), &todo, max);
5870 for (i = 1; i < map->n; ++i) {
5871 isl_pw_multi_aff *res_i;
5872 isl_set *todo_i;
5874 res_i = basic_map_partial_lexopt_pw_multi_aff(
5875 isl_basic_map_copy(map->p[i]),
5876 isl_set_copy(dom), &todo_i, max);
5878 if (max)
5879 res = isl_pw_multi_aff_union_lexmax(res, res_i);
5880 else
5881 res = isl_pw_multi_aff_union_lexmin(res, res_i);
5883 todo = isl_set_intersect(todo, todo_i);
5886 isl_set_free(dom);
5887 isl_map_free(map);
5889 if (empty)
5890 *empty = todo;
5891 else
5892 isl_set_free(todo);
5894 return res;
5895 error:
5896 if (empty)
5897 *empty = NULL;
5898 isl_set_free(dom);
5899 isl_map_free(map);
5900 return NULL;
5903 #undef TYPE
5904 #define TYPE isl_map
5905 #undef SUFFIX
5906 #define SUFFIX
5907 #undef EMPTY
5908 #define EMPTY isl_map_empty
5909 #undef ADD
5910 #define ADD isl_map_union_disjoint
5911 #include "isl_map_lexopt_templ.c"
5913 /* Given a map "map", compute the lexicographically minimal
5914 * (or maximal) image element for each domain element in dom.
5915 * Set *empty to those elements in dom that do not have an image element.
5917 * We first compute the lexicographically minimal or maximal element
5918 * in the first basic map. This results in a partial solution "res"
5919 * and a subset "todo" of dom that still need to be handled.
5920 * We then consider each of the remaining maps in "map" and successively
5921 * update both "res" and "todo".
5923 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5924 * Assume we are computing the lexicographical maximum.
5925 * We first compute the lexicographically maximal element in basic map i.
5926 * This results in a partial solution res_i and a subset todo_i.
5927 * Then we combine these results with those obtain for the first k basic maps
5928 * to obtain a result that is valid for the first k+1 basic maps.
5929 * In particular, the set where there is no solution is the set where
5930 * there is no solution for the first k basic maps and also no solution
5931 * for the ith basic map, i.e.,
5933 * todo^i = todo^k * todo_i
5935 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5936 * solutions, arbitrarily breaking ties in favor of res^k.
5937 * That is, when res^k(a) >= res_i(a), we pick res^k and
5938 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5939 * the lexicographic order.)
5940 * In practice, we compute
5942 * res^k * (res_i . "<=")
5944 * and
5946 * res_i * (res^k . "<")
5948 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5949 * where only one of res^k and res_i provides a solution and we simply pick
5950 * that one, i.e.,
5952 * res^k * todo_i
5953 * and
5954 * res_i * todo^k
5956 * Note that we only compute these intersections when dom(res^k) intersects
5957 * dom(res_i). Otherwise, the only effect of these intersections is to
5958 * potentially break up res^k and res_i into smaller pieces.
5959 * We want to avoid such splintering as much as possible.
5960 * In fact, an earlier implementation of this function would look for
5961 * better results in the domain of res^k and for extra results in todo^k,
5962 * but this would always result in a splintering according to todo^k,
5963 * even when the domain of basic map i is disjoint from the domains of
5964 * the previous basic maps.
5966 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5967 __isl_take isl_map *map, __isl_take isl_set *dom,
5968 __isl_give isl_set **empty, int max)
5970 int i;
5971 struct isl_map *res;
5972 struct isl_set *todo;
5974 if (!map || !dom)
5975 goto error;
5977 if (isl_map_plain_is_empty(map)) {
5978 if (empty)
5979 *empty = dom;
5980 else
5981 isl_set_free(dom);
5982 return map;
5985 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5986 isl_set_copy(dom), &todo, max);
5988 for (i = 1; i < map->n; ++i) {
5989 isl_map *lt, *le;
5990 isl_map *res_i;
5991 isl_set *todo_i;
5992 isl_space *dim = isl_space_range(isl_map_get_space(res));
5994 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5995 isl_set_copy(dom), &todo_i, max);
5997 if (max) {
5998 lt = isl_map_lex_lt(isl_space_copy(dim));
5999 le = isl_map_lex_le(dim);
6000 } else {
6001 lt = isl_map_lex_gt(isl_space_copy(dim));
6002 le = isl_map_lex_ge(dim);
6004 lt = isl_map_apply_range(isl_map_copy(res), lt);
6005 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6006 le = isl_map_apply_range(isl_map_copy(res_i), le);
6007 le = isl_map_intersect(le, isl_map_copy(res));
6009 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6010 res = isl_map_intersect_domain(res,
6011 isl_set_copy(todo_i));
6012 res_i = isl_map_intersect_domain(res_i,
6013 isl_set_copy(todo));
6016 res = isl_map_union_disjoint(res, res_i);
6017 res = isl_map_union_disjoint(res, lt);
6018 res = isl_map_union_disjoint(res, le);
6020 todo = isl_set_intersect(todo, todo_i);
6023 isl_set_free(dom);
6024 isl_map_free(map);
6026 if (empty)
6027 *empty = todo;
6028 else
6029 isl_set_free(todo);
6031 return res;
6032 error:
6033 if (empty)
6034 *empty = NULL;
6035 isl_set_free(dom);
6036 isl_map_free(map);
6037 return NULL;
6040 __isl_give isl_map *isl_map_partial_lexmax(
6041 __isl_take isl_map *map, __isl_take isl_set *dom,
6042 __isl_give isl_set **empty)
6044 return isl_map_partial_lexopt(map, dom, empty, 1);
6047 __isl_give isl_map *isl_map_partial_lexmin(
6048 __isl_take isl_map *map, __isl_take isl_set *dom,
6049 __isl_give isl_set **empty)
6051 return isl_map_partial_lexopt(map, dom, empty, 0);
6054 __isl_give isl_set *isl_set_partial_lexmin(
6055 __isl_take isl_set *set, __isl_take isl_set *dom,
6056 __isl_give isl_set **empty)
6058 return (struct isl_set *)
6059 isl_map_partial_lexmin((struct isl_map *)set,
6060 dom, empty);
6063 __isl_give isl_set *isl_set_partial_lexmax(
6064 __isl_take isl_set *set, __isl_take isl_set *dom,
6065 __isl_give isl_set **empty)
6067 return (struct isl_set *)
6068 isl_map_partial_lexmax((struct isl_map *)set,
6069 dom, empty);
6072 /* Compute the lexicographic minimum (or maximum if "max" is set)
6073 * of "bmap" over its domain.
6075 * Since we are not interested in the part of the domain space where
6076 * there is no solution, we initialize the domain to those constraints
6077 * of "bmap" that only involve the parameters and the input dimensions.
6078 * This relieves the parametric programming engine from detecting those
6079 * inequalities and transferring them to the context. More importantly,
6080 * it ensures that those inequalities are transferred first and not
6081 * intermixed with inequalities that actually split the domain.
6083 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6085 int n_div;
6086 int n_out;
6087 isl_basic_map *copy;
6088 isl_basic_set *dom;
6090 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6091 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6092 copy = isl_basic_map_copy(bmap);
6093 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6094 isl_dim_div, 0, n_div);
6095 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6096 isl_dim_out, 0, n_out);
6097 dom = isl_basic_map_domain(copy);
6098 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6101 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6103 return isl_basic_map_lexopt(bmap, 0);
6106 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6108 return isl_basic_map_lexopt(bmap, 1);
6111 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6113 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6116 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6118 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6121 /* Extract the first and only affine expression from list
6122 * and then add it to *pwaff with the given dom.
6123 * This domain is known to be disjoint from other domains
6124 * because of the way isl_basic_map_foreach_lexmax works.
6126 static int update_dim_opt(__isl_take isl_basic_set *dom,
6127 __isl_take isl_aff_list *list, void *user)
6129 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6130 isl_aff *aff;
6131 isl_pw_aff **pwaff = user;
6132 isl_pw_aff *pwaff_i;
6134 if (!list)
6135 goto error;
6136 if (isl_aff_list_n_aff(list) != 1)
6137 isl_die(ctx, isl_error_internal,
6138 "expecting single element list", goto error);
6140 aff = isl_aff_list_get_aff(list, 0);
6141 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6143 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6145 isl_aff_list_free(list);
6147 return 0;
6148 error:
6149 isl_basic_set_free(dom);
6150 isl_aff_list_free(list);
6151 return -1;
6154 /* Given a basic map with one output dimension, compute the minimum or
6155 * maximum of that dimension as an isl_pw_aff.
6157 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6158 * call update_dim_opt on each leaf of the result.
6160 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6161 int max)
6163 isl_space *dim = isl_basic_map_get_space(bmap);
6164 isl_pw_aff *pwaff;
6165 int r;
6167 dim = isl_space_from_domain(isl_space_domain(dim));
6168 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6169 pwaff = isl_pw_aff_empty(dim);
6171 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6172 if (r < 0)
6173 return isl_pw_aff_free(pwaff);
6175 return pwaff;
6178 /* Compute the minimum or maximum of the given output dimension
6179 * as a function of the parameters and the input dimensions,
6180 * but independently of the other output dimensions.
6182 * We first project out the other output dimension and then compute
6183 * the "lexicographic" maximum in each basic map, combining the results
6184 * using isl_pw_aff_union_max.
6186 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6187 int max)
6189 int i;
6190 isl_pw_aff *pwaff;
6191 unsigned n_out;
6193 n_out = isl_map_dim(map, isl_dim_out);
6194 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6195 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6196 if (!map)
6197 return NULL;
6199 if (map->n == 0) {
6200 isl_space *dim = isl_map_get_space(map);
6201 dim = isl_space_domain(isl_space_from_range(dim));
6202 isl_map_free(map);
6203 return isl_pw_aff_empty(dim);
6206 pwaff = basic_map_dim_opt(map->p[0], max);
6207 for (i = 1; i < map->n; ++i) {
6208 isl_pw_aff *pwaff_i;
6210 pwaff_i = basic_map_dim_opt(map->p[i], max);
6211 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6214 isl_map_free(map);
6216 return pwaff;
6219 /* Compute the maximum of the given output dimension as a function of the
6220 * parameters and input dimensions, but independently of
6221 * the other output dimensions.
6223 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6225 return map_dim_opt(map, pos, 1);
6228 /* Compute the minimum or maximum of the given set dimension
6229 * as a function of the parameters,
6230 * but independently of the other set dimensions.
6232 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6233 int max)
6235 return map_dim_opt(set, pos, max);
6238 /* Compute the maximum of the given set dimension as a function of the
6239 * parameters, but independently of the other set dimensions.
6241 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6243 return set_dim_opt(set, pos, 1);
6246 /* Compute the minimum of the given set dimension as a function of the
6247 * parameters, but independently of the other set dimensions.
6249 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6251 return set_dim_opt(set, pos, 0);
6254 /* Apply a preimage specified by "mat" on the parameters of "bset".
6255 * bset is assumed to have only parameters and divs.
6257 static struct isl_basic_set *basic_set_parameter_preimage(
6258 struct isl_basic_set *bset, struct isl_mat *mat)
6260 unsigned nparam;
6262 if (!bset || !mat)
6263 goto error;
6265 bset->dim = isl_space_cow(bset->dim);
6266 if (!bset->dim)
6267 goto error;
6269 nparam = isl_basic_set_dim(bset, isl_dim_param);
6271 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6273 bset->dim->nparam = 0;
6274 bset->dim->n_out = nparam;
6275 bset = isl_basic_set_preimage(bset, mat);
6276 if (bset) {
6277 bset->dim->nparam = bset->dim->n_out;
6278 bset->dim->n_out = 0;
6280 return bset;
6281 error:
6282 isl_mat_free(mat);
6283 isl_basic_set_free(bset);
6284 return NULL;
6287 /* Apply a preimage specified by "mat" on the parameters of "set".
6288 * set is assumed to have only parameters and divs.
6290 static struct isl_set *set_parameter_preimage(
6291 struct isl_set *set, struct isl_mat *mat)
6293 isl_space *dim = NULL;
6294 unsigned nparam;
6296 if (!set || !mat)
6297 goto error;
6299 dim = isl_space_copy(set->dim);
6300 dim = isl_space_cow(dim);
6301 if (!dim)
6302 goto error;
6304 nparam = isl_set_dim(set, isl_dim_param);
6306 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6308 dim->nparam = 0;
6309 dim->n_out = nparam;
6310 isl_set_reset_space(set, dim);
6311 set = isl_set_preimage(set, mat);
6312 if (!set)
6313 goto error2;
6314 dim = isl_space_copy(set->dim);
6315 dim = isl_space_cow(dim);
6316 if (!dim)
6317 goto error2;
6318 dim->nparam = dim->n_out;
6319 dim->n_out = 0;
6320 isl_set_reset_space(set, dim);
6321 return set;
6322 error:
6323 isl_space_free(dim);
6324 isl_mat_free(mat);
6325 error2:
6326 isl_set_free(set);
6327 return NULL;
6330 /* Intersect the basic set "bset" with the affine space specified by the
6331 * equalities in "eq".
6333 static struct isl_basic_set *basic_set_append_equalities(
6334 struct isl_basic_set *bset, struct isl_mat *eq)
6336 int i, k;
6337 unsigned len;
6339 if (!bset || !eq)
6340 goto error;
6342 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6343 eq->n_row, 0);
6344 if (!bset)
6345 goto error;
6347 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6348 for (i = 0; i < eq->n_row; ++i) {
6349 k = isl_basic_set_alloc_equality(bset);
6350 if (k < 0)
6351 goto error;
6352 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6353 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6355 isl_mat_free(eq);
6357 bset = isl_basic_set_gauss(bset, NULL);
6358 bset = isl_basic_set_finalize(bset);
6360 return bset;
6361 error:
6362 isl_mat_free(eq);
6363 isl_basic_set_free(bset);
6364 return NULL;
6367 /* Intersect the set "set" with the affine space specified by the
6368 * equalities in "eq".
6370 static struct isl_set *set_append_equalities(struct isl_set *set,
6371 struct isl_mat *eq)
6373 int i;
6375 if (!set || !eq)
6376 goto error;
6378 for (i = 0; i < set->n; ++i) {
6379 set->p[i] = basic_set_append_equalities(set->p[i],
6380 isl_mat_copy(eq));
6381 if (!set->p[i])
6382 goto error;
6384 isl_mat_free(eq);
6385 return set;
6386 error:
6387 isl_mat_free(eq);
6388 isl_set_free(set);
6389 return NULL;
6392 /* Given a basic set "bset" that only involves parameters and existentially
6393 * quantified variables, return the index of the first equality
6394 * that only involves parameters. If there is no such equality then
6395 * return bset->n_eq.
6397 * This function assumes that isl_basic_set_gauss has been called on "bset".
6399 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6401 int i, j;
6402 unsigned nparam, n_div;
6404 if (!bset)
6405 return -1;
6407 nparam = isl_basic_set_dim(bset, isl_dim_param);
6408 n_div = isl_basic_set_dim(bset, isl_dim_div);
6410 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6411 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6412 ++i;
6415 return i;
6418 /* Compute an explicit representation for the existentially quantified
6419 * variables in "bset" by computing the "minimal value" of the set
6420 * variables. Since there are no set variables, the computation of
6421 * the minimal value essentially computes an explicit representation
6422 * of the non-empty part(s) of "bset".
6424 * The input only involves parameters and existentially quantified variables.
6425 * All equalities among parameters have been removed.
6427 * Since the existentially quantified variables in the result are in general
6428 * going to be different from those in the input, we first replace
6429 * them by the minimal number of variables based on their equalities.
6430 * This should simplify the parametric integer programming.
6432 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6434 isl_morph *morph1, *morph2;
6435 isl_set *set;
6436 unsigned n;
6438 if (!bset)
6439 return NULL;
6440 if (bset->n_eq == 0)
6441 return isl_basic_set_lexmin(bset);
6443 morph1 = isl_basic_set_parameter_compression(bset);
6444 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6445 bset = isl_basic_set_lift(bset);
6446 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6447 bset = isl_morph_basic_set(morph2, bset);
6448 n = isl_basic_set_dim(bset, isl_dim_set);
6449 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6451 set = isl_basic_set_lexmin(bset);
6453 set = isl_morph_set(isl_morph_inverse(morph1), set);
6455 return set;
6458 /* Project the given basic set onto its parameter domain, possibly introducing
6459 * new, explicit, existential variables in the constraints.
6460 * The input has parameters and (possibly implicit) existential variables.
6461 * The output has the same parameters, but only
6462 * explicit existentially quantified variables.
6464 * The actual projection is performed by pip, but pip doesn't seem
6465 * to like equalities very much, so we first remove the equalities
6466 * among the parameters by performing a variable compression on
6467 * the parameters. Afterward, an inverse transformation is performed
6468 * and the equalities among the parameters are inserted back in.
6470 * The variable compression on the parameters may uncover additional
6471 * equalities that were only implicit before. We therefore check
6472 * if there are any new parameter equalities in the result and
6473 * if so recurse. The removal of parameter equalities is required
6474 * for the parameter compression performed by base_compute_divs.
6476 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6478 int i;
6479 struct isl_mat *eq;
6480 struct isl_mat *T, *T2;
6481 struct isl_set *set;
6482 unsigned nparam;
6484 bset = isl_basic_set_cow(bset);
6485 if (!bset)
6486 return NULL;
6488 if (bset->n_eq == 0)
6489 return base_compute_divs(bset);
6491 bset = isl_basic_set_gauss(bset, NULL);
6492 if (!bset)
6493 return NULL;
6494 if (isl_basic_set_plain_is_empty(bset))
6495 return isl_set_from_basic_set(bset);
6497 i = first_parameter_equality(bset);
6498 if (i == bset->n_eq)
6499 return base_compute_divs(bset);
6501 nparam = isl_basic_set_dim(bset, isl_dim_param);
6502 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6503 0, 1 + nparam);
6504 eq = isl_mat_cow(eq);
6505 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6506 if (T && T->n_col == 0) {
6507 isl_mat_free(T);
6508 isl_mat_free(T2);
6509 isl_mat_free(eq);
6510 bset = isl_basic_set_set_to_empty(bset);
6511 return isl_set_from_basic_set(bset);
6513 bset = basic_set_parameter_preimage(bset, T);
6515 i = first_parameter_equality(bset);
6516 if (!bset)
6517 set = NULL;
6518 else if (i == bset->n_eq)
6519 set = base_compute_divs(bset);
6520 else
6521 set = parameter_compute_divs(bset);
6522 set = set_parameter_preimage(set, T2);
6523 set = set_append_equalities(set, eq);
6524 return set;
6527 /* Insert the divs from "ls" before those of "bmap".
6529 * The number of columns is not changed, which means that the last
6530 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6531 * The caller is responsible for removing the same number of dimensions
6532 * from the space of "bmap".
6534 static __isl_give isl_basic_map *insert_divs_from_local_space(
6535 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6537 int i;
6538 int n_div;
6539 int old_n_div;
6541 n_div = isl_local_space_dim(ls, isl_dim_div);
6542 if (n_div == 0)
6543 return bmap;
6545 old_n_div = bmap->n_div;
6546 bmap = insert_div_rows(bmap, n_div);
6547 if (!bmap)
6548 return NULL;
6550 for (i = 0; i < n_div; ++i) {
6551 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6552 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6555 return bmap;
6558 /* Replace the space of "bmap" by the space and divs of "ls".
6560 * If "ls" has any divs, then we simplify the result since we may
6561 * have discovered some additional equalities that could simplify
6562 * the div expressions.
6564 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6565 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6567 int n_div;
6569 bmap = isl_basic_map_cow(bmap);
6570 if (!bmap || !ls)
6571 goto error;
6573 n_div = isl_local_space_dim(ls, isl_dim_div);
6574 bmap = insert_divs_from_local_space(bmap, ls);
6575 if (!bmap)
6576 goto error;
6578 isl_space_free(bmap->dim);
6579 bmap->dim = isl_local_space_get_space(ls);
6580 if (!bmap->dim)
6581 goto error;
6583 isl_local_space_free(ls);
6584 if (n_div > 0)
6585 bmap = isl_basic_map_simplify(bmap);
6586 bmap = isl_basic_map_finalize(bmap);
6587 return bmap;
6588 error:
6589 isl_basic_map_free(bmap);
6590 isl_local_space_free(ls);
6591 return NULL;
6594 /* Replace the space of "map" by the space and divs of "ls".
6596 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6597 __isl_take isl_local_space *ls)
6599 int i;
6601 map = isl_map_cow(map);
6602 if (!map || !ls)
6603 goto error;
6605 for (i = 0; i < map->n; ++i) {
6606 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6607 isl_local_space_copy(ls));
6608 if (!map->p[i])
6609 goto error;
6611 isl_space_free(map->dim);
6612 map->dim = isl_local_space_get_space(ls);
6613 if (!map->dim)
6614 goto error;
6616 isl_local_space_free(ls);
6617 return map;
6618 error:
6619 isl_local_space_free(ls);
6620 isl_map_free(map);
6621 return NULL;
6624 /* Compute an explicit representation for the existentially
6625 * quantified variables for which do not know any explicit representation yet.
6627 * We first sort the existentially quantified variables so that the
6628 * existentially quantified variables for which we already have an explicit
6629 * representation are placed before those for which we do not.
6630 * The input dimensions, the output dimensions and the existentially
6631 * quantified variables for which we already have an explicit
6632 * representation are then turned into parameters.
6633 * compute_divs returns a map with the same parameters and
6634 * no input or output dimensions and the dimension specification
6635 * is reset to that of the input, including the existentially quantified
6636 * variables for which we already had an explicit representation.
6638 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6640 struct isl_basic_set *bset;
6641 struct isl_set *set;
6642 struct isl_map *map;
6643 isl_space *dim;
6644 isl_local_space *ls;
6645 unsigned nparam;
6646 unsigned n_in;
6647 unsigned n_out;
6648 unsigned n_known;
6649 int i;
6651 bmap = isl_basic_map_sort_divs(bmap);
6652 bmap = isl_basic_map_cow(bmap);
6653 if (!bmap)
6654 return NULL;
6656 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6657 if (isl_int_is_zero(bmap->div[n_known][0]))
6658 break;
6660 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6661 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6662 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6663 dim = isl_space_set_alloc(bmap->ctx,
6664 nparam + n_in + n_out + n_known, 0);
6665 if (!dim)
6666 goto error;
6668 ls = isl_basic_map_get_local_space(bmap);
6669 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6670 n_known, bmap->n_div - n_known);
6671 if (n_known > 0) {
6672 for (i = n_known; i < bmap->n_div; ++i)
6673 swap_div(bmap, i - n_known, i);
6674 bmap->n_div -= n_known;
6675 bmap->extra -= n_known;
6677 bmap = isl_basic_map_reset_space(bmap, dim);
6678 bset = (struct isl_basic_set *)bmap;
6680 set = parameter_compute_divs(bset);
6681 map = (struct isl_map *)set;
6682 map = replace_space_by_local_space(map, ls);
6684 return map;
6685 error:
6686 isl_basic_map_free(bmap);
6687 return NULL;
6690 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6692 int i;
6693 unsigned off;
6695 if (!bmap)
6696 return -1;
6698 off = isl_space_dim(bmap->dim, isl_dim_all);
6699 for (i = 0; i < bmap->n_div; ++i) {
6700 if (isl_int_is_zero(bmap->div[i][0]))
6701 return 0;
6702 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6703 return -1);
6705 return 1;
6708 static int map_divs_known(__isl_keep isl_map *map)
6710 int i;
6712 if (!map)
6713 return -1;
6715 for (i = 0; i < map->n; ++i) {
6716 int known = isl_basic_map_divs_known(map->p[i]);
6717 if (known <= 0)
6718 return known;
6721 return 1;
6724 /* If bmap contains any unknown divs, then compute explicit
6725 * expressions for them. However, this computation may be
6726 * quite expensive, so first try to remove divs that aren't
6727 * strictly needed.
6729 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6731 int known;
6732 struct isl_map *map;
6734 known = isl_basic_map_divs_known(bmap);
6735 if (known < 0)
6736 goto error;
6737 if (known)
6738 return isl_map_from_basic_map(bmap);
6740 bmap = isl_basic_map_drop_redundant_divs(bmap);
6742 known = isl_basic_map_divs_known(bmap);
6743 if (known < 0)
6744 goto error;
6745 if (known)
6746 return isl_map_from_basic_map(bmap);
6748 map = compute_divs(bmap);
6749 return map;
6750 error:
6751 isl_basic_map_free(bmap);
6752 return NULL;
6755 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6757 int i;
6758 int known;
6759 struct isl_map *res;
6761 if (!map)
6762 return NULL;
6763 if (map->n == 0)
6764 return map;
6766 known = map_divs_known(map);
6767 if (known < 0) {
6768 isl_map_free(map);
6769 return NULL;
6771 if (known)
6772 return map;
6774 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6775 for (i = 1 ; i < map->n; ++i) {
6776 struct isl_map *r2;
6777 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6778 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6779 res = isl_map_union_disjoint(res, r2);
6780 else
6781 res = isl_map_union(res, r2);
6783 isl_map_free(map);
6785 return res;
6788 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6790 return (struct isl_set *)
6791 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6794 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6796 return (struct isl_set *)
6797 isl_map_compute_divs((struct isl_map *)set);
6800 struct isl_set *isl_map_domain(struct isl_map *map)
6802 int i;
6803 struct isl_set *set;
6805 if (!map)
6806 goto error;
6808 map = isl_map_cow(map);
6809 if (!map)
6810 return NULL;
6812 set = (struct isl_set *)map;
6813 set->dim = isl_space_domain(set->dim);
6814 if (!set->dim)
6815 goto error;
6816 for (i = 0; i < map->n; ++i) {
6817 set->p[i] = isl_basic_map_domain(map->p[i]);
6818 if (!set->p[i])
6819 goto error;
6821 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6822 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6823 return set;
6824 error:
6825 isl_map_free(map);
6826 return NULL;
6829 /* Return the union of "map1" and "map2", where we assume for now that
6830 * "map1" and "map2" are disjoint. Note that the basic maps inside
6831 * "map1" or "map2" may not be disjoint from each other.
6832 * Also note that this function is also called from isl_map_union,
6833 * which takes care of handling the situation where "map1" and "map2"
6834 * may not be disjoint.
6836 * If one of the inputs is empty, we can simply return the other input.
6837 * Similarly, if one of the inputs is universal, then it is equal to the union.
6839 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6840 __isl_take isl_map *map2)
6842 int i;
6843 unsigned flags = 0;
6844 struct isl_map *map = NULL;
6845 int is_universe;
6847 if (!map1 || !map2)
6848 goto error;
6850 if (map1->n == 0) {
6851 isl_map_free(map1);
6852 return map2;
6854 if (map2->n == 0) {
6855 isl_map_free(map2);
6856 return map1;
6859 is_universe = isl_map_plain_is_universe(map1);
6860 if (is_universe < 0)
6861 goto error;
6862 if (is_universe) {
6863 isl_map_free(map2);
6864 return map1;
6867 is_universe = isl_map_plain_is_universe(map2);
6868 if (is_universe < 0)
6869 goto error;
6870 if (is_universe) {
6871 isl_map_free(map1);
6872 return map2;
6875 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6877 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6878 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6879 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6881 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6882 map1->n + map2->n, flags);
6883 if (!map)
6884 goto error;
6885 for (i = 0; i < map1->n; ++i) {
6886 map = isl_map_add_basic_map(map,
6887 isl_basic_map_copy(map1->p[i]));
6888 if (!map)
6889 goto error;
6891 for (i = 0; i < map2->n; ++i) {
6892 map = isl_map_add_basic_map(map,
6893 isl_basic_map_copy(map2->p[i]));
6894 if (!map)
6895 goto error;
6897 isl_map_free(map1);
6898 isl_map_free(map2);
6899 return map;
6900 error:
6901 isl_map_free(map);
6902 isl_map_free(map1);
6903 isl_map_free(map2);
6904 return NULL;
6907 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6908 __isl_take isl_map *map2)
6910 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6913 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6915 map1 = isl_map_union_disjoint(map1, map2);
6916 if (!map1)
6917 return NULL;
6918 if (map1->n > 1)
6919 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6920 return map1;
6923 struct isl_set *isl_set_union_disjoint(
6924 struct isl_set *set1, struct isl_set *set2)
6926 return (struct isl_set *)
6927 isl_map_union_disjoint(
6928 (struct isl_map *)set1, (struct isl_map *)set2);
6931 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6933 return (struct isl_set *)
6934 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6937 /* Apply "fn" to pairs of elements from "map" and "set" and collect
6938 * the results.
6940 * "map" and "set" are assumed to be compatible and non-NULL.
6942 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
6943 __isl_take isl_set *set,
6944 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
6945 __isl_take isl_basic_set *bset))
6947 unsigned flags = 0;
6948 struct isl_map *result;
6949 int i, j;
6951 if (isl_set_plain_is_universe(set)) {
6952 isl_set_free(set);
6953 return map;
6956 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6957 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6958 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6960 result = isl_map_alloc_space(isl_space_copy(map->dim),
6961 map->n * set->n, flags);
6962 for (i = 0; result && i < map->n; ++i)
6963 for (j = 0; j < set->n; ++j) {
6964 result = isl_map_add_basic_map(result,
6965 fn(isl_basic_map_copy(map->p[i]),
6966 isl_basic_set_copy(set->p[j])));
6967 if (!result)
6968 break;
6971 isl_map_free(map);
6972 isl_set_free(set);
6973 return result;
6976 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6977 __isl_take isl_set *set)
6979 if (!map || !set)
6980 goto error;
6982 if (!isl_map_compatible_range(map, set))
6983 isl_die(set->ctx, isl_error_invalid,
6984 "incompatible spaces", goto error);
6986 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
6987 error:
6988 isl_map_free(map);
6989 isl_set_free(set);
6990 return NULL;
6993 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6994 __isl_take isl_set *set)
6996 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6999 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7000 __isl_take isl_set *set)
7002 if (!map || !set)
7003 goto error;
7005 if (!isl_map_compatible_domain(map, set))
7006 isl_die(set->ctx, isl_error_invalid,
7007 "incompatible spaces", goto error);
7009 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7010 error:
7011 isl_map_free(map);
7012 isl_set_free(set);
7013 return NULL;
7016 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7017 __isl_take isl_set *set)
7019 return isl_map_align_params_map_map_and(map, set,
7020 &map_intersect_domain);
7023 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7024 __isl_take isl_map *map2)
7026 if (!map1 || !map2)
7027 goto error;
7028 map1 = isl_map_reverse(map1);
7029 map1 = isl_map_apply_range(map1, map2);
7030 return isl_map_reverse(map1);
7031 error:
7032 isl_map_free(map1);
7033 isl_map_free(map2);
7034 return NULL;
7037 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7038 __isl_take isl_map *map2)
7040 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7043 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7044 __isl_take isl_map *map2)
7046 isl_space *dim_result;
7047 struct isl_map *result;
7048 int i, j;
7050 if (!map1 || !map2)
7051 goto error;
7053 dim_result = isl_space_join(isl_space_copy(map1->dim),
7054 isl_space_copy(map2->dim));
7056 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7057 if (!result)
7058 goto error;
7059 for (i = 0; i < map1->n; ++i)
7060 for (j = 0; j < map2->n; ++j) {
7061 result = isl_map_add_basic_map(result,
7062 isl_basic_map_apply_range(
7063 isl_basic_map_copy(map1->p[i]),
7064 isl_basic_map_copy(map2->p[j])));
7065 if (!result)
7066 goto error;
7068 isl_map_free(map1);
7069 isl_map_free(map2);
7070 if (result && result->n <= 1)
7071 ISL_F_SET(result, ISL_MAP_DISJOINT);
7072 return result;
7073 error:
7074 isl_map_free(map1);
7075 isl_map_free(map2);
7076 return NULL;
7079 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7080 __isl_take isl_map *map2)
7082 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7086 * returns range - domain
7088 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7090 isl_space *dims, *target_dim;
7091 struct isl_basic_set *bset;
7092 unsigned dim;
7093 unsigned nparam;
7094 int i;
7096 if (!bmap)
7097 goto error;
7098 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
7099 bmap->dim, isl_dim_out),
7100 goto error);
7101 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7102 dim = isl_basic_map_n_in(bmap);
7103 nparam = isl_basic_map_n_param(bmap);
7104 bset = isl_basic_set_from_basic_map(bmap);
7105 bset = isl_basic_set_cow(bset);
7106 dims = isl_basic_set_get_space(bset);
7107 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7108 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7109 bset = isl_basic_set_swap_vars(bset, 2*dim);
7110 for (i = 0; i < dim; ++i) {
7111 int j = isl_basic_map_alloc_equality(
7112 (struct isl_basic_map *)bset);
7113 if (j < 0) {
7114 bset = isl_basic_set_free(bset);
7115 break;
7117 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7118 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7119 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7120 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7122 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7123 bset = isl_basic_set_reset_space(bset, target_dim);
7124 return bset;
7125 error:
7126 isl_basic_map_free(bmap);
7127 return NULL;
7131 * returns range - domain
7133 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7135 int i;
7136 isl_space *dim;
7137 struct isl_set *result;
7139 if (!map)
7140 return NULL;
7142 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
7143 map->dim, isl_dim_out),
7144 goto error);
7145 dim = isl_map_get_space(map);
7146 dim = isl_space_domain(dim);
7147 result = isl_set_alloc_space(dim, map->n, 0);
7148 if (!result)
7149 goto error;
7150 for (i = 0; i < map->n; ++i)
7151 result = isl_set_add_basic_set(result,
7152 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7153 isl_map_free(map);
7154 return result;
7155 error:
7156 isl_map_free(map);
7157 return NULL;
7161 * returns [domain -> range] -> range - domain
7163 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7164 __isl_take isl_basic_map *bmap)
7166 int i, k;
7167 isl_space *dim;
7168 isl_basic_map *domain;
7169 int nparam, n;
7170 unsigned total;
7172 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
7173 isl_die(bmap->ctx, isl_error_invalid,
7174 "domain and range don't match", goto error);
7176 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7177 n = isl_basic_map_dim(bmap, isl_dim_in);
7179 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7180 domain = isl_basic_map_universe(dim);
7182 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7183 bmap = isl_basic_map_apply_range(bmap, domain);
7184 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7186 total = isl_basic_map_total_dim(bmap);
7188 for (i = 0; i < n; ++i) {
7189 k = isl_basic_map_alloc_equality(bmap);
7190 if (k < 0)
7191 goto error;
7192 isl_seq_clr(bmap->eq[k], 1 + total);
7193 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7194 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7195 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7198 bmap = isl_basic_map_gauss(bmap, NULL);
7199 return isl_basic_map_finalize(bmap);
7200 error:
7201 isl_basic_map_free(bmap);
7202 return NULL;
7206 * returns [domain -> range] -> range - domain
7208 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7210 int i;
7211 isl_space *domain_dim;
7213 if (!map)
7214 return NULL;
7216 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
7217 isl_die(map->ctx, isl_error_invalid,
7218 "domain and range don't match", goto error);
7220 map = isl_map_cow(map);
7221 if (!map)
7222 return NULL;
7224 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7225 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7226 map->dim = isl_space_join(map->dim, domain_dim);
7227 if (!map->dim)
7228 goto error;
7229 for (i = 0; i < map->n; ++i) {
7230 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7231 if (!map->p[i])
7232 goto error;
7234 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7235 return map;
7236 error:
7237 isl_map_free(map);
7238 return NULL;
7241 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7243 struct isl_basic_map *bmap;
7244 unsigned nparam;
7245 unsigned dim;
7246 int i;
7248 if (!dims)
7249 return NULL;
7251 nparam = dims->nparam;
7252 dim = dims->n_out;
7253 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7254 if (!bmap)
7255 goto error;
7257 for (i = 0; i < dim; ++i) {
7258 int j = isl_basic_map_alloc_equality(bmap);
7259 if (j < 0)
7260 goto error;
7261 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7262 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7263 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7265 return isl_basic_map_finalize(bmap);
7266 error:
7267 isl_basic_map_free(bmap);
7268 return NULL;
7271 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7273 if (!dim)
7274 return NULL;
7275 if (dim->n_in != dim->n_out)
7276 isl_die(dim->ctx, isl_error_invalid,
7277 "number of input and output dimensions needs to be "
7278 "the same", goto error);
7279 return basic_map_identity(dim);
7280 error:
7281 isl_space_free(dim);
7282 return NULL;
7285 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7287 if (!model || !model->dim)
7288 return NULL;
7289 return isl_basic_map_identity(isl_space_copy(model->dim));
7292 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7294 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7297 struct isl_map *isl_map_identity_like(struct isl_map *model)
7299 if (!model || !model->dim)
7300 return NULL;
7301 return isl_map_identity(isl_space_copy(model->dim));
7304 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7306 if (!model || !model->dim)
7307 return NULL;
7308 return isl_map_identity(isl_space_copy(model->dim));
7311 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7313 isl_space *dim = isl_set_get_space(set);
7314 isl_map *id;
7315 id = isl_map_identity(isl_space_map_from_set(dim));
7316 return isl_map_intersect_range(id, set);
7319 /* Construct a basic set with all set dimensions having only non-negative
7320 * values.
7322 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7323 __isl_take isl_space *space)
7325 int i;
7326 unsigned nparam;
7327 unsigned dim;
7328 struct isl_basic_set *bset;
7330 if (!space)
7331 return NULL;
7332 nparam = space->nparam;
7333 dim = space->n_out;
7334 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7335 if (!bset)
7336 return NULL;
7337 for (i = 0; i < dim; ++i) {
7338 int k = isl_basic_set_alloc_inequality(bset);
7339 if (k < 0)
7340 goto error;
7341 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7342 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7344 return bset;
7345 error:
7346 isl_basic_set_free(bset);
7347 return NULL;
7350 /* Construct the half-space x_pos >= 0.
7352 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7353 int pos)
7355 int k;
7356 isl_basic_set *nonneg;
7358 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7359 k = isl_basic_set_alloc_inequality(nonneg);
7360 if (k < 0)
7361 goto error;
7362 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7363 isl_int_set_si(nonneg->ineq[k][pos], 1);
7365 return isl_basic_set_finalize(nonneg);
7366 error:
7367 isl_basic_set_free(nonneg);
7368 return NULL;
7371 /* Construct the half-space x_pos <= -1.
7373 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7375 int k;
7376 isl_basic_set *neg;
7378 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7379 k = isl_basic_set_alloc_inequality(neg);
7380 if (k < 0)
7381 goto error;
7382 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7383 isl_int_set_si(neg->ineq[k][0], -1);
7384 isl_int_set_si(neg->ineq[k][pos], -1);
7386 return isl_basic_set_finalize(neg);
7387 error:
7388 isl_basic_set_free(neg);
7389 return NULL;
7392 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7393 enum isl_dim_type type, unsigned first, unsigned n)
7395 int i;
7396 isl_basic_set *nonneg;
7397 isl_basic_set *neg;
7399 if (!set)
7400 return NULL;
7401 if (n == 0)
7402 return set;
7404 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7406 for (i = 0; i < n; ++i) {
7407 nonneg = nonneg_halfspace(isl_set_get_space(set),
7408 pos(set->dim, type) + first + i);
7409 neg = neg_halfspace(isl_set_get_space(set),
7410 pos(set->dim, type) + first + i);
7412 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7415 return set;
7416 error:
7417 isl_set_free(set);
7418 return NULL;
7421 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7422 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7423 void *user)
7425 isl_set *half;
7427 if (!set)
7428 return -1;
7429 if (isl_set_plain_is_empty(set)) {
7430 isl_set_free(set);
7431 return 0;
7433 if (first == len)
7434 return fn(set, signs, user);
7436 signs[first] = 1;
7437 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7438 1 + first));
7439 half = isl_set_intersect(half, isl_set_copy(set));
7440 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7441 goto error;
7443 signs[first] = -1;
7444 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7445 1 + first));
7446 half = isl_set_intersect(half, set);
7447 return foreach_orthant(half, signs, first + 1, len, fn, user);
7448 error:
7449 isl_set_free(set);
7450 return -1;
7453 /* Call "fn" on the intersections of "set" with each of the orthants
7454 * (except for obviously empty intersections). The orthant is identified
7455 * by the signs array, with each entry having value 1 or -1 according
7456 * to the sign of the corresponding variable.
7458 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7459 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7460 void *user)
7462 unsigned nparam;
7463 unsigned nvar;
7464 int *signs;
7465 int r;
7467 if (!set)
7468 return -1;
7469 if (isl_set_plain_is_empty(set))
7470 return 0;
7472 nparam = isl_set_dim(set, isl_dim_param);
7473 nvar = isl_set_dim(set, isl_dim_set);
7475 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7477 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7478 fn, user);
7480 free(signs);
7482 return r;
7485 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7487 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7490 int isl_basic_map_is_subset(
7491 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7493 int is_subset;
7494 struct isl_map *map1;
7495 struct isl_map *map2;
7497 if (!bmap1 || !bmap2)
7498 return -1;
7500 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7501 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7503 is_subset = isl_map_is_subset(map1, map2);
7505 isl_map_free(map1);
7506 isl_map_free(map2);
7508 return is_subset;
7511 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7512 __isl_keep isl_basic_set *bset2)
7514 return isl_basic_map_is_subset(bset1, bset2);
7517 int isl_basic_map_is_equal(
7518 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7520 int is_subset;
7522 if (!bmap1 || !bmap2)
7523 return -1;
7524 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7525 if (is_subset != 1)
7526 return is_subset;
7527 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7528 return is_subset;
7531 int isl_basic_set_is_equal(
7532 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7534 return isl_basic_map_is_equal(
7535 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7538 int isl_map_is_empty(struct isl_map *map)
7540 int i;
7541 int is_empty;
7543 if (!map)
7544 return -1;
7545 for (i = 0; i < map->n; ++i) {
7546 is_empty = isl_basic_map_is_empty(map->p[i]);
7547 if (is_empty < 0)
7548 return -1;
7549 if (!is_empty)
7550 return 0;
7552 return 1;
7555 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7557 return map ? map->n == 0 : -1;
7560 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7562 return isl_map_plain_is_empty(map);
7565 int isl_set_plain_is_empty(struct isl_set *set)
7567 return set ? set->n == 0 : -1;
7570 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7572 return isl_set_plain_is_empty(set);
7575 int isl_set_is_empty(struct isl_set *set)
7577 return isl_map_is_empty((struct isl_map *)set);
7580 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7582 if (!map1 || !map2)
7583 return -1;
7585 return isl_space_is_equal(map1->dim, map2->dim);
7588 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7590 if (!set1 || !set2)
7591 return -1;
7593 return isl_space_is_equal(set1->dim, set2->dim);
7596 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7598 int is_subset;
7600 if (!map1 || !map2)
7601 return -1;
7602 is_subset = isl_map_is_subset(map1, map2);
7603 if (is_subset != 1)
7604 return is_subset;
7605 is_subset = isl_map_is_subset(map2, map1);
7606 return is_subset;
7609 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7611 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7614 int isl_basic_map_is_strict_subset(
7615 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7617 int is_subset;
7619 if (!bmap1 || !bmap2)
7620 return -1;
7621 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7622 if (is_subset != 1)
7623 return is_subset;
7624 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7625 if (is_subset == -1)
7626 return is_subset;
7627 return !is_subset;
7630 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7632 int is_subset;
7634 if (!map1 || !map2)
7635 return -1;
7636 is_subset = isl_map_is_subset(map1, map2);
7637 if (is_subset != 1)
7638 return is_subset;
7639 is_subset = isl_map_is_subset(map2, map1);
7640 if (is_subset == -1)
7641 return is_subset;
7642 return !is_subset;
7645 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7647 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7650 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7652 if (!bmap)
7653 return -1;
7654 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7657 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7659 if (!bset)
7660 return -1;
7661 return bset->n_eq == 0 && bset->n_ineq == 0;
7664 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7666 int i;
7668 if (!map)
7669 return -1;
7671 for (i = 0; i < map->n; ++i) {
7672 int r = isl_basic_map_is_universe(map->p[i]);
7673 if (r < 0 || r)
7674 return r;
7677 return 0;
7680 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7682 return isl_map_plain_is_universe((isl_map *) set);
7685 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7687 return isl_set_plain_is_universe(set);
7690 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7692 struct isl_basic_set *bset = NULL;
7693 struct isl_vec *sample = NULL;
7694 int empty;
7695 unsigned total;
7697 if (!bmap)
7698 return -1;
7700 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7701 return 1;
7703 if (isl_basic_map_is_universe(bmap))
7704 return 0;
7706 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7707 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7708 copy = isl_basic_map_remove_redundancies(copy);
7709 empty = isl_basic_map_plain_is_empty(copy);
7710 isl_basic_map_free(copy);
7711 return empty;
7714 total = 1 + isl_basic_map_total_dim(bmap);
7715 if (bmap->sample && bmap->sample->size == total) {
7716 int contains = isl_basic_map_contains(bmap, bmap->sample);
7717 if (contains < 0)
7718 return -1;
7719 if (contains)
7720 return 0;
7722 isl_vec_free(bmap->sample);
7723 bmap->sample = NULL;
7724 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7725 if (!bset)
7726 return -1;
7727 sample = isl_basic_set_sample_vec(bset);
7728 if (!sample)
7729 return -1;
7730 empty = sample->size == 0;
7731 isl_vec_free(bmap->sample);
7732 bmap->sample = sample;
7733 if (empty)
7734 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7736 return empty;
7739 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7741 if (!bmap)
7742 return -1;
7743 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7746 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7748 return isl_basic_map_plain_is_empty(bmap);
7751 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7753 if (!bset)
7754 return -1;
7755 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7758 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7760 return isl_basic_set_plain_is_empty(bset);
7763 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7765 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7768 struct isl_map *isl_basic_map_union(
7769 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7771 struct isl_map *map;
7772 if (!bmap1 || !bmap2)
7773 goto error;
7775 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7777 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7778 if (!map)
7779 goto error;
7780 map = isl_map_add_basic_map(map, bmap1);
7781 map = isl_map_add_basic_map(map, bmap2);
7782 return map;
7783 error:
7784 isl_basic_map_free(bmap1);
7785 isl_basic_map_free(bmap2);
7786 return NULL;
7789 struct isl_set *isl_basic_set_union(
7790 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7792 return (struct isl_set *)isl_basic_map_union(
7793 (struct isl_basic_map *)bset1,
7794 (struct isl_basic_map *)bset2);
7797 /* Order divs such that any div only depends on previous divs */
7798 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7800 int i;
7801 unsigned off;
7803 if (!bmap)
7804 return NULL;
7806 off = isl_space_dim(bmap->dim, isl_dim_all);
7808 for (i = 0; i < bmap->n_div; ++i) {
7809 int pos;
7810 if (isl_int_is_zero(bmap->div[i][0]))
7811 continue;
7812 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7813 bmap->n_div-i);
7814 if (pos == -1)
7815 continue;
7816 isl_basic_map_swap_div(bmap, i, i + pos);
7817 --i;
7819 return bmap;
7822 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7824 return (struct isl_basic_set *)
7825 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7828 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7830 int i;
7832 if (!map)
7833 return 0;
7835 for (i = 0; i < map->n; ++i) {
7836 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7837 if (!map->p[i])
7838 goto error;
7841 return map;
7842 error:
7843 isl_map_free(map);
7844 return NULL;
7847 /* Apply the expansion computed by isl_merge_divs.
7848 * The expansion itself is given by "exp" while the resulting
7849 * list of divs is given by "div".
7851 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7852 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7854 int i, j;
7855 int n_div;
7857 bset = isl_basic_set_cow(bset);
7858 if (!bset || !div)
7859 goto error;
7861 if (div->n_row < bset->n_div)
7862 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7863 "not an expansion", goto error);
7865 n_div = bset->n_div;
7866 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
7867 div->n_row - n_div, 0,
7868 2 * (div->n_row - n_div));
7870 for (i = n_div; i < div->n_row; ++i)
7871 if (isl_basic_set_alloc_div(bset) < 0)
7872 goto error;
7874 j = n_div - 1;
7875 for (i = div->n_row - 1; i >= 0; --i) {
7876 if (j >= 0 && exp[j] == i) {
7877 if (i != j)
7878 isl_basic_map_swap_div(bset, i, j);
7879 j--;
7880 } else {
7881 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
7882 if (isl_basic_map_add_div_constraints(bset, i) < 0)
7883 goto error;
7887 isl_mat_free(div);
7888 return bset;
7889 error:
7890 isl_basic_set_free(bset);
7891 isl_mat_free(div);
7892 return NULL;
7895 /* Look for a div in dst that corresponds to the div "div" in src.
7896 * The divs before "div" in src and dst are assumed to be the same.
7898 * Returns -1 if no corresponding div was found and the position
7899 * of the corresponding div in dst otherwise.
7901 static int find_div(struct isl_basic_map *dst,
7902 struct isl_basic_map *src, unsigned div)
7904 int i;
7906 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7908 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7909 for (i = div; i < dst->n_div; ++i)
7910 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7911 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7912 dst->n_div - div) == -1)
7913 return i;
7914 return -1;
7917 struct isl_basic_map *isl_basic_map_align_divs(
7918 struct isl_basic_map *dst, struct isl_basic_map *src)
7920 int i;
7921 unsigned total;
7923 if (!dst || !src)
7924 goto error;
7926 if (src->n_div == 0)
7927 return dst;
7929 for (i = 0; i < src->n_div; ++i)
7930 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7932 src = isl_basic_map_order_divs(src);
7933 dst = isl_basic_map_cow(dst);
7934 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7935 src->n_div, 0, 2 * src->n_div);
7936 if (!dst)
7937 return NULL;
7938 total = isl_space_dim(src->dim, isl_dim_all);
7939 for (i = 0; i < src->n_div; ++i) {
7940 int j = find_div(dst, src, i);
7941 if (j < 0) {
7942 j = isl_basic_map_alloc_div(dst);
7943 if (j < 0)
7944 goto error;
7945 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7946 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7947 if (isl_basic_map_add_div_constraints(dst, j) < 0)
7948 goto error;
7950 if (j != i)
7951 isl_basic_map_swap_div(dst, i, j);
7953 return dst;
7954 error:
7955 isl_basic_map_free(dst);
7956 return NULL;
7959 struct isl_basic_set *isl_basic_set_align_divs(
7960 struct isl_basic_set *dst, struct isl_basic_set *src)
7962 return (struct isl_basic_set *)isl_basic_map_align_divs(
7963 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7966 struct isl_map *isl_map_align_divs(struct isl_map *map)
7968 int i;
7970 if (!map)
7971 return NULL;
7972 if (map->n == 0)
7973 return map;
7974 map = isl_map_compute_divs(map);
7975 map = isl_map_cow(map);
7976 if (!map)
7977 return NULL;
7979 for (i = 1; i < map->n; ++i)
7980 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7981 for (i = 1; i < map->n; ++i)
7982 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7984 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7985 return map;
7988 struct isl_set *isl_set_align_divs(struct isl_set *set)
7990 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7993 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7994 __isl_take isl_map *map)
7996 if (!set || !map)
7997 goto error;
7998 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7999 map = isl_map_intersect_domain(map, set);
8000 set = isl_map_range(map);
8001 return set;
8002 error:
8003 isl_set_free(set);
8004 isl_map_free(map);
8005 return NULL;
8008 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8009 __isl_take isl_map *map)
8011 return isl_map_align_params_map_map_and(set, map, &set_apply);
8014 /* There is no need to cow as removing empty parts doesn't change
8015 * the meaning of the set.
8017 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8019 int i;
8021 if (!map)
8022 return NULL;
8024 for (i = map->n - 1; i >= 0; --i)
8025 remove_if_empty(map, i);
8027 return map;
8030 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8032 return (struct isl_set *)
8033 isl_map_remove_empty_parts((struct isl_map *)set);
8036 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8038 struct isl_basic_map *bmap;
8039 if (!map || map->n == 0)
8040 return NULL;
8041 bmap = map->p[map->n-1];
8042 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8043 return isl_basic_map_copy(bmap);
8046 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8048 return (struct isl_basic_set *)
8049 isl_map_copy_basic_map((struct isl_map *)set);
8052 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8053 __isl_keep isl_basic_map *bmap)
8055 int i;
8057 if (!map || !bmap)
8058 goto error;
8059 for (i = map->n-1; i >= 0; --i) {
8060 if (map->p[i] != bmap)
8061 continue;
8062 map = isl_map_cow(map);
8063 if (!map)
8064 goto error;
8065 isl_basic_map_free(map->p[i]);
8066 if (i != map->n-1) {
8067 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8068 map->p[i] = map->p[map->n-1];
8070 map->n--;
8071 return map;
8073 return map;
8074 error:
8075 isl_map_free(map);
8076 return NULL;
8079 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8080 struct isl_basic_set *bset)
8082 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8083 (struct isl_basic_map *)bset);
8086 /* Given two basic sets bset1 and bset2, compute the maximal difference
8087 * between the values of dimension pos in bset1 and those in bset2
8088 * for any common value of the parameters and dimensions preceding pos.
8090 static enum isl_lp_result basic_set_maximal_difference_at(
8091 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8092 int pos, isl_int *opt)
8094 isl_space *dims;
8095 struct isl_basic_map *bmap1 = NULL;
8096 struct isl_basic_map *bmap2 = NULL;
8097 struct isl_ctx *ctx;
8098 struct isl_vec *obj;
8099 unsigned total;
8100 unsigned nparam;
8101 unsigned dim1, dim2;
8102 enum isl_lp_result res;
8104 if (!bset1 || !bset2)
8105 return isl_lp_error;
8107 nparam = isl_basic_set_n_param(bset1);
8108 dim1 = isl_basic_set_n_dim(bset1);
8109 dim2 = isl_basic_set_n_dim(bset2);
8110 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8111 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8112 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8113 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8114 if (!bmap1 || !bmap2)
8115 goto error;
8116 bmap1 = isl_basic_map_cow(bmap1);
8117 bmap1 = isl_basic_map_extend(bmap1, nparam,
8118 pos, (dim1 - pos) + (dim2 - pos),
8119 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8120 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8121 if (!bmap1)
8122 goto error;
8123 total = isl_basic_map_total_dim(bmap1);
8124 ctx = bmap1->ctx;
8125 obj = isl_vec_alloc(ctx, 1 + total);
8126 if (!obj)
8127 goto error2;
8128 isl_seq_clr(obj->block.data, 1 + total);
8129 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8130 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8131 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8132 opt, NULL, NULL);
8133 isl_basic_map_free(bmap1);
8134 isl_vec_free(obj);
8135 return res;
8136 error:
8137 isl_basic_map_free(bmap2);
8138 error2:
8139 isl_basic_map_free(bmap1);
8140 return isl_lp_error;
8143 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8144 * for any common value of the parameters and dimensions preceding pos
8145 * in both basic sets, the values of dimension pos in bset1 are
8146 * smaller or larger than those in bset2.
8148 * Returns
8149 * 1 if bset1 follows bset2
8150 * -1 if bset1 precedes bset2
8151 * 0 if bset1 and bset2 are incomparable
8152 * -2 if some error occurred.
8154 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8155 struct isl_basic_set *bset2, int pos)
8157 isl_int opt;
8158 enum isl_lp_result res;
8159 int cmp;
8161 isl_int_init(opt);
8163 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8165 if (res == isl_lp_empty)
8166 cmp = 0;
8167 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8168 res == isl_lp_unbounded)
8169 cmp = 1;
8170 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8171 cmp = -1;
8172 else
8173 cmp = -2;
8175 isl_int_clear(opt);
8176 return cmp;
8179 /* Given two basic sets bset1 and bset2, check whether
8180 * for any common value of the parameters and dimensions preceding pos
8181 * there is a value of dimension pos in bset1 that is larger
8182 * than a value of the same dimension in bset2.
8184 * Return
8185 * 1 if there exists such a pair
8186 * 0 if there is no such pair, but there is a pair of equal values
8187 * -1 otherwise
8188 * -2 if some error occurred.
8190 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8191 __isl_keep isl_basic_set *bset2, int pos)
8193 isl_int opt;
8194 enum isl_lp_result res;
8195 int cmp;
8197 isl_int_init(opt);
8199 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8201 if (res == isl_lp_empty)
8202 cmp = -1;
8203 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8204 res == isl_lp_unbounded)
8205 cmp = 1;
8206 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8207 cmp = -1;
8208 else if (res == isl_lp_ok)
8209 cmp = 0;
8210 else
8211 cmp = -2;
8213 isl_int_clear(opt);
8214 return cmp;
8217 /* Given two sets set1 and set2, check whether
8218 * for any common value of the parameters and dimensions preceding pos
8219 * there is a value of dimension pos in set1 that is larger
8220 * than a value of the same dimension in set2.
8222 * Return
8223 * 1 if there exists such a pair
8224 * 0 if there is no such pair, but there is a pair of equal values
8225 * -1 otherwise
8226 * -2 if some error occurred.
8228 int isl_set_follows_at(__isl_keep isl_set *set1,
8229 __isl_keep isl_set *set2, int pos)
8231 int i, j;
8232 int follows = -1;
8234 if (!set1 || !set2)
8235 return -2;
8237 for (i = 0; i < set1->n; ++i)
8238 for (j = 0; j < set2->n; ++j) {
8239 int f;
8240 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8241 if (f == 1 || f == -2)
8242 return f;
8243 if (f > follows)
8244 follows = f;
8247 return follows;
8250 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8251 unsigned pos, isl_int *val)
8253 int i;
8254 int d;
8255 unsigned total;
8257 if (!bmap)
8258 return -1;
8259 total = isl_basic_map_total_dim(bmap);
8260 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8261 for (; d+1 > pos; --d)
8262 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8263 break;
8264 if (d != pos)
8265 continue;
8266 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8267 return 0;
8268 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8269 return 0;
8270 if (!isl_int_is_one(bmap->eq[i][1+d]))
8271 return 0;
8272 if (val)
8273 isl_int_neg(*val, bmap->eq[i][0]);
8274 return 1;
8276 return 0;
8279 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8280 unsigned pos, isl_int *val)
8282 int i;
8283 isl_int v;
8284 isl_int tmp;
8285 int fixed;
8287 if (!map)
8288 return -1;
8289 if (map->n == 0)
8290 return 0;
8291 if (map->n == 1)
8292 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8293 isl_int_init(v);
8294 isl_int_init(tmp);
8295 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8296 for (i = 1; fixed == 1 && i < map->n; ++i) {
8297 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8298 if (fixed == 1 && isl_int_ne(tmp, v))
8299 fixed = 0;
8301 if (val)
8302 isl_int_set(*val, v);
8303 isl_int_clear(tmp);
8304 isl_int_clear(v);
8305 return fixed;
8308 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8309 unsigned pos, isl_int *val)
8311 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8312 pos, val);
8315 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8316 isl_int *val)
8318 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8321 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8322 enum isl_dim_type type, unsigned pos, isl_int *val)
8324 if (pos >= isl_basic_map_dim(bmap, type))
8325 return -1;
8326 return isl_basic_map_plain_has_fixed_var(bmap,
8327 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8330 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8331 enum isl_dim_type type, unsigned pos, isl_int *val)
8333 if (pos >= isl_map_dim(map, type))
8334 return -1;
8335 return isl_map_plain_has_fixed_var(map,
8336 map_offset(map, type) - 1 + pos, val);
8339 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8340 enum isl_dim_type type, unsigned pos, isl_int *val)
8342 return isl_map_plain_is_fixed(set, type, pos, val);
8345 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8346 enum isl_dim_type type, unsigned pos, isl_int *val)
8348 return isl_map_plain_is_fixed(map, type, pos, val);
8351 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8352 * then return this fixed value in *val.
8354 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8355 unsigned dim, isl_int *val)
8357 return isl_basic_set_plain_has_fixed_var(bset,
8358 isl_basic_set_n_param(bset) + dim, val);
8361 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8362 * then return this fixed value in *val.
8364 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8365 unsigned dim, isl_int *val)
8367 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8370 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8371 unsigned dim, isl_int *val)
8373 return isl_set_plain_dim_is_fixed(set, dim, val);
8376 /* Check if input variable in has fixed value and if so and if val is not NULL,
8377 * then return this fixed value in *val.
8379 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8380 unsigned in, isl_int *val)
8382 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8385 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8386 * and if val is not NULL, then return this lower bound in *val.
8388 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8389 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8391 int i, i_eq = -1, i_ineq = -1;
8392 isl_int *c;
8393 unsigned total;
8394 unsigned nparam;
8396 if (!bset)
8397 return -1;
8398 total = isl_basic_set_total_dim(bset);
8399 nparam = isl_basic_set_n_param(bset);
8400 for (i = 0; i < bset->n_eq; ++i) {
8401 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8402 continue;
8403 if (i_eq != -1)
8404 return 0;
8405 i_eq = i;
8407 for (i = 0; i < bset->n_ineq; ++i) {
8408 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8409 continue;
8410 if (i_eq != -1 || i_ineq != -1)
8411 return 0;
8412 i_ineq = i;
8414 if (i_eq == -1 && i_ineq == -1)
8415 return 0;
8416 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8417 /* The coefficient should always be one due to normalization. */
8418 if (!isl_int_is_one(c[1+nparam+dim]))
8419 return 0;
8420 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8421 return 0;
8422 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8423 total - nparam - dim - 1) != -1)
8424 return 0;
8425 if (val)
8426 isl_int_neg(*val, c[0]);
8427 return 1;
8430 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8431 unsigned dim, isl_int *val)
8433 int i;
8434 isl_int v;
8435 isl_int tmp;
8436 int fixed;
8438 if (!set)
8439 return -1;
8440 if (set->n == 0)
8441 return 0;
8442 if (set->n == 1)
8443 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8444 dim, val);
8445 isl_int_init(v);
8446 isl_int_init(tmp);
8447 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8448 dim, &v);
8449 for (i = 1; fixed == 1 && i < set->n; ++i) {
8450 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8451 dim, &tmp);
8452 if (fixed == 1 && isl_int_ne(tmp, v))
8453 fixed = 0;
8455 if (val)
8456 isl_int_set(*val, v);
8457 isl_int_clear(tmp);
8458 isl_int_clear(v);
8459 return fixed;
8462 struct constraint {
8463 unsigned size;
8464 isl_int *c;
8467 /* uset_gist depends on constraints without existentially quantified
8468 * variables sorting first.
8470 static int qsort_constraint_cmp(const void *p1, const void *p2)
8472 const struct constraint *c1 = (const struct constraint *)p1;
8473 const struct constraint *c2 = (const struct constraint *)p2;
8474 int l1, l2;
8475 unsigned size = isl_min(c1->size, c2->size);
8477 l1 = isl_seq_last_non_zero(c1->c + 1, size);
8478 l2 = isl_seq_last_non_zero(c2->c + 1, size);
8480 if (l1 != l2)
8481 return l1 - l2;
8483 return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8486 static struct isl_basic_map *isl_basic_map_sort_constraints(
8487 struct isl_basic_map *bmap)
8489 int i;
8490 struct constraint *c;
8491 unsigned total;
8493 if (!bmap)
8494 return NULL;
8495 total = isl_basic_map_total_dim(bmap);
8496 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8497 if (!c)
8498 goto error;
8499 for (i = 0; i < bmap->n_ineq; ++i) {
8500 c[i].size = total;
8501 c[i].c = bmap->ineq[i];
8503 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8504 for (i = 0; i < bmap->n_ineq; ++i)
8505 bmap->ineq[i] = c[i].c;
8506 free(c);
8507 return bmap;
8508 error:
8509 isl_basic_map_free(bmap);
8510 return NULL;
8513 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8514 __isl_take isl_basic_set *bset)
8516 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8517 (struct isl_basic_map *)bset);
8520 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8522 if (!bmap)
8523 return NULL;
8524 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8525 return bmap;
8526 bmap = isl_basic_map_remove_redundancies(bmap);
8527 bmap = isl_basic_map_sort_constraints(bmap);
8528 if (bmap)
8529 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8530 return bmap;
8533 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8535 return (struct isl_basic_set *)isl_basic_map_normalize(
8536 (struct isl_basic_map *)bset);
8539 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8540 const __isl_keep isl_basic_map *bmap2)
8542 int i, cmp;
8543 unsigned total;
8545 if (bmap1 == bmap2)
8546 return 0;
8547 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8548 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8549 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8550 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8551 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8552 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8553 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8554 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8555 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8556 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8557 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8558 return 0;
8559 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8560 return 1;
8561 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8562 return -1;
8563 if (bmap1->n_eq != bmap2->n_eq)
8564 return bmap1->n_eq - bmap2->n_eq;
8565 if (bmap1->n_ineq != bmap2->n_ineq)
8566 return bmap1->n_ineq - bmap2->n_ineq;
8567 if (bmap1->n_div != bmap2->n_div)
8568 return bmap1->n_div - bmap2->n_div;
8569 total = isl_basic_map_total_dim(bmap1);
8570 for (i = 0; i < bmap1->n_eq; ++i) {
8571 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8572 if (cmp)
8573 return cmp;
8575 for (i = 0; i < bmap1->n_ineq; ++i) {
8576 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8577 if (cmp)
8578 return cmp;
8580 for (i = 0; i < bmap1->n_div; ++i) {
8581 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8582 if (cmp)
8583 return cmp;
8585 return 0;
8588 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8589 const __isl_keep isl_basic_set *bset2)
8591 return isl_basic_map_plain_cmp(bset1, bset2);
8594 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8596 int i, cmp;
8598 if (set1 == set2)
8599 return 0;
8600 if (set1->n != set2->n)
8601 return set1->n - set2->n;
8603 for (i = 0; i < set1->n; ++i) {
8604 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8605 if (cmp)
8606 return cmp;
8609 return 0;
8612 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8613 __isl_keep isl_basic_map *bmap2)
8615 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8618 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8619 __isl_keep isl_basic_set *bset2)
8621 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8622 (isl_basic_map *)bset2);
8625 static int qsort_bmap_cmp(const void *p1, const void *p2)
8627 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8628 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8630 return isl_basic_map_plain_cmp(bmap1, bmap2);
8633 /* We normalize in place, but if anything goes wrong we need
8634 * to return NULL, so we need to make sure we don't change the
8635 * meaning of any possible other copies of map.
8637 struct isl_map *isl_map_normalize(struct isl_map *map)
8639 int i, j;
8640 struct isl_basic_map *bmap;
8642 if (!map)
8643 return NULL;
8644 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8645 return map;
8646 for (i = 0; i < map->n; ++i) {
8647 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8648 if (!bmap)
8649 goto error;
8650 isl_basic_map_free(map->p[i]);
8651 map->p[i] = bmap;
8653 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8654 ISL_F_SET(map, ISL_MAP_NORMALIZED);
8655 map = isl_map_remove_empty_parts(map);
8656 if (!map)
8657 return NULL;
8658 for (i = map->n - 1; i >= 1; --i) {
8659 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8660 continue;
8661 isl_basic_map_free(map->p[i-1]);
8662 for (j = i; j < map->n; ++j)
8663 map->p[j-1] = map->p[j];
8664 map->n--;
8666 return map;
8667 error:
8668 isl_map_free(map);
8669 return NULL;
8673 struct isl_set *isl_set_normalize(struct isl_set *set)
8675 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8678 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8680 int i;
8681 int equal;
8683 if (!map1 || !map2)
8684 return -1;
8686 if (map1 == map2)
8687 return 1;
8688 if (!isl_space_is_equal(map1->dim, map2->dim))
8689 return 0;
8691 map1 = isl_map_copy(map1);
8692 map2 = isl_map_copy(map2);
8693 map1 = isl_map_normalize(map1);
8694 map2 = isl_map_normalize(map2);
8695 if (!map1 || !map2)
8696 goto error;
8697 equal = map1->n == map2->n;
8698 for (i = 0; equal && i < map1->n; ++i) {
8699 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8700 if (equal < 0)
8701 goto error;
8703 isl_map_free(map1);
8704 isl_map_free(map2);
8705 return equal;
8706 error:
8707 isl_map_free(map1);
8708 isl_map_free(map2);
8709 return -1;
8712 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8714 return isl_map_plain_is_equal(map1, map2);
8717 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8719 return isl_map_plain_is_equal((struct isl_map *)set1,
8720 (struct isl_map *)set2);
8723 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8725 return isl_set_plain_is_equal(set1, set2);
8728 /* Return an interval that ranges from min to max (inclusive)
8730 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8731 isl_int min, isl_int max)
8733 int k;
8734 struct isl_basic_set *bset = NULL;
8736 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8737 if (!bset)
8738 goto error;
8740 k = isl_basic_set_alloc_inequality(bset);
8741 if (k < 0)
8742 goto error;
8743 isl_int_set_si(bset->ineq[k][1], 1);
8744 isl_int_neg(bset->ineq[k][0], min);
8746 k = isl_basic_set_alloc_inequality(bset);
8747 if (k < 0)
8748 goto error;
8749 isl_int_set_si(bset->ineq[k][1], -1);
8750 isl_int_set(bset->ineq[k][0], max);
8752 return bset;
8753 error:
8754 isl_basic_set_free(bset);
8755 return NULL;
8758 /* Return the Cartesian product of the basic sets in list (in the given order).
8760 __isl_give isl_basic_set *isl_basic_set_list_product(
8761 __isl_take struct isl_basic_set_list *list)
8763 int i;
8764 unsigned dim;
8765 unsigned nparam;
8766 unsigned extra;
8767 unsigned n_eq;
8768 unsigned n_ineq;
8769 struct isl_basic_set *product = NULL;
8771 if (!list)
8772 goto error;
8773 isl_assert(list->ctx, list->n > 0, goto error);
8774 isl_assert(list->ctx, list->p[0], goto error);
8775 nparam = isl_basic_set_n_param(list->p[0]);
8776 dim = isl_basic_set_n_dim(list->p[0]);
8777 extra = list->p[0]->n_div;
8778 n_eq = list->p[0]->n_eq;
8779 n_ineq = list->p[0]->n_ineq;
8780 for (i = 1; i < list->n; ++i) {
8781 isl_assert(list->ctx, list->p[i], goto error);
8782 isl_assert(list->ctx,
8783 nparam == isl_basic_set_n_param(list->p[i]), goto error);
8784 dim += isl_basic_set_n_dim(list->p[i]);
8785 extra += list->p[i]->n_div;
8786 n_eq += list->p[i]->n_eq;
8787 n_ineq += list->p[i]->n_ineq;
8789 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
8790 n_eq, n_ineq);
8791 if (!product)
8792 goto error;
8793 dim = 0;
8794 for (i = 0; i < list->n; ++i) {
8795 isl_basic_set_add_constraints(product,
8796 isl_basic_set_copy(list->p[i]), dim);
8797 dim += isl_basic_set_n_dim(list->p[i]);
8799 isl_basic_set_list_free(list);
8800 return product;
8801 error:
8802 isl_basic_set_free(product);
8803 isl_basic_set_list_free(list);
8804 return NULL;
8807 struct isl_basic_map *isl_basic_map_product(
8808 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8810 isl_space *dim_result = NULL;
8811 struct isl_basic_map *bmap;
8812 unsigned in1, in2, out1, out2, nparam, total, pos;
8813 struct isl_dim_map *dim_map1, *dim_map2;
8815 if (!bmap1 || !bmap2)
8816 goto error;
8818 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
8819 bmap2->dim, isl_dim_param), goto error);
8820 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
8821 isl_space_copy(bmap2->dim));
8823 in1 = isl_basic_map_n_in(bmap1);
8824 in2 = isl_basic_map_n_in(bmap2);
8825 out1 = isl_basic_map_n_out(bmap1);
8826 out2 = isl_basic_map_n_out(bmap2);
8827 nparam = isl_basic_map_n_param(bmap1);
8829 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
8830 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8831 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8832 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8833 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8834 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8835 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8836 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8837 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8838 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8839 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8841 bmap = isl_basic_map_alloc_space(dim_result,
8842 bmap1->n_div + bmap2->n_div,
8843 bmap1->n_eq + bmap2->n_eq,
8844 bmap1->n_ineq + bmap2->n_ineq);
8845 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8846 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8847 bmap = isl_basic_map_simplify(bmap);
8848 return isl_basic_map_finalize(bmap);
8849 error:
8850 isl_basic_map_free(bmap1);
8851 isl_basic_map_free(bmap2);
8852 return NULL;
8855 __isl_give isl_basic_map *isl_basic_map_flat_product(
8856 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8858 isl_basic_map *prod;
8860 prod = isl_basic_map_product(bmap1, bmap2);
8861 prod = isl_basic_map_flatten(prod);
8862 return prod;
8865 __isl_give isl_basic_set *isl_basic_set_flat_product(
8866 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
8868 return isl_basic_map_flat_range_product(bset1, bset2);
8871 __isl_give isl_basic_map *isl_basic_map_domain_product(
8872 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8874 isl_space *space_result = NULL;
8875 isl_basic_map *bmap;
8876 unsigned in1, in2, out, nparam, total, pos;
8877 struct isl_dim_map *dim_map1, *dim_map2;
8879 if (!bmap1 || !bmap2)
8880 goto error;
8882 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8883 isl_space_copy(bmap2->dim));
8885 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8886 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8887 out = isl_basic_map_dim(bmap1, isl_dim_out);
8888 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8890 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8891 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8892 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8893 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8894 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8895 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8896 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8897 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8898 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8899 isl_dim_map_div(dim_map1, bmap1, pos += out);
8900 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8902 bmap = isl_basic_map_alloc_space(space_result,
8903 bmap1->n_div + bmap2->n_div,
8904 bmap1->n_eq + bmap2->n_eq,
8905 bmap1->n_ineq + bmap2->n_ineq);
8906 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8907 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8908 bmap = isl_basic_map_simplify(bmap);
8909 return isl_basic_map_finalize(bmap);
8910 error:
8911 isl_basic_map_free(bmap1);
8912 isl_basic_map_free(bmap2);
8913 return NULL;
8916 __isl_give isl_basic_map *isl_basic_map_range_product(
8917 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8919 isl_space *dim_result = NULL;
8920 isl_basic_map *bmap;
8921 unsigned in, out1, out2, nparam, total, pos;
8922 struct isl_dim_map *dim_map1, *dim_map2;
8924 if (!bmap1 || !bmap2)
8925 goto error;
8927 if (!isl_space_match(bmap1->dim, isl_dim_param,
8928 bmap2->dim, isl_dim_param))
8929 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
8930 "parameters don't match", goto error);
8932 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8933 isl_space_copy(bmap2->dim));
8935 in = isl_basic_map_dim(bmap1, isl_dim_in);
8936 out1 = isl_basic_map_n_out(bmap1);
8937 out2 = isl_basic_map_n_out(bmap2);
8938 nparam = isl_basic_map_n_param(bmap1);
8940 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8941 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8942 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8943 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8944 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8945 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8946 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8947 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8948 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8949 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8950 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8952 bmap = isl_basic_map_alloc_space(dim_result,
8953 bmap1->n_div + bmap2->n_div,
8954 bmap1->n_eq + bmap2->n_eq,
8955 bmap1->n_ineq + bmap2->n_ineq);
8956 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8957 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8958 bmap = isl_basic_map_simplify(bmap);
8959 return isl_basic_map_finalize(bmap);
8960 error:
8961 isl_basic_map_free(bmap1);
8962 isl_basic_map_free(bmap2);
8963 return NULL;
8966 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8967 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8969 isl_basic_map *prod;
8971 prod = isl_basic_map_range_product(bmap1, bmap2);
8972 prod = isl_basic_map_flatten_range(prod);
8973 return prod;
8976 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8977 __isl_take isl_map *map2,
8978 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8979 __isl_take isl_space *right),
8980 __isl_give isl_basic_map *(*basic_map_product)(
8981 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8983 unsigned flags = 0;
8984 struct isl_map *result;
8985 int i, j;
8987 if (!map1 || !map2)
8988 goto error;
8990 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8991 map2->dim, isl_dim_param), goto error);
8993 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8994 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8995 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8997 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8998 isl_space_copy(map2->dim)),
8999 map1->n * map2->n, flags);
9000 if (!result)
9001 goto error;
9002 for (i = 0; i < map1->n; ++i)
9003 for (j = 0; j < map2->n; ++j) {
9004 struct isl_basic_map *part;
9005 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9006 isl_basic_map_copy(map2->p[j]));
9007 if (isl_basic_map_is_empty(part))
9008 isl_basic_map_free(part);
9009 else
9010 result = isl_map_add_basic_map(result, part);
9011 if (!result)
9012 goto error;
9014 isl_map_free(map1);
9015 isl_map_free(map2);
9016 return result;
9017 error:
9018 isl_map_free(map1);
9019 isl_map_free(map2);
9020 return NULL;
9023 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9025 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9026 __isl_take isl_map *map2)
9028 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
9031 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9032 __isl_take isl_map *map2)
9034 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9037 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9039 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9040 __isl_take isl_map *map2)
9042 isl_map *prod;
9044 prod = isl_map_product(map1, map2);
9045 prod = isl_map_flatten(prod);
9046 return prod;
9049 /* Given two set A and B, construct its Cartesian product A x B.
9051 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9053 return isl_map_range_product(set1, set2);
9056 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9057 __isl_take isl_set *set2)
9059 return isl_map_flat_range_product(set1, set2);
9062 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9064 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9065 __isl_take isl_map *map2)
9067 return map_product(map1, map2, &isl_space_domain_product,
9068 &isl_basic_map_domain_product);
9071 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9073 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9074 __isl_take isl_map *map2)
9076 return map_product(map1, map2, &isl_space_range_product,
9077 &isl_basic_map_range_product);
9080 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9081 __isl_take isl_map *map2)
9083 return isl_map_align_params_map_map_and(map1, map2,
9084 &map_domain_product_aligned);
9087 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9088 __isl_take isl_map *map2)
9090 return isl_map_align_params_map_map_and(map1, map2,
9091 &map_range_product_aligned);
9094 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9096 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9097 __isl_take isl_map *map2)
9099 isl_map *prod;
9101 prod = isl_map_domain_product(map1, map2);
9102 prod = isl_map_flatten_domain(prod);
9103 return prod;
9106 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9108 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9109 __isl_take isl_map *map2)
9111 isl_map *prod;
9113 prod = isl_map_range_product(map1, map2);
9114 prod = isl_map_flatten_range(prod);
9115 return prod;
9118 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9120 int i;
9121 uint32_t hash = isl_hash_init();
9122 unsigned total;
9124 if (!bmap)
9125 return 0;
9126 bmap = isl_basic_map_copy(bmap);
9127 bmap = isl_basic_map_normalize(bmap);
9128 if (!bmap)
9129 return 0;
9130 total = isl_basic_map_total_dim(bmap);
9131 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9132 for (i = 0; i < bmap->n_eq; ++i) {
9133 uint32_t c_hash;
9134 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9135 isl_hash_hash(hash, c_hash);
9137 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9138 for (i = 0; i < bmap->n_ineq; ++i) {
9139 uint32_t c_hash;
9140 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9141 isl_hash_hash(hash, c_hash);
9143 isl_hash_byte(hash, bmap->n_div & 0xFF);
9144 for (i = 0; i < bmap->n_div; ++i) {
9145 uint32_t c_hash;
9146 if (isl_int_is_zero(bmap->div[i][0]))
9147 continue;
9148 isl_hash_byte(hash, i & 0xFF);
9149 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9150 isl_hash_hash(hash, c_hash);
9152 isl_basic_map_free(bmap);
9153 return hash;
9156 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9158 return isl_basic_map_get_hash((isl_basic_map *)bset);
9161 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9163 int i;
9164 uint32_t hash;
9166 if (!map)
9167 return 0;
9168 map = isl_map_copy(map);
9169 map = isl_map_normalize(map);
9170 if (!map)
9171 return 0;
9173 hash = isl_hash_init();
9174 for (i = 0; i < map->n; ++i) {
9175 uint32_t bmap_hash;
9176 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9177 isl_hash_hash(hash, bmap_hash);
9180 isl_map_free(map);
9182 return hash;
9185 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9187 return isl_map_get_hash((isl_map *)set);
9190 /* Check if the value for dimension dim is completely determined
9191 * by the values of the other parameters and variables.
9192 * That is, check if dimension dim is involved in an equality.
9194 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9196 int i;
9197 unsigned nparam;
9199 if (!bset)
9200 return -1;
9201 nparam = isl_basic_set_n_param(bset);
9202 for (i = 0; i < bset->n_eq; ++i)
9203 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9204 return 1;
9205 return 0;
9208 /* Check if the value for dimension dim is completely determined
9209 * by the values of the other parameters and variables.
9210 * That is, check if dimension dim is involved in an equality
9211 * for each of the subsets.
9213 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9215 int i;
9217 if (!set)
9218 return -1;
9219 for (i = 0; i < set->n; ++i) {
9220 int unique;
9221 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9222 if (unique != 1)
9223 return unique;
9225 return 1;
9228 int isl_set_n_basic_set(__isl_keep isl_set *set)
9230 return set ? set->n : 0;
9233 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9234 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9236 int i;
9238 if (!map)
9239 return -1;
9241 for (i = 0; i < map->n; ++i)
9242 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9243 return -1;
9245 return 0;
9248 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9249 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9251 int i;
9253 if (!set)
9254 return -1;
9256 for (i = 0; i < set->n; ++i)
9257 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9258 return -1;
9260 return 0;
9263 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9265 isl_space *dim;
9267 if (!bset)
9268 return NULL;
9270 bset = isl_basic_set_cow(bset);
9271 if (!bset)
9272 return NULL;
9274 dim = isl_basic_set_get_space(bset);
9275 dim = isl_space_lift(dim, bset->n_div);
9276 if (!dim)
9277 goto error;
9278 isl_space_free(bset->dim);
9279 bset->dim = dim;
9280 bset->extra -= bset->n_div;
9281 bset->n_div = 0;
9283 bset = isl_basic_set_finalize(bset);
9285 return bset;
9286 error:
9287 isl_basic_set_free(bset);
9288 return NULL;
9291 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9293 int i;
9294 isl_space *dim;
9295 unsigned n_div;
9297 set = isl_set_align_divs(set);
9299 if (!set)
9300 return NULL;
9302 set = isl_set_cow(set);
9303 if (!set)
9304 return NULL;
9306 n_div = set->p[0]->n_div;
9307 dim = isl_set_get_space(set);
9308 dim = isl_space_lift(dim, n_div);
9309 if (!dim)
9310 goto error;
9311 isl_space_free(set->dim);
9312 set->dim = dim;
9314 for (i = 0; i < set->n; ++i) {
9315 set->p[i] = isl_basic_set_lift(set->p[i]);
9316 if (!set->p[i])
9317 goto error;
9320 return set;
9321 error:
9322 isl_set_free(set);
9323 return NULL;
9326 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9328 isl_space *dim;
9329 struct isl_basic_map *bmap;
9330 unsigned n_set;
9331 unsigned n_div;
9332 unsigned n_param;
9333 unsigned total;
9334 int i, k, l;
9336 set = isl_set_align_divs(set);
9338 if (!set)
9339 return NULL;
9341 dim = isl_set_get_space(set);
9342 if (set->n == 0 || set->p[0]->n_div == 0) {
9343 isl_set_free(set);
9344 return isl_map_identity(isl_space_map_from_set(dim));
9347 n_div = set->p[0]->n_div;
9348 dim = isl_space_map_from_set(dim);
9349 n_param = isl_space_dim(dim, isl_dim_param);
9350 n_set = isl_space_dim(dim, isl_dim_in);
9351 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9352 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9353 for (i = 0; i < n_set; ++i)
9354 bmap = var_equal(bmap, i);
9356 total = n_param + n_set + n_set + n_div;
9357 for (i = 0; i < n_div; ++i) {
9358 k = isl_basic_map_alloc_inequality(bmap);
9359 if (k < 0)
9360 goto error;
9361 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9362 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9363 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9364 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9365 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9366 set->p[0]->div[i][0]);
9368 l = isl_basic_map_alloc_inequality(bmap);
9369 if (l < 0)
9370 goto error;
9371 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9372 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9373 set->p[0]->div[i][0]);
9374 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9377 isl_set_free(set);
9378 bmap = isl_basic_map_simplify(bmap);
9379 bmap = isl_basic_map_finalize(bmap);
9380 return isl_map_from_basic_map(bmap);
9381 error:
9382 isl_set_free(set);
9383 isl_basic_map_free(bmap);
9384 return NULL;
9387 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9389 unsigned dim;
9390 int size = 0;
9392 if (!bset)
9393 return -1;
9395 dim = isl_basic_set_total_dim(bset);
9396 size += bset->n_eq * (1 + dim);
9397 size += bset->n_ineq * (1 + dim);
9398 size += bset->n_div * (2 + dim);
9400 return size;
9403 int isl_set_size(__isl_keep isl_set *set)
9405 int i;
9406 int size = 0;
9408 if (!set)
9409 return -1;
9411 for (i = 0; i < set->n; ++i)
9412 size += isl_basic_set_size(set->p[i]);
9414 return size;
9417 /* Check if there is any lower bound (if lower == 0) and/or upper
9418 * bound (if upper == 0) on the specified dim.
9420 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9421 enum isl_dim_type type, unsigned pos, int lower, int upper)
9423 int i;
9425 if (!bmap)
9426 return -1;
9428 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9430 pos += isl_basic_map_offset(bmap, type);
9432 for (i = 0; i < bmap->n_div; ++i) {
9433 if (isl_int_is_zero(bmap->div[i][0]))
9434 continue;
9435 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9436 return 1;
9439 for (i = 0; i < bmap->n_eq; ++i)
9440 if (!isl_int_is_zero(bmap->eq[i][pos]))
9441 return 1;
9443 for (i = 0; i < bmap->n_ineq; ++i) {
9444 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
9445 if (sgn > 0)
9446 lower = 1;
9447 if (sgn < 0)
9448 upper = 1;
9451 return lower && upper;
9454 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9455 enum isl_dim_type type, unsigned pos)
9457 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9460 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9461 enum isl_dim_type type, unsigned pos)
9463 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9466 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9467 enum isl_dim_type type, unsigned pos)
9469 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9472 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9473 enum isl_dim_type type, unsigned pos)
9475 int i;
9477 if (!map)
9478 return -1;
9480 for (i = 0; i < map->n; ++i) {
9481 int bounded;
9482 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9483 if (bounded < 0 || !bounded)
9484 return bounded;
9487 return 1;
9490 /* Return 1 if the specified dim is involved in both an upper bound
9491 * and a lower bound.
9493 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9494 enum isl_dim_type type, unsigned pos)
9496 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9499 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9501 static int has_any_bound(__isl_keep isl_map *map,
9502 enum isl_dim_type type, unsigned pos,
9503 int (*fn)(__isl_keep isl_basic_map *bmap,
9504 enum isl_dim_type type, unsigned pos))
9506 int i;
9508 if (!map)
9509 return -1;
9511 for (i = 0; i < map->n; ++i) {
9512 int bounded;
9513 bounded = fn(map->p[i], type, pos);
9514 if (bounded < 0 || bounded)
9515 return bounded;
9518 return 0;
9521 /* Return 1 if the specified dim is involved in any lower bound.
9523 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
9524 enum isl_dim_type type, unsigned pos)
9526 return has_any_bound(set, type, pos,
9527 &isl_basic_map_dim_has_lower_bound);
9530 /* Return 1 if the specified dim is involved in any upper bound.
9532 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
9533 enum isl_dim_type type, unsigned pos)
9535 return has_any_bound(set, type, pos,
9536 &isl_basic_map_dim_has_upper_bound);
9539 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9541 static int has_bound(__isl_keep isl_map *map,
9542 enum isl_dim_type type, unsigned pos,
9543 int (*fn)(__isl_keep isl_basic_map *bmap,
9544 enum isl_dim_type type, unsigned pos))
9546 int i;
9548 if (!map)
9549 return -1;
9551 for (i = 0; i < map->n; ++i) {
9552 int bounded;
9553 bounded = fn(map->p[i], type, pos);
9554 if (bounded < 0 || !bounded)
9555 return bounded;
9558 return 1;
9561 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9563 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9564 enum isl_dim_type type, unsigned pos)
9566 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9569 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9571 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9572 enum isl_dim_type type, unsigned pos)
9574 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9577 /* For each of the "n" variables starting at "first", determine
9578 * the sign of the variable and put the results in the first "n"
9579 * elements of the array "signs".
9580 * Sign
9581 * 1 means that the variable is non-negative
9582 * -1 means that the variable is non-positive
9583 * 0 means the variable attains both positive and negative values.
9585 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9586 unsigned first, unsigned n, int *signs)
9588 isl_vec *bound = NULL;
9589 struct isl_tab *tab = NULL;
9590 struct isl_tab_undo *snap;
9591 int i;
9593 if (!bset || !signs)
9594 return -1;
9596 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9597 tab = isl_tab_from_basic_set(bset, 0);
9598 if (!bound || !tab)
9599 goto error;
9601 isl_seq_clr(bound->el, bound->size);
9602 isl_int_set_si(bound->el[0], -1);
9604 snap = isl_tab_snap(tab);
9605 for (i = 0; i < n; ++i) {
9606 int empty;
9608 isl_int_set_si(bound->el[1 + first + i], -1);
9609 if (isl_tab_add_ineq(tab, bound->el) < 0)
9610 goto error;
9611 empty = tab->empty;
9612 isl_int_set_si(bound->el[1 + first + i], 0);
9613 if (isl_tab_rollback(tab, snap) < 0)
9614 goto error;
9616 if (empty) {
9617 signs[i] = 1;
9618 continue;
9621 isl_int_set_si(bound->el[1 + first + i], 1);
9622 if (isl_tab_add_ineq(tab, bound->el) < 0)
9623 goto error;
9624 empty = tab->empty;
9625 isl_int_set_si(bound->el[1 + first + i], 0);
9626 if (isl_tab_rollback(tab, snap) < 0)
9627 goto error;
9629 signs[i] = empty ? -1 : 0;
9632 isl_tab_free(tab);
9633 isl_vec_free(bound);
9634 return 0;
9635 error:
9636 isl_tab_free(tab);
9637 isl_vec_free(bound);
9638 return -1;
9641 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
9642 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
9644 if (!bset || !signs)
9645 return -1;
9646 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
9647 return -1);
9649 first += pos(bset->dim, type) - 1;
9650 return isl_basic_set_vars_get_sign(bset, first, n, signs);
9653 /* Check if the given basic map is obviously single-valued.
9654 * In particular, for each output dimension, check that there is
9655 * an equality that defines the output dimension in terms of
9656 * earlier dimensions.
9658 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
9660 int i, j;
9661 unsigned total;
9662 unsigned n_out;
9663 unsigned o_out;
9665 if (!bmap)
9666 return -1;
9668 total = 1 + isl_basic_map_total_dim(bmap);
9669 n_out = isl_basic_map_dim(bmap, isl_dim_out);
9670 o_out = isl_basic_map_offset(bmap, isl_dim_out);
9672 for (i = 0; i < n_out; ++i) {
9673 for (j = 0; j < bmap->n_eq; ++j) {
9674 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
9675 continue;
9676 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
9677 total - (o_out + i + 1)) == -1)
9678 break;
9680 if (j >= bmap->n_eq)
9681 return 0;
9684 return 1;
9687 /* Check if the given basic map is single-valued.
9688 * We simply compute
9690 * M \circ M^-1
9692 * and check if the result is a subset of the identity mapping.
9694 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
9696 isl_space *space;
9697 isl_basic_map *test;
9698 isl_basic_map *id;
9699 int sv;
9701 sv = isl_basic_map_plain_is_single_valued(bmap);
9702 if (sv < 0 || sv)
9703 return sv;
9705 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
9706 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
9708 space = isl_basic_map_get_space(bmap);
9709 space = isl_space_map_from_set(isl_space_range(space));
9710 id = isl_basic_map_identity(space);
9712 sv = isl_basic_map_is_subset(test, id);
9714 isl_basic_map_free(test);
9715 isl_basic_map_free(id);
9717 return sv;
9720 /* Check if the given map is obviously single-valued.
9722 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
9724 if (!map)
9725 return -1;
9726 if (map->n == 0)
9727 return 1;
9728 if (map->n >= 2)
9729 return 0;
9731 return isl_basic_map_plain_is_single_valued(map->p[0]);
9734 /* Check if the given map is single-valued.
9735 * We simply compute
9737 * M \circ M^-1
9739 * and check if the result is a subset of the identity mapping.
9741 int isl_map_is_single_valued(__isl_keep isl_map *map)
9743 isl_space *dim;
9744 isl_map *test;
9745 isl_map *id;
9746 int sv;
9748 sv = isl_map_plain_is_single_valued(map);
9749 if (sv < 0 || sv)
9750 return sv;
9752 test = isl_map_reverse(isl_map_copy(map));
9753 test = isl_map_apply_range(test, isl_map_copy(map));
9755 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
9756 id = isl_map_identity(dim);
9758 sv = isl_map_is_subset(test, id);
9760 isl_map_free(test);
9761 isl_map_free(id);
9763 return sv;
9766 int isl_map_is_injective(__isl_keep isl_map *map)
9768 int in;
9770 map = isl_map_copy(map);
9771 map = isl_map_reverse(map);
9772 in = isl_map_is_single_valued(map);
9773 isl_map_free(map);
9775 return in;
9778 /* Check if the given map is obviously injective.
9780 int isl_map_plain_is_injective(__isl_keep isl_map *map)
9782 int in;
9784 map = isl_map_copy(map);
9785 map = isl_map_reverse(map);
9786 in = isl_map_plain_is_single_valued(map);
9787 isl_map_free(map);
9789 return in;
9792 int isl_map_is_bijective(__isl_keep isl_map *map)
9794 int sv;
9796 sv = isl_map_is_single_valued(map);
9797 if (sv < 0 || !sv)
9798 return sv;
9800 return isl_map_is_injective(map);
9803 int isl_set_is_singleton(__isl_keep isl_set *set)
9805 return isl_map_is_single_valued((isl_map *)set);
9808 int isl_map_is_translation(__isl_keep isl_map *map)
9810 int ok;
9811 isl_set *delta;
9813 delta = isl_map_deltas(isl_map_copy(map));
9814 ok = isl_set_is_singleton(delta);
9815 isl_set_free(delta);
9817 return ok;
9820 static int unique(isl_int *p, unsigned pos, unsigned len)
9822 if (isl_seq_first_non_zero(p, pos) != -1)
9823 return 0;
9824 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
9825 return 0;
9826 return 1;
9829 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
9831 int i, j;
9832 unsigned nvar;
9833 unsigned ovar;
9835 if (!bset)
9836 return -1;
9838 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
9839 return 0;
9841 nvar = isl_basic_set_dim(bset, isl_dim_set);
9842 ovar = isl_space_offset(bset->dim, isl_dim_set);
9843 for (j = 0; j < nvar; ++j) {
9844 int lower = 0, upper = 0;
9845 for (i = 0; i < bset->n_eq; ++i) {
9846 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
9847 continue;
9848 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
9849 return 0;
9850 break;
9852 if (i < bset->n_eq)
9853 continue;
9854 for (i = 0; i < bset->n_ineq; ++i) {
9855 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
9856 continue;
9857 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
9858 return 0;
9859 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
9860 lower = 1;
9861 else
9862 upper = 1;
9864 if (!lower || !upper)
9865 return 0;
9868 return 1;
9871 int isl_set_is_box(__isl_keep isl_set *set)
9873 if (!set)
9874 return -1;
9875 if (set->n != 1)
9876 return 0;
9878 return isl_basic_set_is_box(set->p[0]);
9881 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
9883 if (!bset)
9884 return -1;
9886 return isl_space_is_wrapping(bset->dim);
9889 int isl_set_is_wrapping(__isl_keep isl_set *set)
9891 if (!set)
9892 return -1;
9894 return isl_space_is_wrapping(set->dim);
9897 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
9899 bmap = isl_basic_map_cow(bmap);
9900 if (!bmap)
9901 return NULL;
9903 bmap->dim = isl_space_wrap(bmap->dim);
9904 if (!bmap->dim)
9905 goto error;
9907 bmap = isl_basic_map_finalize(bmap);
9909 return (isl_basic_set *)bmap;
9910 error:
9911 isl_basic_map_free(bmap);
9912 return NULL;
9915 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
9917 int i;
9919 map = isl_map_cow(map);
9920 if (!map)
9921 return NULL;
9923 for (i = 0; i < map->n; ++i) {
9924 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
9925 if (!map->p[i])
9926 goto error;
9928 map->dim = isl_space_wrap(map->dim);
9929 if (!map->dim)
9930 goto error;
9932 return (isl_set *)map;
9933 error:
9934 isl_map_free(map);
9935 return NULL;
9938 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
9940 bset = isl_basic_set_cow(bset);
9941 if (!bset)
9942 return NULL;
9944 bset->dim = isl_space_unwrap(bset->dim);
9945 if (!bset->dim)
9946 goto error;
9948 bset = isl_basic_set_finalize(bset);
9950 return (isl_basic_map *)bset;
9951 error:
9952 isl_basic_set_free(bset);
9953 return NULL;
9956 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
9958 int i;
9960 if (!set)
9961 return NULL;
9963 if (!isl_set_is_wrapping(set))
9964 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
9965 goto error);
9967 set = isl_set_cow(set);
9968 if (!set)
9969 return NULL;
9971 for (i = 0; i < set->n; ++i) {
9972 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9973 if (!set->p[i])
9974 goto error;
9977 set->dim = isl_space_unwrap(set->dim);
9978 if (!set->dim)
9979 goto error;
9981 return (isl_map *)set;
9982 error:
9983 isl_set_free(set);
9984 return NULL;
9987 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9988 enum isl_dim_type type)
9990 if (!bmap)
9991 return NULL;
9993 if (!isl_space_is_named_or_nested(bmap->dim, type))
9994 return bmap;
9996 bmap = isl_basic_map_cow(bmap);
9997 if (!bmap)
9998 return NULL;
10000 bmap->dim = isl_space_reset(bmap->dim, type);
10001 if (!bmap->dim)
10002 goto error;
10004 bmap = isl_basic_map_finalize(bmap);
10006 return bmap;
10007 error:
10008 isl_basic_map_free(bmap);
10009 return NULL;
10012 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10013 enum isl_dim_type type)
10015 int i;
10017 if (!map)
10018 return NULL;
10020 if (!isl_space_is_named_or_nested(map->dim, type))
10021 return map;
10023 map = isl_map_cow(map);
10024 if (!map)
10025 return NULL;
10027 for (i = 0; i < map->n; ++i) {
10028 map->p[i] = isl_basic_map_reset(map->p[i], type);
10029 if (!map->p[i])
10030 goto error;
10032 map->dim = isl_space_reset(map->dim, type);
10033 if (!map->dim)
10034 goto error;
10036 return map;
10037 error:
10038 isl_map_free(map);
10039 return NULL;
10042 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10044 if (!bmap)
10045 return NULL;
10047 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10048 return bmap;
10050 bmap = isl_basic_map_cow(bmap);
10051 if (!bmap)
10052 return NULL;
10054 bmap->dim = isl_space_flatten(bmap->dim);
10055 if (!bmap->dim)
10056 goto error;
10058 bmap = isl_basic_map_finalize(bmap);
10060 return bmap;
10061 error:
10062 isl_basic_map_free(bmap);
10063 return NULL;
10066 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10068 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10071 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10072 __isl_take isl_basic_map *bmap)
10074 if (!bmap)
10075 return NULL;
10077 if (!bmap->dim->nested[0])
10078 return bmap;
10080 bmap = isl_basic_map_cow(bmap);
10081 if (!bmap)
10082 return NULL;
10084 bmap->dim = isl_space_flatten_domain(bmap->dim);
10085 if (!bmap->dim)
10086 goto error;
10088 bmap = isl_basic_map_finalize(bmap);
10090 return bmap;
10091 error:
10092 isl_basic_map_free(bmap);
10093 return NULL;
10096 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10097 __isl_take isl_basic_map *bmap)
10099 if (!bmap)
10100 return NULL;
10102 if (!bmap->dim->nested[1])
10103 return bmap;
10105 bmap = isl_basic_map_cow(bmap);
10106 if (!bmap)
10107 return NULL;
10109 bmap->dim = isl_space_flatten_range(bmap->dim);
10110 if (!bmap->dim)
10111 goto error;
10113 bmap = isl_basic_map_finalize(bmap);
10115 return bmap;
10116 error:
10117 isl_basic_map_free(bmap);
10118 return NULL;
10121 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10123 int i;
10125 if (!map)
10126 return NULL;
10128 if (!map->dim->nested[0] && !map->dim->nested[1])
10129 return map;
10131 map = isl_map_cow(map);
10132 if (!map)
10133 return NULL;
10135 for (i = 0; i < map->n; ++i) {
10136 map->p[i] = isl_basic_map_flatten(map->p[i]);
10137 if (!map->p[i])
10138 goto error;
10140 map->dim = isl_space_flatten(map->dim);
10141 if (!map->dim)
10142 goto error;
10144 return map;
10145 error:
10146 isl_map_free(map);
10147 return NULL;
10150 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10152 return (isl_set *)isl_map_flatten((isl_map *)set);
10155 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10157 isl_space *dim, *flat_dim;
10158 isl_map *map;
10160 dim = isl_set_get_space(set);
10161 flat_dim = isl_space_flatten(isl_space_copy(dim));
10162 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10163 map = isl_map_intersect_domain(map, set);
10165 return map;
10168 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10170 int i;
10172 if (!map)
10173 return NULL;
10175 if (!map->dim->nested[0])
10176 return map;
10178 map = isl_map_cow(map);
10179 if (!map)
10180 return NULL;
10182 for (i = 0; i < map->n; ++i) {
10183 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10184 if (!map->p[i])
10185 goto error;
10187 map->dim = isl_space_flatten_domain(map->dim);
10188 if (!map->dim)
10189 goto error;
10191 return map;
10192 error:
10193 isl_map_free(map);
10194 return NULL;
10197 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10199 int i;
10201 if (!map)
10202 return NULL;
10204 if (!map->dim->nested[1])
10205 return map;
10207 map = isl_map_cow(map);
10208 if (!map)
10209 return NULL;
10211 for (i = 0; i < map->n; ++i) {
10212 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10213 if (!map->p[i])
10214 goto error;
10216 map->dim = isl_space_flatten_range(map->dim);
10217 if (!map->dim)
10218 goto error;
10220 return map;
10221 error:
10222 isl_map_free(map);
10223 return NULL;
10226 /* Reorder the dimensions of "bmap" according to the given dim_map
10227 * and set the dimension specification to "dim".
10229 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10230 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10232 isl_basic_map *res;
10233 unsigned flags;
10235 bmap = isl_basic_map_cow(bmap);
10236 if (!bmap || !dim || !dim_map)
10237 goto error;
10239 flags = bmap->flags;
10240 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10241 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10242 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10243 res = isl_basic_map_alloc_space(dim,
10244 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10245 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10246 if (res)
10247 res->flags = flags;
10248 res = isl_basic_map_finalize(res);
10249 return res;
10250 error:
10251 free(dim_map);
10252 isl_basic_map_free(bmap);
10253 isl_space_free(dim);
10254 return NULL;
10257 /* Reorder the dimensions of "map" according to given reordering.
10259 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10260 __isl_take isl_reordering *r)
10262 int i;
10263 struct isl_dim_map *dim_map;
10265 map = isl_map_cow(map);
10266 dim_map = isl_dim_map_from_reordering(r);
10267 if (!map || !r || !dim_map)
10268 goto error;
10270 for (i = 0; i < map->n; ++i) {
10271 struct isl_dim_map *dim_map_i;
10273 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10275 map->p[i] = isl_basic_map_realign(map->p[i],
10276 isl_space_copy(r->dim), dim_map_i);
10278 if (!map->p[i])
10279 goto error;
10282 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10284 isl_reordering_free(r);
10285 free(dim_map);
10286 return map;
10287 error:
10288 free(dim_map);
10289 isl_map_free(map);
10290 isl_reordering_free(r);
10291 return NULL;
10294 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10295 __isl_take isl_reordering *r)
10297 return (isl_set *)isl_map_realign((isl_map *)set, r);
10300 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10301 __isl_take isl_space *model)
10303 isl_ctx *ctx;
10305 if (!map || !model)
10306 goto error;
10308 ctx = isl_space_get_ctx(model);
10309 if (!isl_space_has_named_params(model))
10310 isl_die(ctx, isl_error_invalid,
10311 "model has unnamed parameters", goto error);
10312 if (!isl_space_has_named_params(map->dim))
10313 isl_die(ctx, isl_error_invalid,
10314 "relation has unnamed parameters", goto error);
10315 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10316 isl_reordering *exp;
10318 model = isl_space_drop_dims(model, isl_dim_in,
10319 0, isl_space_dim(model, isl_dim_in));
10320 model = isl_space_drop_dims(model, isl_dim_out,
10321 0, isl_space_dim(model, isl_dim_out));
10322 exp = isl_parameter_alignment_reordering(map->dim, model);
10323 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10324 map = isl_map_realign(map, exp);
10327 isl_space_free(model);
10328 return map;
10329 error:
10330 isl_space_free(model);
10331 isl_map_free(map);
10332 return NULL;
10335 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10336 __isl_take isl_space *model)
10338 return isl_map_align_params(set, model);
10341 /* Align the parameters of "bmap" to those of "model", introducing
10342 * additional parameters if needed.
10344 __isl_give isl_basic_map *isl_basic_map_align_params(
10345 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10347 isl_ctx *ctx;
10349 if (!bmap || !model)
10350 goto error;
10352 ctx = isl_space_get_ctx(model);
10353 if (!isl_space_has_named_params(model))
10354 isl_die(ctx, isl_error_invalid,
10355 "model has unnamed parameters", goto error);
10356 if (!isl_space_has_named_params(bmap->dim))
10357 isl_die(ctx, isl_error_invalid,
10358 "relation has unnamed parameters", goto error);
10359 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10360 isl_reordering *exp;
10361 struct isl_dim_map *dim_map;
10363 model = isl_space_drop_dims(model, isl_dim_in,
10364 0, isl_space_dim(model, isl_dim_in));
10365 model = isl_space_drop_dims(model, isl_dim_out,
10366 0, isl_space_dim(model, isl_dim_out));
10367 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10368 exp = isl_reordering_extend_space(exp,
10369 isl_basic_map_get_space(bmap));
10370 dim_map = isl_dim_map_from_reordering(exp);
10371 bmap = isl_basic_map_realign(bmap,
10372 exp ? isl_space_copy(exp->dim) : NULL,
10373 isl_dim_map_extend(dim_map, bmap));
10374 isl_reordering_free(exp);
10375 free(dim_map);
10378 isl_space_free(model);
10379 return bmap;
10380 error:
10381 isl_space_free(model);
10382 isl_basic_map_free(bmap);
10383 return NULL;
10386 /* Align the parameters of "bset" to those of "model", introducing
10387 * additional parameters if needed.
10389 __isl_give isl_basic_set *isl_basic_set_align_params(
10390 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10392 return isl_basic_map_align_params(bset, model);
10395 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10396 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10397 enum isl_dim_type c2, enum isl_dim_type c3,
10398 enum isl_dim_type c4, enum isl_dim_type c5)
10400 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10401 struct isl_mat *mat;
10402 int i, j, k;
10403 int pos;
10405 if (!bmap)
10406 return NULL;
10407 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
10408 isl_basic_map_total_dim(bmap) + 1);
10409 if (!mat)
10410 return NULL;
10411 for (i = 0; i < bmap->n_eq; ++i)
10412 for (j = 0, pos = 0; j < 5; ++j) {
10413 int off = isl_basic_map_offset(bmap, c[j]);
10414 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10415 isl_int_set(mat->row[i][pos],
10416 bmap->eq[i][off + k]);
10417 ++pos;
10421 return mat;
10424 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
10425 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10426 enum isl_dim_type c2, enum isl_dim_type c3,
10427 enum isl_dim_type c4, enum isl_dim_type c5)
10429 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10430 struct isl_mat *mat;
10431 int i, j, k;
10432 int pos;
10434 if (!bmap)
10435 return NULL;
10436 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
10437 isl_basic_map_total_dim(bmap) + 1);
10438 if (!mat)
10439 return NULL;
10440 for (i = 0; i < bmap->n_ineq; ++i)
10441 for (j = 0, pos = 0; j < 5; ++j) {
10442 int off = isl_basic_map_offset(bmap, c[j]);
10443 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10444 isl_int_set(mat->row[i][pos],
10445 bmap->ineq[i][off + k]);
10446 ++pos;
10450 return mat;
10453 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
10454 __isl_take isl_space *dim,
10455 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10456 enum isl_dim_type c2, enum isl_dim_type c3,
10457 enum isl_dim_type c4, enum isl_dim_type c5)
10459 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10460 isl_basic_map *bmap;
10461 unsigned total;
10462 unsigned extra;
10463 int i, j, k, l;
10464 int pos;
10466 if (!dim || !eq || !ineq)
10467 goto error;
10469 if (eq->n_col != ineq->n_col)
10470 isl_die(dim->ctx, isl_error_invalid,
10471 "equalities and inequalities matrices should have "
10472 "same number of columns", goto error);
10474 total = 1 + isl_space_dim(dim, isl_dim_all);
10476 if (eq->n_col < total)
10477 isl_die(dim->ctx, isl_error_invalid,
10478 "number of columns too small", goto error);
10480 extra = eq->n_col - total;
10482 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
10483 eq->n_row, ineq->n_row);
10484 if (!bmap)
10485 goto error;
10486 for (i = 0; i < extra; ++i) {
10487 k = isl_basic_map_alloc_div(bmap);
10488 if (k < 0)
10489 goto error;
10490 isl_int_set_si(bmap->div[k][0], 0);
10492 for (i = 0; i < eq->n_row; ++i) {
10493 l = isl_basic_map_alloc_equality(bmap);
10494 if (l < 0)
10495 goto error;
10496 for (j = 0, pos = 0; j < 5; ++j) {
10497 int off = isl_basic_map_offset(bmap, c[j]);
10498 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10499 isl_int_set(bmap->eq[l][off + k],
10500 eq->row[i][pos]);
10501 ++pos;
10505 for (i = 0; i < ineq->n_row; ++i) {
10506 l = isl_basic_map_alloc_inequality(bmap);
10507 if (l < 0)
10508 goto error;
10509 for (j = 0, pos = 0; j < 5; ++j) {
10510 int off = isl_basic_map_offset(bmap, c[j]);
10511 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10512 isl_int_set(bmap->ineq[l][off + k],
10513 ineq->row[i][pos]);
10514 ++pos;
10519 isl_space_free(dim);
10520 isl_mat_free(eq);
10521 isl_mat_free(ineq);
10523 bmap = isl_basic_map_simplify(bmap);
10524 return isl_basic_map_finalize(bmap);
10525 error:
10526 isl_space_free(dim);
10527 isl_mat_free(eq);
10528 isl_mat_free(ineq);
10529 return NULL;
10532 __isl_give isl_mat *isl_basic_set_equalities_matrix(
10533 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10534 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10536 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
10537 c1, c2, c3, c4, isl_dim_in);
10540 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
10541 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10542 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10544 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
10545 c1, c2, c3, c4, isl_dim_in);
10548 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
10549 __isl_take isl_space *dim,
10550 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10551 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10553 return (isl_basic_set*)
10554 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
10555 c1, c2, c3, c4, isl_dim_in);
10558 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
10560 if (!bmap)
10561 return -1;
10563 return isl_space_can_zip(bmap->dim);
10566 int isl_map_can_zip(__isl_keep isl_map *map)
10568 if (!map)
10569 return -1;
10571 return isl_space_can_zip(map->dim);
10574 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10575 * (A -> C) -> (B -> D).
10577 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10579 unsigned pos;
10580 unsigned n1;
10581 unsigned n2;
10583 if (!bmap)
10584 return NULL;
10586 if (!isl_basic_map_can_zip(bmap))
10587 isl_die(bmap->ctx, isl_error_invalid,
10588 "basic map cannot be zipped", goto error);
10589 pos = isl_basic_map_offset(bmap, isl_dim_in) +
10590 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10591 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10592 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10593 bmap = isl_basic_map_cow(bmap);
10594 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10595 if (!bmap)
10596 return NULL;
10597 bmap->dim = isl_space_zip(bmap->dim);
10598 if (!bmap->dim)
10599 goto error;
10600 return bmap;
10601 error:
10602 isl_basic_map_free(bmap);
10603 return NULL;
10606 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10607 * (A -> C) -> (B -> D).
10609 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
10611 int i;
10613 if (!map)
10614 return NULL;
10616 if (!isl_map_can_zip(map))
10617 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
10618 goto error);
10620 map = isl_map_cow(map);
10621 if (!map)
10622 return NULL;
10624 for (i = 0; i < map->n; ++i) {
10625 map->p[i] = isl_basic_map_zip(map->p[i]);
10626 if (!map->p[i])
10627 goto error;
10630 map->dim = isl_space_zip(map->dim);
10631 if (!map->dim)
10632 goto error;
10634 return map;
10635 error:
10636 isl_map_free(map);
10637 return NULL;
10640 /* Can we apply isl_basic_map_curry to "bmap"?
10641 * That is, does it have a nested relation in its domain?
10643 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
10645 if (!bmap)
10646 return -1;
10648 return isl_space_can_curry(bmap->dim);
10651 /* Can we apply isl_map_curry to "map"?
10652 * That is, does it have a nested relation in its domain?
10654 int isl_map_can_curry(__isl_keep isl_map *map)
10656 if (!map)
10657 return -1;
10659 return isl_space_can_curry(map->dim);
10662 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10663 * A -> (B -> C).
10665 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
10668 if (!bmap)
10669 return NULL;
10671 if (!isl_basic_map_can_curry(bmap))
10672 isl_die(bmap->ctx, isl_error_invalid,
10673 "basic map cannot be curried", goto error);
10674 bmap = isl_basic_map_cow(bmap);
10675 if (!bmap)
10676 return NULL;
10677 bmap->dim = isl_space_curry(bmap->dim);
10678 if (!bmap->dim)
10679 goto error;
10680 return bmap;
10681 error:
10682 isl_basic_map_free(bmap);
10683 return NULL;
10686 /* Given a map (A -> B) -> C, return the corresponding map
10687 * A -> (B -> C).
10689 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
10691 int i;
10693 if (!map)
10694 return NULL;
10696 if (!isl_map_can_curry(map))
10697 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
10698 goto error);
10700 map = isl_map_cow(map);
10701 if (!map)
10702 return NULL;
10704 for (i = 0; i < map->n; ++i) {
10705 map->p[i] = isl_basic_map_curry(map->p[i]);
10706 if (!map->p[i])
10707 goto error;
10710 map->dim = isl_space_curry(map->dim);
10711 if (!map->dim)
10712 goto error;
10714 return map;
10715 error:
10716 isl_map_free(map);
10717 return NULL;
10720 /* Can we apply isl_basic_map_uncurry to "bmap"?
10721 * That is, does it have a nested relation in its domain?
10723 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
10725 if (!bmap)
10726 return -1;
10728 return isl_space_can_uncurry(bmap->dim);
10731 /* Can we apply isl_map_uncurry to "map"?
10732 * That is, does it have a nested relation in its domain?
10734 int isl_map_can_uncurry(__isl_keep isl_map *map)
10736 if (!map)
10737 return -1;
10739 return isl_space_can_uncurry(map->dim);
10742 /* Given a basic map A -> (B -> C), return the corresponding basic map
10743 * (A -> B) -> C.
10745 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
10748 if (!bmap)
10749 return NULL;
10751 if (!isl_basic_map_can_uncurry(bmap))
10752 isl_die(bmap->ctx, isl_error_invalid,
10753 "basic map cannot be uncurried",
10754 return isl_basic_map_free(bmap));
10755 bmap = isl_basic_map_cow(bmap);
10756 if (!bmap)
10757 return NULL;
10758 bmap->dim = isl_space_uncurry(bmap->dim);
10759 if (!bmap->dim)
10760 return isl_basic_map_free(bmap);
10761 return bmap;
10764 /* Given a map A -> (B -> C), return the corresponding map
10765 * (A -> B) -> C.
10767 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
10769 int i;
10771 if (!map)
10772 return NULL;
10774 if (!isl_map_can_uncurry(map))
10775 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
10776 return isl_map_free(map));
10778 map = isl_map_cow(map);
10779 if (!map)
10780 return NULL;
10782 for (i = 0; i < map->n; ++i) {
10783 map->p[i] = isl_basic_map_uncurry(map->p[i]);
10784 if (!map->p[i])
10785 return isl_map_free(map);
10788 map->dim = isl_space_uncurry(map->dim);
10789 if (!map->dim)
10790 return isl_map_free(map);
10792 return map;
10795 /* Construct a basic map mapping the domain of the affine expression
10796 * to a one-dimensional range prescribed by the affine expression.
10798 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
10800 int k;
10801 int pos;
10802 isl_local_space *ls;
10803 isl_basic_map *bmap;
10805 if (!aff)
10806 return NULL;
10808 ls = isl_aff_get_local_space(aff);
10809 bmap = isl_basic_map_from_local_space(ls);
10810 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
10811 k = isl_basic_map_alloc_equality(bmap);
10812 if (k < 0)
10813 goto error;
10815 pos = isl_basic_map_offset(bmap, isl_dim_out);
10816 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
10817 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
10818 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
10819 aff->v->size - (pos + 1));
10821 isl_aff_free(aff);
10822 bmap = isl_basic_map_finalize(bmap);
10823 return bmap;
10824 error:
10825 isl_aff_free(aff);
10826 isl_basic_map_free(bmap);
10827 return NULL;
10830 /* Construct a map mapping the domain of the affine expression
10831 * to a one-dimensional range prescribed by the affine expression.
10833 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
10835 isl_basic_map *bmap;
10837 bmap = isl_basic_map_from_aff(aff);
10838 return isl_map_from_basic_map(bmap);
10841 /* Construct a basic map mapping the domain the multi-affine expression
10842 * to its range, with each dimension in the range equated to the
10843 * corresponding affine expression.
10845 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
10846 __isl_take isl_multi_aff *maff)
10848 int i;
10849 isl_space *space;
10850 isl_basic_map *bmap;
10852 if (!maff)
10853 return NULL;
10855 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
10856 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
10857 "invalid space", return isl_multi_aff_free(maff));
10859 space = isl_space_domain(isl_multi_aff_get_space(maff));
10860 bmap = isl_basic_map_universe(isl_space_from_domain(space));
10862 for (i = 0; i < maff->n; ++i) {
10863 isl_aff *aff;
10864 isl_basic_map *bmap_i;
10866 aff = isl_aff_copy(maff->p[i]);
10867 bmap_i = isl_basic_map_from_aff(aff);
10869 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10872 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
10874 isl_multi_aff_free(maff);
10875 return bmap;
10878 /* Construct a map mapping the domain the multi-affine expression
10879 * to its range, with each dimension in the range equated to the
10880 * corresponding affine expression.
10882 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
10884 isl_basic_map *bmap;
10886 bmap = isl_basic_map_from_multi_aff(maff);
10887 return isl_map_from_basic_map(bmap);
10890 /* Construct a basic map mapping a domain in the given space to
10891 * to an n-dimensional range, with n the number of elements in the list,
10892 * where each coordinate in the range is prescribed by the
10893 * corresponding affine expression.
10894 * The domains of all affine expressions in the list are assumed to match
10895 * domain_dim.
10897 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
10898 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
10900 int i;
10901 isl_space *dim;
10902 isl_basic_map *bmap;
10904 if (!list)
10905 return NULL;
10907 dim = isl_space_from_domain(domain_dim);
10908 bmap = isl_basic_map_universe(dim);
10910 for (i = 0; i < list->n; ++i) {
10911 isl_aff *aff;
10912 isl_basic_map *bmap_i;
10914 aff = isl_aff_copy(list->p[i]);
10915 bmap_i = isl_basic_map_from_aff(aff);
10917 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10920 isl_aff_list_free(list);
10921 return bmap;
10924 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
10925 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10927 return isl_map_equate(set, type1, pos1, type2, pos2);
10930 /* Construct a basic map where the given dimensions are equal to each other.
10932 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
10933 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10935 isl_basic_map *bmap = NULL;
10936 int i;
10938 if (!space)
10939 return NULL;
10941 if (pos1 >= isl_space_dim(space, type1))
10942 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10943 "index out of bounds", goto error);
10944 if (pos2 >= isl_space_dim(space, type2))
10945 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10946 "index out of bounds", goto error);
10948 if (type1 == type2 && pos1 == pos2)
10949 return isl_basic_map_universe(space);
10951 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
10952 i = isl_basic_map_alloc_equality(bmap);
10953 if (i < 0)
10954 goto error;
10955 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10956 pos1 += isl_basic_map_offset(bmap, type1);
10957 pos2 += isl_basic_map_offset(bmap, type2);
10958 isl_int_set_si(bmap->eq[i][pos1], -1);
10959 isl_int_set_si(bmap->eq[i][pos2], 1);
10960 bmap = isl_basic_map_finalize(bmap);
10961 isl_space_free(space);
10962 return bmap;
10963 error:
10964 isl_space_free(space);
10965 isl_basic_map_free(bmap);
10966 return NULL;
10969 /* Add a constraint imposing that the given two dimensions are equal.
10971 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
10972 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10974 isl_basic_map *eq;
10976 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
10978 bmap = isl_basic_map_intersect(bmap, eq);
10980 return bmap;
10983 /* Add a constraint imposing that the given two dimensions are equal.
10985 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
10986 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10988 isl_basic_map *bmap;
10990 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
10992 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10994 return map;
10997 /* Add a constraint imposing that the given two dimensions have opposite values.
10999 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11000 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11002 isl_basic_map *bmap = NULL;
11003 int i;
11005 if (!map)
11006 return NULL;
11008 if (pos1 >= isl_map_dim(map, type1))
11009 isl_die(map->ctx, isl_error_invalid,
11010 "index out of bounds", goto error);
11011 if (pos2 >= isl_map_dim(map, type2))
11012 isl_die(map->ctx, isl_error_invalid,
11013 "index out of bounds", goto error);
11015 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11016 i = isl_basic_map_alloc_equality(bmap);
11017 if (i < 0)
11018 goto error;
11019 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11020 pos1 += isl_basic_map_offset(bmap, type1);
11021 pos2 += isl_basic_map_offset(bmap, type2);
11022 isl_int_set_si(bmap->eq[i][pos1], 1);
11023 isl_int_set_si(bmap->eq[i][pos2], 1);
11024 bmap = isl_basic_map_finalize(bmap);
11026 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11028 return map;
11029 error:
11030 isl_basic_map_free(bmap);
11031 isl_map_free(map);
11032 return NULL;
11035 /* Add a constraint imposing that the value of the first dimension is
11036 * greater than or equal to that of the second.
11038 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11039 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11041 isl_constraint *c;
11042 isl_local_space *ls;
11044 if (!bmap)
11045 return NULL;
11047 if (pos1 >= isl_basic_map_dim(bmap, type1))
11048 isl_die(bmap->ctx, isl_error_invalid,
11049 "index out of bounds", return isl_basic_map_free(bmap));
11050 if (pos2 >= isl_basic_map_dim(bmap, type2))
11051 isl_die(bmap->ctx, isl_error_invalid,
11052 "index out of bounds", return isl_basic_map_free(bmap));
11054 if (type1 == type2 && pos1 == pos2)
11055 return bmap;
11057 ls = isl_local_space_from_space(isl_basic_map_get_space(bmap));
11058 c = isl_inequality_alloc(ls);
11059 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11060 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11061 bmap = isl_basic_map_add_constraint(bmap, c);
11063 return bmap;
11066 /* Construct a basic map where the value of the first dimension is
11067 * greater than that of the second.
11069 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11070 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11072 isl_basic_map *bmap = NULL;
11073 int i;
11075 if (!space)
11076 return NULL;
11078 if (pos1 >= isl_space_dim(space, type1))
11079 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11080 "index out of bounds", goto error);
11081 if (pos2 >= isl_space_dim(space, type2))
11082 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11083 "index out of bounds", goto error);
11085 if (type1 == type2 && pos1 == pos2)
11086 return isl_basic_map_empty(space);
11088 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11089 i = isl_basic_map_alloc_inequality(bmap);
11090 if (i < 0)
11091 goto error;
11092 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11093 pos1 += isl_basic_map_offset(bmap, type1);
11094 pos2 += isl_basic_map_offset(bmap, type2);
11095 isl_int_set_si(bmap->ineq[i][pos1], 1);
11096 isl_int_set_si(bmap->ineq[i][pos2], -1);
11097 isl_int_set_si(bmap->ineq[i][0], -1);
11098 bmap = isl_basic_map_finalize(bmap);
11100 return bmap;
11101 error:
11102 isl_space_free(space);
11103 isl_basic_map_free(bmap);
11104 return NULL;
11107 /* Add a constraint imposing that the value of the first dimension is
11108 * greater than that of the second.
11110 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11111 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11113 isl_basic_map *gt;
11115 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11117 bmap = isl_basic_map_intersect(bmap, gt);
11119 return bmap;
11122 /* Add a constraint imposing that the value of the first dimension is
11123 * greater than that of the second.
11125 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11126 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11128 isl_basic_map *bmap;
11130 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11132 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11134 return map;
11137 /* Add a constraint imposing that the value of the first dimension is
11138 * smaller than that of the second.
11140 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11141 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11143 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11146 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11147 int pos)
11149 isl_aff *div;
11150 isl_local_space *ls;
11152 if (!bmap)
11153 return NULL;
11155 if (!isl_basic_map_divs_known(bmap))
11156 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11157 "some divs are unknown", return NULL);
11159 ls = isl_basic_map_get_local_space(bmap);
11160 div = isl_local_space_get_div(ls, pos);
11161 isl_local_space_free(ls);
11163 return div;
11166 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11167 int pos)
11169 return isl_basic_map_get_div(bset, pos);
11172 /* Plug in "subs" for dimension "type", "pos" of "bset".
11174 * Let i be the dimension to replace and let "subs" be of the form
11176 * f/d
11178 * Any integer division with a non-zero coefficient for i,
11180 * floor((a i + g)/m)
11182 * is replaced by
11184 * floor((a f + d g)/(m d))
11186 * Constraints of the form
11188 * a i + g
11190 * are replaced by
11192 * a f + d g
11194 * We currently require that "subs" is an integral expression.
11195 * Handling rational expressions may require us to add stride constraints
11196 * as we do in isl_basic_set_preimage_multi_aff.
11198 __isl_give isl_basic_set *isl_basic_set_substitute(
11199 __isl_take isl_basic_set *bset,
11200 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11202 int i;
11203 isl_int v;
11204 isl_ctx *ctx;
11206 if (bset && isl_basic_set_plain_is_empty(bset))
11207 return bset;
11209 bset = isl_basic_set_cow(bset);
11210 if (!bset || !subs)
11211 goto error;
11213 ctx = isl_basic_set_get_ctx(bset);
11214 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11215 isl_die(ctx, isl_error_invalid,
11216 "spaces don't match", goto error);
11217 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11218 isl_die(ctx, isl_error_unsupported,
11219 "cannot handle divs yet", goto error);
11220 if (!isl_int_is_one(subs->v->el[0]))
11221 isl_die(ctx, isl_error_invalid,
11222 "can only substitute integer expressions", goto error);
11224 pos += isl_basic_set_offset(bset, type);
11226 isl_int_init(v);
11228 for (i = 0; i < bset->n_eq; ++i) {
11229 if (isl_int_is_zero(bset->eq[i][pos]))
11230 continue;
11231 isl_int_set(v, bset->eq[i][pos]);
11232 isl_int_set_si(bset->eq[i][pos], 0);
11233 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11234 v, subs->v->el + 1, subs->v->size - 1);
11237 for (i = 0; i < bset->n_ineq; ++i) {
11238 if (isl_int_is_zero(bset->ineq[i][pos]))
11239 continue;
11240 isl_int_set(v, bset->ineq[i][pos]);
11241 isl_int_set_si(bset->ineq[i][pos], 0);
11242 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11243 v, subs->v->el + 1, subs->v->size - 1);
11246 for (i = 0; i < bset->n_div; ++i) {
11247 if (isl_int_is_zero(bset->div[i][1 + pos]))
11248 continue;
11249 isl_int_set(v, bset->div[i][1 + pos]);
11250 isl_int_set_si(bset->div[i][1 + pos], 0);
11251 isl_seq_combine(bset->div[i] + 1,
11252 subs->v->el[0], bset->div[i] + 1,
11253 v, subs->v->el + 1, subs->v->size - 1);
11254 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11257 isl_int_clear(v);
11259 bset = isl_basic_set_simplify(bset);
11260 return isl_basic_set_finalize(bset);
11261 error:
11262 isl_basic_set_free(bset);
11263 return NULL;
11266 /* Plug in "subs" for dimension "type", "pos" of "set".
11268 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11269 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11271 int i;
11273 if (set && isl_set_plain_is_empty(set))
11274 return set;
11276 set = isl_set_cow(set);
11277 if (!set || !subs)
11278 goto error;
11280 for (i = set->n - 1; i >= 0; --i) {
11281 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11282 if (remove_if_empty(set, i) < 0)
11283 goto error;
11286 return set;
11287 error:
11288 isl_set_free(set);
11289 return NULL;
11292 /* Check if the range of "ma" is compatible with the domain or range
11293 * (depending on "type") of "bmap".
11294 * Return -1 if anything is wrong.
11296 static int check_basic_map_compatible_range_multi_aff(
11297 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11298 __isl_keep isl_multi_aff *ma)
11300 int m;
11301 isl_space *ma_space;
11303 ma_space = isl_multi_aff_get_space(ma);
11304 m = isl_space_tuple_match(bmap->dim, type, ma_space, isl_dim_out);
11305 isl_space_free(ma_space);
11306 if (m >= 0 && !m)
11307 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11308 "spaces don't match", return -1);
11309 return m;
11312 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11313 * coefficients before the transformed range of dimensions,
11314 * the "n_after" coefficients after the transformed range of dimensions
11315 * and the coefficients of the other divs in "bmap".
11317 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
11318 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
11320 int i;
11321 int n_param;
11322 int n_set;
11323 isl_local_space *ls;
11325 if (n_div == 0)
11326 return 0;
11328 ls = isl_aff_get_domain_local_space(ma->p[0]);
11329 if (!ls)
11330 return -1;
11332 n_param = isl_local_space_dim(ls, isl_dim_param);
11333 n_set = isl_local_space_dim(ls, isl_dim_set);
11334 for (i = 0; i < n_div; ++i) {
11335 int o_bmap = 0, o_ls = 0;
11337 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
11338 o_bmap += 1 + 1 + n_param;
11339 o_ls += 1 + 1 + n_param;
11340 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
11341 o_bmap += n_before;
11342 isl_seq_cpy(bmap->div[i] + o_bmap,
11343 ls->div->row[i] + o_ls, n_set);
11344 o_bmap += n_set;
11345 o_ls += n_set;
11346 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
11347 o_bmap += n_after;
11348 isl_seq_cpy(bmap->div[i] + o_bmap,
11349 ls->div->row[i] + o_ls, n_div);
11350 o_bmap += n_div;
11351 o_ls += n_div;
11352 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
11353 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
11354 goto error;
11357 isl_local_space_free(ls);
11358 return 0;
11359 error:
11360 isl_local_space_free(ls);
11361 return -1;
11364 /* How many stride constraints does "ma" enforce?
11365 * That is, how many of the affine expressions have a denominator
11366 * different from one?
11368 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
11370 int i;
11371 int strides = 0;
11373 for (i = 0; i < ma->n; ++i)
11374 if (!isl_int_is_one(ma->p[i]->v->el[0]))
11375 strides++;
11377 return strides;
11380 /* For each affine expression in ma of the form
11382 * x_i = (f_i y + h_i)/m_i
11384 * with m_i different from one, add a constraint to "bmap"
11385 * of the form
11387 * f_i y + h_i = m_i alpha_i
11389 * with alpha_i an additional existentially quantified variable.
11391 static __isl_give isl_basic_map *add_ma_strides(
11392 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
11393 int n_before, int n_after)
11395 int i, k;
11396 int div;
11397 int total;
11398 int n_param;
11399 int n_in;
11400 int n_div;
11402 total = isl_basic_map_total_dim(bmap);
11403 n_param = isl_multi_aff_dim(ma, isl_dim_param);
11404 n_in = isl_multi_aff_dim(ma, isl_dim_in);
11405 n_div = isl_multi_aff_dim(ma, isl_dim_div);
11406 for (i = 0; i < ma->n; ++i) {
11407 int o_bmap = 0, o_ma = 1;
11409 if (isl_int_is_one(ma->p[i]->v->el[0]))
11410 continue;
11411 div = isl_basic_map_alloc_div(bmap);
11412 k = isl_basic_map_alloc_equality(bmap);
11413 if (div < 0 || k < 0)
11414 goto error;
11415 isl_int_set_si(bmap->div[div][0], 0);
11416 isl_seq_cpy(bmap->eq[k] + o_bmap,
11417 ma->p[i]->v->el + o_ma, 1 + n_param);
11418 o_bmap += 1 + n_param;
11419 o_ma += 1 + n_param;
11420 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
11421 o_bmap += n_before;
11422 isl_seq_cpy(bmap->eq[k] + o_bmap,
11423 ma->p[i]->v->el + o_ma, n_in);
11424 o_bmap += n_in;
11425 o_ma += n_in;
11426 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
11427 o_bmap += n_after;
11428 isl_seq_cpy(bmap->eq[k] + o_bmap,
11429 ma->p[i]->v->el + o_ma, n_div);
11430 o_bmap += n_div;
11431 o_ma += n_div;
11432 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
11433 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
11434 total++;
11437 return bmap;
11438 error:
11439 isl_basic_map_free(bmap);
11440 return NULL;
11443 /* Replace the domain or range space (depending on "type) of "space" by "set".
11445 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
11446 enum isl_dim_type type, __isl_take isl_space *set)
11448 if (type == isl_dim_in) {
11449 space = isl_space_range(space);
11450 space = isl_space_map_from_domain_and_range(set, space);
11451 } else {
11452 space = isl_space_domain(space);
11453 space = isl_space_map_from_domain_and_range(space, set);
11456 return space;
11459 /* Compute the preimage of the domain or range (depending on "type")
11460 * of "bmap" under the function represented by "ma".
11461 * In other words, plug in "ma" in the domain or range of "bmap".
11462 * The result is a basic map that lives in the same space as "bmap"
11463 * except that the domain or range has been replaced by
11464 * the domain space of "ma".
11466 * If bmap is represented by
11468 * A(p) + S u + B x + T v + C(divs) >= 0,
11470 * where u and x are input and output dimensions if type == isl_dim_out
11471 * while x and v are input and output dimensions if type == isl_dim_in,
11472 * and ma is represented by
11474 * x = D(p) + F(y) + G(divs')
11476 * then the result is
11478 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11480 * The divs in the input set are similarly adjusted.
11481 * In particular
11483 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11485 * becomes
11487 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11488 * B_i G(divs') + c_i(divs))/n_i)
11490 * If bmap is not a rational map and if F(y) involves any denominators
11492 * x_i = (f_i y + h_i)/m_i
11494 * then additional constraints are added to ensure that we only
11495 * map back integer points. That is we enforce
11497 * f_i y + h_i = m_i alpha_i
11499 * with alpha_i an additional existentially quantified variable.
11501 * We first copy over the divs from "ma".
11502 * Then we add the modified constraints and divs from "bmap".
11503 * Finally, we add the stride constraints, if needed.
11505 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
11506 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
11507 __isl_take isl_multi_aff *ma)
11509 int i, k;
11510 isl_space *space;
11511 isl_basic_map *res = NULL;
11512 int n_before, n_after, n_div_bmap, n_div_ma;
11513 isl_int f, c1, c2, g;
11514 int rational, strides;
11516 isl_int_init(f);
11517 isl_int_init(c1);
11518 isl_int_init(c2);
11519 isl_int_init(g);
11521 ma = isl_multi_aff_align_divs(ma);
11522 if (!bmap || !ma)
11523 goto error;
11524 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
11525 goto error;
11527 if (type == isl_dim_in) {
11528 n_before = 0;
11529 n_after = isl_basic_map_dim(bmap, isl_dim_out);
11530 } else {
11531 n_before = isl_basic_map_dim(bmap, isl_dim_in);
11532 n_after = 0;
11534 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
11535 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
11537 space = isl_multi_aff_get_domain_space(ma);
11538 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
11539 rational = isl_basic_map_is_rational(bmap);
11540 strides = rational ? 0 : multi_aff_strides(ma);
11541 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
11542 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
11543 if (rational)
11544 res = isl_basic_map_set_rational(res);
11546 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
11547 if (isl_basic_map_alloc_div(res) < 0)
11548 goto error;
11550 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
11551 goto error;
11553 for (i = 0; i < bmap->n_eq; ++i) {
11554 k = isl_basic_map_alloc_equality(res);
11555 if (k < 0)
11556 goto error;
11557 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
11558 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11561 for (i = 0; i < bmap->n_ineq; ++i) {
11562 k = isl_basic_map_alloc_inequality(res);
11563 if (k < 0)
11564 goto error;
11565 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
11566 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11569 for (i = 0; i < bmap->n_div; ++i) {
11570 if (isl_int_is_zero(bmap->div[i][0])) {
11571 isl_int_set_si(res->div[n_div_ma + i][0], 0);
11572 continue;
11574 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
11575 n_before, n_after, n_div_ma, n_div_bmap,
11576 f, c1, c2, g, 1);
11579 if (strides)
11580 res = add_ma_strides(res, ma, n_before, n_after);
11582 isl_int_clear(f);
11583 isl_int_clear(c1);
11584 isl_int_clear(c2);
11585 isl_int_clear(g);
11586 isl_basic_map_free(bmap);
11587 isl_multi_aff_free(ma);
11588 res = isl_basic_set_simplify(res);
11589 return isl_basic_map_finalize(res);
11590 error:
11591 isl_int_clear(f);
11592 isl_int_clear(c1);
11593 isl_int_clear(c2);
11594 isl_int_clear(g);
11595 isl_basic_map_free(bmap);
11596 isl_multi_aff_free(ma);
11597 isl_basic_map_free(res);
11598 return NULL;
11601 /* Compute the preimage of "bset" under the function represented by "ma".
11602 * In other words, plug in "ma" in "bset". The result is a basic set
11603 * that lives in the domain space of "ma".
11605 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
11606 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
11608 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
11611 /* Check if the range of "ma" is compatible with the domain or range
11612 * (depending on "type") of "map".
11613 * Return -1 if anything is wrong.
11615 static int check_map_compatible_range_multi_aff(
11616 __isl_keep isl_map *map, enum isl_dim_type type,
11617 __isl_keep isl_multi_aff *ma)
11619 int m;
11620 isl_space *ma_space;
11622 ma_space = isl_multi_aff_get_space(ma);
11623 m = isl_space_tuple_match(map->dim, type, ma_space, isl_dim_out);
11624 isl_space_free(ma_space);
11625 if (m >= 0 && !m)
11626 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11627 "spaces don't match", return -1);
11628 return m;
11631 /* Compute the preimage of the domain or range (depending on "type")
11632 * of "map" under the function represented by "ma".
11633 * In other words, plug in "ma" in the domain or range of "map".
11634 * The result is a map that lives in the same space as "map"
11635 * except that the domain or range has been replaced by
11636 * the domain space of "ma".
11638 * The parameters are assumed to have been aligned.
11640 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
11641 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11643 int i;
11644 isl_space *space;
11646 map = isl_map_cow(map);
11647 ma = isl_multi_aff_align_divs(ma);
11648 if (!map || !ma)
11649 goto error;
11650 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
11651 goto error;
11653 for (i = 0; i < map->n; ++i) {
11654 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
11655 isl_multi_aff_copy(ma));
11656 if (!map->p[i])
11657 goto error;
11660 space = isl_multi_aff_get_domain_space(ma);
11661 space = isl_space_set(isl_map_get_space(map), type, space);
11663 isl_space_free(map->dim);
11664 map->dim = space;
11665 if (!map->dim)
11666 goto error;
11668 isl_multi_aff_free(ma);
11669 if (map->n > 1)
11670 ISL_F_CLR(map, ISL_MAP_DISJOINT);
11671 ISL_F_CLR(map, ISL_SET_NORMALIZED);
11672 return map;
11673 error:
11674 isl_multi_aff_free(ma);
11675 isl_map_free(map);
11676 return NULL;
11679 /* Compute the preimage of the domain or range (depending on "type")
11680 * of "map" under the function represented by "ma".
11681 * In other words, plug in "ma" in the domain or range of "map".
11682 * The result is a map that lives in the same space as "map"
11683 * except that the domain or range has been replaced by
11684 * the domain space of "ma".
11686 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
11687 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11689 if (!map || !ma)
11690 goto error;
11692 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
11693 return map_preimage_multi_aff(map, type, ma);
11695 if (!isl_space_has_named_params(map->dim) ||
11696 !isl_space_has_named_params(ma->space))
11697 isl_die(map->ctx, isl_error_invalid,
11698 "unaligned unnamed parameters", goto error);
11699 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
11700 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
11702 return map_preimage_multi_aff(map, type, ma);
11703 error:
11704 isl_multi_aff_free(ma);
11705 return isl_map_free(map);
11708 /* Compute the preimage of "set" under the function represented by "ma".
11709 * In other words, plug in "ma" "set". The result is a set
11710 * that lives in the domain space of "ma".
11712 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
11713 __isl_take isl_multi_aff *ma)
11715 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
11718 /* Compute the preimage of the domain of "map" under the function
11719 * represented by "ma".
11720 * In other words, plug in "ma" in the domain of "map".
11721 * The result is a map that lives in the same space as "map"
11722 * except that the domain has been replaced by the domain space of "ma".
11724 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
11725 __isl_take isl_multi_aff *ma)
11727 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
11730 /* Compute the preimage of "set" under the function represented by "pma".
11731 * In other words, plug in "pma" in "set. The result is a set
11732 * that lives in the domain space of "pma".
11734 static __isl_give isl_set *set_preimage_pw_multi_aff(__isl_take isl_set *set,
11735 __isl_take isl_pw_multi_aff *pma)
11737 int i;
11738 isl_set *res;
11740 if (!pma)
11741 goto error;
11743 if (pma->n == 0) {
11744 isl_pw_multi_aff_free(pma);
11745 res = isl_set_empty(isl_set_get_space(set));
11746 isl_set_free(set);
11747 return res;
11750 res = isl_set_preimage_multi_aff(isl_set_copy(set),
11751 isl_multi_aff_copy(pma->p[0].maff));
11752 res = isl_set_intersect(res, isl_set_copy(pma->p[0].set));
11754 for (i = 1; i < pma->n; ++i) {
11755 isl_set *res_i;
11757 res_i = isl_set_preimage_multi_aff(isl_set_copy(set),
11758 isl_multi_aff_copy(pma->p[i].maff));
11759 res_i = isl_set_intersect(res_i, isl_set_copy(pma->p[i].set));
11760 res = isl_set_union(res, res_i);
11763 isl_pw_multi_aff_free(pma);
11764 isl_set_free(set);
11765 return res;
11766 error:
11767 isl_pw_multi_aff_free(pma);
11768 isl_set_free(set);
11769 return NULL;
11772 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
11773 __isl_take isl_pw_multi_aff *pma)
11775 if (!set || !pma)
11776 goto error;
11778 if (isl_space_match(set->dim, isl_dim_param, pma->dim, isl_dim_param))
11779 return set_preimage_pw_multi_aff(set, pma);
11781 if (!isl_space_has_named_params(set->dim) ||
11782 !isl_space_has_named_params(pma->dim))
11783 isl_die(set->ctx, isl_error_invalid,
11784 "unaligned unnamed parameters", goto error);
11785 set = isl_set_align_params(set, isl_pw_multi_aff_get_space(pma));
11786 pma = isl_pw_multi_aff_align_params(pma, isl_set_get_space(set));
11788 return set_preimage_pw_multi_aff(set, pma);
11789 error:
11790 isl_pw_multi_aff_free(pma);
11791 return isl_set_free(set);