add isl_local_space_set_tuple_id
[isl.git] / isl_map.c
blob7e68a653bf3eab056acdf57cdb1d674d711e746c
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <string.h>
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include <isl_blk.h>
19 #include "isl_space_private.h"
20 #include "isl_equalities.h"
21 #include <isl_lp_private.h>
22 #include <isl_seq.h>
23 #include <isl/set.h>
24 #include <isl/map.h>
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
27 #include "isl_tab.h"
28 #include <isl/vec.h>
29 #include <isl_mat_private.h>
30 #include <isl_vec_private.h>
31 #include <isl_dim_map.h>
32 #include <isl_local_space_private.h>
33 #include <isl_aff_private.h>
34 #include <isl_options_private.h>
35 #include <isl_morph.h>
36 #include <isl_val_private.h>
37 #include <isl/deprecated/map_int.h>
38 #include <isl/deprecated/set_int.h>
40 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
42 switch (type) {
43 case isl_dim_param: return dim->nparam;
44 case isl_dim_in: return dim->n_in;
45 case isl_dim_out: return dim->n_out;
46 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
47 default: return 0;
51 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
53 switch (type) {
54 case isl_dim_param: return 1;
55 case isl_dim_in: return 1 + dim->nparam;
56 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
57 default: return 0;
61 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
62 enum isl_dim_type type)
64 if (!bmap)
65 return 0;
66 switch (type) {
67 case isl_dim_cst: return 1;
68 case isl_dim_param:
69 case isl_dim_in:
70 case isl_dim_out: return isl_space_dim(bmap->dim, type);
71 case isl_dim_div: return bmap->n_div;
72 case isl_dim_all: return isl_basic_map_total_dim(bmap);
73 default: return 0;
77 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
79 return map ? n(map->dim, type) : 0;
82 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
84 return set ? n(set->dim, type) : 0;
87 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
88 enum isl_dim_type type)
90 isl_space *dim = bmap->dim;
91 switch (type) {
92 case isl_dim_cst: return 0;
93 case isl_dim_param: return 1;
94 case isl_dim_in: return 1 + dim->nparam;
95 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
96 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
97 default: return 0;
101 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
102 enum isl_dim_type type)
104 return isl_basic_map_offset(bset, type);
107 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
109 return pos(map->dim, type);
112 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
113 enum isl_dim_type type)
115 return isl_basic_map_dim(bset, type);
118 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
120 return isl_basic_set_dim(bset, isl_dim_set);
123 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
125 return isl_basic_set_dim(bset, isl_dim_param);
128 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
130 if (!bset)
131 return 0;
132 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
135 unsigned isl_set_n_dim(__isl_keep isl_set *set)
137 return isl_set_dim(set, isl_dim_set);
140 unsigned isl_set_n_param(__isl_keep isl_set *set)
142 return isl_set_dim(set, isl_dim_param);
145 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
147 return bmap ? bmap->dim->n_in : 0;
150 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
152 return bmap ? bmap->dim->n_out : 0;
155 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
157 return bmap ? bmap->dim->nparam : 0;
160 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
162 return bmap ? bmap->n_div : 0;
165 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
167 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
170 unsigned isl_map_n_in(const struct isl_map *map)
172 return map ? map->dim->n_in : 0;
175 unsigned isl_map_n_out(const struct isl_map *map)
177 return map ? map->dim->n_out : 0;
180 unsigned isl_map_n_param(const struct isl_map *map)
182 return map ? map->dim->nparam : 0;
185 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
187 int m;
188 if (!map || !set)
189 return -1;
190 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
191 if (m < 0 || !m)
192 return m;
193 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
196 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
197 struct isl_basic_set *bset)
199 int m;
200 if (!bmap || !bset)
201 return -1;
202 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
203 if (m < 0 || !m)
204 return m;
205 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
208 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
210 int m;
211 if (!map || !set)
212 return -1;
213 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
214 if (m < 0 || !m)
215 return m;
216 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
219 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
220 struct isl_basic_set *bset)
222 int m;
223 if (!bmap || !bset)
224 return -1;
225 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
226 if (m < 0 || !m)
227 return m;
228 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
231 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
233 return bmap ? bmap->ctx : NULL;
236 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
238 return bset ? bset->ctx : NULL;
241 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
243 return map ? map->ctx : NULL;
246 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
248 return set ? set->ctx : NULL;
251 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
253 if (!bmap)
254 return NULL;
255 return isl_space_copy(bmap->dim);
258 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
260 if (!bset)
261 return NULL;
262 return isl_space_copy(bset->dim);
265 /* Extract the divs in "bmap" as a matrix.
267 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
269 int i;
270 isl_ctx *ctx;
271 isl_mat *div;
272 unsigned total;
273 unsigned cols;
275 if (!bmap)
276 return NULL;
278 ctx = isl_basic_map_get_ctx(bmap);
279 total = isl_space_dim(bmap->dim, isl_dim_all);
280 cols = 1 + 1 + total + bmap->n_div;
281 div = isl_mat_alloc(ctx, bmap->n_div, cols);
282 if (!div)
283 return NULL;
285 for (i = 0; i < bmap->n_div; ++i)
286 isl_seq_cpy(div->row[i], bmap->div[i], cols);
288 return div;
291 /* Extract the divs in "bset" as a matrix.
293 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
295 return isl_basic_map_get_divs(bset);
298 __isl_give isl_local_space *isl_basic_map_get_local_space(
299 __isl_keep isl_basic_map *bmap)
301 isl_mat *div;
303 if (!bmap)
304 return NULL;
306 div = isl_basic_map_get_divs(bmap);
307 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
310 __isl_give isl_local_space *isl_basic_set_get_local_space(
311 __isl_keep isl_basic_set *bset)
313 return isl_basic_map_get_local_space(bset);
316 __isl_give isl_basic_map *isl_basic_map_from_local_space(
317 __isl_take isl_local_space *ls)
319 int i;
320 int n_div;
321 isl_basic_map *bmap;
323 if (!ls)
324 return NULL;
326 n_div = isl_local_space_dim(ls, isl_dim_div);
327 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
328 n_div, 0, 2 * n_div);
330 for (i = 0; i < n_div; ++i)
331 if (isl_basic_map_alloc_div(bmap) < 0)
332 goto error;
334 for (i = 0; i < n_div; ++i) {
335 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
336 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
337 goto error;
340 isl_local_space_free(ls);
341 return bmap;
342 error:
343 isl_local_space_free(ls);
344 isl_basic_map_free(bmap);
345 return NULL;
348 __isl_give isl_basic_set *isl_basic_set_from_local_space(
349 __isl_take isl_local_space *ls)
351 return isl_basic_map_from_local_space(ls);
354 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
356 if (!map)
357 return NULL;
358 return isl_space_copy(map->dim);
361 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
363 if (!set)
364 return NULL;
365 return isl_space_copy(set->dim);
368 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
369 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
371 bmap = isl_basic_map_cow(bmap);
372 if (!bmap)
373 return NULL;
374 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
375 if (!bmap->dim)
376 goto error;
377 bmap = isl_basic_map_finalize(bmap);
378 return bmap;
379 error:
380 isl_basic_map_free(bmap);
381 return NULL;
384 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
385 __isl_take isl_basic_set *bset, const char *s)
387 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
390 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
391 enum isl_dim_type type)
393 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
396 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
397 enum isl_dim_type type, const char *s)
399 int i;
401 map = isl_map_cow(map);
402 if (!map)
403 return NULL;
405 map->dim = isl_space_set_tuple_name(map->dim, type, s);
406 if (!map->dim)
407 goto error;
409 for (i = 0; i < map->n; ++i) {
410 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
411 if (!map->p[i])
412 goto error;
415 return map;
416 error:
417 isl_map_free(map);
418 return NULL;
421 /* Replace the identifier of the tuple of type "type" by "id".
423 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
424 __isl_take isl_basic_map *bmap,
425 enum isl_dim_type type, __isl_take isl_id *id)
427 bmap = isl_basic_map_cow(bmap);
428 if (!bmap)
429 return isl_id_free(id);
430 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
431 if (!bmap->dim)
432 return isl_basic_map_free(bmap);
433 bmap = isl_basic_map_finalize(bmap);
434 return bmap;
437 /* Replace the identifier of the tuple by "id".
439 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
440 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
442 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
445 /* Does the input or output tuple have a name?
447 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
449 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
452 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
453 enum isl_dim_type type)
455 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
458 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
459 const char *s)
461 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
464 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
465 enum isl_dim_type type, __isl_take isl_id *id)
467 map = isl_map_cow(map);
468 if (!map)
469 return isl_id_free(id);
471 map->dim = isl_space_set_tuple_id(map->dim, type, id);
473 return isl_map_reset_space(map, isl_space_copy(map->dim));
476 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
477 __isl_take isl_id *id)
479 return isl_map_set_tuple_id(set, isl_dim_set, id);
482 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
483 enum isl_dim_type type)
485 map = isl_map_cow(map);
486 if (!map)
487 return NULL;
489 map->dim = isl_space_reset_tuple_id(map->dim, type);
491 return isl_map_reset_space(map, isl_space_copy(map->dim));
494 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
496 return isl_map_reset_tuple_id(set, isl_dim_set);
499 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
501 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
504 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
505 enum isl_dim_type type)
507 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
510 int isl_set_has_tuple_id(__isl_keep isl_set *set)
512 return isl_map_has_tuple_id(set, isl_dim_set);
515 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
517 return isl_map_get_tuple_id(set, isl_dim_set);
520 /* Does the set tuple have a name?
522 int isl_set_has_tuple_name(__isl_keep isl_set *set)
524 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
528 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
530 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
533 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
535 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
538 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
539 enum isl_dim_type type, unsigned pos)
541 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
544 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
545 enum isl_dim_type type, unsigned pos)
547 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
550 /* Does the given dimension have a name?
552 int isl_map_has_dim_name(__isl_keep isl_map *map,
553 enum isl_dim_type type, unsigned pos)
555 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
558 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
559 enum isl_dim_type type, unsigned pos)
561 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
564 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
565 enum isl_dim_type type, unsigned pos)
567 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
570 /* Does the given dimension have a name?
572 int isl_set_has_dim_name(__isl_keep isl_set *set,
573 enum isl_dim_type type, unsigned pos)
575 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
578 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
579 __isl_take isl_basic_map *bmap,
580 enum isl_dim_type type, unsigned pos, const char *s)
582 bmap = isl_basic_map_cow(bmap);
583 if (!bmap)
584 return NULL;
585 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
586 if (!bmap->dim)
587 goto error;
588 return isl_basic_map_finalize(bmap);
589 error:
590 isl_basic_map_free(bmap);
591 return NULL;
594 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
595 enum isl_dim_type type, unsigned pos, const char *s)
597 int i;
599 map = isl_map_cow(map);
600 if (!map)
601 return NULL;
603 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
604 if (!map->dim)
605 goto error;
607 for (i = 0; i < map->n; ++i) {
608 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
609 if (!map->p[i])
610 goto error;
613 return map;
614 error:
615 isl_map_free(map);
616 return NULL;
619 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
620 __isl_take isl_basic_set *bset,
621 enum isl_dim_type type, unsigned pos, const char *s)
623 return (isl_basic_set *)isl_basic_map_set_dim_name(
624 (isl_basic_map *)bset, type, pos, s);
627 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
628 enum isl_dim_type type, unsigned pos, const char *s)
630 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
633 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
634 enum isl_dim_type type, unsigned pos)
636 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
639 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
640 enum isl_dim_type type, unsigned pos)
642 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
645 int isl_map_has_dim_id(__isl_keep isl_map *map,
646 enum isl_dim_type type, unsigned pos)
648 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
651 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
652 enum isl_dim_type type, unsigned pos)
654 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
657 int isl_set_has_dim_id(__isl_keep isl_set *set,
658 enum isl_dim_type type, unsigned pos)
660 return isl_map_has_dim_id(set, type, pos);
663 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
664 enum isl_dim_type type, unsigned pos)
666 return isl_map_get_dim_id(set, type, pos);
669 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
670 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
672 map = isl_map_cow(map);
673 if (!map)
674 return isl_id_free(id);
676 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
678 return isl_map_reset_space(map, isl_space_copy(map->dim));
681 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
682 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
684 return isl_map_set_dim_id(set, type, pos, id);
687 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
688 __isl_keep isl_id *id)
690 if (!map)
691 return -1;
692 return isl_space_find_dim_by_id(map->dim, type, id);
695 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
696 __isl_keep isl_id *id)
698 return isl_map_find_dim_by_id(set, type, id);
701 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
702 const char *name)
704 if (!map)
705 return -1;
706 return isl_space_find_dim_by_name(map->dim, type, name);
709 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
710 const char *name)
712 return isl_map_find_dim_by_name(set, type, name);
715 /* Reset the user pointer on all identifiers of parameters and tuples
716 * of the space of "map".
718 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
720 isl_space *space;
722 space = isl_map_get_space(map);
723 space = isl_space_reset_user(space);
724 map = isl_map_reset_space(map, space);
726 return map;
729 /* Reset the user pointer on all identifiers of parameters and tuples
730 * of the space of "set".
732 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
734 return isl_map_reset_user(set);
737 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
739 if (!bmap)
740 return -1;
741 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
744 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
746 return isl_basic_map_is_rational(bset);
749 /* Does "bmap" contain any rational points?
751 * If "bmap" has an equality for each dimension, equating the dimension
752 * to an integer constant, then it has no rational points, even if it
753 * is marked as rational.
755 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
757 int has_rational = 1;
758 unsigned total;
760 if (!bmap)
761 return -1;
762 if (isl_basic_map_plain_is_empty(bmap))
763 return 0;
764 if (!isl_basic_map_is_rational(bmap))
765 return 0;
766 bmap = isl_basic_map_copy(bmap);
767 bmap = isl_basic_map_implicit_equalities(bmap);
768 if (!bmap)
769 return -1;
770 total = isl_basic_map_total_dim(bmap);
771 if (bmap->n_eq == total) {
772 int i, j;
773 for (i = 0; i < bmap->n_eq; ++i) {
774 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
775 if (j < 0)
776 break;
777 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
778 !isl_int_is_negone(bmap->eq[i][1 + j]))
779 break;
780 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
781 total - j - 1);
782 if (j >= 0)
783 break;
785 if (i == bmap->n_eq)
786 has_rational = 0;
788 isl_basic_map_free(bmap);
790 return has_rational;
793 /* Does "map" contain any rational points?
795 int isl_map_has_rational(__isl_keep isl_map *map)
797 int i;
798 int has_rational;
800 if (!map)
801 return -1;
802 for (i = 0; i < map->n; ++i) {
803 has_rational = isl_basic_map_has_rational(map->p[i]);
804 if (has_rational < 0)
805 return -1;
806 if (has_rational)
807 return 1;
809 return 0;
812 /* Does "set" contain any rational points?
814 int isl_set_has_rational(__isl_keep isl_set *set)
816 return isl_map_has_rational(set);
819 /* Is this basic set a parameter domain?
821 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
823 if (!bset)
824 return -1;
825 return isl_space_is_params(bset->dim);
828 /* Is this set a parameter domain?
830 int isl_set_is_params(__isl_keep isl_set *set)
832 if (!set)
833 return -1;
834 return isl_space_is_params(set->dim);
837 /* Is this map actually a parameter domain?
838 * Users should never call this function. Outside of isl,
839 * a map can never be a parameter domain.
841 int isl_map_is_params(__isl_keep isl_map *map)
843 if (!map)
844 return -1;
845 return isl_space_is_params(map->dim);
848 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
849 struct isl_basic_map *bmap, unsigned extra,
850 unsigned n_eq, unsigned n_ineq)
852 int i;
853 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
855 bmap->ctx = ctx;
856 isl_ctx_ref(ctx);
858 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
859 if (isl_blk_is_error(bmap->block))
860 goto error;
862 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
863 if ((n_ineq + n_eq) && !bmap->ineq)
864 goto error;
866 if (extra == 0) {
867 bmap->block2 = isl_blk_empty();
868 bmap->div = NULL;
869 } else {
870 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
871 if (isl_blk_is_error(bmap->block2))
872 goto error;
874 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
875 if (!bmap->div)
876 goto error;
879 for (i = 0; i < n_ineq + n_eq; ++i)
880 bmap->ineq[i] = bmap->block.data + i * row_size;
882 for (i = 0; i < extra; ++i)
883 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
885 bmap->ref = 1;
886 bmap->flags = 0;
887 bmap->c_size = n_eq + n_ineq;
888 bmap->eq = bmap->ineq + n_ineq;
889 bmap->extra = extra;
890 bmap->n_eq = 0;
891 bmap->n_ineq = 0;
892 bmap->n_div = 0;
893 bmap->sample = NULL;
895 return bmap;
896 error:
897 isl_basic_map_free(bmap);
898 return NULL;
901 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
902 unsigned nparam, unsigned dim, unsigned extra,
903 unsigned n_eq, unsigned n_ineq)
905 struct isl_basic_map *bmap;
906 isl_space *space;
908 space = isl_space_set_alloc(ctx, nparam, dim);
909 if (!space)
910 return NULL;
912 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
913 return (struct isl_basic_set *)bmap;
916 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
917 unsigned extra, unsigned n_eq, unsigned n_ineq)
919 struct isl_basic_map *bmap;
920 if (!dim)
921 return NULL;
922 isl_assert(dim->ctx, dim->n_in == 0, goto error);
923 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
924 return (struct isl_basic_set *)bmap;
925 error:
926 isl_space_free(dim);
927 return NULL;
930 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
931 unsigned extra, unsigned n_eq, unsigned n_ineq)
933 struct isl_basic_map *bmap;
935 if (!dim)
936 return NULL;
937 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
938 if (!bmap)
939 goto error;
940 bmap->dim = dim;
942 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
943 error:
944 isl_space_free(dim);
945 return NULL;
948 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
949 unsigned nparam, unsigned in, unsigned out, unsigned extra,
950 unsigned n_eq, unsigned n_ineq)
952 struct isl_basic_map *bmap;
953 isl_space *dim;
955 dim = isl_space_alloc(ctx, nparam, in, out);
956 if (!dim)
957 return NULL;
959 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
960 return bmap;
963 static void dup_constraints(
964 struct isl_basic_map *dst, struct isl_basic_map *src)
966 int i;
967 unsigned total = isl_basic_map_total_dim(src);
969 for (i = 0; i < src->n_eq; ++i) {
970 int j = isl_basic_map_alloc_equality(dst);
971 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
974 for (i = 0; i < src->n_ineq; ++i) {
975 int j = isl_basic_map_alloc_inequality(dst);
976 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
979 for (i = 0; i < src->n_div; ++i) {
980 int j = isl_basic_map_alloc_div(dst);
981 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
983 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
986 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
988 struct isl_basic_map *dup;
990 if (!bmap)
991 return NULL;
992 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
993 bmap->n_div, bmap->n_eq, bmap->n_ineq);
994 if (!dup)
995 return NULL;
996 dup_constraints(dup, bmap);
997 dup->flags = bmap->flags;
998 dup->sample = isl_vec_copy(bmap->sample);
999 return dup;
1002 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1004 struct isl_basic_map *dup;
1006 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1007 return (struct isl_basic_set *)dup;
1010 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1012 if (!bset)
1013 return NULL;
1015 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1016 bset->ref++;
1017 return bset;
1019 return isl_basic_set_dup(bset);
1022 struct isl_set *isl_set_copy(struct isl_set *set)
1024 if (!set)
1025 return NULL;
1027 set->ref++;
1028 return set;
1031 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1033 if (!bmap)
1034 return NULL;
1036 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1037 bmap->ref++;
1038 return bmap;
1040 bmap = isl_basic_map_dup(bmap);
1041 if (bmap)
1042 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1043 return bmap;
1046 struct isl_map *isl_map_copy(struct isl_map *map)
1048 if (!map)
1049 return NULL;
1051 map->ref++;
1052 return map;
1055 void *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1057 if (!bmap)
1058 return NULL;
1060 if (--bmap->ref > 0)
1061 return NULL;
1063 isl_ctx_deref(bmap->ctx);
1064 free(bmap->div);
1065 isl_blk_free(bmap->ctx, bmap->block2);
1066 free(bmap->ineq);
1067 isl_blk_free(bmap->ctx, bmap->block);
1068 isl_vec_free(bmap->sample);
1069 isl_space_free(bmap->dim);
1070 free(bmap);
1072 return NULL;
1075 void *isl_basic_set_free(struct isl_basic_set *bset)
1077 return isl_basic_map_free((struct isl_basic_map *)bset);
1080 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1082 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1085 __isl_give isl_map *isl_map_align_params_map_map_and(
1086 __isl_take isl_map *map1, __isl_take isl_map *map2,
1087 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1088 __isl_take isl_map *map2))
1090 if (!map1 || !map2)
1091 goto error;
1092 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1093 return fn(map1, map2);
1094 if (!isl_space_has_named_params(map1->dim) ||
1095 !isl_space_has_named_params(map2->dim))
1096 isl_die(map1->ctx, isl_error_invalid,
1097 "unaligned unnamed parameters", goto error);
1098 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1099 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1100 return fn(map1, map2);
1101 error:
1102 isl_map_free(map1);
1103 isl_map_free(map2);
1104 return NULL;
1107 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1108 __isl_keep isl_map *map2,
1109 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1111 int r;
1113 if (!map1 || !map2)
1114 return -1;
1115 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1116 return fn(map1, map2);
1117 if (!isl_space_has_named_params(map1->dim) ||
1118 !isl_space_has_named_params(map2->dim))
1119 isl_die(map1->ctx, isl_error_invalid,
1120 "unaligned unnamed parameters", return -1);
1121 map1 = isl_map_copy(map1);
1122 map2 = isl_map_copy(map2);
1123 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1124 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1125 r = fn(map1, map2);
1126 isl_map_free(map1);
1127 isl_map_free(map2);
1128 return r;
1131 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1133 struct isl_ctx *ctx;
1134 if (!bmap)
1135 return -1;
1136 ctx = bmap->ctx;
1137 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1138 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1139 return -1);
1140 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1141 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1142 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1143 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1144 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1145 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1146 isl_int *t;
1147 int j = isl_basic_map_alloc_inequality(bmap);
1148 if (j < 0)
1149 return -1;
1150 t = bmap->ineq[j];
1151 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1152 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1153 bmap->eq[-1] = t;
1154 bmap->n_eq++;
1155 bmap->n_ineq--;
1156 bmap->eq--;
1157 return 0;
1159 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1160 bmap->extra - bmap->n_div);
1161 return bmap->n_eq++;
1164 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1166 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1169 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1171 if (!bmap)
1172 return -1;
1173 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1174 bmap->n_eq -= n;
1175 return 0;
1178 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1180 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1183 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1185 isl_int *t;
1186 if (!bmap)
1187 return -1;
1188 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1190 if (pos != bmap->n_eq - 1) {
1191 t = bmap->eq[pos];
1192 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1193 bmap->eq[bmap->n_eq - 1] = t;
1195 bmap->n_eq--;
1196 return 0;
1199 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1201 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1204 /* Turn inequality "pos" of "bmap" into an equality.
1206 * In particular, we move the inequality in front of the equalities
1207 * and move the last inequality in the position of the moved inequality.
1208 * Note that isl_tab_make_equalities_explicit depends on this particular
1209 * change in the ordering of the constraints.
1211 void isl_basic_map_inequality_to_equality(
1212 struct isl_basic_map *bmap, unsigned pos)
1214 isl_int *t;
1216 t = bmap->ineq[pos];
1217 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1218 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1219 bmap->eq[-1] = t;
1220 bmap->n_eq++;
1221 bmap->n_ineq--;
1222 bmap->eq--;
1223 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1224 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1225 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1226 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1229 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1231 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1234 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1236 struct isl_ctx *ctx;
1237 if (!bmap)
1238 return -1;
1239 ctx = bmap->ctx;
1240 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1241 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1242 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1243 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1244 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1245 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1246 1 + isl_basic_map_total_dim(bmap),
1247 bmap->extra - bmap->n_div);
1248 return bmap->n_ineq++;
1251 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1253 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1256 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1258 if (!bmap)
1259 return -1;
1260 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1261 bmap->n_ineq -= n;
1262 return 0;
1265 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1267 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1270 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1272 isl_int *t;
1273 if (!bmap)
1274 return -1;
1275 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1277 if (pos != bmap->n_ineq - 1) {
1278 t = bmap->ineq[pos];
1279 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1280 bmap->ineq[bmap->n_ineq - 1] = t;
1281 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1283 bmap->n_ineq--;
1284 return 0;
1287 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1289 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1292 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1293 isl_int *eq)
1295 int k;
1297 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1298 if (!bmap)
1299 return NULL;
1300 k = isl_basic_map_alloc_equality(bmap);
1301 if (k < 0)
1302 goto error;
1303 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1304 return bmap;
1305 error:
1306 isl_basic_map_free(bmap);
1307 return NULL;
1310 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1311 isl_int *eq)
1313 return (isl_basic_set *)
1314 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1317 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1318 isl_int *ineq)
1320 int k;
1322 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1323 if (!bmap)
1324 return NULL;
1325 k = isl_basic_map_alloc_inequality(bmap);
1326 if (k < 0)
1327 goto error;
1328 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1329 return bmap;
1330 error:
1331 isl_basic_map_free(bmap);
1332 return NULL;
1335 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1336 isl_int *ineq)
1338 return (isl_basic_set *)
1339 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1342 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1344 if (!bmap)
1345 return -1;
1346 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1347 isl_seq_clr(bmap->div[bmap->n_div] +
1348 1 + 1 + isl_basic_map_total_dim(bmap),
1349 bmap->extra - bmap->n_div);
1350 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1351 return bmap->n_div++;
1354 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1356 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1359 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1361 if (!bmap)
1362 return -1;
1363 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1364 bmap->n_div -= n;
1365 return 0;
1368 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1370 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1373 /* Copy constraint from src to dst, putting the vars of src at offset
1374 * dim_off in dst and the divs of src at offset div_off in dst.
1375 * If both sets are actually map, then dim_off applies to the input
1376 * variables.
1378 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1379 struct isl_basic_map *src_map, isl_int *src,
1380 unsigned in_off, unsigned out_off, unsigned div_off)
1382 unsigned src_nparam = isl_basic_map_n_param(src_map);
1383 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1384 unsigned src_in = isl_basic_map_n_in(src_map);
1385 unsigned dst_in = isl_basic_map_n_in(dst_map);
1386 unsigned src_out = isl_basic_map_n_out(src_map);
1387 unsigned dst_out = isl_basic_map_n_out(dst_map);
1388 isl_int_set(dst[0], src[0]);
1389 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1390 if (dst_nparam > src_nparam)
1391 isl_seq_clr(dst+1+src_nparam,
1392 dst_nparam - src_nparam);
1393 isl_seq_clr(dst+1+dst_nparam, in_off);
1394 isl_seq_cpy(dst+1+dst_nparam+in_off,
1395 src+1+src_nparam,
1396 isl_min(dst_in-in_off, src_in));
1397 if (dst_in-in_off > src_in)
1398 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1399 dst_in - in_off - src_in);
1400 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1401 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1402 src+1+src_nparam+src_in,
1403 isl_min(dst_out-out_off, src_out));
1404 if (dst_out-out_off > src_out)
1405 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1406 dst_out - out_off - src_out);
1407 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1408 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1409 src+1+src_nparam+src_in+src_out,
1410 isl_min(dst_map->extra-div_off, src_map->n_div));
1411 if (dst_map->n_div-div_off > src_map->n_div)
1412 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1413 div_off+src_map->n_div,
1414 dst_map->n_div - div_off - src_map->n_div);
1417 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1418 struct isl_basic_map *src_map, isl_int *src,
1419 unsigned in_off, unsigned out_off, unsigned div_off)
1421 isl_int_set(dst[0], src[0]);
1422 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1425 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1426 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1428 int i;
1429 unsigned div_off;
1431 if (!bmap1 || !bmap2)
1432 goto error;
1434 div_off = bmap1->n_div;
1436 for (i = 0; i < bmap2->n_eq; ++i) {
1437 int i1 = isl_basic_map_alloc_equality(bmap1);
1438 if (i1 < 0)
1439 goto error;
1440 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1441 i_pos, o_pos, div_off);
1444 for (i = 0; i < bmap2->n_ineq; ++i) {
1445 int i1 = isl_basic_map_alloc_inequality(bmap1);
1446 if (i1 < 0)
1447 goto error;
1448 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1449 i_pos, o_pos, div_off);
1452 for (i = 0; i < bmap2->n_div; ++i) {
1453 int i1 = isl_basic_map_alloc_div(bmap1);
1454 if (i1 < 0)
1455 goto error;
1456 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1457 i_pos, o_pos, div_off);
1460 isl_basic_map_free(bmap2);
1462 return bmap1;
1464 error:
1465 isl_basic_map_free(bmap1);
1466 isl_basic_map_free(bmap2);
1467 return NULL;
1470 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1471 struct isl_basic_set *bset2, unsigned pos)
1473 return (struct isl_basic_set *)
1474 add_constraints((struct isl_basic_map *)bset1,
1475 (struct isl_basic_map *)bset2, 0, pos);
1478 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1479 __isl_take isl_space *dim, unsigned extra,
1480 unsigned n_eq, unsigned n_ineq)
1482 struct isl_basic_map *ext;
1483 unsigned flags;
1484 int dims_ok;
1486 if (!dim)
1487 goto error;
1489 if (!base)
1490 goto error;
1492 dims_ok = isl_space_is_equal(base->dim, dim) &&
1493 base->extra >= base->n_div + extra;
1495 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1496 room_for_ineq(base, n_ineq)) {
1497 isl_space_free(dim);
1498 return base;
1501 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1502 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1503 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1504 extra += base->extra;
1505 n_eq += base->n_eq;
1506 n_ineq += base->n_ineq;
1508 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1509 dim = NULL;
1510 if (!ext)
1511 goto error;
1513 if (dims_ok)
1514 ext->sample = isl_vec_copy(base->sample);
1515 flags = base->flags;
1516 ext = add_constraints(ext, base, 0, 0);
1517 if (ext) {
1518 ext->flags = flags;
1519 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1522 return ext;
1524 error:
1525 isl_space_free(dim);
1526 isl_basic_map_free(base);
1527 return NULL;
1530 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1531 __isl_take isl_space *dim, unsigned extra,
1532 unsigned n_eq, unsigned n_ineq)
1534 return (struct isl_basic_set *)
1535 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1536 extra, n_eq, n_ineq);
1539 struct isl_basic_map *isl_basic_map_extend_constraints(
1540 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1542 if (!base)
1543 return NULL;
1544 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1545 0, n_eq, n_ineq);
1548 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1549 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1550 unsigned n_eq, unsigned n_ineq)
1552 struct isl_basic_map *bmap;
1553 isl_space *dim;
1555 if (!base)
1556 return NULL;
1557 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1558 if (!dim)
1559 goto error;
1561 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1562 return bmap;
1563 error:
1564 isl_basic_map_free(base);
1565 return NULL;
1568 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1569 unsigned nparam, unsigned dim, unsigned extra,
1570 unsigned n_eq, unsigned n_ineq)
1572 return (struct isl_basic_set *)
1573 isl_basic_map_extend((struct isl_basic_map *)base,
1574 nparam, 0, dim, extra, n_eq, n_ineq);
1577 struct isl_basic_set *isl_basic_set_extend_constraints(
1578 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1580 return (struct isl_basic_set *)
1581 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1582 n_eq, n_ineq);
1585 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1587 return (struct isl_basic_set *)
1588 isl_basic_map_cow((struct isl_basic_map *)bset);
1591 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1593 if (!bmap)
1594 return NULL;
1596 if (bmap->ref > 1) {
1597 bmap->ref--;
1598 bmap = isl_basic_map_dup(bmap);
1600 if (bmap)
1601 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1602 return bmap;
1605 struct isl_set *isl_set_cow(struct isl_set *set)
1607 if (!set)
1608 return NULL;
1610 if (set->ref == 1)
1611 return set;
1612 set->ref--;
1613 return isl_set_dup(set);
1616 struct isl_map *isl_map_cow(struct isl_map *map)
1618 if (!map)
1619 return NULL;
1621 if (map->ref == 1)
1622 return map;
1623 map->ref--;
1624 return isl_map_dup(map);
1627 static void swap_vars(struct isl_blk blk, isl_int *a,
1628 unsigned a_len, unsigned b_len)
1630 isl_seq_cpy(blk.data, a+a_len, b_len);
1631 isl_seq_cpy(blk.data+b_len, a, a_len);
1632 isl_seq_cpy(a, blk.data, b_len+a_len);
1635 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1636 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1638 int i;
1639 struct isl_blk blk;
1641 if (!bmap)
1642 goto error;
1644 isl_assert(bmap->ctx,
1645 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1647 if (n1 == 0 || n2 == 0)
1648 return bmap;
1650 bmap = isl_basic_map_cow(bmap);
1651 if (!bmap)
1652 return NULL;
1654 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1655 if (isl_blk_is_error(blk))
1656 goto error;
1658 for (i = 0; i < bmap->n_eq; ++i)
1659 swap_vars(blk,
1660 bmap->eq[i] + pos, n1, n2);
1662 for (i = 0; i < bmap->n_ineq; ++i)
1663 swap_vars(blk,
1664 bmap->ineq[i] + pos, n1, n2);
1666 for (i = 0; i < bmap->n_div; ++i)
1667 swap_vars(blk,
1668 bmap->div[i]+1 + pos, n1, n2);
1670 isl_blk_free(bmap->ctx, blk);
1672 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1673 bmap = isl_basic_map_gauss(bmap, NULL);
1674 return isl_basic_map_finalize(bmap);
1675 error:
1676 isl_basic_map_free(bmap);
1677 return NULL;
1680 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1681 __isl_take isl_basic_set *bset, unsigned n)
1683 unsigned dim;
1684 unsigned nparam;
1686 if (!bset)
1687 return NULL;
1689 nparam = isl_basic_set_n_param(bset);
1690 dim = isl_basic_set_n_dim(bset);
1691 isl_assert(bset->ctx, n <= dim, goto error);
1693 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1694 error:
1695 isl_basic_set_free(bset);
1696 return NULL;
1699 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1701 int i = 0;
1702 unsigned total;
1703 if (!bmap)
1704 goto error;
1705 total = isl_basic_map_total_dim(bmap);
1706 isl_basic_map_free_div(bmap, bmap->n_div);
1707 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1708 if (bmap->n_eq > 0)
1709 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1710 else {
1711 i = isl_basic_map_alloc_equality(bmap);
1712 if (i < 0)
1713 goto error;
1715 isl_int_set_si(bmap->eq[i][0], 1);
1716 isl_seq_clr(bmap->eq[i]+1, total);
1717 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1718 isl_vec_free(bmap->sample);
1719 bmap->sample = NULL;
1720 return isl_basic_map_finalize(bmap);
1721 error:
1722 isl_basic_map_free(bmap);
1723 return NULL;
1726 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1728 return (struct isl_basic_set *)
1729 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1732 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1733 * of "bmap").
1735 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1737 isl_int *t = bmap->div[a];
1738 bmap->div[a] = bmap->div[b];
1739 bmap->div[b] = t;
1742 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1743 * div definitions accordingly.
1745 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1747 int i;
1748 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1750 swap_div(bmap, a, b);
1752 for (i = 0; i < bmap->n_eq; ++i)
1753 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1755 for (i = 0; i < bmap->n_ineq; ++i)
1756 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1758 for (i = 0; i < bmap->n_div; ++i)
1759 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1760 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1763 /* Eliminate the specified n dimensions starting at first from the
1764 * constraints, without removing the dimensions from the space.
1765 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1767 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1768 enum isl_dim_type type, unsigned first, unsigned n)
1770 int i;
1772 if (!map)
1773 return NULL;
1774 if (n == 0)
1775 return map;
1777 if (first + n > isl_map_dim(map, type) || first + n < first)
1778 isl_die(map->ctx, isl_error_invalid,
1779 "index out of bounds", goto error);
1781 map = isl_map_cow(map);
1782 if (!map)
1783 return NULL;
1785 for (i = 0; i < map->n; ++i) {
1786 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1787 if (!map->p[i])
1788 goto error;
1790 return map;
1791 error:
1792 isl_map_free(map);
1793 return NULL;
1796 /* Eliminate the specified n dimensions starting at first from the
1797 * constraints, without removing the dimensions from the space.
1798 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1800 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1801 enum isl_dim_type type, unsigned first, unsigned n)
1803 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1806 /* Eliminate the specified n dimensions starting at first from the
1807 * constraints, without removing the dimensions from the space.
1808 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1810 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1811 unsigned first, unsigned n)
1813 return isl_set_eliminate(set, isl_dim_set, first, n);
1816 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1817 __isl_take isl_basic_map *bmap)
1819 if (!bmap)
1820 return NULL;
1821 bmap = isl_basic_map_eliminate_vars(bmap,
1822 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1823 if (!bmap)
1824 return NULL;
1825 bmap->n_div = 0;
1826 return isl_basic_map_finalize(bmap);
1829 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1830 __isl_take isl_basic_set *bset)
1832 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1833 (struct isl_basic_map *)bset);
1836 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1838 int i;
1840 if (!map)
1841 return NULL;
1842 if (map->n == 0)
1843 return map;
1845 map = isl_map_cow(map);
1846 if (!map)
1847 return NULL;
1849 for (i = 0; i < map->n; ++i) {
1850 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1851 if (!map->p[i])
1852 goto error;
1854 return map;
1855 error:
1856 isl_map_free(map);
1857 return NULL;
1860 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1862 return isl_map_remove_divs(set);
1865 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1866 enum isl_dim_type type, unsigned first, unsigned n)
1868 if (!bmap)
1869 return NULL;
1870 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1871 goto error);
1872 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1873 return bmap;
1874 bmap = isl_basic_map_eliminate_vars(bmap,
1875 isl_basic_map_offset(bmap, type) - 1 + first, n);
1876 if (!bmap)
1877 return bmap;
1878 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1879 return bmap;
1880 bmap = isl_basic_map_drop(bmap, type, first, n);
1881 return bmap;
1882 error:
1883 isl_basic_map_free(bmap);
1884 return NULL;
1887 /* Return true if the definition of the given div (recursively) involves
1888 * any of the given variables.
1890 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1891 unsigned first, unsigned n)
1893 int i;
1894 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1896 if (isl_int_is_zero(bmap->div[div][0]))
1897 return 0;
1898 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1899 return 1;
1901 for (i = bmap->n_div - 1; i >= 0; --i) {
1902 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1903 continue;
1904 if (div_involves_vars(bmap, i, first, n))
1905 return 1;
1908 return 0;
1911 /* Try and add a lower and/or upper bound on "div" to "bmap"
1912 * based on inequality "i".
1913 * "total" is the total number of variables (excluding the divs).
1914 * "v" is a temporary object that can be used during the calculations.
1915 * If "lb" is set, then a lower bound should be constructed.
1916 * If "ub" is set, then an upper bound should be constructed.
1918 * The calling function has already checked that the inequality does not
1919 * reference "div", but we still need to check that the inequality is
1920 * of the right form. We'll consider the case where we want to construct
1921 * a lower bound. The construction of upper bounds is similar.
1923 * Let "div" be of the form
1925 * q = floor((a + f(x))/d)
1927 * We essentially check if constraint "i" is of the form
1929 * b + f(x) >= 0
1931 * so that we can use it to derive a lower bound on "div".
1932 * However, we allow a slightly more general form
1934 * b + g(x) >= 0
1936 * with the condition that the coefficients of g(x) - f(x) are all
1937 * divisible by d.
1938 * Rewriting this constraint as
1940 * 0 >= -b - g(x)
1942 * adding a + f(x) to both sides and dividing by d, we obtain
1944 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1946 * Taking the floor on both sides, we obtain
1948 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1950 * or
1952 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1954 * In the case of an upper bound, we construct the constraint
1956 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1959 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1960 __isl_take isl_basic_map *bmap, int div, int i,
1961 unsigned total, isl_int v, int lb, int ub)
1963 int j;
1965 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1966 if (lb) {
1967 isl_int_sub(v, bmap->ineq[i][1 + j],
1968 bmap->div[div][1 + 1 + j]);
1969 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1971 if (ub) {
1972 isl_int_add(v, bmap->ineq[i][1 + j],
1973 bmap->div[div][1 + 1 + j]);
1974 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1977 if (!lb && !ub)
1978 return bmap;
1980 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1981 if (lb) {
1982 int k = isl_basic_map_alloc_inequality(bmap);
1983 if (k < 0)
1984 goto error;
1985 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1986 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
1987 bmap->div[div][1 + j]);
1988 isl_int_cdiv_q(bmap->ineq[k][j],
1989 bmap->ineq[k][j], bmap->div[div][0]);
1991 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
1993 if (ub) {
1994 int k = isl_basic_map_alloc_inequality(bmap);
1995 if (k < 0)
1996 goto error;
1997 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1998 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
1999 bmap->div[div][1 + j]);
2000 isl_int_fdiv_q(bmap->ineq[k][j],
2001 bmap->ineq[k][j], bmap->div[div][0]);
2003 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2006 return bmap;
2007 error:
2008 isl_basic_map_free(bmap);
2009 return NULL;
2012 /* This function is called right before "div" is eliminated from "bmap"
2013 * using Fourier-Motzkin.
2014 * Look through the constraints of "bmap" for constraints on the argument
2015 * of the integer division and use them to construct constraints on the
2016 * integer division itself. These constraints can then be combined
2017 * during the Fourier-Motzkin elimination.
2018 * Note that it is only useful to introduce lower bounds on "div"
2019 * if "bmap" already contains upper bounds on "div" as the newly
2020 * introduce lower bounds can then be combined with the pre-existing
2021 * upper bounds. Similarly for upper bounds.
2022 * We therefore first check if "bmap" contains any lower and/or upper bounds
2023 * on "div".
2025 * It is interesting to note that the introduction of these constraints
2026 * can indeed lead to more accurate results, even when compared to
2027 * deriving constraints on the argument of "div" from constraints on "div".
2028 * Consider, for example, the set
2030 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2032 * The second constraint can be rewritten as
2034 * 2 * [(-i-2j+3)/4] + k >= 0
2036 * from which we can derive
2038 * -i - 2j + 3 >= -2k
2040 * or
2042 * i + 2j <= 3 + 2k
2044 * Combined with the first constraint, we obtain
2046 * -3 <= 3 + 2k or k >= -3
2048 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2049 * the first constraint, we obtain
2051 * [(i + 2j)/4] >= [-3/4] = -1
2053 * Combining this constraint with the second constraint, we obtain
2055 * k >= -2
2057 static __isl_give isl_basic_map *insert_bounds_on_div(
2058 __isl_take isl_basic_map *bmap, int div)
2060 int i;
2061 int check_lb, check_ub;
2062 isl_int v;
2063 unsigned total;
2065 if (!bmap)
2066 return NULL;
2068 if (isl_int_is_zero(bmap->div[div][0]))
2069 return bmap;
2071 total = isl_space_dim(bmap->dim, isl_dim_all);
2073 check_lb = 0;
2074 check_ub = 0;
2075 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2076 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2077 if (s > 0)
2078 check_ub = 1;
2079 if (s < 0)
2080 check_lb = 1;
2083 if (!check_lb && !check_ub)
2084 return bmap;
2086 isl_int_init(v);
2088 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2089 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2090 continue;
2092 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2093 check_lb, check_ub);
2096 isl_int_clear(v);
2098 return bmap;
2101 /* Remove all divs (recursively) involving any of the given dimensions
2102 * in their definitions.
2104 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2105 __isl_take isl_basic_map *bmap,
2106 enum isl_dim_type type, unsigned first, unsigned n)
2108 int i;
2110 if (!bmap)
2111 return NULL;
2112 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2113 goto error);
2114 first += isl_basic_map_offset(bmap, type);
2116 for (i = bmap->n_div - 1; i >= 0; --i) {
2117 if (!div_involves_vars(bmap, i, first, n))
2118 continue;
2119 bmap = insert_bounds_on_div(bmap, i);
2120 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2121 if (!bmap)
2122 return NULL;
2123 i = bmap->n_div;
2126 return bmap;
2127 error:
2128 isl_basic_map_free(bmap);
2129 return NULL;
2132 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2133 __isl_take isl_basic_set *bset,
2134 enum isl_dim_type type, unsigned first, unsigned n)
2136 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2139 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2140 enum isl_dim_type type, unsigned first, unsigned n)
2142 int i;
2144 if (!map)
2145 return NULL;
2146 if (map->n == 0)
2147 return map;
2149 map = isl_map_cow(map);
2150 if (!map)
2151 return NULL;
2153 for (i = 0; i < map->n; ++i) {
2154 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2155 type, first, n);
2156 if (!map->p[i])
2157 goto error;
2159 return map;
2160 error:
2161 isl_map_free(map);
2162 return NULL;
2165 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2166 enum isl_dim_type type, unsigned first, unsigned n)
2168 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2169 type, first, n);
2172 /* Does the desciption of "bmap" depend on the specified dimensions?
2173 * We also check whether the dimensions appear in any of the div definitions.
2174 * In principle there is no need for this check. If the dimensions appear
2175 * in a div definition, they also appear in the defining constraints of that
2176 * div.
2178 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2179 enum isl_dim_type type, unsigned first, unsigned n)
2181 int i;
2183 if (!bmap)
2184 return -1;
2186 if (first + n > isl_basic_map_dim(bmap, type))
2187 isl_die(bmap->ctx, isl_error_invalid,
2188 "index out of bounds", return -1);
2190 first += isl_basic_map_offset(bmap, type);
2191 for (i = 0; i < bmap->n_eq; ++i)
2192 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2193 return 1;
2194 for (i = 0; i < bmap->n_ineq; ++i)
2195 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2196 return 1;
2197 for (i = 0; i < bmap->n_div; ++i) {
2198 if (isl_int_is_zero(bmap->div[i][0]))
2199 continue;
2200 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2201 return 1;
2204 return 0;
2207 int isl_map_involves_dims(__isl_keep isl_map *map,
2208 enum isl_dim_type type, unsigned first, unsigned n)
2210 int i;
2212 if (!map)
2213 return -1;
2215 if (first + n > isl_map_dim(map, type))
2216 isl_die(map->ctx, isl_error_invalid,
2217 "index out of bounds", return -1);
2219 for (i = 0; i < map->n; ++i) {
2220 int involves = isl_basic_map_involves_dims(map->p[i],
2221 type, first, n);
2222 if (involves < 0 || involves)
2223 return involves;
2226 return 0;
2229 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2230 enum isl_dim_type type, unsigned first, unsigned n)
2232 return isl_basic_map_involves_dims(bset, type, first, n);
2235 int isl_set_involves_dims(__isl_keep isl_set *set,
2236 enum isl_dim_type type, unsigned first, unsigned n)
2238 return isl_map_involves_dims(set, type, first, n);
2241 /* Return true if the definition of the given div is unknown or depends
2242 * on unknown divs.
2244 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2246 int i;
2247 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2249 if (isl_int_is_zero(bmap->div[div][0]))
2250 return 1;
2252 for (i = bmap->n_div - 1; i >= 0; --i) {
2253 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2254 continue;
2255 if (div_is_unknown(bmap, i))
2256 return 1;
2259 return 0;
2262 /* Remove all divs that are unknown or defined in terms of unknown divs.
2264 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2265 __isl_take isl_basic_map *bmap)
2267 int i;
2269 if (!bmap)
2270 return NULL;
2272 for (i = bmap->n_div - 1; i >= 0; --i) {
2273 if (!div_is_unknown(bmap, i))
2274 continue;
2275 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2276 if (!bmap)
2277 return NULL;
2278 i = bmap->n_div;
2281 return bmap;
2284 /* Remove all divs that are unknown or defined in terms of unknown divs.
2286 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2287 __isl_take isl_basic_set *bset)
2289 return isl_basic_map_remove_unknown_divs(bset);
2292 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2294 int i;
2296 if (!map)
2297 return NULL;
2298 if (map->n == 0)
2299 return map;
2301 map = isl_map_cow(map);
2302 if (!map)
2303 return NULL;
2305 for (i = 0; i < map->n; ++i) {
2306 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2307 if (!map->p[i])
2308 goto error;
2310 return map;
2311 error:
2312 isl_map_free(map);
2313 return NULL;
2316 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2318 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2321 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2322 __isl_take isl_basic_set *bset,
2323 enum isl_dim_type type, unsigned first, unsigned n)
2325 return (isl_basic_set *)
2326 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2329 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2330 enum isl_dim_type type, unsigned first, unsigned n)
2332 int i;
2334 if (n == 0)
2335 return map;
2337 map = isl_map_cow(map);
2338 if (!map)
2339 return NULL;
2340 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2342 for (i = 0; i < map->n; ++i) {
2343 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2344 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2345 if (!map->p[i])
2346 goto error;
2348 map = isl_map_drop(map, type, first, n);
2349 return map;
2350 error:
2351 isl_map_free(map);
2352 return NULL;
2355 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2356 enum isl_dim_type type, unsigned first, unsigned n)
2358 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2361 /* Project out n inputs starting at first using Fourier-Motzkin */
2362 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2363 unsigned first, unsigned n)
2365 return isl_map_remove_dims(map, isl_dim_in, first, n);
2368 static void dump_term(struct isl_basic_map *bmap,
2369 isl_int c, int pos, FILE *out)
2371 const char *name;
2372 unsigned in = isl_basic_map_n_in(bmap);
2373 unsigned dim = in + isl_basic_map_n_out(bmap);
2374 unsigned nparam = isl_basic_map_n_param(bmap);
2375 if (!pos)
2376 isl_int_print(out, c, 0);
2377 else {
2378 if (!isl_int_is_one(c))
2379 isl_int_print(out, c, 0);
2380 if (pos < 1 + nparam) {
2381 name = isl_space_get_dim_name(bmap->dim,
2382 isl_dim_param, pos - 1);
2383 if (name)
2384 fprintf(out, "%s", name);
2385 else
2386 fprintf(out, "p%d", pos - 1);
2387 } else if (pos < 1 + nparam + in)
2388 fprintf(out, "i%d", pos - 1 - nparam);
2389 else if (pos < 1 + nparam + dim)
2390 fprintf(out, "o%d", pos - 1 - nparam - in);
2391 else
2392 fprintf(out, "e%d", pos - 1 - nparam - dim);
2396 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2397 int sign, FILE *out)
2399 int i;
2400 int first;
2401 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2402 isl_int v;
2404 isl_int_init(v);
2405 for (i = 0, first = 1; i < len; ++i) {
2406 if (isl_int_sgn(c[i]) * sign <= 0)
2407 continue;
2408 if (!first)
2409 fprintf(out, " + ");
2410 first = 0;
2411 isl_int_abs(v, c[i]);
2412 dump_term(bmap, v, i, out);
2414 isl_int_clear(v);
2415 if (first)
2416 fprintf(out, "0");
2419 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2420 const char *op, FILE *out, int indent)
2422 int i;
2424 fprintf(out, "%*s", indent, "");
2426 dump_constraint_sign(bmap, c, 1, out);
2427 fprintf(out, " %s ", op);
2428 dump_constraint_sign(bmap, c, -1, out);
2430 fprintf(out, "\n");
2432 for (i = bmap->n_div; i < bmap->extra; ++i) {
2433 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2434 continue;
2435 fprintf(out, "%*s", indent, "");
2436 fprintf(out, "ERROR: unused div coefficient not zero\n");
2437 abort();
2441 static void dump_constraints(struct isl_basic_map *bmap,
2442 isl_int **c, unsigned n,
2443 const char *op, FILE *out, int indent)
2445 int i;
2447 for (i = 0; i < n; ++i)
2448 dump_constraint(bmap, c[i], op, out, indent);
2451 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2453 int j;
2454 int first = 1;
2455 unsigned total = isl_basic_map_total_dim(bmap);
2457 for (j = 0; j < 1 + total; ++j) {
2458 if (isl_int_is_zero(exp[j]))
2459 continue;
2460 if (!first && isl_int_is_pos(exp[j]))
2461 fprintf(out, "+");
2462 dump_term(bmap, exp[j], j, out);
2463 first = 0;
2467 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2469 int i;
2471 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2472 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2474 for (i = 0; i < bmap->n_div; ++i) {
2475 fprintf(out, "%*s", indent, "");
2476 fprintf(out, "e%d = [(", i);
2477 dump_affine(bmap, bmap->div[i]+1, out);
2478 fprintf(out, ")/");
2479 isl_int_print(out, bmap->div[i][0], 0);
2480 fprintf(out, "]\n");
2484 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2485 FILE *out, int indent)
2487 if (!bset) {
2488 fprintf(out, "null basic set\n");
2489 return;
2492 fprintf(out, "%*s", indent, "");
2493 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2494 bset->ref, bset->dim->nparam, bset->dim->n_out,
2495 bset->extra, bset->flags);
2496 dump((struct isl_basic_map *)bset, out, indent);
2499 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2500 FILE *out, int indent)
2502 if (!bmap) {
2503 fprintf(out, "null basic map\n");
2504 return;
2507 fprintf(out, "%*s", indent, "");
2508 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2509 "flags: %x, n_name: %d\n",
2510 bmap->ref,
2511 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2512 bmap->extra, bmap->flags, bmap->dim->n_id);
2513 dump(bmap, out, indent);
2516 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2518 unsigned total;
2519 if (!bmap)
2520 return -1;
2521 total = isl_basic_map_total_dim(bmap);
2522 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2523 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2524 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2525 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2526 return 0;
2529 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2530 unsigned flags)
2532 struct isl_set *set;
2534 if (!dim)
2535 return NULL;
2536 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2537 isl_assert(dim->ctx, n >= 0, goto error);
2538 set = isl_alloc(dim->ctx, struct isl_set,
2539 sizeof(struct isl_set) +
2540 (n - 1) * sizeof(struct isl_basic_set *));
2541 if (!set)
2542 goto error;
2544 set->ctx = dim->ctx;
2545 isl_ctx_ref(set->ctx);
2546 set->ref = 1;
2547 set->size = n;
2548 set->n = 0;
2549 set->dim = dim;
2550 set->flags = flags;
2551 return set;
2552 error:
2553 isl_space_free(dim);
2554 return NULL;
2557 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2558 unsigned nparam, unsigned dim, int n, unsigned flags)
2560 struct isl_set *set;
2561 isl_space *dims;
2563 dims = isl_space_alloc(ctx, nparam, 0, dim);
2564 if (!dims)
2565 return NULL;
2567 set = isl_set_alloc_space(dims, n, flags);
2568 return set;
2571 /* Make sure "map" has room for at least "n" more basic maps.
2573 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2575 int i;
2576 struct isl_map *grown = NULL;
2578 if (!map)
2579 return NULL;
2580 isl_assert(map->ctx, n >= 0, goto error);
2581 if (map->n + n <= map->size)
2582 return map;
2583 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2584 if (!grown)
2585 goto error;
2586 for (i = 0; i < map->n; ++i) {
2587 grown->p[i] = isl_basic_map_copy(map->p[i]);
2588 if (!grown->p[i])
2589 goto error;
2590 grown->n++;
2592 isl_map_free(map);
2593 return grown;
2594 error:
2595 isl_map_free(grown);
2596 isl_map_free(map);
2597 return NULL;
2600 /* Make sure "set" has room for at least "n" more basic sets.
2602 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2604 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2607 struct isl_set *isl_set_dup(struct isl_set *set)
2609 int i;
2610 struct isl_set *dup;
2612 if (!set)
2613 return NULL;
2615 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2616 if (!dup)
2617 return NULL;
2618 for (i = 0; i < set->n; ++i)
2619 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2620 return dup;
2623 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2625 return isl_map_from_basic_map(bset);
2628 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2630 struct isl_map *map;
2632 if (!bmap)
2633 return NULL;
2635 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2636 return isl_map_add_basic_map(map, bmap);
2639 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2640 __isl_take isl_basic_set *bset)
2642 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2643 (struct isl_basic_map *)bset);
2646 void *isl_set_free(__isl_take isl_set *set)
2648 int i;
2650 if (!set)
2651 return NULL;
2653 if (--set->ref > 0)
2654 return NULL;
2656 isl_ctx_deref(set->ctx);
2657 for (i = 0; i < set->n; ++i)
2658 isl_basic_set_free(set->p[i]);
2659 isl_space_free(set->dim);
2660 free(set);
2662 return NULL;
2665 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2667 int i;
2669 if (!set) {
2670 fprintf(out, "null set\n");
2671 return;
2674 fprintf(out, "%*s", indent, "");
2675 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2676 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2677 set->flags);
2678 for (i = 0; i < set->n; ++i) {
2679 fprintf(out, "%*s", indent, "");
2680 fprintf(out, "basic set %d:\n", i);
2681 isl_basic_set_print_internal(set->p[i], out, indent+4);
2685 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2687 int i;
2689 if (!map) {
2690 fprintf(out, "null map\n");
2691 return;
2694 fprintf(out, "%*s", indent, "");
2695 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2696 "flags: %x, n_name: %d\n",
2697 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2698 map->dim->n_out, map->flags, map->dim->n_id);
2699 for (i = 0; i < map->n; ++i) {
2700 fprintf(out, "%*s", indent, "");
2701 fprintf(out, "basic map %d:\n", i);
2702 isl_basic_map_print_internal(map->p[i], out, indent+4);
2706 struct isl_basic_map *isl_basic_map_intersect_domain(
2707 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2709 struct isl_basic_map *bmap_domain;
2711 if (!bmap || !bset)
2712 goto error;
2714 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2715 bset->dim, isl_dim_param), goto error);
2717 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2718 isl_assert(bset->ctx,
2719 isl_basic_map_compatible_domain(bmap, bset), goto error);
2721 bmap = isl_basic_map_cow(bmap);
2722 if (!bmap)
2723 goto error;
2724 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2725 bset->n_div, bset->n_eq, bset->n_ineq);
2726 bmap_domain = isl_basic_map_from_domain(bset);
2727 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2729 bmap = isl_basic_map_simplify(bmap);
2730 return isl_basic_map_finalize(bmap);
2731 error:
2732 isl_basic_map_free(bmap);
2733 isl_basic_set_free(bset);
2734 return NULL;
2737 struct isl_basic_map *isl_basic_map_intersect_range(
2738 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2740 struct isl_basic_map *bmap_range;
2742 if (!bmap || !bset)
2743 goto error;
2745 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2746 bset->dim, isl_dim_param), goto error);
2748 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2749 isl_assert(bset->ctx,
2750 isl_basic_map_compatible_range(bmap, bset), goto error);
2752 if (isl_basic_set_is_universe(bset)) {
2753 isl_basic_set_free(bset);
2754 return bmap;
2757 bmap = isl_basic_map_cow(bmap);
2758 if (!bmap)
2759 goto error;
2760 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2761 bset->n_div, bset->n_eq, bset->n_ineq);
2762 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2763 bmap = add_constraints(bmap, bmap_range, 0, 0);
2765 bmap = isl_basic_map_simplify(bmap);
2766 return isl_basic_map_finalize(bmap);
2767 error:
2768 isl_basic_map_free(bmap);
2769 isl_basic_set_free(bset);
2770 return NULL;
2773 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2775 int i;
2776 unsigned total;
2777 isl_int s;
2779 if (!bmap || !vec)
2780 return -1;
2782 total = 1 + isl_basic_map_total_dim(bmap);
2783 if (total != vec->size)
2784 return -1;
2786 isl_int_init(s);
2788 for (i = 0; i < bmap->n_eq; ++i) {
2789 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2790 if (!isl_int_is_zero(s)) {
2791 isl_int_clear(s);
2792 return 0;
2796 for (i = 0; i < bmap->n_ineq; ++i) {
2797 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2798 if (isl_int_is_neg(s)) {
2799 isl_int_clear(s);
2800 return 0;
2804 isl_int_clear(s);
2806 return 1;
2809 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2811 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2814 struct isl_basic_map *isl_basic_map_intersect(
2815 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2817 struct isl_vec *sample = NULL;
2819 if (!bmap1 || !bmap2)
2820 goto error;
2822 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2823 bmap2->dim, isl_dim_param), goto error);
2824 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2825 isl_space_dim(bmap1->dim, isl_dim_param) &&
2826 isl_space_dim(bmap2->dim, isl_dim_all) !=
2827 isl_space_dim(bmap2->dim, isl_dim_param))
2828 return isl_basic_map_intersect(bmap2, bmap1);
2830 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2831 isl_space_dim(bmap2->dim, isl_dim_param))
2832 isl_assert(bmap1->ctx,
2833 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2835 if (bmap1->sample &&
2836 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2837 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2838 sample = isl_vec_copy(bmap1->sample);
2839 else if (bmap2->sample &&
2840 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2841 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2842 sample = isl_vec_copy(bmap2->sample);
2844 bmap1 = isl_basic_map_cow(bmap1);
2845 if (!bmap1)
2846 goto error;
2847 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2848 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2849 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2851 if (!bmap1)
2852 isl_vec_free(sample);
2853 else if (sample) {
2854 isl_vec_free(bmap1->sample);
2855 bmap1->sample = sample;
2858 bmap1 = isl_basic_map_simplify(bmap1);
2859 return isl_basic_map_finalize(bmap1);
2860 error:
2861 if (sample)
2862 isl_vec_free(sample);
2863 isl_basic_map_free(bmap1);
2864 isl_basic_map_free(bmap2);
2865 return NULL;
2868 struct isl_basic_set *isl_basic_set_intersect(
2869 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2871 return (struct isl_basic_set *)
2872 isl_basic_map_intersect(
2873 (struct isl_basic_map *)bset1,
2874 (struct isl_basic_map *)bset2);
2877 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2878 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2880 return isl_basic_set_intersect(bset1, bset2);
2883 /* Special case of isl_map_intersect, where both map1 and map2
2884 * are convex, without any divs and such that either map1 or map2
2885 * contains a single constraint. This constraint is then simply
2886 * added to the other map.
2888 static __isl_give isl_map *map_intersect_add_constraint(
2889 __isl_take isl_map *map1, __isl_take isl_map *map2)
2891 isl_assert(map1->ctx, map1->n == 1, goto error);
2892 isl_assert(map2->ctx, map1->n == 1, goto error);
2893 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2894 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2896 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2897 return isl_map_intersect(map2, map1);
2899 isl_assert(map2->ctx,
2900 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2902 map1 = isl_map_cow(map1);
2903 if (!map1)
2904 goto error;
2905 if (isl_map_plain_is_empty(map1)) {
2906 isl_map_free(map2);
2907 return map1;
2909 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2910 if (map2->p[0]->n_eq == 1)
2911 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2912 else
2913 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2914 map2->p[0]->ineq[0]);
2916 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2917 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2918 if (!map1->p[0])
2919 goto error;
2921 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2922 isl_basic_map_free(map1->p[0]);
2923 map1->n = 0;
2926 isl_map_free(map2);
2928 return map1;
2929 error:
2930 isl_map_free(map1);
2931 isl_map_free(map2);
2932 return NULL;
2935 /* map2 may be either a parameter domain or a map living in the same
2936 * space as map1.
2938 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2939 __isl_take isl_map *map2)
2941 unsigned flags = 0;
2942 isl_map *result;
2943 int i, j;
2945 if (!map1 || !map2)
2946 goto error;
2948 if ((isl_map_plain_is_empty(map1) ||
2949 isl_map_plain_is_universe(map2)) &&
2950 isl_space_is_equal(map1->dim, map2->dim)) {
2951 isl_map_free(map2);
2952 return map1;
2954 if ((isl_map_plain_is_empty(map2) ||
2955 isl_map_plain_is_universe(map1)) &&
2956 isl_space_is_equal(map1->dim, map2->dim)) {
2957 isl_map_free(map1);
2958 return map2;
2961 if (map1->n == 1 && map2->n == 1 &&
2962 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2963 isl_space_is_equal(map1->dim, map2->dim) &&
2964 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2965 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2966 return map_intersect_add_constraint(map1, map2);
2968 if (isl_space_dim(map2->dim, isl_dim_all) !=
2969 isl_space_dim(map2->dim, isl_dim_param))
2970 isl_assert(map1->ctx,
2971 isl_space_is_equal(map1->dim, map2->dim), goto error);
2973 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2974 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2975 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2977 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2978 map1->n * map2->n, flags);
2979 if (!result)
2980 goto error;
2981 for (i = 0; i < map1->n; ++i)
2982 for (j = 0; j < map2->n; ++j) {
2983 struct isl_basic_map *part;
2984 part = isl_basic_map_intersect(
2985 isl_basic_map_copy(map1->p[i]),
2986 isl_basic_map_copy(map2->p[j]));
2987 if (isl_basic_map_is_empty(part) < 0)
2988 part = isl_basic_map_free(part);
2989 result = isl_map_add_basic_map(result, part);
2990 if (!result)
2991 goto error;
2993 isl_map_free(map1);
2994 isl_map_free(map2);
2995 return result;
2996 error:
2997 isl_map_free(map1);
2998 isl_map_free(map2);
2999 return NULL;
3002 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3003 __isl_take isl_map *map2)
3005 if (!map1 || !map2)
3006 goto error;
3007 if (!isl_space_is_equal(map1->dim, map2->dim))
3008 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3009 "spaces don't match", goto error);
3010 return map_intersect_internal(map1, map2);
3011 error:
3012 isl_map_free(map1);
3013 isl_map_free(map2);
3014 return NULL;
3017 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3018 __isl_take isl_map *map2)
3020 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3023 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3025 return (struct isl_set *)
3026 isl_map_intersect((struct isl_map *)set1,
3027 (struct isl_map *)set2);
3030 /* map_intersect_internal accepts intersections
3031 * with parameter domains, so we can just call that function.
3033 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3034 __isl_take isl_set *params)
3036 return map_intersect_internal(map, params);
3039 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3040 __isl_take isl_map *map2)
3042 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3045 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3046 __isl_take isl_set *params)
3048 return isl_map_intersect_params(set, params);
3051 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3053 isl_space *dim;
3054 struct isl_basic_set *bset;
3055 unsigned in;
3057 if (!bmap)
3058 return NULL;
3059 bmap = isl_basic_map_cow(bmap);
3060 if (!bmap)
3061 return NULL;
3062 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3063 in = isl_basic_map_n_in(bmap);
3064 bset = isl_basic_set_from_basic_map(bmap);
3065 bset = isl_basic_set_swap_vars(bset, in);
3066 return isl_basic_map_from_basic_set(bset, dim);
3069 static __isl_give isl_basic_map *basic_map_space_reset(
3070 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3072 isl_space *space;
3074 if (!bmap)
3075 return NULL;
3076 if (!isl_space_is_named_or_nested(bmap->dim, type))
3077 return bmap;
3079 space = isl_basic_map_get_space(bmap);
3080 space = isl_space_reset(space, type);
3081 bmap = isl_basic_map_reset_space(bmap, space);
3082 return bmap;
3085 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3086 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3087 unsigned pos, unsigned n)
3089 isl_space *res_dim;
3090 struct isl_basic_map *res;
3091 struct isl_dim_map *dim_map;
3092 unsigned total, off;
3093 enum isl_dim_type t;
3095 if (n == 0)
3096 return basic_map_space_reset(bmap, type);
3098 if (!bmap)
3099 return NULL;
3101 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3103 total = isl_basic_map_total_dim(bmap) + n;
3104 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3105 off = 0;
3106 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3107 if (t != type) {
3108 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3109 } else {
3110 unsigned size = isl_basic_map_dim(bmap, t);
3111 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3112 0, pos, off);
3113 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3114 pos, size - pos, off + pos + n);
3116 off += isl_space_dim(res_dim, t);
3118 isl_dim_map_div(dim_map, bmap, off);
3120 res = isl_basic_map_alloc_space(res_dim,
3121 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3122 if (isl_basic_map_is_rational(bmap))
3123 res = isl_basic_map_set_rational(res);
3124 if (isl_basic_map_plain_is_empty(bmap)) {
3125 isl_basic_map_free(bmap);
3126 free(dim_map);
3127 return isl_basic_map_set_to_empty(res);
3129 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3130 return isl_basic_map_finalize(res);
3133 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3134 __isl_take isl_basic_set *bset,
3135 enum isl_dim_type type, unsigned pos, unsigned n)
3137 return isl_basic_map_insert_dims(bset, type, pos, n);
3140 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3141 enum isl_dim_type type, unsigned n)
3143 if (!bmap)
3144 return NULL;
3145 return isl_basic_map_insert_dims(bmap, type,
3146 isl_basic_map_dim(bmap, type), n);
3149 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3150 enum isl_dim_type type, unsigned n)
3152 if (!bset)
3153 return NULL;
3154 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3155 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3156 error:
3157 isl_basic_set_free(bset);
3158 return NULL;
3161 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3162 enum isl_dim_type type)
3164 isl_space *space;
3166 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3167 return map;
3169 space = isl_map_get_space(map);
3170 space = isl_space_reset(space, type);
3171 map = isl_map_reset_space(map, space);
3172 return map;
3175 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3176 enum isl_dim_type type, unsigned pos, unsigned n)
3178 int i;
3180 if (n == 0)
3181 return map_space_reset(map, type);
3183 map = isl_map_cow(map);
3184 if (!map)
3185 return NULL;
3187 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3188 if (!map->dim)
3189 goto error;
3191 for (i = 0; i < map->n; ++i) {
3192 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3193 if (!map->p[i])
3194 goto error;
3197 return map;
3198 error:
3199 isl_map_free(map);
3200 return NULL;
3203 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3204 enum isl_dim_type type, unsigned pos, unsigned n)
3206 return isl_map_insert_dims(set, type, pos, n);
3209 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3210 enum isl_dim_type type, unsigned n)
3212 if (!map)
3213 return NULL;
3214 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3217 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3218 enum isl_dim_type type, unsigned n)
3220 if (!set)
3221 return NULL;
3222 isl_assert(set->ctx, type != isl_dim_in, goto error);
3223 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3224 error:
3225 isl_set_free(set);
3226 return NULL;
3229 __isl_give isl_basic_map *isl_basic_map_move_dims(
3230 __isl_take isl_basic_map *bmap,
3231 enum isl_dim_type dst_type, unsigned dst_pos,
3232 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3234 struct isl_dim_map *dim_map;
3235 struct isl_basic_map *res;
3236 enum isl_dim_type t;
3237 unsigned total, off;
3239 if (!bmap)
3240 return NULL;
3241 if (n == 0)
3242 return bmap;
3244 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3245 goto error);
3247 if (dst_type == src_type && dst_pos == src_pos)
3248 return bmap;
3250 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3252 if (pos(bmap->dim, dst_type) + dst_pos ==
3253 pos(bmap->dim, src_type) + src_pos +
3254 ((src_type < dst_type) ? n : 0)) {
3255 bmap = isl_basic_map_cow(bmap);
3256 if (!bmap)
3257 return NULL;
3259 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3260 src_type, src_pos, n);
3261 if (!bmap->dim)
3262 goto error;
3264 bmap = isl_basic_map_finalize(bmap);
3266 return bmap;
3269 total = isl_basic_map_total_dim(bmap);
3270 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3272 off = 0;
3273 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3274 unsigned size = isl_space_dim(bmap->dim, t);
3275 if (t == dst_type) {
3276 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3277 0, dst_pos, off);
3278 off += dst_pos;
3279 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3280 src_pos, n, off);
3281 off += n;
3282 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3283 dst_pos, size - dst_pos, off);
3284 off += size - dst_pos;
3285 } else if (t == src_type) {
3286 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3287 0, src_pos, off);
3288 off += src_pos;
3289 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3290 src_pos + n, size - src_pos - n, off);
3291 off += size - src_pos - n;
3292 } else {
3293 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3294 off += size;
3297 isl_dim_map_div(dim_map, bmap, off);
3299 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3300 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3301 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3302 if (!bmap)
3303 goto error;
3305 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3306 src_type, src_pos, n);
3307 if (!bmap->dim)
3308 goto error;
3310 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3311 bmap = isl_basic_map_gauss(bmap, NULL);
3312 bmap = isl_basic_map_finalize(bmap);
3314 return bmap;
3315 error:
3316 isl_basic_map_free(bmap);
3317 return NULL;
3320 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3321 enum isl_dim_type dst_type, unsigned dst_pos,
3322 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3324 return (isl_basic_set *)isl_basic_map_move_dims(
3325 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3328 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3329 enum isl_dim_type dst_type, unsigned dst_pos,
3330 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3332 if (!set)
3333 return NULL;
3334 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3335 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3336 src_type, src_pos, n);
3337 error:
3338 isl_set_free(set);
3339 return NULL;
3342 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3343 enum isl_dim_type dst_type, unsigned dst_pos,
3344 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3346 int i;
3348 if (!map)
3349 return NULL;
3350 if (n == 0)
3351 return map;
3353 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3354 goto error);
3356 if (dst_type == src_type && dst_pos == src_pos)
3357 return map;
3359 isl_assert(map->ctx, dst_type != src_type, goto error);
3361 map = isl_map_cow(map);
3362 if (!map)
3363 return NULL;
3365 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3366 if (!map->dim)
3367 goto error;
3369 for (i = 0; i < map->n; ++i) {
3370 map->p[i] = isl_basic_map_move_dims(map->p[i],
3371 dst_type, dst_pos,
3372 src_type, src_pos, n);
3373 if (!map->p[i])
3374 goto error;
3377 return map;
3378 error:
3379 isl_map_free(map);
3380 return NULL;
3383 /* Move the specified dimensions to the last columns right before
3384 * the divs. Don't change the dimension specification of bmap.
3385 * That's the responsibility of the caller.
3387 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3388 enum isl_dim_type type, unsigned first, unsigned n)
3390 struct isl_dim_map *dim_map;
3391 struct isl_basic_map *res;
3392 enum isl_dim_type t;
3393 unsigned total, off;
3395 if (!bmap)
3396 return NULL;
3397 if (pos(bmap->dim, type) + first + n ==
3398 1 + isl_space_dim(bmap->dim, isl_dim_all))
3399 return bmap;
3401 total = isl_basic_map_total_dim(bmap);
3402 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3404 off = 0;
3405 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3406 unsigned size = isl_space_dim(bmap->dim, t);
3407 if (t == type) {
3408 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3409 0, first, off);
3410 off += first;
3411 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3412 first, n, total - bmap->n_div - n);
3413 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3414 first + n, size - (first + n), off);
3415 off += size - (first + n);
3416 } else {
3417 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3418 off += size;
3421 isl_dim_map_div(dim_map, bmap, off + n);
3423 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3424 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3425 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3426 return res;
3429 /* Insert "n" rows in the divs of "bmap".
3431 * The number of columns is not changed, which means that the last
3432 * dimensions of "bmap" are being reintepreted as the new divs.
3433 * The space of "bmap" is not adjusted, however, which means
3434 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3435 * from the space of "bmap" is the responsibility of the caller.
3437 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3438 int n)
3440 int i;
3441 size_t row_size;
3442 isl_int **new_div;
3443 isl_int *old;
3445 bmap = isl_basic_map_cow(bmap);
3446 if (!bmap)
3447 return NULL;
3449 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3450 old = bmap->block2.data;
3451 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3452 (bmap->extra + n) * (1 + row_size));
3453 if (!bmap->block2.data)
3454 return isl_basic_map_free(bmap);
3455 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3456 if (!new_div)
3457 return isl_basic_map_free(bmap);
3458 for (i = 0; i < n; ++i) {
3459 new_div[i] = bmap->block2.data +
3460 (bmap->extra + i) * (1 + row_size);
3461 isl_seq_clr(new_div[i], 1 + row_size);
3463 for (i = 0; i < bmap->extra; ++i)
3464 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3465 free(bmap->div);
3466 bmap->div = new_div;
3467 bmap->n_div += n;
3468 bmap->extra += n;
3470 return bmap;
3473 /* Turn the n dimensions of type type, starting at first
3474 * into existentially quantified variables.
3476 __isl_give isl_basic_map *isl_basic_map_project_out(
3477 __isl_take isl_basic_map *bmap,
3478 enum isl_dim_type type, unsigned first, unsigned n)
3480 if (n == 0)
3481 return basic_map_space_reset(bmap, type);
3483 if (!bmap)
3484 return NULL;
3486 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3487 return isl_basic_map_remove_dims(bmap, type, first, n);
3489 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3490 goto error);
3492 bmap = move_last(bmap, type, first, n);
3493 bmap = isl_basic_map_cow(bmap);
3494 bmap = insert_div_rows(bmap, n);
3495 if (!bmap)
3496 return NULL;
3498 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3499 if (!bmap->dim)
3500 goto error;
3501 bmap = isl_basic_map_simplify(bmap);
3502 bmap = isl_basic_map_drop_redundant_divs(bmap);
3503 return isl_basic_map_finalize(bmap);
3504 error:
3505 isl_basic_map_free(bmap);
3506 return NULL;
3509 /* Turn the n dimensions of type type, starting at first
3510 * into existentially quantified variables.
3512 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3513 enum isl_dim_type type, unsigned first, unsigned n)
3515 return (isl_basic_set *)isl_basic_map_project_out(
3516 (isl_basic_map *)bset, type, first, n);
3519 /* Turn the n dimensions of type type, starting at first
3520 * into existentially quantified variables.
3522 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3523 enum isl_dim_type type, unsigned first, unsigned n)
3525 int i;
3527 if (!map)
3528 return NULL;
3530 if (n == 0)
3531 return map_space_reset(map, type);
3533 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3535 map = isl_map_cow(map);
3536 if (!map)
3537 return NULL;
3539 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3540 if (!map->dim)
3541 goto error;
3543 for (i = 0; i < map->n; ++i) {
3544 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3545 if (!map->p[i])
3546 goto error;
3549 return map;
3550 error:
3551 isl_map_free(map);
3552 return NULL;
3555 /* Turn the n dimensions of type type, starting at first
3556 * into existentially quantified variables.
3558 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3559 enum isl_dim_type type, unsigned first, unsigned n)
3561 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3564 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3566 int i, j;
3568 for (i = 0; i < n; ++i) {
3569 j = isl_basic_map_alloc_div(bmap);
3570 if (j < 0)
3571 goto error;
3572 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3574 return bmap;
3575 error:
3576 isl_basic_map_free(bmap);
3577 return NULL;
3580 struct isl_basic_map *isl_basic_map_apply_range(
3581 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3583 isl_space *dim_result = NULL;
3584 struct isl_basic_map *bmap;
3585 unsigned n_in, n_out, n, nparam, total, pos;
3586 struct isl_dim_map *dim_map1, *dim_map2;
3588 if (!bmap1 || !bmap2)
3589 goto error;
3590 if (!isl_space_match(bmap1->dim, isl_dim_param,
3591 bmap2->dim, isl_dim_param))
3592 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3593 "parameters don't match", goto error);
3594 if (!isl_space_tuple_match(bmap1->dim, isl_dim_out,
3595 bmap2->dim, isl_dim_in))
3596 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3597 "spaces don't match", goto error);
3599 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3600 isl_space_copy(bmap2->dim));
3602 n_in = isl_basic_map_n_in(bmap1);
3603 n_out = isl_basic_map_n_out(bmap2);
3604 n = isl_basic_map_n_out(bmap1);
3605 nparam = isl_basic_map_n_param(bmap1);
3607 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3608 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3609 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3610 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3611 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3612 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3613 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3614 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3615 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3616 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3617 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3619 bmap = isl_basic_map_alloc_space(dim_result,
3620 bmap1->n_div + bmap2->n_div + n,
3621 bmap1->n_eq + bmap2->n_eq,
3622 bmap1->n_ineq + bmap2->n_ineq);
3623 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3624 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3625 bmap = add_divs(bmap, n);
3626 bmap = isl_basic_map_simplify(bmap);
3627 bmap = isl_basic_map_drop_redundant_divs(bmap);
3628 return isl_basic_map_finalize(bmap);
3629 error:
3630 isl_basic_map_free(bmap1);
3631 isl_basic_map_free(bmap2);
3632 return NULL;
3635 struct isl_basic_set *isl_basic_set_apply(
3636 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3638 if (!bset || !bmap)
3639 goto error;
3641 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3642 goto error);
3644 return (struct isl_basic_set *)
3645 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3646 error:
3647 isl_basic_set_free(bset);
3648 isl_basic_map_free(bmap);
3649 return NULL;
3652 struct isl_basic_map *isl_basic_map_apply_domain(
3653 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3655 if (!bmap1 || !bmap2)
3656 goto error;
3658 isl_assert(bmap1->ctx,
3659 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3660 isl_assert(bmap1->ctx,
3661 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3662 goto error);
3664 bmap1 = isl_basic_map_reverse(bmap1);
3665 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3666 return isl_basic_map_reverse(bmap1);
3667 error:
3668 isl_basic_map_free(bmap1);
3669 isl_basic_map_free(bmap2);
3670 return NULL;
3673 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3674 * A \cap B -> f(A) + f(B)
3676 struct isl_basic_map *isl_basic_map_sum(
3677 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3679 unsigned n_in, n_out, nparam, total, pos;
3680 struct isl_basic_map *bmap = NULL;
3681 struct isl_dim_map *dim_map1, *dim_map2;
3682 int i;
3684 if (!bmap1 || !bmap2)
3685 goto error;
3687 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3688 goto error);
3690 nparam = isl_basic_map_n_param(bmap1);
3691 n_in = isl_basic_map_n_in(bmap1);
3692 n_out = isl_basic_map_n_out(bmap1);
3694 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3695 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3696 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3697 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3698 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3699 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3700 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3701 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3702 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3703 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3704 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3706 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3707 bmap1->n_div + bmap2->n_div + 2 * n_out,
3708 bmap1->n_eq + bmap2->n_eq + n_out,
3709 bmap1->n_ineq + bmap2->n_ineq);
3710 for (i = 0; i < n_out; ++i) {
3711 int j = isl_basic_map_alloc_equality(bmap);
3712 if (j < 0)
3713 goto error;
3714 isl_seq_clr(bmap->eq[j], 1+total);
3715 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3716 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3717 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3719 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3720 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3721 bmap = add_divs(bmap, 2 * n_out);
3723 bmap = isl_basic_map_simplify(bmap);
3724 return isl_basic_map_finalize(bmap);
3725 error:
3726 isl_basic_map_free(bmap);
3727 isl_basic_map_free(bmap1);
3728 isl_basic_map_free(bmap2);
3729 return NULL;
3732 /* Given two maps A -> f(A) and B -> g(B), construct a map
3733 * A \cap B -> f(A) + f(B)
3735 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3737 struct isl_map *result;
3738 int i, j;
3740 if (!map1 || !map2)
3741 goto error;
3743 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3745 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3746 map1->n * map2->n, 0);
3747 if (!result)
3748 goto error;
3749 for (i = 0; i < map1->n; ++i)
3750 for (j = 0; j < map2->n; ++j) {
3751 struct isl_basic_map *part;
3752 part = isl_basic_map_sum(
3753 isl_basic_map_copy(map1->p[i]),
3754 isl_basic_map_copy(map2->p[j]));
3755 if (isl_basic_map_is_empty(part))
3756 isl_basic_map_free(part);
3757 else
3758 result = isl_map_add_basic_map(result, part);
3759 if (!result)
3760 goto error;
3762 isl_map_free(map1);
3763 isl_map_free(map2);
3764 return result;
3765 error:
3766 isl_map_free(map1);
3767 isl_map_free(map2);
3768 return NULL;
3771 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3772 __isl_take isl_set *set2)
3774 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3777 /* Given a basic map A -> f(A), construct A -> -f(A).
3779 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3781 int i, j;
3782 unsigned off, n;
3784 bmap = isl_basic_map_cow(bmap);
3785 if (!bmap)
3786 return NULL;
3788 n = isl_basic_map_dim(bmap, isl_dim_out);
3789 off = isl_basic_map_offset(bmap, isl_dim_out);
3790 for (i = 0; i < bmap->n_eq; ++i)
3791 for (j = 0; j < n; ++j)
3792 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3793 for (i = 0; i < bmap->n_ineq; ++i)
3794 for (j = 0; j < n; ++j)
3795 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3796 for (i = 0; i < bmap->n_div; ++i)
3797 for (j = 0; j < n; ++j)
3798 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3799 bmap = isl_basic_map_gauss(bmap, NULL);
3800 return isl_basic_map_finalize(bmap);
3803 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3805 return isl_basic_map_neg(bset);
3808 /* Given a map A -> f(A), construct A -> -f(A).
3810 struct isl_map *isl_map_neg(struct isl_map *map)
3812 int i;
3814 map = isl_map_cow(map);
3815 if (!map)
3816 return NULL;
3818 for (i = 0; i < map->n; ++i) {
3819 map->p[i] = isl_basic_map_neg(map->p[i]);
3820 if (!map->p[i])
3821 goto error;
3824 return map;
3825 error:
3826 isl_map_free(map);
3827 return NULL;
3830 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3832 return (isl_set *)isl_map_neg((isl_map *)set);
3835 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3836 * A -> floor(f(A)/d).
3838 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3839 isl_int d)
3841 unsigned n_in, n_out, nparam, total, pos;
3842 struct isl_basic_map *result = NULL;
3843 struct isl_dim_map *dim_map;
3844 int i;
3846 if (!bmap)
3847 return NULL;
3849 nparam = isl_basic_map_n_param(bmap);
3850 n_in = isl_basic_map_n_in(bmap);
3851 n_out = isl_basic_map_n_out(bmap);
3853 total = nparam + n_in + n_out + bmap->n_div + n_out;
3854 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3855 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3856 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3857 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3858 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3860 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3861 bmap->n_div + n_out,
3862 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3863 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3864 result = add_divs(result, n_out);
3865 for (i = 0; i < n_out; ++i) {
3866 int j;
3867 j = isl_basic_map_alloc_inequality(result);
3868 if (j < 0)
3869 goto error;
3870 isl_seq_clr(result->ineq[j], 1+total);
3871 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3872 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3873 j = isl_basic_map_alloc_inequality(result);
3874 if (j < 0)
3875 goto error;
3876 isl_seq_clr(result->ineq[j], 1+total);
3877 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3878 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3879 isl_int_sub_ui(result->ineq[j][0], d, 1);
3882 result = isl_basic_map_simplify(result);
3883 return isl_basic_map_finalize(result);
3884 error:
3885 isl_basic_map_free(result);
3886 return NULL;
3889 /* Given a map A -> f(A) and an integer d, construct a map
3890 * A -> floor(f(A)/d).
3892 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3894 int i;
3896 map = isl_map_cow(map);
3897 if (!map)
3898 return NULL;
3900 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3901 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3902 for (i = 0; i < map->n; ++i) {
3903 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3904 if (!map->p[i])
3905 goto error;
3908 return map;
3909 error:
3910 isl_map_free(map);
3911 return NULL;
3914 /* Given a map A -> f(A) and an integer d, construct a map
3915 * A -> floor(f(A)/d).
3917 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3918 __isl_take isl_val *d)
3920 if (!map || !d)
3921 goto error;
3922 if (!isl_val_is_int(d))
3923 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3924 "expecting integer denominator", goto error);
3925 map = isl_map_floordiv(map, d->n);
3926 isl_val_free(d);
3927 return map;
3928 error:
3929 isl_map_free(map);
3930 isl_val_free(d);
3931 return NULL;
3934 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3936 int i;
3937 unsigned nparam;
3938 unsigned n_in;
3940 i = isl_basic_map_alloc_equality(bmap);
3941 if (i < 0)
3942 goto error;
3943 nparam = isl_basic_map_n_param(bmap);
3944 n_in = isl_basic_map_n_in(bmap);
3945 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3946 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3947 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3948 return isl_basic_map_finalize(bmap);
3949 error:
3950 isl_basic_map_free(bmap);
3951 return NULL;
3954 /* Add a constraints to "bmap" expressing i_pos < o_pos
3956 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3958 int i;
3959 unsigned nparam;
3960 unsigned n_in;
3962 i = isl_basic_map_alloc_inequality(bmap);
3963 if (i < 0)
3964 goto error;
3965 nparam = isl_basic_map_n_param(bmap);
3966 n_in = isl_basic_map_n_in(bmap);
3967 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3968 isl_int_set_si(bmap->ineq[i][0], -1);
3969 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3970 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3971 return isl_basic_map_finalize(bmap);
3972 error:
3973 isl_basic_map_free(bmap);
3974 return NULL;
3977 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3979 static __isl_give isl_basic_map *var_less_or_equal(
3980 __isl_take isl_basic_map *bmap, unsigned pos)
3982 int i;
3983 unsigned nparam;
3984 unsigned n_in;
3986 i = isl_basic_map_alloc_inequality(bmap);
3987 if (i < 0)
3988 goto error;
3989 nparam = isl_basic_map_n_param(bmap);
3990 n_in = isl_basic_map_n_in(bmap);
3991 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3992 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3993 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3994 return isl_basic_map_finalize(bmap);
3995 error:
3996 isl_basic_map_free(bmap);
3997 return NULL;
4000 /* Add a constraints to "bmap" expressing i_pos > o_pos
4002 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4004 int i;
4005 unsigned nparam;
4006 unsigned n_in;
4008 i = isl_basic_map_alloc_inequality(bmap);
4009 if (i < 0)
4010 goto error;
4011 nparam = isl_basic_map_n_param(bmap);
4012 n_in = isl_basic_map_n_in(bmap);
4013 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4014 isl_int_set_si(bmap->ineq[i][0], -1);
4015 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4016 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4017 return isl_basic_map_finalize(bmap);
4018 error:
4019 isl_basic_map_free(bmap);
4020 return NULL;
4023 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4025 static __isl_give isl_basic_map *var_more_or_equal(
4026 __isl_take isl_basic_map *bmap, unsigned pos)
4028 int i;
4029 unsigned nparam;
4030 unsigned n_in;
4032 i = isl_basic_map_alloc_inequality(bmap);
4033 if (i < 0)
4034 goto error;
4035 nparam = isl_basic_map_n_param(bmap);
4036 n_in = isl_basic_map_n_in(bmap);
4037 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4038 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4039 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4040 return isl_basic_map_finalize(bmap);
4041 error:
4042 isl_basic_map_free(bmap);
4043 return NULL;
4046 __isl_give isl_basic_map *isl_basic_map_equal(
4047 __isl_take isl_space *dim, unsigned n_equal)
4049 int i;
4050 struct isl_basic_map *bmap;
4051 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4052 if (!bmap)
4053 return NULL;
4054 for (i = 0; i < n_equal && bmap; ++i)
4055 bmap = var_equal(bmap, i);
4056 return isl_basic_map_finalize(bmap);
4059 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4061 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4062 unsigned pos)
4064 int i;
4065 struct isl_basic_map *bmap;
4066 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4067 if (!bmap)
4068 return NULL;
4069 for (i = 0; i < pos && bmap; ++i)
4070 bmap = var_equal(bmap, i);
4071 if (bmap)
4072 bmap = var_less(bmap, pos);
4073 return isl_basic_map_finalize(bmap);
4076 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4078 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4079 __isl_take isl_space *dim, unsigned pos)
4081 int i;
4082 isl_basic_map *bmap;
4084 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4085 for (i = 0; i < pos; ++i)
4086 bmap = var_equal(bmap, i);
4087 bmap = var_less_or_equal(bmap, pos);
4088 return isl_basic_map_finalize(bmap);
4091 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4093 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4094 unsigned pos)
4096 int i;
4097 struct isl_basic_map *bmap;
4098 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4099 if (!bmap)
4100 return NULL;
4101 for (i = 0; i < pos && bmap; ++i)
4102 bmap = var_equal(bmap, i);
4103 if (bmap)
4104 bmap = var_more(bmap, pos);
4105 return isl_basic_map_finalize(bmap);
4108 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4110 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4111 __isl_take isl_space *dim, unsigned pos)
4113 int i;
4114 isl_basic_map *bmap;
4116 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4117 for (i = 0; i < pos; ++i)
4118 bmap = var_equal(bmap, i);
4119 bmap = var_more_or_equal(bmap, pos);
4120 return isl_basic_map_finalize(bmap);
4123 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4124 unsigned n, int equal)
4126 struct isl_map *map;
4127 int i;
4129 if (n == 0 && equal)
4130 return isl_map_universe(dims);
4132 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4134 for (i = 0; i + 1 < n; ++i)
4135 map = isl_map_add_basic_map(map,
4136 isl_basic_map_less_at(isl_space_copy(dims), i));
4137 if (n > 0) {
4138 if (equal)
4139 map = isl_map_add_basic_map(map,
4140 isl_basic_map_less_or_equal_at(dims, n - 1));
4141 else
4142 map = isl_map_add_basic_map(map,
4143 isl_basic_map_less_at(dims, n - 1));
4144 } else
4145 isl_space_free(dims);
4147 return map;
4150 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4152 if (!dims)
4153 return NULL;
4154 return map_lex_lte_first(dims, dims->n_out, equal);
4157 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4159 return map_lex_lte_first(dim, n, 0);
4162 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4164 return map_lex_lte_first(dim, n, 1);
4167 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4169 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4172 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4174 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4177 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4178 unsigned n, int equal)
4180 struct isl_map *map;
4181 int i;
4183 if (n == 0 && equal)
4184 return isl_map_universe(dims);
4186 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4188 for (i = 0; i + 1 < n; ++i)
4189 map = isl_map_add_basic_map(map,
4190 isl_basic_map_more_at(isl_space_copy(dims), i));
4191 if (n > 0) {
4192 if (equal)
4193 map = isl_map_add_basic_map(map,
4194 isl_basic_map_more_or_equal_at(dims, n - 1));
4195 else
4196 map = isl_map_add_basic_map(map,
4197 isl_basic_map_more_at(dims, n - 1));
4198 } else
4199 isl_space_free(dims);
4201 return map;
4204 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4206 if (!dims)
4207 return NULL;
4208 return map_lex_gte_first(dims, dims->n_out, equal);
4211 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4213 return map_lex_gte_first(dim, n, 0);
4216 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4218 return map_lex_gte_first(dim, n, 1);
4221 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4223 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4226 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4228 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4231 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4232 __isl_take isl_set *set2)
4234 isl_map *map;
4235 map = isl_map_lex_le(isl_set_get_space(set1));
4236 map = isl_map_intersect_domain(map, set1);
4237 map = isl_map_intersect_range(map, set2);
4238 return map;
4241 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4242 __isl_take isl_set *set2)
4244 isl_map *map;
4245 map = isl_map_lex_lt(isl_set_get_space(set1));
4246 map = isl_map_intersect_domain(map, set1);
4247 map = isl_map_intersect_range(map, set2);
4248 return map;
4251 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4252 __isl_take isl_set *set2)
4254 isl_map *map;
4255 map = isl_map_lex_ge(isl_set_get_space(set1));
4256 map = isl_map_intersect_domain(map, set1);
4257 map = isl_map_intersect_range(map, set2);
4258 return map;
4261 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4262 __isl_take isl_set *set2)
4264 isl_map *map;
4265 map = isl_map_lex_gt(isl_set_get_space(set1));
4266 map = isl_map_intersect_domain(map, set1);
4267 map = isl_map_intersect_range(map, set2);
4268 return map;
4271 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4272 __isl_take isl_map *map2)
4274 isl_map *map;
4275 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4276 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4277 map = isl_map_apply_range(map, isl_map_reverse(map2));
4278 return map;
4281 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4282 __isl_take isl_map *map2)
4284 isl_map *map;
4285 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4286 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4287 map = isl_map_apply_range(map, isl_map_reverse(map2));
4288 return map;
4291 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4292 __isl_take isl_map *map2)
4294 isl_map *map;
4295 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4296 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4297 map = isl_map_apply_range(map, isl_map_reverse(map2));
4298 return map;
4301 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4302 __isl_take isl_map *map2)
4304 isl_map *map;
4305 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4306 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4307 map = isl_map_apply_range(map, isl_map_reverse(map2));
4308 return map;
4311 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4312 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4314 struct isl_basic_map *bmap;
4316 bset = isl_basic_set_cow(bset);
4317 if (!bset || !dim)
4318 goto error;
4320 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4321 isl_space_free(bset->dim);
4322 bmap = (struct isl_basic_map *) bset;
4323 bmap->dim = dim;
4324 return isl_basic_map_finalize(bmap);
4325 error:
4326 isl_basic_set_free(bset);
4327 isl_space_free(dim);
4328 return NULL;
4331 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4333 if (!bmap)
4334 goto error;
4335 if (bmap->dim->n_in == 0)
4336 return (struct isl_basic_set *)bmap;
4337 bmap = isl_basic_map_cow(bmap);
4338 if (!bmap)
4339 goto error;
4340 bmap->dim = isl_space_as_set_space(bmap->dim);
4341 if (!bmap->dim)
4342 goto error;
4343 bmap = isl_basic_map_finalize(bmap);
4344 return (struct isl_basic_set *)bmap;
4345 error:
4346 isl_basic_map_free(bmap);
4347 return NULL;
4350 /* For a div d = floor(f/m), add the constraints
4352 * f - m d >= 0
4353 * -(f-(n-1)) + m d >= 0
4355 * Note that the second constraint is the negation of
4357 * f - m d >= n
4359 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4360 unsigned pos, isl_int *div)
4362 int i, j;
4363 unsigned total = isl_basic_map_total_dim(bmap);
4365 i = isl_basic_map_alloc_inequality(bmap);
4366 if (i < 0)
4367 return -1;
4368 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4369 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4371 j = isl_basic_map_alloc_inequality(bmap);
4372 if (j < 0)
4373 return -1;
4374 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
4375 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
4376 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
4377 return j;
4380 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4381 unsigned pos, isl_int *div)
4383 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4384 pos, div);
4387 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4389 unsigned total = isl_basic_map_total_dim(bmap);
4390 unsigned div_pos = total - bmap->n_div + div;
4392 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4393 bmap->div[div]);
4396 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4398 return isl_basic_map_add_div_constraints(bset, div);
4401 struct isl_basic_set *isl_basic_map_underlying_set(
4402 struct isl_basic_map *bmap)
4404 if (!bmap)
4405 goto error;
4406 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4407 bmap->n_div == 0 &&
4408 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4409 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4410 return (struct isl_basic_set *)bmap;
4411 bmap = isl_basic_map_cow(bmap);
4412 if (!bmap)
4413 goto error;
4414 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4415 if (!bmap->dim)
4416 goto error;
4417 bmap->extra -= bmap->n_div;
4418 bmap->n_div = 0;
4419 bmap = isl_basic_map_finalize(bmap);
4420 return (struct isl_basic_set *)bmap;
4421 error:
4422 isl_basic_map_free(bmap);
4423 return NULL;
4426 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4427 __isl_take isl_basic_set *bset)
4429 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4432 struct isl_basic_map *isl_basic_map_overlying_set(
4433 struct isl_basic_set *bset, struct isl_basic_map *like)
4435 struct isl_basic_map *bmap;
4436 struct isl_ctx *ctx;
4437 unsigned total;
4438 int i;
4440 if (!bset || !like)
4441 goto error;
4442 ctx = bset->ctx;
4443 isl_assert(ctx, bset->n_div == 0, goto error);
4444 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4445 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4446 goto error);
4447 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4448 isl_basic_map_free(like);
4449 return (struct isl_basic_map *)bset;
4451 bset = isl_basic_set_cow(bset);
4452 if (!bset)
4453 goto error;
4454 total = bset->dim->n_out + bset->extra;
4455 bmap = (struct isl_basic_map *)bset;
4456 isl_space_free(bmap->dim);
4457 bmap->dim = isl_space_copy(like->dim);
4458 if (!bmap->dim)
4459 goto error;
4460 bmap->n_div = like->n_div;
4461 bmap->extra += like->n_div;
4462 if (bmap->extra) {
4463 unsigned ltotal;
4464 isl_int **div;
4465 ltotal = total - bmap->extra + like->extra;
4466 if (ltotal > total)
4467 ltotal = total;
4468 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4469 bmap->extra * (1 + 1 + total));
4470 if (isl_blk_is_error(bmap->block2))
4471 goto error;
4472 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4473 if (!div)
4474 goto error;
4475 bmap->div = div;
4476 for (i = 0; i < bmap->extra; ++i)
4477 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4478 for (i = 0; i < like->n_div; ++i) {
4479 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4480 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4482 bmap = isl_basic_map_extend_constraints(bmap,
4483 0, 2 * like->n_div);
4484 for (i = 0; i < like->n_div; ++i) {
4485 if (!bmap)
4486 break;
4487 if (isl_int_is_zero(bmap->div[i][0]))
4488 continue;
4489 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4490 bmap = isl_basic_map_free(bmap);
4493 isl_basic_map_free(like);
4494 bmap = isl_basic_map_simplify(bmap);
4495 bmap = isl_basic_map_finalize(bmap);
4496 return bmap;
4497 error:
4498 isl_basic_map_free(like);
4499 isl_basic_set_free(bset);
4500 return NULL;
4503 struct isl_basic_set *isl_basic_set_from_underlying_set(
4504 struct isl_basic_set *bset, struct isl_basic_set *like)
4506 return (struct isl_basic_set *)
4507 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4510 struct isl_set *isl_set_from_underlying_set(
4511 struct isl_set *set, struct isl_basic_set *like)
4513 int i;
4515 if (!set || !like)
4516 goto error;
4517 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4518 goto error);
4519 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4520 isl_basic_set_free(like);
4521 return set;
4523 set = isl_set_cow(set);
4524 if (!set)
4525 goto error;
4526 for (i = 0; i < set->n; ++i) {
4527 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4528 isl_basic_set_copy(like));
4529 if (!set->p[i])
4530 goto error;
4532 isl_space_free(set->dim);
4533 set->dim = isl_space_copy(like->dim);
4534 if (!set->dim)
4535 goto error;
4536 isl_basic_set_free(like);
4537 return set;
4538 error:
4539 isl_basic_set_free(like);
4540 isl_set_free(set);
4541 return NULL;
4544 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4546 int i;
4548 map = isl_map_cow(map);
4549 if (!map)
4550 return NULL;
4551 map->dim = isl_space_cow(map->dim);
4552 if (!map->dim)
4553 goto error;
4555 for (i = 1; i < map->n; ++i)
4556 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4557 goto error);
4558 for (i = 0; i < map->n; ++i) {
4559 map->p[i] = (struct isl_basic_map *)
4560 isl_basic_map_underlying_set(map->p[i]);
4561 if (!map->p[i])
4562 goto error;
4564 if (map->n == 0)
4565 map->dim = isl_space_underlying(map->dim, 0);
4566 else {
4567 isl_space_free(map->dim);
4568 map->dim = isl_space_copy(map->p[0]->dim);
4570 if (!map->dim)
4571 goto error;
4572 return (struct isl_set *)map;
4573 error:
4574 isl_map_free(map);
4575 return NULL;
4578 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4580 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4583 __isl_give isl_basic_map *isl_basic_map_reset_space(
4584 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4586 bmap = isl_basic_map_cow(bmap);
4587 if (!bmap || !dim)
4588 goto error;
4590 isl_space_free(bmap->dim);
4591 bmap->dim = dim;
4593 bmap = isl_basic_map_finalize(bmap);
4595 return bmap;
4596 error:
4597 isl_basic_map_free(bmap);
4598 isl_space_free(dim);
4599 return NULL;
4602 __isl_give isl_basic_set *isl_basic_set_reset_space(
4603 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4605 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4606 dim);
4609 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4610 __isl_take isl_space *dim)
4612 int i;
4614 map = isl_map_cow(map);
4615 if (!map || !dim)
4616 goto error;
4618 for (i = 0; i < map->n; ++i) {
4619 map->p[i] = isl_basic_map_reset_space(map->p[i],
4620 isl_space_copy(dim));
4621 if (!map->p[i])
4622 goto error;
4624 isl_space_free(map->dim);
4625 map->dim = dim;
4627 return map;
4628 error:
4629 isl_map_free(map);
4630 isl_space_free(dim);
4631 return NULL;
4634 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4635 __isl_take isl_space *dim)
4637 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4640 /* Compute the parameter domain of the given basic set.
4642 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4644 isl_space *space;
4645 unsigned n;
4647 if (isl_basic_set_is_params(bset))
4648 return bset;
4650 n = isl_basic_set_dim(bset, isl_dim_set);
4651 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4652 space = isl_basic_set_get_space(bset);
4653 space = isl_space_params(space);
4654 bset = isl_basic_set_reset_space(bset, space);
4655 return bset;
4658 /* Construct a zero-dimensional basic set with the given parameter domain.
4660 __isl_give isl_basic_set *isl_basic_set_from_params(
4661 __isl_take isl_basic_set *bset)
4663 isl_space *space;
4664 space = isl_basic_set_get_space(bset);
4665 space = isl_space_set_from_params(space);
4666 bset = isl_basic_set_reset_space(bset, space);
4667 return bset;
4670 /* Compute the parameter domain of the given set.
4672 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4674 isl_space *space;
4675 unsigned n;
4677 if (isl_set_is_params(set))
4678 return set;
4680 n = isl_set_dim(set, isl_dim_set);
4681 set = isl_set_project_out(set, isl_dim_set, 0, n);
4682 space = isl_set_get_space(set);
4683 space = isl_space_params(space);
4684 set = isl_set_reset_space(set, space);
4685 return set;
4688 /* Construct a zero-dimensional set with the given parameter domain.
4690 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4692 isl_space *space;
4693 space = isl_set_get_space(set);
4694 space = isl_space_set_from_params(space);
4695 set = isl_set_reset_space(set, space);
4696 return set;
4699 /* Compute the parameter domain of the given map.
4701 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4703 isl_space *space;
4704 unsigned n;
4706 n = isl_map_dim(map, isl_dim_in);
4707 map = isl_map_project_out(map, isl_dim_in, 0, n);
4708 n = isl_map_dim(map, isl_dim_out);
4709 map = isl_map_project_out(map, isl_dim_out, 0, n);
4710 space = isl_map_get_space(map);
4711 space = isl_space_params(space);
4712 map = isl_map_reset_space(map, space);
4713 return map;
4716 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4718 isl_space *dim;
4719 struct isl_basic_set *domain;
4720 unsigned n_in;
4721 unsigned n_out;
4723 if (!bmap)
4724 return NULL;
4725 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4727 n_in = isl_basic_map_n_in(bmap);
4728 n_out = isl_basic_map_n_out(bmap);
4729 domain = isl_basic_set_from_basic_map(bmap);
4730 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4732 domain = isl_basic_set_reset_space(domain, dim);
4734 return domain;
4737 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4739 if (!bmap)
4740 return -1;
4741 return isl_space_may_be_set(bmap->dim);
4744 /* Is this basic map actually a set?
4745 * Users should never call this function. Outside of isl,
4746 * the type should indicate whether something is a set or a map.
4748 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4750 if (!bmap)
4751 return -1;
4752 return isl_space_is_set(bmap->dim);
4755 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4757 if (!bmap)
4758 return NULL;
4759 if (isl_basic_map_is_set(bmap))
4760 return bmap;
4761 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4764 __isl_give isl_basic_map *isl_basic_map_domain_map(
4765 __isl_take isl_basic_map *bmap)
4767 int i, k;
4768 isl_space *dim;
4769 isl_basic_map *domain;
4770 int nparam, n_in, n_out;
4771 unsigned total;
4773 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4774 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4775 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4777 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4778 domain = isl_basic_map_universe(dim);
4780 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4781 bmap = isl_basic_map_apply_range(bmap, domain);
4782 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4784 total = isl_basic_map_total_dim(bmap);
4786 for (i = 0; i < n_in; ++i) {
4787 k = isl_basic_map_alloc_equality(bmap);
4788 if (k < 0)
4789 goto error;
4790 isl_seq_clr(bmap->eq[k], 1 + total);
4791 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4792 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4795 bmap = isl_basic_map_gauss(bmap, NULL);
4796 return isl_basic_map_finalize(bmap);
4797 error:
4798 isl_basic_map_free(bmap);
4799 return NULL;
4802 __isl_give isl_basic_map *isl_basic_map_range_map(
4803 __isl_take isl_basic_map *bmap)
4805 int i, k;
4806 isl_space *dim;
4807 isl_basic_map *range;
4808 int nparam, n_in, n_out;
4809 unsigned total;
4811 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4812 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4813 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4815 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4816 range = isl_basic_map_universe(dim);
4818 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4819 bmap = isl_basic_map_apply_range(bmap, range);
4820 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4822 total = isl_basic_map_total_dim(bmap);
4824 for (i = 0; i < n_out; ++i) {
4825 k = isl_basic_map_alloc_equality(bmap);
4826 if (k < 0)
4827 goto error;
4828 isl_seq_clr(bmap->eq[k], 1 + total);
4829 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4830 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4833 bmap = isl_basic_map_gauss(bmap, NULL);
4834 return isl_basic_map_finalize(bmap);
4835 error:
4836 isl_basic_map_free(bmap);
4837 return NULL;
4840 int isl_map_may_be_set(__isl_keep isl_map *map)
4842 if (!map)
4843 return -1;
4844 return isl_space_may_be_set(map->dim);
4847 /* Is this map actually a set?
4848 * Users should never call this function. Outside of isl,
4849 * the type should indicate whether something is a set or a map.
4851 int isl_map_is_set(__isl_keep isl_map *map)
4853 if (!map)
4854 return -1;
4855 return isl_space_is_set(map->dim);
4858 struct isl_set *isl_map_range(struct isl_map *map)
4860 int i;
4861 struct isl_set *set;
4863 if (!map)
4864 goto error;
4865 if (isl_map_is_set(map))
4866 return (isl_set *)map;
4868 map = isl_map_cow(map);
4869 if (!map)
4870 goto error;
4872 set = (struct isl_set *) map;
4873 set->dim = isl_space_range(set->dim);
4874 if (!set->dim)
4875 goto error;
4876 for (i = 0; i < map->n; ++i) {
4877 set->p[i] = isl_basic_map_range(map->p[i]);
4878 if (!set->p[i])
4879 goto error;
4881 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4882 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4883 return set;
4884 error:
4885 isl_map_free(map);
4886 return NULL;
4889 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4891 int i;
4893 map = isl_map_cow(map);
4894 if (!map)
4895 return NULL;
4897 map->dim = isl_space_domain_map(map->dim);
4898 if (!map->dim)
4899 goto error;
4900 for (i = 0; i < map->n; ++i) {
4901 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4902 if (!map->p[i])
4903 goto error;
4905 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4906 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4907 return map;
4908 error:
4909 isl_map_free(map);
4910 return NULL;
4913 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4915 int i;
4916 isl_space *range_dim;
4918 map = isl_map_cow(map);
4919 if (!map)
4920 return NULL;
4922 range_dim = isl_space_range(isl_map_get_space(map));
4923 range_dim = isl_space_from_range(range_dim);
4924 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4925 map->dim = isl_space_join(map->dim, range_dim);
4926 if (!map->dim)
4927 goto error;
4928 for (i = 0; i < map->n; ++i) {
4929 map->p[i] = isl_basic_map_range_map(map->p[i]);
4930 if (!map->p[i])
4931 goto error;
4933 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4934 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4935 return map;
4936 error:
4937 isl_map_free(map);
4938 return NULL;
4941 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4942 __isl_take isl_space *dim)
4944 int i;
4945 struct isl_map *map = NULL;
4947 set = isl_set_cow(set);
4948 if (!set || !dim)
4949 goto error;
4950 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4951 map = (struct isl_map *)set;
4952 for (i = 0; i < set->n; ++i) {
4953 map->p[i] = isl_basic_map_from_basic_set(
4954 set->p[i], isl_space_copy(dim));
4955 if (!map->p[i])
4956 goto error;
4958 isl_space_free(map->dim);
4959 map->dim = dim;
4960 return map;
4961 error:
4962 isl_space_free(dim);
4963 isl_set_free(set);
4964 return NULL;
4967 __isl_give isl_basic_map *isl_basic_map_from_domain(
4968 __isl_take isl_basic_set *bset)
4970 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4973 __isl_give isl_basic_map *isl_basic_map_from_range(
4974 __isl_take isl_basic_set *bset)
4976 isl_space *space;
4977 space = isl_basic_set_get_space(bset);
4978 space = isl_space_from_range(space);
4979 bset = isl_basic_set_reset_space(bset, space);
4980 return (isl_basic_map *)bset;
4983 struct isl_map *isl_map_from_range(struct isl_set *set)
4985 isl_space *space;
4986 space = isl_set_get_space(set);
4987 space = isl_space_from_range(space);
4988 set = isl_set_reset_space(set, space);
4989 return (struct isl_map *)set;
4992 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4994 return isl_map_reverse(isl_map_from_range(set));
4997 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4998 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5000 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5003 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5004 __isl_take isl_set *range)
5006 return isl_map_apply_range(isl_map_reverse(domain), range);
5009 struct isl_set *isl_set_from_map(struct isl_map *map)
5011 int i;
5012 struct isl_set *set = NULL;
5014 if (!map)
5015 return NULL;
5016 map = isl_map_cow(map);
5017 if (!map)
5018 return NULL;
5019 map->dim = isl_space_as_set_space(map->dim);
5020 if (!map->dim)
5021 goto error;
5022 set = (struct isl_set *)map;
5023 for (i = 0; i < map->n; ++i) {
5024 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
5025 if (!set->p[i])
5026 goto error;
5028 return set;
5029 error:
5030 isl_map_free(map);
5031 return NULL;
5034 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5035 unsigned flags)
5037 struct isl_map *map;
5039 if (!dim)
5040 return NULL;
5041 if (n < 0)
5042 isl_die(dim->ctx, isl_error_internal,
5043 "negative number of basic maps", goto error);
5044 map = isl_alloc(dim->ctx, struct isl_map,
5045 sizeof(struct isl_map) +
5046 (n - 1) * sizeof(struct isl_basic_map *));
5047 if (!map)
5048 goto error;
5050 map->ctx = dim->ctx;
5051 isl_ctx_ref(map->ctx);
5052 map->ref = 1;
5053 map->size = n;
5054 map->n = 0;
5055 map->dim = dim;
5056 map->flags = flags;
5057 return map;
5058 error:
5059 isl_space_free(dim);
5060 return NULL;
5063 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5064 unsigned nparam, unsigned in, unsigned out, int n,
5065 unsigned flags)
5067 struct isl_map *map;
5068 isl_space *dims;
5070 dims = isl_space_alloc(ctx, nparam, in, out);
5071 if (!dims)
5072 return NULL;
5074 map = isl_map_alloc_space(dims, n, flags);
5075 return map;
5078 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5080 struct isl_basic_map *bmap;
5081 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5082 bmap = isl_basic_map_set_to_empty(bmap);
5083 return bmap;
5086 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5088 struct isl_basic_set *bset;
5089 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5090 bset = isl_basic_set_set_to_empty(bset);
5091 return bset;
5094 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5096 struct isl_basic_map *bmap;
5097 if (!model)
5098 return NULL;
5099 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5100 bmap = isl_basic_map_set_to_empty(bmap);
5101 return bmap;
5104 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5106 struct isl_basic_map *bmap;
5107 if (!model)
5108 return NULL;
5109 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5110 bmap = isl_basic_map_set_to_empty(bmap);
5111 return bmap;
5114 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5116 struct isl_basic_set *bset;
5117 if (!model)
5118 return NULL;
5119 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5120 bset = isl_basic_set_set_to_empty(bset);
5121 return bset;
5124 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5126 struct isl_basic_map *bmap;
5127 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5128 bmap = isl_basic_map_finalize(bmap);
5129 return bmap;
5132 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5134 struct isl_basic_set *bset;
5135 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5136 bset = isl_basic_set_finalize(bset);
5137 return bset;
5140 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5142 int i;
5143 unsigned total = isl_space_dim(dim, isl_dim_all);
5144 isl_basic_map *bmap;
5146 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5147 for (i = 0; i < total; ++i) {
5148 int k = isl_basic_map_alloc_inequality(bmap);
5149 if (k < 0)
5150 goto error;
5151 isl_seq_clr(bmap->ineq[k], 1 + total);
5152 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5154 return bmap;
5155 error:
5156 isl_basic_map_free(bmap);
5157 return NULL;
5160 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5162 return isl_basic_map_nat_universe(dim);
5165 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5167 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5170 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5172 return isl_map_nat_universe(dim);
5175 __isl_give isl_basic_map *isl_basic_map_universe_like(
5176 __isl_keep isl_basic_map *model)
5178 if (!model)
5179 return NULL;
5180 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5183 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5185 if (!model)
5186 return NULL;
5187 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5190 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5191 __isl_keep isl_set *model)
5193 if (!model)
5194 return NULL;
5195 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5198 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5200 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5203 struct isl_map *isl_map_empty_like(struct isl_map *model)
5205 if (!model)
5206 return NULL;
5207 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5210 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5212 if (!model)
5213 return NULL;
5214 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5217 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5219 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5222 struct isl_set *isl_set_empty_like(struct isl_set *model)
5224 if (!model)
5225 return NULL;
5226 return isl_set_empty(isl_space_copy(model->dim));
5229 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5231 struct isl_map *map;
5232 if (!dim)
5233 return NULL;
5234 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5235 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5236 return map;
5239 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5241 struct isl_set *set;
5242 if (!dim)
5243 return NULL;
5244 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5245 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5246 return set;
5249 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5251 if (!model)
5252 return NULL;
5253 return isl_set_universe(isl_space_copy(model->dim));
5256 struct isl_map *isl_map_dup(struct isl_map *map)
5258 int i;
5259 struct isl_map *dup;
5261 if (!map)
5262 return NULL;
5263 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5264 for (i = 0; i < map->n; ++i)
5265 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5266 return dup;
5269 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5270 __isl_take isl_basic_map *bmap)
5272 if (!bmap || !map)
5273 goto error;
5274 if (isl_basic_map_plain_is_empty(bmap)) {
5275 isl_basic_map_free(bmap);
5276 return map;
5278 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5279 isl_assert(map->ctx, map->n < map->size, goto error);
5280 map->p[map->n] = bmap;
5281 map->n++;
5282 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5283 return map;
5284 error:
5285 if (map)
5286 isl_map_free(map);
5287 if (bmap)
5288 isl_basic_map_free(bmap);
5289 return NULL;
5292 void *isl_map_free(struct isl_map *map)
5294 int i;
5296 if (!map)
5297 return NULL;
5299 if (--map->ref > 0)
5300 return NULL;
5302 isl_ctx_deref(map->ctx);
5303 for (i = 0; i < map->n; ++i)
5304 isl_basic_map_free(map->p[i]);
5305 isl_space_free(map->dim);
5306 free(map);
5308 return NULL;
5311 struct isl_map *isl_map_extend(struct isl_map *base,
5312 unsigned nparam, unsigned n_in, unsigned n_out)
5314 int i;
5316 base = isl_map_cow(base);
5317 if (!base)
5318 return NULL;
5320 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5321 if (!base->dim)
5322 goto error;
5323 for (i = 0; i < base->n; ++i) {
5324 base->p[i] = isl_basic_map_extend_space(base->p[i],
5325 isl_space_copy(base->dim), 0, 0, 0);
5326 if (!base->p[i])
5327 goto error;
5329 return base;
5330 error:
5331 isl_map_free(base);
5332 return NULL;
5335 struct isl_set *isl_set_extend(struct isl_set *base,
5336 unsigned nparam, unsigned dim)
5338 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5339 nparam, 0, dim);
5342 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5343 struct isl_basic_map *bmap, unsigned pos, int value)
5345 int j;
5347 bmap = isl_basic_map_cow(bmap);
5348 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5349 j = isl_basic_map_alloc_equality(bmap);
5350 if (j < 0)
5351 goto error;
5352 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5353 isl_int_set_si(bmap->eq[j][pos], -1);
5354 isl_int_set_si(bmap->eq[j][0], value);
5355 bmap = isl_basic_map_simplify(bmap);
5356 return isl_basic_map_finalize(bmap);
5357 error:
5358 isl_basic_map_free(bmap);
5359 return NULL;
5362 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5363 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5365 int j;
5367 bmap = isl_basic_map_cow(bmap);
5368 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5369 j = isl_basic_map_alloc_equality(bmap);
5370 if (j < 0)
5371 goto error;
5372 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5373 isl_int_set_si(bmap->eq[j][pos], -1);
5374 isl_int_set(bmap->eq[j][0], value);
5375 bmap = isl_basic_map_simplify(bmap);
5376 return isl_basic_map_finalize(bmap);
5377 error:
5378 isl_basic_map_free(bmap);
5379 return NULL;
5382 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5383 enum isl_dim_type type, unsigned pos, int value)
5385 if (!bmap)
5386 return NULL;
5387 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5388 return isl_basic_map_fix_pos_si(bmap,
5389 isl_basic_map_offset(bmap, type) + pos, value);
5390 error:
5391 isl_basic_map_free(bmap);
5392 return NULL;
5395 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5396 enum isl_dim_type type, unsigned pos, isl_int value)
5398 if (!bmap)
5399 return NULL;
5400 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5401 return isl_basic_map_fix_pos(bmap,
5402 isl_basic_map_offset(bmap, type) + pos, value);
5403 error:
5404 isl_basic_map_free(bmap);
5405 return NULL;
5408 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5409 * to be equal to "v".
5411 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5412 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5414 if (!bmap || !v)
5415 goto error;
5416 if (!isl_val_is_int(v))
5417 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5418 "expecting integer value", goto error);
5419 if (pos >= isl_basic_map_dim(bmap, type))
5420 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5421 "index out of bounds", goto error);
5422 pos += isl_basic_map_offset(bmap, type);
5423 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5424 isl_val_free(v);
5425 return bmap;
5426 error:
5427 isl_basic_map_free(bmap);
5428 isl_val_free(v);
5429 return NULL;
5432 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5433 * to be equal to "v".
5435 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5436 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5438 return isl_basic_map_fix_val(bset, type, pos, v);
5441 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5442 enum isl_dim_type type, unsigned pos, int value)
5444 return (struct isl_basic_set *)
5445 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5446 type, pos, value);
5449 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5450 enum isl_dim_type type, unsigned pos, isl_int value)
5452 return (struct isl_basic_set *)
5453 isl_basic_map_fix((struct isl_basic_map *)bset,
5454 type, pos, value);
5457 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5458 unsigned input, int value)
5460 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5463 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5464 unsigned dim, int value)
5466 return (struct isl_basic_set *)
5467 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5468 isl_dim_set, dim, value);
5471 static int remove_if_empty(__isl_keep isl_map *map, int i)
5473 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5475 if (empty < 0)
5476 return -1;
5477 if (!empty)
5478 return 0;
5480 isl_basic_map_free(map->p[i]);
5481 if (i != map->n - 1) {
5482 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5483 map->p[i] = map->p[map->n - 1];
5485 map->n--;
5487 return 0;
5490 /* Perform "fn" on each basic map of "map", where we may not be holding
5491 * the only reference to "map".
5492 * In particular, "fn" should be a semantics preserving operation
5493 * that we want to apply to all copies of "map". We therefore need
5494 * to be careful not to modify "map" in a way that breaks "map"
5495 * in case anything goes wrong.
5497 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5498 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5500 struct isl_basic_map *bmap;
5501 int i;
5503 if (!map)
5504 return NULL;
5506 for (i = map->n - 1; i >= 0; --i) {
5507 bmap = isl_basic_map_copy(map->p[i]);
5508 bmap = fn(bmap);
5509 if (!bmap)
5510 goto error;
5511 isl_basic_map_free(map->p[i]);
5512 map->p[i] = bmap;
5513 if (remove_if_empty(map, i) < 0)
5514 goto error;
5517 return map;
5518 error:
5519 isl_map_free(map);
5520 return NULL;
5523 struct isl_map *isl_map_fix_si(struct isl_map *map,
5524 enum isl_dim_type type, unsigned pos, int value)
5526 int i;
5528 map = isl_map_cow(map);
5529 if (!map)
5530 return NULL;
5532 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5533 for (i = map->n - 1; i >= 0; --i) {
5534 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5535 if (remove_if_empty(map, i) < 0)
5536 goto error;
5538 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5539 return map;
5540 error:
5541 isl_map_free(map);
5542 return NULL;
5545 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5546 enum isl_dim_type type, unsigned pos, int value)
5548 return (struct isl_set *)
5549 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5552 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5553 enum isl_dim_type type, unsigned pos, isl_int value)
5555 int i;
5557 map = isl_map_cow(map);
5558 if (!map)
5559 return NULL;
5561 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5562 for (i = 0; i < map->n; ++i) {
5563 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5564 if (!map->p[i])
5565 goto error;
5567 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5568 return map;
5569 error:
5570 isl_map_free(map);
5571 return NULL;
5574 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5575 enum isl_dim_type type, unsigned pos, isl_int value)
5577 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5580 /* Fix the value of the variable at position "pos" of type "type" of "map"
5581 * to be equal to "v".
5583 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5584 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5586 int i;
5588 map = isl_map_cow(map);
5589 if (!map || !v)
5590 goto error;
5592 if (!isl_val_is_int(v))
5593 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5594 "expecting integer value", goto error);
5595 if (pos >= isl_map_dim(map, type))
5596 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5597 "index out of bounds", goto error);
5598 for (i = map->n - 1; i >= 0; --i) {
5599 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5600 isl_val_copy(v));
5601 if (remove_if_empty(map, i) < 0)
5602 goto error;
5604 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5605 isl_val_free(v);
5606 return map;
5607 error:
5608 isl_map_free(map);
5609 isl_val_free(v);
5610 return NULL;
5613 /* Fix the value of the variable at position "pos" of type "type" of "set"
5614 * to be equal to "v".
5616 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5617 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5619 return isl_map_fix_val(set, type, pos, v);
5622 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5623 unsigned input, int value)
5625 return isl_map_fix_si(map, isl_dim_in, input, value);
5628 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5630 return (struct isl_set *)
5631 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5634 static __isl_give isl_basic_map *basic_map_bound_si(
5635 __isl_take isl_basic_map *bmap,
5636 enum isl_dim_type type, unsigned pos, int value, int upper)
5638 int j;
5640 if (!bmap)
5641 return NULL;
5642 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5643 pos += isl_basic_map_offset(bmap, type);
5644 bmap = isl_basic_map_cow(bmap);
5645 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5646 j = isl_basic_map_alloc_inequality(bmap);
5647 if (j < 0)
5648 goto error;
5649 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5650 if (upper) {
5651 isl_int_set_si(bmap->ineq[j][pos], -1);
5652 isl_int_set_si(bmap->ineq[j][0], value);
5653 } else {
5654 isl_int_set_si(bmap->ineq[j][pos], 1);
5655 isl_int_set_si(bmap->ineq[j][0], -value);
5657 bmap = isl_basic_map_simplify(bmap);
5658 return isl_basic_map_finalize(bmap);
5659 error:
5660 isl_basic_map_free(bmap);
5661 return NULL;
5664 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5665 __isl_take isl_basic_map *bmap,
5666 enum isl_dim_type type, unsigned pos, int value)
5668 return basic_map_bound_si(bmap, type, pos, value, 0);
5671 /* Constrain the values of the given dimension to be no greater than "value".
5673 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5674 __isl_take isl_basic_map *bmap,
5675 enum isl_dim_type type, unsigned pos, int value)
5677 return basic_map_bound_si(bmap, type, pos, value, 1);
5680 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5681 unsigned dim, isl_int value)
5683 int j;
5685 bset = isl_basic_set_cow(bset);
5686 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5687 j = isl_basic_set_alloc_inequality(bset);
5688 if (j < 0)
5689 goto error;
5690 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5691 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5692 isl_int_neg(bset->ineq[j][0], value);
5693 bset = isl_basic_set_simplify(bset);
5694 return isl_basic_set_finalize(bset);
5695 error:
5696 isl_basic_set_free(bset);
5697 return NULL;
5700 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5701 enum isl_dim_type type, unsigned pos, int value, int upper)
5703 int i;
5705 map = isl_map_cow(map);
5706 if (!map)
5707 return NULL;
5709 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5710 for (i = 0; i < map->n; ++i) {
5711 map->p[i] = basic_map_bound_si(map->p[i],
5712 type, pos, value, upper);
5713 if (!map->p[i])
5714 goto error;
5716 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5717 return map;
5718 error:
5719 isl_map_free(map);
5720 return NULL;
5723 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5724 enum isl_dim_type type, unsigned pos, int value)
5726 return map_bound_si(map, type, pos, value, 0);
5729 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5730 enum isl_dim_type type, unsigned pos, int value)
5732 return map_bound_si(map, type, pos, value, 1);
5735 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5736 enum isl_dim_type type, unsigned pos, int value)
5738 return (struct isl_set *)
5739 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5742 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5743 enum isl_dim_type type, unsigned pos, int value)
5745 return isl_map_upper_bound_si(set, type, pos, value);
5748 /* Bound the given variable of "bmap" from below (or above is "upper"
5749 * is set) to "value".
5751 static __isl_give isl_basic_map *basic_map_bound(
5752 __isl_take isl_basic_map *bmap,
5753 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5755 int j;
5757 if (!bmap)
5758 return NULL;
5759 if (pos >= isl_basic_map_dim(bmap, type))
5760 isl_die(bmap->ctx, isl_error_invalid,
5761 "index out of bounds", goto error);
5762 pos += isl_basic_map_offset(bmap, type);
5763 bmap = isl_basic_map_cow(bmap);
5764 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5765 j = isl_basic_map_alloc_inequality(bmap);
5766 if (j < 0)
5767 goto error;
5768 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5769 if (upper) {
5770 isl_int_set_si(bmap->ineq[j][pos], -1);
5771 isl_int_set(bmap->ineq[j][0], value);
5772 } else {
5773 isl_int_set_si(bmap->ineq[j][pos], 1);
5774 isl_int_neg(bmap->ineq[j][0], value);
5776 bmap = isl_basic_map_simplify(bmap);
5777 return isl_basic_map_finalize(bmap);
5778 error:
5779 isl_basic_map_free(bmap);
5780 return NULL;
5783 /* Bound the given variable of "map" from below (or above is "upper"
5784 * is set) to "value".
5786 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5787 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5789 int i;
5791 map = isl_map_cow(map);
5792 if (!map)
5793 return NULL;
5795 if (pos >= isl_map_dim(map, type))
5796 isl_die(map->ctx, isl_error_invalid,
5797 "index out of bounds", goto error);
5798 for (i = map->n - 1; i >= 0; --i) {
5799 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5800 if (remove_if_empty(map, i) < 0)
5801 goto error;
5803 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5804 return map;
5805 error:
5806 isl_map_free(map);
5807 return NULL;
5810 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5811 enum isl_dim_type type, unsigned pos, isl_int value)
5813 return map_bound(map, type, pos, value, 0);
5816 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5817 enum isl_dim_type type, unsigned pos, isl_int value)
5819 return map_bound(map, type, pos, value, 1);
5822 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5823 enum isl_dim_type type, unsigned pos, isl_int value)
5825 return isl_map_lower_bound(set, type, pos, value);
5828 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5829 enum isl_dim_type type, unsigned pos, isl_int value)
5831 return isl_map_upper_bound(set, type, pos, value);
5834 /* Force the values of the variable at position "pos" of type "type" of "set"
5835 * to be no smaller than "value".
5837 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5838 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5840 if (!value)
5841 goto error;
5842 if (!isl_val_is_int(value))
5843 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5844 "expecting integer value", goto error);
5845 set = isl_set_lower_bound(set, type, pos, value->n);
5846 isl_val_free(value);
5847 return set;
5848 error:
5849 isl_val_free(value);
5850 isl_set_free(set);
5851 return NULL;
5854 /* Force the values of the variable at position "pos" of type "type" of "set"
5855 * to be no greater than "value".
5857 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5858 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5860 if (!value)
5861 goto error;
5862 if (!isl_val_is_int(value))
5863 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5864 "expecting integer value", goto error);
5865 set = isl_set_upper_bound(set, type, pos, value->n);
5866 isl_val_free(value);
5867 return set;
5868 error:
5869 isl_val_free(value);
5870 isl_set_free(set);
5871 return NULL;
5874 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5875 isl_int value)
5877 int i;
5879 set = isl_set_cow(set);
5880 if (!set)
5881 return NULL;
5883 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5884 for (i = 0; i < set->n; ++i) {
5885 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5886 if (!set->p[i])
5887 goto error;
5889 return set;
5890 error:
5891 isl_set_free(set);
5892 return NULL;
5895 struct isl_map *isl_map_reverse(struct isl_map *map)
5897 int i;
5899 map = isl_map_cow(map);
5900 if (!map)
5901 return NULL;
5903 map->dim = isl_space_reverse(map->dim);
5904 if (!map->dim)
5905 goto error;
5906 for (i = 0; i < map->n; ++i) {
5907 map->p[i] = isl_basic_map_reverse(map->p[i]);
5908 if (!map->p[i])
5909 goto error;
5911 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5912 return map;
5913 error:
5914 isl_map_free(map);
5915 return NULL;
5918 static struct isl_map *isl_basic_map_partial_lexopt(
5919 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5920 struct isl_set **empty, int max)
5922 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5925 struct isl_map *isl_basic_map_partial_lexmax(
5926 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5927 struct isl_set **empty)
5929 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5932 struct isl_map *isl_basic_map_partial_lexmin(
5933 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5934 struct isl_set **empty)
5936 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5939 struct isl_set *isl_basic_set_partial_lexmin(
5940 struct isl_basic_set *bset, struct isl_basic_set *dom,
5941 struct isl_set **empty)
5943 return (struct isl_set *)
5944 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5945 dom, empty);
5948 struct isl_set *isl_basic_set_partial_lexmax(
5949 struct isl_basic_set *bset, struct isl_basic_set *dom,
5950 struct isl_set **empty)
5952 return (struct isl_set *)
5953 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5954 dom, empty);
5957 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5958 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5959 __isl_give isl_set **empty)
5961 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5964 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5965 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5966 __isl_give isl_set **empty)
5968 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5971 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5972 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5973 __isl_give isl_set **empty)
5975 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5978 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5979 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5980 __isl_give isl_set **empty)
5982 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5985 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5986 __isl_take isl_basic_map *bmap, int max)
5988 isl_basic_set *dom = NULL;
5989 isl_space *dom_space;
5991 if (!bmap)
5992 goto error;
5993 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5994 dom = isl_basic_set_universe(dom_space);
5995 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5996 error:
5997 isl_basic_map_free(bmap);
5998 return NULL;
6001 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6002 __isl_take isl_basic_map *bmap)
6004 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6007 #undef TYPE
6008 #define TYPE isl_pw_multi_aff
6009 #undef SUFFIX
6010 #define SUFFIX _pw_multi_aff
6011 #undef EMPTY
6012 #define EMPTY isl_pw_multi_aff_empty
6013 #undef ADD
6014 #define ADD isl_pw_multi_aff_union_add
6015 #include "isl_map_lexopt_templ.c"
6017 /* Given a map "map", compute the lexicographically minimal
6018 * (or maximal) image element for each domain element in dom,
6019 * in the form of an isl_pw_multi_aff.
6020 * Set *empty to those elements in dom that do not have an image element.
6022 * We first compute the lexicographically minimal or maximal element
6023 * in the first basic map. This results in a partial solution "res"
6024 * and a subset "todo" of dom that still need to be handled.
6025 * We then consider each of the remaining maps in "map" and successively
6026 * update both "res" and "todo".
6028 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6029 __isl_take isl_map *map, __isl_take isl_set *dom,
6030 __isl_give isl_set **empty, int max)
6032 int i;
6033 isl_pw_multi_aff *res;
6034 isl_set *todo;
6036 if (!map || !dom)
6037 goto error;
6039 if (isl_map_plain_is_empty(map)) {
6040 if (empty)
6041 *empty = dom;
6042 else
6043 isl_set_free(dom);
6044 return isl_pw_multi_aff_from_map(map);
6047 res = basic_map_partial_lexopt_pw_multi_aff(
6048 isl_basic_map_copy(map->p[0]),
6049 isl_set_copy(dom), &todo, max);
6051 for (i = 1; i < map->n; ++i) {
6052 isl_pw_multi_aff *res_i;
6053 isl_set *todo_i;
6055 res_i = basic_map_partial_lexopt_pw_multi_aff(
6056 isl_basic_map_copy(map->p[i]),
6057 isl_set_copy(dom), &todo_i, max);
6059 if (max)
6060 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6061 else
6062 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6064 todo = isl_set_intersect(todo, todo_i);
6067 isl_set_free(dom);
6068 isl_map_free(map);
6070 if (empty)
6071 *empty = todo;
6072 else
6073 isl_set_free(todo);
6075 return res;
6076 error:
6077 if (empty)
6078 *empty = NULL;
6079 isl_set_free(dom);
6080 isl_map_free(map);
6081 return NULL;
6084 #undef TYPE
6085 #define TYPE isl_map
6086 #undef SUFFIX
6087 #define SUFFIX
6088 #undef EMPTY
6089 #define EMPTY isl_map_empty
6090 #undef ADD
6091 #define ADD isl_map_union_disjoint
6092 #include "isl_map_lexopt_templ.c"
6094 /* Given a map "map", compute the lexicographically minimal
6095 * (or maximal) image element for each domain element in dom.
6096 * Set *empty to those elements in dom that do not have an image element.
6098 * We first compute the lexicographically minimal or maximal element
6099 * in the first basic map. This results in a partial solution "res"
6100 * and a subset "todo" of dom that still need to be handled.
6101 * We then consider each of the remaining maps in "map" and successively
6102 * update both "res" and "todo".
6104 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6105 * Assume we are computing the lexicographical maximum.
6106 * We first compute the lexicographically maximal element in basic map i.
6107 * This results in a partial solution res_i and a subset todo_i.
6108 * Then we combine these results with those obtain for the first k basic maps
6109 * to obtain a result that is valid for the first k+1 basic maps.
6110 * In particular, the set where there is no solution is the set where
6111 * there is no solution for the first k basic maps and also no solution
6112 * for the ith basic map, i.e.,
6114 * todo^i = todo^k * todo_i
6116 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6117 * solutions, arbitrarily breaking ties in favor of res^k.
6118 * That is, when res^k(a) >= res_i(a), we pick res^k and
6119 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6120 * the lexicographic order.)
6121 * In practice, we compute
6123 * res^k * (res_i . "<=")
6125 * and
6127 * res_i * (res^k . "<")
6129 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6130 * where only one of res^k and res_i provides a solution and we simply pick
6131 * that one, i.e.,
6133 * res^k * todo_i
6134 * and
6135 * res_i * todo^k
6137 * Note that we only compute these intersections when dom(res^k) intersects
6138 * dom(res_i). Otherwise, the only effect of these intersections is to
6139 * potentially break up res^k and res_i into smaller pieces.
6140 * We want to avoid such splintering as much as possible.
6141 * In fact, an earlier implementation of this function would look for
6142 * better results in the domain of res^k and for extra results in todo^k,
6143 * but this would always result in a splintering according to todo^k,
6144 * even when the domain of basic map i is disjoint from the domains of
6145 * the previous basic maps.
6147 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6148 __isl_take isl_map *map, __isl_take isl_set *dom,
6149 __isl_give isl_set **empty, int max)
6151 int i;
6152 struct isl_map *res;
6153 struct isl_set *todo;
6155 if (!map || !dom)
6156 goto error;
6158 if (isl_map_plain_is_empty(map)) {
6159 if (empty)
6160 *empty = dom;
6161 else
6162 isl_set_free(dom);
6163 return map;
6166 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6167 isl_set_copy(dom), &todo, max);
6169 for (i = 1; i < map->n; ++i) {
6170 isl_map *lt, *le;
6171 isl_map *res_i;
6172 isl_set *todo_i;
6173 isl_space *dim = isl_space_range(isl_map_get_space(res));
6175 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6176 isl_set_copy(dom), &todo_i, max);
6178 if (max) {
6179 lt = isl_map_lex_lt(isl_space_copy(dim));
6180 le = isl_map_lex_le(dim);
6181 } else {
6182 lt = isl_map_lex_gt(isl_space_copy(dim));
6183 le = isl_map_lex_ge(dim);
6185 lt = isl_map_apply_range(isl_map_copy(res), lt);
6186 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6187 le = isl_map_apply_range(isl_map_copy(res_i), le);
6188 le = isl_map_intersect(le, isl_map_copy(res));
6190 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6191 res = isl_map_intersect_domain(res,
6192 isl_set_copy(todo_i));
6193 res_i = isl_map_intersect_domain(res_i,
6194 isl_set_copy(todo));
6197 res = isl_map_union_disjoint(res, res_i);
6198 res = isl_map_union_disjoint(res, lt);
6199 res = isl_map_union_disjoint(res, le);
6201 todo = isl_set_intersect(todo, todo_i);
6204 isl_set_free(dom);
6205 isl_map_free(map);
6207 if (empty)
6208 *empty = todo;
6209 else
6210 isl_set_free(todo);
6212 return res;
6213 error:
6214 if (empty)
6215 *empty = NULL;
6216 isl_set_free(dom);
6217 isl_map_free(map);
6218 return NULL;
6221 __isl_give isl_map *isl_map_partial_lexmax(
6222 __isl_take isl_map *map, __isl_take isl_set *dom,
6223 __isl_give isl_set **empty)
6225 return isl_map_partial_lexopt(map, dom, empty, 1);
6228 __isl_give isl_map *isl_map_partial_lexmin(
6229 __isl_take isl_map *map, __isl_take isl_set *dom,
6230 __isl_give isl_set **empty)
6232 return isl_map_partial_lexopt(map, dom, empty, 0);
6235 __isl_give isl_set *isl_set_partial_lexmin(
6236 __isl_take isl_set *set, __isl_take isl_set *dom,
6237 __isl_give isl_set **empty)
6239 return (struct isl_set *)
6240 isl_map_partial_lexmin((struct isl_map *)set,
6241 dom, empty);
6244 __isl_give isl_set *isl_set_partial_lexmax(
6245 __isl_take isl_set *set, __isl_take isl_set *dom,
6246 __isl_give isl_set **empty)
6248 return (struct isl_set *)
6249 isl_map_partial_lexmax((struct isl_map *)set,
6250 dom, empty);
6253 /* Compute the lexicographic minimum (or maximum if "max" is set)
6254 * of "bmap" over its domain.
6256 * Since we are not interested in the part of the domain space where
6257 * there is no solution, we initialize the domain to those constraints
6258 * of "bmap" that only involve the parameters and the input dimensions.
6259 * This relieves the parametric programming engine from detecting those
6260 * inequalities and transferring them to the context. More importantly,
6261 * it ensures that those inequalities are transferred first and not
6262 * intermixed with inequalities that actually split the domain.
6264 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6266 int n_div;
6267 int n_out;
6268 isl_basic_map *copy;
6269 isl_basic_set *dom;
6271 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6272 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6273 copy = isl_basic_map_copy(bmap);
6274 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6275 isl_dim_div, 0, n_div);
6276 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6277 isl_dim_out, 0, n_out);
6278 dom = isl_basic_map_domain(copy);
6279 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6282 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6284 return isl_basic_map_lexopt(bmap, 0);
6287 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6289 return isl_basic_map_lexopt(bmap, 1);
6292 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6294 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6297 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6299 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6302 /* Extract the first and only affine expression from list
6303 * and then add it to *pwaff with the given dom.
6304 * This domain is known to be disjoint from other domains
6305 * because of the way isl_basic_map_foreach_lexmax works.
6307 static int update_dim_opt(__isl_take isl_basic_set *dom,
6308 __isl_take isl_aff_list *list, void *user)
6310 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6311 isl_aff *aff;
6312 isl_pw_aff **pwaff = user;
6313 isl_pw_aff *pwaff_i;
6315 if (!list)
6316 goto error;
6317 if (isl_aff_list_n_aff(list) != 1)
6318 isl_die(ctx, isl_error_internal,
6319 "expecting single element list", goto error);
6321 aff = isl_aff_list_get_aff(list, 0);
6322 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6324 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6326 isl_aff_list_free(list);
6328 return 0;
6329 error:
6330 isl_basic_set_free(dom);
6331 isl_aff_list_free(list);
6332 return -1;
6335 /* Given a basic map with one output dimension, compute the minimum or
6336 * maximum of that dimension as an isl_pw_aff.
6338 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6339 * call update_dim_opt on each leaf of the result.
6341 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6342 int max)
6344 isl_space *dim = isl_basic_map_get_space(bmap);
6345 isl_pw_aff *pwaff;
6346 int r;
6348 dim = isl_space_from_domain(isl_space_domain(dim));
6349 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6350 pwaff = isl_pw_aff_empty(dim);
6352 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6353 if (r < 0)
6354 return isl_pw_aff_free(pwaff);
6356 return pwaff;
6359 /* Compute the minimum or maximum of the given output dimension
6360 * as a function of the parameters and the input dimensions,
6361 * but independently of the other output dimensions.
6363 * We first project out the other output dimension and then compute
6364 * the "lexicographic" maximum in each basic map, combining the results
6365 * using isl_pw_aff_union_max.
6367 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6368 int max)
6370 int i;
6371 isl_pw_aff *pwaff;
6372 unsigned n_out;
6374 n_out = isl_map_dim(map, isl_dim_out);
6375 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6376 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6377 if (!map)
6378 return NULL;
6380 if (map->n == 0) {
6381 isl_space *dim = isl_map_get_space(map);
6382 dim = isl_space_domain(isl_space_from_range(dim));
6383 isl_map_free(map);
6384 return isl_pw_aff_empty(dim);
6387 pwaff = basic_map_dim_opt(map->p[0], max);
6388 for (i = 1; i < map->n; ++i) {
6389 isl_pw_aff *pwaff_i;
6391 pwaff_i = basic_map_dim_opt(map->p[i], max);
6392 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6395 isl_map_free(map);
6397 return pwaff;
6400 /* Compute the maximum of the given output dimension as a function of the
6401 * parameters and input dimensions, but independently of
6402 * the other output dimensions.
6404 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6406 return map_dim_opt(map, pos, 1);
6409 /* Compute the minimum or maximum of the given set dimension
6410 * as a function of the parameters,
6411 * but independently of the other set dimensions.
6413 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6414 int max)
6416 return map_dim_opt(set, pos, max);
6419 /* Compute the maximum of the given set dimension as a function of the
6420 * parameters, but independently of the other set dimensions.
6422 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6424 return set_dim_opt(set, pos, 1);
6427 /* Compute the minimum of the given set dimension as a function of the
6428 * parameters, but independently of the other set dimensions.
6430 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6432 return set_dim_opt(set, pos, 0);
6435 /* Apply a preimage specified by "mat" on the parameters of "bset".
6436 * bset is assumed to have only parameters and divs.
6438 static struct isl_basic_set *basic_set_parameter_preimage(
6439 struct isl_basic_set *bset, struct isl_mat *mat)
6441 unsigned nparam;
6443 if (!bset || !mat)
6444 goto error;
6446 bset->dim = isl_space_cow(bset->dim);
6447 if (!bset->dim)
6448 goto error;
6450 nparam = isl_basic_set_dim(bset, isl_dim_param);
6452 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6454 bset->dim->nparam = 0;
6455 bset->dim->n_out = nparam;
6456 bset = isl_basic_set_preimage(bset, mat);
6457 if (bset) {
6458 bset->dim->nparam = bset->dim->n_out;
6459 bset->dim->n_out = 0;
6461 return bset;
6462 error:
6463 isl_mat_free(mat);
6464 isl_basic_set_free(bset);
6465 return NULL;
6468 /* Apply a preimage specified by "mat" on the parameters of "set".
6469 * set is assumed to have only parameters and divs.
6471 static struct isl_set *set_parameter_preimage(
6472 struct isl_set *set, struct isl_mat *mat)
6474 isl_space *dim = NULL;
6475 unsigned nparam;
6477 if (!set || !mat)
6478 goto error;
6480 dim = isl_space_copy(set->dim);
6481 dim = isl_space_cow(dim);
6482 if (!dim)
6483 goto error;
6485 nparam = isl_set_dim(set, isl_dim_param);
6487 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6489 dim->nparam = 0;
6490 dim->n_out = nparam;
6491 isl_set_reset_space(set, dim);
6492 set = isl_set_preimage(set, mat);
6493 if (!set)
6494 goto error2;
6495 dim = isl_space_copy(set->dim);
6496 dim = isl_space_cow(dim);
6497 if (!dim)
6498 goto error2;
6499 dim->nparam = dim->n_out;
6500 dim->n_out = 0;
6501 isl_set_reset_space(set, dim);
6502 return set;
6503 error:
6504 isl_space_free(dim);
6505 isl_mat_free(mat);
6506 error2:
6507 isl_set_free(set);
6508 return NULL;
6511 /* Intersect the basic set "bset" with the affine space specified by the
6512 * equalities in "eq".
6514 static struct isl_basic_set *basic_set_append_equalities(
6515 struct isl_basic_set *bset, struct isl_mat *eq)
6517 int i, k;
6518 unsigned len;
6520 if (!bset || !eq)
6521 goto error;
6523 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6524 eq->n_row, 0);
6525 if (!bset)
6526 goto error;
6528 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6529 for (i = 0; i < eq->n_row; ++i) {
6530 k = isl_basic_set_alloc_equality(bset);
6531 if (k < 0)
6532 goto error;
6533 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6534 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6536 isl_mat_free(eq);
6538 bset = isl_basic_set_gauss(bset, NULL);
6539 bset = isl_basic_set_finalize(bset);
6541 return bset;
6542 error:
6543 isl_mat_free(eq);
6544 isl_basic_set_free(bset);
6545 return NULL;
6548 /* Intersect the set "set" with the affine space specified by the
6549 * equalities in "eq".
6551 static struct isl_set *set_append_equalities(struct isl_set *set,
6552 struct isl_mat *eq)
6554 int i;
6556 if (!set || !eq)
6557 goto error;
6559 for (i = 0; i < set->n; ++i) {
6560 set->p[i] = basic_set_append_equalities(set->p[i],
6561 isl_mat_copy(eq));
6562 if (!set->p[i])
6563 goto error;
6565 isl_mat_free(eq);
6566 return set;
6567 error:
6568 isl_mat_free(eq);
6569 isl_set_free(set);
6570 return NULL;
6573 /* Given a basic set "bset" that only involves parameters and existentially
6574 * quantified variables, return the index of the first equality
6575 * that only involves parameters. If there is no such equality then
6576 * return bset->n_eq.
6578 * This function assumes that isl_basic_set_gauss has been called on "bset".
6580 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6582 int i, j;
6583 unsigned nparam, n_div;
6585 if (!bset)
6586 return -1;
6588 nparam = isl_basic_set_dim(bset, isl_dim_param);
6589 n_div = isl_basic_set_dim(bset, isl_dim_div);
6591 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6592 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6593 ++i;
6596 return i;
6599 /* Compute an explicit representation for the existentially quantified
6600 * variables in "bset" by computing the "minimal value" of the set
6601 * variables. Since there are no set variables, the computation of
6602 * the minimal value essentially computes an explicit representation
6603 * of the non-empty part(s) of "bset".
6605 * The input only involves parameters and existentially quantified variables.
6606 * All equalities among parameters have been removed.
6608 * Since the existentially quantified variables in the result are in general
6609 * going to be different from those in the input, we first replace
6610 * them by the minimal number of variables based on their equalities.
6611 * This should simplify the parametric integer programming.
6613 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6615 isl_morph *morph1, *morph2;
6616 isl_set *set;
6617 unsigned n;
6619 if (!bset)
6620 return NULL;
6621 if (bset->n_eq == 0)
6622 return isl_basic_set_lexmin(bset);
6624 morph1 = isl_basic_set_parameter_compression(bset);
6625 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6626 bset = isl_basic_set_lift(bset);
6627 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6628 bset = isl_morph_basic_set(morph2, bset);
6629 n = isl_basic_set_dim(bset, isl_dim_set);
6630 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6632 set = isl_basic_set_lexmin(bset);
6634 set = isl_morph_set(isl_morph_inverse(morph1), set);
6636 return set;
6639 /* Project the given basic set onto its parameter domain, possibly introducing
6640 * new, explicit, existential variables in the constraints.
6641 * The input has parameters and (possibly implicit) existential variables.
6642 * The output has the same parameters, but only
6643 * explicit existentially quantified variables.
6645 * The actual projection is performed by pip, but pip doesn't seem
6646 * to like equalities very much, so we first remove the equalities
6647 * among the parameters by performing a variable compression on
6648 * the parameters. Afterward, an inverse transformation is performed
6649 * and the equalities among the parameters are inserted back in.
6651 * The variable compression on the parameters may uncover additional
6652 * equalities that were only implicit before. We therefore check
6653 * if there are any new parameter equalities in the result and
6654 * if so recurse. The removal of parameter equalities is required
6655 * for the parameter compression performed by base_compute_divs.
6657 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6659 int i;
6660 struct isl_mat *eq;
6661 struct isl_mat *T, *T2;
6662 struct isl_set *set;
6663 unsigned nparam;
6665 bset = isl_basic_set_cow(bset);
6666 if (!bset)
6667 return NULL;
6669 if (bset->n_eq == 0)
6670 return base_compute_divs(bset);
6672 bset = isl_basic_set_gauss(bset, NULL);
6673 if (!bset)
6674 return NULL;
6675 if (isl_basic_set_plain_is_empty(bset))
6676 return isl_set_from_basic_set(bset);
6678 i = first_parameter_equality(bset);
6679 if (i == bset->n_eq)
6680 return base_compute_divs(bset);
6682 nparam = isl_basic_set_dim(bset, isl_dim_param);
6683 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6684 0, 1 + nparam);
6685 eq = isl_mat_cow(eq);
6686 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6687 if (T && T->n_col == 0) {
6688 isl_mat_free(T);
6689 isl_mat_free(T2);
6690 isl_mat_free(eq);
6691 bset = isl_basic_set_set_to_empty(bset);
6692 return isl_set_from_basic_set(bset);
6694 bset = basic_set_parameter_preimage(bset, T);
6696 i = first_parameter_equality(bset);
6697 if (!bset)
6698 set = NULL;
6699 else if (i == bset->n_eq)
6700 set = base_compute_divs(bset);
6701 else
6702 set = parameter_compute_divs(bset);
6703 set = set_parameter_preimage(set, T2);
6704 set = set_append_equalities(set, eq);
6705 return set;
6708 /* Insert the divs from "ls" before those of "bmap".
6710 * The number of columns is not changed, which means that the last
6711 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6712 * The caller is responsible for removing the same number of dimensions
6713 * from the space of "bmap".
6715 static __isl_give isl_basic_map *insert_divs_from_local_space(
6716 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6718 int i;
6719 int n_div;
6720 int old_n_div;
6722 n_div = isl_local_space_dim(ls, isl_dim_div);
6723 if (n_div == 0)
6724 return bmap;
6726 old_n_div = bmap->n_div;
6727 bmap = insert_div_rows(bmap, n_div);
6728 if (!bmap)
6729 return NULL;
6731 for (i = 0; i < n_div; ++i) {
6732 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6733 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6736 return bmap;
6739 /* Replace the space of "bmap" by the space and divs of "ls".
6741 * If "ls" has any divs, then we simplify the result since we may
6742 * have discovered some additional equalities that could simplify
6743 * the div expressions.
6745 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6746 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6748 int n_div;
6750 bmap = isl_basic_map_cow(bmap);
6751 if (!bmap || !ls)
6752 goto error;
6754 n_div = isl_local_space_dim(ls, isl_dim_div);
6755 bmap = insert_divs_from_local_space(bmap, ls);
6756 if (!bmap)
6757 goto error;
6759 isl_space_free(bmap->dim);
6760 bmap->dim = isl_local_space_get_space(ls);
6761 if (!bmap->dim)
6762 goto error;
6764 isl_local_space_free(ls);
6765 if (n_div > 0)
6766 bmap = isl_basic_map_simplify(bmap);
6767 bmap = isl_basic_map_finalize(bmap);
6768 return bmap;
6769 error:
6770 isl_basic_map_free(bmap);
6771 isl_local_space_free(ls);
6772 return NULL;
6775 /* Replace the space of "map" by the space and divs of "ls".
6777 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6778 __isl_take isl_local_space *ls)
6780 int i;
6782 map = isl_map_cow(map);
6783 if (!map || !ls)
6784 goto error;
6786 for (i = 0; i < map->n; ++i) {
6787 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6788 isl_local_space_copy(ls));
6789 if (!map->p[i])
6790 goto error;
6792 isl_space_free(map->dim);
6793 map->dim = isl_local_space_get_space(ls);
6794 if (!map->dim)
6795 goto error;
6797 isl_local_space_free(ls);
6798 return map;
6799 error:
6800 isl_local_space_free(ls);
6801 isl_map_free(map);
6802 return NULL;
6805 /* Compute an explicit representation for the existentially
6806 * quantified variables for which do not know any explicit representation yet.
6808 * We first sort the existentially quantified variables so that the
6809 * existentially quantified variables for which we already have an explicit
6810 * representation are placed before those for which we do not.
6811 * The input dimensions, the output dimensions and the existentially
6812 * quantified variables for which we already have an explicit
6813 * representation are then turned into parameters.
6814 * compute_divs returns a map with the same parameters and
6815 * no input or output dimensions and the dimension specification
6816 * is reset to that of the input, including the existentially quantified
6817 * variables for which we already had an explicit representation.
6819 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6821 struct isl_basic_set *bset;
6822 struct isl_set *set;
6823 struct isl_map *map;
6824 isl_space *dim;
6825 isl_local_space *ls;
6826 unsigned nparam;
6827 unsigned n_in;
6828 unsigned n_out;
6829 unsigned n_known;
6830 int i;
6832 bmap = isl_basic_map_sort_divs(bmap);
6833 bmap = isl_basic_map_cow(bmap);
6834 if (!bmap)
6835 return NULL;
6837 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6838 if (isl_int_is_zero(bmap->div[n_known][0]))
6839 break;
6841 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6842 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6843 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6844 dim = isl_space_set_alloc(bmap->ctx,
6845 nparam + n_in + n_out + n_known, 0);
6846 if (!dim)
6847 goto error;
6849 ls = isl_basic_map_get_local_space(bmap);
6850 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6851 n_known, bmap->n_div - n_known);
6852 if (n_known > 0) {
6853 for (i = n_known; i < bmap->n_div; ++i)
6854 swap_div(bmap, i - n_known, i);
6855 bmap->n_div -= n_known;
6856 bmap->extra -= n_known;
6858 bmap = isl_basic_map_reset_space(bmap, dim);
6859 bset = (struct isl_basic_set *)bmap;
6861 set = parameter_compute_divs(bset);
6862 map = (struct isl_map *)set;
6863 map = replace_space_by_local_space(map, ls);
6865 return map;
6866 error:
6867 isl_basic_map_free(bmap);
6868 return NULL;
6871 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6873 int i;
6874 unsigned off;
6876 if (!bmap)
6877 return -1;
6879 off = isl_space_dim(bmap->dim, isl_dim_all);
6880 for (i = 0; i < bmap->n_div; ++i) {
6881 if (isl_int_is_zero(bmap->div[i][0]))
6882 return 0;
6883 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6884 return -1);
6886 return 1;
6889 static int map_divs_known(__isl_keep isl_map *map)
6891 int i;
6893 if (!map)
6894 return -1;
6896 for (i = 0; i < map->n; ++i) {
6897 int known = isl_basic_map_divs_known(map->p[i]);
6898 if (known <= 0)
6899 return known;
6902 return 1;
6905 /* If bmap contains any unknown divs, then compute explicit
6906 * expressions for them. However, this computation may be
6907 * quite expensive, so first try to remove divs that aren't
6908 * strictly needed.
6910 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6912 int known;
6913 struct isl_map *map;
6915 known = isl_basic_map_divs_known(bmap);
6916 if (known < 0)
6917 goto error;
6918 if (known)
6919 return isl_map_from_basic_map(bmap);
6921 bmap = isl_basic_map_drop_redundant_divs(bmap);
6923 known = isl_basic_map_divs_known(bmap);
6924 if (known < 0)
6925 goto error;
6926 if (known)
6927 return isl_map_from_basic_map(bmap);
6929 map = compute_divs(bmap);
6930 return map;
6931 error:
6932 isl_basic_map_free(bmap);
6933 return NULL;
6936 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6938 int i;
6939 int known;
6940 struct isl_map *res;
6942 if (!map)
6943 return NULL;
6944 if (map->n == 0)
6945 return map;
6947 known = map_divs_known(map);
6948 if (known < 0) {
6949 isl_map_free(map);
6950 return NULL;
6952 if (known)
6953 return map;
6955 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6956 for (i = 1 ; i < map->n; ++i) {
6957 struct isl_map *r2;
6958 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6959 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6960 res = isl_map_union_disjoint(res, r2);
6961 else
6962 res = isl_map_union(res, r2);
6964 isl_map_free(map);
6966 return res;
6969 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6971 return (struct isl_set *)
6972 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6975 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6977 return (struct isl_set *)
6978 isl_map_compute_divs((struct isl_map *)set);
6981 struct isl_set *isl_map_domain(struct isl_map *map)
6983 int i;
6984 struct isl_set *set;
6986 if (!map)
6987 goto error;
6989 map = isl_map_cow(map);
6990 if (!map)
6991 return NULL;
6993 set = (struct isl_set *)map;
6994 set->dim = isl_space_domain(set->dim);
6995 if (!set->dim)
6996 goto error;
6997 for (i = 0; i < map->n; ++i) {
6998 set->p[i] = isl_basic_map_domain(map->p[i]);
6999 if (!set->p[i])
7000 goto error;
7002 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7003 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7004 return set;
7005 error:
7006 isl_map_free(map);
7007 return NULL;
7010 /* Return the union of "map1" and "map2", where we assume for now that
7011 * "map1" and "map2" are disjoint. Note that the basic maps inside
7012 * "map1" or "map2" may not be disjoint from each other.
7013 * Also note that this function is also called from isl_map_union,
7014 * which takes care of handling the situation where "map1" and "map2"
7015 * may not be disjoint.
7017 * If one of the inputs is empty, we can simply return the other input.
7018 * Similarly, if one of the inputs is universal, then it is equal to the union.
7020 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7021 __isl_take isl_map *map2)
7023 int i;
7024 unsigned flags = 0;
7025 struct isl_map *map = NULL;
7026 int is_universe;
7028 if (!map1 || !map2)
7029 goto error;
7031 if (map1->n == 0) {
7032 isl_map_free(map1);
7033 return map2;
7035 if (map2->n == 0) {
7036 isl_map_free(map2);
7037 return map1;
7040 is_universe = isl_map_plain_is_universe(map1);
7041 if (is_universe < 0)
7042 goto error;
7043 if (is_universe) {
7044 isl_map_free(map2);
7045 return map1;
7048 is_universe = isl_map_plain_is_universe(map2);
7049 if (is_universe < 0)
7050 goto error;
7051 if (is_universe) {
7052 isl_map_free(map1);
7053 return map2;
7056 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
7058 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7059 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7060 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7062 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7063 map1->n + map2->n, flags);
7064 if (!map)
7065 goto error;
7066 for (i = 0; i < map1->n; ++i) {
7067 map = isl_map_add_basic_map(map,
7068 isl_basic_map_copy(map1->p[i]));
7069 if (!map)
7070 goto error;
7072 for (i = 0; i < map2->n; ++i) {
7073 map = isl_map_add_basic_map(map,
7074 isl_basic_map_copy(map2->p[i]));
7075 if (!map)
7076 goto error;
7078 isl_map_free(map1);
7079 isl_map_free(map2);
7080 return map;
7081 error:
7082 isl_map_free(map);
7083 isl_map_free(map1);
7084 isl_map_free(map2);
7085 return NULL;
7088 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7089 __isl_take isl_map *map2)
7091 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7094 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
7096 map1 = isl_map_union_disjoint(map1, map2);
7097 if (!map1)
7098 return NULL;
7099 if (map1->n > 1)
7100 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7101 return map1;
7104 struct isl_set *isl_set_union_disjoint(
7105 struct isl_set *set1, struct isl_set *set2)
7107 return (struct isl_set *)
7108 isl_map_union_disjoint(
7109 (struct isl_map *)set1, (struct isl_map *)set2);
7112 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7114 return (struct isl_set *)
7115 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7118 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7119 * the results.
7121 * "map" and "set" are assumed to be compatible and non-NULL.
7123 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7124 __isl_take isl_set *set,
7125 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7126 __isl_take isl_basic_set *bset))
7128 unsigned flags = 0;
7129 struct isl_map *result;
7130 int i, j;
7132 if (isl_set_plain_is_universe(set)) {
7133 isl_set_free(set);
7134 return map;
7137 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7138 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7139 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7141 result = isl_map_alloc_space(isl_space_copy(map->dim),
7142 map->n * set->n, flags);
7143 for (i = 0; result && i < map->n; ++i)
7144 for (j = 0; j < set->n; ++j) {
7145 result = isl_map_add_basic_map(result,
7146 fn(isl_basic_map_copy(map->p[i]),
7147 isl_basic_set_copy(set->p[j])));
7148 if (!result)
7149 break;
7152 isl_map_free(map);
7153 isl_set_free(set);
7154 return result;
7157 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7158 __isl_take isl_set *set)
7160 if (!map || !set)
7161 goto error;
7163 if (!isl_map_compatible_range(map, set))
7164 isl_die(set->ctx, isl_error_invalid,
7165 "incompatible spaces", goto error);
7167 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7168 error:
7169 isl_map_free(map);
7170 isl_set_free(set);
7171 return NULL;
7174 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7175 __isl_take isl_set *set)
7177 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7180 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7181 __isl_take isl_set *set)
7183 if (!map || !set)
7184 goto error;
7186 if (!isl_map_compatible_domain(map, set))
7187 isl_die(set->ctx, isl_error_invalid,
7188 "incompatible spaces", goto error);
7190 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7191 error:
7192 isl_map_free(map);
7193 isl_set_free(set);
7194 return NULL;
7197 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7198 __isl_take isl_set *set)
7200 return isl_map_align_params_map_map_and(map, set,
7201 &map_intersect_domain);
7204 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7205 __isl_take isl_map *map2)
7207 if (!map1 || !map2)
7208 goto error;
7209 map1 = isl_map_reverse(map1);
7210 map1 = isl_map_apply_range(map1, map2);
7211 return isl_map_reverse(map1);
7212 error:
7213 isl_map_free(map1);
7214 isl_map_free(map2);
7215 return NULL;
7218 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7219 __isl_take isl_map *map2)
7221 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7224 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7225 __isl_take isl_map *map2)
7227 isl_space *dim_result;
7228 struct isl_map *result;
7229 int i, j;
7231 if (!map1 || !map2)
7232 goto error;
7234 dim_result = isl_space_join(isl_space_copy(map1->dim),
7235 isl_space_copy(map2->dim));
7237 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7238 if (!result)
7239 goto error;
7240 for (i = 0; i < map1->n; ++i)
7241 for (j = 0; j < map2->n; ++j) {
7242 result = isl_map_add_basic_map(result,
7243 isl_basic_map_apply_range(
7244 isl_basic_map_copy(map1->p[i]),
7245 isl_basic_map_copy(map2->p[j])));
7246 if (!result)
7247 goto error;
7249 isl_map_free(map1);
7250 isl_map_free(map2);
7251 if (result && result->n <= 1)
7252 ISL_F_SET(result, ISL_MAP_DISJOINT);
7253 return result;
7254 error:
7255 isl_map_free(map1);
7256 isl_map_free(map2);
7257 return NULL;
7260 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7261 __isl_take isl_map *map2)
7263 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7267 * returns range - domain
7269 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7271 isl_space *dims, *target_dim;
7272 struct isl_basic_set *bset;
7273 unsigned dim;
7274 unsigned nparam;
7275 int i;
7277 if (!bmap)
7278 goto error;
7279 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
7280 bmap->dim, isl_dim_out),
7281 goto error);
7282 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7283 dim = isl_basic_map_n_in(bmap);
7284 nparam = isl_basic_map_n_param(bmap);
7285 bset = isl_basic_set_from_basic_map(bmap);
7286 bset = isl_basic_set_cow(bset);
7287 dims = isl_basic_set_get_space(bset);
7288 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7289 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7290 bset = isl_basic_set_swap_vars(bset, 2*dim);
7291 for (i = 0; i < dim; ++i) {
7292 int j = isl_basic_map_alloc_equality(
7293 (struct isl_basic_map *)bset);
7294 if (j < 0) {
7295 bset = isl_basic_set_free(bset);
7296 break;
7298 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7299 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7300 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7301 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7303 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7304 bset = isl_basic_set_reset_space(bset, target_dim);
7305 return bset;
7306 error:
7307 isl_basic_map_free(bmap);
7308 return NULL;
7312 * returns range - domain
7314 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7316 int i;
7317 isl_space *dim;
7318 struct isl_set *result;
7320 if (!map)
7321 return NULL;
7323 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
7324 map->dim, isl_dim_out),
7325 goto error);
7326 dim = isl_map_get_space(map);
7327 dim = isl_space_domain(dim);
7328 result = isl_set_alloc_space(dim, map->n, 0);
7329 if (!result)
7330 goto error;
7331 for (i = 0; i < map->n; ++i)
7332 result = isl_set_add_basic_set(result,
7333 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7334 isl_map_free(map);
7335 return result;
7336 error:
7337 isl_map_free(map);
7338 return NULL;
7342 * returns [domain -> range] -> range - domain
7344 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7345 __isl_take isl_basic_map *bmap)
7347 int i, k;
7348 isl_space *dim;
7349 isl_basic_map *domain;
7350 int nparam, n;
7351 unsigned total;
7353 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
7354 isl_die(bmap->ctx, isl_error_invalid,
7355 "domain and range don't match", goto error);
7357 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7358 n = isl_basic_map_dim(bmap, isl_dim_in);
7360 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7361 domain = isl_basic_map_universe(dim);
7363 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7364 bmap = isl_basic_map_apply_range(bmap, domain);
7365 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7367 total = isl_basic_map_total_dim(bmap);
7369 for (i = 0; i < n; ++i) {
7370 k = isl_basic_map_alloc_equality(bmap);
7371 if (k < 0)
7372 goto error;
7373 isl_seq_clr(bmap->eq[k], 1 + total);
7374 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7375 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7376 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7379 bmap = isl_basic_map_gauss(bmap, NULL);
7380 return isl_basic_map_finalize(bmap);
7381 error:
7382 isl_basic_map_free(bmap);
7383 return NULL;
7387 * returns [domain -> range] -> range - domain
7389 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7391 int i;
7392 isl_space *domain_dim;
7394 if (!map)
7395 return NULL;
7397 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
7398 isl_die(map->ctx, isl_error_invalid,
7399 "domain and range don't match", goto error);
7401 map = isl_map_cow(map);
7402 if (!map)
7403 return NULL;
7405 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7406 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7407 map->dim = isl_space_join(map->dim, domain_dim);
7408 if (!map->dim)
7409 goto error;
7410 for (i = 0; i < map->n; ++i) {
7411 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7412 if (!map->p[i])
7413 goto error;
7415 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7416 return map;
7417 error:
7418 isl_map_free(map);
7419 return NULL;
7422 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7424 struct isl_basic_map *bmap;
7425 unsigned nparam;
7426 unsigned dim;
7427 int i;
7429 if (!dims)
7430 return NULL;
7432 nparam = dims->nparam;
7433 dim = dims->n_out;
7434 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7435 if (!bmap)
7436 goto error;
7438 for (i = 0; i < dim; ++i) {
7439 int j = isl_basic_map_alloc_equality(bmap);
7440 if (j < 0)
7441 goto error;
7442 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7443 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7444 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7446 return isl_basic_map_finalize(bmap);
7447 error:
7448 isl_basic_map_free(bmap);
7449 return NULL;
7452 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7454 if (!dim)
7455 return NULL;
7456 if (dim->n_in != dim->n_out)
7457 isl_die(dim->ctx, isl_error_invalid,
7458 "number of input and output dimensions needs to be "
7459 "the same", goto error);
7460 return basic_map_identity(dim);
7461 error:
7462 isl_space_free(dim);
7463 return NULL;
7466 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7468 if (!model || !model->dim)
7469 return NULL;
7470 return isl_basic_map_identity(isl_space_copy(model->dim));
7473 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7475 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7478 struct isl_map *isl_map_identity_like(struct isl_map *model)
7480 if (!model || !model->dim)
7481 return NULL;
7482 return isl_map_identity(isl_space_copy(model->dim));
7485 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7487 if (!model || !model->dim)
7488 return NULL;
7489 return isl_map_identity(isl_space_copy(model->dim));
7492 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7494 isl_space *dim = isl_set_get_space(set);
7495 isl_map *id;
7496 id = isl_map_identity(isl_space_map_from_set(dim));
7497 return isl_map_intersect_range(id, set);
7500 /* Construct a basic set with all set dimensions having only non-negative
7501 * values.
7503 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7504 __isl_take isl_space *space)
7506 int i;
7507 unsigned nparam;
7508 unsigned dim;
7509 struct isl_basic_set *bset;
7511 if (!space)
7512 return NULL;
7513 nparam = space->nparam;
7514 dim = space->n_out;
7515 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7516 if (!bset)
7517 return NULL;
7518 for (i = 0; i < dim; ++i) {
7519 int k = isl_basic_set_alloc_inequality(bset);
7520 if (k < 0)
7521 goto error;
7522 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7523 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7525 return bset;
7526 error:
7527 isl_basic_set_free(bset);
7528 return NULL;
7531 /* Construct the half-space x_pos >= 0.
7533 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7534 int pos)
7536 int k;
7537 isl_basic_set *nonneg;
7539 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7540 k = isl_basic_set_alloc_inequality(nonneg);
7541 if (k < 0)
7542 goto error;
7543 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7544 isl_int_set_si(nonneg->ineq[k][pos], 1);
7546 return isl_basic_set_finalize(nonneg);
7547 error:
7548 isl_basic_set_free(nonneg);
7549 return NULL;
7552 /* Construct the half-space x_pos <= -1.
7554 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7556 int k;
7557 isl_basic_set *neg;
7559 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7560 k = isl_basic_set_alloc_inequality(neg);
7561 if (k < 0)
7562 goto error;
7563 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7564 isl_int_set_si(neg->ineq[k][0], -1);
7565 isl_int_set_si(neg->ineq[k][pos], -1);
7567 return isl_basic_set_finalize(neg);
7568 error:
7569 isl_basic_set_free(neg);
7570 return NULL;
7573 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7574 enum isl_dim_type type, unsigned first, unsigned n)
7576 int i;
7577 isl_basic_set *nonneg;
7578 isl_basic_set *neg;
7580 if (!set)
7581 return NULL;
7582 if (n == 0)
7583 return set;
7585 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7587 for (i = 0; i < n; ++i) {
7588 nonneg = nonneg_halfspace(isl_set_get_space(set),
7589 pos(set->dim, type) + first + i);
7590 neg = neg_halfspace(isl_set_get_space(set),
7591 pos(set->dim, type) + first + i);
7593 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7596 return set;
7597 error:
7598 isl_set_free(set);
7599 return NULL;
7602 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7603 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7604 void *user)
7606 isl_set *half;
7608 if (!set)
7609 return -1;
7610 if (isl_set_plain_is_empty(set)) {
7611 isl_set_free(set);
7612 return 0;
7614 if (first == len)
7615 return fn(set, signs, user);
7617 signs[first] = 1;
7618 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7619 1 + first));
7620 half = isl_set_intersect(half, isl_set_copy(set));
7621 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7622 goto error;
7624 signs[first] = -1;
7625 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7626 1 + first));
7627 half = isl_set_intersect(half, set);
7628 return foreach_orthant(half, signs, first + 1, len, fn, user);
7629 error:
7630 isl_set_free(set);
7631 return -1;
7634 /* Call "fn" on the intersections of "set" with each of the orthants
7635 * (except for obviously empty intersections). The orthant is identified
7636 * by the signs array, with each entry having value 1 or -1 according
7637 * to the sign of the corresponding variable.
7639 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7640 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7641 void *user)
7643 unsigned nparam;
7644 unsigned nvar;
7645 int *signs;
7646 int r;
7648 if (!set)
7649 return -1;
7650 if (isl_set_plain_is_empty(set))
7651 return 0;
7653 nparam = isl_set_dim(set, isl_dim_param);
7654 nvar = isl_set_dim(set, isl_dim_set);
7656 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7658 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7659 fn, user);
7661 free(signs);
7663 return r;
7666 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7668 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7671 int isl_basic_map_is_subset(
7672 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7674 int is_subset;
7675 struct isl_map *map1;
7676 struct isl_map *map2;
7678 if (!bmap1 || !bmap2)
7679 return -1;
7681 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7682 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7684 is_subset = isl_map_is_subset(map1, map2);
7686 isl_map_free(map1);
7687 isl_map_free(map2);
7689 return is_subset;
7692 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7693 __isl_keep isl_basic_set *bset2)
7695 return isl_basic_map_is_subset(bset1, bset2);
7698 int isl_basic_map_is_equal(
7699 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7701 int is_subset;
7703 if (!bmap1 || !bmap2)
7704 return -1;
7705 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7706 if (is_subset != 1)
7707 return is_subset;
7708 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7709 return is_subset;
7712 int isl_basic_set_is_equal(
7713 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7715 return isl_basic_map_is_equal(
7716 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7719 int isl_map_is_empty(struct isl_map *map)
7721 int i;
7722 int is_empty;
7724 if (!map)
7725 return -1;
7726 for (i = 0; i < map->n; ++i) {
7727 is_empty = isl_basic_map_is_empty(map->p[i]);
7728 if (is_empty < 0)
7729 return -1;
7730 if (!is_empty)
7731 return 0;
7733 return 1;
7736 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7738 return map ? map->n == 0 : -1;
7741 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7743 return isl_map_plain_is_empty(map);
7746 int isl_set_plain_is_empty(struct isl_set *set)
7748 return set ? set->n == 0 : -1;
7751 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7753 return isl_set_plain_is_empty(set);
7756 int isl_set_is_empty(struct isl_set *set)
7758 return isl_map_is_empty((struct isl_map *)set);
7761 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7763 if (!map1 || !map2)
7764 return -1;
7766 return isl_space_is_equal(map1->dim, map2->dim);
7769 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7771 if (!set1 || !set2)
7772 return -1;
7774 return isl_space_is_equal(set1->dim, set2->dim);
7777 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7779 int is_subset;
7781 if (!map1 || !map2)
7782 return -1;
7783 is_subset = isl_map_is_subset(map1, map2);
7784 if (is_subset != 1)
7785 return is_subset;
7786 is_subset = isl_map_is_subset(map2, map1);
7787 return is_subset;
7790 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7792 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7795 int isl_basic_map_is_strict_subset(
7796 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7798 int is_subset;
7800 if (!bmap1 || !bmap2)
7801 return -1;
7802 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7803 if (is_subset != 1)
7804 return is_subset;
7805 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7806 if (is_subset == -1)
7807 return is_subset;
7808 return !is_subset;
7811 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7813 int is_subset;
7815 if (!map1 || !map2)
7816 return -1;
7817 is_subset = isl_map_is_subset(map1, map2);
7818 if (is_subset != 1)
7819 return is_subset;
7820 is_subset = isl_map_is_subset(map2, map1);
7821 if (is_subset == -1)
7822 return is_subset;
7823 return !is_subset;
7826 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7828 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7831 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7833 if (!bmap)
7834 return -1;
7835 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7838 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7840 if (!bset)
7841 return -1;
7842 return bset->n_eq == 0 && bset->n_ineq == 0;
7845 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7847 int i;
7849 if (!map)
7850 return -1;
7852 for (i = 0; i < map->n; ++i) {
7853 int r = isl_basic_map_is_universe(map->p[i]);
7854 if (r < 0 || r)
7855 return r;
7858 return 0;
7861 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7863 return isl_map_plain_is_universe((isl_map *) set);
7866 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7868 return isl_set_plain_is_universe(set);
7871 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7873 struct isl_basic_set *bset = NULL;
7874 struct isl_vec *sample = NULL;
7875 int empty;
7876 unsigned total;
7878 if (!bmap)
7879 return -1;
7881 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7882 return 1;
7884 if (isl_basic_map_is_universe(bmap))
7885 return 0;
7887 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7888 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7889 copy = isl_basic_map_remove_redundancies(copy);
7890 empty = isl_basic_map_plain_is_empty(copy);
7891 isl_basic_map_free(copy);
7892 return empty;
7895 total = 1 + isl_basic_map_total_dim(bmap);
7896 if (bmap->sample && bmap->sample->size == total) {
7897 int contains = isl_basic_map_contains(bmap, bmap->sample);
7898 if (contains < 0)
7899 return -1;
7900 if (contains)
7901 return 0;
7903 isl_vec_free(bmap->sample);
7904 bmap->sample = NULL;
7905 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7906 if (!bset)
7907 return -1;
7908 sample = isl_basic_set_sample_vec(bset);
7909 if (!sample)
7910 return -1;
7911 empty = sample->size == 0;
7912 isl_vec_free(bmap->sample);
7913 bmap->sample = sample;
7914 if (empty)
7915 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7917 return empty;
7920 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7922 if (!bmap)
7923 return -1;
7924 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7927 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7929 return isl_basic_map_plain_is_empty(bmap);
7932 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7934 if (!bset)
7935 return -1;
7936 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7939 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7941 return isl_basic_set_plain_is_empty(bset);
7944 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7946 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7949 struct isl_map *isl_basic_map_union(
7950 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7952 struct isl_map *map;
7953 if (!bmap1 || !bmap2)
7954 goto error;
7956 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7958 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7959 if (!map)
7960 goto error;
7961 map = isl_map_add_basic_map(map, bmap1);
7962 map = isl_map_add_basic_map(map, bmap2);
7963 return map;
7964 error:
7965 isl_basic_map_free(bmap1);
7966 isl_basic_map_free(bmap2);
7967 return NULL;
7970 struct isl_set *isl_basic_set_union(
7971 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7973 return (struct isl_set *)isl_basic_map_union(
7974 (struct isl_basic_map *)bset1,
7975 (struct isl_basic_map *)bset2);
7978 /* Order divs such that any div only depends on previous divs */
7979 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7981 int i;
7982 unsigned off;
7984 if (!bmap)
7985 return NULL;
7987 off = isl_space_dim(bmap->dim, isl_dim_all);
7989 for (i = 0; i < bmap->n_div; ++i) {
7990 int pos;
7991 if (isl_int_is_zero(bmap->div[i][0]))
7992 continue;
7993 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7994 bmap->n_div-i);
7995 if (pos == -1)
7996 continue;
7997 isl_basic_map_swap_div(bmap, i, i + pos);
7998 --i;
8000 return bmap;
8003 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8005 return (struct isl_basic_set *)
8006 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8009 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8011 int i;
8013 if (!map)
8014 return 0;
8016 for (i = 0; i < map->n; ++i) {
8017 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8018 if (!map->p[i])
8019 goto error;
8022 return map;
8023 error:
8024 isl_map_free(map);
8025 return NULL;
8028 /* Apply the expansion computed by isl_merge_divs.
8029 * The expansion itself is given by "exp" while the resulting
8030 * list of divs is given by "div".
8032 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8033 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8035 int i, j;
8036 int n_div;
8038 bset = isl_basic_set_cow(bset);
8039 if (!bset || !div)
8040 goto error;
8042 if (div->n_row < bset->n_div)
8043 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8044 "not an expansion", goto error);
8046 n_div = bset->n_div;
8047 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8048 div->n_row - n_div, 0,
8049 2 * (div->n_row - n_div));
8051 for (i = n_div; i < div->n_row; ++i)
8052 if (isl_basic_set_alloc_div(bset) < 0)
8053 goto error;
8055 j = n_div - 1;
8056 for (i = div->n_row - 1; i >= 0; --i) {
8057 if (j >= 0 && exp[j] == i) {
8058 if (i != j)
8059 isl_basic_map_swap_div(bset, i, j);
8060 j--;
8061 } else {
8062 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8063 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8064 goto error;
8068 isl_mat_free(div);
8069 return bset;
8070 error:
8071 isl_basic_set_free(bset);
8072 isl_mat_free(div);
8073 return NULL;
8076 /* Look for a div in dst that corresponds to the div "div" in src.
8077 * The divs before "div" in src and dst are assumed to be the same.
8079 * Returns -1 if no corresponding div was found and the position
8080 * of the corresponding div in dst otherwise.
8082 static int find_div(struct isl_basic_map *dst,
8083 struct isl_basic_map *src, unsigned div)
8085 int i;
8087 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8089 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8090 for (i = div; i < dst->n_div; ++i)
8091 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8092 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8093 dst->n_div - div) == -1)
8094 return i;
8095 return -1;
8098 struct isl_basic_map *isl_basic_map_align_divs(
8099 struct isl_basic_map *dst, struct isl_basic_map *src)
8101 int i;
8102 unsigned total;
8104 if (!dst || !src)
8105 goto error;
8107 if (src->n_div == 0)
8108 return dst;
8110 for (i = 0; i < src->n_div; ++i)
8111 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
8113 src = isl_basic_map_order_divs(src);
8114 dst = isl_basic_map_cow(dst);
8115 if (!dst)
8116 return NULL;
8117 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
8118 src->n_div, 0, 2 * src->n_div);
8119 if (!dst)
8120 return NULL;
8121 total = isl_space_dim(src->dim, isl_dim_all);
8122 for (i = 0; i < src->n_div; ++i) {
8123 int j = find_div(dst, src, i);
8124 if (j < 0) {
8125 j = isl_basic_map_alloc_div(dst);
8126 if (j < 0)
8127 goto error;
8128 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8129 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8130 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8131 goto error;
8133 if (j != i)
8134 isl_basic_map_swap_div(dst, i, j);
8136 return dst;
8137 error:
8138 isl_basic_map_free(dst);
8139 return NULL;
8142 struct isl_basic_set *isl_basic_set_align_divs(
8143 struct isl_basic_set *dst, struct isl_basic_set *src)
8145 return (struct isl_basic_set *)isl_basic_map_align_divs(
8146 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8149 struct isl_map *isl_map_align_divs(struct isl_map *map)
8151 int i;
8153 if (!map)
8154 return NULL;
8155 if (map->n == 0)
8156 return map;
8157 map = isl_map_compute_divs(map);
8158 map = isl_map_cow(map);
8159 if (!map)
8160 return NULL;
8162 for (i = 1; i < map->n; ++i)
8163 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8164 for (i = 1; i < map->n; ++i) {
8165 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8166 if (!map->p[i])
8167 return isl_map_free(map);
8170 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8171 return map;
8174 struct isl_set *isl_set_align_divs(struct isl_set *set)
8176 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8179 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8180 __isl_take isl_map *map)
8182 if (!set || !map)
8183 goto error;
8184 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8185 map = isl_map_intersect_domain(map, set);
8186 set = isl_map_range(map);
8187 return set;
8188 error:
8189 isl_set_free(set);
8190 isl_map_free(map);
8191 return NULL;
8194 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8195 __isl_take isl_map *map)
8197 return isl_map_align_params_map_map_and(set, map, &set_apply);
8200 /* There is no need to cow as removing empty parts doesn't change
8201 * the meaning of the set.
8203 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8205 int i;
8207 if (!map)
8208 return NULL;
8210 for (i = map->n - 1; i >= 0; --i)
8211 remove_if_empty(map, i);
8213 return map;
8216 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8218 return (struct isl_set *)
8219 isl_map_remove_empty_parts((struct isl_map *)set);
8222 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8224 struct isl_basic_map *bmap;
8225 if (!map || map->n == 0)
8226 return NULL;
8227 bmap = map->p[map->n-1];
8228 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8229 return isl_basic_map_copy(bmap);
8232 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8234 return (struct isl_basic_set *)
8235 isl_map_copy_basic_map((struct isl_map *)set);
8238 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8239 __isl_keep isl_basic_map *bmap)
8241 int i;
8243 if (!map || !bmap)
8244 goto error;
8245 for (i = map->n-1; i >= 0; --i) {
8246 if (map->p[i] != bmap)
8247 continue;
8248 map = isl_map_cow(map);
8249 if (!map)
8250 goto error;
8251 isl_basic_map_free(map->p[i]);
8252 if (i != map->n-1) {
8253 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8254 map->p[i] = map->p[map->n-1];
8256 map->n--;
8257 return map;
8259 return map;
8260 error:
8261 isl_map_free(map);
8262 return NULL;
8265 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8266 struct isl_basic_set *bset)
8268 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8269 (struct isl_basic_map *)bset);
8272 /* Given two basic sets bset1 and bset2, compute the maximal difference
8273 * between the values of dimension pos in bset1 and those in bset2
8274 * for any common value of the parameters and dimensions preceding pos.
8276 static enum isl_lp_result basic_set_maximal_difference_at(
8277 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8278 int pos, isl_int *opt)
8280 isl_space *dims;
8281 struct isl_basic_map *bmap1 = NULL;
8282 struct isl_basic_map *bmap2 = NULL;
8283 struct isl_ctx *ctx;
8284 struct isl_vec *obj;
8285 unsigned total;
8286 unsigned nparam;
8287 unsigned dim1, dim2;
8288 enum isl_lp_result res;
8290 if (!bset1 || !bset2)
8291 return isl_lp_error;
8293 nparam = isl_basic_set_n_param(bset1);
8294 dim1 = isl_basic_set_n_dim(bset1);
8295 dim2 = isl_basic_set_n_dim(bset2);
8296 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8297 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8298 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8299 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8300 if (!bmap1 || !bmap2)
8301 goto error;
8302 bmap1 = isl_basic_map_cow(bmap1);
8303 bmap1 = isl_basic_map_extend(bmap1, nparam,
8304 pos, (dim1 - pos) + (dim2 - pos),
8305 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8306 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8307 if (!bmap1)
8308 goto error;
8309 total = isl_basic_map_total_dim(bmap1);
8310 ctx = bmap1->ctx;
8311 obj = isl_vec_alloc(ctx, 1 + total);
8312 if (!obj)
8313 goto error2;
8314 isl_seq_clr(obj->block.data, 1 + total);
8315 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8316 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8317 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8318 opt, NULL, NULL);
8319 isl_basic_map_free(bmap1);
8320 isl_vec_free(obj);
8321 return res;
8322 error:
8323 isl_basic_map_free(bmap2);
8324 error2:
8325 isl_basic_map_free(bmap1);
8326 return isl_lp_error;
8329 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8330 * for any common value of the parameters and dimensions preceding pos
8331 * in both basic sets, the values of dimension pos in bset1 are
8332 * smaller or larger than those in bset2.
8334 * Returns
8335 * 1 if bset1 follows bset2
8336 * -1 if bset1 precedes bset2
8337 * 0 if bset1 and bset2 are incomparable
8338 * -2 if some error occurred.
8340 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8341 struct isl_basic_set *bset2, int pos)
8343 isl_int opt;
8344 enum isl_lp_result res;
8345 int cmp;
8347 isl_int_init(opt);
8349 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8351 if (res == isl_lp_empty)
8352 cmp = 0;
8353 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8354 res == isl_lp_unbounded)
8355 cmp = 1;
8356 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8357 cmp = -1;
8358 else
8359 cmp = -2;
8361 isl_int_clear(opt);
8362 return cmp;
8365 /* Given two basic sets bset1 and bset2, check whether
8366 * for any common value of the parameters and dimensions preceding pos
8367 * there is a value of dimension pos in bset1 that is larger
8368 * than a value of the same dimension in bset2.
8370 * Return
8371 * 1 if there exists such a pair
8372 * 0 if there is no such pair, but there is a pair of equal values
8373 * -1 otherwise
8374 * -2 if some error occurred.
8376 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8377 __isl_keep isl_basic_set *bset2, int pos)
8379 isl_int opt;
8380 enum isl_lp_result res;
8381 int cmp;
8383 isl_int_init(opt);
8385 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8387 if (res == isl_lp_empty)
8388 cmp = -1;
8389 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8390 res == isl_lp_unbounded)
8391 cmp = 1;
8392 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8393 cmp = -1;
8394 else if (res == isl_lp_ok)
8395 cmp = 0;
8396 else
8397 cmp = -2;
8399 isl_int_clear(opt);
8400 return cmp;
8403 /* Given two sets set1 and set2, check whether
8404 * for any common value of the parameters and dimensions preceding pos
8405 * there is a value of dimension pos in set1 that is larger
8406 * than a value of the same dimension in set2.
8408 * Return
8409 * 1 if there exists such a pair
8410 * 0 if there is no such pair, but there is a pair of equal values
8411 * -1 otherwise
8412 * -2 if some error occurred.
8414 int isl_set_follows_at(__isl_keep isl_set *set1,
8415 __isl_keep isl_set *set2, int pos)
8417 int i, j;
8418 int follows = -1;
8420 if (!set1 || !set2)
8421 return -2;
8423 for (i = 0; i < set1->n; ++i)
8424 for (j = 0; j < set2->n; ++j) {
8425 int f;
8426 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8427 if (f == 1 || f == -2)
8428 return f;
8429 if (f > follows)
8430 follows = f;
8433 return follows;
8436 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8437 unsigned pos, isl_int *val)
8439 int i;
8440 int d;
8441 unsigned total;
8443 if (!bmap)
8444 return -1;
8445 total = isl_basic_map_total_dim(bmap);
8446 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8447 for (; d+1 > pos; --d)
8448 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8449 break;
8450 if (d != pos)
8451 continue;
8452 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8453 return 0;
8454 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8455 return 0;
8456 if (!isl_int_is_one(bmap->eq[i][1+d]))
8457 return 0;
8458 if (val)
8459 isl_int_neg(*val, bmap->eq[i][0]);
8460 return 1;
8462 return 0;
8465 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8466 unsigned pos, isl_int *val)
8468 int i;
8469 isl_int v;
8470 isl_int tmp;
8471 int fixed;
8473 if (!map)
8474 return -1;
8475 if (map->n == 0)
8476 return 0;
8477 if (map->n == 1)
8478 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8479 isl_int_init(v);
8480 isl_int_init(tmp);
8481 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8482 for (i = 1; fixed == 1 && i < map->n; ++i) {
8483 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8484 if (fixed == 1 && isl_int_ne(tmp, v))
8485 fixed = 0;
8487 if (val)
8488 isl_int_set(*val, v);
8489 isl_int_clear(tmp);
8490 isl_int_clear(v);
8491 return fixed;
8494 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8495 unsigned pos, isl_int *val)
8497 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8498 pos, val);
8501 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8502 isl_int *val)
8504 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8507 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8508 enum isl_dim_type type, unsigned pos, isl_int *val)
8510 if (pos >= isl_basic_map_dim(bmap, type))
8511 return -1;
8512 return isl_basic_map_plain_has_fixed_var(bmap,
8513 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8516 /* If "bmap" obviously lies on a hyperplane where the given dimension
8517 * has a fixed value, then return that value.
8518 * Otherwise return NaN.
8520 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8521 __isl_keep isl_basic_map *bmap,
8522 enum isl_dim_type type, unsigned pos)
8524 isl_ctx *ctx;
8525 isl_val *v;
8526 int fixed;
8528 if (!bmap)
8529 return NULL;
8530 ctx = isl_basic_map_get_ctx(bmap);
8531 v = isl_val_alloc(ctx);
8532 if (!v)
8533 return NULL;
8534 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8535 if (fixed < 0)
8536 return isl_val_free(v);
8537 if (fixed) {
8538 isl_int_set_si(v->d, 1);
8539 return v;
8541 isl_val_free(v);
8542 return isl_val_nan(ctx);
8545 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8546 enum isl_dim_type type, unsigned pos, isl_int *val)
8548 if (pos >= isl_map_dim(map, type))
8549 return -1;
8550 return isl_map_plain_has_fixed_var(map,
8551 map_offset(map, type) - 1 + pos, val);
8554 /* If "map" obviously lies on a hyperplane where the given dimension
8555 * has a fixed value, then return that value.
8556 * Otherwise return NaN.
8558 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8559 enum isl_dim_type type, unsigned pos)
8561 isl_ctx *ctx;
8562 isl_val *v;
8563 int fixed;
8565 if (!map)
8566 return NULL;
8567 ctx = isl_map_get_ctx(map);
8568 v = isl_val_alloc(ctx);
8569 if (!v)
8570 return NULL;
8571 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8572 if (fixed < 0)
8573 return isl_val_free(v);
8574 if (fixed) {
8575 isl_int_set_si(v->d, 1);
8576 return v;
8578 isl_val_free(v);
8579 return isl_val_nan(ctx);
8582 /* If "set" obviously lies on a hyperplane where the given dimension
8583 * has a fixed value, then return that value.
8584 * Otherwise return NaN.
8586 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8587 enum isl_dim_type type, unsigned pos)
8589 return isl_map_plain_get_val_if_fixed(set, type, pos);
8592 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8593 enum isl_dim_type type, unsigned pos, isl_int *val)
8595 return isl_map_plain_is_fixed(set, type, pos, val);
8598 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8599 enum isl_dim_type type, unsigned pos, isl_int *val)
8601 return isl_map_plain_is_fixed(map, type, pos, val);
8604 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8605 * then return this fixed value in *val.
8607 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8608 unsigned dim, isl_int *val)
8610 return isl_basic_set_plain_has_fixed_var(bset,
8611 isl_basic_set_n_param(bset) + dim, val);
8614 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8615 * then return this fixed value in *val.
8617 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8618 unsigned dim, isl_int *val)
8620 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8623 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8624 unsigned dim, isl_int *val)
8626 return isl_set_plain_dim_is_fixed(set, dim, val);
8629 /* Check if input variable in has fixed value and if so and if val is not NULL,
8630 * then return this fixed value in *val.
8632 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8633 unsigned in, isl_int *val)
8635 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8638 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8639 * and if val is not NULL, then return this lower bound in *val.
8641 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8642 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8644 int i, i_eq = -1, i_ineq = -1;
8645 isl_int *c;
8646 unsigned total;
8647 unsigned nparam;
8649 if (!bset)
8650 return -1;
8651 total = isl_basic_set_total_dim(bset);
8652 nparam = isl_basic_set_n_param(bset);
8653 for (i = 0; i < bset->n_eq; ++i) {
8654 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8655 continue;
8656 if (i_eq != -1)
8657 return 0;
8658 i_eq = i;
8660 for (i = 0; i < bset->n_ineq; ++i) {
8661 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8662 continue;
8663 if (i_eq != -1 || i_ineq != -1)
8664 return 0;
8665 i_ineq = i;
8667 if (i_eq == -1 && i_ineq == -1)
8668 return 0;
8669 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8670 /* The coefficient should always be one due to normalization. */
8671 if (!isl_int_is_one(c[1+nparam+dim]))
8672 return 0;
8673 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8674 return 0;
8675 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8676 total - nparam - dim - 1) != -1)
8677 return 0;
8678 if (val)
8679 isl_int_neg(*val, c[0]);
8680 return 1;
8683 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8684 unsigned dim, isl_int *val)
8686 int i;
8687 isl_int v;
8688 isl_int tmp;
8689 int fixed;
8691 if (!set)
8692 return -1;
8693 if (set->n == 0)
8694 return 0;
8695 if (set->n == 1)
8696 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8697 dim, val);
8698 isl_int_init(v);
8699 isl_int_init(tmp);
8700 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8701 dim, &v);
8702 for (i = 1; fixed == 1 && i < set->n; ++i) {
8703 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8704 dim, &tmp);
8705 if (fixed == 1 && isl_int_ne(tmp, v))
8706 fixed = 0;
8708 if (val)
8709 isl_int_set(*val, v);
8710 isl_int_clear(tmp);
8711 isl_int_clear(v);
8712 return fixed;
8715 struct constraint {
8716 unsigned size;
8717 isl_int *c;
8720 /* uset_gist depends on constraints without existentially quantified
8721 * variables sorting first.
8723 static int qsort_constraint_cmp(const void *p1, const void *p2)
8725 const struct constraint *c1 = (const struct constraint *)p1;
8726 const struct constraint *c2 = (const struct constraint *)p2;
8727 int l1, l2;
8728 unsigned size = isl_min(c1->size, c2->size);
8730 l1 = isl_seq_last_non_zero(c1->c + 1, size);
8731 l2 = isl_seq_last_non_zero(c2->c + 1, size);
8733 if (l1 != l2)
8734 return l1 - l2;
8736 return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8739 static struct isl_basic_map *isl_basic_map_sort_constraints(
8740 struct isl_basic_map *bmap)
8742 int i;
8743 struct constraint *c;
8744 unsigned total;
8746 if (!bmap)
8747 return NULL;
8748 if (bmap->n_ineq == 0)
8749 return bmap;
8750 total = isl_basic_map_total_dim(bmap);
8751 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8752 if (!c)
8753 goto error;
8754 for (i = 0; i < bmap->n_ineq; ++i) {
8755 c[i].size = total;
8756 c[i].c = bmap->ineq[i];
8758 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8759 for (i = 0; i < bmap->n_ineq; ++i)
8760 bmap->ineq[i] = c[i].c;
8761 free(c);
8762 return bmap;
8763 error:
8764 isl_basic_map_free(bmap);
8765 return NULL;
8768 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8769 __isl_take isl_basic_set *bset)
8771 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8772 (struct isl_basic_map *)bset);
8775 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8777 if (!bmap)
8778 return NULL;
8779 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8780 return bmap;
8781 bmap = isl_basic_map_remove_redundancies(bmap);
8782 bmap = isl_basic_map_sort_constraints(bmap);
8783 if (bmap)
8784 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8785 return bmap;
8788 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8790 return (struct isl_basic_set *)isl_basic_map_normalize(
8791 (struct isl_basic_map *)bset);
8794 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8795 const __isl_keep isl_basic_map *bmap2)
8797 int i, cmp;
8798 unsigned total;
8800 if (bmap1 == bmap2)
8801 return 0;
8802 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8803 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8804 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8805 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8806 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8807 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8808 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8809 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8810 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8811 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8812 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8813 return 0;
8814 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8815 return 1;
8816 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8817 return -1;
8818 if (bmap1->n_eq != bmap2->n_eq)
8819 return bmap1->n_eq - bmap2->n_eq;
8820 if (bmap1->n_ineq != bmap2->n_ineq)
8821 return bmap1->n_ineq - bmap2->n_ineq;
8822 if (bmap1->n_div != bmap2->n_div)
8823 return bmap1->n_div - bmap2->n_div;
8824 total = isl_basic_map_total_dim(bmap1);
8825 for (i = 0; i < bmap1->n_eq; ++i) {
8826 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8827 if (cmp)
8828 return cmp;
8830 for (i = 0; i < bmap1->n_ineq; ++i) {
8831 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8832 if (cmp)
8833 return cmp;
8835 for (i = 0; i < bmap1->n_div; ++i) {
8836 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8837 if (cmp)
8838 return cmp;
8840 return 0;
8843 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8844 const __isl_keep isl_basic_set *bset2)
8846 return isl_basic_map_plain_cmp(bset1, bset2);
8849 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8851 int i, cmp;
8853 if (set1 == set2)
8854 return 0;
8855 if (set1->n != set2->n)
8856 return set1->n - set2->n;
8858 for (i = 0; i < set1->n; ++i) {
8859 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8860 if (cmp)
8861 return cmp;
8864 return 0;
8867 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8868 __isl_keep isl_basic_map *bmap2)
8870 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8873 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8874 __isl_keep isl_basic_set *bset2)
8876 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8877 (isl_basic_map *)bset2);
8880 static int qsort_bmap_cmp(const void *p1, const void *p2)
8882 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8883 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8885 return isl_basic_map_plain_cmp(bmap1, bmap2);
8888 /* We normalize in place, but if anything goes wrong we need
8889 * to return NULL, so we need to make sure we don't change the
8890 * meaning of any possible other copies of map.
8892 struct isl_map *isl_map_normalize(struct isl_map *map)
8894 int i, j;
8895 struct isl_basic_map *bmap;
8897 if (!map)
8898 return NULL;
8899 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8900 return map;
8901 for (i = 0; i < map->n; ++i) {
8902 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8903 if (!bmap)
8904 goto error;
8905 isl_basic_map_free(map->p[i]);
8906 map->p[i] = bmap;
8908 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8909 ISL_F_SET(map, ISL_MAP_NORMALIZED);
8910 map = isl_map_remove_empty_parts(map);
8911 if (!map)
8912 return NULL;
8913 for (i = map->n - 1; i >= 1; --i) {
8914 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8915 continue;
8916 isl_basic_map_free(map->p[i-1]);
8917 for (j = i; j < map->n; ++j)
8918 map->p[j-1] = map->p[j];
8919 map->n--;
8921 return map;
8922 error:
8923 isl_map_free(map);
8924 return NULL;
8928 struct isl_set *isl_set_normalize(struct isl_set *set)
8930 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8933 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8935 int i;
8936 int equal;
8938 if (!map1 || !map2)
8939 return -1;
8941 if (map1 == map2)
8942 return 1;
8943 if (!isl_space_is_equal(map1->dim, map2->dim))
8944 return 0;
8946 map1 = isl_map_copy(map1);
8947 map2 = isl_map_copy(map2);
8948 map1 = isl_map_normalize(map1);
8949 map2 = isl_map_normalize(map2);
8950 if (!map1 || !map2)
8951 goto error;
8952 equal = map1->n == map2->n;
8953 for (i = 0; equal && i < map1->n; ++i) {
8954 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8955 if (equal < 0)
8956 goto error;
8958 isl_map_free(map1);
8959 isl_map_free(map2);
8960 return equal;
8961 error:
8962 isl_map_free(map1);
8963 isl_map_free(map2);
8964 return -1;
8967 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8969 return isl_map_plain_is_equal(map1, map2);
8972 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8974 return isl_map_plain_is_equal((struct isl_map *)set1,
8975 (struct isl_map *)set2);
8978 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8980 return isl_set_plain_is_equal(set1, set2);
8983 /* Return an interval that ranges from min to max (inclusive)
8985 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8986 isl_int min, isl_int max)
8988 int k;
8989 struct isl_basic_set *bset = NULL;
8991 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8992 if (!bset)
8993 goto error;
8995 k = isl_basic_set_alloc_inequality(bset);
8996 if (k < 0)
8997 goto error;
8998 isl_int_set_si(bset->ineq[k][1], 1);
8999 isl_int_neg(bset->ineq[k][0], min);
9001 k = isl_basic_set_alloc_inequality(bset);
9002 if (k < 0)
9003 goto error;
9004 isl_int_set_si(bset->ineq[k][1], -1);
9005 isl_int_set(bset->ineq[k][0], max);
9007 return bset;
9008 error:
9009 isl_basic_set_free(bset);
9010 return NULL;
9013 /* Return the Cartesian product of the basic sets in list (in the given order).
9015 __isl_give isl_basic_set *isl_basic_set_list_product(
9016 __isl_take struct isl_basic_set_list *list)
9018 int i;
9019 unsigned dim;
9020 unsigned nparam;
9021 unsigned extra;
9022 unsigned n_eq;
9023 unsigned n_ineq;
9024 struct isl_basic_set *product = NULL;
9026 if (!list)
9027 goto error;
9028 isl_assert(list->ctx, list->n > 0, goto error);
9029 isl_assert(list->ctx, list->p[0], goto error);
9030 nparam = isl_basic_set_n_param(list->p[0]);
9031 dim = isl_basic_set_n_dim(list->p[0]);
9032 extra = list->p[0]->n_div;
9033 n_eq = list->p[0]->n_eq;
9034 n_ineq = list->p[0]->n_ineq;
9035 for (i = 1; i < list->n; ++i) {
9036 isl_assert(list->ctx, list->p[i], goto error);
9037 isl_assert(list->ctx,
9038 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9039 dim += isl_basic_set_n_dim(list->p[i]);
9040 extra += list->p[i]->n_div;
9041 n_eq += list->p[i]->n_eq;
9042 n_ineq += list->p[i]->n_ineq;
9044 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9045 n_eq, n_ineq);
9046 if (!product)
9047 goto error;
9048 dim = 0;
9049 for (i = 0; i < list->n; ++i) {
9050 isl_basic_set_add_constraints(product,
9051 isl_basic_set_copy(list->p[i]), dim);
9052 dim += isl_basic_set_n_dim(list->p[i]);
9054 isl_basic_set_list_free(list);
9055 return product;
9056 error:
9057 isl_basic_set_free(product);
9058 isl_basic_set_list_free(list);
9059 return NULL;
9062 struct isl_basic_map *isl_basic_map_product(
9063 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9065 isl_space *dim_result = NULL;
9066 struct isl_basic_map *bmap;
9067 unsigned in1, in2, out1, out2, nparam, total, pos;
9068 struct isl_dim_map *dim_map1, *dim_map2;
9070 if (!bmap1 || !bmap2)
9071 goto error;
9073 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9074 bmap2->dim, isl_dim_param), goto error);
9075 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9076 isl_space_copy(bmap2->dim));
9078 in1 = isl_basic_map_n_in(bmap1);
9079 in2 = isl_basic_map_n_in(bmap2);
9080 out1 = isl_basic_map_n_out(bmap1);
9081 out2 = isl_basic_map_n_out(bmap2);
9082 nparam = isl_basic_map_n_param(bmap1);
9084 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9085 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9086 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9087 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9088 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9089 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9090 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9091 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9092 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9093 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9094 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9096 bmap = isl_basic_map_alloc_space(dim_result,
9097 bmap1->n_div + bmap2->n_div,
9098 bmap1->n_eq + bmap2->n_eq,
9099 bmap1->n_ineq + bmap2->n_ineq);
9100 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9101 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9102 bmap = isl_basic_map_simplify(bmap);
9103 return isl_basic_map_finalize(bmap);
9104 error:
9105 isl_basic_map_free(bmap1);
9106 isl_basic_map_free(bmap2);
9107 return NULL;
9110 __isl_give isl_basic_map *isl_basic_map_flat_product(
9111 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9113 isl_basic_map *prod;
9115 prod = isl_basic_map_product(bmap1, bmap2);
9116 prod = isl_basic_map_flatten(prod);
9117 return prod;
9120 __isl_give isl_basic_set *isl_basic_set_flat_product(
9121 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9123 return isl_basic_map_flat_range_product(bset1, bset2);
9126 __isl_give isl_basic_map *isl_basic_map_domain_product(
9127 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9129 isl_space *space_result = NULL;
9130 isl_basic_map *bmap;
9131 unsigned in1, in2, out, nparam, total, pos;
9132 struct isl_dim_map *dim_map1, *dim_map2;
9134 if (!bmap1 || !bmap2)
9135 goto error;
9137 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9138 isl_space_copy(bmap2->dim));
9140 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9141 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9142 out = isl_basic_map_dim(bmap1, isl_dim_out);
9143 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9145 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9146 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9147 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9148 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9149 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9150 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9151 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9152 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9153 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9154 isl_dim_map_div(dim_map1, bmap1, pos += out);
9155 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9157 bmap = isl_basic_map_alloc_space(space_result,
9158 bmap1->n_div + bmap2->n_div,
9159 bmap1->n_eq + bmap2->n_eq,
9160 bmap1->n_ineq + bmap2->n_ineq);
9161 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9162 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9163 bmap = isl_basic_map_simplify(bmap);
9164 return isl_basic_map_finalize(bmap);
9165 error:
9166 isl_basic_map_free(bmap1);
9167 isl_basic_map_free(bmap2);
9168 return NULL;
9171 __isl_give isl_basic_map *isl_basic_map_range_product(
9172 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9174 isl_space *dim_result = NULL;
9175 isl_basic_map *bmap;
9176 unsigned in, out1, out2, nparam, total, pos;
9177 struct isl_dim_map *dim_map1, *dim_map2;
9179 if (!bmap1 || !bmap2)
9180 goto error;
9182 if (!isl_space_match(bmap1->dim, isl_dim_param,
9183 bmap2->dim, isl_dim_param))
9184 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9185 "parameters don't match", goto error);
9187 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9188 isl_space_copy(bmap2->dim));
9190 in = isl_basic_map_dim(bmap1, isl_dim_in);
9191 out1 = isl_basic_map_n_out(bmap1);
9192 out2 = isl_basic_map_n_out(bmap2);
9193 nparam = isl_basic_map_n_param(bmap1);
9195 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9196 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9197 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9198 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9199 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9200 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9201 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9202 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9203 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9204 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9205 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9207 bmap = isl_basic_map_alloc_space(dim_result,
9208 bmap1->n_div + bmap2->n_div,
9209 bmap1->n_eq + bmap2->n_eq,
9210 bmap1->n_ineq + bmap2->n_ineq);
9211 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9212 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9213 bmap = isl_basic_map_simplify(bmap);
9214 return isl_basic_map_finalize(bmap);
9215 error:
9216 isl_basic_map_free(bmap1);
9217 isl_basic_map_free(bmap2);
9218 return NULL;
9221 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9222 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9224 isl_basic_map *prod;
9226 prod = isl_basic_map_range_product(bmap1, bmap2);
9227 prod = isl_basic_map_flatten_range(prod);
9228 return prod;
9231 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9232 __isl_take isl_map *map2,
9233 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
9234 __isl_take isl_space *right),
9235 __isl_give isl_basic_map *(*basic_map_product)(
9236 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
9238 unsigned flags = 0;
9239 struct isl_map *result;
9240 int i, j;
9242 if (!map1 || !map2)
9243 goto error;
9245 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9246 map2->dim, isl_dim_param), goto error);
9248 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9249 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9250 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9252 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
9253 isl_space_copy(map2->dim)),
9254 map1->n * map2->n, flags);
9255 if (!result)
9256 goto error;
9257 for (i = 0; i < map1->n; ++i)
9258 for (j = 0; j < map2->n; ++j) {
9259 struct isl_basic_map *part;
9260 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9261 isl_basic_map_copy(map2->p[j]));
9262 if (isl_basic_map_is_empty(part))
9263 isl_basic_map_free(part);
9264 else
9265 result = isl_map_add_basic_map(result, part);
9266 if (!result)
9267 goto error;
9269 isl_map_free(map1);
9270 isl_map_free(map2);
9271 return result;
9272 error:
9273 isl_map_free(map1);
9274 isl_map_free(map2);
9275 return NULL;
9278 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9280 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9281 __isl_take isl_map *map2)
9283 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
9286 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9287 __isl_take isl_map *map2)
9289 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9292 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9294 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9295 __isl_take isl_map *map2)
9297 isl_map *prod;
9299 prod = isl_map_product(map1, map2);
9300 prod = isl_map_flatten(prod);
9301 return prod;
9304 /* Given two set A and B, construct its Cartesian product A x B.
9306 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9308 return isl_map_range_product(set1, set2);
9311 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9312 __isl_take isl_set *set2)
9314 return isl_map_flat_range_product(set1, set2);
9317 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9319 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9320 __isl_take isl_map *map2)
9322 return map_product(map1, map2, &isl_space_domain_product,
9323 &isl_basic_map_domain_product);
9326 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9328 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9329 __isl_take isl_map *map2)
9331 return map_product(map1, map2, &isl_space_range_product,
9332 &isl_basic_map_range_product);
9335 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9336 __isl_take isl_map *map2)
9338 return isl_map_align_params_map_map_and(map1, map2,
9339 &map_domain_product_aligned);
9342 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9343 __isl_take isl_map *map2)
9345 return isl_map_align_params_map_map_and(map1, map2,
9346 &map_range_product_aligned);
9349 /* Given a map A -> [B -> C], extract the map A -> B.
9351 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9353 isl_space *space;
9354 int total, keep;
9356 if (!map)
9357 return NULL;
9358 if (!isl_space_range_is_wrapping(map->dim))
9359 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9360 "range is not a product", return isl_map_free(map));
9362 space = isl_map_get_space(map);
9363 total = isl_space_dim(space, isl_dim_out);
9364 space = isl_space_range_factor_domain(space);
9365 keep = isl_space_dim(space, isl_dim_out);
9366 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9367 map = isl_map_reset_space(map, space);
9369 return map;
9372 /* Given a map A -> [B -> C], extract the map A -> C.
9374 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9376 isl_space *space;
9377 int total, keep;
9379 if (!map)
9380 return NULL;
9381 if (!isl_space_range_is_wrapping(map->dim))
9382 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9383 "range is not a product", return isl_map_free(map));
9385 space = isl_map_get_space(map);
9386 total = isl_space_dim(space, isl_dim_out);
9387 space = isl_space_range_factor_range(space);
9388 keep = isl_space_dim(space, isl_dim_out);
9389 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9390 map = isl_map_reset_space(map, space);
9392 return map;
9395 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9397 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9398 __isl_take isl_map *map2)
9400 isl_map *prod;
9402 prod = isl_map_domain_product(map1, map2);
9403 prod = isl_map_flatten_domain(prod);
9404 return prod;
9407 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9409 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9410 __isl_take isl_map *map2)
9412 isl_map *prod;
9414 prod = isl_map_range_product(map1, map2);
9415 prod = isl_map_flatten_range(prod);
9416 return prod;
9419 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9421 int i;
9422 uint32_t hash = isl_hash_init();
9423 unsigned total;
9425 if (!bmap)
9426 return 0;
9427 bmap = isl_basic_map_copy(bmap);
9428 bmap = isl_basic_map_normalize(bmap);
9429 if (!bmap)
9430 return 0;
9431 total = isl_basic_map_total_dim(bmap);
9432 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9433 for (i = 0; i < bmap->n_eq; ++i) {
9434 uint32_t c_hash;
9435 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9436 isl_hash_hash(hash, c_hash);
9438 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9439 for (i = 0; i < bmap->n_ineq; ++i) {
9440 uint32_t c_hash;
9441 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9442 isl_hash_hash(hash, c_hash);
9444 isl_hash_byte(hash, bmap->n_div & 0xFF);
9445 for (i = 0; i < bmap->n_div; ++i) {
9446 uint32_t c_hash;
9447 if (isl_int_is_zero(bmap->div[i][0]))
9448 continue;
9449 isl_hash_byte(hash, i & 0xFF);
9450 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9451 isl_hash_hash(hash, c_hash);
9453 isl_basic_map_free(bmap);
9454 return hash;
9457 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9459 return isl_basic_map_get_hash((isl_basic_map *)bset);
9462 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9464 int i;
9465 uint32_t hash;
9467 if (!map)
9468 return 0;
9469 map = isl_map_copy(map);
9470 map = isl_map_normalize(map);
9471 if (!map)
9472 return 0;
9474 hash = isl_hash_init();
9475 for (i = 0; i < map->n; ++i) {
9476 uint32_t bmap_hash;
9477 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9478 isl_hash_hash(hash, bmap_hash);
9481 isl_map_free(map);
9483 return hash;
9486 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9488 return isl_map_get_hash((isl_map *)set);
9491 /* Check if the value for dimension dim is completely determined
9492 * by the values of the other parameters and variables.
9493 * That is, check if dimension dim is involved in an equality.
9495 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9497 int i;
9498 unsigned nparam;
9500 if (!bset)
9501 return -1;
9502 nparam = isl_basic_set_n_param(bset);
9503 for (i = 0; i < bset->n_eq; ++i)
9504 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9505 return 1;
9506 return 0;
9509 /* Check if the value for dimension dim is completely determined
9510 * by the values of the other parameters and variables.
9511 * That is, check if dimension dim is involved in an equality
9512 * for each of the subsets.
9514 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9516 int i;
9518 if (!set)
9519 return -1;
9520 for (i = 0; i < set->n; ++i) {
9521 int unique;
9522 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9523 if (unique != 1)
9524 return unique;
9526 return 1;
9529 int isl_set_n_basic_set(__isl_keep isl_set *set)
9531 return set ? set->n : 0;
9534 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9535 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9537 int i;
9539 if (!map)
9540 return -1;
9542 for (i = 0; i < map->n; ++i)
9543 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9544 return -1;
9546 return 0;
9549 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9550 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9552 int i;
9554 if (!set)
9555 return -1;
9557 for (i = 0; i < set->n; ++i)
9558 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9559 return -1;
9561 return 0;
9564 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9566 isl_space *dim;
9568 if (!bset)
9569 return NULL;
9571 bset = isl_basic_set_cow(bset);
9572 if (!bset)
9573 return NULL;
9575 dim = isl_basic_set_get_space(bset);
9576 dim = isl_space_lift(dim, bset->n_div);
9577 if (!dim)
9578 goto error;
9579 isl_space_free(bset->dim);
9580 bset->dim = dim;
9581 bset->extra -= bset->n_div;
9582 bset->n_div = 0;
9584 bset = isl_basic_set_finalize(bset);
9586 return bset;
9587 error:
9588 isl_basic_set_free(bset);
9589 return NULL;
9592 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9594 int i;
9595 isl_space *dim;
9596 unsigned n_div;
9598 set = isl_set_align_divs(set);
9600 if (!set)
9601 return NULL;
9603 set = isl_set_cow(set);
9604 if (!set)
9605 return NULL;
9607 n_div = set->p[0]->n_div;
9608 dim = isl_set_get_space(set);
9609 dim = isl_space_lift(dim, n_div);
9610 if (!dim)
9611 goto error;
9612 isl_space_free(set->dim);
9613 set->dim = dim;
9615 for (i = 0; i < set->n; ++i) {
9616 set->p[i] = isl_basic_set_lift(set->p[i]);
9617 if (!set->p[i])
9618 goto error;
9621 return set;
9622 error:
9623 isl_set_free(set);
9624 return NULL;
9627 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9629 isl_space *dim;
9630 struct isl_basic_map *bmap;
9631 unsigned n_set;
9632 unsigned n_div;
9633 unsigned n_param;
9634 unsigned total;
9635 int i, k, l;
9637 set = isl_set_align_divs(set);
9639 if (!set)
9640 return NULL;
9642 dim = isl_set_get_space(set);
9643 if (set->n == 0 || set->p[0]->n_div == 0) {
9644 isl_set_free(set);
9645 return isl_map_identity(isl_space_map_from_set(dim));
9648 n_div = set->p[0]->n_div;
9649 dim = isl_space_map_from_set(dim);
9650 n_param = isl_space_dim(dim, isl_dim_param);
9651 n_set = isl_space_dim(dim, isl_dim_in);
9652 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9653 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9654 for (i = 0; i < n_set; ++i)
9655 bmap = var_equal(bmap, i);
9657 total = n_param + n_set + n_set + n_div;
9658 for (i = 0; i < n_div; ++i) {
9659 k = isl_basic_map_alloc_inequality(bmap);
9660 if (k < 0)
9661 goto error;
9662 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9663 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9664 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9665 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9666 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9667 set->p[0]->div[i][0]);
9669 l = isl_basic_map_alloc_inequality(bmap);
9670 if (l < 0)
9671 goto error;
9672 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9673 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9674 set->p[0]->div[i][0]);
9675 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9678 isl_set_free(set);
9679 bmap = isl_basic_map_simplify(bmap);
9680 bmap = isl_basic_map_finalize(bmap);
9681 return isl_map_from_basic_map(bmap);
9682 error:
9683 isl_set_free(set);
9684 isl_basic_map_free(bmap);
9685 return NULL;
9688 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9690 unsigned dim;
9691 int size = 0;
9693 if (!bset)
9694 return -1;
9696 dim = isl_basic_set_total_dim(bset);
9697 size += bset->n_eq * (1 + dim);
9698 size += bset->n_ineq * (1 + dim);
9699 size += bset->n_div * (2 + dim);
9701 return size;
9704 int isl_set_size(__isl_keep isl_set *set)
9706 int i;
9707 int size = 0;
9709 if (!set)
9710 return -1;
9712 for (i = 0; i < set->n; ++i)
9713 size += isl_basic_set_size(set->p[i]);
9715 return size;
9718 /* Check if there is any lower bound (if lower == 0) and/or upper
9719 * bound (if upper == 0) on the specified dim.
9721 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9722 enum isl_dim_type type, unsigned pos, int lower, int upper)
9724 int i;
9726 if (!bmap)
9727 return -1;
9729 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9731 pos += isl_basic_map_offset(bmap, type);
9733 for (i = 0; i < bmap->n_div; ++i) {
9734 if (isl_int_is_zero(bmap->div[i][0]))
9735 continue;
9736 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9737 return 1;
9740 for (i = 0; i < bmap->n_eq; ++i)
9741 if (!isl_int_is_zero(bmap->eq[i][pos]))
9742 return 1;
9744 for (i = 0; i < bmap->n_ineq; ++i) {
9745 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
9746 if (sgn > 0)
9747 lower = 1;
9748 if (sgn < 0)
9749 upper = 1;
9752 return lower && upper;
9755 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9756 enum isl_dim_type type, unsigned pos)
9758 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9761 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9762 enum isl_dim_type type, unsigned pos)
9764 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9767 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9768 enum isl_dim_type type, unsigned pos)
9770 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9773 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9774 enum isl_dim_type type, unsigned pos)
9776 int i;
9778 if (!map)
9779 return -1;
9781 for (i = 0; i < map->n; ++i) {
9782 int bounded;
9783 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9784 if (bounded < 0 || !bounded)
9785 return bounded;
9788 return 1;
9791 /* Return 1 if the specified dim is involved in both an upper bound
9792 * and a lower bound.
9794 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9795 enum isl_dim_type type, unsigned pos)
9797 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9800 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9802 static int has_any_bound(__isl_keep isl_map *map,
9803 enum isl_dim_type type, unsigned pos,
9804 int (*fn)(__isl_keep isl_basic_map *bmap,
9805 enum isl_dim_type type, unsigned pos))
9807 int i;
9809 if (!map)
9810 return -1;
9812 for (i = 0; i < map->n; ++i) {
9813 int bounded;
9814 bounded = fn(map->p[i], type, pos);
9815 if (bounded < 0 || bounded)
9816 return bounded;
9819 return 0;
9822 /* Return 1 if the specified dim is involved in any lower bound.
9824 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
9825 enum isl_dim_type type, unsigned pos)
9827 return has_any_bound(set, type, pos,
9828 &isl_basic_map_dim_has_lower_bound);
9831 /* Return 1 if the specified dim is involved in any upper bound.
9833 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
9834 enum isl_dim_type type, unsigned pos)
9836 return has_any_bound(set, type, pos,
9837 &isl_basic_map_dim_has_upper_bound);
9840 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9842 static int has_bound(__isl_keep isl_map *map,
9843 enum isl_dim_type type, unsigned pos,
9844 int (*fn)(__isl_keep isl_basic_map *bmap,
9845 enum isl_dim_type type, unsigned pos))
9847 int i;
9849 if (!map)
9850 return -1;
9852 for (i = 0; i < map->n; ++i) {
9853 int bounded;
9854 bounded = fn(map->p[i], type, pos);
9855 if (bounded < 0 || !bounded)
9856 return bounded;
9859 return 1;
9862 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9864 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9865 enum isl_dim_type type, unsigned pos)
9867 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9870 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9872 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9873 enum isl_dim_type type, unsigned pos)
9875 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9878 /* For each of the "n" variables starting at "first", determine
9879 * the sign of the variable and put the results in the first "n"
9880 * elements of the array "signs".
9881 * Sign
9882 * 1 means that the variable is non-negative
9883 * -1 means that the variable is non-positive
9884 * 0 means the variable attains both positive and negative values.
9886 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9887 unsigned first, unsigned n, int *signs)
9889 isl_vec *bound = NULL;
9890 struct isl_tab *tab = NULL;
9891 struct isl_tab_undo *snap;
9892 int i;
9894 if (!bset || !signs)
9895 return -1;
9897 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9898 tab = isl_tab_from_basic_set(bset, 0);
9899 if (!bound || !tab)
9900 goto error;
9902 isl_seq_clr(bound->el, bound->size);
9903 isl_int_set_si(bound->el[0], -1);
9905 snap = isl_tab_snap(tab);
9906 for (i = 0; i < n; ++i) {
9907 int empty;
9909 isl_int_set_si(bound->el[1 + first + i], -1);
9910 if (isl_tab_add_ineq(tab, bound->el) < 0)
9911 goto error;
9912 empty = tab->empty;
9913 isl_int_set_si(bound->el[1 + first + i], 0);
9914 if (isl_tab_rollback(tab, snap) < 0)
9915 goto error;
9917 if (empty) {
9918 signs[i] = 1;
9919 continue;
9922 isl_int_set_si(bound->el[1 + first + i], 1);
9923 if (isl_tab_add_ineq(tab, bound->el) < 0)
9924 goto error;
9925 empty = tab->empty;
9926 isl_int_set_si(bound->el[1 + first + i], 0);
9927 if (isl_tab_rollback(tab, snap) < 0)
9928 goto error;
9930 signs[i] = empty ? -1 : 0;
9933 isl_tab_free(tab);
9934 isl_vec_free(bound);
9935 return 0;
9936 error:
9937 isl_tab_free(tab);
9938 isl_vec_free(bound);
9939 return -1;
9942 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
9943 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
9945 if (!bset || !signs)
9946 return -1;
9947 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
9948 return -1);
9950 first += pos(bset->dim, type) - 1;
9951 return isl_basic_set_vars_get_sign(bset, first, n, signs);
9954 /* Check if the given basic map is obviously single-valued.
9955 * In particular, for each output dimension, check that there is
9956 * an equality that defines the output dimension in terms of
9957 * earlier dimensions.
9959 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
9961 int i, j;
9962 unsigned total;
9963 unsigned n_out;
9964 unsigned o_out;
9966 if (!bmap)
9967 return -1;
9969 total = 1 + isl_basic_map_total_dim(bmap);
9970 n_out = isl_basic_map_dim(bmap, isl_dim_out);
9971 o_out = isl_basic_map_offset(bmap, isl_dim_out);
9973 for (i = 0; i < n_out; ++i) {
9974 for (j = 0; j < bmap->n_eq; ++j) {
9975 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
9976 continue;
9977 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
9978 total - (o_out + i + 1)) == -1)
9979 break;
9981 if (j >= bmap->n_eq)
9982 return 0;
9985 return 1;
9988 /* Check if the given basic map is single-valued.
9989 * We simply compute
9991 * M \circ M^-1
9993 * and check if the result is a subset of the identity mapping.
9995 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
9997 isl_space *space;
9998 isl_basic_map *test;
9999 isl_basic_map *id;
10000 int sv;
10002 sv = isl_basic_map_plain_is_single_valued(bmap);
10003 if (sv < 0 || sv)
10004 return sv;
10006 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10007 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10009 space = isl_basic_map_get_space(bmap);
10010 space = isl_space_map_from_set(isl_space_range(space));
10011 id = isl_basic_map_identity(space);
10013 sv = isl_basic_map_is_subset(test, id);
10015 isl_basic_map_free(test);
10016 isl_basic_map_free(id);
10018 return sv;
10021 /* Check if the given map is obviously single-valued.
10023 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10025 if (!map)
10026 return -1;
10027 if (map->n == 0)
10028 return 1;
10029 if (map->n >= 2)
10030 return 0;
10032 return isl_basic_map_plain_is_single_valued(map->p[0]);
10035 /* Check if the given map is single-valued.
10036 * We simply compute
10038 * M \circ M^-1
10040 * and check if the result is a subset of the identity mapping.
10042 int isl_map_is_single_valued(__isl_keep isl_map *map)
10044 isl_space *dim;
10045 isl_map *test;
10046 isl_map *id;
10047 int sv;
10049 sv = isl_map_plain_is_single_valued(map);
10050 if (sv < 0 || sv)
10051 return sv;
10053 test = isl_map_reverse(isl_map_copy(map));
10054 test = isl_map_apply_range(test, isl_map_copy(map));
10056 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10057 id = isl_map_identity(dim);
10059 sv = isl_map_is_subset(test, id);
10061 isl_map_free(test);
10062 isl_map_free(id);
10064 return sv;
10067 int isl_map_is_injective(__isl_keep isl_map *map)
10069 int in;
10071 map = isl_map_copy(map);
10072 map = isl_map_reverse(map);
10073 in = isl_map_is_single_valued(map);
10074 isl_map_free(map);
10076 return in;
10079 /* Check if the given map is obviously injective.
10081 int isl_map_plain_is_injective(__isl_keep isl_map *map)
10083 int in;
10085 map = isl_map_copy(map);
10086 map = isl_map_reverse(map);
10087 in = isl_map_plain_is_single_valued(map);
10088 isl_map_free(map);
10090 return in;
10093 int isl_map_is_bijective(__isl_keep isl_map *map)
10095 int sv;
10097 sv = isl_map_is_single_valued(map);
10098 if (sv < 0 || !sv)
10099 return sv;
10101 return isl_map_is_injective(map);
10104 int isl_set_is_singleton(__isl_keep isl_set *set)
10106 return isl_map_is_single_valued((isl_map *)set);
10109 int isl_map_is_translation(__isl_keep isl_map *map)
10111 int ok;
10112 isl_set *delta;
10114 delta = isl_map_deltas(isl_map_copy(map));
10115 ok = isl_set_is_singleton(delta);
10116 isl_set_free(delta);
10118 return ok;
10121 static int unique(isl_int *p, unsigned pos, unsigned len)
10123 if (isl_seq_first_non_zero(p, pos) != -1)
10124 return 0;
10125 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10126 return 0;
10127 return 1;
10130 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10132 int i, j;
10133 unsigned nvar;
10134 unsigned ovar;
10136 if (!bset)
10137 return -1;
10139 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10140 return 0;
10142 nvar = isl_basic_set_dim(bset, isl_dim_set);
10143 ovar = isl_space_offset(bset->dim, isl_dim_set);
10144 for (j = 0; j < nvar; ++j) {
10145 int lower = 0, upper = 0;
10146 for (i = 0; i < bset->n_eq; ++i) {
10147 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10148 continue;
10149 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10150 return 0;
10151 break;
10153 if (i < bset->n_eq)
10154 continue;
10155 for (i = 0; i < bset->n_ineq; ++i) {
10156 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10157 continue;
10158 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10159 return 0;
10160 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10161 lower = 1;
10162 else
10163 upper = 1;
10165 if (!lower || !upper)
10166 return 0;
10169 return 1;
10172 int isl_set_is_box(__isl_keep isl_set *set)
10174 if (!set)
10175 return -1;
10176 if (set->n != 1)
10177 return 0;
10179 return isl_basic_set_is_box(set->p[0]);
10182 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10184 if (!bset)
10185 return -1;
10187 return isl_space_is_wrapping(bset->dim);
10190 int isl_set_is_wrapping(__isl_keep isl_set *set)
10192 if (!set)
10193 return -1;
10195 return isl_space_is_wrapping(set->dim);
10198 /* Is the range of "map" a wrapped relation?
10200 int isl_map_range_is_wrapping(__isl_keep isl_map *map)
10202 if (!map)
10203 return -1;
10205 return isl_space_range_is_wrapping(map->dim);
10208 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10210 bmap = isl_basic_map_cow(bmap);
10211 if (!bmap)
10212 return NULL;
10214 bmap->dim = isl_space_wrap(bmap->dim);
10215 if (!bmap->dim)
10216 goto error;
10218 bmap = isl_basic_map_finalize(bmap);
10220 return (isl_basic_set *)bmap;
10221 error:
10222 isl_basic_map_free(bmap);
10223 return NULL;
10226 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10228 int i;
10230 map = isl_map_cow(map);
10231 if (!map)
10232 return NULL;
10234 for (i = 0; i < map->n; ++i) {
10235 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10236 if (!map->p[i])
10237 goto error;
10239 map->dim = isl_space_wrap(map->dim);
10240 if (!map->dim)
10241 goto error;
10243 return (isl_set *)map;
10244 error:
10245 isl_map_free(map);
10246 return NULL;
10249 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10251 bset = isl_basic_set_cow(bset);
10252 if (!bset)
10253 return NULL;
10255 bset->dim = isl_space_unwrap(bset->dim);
10256 if (!bset->dim)
10257 goto error;
10259 bset = isl_basic_set_finalize(bset);
10261 return (isl_basic_map *)bset;
10262 error:
10263 isl_basic_set_free(bset);
10264 return NULL;
10267 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10269 int i;
10271 if (!set)
10272 return NULL;
10274 if (!isl_set_is_wrapping(set))
10275 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10276 goto error);
10278 set = isl_set_cow(set);
10279 if (!set)
10280 return NULL;
10282 for (i = 0; i < set->n; ++i) {
10283 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10284 if (!set->p[i])
10285 goto error;
10288 set->dim = isl_space_unwrap(set->dim);
10289 if (!set->dim)
10290 goto error;
10292 return (isl_map *)set;
10293 error:
10294 isl_set_free(set);
10295 return NULL;
10298 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10299 enum isl_dim_type type)
10301 if (!bmap)
10302 return NULL;
10304 if (!isl_space_is_named_or_nested(bmap->dim, type))
10305 return bmap;
10307 bmap = isl_basic_map_cow(bmap);
10308 if (!bmap)
10309 return NULL;
10311 bmap->dim = isl_space_reset(bmap->dim, type);
10312 if (!bmap->dim)
10313 goto error;
10315 bmap = isl_basic_map_finalize(bmap);
10317 return bmap;
10318 error:
10319 isl_basic_map_free(bmap);
10320 return NULL;
10323 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10324 enum isl_dim_type type)
10326 int i;
10328 if (!map)
10329 return NULL;
10331 if (!isl_space_is_named_or_nested(map->dim, type))
10332 return map;
10334 map = isl_map_cow(map);
10335 if (!map)
10336 return NULL;
10338 for (i = 0; i < map->n; ++i) {
10339 map->p[i] = isl_basic_map_reset(map->p[i], type);
10340 if (!map->p[i])
10341 goto error;
10343 map->dim = isl_space_reset(map->dim, type);
10344 if (!map->dim)
10345 goto error;
10347 return map;
10348 error:
10349 isl_map_free(map);
10350 return NULL;
10353 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10355 if (!bmap)
10356 return NULL;
10358 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10359 return bmap;
10361 bmap = isl_basic_map_cow(bmap);
10362 if (!bmap)
10363 return NULL;
10365 bmap->dim = isl_space_flatten(bmap->dim);
10366 if (!bmap->dim)
10367 goto error;
10369 bmap = isl_basic_map_finalize(bmap);
10371 return bmap;
10372 error:
10373 isl_basic_map_free(bmap);
10374 return NULL;
10377 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10379 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10382 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10383 __isl_take isl_basic_map *bmap)
10385 if (!bmap)
10386 return NULL;
10388 if (!bmap->dim->nested[0])
10389 return bmap;
10391 bmap = isl_basic_map_cow(bmap);
10392 if (!bmap)
10393 return NULL;
10395 bmap->dim = isl_space_flatten_domain(bmap->dim);
10396 if (!bmap->dim)
10397 goto error;
10399 bmap = isl_basic_map_finalize(bmap);
10401 return bmap;
10402 error:
10403 isl_basic_map_free(bmap);
10404 return NULL;
10407 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10408 __isl_take isl_basic_map *bmap)
10410 if (!bmap)
10411 return NULL;
10413 if (!bmap->dim->nested[1])
10414 return bmap;
10416 bmap = isl_basic_map_cow(bmap);
10417 if (!bmap)
10418 return NULL;
10420 bmap->dim = isl_space_flatten_range(bmap->dim);
10421 if (!bmap->dim)
10422 goto error;
10424 bmap = isl_basic_map_finalize(bmap);
10426 return bmap;
10427 error:
10428 isl_basic_map_free(bmap);
10429 return NULL;
10432 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10434 int i;
10436 if (!map)
10437 return NULL;
10439 if (!map->dim->nested[0] && !map->dim->nested[1])
10440 return map;
10442 map = isl_map_cow(map);
10443 if (!map)
10444 return NULL;
10446 for (i = 0; i < map->n; ++i) {
10447 map->p[i] = isl_basic_map_flatten(map->p[i]);
10448 if (!map->p[i])
10449 goto error;
10451 map->dim = isl_space_flatten(map->dim);
10452 if (!map->dim)
10453 goto error;
10455 return map;
10456 error:
10457 isl_map_free(map);
10458 return NULL;
10461 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10463 return (isl_set *)isl_map_flatten((isl_map *)set);
10466 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10468 isl_space *dim, *flat_dim;
10469 isl_map *map;
10471 dim = isl_set_get_space(set);
10472 flat_dim = isl_space_flatten(isl_space_copy(dim));
10473 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10474 map = isl_map_intersect_domain(map, set);
10476 return map;
10479 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10481 int i;
10483 if (!map)
10484 return NULL;
10486 if (!map->dim->nested[0])
10487 return map;
10489 map = isl_map_cow(map);
10490 if (!map)
10491 return NULL;
10493 for (i = 0; i < map->n; ++i) {
10494 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10495 if (!map->p[i])
10496 goto error;
10498 map->dim = isl_space_flatten_domain(map->dim);
10499 if (!map->dim)
10500 goto error;
10502 return map;
10503 error:
10504 isl_map_free(map);
10505 return NULL;
10508 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10510 int i;
10512 if (!map)
10513 return NULL;
10515 if (!map->dim->nested[1])
10516 return map;
10518 map = isl_map_cow(map);
10519 if (!map)
10520 return NULL;
10522 for (i = 0; i < map->n; ++i) {
10523 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10524 if (!map->p[i])
10525 goto error;
10527 map->dim = isl_space_flatten_range(map->dim);
10528 if (!map->dim)
10529 goto error;
10531 return map;
10532 error:
10533 isl_map_free(map);
10534 return NULL;
10537 /* Reorder the dimensions of "bmap" according to the given dim_map
10538 * and set the dimension specification to "dim".
10540 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10541 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10543 isl_basic_map *res;
10544 unsigned flags;
10546 bmap = isl_basic_map_cow(bmap);
10547 if (!bmap || !dim || !dim_map)
10548 goto error;
10550 flags = bmap->flags;
10551 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10552 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10553 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10554 res = isl_basic_map_alloc_space(dim,
10555 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10556 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10557 if (res)
10558 res->flags = flags;
10559 res = isl_basic_map_finalize(res);
10560 return res;
10561 error:
10562 free(dim_map);
10563 isl_basic_map_free(bmap);
10564 isl_space_free(dim);
10565 return NULL;
10568 /* Reorder the dimensions of "map" according to given reordering.
10570 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10571 __isl_take isl_reordering *r)
10573 int i;
10574 struct isl_dim_map *dim_map;
10576 map = isl_map_cow(map);
10577 dim_map = isl_dim_map_from_reordering(r);
10578 if (!map || !r || !dim_map)
10579 goto error;
10581 for (i = 0; i < map->n; ++i) {
10582 struct isl_dim_map *dim_map_i;
10584 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10586 map->p[i] = isl_basic_map_realign(map->p[i],
10587 isl_space_copy(r->dim), dim_map_i);
10589 if (!map->p[i])
10590 goto error;
10593 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10595 isl_reordering_free(r);
10596 free(dim_map);
10597 return map;
10598 error:
10599 free(dim_map);
10600 isl_map_free(map);
10601 isl_reordering_free(r);
10602 return NULL;
10605 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10606 __isl_take isl_reordering *r)
10608 return (isl_set *)isl_map_realign((isl_map *)set, r);
10611 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10612 __isl_take isl_space *model)
10614 isl_ctx *ctx;
10616 if (!map || !model)
10617 goto error;
10619 ctx = isl_space_get_ctx(model);
10620 if (!isl_space_has_named_params(model))
10621 isl_die(ctx, isl_error_invalid,
10622 "model has unnamed parameters", goto error);
10623 if (!isl_space_has_named_params(map->dim))
10624 isl_die(ctx, isl_error_invalid,
10625 "relation has unnamed parameters", goto error);
10626 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10627 isl_reordering *exp;
10629 model = isl_space_drop_dims(model, isl_dim_in,
10630 0, isl_space_dim(model, isl_dim_in));
10631 model = isl_space_drop_dims(model, isl_dim_out,
10632 0, isl_space_dim(model, isl_dim_out));
10633 exp = isl_parameter_alignment_reordering(map->dim, model);
10634 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10635 map = isl_map_realign(map, exp);
10638 isl_space_free(model);
10639 return map;
10640 error:
10641 isl_space_free(model);
10642 isl_map_free(map);
10643 return NULL;
10646 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10647 __isl_take isl_space *model)
10649 return isl_map_align_params(set, model);
10652 /* Align the parameters of "bmap" to those of "model", introducing
10653 * additional parameters if needed.
10655 __isl_give isl_basic_map *isl_basic_map_align_params(
10656 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10658 isl_ctx *ctx;
10660 if (!bmap || !model)
10661 goto error;
10663 ctx = isl_space_get_ctx(model);
10664 if (!isl_space_has_named_params(model))
10665 isl_die(ctx, isl_error_invalid,
10666 "model has unnamed parameters", goto error);
10667 if (!isl_space_has_named_params(bmap->dim))
10668 isl_die(ctx, isl_error_invalid,
10669 "relation has unnamed parameters", goto error);
10670 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10671 isl_reordering *exp;
10672 struct isl_dim_map *dim_map;
10674 model = isl_space_drop_dims(model, isl_dim_in,
10675 0, isl_space_dim(model, isl_dim_in));
10676 model = isl_space_drop_dims(model, isl_dim_out,
10677 0, isl_space_dim(model, isl_dim_out));
10678 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10679 exp = isl_reordering_extend_space(exp,
10680 isl_basic_map_get_space(bmap));
10681 dim_map = isl_dim_map_from_reordering(exp);
10682 bmap = isl_basic_map_realign(bmap,
10683 exp ? isl_space_copy(exp->dim) : NULL,
10684 isl_dim_map_extend(dim_map, bmap));
10685 isl_reordering_free(exp);
10686 free(dim_map);
10689 isl_space_free(model);
10690 return bmap;
10691 error:
10692 isl_space_free(model);
10693 isl_basic_map_free(bmap);
10694 return NULL;
10697 /* Align the parameters of "bset" to those of "model", introducing
10698 * additional parameters if needed.
10700 __isl_give isl_basic_set *isl_basic_set_align_params(
10701 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10703 return isl_basic_map_align_params(bset, model);
10706 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10707 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10708 enum isl_dim_type c2, enum isl_dim_type c3,
10709 enum isl_dim_type c4, enum isl_dim_type c5)
10711 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10712 struct isl_mat *mat;
10713 int i, j, k;
10714 int pos;
10716 if (!bmap)
10717 return NULL;
10718 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
10719 isl_basic_map_total_dim(bmap) + 1);
10720 if (!mat)
10721 return NULL;
10722 for (i = 0; i < bmap->n_eq; ++i)
10723 for (j = 0, pos = 0; j < 5; ++j) {
10724 int off = isl_basic_map_offset(bmap, c[j]);
10725 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10726 isl_int_set(mat->row[i][pos],
10727 bmap->eq[i][off + k]);
10728 ++pos;
10732 return mat;
10735 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
10736 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10737 enum isl_dim_type c2, enum isl_dim_type c3,
10738 enum isl_dim_type c4, enum isl_dim_type c5)
10740 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10741 struct isl_mat *mat;
10742 int i, j, k;
10743 int pos;
10745 if (!bmap)
10746 return NULL;
10747 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
10748 isl_basic_map_total_dim(bmap) + 1);
10749 if (!mat)
10750 return NULL;
10751 for (i = 0; i < bmap->n_ineq; ++i)
10752 for (j = 0, pos = 0; j < 5; ++j) {
10753 int off = isl_basic_map_offset(bmap, c[j]);
10754 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10755 isl_int_set(mat->row[i][pos],
10756 bmap->ineq[i][off + k]);
10757 ++pos;
10761 return mat;
10764 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
10765 __isl_take isl_space *dim,
10766 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10767 enum isl_dim_type c2, enum isl_dim_type c3,
10768 enum isl_dim_type c4, enum isl_dim_type c5)
10770 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10771 isl_basic_map *bmap;
10772 unsigned total;
10773 unsigned extra;
10774 int i, j, k, l;
10775 int pos;
10777 if (!dim || !eq || !ineq)
10778 goto error;
10780 if (eq->n_col != ineq->n_col)
10781 isl_die(dim->ctx, isl_error_invalid,
10782 "equalities and inequalities matrices should have "
10783 "same number of columns", goto error);
10785 total = 1 + isl_space_dim(dim, isl_dim_all);
10787 if (eq->n_col < total)
10788 isl_die(dim->ctx, isl_error_invalid,
10789 "number of columns too small", goto error);
10791 extra = eq->n_col - total;
10793 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
10794 eq->n_row, ineq->n_row);
10795 if (!bmap)
10796 goto error;
10797 for (i = 0; i < extra; ++i) {
10798 k = isl_basic_map_alloc_div(bmap);
10799 if (k < 0)
10800 goto error;
10801 isl_int_set_si(bmap->div[k][0], 0);
10803 for (i = 0; i < eq->n_row; ++i) {
10804 l = isl_basic_map_alloc_equality(bmap);
10805 if (l < 0)
10806 goto error;
10807 for (j = 0, pos = 0; j < 5; ++j) {
10808 int off = isl_basic_map_offset(bmap, c[j]);
10809 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10810 isl_int_set(bmap->eq[l][off + k],
10811 eq->row[i][pos]);
10812 ++pos;
10816 for (i = 0; i < ineq->n_row; ++i) {
10817 l = isl_basic_map_alloc_inequality(bmap);
10818 if (l < 0)
10819 goto error;
10820 for (j = 0, pos = 0; j < 5; ++j) {
10821 int off = isl_basic_map_offset(bmap, c[j]);
10822 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10823 isl_int_set(bmap->ineq[l][off + k],
10824 ineq->row[i][pos]);
10825 ++pos;
10830 isl_space_free(dim);
10831 isl_mat_free(eq);
10832 isl_mat_free(ineq);
10834 bmap = isl_basic_map_simplify(bmap);
10835 return isl_basic_map_finalize(bmap);
10836 error:
10837 isl_space_free(dim);
10838 isl_mat_free(eq);
10839 isl_mat_free(ineq);
10840 return NULL;
10843 __isl_give isl_mat *isl_basic_set_equalities_matrix(
10844 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10845 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10847 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
10848 c1, c2, c3, c4, isl_dim_in);
10851 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
10852 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10853 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10855 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
10856 c1, c2, c3, c4, isl_dim_in);
10859 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
10860 __isl_take isl_space *dim,
10861 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10862 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10864 return (isl_basic_set*)
10865 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
10866 c1, c2, c3, c4, isl_dim_in);
10869 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
10871 if (!bmap)
10872 return -1;
10874 return isl_space_can_zip(bmap->dim);
10877 int isl_map_can_zip(__isl_keep isl_map *map)
10879 if (!map)
10880 return -1;
10882 return isl_space_can_zip(map->dim);
10885 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10886 * (A -> C) -> (B -> D).
10888 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10890 unsigned pos;
10891 unsigned n1;
10892 unsigned n2;
10894 if (!bmap)
10895 return NULL;
10897 if (!isl_basic_map_can_zip(bmap))
10898 isl_die(bmap->ctx, isl_error_invalid,
10899 "basic map cannot be zipped", goto error);
10900 pos = isl_basic_map_offset(bmap, isl_dim_in) +
10901 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10902 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10903 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10904 bmap = isl_basic_map_cow(bmap);
10905 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10906 if (!bmap)
10907 return NULL;
10908 bmap->dim = isl_space_zip(bmap->dim);
10909 if (!bmap->dim)
10910 goto error;
10911 return bmap;
10912 error:
10913 isl_basic_map_free(bmap);
10914 return NULL;
10917 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10918 * (A -> C) -> (B -> D).
10920 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
10922 int i;
10924 if (!map)
10925 return NULL;
10927 if (!isl_map_can_zip(map))
10928 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
10929 goto error);
10931 map = isl_map_cow(map);
10932 if (!map)
10933 return NULL;
10935 for (i = 0; i < map->n; ++i) {
10936 map->p[i] = isl_basic_map_zip(map->p[i]);
10937 if (!map->p[i])
10938 goto error;
10941 map->dim = isl_space_zip(map->dim);
10942 if (!map->dim)
10943 goto error;
10945 return map;
10946 error:
10947 isl_map_free(map);
10948 return NULL;
10951 /* Can we apply isl_basic_map_curry to "bmap"?
10952 * That is, does it have a nested relation in its domain?
10954 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
10956 if (!bmap)
10957 return -1;
10959 return isl_space_can_curry(bmap->dim);
10962 /* Can we apply isl_map_curry to "map"?
10963 * That is, does it have a nested relation in its domain?
10965 int isl_map_can_curry(__isl_keep isl_map *map)
10967 if (!map)
10968 return -1;
10970 return isl_space_can_curry(map->dim);
10973 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10974 * A -> (B -> C).
10976 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
10979 if (!bmap)
10980 return NULL;
10982 if (!isl_basic_map_can_curry(bmap))
10983 isl_die(bmap->ctx, isl_error_invalid,
10984 "basic map cannot be curried", goto error);
10985 bmap = isl_basic_map_cow(bmap);
10986 if (!bmap)
10987 return NULL;
10988 bmap->dim = isl_space_curry(bmap->dim);
10989 if (!bmap->dim)
10990 goto error;
10991 return bmap;
10992 error:
10993 isl_basic_map_free(bmap);
10994 return NULL;
10997 /* Given a map (A -> B) -> C, return the corresponding map
10998 * A -> (B -> C).
11000 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11002 int i;
11004 if (!map)
11005 return NULL;
11007 if (!isl_map_can_curry(map))
11008 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11009 goto error);
11011 map = isl_map_cow(map);
11012 if (!map)
11013 return NULL;
11015 for (i = 0; i < map->n; ++i) {
11016 map->p[i] = isl_basic_map_curry(map->p[i]);
11017 if (!map->p[i])
11018 goto error;
11021 map->dim = isl_space_curry(map->dim);
11022 if (!map->dim)
11023 goto error;
11025 return map;
11026 error:
11027 isl_map_free(map);
11028 return NULL;
11031 /* Can we apply isl_basic_map_uncurry to "bmap"?
11032 * That is, does it have a nested relation in its domain?
11034 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11036 if (!bmap)
11037 return -1;
11039 return isl_space_can_uncurry(bmap->dim);
11042 /* Can we apply isl_map_uncurry to "map"?
11043 * That is, does it have a nested relation in its domain?
11045 int isl_map_can_uncurry(__isl_keep isl_map *map)
11047 if (!map)
11048 return -1;
11050 return isl_space_can_uncurry(map->dim);
11053 /* Given a basic map A -> (B -> C), return the corresponding basic map
11054 * (A -> B) -> C.
11056 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11059 if (!bmap)
11060 return NULL;
11062 if (!isl_basic_map_can_uncurry(bmap))
11063 isl_die(bmap->ctx, isl_error_invalid,
11064 "basic map cannot be uncurried",
11065 return isl_basic_map_free(bmap));
11066 bmap = isl_basic_map_cow(bmap);
11067 if (!bmap)
11068 return NULL;
11069 bmap->dim = isl_space_uncurry(bmap->dim);
11070 if (!bmap->dim)
11071 return isl_basic_map_free(bmap);
11072 return bmap;
11075 /* Given a map A -> (B -> C), return the corresponding map
11076 * (A -> B) -> C.
11078 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11080 int i;
11082 if (!map)
11083 return NULL;
11085 if (!isl_map_can_uncurry(map))
11086 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11087 return isl_map_free(map));
11089 map = isl_map_cow(map);
11090 if (!map)
11091 return NULL;
11093 for (i = 0; i < map->n; ++i) {
11094 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11095 if (!map->p[i])
11096 return isl_map_free(map);
11099 map->dim = isl_space_uncurry(map->dim);
11100 if (!map->dim)
11101 return isl_map_free(map);
11103 return map;
11106 /* Construct a basic map mapping the domain of the affine expression
11107 * to a one-dimensional range prescribed by the affine expression.
11109 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11111 int k;
11112 int pos;
11113 isl_local_space *ls;
11114 isl_basic_map *bmap;
11116 if (!aff)
11117 return NULL;
11119 ls = isl_aff_get_local_space(aff);
11120 bmap = isl_basic_map_from_local_space(ls);
11121 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11122 k = isl_basic_map_alloc_equality(bmap);
11123 if (k < 0)
11124 goto error;
11126 pos = isl_basic_map_offset(bmap, isl_dim_out);
11127 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11128 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11129 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11130 aff->v->size - (pos + 1));
11132 isl_aff_free(aff);
11133 bmap = isl_basic_map_finalize(bmap);
11134 return bmap;
11135 error:
11136 isl_aff_free(aff);
11137 isl_basic_map_free(bmap);
11138 return NULL;
11141 /* Construct a map mapping the domain of the affine expression
11142 * to a one-dimensional range prescribed by the affine expression.
11144 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11146 isl_basic_map *bmap;
11148 bmap = isl_basic_map_from_aff(aff);
11149 return isl_map_from_basic_map(bmap);
11152 /* Construct a basic map mapping the domain the multi-affine expression
11153 * to its range, with each dimension in the range equated to the
11154 * corresponding affine expression.
11156 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11157 __isl_take isl_multi_aff *maff)
11159 int i;
11160 isl_space *space;
11161 isl_basic_map *bmap;
11163 if (!maff)
11164 return NULL;
11166 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11167 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11168 "invalid space", return isl_multi_aff_free(maff));
11170 space = isl_space_domain(isl_multi_aff_get_space(maff));
11171 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11173 for (i = 0; i < maff->n; ++i) {
11174 isl_aff *aff;
11175 isl_basic_map *bmap_i;
11177 aff = isl_aff_copy(maff->p[i]);
11178 bmap_i = isl_basic_map_from_aff(aff);
11180 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11183 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11185 isl_multi_aff_free(maff);
11186 return bmap;
11189 /* Construct a map mapping the domain the multi-affine expression
11190 * to its range, with each dimension in the range equated to the
11191 * corresponding affine expression.
11193 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11195 isl_basic_map *bmap;
11197 bmap = isl_basic_map_from_multi_aff(maff);
11198 return isl_map_from_basic_map(bmap);
11201 /* Construct a basic map mapping a domain in the given space to
11202 * to an n-dimensional range, with n the number of elements in the list,
11203 * where each coordinate in the range is prescribed by the
11204 * corresponding affine expression.
11205 * The domains of all affine expressions in the list are assumed to match
11206 * domain_dim.
11208 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11209 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11211 int i;
11212 isl_space *dim;
11213 isl_basic_map *bmap;
11215 if (!list)
11216 return NULL;
11218 dim = isl_space_from_domain(domain_dim);
11219 bmap = isl_basic_map_universe(dim);
11221 for (i = 0; i < list->n; ++i) {
11222 isl_aff *aff;
11223 isl_basic_map *bmap_i;
11225 aff = isl_aff_copy(list->p[i]);
11226 bmap_i = isl_basic_map_from_aff(aff);
11228 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11231 isl_aff_list_free(list);
11232 return bmap;
11235 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11236 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11238 return isl_map_equate(set, type1, pos1, type2, pos2);
11241 /* Construct a basic map where the given dimensions are equal to each other.
11243 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11244 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11246 isl_basic_map *bmap = NULL;
11247 int i;
11249 if (!space)
11250 return NULL;
11252 if (pos1 >= isl_space_dim(space, type1))
11253 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11254 "index out of bounds", goto error);
11255 if (pos2 >= isl_space_dim(space, type2))
11256 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11257 "index out of bounds", goto error);
11259 if (type1 == type2 && pos1 == pos2)
11260 return isl_basic_map_universe(space);
11262 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11263 i = isl_basic_map_alloc_equality(bmap);
11264 if (i < 0)
11265 goto error;
11266 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11267 pos1 += isl_basic_map_offset(bmap, type1);
11268 pos2 += isl_basic_map_offset(bmap, type2);
11269 isl_int_set_si(bmap->eq[i][pos1], -1);
11270 isl_int_set_si(bmap->eq[i][pos2], 1);
11271 bmap = isl_basic_map_finalize(bmap);
11272 isl_space_free(space);
11273 return bmap;
11274 error:
11275 isl_space_free(space);
11276 isl_basic_map_free(bmap);
11277 return NULL;
11280 /* Add a constraint imposing that the given two dimensions are equal.
11282 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11283 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11285 isl_basic_map *eq;
11287 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11289 bmap = isl_basic_map_intersect(bmap, eq);
11291 return bmap;
11294 /* Add a constraint imposing that the given two dimensions are equal.
11296 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11297 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11299 isl_basic_map *bmap;
11301 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11303 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11305 return map;
11308 /* Add a constraint imposing that the given two dimensions have opposite values.
11310 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11311 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11313 isl_basic_map *bmap = NULL;
11314 int i;
11316 if (!map)
11317 return NULL;
11319 if (pos1 >= isl_map_dim(map, type1))
11320 isl_die(map->ctx, isl_error_invalid,
11321 "index out of bounds", goto error);
11322 if (pos2 >= isl_map_dim(map, type2))
11323 isl_die(map->ctx, isl_error_invalid,
11324 "index out of bounds", goto error);
11326 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11327 i = isl_basic_map_alloc_equality(bmap);
11328 if (i < 0)
11329 goto error;
11330 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11331 pos1 += isl_basic_map_offset(bmap, type1);
11332 pos2 += isl_basic_map_offset(bmap, type2);
11333 isl_int_set_si(bmap->eq[i][pos1], 1);
11334 isl_int_set_si(bmap->eq[i][pos2], 1);
11335 bmap = isl_basic_map_finalize(bmap);
11337 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11339 return map;
11340 error:
11341 isl_basic_map_free(bmap);
11342 isl_map_free(map);
11343 return NULL;
11346 /* Add a constraint imposing that the value of the first dimension is
11347 * greater than or equal to that of the second.
11349 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11350 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11352 isl_constraint *c;
11353 isl_local_space *ls;
11355 if (!bmap)
11356 return NULL;
11358 if (pos1 >= isl_basic_map_dim(bmap, type1))
11359 isl_die(bmap->ctx, isl_error_invalid,
11360 "index out of bounds", return isl_basic_map_free(bmap));
11361 if (pos2 >= isl_basic_map_dim(bmap, type2))
11362 isl_die(bmap->ctx, isl_error_invalid,
11363 "index out of bounds", return isl_basic_map_free(bmap));
11365 if (type1 == type2 && pos1 == pos2)
11366 return bmap;
11368 ls = isl_local_space_from_space(isl_basic_map_get_space(bmap));
11369 c = isl_inequality_alloc(ls);
11370 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11371 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11372 bmap = isl_basic_map_add_constraint(bmap, c);
11374 return bmap;
11377 /* Construct a basic map where the value of the first dimension is
11378 * greater than that of the second.
11380 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11381 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11383 isl_basic_map *bmap = NULL;
11384 int i;
11386 if (!space)
11387 return NULL;
11389 if (pos1 >= isl_space_dim(space, type1))
11390 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11391 "index out of bounds", goto error);
11392 if (pos2 >= isl_space_dim(space, type2))
11393 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11394 "index out of bounds", goto error);
11396 if (type1 == type2 && pos1 == pos2)
11397 return isl_basic_map_empty(space);
11399 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11400 i = isl_basic_map_alloc_inequality(bmap);
11401 if (i < 0)
11402 goto error;
11403 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11404 pos1 += isl_basic_map_offset(bmap, type1);
11405 pos2 += isl_basic_map_offset(bmap, type2);
11406 isl_int_set_si(bmap->ineq[i][pos1], 1);
11407 isl_int_set_si(bmap->ineq[i][pos2], -1);
11408 isl_int_set_si(bmap->ineq[i][0], -1);
11409 bmap = isl_basic_map_finalize(bmap);
11411 return bmap;
11412 error:
11413 isl_space_free(space);
11414 isl_basic_map_free(bmap);
11415 return NULL;
11418 /* Add a constraint imposing that the value of the first dimension is
11419 * greater than that of the second.
11421 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11422 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11424 isl_basic_map *gt;
11426 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11428 bmap = isl_basic_map_intersect(bmap, gt);
11430 return bmap;
11433 /* Add a constraint imposing that the value of the first dimension is
11434 * greater than that of the second.
11436 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11437 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11439 isl_basic_map *bmap;
11441 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11443 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11445 return map;
11448 /* Add a constraint imposing that the value of the first dimension is
11449 * smaller than that of the second.
11451 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11452 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11454 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11457 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11458 int pos)
11460 isl_aff *div;
11461 isl_local_space *ls;
11463 if (!bmap)
11464 return NULL;
11466 if (!isl_basic_map_divs_known(bmap))
11467 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11468 "some divs are unknown", return NULL);
11470 ls = isl_basic_map_get_local_space(bmap);
11471 div = isl_local_space_get_div(ls, pos);
11472 isl_local_space_free(ls);
11474 return div;
11477 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11478 int pos)
11480 return isl_basic_map_get_div(bset, pos);
11483 /* Plug in "subs" for dimension "type", "pos" of "bset".
11485 * Let i be the dimension to replace and let "subs" be of the form
11487 * f/d
11489 * Any integer division with a non-zero coefficient for i,
11491 * floor((a i + g)/m)
11493 * is replaced by
11495 * floor((a f + d g)/(m d))
11497 * Constraints of the form
11499 * a i + g
11501 * are replaced by
11503 * a f + d g
11505 * We currently require that "subs" is an integral expression.
11506 * Handling rational expressions may require us to add stride constraints
11507 * as we do in isl_basic_set_preimage_multi_aff.
11509 __isl_give isl_basic_set *isl_basic_set_substitute(
11510 __isl_take isl_basic_set *bset,
11511 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11513 int i;
11514 isl_int v;
11515 isl_ctx *ctx;
11517 if (bset && isl_basic_set_plain_is_empty(bset))
11518 return bset;
11520 bset = isl_basic_set_cow(bset);
11521 if (!bset || !subs)
11522 goto error;
11524 ctx = isl_basic_set_get_ctx(bset);
11525 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11526 isl_die(ctx, isl_error_invalid,
11527 "spaces don't match", goto error);
11528 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11529 isl_die(ctx, isl_error_unsupported,
11530 "cannot handle divs yet", goto error);
11531 if (!isl_int_is_one(subs->v->el[0]))
11532 isl_die(ctx, isl_error_invalid,
11533 "can only substitute integer expressions", goto error);
11535 pos += isl_basic_set_offset(bset, type);
11537 isl_int_init(v);
11539 for (i = 0; i < bset->n_eq; ++i) {
11540 if (isl_int_is_zero(bset->eq[i][pos]))
11541 continue;
11542 isl_int_set(v, bset->eq[i][pos]);
11543 isl_int_set_si(bset->eq[i][pos], 0);
11544 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11545 v, subs->v->el + 1, subs->v->size - 1);
11548 for (i = 0; i < bset->n_ineq; ++i) {
11549 if (isl_int_is_zero(bset->ineq[i][pos]))
11550 continue;
11551 isl_int_set(v, bset->ineq[i][pos]);
11552 isl_int_set_si(bset->ineq[i][pos], 0);
11553 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11554 v, subs->v->el + 1, subs->v->size - 1);
11557 for (i = 0; i < bset->n_div; ++i) {
11558 if (isl_int_is_zero(bset->div[i][1 + pos]))
11559 continue;
11560 isl_int_set(v, bset->div[i][1 + pos]);
11561 isl_int_set_si(bset->div[i][1 + pos], 0);
11562 isl_seq_combine(bset->div[i] + 1,
11563 subs->v->el[0], bset->div[i] + 1,
11564 v, subs->v->el + 1, subs->v->size - 1);
11565 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11568 isl_int_clear(v);
11570 bset = isl_basic_set_simplify(bset);
11571 return isl_basic_set_finalize(bset);
11572 error:
11573 isl_basic_set_free(bset);
11574 return NULL;
11577 /* Plug in "subs" for dimension "type", "pos" of "set".
11579 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11580 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11582 int i;
11584 if (set && isl_set_plain_is_empty(set))
11585 return set;
11587 set = isl_set_cow(set);
11588 if (!set || !subs)
11589 goto error;
11591 for (i = set->n - 1; i >= 0; --i) {
11592 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11593 if (remove_if_empty(set, i) < 0)
11594 goto error;
11597 return set;
11598 error:
11599 isl_set_free(set);
11600 return NULL;
11603 /* Check if the range of "ma" is compatible with the domain or range
11604 * (depending on "type") of "bmap".
11605 * Return -1 if anything is wrong.
11607 static int check_basic_map_compatible_range_multi_aff(
11608 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11609 __isl_keep isl_multi_aff *ma)
11611 int m;
11612 isl_space *ma_space;
11614 ma_space = isl_multi_aff_get_space(ma);
11615 m = isl_space_tuple_match(bmap->dim, type, ma_space, isl_dim_out);
11616 isl_space_free(ma_space);
11617 if (m >= 0 && !m)
11618 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11619 "spaces don't match", return -1);
11620 return m;
11623 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11624 * coefficients before the transformed range of dimensions,
11625 * the "n_after" coefficients after the transformed range of dimensions
11626 * and the coefficients of the other divs in "bmap".
11628 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
11629 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
11631 int i;
11632 int n_param;
11633 int n_set;
11634 isl_local_space *ls;
11636 if (n_div == 0)
11637 return 0;
11639 ls = isl_aff_get_domain_local_space(ma->p[0]);
11640 if (!ls)
11641 return -1;
11643 n_param = isl_local_space_dim(ls, isl_dim_param);
11644 n_set = isl_local_space_dim(ls, isl_dim_set);
11645 for (i = 0; i < n_div; ++i) {
11646 int o_bmap = 0, o_ls = 0;
11648 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
11649 o_bmap += 1 + 1 + n_param;
11650 o_ls += 1 + 1 + n_param;
11651 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
11652 o_bmap += n_before;
11653 isl_seq_cpy(bmap->div[i] + o_bmap,
11654 ls->div->row[i] + o_ls, n_set);
11655 o_bmap += n_set;
11656 o_ls += n_set;
11657 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
11658 o_bmap += n_after;
11659 isl_seq_cpy(bmap->div[i] + o_bmap,
11660 ls->div->row[i] + o_ls, n_div);
11661 o_bmap += n_div;
11662 o_ls += n_div;
11663 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
11664 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
11665 goto error;
11668 isl_local_space_free(ls);
11669 return 0;
11670 error:
11671 isl_local_space_free(ls);
11672 return -1;
11675 /* How many stride constraints does "ma" enforce?
11676 * That is, how many of the affine expressions have a denominator
11677 * different from one?
11679 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
11681 int i;
11682 int strides = 0;
11684 for (i = 0; i < ma->n; ++i)
11685 if (!isl_int_is_one(ma->p[i]->v->el[0]))
11686 strides++;
11688 return strides;
11691 /* For each affine expression in ma of the form
11693 * x_i = (f_i y + h_i)/m_i
11695 * with m_i different from one, add a constraint to "bmap"
11696 * of the form
11698 * f_i y + h_i = m_i alpha_i
11700 * with alpha_i an additional existentially quantified variable.
11702 static __isl_give isl_basic_map *add_ma_strides(
11703 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
11704 int n_before, int n_after)
11706 int i, k;
11707 int div;
11708 int total;
11709 int n_param;
11710 int n_in;
11711 int n_div;
11713 total = isl_basic_map_total_dim(bmap);
11714 n_param = isl_multi_aff_dim(ma, isl_dim_param);
11715 n_in = isl_multi_aff_dim(ma, isl_dim_in);
11716 n_div = isl_multi_aff_dim(ma, isl_dim_div);
11717 for (i = 0; i < ma->n; ++i) {
11718 int o_bmap = 0, o_ma = 1;
11720 if (isl_int_is_one(ma->p[i]->v->el[0]))
11721 continue;
11722 div = isl_basic_map_alloc_div(bmap);
11723 k = isl_basic_map_alloc_equality(bmap);
11724 if (div < 0 || k < 0)
11725 goto error;
11726 isl_int_set_si(bmap->div[div][0], 0);
11727 isl_seq_cpy(bmap->eq[k] + o_bmap,
11728 ma->p[i]->v->el + o_ma, 1 + n_param);
11729 o_bmap += 1 + n_param;
11730 o_ma += 1 + n_param;
11731 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
11732 o_bmap += n_before;
11733 isl_seq_cpy(bmap->eq[k] + o_bmap,
11734 ma->p[i]->v->el + o_ma, n_in);
11735 o_bmap += n_in;
11736 o_ma += n_in;
11737 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
11738 o_bmap += n_after;
11739 isl_seq_cpy(bmap->eq[k] + o_bmap,
11740 ma->p[i]->v->el + o_ma, n_div);
11741 o_bmap += n_div;
11742 o_ma += n_div;
11743 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
11744 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
11745 total++;
11748 return bmap;
11749 error:
11750 isl_basic_map_free(bmap);
11751 return NULL;
11754 /* Replace the domain or range space (depending on "type) of "space" by "set".
11756 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
11757 enum isl_dim_type type, __isl_take isl_space *set)
11759 if (type == isl_dim_in) {
11760 space = isl_space_range(space);
11761 space = isl_space_map_from_domain_and_range(set, space);
11762 } else {
11763 space = isl_space_domain(space);
11764 space = isl_space_map_from_domain_and_range(space, set);
11767 return space;
11770 /* Compute the preimage of the domain or range (depending on "type")
11771 * of "bmap" under the function represented by "ma".
11772 * In other words, plug in "ma" in the domain or range of "bmap".
11773 * The result is a basic map that lives in the same space as "bmap"
11774 * except that the domain or range has been replaced by
11775 * the domain space of "ma".
11777 * If bmap is represented by
11779 * A(p) + S u + B x + T v + C(divs) >= 0,
11781 * where u and x are input and output dimensions if type == isl_dim_out
11782 * while x and v are input and output dimensions if type == isl_dim_in,
11783 * and ma is represented by
11785 * x = D(p) + F(y) + G(divs')
11787 * then the result is
11789 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11791 * The divs in the input set are similarly adjusted.
11792 * In particular
11794 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11796 * becomes
11798 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11799 * B_i G(divs') + c_i(divs))/n_i)
11801 * If bmap is not a rational map and if F(y) involves any denominators
11803 * x_i = (f_i y + h_i)/m_i
11805 * then additional constraints are added to ensure that we only
11806 * map back integer points. That is we enforce
11808 * f_i y + h_i = m_i alpha_i
11810 * with alpha_i an additional existentially quantified variable.
11812 * We first copy over the divs from "ma".
11813 * Then we add the modified constraints and divs from "bmap".
11814 * Finally, we add the stride constraints, if needed.
11816 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
11817 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
11818 __isl_take isl_multi_aff *ma)
11820 int i, k;
11821 isl_space *space;
11822 isl_basic_map *res = NULL;
11823 int n_before, n_after, n_div_bmap, n_div_ma;
11824 isl_int f, c1, c2, g;
11825 int rational, strides;
11827 isl_int_init(f);
11828 isl_int_init(c1);
11829 isl_int_init(c2);
11830 isl_int_init(g);
11832 ma = isl_multi_aff_align_divs(ma);
11833 if (!bmap || !ma)
11834 goto error;
11835 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
11836 goto error;
11838 if (type == isl_dim_in) {
11839 n_before = 0;
11840 n_after = isl_basic_map_dim(bmap, isl_dim_out);
11841 } else {
11842 n_before = isl_basic_map_dim(bmap, isl_dim_in);
11843 n_after = 0;
11845 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
11846 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
11848 space = isl_multi_aff_get_domain_space(ma);
11849 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
11850 rational = isl_basic_map_is_rational(bmap);
11851 strides = rational ? 0 : multi_aff_strides(ma);
11852 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
11853 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
11854 if (rational)
11855 res = isl_basic_map_set_rational(res);
11857 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
11858 if (isl_basic_map_alloc_div(res) < 0)
11859 goto error;
11861 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
11862 goto error;
11864 for (i = 0; i < bmap->n_eq; ++i) {
11865 k = isl_basic_map_alloc_equality(res);
11866 if (k < 0)
11867 goto error;
11868 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
11869 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11872 for (i = 0; i < bmap->n_ineq; ++i) {
11873 k = isl_basic_map_alloc_inequality(res);
11874 if (k < 0)
11875 goto error;
11876 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
11877 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11880 for (i = 0; i < bmap->n_div; ++i) {
11881 if (isl_int_is_zero(bmap->div[i][0])) {
11882 isl_int_set_si(res->div[n_div_ma + i][0], 0);
11883 continue;
11885 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
11886 n_before, n_after, n_div_ma, n_div_bmap,
11887 f, c1, c2, g, 1);
11890 if (strides)
11891 res = add_ma_strides(res, ma, n_before, n_after);
11893 isl_int_clear(f);
11894 isl_int_clear(c1);
11895 isl_int_clear(c2);
11896 isl_int_clear(g);
11897 isl_basic_map_free(bmap);
11898 isl_multi_aff_free(ma);
11899 res = isl_basic_set_simplify(res);
11900 return isl_basic_map_finalize(res);
11901 error:
11902 isl_int_clear(f);
11903 isl_int_clear(c1);
11904 isl_int_clear(c2);
11905 isl_int_clear(g);
11906 isl_basic_map_free(bmap);
11907 isl_multi_aff_free(ma);
11908 isl_basic_map_free(res);
11909 return NULL;
11912 /* Compute the preimage of "bset" under the function represented by "ma".
11913 * In other words, plug in "ma" in "bset". The result is a basic set
11914 * that lives in the domain space of "ma".
11916 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
11917 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
11919 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
11922 /* Compute the preimage of the domain of "bmap" under the function
11923 * represented by "ma".
11924 * In other words, plug in "ma" in the domain of "bmap".
11925 * The result is a basic map that lives in the same space as "bmap"
11926 * except that the domain has been replaced by the domain space of "ma".
11928 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
11929 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
11931 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
11934 /* Compute the preimage of the range of "bmap" under the function
11935 * represented by "ma".
11936 * In other words, plug in "ma" in the range of "bmap".
11937 * The result is a basic map that lives in the same space as "bmap"
11938 * except that the range has been replaced by the domain space of "ma".
11940 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
11941 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
11943 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
11946 /* Check if the range of "ma" is compatible with the domain or range
11947 * (depending on "type") of "map".
11948 * Return -1 if anything is wrong.
11950 static int check_map_compatible_range_multi_aff(
11951 __isl_keep isl_map *map, enum isl_dim_type type,
11952 __isl_keep isl_multi_aff *ma)
11954 int m;
11955 isl_space *ma_space;
11957 ma_space = isl_multi_aff_get_space(ma);
11958 m = isl_space_tuple_match(map->dim, type, ma_space, isl_dim_out);
11959 isl_space_free(ma_space);
11960 if (m >= 0 && !m)
11961 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11962 "spaces don't match", return -1);
11963 return m;
11966 /* Compute the preimage of the domain or range (depending on "type")
11967 * of "map" under the function represented by "ma".
11968 * In other words, plug in "ma" in the domain or range of "map".
11969 * The result is a map that lives in the same space as "map"
11970 * except that the domain or range has been replaced by
11971 * the domain space of "ma".
11973 * The parameters are assumed to have been aligned.
11975 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
11976 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11978 int i;
11979 isl_space *space;
11981 map = isl_map_cow(map);
11982 ma = isl_multi_aff_align_divs(ma);
11983 if (!map || !ma)
11984 goto error;
11985 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
11986 goto error;
11988 for (i = 0; i < map->n; ++i) {
11989 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
11990 isl_multi_aff_copy(ma));
11991 if (!map->p[i])
11992 goto error;
11995 space = isl_multi_aff_get_domain_space(ma);
11996 space = isl_space_set(isl_map_get_space(map), type, space);
11998 isl_space_free(map->dim);
11999 map->dim = space;
12000 if (!map->dim)
12001 goto error;
12003 isl_multi_aff_free(ma);
12004 if (map->n > 1)
12005 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12006 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12007 return map;
12008 error:
12009 isl_multi_aff_free(ma);
12010 isl_map_free(map);
12011 return NULL;
12014 /* Compute the preimage of the domain or range (depending on "type")
12015 * of "map" under the function represented by "ma".
12016 * In other words, plug in "ma" in the domain or range of "map".
12017 * The result is a map that lives in the same space as "map"
12018 * except that the domain or range has been replaced by
12019 * the domain space of "ma".
12021 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12022 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12024 if (!map || !ma)
12025 goto error;
12027 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12028 return map_preimage_multi_aff(map, type, ma);
12030 if (!isl_space_has_named_params(map->dim) ||
12031 !isl_space_has_named_params(ma->space))
12032 isl_die(map->ctx, isl_error_invalid,
12033 "unaligned unnamed parameters", goto error);
12034 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12035 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12037 return map_preimage_multi_aff(map, type, ma);
12038 error:
12039 isl_multi_aff_free(ma);
12040 return isl_map_free(map);
12043 /* Compute the preimage of "set" under the function represented by "ma".
12044 * In other words, plug in "ma" "set". The result is a set
12045 * that lives in the domain space of "ma".
12047 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12048 __isl_take isl_multi_aff *ma)
12050 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12053 /* Compute the preimage of the domain of "map" under the function
12054 * represented by "ma".
12055 * In other words, plug in "ma" in the domain of "map".
12056 * The result is a map that lives in the same space as "map"
12057 * except that the domain has been replaced by the domain space of "ma".
12059 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12060 __isl_take isl_multi_aff *ma)
12062 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12065 /* Compute the preimage of "map" under the function represented by "pma".
12066 * In other words, plug in "pma" in the domain or range of "map".
12067 * The result is a map that lives in the same space as "map",
12068 * except that the space of type "type" has been replaced by
12069 * the domain space of "pma".
12071 * The parameters of "map" and "pma" are assumed to have been aligned.
12073 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12074 __isl_take isl_map *map, enum isl_dim_type type,
12075 __isl_take isl_pw_multi_aff *pma)
12077 int i;
12078 isl_map *res;
12080 if (!pma)
12081 goto error;
12083 if (pma->n == 0) {
12084 isl_pw_multi_aff_free(pma);
12085 res = isl_map_empty(isl_map_get_space(map));
12086 isl_map_free(map);
12087 return res;
12090 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12091 isl_multi_aff_copy(pma->p[0].maff));
12092 if (type == isl_dim_in)
12093 res = isl_map_intersect_domain(res,
12094 isl_map_copy(pma->p[0].set));
12095 else
12096 res = isl_map_intersect_range(res,
12097 isl_map_copy(pma->p[0].set));
12099 for (i = 1; i < pma->n; ++i) {
12100 isl_map *res_i;
12102 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12103 isl_multi_aff_copy(pma->p[i].maff));
12104 if (type == isl_dim_in)
12105 res_i = isl_map_intersect_domain(res_i,
12106 isl_map_copy(pma->p[i].set));
12107 else
12108 res_i = isl_map_intersect_range(res_i,
12109 isl_map_copy(pma->p[i].set));
12110 res = isl_map_union(res, res_i);
12113 isl_pw_multi_aff_free(pma);
12114 isl_map_free(map);
12115 return res;
12116 error:
12117 isl_pw_multi_aff_free(pma);
12118 isl_map_free(map);
12119 return NULL;
12122 /* Compute the preimage of "map" under the function represented by "pma".
12123 * In other words, plug in "pma" in the domain or range of "map".
12124 * The result is a map that lives in the same space as "map",
12125 * except that the space of type "type" has been replaced by
12126 * the domain space of "pma".
12128 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12129 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12131 if (!map || !pma)
12132 goto error;
12134 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12135 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12137 if (!isl_space_has_named_params(map->dim) ||
12138 !isl_space_has_named_params(pma->dim))
12139 isl_die(map->ctx, isl_error_invalid,
12140 "unaligned unnamed parameters", goto error);
12141 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12142 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12144 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12145 error:
12146 isl_pw_multi_aff_free(pma);
12147 return isl_map_free(map);
12150 /* Compute the preimage of "set" under the function represented by "pma".
12151 * In other words, plug in "pma" in "set". The result is a set
12152 * that lives in the domain space of "pma".
12154 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12155 __isl_take isl_pw_multi_aff *pma)
12157 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12160 /* Compute the preimage of "map" under the function represented by "pma".
12161 * In other words, plug in "pma" in the domain "map".
12162 * The result is a map that lives in the same space as "map",
12163 * except that domain space has been replaced by the domain space of "pma".
12165 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12166 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12168 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12171 /* Compute the preimage of "map" under the function represented by "mpa".
12172 * In other words, plug in "mpa" in the domain or range of "map".
12173 * The result is a map that lives in the same space as "map",
12174 * except that the space of type "type" has been replaced by
12175 * the domain space of "mpa".
12177 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12178 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12180 isl_pw_multi_aff *pma;
12182 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12183 return isl_map_preimage_pw_multi_aff(map, type, pma);
12186 /* Compute the preimage of "map" under the function represented by "mpa".
12187 * In other words, plug in "mpa" in the domain "map".
12188 * The result is a map that lives in the same space as "map",
12189 * except that domain space has been replaced by the domain space of "mpa".
12191 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12192 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12194 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12197 /* Compute the preimage of "set" by the function represented by "mpa".
12198 * In other words, plug in "mpa" in "set".
12200 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12201 __isl_take isl_multi_pw_aff *mpa)
12203 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);