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