add isl_local_space_has_dim_id and isl_local_space_get_dim_id
[isl.git] / isl_map.c
blobe68b192a24ef05ab717e7091a504146fe2191073
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012 Ecole Normale Superieure
6 * Use of this software is governed by the GNU LGPLv2.1 license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <string.h>
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include <isl/blk.h>
19 #include "isl_space_private.h"
20 #include "isl_equalities.h"
21 #include <isl_list_private.h>
22 #include <isl/lp.h>
23 #include <isl/seq.h>
24 #include <isl/set.h>
25 #include <isl/map.h>
26 #include "isl_map_piplib.h"
27 #include <isl_reordering.h>
28 #include "isl_sample.h"
29 #include "isl_tab.h"
30 #include <isl/vec.h>
31 #include <isl_mat_private.h>
32 #include <isl_dim_map.h>
33 #include <isl_local_space_private.h>
34 #include <isl_aff_private.h>
35 #include <isl_options_private.h>
37 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
39 switch (type) {
40 case isl_dim_param: return dim->nparam;
41 case isl_dim_in: return dim->n_in;
42 case isl_dim_out: return dim->n_out;
43 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
44 default: return 0;
48 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
50 switch (type) {
51 case isl_dim_param: return 1;
52 case isl_dim_in: return 1 + dim->nparam;
53 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
54 default: return 0;
58 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
59 enum isl_dim_type type)
61 if (!bmap)
62 return 0;
63 switch (type) {
64 case isl_dim_cst: return 1;
65 case isl_dim_param:
66 case isl_dim_in:
67 case isl_dim_out: return isl_space_dim(bmap->dim, type);
68 case isl_dim_div: return bmap->n_div;
69 case isl_dim_all: return isl_basic_map_total_dim(bmap);
70 default: return 0;
74 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
76 return map ? n(map->dim, type) : 0;
79 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
81 return set ? n(set->dim, type) : 0;
84 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
85 enum isl_dim_type type)
87 isl_space *dim = bmap->dim;
88 switch (type) {
89 case isl_dim_cst: return 0;
90 case isl_dim_param: return 1;
91 case isl_dim_in: return 1 + dim->nparam;
92 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
93 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
94 default: return 0;
98 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
99 enum isl_dim_type type)
101 return isl_basic_map_offset(bset, type);
104 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
106 return pos(map->dim, type);
109 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
110 enum isl_dim_type type)
112 return isl_basic_map_dim(bset, type);
115 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
117 return isl_basic_set_dim(bset, isl_dim_set);
120 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
122 return isl_basic_set_dim(bset, isl_dim_param);
125 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
127 if (!bset)
128 return 0;
129 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
132 unsigned isl_set_n_dim(__isl_keep isl_set *set)
134 return isl_set_dim(set, isl_dim_set);
137 unsigned isl_set_n_param(__isl_keep isl_set *set)
139 return isl_set_dim(set, isl_dim_param);
142 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
144 return bmap ? bmap->dim->n_in : 0;
147 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
149 return bmap ? bmap->dim->n_out : 0;
152 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
154 return bmap ? bmap->dim->nparam : 0;
157 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
159 return bmap ? bmap->n_div : 0;
162 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
164 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
167 unsigned isl_map_n_in(const struct isl_map *map)
169 return map ? map->dim->n_in : 0;
172 unsigned isl_map_n_out(const struct isl_map *map)
174 return map ? map->dim->n_out : 0;
177 unsigned isl_map_n_param(const struct isl_map *map)
179 return map ? map->dim->nparam : 0;
182 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
184 int m;
185 if (!map || !set)
186 return -1;
187 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
188 if (m < 0 || !m)
189 return m;
190 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
193 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
194 struct isl_basic_set *bset)
196 int m;
197 if (!bmap || !bset)
198 return -1;
199 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
200 if (m < 0 || !m)
201 return m;
202 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
205 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
207 int m;
208 if (!map || !set)
209 return -1;
210 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
211 if (m < 0 || !m)
212 return m;
213 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
216 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
217 struct isl_basic_set *bset)
219 int m;
220 if (!bmap || !bset)
221 return -1;
222 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
223 if (m < 0 || !m)
224 return m;
225 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
228 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
230 return bmap ? bmap->ctx : NULL;
233 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
235 return bset ? bset->ctx : NULL;
238 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
240 return map ? map->ctx : NULL;
243 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
245 return set ? set->ctx : NULL;
248 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
250 if (!bmap)
251 return NULL;
252 return isl_space_copy(bmap->dim);
255 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
257 if (!bset)
258 return NULL;
259 return isl_space_copy(bset->dim);
262 /* Extract the divs in "bmap" as a matrix.
264 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
266 int i;
267 isl_ctx *ctx;
268 isl_mat *div;
269 unsigned total;
270 unsigned cols;
272 if (!bmap)
273 return NULL;
275 ctx = isl_basic_map_get_ctx(bmap);
276 total = isl_space_dim(bmap->dim, isl_dim_all);
277 cols = 1 + 1 + total + bmap->n_div;
278 div = isl_mat_alloc(ctx, bmap->n_div, cols);
279 if (!div)
280 return NULL;
282 for (i = 0; i < bmap->n_div; ++i)
283 isl_seq_cpy(div->row[i], bmap->div[i], cols);
285 return div;
288 __isl_give isl_local_space *isl_basic_map_get_local_space(
289 __isl_keep isl_basic_map *bmap)
291 isl_mat *div;
293 if (!bmap)
294 return NULL;
296 div = isl_basic_map_get_divs(bmap);
297 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
300 __isl_give isl_local_space *isl_basic_set_get_local_space(
301 __isl_keep isl_basic_set *bset)
303 return isl_basic_map_get_local_space(bset);
306 __isl_give isl_basic_map *isl_basic_map_from_local_space(
307 __isl_take isl_local_space *ls)
309 int i;
310 int n_div;
311 isl_basic_map *bmap;
313 if (!ls)
314 return NULL;
316 n_div = isl_local_space_dim(ls, isl_dim_div);
317 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
318 n_div, 0, 2 * n_div);
320 for (i = 0; i < n_div; ++i)
321 if (isl_basic_map_alloc_div(bmap) < 0)
322 goto error;
324 for (i = 0; i < n_div; ++i) {
325 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
326 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
327 goto error;
330 isl_local_space_free(ls);
331 return bmap;
332 error:
333 isl_local_space_free(ls);
334 isl_basic_map_free(bmap);
335 return NULL;
338 __isl_give isl_basic_set *isl_basic_set_from_local_space(
339 __isl_take isl_local_space *ls)
341 return isl_basic_map_from_local_space(ls);
344 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
346 if (!map)
347 return NULL;
348 return isl_space_copy(map->dim);
351 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
353 if (!set)
354 return NULL;
355 return isl_space_copy(set->dim);
358 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
359 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
361 bmap = isl_basic_map_cow(bmap);
362 if (!bmap)
363 return NULL;
364 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
365 if (!bmap->dim)
366 goto error;
367 bmap = isl_basic_map_finalize(bmap);
368 return bmap;
369 error:
370 isl_basic_map_free(bmap);
371 return NULL;
374 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
375 __isl_take isl_basic_set *bset, const char *s)
377 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
380 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
381 enum isl_dim_type type)
383 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
386 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
387 enum isl_dim_type type, const char *s)
389 int i;
391 map = isl_map_cow(map);
392 if (!map)
393 return NULL;
395 map->dim = isl_space_set_tuple_name(map->dim, type, s);
396 if (!map->dim)
397 goto error;
399 for (i = 0; i < map->n; ++i) {
400 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
401 if (!map->p[i])
402 goto error;
405 return map;
406 error:
407 isl_map_free(map);
408 return NULL;
411 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
412 enum isl_dim_type type)
414 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
417 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
418 const char *s)
420 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
423 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
424 enum isl_dim_type type, __isl_take isl_id *id)
426 map = isl_map_cow(map);
427 if (!map)
428 return isl_id_free(id);
430 map->dim = isl_space_set_tuple_id(map->dim, type, id);
432 return isl_map_reset_space(map, isl_space_copy(map->dim));
435 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
436 __isl_take isl_id *id)
438 return isl_map_set_tuple_id(set, isl_dim_set, id);
441 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
442 enum isl_dim_type type)
444 map = isl_map_cow(map);
445 if (!map)
446 return NULL;
448 map->dim = isl_space_reset_tuple_id(map->dim, type);
450 return isl_map_reset_space(map, isl_space_copy(map->dim));
453 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
455 return isl_map_reset_tuple_id(set, isl_dim_set);
458 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
460 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
463 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
464 enum isl_dim_type type)
466 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
469 int isl_set_has_tuple_id(__isl_keep isl_set *set)
471 return isl_map_has_tuple_id(set, isl_dim_set);
474 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
476 return isl_map_get_tuple_id(set, isl_dim_set);
479 /* Does the set tuple have a name?
481 int isl_set_has_tuple_name(__isl_keep isl_set *set)
483 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
487 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
489 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
492 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
494 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
497 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
498 enum isl_dim_type type, unsigned pos)
500 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
503 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
504 enum isl_dim_type type, unsigned pos)
506 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
509 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
510 enum isl_dim_type type, unsigned pos)
512 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
515 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
516 enum isl_dim_type type, unsigned pos)
518 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
521 /* Does the given dimension have a name?
523 int isl_set_has_dim_name(__isl_keep isl_set *set,
524 enum isl_dim_type type, unsigned pos)
526 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
529 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
530 __isl_take isl_basic_map *bmap,
531 enum isl_dim_type type, unsigned pos, const char *s)
533 bmap = isl_basic_map_cow(bmap);
534 if (!bmap)
535 return NULL;
536 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
537 if (!bmap->dim)
538 goto error;
539 return isl_basic_map_finalize(bmap);
540 error:
541 isl_basic_map_free(bmap);
542 return NULL;
545 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
546 enum isl_dim_type type, unsigned pos, const char *s)
548 int i;
550 map = isl_map_cow(map);
551 if (!map)
552 return NULL;
554 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
555 if (!map->dim)
556 goto error;
558 for (i = 0; i < map->n; ++i) {
559 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
560 if (!map->p[i])
561 goto error;
564 return map;
565 error:
566 isl_map_free(map);
567 return NULL;
570 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
571 __isl_take isl_basic_set *bset,
572 enum isl_dim_type type, unsigned pos, const char *s)
574 return (isl_basic_set *)isl_basic_map_set_dim_name(
575 (isl_basic_map *)bset, type, pos, s);
578 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
579 enum isl_dim_type type, unsigned pos, const char *s)
581 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
584 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
585 enum isl_dim_type type, unsigned pos)
587 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
590 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
591 enum isl_dim_type type, unsigned pos)
593 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
596 int isl_map_has_dim_id(__isl_keep isl_map *map,
597 enum isl_dim_type type, unsigned pos)
599 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
602 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
603 enum isl_dim_type type, unsigned pos)
605 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
608 int isl_set_has_dim_id(__isl_keep isl_set *set,
609 enum isl_dim_type type, unsigned pos)
611 return isl_map_has_dim_id(set, type, pos);
614 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
615 enum isl_dim_type type, unsigned pos)
617 return isl_map_get_dim_id(set, type, pos);
620 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
621 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
623 map = isl_map_cow(map);
624 if (!map)
625 return isl_id_free(id);
627 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
629 return isl_map_reset_space(map, isl_space_copy(map->dim));
632 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
633 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
635 return isl_map_set_dim_id(set, type, pos, id);
638 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
639 __isl_keep isl_id *id)
641 if (!map)
642 return -1;
643 return isl_space_find_dim_by_id(map->dim, type, id);
646 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
647 __isl_keep isl_id *id)
649 return isl_map_find_dim_by_id(set, type, id);
652 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
653 const char *name)
655 if (!map)
656 return -1;
657 return isl_space_find_dim_by_name(map->dim, type, name);
660 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
661 const char *name)
663 return isl_map_find_dim_by_name(set, type, name);
666 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
668 if (!bmap)
669 return -1;
670 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
673 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
675 return isl_basic_map_is_rational(bset);
678 /* Is this basic set a parameter domain?
680 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
682 if (!bset)
683 return -1;
684 return isl_space_is_params(bset->dim);
687 /* Is this set a parameter domain?
689 int isl_set_is_params(__isl_keep isl_set *set)
691 if (!set)
692 return -1;
693 return isl_space_is_params(set->dim);
696 /* Is this map actually a parameter domain?
697 * Users should never call this function. Outside of isl,
698 * a map can never be a parameter domain.
700 int isl_map_is_params(__isl_keep isl_map *map)
702 if (!map)
703 return -1;
704 return isl_space_is_params(map->dim);
707 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
708 struct isl_basic_map *bmap, unsigned extra,
709 unsigned n_eq, unsigned n_ineq)
711 int i;
712 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
714 bmap->ctx = ctx;
715 isl_ctx_ref(ctx);
717 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
718 if (isl_blk_is_error(bmap->block))
719 goto error;
721 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
722 if (!bmap->ineq)
723 goto error;
725 if (extra == 0) {
726 bmap->block2 = isl_blk_empty();
727 bmap->div = NULL;
728 } else {
729 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
730 if (isl_blk_is_error(bmap->block2))
731 goto error;
733 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
734 if (!bmap->div)
735 goto error;
738 for (i = 0; i < n_ineq + n_eq; ++i)
739 bmap->ineq[i] = bmap->block.data + i * row_size;
741 for (i = 0; i < extra; ++i)
742 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
744 bmap->ref = 1;
745 bmap->flags = 0;
746 bmap->c_size = n_eq + n_ineq;
747 bmap->eq = bmap->ineq + n_ineq;
748 bmap->extra = extra;
749 bmap->n_eq = 0;
750 bmap->n_ineq = 0;
751 bmap->n_div = 0;
752 bmap->sample = NULL;
754 return bmap;
755 error:
756 isl_basic_map_free(bmap);
757 return NULL;
760 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
761 unsigned nparam, unsigned dim, unsigned extra,
762 unsigned n_eq, unsigned n_ineq)
764 struct isl_basic_map *bmap;
765 isl_space *space;
767 space = isl_space_set_alloc(ctx, nparam, dim);
768 if (!space)
769 return NULL;
771 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
772 return (struct isl_basic_set *)bmap;
775 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
776 unsigned extra, unsigned n_eq, unsigned n_ineq)
778 struct isl_basic_map *bmap;
779 if (!dim)
780 return NULL;
781 isl_assert(dim->ctx, dim->n_in == 0, goto error);
782 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
783 return (struct isl_basic_set *)bmap;
784 error:
785 isl_space_free(dim);
786 return NULL;
789 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
790 unsigned extra, unsigned n_eq, unsigned n_ineq)
792 struct isl_basic_map *bmap;
794 if (!dim)
795 return NULL;
796 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
797 if (!bmap)
798 goto error;
799 bmap->dim = dim;
801 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
802 error:
803 isl_space_free(dim);
804 return NULL;
807 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
808 unsigned nparam, unsigned in, unsigned out, unsigned extra,
809 unsigned n_eq, unsigned n_ineq)
811 struct isl_basic_map *bmap;
812 isl_space *dim;
814 dim = isl_space_alloc(ctx, nparam, in, out);
815 if (!dim)
816 return NULL;
818 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
819 return bmap;
822 static void dup_constraints(
823 struct isl_basic_map *dst, struct isl_basic_map *src)
825 int i;
826 unsigned total = isl_basic_map_total_dim(src);
828 for (i = 0; i < src->n_eq; ++i) {
829 int j = isl_basic_map_alloc_equality(dst);
830 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
833 for (i = 0; i < src->n_ineq; ++i) {
834 int j = isl_basic_map_alloc_inequality(dst);
835 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
838 for (i = 0; i < src->n_div; ++i) {
839 int j = isl_basic_map_alloc_div(dst);
840 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
842 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
845 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
847 struct isl_basic_map *dup;
849 if (!bmap)
850 return NULL;
851 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
852 bmap->n_div, bmap->n_eq, bmap->n_ineq);
853 if (!dup)
854 return NULL;
855 dup_constraints(dup, bmap);
856 dup->flags = bmap->flags;
857 dup->sample = isl_vec_copy(bmap->sample);
858 return dup;
861 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
863 struct isl_basic_map *dup;
865 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
866 return (struct isl_basic_set *)dup;
869 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
871 if (!bset)
872 return NULL;
874 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
875 bset->ref++;
876 return bset;
878 return isl_basic_set_dup(bset);
881 struct isl_set *isl_set_copy(struct isl_set *set)
883 if (!set)
884 return NULL;
886 set->ref++;
887 return set;
890 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
892 if (!bmap)
893 return NULL;
895 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
896 bmap->ref++;
897 return bmap;
899 bmap = isl_basic_map_dup(bmap);
900 if (bmap)
901 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
902 return bmap;
905 struct isl_map *isl_map_copy(struct isl_map *map)
907 if (!map)
908 return NULL;
910 map->ref++;
911 return map;
914 void isl_basic_map_free(struct isl_basic_map *bmap)
916 if (!bmap)
917 return;
919 if (--bmap->ref > 0)
920 return;
922 isl_ctx_deref(bmap->ctx);
923 free(bmap->div);
924 isl_blk_free(bmap->ctx, bmap->block2);
925 free(bmap->ineq);
926 isl_blk_free(bmap->ctx, bmap->block);
927 isl_vec_free(bmap->sample);
928 isl_space_free(bmap->dim);
929 free(bmap);
932 void isl_basic_set_free(struct isl_basic_set *bset)
934 isl_basic_map_free((struct isl_basic_map *)bset);
937 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
939 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
942 __isl_give isl_map *isl_map_align_params_map_map_and(
943 __isl_take isl_map *map1, __isl_take isl_map *map2,
944 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
945 __isl_take isl_map *map2))
947 if (!map1 || !map2)
948 goto error;
949 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
950 return fn(map1, map2);
951 if (!isl_space_has_named_params(map1->dim) ||
952 !isl_space_has_named_params(map2->dim))
953 isl_die(map1->ctx, isl_error_invalid,
954 "unaligned unnamed parameters", goto error);
955 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
956 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
957 return fn(map1, map2);
958 error:
959 isl_map_free(map1);
960 isl_map_free(map2);
961 return NULL;
964 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
965 __isl_keep isl_map *map2,
966 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
968 int r;
970 if (!map1 || !map2)
971 return -1;
972 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
973 return fn(map1, map2);
974 if (!isl_space_has_named_params(map1->dim) ||
975 !isl_space_has_named_params(map2->dim))
976 isl_die(map1->ctx, isl_error_invalid,
977 "unaligned unnamed parameters", return -1);
978 map1 = isl_map_copy(map1);
979 map2 = isl_map_copy(map2);
980 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
981 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
982 r = fn(map1, map2);
983 isl_map_free(map1);
984 isl_map_free(map2);
985 return r;
988 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
990 struct isl_ctx *ctx;
991 if (!bmap)
992 return -1;
993 ctx = bmap->ctx;
994 isl_assert(ctx, room_for_con(bmap, 1), return -1);
995 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
996 return -1);
997 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
998 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
999 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1000 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1001 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1002 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1003 isl_int *t;
1004 int j = isl_basic_map_alloc_inequality(bmap);
1005 if (j < 0)
1006 return -1;
1007 t = bmap->ineq[j];
1008 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1009 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1010 bmap->eq[-1] = t;
1011 bmap->n_eq++;
1012 bmap->n_ineq--;
1013 bmap->eq--;
1014 return 0;
1016 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1017 bmap->extra - bmap->n_div);
1018 return bmap->n_eq++;
1021 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1023 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1026 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1028 if (!bmap)
1029 return -1;
1030 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1031 bmap->n_eq -= n;
1032 return 0;
1035 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1037 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1040 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1042 isl_int *t;
1043 if (!bmap)
1044 return -1;
1045 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1047 if (pos != bmap->n_eq - 1) {
1048 t = bmap->eq[pos];
1049 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1050 bmap->eq[bmap->n_eq - 1] = t;
1052 bmap->n_eq--;
1053 return 0;
1056 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1058 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1061 void isl_basic_map_inequality_to_equality(
1062 struct isl_basic_map *bmap, unsigned pos)
1064 isl_int *t;
1066 t = bmap->ineq[pos];
1067 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1068 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1069 bmap->eq[-1] = t;
1070 bmap->n_eq++;
1071 bmap->n_ineq--;
1072 bmap->eq--;
1073 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1074 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1075 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1076 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1079 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1081 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1084 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1086 struct isl_ctx *ctx;
1087 if (!bmap)
1088 return -1;
1089 ctx = bmap->ctx;
1090 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1091 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1092 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1093 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1094 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1095 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1096 1 + isl_basic_map_total_dim(bmap),
1097 bmap->extra - bmap->n_div);
1098 return bmap->n_ineq++;
1101 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1103 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1106 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1108 if (!bmap)
1109 return -1;
1110 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1111 bmap->n_ineq -= n;
1112 return 0;
1115 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1117 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1120 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1122 isl_int *t;
1123 if (!bmap)
1124 return -1;
1125 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1127 if (pos != bmap->n_ineq - 1) {
1128 t = bmap->ineq[pos];
1129 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1130 bmap->ineq[bmap->n_ineq - 1] = t;
1131 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1133 bmap->n_ineq--;
1134 return 0;
1137 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1139 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1142 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1143 isl_int *eq)
1145 int k;
1147 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1148 if (!bmap)
1149 return NULL;
1150 k = isl_basic_map_alloc_equality(bmap);
1151 if (k < 0)
1152 goto error;
1153 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1154 return bmap;
1155 error:
1156 isl_basic_map_free(bmap);
1157 return NULL;
1160 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1161 isl_int *eq)
1163 return (isl_basic_set *)
1164 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1167 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1168 isl_int *ineq)
1170 int k;
1172 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1173 if (!bmap)
1174 return NULL;
1175 k = isl_basic_map_alloc_inequality(bmap);
1176 if (k < 0)
1177 goto error;
1178 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1179 return bmap;
1180 error:
1181 isl_basic_map_free(bmap);
1182 return NULL;
1185 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1186 isl_int *ineq)
1188 return (isl_basic_set *)
1189 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1192 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1194 if (!bmap)
1195 return -1;
1196 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1197 isl_seq_clr(bmap->div[bmap->n_div] +
1198 1 + 1 + isl_basic_map_total_dim(bmap),
1199 bmap->extra - bmap->n_div);
1200 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1201 return bmap->n_div++;
1204 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1206 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1209 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1211 if (!bmap)
1212 return -1;
1213 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1214 bmap->n_div -= n;
1215 return 0;
1218 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1220 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1223 /* Copy constraint from src to dst, putting the vars of src at offset
1224 * dim_off in dst and the divs of src at offset div_off in dst.
1225 * If both sets are actually map, then dim_off applies to the input
1226 * variables.
1228 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1229 struct isl_basic_map *src_map, isl_int *src,
1230 unsigned in_off, unsigned out_off, unsigned div_off)
1232 unsigned src_nparam = isl_basic_map_n_param(src_map);
1233 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1234 unsigned src_in = isl_basic_map_n_in(src_map);
1235 unsigned dst_in = isl_basic_map_n_in(dst_map);
1236 unsigned src_out = isl_basic_map_n_out(src_map);
1237 unsigned dst_out = isl_basic_map_n_out(dst_map);
1238 isl_int_set(dst[0], src[0]);
1239 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1240 if (dst_nparam > src_nparam)
1241 isl_seq_clr(dst+1+src_nparam,
1242 dst_nparam - src_nparam);
1243 isl_seq_clr(dst+1+dst_nparam, in_off);
1244 isl_seq_cpy(dst+1+dst_nparam+in_off,
1245 src+1+src_nparam,
1246 isl_min(dst_in-in_off, src_in));
1247 if (dst_in-in_off > src_in)
1248 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1249 dst_in - in_off - src_in);
1250 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1251 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1252 src+1+src_nparam+src_in,
1253 isl_min(dst_out-out_off, src_out));
1254 if (dst_out-out_off > src_out)
1255 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1256 dst_out - out_off - src_out);
1257 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1258 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1259 src+1+src_nparam+src_in+src_out,
1260 isl_min(dst_map->extra-div_off, src_map->n_div));
1261 if (dst_map->n_div-div_off > src_map->n_div)
1262 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1263 div_off+src_map->n_div,
1264 dst_map->n_div - div_off - src_map->n_div);
1267 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1268 struct isl_basic_map *src_map, isl_int *src,
1269 unsigned in_off, unsigned out_off, unsigned div_off)
1271 isl_int_set(dst[0], src[0]);
1272 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1275 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1276 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1278 int i;
1279 unsigned div_off;
1281 if (!bmap1 || !bmap2)
1282 goto error;
1284 div_off = bmap1->n_div;
1286 for (i = 0; i < bmap2->n_eq; ++i) {
1287 int i1 = isl_basic_map_alloc_equality(bmap1);
1288 if (i1 < 0)
1289 goto error;
1290 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1291 i_pos, o_pos, div_off);
1294 for (i = 0; i < bmap2->n_ineq; ++i) {
1295 int i1 = isl_basic_map_alloc_inequality(bmap1);
1296 if (i1 < 0)
1297 goto error;
1298 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1299 i_pos, o_pos, div_off);
1302 for (i = 0; i < bmap2->n_div; ++i) {
1303 int i1 = isl_basic_map_alloc_div(bmap1);
1304 if (i1 < 0)
1305 goto error;
1306 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1307 i_pos, o_pos, div_off);
1310 isl_basic_map_free(bmap2);
1312 return bmap1;
1314 error:
1315 isl_basic_map_free(bmap1);
1316 isl_basic_map_free(bmap2);
1317 return NULL;
1320 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1321 struct isl_basic_set *bset2, unsigned pos)
1323 return (struct isl_basic_set *)
1324 add_constraints((struct isl_basic_map *)bset1,
1325 (struct isl_basic_map *)bset2, 0, pos);
1328 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1329 __isl_take isl_space *dim, unsigned extra,
1330 unsigned n_eq, unsigned n_ineq)
1332 struct isl_basic_map *ext;
1333 unsigned flags;
1334 int dims_ok;
1336 if (!dim)
1337 goto error;
1339 if (!base)
1340 goto error;
1342 dims_ok = isl_space_is_equal(base->dim, dim) &&
1343 base->extra >= base->n_div + extra;
1345 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1346 room_for_ineq(base, n_ineq)) {
1347 isl_space_free(dim);
1348 return base;
1351 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1352 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1353 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1354 extra += base->extra;
1355 n_eq += base->n_eq;
1356 n_ineq += base->n_ineq;
1358 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1359 dim = NULL;
1360 if (!ext)
1361 goto error;
1363 if (dims_ok)
1364 ext->sample = isl_vec_copy(base->sample);
1365 flags = base->flags;
1366 ext = add_constraints(ext, base, 0, 0);
1367 if (ext) {
1368 ext->flags = flags;
1369 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1372 return ext;
1374 error:
1375 isl_space_free(dim);
1376 isl_basic_map_free(base);
1377 return NULL;
1380 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1381 __isl_take isl_space *dim, unsigned extra,
1382 unsigned n_eq, unsigned n_ineq)
1384 return (struct isl_basic_set *)
1385 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1386 extra, n_eq, n_ineq);
1389 struct isl_basic_map *isl_basic_map_extend_constraints(
1390 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1392 if (!base)
1393 return NULL;
1394 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1395 0, n_eq, n_ineq);
1398 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1399 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1400 unsigned n_eq, unsigned n_ineq)
1402 struct isl_basic_map *bmap;
1403 isl_space *dim;
1405 if (!base)
1406 return NULL;
1407 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1408 if (!dim)
1409 goto error;
1411 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1412 return bmap;
1413 error:
1414 isl_basic_map_free(base);
1415 return NULL;
1418 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1419 unsigned nparam, unsigned dim, unsigned extra,
1420 unsigned n_eq, unsigned n_ineq)
1422 return (struct isl_basic_set *)
1423 isl_basic_map_extend((struct isl_basic_map *)base,
1424 nparam, 0, dim, extra, n_eq, n_ineq);
1427 struct isl_basic_set *isl_basic_set_extend_constraints(
1428 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1430 return (struct isl_basic_set *)
1431 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1432 n_eq, n_ineq);
1435 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1437 return (struct isl_basic_set *)
1438 isl_basic_map_cow((struct isl_basic_map *)bset);
1441 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1443 if (!bmap)
1444 return NULL;
1446 if (bmap->ref > 1) {
1447 bmap->ref--;
1448 bmap = isl_basic_map_dup(bmap);
1450 if (bmap)
1451 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1452 return bmap;
1455 struct isl_set *isl_set_cow(struct isl_set *set)
1457 if (!set)
1458 return NULL;
1460 if (set->ref == 1)
1461 return set;
1462 set->ref--;
1463 return isl_set_dup(set);
1466 struct isl_map *isl_map_cow(struct isl_map *map)
1468 if (!map)
1469 return NULL;
1471 if (map->ref == 1)
1472 return map;
1473 map->ref--;
1474 return isl_map_dup(map);
1477 static void swap_vars(struct isl_blk blk, isl_int *a,
1478 unsigned a_len, unsigned b_len)
1480 isl_seq_cpy(blk.data, a+a_len, b_len);
1481 isl_seq_cpy(blk.data+b_len, a, a_len);
1482 isl_seq_cpy(a, blk.data, b_len+a_len);
1485 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1486 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1488 int i;
1489 struct isl_blk blk;
1491 if (!bmap)
1492 goto error;
1494 isl_assert(bmap->ctx,
1495 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1497 if (n1 == 0 || n2 == 0)
1498 return bmap;
1500 bmap = isl_basic_map_cow(bmap);
1501 if (!bmap)
1502 return NULL;
1504 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1505 if (isl_blk_is_error(blk))
1506 goto error;
1508 for (i = 0; i < bmap->n_eq; ++i)
1509 swap_vars(blk,
1510 bmap->eq[i] + pos, n1, n2);
1512 for (i = 0; i < bmap->n_ineq; ++i)
1513 swap_vars(blk,
1514 bmap->ineq[i] + pos, n1, n2);
1516 for (i = 0; i < bmap->n_div; ++i)
1517 swap_vars(blk,
1518 bmap->div[i]+1 + pos, n1, n2);
1520 isl_blk_free(bmap->ctx, blk);
1522 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1523 bmap = isl_basic_map_gauss(bmap, NULL);
1524 return isl_basic_map_finalize(bmap);
1525 error:
1526 isl_basic_map_free(bmap);
1527 return NULL;
1530 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1531 __isl_take isl_basic_set *bset, unsigned n)
1533 unsigned dim;
1534 unsigned nparam;
1536 nparam = isl_basic_set_n_param(bset);
1537 dim = isl_basic_set_n_dim(bset);
1538 isl_assert(bset->ctx, n <= dim, goto error);
1540 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1541 error:
1542 isl_basic_set_free(bset);
1543 return NULL;
1546 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1548 int i = 0;
1549 unsigned total;
1550 if (!bmap)
1551 goto error;
1552 total = isl_basic_map_total_dim(bmap);
1553 isl_basic_map_free_div(bmap, bmap->n_div);
1554 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1555 if (bmap->n_eq > 0)
1556 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1557 else {
1558 i = isl_basic_map_alloc_equality(bmap);
1559 if (i < 0)
1560 goto error;
1562 isl_int_set_si(bmap->eq[i][0], 1);
1563 isl_seq_clr(bmap->eq[i]+1, total);
1564 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1565 isl_vec_free(bmap->sample);
1566 bmap->sample = NULL;
1567 return isl_basic_map_finalize(bmap);
1568 error:
1569 isl_basic_map_free(bmap);
1570 return NULL;
1573 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1575 return (struct isl_basic_set *)
1576 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1579 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1581 int i;
1582 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1583 isl_int *t = bmap->div[a];
1584 bmap->div[a] = bmap->div[b];
1585 bmap->div[b] = t;
1587 for (i = 0; i < bmap->n_eq; ++i)
1588 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1590 for (i = 0; i < bmap->n_ineq; ++i)
1591 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1593 for (i = 0; i < bmap->n_div; ++i)
1594 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1595 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1598 /* Eliminate the specified n dimensions starting at first from the
1599 * constraints, without removing the dimensions from the space.
1600 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1602 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1603 enum isl_dim_type type, unsigned first, unsigned n)
1605 int i;
1607 if (!map)
1608 return NULL;
1609 if (n == 0)
1610 return map;
1612 if (first + n > isl_map_dim(map, type) || first + n < first)
1613 isl_die(map->ctx, isl_error_invalid,
1614 "index out of bounds", goto error);
1616 map = isl_map_cow(map);
1617 if (!map)
1618 return NULL;
1620 for (i = 0; i < map->n; ++i) {
1621 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1622 if (!map->p[i])
1623 goto error;
1625 return map;
1626 error:
1627 isl_map_free(map);
1628 return NULL;
1631 /* Eliminate the specified n dimensions starting at first from the
1632 * constraints, without removing the dimensions from the space.
1633 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1635 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1636 enum isl_dim_type type, unsigned first, unsigned n)
1638 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1641 /* Eliminate the specified n dimensions starting at first from the
1642 * constraints, without removing the dimensions from the space.
1643 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1645 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1646 unsigned first, unsigned n)
1648 return isl_set_eliminate(set, isl_dim_set, first, n);
1651 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1652 __isl_take isl_basic_map *bmap)
1654 if (!bmap)
1655 return NULL;
1656 bmap = isl_basic_map_eliminate_vars(bmap,
1657 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1658 if (!bmap)
1659 return NULL;
1660 bmap->n_div = 0;
1661 return isl_basic_map_finalize(bmap);
1664 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1665 __isl_take isl_basic_set *bset)
1667 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1668 (struct isl_basic_map *)bset);
1671 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1673 int i;
1675 if (!map)
1676 return NULL;
1677 if (map->n == 0)
1678 return map;
1680 map = isl_map_cow(map);
1681 if (!map)
1682 return NULL;
1684 for (i = 0; i < map->n; ++i) {
1685 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1686 if (!map->p[i])
1687 goto error;
1689 return map;
1690 error:
1691 isl_map_free(map);
1692 return NULL;
1695 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1697 return isl_map_remove_divs(set);
1700 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1701 enum isl_dim_type type, unsigned first, unsigned n)
1703 if (!bmap)
1704 return NULL;
1705 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1706 goto error);
1707 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1708 return bmap;
1709 bmap = isl_basic_map_eliminate_vars(bmap,
1710 isl_basic_map_offset(bmap, type) - 1 + first, n);
1711 if (!bmap)
1712 return bmap;
1713 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1714 return bmap;
1715 bmap = isl_basic_map_drop(bmap, type, first, n);
1716 return bmap;
1717 error:
1718 isl_basic_map_free(bmap);
1719 return NULL;
1722 /* Return true if the definition of the given div (recursively) involves
1723 * any of the given variables.
1725 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1726 unsigned first, unsigned n)
1728 int i;
1729 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1731 if (isl_int_is_zero(bmap->div[div][0]))
1732 return 0;
1733 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1734 return 1;
1736 for (i = bmap->n_div - 1; i >= 0; --i) {
1737 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1738 continue;
1739 if (div_involves_vars(bmap, i, first, n))
1740 return 1;
1743 return 0;
1746 /* Try and add a lower and/or upper bound on "div" to "bmap"
1747 * based on inequality "i".
1748 * "total" is the total number of variables (excluding the divs).
1749 * "v" is a temporary object that can be used during the calculations.
1750 * If "lb" is set, then a lower bound should be constructed.
1751 * If "ub" is set, then an upper bound should be constructed.
1753 * The calling function has already checked that the inequality does not
1754 * reference "div", but we still need to check that the inequality is
1755 * of the right form. We'll consider the case where we want to construct
1756 * a lower bound. The construction of upper bounds is similar.
1758 * Let "div" be of the form
1760 * q = floor((a + f(x))/d)
1762 * We essentially check if constraint "i" is of the form
1764 * b + f(x) >= 0
1766 * so that we can use it to derive a lower bound on "div".
1767 * However, we allow a slightly more general form
1769 * b + g(x) >= 0
1771 * with the condition that the coefficients of g(x) - f(x) are all
1772 * divisible by d.
1773 * Rewriting this constraint as
1775 * 0 >= -b - g(x)
1777 * adding a + f(x) to both sides and dividing by d, we obtain
1779 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1781 * Taking the floor on both sides, we obtain
1783 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1785 * or
1787 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1789 * In the case of an upper bound, we construct the constraint
1791 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1794 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1795 __isl_take isl_basic_map *bmap, int div, int i,
1796 unsigned total, isl_int v, int lb, int ub)
1798 int j;
1800 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1801 if (lb) {
1802 isl_int_sub(v, bmap->ineq[i][1 + j],
1803 bmap->div[div][1 + 1 + j]);
1804 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1806 if (ub) {
1807 isl_int_add(v, bmap->ineq[i][1 + j],
1808 bmap->div[div][1 + 1 + j]);
1809 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1812 if (!lb && !ub)
1813 return bmap;
1815 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1816 if (lb) {
1817 int k = isl_basic_map_alloc_inequality(bmap);
1818 if (k < 0)
1819 goto error;
1820 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1821 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
1822 bmap->div[div][1 + j]);
1823 isl_int_cdiv_q(bmap->ineq[k][j],
1824 bmap->ineq[k][j], bmap->div[div][0]);
1826 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
1828 if (ub) {
1829 int k = isl_basic_map_alloc_inequality(bmap);
1830 if (k < 0)
1831 goto error;
1832 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1833 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
1834 bmap->div[div][1 + j]);
1835 isl_int_fdiv_q(bmap->ineq[k][j],
1836 bmap->ineq[k][j], bmap->div[div][0]);
1838 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
1841 return bmap;
1842 error:
1843 isl_basic_map_free(bmap);
1844 return NULL;
1847 /* This function is called right before "div" is eliminated from "bmap"
1848 * using Fourier-Motzkin.
1849 * Look through the constraints of "bmap" for constraints on the argument
1850 * of the integer division and use them to construct constraints on the
1851 * integer division itself. These constraints can then be combined
1852 * during the Fourier-Motzkin elimination.
1853 * Note that it is only useful to introduce lower bounds on "div"
1854 * if "bmap" already contains upper bounds on "div" as the newly
1855 * introduce lower bounds can then be combined with the pre-existing
1856 * upper bounds. Similarly for upper bounds.
1857 * We therefore first check if "bmap" contains any lower and/or upper bounds
1858 * on "div".
1860 * It is interesting to note that the introduction of these constraints
1861 * can indeed lead to more accurate results, even when compared to
1862 * deriving constraints on the argument of "div" from constraints on "div".
1863 * Consider, for example, the set
1865 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
1867 * The second constraint can be rewritten as
1869 * 2 * [(-i-2j+3)/4] + k >= 0
1871 * from which we can derive
1873 * -i - 2j + 3 >= -2k
1875 * or
1877 * i + 2j <= 3 + 2k
1879 * Combined with the first constraint, we obtain
1881 * -3 <= 3 + 2k or k >= -3
1883 * If, on the other hand we derive a constraint on [(i+2j)/4] from
1884 * the first constraint, we obtain
1886 * [(i + 2j)/4] >= [-3/4] = -1
1888 * Combining this constraint with the second constraint, we obtain
1890 * k >= -2
1892 static __isl_give isl_basic_map *insert_bounds_on_div(
1893 __isl_take isl_basic_map *bmap, int div)
1895 int i;
1896 int check_lb, check_ub;
1897 isl_int v;
1898 unsigned total;
1900 if (!bmap)
1901 return NULL;
1903 if (isl_int_is_zero(bmap->div[div][0]))
1904 return bmap;
1906 total = isl_space_dim(bmap->dim, isl_dim_all);
1908 check_lb = 0;
1909 check_ub = 0;
1910 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
1911 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
1912 if (s > 0)
1913 check_ub = 1;
1914 if (s < 0)
1915 check_lb = 1;
1918 if (!check_lb && !check_ub)
1919 return bmap;
1921 isl_int_init(v);
1923 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
1924 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
1925 continue;
1927 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
1928 check_lb, check_ub);
1931 isl_int_clear(v);
1933 return bmap;
1936 /* Remove all divs (recursively) involving any of the given dimensions
1937 * in their definitions.
1939 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1940 __isl_take isl_basic_map *bmap,
1941 enum isl_dim_type type, unsigned first, unsigned n)
1943 int i;
1945 if (!bmap)
1946 return NULL;
1947 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1948 goto error);
1949 first += isl_basic_map_offset(bmap, type);
1951 for (i = bmap->n_div - 1; i >= 0; --i) {
1952 if (!div_involves_vars(bmap, i, first, n))
1953 continue;
1954 bmap = insert_bounds_on_div(bmap, i);
1955 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1956 if (!bmap)
1957 return NULL;
1958 i = bmap->n_div;
1961 return bmap;
1962 error:
1963 isl_basic_map_free(bmap);
1964 return NULL;
1967 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1968 enum isl_dim_type type, unsigned first, unsigned n)
1970 int i;
1972 if (!map)
1973 return NULL;
1974 if (map->n == 0)
1975 return map;
1977 map = isl_map_cow(map);
1978 if (!map)
1979 return NULL;
1981 for (i = 0; i < map->n; ++i) {
1982 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1983 type, first, n);
1984 if (!map->p[i])
1985 goto error;
1987 return map;
1988 error:
1989 isl_map_free(map);
1990 return NULL;
1993 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1994 enum isl_dim_type type, unsigned first, unsigned n)
1996 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1997 type, first, n);
2000 /* Does the desciption of "bmap" depend on the specified dimensions?
2001 * We also check whether the dimensions appear in any of the div definitions.
2002 * In principle there is no need for this check. If the dimensions appear
2003 * in a div definition, they also appear in the defining constraints of that
2004 * div.
2006 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2007 enum isl_dim_type type, unsigned first, unsigned n)
2009 int i;
2011 if (!bmap)
2012 return -1;
2014 if (first + n > isl_basic_map_dim(bmap, type))
2015 isl_die(bmap->ctx, isl_error_invalid,
2016 "index out of bounds", return -1);
2018 first += isl_basic_map_offset(bmap, type);
2019 for (i = 0; i < bmap->n_eq; ++i)
2020 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2021 return 1;
2022 for (i = 0; i < bmap->n_ineq; ++i)
2023 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2024 return 1;
2025 for (i = 0; i < bmap->n_div; ++i) {
2026 if (isl_int_is_zero(bmap->div[i][0]))
2027 continue;
2028 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2029 return 1;
2032 return 0;
2035 int isl_map_involves_dims(__isl_keep isl_map *map,
2036 enum isl_dim_type type, unsigned first, unsigned n)
2038 int i;
2040 if (!map)
2041 return -1;
2043 if (first + n > isl_map_dim(map, type))
2044 isl_die(map->ctx, isl_error_invalid,
2045 "index out of bounds", return -1);
2047 for (i = 0; i < map->n; ++i) {
2048 int involves = isl_basic_map_involves_dims(map->p[i],
2049 type, first, n);
2050 if (involves < 0 || involves)
2051 return involves;
2054 return 0;
2057 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2058 enum isl_dim_type type, unsigned first, unsigned n)
2060 return isl_basic_map_involves_dims(bset, type, first, n);
2063 int isl_set_involves_dims(__isl_keep isl_set *set,
2064 enum isl_dim_type type, unsigned first, unsigned n)
2066 return isl_map_involves_dims(set, type, first, n);
2069 /* Return true if the definition of the given div is unknown or depends
2070 * on unknown divs.
2072 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2074 int i;
2075 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2077 if (isl_int_is_zero(bmap->div[div][0]))
2078 return 1;
2080 for (i = bmap->n_div - 1; i >= 0; --i) {
2081 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2082 continue;
2083 if (div_is_unknown(bmap, i))
2084 return 1;
2087 return 0;
2090 /* Remove all divs that are unknown or defined in terms of unknown divs.
2092 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2093 __isl_take isl_basic_map *bmap)
2095 int i;
2097 if (!bmap)
2098 return NULL;
2100 for (i = bmap->n_div - 1; i >= 0; --i) {
2101 if (!div_is_unknown(bmap, i))
2102 continue;
2103 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2104 if (!bmap)
2105 return NULL;
2106 i = bmap->n_div;
2109 return bmap;
2112 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2114 int i;
2116 if (!map)
2117 return NULL;
2118 if (map->n == 0)
2119 return map;
2121 map = isl_map_cow(map);
2122 if (!map)
2123 return NULL;
2125 for (i = 0; i < map->n; ++i) {
2126 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2127 if (!map->p[i])
2128 goto error;
2130 return map;
2131 error:
2132 isl_map_free(map);
2133 return NULL;
2136 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2138 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2141 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2142 __isl_take isl_basic_set *bset,
2143 enum isl_dim_type type, unsigned first, unsigned n)
2145 return (isl_basic_set *)
2146 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2149 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2150 enum isl_dim_type type, unsigned first, unsigned n)
2152 int i;
2154 if (n == 0)
2155 return map;
2157 map = isl_map_cow(map);
2158 if (!map)
2159 return NULL;
2160 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2162 for (i = 0; i < map->n; ++i) {
2163 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2164 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2165 if (!map->p[i])
2166 goto error;
2168 map = isl_map_drop(map, type, first, n);
2169 return map;
2170 error:
2171 isl_map_free(map);
2172 return NULL;
2175 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2176 enum isl_dim_type type, unsigned first, unsigned n)
2178 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2181 /* Project out n inputs starting at first using Fourier-Motzkin */
2182 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2183 unsigned first, unsigned n)
2185 return isl_map_remove_dims(map, isl_dim_in, first, n);
2188 static void dump_term(struct isl_basic_map *bmap,
2189 isl_int c, int pos, FILE *out)
2191 const char *name;
2192 unsigned in = isl_basic_map_n_in(bmap);
2193 unsigned dim = in + isl_basic_map_n_out(bmap);
2194 unsigned nparam = isl_basic_map_n_param(bmap);
2195 if (!pos)
2196 isl_int_print(out, c, 0);
2197 else {
2198 if (!isl_int_is_one(c))
2199 isl_int_print(out, c, 0);
2200 if (pos < 1 + nparam) {
2201 name = isl_space_get_dim_name(bmap->dim,
2202 isl_dim_param, pos - 1);
2203 if (name)
2204 fprintf(out, "%s", name);
2205 else
2206 fprintf(out, "p%d", pos - 1);
2207 } else if (pos < 1 + nparam + in)
2208 fprintf(out, "i%d", pos - 1 - nparam);
2209 else if (pos < 1 + nparam + dim)
2210 fprintf(out, "o%d", pos - 1 - nparam - in);
2211 else
2212 fprintf(out, "e%d", pos - 1 - nparam - dim);
2216 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2217 int sign, FILE *out)
2219 int i;
2220 int first;
2221 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2222 isl_int v;
2224 isl_int_init(v);
2225 for (i = 0, first = 1; i < len; ++i) {
2226 if (isl_int_sgn(c[i]) * sign <= 0)
2227 continue;
2228 if (!first)
2229 fprintf(out, " + ");
2230 first = 0;
2231 isl_int_abs(v, c[i]);
2232 dump_term(bmap, v, i, out);
2234 isl_int_clear(v);
2235 if (first)
2236 fprintf(out, "0");
2239 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2240 const char *op, FILE *out, int indent)
2242 int i;
2244 fprintf(out, "%*s", indent, "");
2246 dump_constraint_sign(bmap, c, 1, out);
2247 fprintf(out, " %s ", op);
2248 dump_constraint_sign(bmap, c, -1, out);
2250 fprintf(out, "\n");
2252 for (i = bmap->n_div; i < bmap->extra; ++i) {
2253 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2254 continue;
2255 fprintf(out, "%*s", indent, "");
2256 fprintf(out, "ERROR: unused div coefficient not zero\n");
2257 abort();
2261 static void dump_constraints(struct isl_basic_map *bmap,
2262 isl_int **c, unsigned n,
2263 const char *op, FILE *out, int indent)
2265 int i;
2267 for (i = 0; i < n; ++i)
2268 dump_constraint(bmap, c[i], op, out, indent);
2271 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2273 int j;
2274 int first = 1;
2275 unsigned total = isl_basic_map_total_dim(bmap);
2277 for (j = 0; j < 1 + total; ++j) {
2278 if (isl_int_is_zero(exp[j]))
2279 continue;
2280 if (!first && isl_int_is_pos(exp[j]))
2281 fprintf(out, "+");
2282 dump_term(bmap, exp[j], j, out);
2283 first = 0;
2287 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2289 int i;
2291 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2292 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2294 for (i = 0; i < bmap->n_div; ++i) {
2295 fprintf(out, "%*s", indent, "");
2296 fprintf(out, "e%d = [(", i);
2297 dump_affine(bmap, bmap->div[i]+1, out);
2298 fprintf(out, ")/");
2299 isl_int_print(out, bmap->div[i][0], 0);
2300 fprintf(out, "]\n");
2304 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2305 FILE *out, int indent)
2307 if (!bset) {
2308 fprintf(out, "null basic set\n");
2309 return;
2312 fprintf(out, "%*s", indent, "");
2313 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2314 bset->ref, bset->dim->nparam, bset->dim->n_out,
2315 bset->extra, bset->flags);
2316 dump((struct isl_basic_map *)bset, out, indent);
2319 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2320 FILE *out, int indent)
2322 if (!bmap) {
2323 fprintf(out, "null basic map\n");
2324 return;
2327 fprintf(out, "%*s", indent, "");
2328 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2329 "flags: %x, n_name: %d\n",
2330 bmap->ref,
2331 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2332 bmap->extra, bmap->flags, bmap->dim->n_id);
2333 dump(bmap, out, indent);
2336 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2338 unsigned total;
2339 if (!bmap)
2340 return -1;
2341 total = isl_basic_map_total_dim(bmap);
2342 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2343 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2344 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2345 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2346 return 0;
2349 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2350 unsigned flags)
2352 struct isl_set *set;
2354 if (!dim)
2355 return NULL;
2356 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2357 isl_assert(dim->ctx, n >= 0, goto error);
2358 set = isl_alloc(dim->ctx, struct isl_set,
2359 sizeof(struct isl_set) +
2360 (n - 1) * sizeof(struct isl_basic_set *));
2361 if (!set)
2362 goto error;
2364 set->ctx = dim->ctx;
2365 isl_ctx_ref(set->ctx);
2366 set->ref = 1;
2367 set->size = n;
2368 set->n = 0;
2369 set->dim = dim;
2370 set->flags = flags;
2371 return set;
2372 error:
2373 isl_space_free(dim);
2374 return NULL;
2377 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2378 unsigned nparam, unsigned dim, int n, unsigned flags)
2380 struct isl_set *set;
2381 isl_space *dims;
2383 dims = isl_space_alloc(ctx, nparam, 0, dim);
2384 if (!dims)
2385 return NULL;
2387 set = isl_set_alloc_space(dims, n, flags);
2388 return set;
2391 /* Make sure "map" has room for at least "n" more basic maps.
2393 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2395 int i;
2396 struct isl_map *grown = NULL;
2398 if (!map)
2399 return NULL;
2400 isl_assert(map->ctx, n >= 0, goto error);
2401 if (map->n + n <= map->size)
2402 return map;
2403 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2404 if (!grown)
2405 goto error;
2406 for (i = 0; i < map->n; ++i) {
2407 grown->p[i] = isl_basic_map_copy(map->p[i]);
2408 if (!grown->p[i])
2409 goto error;
2410 grown->n++;
2412 isl_map_free(map);
2413 return grown;
2414 error:
2415 isl_map_free(grown);
2416 isl_map_free(map);
2417 return NULL;
2420 /* Make sure "set" has room for at least "n" more basic sets.
2422 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2424 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2427 struct isl_set *isl_set_dup(struct isl_set *set)
2429 int i;
2430 struct isl_set *dup;
2432 if (!set)
2433 return NULL;
2435 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2436 if (!dup)
2437 return NULL;
2438 for (i = 0; i < set->n; ++i)
2439 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2440 return dup;
2443 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2445 return isl_map_from_basic_map(bset);
2448 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2450 struct isl_map *map;
2452 if (!bmap)
2453 return NULL;
2455 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2456 return isl_map_add_basic_map(map, bmap);
2459 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2460 __isl_take isl_basic_set *bset)
2462 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2463 (struct isl_basic_map *)bset);
2466 void isl_set_free(struct isl_set *set)
2468 int i;
2470 if (!set)
2471 return;
2473 if (--set->ref > 0)
2474 return;
2476 isl_ctx_deref(set->ctx);
2477 for (i = 0; i < set->n; ++i)
2478 isl_basic_set_free(set->p[i]);
2479 isl_space_free(set->dim);
2480 free(set);
2483 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2485 int i;
2487 if (!set) {
2488 fprintf(out, "null set\n");
2489 return;
2492 fprintf(out, "%*s", indent, "");
2493 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2494 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2495 set->flags);
2496 for (i = 0; i < set->n; ++i) {
2497 fprintf(out, "%*s", indent, "");
2498 fprintf(out, "basic set %d:\n", i);
2499 isl_basic_set_print_internal(set->p[i], out, indent+4);
2503 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2505 int i;
2507 if (!map) {
2508 fprintf(out, "null map\n");
2509 return;
2512 fprintf(out, "%*s", indent, "");
2513 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2514 "flags: %x, n_name: %d\n",
2515 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2516 map->dim->n_out, map->flags, map->dim->n_id);
2517 for (i = 0; i < map->n; ++i) {
2518 fprintf(out, "%*s", indent, "");
2519 fprintf(out, "basic map %d:\n", i);
2520 isl_basic_map_print_internal(map->p[i], out, indent+4);
2524 struct isl_basic_map *isl_basic_map_intersect_domain(
2525 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2527 struct isl_basic_map *bmap_domain;
2529 if (!bmap || !bset)
2530 goto error;
2532 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2533 bset->dim, isl_dim_param), goto error);
2535 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2536 isl_assert(bset->ctx,
2537 isl_basic_map_compatible_domain(bmap, bset), goto error);
2539 bmap = isl_basic_map_cow(bmap);
2540 if (!bmap)
2541 goto error;
2542 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2543 bset->n_div, bset->n_eq, bset->n_ineq);
2544 bmap_domain = isl_basic_map_from_domain(bset);
2545 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2547 bmap = isl_basic_map_simplify(bmap);
2548 return isl_basic_map_finalize(bmap);
2549 error:
2550 isl_basic_map_free(bmap);
2551 isl_basic_set_free(bset);
2552 return NULL;
2555 struct isl_basic_map *isl_basic_map_intersect_range(
2556 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2558 struct isl_basic_map *bmap_range;
2560 if (!bmap || !bset)
2561 goto error;
2563 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2564 bset->dim, isl_dim_param), goto error);
2566 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2567 isl_assert(bset->ctx,
2568 isl_basic_map_compatible_range(bmap, bset), goto error);
2570 if (isl_basic_set_is_universe(bset)) {
2571 isl_basic_set_free(bset);
2572 return bmap;
2575 bmap = isl_basic_map_cow(bmap);
2576 if (!bmap)
2577 goto error;
2578 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2579 bset->n_div, bset->n_eq, bset->n_ineq);
2580 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2581 bmap = add_constraints(bmap, bmap_range, 0, 0);
2583 bmap = isl_basic_map_simplify(bmap);
2584 return isl_basic_map_finalize(bmap);
2585 error:
2586 isl_basic_map_free(bmap);
2587 isl_basic_set_free(bset);
2588 return NULL;
2591 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2593 int i;
2594 unsigned total;
2595 isl_int s;
2597 total = 1 + isl_basic_map_total_dim(bmap);
2598 if (total != vec->size)
2599 return -1;
2601 isl_int_init(s);
2603 for (i = 0; i < bmap->n_eq; ++i) {
2604 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2605 if (!isl_int_is_zero(s)) {
2606 isl_int_clear(s);
2607 return 0;
2611 for (i = 0; i < bmap->n_ineq; ++i) {
2612 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2613 if (isl_int_is_neg(s)) {
2614 isl_int_clear(s);
2615 return 0;
2619 isl_int_clear(s);
2621 return 1;
2624 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2626 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2629 struct isl_basic_map *isl_basic_map_intersect(
2630 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2632 struct isl_vec *sample = NULL;
2634 if (!bmap1 || !bmap2)
2635 goto error;
2637 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2638 bmap2->dim, isl_dim_param), goto error);
2639 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2640 isl_space_dim(bmap1->dim, isl_dim_param) &&
2641 isl_space_dim(bmap2->dim, isl_dim_all) !=
2642 isl_space_dim(bmap2->dim, isl_dim_param))
2643 return isl_basic_map_intersect(bmap2, bmap1);
2645 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2646 isl_space_dim(bmap2->dim, isl_dim_param))
2647 isl_assert(bmap1->ctx,
2648 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2650 if (bmap1->sample &&
2651 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2652 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2653 sample = isl_vec_copy(bmap1->sample);
2654 else if (bmap2->sample &&
2655 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2656 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2657 sample = isl_vec_copy(bmap2->sample);
2659 bmap1 = isl_basic_map_cow(bmap1);
2660 if (!bmap1)
2661 goto error;
2662 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2663 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2664 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2666 if (!bmap1)
2667 isl_vec_free(sample);
2668 else if (sample) {
2669 isl_vec_free(bmap1->sample);
2670 bmap1->sample = sample;
2673 bmap1 = isl_basic_map_simplify(bmap1);
2674 return isl_basic_map_finalize(bmap1);
2675 error:
2676 if (sample)
2677 isl_vec_free(sample);
2678 isl_basic_map_free(bmap1);
2679 isl_basic_map_free(bmap2);
2680 return NULL;
2683 struct isl_basic_set *isl_basic_set_intersect(
2684 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2686 return (struct isl_basic_set *)
2687 isl_basic_map_intersect(
2688 (struct isl_basic_map *)bset1,
2689 (struct isl_basic_map *)bset2);
2692 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2693 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2695 return isl_basic_set_intersect(bset1, bset2);
2698 /* Special case of isl_map_intersect, where both map1 and map2
2699 * are convex, without any divs and such that either map1 or map2
2700 * contains a single constraint. This constraint is then simply
2701 * added to the other map.
2703 static __isl_give isl_map *map_intersect_add_constraint(
2704 __isl_take isl_map *map1, __isl_take isl_map *map2)
2706 isl_assert(map1->ctx, map1->n == 1, goto error);
2707 isl_assert(map2->ctx, map1->n == 1, goto error);
2708 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2709 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2711 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2712 return isl_map_intersect(map2, map1);
2714 isl_assert(map2->ctx,
2715 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2717 map1 = isl_map_cow(map1);
2718 if (!map1)
2719 goto error;
2720 if (isl_map_plain_is_empty(map1)) {
2721 isl_map_free(map2);
2722 return map1;
2724 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2725 if (map2->p[0]->n_eq == 1)
2726 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2727 else
2728 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2729 map2->p[0]->ineq[0]);
2731 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2732 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2733 if (!map1->p[0])
2734 goto error;
2736 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2737 isl_basic_map_free(map1->p[0]);
2738 map1->n = 0;
2741 isl_map_free(map2);
2743 return map1;
2744 error:
2745 isl_map_free(map1);
2746 isl_map_free(map2);
2747 return NULL;
2750 /* map2 may be either a parameter domain or a map living in the same
2751 * space as map1.
2753 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2754 __isl_take isl_map *map2)
2756 unsigned flags = 0;
2757 struct isl_map *result;
2758 int i, j;
2760 if (!map1 || !map2)
2761 goto error;
2763 if (isl_map_plain_is_empty(map1) &&
2764 isl_space_is_equal(map1->dim, map2->dim)) {
2765 isl_map_free(map2);
2766 return map1;
2768 if (isl_map_plain_is_empty(map2) &&
2769 isl_space_is_equal(map1->dim, map2->dim)) {
2770 isl_map_free(map1);
2771 return map2;
2774 if (map1->n == 1 && map2->n == 1 &&
2775 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2776 isl_space_is_equal(map1->dim, map2->dim) &&
2777 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2778 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2779 return map_intersect_add_constraint(map1, map2);
2781 if (isl_space_dim(map2->dim, isl_dim_all) !=
2782 isl_space_dim(map2->dim, isl_dim_param))
2783 isl_assert(map1->ctx,
2784 isl_space_is_equal(map1->dim, map2->dim), goto error);
2786 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2787 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2788 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2790 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2791 map1->n * map2->n, flags);
2792 if (!result)
2793 goto error;
2794 for (i = 0; i < map1->n; ++i)
2795 for (j = 0; j < map2->n; ++j) {
2796 struct isl_basic_map *part;
2797 part = isl_basic_map_intersect(
2798 isl_basic_map_copy(map1->p[i]),
2799 isl_basic_map_copy(map2->p[j]));
2800 if (isl_basic_map_is_empty(part))
2801 isl_basic_map_free(part);
2802 else
2803 result = isl_map_add_basic_map(result, part);
2804 if (!result)
2805 goto error;
2807 isl_map_free(map1);
2808 isl_map_free(map2);
2809 return result;
2810 error:
2811 isl_map_free(map1);
2812 isl_map_free(map2);
2813 return NULL;
2816 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2817 __isl_take isl_map *map2)
2819 if (!map1 || !map2)
2820 goto error;
2821 if (!isl_space_is_equal(map1->dim, map2->dim))
2822 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2823 "spaces don't match", goto error);
2824 return map_intersect_internal(map1, map2);
2825 error:
2826 isl_map_free(map1);
2827 isl_map_free(map2);
2828 return NULL;
2831 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2832 __isl_take isl_map *map2)
2834 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2837 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2839 return (struct isl_set *)
2840 isl_map_intersect((struct isl_map *)set1,
2841 (struct isl_map *)set2);
2844 /* map_intersect_internal accepts intersections
2845 * with parameter domains, so we can just call that function.
2847 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2848 __isl_take isl_set *params)
2850 return map_intersect_internal(map, params);
2853 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2854 __isl_take isl_map *map2)
2856 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2859 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2860 __isl_take isl_set *params)
2862 return isl_map_intersect_params(set, params);
2865 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2867 isl_space *dim;
2868 struct isl_basic_set *bset;
2869 unsigned in;
2871 if (!bmap)
2872 return NULL;
2873 bmap = isl_basic_map_cow(bmap);
2874 if (!bmap)
2875 return NULL;
2876 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2877 in = isl_basic_map_n_in(bmap);
2878 bset = isl_basic_set_from_basic_map(bmap);
2879 bset = isl_basic_set_swap_vars(bset, in);
2880 return isl_basic_map_from_basic_set(bset, dim);
2883 static __isl_give isl_basic_map *basic_map_space_reset(
2884 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2886 isl_space *space;
2888 if (!isl_space_is_named_or_nested(bmap->dim, type))
2889 return bmap;
2891 space = isl_basic_map_get_space(bmap);
2892 space = isl_space_reset(space, type);
2893 bmap = isl_basic_map_reset_space(bmap, space);
2894 return bmap;
2897 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2898 enum isl_dim_type type, unsigned pos, unsigned n)
2900 isl_space *res_dim;
2901 struct isl_basic_map *res;
2902 struct isl_dim_map *dim_map;
2903 unsigned total, off;
2904 enum isl_dim_type t;
2906 if (n == 0)
2907 return basic_map_space_reset(bmap, type);
2909 if (!bmap)
2910 return NULL;
2912 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2914 total = isl_basic_map_total_dim(bmap) + n;
2915 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2916 off = 0;
2917 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2918 if (t != type) {
2919 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2920 } else {
2921 unsigned size = isl_basic_map_dim(bmap, t);
2922 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2923 0, pos, off);
2924 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2925 pos, size - pos, off + pos + n);
2927 off += isl_space_dim(res_dim, t);
2929 isl_dim_map_div(dim_map, bmap, off);
2931 res = isl_basic_map_alloc_space(res_dim,
2932 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2933 if (isl_basic_map_is_rational(bmap))
2934 res = isl_basic_map_set_rational(res);
2935 if (isl_basic_map_plain_is_empty(bmap)) {
2936 isl_basic_map_free(bmap);
2937 return isl_basic_map_set_to_empty(res);
2939 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2940 return isl_basic_map_finalize(res);
2943 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2944 enum isl_dim_type type, unsigned n)
2946 if (!bmap)
2947 return NULL;
2948 return isl_basic_map_insert(bmap, type,
2949 isl_basic_map_dim(bmap, type), n);
2952 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2953 enum isl_dim_type type, unsigned n)
2955 if (!bset)
2956 return NULL;
2957 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2958 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2959 error:
2960 isl_basic_set_free(bset);
2961 return NULL;
2964 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2965 enum isl_dim_type type)
2967 isl_space *space;
2969 if (!map || !isl_space_is_named_or_nested(map->dim, type))
2970 return map;
2972 space = isl_map_get_space(map);
2973 space = isl_space_reset(space, type);
2974 map = isl_map_reset_space(map, space);
2975 return map;
2978 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2979 enum isl_dim_type type, unsigned pos, unsigned n)
2981 int i;
2983 if (n == 0)
2984 return map_space_reset(map, type);
2986 map = isl_map_cow(map);
2987 if (!map)
2988 return NULL;
2990 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
2991 if (!map->dim)
2992 goto error;
2994 for (i = 0; i < map->n; ++i) {
2995 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2996 if (!map->p[i])
2997 goto error;
3000 return map;
3001 error:
3002 isl_map_free(map);
3003 return NULL;
3006 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3007 enum isl_dim_type type, unsigned pos, unsigned n)
3009 return isl_map_insert_dims(set, type, pos, n);
3012 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3013 enum isl_dim_type type, unsigned n)
3015 if (!map)
3016 return NULL;
3017 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3020 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3021 enum isl_dim_type type, unsigned n)
3023 if (!set)
3024 return NULL;
3025 isl_assert(set->ctx, type != isl_dim_in, goto error);
3026 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3027 error:
3028 isl_set_free(set);
3029 return NULL;
3032 __isl_give isl_basic_map *isl_basic_map_move_dims(
3033 __isl_take isl_basic_map *bmap,
3034 enum isl_dim_type dst_type, unsigned dst_pos,
3035 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3037 struct isl_dim_map *dim_map;
3038 struct isl_basic_map *res;
3039 enum isl_dim_type t;
3040 unsigned total, off;
3042 if (!bmap)
3043 return NULL;
3044 if (n == 0)
3045 return bmap;
3047 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3048 goto error);
3050 if (dst_type == src_type && dst_pos == src_pos)
3051 return bmap;
3053 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3055 if (pos(bmap->dim, dst_type) + dst_pos ==
3056 pos(bmap->dim, src_type) + src_pos +
3057 ((src_type < dst_type) ? n : 0)) {
3058 bmap = isl_basic_map_cow(bmap);
3059 if (!bmap)
3060 return NULL;
3062 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3063 src_type, src_pos, n);
3064 if (!bmap->dim)
3065 goto error;
3067 bmap = isl_basic_map_finalize(bmap);
3069 return bmap;
3072 total = isl_basic_map_total_dim(bmap);
3073 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3075 off = 0;
3076 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3077 unsigned size = isl_space_dim(bmap->dim, t);
3078 if (t == dst_type) {
3079 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3080 0, dst_pos, off);
3081 off += dst_pos;
3082 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3083 src_pos, n, off);
3084 off += n;
3085 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3086 dst_pos, size - dst_pos, off);
3087 off += size - dst_pos;
3088 } else if (t == src_type) {
3089 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3090 0, src_pos, off);
3091 off += src_pos;
3092 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3093 src_pos + n, size - src_pos - n, off);
3094 off += size - src_pos - n;
3095 } else {
3096 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3097 off += size;
3100 isl_dim_map_div(dim_map, bmap, off);
3102 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3103 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3104 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3106 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3107 src_type, src_pos, n);
3108 if (!bmap->dim)
3109 goto error;
3111 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3112 bmap = isl_basic_map_gauss(bmap, NULL);
3113 bmap = isl_basic_map_finalize(bmap);
3115 return bmap;
3116 error:
3117 isl_basic_map_free(bmap);
3118 return NULL;
3121 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3122 enum isl_dim_type dst_type, unsigned dst_pos,
3123 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3125 return (isl_basic_set *)isl_basic_map_move_dims(
3126 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3129 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3130 enum isl_dim_type dst_type, unsigned dst_pos,
3131 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3133 if (!set)
3134 return NULL;
3135 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3136 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3137 src_type, src_pos, n);
3138 error:
3139 isl_set_free(set);
3140 return NULL;
3143 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3144 enum isl_dim_type dst_type, unsigned dst_pos,
3145 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3147 int i;
3149 if (!map)
3150 return NULL;
3151 if (n == 0)
3152 return map;
3154 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3155 goto error);
3157 if (dst_type == src_type && dst_pos == src_pos)
3158 return map;
3160 isl_assert(map->ctx, dst_type != src_type, goto error);
3162 map = isl_map_cow(map);
3163 if (!map)
3164 return NULL;
3166 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3167 if (!map->dim)
3168 goto error;
3170 for (i = 0; i < map->n; ++i) {
3171 map->p[i] = isl_basic_map_move_dims(map->p[i],
3172 dst_type, dst_pos,
3173 src_type, src_pos, n);
3174 if (!map->p[i])
3175 goto error;
3178 return map;
3179 error:
3180 isl_map_free(map);
3181 return NULL;
3184 /* Move the specified dimensions to the last columns right before
3185 * the divs. Don't change the dimension specification of bmap.
3186 * That's the responsibility of the caller.
3188 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3189 enum isl_dim_type type, unsigned first, unsigned n)
3191 struct isl_dim_map *dim_map;
3192 struct isl_basic_map *res;
3193 enum isl_dim_type t;
3194 unsigned total, off;
3196 if (!bmap)
3197 return NULL;
3198 if (pos(bmap->dim, type) + first + n ==
3199 1 + isl_space_dim(bmap->dim, isl_dim_all))
3200 return bmap;
3202 total = isl_basic_map_total_dim(bmap);
3203 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3205 off = 0;
3206 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3207 unsigned size = isl_space_dim(bmap->dim, t);
3208 if (t == type) {
3209 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3210 0, first, off);
3211 off += first;
3212 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3213 first, n, total - bmap->n_div - n);
3214 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3215 first + n, size - (first + n), off);
3216 off += size - (first + n);
3217 } else {
3218 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3219 off += size;
3222 isl_dim_map_div(dim_map, bmap, off + n);
3224 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3225 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3226 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3227 return res;
3230 /* Turn the n dimensions of type type, starting at first
3231 * into existentially quantified variables.
3233 __isl_give isl_basic_map *isl_basic_map_project_out(
3234 __isl_take isl_basic_map *bmap,
3235 enum isl_dim_type type, unsigned first, unsigned n)
3237 int i;
3238 size_t row_size;
3239 isl_int **new_div;
3240 isl_int *old;
3242 if (n == 0)
3243 return basic_map_space_reset(bmap, type);
3245 if (!bmap)
3246 return NULL;
3248 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3249 return isl_basic_map_remove_dims(bmap, type, first, n);
3251 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3252 goto error);
3254 bmap = move_last(bmap, type, first, n);
3255 bmap = isl_basic_map_cow(bmap);
3256 if (!bmap)
3257 return NULL;
3259 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3260 old = bmap->block2.data;
3261 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3262 (bmap->extra + n) * (1 + row_size));
3263 if (!bmap->block2.data)
3264 goto error;
3265 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3266 if (!new_div)
3267 goto error;
3268 for (i = 0; i < n; ++i) {
3269 new_div[i] = bmap->block2.data +
3270 (bmap->extra + i) * (1 + row_size);
3271 isl_seq_clr(new_div[i], 1 + row_size);
3273 for (i = 0; i < bmap->extra; ++i)
3274 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3275 free(bmap->div);
3276 bmap->div = new_div;
3277 bmap->n_div += n;
3278 bmap->extra += n;
3280 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3281 if (!bmap->dim)
3282 goto error;
3283 bmap = isl_basic_map_simplify(bmap);
3284 bmap = isl_basic_map_drop_redundant_divs(bmap);
3285 return isl_basic_map_finalize(bmap);
3286 error:
3287 isl_basic_map_free(bmap);
3288 return NULL;
3291 /* Turn the n dimensions of type type, starting at first
3292 * into existentially quantified variables.
3294 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3295 enum isl_dim_type type, unsigned first, unsigned n)
3297 return (isl_basic_set *)isl_basic_map_project_out(
3298 (isl_basic_map *)bset, type, first, n);
3301 /* Turn the n dimensions of type type, starting at first
3302 * into existentially quantified variables.
3304 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3305 enum isl_dim_type type, unsigned first, unsigned n)
3307 int i;
3309 if (!map)
3310 return NULL;
3312 if (n == 0)
3313 return map_space_reset(map, type);
3315 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3317 map = isl_map_cow(map);
3318 if (!map)
3319 return NULL;
3321 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3322 if (!map->dim)
3323 goto error;
3325 for (i = 0; i < map->n; ++i) {
3326 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3327 if (!map->p[i])
3328 goto error;
3331 return map;
3332 error:
3333 isl_map_free(map);
3334 return NULL;
3337 /* Turn the n dimensions of type type, starting at first
3338 * into existentially quantified variables.
3340 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3341 enum isl_dim_type type, unsigned first, unsigned n)
3343 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3346 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3348 int i, j;
3350 for (i = 0; i < n; ++i) {
3351 j = isl_basic_map_alloc_div(bmap);
3352 if (j < 0)
3353 goto error;
3354 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3356 return bmap;
3357 error:
3358 isl_basic_map_free(bmap);
3359 return NULL;
3362 struct isl_basic_map *isl_basic_map_apply_range(
3363 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3365 isl_space *dim_result = NULL;
3366 struct isl_basic_map *bmap;
3367 unsigned n_in, n_out, n, nparam, total, pos;
3368 struct isl_dim_map *dim_map1, *dim_map2;
3370 if (!bmap1 || !bmap2)
3371 goto error;
3373 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3374 isl_space_copy(bmap2->dim));
3376 n_in = isl_basic_map_n_in(bmap1);
3377 n_out = isl_basic_map_n_out(bmap2);
3378 n = isl_basic_map_n_out(bmap1);
3379 nparam = isl_basic_map_n_param(bmap1);
3381 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3382 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3383 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3384 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3385 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3386 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3387 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3388 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3389 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3390 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3391 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3393 bmap = isl_basic_map_alloc_space(dim_result,
3394 bmap1->n_div + bmap2->n_div + n,
3395 bmap1->n_eq + bmap2->n_eq,
3396 bmap1->n_ineq + bmap2->n_ineq);
3397 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3398 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3399 bmap = add_divs(bmap, n);
3400 bmap = isl_basic_map_simplify(bmap);
3401 bmap = isl_basic_map_drop_redundant_divs(bmap);
3402 return isl_basic_map_finalize(bmap);
3403 error:
3404 isl_basic_map_free(bmap1);
3405 isl_basic_map_free(bmap2);
3406 return NULL;
3409 struct isl_basic_set *isl_basic_set_apply(
3410 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3412 if (!bset || !bmap)
3413 goto error;
3415 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3416 goto error);
3418 return (struct isl_basic_set *)
3419 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3420 error:
3421 isl_basic_set_free(bset);
3422 isl_basic_map_free(bmap);
3423 return NULL;
3426 struct isl_basic_map *isl_basic_map_apply_domain(
3427 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3429 if (!bmap1 || !bmap2)
3430 goto error;
3432 isl_assert(bmap1->ctx,
3433 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3434 isl_assert(bmap1->ctx,
3435 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3436 goto error);
3438 bmap1 = isl_basic_map_reverse(bmap1);
3439 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3440 return isl_basic_map_reverse(bmap1);
3441 error:
3442 isl_basic_map_free(bmap1);
3443 isl_basic_map_free(bmap2);
3444 return NULL;
3447 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3448 * A \cap B -> f(A) + f(B)
3450 struct isl_basic_map *isl_basic_map_sum(
3451 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3453 unsigned n_in, n_out, nparam, total, pos;
3454 struct isl_basic_map *bmap = NULL;
3455 struct isl_dim_map *dim_map1, *dim_map2;
3456 int i;
3458 if (!bmap1 || !bmap2)
3459 goto error;
3461 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3462 goto error);
3464 nparam = isl_basic_map_n_param(bmap1);
3465 n_in = isl_basic_map_n_in(bmap1);
3466 n_out = isl_basic_map_n_out(bmap1);
3468 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3469 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3470 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3471 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3472 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3473 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3474 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3475 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3476 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3477 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3478 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3480 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3481 bmap1->n_div + bmap2->n_div + 2 * n_out,
3482 bmap1->n_eq + bmap2->n_eq + n_out,
3483 bmap1->n_ineq + bmap2->n_ineq);
3484 for (i = 0; i < n_out; ++i) {
3485 int j = isl_basic_map_alloc_equality(bmap);
3486 if (j < 0)
3487 goto error;
3488 isl_seq_clr(bmap->eq[j], 1+total);
3489 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3490 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3491 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3493 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3494 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3495 bmap = add_divs(bmap, 2 * n_out);
3497 bmap = isl_basic_map_simplify(bmap);
3498 return isl_basic_map_finalize(bmap);
3499 error:
3500 isl_basic_map_free(bmap);
3501 isl_basic_map_free(bmap1);
3502 isl_basic_map_free(bmap2);
3503 return NULL;
3506 /* Given two maps A -> f(A) and B -> g(B), construct a map
3507 * A \cap B -> f(A) + f(B)
3509 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3511 struct isl_map *result;
3512 int i, j;
3514 if (!map1 || !map2)
3515 goto error;
3517 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3519 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3520 map1->n * map2->n, 0);
3521 if (!result)
3522 goto error;
3523 for (i = 0; i < map1->n; ++i)
3524 for (j = 0; j < map2->n; ++j) {
3525 struct isl_basic_map *part;
3526 part = isl_basic_map_sum(
3527 isl_basic_map_copy(map1->p[i]),
3528 isl_basic_map_copy(map2->p[j]));
3529 if (isl_basic_map_is_empty(part))
3530 isl_basic_map_free(part);
3531 else
3532 result = isl_map_add_basic_map(result, part);
3533 if (!result)
3534 goto error;
3536 isl_map_free(map1);
3537 isl_map_free(map2);
3538 return result;
3539 error:
3540 isl_map_free(map1);
3541 isl_map_free(map2);
3542 return NULL;
3545 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3546 __isl_take isl_set *set2)
3548 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3551 /* Given a basic map A -> f(A), construct A -> -f(A).
3553 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3555 int i, j;
3556 unsigned off, n;
3558 bmap = isl_basic_map_cow(bmap);
3559 if (!bmap)
3560 return NULL;
3562 n = isl_basic_map_dim(bmap, isl_dim_out);
3563 off = isl_basic_map_offset(bmap, isl_dim_out);
3564 for (i = 0; i < bmap->n_eq; ++i)
3565 for (j = 0; j < n; ++j)
3566 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3567 for (i = 0; i < bmap->n_ineq; ++i)
3568 for (j = 0; j < n; ++j)
3569 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3570 for (i = 0; i < bmap->n_div; ++i)
3571 for (j = 0; j < n; ++j)
3572 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3573 bmap = isl_basic_map_gauss(bmap, NULL);
3574 return isl_basic_map_finalize(bmap);
3577 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3579 return isl_basic_map_neg(bset);
3582 /* Given a map A -> f(A), construct A -> -f(A).
3584 struct isl_map *isl_map_neg(struct isl_map *map)
3586 int i;
3588 map = isl_map_cow(map);
3589 if (!map)
3590 return NULL;
3592 for (i = 0; i < map->n; ++i) {
3593 map->p[i] = isl_basic_map_neg(map->p[i]);
3594 if (!map->p[i])
3595 goto error;
3598 return map;
3599 error:
3600 isl_map_free(map);
3601 return NULL;
3604 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3606 return (isl_set *)isl_map_neg((isl_map *)set);
3609 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3610 * A -> floor(f(A)/d).
3612 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3613 isl_int d)
3615 unsigned n_in, n_out, nparam, total, pos;
3616 struct isl_basic_map *result = NULL;
3617 struct isl_dim_map *dim_map;
3618 int i;
3620 if (!bmap)
3621 return NULL;
3623 nparam = isl_basic_map_n_param(bmap);
3624 n_in = isl_basic_map_n_in(bmap);
3625 n_out = isl_basic_map_n_out(bmap);
3627 total = nparam + n_in + n_out + bmap->n_div + n_out;
3628 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3629 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3630 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3631 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3632 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3634 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3635 bmap->n_div + n_out,
3636 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3637 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3638 result = add_divs(result, n_out);
3639 for (i = 0; i < n_out; ++i) {
3640 int j;
3641 j = isl_basic_map_alloc_inequality(result);
3642 if (j < 0)
3643 goto error;
3644 isl_seq_clr(result->ineq[j], 1+total);
3645 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3646 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3647 j = isl_basic_map_alloc_inequality(result);
3648 if (j < 0)
3649 goto error;
3650 isl_seq_clr(result->ineq[j], 1+total);
3651 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3652 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3653 isl_int_sub_ui(result->ineq[j][0], d, 1);
3656 result = isl_basic_map_simplify(result);
3657 return isl_basic_map_finalize(result);
3658 error:
3659 isl_basic_map_free(result);
3660 return NULL;
3663 /* Given a map A -> f(A) and an integer d, construct a map
3664 * A -> floor(f(A)/d).
3666 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3668 int i;
3670 map = isl_map_cow(map);
3671 if (!map)
3672 return NULL;
3674 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3675 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3676 for (i = 0; i < map->n; ++i) {
3677 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3678 if (!map->p[i])
3679 goto error;
3682 return map;
3683 error:
3684 isl_map_free(map);
3685 return NULL;
3688 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3690 int i;
3691 unsigned nparam;
3692 unsigned n_in;
3694 i = isl_basic_map_alloc_equality(bmap);
3695 if (i < 0)
3696 goto error;
3697 nparam = isl_basic_map_n_param(bmap);
3698 n_in = isl_basic_map_n_in(bmap);
3699 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3700 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3701 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3702 return isl_basic_map_finalize(bmap);
3703 error:
3704 isl_basic_map_free(bmap);
3705 return NULL;
3708 /* Add a constraints to "bmap" expressing i_pos < o_pos
3710 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3712 int i;
3713 unsigned nparam;
3714 unsigned n_in;
3716 i = isl_basic_map_alloc_inequality(bmap);
3717 if (i < 0)
3718 goto error;
3719 nparam = isl_basic_map_n_param(bmap);
3720 n_in = isl_basic_map_n_in(bmap);
3721 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3722 isl_int_set_si(bmap->ineq[i][0], -1);
3723 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3724 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3725 return isl_basic_map_finalize(bmap);
3726 error:
3727 isl_basic_map_free(bmap);
3728 return NULL;
3731 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3733 static __isl_give isl_basic_map *var_less_or_equal(
3734 __isl_take isl_basic_map *bmap, unsigned pos)
3736 int i;
3737 unsigned nparam;
3738 unsigned n_in;
3740 i = isl_basic_map_alloc_inequality(bmap);
3741 if (i < 0)
3742 goto error;
3743 nparam = isl_basic_map_n_param(bmap);
3744 n_in = isl_basic_map_n_in(bmap);
3745 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3746 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3747 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3748 return isl_basic_map_finalize(bmap);
3749 error:
3750 isl_basic_map_free(bmap);
3751 return NULL;
3754 /* Add a constraints to "bmap" expressing i_pos > o_pos
3756 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3758 int i;
3759 unsigned nparam;
3760 unsigned n_in;
3762 i = isl_basic_map_alloc_inequality(bmap);
3763 if (i < 0)
3764 goto error;
3765 nparam = isl_basic_map_n_param(bmap);
3766 n_in = isl_basic_map_n_in(bmap);
3767 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3768 isl_int_set_si(bmap->ineq[i][0], -1);
3769 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3770 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3771 return isl_basic_map_finalize(bmap);
3772 error:
3773 isl_basic_map_free(bmap);
3774 return NULL;
3777 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3779 static __isl_give isl_basic_map *var_more_or_equal(
3780 __isl_take isl_basic_map *bmap, unsigned pos)
3782 int i;
3783 unsigned nparam;
3784 unsigned n_in;
3786 i = isl_basic_map_alloc_inequality(bmap);
3787 if (i < 0)
3788 goto error;
3789 nparam = isl_basic_map_n_param(bmap);
3790 n_in = isl_basic_map_n_in(bmap);
3791 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3792 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3793 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3794 return isl_basic_map_finalize(bmap);
3795 error:
3796 isl_basic_map_free(bmap);
3797 return NULL;
3800 __isl_give isl_basic_map *isl_basic_map_equal(
3801 __isl_take isl_space *dim, unsigned n_equal)
3803 int i;
3804 struct isl_basic_map *bmap;
3805 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3806 if (!bmap)
3807 return NULL;
3808 for (i = 0; i < n_equal && bmap; ++i)
3809 bmap = var_equal(bmap, i);
3810 return isl_basic_map_finalize(bmap);
3813 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3815 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3816 unsigned pos)
3818 int i;
3819 struct isl_basic_map *bmap;
3820 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3821 if (!bmap)
3822 return NULL;
3823 for (i = 0; i < pos && bmap; ++i)
3824 bmap = var_equal(bmap, i);
3825 if (bmap)
3826 bmap = var_less(bmap, pos);
3827 return isl_basic_map_finalize(bmap);
3830 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3832 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3833 __isl_take isl_space *dim, unsigned pos)
3835 int i;
3836 isl_basic_map *bmap;
3838 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3839 for (i = 0; i < pos; ++i)
3840 bmap = var_equal(bmap, i);
3841 bmap = var_less_or_equal(bmap, pos);
3842 return isl_basic_map_finalize(bmap);
3845 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3847 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3848 unsigned pos)
3850 int i;
3851 struct isl_basic_map *bmap;
3852 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3853 if (!bmap)
3854 return NULL;
3855 for (i = 0; i < pos && bmap; ++i)
3856 bmap = var_equal(bmap, i);
3857 if (bmap)
3858 bmap = var_more(bmap, pos);
3859 return isl_basic_map_finalize(bmap);
3862 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3864 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3865 __isl_take isl_space *dim, unsigned pos)
3867 int i;
3868 isl_basic_map *bmap;
3870 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3871 for (i = 0; i < pos; ++i)
3872 bmap = var_equal(bmap, i);
3873 bmap = var_more_or_equal(bmap, pos);
3874 return isl_basic_map_finalize(bmap);
3877 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3878 unsigned n, int equal)
3880 struct isl_map *map;
3881 int i;
3883 if (n == 0 && equal)
3884 return isl_map_universe(dims);
3886 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3888 for (i = 0; i + 1 < n; ++i)
3889 map = isl_map_add_basic_map(map,
3890 isl_basic_map_less_at(isl_space_copy(dims), i));
3891 if (n > 0) {
3892 if (equal)
3893 map = isl_map_add_basic_map(map,
3894 isl_basic_map_less_or_equal_at(dims, n - 1));
3895 else
3896 map = isl_map_add_basic_map(map,
3897 isl_basic_map_less_at(dims, n - 1));
3898 } else
3899 isl_space_free(dims);
3901 return map;
3904 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3906 if (!dims)
3907 return NULL;
3908 return map_lex_lte_first(dims, dims->n_out, equal);
3911 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3913 return map_lex_lte_first(dim, n, 0);
3916 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3918 return map_lex_lte_first(dim, n, 1);
3921 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3923 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3926 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3928 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3931 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3932 unsigned n, int equal)
3934 struct isl_map *map;
3935 int i;
3937 if (n == 0 && equal)
3938 return isl_map_universe(dims);
3940 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3942 for (i = 0; i + 1 < n; ++i)
3943 map = isl_map_add_basic_map(map,
3944 isl_basic_map_more_at(isl_space_copy(dims), i));
3945 if (n > 0) {
3946 if (equal)
3947 map = isl_map_add_basic_map(map,
3948 isl_basic_map_more_or_equal_at(dims, n - 1));
3949 else
3950 map = isl_map_add_basic_map(map,
3951 isl_basic_map_more_at(dims, n - 1));
3952 } else
3953 isl_space_free(dims);
3955 return map;
3958 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3960 if (!dims)
3961 return NULL;
3962 return map_lex_gte_first(dims, dims->n_out, equal);
3965 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3967 return map_lex_gte_first(dim, n, 0);
3970 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3972 return map_lex_gte_first(dim, n, 1);
3975 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3977 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3980 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
3982 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
3985 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3986 __isl_take isl_set *set2)
3988 isl_map *map;
3989 map = isl_map_lex_le(isl_set_get_space(set1));
3990 map = isl_map_intersect_domain(map, set1);
3991 map = isl_map_intersect_range(map, set2);
3992 return map;
3995 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3996 __isl_take isl_set *set2)
3998 isl_map *map;
3999 map = isl_map_lex_lt(isl_set_get_space(set1));
4000 map = isl_map_intersect_domain(map, set1);
4001 map = isl_map_intersect_range(map, set2);
4002 return map;
4005 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4006 __isl_take isl_set *set2)
4008 isl_map *map;
4009 map = isl_map_lex_ge(isl_set_get_space(set1));
4010 map = isl_map_intersect_domain(map, set1);
4011 map = isl_map_intersect_range(map, set2);
4012 return map;
4015 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4016 __isl_take isl_set *set2)
4018 isl_map *map;
4019 map = isl_map_lex_gt(isl_set_get_space(set1));
4020 map = isl_map_intersect_domain(map, set1);
4021 map = isl_map_intersect_range(map, set2);
4022 return map;
4025 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4026 __isl_take isl_map *map2)
4028 isl_map *map;
4029 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4030 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4031 map = isl_map_apply_range(map, isl_map_reverse(map2));
4032 return map;
4035 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4036 __isl_take isl_map *map2)
4038 isl_map *map;
4039 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4040 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4041 map = isl_map_apply_range(map, isl_map_reverse(map2));
4042 return map;
4045 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4046 __isl_take isl_map *map2)
4048 isl_map *map;
4049 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4050 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4051 map = isl_map_apply_range(map, isl_map_reverse(map2));
4052 return map;
4055 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4056 __isl_take isl_map *map2)
4058 isl_map *map;
4059 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4060 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4061 map = isl_map_apply_range(map, isl_map_reverse(map2));
4062 return map;
4065 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4066 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4068 struct isl_basic_map *bmap;
4070 bset = isl_basic_set_cow(bset);
4071 if (!bset || !dim)
4072 goto error;
4074 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4075 isl_space_free(bset->dim);
4076 bmap = (struct isl_basic_map *) bset;
4077 bmap->dim = dim;
4078 return isl_basic_map_finalize(bmap);
4079 error:
4080 isl_basic_set_free(bset);
4081 isl_space_free(dim);
4082 return NULL;
4085 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4087 if (!bmap)
4088 goto error;
4089 if (bmap->dim->n_in == 0)
4090 return (struct isl_basic_set *)bmap;
4091 bmap = isl_basic_map_cow(bmap);
4092 if (!bmap)
4093 goto error;
4094 bmap->dim = isl_space_as_set_space(bmap->dim);
4095 if (!bmap->dim)
4096 goto error;
4097 bmap = isl_basic_map_finalize(bmap);
4098 return (struct isl_basic_set *)bmap;
4099 error:
4100 isl_basic_map_free(bmap);
4101 return NULL;
4104 /* For a div d = floor(f/m), add the constraints
4106 * f - m d >= 0
4107 * -(f-(n-1)) + m d >= 0
4109 * Note that the second constraint is the negation of
4111 * f - m d >= n
4113 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4114 unsigned pos, isl_int *div)
4116 int i, j;
4117 unsigned total = isl_basic_map_total_dim(bmap);
4119 i = isl_basic_map_alloc_inequality(bmap);
4120 if (i < 0)
4121 return -1;
4122 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4123 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4125 j = isl_basic_map_alloc_inequality(bmap);
4126 if (j < 0)
4127 return -1;
4128 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
4129 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
4130 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
4131 return j;
4134 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4135 unsigned pos, isl_int *div)
4137 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4138 pos, div);
4141 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4143 unsigned total = isl_basic_map_total_dim(bmap);
4144 unsigned div_pos = total - bmap->n_div + div;
4146 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4147 bmap->div[div]);
4150 struct isl_basic_set *isl_basic_map_underlying_set(
4151 struct isl_basic_map *bmap)
4153 if (!bmap)
4154 goto error;
4155 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4156 bmap->n_div == 0 &&
4157 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4158 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4159 return (struct isl_basic_set *)bmap;
4160 bmap = isl_basic_map_cow(bmap);
4161 if (!bmap)
4162 goto error;
4163 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4164 if (!bmap->dim)
4165 goto error;
4166 bmap->extra -= bmap->n_div;
4167 bmap->n_div = 0;
4168 bmap = isl_basic_map_finalize(bmap);
4169 return (struct isl_basic_set *)bmap;
4170 error:
4171 return NULL;
4174 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4175 __isl_take isl_basic_set *bset)
4177 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4180 struct isl_basic_map *isl_basic_map_overlying_set(
4181 struct isl_basic_set *bset, struct isl_basic_map *like)
4183 struct isl_basic_map *bmap;
4184 struct isl_ctx *ctx;
4185 unsigned total;
4186 int i;
4188 if (!bset || !like)
4189 goto error;
4190 ctx = bset->ctx;
4191 isl_assert(ctx, bset->n_div == 0, goto error);
4192 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4193 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4194 goto error);
4195 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4196 isl_basic_map_free(like);
4197 return (struct isl_basic_map *)bset;
4199 bset = isl_basic_set_cow(bset);
4200 if (!bset)
4201 goto error;
4202 total = bset->dim->n_out + bset->extra;
4203 bmap = (struct isl_basic_map *)bset;
4204 isl_space_free(bmap->dim);
4205 bmap->dim = isl_space_copy(like->dim);
4206 if (!bmap->dim)
4207 goto error;
4208 bmap->n_div = like->n_div;
4209 bmap->extra += like->n_div;
4210 if (bmap->extra) {
4211 unsigned ltotal;
4212 isl_int **div;
4213 ltotal = total - bmap->extra + like->extra;
4214 if (ltotal > total)
4215 ltotal = total;
4216 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4217 bmap->extra * (1 + 1 + total));
4218 if (isl_blk_is_error(bmap->block2))
4219 goto error;
4220 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4221 if (!div)
4222 goto error;
4223 bmap->div = div;
4224 for (i = 0; i < bmap->extra; ++i)
4225 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4226 for (i = 0; i < like->n_div; ++i) {
4227 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4228 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4230 bmap = isl_basic_map_extend_constraints(bmap,
4231 0, 2 * like->n_div);
4232 for (i = 0; i < like->n_div; ++i) {
4233 if (isl_int_is_zero(bmap->div[i][0]))
4234 continue;
4235 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4236 goto error;
4239 isl_basic_map_free(like);
4240 bmap = isl_basic_map_simplify(bmap);
4241 bmap = isl_basic_map_finalize(bmap);
4242 return bmap;
4243 error:
4244 isl_basic_map_free(like);
4245 isl_basic_set_free(bset);
4246 return NULL;
4249 struct isl_basic_set *isl_basic_set_from_underlying_set(
4250 struct isl_basic_set *bset, struct isl_basic_set *like)
4252 return (struct isl_basic_set *)
4253 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4256 struct isl_set *isl_set_from_underlying_set(
4257 struct isl_set *set, struct isl_basic_set *like)
4259 int i;
4261 if (!set || !like)
4262 goto error;
4263 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4264 goto error);
4265 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4266 isl_basic_set_free(like);
4267 return set;
4269 set = isl_set_cow(set);
4270 if (!set)
4271 goto error;
4272 for (i = 0; i < set->n; ++i) {
4273 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4274 isl_basic_set_copy(like));
4275 if (!set->p[i])
4276 goto error;
4278 isl_space_free(set->dim);
4279 set->dim = isl_space_copy(like->dim);
4280 if (!set->dim)
4281 goto error;
4282 isl_basic_set_free(like);
4283 return set;
4284 error:
4285 isl_basic_set_free(like);
4286 isl_set_free(set);
4287 return NULL;
4290 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4292 int i;
4294 map = isl_map_cow(map);
4295 if (!map)
4296 return NULL;
4297 map->dim = isl_space_cow(map->dim);
4298 if (!map->dim)
4299 goto error;
4301 for (i = 1; i < map->n; ++i)
4302 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4303 goto error);
4304 for (i = 0; i < map->n; ++i) {
4305 map->p[i] = (struct isl_basic_map *)
4306 isl_basic_map_underlying_set(map->p[i]);
4307 if (!map->p[i])
4308 goto error;
4310 if (map->n == 0)
4311 map->dim = isl_space_underlying(map->dim, 0);
4312 else {
4313 isl_space_free(map->dim);
4314 map->dim = isl_space_copy(map->p[0]->dim);
4316 if (!map->dim)
4317 goto error;
4318 return (struct isl_set *)map;
4319 error:
4320 isl_map_free(map);
4321 return NULL;
4324 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4326 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4329 __isl_give isl_basic_map *isl_basic_map_reset_space(
4330 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4332 bmap = isl_basic_map_cow(bmap);
4333 if (!bmap || !dim)
4334 goto error;
4336 isl_space_free(bmap->dim);
4337 bmap->dim = dim;
4339 bmap = isl_basic_map_finalize(bmap);
4341 return bmap;
4342 error:
4343 isl_basic_map_free(bmap);
4344 isl_space_free(dim);
4345 return NULL;
4348 __isl_give isl_basic_set *isl_basic_set_reset_space(
4349 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4351 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4352 dim);
4355 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4356 __isl_take isl_space *dim)
4358 int i;
4360 map = isl_map_cow(map);
4361 if (!map || !dim)
4362 goto error;
4364 for (i = 0; i < map->n; ++i) {
4365 map->p[i] = isl_basic_map_reset_space(map->p[i],
4366 isl_space_copy(dim));
4367 if (!map->p[i])
4368 goto error;
4370 isl_space_free(map->dim);
4371 map->dim = dim;
4373 return map;
4374 error:
4375 isl_map_free(map);
4376 isl_space_free(dim);
4377 return NULL;
4380 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4381 __isl_take isl_space *dim)
4383 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4386 /* Compute the parameter domain of the given basic set.
4388 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4390 isl_space *space;
4391 unsigned n;
4393 if (isl_basic_set_is_params(bset))
4394 return bset;
4396 n = isl_basic_set_dim(bset, isl_dim_set);
4397 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4398 space = isl_basic_set_get_space(bset);
4399 space = isl_space_params(space);
4400 bset = isl_basic_set_reset_space(bset, space);
4401 return bset;
4404 /* Compute the parameter domain of the given set.
4406 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4408 isl_space *space;
4409 unsigned n;
4411 if (isl_set_is_params(set))
4412 return set;
4414 n = isl_set_dim(set, isl_dim_set);
4415 set = isl_set_project_out(set, isl_dim_set, 0, n);
4416 space = isl_set_get_space(set);
4417 space = isl_space_params(space);
4418 set = isl_set_reset_space(set, space);
4419 return set;
4422 /* Construct a zero-dimensional set with the given parameter domain.
4424 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4426 isl_space *space;
4427 space = isl_set_get_space(set);
4428 space = isl_space_set_from_params(space);
4429 set = isl_set_reset_space(set, space);
4430 return set;
4433 /* Compute the parameter domain of the given map.
4435 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4437 isl_space *space;
4438 unsigned n;
4440 n = isl_map_dim(map, isl_dim_in);
4441 map = isl_map_project_out(map, isl_dim_in, 0, n);
4442 n = isl_map_dim(map, isl_dim_out);
4443 map = isl_map_project_out(map, isl_dim_out, 0, n);
4444 space = isl_map_get_space(map);
4445 space = isl_space_params(space);
4446 map = isl_map_reset_space(map, space);
4447 return map;
4450 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4452 isl_space *dim;
4453 struct isl_basic_set *domain;
4454 unsigned n_in;
4455 unsigned n_out;
4457 if (!bmap)
4458 return NULL;
4459 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4461 n_in = isl_basic_map_n_in(bmap);
4462 n_out = isl_basic_map_n_out(bmap);
4463 domain = isl_basic_set_from_basic_map(bmap);
4464 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4466 domain = isl_basic_set_reset_space(domain, dim);
4468 return domain;
4471 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4473 if (!bmap)
4474 return -1;
4475 return isl_space_may_be_set(bmap->dim);
4478 /* Is this basic map actually a set?
4479 * Users should never call this function. Outside of isl,
4480 * the type should indicate whether something is a set or a map.
4482 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4484 if (!bmap)
4485 return -1;
4486 return isl_space_is_set(bmap->dim);
4489 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4491 if (!bmap)
4492 return NULL;
4493 if (isl_basic_map_is_set(bmap))
4494 return bmap;
4495 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4498 __isl_give isl_basic_map *isl_basic_map_domain_map(
4499 __isl_take isl_basic_map *bmap)
4501 int i, k;
4502 isl_space *dim;
4503 isl_basic_map *domain;
4504 int nparam, n_in, n_out;
4505 unsigned total;
4507 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4508 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4509 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4511 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4512 domain = isl_basic_map_universe(dim);
4514 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4515 bmap = isl_basic_map_apply_range(bmap, domain);
4516 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4518 total = isl_basic_map_total_dim(bmap);
4520 for (i = 0; i < n_in; ++i) {
4521 k = isl_basic_map_alloc_equality(bmap);
4522 if (k < 0)
4523 goto error;
4524 isl_seq_clr(bmap->eq[k], 1 + total);
4525 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4526 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4529 bmap = isl_basic_map_gauss(bmap, NULL);
4530 return isl_basic_map_finalize(bmap);
4531 error:
4532 isl_basic_map_free(bmap);
4533 return NULL;
4536 __isl_give isl_basic_map *isl_basic_map_range_map(
4537 __isl_take isl_basic_map *bmap)
4539 int i, k;
4540 isl_space *dim;
4541 isl_basic_map *range;
4542 int nparam, n_in, n_out;
4543 unsigned total;
4545 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4546 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4547 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4549 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4550 range = isl_basic_map_universe(dim);
4552 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4553 bmap = isl_basic_map_apply_range(bmap, range);
4554 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4556 total = isl_basic_map_total_dim(bmap);
4558 for (i = 0; i < n_out; ++i) {
4559 k = isl_basic_map_alloc_equality(bmap);
4560 if (k < 0)
4561 goto error;
4562 isl_seq_clr(bmap->eq[k], 1 + total);
4563 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4564 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4567 bmap = isl_basic_map_gauss(bmap, NULL);
4568 return isl_basic_map_finalize(bmap);
4569 error:
4570 isl_basic_map_free(bmap);
4571 return NULL;
4574 int isl_map_may_be_set(__isl_keep isl_map *map)
4576 if (!map)
4577 return -1;
4578 return isl_space_may_be_set(map->dim);
4581 /* Is this map actually a set?
4582 * Users should never call this function. Outside of isl,
4583 * the type should indicate whether something is a set or a map.
4585 int isl_map_is_set(__isl_keep isl_map *map)
4587 if (!map)
4588 return -1;
4589 return isl_space_is_set(map->dim);
4592 struct isl_set *isl_map_range(struct isl_map *map)
4594 int i;
4595 struct isl_set *set;
4597 if (!map)
4598 goto error;
4599 if (isl_map_is_set(map))
4600 return (isl_set *)map;
4602 map = isl_map_cow(map);
4603 if (!map)
4604 goto error;
4606 set = (struct isl_set *) map;
4607 set->dim = isl_space_range(set->dim);
4608 if (!set->dim)
4609 goto error;
4610 for (i = 0; i < map->n; ++i) {
4611 set->p[i] = isl_basic_map_range(map->p[i]);
4612 if (!set->p[i])
4613 goto error;
4615 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4616 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4617 return set;
4618 error:
4619 isl_map_free(map);
4620 return NULL;
4623 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4625 int i;
4626 isl_space *domain_dim;
4628 map = isl_map_cow(map);
4629 if (!map)
4630 return NULL;
4632 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4633 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4634 map->dim = isl_space_join(map->dim, domain_dim);
4635 if (!map->dim)
4636 goto error;
4637 for (i = 0; i < map->n; ++i) {
4638 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4639 if (!map->p[i])
4640 goto error;
4642 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4643 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4644 return map;
4645 error:
4646 isl_map_free(map);
4647 return NULL;
4650 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4652 int i;
4653 isl_space *range_dim;
4655 map = isl_map_cow(map);
4656 if (!map)
4657 return NULL;
4659 range_dim = isl_space_range(isl_map_get_space(map));
4660 range_dim = isl_space_from_range(range_dim);
4661 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4662 map->dim = isl_space_join(map->dim, range_dim);
4663 if (!map->dim)
4664 goto error;
4665 for (i = 0; i < map->n; ++i) {
4666 map->p[i] = isl_basic_map_range_map(map->p[i]);
4667 if (!map->p[i])
4668 goto error;
4670 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4671 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4672 return map;
4673 error:
4674 isl_map_free(map);
4675 return NULL;
4678 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4679 __isl_take isl_space *dim)
4681 int i;
4682 struct isl_map *map = NULL;
4684 set = isl_set_cow(set);
4685 if (!set || !dim)
4686 goto error;
4687 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4688 map = (struct isl_map *)set;
4689 for (i = 0; i < set->n; ++i) {
4690 map->p[i] = isl_basic_map_from_basic_set(
4691 set->p[i], isl_space_copy(dim));
4692 if (!map->p[i])
4693 goto error;
4695 isl_space_free(map->dim);
4696 map->dim = dim;
4697 return map;
4698 error:
4699 isl_space_free(dim);
4700 isl_set_free(set);
4701 return NULL;
4704 __isl_give isl_basic_map *isl_basic_map_from_domain(
4705 __isl_take isl_basic_set *bset)
4707 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4710 __isl_give isl_basic_map *isl_basic_map_from_range(
4711 __isl_take isl_basic_set *bset)
4713 isl_space *space;
4714 space = isl_basic_set_get_space(bset);
4715 space = isl_space_from_range(space);
4716 bset = isl_basic_set_reset_space(bset, space);
4717 return (isl_basic_map *)bset;
4720 struct isl_map *isl_map_from_range(struct isl_set *set)
4722 isl_space *space;
4723 space = isl_set_get_space(set);
4724 space = isl_space_from_range(space);
4725 set = isl_set_reset_space(set, space);
4726 return (struct isl_map *)set;
4729 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4731 return isl_map_reverse(isl_map_from_range(set));
4734 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4735 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4737 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4740 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4741 __isl_take isl_set *range)
4743 return isl_map_apply_range(isl_map_reverse(domain), range);
4746 struct isl_set *isl_set_from_map(struct isl_map *map)
4748 int i;
4749 struct isl_set *set = NULL;
4751 if (!map)
4752 return NULL;
4753 map = isl_map_cow(map);
4754 if (!map)
4755 return NULL;
4756 map->dim = isl_space_as_set_space(map->dim);
4757 if (!map->dim)
4758 goto error;
4759 set = (struct isl_set *)map;
4760 for (i = 0; i < map->n; ++i) {
4761 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4762 if (!set->p[i])
4763 goto error;
4765 return set;
4766 error:
4767 isl_map_free(map);
4768 return NULL;
4771 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4772 unsigned flags)
4774 struct isl_map *map;
4776 if (!dim)
4777 return NULL;
4778 if (n < 0)
4779 isl_die(dim->ctx, isl_error_internal,
4780 "negative number of basic maps", goto error);
4781 map = isl_alloc(dim->ctx, struct isl_map,
4782 sizeof(struct isl_map) +
4783 (n - 1) * sizeof(struct isl_basic_map *));
4784 if (!map)
4785 goto error;
4787 map->ctx = dim->ctx;
4788 isl_ctx_ref(map->ctx);
4789 map->ref = 1;
4790 map->size = n;
4791 map->n = 0;
4792 map->dim = dim;
4793 map->flags = flags;
4794 return map;
4795 error:
4796 isl_space_free(dim);
4797 return NULL;
4800 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4801 unsigned nparam, unsigned in, unsigned out, int n,
4802 unsigned flags)
4804 struct isl_map *map;
4805 isl_space *dims;
4807 dims = isl_space_alloc(ctx, nparam, in, out);
4808 if (!dims)
4809 return NULL;
4811 map = isl_map_alloc_space(dims, n, flags);
4812 return map;
4815 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4817 struct isl_basic_map *bmap;
4818 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4819 bmap = isl_basic_map_set_to_empty(bmap);
4820 return bmap;
4823 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4825 struct isl_basic_set *bset;
4826 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4827 bset = isl_basic_set_set_to_empty(bset);
4828 return bset;
4831 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4833 struct isl_basic_map *bmap;
4834 if (!model)
4835 return NULL;
4836 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4837 bmap = isl_basic_map_set_to_empty(bmap);
4838 return bmap;
4841 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4843 struct isl_basic_map *bmap;
4844 if (!model)
4845 return NULL;
4846 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4847 bmap = isl_basic_map_set_to_empty(bmap);
4848 return bmap;
4851 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4853 struct isl_basic_set *bset;
4854 if (!model)
4855 return NULL;
4856 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4857 bset = isl_basic_set_set_to_empty(bset);
4858 return bset;
4861 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4863 struct isl_basic_map *bmap;
4864 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4865 bmap = isl_basic_map_finalize(bmap);
4866 return bmap;
4869 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4871 struct isl_basic_set *bset;
4872 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4873 bset = isl_basic_set_finalize(bset);
4874 return bset;
4877 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4879 int i;
4880 unsigned total = isl_space_dim(dim, isl_dim_all);
4881 isl_basic_map *bmap;
4883 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4884 for (i = 0; i < total; ++i) {
4885 int k = isl_basic_map_alloc_inequality(bmap);
4886 if (k < 0)
4887 goto error;
4888 isl_seq_clr(bmap->ineq[k], 1 + total);
4889 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4891 return bmap;
4892 error:
4893 isl_basic_map_free(bmap);
4894 return NULL;
4897 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4899 return isl_basic_map_nat_universe(dim);
4902 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4904 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4907 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4909 return isl_map_nat_universe(dim);
4912 __isl_give isl_basic_map *isl_basic_map_universe_like(
4913 __isl_keep isl_basic_map *model)
4915 if (!model)
4916 return NULL;
4917 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4920 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4922 if (!model)
4923 return NULL;
4924 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4927 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4928 __isl_keep isl_set *model)
4930 if (!model)
4931 return NULL;
4932 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4935 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4937 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4940 struct isl_map *isl_map_empty_like(struct isl_map *model)
4942 if (!model)
4943 return NULL;
4944 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4947 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4949 if (!model)
4950 return NULL;
4951 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4954 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4956 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4959 struct isl_set *isl_set_empty_like(struct isl_set *model)
4961 if (!model)
4962 return NULL;
4963 return isl_set_empty(isl_space_copy(model->dim));
4966 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4968 struct isl_map *map;
4969 if (!dim)
4970 return NULL;
4971 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4972 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4973 return map;
4976 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4978 struct isl_set *set;
4979 if (!dim)
4980 return NULL;
4981 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4982 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4983 return set;
4986 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4988 if (!model)
4989 return NULL;
4990 return isl_set_universe(isl_space_copy(model->dim));
4993 struct isl_map *isl_map_dup(struct isl_map *map)
4995 int i;
4996 struct isl_map *dup;
4998 if (!map)
4999 return NULL;
5000 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5001 for (i = 0; i < map->n; ++i)
5002 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5003 return dup;
5006 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5007 __isl_take isl_basic_map *bmap)
5009 if (!bmap || !map)
5010 goto error;
5011 if (isl_basic_map_plain_is_empty(bmap)) {
5012 isl_basic_map_free(bmap);
5013 return map;
5015 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5016 isl_assert(map->ctx, map->n < map->size, goto error);
5017 map->p[map->n] = bmap;
5018 map->n++;
5019 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5020 return map;
5021 error:
5022 if (map)
5023 isl_map_free(map);
5024 if (bmap)
5025 isl_basic_map_free(bmap);
5026 return NULL;
5029 void isl_map_free(struct isl_map *map)
5031 int i;
5033 if (!map)
5034 return;
5036 if (--map->ref > 0)
5037 return;
5039 isl_ctx_deref(map->ctx);
5040 for (i = 0; i < map->n; ++i)
5041 isl_basic_map_free(map->p[i]);
5042 isl_space_free(map->dim);
5043 free(map);
5046 struct isl_map *isl_map_extend(struct isl_map *base,
5047 unsigned nparam, unsigned n_in, unsigned n_out)
5049 int i;
5051 base = isl_map_cow(base);
5052 if (!base)
5053 return NULL;
5055 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5056 if (!base->dim)
5057 goto error;
5058 for (i = 0; i < base->n; ++i) {
5059 base->p[i] = isl_basic_map_extend_space(base->p[i],
5060 isl_space_copy(base->dim), 0, 0, 0);
5061 if (!base->p[i])
5062 goto error;
5064 return base;
5065 error:
5066 isl_map_free(base);
5067 return NULL;
5070 struct isl_set *isl_set_extend(struct isl_set *base,
5071 unsigned nparam, unsigned dim)
5073 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5074 nparam, 0, dim);
5077 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5078 struct isl_basic_map *bmap, unsigned pos, int value)
5080 int j;
5082 bmap = isl_basic_map_cow(bmap);
5083 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5084 j = isl_basic_map_alloc_equality(bmap);
5085 if (j < 0)
5086 goto error;
5087 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5088 isl_int_set_si(bmap->eq[j][pos], -1);
5089 isl_int_set_si(bmap->eq[j][0], value);
5090 bmap = isl_basic_map_simplify(bmap);
5091 return isl_basic_map_finalize(bmap);
5092 error:
5093 isl_basic_map_free(bmap);
5094 return NULL;
5097 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5098 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5100 int j;
5102 bmap = isl_basic_map_cow(bmap);
5103 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5104 j = isl_basic_map_alloc_equality(bmap);
5105 if (j < 0)
5106 goto error;
5107 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5108 isl_int_set_si(bmap->eq[j][pos], -1);
5109 isl_int_set(bmap->eq[j][0], value);
5110 bmap = isl_basic_map_simplify(bmap);
5111 return isl_basic_map_finalize(bmap);
5112 error:
5113 isl_basic_map_free(bmap);
5114 return NULL;
5117 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5118 enum isl_dim_type type, unsigned pos, int value)
5120 if (!bmap)
5121 return NULL;
5122 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5123 return isl_basic_map_fix_pos_si(bmap,
5124 isl_basic_map_offset(bmap, type) + pos, value);
5125 error:
5126 isl_basic_map_free(bmap);
5127 return NULL;
5130 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5131 enum isl_dim_type type, unsigned pos, isl_int value)
5133 if (!bmap)
5134 return NULL;
5135 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5136 return isl_basic_map_fix_pos(bmap,
5137 isl_basic_map_offset(bmap, type) + pos, value);
5138 error:
5139 isl_basic_map_free(bmap);
5140 return NULL;
5143 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5144 enum isl_dim_type type, unsigned pos, int value)
5146 return (struct isl_basic_set *)
5147 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5148 type, pos, value);
5151 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5152 enum isl_dim_type type, unsigned pos, isl_int value)
5154 return (struct isl_basic_set *)
5155 isl_basic_map_fix((struct isl_basic_map *)bset,
5156 type, pos, value);
5159 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5160 unsigned input, int value)
5162 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5165 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5166 unsigned dim, int value)
5168 return (struct isl_basic_set *)
5169 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5170 isl_dim_set, dim, value);
5173 static int remove_if_empty(__isl_keep isl_map *map, int i)
5175 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5177 if (empty < 0)
5178 return -1;
5179 if (!empty)
5180 return 0;
5182 isl_basic_map_free(map->p[i]);
5183 if (i != map->n - 1) {
5184 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5185 map->p[i] = map->p[map->n - 1];
5187 map->n--;
5189 return 0;
5192 struct isl_map *isl_map_fix_si(struct isl_map *map,
5193 enum isl_dim_type type, unsigned pos, int value)
5195 int i;
5197 map = isl_map_cow(map);
5198 if (!map)
5199 return NULL;
5201 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5202 for (i = map->n - 1; i >= 0; --i) {
5203 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5204 if (remove_if_empty(map, i) < 0)
5205 goto error;
5207 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5208 return map;
5209 error:
5210 isl_map_free(map);
5211 return NULL;
5214 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5215 enum isl_dim_type type, unsigned pos, int value)
5217 return (struct isl_set *)
5218 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5221 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5222 enum isl_dim_type type, unsigned pos, isl_int value)
5224 int i;
5226 map = isl_map_cow(map);
5227 if (!map)
5228 return NULL;
5230 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5231 for (i = 0; i < map->n; ++i) {
5232 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5233 if (!map->p[i])
5234 goto error;
5236 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5237 return map;
5238 error:
5239 isl_map_free(map);
5240 return NULL;
5243 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5244 enum isl_dim_type type, unsigned pos, isl_int value)
5246 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5249 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5250 unsigned input, int value)
5252 return isl_map_fix_si(map, isl_dim_in, input, value);
5255 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5257 return (struct isl_set *)
5258 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5261 static __isl_give isl_basic_map *basic_map_bound_si(
5262 __isl_take isl_basic_map *bmap,
5263 enum isl_dim_type type, unsigned pos, int value, int upper)
5265 int j;
5267 if (!bmap)
5268 return NULL;
5269 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5270 pos += isl_basic_map_offset(bmap, type);
5271 bmap = isl_basic_map_cow(bmap);
5272 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5273 j = isl_basic_map_alloc_inequality(bmap);
5274 if (j < 0)
5275 goto error;
5276 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5277 if (upper) {
5278 isl_int_set_si(bmap->ineq[j][pos], -1);
5279 isl_int_set_si(bmap->ineq[j][0], value);
5280 } else {
5281 isl_int_set_si(bmap->ineq[j][pos], 1);
5282 isl_int_set_si(bmap->ineq[j][0], -value);
5284 bmap = isl_basic_map_simplify(bmap);
5285 return isl_basic_map_finalize(bmap);
5286 error:
5287 isl_basic_map_free(bmap);
5288 return NULL;
5291 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5292 __isl_take isl_basic_map *bmap,
5293 enum isl_dim_type type, unsigned pos, int value)
5295 return basic_map_bound_si(bmap, type, pos, value, 0);
5298 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5299 unsigned dim, isl_int value)
5301 int j;
5303 bset = isl_basic_set_cow(bset);
5304 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5305 j = isl_basic_set_alloc_inequality(bset);
5306 if (j < 0)
5307 goto error;
5308 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5309 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5310 isl_int_neg(bset->ineq[j][0], value);
5311 bset = isl_basic_set_simplify(bset);
5312 return isl_basic_set_finalize(bset);
5313 error:
5314 isl_basic_set_free(bset);
5315 return NULL;
5318 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5319 enum isl_dim_type type, unsigned pos, int value, int upper)
5321 int i;
5323 map = isl_map_cow(map);
5324 if (!map)
5325 return NULL;
5327 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5328 for (i = 0; i < map->n; ++i) {
5329 map->p[i] = basic_map_bound_si(map->p[i],
5330 type, pos, value, upper);
5331 if (!map->p[i])
5332 goto error;
5334 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5335 return map;
5336 error:
5337 isl_map_free(map);
5338 return NULL;
5341 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5342 enum isl_dim_type type, unsigned pos, int value)
5344 return map_bound_si(map, type, pos, value, 0);
5347 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5348 enum isl_dim_type type, unsigned pos, int value)
5350 return map_bound_si(map, type, pos, value, 1);
5353 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5354 enum isl_dim_type type, unsigned pos, int value)
5356 return (struct isl_set *)
5357 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5360 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5361 enum isl_dim_type type, unsigned pos, int value)
5363 return isl_map_upper_bound_si(set, type, pos, value);
5366 /* Bound the given variable of "bmap" from below (or above is "upper"
5367 * is set) to "value".
5369 static __isl_give isl_basic_map *basic_map_bound(
5370 __isl_take isl_basic_map *bmap,
5371 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5373 int j;
5375 if (!bmap)
5376 return NULL;
5377 if (pos >= isl_basic_map_dim(bmap, type))
5378 isl_die(bmap->ctx, isl_error_invalid,
5379 "index out of bounds", goto error);
5380 pos += isl_basic_map_offset(bmap, type);
5381 bmap = isl_basic_map_cow(bmap);
5382 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5383 j = isl_basic_map_alloc_inequality(bmap);
5384 if (j < 0)
5385 goto error;
5386 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5387 if (upper) {
5388 isl_int_set_si(bmap->ineq[j][pos], -1);
5389 isl_int_set(bmap->ineq[j][0], value);
5390 } else {
5391 isl_int_set_si(bmap->ineq[j][pos], 1);
5392 isl_int_neg(bmap->ineq[j][0], value);
5394 bmap = isl_basic_map_simplify(bmap);
5395 return isl_basic_map_finalize(bmap);
5396 error:
5397 isl_basic_map_free(bmap);
5398 return NULL;
5401 /* Bound the given variable of "map" from below (or above is "upper"
5402 * is set) to "value".
5404 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5405 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5407 int i;
5409 map = isl_map_cow(map);
5410 if (!map)
5411 return NULL;
5413 if (pos >= isl_map_dim(map, type))
5414 isl_die(map->ctx, isl_error_invalid,
5415 "index out of bounds", goto error);
5416 for (i = map->n - 1; i >= 0; --i) {
5417 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5418 if (remove_if_empty(map, i) < 0)
5419 goto error;
5421 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5422 return map;
5423 error:
5424 isl_map_free(map);
5425 return NULL;
5428 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5429 enum isl_dim_type type, unsigned pos, isl_int value)
5431 return map_bound(map, type, pos, value, 0);
5434 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5435 enum isl_dim_type type, unsigned pos, isl_int value)
5437 return map_bound(map, type, pos, value, 1);
5440 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5441 enum isl_dim_type type, unsigned pos, isl_int value)
5443 return isl_map_lower_bound(set, type, pos, value);
5446 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5447 enum isl_dim_type type, unsigned pos, isl_int value)
5449 return isl_map_upper_bound(set, type, pos, value);
5452 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5453 isl_int value)
5455 int i;
5457 set = isl_set_cow(set);
5458 if (!set)
5459 return NULL;
5461 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5462 for (i = 0; i < set->n; ++i) {
5463 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5464 if (!set->p[i])
5465 goto error;
5467 return set;
5468 error:
5469 isl_set_free(set);
5470 return NULL;
5473 struct isl_map *isl_map_reverse(struct isl_map *map)
5475 int i;
5477 map = isl_map_cow(map);
5478 if (!map)
5479 return NULL;
5481 map->dim = isl_space_reverse(map->dim);
5482 if (!map->dim)
5483 goto error;
5484 for (i = 0; i < map->n; ++i) {
5485 map->p[i] = isl_basic_map_reverse(map->p[i]);
5486 if (!map->p[i])
5487 goto error;
5489 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5490 return map;
5491 error:
5492 isl_map_free(map);
5493 return NULL;
5496 static struct isl_map *isl_basic_map_partial_lexopt(
5497 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5498 struct isl_set **empty, int max)
5500 if (!bmap)
5501 goto error;
5502 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5503 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5504 else
5505 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5506 error:
5507 isl_basic_map_free(bmap);
5508 isl_basic_set_free(dom);
5509 if (empty)
5510 *empty = NULL;
5511 return NULL;
5514 struct isl_map *isl_basic_map_partial_lexmax(
5515 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5516 struct isl_set **empty)
5518 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5521 struct isl_map *isl_basic_map_partial_lexmin(
5522 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5523 struct isl_set **empty)
5525 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5528 struct isl_set *isl_basic_set_partial_lexmin(
5529 struct isl_basic_set *bset, struct isl_basic_set *dom,
5530 struct isl_set **empty)
5532 return (struct isl_set *)
5533 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5534 dom, empty);
5537 struct isl_set *isl_basic_set_partial_lexmax(
5538 struct isl_basic_set *bset, struct isl_basic_set *dom,
5539 struct isl_set **empty)
5541 return (struct isl_set *)
5542 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5543 dom, empty);
5546 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5547 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5548 __isl_give isl_set **empty)
5550 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5553 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5554 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5555 __isl_give isl_set **empty)
5557 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5560 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5561 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5562 __isl_give isl_set **empty)
5564 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5567 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5568 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5569 __isl_give isl_set **empty)
5571 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5574 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5575 __isl_take isl_basic_map *bmap, int max)
5577 isl_basic_set *dom = NULL;
5578 isl_space *dom_space;
5580 if (!bmap)
5581 goto error;
5582 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5583 dom = isl_basic_set_universe(dom_space);
5584 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5585 error:
5586 isl_basic_map_free(bmap);
5587 return NULL;
5590 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5591 __isl_take isl_basic_map *bmap)
5593 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5596 /* Given a basic map "bmap", compute the lexicographically minimal
5597 * (or maximal) image element for each domain element in dom.
5598 * Set *empty to those elements in dom that do not have an image element.
5600 * We first make sure the basic sets in dom are disjoint and then
5601 * simply collect the results over each of the basic sets separately.
5602 * We could probably improve the efficiency a bit by moving the union
5603 * domain down into the parametric integer programming.
5605 static __isl_give isl_map *basic_map_partial_lexopt(
5606 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
5607 __isl_give isl_set **empty, int max)
5609 int i;
5610 struct isl_map *res;
5612 dom = isl_set_make_disjoint(dom);
5613 if (!dom)
5614 goto error;
5616 if (isl_set_plain_is_empty(dom)) {
5617 res = isl_map_empty_like_basic_map(bmap);
5618 *empty = isl_set_empty_like(dom);
5619 isl_set_free(dom);
5620 isl_basic_map_free(bmap);
5621 return res;
5624 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5625 isl_basic_set_copy(dom->p[0]), empty, max);
5627 for (i = 1; i < dom->n; ++i) {
5628 struct isl_map *res_i;
5629 struct isl_set *empty_i;
5631 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5632 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5634 res = isl_map_union_disjoint(res, res_i);
5635 *empty = isl_set_union_disjoint(*empty, empty_i);
5638 isl_set_free(dom);
5639 isl_basic_map_free(bmap);
5640 return res;
5641 error:
5642 *empty = NULL;
5643 isl_set_free(dom);
5644 isl_basic_map_free(bmap);
5645 return NULL;
5648 /* Given a map "map", compute the lexicographically minimal
5649 * (or maximal) image element for each domain element in dom.
5650 * Set *empty to those elements in dom that do not have an image element.
5652 * We first compute the lexicographically minimal or maximal element
5653 * in the first basic map. This results in a partial solution "res"
5654 * and a subset "todo" of dom that still need to be handled.
5655 * We then consider each of the remaining maps in "map" and successively
5656 * improve both "res" and "todo".
5658 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5659 * Assume we are computing the lexicographical maximum.
5660 * We first compute the lexicographically maximal element in basic map i.
5661 * This results in a partial solution res_i and a subset todo_i.
5662 * Then we combine these results with those obtain for the first k basic maps
5663 * to obtain a result that is valid for the first k+1 basic maps.
5664 * In particular, the set where there is no solution is the set where
5665 * there is no solution for the first k basic maps and also no solution
5666 * for the ith basic map, i.e.,
5668 * todo^i = todo^k * todo_i
5670 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5671 * solutions, arbitrarily breaking ties in favor of res^k.
5672 * That is, when res^k(a) >= res_i(a), we pick res^k and
5673 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5674 * the lexicographic order.)
5675 * In practice, we compute
5677 * res^k * (res_i . "<=")
5679 * and
5681 * res_i * (res^k . "<")
5683 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5684 * where only one of res^k and res_i provides a solution and we simply pick
5685 * that one, i.e.,
5687 * res^k * todo_i
5688 * and
5689 * res_i * todo^k
5691 * Note that we only compute these intersections when dom(res^k) intersects
5692 * dom(res_i). Otherwise, the only effect of these intersections is to
5693 * potentially break up res^k and res_i into smaller pieces.
5694 * We want to avoid such splintering as much as possible.
5695 * In fact, an earlier implementation of this function would look for
5696 * better results in the domain of res^k and for extra results in todo^k,
5697 * but this would always result in a splintering according to todo^k,
5698 * even when the domain of basic map i is disjoint from the domains of
5699 * the previous basic maps.
5701 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5702 __isl_take isl_map *map, __isl_take isl_set *dom,
5703 __isl_give isl_set **empty, int max)
5705 int i;
5706 struct isl_map *res;
5707 struct isl_set *todo;
5709 if (!map || !dom)
5710 goto error;
5712 if (isl_map_plain_is_empty(map)) {
5713 if (empty)
5714 *empty = dom;
5715 else
5716 isl_set_free(dom);
5717 return map;
5720 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5721 isl_set_copy(dom), &todo, max);
5723 for (i = 1; i < map->n; ++i) {
5724 isl_map *lt, *le;
5725 isl_map *res_i;
5726 isl_set *todo_i;
5727 isl_space *dim = isl_space_range(isl_map_get_space(res));
5729 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5730 isl_set_copy(dom), &todo_i, max);
5732 if (max) {
5733 lt = isl_map_lex_lt(isl_space_copy(dim));
5734 le = isl_map_lex_le(dim);
5735 } else {
5736 lt = isl_map_lex_gt(isl_space_copy(dim));
5737 le = isl_map_lex_ge(dim);
5739 lt = isl_map_apply_range(isl_map_copy(res), lt);
5740 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5741 le = isl_map_apply_range(isl_map_copy(res_i), le);
5742 le = isl_map_intersect(le, isl_map_copy(res));
5744 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5745 res = isl_map_intersect_domain(res,
5746 isl_set_copy(todo_i));
5747 res_i = isl_map_intersect_domain(res_i,
5748 isl_set_copy(todo));
5751 res = isl_map_union_disjoint(res, res_i);
5752 res = isl_map_union_disjoint(res, lt);
5753 res = isl_map_union_disjoint(res, le);
5755 todo = isl_set_intersect(todo, todo_i);
5758 isl_set_free(dom);
5759 isl_map_free(map);
5761 if (empty)
5762 *empty = todo;
5763 else
5764 isl_set_free(todo);
5766 return res;
5767 error:
5768 if (empty)
5769 *empty = NULL;
5770 isl_set_free(dom);
5771 isl_map_free(map);
5772 return NULL;
5775 /* Given a map "map", compute the lexicographically minimal
5776 * (or maximal) image element for each domain element in dom.
5777 * Set *empty to those elements in dom that do not have an image element.
5779 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5781 static __isl_give isl_map *isl_map_partial_lexopt(
5782 __isl_take isl_map *map, __isl_take isl_set *dom,
5783 __isl_give isl_set **empty, int max)
5785 if (!map || !dom)
5786 goto error;
5787 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5788 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5789 if (!isl_space_has_named_params(map->dim) ||
5790 !isl_space_has_named_params(dom->dim))
5791 isl_die(map->ctx, isl_error_invalid,
5792 "unaligned unnamed parameters", goto error);
5793 map = isl_map_align_params(map, isl_map_get_space(dom));
5794 dom = isl_map_align_params(dom, isl_map_get_space(map));
5795 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5796 error:
5797 if (empty)
5798 *empty = NULL;
5799 isl_set_free(dom);
5800 isl_map_free(map);
5801 return NULL;
5804 __isl_give isl_map *isl_map_partial_lexmax(
5805 __isl_take isl_map *map, __isl_take isl_set *dom,
5806 __isl_give isl_set **empty)
5808 return isl_map_partial_lexopt(map, dom, empty, 1);
5811 __isl_give isl_map *isl_map_partial_lexmin(
5812 __isl_take isl_map *map, __isl_take isl_set *dom,
5813 __isl_give isl_set **empty)
5815 return isl_map_partial_lexopt(map, dom, empty, 0);
5818 __isl_give isl_set *isl_set_partial_lexmin(
5819 __isl_take isl_set *set, __isl_take isl_set *dom,
5820 __isl_give isl_set **empty)
5822 return (struct isl_set *)
5823 isl_map_partial_lexmin((struct isl_map *)set,
5824 dom, empty);
5827 __isl_give isl_set *isl_set_partial_lexmax(
5828 __isl_take isl_set *set, __isl_take isl_set *dom,
5829 __isl_give isl_set **empty)
5831 return (struct isl_set *)
5832 isl_map_partial_lexmax((struct isl_map *)set,
5833 dom, empty);
5836 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5838 struct isl_basic_set *dom = NULL;
5839 isl_space *dom_dim;
5841 if (!bmap)
5842 goto error;
5843 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5844 dom = isl_basic_set_universe(dom_dim);
5845 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5846 error:
5847 isl_basic_map_free(bmap);
5848 return NULL;
5851 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5853 return isl_basic_map_lexopt(bmap, 0);
5856 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5858 return isl_basic_map_lexopt(bmap, 1);
5861 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5863 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5866 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5868 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5871 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5873 struct isl_set *dom = NULL;
5874 isl_space *dom_dim;
5876 if (!map)
5877 goto error;
5878 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5879 dom = isl_set_universe(dom_dim);
5880 return isl_map_partial_lexopt(map, dom, NULL, max);
5881 error:
5882 isl_map_free(map);
5883 return NULL;
5886 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5888 return isl_map_lexopt(map, 0);
5891 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5893 return isl_map_lexopt(map, 1);
5896 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5898 return (isl_set *)isl_map_lexmin((isl_map *)set);
5901 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5903 return (isl_set *)isl_map_lexmax((isl_map *)set);
5906 /* Extract the first and only affine expression from list
5907 * and then add it to *pwaff with the given dom.
5908 * This domain is known to be disjoint from other domains
5909 * because of the way isl_basic_map_foreach_lexmax works.
5911 static int update_dim_opt(__isl_take isl_basic_set *dom,
5912 __isl_take isl_aff_list *list, void *user)
5914 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5915 isl_aff *aff;
5916 isl_pw_aff **pwaff = user;
5917 isl_pw_aff *pwaff_i;
5919 if (isl_aff_list_n_aff(list) != 1)
5920 isl_die(ctx, isl_error_internal,
5921 "expecting single element list", goto error);
5923 aff = isl_aff_list_get_aff(list, 0);
5924 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5926 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5928 isl_aff_list_free(list);
5930 return 0;
5931 error:
5932 isl_basic_set_free(dom);
5933 isl_aff_list_free(list);
5934 return -1;
5937 /* Given a basic map with one output dimension, compute the minimum or
5938 * maximum of that dimension as an isl_pw_aff.
5940 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5941 * call update_dim_opt on each leaf of the result.
5943 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
5944 int max)
5946 isl_space *dim = isl_basic_map_get_space(bmap);
5947 isl_pw_aff *pwaff;
5948 int r;
5950 dim = isl_space_from_domain(isl_space_domain(dim));
5951 dim = isl_space_add_dims(dim, isl_dim_out, 1);
5952 pwaff = isl_pw_aff_empty(dim);
5954 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
5955 if (r < 0)
5956 return isl_pw_aff_free(pwaff);
5958 return pwaff;
5961 /* Compute the minimum or maximum of the given output dimension
5962 * as a function of the parameters and the input dimensions,
5963 * but independently of the other output dimensions.
5965 * We first project out the other output dimension and then compute
5966 * the "lexicographic" maximum in each basic map, combining the results
5967 * using isl_pw_aff_union_max.
5969 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
5970 int max)
5972 int i;
5973 isl_pw_aff *pwaff;
5974 unsigned n_out;
5976 n_out = isl_map_dim(map, isl_dim_out);
5977 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
5978 map = isl_map_project_out(map, isl_dim_out, 0, pos);
5979 if (!map)
5980 return NULL;
5982 if (map->n == 0) {
5983 isl_space *dim = isl_map_get_space(map);
5984 dim = isl_space_domain(isl_space_from_range(dim));
5985 isl_map_free(map);
5986 return isl_pw_aff_empty(dim);
5989 pwaff = basic_map_dim_opt(map->p[0], max);
5990 for (i = 1; i < map->n; ++i) {
5991 isl_pw_aff *pwaff_i;
5993 pwaff_i = basic_map_dim_opt(map->p[i], max);
5994 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
5997 isl_map_free(map);
5999 return pwaff;
6002 /* Compute the maximum of the given output dimension as a function of the
6003 * parameters and input dimensions, but independently of
6004 * the other output dimensions.
6006 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6008 return map_dim_opt(map, pos, 1);
6011 /* Compute the minimum or maximum of the given set dimension
6012 * as a function of the parameters,
6013 * but independently of the other set dimensions.
6015 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6016 int max)
6018 return map_dim_opt(set, pos, max);
6021 /* Compute the maximum of the given set dimension as a function of the
6022 * parameters, but independently of the other set dimensions.
6024 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6026 return set_dim_opt(set, pos, 1);
6029 /* Compute the minimum of the given set dimension as a function of the
6030 * parameters, but independently of the other set dimensions.
6032 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6034 return set_dim_opt(set, pos, 0);
6037 /* Apply a preimage specified by "mat" on the parameters of "bset".
6038 * bset is assumed to have only parameters and divs.
6040 static struct isl_basic_set *basic_set_parameter_preimage(
6041 struct isl_basic_set *bset, struct isl_mat *mat)
6043 unsigned nparam;
6045 if (!bset || !mat)
6046 goto error;
6048 bset->dim = isl_space_cow(bset->dim);
6049 if (!bset->dim)
6050 goto error;
6052 nparam = isl_basic_set_dim(bset, isl_dim_param);
6054 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6056 bset->dim->nparam = 0;
6057 bset->dim->n_out = nparam;
6058 bset = isl_basic_set_preimage(bset, mat);
6059 if (bset) {
6060 bset->dim->nparam = bset->dim->n_out;
6061 bset->dim->n_out = 0;
6063 return bset;
6064 error:
6065 isl_mat_free(mat);
6066 isl_basic_set_free(bset);
6067 return NULL;
6070 /* Apply a preimage specified by "mat" on the parameters of "set".
6071 * set is assumed to have only parameters and divs.
6073 static struct isl_set *set_parameter_preimage(
6074 struct isl_set *set, struct isl_mat *mat)
6076 isl_space *dim = NULL;
6077 unsigned nparam;
6079 if (!set || !mat)
6080 goto error;
6082 dim = isl_space_copy(set->dim);
6083 dim = isl_space_cow(dim);
6084 if (!dim)
6085 goto error;
6087 nparam = isl_set_dim(set, isl_dim_param);
6089 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6091 dim->nparam = 0;
6092 dim->n_out = nparam;
6093 isl_set_reset_space(set, dim);
6094 set = isl_set_preimage(set, mat);
6095 if (!set)
6096 goto error2;
6097 dim = isl_space_copy(set->dim);
6098 dim = isl_space_cow(dim);
6099 if (!dim)
6100 goto error2;
6101 dim->nparam = dim->n_out;
6102 dim->n_out = 0;
6103 isl_set_reset_space(set, dim);
6104 return set;
6105 error:
6106 isl_space_free(dim);
6107 isl_mat_free(mat);
6108 error2:
6109 isl_set_free(set);
6110 return NULL;
6113 /* Intersect the basic set "bset" with the affine space specified by the
6114 * equalities in "eq".
6116 static struct isl_basic_set *basic_set_append_equalities(
6117 struct isl_basic_set *bset, struct isl_mat *eq)
6119 int i, k;
6120 unsigned len;
6122 if (!bset || !eq)
6123 goto error;
6125 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6126 eq->n_row, 0);
6127 if (!bset)
6128 goto error;
6130 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6131 for (i = 0; i < eq->n_row; ++i) {
6132 k = isl_basic_set_alloc_equality(bset);
6133 if (k < 0)
6134 goto error;
6135 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6136 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6138 isl_mat_free(eq);
6140 bset = isl_basic_set_gauss(bset, NULL);
6141 bset = isl_basic_set_finalize(bset);
6143 return bset;
6144 error:
6145 isl_mat_free(eq);
6146 isl_basic_set_free(bset);
6147 return NULL;
6150 /* Intersect the set "set" with the affine space specified by the
6151 * equalities in "eq".
6153 static struct isl_set *set_append_equalities(struct isl_set *set,
6154 struct isl_mat *eq)
6156 int i;
6158 if (!set || !eq)
6159 goto error;
6161 for (i = 0; i < set->n; ++i) {
6162 set->p[i] = basic_set_append_equalities(set->p[i],
6163 isl_mat_copy(eq));
6164 if (!set->p[i])
6165 goto error;
6167 isl_mat_free(eq);
6168 return set;
6169 error:
6170 isl_mat_free(eq);
6171 isl_set_free(set);
6172 return NULL;
6175 /* Project the given basic set onto its parameter domain, possibly introducing
6176 * new, explicit, existential variables in the constraints.
6177 * The input has parameters and (possibly implicit) existential variables.
6178 * The output has the same parameters, but only
6179 * explicit existentially quantified variables.
6181 * The actual projection is performed by pip, but pip doesn't seem
6182 * to like equalities very much, so we first remove the equalities
6183 * among the parameters by performing a variable compression on
6184 * the parameters. Afterward, an inverse transformation is performed
6185 * and the equalities among the parameters are inserted back in.
6187 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6189 int i, j;
6190 struct isl_mat *eq;
6191 struct isl_mat *T, *T2;
6192 struct isl_set *set;
6193 unsigned nparam, n_div;
6195 bset = isl_basic_set_cow(bset);
6196 if (!bset)
6197 return NULL;
6199 if (bset->n_eq == 0)
6200 return isl_basic_set_lexmin(bset);
6202 isl_basic_set_gauss(bset, NULL);
6204 nparam = isl_basic_set_dim(bset, isl_dim_param);
6205 n_div = isl_basic_set_dim(bset, isl_dim_div);
6207 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6208 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6209 ++i;
6211 if (i == bset->n_eq)
6212 return isl_basic_set_lexmin(bset);
6214 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6215 0, 1 + nparam);
6216 eq = isl_mat_cow(eq);
6217 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6218 if (T && T->n_col == 0) {
6219 isl_mat_free(T);
6220 isl_mat_free(T2);
6221 isl_mat_free(eq);
6222 bset = isl_basic_set_set_to_empty(bset);
6223 return isl_set_from_basic_set(bset);
6225 bset = basic_set_parameter_preimage(bset, T);
6227 set = isl_basic_set_lexmin(bset);
6228 set = set_parameter_preimage(set, T2);
6229 set = set_append_equalities(set, eq);
6230 return set;
6233 /* Compute an explicit representation for all the existentially
6234 * quantified variables.
6235 * The input and output dimensions are first turned into parameters.
6236 * compute_divs then returns a map with the same parameters and
6237 * no input or output dimensions and the dimension specification
6238 * is reset to that of the input.
6240 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6242 struct isl_basic_set *bset;
6243 struct isl_set *set;
6244 struct isl_map *map;
6245 isl_space *dim, *orig_dim = NULL;
6246 unsigned nparam;
6247 unsigned n_in;
6248 unsigned n_out;
6250 bmap = isl_basic_map_cow(bmap);
6251 if (!bmap)
6252 return NULL;
6254 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6255 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6256 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6257 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
6258 if (!dim)
6259 goto error;
6261 orig_dim = bmap->dim;
6262 bmap->dim = dim;
6263 bset = (struct isl_basic_set *)bmap;
6265 set = parameter_compute_divs(bset);
6266 map = (struct isl_map *)set;
6267 map = isl_map_reset_space(map, orig_dim);
6269 return map;
6270 error:
6271 isl_basic_map_free(bmap);
6272 return NULL;
6275 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6277 int i;
6278 unsigned off;
6280 if (!bmap)
6281 return -1;
6283 off = isl_space_dim(bmap->dim, isl_dim_all);
6284 for (i = 0; i < bmap->n_div; ++i) {
6285 if (isl_int_is_zero(bmap->div[i][0]))
6286 return 0;
6287 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6288 return -1);
6290 return 1;
6293 static int map_divs_known(__isl_keep isl_map *map)
6295 int i;
6297 if (!map)
6298 return -1;
6300 for (i = 0; i < map->n; ++i) {
6301 int known = isl_basic_map_divs_known(map->p[i]);
6302 if (known <= 0)
6303 return known;
6306 return 1;
6309 /* If bmap contains any unknown divs, then compute explicit
6310 * expressions for them. However, this computation may be
6311 * quite expensive, so first try to remove divs that aren't
6312 * strictly needed.
6314 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6316 int known;
6317 struct isl_map *map;
6319 known = isl_basic_map_divs_known(bmap);
6320 if (known < 0)
6321 goto error;
6322 if (known)
6323 return isl_map_from_basic_map(bmap);
6325 bmap = isl_basic_map_drop_redundant_divs(bmap);
6327 known = isl_basic_map_divs_known(bmap);
6328 if (known < 0)
6329 goto error;
6330 if (known)
6331 return isl_map_from_basic_map(bmap);
6333 map = compute_divs(bmap);
6334 return map;
6335 error:
6336 isl_basic_map_free(bmap);
6337 return NULL;
6340 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6342 int i;
6343 int known;
6344 struct isl_map *res;
6346 if (!map)
6347 return NULL;
6348 if (map->n == 0)
6349 return map;
6351 known = map_divs_known(map);
6352 if (known < 0) {
6353 isl_map_free(map);
6354 return NULL;
6356 if (known)
6357 return map;
6359 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6360 for (i = 1 ; i < map->n; ++i) {
6361 struct isl_map *r2;
6362 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6363 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6364 res = isl_map_union_disjoint(res, r2);
6365 else
6366 res = isl_map_union(res, r2);
6368 isl_map_free(map);
6370 return res;
6373 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6375 return (struct isl_set *)
6376 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6379 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6381 return (struct isl_set *)
6382 isl_map_compute_divs((struct isl_map *)set);
6385 struct isl_set *isl_map_domain(struct isl_map *map)
6387 int i;
6388 struct isl_set *set;
6390 if (!map)
6391 goto error;
6393 map = isl_map_cow(map);
6394 if (!map)
6395 return NULL;
6397 set = (struct isl_set *)map;
6398 set->dim = isl_space_domain(set->dim);
6399 if (!set->dim)
6400 goto error;
6401 for (i = 0; i < map->n; ++i) {
6402 set->p[i] = isl_basic_map_domain(map->p[i]);
6403 if (!set->p[i])
6404 goto error;
6406 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6407 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6408 return set;
6409 error:
6410 isl_map_free(map);
6411 return NULL;
6414 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6415 __isl_take isl_map *map2)
6417 int i;
6418 unsigned flags = 0;
6419 struct isl_map *map = NULL;
6421 if (!map1 || !map2)
6422 goto error;
6424 if (map1->n == 0) {
6425 isl_map_free(map1);
6426 return map2;
6428 if (map2->n == 0) {
6429 isl_map_free(map2);
6430 return map1;
6433 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6435 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6436 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6437 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6439 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6440 map1->n + map2->n, flags);
6441 if (!map)
6442 goto error;
6443 for (i = 0; i < map1->n; ++i) {
6444 map = isl_map_add_basic_map(map,
6445 isl_basic_map_copy(map1->p[i]));
6446 if (!map)
6447 goto error;
6449 for (i = 0; i < map2->n; ++i) {
6450 map = isl_map_add_basic_map(map,
6451 isl_basic_map_copy(map2->p[i]));
6452 if (!map)
6453 goto error;
6455 isl_map_free(map1);
6456 isl_map_free(map2);
6457 return map;
6458 error:
6459 isl_map_free(map);
6460 isl_map_free(map1);
6461 isl_map_free(map2);
6462 return NULL;
6465 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6466 __isl_take isl_map *map2)
6468 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6471 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6473 map1 = isl_map_union_disjoint(map1, map2);
6474 if (!map1)
6475 return NULL;
6476 if (map1->n > 1)
6477 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6478 return map1;
6481 struct isl_set *isl_set_union_disjoint(
6482 struct isl_set *set1, struct isl_set *set2)
6484 return (struct isl_set *)
6485 isl_map_union_disjoint(
6486 (struct isl_map *)set1, (struct isl_map *)set2);
6489 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6491 return (struct isl_set *)
6492 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6495 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6496 __isl_take isl_set *set)
6498 unsigned flags = 0;
6499 struct isl_map *result;
6500 int i, j;
6502 if (!map || !set)
6503 goto error;
6505 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
6506 isl_die(set->ctx, isl_error_invalid,
6507 "parameters don't match", goto error);
6509 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
6510 !isl_map_compatible_range(map, set))
6511 isl_die(set->ctx, isl_error_invalid,
6512 "incompatible spaces", goto error);
6514 if (isl_set_plain_is_universe(set)) {
6515 isl_set_free(set);
6516 return map;
6519 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6520 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6521 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6523 result = isl_map_alloc_space(isl_space_copy(map->dim),
6524 map->n * set->n, flags);
6525 if (!result)
6526 goto error;
6527 for (i = 0; i < map->n; ++i)
6528 for (j = 0; j < set->n; ++j) {
6529 result = isl_map_add_basic_map(result,
6530 isl_basic_map_intersect_range(
6531 isl_basic_map_copy(map->p[i]),
6532 isl_basic_set_copy(set->p[j])));
6533 if (!result)
6534 goto error;
6536 isl_map_free(map);
6537 isl_set_free(set);
6538 return result;
6539 error:
6540 isl_map_free(map);
6541 isl_set_free(set);
6542 return NULL;
6545 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6546 __isl_take isl_set *set)
6548 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6551 struct isl_map *isl_map_intersect_domain(
6552 struct isl_map *map, struct isl_set *set)
6554 return isl_map_reverse(
6555 isl_map_intersect_range(isl_map_reverse(map), set));
6558 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6559 __isl_take isl_map *map2)
6561 if (!map1 || !map2)
6562 goto error;
6563 map1 = isl_map_reverse(map1);
6564 map1 = isl_map_apply_range(map1, map2);
6565 return isl_map_reverse(map1);
6566 error:
6567 isl_map_free(map1);
6568 isl_map_free(map2);
6569 return NULL;
6572 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6573 __isl_take isl_map *map2)
6575 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6578 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6579 __isl_take isl_map *map2)
6581 isl_space *dim_result;
6582 struct isl_map *result;
6583 int i, j;
6585 if (!map1 || !map2)
6586 goto error;
6588 dim_result = isl_space_join(isl_space_copy(map1->dim),
6589 isl_space_copy(map2->dim));
6591 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6592 if (!result)
6593 goto error;
6594 for (i = 0; i < map1->n; ++i)
6595 for (j = 0; j < map2->n; ++j) {
6596 result = isl_map_add_basic_map(result,
6597 isl_basic_map_apply_range(
6598 isl_basic_map_copy(map1->p[i]),
6599 isl_basic_map_copy(map2->p[j])));
6600 if (!result)
6601 goto error;
6603 isl_map_free(map1);
6604 isl_map_free(map2);
6605 if (result && result->n <= 1)
6606 ISL_F_SET(result, ISL_MAP_DISJOINT);
6607 return result;
6608 error:
6609 isl_map_free(map1);
6610 isl_map_free(map2);
6611 return NULL;
6614 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6615 __isl_take isl_map *map2)
6617 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6621 * returns range - domain
6623 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6625 isl_space *dims, *target_dim;
6626 struct isl_basic_set *bset;
6627 unsigned dim;
6628 unsigned nparam;
6629 int i;
6631 if (!bmap)
6632 goto error;
6633 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6634 bmap->dim, isl_dim_out),
6635 goto error);
6636 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6637 dim = isl_basic_map_n_in(bmap);
6638 nparam = isl_basic_map_n_param(bmap);
6639 bset = isl_basic_set_from_basic_map(bmap);
6640 bset = isl_basic_set_cow(bset);
6641 dims = isl_basic_set_get_space(bset);
6642 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6643 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6644 bset = isl_basic_set_swap_vars(bset, 2*dim);
6645 for (i = 0; i < dim; ++i) {
6646 int j = isl_basic_map_alloc_equality(
6647 (struct isl_basic_map *)bset);
6648 if (j < 0)
6649 goto error;
6650 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6651 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6652 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6653 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6655 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6656 bset = isl_basic_set_reset_space(bset, target_dim);
6657 return bset;
6658 error:
6659 isl_basic_map_free(bmap);
6660 return NULL;
6664 * returns range - domain
6666 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
6668 int i;
6669 isl_space *dim;
6670 struct isl_set *result;
6672 if (!map)
6673 return NULL;
6675 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6676 map->dim, isl_dim_out),
6677 goto error);
6678 dim = isl_map_get_space(map);
6679 dim = isl_space_domain(dim);
6680 result = isl_set_alloc_space(dim, map->n, 0);
6681 if (!result)
6682 goto error;
6683 for (i = 0; i < map->n; ++i)
6684 result = isl_set_add_basic_set(result,
6685 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6686 isl_map_free(map);
6687 return result;
6688 error:
6689 isl_map_free(map);
6690 return NULL;
6694 * returns [domain -> range] -> range - domain
6696 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6697 __isl_take isl_basic_map *bmap)
6699 int i, k;
6700 isl_space *dim;
6701 isl_basic_map *domain;
6702 int nparam, n;
6703 unsigned total;
6705 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6706 isl_die(bmap->ctx, isl_error_invalid,
6707 "domain and range don't match", goto error);
6709 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6710 n = isl_basic_map_dim(bmap, isl_dim_in);
6712 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6713 domain = isl_basic_map_universe(dim);
6715 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6716 bmap = isl_basic_map_apply_range(bmap, domain);
6717 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6719 total = isl_basic_map_total_dim(bmap);
6721 for (i = 0; i < n; ++i) {
6722 k = isl_basic_map_alloc_equality(bmap);
6723 if (k < 0)
6724 goto error;
6725 isl_seq_clr(bmap->eq[k], 1 + total);
6726 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6727 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6728 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6731 bmap = isl_basic_map_gauss(bmap, NULL);
6732 return isl_basic_map_finalize(bmap);
6733 error:
6734 isl_basic_map_free(bmap);
6735 return NULL;
6739 * returns [domain -> range] -> range - domain
6741 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6743 int i;
6744 isl_space *domain_dim;
6746 if (!map)
6747 return NULL;
6749 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6750 isl_die(map->ctx, isl_error_invalid,
6751 "domain and range don't match", goto error);
6753 map = isl_map_cow(map);
6754 if (!map)
6755 return NULL;
6757 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6758 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6759 map->dim = isl_space_join(map->dim, domain_dim);
6760 if (!map->dim)
6761 goto error;
6762 for (i = 0; i < map->n; ++i) {
6763 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6764 if (!map->p[i])
6765 goto error;
6767 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6768 return map;
6769 error:
6770 isl_map_free(map);
6771 return NULL;
6774 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6776 struct isl_basic_map *bmap;
6777 unsigned nparam;
6778 unsigned dim;
6779 int i;
6781 if (!dims)
6782 return NULL;
6784 nparam = dims->nparam;
6785 dim = dims->n_out;
6786 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6787 if (!bmap)
6788 goto error;
6790 for (i = 0; i < dim; ++i) {
6791 int j = isl_basic_map_alloc_equality(bmap);
6792 if (j < 0)
6793 goto error;
6794 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6795 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6796 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6798 return isl_basic_map_finalize(bmap);
6799 error:
6800 isl_basic_map_free(bmap);
6801 return NULL;
6804 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6806 if (!dim)
6807 return NULL;
6808 if (dim->n_in != dim->n_out)
6809 isl_die(dim->ctx, isl_error_invalid,
6810 "number of input and output dimensions needs to be "
6811 "the same", goto error);
6812 return basic_map_identity(dim);
6813 error:
6814 isl_space_free(dim);
6815 return NULL;
6818 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6820 if (!model || !model->dim)
6821 return NULL;
6822 return isl_basic_map_identity(isl_space_copy(model->dim));
6825 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6827 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6830 struct isl_map *isl_map_identity_like(struct isl_map *model)
6832 if (!model || !model->dim)
6833 return NULL;
6834 return isl_map_identity(isl_space_copy(model->dim));
6837 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6839 if (!model || !model->dim)
6840 return NULL;
6841 return isl_map_identity(isl_space_copy(model->dim));
6844 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6846 isl_space *dim = isl_set_get_space(set);
6847 isl_map *id;
6848 id = isl_map_identity(isl_space_map_from_set(dim));
6849 return isl_map_intersect_range(id, set);
6852 /* Construct a basic set with all set dimensions having only non-negative
6853 * values.
6855 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6856 __isl_take isl_space *space)
6858 int i;
6859 unsigned nparam;
6860 unsigned dim;
6861 struct isl_basic_set *bset;
6863 if (!space)
6864 return NULL;
6865 nparam = space->nparam;
6866 dim = space->n_out;
6867 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6868 if (!bset)
6869 return NULL;
6870 for (i = 0; i < dim; ++i) {
6871 int k = isl_basic_set_alloc_inequality(bset);
6872 if (k < 0)
6873 goto error;
6874 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6875 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6877 return bset;
6878 error:
6879 isl_basic_set_free(bset);
6880 return NULL;
6883 /* Construct the half-space x_pos >= 0.
6885 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6886 int pos)
6888 int k;
6889 isl_basic_set *nonneg;
6891 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6892 k = isl_basic_set_alloc_inequality(nonneg);
6893 if (k < 0)
6894 goto error;
6895 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6896 isl_int_set_si(nonneg->ineq[k][pos], 1);
6898 return isl_basic_set_finalize(nonneg);
6899 error:
6900 isl_basic_set_free(nonneg);
6901 return NULL;
6904 /* Construct the half-space x_pos <= -1.
6906 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6908 int k;
6909 isl_basic_set *neg;
6911 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6912 k = isl_basic_set_alloc_inequality(neg);
6913 if (k < 0)
6914 goto error;
6915 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6916 isl_int_set_si(neg->ineq[k][0], -1);
6917 isl_int_set_si(neg->ineq[k][pos], -1);
6919 return isl_basic_set_finalize(neg);
6920 error:
6921 isl_basic_set_free(neg);
6922 return NULL;
6925 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6926 enum isl_dim_type type, unsigned first, unsigned n)
6928 int i;
6929 isl_basic_set *nonneg;
6930 isl_basic_set *neg;
6932 if (!set)
6933 return NULL;
6934 if (n == 0)
6935 return set;
6937 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6939 for (i = 0; i < n; ++i) {
6940 nonneg = nonneg_halfspace(isl_set_get_space(set),
6941 pos(set->dim, type) + first + i);
6942 neg = neg_halfspace(isl_set_get_space(set),
6943 pos(set->dim, type) + first + i);
6945 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6948 return set;
6949 error:
6950 isl_set_free(set);
6951 return NULL;
6954 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6955 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6956 void *user)
6958 isl_set *half;
6960 if (!set)
6961 return -1;
6962 if (isl_set_plain_is_empty(set)) {
6963 isl_set_free(set);
6964 return 0;
6966 if (first == len)
6967 return fn(set, signs, user);
6969 signs[first] = 1;
6970 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6971 1 + first));
6972 half = isl_set_intersect(half, isl_set_copy(set));
6973 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6974 goto error;
6976 signs[first] = -1;
6977 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6978 1 + first));
6979 half = isl_set_intersect(half, set);
6980 return foreach_orthant(half, signs, first + 1, len, fn, user);
6981 error:
6982 isl_set_free(set);
6983 return -1;
6986 /* Call "fn" on the intersections of "set" with each of the orthants
6987 * (except for obviously empty intersections). The orthant is identified
6988 * by the signs array, with each entry having value 1 or -1 according
6989 * to the sign of the corresponding variable.
6991 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6992 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6993 void *user)
6995 unsigned nparam;
6996 unsigned nvar;
6997 int *signs;
6998 int r;
7000 if (!set)
7001 return -1;
7002 if (isl_set_plain_is_empty(set))
7003 return 0;
7005 nparam = isl_set_dim(set, isl_dim_param);
7006 nvar = isl_set_dim(set, isl_dim_set);
7008 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7010 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7011 fn, user);
7013 free(signs);
7015 return r;
7018 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7020 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7023 int isl_basic_map_is_subset(
7024 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7026 int is_subset;
7027 struct isl_map *map1;
7028 struct isl_map *map2;
7030 if (!bmap1 || !bmap2)
7031 return -1;
7033 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7034 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7036 is_subset = isl_map_is_subset(map1, map2);
7038 isl_map_free(map1);
7039 isl_map_free(map2);
7041 return is_subset;
7044 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7045 __isl_keep isl_basic_set *bset2)
7047 return isl_basic_map_is_subset(bset1, bset2);
7050 int isl_basic_map_is_equal(
7051 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7053 int is_subset;
7055 if (!bmap1 || !bmap2)
7056 return -1;
7057 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7058 if (is_subset != 1)
7059 return is_subset;
7060 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7061 return is_subset;
7064 int isl_basic_set_is_equal(
7065 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7067 return isl_basic_map_is_equal(
7068 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7071 int isl_map_is_empty(struct isl_map *map)
7073 int i;
7074 int is_empty;
7076 if (!map)
7077 return -1;
7078 for (i = 0; i < map->n; ++i) {
7079 is_empty = isl_basic_map_is_empty(map->p[i]);
7080 if (is_empty < 0)
7081 return -1;
7082 if (!is_empty)
7083 return 0;
7085 return 1;
7088 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7090 return map ? map->n == 0 : -1;
7093 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7095 return isl_map_plain_is_empty(map);
7098 int isl_set_plain_is_empty(struct isl_set *set)
7100 return set ? set->n == 0 : -1;
7103 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7105 return isl_set_plain_is_empty(set);
7108 int isl_set_is_empty(struct isl_set *set)
7110 return isl_map_is_empty((struct isl_map *)set);
7113 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7115 if (!map1 || !map2)
7116 return -1;
7118 return isl_space_is_equal(map1->dim, map2->dim);
7121 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7123 if (!set1 || !set2)
7124 return -1;
7126 return isl_space_is_equal(set1->dim, set2->dim);
7129 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7131 int is_subset;
7133 if (!map1 || !map2)
7134 return -1;
7135 is_subset = isl_map_is_subset(map1, map2);
7136 if (is_subset != 1)
7137 return is_subset;
7138 is_subset = isl_map_is_subset(map2, map1);
7139 return is_subset;
7142 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7144 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7147 int isl_basic_map_is_strict_subset(
7148 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7150 int is_subset;
7152 if (!bmap1 || !bmap2)
7153 return -1;
7154 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7155 if (is_subset != 1)
7156 return is_subset;
7157 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7158 if (is_subset == -1)
7159 return is_subset;
7160 return !is_subset;
7163 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7165 int is_subset;
7167 if (!map1 || !map2)
7168 return -1;
7169 is_subset = isl_map_is_subset(map1, map2);
7170 if (is_subset != 1)
7171 return is_subset;
7172 is_subset = isl_map_is_subset(map2, map1);
7173 if (is_subset == -1)
7174 return is_subset;
7175 return !is_subset;
7178 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7180 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7183 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7185 if (!bmap)
7186 return -1;
7187 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7190 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7192 if (!bset)
7193 return -1;
7194 return bset->n_eq == 0 && bset->n_ineq == 0;
7197 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7199 int i;
7201 if (!map)
7202 return -1;
7204 for (i = 0; i < map->n; ++i) {
7205 int r = isl_basic_map_is_universe(map->p[i]);
7206 if (r < 0 || r)
7207 return r;
7210 return 0;
7213 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7215 return isl_map_plain_is_universe((isl_map *) set);
7218 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7220 return isl_set_plain_is_universe(set);
7223 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7225 struct isl_basic_set *bset = NULL;
7226 struct isl_vec *sample = NULL;
7227 int empty;
7228 unsigned total;
7230 if (!bmap)
7231 return -1;
7233 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7234 return 1;
7236 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7237 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7238 copy = isl_basic_map_remove_redundancies(copy);
7239 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
7240 isl_basic_map_free(copy);
7241 return empty;
7244 total = 1 + isl_basic_map_total_dim(bmap);
7245 if (bmap->sample && bmap->sample->size == total) {
7246 int contains = isl_basic_map_contains(bmap, bmap->sample);
7247 if (contains < 0)
7248 return -1;
7249 if (contains)
7250 return 0;
7252 isl_vec_free(bmap->sample);
7253 bmap->sample = NULL;
7254 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7255 if (!bset)
7256 return -1;
7257 sample = isl_basic_set_sample_vec(bset);
7258 if (!sample)
7259 return -1;
7260 empty = sample->size == 0;
7261 isl_vec_free(bmap->sample);
7262 bmap->sample = sample;
7263 if (empty)
7264 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7266 return empty;
7269 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7271 if (!bmap)
7272 return -1;
7273 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7276 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7278 return isl_basic_map_plain_is_empty(bmap);
7281 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7283 if (!bset)
7284 return -1;
7285 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7288 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7290 return isl_basic_set_plain_is_empty(bset);
7293 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7295 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7298 struct isl_map *isl_basic_map_union(
7299 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7301 struct isl_map *map;
7302 if (!bmap1 || !bmap2)
7303 goto error;
7305 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7307 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7308 if (!map)
7309 goto error;
7310 map = isl_map_add_basic_map(map, bmap1);
7311 map = isl_map_add_basic_map(map, bmap2);
7312 return map;
7313 error:
7314 isl_basic_map_free(bmap1);
7315 isl_basic_map_free(bmap2);
7316 return NULL;
7319 struct isl_set *isl_basic_set_union(
7320 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7322 return (struct isl_set *)isl_basic_map_union(
7323 (struct isl_basic_map *)bset1,
7324 (struct isl_basic_map *)bset2);
7327 /* Order divs such that any div only depends on previous divs */
7328 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7330 int i;
7331 unsigned off;
7333 if (!bmap)
7334 return NULL;
7336 off = isl_space_dim(bmap->dim, isl_dim_all);
7338 for (i = 0; i < bmap->n_div; ++i) {
7339 int pos;
7340 if (isl_int_is_zero(bmap->div[i][0]))
7341 continue;
7342 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7343 bmap->n_div-i);
7344 if (pos == -1)
7345 continue;
7346 isl_basic_map_swap_div(bmap, i, i + pos);
7347 --i;
7349 return bmap;
7352 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7354 return (struct isl_basic_set *)
7355 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7358 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7360 int i;
7362 if (!map)
7363 return 0;
7365 for (i = 0; i < map->n; ++i) {
7366 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7367 if (!map->p[i])
7368 goto error;
7371 return map;
7372 error:
7373 isl_map_free(map);
7374 return NULL;
7377 /* Apply the expansion computed by isl_merge_divs.
7378 * The expansion itself is given by "exp" while the resulting
7379 * list of divs is given by "div".
7381 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7382 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7384 int i, j;
7385 int n_div;
7387 bset = isl_basic_set_cow(bset);
7388 if (!bset || !div)
7389 goto error;
7391 if (div->n_row < bset->n_div)
7392 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7393 "not an expansion", goto error);
7395 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
7396 div->n_row - bset->n_div, 0,
7397 2 * (div->n_row - bset->n_div));
7399 n_div = bset->n_div;
7400 for (i = n_div; i < div->n_row; ++i)
7401 if (isl_basic_set_alloc_div(bset) < 0)
7402 goto error;
7404 j = n_div - 1;
7405 for (i = div->n_row - 1; i >= 0; --i) {
7406 if (j >= 0 && exp[j] == i) {
7407 if (i != j)
7408 isl_basic_map_swap_div(bset, i, j);
7409 j--;
7410 } else {
7411 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
7412 if (isl_basic_map_add_div_constraints(bset, i) < 0)
7413 goto error;
7417 isl_mat_free(div);
7418 return bset;
7419 error:
7420 isl_basic_set_free(bset);
7421 isl_mat_free(div);
7422 return NULL;
7425 /* Look for a div in dst that corresponds to the div "div" in src.
7426 * The divs before "div" in src and dst are assumed to be the same.
7428 * Returns -1 if no corresponding div was found and the position
7429 * of the corresponding div in dst otherwise.
7431 static int find_div(struct isl_basic_map *dst,
7432 struct isl_basic_map *src, unsigned div)
7434 int i;
7436 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7438 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7439 for (i = div; i < dst->n_div; ++i)
7440 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7441 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7442 dst->n_div - div) == -1)
7443 return i;
7444 return -1;
7447 struct isl_basic_map *isl_basic_map_align_divs(
7448 struct isl_basic_map *dst, struct isl_basic_map *src)
7450 int i;
7451 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7453 if (!dst || !src)
7454 goto error;
7456 if (src->n_div == 0)
7457 return dst;
7459 for (i = 0; i < src->n_div; ++i)
7460 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7462 src = isl_basic_map_order_divs(src);
7463 dst = isl_basic_map_cow(dst);
7464 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7465 src->n_div, 0, 2 * src->n_div);
7466 if (!dst)
7467 return NULL;
7468 for (i = 0; i < src->n_div; ++i) {
7469 int j = find_div(dst, src, i);
7470 if (j < 0) {
7471 j = isl_basic_map_alloc_div(dst);
7472 if (j < 0)
7473 goto error;
7474 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7475 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7476 if (isl_basic_map_add_div_constraints(dst, j) < 0)
7477 goto error;
7479 if (j != i)
7480 isl_basic_map_swap_div(dst, i, j);
7482 return dst;
7483 error:
7484 isl_basic_map_free(dst);
7485 return NULL;
7488 struct isl_basic_set *isl_basic_set_align_divs(
7489 struct isl_basic_set *dst, struct isl_basic_set *src)
7491 return (struct isl_basic_set *)isl_basic_map_align_divs(
7492 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7495 struct isl_map *isl_map_align_divs(struct isl_map *map)
7497 int i;
7499 if (!map)
7500 return NULL;
7501 if (map->n == 0)
7502 return map;
7503 map = isl_map_compute_divs(map);
7504 map = isl_map_cow(map);
7505 if (!map)
7506 return NULL;
7508 for (i = 1; i < map->n; ++i)
7509 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7510 for (i = 1; i < map->n; ++i)
7511 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7513 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7514 return map;
7517 struct isl_set *isl_set_align_divs(struct isl_set *set)
7519 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7522 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7523 __isl_take isl_map *map)
7525 if (!set || !map)
7526 goto error;
7527 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7528 map = isl_map_intersect_domain(map, set);
7529 set = isl_map_range(map);
7530 return set;
7531 error:
7532 isl_set_free(set);
7533 isl_map_free(map);
7534 return NULL;
7537 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7538 __isl_take isl_map *map)
7540 return isl_map_align_params_map_map_and(set, map, &set_apply);
7543 /* There is no need to cow as removing empty parts doesn't change
7544 * the meaning of the set.
7546 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7548 int i;
7550 if (!map)
7551 return NULL;
7553 for (i = map->n - 1; i >= 0; --i)
7554 remove_if_empty(map, i);
7556 return map;
7559 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7561 return (struct isl_set *)
7562 isl_map_remove_empty_parts((struct isl_map *)set);
7565 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7567 struct isl_basic_map *bmap;
7568 if (!map || map->n == 0)
7569 return NULL;
7570 bmap = map->p[map->n-1];
7571 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7572 return isl_basic_map_copy(bmap);
7575 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7577 return (struct isl_basic_set *)
7578 isl_map_copy_basic_map((struct isl_map *)set);
7581 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7582 __isl_keep isl_basic_map *bmap)
7584 int i;
7586 if (!map || !bmap)
7587 goto error;
7588 for (i = map->n-1; i >= 0; --i) {
7589 if (map->p[i] != bmap)
7590 continue;
7591 map = isl_map_cow(map);
7592 if (!map)
7593 goto error;
7594 isl_basic_map_free(map->p[i]);
7595 if (i != map->n-1) {
7596 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7597 map->p[i] = map->p[map->n-1];
7599 map->n--;
7600 return map;
7602 return map;
7603 error:
7604 isl_map_free(map);
7605 return NULL;
7608 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7609 struct isl_basic_set *bset)
7611 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7612 (struct isl_basic_map *)bset);
7615 /* Given two basic sets bset1 and bset2, compute the maximal difference
7616 * between the values of dimension pos in bset1 and those in bset2
7617 * for any common value of the parameters and dimensions preceding pos.
7619 static enum isl_lp_result basic_set_maximal_difference_at(
7620 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7621 int pos, isl_int *opt)
7623 isl_space *dims;
7624 struct isl_basic_map *bmap1 = NULL;
7625 struct isl_basic_map *bmap2 = NULL;
7626 struct isl_ctx *ctx;
7627 struct isl_vec *obj;
7628 unsigned total;
7629 unsigned nparam;
7630 unsigned dim1, dim2;
7631 enum isl_lp_result res;
7633 if (!bset1 || !bset2)
7634 return isl_lp_error;
7636 nparam = isl_basic_set_n_param(bset1);
7637 dim1 = isl_basic_set_n_dim(bset1);
7638 dim2 = isl_basic_set_n_dim(bset2);
7639 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7640 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7641 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7642 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7643 if (!bmap1 || !bmap2)
7644 goto error;
7645 bmap1 = isl_basic_map_cow(bmap1);
7646 bmap1 = isl_basic_map_extend(bmap1, nparam,
7647 pos, (dim1 - pos) + (dim2 - pos),
7648 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7649 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7650 if (!bmap1)
7651 goto error;
7652 total = isl_basic_map_total_dim(bmap1);
7653 ctx = bmap1->ctx;
7654 obj = isl_vec_alloc(ctx, 1 + total);
7655 isl_seq_clr(obj->block.data, 1 + total);
7656 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7657 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7658 if (!obj)
7659 goto error;
7660 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7661 opt, NULL, NULL);
7662 isl_basic_map_free(bmap1);
7663 isl_vec_free(obj);
7664 return res;
7665 error:
7666 isl_basic_map_free(bmap1);
7667 isl_basic_map_free(bmap2);
7668 return isl_lp_error;
7671 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7672 * for any common value of the parameters and dimensions preceding pos
7673 * in both basic sets, the values of dimension pos in bset1 are
7674 * smaller or larger than those in bset2.
7676 * Returns
7677 * 1 if bset1 follows bset2
7678 * -1 if bset1 precedes bset2
7679 * 0 if bset1 and bset2 are incomparable
7680 * -2 if some error occurred.
7682 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7683 struct isl_basic_set *bset2, int pos)
7685 isl_int opt;
7686 enum isl_lp_result res;
7687 int cmp;
7689 isl_int_init(opt);
7691 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7693 if (res == isl_lp_empty)
7694 cmp = 0;
7695 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7696 res == isl_lp_unbounded)
7697 cmp = 1;
7698 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7699 cmp = -1;
7700 else
7701 cmp = -2;
7703 isl_int_clear(opt);
7704 return cmp;
7707 /* Given two basic sets bset1 and bset2, check whether
7708 * for any common value of the parameters and dimensions preceding pos
7709 * there is a value of dimension pos in bset1 that is larger
7710 * than a value of the same dimension in bset2.
7712 * Return
7713 * 1 if there exists such a pair
7714 * 0 if there is no such pair, but there is a pair of equal values
7715 * -1 otherwise
7716 * -2 if some error occurred.
7718 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7719 __isl_keep isl_basic_set *bset2, int pos)
7721 isl_int opt;
7722 enum isl_lp_result res;
7723 int cmp;
7725 isl_int_init(opt);
7727 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7729 if (res == isl_lp_empty)
7730 cmp = -1;
7731 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7732 res == isl_lp_unbounded)
7733 cmp = 1;
7734 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7735 cmp = -1;
7736 else if (res == isl_lp_ok)
7737 cmp = 0;
7738 else
7739 cmp = -2;
7741 isl_int_clear(opt);
7742 return cmp;
7745 /* Given two sets set1 and set2, check whether
7746 * for any common value of the parameters and dimensions preceding pos
7747 * there is a value of dimension pos in set1 that is larger
7748 * than a value of the same dimension in set2.
7750 * Return
7751 * 1 if there exists such a pair
7752 * 0 if there is no such pair, but there is a pair of equal values
7753 * -1 otherwise
7754 * -2 if some error occurred.
7756 int isl_set_follows_at(__isl_keep isl_set *set1,
7757 __isl_keep isl_set *set2, int pos)
7759 int i, j;
7760 int follows = -1;
7762 if (!set1 || !set2)
7763 return -2;
7765 for (i = 0; i < set1->n; ++i)
7766 for (j = 0; j < set2->n; ++j) {
7767 int f;
7768 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7769 if (f == 1 || f == -2)
7770 return f;
7771 if (f > follows)
7772 follows = f;
7775 return follows;
7778 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7779 unsigned pos, isl_int *val)
7781 int i;
7782 int d;
7783 unsigned total;
7785 if (!bmap)
7786 return -1;
7787 total = isl_basic_map_total_dim(bmap);
7788 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7789 for (; d+1 > pos; --d)
7790 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7791 break;
7792 if (d != pos)
7793 continue;
7794 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7795 return 0;
7796 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7797 return 0;
7798 if (!isl_int_is_one(bmap->eq[i][1+d]))
7799 return 0;
7800 if (val)
7801 isl_int_neg(*val, bmap->eq[i][0]);
7802 return 1;
7804 return 0;
7807 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7808 unsigned pos, isl_int *val)
7810 int i;
7811 isl_int v;
7812 isl_int tmp;
7813 int fixed;
7815 if (!map)
7816 return -1;
7817 if (map->n == 0)
7818 return 0;
7819 if (map->n == 1)
7820 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7821 isl_int_init(v);
7822 isl_int_init(tmp);
7823 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7824 for (i = 1; fixed == 1 && i < map->n; ++i) {
7825 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7826 if (fixed == 1 && isl_int_ne(tmp, v))
7827 fixed = 0;
7829 if (val)
7830 isl_int_set(*val, v);
7831 isl_int_clear(tmp);
7832 isl_int_clear(v);
7833 return fixed;
7836 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7837 unsigned pos, isl_int *val)
7839 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7840 pos, val);
7843 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7844 isl_int *val)
7846 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7849 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7850 enum isl_dim_type type, unsigned pos, isl_int *val)
7852 if (pos >= isl_basic_map_dim(bmap, type))
7853 return -1;
7854 return isl_basic_map_plain_has_fixed_var(bmap,
7855 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7858 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7859 enum isl_dim_type type, unsigned pos, isl_int *val)
7861 if (pos >= isl_map_dim(map, type))
7862 return -1;
7863 return isl_map_plain_has_fixed_var(map,
7864 map_offset(map, type) - 1 + pos, val);
7867 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
7868 enum isl_dim_type type, unsigned pos, isl_int *val)
7870 return isl_map_plain_is_fixed(set, type, pos, val);
7873 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7874 enum isl_dim_type type, unsigned pos, isl_int *val)
7876 return isl_map_plain_is_fixed(map, type, pos, val);
7879 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7880 * then return this fixed value in *val.
7882 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7883 unsigned dim, isl_int *val)
7885 return isl_basic_set_plain_has_fixed_var(bset,
7886 isl_basic_set_n_param(bset) + dim, val);
7889 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7890 * then return this fixed value in *val.
7892 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7893 unsigned dim, isl_int *val)
7895 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7898 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7899 unsigned dim, isl_int *val)
7901 return isl_set_plain_dim_is_fixed(set, dim, val);
7904 /* Check if input variable in has fixed value and if so and if val is not NULL,
7905 * then return this fixed value in *val.
7907 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7908 unsigned in, isl_int *val)
7910 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7913 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7914 * and if val is not NULL, then return this lower bound in *val.
7916 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7917 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7919 int i, i_eq = -1, i_ineq = -1;
7920 isl_int *c;
7921 unsigned total;
7922 unsigned nparam;
7924 if (!bset)
7925 return -1;
7926 total = isl_basic_set_total_dim(bset);
7927 nparam = isl_basic_set_n_param(bset);
7928 for (i = 0; i < bset->n_eq; ++i) {
7929 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7930 continue;
7931 if (i_eq != -1)
7932 return 0;
7933 i_eq = i;
7935 for (i = 0; i < bset->n_ineq; ++i) {
7936 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7937 continue;
7938 if (i_eq != -1 || i_ineq != -1)
7939 return 0;
7940 i_ineq = i;
7942 if (i_eq == -1 && i_ineq == -1)
7943 return 0;
7944 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7945 /* The coefficient should always be one due to normalization. */
7946 if (!isl_int_is_one(c[1+nparam+dim]))
7947 return 0;
7948 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7949 return 0;
7950 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7951 total - nparam - dim - 1) != -1)
7952 return 0;
7953 if (val)
7954 isl_int_neg(*val, c[0]);
7955 return 1;
7958 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7959 unsigned dim, isl_int *val)
7961 int i;
7962 isl_int v;
7963 isl_int tmp;
7964 int fixed;
7966 if (!set)
7967 return -1;
7968 if (set->n == 0)
7969 return 0;
7970 if (set->n == 1)
7971 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7972 dim, val);
7973 isl_int_init(v);
7974 isl_int_init(tmp);
7975 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7976 dim, &v);
7977 for (i = 1; fixed == 1 && i < set->n; ++i) {
7978 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7979 dim, &tmp);
7980 if (fixed == 1 && isl_int_ne(tmp, v))
7981 fixed = 0;
7983 if (val)
7984 isl_int_set(*val, v);
7985 isl_int_clear(tmp);
7986 isl_int_clear(v);
7987 return fixed;
7990 struct constraint {
7991 unsigned size;
7992 isl_int *c;
7995 /* uset_gist depends on constraints without existentially quantified
7996 * variables sorting first.
7998 static int qsort_constraint_cmp(const void *p1, const void *p2)
8000 const struct constraint *c1 = (const struct constraint *)p1;
8001 const struct constraint *c2 = (const struct constraint *)p2;
8002 int l1, l2;
8003 unsigned size = isl_min(c1->size, c2->size);
8005 l1 = isl_seq_last_non_zero(c1->c, size);
8006 l2 = isl_seq_last_non_zero(c2->c, size);
8008 if (l1 != l2)
8009 return l1 - l2;
8011 return isl_seq_cmp(c1->c, c2->c, size);
8014 static struct isl_basic_map *isl_basic_map_sort_constraints(
8015 struct isl_basic_map *bmap)
8017 int i;
8018 struct constraint *c;
8019 unsigned total;
8021 if (!bmap)
8022 return NULL;
8023 total = isl_basic_map_total_dim(bmap);
8024 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8025 if (!c)
8026 goto error;
8027 for (i = 0; i < bmap->n_ineq; ++i) {
8028 c[i].size = total;
8029 c[i].c = bmap->ineq[i];
8031 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8032 for (i = 0; i < bmap->n_ineq; ++i)
8033 bmap->ineq[i] = c[i].c;
8034 free(c);
8035 return bmap;
8036 error:
8037 isl_basic_map_free(bmap);
8038 return NULL;
8041 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8042 __isl_take isl_basic_set *bset)
8044 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8045 (struct isl_basic_map *)bset);
8048 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8050 if (!bmap)
8051 return NULL;
8052 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8053 return bmap;
8054 bmap = isl_basic_map_remove_redundancies(bmap);
8055 bmap = isl_basic_map_sort_constraints(bmap);
8056 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8057 return bmap;
8060 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8062 return (struct isl_basic_set *)isl_basic_map_normalize(
8063 (struct isl_basic_map *)bset);
8066 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8067 const __isl_keep isl_basic_map *bmap2)
8069 int i, cmp;
8070 unsigned total;
8072 if (bmap1 == bmap2)
8073 return 0;
8074 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8075 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8076 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8077 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8078 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8079 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8080 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8081 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8082 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8083 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8084 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8085 return 0;
8086 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8087 return 1;
8088 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8089 return -1;
8090 if (bmap1->n_eq != bmap2->n_eq)
8091 return bmap1->n_eq - bmap2->n_eq;
8092 if (bmap1->n_ineq != bmap2->n_ineq)
8093 return bmap1->n_ineq - bmap2->n_ineq;
8094 if (bmap1->n_div != bmap2->n_div)
8095 return bmap1->n_div - bmap2->n_div;
8096 total = isl_basic_map_total_dim(bmap1);
8097 for (i = 0; i < bmap1->n_eq; ++i) {
8098 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8099 if (cmp)
8100 return cmp;
8102 for (i = 0; i < bmap1->n_ineq; ++i) {
8103 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8104 if (cmp)
8105 return cmp;
8107 for (i = 0; i < bmap1->n_div; ++i) {
8108 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8109 if (cmp)
8110 return cmp;
8112 return 0;
8115 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8116 const __isl_keep isl_basic_set *bset2)
8118 return isl_basic_map_plain_cmp(bset1, bset2);
8121 int isl_set_plain_cmp(const __isl_keep isl_set *set1,
8122 const __isl_keep isl_set *set2)
8124 int i, cmp;
8126 if (set1 == set2)
8127 return 0;
8128 if (set1->n != set2->n)
8129 return set1->n - set2->n;
8131 for (i = 0; i < set1->n; ++i) {
8132 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8133 if (cmp)
8134 return cmp;
8137 return 0;
8140 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8141 __isl_keep isl_basic_map *bmap2)
8143 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8146 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8147 __isl_keep isl_basic_set *bset2)
8149 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8150 (isl_basic_map *)bset2);
8153 static int qsort_bmap_cmp(const void *p1, const void *p2)
8155 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8156 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8158 return isl_basic_map_plain_cmp(bmap1, bmap2);
8161 /* We normalize in place, but if anything goes wrong we need
8162 * to return NULL, so we need to make sure we don't change the
8163 * meaning of any possible other copies of map.
8165 struct isl_map *isl_map_normalize(struct isl_map *map)
8167 int i, j;
8168 struct isl_basic_map *bmap;
8170 if (!map)
8171 return NULL;
8172 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8173 return map;
8174 for (i = 0; i < map->n; ++i) {
8175 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8176 if (!bmap)
8177 goto error;
8178 isl_basic_map_free(map->p[i]);
8179 map->p[i] = bmap;
8181 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8182 ISL_F_SET(map, ISL_MAP_NORMALIZED);
8183 map = isl_map_remove_empty_parts(map);
8184 if (!map)
8185 return NULL;
8186 for (i = map->n - 1; i >= 1; --i) {
8187 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8188 continue;
8189 isl_basic_map_free(map->p[i-1]);
8190 for (j = i; j < map->n; ++j)
8191 map->p[j-1] = map->p[j];
8192 map->n--;
8194 return map;
8195 error:
8196 isl_map_free(map);
8197 return NULL;
8201 struct isl_set *isl_set_normalize(struct isl_set *set)
8203 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8206 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8208 int i;
8209 int equal;
8211 if (!map1 || !map2)
8212 return -1;
8214 if (map1 == map2)
8215 return 1;
8216 if (!isl_space_is_equal(map1->dim, map2->dim))
8217 return 0;
8219 map1 = isl_map_copy(map1);
8220 map2 = isl_map_copy(map2);
8221 map1 = isl_map_normalize(map1);
8222 map2 = isl_map_normalize(map2);
8223 if (!map1 || !map2)
8224 goto error;
8225 equal = map1->n == map2->n;
8226 for (i = 0; equal && i < map1->n; ++i) {
8227 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8228 if (equal < 0)
8229 goto error;
8231 isl_map_free(map1);
8232 isl_map_free(map2);
8233 return equal;
8234 error:
8235 isl_map_free(map1);
8236 isl_map_free(map2);
8237 return -1;
8240 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8242 return isl_map_plain_is_equal(map1, map2);
8245 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8247 return isl_map_plain_is_equal((struct isl_map *)set1,
8248 (struct isl_map *)set2);
8251 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8253 return isl_set_plain_is_equal(set1, set2);
8256 /* Return an interval that ranges from min to max (inclusive)
8258 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8259 isl_int min, isl_int max)
8261 int k;
8262 struct isl_basic_set *bset = NULL;
8264 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8265 if (!bset)
8266 goto error;
8268 k = isl_basic_set_alloc_inequality(bset);
8269 if (k < 0)
8270 goto error;
8271 isl_int_set_si(bset->ineq[k][1], 1);
8272 isl_int_neg(bset->ineq[k][0], min);
8274 k = isl_basic_set_alloc_inequality(bset);
8275 if (k < 0)
8276 goto error;
8277 isl_int_set_si(bset->ineq[k][1], -1);
8278 isl_int_set(bset->ineq[k][0], max);
8280 return bset;
8281 error:
8282 isl_basic_set_free(bset);
8283 return NULL;
8286 /* Return the Cartesian product of the basic sets in list (in the given order).
8288 __isl_give isl_basic_set *isl_basic_set_list_product(
8289 __isl_take struct isl_basic_set_list *list)
8291 int i;
8292 unsigned dim;
8293 unsigned nparam;
8294 unsigned extra;
8295 unsigned n_eq;
8296 unsigned n_ineq;
8297 struct isl_basic_set *product = NULL;
8299 if (!list)
8300 goto error;
8301 isl_assert(list->ctx, list->n > 0, goto error);
8302 isl_assert(list->ctx, list->p[0], goto error);
8303 nparam = isl_basic_set_n_param(list->p[0]);
8304 dim = isl_basic_set_n_dim(list->p[0]);
8305 extra = list->p[0]->n_div;
8306 n_eq = list->p[0]->n_eq;
8307 n_ineq = list->p[0]->n_ineq;
8308 for (i = 1; i < list->n; ++i) {
8309 isl_assert(list->ctx, list->p[i], goto error);
8310 isl_assert(list->ctx,
8311 nparam == isl_basic_set_n_param(list->p[i]), goto error);
8312 dim += isl_basic_set_n_dim(list->p[i]);
8313 extra += list->p[i]->n_div;
8314 n_eq += list->p[i]->n_eq;
8315 n_ineq += list->p[i]->n_ineq;
8317 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
8318 n_eq, n_ineq);
8319 if (!product)
8320 goto error;
8321 dim = 0;
8322 for (i = 0; i < list->n; ++i) {
8323 isl_basic_set_add_constraints(product,
8324 isl_basic_set_copy(list->p[i]), dim);
8325 dim += isl_basic_set_n_dim(list->p[i]);
8327 isl_basic_set_list_free(list);
8328 return product;
8329 error:
8330 isl_basic_set_free(product);
8331 isl_basic_set_list_free(list);
8332 return NULL;
8335 struct isl_basic_map *isl_basic_map_product(
8336 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8338 isl_space *dim_result = NULL;
8339 struct isl_basic_map *bmap;
8340 unsigned in1, in2, out1, out2, nparam, total, pos;
8341 struct isl_dim_map *dim_map1, *dim_map2;
8343 if (!bmap1 || !bmap2)
8344 goto error;
8346 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
8347 bmap2->dim, isl_dim_param), goto error);
8348 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
8349 isl_space_copy(bmap2->dim));
8351 in1 = isl_basic_map_n_in(bmap1);
8352 in2 = isl_basic_map_n_in(bmap2);
8353 out1 = isl_basic_map_n_out(bmap1);
8354 out2 = isl_basic_map_n_out(bmap2);
8355 nparam = isl_basic_map_n_param(bmap1);
8357 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
8358 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8359 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8360 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8361 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8362 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8363 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8364 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8365 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8366 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8367 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8369 bmap = isl_basic_map_alloc_space(dim_result,
8370 bmap1->n_div + bmap2->n_div,
8371 bmap1->n_eq + bmap2->n_eq,
8372 bmap1->n_ineq + bmap2->n_ineq);
8373 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8374 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8375 bmap = isl_basic_map_simplify(bmap);
8376 return isl_basic_map_finalize(bmap);
8377 error:
8378 isl_basic_map_free(bmap1);
8379 isl_basic_map_free(bmap2);
8380 return NULL;
8383 __isl_give isl_basic_map *isl_basic_map_flat_product(
8384 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8386 isl_basic_map *prod;
8388 prod = isl_basic_map_product(bmap1, bmap2);
8389 prod = isl_basic_map_flatten(prod);
8390 return prod;
8393 __isl_give isl_basic_set *isl_basic_set_flat_product(
8394 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
8396 return isl_basic_map_flat_range_product(bset1, bset2);
8399 __isl_give isl_basic_map *isl_basic_map_domain_product(
8400 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8402 isl_space *space_result = NULL;
8403 isl_basic_map *bmap;
8404 unsigned in1, in2, out, nparam, total, pos;
8405 struct isl_dim_map *dim_map1, *dim_map2;
8407 if (!bmap1 || !bmap2)
8408 goto error;
8410 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8411 isl_space_copy(bmap2->dim));
8413 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8414 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8415 out = isl_basic_map_dim(bmap1, isl_dim_out);
8416 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8418 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8419 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8420 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8421 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8422 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8423 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8424 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8425 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8426 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8427 isl_dim_map_div(dim_map1, bmap1, pos += out);
8428 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8430 bmap = isl_basic_map_alloc_space(space_result,
8431 bmap1->n_div + bmap2->n_div,
8432 bmap1->n_eq + bmap2->n_eq,
8433 bmap1->n_ineq + bmap2->n_ineq);
8434 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8435 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8436 bmap = isl_basic_map_simplify(bmap);
8437 return isl_basic_map_finalize(bmap);
8438 error:
8439 isl_basic_map_free(bmap1);
8440 isl_basic_map_free(bmap2);
8441 return NULL;
8444 __isl_give isl_basic_map *isl_basic_map_range_product(
8445 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8447 isl_space *dim_result = NULL;
8448 isl_basic_map *bmap;
8449 unsigned in, out1, out2, nparam, total, pos;
8450 struct isl_dim_map *dim_map1, *dim_map2;
8452 if (!bmap1 || !bmap2)
8453 goto error;
8455 if (!isl_space_match(bmap1->dim, isl_dim_param,
8456 bmap2->dim, isl_dim_param))
8457 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
8458 "parameters don't match", goto error);
8460 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8461 isl_space_copy(bmap2->dim));
8463 in = isl_basic_map_dim(bmap1, isl_dim_in);
8464 out1 = isl_basic_map_n_out(bmap1);
8465 out2 = isl_basic_map_n_out(bmap2);
8466 nparam = isl_basic_map_n_param(bmap1);
8468 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8469 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8470 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8471 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8472 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8473 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8474 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8475 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8476 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8477 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8478 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8480 bmap = isl_basic_map_alloc_space(dim_result,
8481 bmap1->n_div + bmap2->n_div,
8482 bmap1->n_eq + bmap2->n_eq,
8483 bmap1->n_ineq + bmap2->n_ineq);
8484 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8485 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8486 bmap = isl_basic_map_simplify(bmap);
8487 return isl_basic_map_finalize(bmap);
8488 error:
8489 isl_basic_map_free(bmap1);
8490 isl_basic_map_free(bmap2);
8491 return NULL;
8494 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8495 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8497 isl_basic_map *prod;
8499 prod = isl_basic_map_range_product(bmap1, bmap2);
8500 prod = isl_basic_map_flatten_range(prod);
8501 return prod;
8504 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8505 __isl_take isl_map *map2,
8506 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8507 __isl_take isl_space *right),
8508 __isl_give isl_basic_map *(*basic_map_product)(
8509 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8511 unsigned flags = 0;
8512 struct isl_map *result;
8513 int i, j;
8515 if (!map1 || !map2)
8516 goto error;
8518 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8519 map2->dim, isl_dim_param), goto error);
8521 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8522 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8523 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8525 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8526 isl_space_copy(map2->dim)),
8527 map1->n * map2->n, flags);
8528 if (!result)
8529 goto error;
8530 for (i = 0; i < map1->n; ++i)
8531 for (j = 0; j < map2->n; ++j) {
8532 struct isl_basic_map *part;
8533 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8534 isl_basic_map_copy(map2->p[j]));
8535 if (isl_basic_map_is_empty(part))
8536 isl_basic_map_free(part);
8537 else
8538 result = isl_map_add_basic_map(result, part);
8539 if (!result)
8540 goto error;
8542 isl_map_free(map1);
8543 isl_map_free(map2);
8544 return result;
8545 error:
8546 isl_map_free(map1);
8547 isl_map_free(map2);
8548 return NULL;
8551 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8553 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8554 __isl_take isl_map *map2)
8556 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8559 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8560 __isl_take isl_map *map2)
8562 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8565 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8567 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8568 __isl_take isl_map *map2)
8570 isl_map *prod;
8572 prod = isl_map_product(map1, map2);
8573 prod = isl_map_flatten(prod);
8574 return prod;
8577 /* Given two set A and B, construct its Cartesian product A x B.
8579 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8581 return isl_map_range_product(set1, set2);
8584 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8585 __isl_take isl_set *set2)
8587 return isl_map_flat_range_product(set1, set2);
8590 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8592 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8593 __isl_take isl_map *map2)
8595 return map_product(map1, map2, &isl_space_domain_product,
8596 &isl_basic_map_domain_product);
8599 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8601 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8602 __isl_take isl_map *map2)
8604 return map_product(map1, map2, &isl_space_range_product,
8605 &isl_basic_map_range_product);
8608 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8609 __isl_take isl_map *map2)
8611 return isl_map_align_params_map_map_and(map1, map2,
8612 &map_domain_product_aligned);
8615 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8616 __isl_take isl_map *map2)
8618 return isl_map_align_params_map_map_and(map1, map2,
8619 &map_range_product_aligned);
8622 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8624 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8625 __isl_take isl_map *map2)
8627 isl_map *prod;
8629 prod = isl_map_domain_product(map1, map2);
8630 prod = isl_map_flatten_domain(prod);
8631 return prod;
8634 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8636 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8637 __isl_take isl_map *map2)
8639 isl_map *prod;
8641 prod = isl_map_range_product(map1, map2);
8642 prod = isl_map_flatten_range(prod);
8643 return prod;
8646 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8648 int i;
8649 uint32_t hash = isl_hash_init();
8650 unsigned total;
8652 if (!bmap)
8653 return 0;
8654 bmap = isl_basic_map_copy(bmap);
8655 bmap = isl_basic_map_normalize(bmap);
8656 if (!bmap)
8657 return 0;
8658 total = isl_basic_map_total_dim(bmap);
8659 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8660 for (i = 0; i < bmap->n_eq; ++i) {
8661 uint32_t c_hash;
8662 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8663 isl_hash_hash(hash, c_hash);
8665 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8666 for (i = 0; i < bmap->n_ineq; ++i) {
8667 uint32_t c_hash;
8668 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8669 isl_hash_hash(hash, c_hash);
8671 isl_hash_byte(hash, bmap->n_div & 0xFF);
8672 for (i = 0; i < bmap->n_div; ++i) {
8673 uint32_t c_hash;
8674 if (isl_int_is_zero(bmap->div[i][0]))
8675 continue;
8676 isl_hash_byte(hash, i & 0xFF);
8677 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8678 isl_hash_hash(hash, c_hash);
8680 isl_basic_map_free(bmap);
8681 return hash;
8684 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8686 return isl_basic_map_get_hash((isl_basic_map *)bset);
8689 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8691 int i;
8692 uint32_t hash;
8694 if (!map)
8695 return 0;
8696 map = isl_map_copy(map);
8697 map = isl_map_normalize(map);
8698 if (!map)
8699 return 0;
8701 hash = isl_hash_init();
8702 for (i = 0; i < map->n; ++i) {
8703 uint32_t bmap_hash;
8704 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8705 isl_hash_hash(hash, bmap_hash);
8708 isl_map_free(map);
8710 return hash;
8713 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8715 return isl_map_get_hash((isl_map *)set);
8718 /* Check if the value for dimension dim is completely determined
8719 * by the values of the other parameters and variables.
8720 * That is, check if dimension dim is involved in an equality.
8722 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8724 int i;
8725 unsigned nparam;
8727 if (!bset)
8728 return -1;
8729 nparam = isl_basic_set_n_param(bset);
8730 for (i = 0; i < bset->n_eq; ++i)
8731 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8732 return 1;
8733 return 0;
8736 /* Check if the value for dimension dim is completely determined
8737 * by the values of the other parameters and variables.
8738 * That is, check if dimension dim is involved in an equality
8739 * for each of the subsets.
8741 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8743 int i;
8745 if (!set)
8746 return -1;
8747 for (i = 0; i < set->n; ++i) {
8748 int unique;
8749 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8750 if (unique != 1)
8751 return unique;
8753 return 1;
8756 int isl_set_n_basic_set(__isl_keep isl_set *set)
8758 return set ? set->n : 0;
8761 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8762 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8764 int i;
8766 if (!map)
8767 return -1;
8769 for (i = 0; i < map->n; ++i)
8770 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8771 return -1;
8773 return 0;
8776 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8777 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8779 int i;
8781 if (!set)
8782 return -1;
8784 for (i = 0; i < set->n; ++i)
8785 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8786 return -1;
8788 return 0;
8791 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8793 isl_space *dim;
8795 if (!bset)
8796 return NULL;
8798 bset = isl_basic_set_cow(bset);
8799 if (!bset)
8800 return NULL;
8802 dim = isl_basic_set_get_space(bset);
8803 dim = isl_space_lift(dim, bset->n_div);
8804 if (!dim)
8805 goto error;
8806 isl_space_free(bset->dim);
8807 bset->dim = dim;
8808 bset->extra -= bset->n_div;
8809 bset->n_div = 0;
8811 bset = isl_basic_set_finalize(bset);
8813 return bset;
8814 error:
8815 isl_basic_set_free(bset);
8816 return NULL;
8819 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8821 int i;
8822 isl_space *dim;
8823 unsigned n_div;
8825 set = isl_set_align_divs(set);
8827 if (!set)
8828 return NULL;
8830 set = isl_set_cow(set);
8831 if (!set)
8832 return NULL;
8834 n_div = set->p[0]->n_div;
8835 dim = isl_set_get_space(set);
8836 dim = isl_space_lift(dim, n_div);
8837 if (!dim)
8838 goto error;
8839 isl_space_free(set->dim);
8840 set->dim = dim;
8842 for (i = 0; i < set->n; ++i) {
8843 set->p[i] = isl_basic_set_lift(set->p[i]);
8844 if (!set->p[i])
8845 goto error;
8848 return set;
8849 error:
8850 isl_set_free(set);
8851 return NULL;
8854 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8856 isl_space *dim;
8857 struct isl_basic_map *bmap;
8858 unsigned n_set;
8859 unsigned n_div;
8860 unsigned n_param;
8861 unsigned total;
8862 int i, k, l;
8864 set = isl_set_align_divs(set);
8866 if (!set)
8867 return NULL;
8869 dim = isl_set_get_space(set);
8870 if (set->n == 0 || set->p[0]->n_div == 0) {
8871 isl_set_free(set);
8872 return isl_map_identity(isl_space_map_from_set(dim));
8875 n_div = set->p[0]->n_div;
8876 dim = isl_space_map_from_set(dim);
8877 n_param = isl_space_dim(dim, isl_dim_param);
8878 n_set = isl_space_dim(dim, isl_dim_in);
8879 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8880 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8881 for (i = 0; i < n_set; ++i)
8882 bmap = var_equal(bmap, i);
8884 total = n_param + n_set + n_set + n_div;
8885 for (i = 0; i < n_div; ++i) {
8886 k = isl_basic_map_alloc_inequality(bmap);
8887 if (k < 0)
8888 goto error;
8889 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8890 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8891 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8892 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8893 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8894 set->p[0]->div[i][0]);
8896 l = isl_basic_map_alloc_inequality(bmap);
8897 if (l < 0)
8898 goto error;
8899 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8900 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8901 set->p[0]->div[i][0]);
8902 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8905 isl_set_free(set);
8906 bmap = isl_basic_map_simplify(bmap);
8907 bmap = isl_basic_map_finalize(bmap);
8908 return isl_map_from_basic_map(bmap);
8909 error:
8910 isl_set_free(set);
8911 isl_basic_map_free(bmap);
8912 return NULL;
8915 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8917 unsigned dim;
8918 int size = 0;
8920 if (!bset)
8921 return -1;
8923 dim = isl_basic_set_total_dim(bset);
8924 size += bset->n_eq * (1 + dim);
8925 size += bset->n_ineq * (1 + dim);
8926 size += bset->n_div * (2 + dim);
8928 return size;
8931 int isl_set_size(__isl_keep isl_set *set)
8933 int i;
8934 int size = 0;
8936 if (!set)
8937 return -1;
8939 for (i = 0; i < set->n; ++i)
8940 size += isl_basic_set_size(set->p[i]);
8942 return size;
8945 /* Check if there is any lower bound (if lower == 0) and/or upper
8946 * bound (if upper == 0) on the specified dim.
8948 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8949 enum isl_dim_type type, unsigned pos, int lower, int upper)
8951 int i;
8953 if (!bmap)
8954 return -1;
8956 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8958 pos += isl_basic_map_offset(bmap, type);
8960 for (i = 0; i < bmap->n_div; ++i) {
8961 if (isl_int_is_zero(bmap->div[i][0]))
8962 continue;
8963 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8964 return 1;
8967 for (i = 0; i < bmap->n_eq; ++i)
8968 if (!isl_int_is_zero(bmap->eq[i][pos]))
8969 return 1;
8971 for (i = 0; i < bmap->n_ineq; ++i) {
8972 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8973 if (sgn > 0)
8974 lower = 1;
8975 if (sgn < 0)
8976 upper = 1;
8979 return lower && upper;
8982 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8983 enum isl_dim_type type, unsigned pos)
8985 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8988 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8989 enum isl_dim_type type, unsigned pos)
8991 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8994 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8995 enum isl_dim_type type, unsigned pos)
8997 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9000 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9001 enum isl_dim_type type, unsigned pos)
9003 int i;
9005 if (!map)
9006 return -1;
9008 for (i = 0; i < map->n; ++i) {
9009 int bounded;
9010 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9011 if (bounded < 0 || !bounded)
9012 return bounded;
9015 return 1;
9018 /* Return 1 if the specified dim is involved in both an upper bound
9019 * and a lower bound.
9021 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9022 enum isl_dim_type type, unsigned pos)
9024 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9027 static int has_bound(__isl_keep isl_map *map,
9028 enum isl_dim_type type, unsigned pos,
9029 int (*fn)(__isl_keep isl_basic_map *bmap,
9030 enum isl_dim_type type, unsigned pos))
9032 int i;
9034 if (!map)
9035 return -1;
9037 for (i = 0; i < map->n; ++i) {
9038 int bounded;
9039 bounded = fn(map->p[i], type, pos);
9040 if (bounded < 0 || bounded)
9041 return bounded;
9044 return 0;
9047 /* Return 1 if the specified dim is involved in any lower bound.
9049 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9050 enum isl_dim_type type, unsigned pos)
9052 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9055 /* Return 1 if the specified dim is involved in any upper bound.
9057 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9058 enum isl_dim_type type, unsigned pos)
9060 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9063 /* For each of the "n" variables starting at "first", determine
9064 * the sign of the variable and put the results in the first "n"
9065 * elements of the array "signs".
9066 * Sign
9067 * 1 means that the variable is non-negative
9068 * -1 means that the variable is non-positive
9069 * 0 means the variable attains both positive and negative values.
9071 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9072 unsigned first, unsigned n, int *signs)
9074 isl_vec *bound = NULL;
9075 struct isl_tab *tab = NULL;
9076 struct isl_tab_undo *snap;
9077 int i;
9079 if (!bset || !signs)
9080 return -1;
9082 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9083 tab = isl_tab_from_basic_set(bset, 0);
9084 if (!bound || !tab)
9085 goto error;
9087 isl_seq_clr(bound->el, bound->size);
9088 isl_int_set_si(bound->el[0], -1);
9090 snap = isl_tab_snap(tab);
9091 for (i = 0; i < n; ++i) {
9092 int empty;
9094 isl_int_set_si(bound->el[1 + first + i], -1);
9095 if (isl_tab_add_ineq(tab, bound->el) < 0)
9096 goto error;
9097 empty = tab->empty;
9098 isl_int_set_si(bound->el[1 + first + i], 0);
9099 if (isl_tab_rollback(tab, snap) < 0)
9100 goto error;
9102 if (empty) {
9103 signs[i] = 1;
9104 continue;
9107 isl_int_set_si(bound->el[1 + first + i], 1);
9108 if (isl_tab_add_ineq(tab, bound->el) < 0)
9109 goto error;
9110 empty = tab->empty;
9111 isl_int_set_si(bound->el[1 + first + i], 0);
9112 if (isl_tab_rollback(tab, snap) < 0)
9113 goto error;
9115 signs[i] = empty ? -1 : 0;
9118 isl_tab_free(tab);
9119 isl_vec_free(bound);
9120 return 0;
9121 error:
9122 isl_tab_free(tab);
9123 isl_vec_free(bound);
9124 return -1;
9127 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
9128 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
9130 if (!bset || !signs)
9131 return -1;
9132 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
9133 return -1);
9135 first += pos(bset->dim, type) - 1;
9136 return isl_basic_set_vars_get_sign(bset, first, n, signs);
9139 /* Check if the given basic map is obviously single-valued.
9140 * In particular, for each output dimension, check that there is
9141 * an equality that defines the output dimension in terms of
9142 * earlier dimensions.
9144 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
9146 int i, j;
9147 unsigned total;
9148 unsigned n_out;
9149 unsigned o_out;
9151 if (!bmap)
9152 return -1;
9154 total = 1 + isl_basic_map_total_dim(bmap);
9155 n_out = isl_basic_map_dim(bmap, isl_dim_out);
9156 o_out = isl_basic_map_offset(bmap, isl_dim_out);
9158 for (i = 0; i < n_out; ++i) {
9159 for (j = 0; j < bmap->n_eq; ++j) {
9160 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
9161 continue;
9162 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
9163 total - (o_out + i + 1)) == -1)
9164 break;
9166 if (j >= bmap->n_eq)
9167 return 0;
9170 return 1;
9173 /* Check if the given basic map is single-valued.
9174 * We simply compute
9176 * M \circ M^-1
9178 * and check if the result is a subset of the identity mapping.
9180 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
9182 isl_space *space;
9183 isl_basic_map *test;
9184 isl_basic_map *id;
9185 int sv;
9187 sv = isl_basic_map_plain_is_single_valued(bmap);
9188 if (sv < 0 || sv)
9189 return sv;
9191 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
9192 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
9194 space = isl_basic_map_get_space(bmap);
9195 space = isl_space_map_from_set(isl_space_range(space));
9196 id = isl_basic_map_identity(space);
9198 sv = isl_basic_map_is_subset(test, id);
9200 isl_basic_map_free(test);
9201 isl_basic_map_free(id);
9203 return sv;
9206 /* Check if the given map is obviously single-valued.
9208 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
9210 if (!map)
9211 return -1;
9212 if (map->n == 0)
9213 return 1;
9214 if (map->n >= 2)
9215 return 0;
9217 return isl_basic_map_plain_is_single_valued(map->p[0]);
9220 /* Check if the given map is single-valued.
9221 * We simply compute
9223 * M \circ M^-1
9225 * and check if the result is a subset of the identity mapping.
9227 int isl_map_is_single_valued(__isl_keep isl_map *map)
9229 isl_space *dim;
9230 isl_map *test;
9231 isl_map *id;
9232 int sv;
9234 sv = isl_map_plain_is_single_valued(map);
9235 if (sv < 0 || sv)
9236 return sv;
9238 test = isl_map_reverse(isl_map_copy(map));
9239 test = isl_map_apply_range(test, isl_map_copy(map));
9241 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
9242 id = isl_map_identity(dim);
9244 sv = isl_map_is_subset(test, id);
9246 isl_map_free(test);
9247 isl_map_free(id);
9249 return sv;
9252 int isl_map_is_injective(__isl_keep isl_map *map)
9254 int in;
9256 map = isl_map_copy(map);
9257 map = isl_map_reverse(map);
9258 in = isl_map_is_single_valued(map);
9259 isl_map_free(map);
9261 return in;
9264 /* Check if the given map is obviously injective.
9266 int isl_map_plain_is_injective(__isl_keep isl_map *map)
9268 int in;
9270 map = isl_map_copy(map);
9271 map = isl_map_reverse(map);
9272 in = isl_map_plain_is_single_valued(map);
9273 isl_map_free(map);
9275 return in;
9278 int isl_map_is_bijective(__isl_keep isl_map *map)
9280 int sv;
9282 sv = isl_map_is_single_valued(map);
9283 if (sv < 0 || !sv)
9284 return sv;
9286 return isl_map_is_injective(map);
9289 int isl_set_is_singleton(__isl_keep isl_set *set)
9291 return isl_map_is_single_valued((isl_map *)set);
9294 int isl_map_is_translation(__isl_keep isl_map *map)
9296 int ok;
9297 isl_set *delta;
9299 delta = isl_map_deltas(isl_map_copy(map));
9300 ok = isl_set_is_singleton(delta);
9301 isl_set_free(delta);
9303 return ok;
9306 static int unique(isl_int *p, unsigned pos, unsigned len)
9308 if (isl_seq_first_non_zero(p, pos) != -1)
9309 return 0;
9310 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
9311 return 0;
9312 return 1;
9315 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
9317 int i, j;
9318 unsigned nvar;
9319 unsigned ovar;
9321 if (!bset)
9322 return -1;
9324 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
9325 return 0;
9327 nvar = isl_basic_set_dim(bset, isl_dim_set);
9328 ovar = isl_space_offset(bset->dim, isl_dim_set);
9329 for (j = 0; j < nvar; ++j) {
9330 int lower = 0, upper = 0;
9331 for (i = 0; i < bset->n_eq; ++i) {
9332 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
9333 continue;
9334 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
9335 return 0;
9336 break;
9338 if (i < bset->n_eq)
9339 continue;
9340 for (i = 0; i < bset->n_ineq; ++i) {
9341 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
9342 continue;
9343 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
9344 return 0;
9345 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
9346 lower = 1;
9347 else
9348 upper = 1;
9350 if (!lower || !upper)
9351 return 0;
9354 return 1;
9357 int isl_set_is_box(__isl_keep isl_set *set)
9359 if (!set)
9360 return -1;
9361 if (set->n != 1)
9362 return 0;
9364 return isl_basic_set_is_box(set->p[0]);
9367 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
9369 if (!bset)
9370 return -1;
9372 return isl_space_is_wrapping(bset->dim);
9375 int isl_set_is_wrapping(__isl_keep isl_set *set)
9377 if (!set)
9378 return -1;
9380 return isl_space_is_wrapping(set->dim);
9383 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
9385 bmap = isl_basic_map_cow(bmap);
9386 if (!bmap)
9387 return NULL;
9389 bmap->dim = isl_space_wrap(bmap->dim);
9390 if (!bmap->dim)
9391 goto error;
9393 bmap = isl_basic_map_finalize(bmap);
9395 return (isl_basic_set *)bmap;
9396 error:
9397 isl_basic_map_free(bmap);
9398 return NULL;
9401 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
9403 int i;
9405 map = isl_map_cow(map);
9406 if (!map)
9407 return NULL;
9409 for (i = 0; i < map->n; ++i) {
9410 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
9411 if (!map->p[i])
9412 goto error;
9414 map->dim = isl_space_wrap(map->dim);
9415 if (!map->dim)
9416 goto error;
9418 return (isl_set *)map;
9419 error:
9420 isl_map_free(map);
9421 return NULL;
9424 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
9426 bset = isl_basic_set_cow(bset);
9427 if (!bset)
9428 return NULL;
9430 bset->dim = isl_space_unwrap(bset->dim);
9431 if (!bset->dim)
9432 goto error;
9434 bset = isl_basic_set_finalize(bset);
9436 return (isl_basic_map *)bset;
9437 error:
9438 isl_basic_set_free(bset);
9439 return NULL;
9442 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
9444 int i;
9446 if (!set)
9447 return NULL;
9449 if (!isl_set_is_wrapping(set))
9450 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
9451 goto error);
9453 set = isl_set_cow(set);
9454 if (!set)
9455 return NULL;
9457 for (i = 0; i < set->n; ++i) {
9458 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9459 if (!set->p[i])
9460 goto error;
9463 set->dim = isl_space_unwrap(set->dim);
9464 if (!set->dim)
9465 goto error;
9467 return (isl_map *)set;
9468 error:
9469 isl_set_free(set);
9470 return NULL;
9473 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9474 enum isl_dim_type type)
9476 if (!bmap)
9477 return NULL;
9479 if (!isl_space_is_named_or_nested(bmap->dim, type))
9480 return bmap;
9482 bmap = isl_basic_map_cow(bmap);
9483 if (!bmap)
9484 return NULL;
9486 bmap->dim = isl_space_reset(bmap->dim, type);
9487 if (!bmap->dim)
9488 goto error;
9490 bmap = isl_basic_map_finalize(bmap);
9492 return bmap;
9493 error:
9494 isl_basic_map_free(bmap);
9495 return NULL;
9498 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
9499 enum isl_dim_type type)
9501 int i;
9503 if (!map)
9504 return NULL;
9506 if (!isl_space_is_named_or_nested(map->dim, type))
9507 return map;
9509 map = isl_map_cow(map);
9510 if (!map)
9511 return NULL;
9513 for (i = 0; i < map->n; ++i) {
9514 map->p[i] = isl_basic_map_reset(map->p[i], type);
9515 if (!map->p[i])
9516 goto error;
9518 map->dim = isl_space_reset(map->dim, type);
9519 if (!map->dim)
9520 goto error;
9522 return map;
9523 error:
9524 isl_map_free(map);
9525 return NULL;
9528 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
9530 if (!bmap)
9531 return NULL;
9533 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
9534 return bmap;
9536 bmap = isl_basic_map_cow(bmap);
9537 if (!bmap)
9538 return NULL;
9540 bmap->dim = isl_space_flatten(bmap->dim);
9541 if (!bmap->dim)
9542 goto error;
9544 bmap = isl_basic_map_finalize(bmap);
9546 return bmap;
9547 error:
9548 isl_basic_map_free(bmap);
9549 return NULL;
9552 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9554 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9557 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9558 __isl_take isl_basic_map *bmap)
9560 if (!bmap)
9561 return NULL;
9563 if (!bmap->dim->nested[0])
9564 return bmap;
9566 bmap = isl_basic_map_cow(bmap);
9567 if (!bmap)
9568 return NULL;
9570 bmap->dim = isl_space_flatten_domain(bmap->dim);
9571 if (!bmap->dim)
9572 goto error;
9574 bmap = isl_basic_map_finalize(bmap);
9576 return bmap;
9577 error:
9578 isl_basic_map_free(bmap);
9579 return NULL;
9582 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9583 __isl_take isl_basic_map *bmap)
9585 if (!bmap)
9586 return NULL;
9588 if (!bmap->dim->nested[1])
9589 return bmap;
9591 bmap = isl_basic_map_cow(bmap);
9592 if (!bmap)
9593 return NULL;
9595 bmap->dim = isl_space_flatten_range(bmap->dim);
9596 if (!bmap->dim)
9597 goto error;
9599 bmap = isl_basic_map_finalize(bmap);
9601 return bmap;
9602 error:
9603 isl_basic_map_free(bmap);
9604 return NULL;
9607 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9609 int i;
9611 if (!map)
9612 return NULL;
9614 if (!map->dim->nested[0] && !map->dim->nested[1])
9615 return map;
9617 map = isl_map_cow(map);
9618 if (!map)
9619 return NULL;
9621 for (i = 0; i < map->n; ++i) {
9622 map->p[i] = isl_basic_map_flatten(map->p[i]);
9623 if (!map->p[i])
9624 goto error;
9626 map->dim = isl_space_flatten(map->dim);
9627 if (!map->dim)
9628 goto error;
9630 return map;
9631 error:
9632 isl_map_free(map);
9633 return NULL;
9636 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9638 return (isl_set *)isl_map_flatten((isl_map *)set);
9641 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9643 isl_space *dim, *flat_dim;
9644 isl_map *map;
9646 dim = isl_set_get_space(set);
9647 flat_dim = isl_space_flatten(isl_space_copy(dim));
9648 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9649 map = isl_map_intersect_domain(map, set);
9651 return map;
9654 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9656 int i;
9658 if (!map)
9659 return NULL;
9661 if (!map->dim->nested[0])
9662 return map;
9664 map = isl_map_cow(map);
9665 if (!map)
9666 return NULL;
9668 for (i = 0; i < map->n; ++i) {
9669 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9670 if (!map->p[i])
9671 goto error;
9673 map->dim = isl_space_flatten_domain(map->dim);
9674 if (!map->dim)
9675 goto error;
9677 return map;
9678 error:
9679 isl_map_free(map);
9680 return NULL;
9683 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9685 int i;
9687 if (!map)
9688 return NULL;
9690 if (!map->dim->nested[1])
9691 return map;
9693 map = isl_map_cow(map);
9694 if (!map)
9695 return NULL;
9697 for (i = 0; i < map->n; ++i) {
9698 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9699 if (!map->p[i])
9700 goto error;
9702 map->dim = isl_space_flatten_range(map->dim);
9703 if (!map->dim)
9704 goto error;
9706 return map;
9707 error:
9708 isl_map_free(map);
9709 return NULL;
9712 /* Reorder the dimensions of "bmap" according to the given dim_map
9713 * and set the dimension specification to "dim".
9715 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9716 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9718 isl_basic_map *res;
9719 unsigned flags;
9721 bmap = isl_basic_map_cow(bmap);
9722 if (!bmap || !dim || !dim_map)
9723 goto error;
9725 flags = bmap->flags;
9726 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
9727 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
9728 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
9729 res = isl_basic_map_alloc_space(dim,
9730 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9731 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9732 if (res)
9733 res->flags = flags;
9734 res = isl_basic_map_finalize(res);
9735 return res;
9736 error:
9737 free(dim_map);
9738 isl_basic_map_free(bmap);
9739 isl_space_free(dim);
9740 return NULL;
9743 /* Reorder the dimensions of "map" according to given reordering.
9745 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9746 __isl_take isl_reordering *r)
9748 int i;
9749 struct isl_dim_map *dim_map;
9751 map = isl_map_cow(map);
9752 dim_map = isl_dim_map_from_reordering(r);
9753 if (!map || !r || !dim_map)
9754 goto error;
9756 for (i = 0; i < map->n; ++i) {
9757 struct isl_dim_map *dim_map_i;
9759 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9761 map->p[i] = isl_basic_map_realign(map->p[i],
9762 isl_space_copy(r->dim), dim_map_i);
9764 if (!map->p[i])
9765 goto error;
9768 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9770 isl_reordering_free(r);
9771 free(dim_map);
9772 return map;
9773 error:
9774 free(dim_map);
9775 isl_map_free(map);
9776 isl_reordering_free(r);
9777 return NULL;
9780 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9781 __isl_take isl_reordering *r)
9783 return (isl_set *)isl_map_realign((isl_map *)set, r);
9786 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9787 __isl_take isl_space *model)
9789 isl_ctx *ctx;
9791 if (!map || !model)
9792 goto error;
9794 ctx = isl_space_get_ctx(model);
9795 if (!isl_space_has_named_params(model))
9796 isl_die(ctx, isl_error_invalid,
9797 "model has unnamed parameters", goto error);
9798 if (!isl_space_has_named_params(map->dim))
9799 isl_die(ctx, isl_error_invalid,
9800 "relation has unnamed parameters", goto error);
9801 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9802 isl_reordering *exp;
9804 model = isl_space_drop_dims(model, isl_dim_in,
9805 0, isl_space_dim(model, isl_dim_in));
9806 model = isl_space_drop_dims(model, isl_dim_out,
9807 0, isl_space_dim(model, isl_dim_out));
9808 exp = isl_parameter_alignment_reordering(map->dim, model);
9809 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9810 map = isl_map_realign(map, exp);
9813 isl_space_free(model);
9814 return map;
9815 error:
9816 isl_space_free(model);
9817 isl_map_free(map);
9818 return NULL;
9821 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9822 __isl_take isl_space *model)
9824 return isl_map_align_params(set, model);
9827 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9828 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9829 enum isl_dim_type c2, enum isl_dim_type c3,
9830 enum isl_dim_type c4, enum isl_dim_type c5)
9832 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9833 struct isl_mat *mat;
9834 int i, j, k;
9835 int pos;
9837 if (!bmap)
9838 return NULL;
9839 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9840 isl_basic_map_total_dim(bmap) + 1);
9841 if (!mat)
9842 return NULL;
9843 for (i = 0; i < bmap->n_eq; ++i)
9844 for (j = 0, pos = 0; j < 5; ++j) {
9845 int off = isl_basic_map_offset(bmap, c[j]);
9846 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9847 isl_int_set(mat->row[i][pos],
9848 bmap->eq[i][off + k]);
9849 ++pos;
9853 return mat;
9856 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9857 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9858 enum isl_dim_type c2, enum isl_dim_type c3,
9859 enum isl_dim_type c4, enum isl_dim_type c5)
9861 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9862 struct isl_mat *mat;
9863 int i, j, k;
9864 int pos;
9866 if (!bmap)
9867 return NULL;
9868 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9869 isl_basic_map_total_dim(bmap) + 1);
9870 if (!mat)
9871 return NULL;
9872 for (i = 0; i < bmap->n_ineq; ++i)
9873 for (j = 0, pos = 0; j < 5; ++j) {
9874 int off = isl_basic_map_offset(bmap, c[j]);
9875 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9876 isl_int_set(mat->row[i][pos],
9877 bmap->ineq[i][off + k]);
9878 ++pos;
9882 return mat;
9885 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9886 __isl_take isl_space *dim,
9887 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9888 enum isl_dim_type c2, enum isl_dim_type c3,
9889 enum isl_dim_type c4, enum isl_dim_type c5)
9891 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9892 isl_basic_map *bmap;
9893 unsigned total;
9894 unsigned extra;
9895 int i, j, k, l;
9896 int pos;
9898 if (!dim || !eq || !ineq)
9899 goto error;
9901 if (eq->n_col != ineq->n_col)
9902 isl_die(dim->ctx, isl_error_invalid,
9903 "equalities and inequalities matrices should have "
9904 "same number of columns", goto error);
9906 total = 1 + isl_space_dim(dim, isl_dim_all);
9908 if (eq->n_col < total)
9909 isl_die(dim->ctx, isl_error_invalid,
9910 "number of columns too small", goto error);
9912 extra = eq->n_col - total;
9914 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9915 eq->n_row, ineq->n_row);
9916 if (!bmap)
9917 goto error;
9918 for (i = 0; i < extra; ++i) {
9919 k = isl_basic_map_alloc_div(bmap);
9920 if (k < 0)
9921 goto error;
9922 isl_int_set_si(bmap->div[k][0], 0);
9924 for (i = 0; i < eq->n_row; ++i) {
9925 l = isl_basic_map_alloc_equality(bmap);
9926 if (l < 0)
9927 goto error;
9928 for (j = 0, pos = 0; j < 5; ++j) {
9929 int off = isl_basic_map_offset(bmap, c[j]);
9930 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9931 isl_int_set(bmap->eq[l][off + k],
9932 eq->row[i][pos]);
9933 ++pos;
9937 for (i = 0; i < ineq->n_row; ++i) {
9938 l = isl_basic_map_alloc_inequality(bmap);
9939 if (l < 0)
9940 goto error;
9941 for (j = 0, pos = 0; j < 5; ++j) {
9942 int off = isl_basic_map_offset(bmap, c[j]);
9943 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9944 isl_int_set(bmap->ineq[l][off + k],
9945 ineq->row[i][pos]);
9946 ++pos;
9951 isl_space_free(dim);
9952 isl_mat_free(eq);
9953 isl_mat_free(ineq);
9955 return bmap;
9956 error:
9957 isl_space_free(dim);
9958 isl_mat_free(eq);
9959 isl_mat_free(ineq);
9960 return NULL;
9963 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9964 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9965 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9967 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9968 c1, c2, c3, c4, isl_dim_in);
9971 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9972 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9973 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9975 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9976 c1, c2, c3, c4, isl_dim_in);
9979 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9980 __isl_take isl_space *dim,
9981 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9982 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9984 return (isl_basic_set*)
9985 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9986 c1, c2, c3, c4, isl_dim_in);
9989 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9991 if (!bmap)
9992 return -1;
9994 return isl_space_can_zip(bmap->dim);
9997 int isl_map_can_zip(__isl_keep isl_map *map)
9999 if (!map)
10000 return -1;
10002 return isl_space_can_zip(map->dim);
10005 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10006 * (A -> C) -> (B -> D).
10008 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10010 unsigned pos;
10011 unsigned n1;
10012 unsigned n2;
10014 if (!bmap)
10015 return NULL;
10017 if (!isl_basic_map_can_zip(bmap))
10018 isl_die(bmap->ctx, isl_error_invalid,
10019 "basic map cannot be zipped", goto error);
10020 pos = isl_basic_map_offset(bmap, isl_dim_in) +
10021 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10022 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10023 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10024 bmap = isl_basic_map_cow(bmap);
10025 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10026 if (!bmap)
10027 return NULL;
10028 bmap->dim = isl_space_zip(bmap->dim);
10029 if (!bmap->dim)
10030 goto error;
10031 return bmap;
10032 error:
10033 isl_basic_map_free(bmap);
10034 return NULL;
10037 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10038 * (A -> C) -> (B -> D).
10040 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
10042 int i;
10044 if (!map)
10045 return NULL;
10047 if (!isl_map_can_zip(map))
10048 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
10049 goto error);
10051 map = isl_map_cow(map);
10052 if (!map)
10053 return NULL;
10055 for (i = 0; i < map->n; ++i) {
10056 map->p[i] = isl_basic_map_zip(map->p[i]);
10057 if (!map->p[i])
10058 goto error;
10061 map->dim = isl_space_zip(map->dim);
10062 if (!map->dim)
10063 goto error;
10065 return map;
10066 error:
10067 isl_map_free(map);
10068 return NULL;
10071 /* Can we apply isl_basic_map_curry to "bmap"?
10072 * That is, does it have a nested relation in its domain?
10074 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
10076 if (!bmap)
10077 return -1;
10079 return isl_space_can_curry(bmap->dim);
10082 /* Can we apply isl_map_curry to "map"?
10083 * That is, does it have a nested relation in its domain?
10085 int isl_map_can_curry(__isl_keep isl_map *map)
10087 if (!map)
10088 return -1;
10090 return isl_space_can_curry(map->dim);
10093 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10094 * A -> (B -> C).
10096 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
10099 if (!bmap)
10100 return NULL;
10102 if (!isl_basic_map_can_curry(bmap))
10103 isl_die(bmap->ctx, isl_error_invalid,
10104 "basic map cannot be curried", goto error);
10105 bmap->dim = isl_space_curry(bmap->dim);
10106 if (!bmap->dim)
10107 goto error;
10108 return bmap;
10109 error:
10110 isl_basic_map_free(bmap);
10111 return NULL;
10114 /* Given a map (A -> B) -> C, return the corresponding map
10115 * A -> (B -> C).
10117 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
10119 int i;
10121 if (!map)
10122 return NULL;
10124 if (!isl_map_can_curry(map))
10125 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
10126 goto error);
10128 map = isl_map_cow(map);
10129 if (!map)
10130 return NULL;
10132 for (i = 0; i < map->n; ++i) {
10133 map->p[i] = isl_basic_map_curry(map->p[i]);
10134 if (!map->p[i])
10135 goto error;
10138 map->dim = isl_space_curry(map->dim);
10139 if (!map->dim)
10140 goto error;
10142 return map;
10143 error:
10144 isl_map_free(map);
10145 return NULL;
10148 /* Construct a basic map mapping the domain of the affine expression
10149 * to a one-dimensional range prescribed by the affine expression.
10151 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
10153 int k;
10154 int pos;
10155 isl_local_space *ls;
10156 isl_basic_map *bmap;
10158 if (!aff)
10159 return NULL;
10161 ls = isl_aff_get_local_space(aff);
10162 bmap = isl_basic_map_from_local_space(ls);
10163 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
10164 k = isl_basic_map_alloc_equality(bmap);
10165 if (k < 0)
10166 goto error;
10168 pos = isl_basic_map_offset(bmap, isl_dim_out);
10169 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
10170 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
10171 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
10172 aff->v->size - (pos + 1));
10174 isl_aff_free(aff);
10175 bmap = isl_basic_map_finalize(bmap);
10176 return bmap;
10177 error:
10178 isl_aff_free(aff);
10179 isl_basic_map_free(bmap);
10180 return NULL;
10183 /* Construct a map mapping the domain of the affine expression
10184 * to a one-dimensional range prescribed by the affine expression.
10186 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
10188 isl_basic_map *bmap;
10190 bmap = isl_basic_map_from_aff(aff);
10191 return isl_map_from_basic_map(bmap);
10194 /* Construct a basic map mapping the domain the multi-affine expression
10195 * to its range, with each dimension in the range equated to the
10196 * corresponding affine expression.
10198 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
10199 __isl_take isl_multi_aff *maff)
10201 int i;
10202 isl_space *space;
10203 isl_basic_map *bmap;
10205 if (!maff)
10206 return NULL;
10208 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
10209 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
10210 "invalid space", return isl_multi_aff_free(maff));
10212 space = isl_space_domain(isl_multi_aff_get_space(maff));
10213 bmap = isl_basic_map_universe(isl_space_from_domain(space));
10215 for (i = 0; i < maff->n; ++i) {
10216 isl_aff *aff;
10217 isl_basic_map *bmap_i;
10219 aff = isl_aff_copy(maff->p[i]);
10220 bmap_i = isl_basic_map_from_aff(aff);
10222 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10225 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
10227 isl_multi_aff_free(maff);
10228 return bmap;
10231 /* Construct a map mapping the domain the multi-affine expression
10232 * to its range, with each dimension in the range equated to the
10233 * corresponding affine expression.
10235 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
10237 isl_basic_map *bmap;
10239 bmap = isl_basic_map_from_multi_aff(maff);
10240 return isl_map_from_basic_map(bmap);
10243 /* Construct a basic map mapping a domain in the given space to
10244 * to an n-dimensional range, with n the number of elements in the list,
10245 * where each coordinate in the range is prescribed by the
10246 * corresponding affine expression.
10247 * The domains of all affine expressions in the list are assumed to match
10248 * domain_dim.
10250 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
10251 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
10253 int i;
10254 isl_space *dim;
10255 isl_basic_map *bmap;
10257 if (!list)
10258 return NULL;
10260 dim = isl_space_from_domain(domain_dim);
10261 bmap = isl_basic_map_universe(dim);
10263 for (i = 0; i < list->n; ++i) {
10264 isl_aff *aff;
10265 isl_basic_map *bmap_i;
10267 aff = isl_aff_copy(list->p[i]);
10268 bmap_i = isl_basic_map_from_aff(aff);
10270 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10273 isl_aff_list_free(list);
10274 return bmap;
10277 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
10278 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10280 return isl_map_equate(set, type1, pos1, type2, pos2);
10283 /* Construct a basic map where the given dimensions are equal to each other.
10285 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
10286 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10288 isl_basic_map *bmap = NULL;
10289 int i;
10291 if (!space)
10292 return NULL;
10294 if (pos1 >= isl_space_dim(space, type1))
10295 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10296 "index out of bounds", goto error);
10297 if (pos2 >= isl_space_dim(space, type2))
10298 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10299 "index out of bounds", goto error);
10301 if (type1 == type2 && pos1 == pos2)
10302 return isl_basic_map_universe(space);
10304 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
10305 i = isl_basic_map_alloc_equality(bmap);
10306 if (i < 0)
10307 goto error;
10308 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10309 pos1 += isl_basic_map_offset(bmap, type1);
10310 pos2 += isl_basic_map_offset(bmap, type2);
10311 isl_int_set_si(bmap->eq[i][pos1], -1);
10312 isl_int_set_si(bmap->eq[i][pos2], 1);
10313 bmap = isl_basic_map_finalize(bmap);
10314 isl_space_free(space);
10315 return bmap;
10316 error:
10317 isl_space_free(space);
10318 isl_basic_map_free(bmap);
10319 return NULL;
10322 /* Add a constraint imposing that the given two dimensions are equal.
10324 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
10325 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10327 isl_basic_map *eq;
10329 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
10331 bmap = isl_basic_map_intersect(bmap, eq);
10333 return bmap;
10336 /* Add a constraint imposing that the given two dimensions are equal.
10338 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
10339 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10341 isl_basic_map *bmap;
10343 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
10345 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10347 return map;
10350 /* Add a constraint imposing that the given two dimensions have opposite values.
10352 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
10353 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10355 isl_basic_map *bmap = NULL;
10356 int i;
10358 if (!map)
10359 return NULL;
10361 if (pos1 >= isl_map_dim(map, type1))
10362 isl_die(map->ctx, isl_error_invalid,
10363 "index out of bounds", goto error);
10364 if (pos2 >= isl_map_dim(map, type2))
10365 isl_die(map->ctx, isl_error_invalid,
10366 "index out of bounds", goto error);
10368 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
10369 i = isl_basic_map_alloc_equality(bmap);
10370 if (i < 0)
10371 goto error;
10372 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10373 pos1 += isl_basic_map_offset(bmap, type1);
10374 pos2 += isl_basic_map_offset(bmap, type2);
10375 isl_int_set_si(bmap->eq[i][pos1], 1);
10376 isl_int_set_si(bmap->eq[i][pos2], 1);
10377 bmap = isl_basic_map_finalize(bmap);
10379 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10381 return map;
10382 error:
10383 isl_basic_map_free(bmap);
10384 isl_map_free(map);
10385 return NULL;
10388 /* Add a constraint imposing that the value of the first dimension is
10389 * greater than that of the second.
10391 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
10392 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10394 isl_basic_map *bmap = NULL;
10395 int i;
10397 if (!map)
10398 return NULL;
10400 if (pos1 >= isl_map_dim(map, type1))
10401 isl_die(map->ctx, isl_error_invalid,
10402 "index out of bounds", goto error);
10403 if (pos2 >= isl_map_dim(map, type2))
10404 isl_die(map->ctx, isl_error_invalid,
10405 "index out of bounds", goto error);
10407 if (type1 == type2 && pos1 == pos2) {
10408 isl_space *space = isl_map_get_space(map);
10409 isl_map_free(map);
10410 return isl_map_empty(space);
10413 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 0, 1);
10414 i = isl_basic_map_alloc_inequality(bmap);
10415 if (i < 0)
10416 goto error;
10417 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
10418 pos1 += isl_basic_map_offset(bmap, type1);
10419 pos2 += isl_basic_map_offset(bmap, type2);
10420 isl_int_set_si(bmap->ineq[i][pos1], 1);
10421 isl_int_set_si(bmap->ineq[i][pos2], -1);
10422 isl_int_set_si(bmap->ineq[i][0], -1);
10423 bmap = isl_basic_map_finalize(bmap);
10425 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10427 return map;
10428 error:
10429 isl_basic_map_free(bmap);
10430 isl_map_free(map);
10431 return NULL;
10434 /* Add a constraint imposing that the value of the first dimension is
10435 * smaller than that of the second.
10437 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
10438 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10440 return isl_map_order_gt(map, type2, pos2, type1, pos1);
10443 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
10444 int pos)
10446 isl_aff *div;
10447 isl_local_space *ls;
10449 if (!bmap)
10450 return NULL;
10452 if (!isl_basic_map_divs_known(bmap))
10453 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
10454 "some divs are unknown", return NULL);
10456 ls = isl_basic_map_get_local_space(bmap);
10457 div = isl_local_space_get_div(ls, pos);
10458 isl_local_space_free(ls);
10460 return div;
10463 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
10464 int pos)
10466 return isl_basic_map_get_div(bset, pos);
10469 /* Plug in "subs" for dimension "type", "pos" of "bset".
10471 * Let i be the dimension to replace and let "subs" be of the form
10473 * f/d
10475 * Any integer division with a non-zero coefficient for i,
10477 * floor((a i + g)/m)
10479 * is replaced by
10481 * floor((a f + d g)/(m d))
10483 * Constraints of the form
10485 * a i + g
10487 * are replaced by
10489 * a f + d g
10491 __isl_give isl_basic_set *isl_basic_set_substitute(
10492 __isl_take isl_basic_set *bset,
10493 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10495 int i;
10496 isl_int v;
10497 isl_ctx *ctx;
10499 if (bset && isl_basic_set_plain_is_empty(bset))
10500 return bset;
10502 bset = isl_basic_set_cow(bset);
10503 if (!bset || !subs)
10504 goto error;
10506 ctx = isl_basic_set_get_ctx(bset);
10507 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
10508 isl_die(ctx, isl_error_invalid,
10509 "spaces don't match", goto error);
10510 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
10511 isl_die(ctx, isl_error_unsupported,
10512 "cannot handle divs yet", goto error);
10514 pos += isl_basic_set_offset(bset, type);
10516 isl_int_init(v);
10518 for (i = 0; i < bset->n_eq; ++i) {
10519 if (isl_int_is_zero(bset->eq[i][pos]))
10520 continue;
10521 isl_int_set(v, bset->eq[i][pos]);
10522 isl_int_set_si(bset->eq[i][pos], 0);
10523 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
10524 v, subs->v->el + 1, subs->v->size - 1);
10527 for (i = 0; i < bset->n_ineq; ++i) {
10528 if (isl_int_is_zero(bset->ineq[i][pos]))
10529 continue;
10530 isl_int_set(v, bset->ineq[i][pos]);
10531 isl_int_set_si(bset->ineq[i][pos], 0);
10532 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
10533 v, subs->v->el + 1, subs->v->size - 1);
10536 for (i = 0; i < bset->n_div; ++i) {
10537 if (isl_int_is_zero(bset->div[i][1 + pos]))
10538 continue;
10539 isl_int_set(v, bset->div[i][1 + pos]);
10540 isl_int_set_si(bset->div[i][1 + pos], 0);
10541 isl_seq_combine(bset->div[i] + 1,
10542 subs->v->el[0], bset->div[i] + 1,
10543 v, subs->v->el + 1, subs->v->size - 1);
10544 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
10547 isl_int_clear(v);
10549 bset = isl_basic_set_simplify(bset);
10550 return isl_basic_set_finalize(bset);
10551 error:
10552 isl_basic_set_free(bset);
10553 return NULL;
10556 /* Plug in "subs" for dimension "type", "pos" of "set".
10558 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
10559 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10561 int i;
10563 if (set && isl_set_plain_is_empty(set))
10564 return set;
10566 set = isl_set_cow(set);
10567 if (!set || !subs)
10568 goto error;
10570 for (i = set->n - 1; i >= 0; --i) {
10571 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
10572 if (remove_if_empty(set, i) < 0)
10573 goto error;
10576 return set;
10577 error:
10578 isl_set_free(set);
10579 return NULL;