add isl_union_set_preimage_pw_multi_aff
[isl.git] / isl_map.c
blobc29edce7353fa72d794aa151af5bed553e9044ff
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 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 constraint
4352 * f - m d >= 0
4354 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4355 unsigned pos, isl_int *div)
4357 int i;
4358 unsigned total = isl_basic_map_total_dim(bmap);
4360 i = isl_basic_map_alloc_inequality(bmap);
4361 if (i < 0)
4362 return -1;
4363 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4364 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4366 return 0;
4369 /* For a div d = floor(f/m), add the constraint
4371 * -(f-(n-1)) + m d >= 0
4373 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4374 unsigned pos, isl_int *div)
4376 int i;
4377 unsigned total = isl_basic_map_total_dim(bmap);
4379 i = isl_basic_map_alloc_inequality(bmap);
4380 if (i < 0)
4381 return -1;
4382 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4383 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4384 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4385 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4387 return 0;
4390 /* For a div d = floor(f/m), add the constraints
4392 * f - m d >= 0
4393 * -(f-(n-1)) + m d >= 0
4395 * Note that the second constraint is the negation of
4397 * f - m d >= n
4399 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4400 unsigned pos, isl_int *div)
4402 if (add_upper_div_constraint(bmap, pos, div) < 0)
4403 return -1;
4404 if (add_lower_div_constraint(bmap, pos, div) < 0)
4405 return -1;
4406 return 0;
4409 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4410 unsigned pos, isl_int *div)
4412 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4413 pos, div);
4416 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4418 unsigned total = isl_basic_map_total_dim(bmap);
4419 unsigned div_pos = total - bmap->n_div + div;
4421 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4422 bmap->div[div]);
4425 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4427 * In particular, if this div is of the form d = floor(f/m),
4428 * then add the constraint
4430 * f - m d >= 0
4432 * if sign < 0 or the constraint
4434 * -(f-(n-1)) + m d >= 0
4436 * if sign > 0.
4438 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4439 unsigned div, int sign)
4441 unsigned total;
4442 unsigned div_pos;
4444 if (!bmap)
4445 return -1;
4447 total = isl_basic_map_total_dim(bmap);
4448 div_pos = total - bmap->n_div + div;
4450 if (sign < 0)
4451 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4452 else
4453 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4456 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4458 return isl_basic_map_add_div_constraints(bset, div);
4461 struct isl_basic_set *isl_basic_map_underlying_set(
4462 struct isl_basic_map *bmap)
4464 if (!bmap)
4465 goto error;
4466 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4467 bmap->n_div == 0 &&
4468 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4469 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4470 return (struct isl_basic_set *)bmap;
4471 bmap = isl_basic_map_cow(bmap);
4472 if (!bmap)
4473 goto error;
4474 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4475 if (!bmap->dim)
4476 goto error;
4477 bmap->extra -= bmap->n_div;
4478 bmap->n_div = 0;
4479 bmap = isl_basic_map_finalize(bmap);
4480 return (struct isl_basic_set *)bmap;
4481 error:
4482 isl_basic_map_free(bmap);
4483 return NULL;
4486 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4487 __isl_take isl_basic_set *bset)
4489 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4492 struct isl_basic_map *isl_basic_map_overlying_set(
4493 struct isl_basic_set *bset, struct isl_basic_map *like)
4495 struct isl_basic_map *bmap;
4496 struct isl_ctx *ctx;
4497 unsigned total;
4498 int i;
4500 if (!bset || !like)
4501 goto error;
4502 ctx = bset->ctx;
4503 isl_assert(ctx, bset->n_div == 0, goto error);
4504 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4505 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4506 goto error);
4507 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4508 isl_basic_map_free(like);
4509 return (struct isl_basic_map *)bset;
4511 bset = isl_basic_set_cow(bset);
4512 if (!bset)
4513 goto error;
4514 total = bset->dim->n_out + bset->extra;
4515 bmap = (struct isl_basic_map *)bset;
4516 isl_space_free(bmap->dim);
4517 bmap->dim = isl_space_copy(like->dim);
4518 if (!bmap->dim)
4519 goto error;
4520 bmap->n_div = like->n_div;
4521 bmap->extra += like->n_div;
4522 if (bmap->extra) {
4523 unsigned ltotal;
4524 isl_int **div;
4525 ltotal = total - bmap->extra + like->extra;
4526 if (ltotal > total)
4527 ltotal = total;
4528 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4529 bmap->extra * (1 + 1 + total));
4530 if (isl_blk_is_error(bmap->block2))
4531 goto error;
4532 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4533 if (!div)
4534 goto error;
4535 bmap->div = div;
4536 for (i = 0; i < bmap->extra; ++i)
4537 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4538 for (i = 0; i < like->n_div; ++i) {
4539 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4540 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4542 bmap = isl_basic_map_extend_constraints(bmap,
4543 0, 2 * like->n_div);
4544 for (i = 0; i < like->n_div; ++i) {
4545 if (!bmap)
4546 break;
4547 if (isl_int_is_zero(bmap->div[i][0]))
4548 continue;
4549 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4550 bmap = isl_basic_map_free(bmap);
4553 isl_basic_map_free(like);
4554 bmap = isl_basic_map_simplify(bmap);
4555 bmap = isl_basic_map_finalize(bmap);
4556 return bmap;
4557 error:
4558 isl_basic_map_free(like);
4559 isl_basic_set_free(bset);
4560 return NULL;
4563 struct isl_basic_set *isl_basic_set_from_underlying_set(
4564 struct isl_basic_set *bset, struct isl_basic_set *like)
4566 return (struct isl_basic_set *)
4567 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4570 struct isl_set *isl_set_from_underlying_set(
4571 struct isl_set *set, struct isl_basic_set *like)
4573 int i;
4575 if (!set || !like)
4576 goto error;
4577 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4578 goto error);
4579 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4580 isl_basic_set_free(like);
4581 return set;
4583 set = isl_set_cow(set);
4584 if (!set)
4585 goto error;
4586 for (i = 0; i < set->n; ++i) {
4587 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4588 isl_basic_set_copy(like));
4589 if (!set->p[i])
4590 goto error;
4592 isl_space_free(set->dim);
4593 set->dim = isl_space_copy(like->dim);
4594 if (!set->dim)
4595 goto error;
4596 isl_basic_set_free(like);
4597 return set;
4598 error:
4599 isl_basic_set_free(like);
4600 isl_set_free(set);
4601 return NULL;
4604 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4606 int i;
4608 map = isl_map_cow(map);
4609 if (!map)
4610 return NULL;
4611 map->dim = isl_space_cow(map->dim);
4612 if (!map->dim)
4613 goto error;
4615 for (i = 1; i < map->n; ++i)
4616 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4617 goto error);
4618 for (i = 0; i < map->n; ++i) {
4619 map->p[i] = (struct isl_basic_map *)
4620 isl_basic_map_underlying_set(map->p[i]);
4621 if (!map->p[i])
4622 goto error;
4624 if (map->n == 0)
4625 map->dim = isl_space_underlying(map->dim, 0);
4626 else {
4627 isl_space_free(map->dim);
4628 map->dim = isl_space_copy(map->p[0]->dim);
4630 if (!map->dim)
4631 goto error;
4632 return (struct isl_set *)map;
4633 error:
4634 isl_map_free(map);
4635 return NULL;
4638 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4640 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4643 __isl_give isl_basic_map *isl_basic_map_reset_space(
4644 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4646 bmap = isl_basic_map_cow(bmap);
4647 if (!bmap || !dim)
4648 goto error;
4650 isl_space_free(bmap->dim);
4651 bmap->dim = dim;
4653 bmap = isl_basic_map_finalize(bmap);
4655 return bmap;
4656 error:
4657 isl_basic_map_free(bmap);
4658 isl_space_free(dim);
4659 return NULL;
4662 __isl_give isl_basic_set *isl_basic_set_reset_space(
4663 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4665 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4666 dim);
4669 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4670 __isl_take isl_space *dim)
4672 int i;
4674 map = isl_map_cow(map);
4675 if (!map || !dim)
4676 goto error;
4678 for (i = 0; i < map->n; ++i) {
4679 map->p[i] = isl_basic_map_reset_space(map->p[i],
4680 isl_space_copy(dim));
4681 if (!map->p[i])
4682 goto error;
4684 isl_space_free(map->dim);
4685 map->dim = dim;
4687 return map;
4688 error:
4689 isl_map_free(map);
4690 isl_space_free(dim);
4691 return NULL;
4694 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4695 __isl_take isl_space *dim)
4697 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4700 /* Compute the parameter domain of the given basic set.
4702 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4704 isl_space *space;
4705 unsigned n;
4707 if (isl_basic_set_is_params(bset))
4708 return bset;
4710 n = isl_basic_set_dim(bset, isl_dim_set);
4711 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4712 space = isl_basic_set_get_space(bset);
4713 space = isl_space_params(space);
4714 bset = isl_basic_set_reset_space(bset, space);
4715 return bset;
4718 /* Construct a zero-dimensional basic set with the given parameter domain.
4720 __isl_give isl_basic_set *isl_basic_set_from_params(
4721 __isl_take isl_basic_set *bset)
4723 isl_space *space;
4724 space = isl_basic_set_get_space(bset);
4725 space = isl_space_set_from_params(space);
4726 bset = isl_basic_set_reset_space(bset, space);
4727 return bset;
4730 /* Compute the parameter domain of the given set.
4732 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4734 isl_space *space;
4735 unsigned n;
4737 if (isl_set_is_params(set))
4738 return set;
4740 n = isl_set_dim(set, isl_dim_set);
4741 set = isl_set_project_out(set, isl_dim_set, 0, n);
4742 space = isl_set_get_space(set);
4743 space = isl_space_params(space);
4744 set = isl_set_reset_space(set, space);
4745 return set;
4748 /* Construct a zero-dimensional set with the given parameter domain.
4750 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4752 isl_space *space;
4753 space = isl_set_get_space(set);
4754 space = isl_space_set_from_params(space);
4755 set = isl_set_reset_space(set, space);
4756 return set;
4759 /* Compute the parameter domain of the given map.
4761 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4763 isl_space *space;
4764 unsigned n;
4766 n = isl_map_dim(map, isl_dim_in);
4767 map = isl_map_project_out(map, isl_dim_in, 0, n);
4768 n = isl_map_dim(map, isl_dim_out);
4769 map = isl_map_project_out(map, isl_dim_out, 0, n);
4770 space = isl_map_get_space(map);
4771 space = isl_space_params(space);
4772 map = isl_map_reset_space(map, space);
4773 return map;
4776 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4778 isl_space *dim;
4779 struct isl_basic_set *domain;
4780 unsigned n_in;
4781 unsigned n_out;
4783 if (!bmap)
4784 return NULL;
4785 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4787 n_in = isl_basic_map_n_in(bmap);
4788 n_out = isl_basic_map_n_out(bmap);
4789 domain = isl_basic_set_from_basic_map(bmap);
4790 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4792 domain = isl_basic_set_reset_space(domain, dim);
4794 return domain;
4797 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4799 if (!bmap)
4800 return -1;
4801 return isl_space_may_be_set(bmap->dim);
4804 /* Is this basic map actually a set?
4805 * Users should never call this function. Outside of isl,
4806 * the type should indicate whether something is a set or a map.
4808 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4810 if (!bmap)
4811 return -1;
4812 return isl_space_is_set(bmap->dim);
4815 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4817 if (!bmap)
4818 return NULL;
4819 if (isl_basic_map_is_set(bmap))
4820 return bmap;
4821 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4824 __isl_give isl_basic_map *isl_basic_map_domain_map(
4825 __isl_take isl_basic_map *bmap)
4827 int i, k;
4828 isl_space *dim;
4829 isl_basic_map *domain;
4830 int nparam, n_in, n_out;
4831 unsigned total;
4833 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4834 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4835 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4837 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4838 domain = isl_basic_map_universe(dim);
4840 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4841 bmap = isl_basic_map_apply_range(bmap, domain);
4842 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4844 total = isl_basic_map_total_dim(bmap);
4846 for (i = 0; i < n_in; ++i) {
4847 k = isl_basic_map_alloc_equality(bmap);
4848 if (k < 0)
4849 goto error;
4850 isl_seq_clr(bmap->eq[k], 1 + total);
4851 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4852 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4855 bmap = isl_basic_map_gauss(bmap, NULL);
4856 return isl_basic_map_finalize(bmap);
4857 error:
4858 isl_basic_map_free(bmap);
4859 return NULL;
4862 __isl_give isl_basic_map *isl_basic_map_range_map(
4863 __isl_take isl_basic_map *bmap)
4865 int i, k;
4866 isl_space *dim;
4867 isl_basic_map *range;
4868 int nparam, n_in, n_out;
4869 unsigned total;
4871 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4872 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4873 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4875 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4876 range = isl_basic_map_universe(dim);
4878 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4879 bmap = isl_basic_map_apply_range(bmap, range);
4880 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4882 total = isl_basic_map_total_dim(bmap);
4884 for (i = 0; i < n_out; ++i) {
4885 k = isl_basic_map_alloc_equality(bmap);
4886 if (k < 0)
4887 goto error;
4888 isl_seq_clr(bmap->eq[k], 1 + total);
4889 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4890 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4893 bmap = isl_basic_map_gauss(bmap, NULL);
4894 return isl_basic_map_finalize(bmap);
4895 error:
4896 isl_basic_map_free(bmap);
4897 return NULL;
4900 int isl_map_may_be_set(__isl_keep isl_map *map)
4902 if (!map)
4903 return -1;
4904 return isl_space_may_be_set(map->dim);
4907 /* Is this map actually a set?
4908 * Users should never call this function. Outside of isl,
4909 * the type should indicate whether something is a set or a map.
4911 int isl_map_is_set(__isl_keep isl_map *map)
4913 if (!map)
4914 return -1;
4915 return isl_space_is_set(map->dim);
4918 struct isl_set *isl_map_range(struct isl_map *map)
4920 int i;
4921 struct isl_set *set;
4923 if (!map)
4924 goto error;
4925 if (isl_map_is_set(map))
4926 return (isl_set *)map;
4928 map = isl_map_cow(map);
4929 if (!map)
4930 goto error;
4932 set = (struct isl_set *) map;
4933 set->dim = isl_space_range(set->dim);
4934 if (!set->dim)
4935 goto error;
4936 for (i = 0; i < map->n; ++i) {
4937 set->p[i] = isl_basic_map_range(map->p[i]);
4938 if (!set->p[i])
4939 goto error;
4941 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4942 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4943 return set;
4944 error:
4945 isl_map_free(map);
4946 return NULL;
4949 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4951 int i;
4953 map = isl_map_cow(map);
4954 if (!map)
4955 return NULL;
4957 map->dim = isl_space_domain_map(map->dim);
4958 if (!map->dim)
4959 goto error;
4960 for (i = 0; i < map->n; ++i) {
4961 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4962 if (!map->p[i])
4963 goto error;
4965 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4966 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4967 return map;
4968 error:
4969 isl_map_free(map);
4970 return NULL;
4973 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4975 int i;
4976 isl_space *range_dim;
4978 map = isl_map_cow(map);
4979 if (!map)
4980 return NULL;
4982 range_dim = isl_space_range(isl_map_get_space(map));
4983 range_dim = isl_space_from_range(range_dim);
4984 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4985 map->dim = isl_space_join(map->dim, range_dim);
4986 if (!map->dim)
4987 goto error;
4988 for (i = 0; i < map->n; ++i) {
4989 map->p[i] = isl_basic_map_range_map(map->p[i]);
4990 if (!map->p[i])
4991 goto error;
4993 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4994 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4995 return map;
4996 error:
4997 isl_map_free(map);
4998 return NULL;
5001 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5002 __isl_take isl_space *dim)
5004 int i;
5005 struct isl_map *map = NULL;
5007 set = isl_set_cow(set);
5008 if (!set || !dim)
5009 goto error;
5010 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5011 map = (struct isl_map *)set;
5012 for (i = 0; i < set->n; ++i) {
5013 map->p[i] = isl_basic_map_from_basic_set(
5014 set->p[i], isl_space_copy(dim));
5015 if (!map->p[i])
5016 goto error;
5018 isl_space_free(map->dim);
5019 map->dim = dim;
5020 return map;
5021 error:
5022 isl_space_free(dim);
5023 isl_set_free(set);
5024 return NULL;
5027 __isl_give isl_basic_map *isl_basic_map_from_domain(
5028 __isl_take isl_basic_set *bset)
5030 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5033 __isl_give isl_basic_map *isl_basic_map_from_range(
5034 __isl_take isl_basic_set *bset)
5036 isl_space *space;
5037 space = isl_basic_set_get_space(bset);
5038 space = isl_space_from_range(space);
5039 bset = isl_basic_set_reset_space(bset, space);
5040 return (isl_basic_map *)bset;
5043 /* Create a relation with the given set as range.
5044 * The domain of the created relation is a zero-dimensional
5045 * flat anonymous space.
5047 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5049 isl_space *space;
5050 space = isl_set_get_space(set);
5051 space = isl_space_from_range(space);
5052 set = isl_set_reset_space(set, space);
5053 return (struct isl_map *)set;
5056 /* Create a relation with the given set as domain.
5057 * The range of the created relation is a zero-dimensional
5058 * flat anonymous space.
5060 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5062 return isl_map_reverse(isl_map_from_range(set));
5065 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5066 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5068 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5071 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5072 __isl_take isl_set *range)
5074 return isl_map_apply_range(isl_map_reverse(domain), range);
5077 struct isl_set *isl_set_from_map(struct isl_map *map)
5079 int i;
5080 struct isl_set *set = NULL;
5082 if (!map)
5083 return NULL;
5084 map = isl_map_cow(map);
5085 if (!map)
5086 return NULL;
5087 map->dim = isl_space_as_set_space(map->dim);
5088 if (!map->dim)
5089 goto error;
5090 set = (struct isl_set *)map;
5091 for (i = 0; i < map->n; ++i) {
5092 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
5093 if (!set->p[i])
5094 goto error;
5096 return set;
5097 error:
5098 isl_map_free(map);
5099 return NULL;
5102 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5103 unsigned flags)
5105 struct isl_map *map;
5107 if (!dim)
5108 return NULL;
5109 if (n < 0)
5110 isl_die(dim->ctx, isl_error_internal,
5111 "negative number of basic maps", goto error);
5112 map = isl_alloc(dim->ctx, struct isl_map,
5113 sizeof(struct isl_map) +
5114 (n - 1) * sizeof(struct isl_basic_map *));
5115 if (!map)
5116 goto error;
5118 map->ctx = dim->ctx;
5119 isl_ctx_ref(map->ctx);
5120 map->ref = 1;
5121 map->size = n;
5122 map->n = 0;
5123 map->dim = dim;
5124 map->flags = flags;
5125 return map;
5126 error:
5127 isl_space_free(dim);
5128 return NULL;
5131 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5132 unsigned nparam, unsigned in, unsigned out, int n,
5133 unsigned flags)
5135 struct isl_map *map;
5136 isl_space *dims;
5138 dims = isl_space_alloc(ctx, nparam, in, out);
5139 if (!dims)
5140 return NULL;
5142 map = isl_map_alloc_space(dims, n, flags);
5143 return map;
5146 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5148 struct isl_basic_map *bmap;
5149 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5150 bmap = isl_basic_map_set_to_empty(bmap);
5151 return bmap;
5154 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5156 struct isl_basic_set *bset;
5157 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5158 bset = isl_basic_set_set_to_empty(bset);
5159 return bset;
5162 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5164 struct isl_basic_map *bmap;
5165 if (!model)
5166 return NULL;
5167 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5168 bmap = isl_basic_map_set_to_empty(bmap);
5169 return bmap;
5172 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5174 struct isl_basic_map *bmap;
5175 if (!model)
5176 return NULL;
5177 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5178 bmap = isl_basic_map_set_to_empty(bmap);
5179 return bmap;
5182 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5184 struct isl_basic_set *bset;
5185 if (!model)
5186 return NULL;
5187 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5188 bset = isl_basic_set_set_to_empty(bset);
5189 return bset;
5192 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5194 struct isl_basic_map *bmap;
5195 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5196 bmap = isl_basic_map_finalize(bmap);
5197 return bmap;
5200 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5202 struct isl_basic_set *bset;
5203 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5204 bset = isl_basic_set_finalize(bset);
5205 return bset;
5208 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5210 int i;
5211 unsigned total = isl_space_dim(dim, isl_dim_all);
5212 isl_basic_map *bmap;
5214 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5215 for (i = 0; i < total; ++i) {
5216 int k = isl_basic_map_alloc_inequality(bmap);
5217 if (k < 0)
5218 goto error;
5219 isl_seq_clr(bmap->ineq[k], 1 + total);
5220 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5222 return bmap;
5223 error:
5224 isl_basic_map_free(bmap);
5225 return NULL;
5228 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5230 return isl_basic_map_nat_universe(dim);
5233 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5235 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5238 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5240 return isl_map_nat_universe(dim);
5243 __isl_give isl_basic_map *isl_basic_map_universe_like(
5244 __isl_keep isl_basic_map *model)
5246 if (!model)
5247 return NULL;
5248 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5251 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5253 if (!model)
5254 return NULL;
5255 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5258 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5259 __isl_keep isl_set *model)
5261 if (!model)
5262 return NULL;
5263 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5266 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5268 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5271 struct isl_map *isl_map_empty_like(struct isl_map *model)
5273 if (!model)
5274 return NULL;
5275 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5278 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5280 if (!model)
5281 return NULL;
5282 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5285 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5287 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5290 struct isl_set *isl_set_empty_like(struct isl_set *model)
5292 if (!model)
5293 return NULL;
5294 return isl_set_empty(isl_space_copy(model->dim));
5297 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5299 struct isl_map *map;
5300 if (!dim)
5301 return NULL;
5302 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5303 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5304 return map;
5307 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5309 struct isl_set *set;
5310 if (!dim)
5311 return NULL;
5312 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5313 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5314 return set;
5317 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5319 if (!model)
5320 return NULL;
5321 return isl_set_universe(isl_space_copy(model->dim));
5324 struct isl_map *isl_map_dup(struct isl_map *map)
5326 int i;
5327 struct isl_map *dup;
5329 if (!map)
5330 return NULL;
5331 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5332 for (i = 0; i < map->n; ++i)
5333 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5334 return dup;
5337 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5338 __isl_take isl_basic_map *bmap)
5340 if (!bmap || !map)
5341 goto error;
5342 if (isl_basic_map_plain_is_empty(bmap)) {
5343 isl_basic_map_free(bmap);
5344 return map;
5346 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5347 isl_assert(map->ctx, map->n < map->size, goto error);
5348 map->p[map->n] = bmap;
5349 map->n++;
5350 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5351 return map;
5352 error:
5353 if (map)
5354 isl_map_free(map);
5355 if (bmap)
5356 isl_basic_map_free(bmap);
5357 return NULL;
5360 void *isl_map_free(struct isl_map *map)
5362 int i;
5364 if (!map)
5365 return NULL;
5367 if (--map->ref > 0)
5368 return NULL;
5370 isl_ctx_deref(map->ctx);
5371 for (i = 0; i < map->n; ++i)
5372 isl_basic_map_free(map->p[i]);
5373 isl_space_free(map->dim);
5374 free(map);
5376 return NULL;
5379 struct isl_map *isl_map_extend(struct isl_map *base,
5380 unsigned nparam, unsigned n_in, unsigned n_out)
5382 int i;
5384 base = isl_map_cow(base);
5385 if (!base)
5386 return NULL;
5388 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5389 if (!base->dim)
5390 goto error;
5391 for (i = 0; i < base->n; ++i) {
5392 base->p[i] = isl_basic_map_extend_space(base->p[i],
5393 isl_space_copy(base->dim), 0, 0, 0);
5394 if (!base->p[i])
5395 goto error;
5397 return base;
5398 error:
5399 isl_map_free(base);
5400 return NULL;
5403 struct isl_set *isl_set_extend(struct isl_set *base,
5404 unsigned nparam, unsigned dim)
5406 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5407 nparam, 0, dim);
5410 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5411 struct isl_basic_map *bmap, unsigned pos, int value)
5413 int j;
5415 bmap = isl_basic_map_cow(bmap);
5416 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5417 j = isl_basic_map_alloc_equality(bmap);
5418 if (j < 0)
5419 goto error;
5420 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5421 isl_int_set_si(bmap->eq[j][pos], -1);
5422 isl_int_set_si(bmap->eq[j][0], value);
5423 bmap = isl_basic_map_simplify(bmap);
5424 return isl_basic_map_finalize(bmap);
5425 error:
5426 isl_basic_map_free(bmap);
5427 return NULL;
5430 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5431 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5433 int j;
5435 bmap = isl_basic_map_cow(bmap);
5436 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5437 j = isl_basic_map_alloc_equality(bmap);
5438 if (j < 0)
5439 goto error;
5440 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5441 isl_int_set_si(bmap->eq[j][pos], -1);
5442 isl_int_set(bmap->eq[j][0], value);
5443 bmap = isl_basic_map_simplify(bmap);
5444 return isl_basic_map_finalize(bmap);
5445 error:
5446 isl_basic_map_free(bmap);
5447 return NULL;
5450 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5451 enum isl_dim_type type, unsigned pos, int value)
5453 if (!bmap)
5454 return NULL;
5455 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5456 return isl_basic_map_fix_pos_si(bmap,
5457 isl_basic_map_offset(bmap, type) + pos, value);
5458 error:
5459 isl_basic_map_free(bmap);
5460 return NULL;
5463 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5464 enum isl_dim_type type, unsigned pos, isl_int value)
5466 if (!bmap)
5467 return NULL;
5468 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5469 return isl_basic_map_fix_pos(bmap,
5470 isl_basic_map_offset(bmap, type) + pos, value);
5471 error:
5472 isl_basic_map_free(bmap);
5473 return NULL;
5476 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5477 * to be equal to "v".
5479 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5480 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5482 if (!bmap || !v)
5483 goto error;
5484 if (!isl_val_is_int(v))
5485 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5486 "expecting integer value", goto error);
5487 if (pos >= isl_basic_map_dim(bmap, type))
5488 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5489 "index out of bounds", goto error);
5490 pos += isl_basic_map_offset(bmap, type);
5491 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5492 isl_val_free(v);
5493 return bmap;
5494 error:
5495 isl_basic_map_free(bmap);
5496 isl_val_free(v);
5497 return NULL;
5500 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5501 * to be equal to "v".
5503 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5504 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5506 return isl_basic_map_fix_val(bset, type, pos, v);
5509 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5510 enum isl_dim_type type, unsigned pos, int value)
5512 return (struct isl_basic_set *)
5513 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5514 type, pos, value);
5517 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5518 enum isl_dim_type type, unsigned pos, isl_int value)
5520 return (struct isl_basic_set *)
5521 isl_basic_map_fix((struct isl_basic_map *)bset,
5522 type, pos, value);
5525 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5526 unsigned input, int value)
5528 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5531 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5532 unsigned dim, int value)
5534 return (struct isl_basic_set *)
5535 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5536 isl_dim_set, dim, value);
5539 static int remove_if_empty(__isl_keep isl_map *map, int i)
5541 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5543 if (empty < 0)
5544 return -1;
5545 if (!empty)
5546 return 0;
5548 isl_basic_map_free(map->p[i]);
5549 if (i != map->n - 1) {
5550 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5551 map->p[i] = map->p[map->n - 1];
5553 map->n--;
5555 return 0;
5558 /* Perform "fn" on each basic map of "map", where we may not be holding
5559 * the only reference to "map".
5560 * In particular, "fn" should be a semantics preserving operation
5561 * that we want to apply to all copies of "map". We therefore need
5562 * to be careful not to modify "map" in a way that breaks "map"
5563 * in case anything goes wrong.
5565 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5566 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5568 struct isl_basic_map *bmap;
5569 int i;
5571 if (!map)
5572 return NULL;
5574 for (i = map->n - 1; i >= 0; --i) {
5575 bmap = isl_basic_map_copy(map->p[i]);
5576 bmap = fn(bmap);
5577 if (!bmap)
5578 goto error;
5579 isl_basic_map_free(map->p[i]);
5580 map->p[i] = bmap;
5581 if (remove_if_empty(map, i) < 0)
5582 goto error;
5585 return map;
5586 error:
5587 isl_map_free(map);
5588 return NULL;
5591 struct isl_map *isl_map_fix_si(struct isl_map *map,
5592 enum isl_dim_type type, unsigned pos, int value)
5594 int i;
5596 map = isl_map_cow(map);
5597 if (!map)
5598 return NULL;
5600 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5601 for (i = map->n - 1; i >= 0; --i) {
5602 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5603 if (remove_if_empty(map, i) < 0)
5604 goto error;
5606 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5607 return map;
5608 error:
5609 isl_map_free(map);
5610 return NULL;
5613 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5614 enum isl_dim_type type, unsigned pos, int value)
5616 return (struct isl_set *)
5617 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5620 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5621 enum isl_dim_type type, unsigned pos, isl_int value)
5623 int i;
5625 map = isl_map_cow(map);
5626 if (!map)
5627 return NULL;
5629 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5630 for (i = 0; i < map->n; ++i) {
5631 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5632 if (!map->p[i])
5633 goto error;
5635 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5636 return map;
5637 error:
5638 isl_map_free(map);
5639 return NULL;
5642 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5643 enum isl_dim_type type, unsigned pos, isl_int value)
5645 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5648 /* Fix the value of the variable at position "pos" of type "type" of "map"
5649 * to be equal to "v".
5651 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5652 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5654 int i;
5656 map = isl_map_cow(map);
5657 if (!map || !v)
5658 goto error;
5660 if (!isl_val_is_int(v))
5661 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5662 "expecting integer value", goto error);
5663 if (pos >= isl_map_dim(map, type))
5664 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5665 "index out of bounds", goto error);
5666 for (i = map->n - 1; i >= 0; --i) {
5667 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5668 isl_val_copy(v));
5669 if (remove_if_empty(map, i) < 0)
5670 goto error;
5672 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5673 isl_val_free(v);
5674 return map;
5675 error:
5676 isl_map_free(map);
5677 isl_val_free(v);
5678 return NULL;
5681 /* Fix the value of the variable at position "pos" of type "type" of "set"
5682 * to be equal to "v".
5684 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5685 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5687 return isl_map_fix_val(set, type, pos, v);
5690 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5691 unsigned input, int value)
5693 return isl_map_fix_si(map, isl_dim_in, input, value);
5696 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5698 return (struct isl_set *)
5699 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5702 static __isl_give isl_basic_map *basic_map_bound_si(
5703 __isl_take isl_basic_map *bmap,
5704 enum isl_dim_type type, unsigned pos, int value, int upper)
5706 int j;
5708 if (!bmap)
5709 return NULL;
5710 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5711 pos += isl_basic_map_offset(bmap, type);
5712 bmap = isl_basic_map_cow(bmap);
5713 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5714 j = isl_basic_map_alloc_inequality(bmap);
5715 if (j < 0)
5716 goto error;
5717 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5718 if (upper) {
5719 isl_int_set_si(bmap->ineq[j][pos], -1);
5720 isl_int_set_si(bmap->ineq[j][0], value);
5721 } else {
5722 isl_int_set_si(bmap->ineq[j][pos], 1);
5723 isl_int_set_si(bmap->ineq[j][0], -value);
5725 bmap = isl_basic_map_simplify(bmap);
5726 return isl_basic_map_finalize(bmap);
5727 error:
5728 isl_basic_map_free(bmap);
5729 return NULL;
5732 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5733 __isl_take isl_basic_map *bmap,
5734 enum isl_dim_type type, unsigned pos, int value)
5736 return basic_map_bound_si(bmap, type, pos, value, 0);
5739 /* Constrain the values of the given dimension to be no greater than "value".
5741 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5742 __isl_take isl_basic_map *bmap,
5743 enum isl_dim_type type, unsigned pos, int value)
5745 return basic_map_bound_si(bmap, type, pos, value, 1);
5748 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5749 unsigned dim, isl_int value)
5751 int j;
5753 bset = isl_basic_set_cow(bset);
5754 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5755 j = isl_basic_set_alloc_inequality(bset);
5756 if (j < 0)
5757 goto error;
5758 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5759 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5760 isl_int_neg(bset->ineq[j][0], value);
5761 bset = isl_basic_set_simplify(bset);
5762 return isl_basic_set_finalize(bset);
5763 error:
5764 isl_basic_set_free(bset);
5765 return NULL;
5768 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5769 enum isl_dim_type type, unsigned pos, int value, int upper)
5771 int i;
5773 map = isl_map_cow(map);
5774 if (!map)
5775 return NULL;
5777 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5778 for (i = 0; i < map->n; ++i) {
5779 map->p[i] = basic_map_bound_si(map->p[i],
5780 type, pos, value, upper);
5781 if (!map->p[i])
5782 goto error;
5784 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5785 return map;
5786 error:
5787 isl_map_free(map);
5788 return NULL;
5791 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5792 enum isl_dim_type type, unsigned pos, int value)
5794 return map_bound_si(map, type, pos, value, 0);
5797 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5798 enum isl_dim_type type, unsigned pos, int value)
5800 return map_bound_si(map, type, pos, value, 1);
5803 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5804 enum isl_dim_type type, unsigned pos, int value)
5806 return (struct isl_set *)
5807 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5810 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5811 enum isl_dim_type type, unsigned pos, int value)
5813 return isl_map_upper_bound_si(set, type, pos, value);
5816 /* Bound the given variable of "bmap" from below (or above is "upper"
5817 * is set) to "value".
5819 static __isl_give isl_basic_map *basic_map_bound(
5820 __isl_take isl_basic_map *bmap,
5821 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5823 int j;
5825 if (!bmap)
5826 return NULL;
5827 if (pos >= isl_basic_map_dim(bmap, type))
5828 isl_die(bmap->ctx, isl_error_invalid,
5829 "index out of bounds", goto error);
5830 pos += isl_basic_map_offset(bmap, type);
5831 bmap = isl_basic_map_cow(bmap);
5832 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5833 j = isl_basic_map_alloc_inequality(bmap);
5834 if (j < 0)
5835 goto error;
5836 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5837 if (upper) {
5838 isl_int_set_si(bmap->ineq[j][pos], -1);
5839 isl_int_set(bmap->ineq[j][0], value);
5840 } else {
5841 isl_int_set_si(bmap->ineq[j][pos], 1);
5842 isl_int_neg(bmap->ineq[j][0], value);
5844 bmap = isl_basic_map_simplify(bmap);
5845 return isl_basic_map_finalize(bmap);
5846 error:
5847 isl_basic_map_free(bmap);
5848 return NULL;
5851 /* Bound the given variable of "map" from below (or above is "upper"
5852 * is set) to "value".
5854 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5855 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5857 int i;
5859 map = isl_map_cow(map);
5860 if (!map)
5861 return NULL;
5863 if (pos >= isl_map_dim(map, type))
5864 isl_die(map->ctx, isl_error_invalid,
5865 "index out of bounds", goto error);
5866 for (i = map->n - 1; i >= 0; --i) {
5867 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5868 if (remove_if_empty(map, i) < 0)
5869 goto error;
5871 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5872 return map;
5873 error:
5874 isl_map_free(map);
5875 return NULL;
5878 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5879 enum isl_dim_type type, unsigned pos, isl_int value)
5881 return map_bound(map, type, pos, value, 0);
5884 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5885 enum isl_dim_type type, unsigned pos, isl_int value)
5887 return map_bound(map, type, pos, value, 1);
5890 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5891 enum isl_dim_type type, unsigned pos, isl_int value)
5893 return isl_map_lower_bound(set, type, pos, value);
5896 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5897 enum isl_dim_type type, unsigned pos, isl_int value)
5899 return isl_map_upper_bound(set, type, pos, value);
5902 /* Force the values of the variable at position "pos" of type "type" of "set"
5903 * to be no smaller than "value".
5905 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5906 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5908 if (!value)
5909 goto error;
5910 if (!isl_val_is_int(value))
5911 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5912 "expecting integer value", goto error);
5913 set = isl_set_lower_bound(set, type, pos, value->n);
5914 isl_val_free(value);
5915 return set;
5916 error:
5917 isl_val_free(value);
5918 isl_set_free(set);
5919 return NULL;
5922 /* Force the values of the variable at position "pos" of type "type" of "set"
5923 * to be no greater than "value".
5925 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5926 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5928 if (!value)
5929 goto error;
5930 if (!isl_val_is_int(value))
5931 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5932 "expecting integer value", goto error);
5933 set = isl_set_upper_bound(set, type, pos, value->n);
5934 isl_val_free(value);
5935 return set;
5936 error:
5937 isl_val_free(value);
5938 isl_set_free(set);
5939 return NULL;
5942 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5943 isl_int value)
5945 int i;
5947 set = isl_set_cow(set);
5948 if (!set)
5949 return NULL;
5951 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5952 for (i = 0; i < set->n; ++i) {
5953 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5954 if (!set->p[i])
5955 goto error;
5957 return set;
5958 error:
5959 isl_set_free(set);
5960 return NULL;
5963 struct isl_map *isl_map_reverse(struct isl_map *map)
5965 int i;
5967 map = isl_map_cow(map);
5968 if (!map)
5969 return NULL;
5971 map->dim = isl_space_reverse(map->dim);
5972 if (!map->dim)
5973 goto error;
5974 for (i = 0; i < map->n; ++i) {
5975 map->p[i] = isl_basic_map_reverse(map->p[i]);
5976 if (!map->p[i])
5977 goto error;
5979 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5980 return map;
5981 error:
5982 isl_map_free(map);
5983 return NULL;
5986 static struct isl_map *isl_basic_map_partial_lexopt(
5987 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5988 struct isl_set **empty, int max)
5990 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5993 struct isl_map *isl_basic_map_partial_lexmax(
5994 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5995 struct isl_set **empty)
5997 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6000 struct isl_map *isl_basic_map_partial_lexmin(
6001 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6002 struct isl_set **empty)
6004 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6007 struct isl_set *isl_basic_set_partial_lexmin(
6008 struct isl_basic_set *bset, struct isl_basic_set *dom,
6009 struct isl_set **empty)
6011 return (struct isl_set *)
6012 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6013 dom, empty);
6016 struct isl_set *isl_basic_set_partial_lexmax(
6017 struct isl_basic_set *bset, struct isl_basic_set *dom,
6018 struct isl_set **empty)
6020 return (struct isl_set *)
6021 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6022 dom, empty);
6025 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6026 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6027 __isl_give isl_set **empty)
6029 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6032 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6033 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6034 __isl_give isl_set **empty)
6036 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6039 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6040 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6041 __isl_give isl_set **empty)
6043 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6046 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6047 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6048 __isl_give isl_set **empty)
6050 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6053 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6054 __isl_take isl_basic_map *bmap, int max)
6056 isl_basic_set *dom = NULL;
6057 isl_space *dom_space;
6059 if (!bmap)
6060 goto error;
6061 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6062 dom = isl_basic_set_universe(dom_space);
6063 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6064 error:
6065 isl_basic_map_free(bmap);
6066 return NULL;
6069 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6070 __isl_take isl_basic_map *bmap)
6072 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6075 #undef TYPE
6076 #define TYPE isl_pw_multi_aff
6077 #undef SUFFIX
6078 #define SUFFIX _pw_multi_aff
6079 #undef EMPTY
6080 #define EMPTY isl_pw_multi_aff_empty
6081 #undef ADD
6082 #define ADD isl_pw_multi_aff_union_add
6083 #include "isl_map_lexopt_templ.c"
6085 /* Given a map "map", compute the lexicographically minimal
6086 * (or maximal) image element for each domain element in dom,
6087 * in the form of an isl_pw_multi_aff.
6088 * Set *empty to those elements in dom that do not have an image element.
6090 * We first compute the lexicographically minimal or maximal element
6091 * in the first basic map. This results in a partial solution "res"
6092 * and a subset "todo" of dom that still need to be handled.
6093 * We then consider each of the remaining maps in "map" and successively
6094 * update both "res" and "todo".
6096 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6097 __isl_take isl_map *map, __isl_take isl_set *dom,
6098 __isl_give isl_set **empty, int max)
6100 int i;
6101 isl_pw_multi_aff *res;
6102 isl_set *todo;
6104 if (!map || !dom)
6105 goto error;
6107 if (isl_map_plain_is_empty(map)) {
6108 if (empty)
6109 *empty = dom;
6110 else
6111 isl_set_free(dom);
6112 return isl_pw_multi_aff_from_map(map);
6115 res = basic_map_partial_lexopt_pw_multi_aff(
6116 isl_basic_map_copy(map->p[0]),
6117 isl_set_copy(dom), &todo, max);
6119 for (i = 1; i < map->n; ++i) {
6120 isl_pw_multi_aff *res_i;
6121 isl_set *todo_i;
6123 res_i = basic_map_partial_lexopt_pw_multi_aff(
6124 isl_basic_map_copy(map->p[i]),
6125 isl_set_copy(dom), &todo_i, max);
6127 if (max)
6128 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6129 else
6130 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6132 todo = isl_set_intersect(todo, todo_i);
6135 isl_set_free(dom);
6136 isl_map_free(map);
6138 if (empty)
6139 *empty = todo;
6140 else
6141 isl_set_free(todo);
6143 return res;
6144 error:
6145 if (empty)
6146 *empty = NULL;
6147 isl_set_free(dom);
6148 isl_map_free(map);
6149 return NULL;
6152 #undef TYPE
6153 #define TYPE isl_map
6154 #undef SUFFIX
6155 #define SUFFIX
6156 #undef EMPTY
6157 #define EMPTY isl_map_empty
6158 #undef ADD
6159 #define ADD isl_map_union_disjoint
6160 #include "isl_map_lexopt_templ.c"
6162 /* Given a map "map", compute the lexicographically minimal
6163 * (or maximal) image element for each domain element in dom.
6164 * Set *empty to those elements in dom that do not have an image element.
6166 * We first compute the lexicographically minimal or maximal element
6167 * in the first basic map. This results in a partial solution "res"
6168 * and a subset "todo" of dom that still need to be handled.
6169 * We then consider each of the remaining maps in "map" and successively
6170 * update both "res" and "todo".
6172 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6173 * Assume we are computing the lexicographical maximum.
6174 * We first compute the lexicographically maximal element in basic map i.
6175 * This results in a partial solution res_i and a subset todo_i.
6176 * Then we combine these results with those obtain for the first k basic maps
6177 * to obtain a result that is valid for the first k+1 basic maps.
6178 * In particular, the set where there is no solution is the set where
6179 * there is no solution for the first k basic maps and also no solution
6180 * for the ith basic map, i.e.,
6182 * todo^i = todo^k * todo_i
6184 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6185 * solutions, arbitrarily breaking ties in favor of res^k.
6186 * That is, when res^k(a) >= res_i(a), we pick res^k and
6187 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6188 * the lexicographic order.)
6189 * In practice, we compute
6191 * res^k * (res_i . "<=")
6193 * and
6195 * res_i * (res^k . "<")
6197 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6198 * where only one of res^k and res_i provides a solution and we simply pick
6199 * that one, i.e.,
6201 * res^k * todo_i
6202 * and
6203 * res_i * todo^k
6205 * Note that we only compute these intersections when dom(res^k) intersects
6206 * dom(res_i). Otherwise, the only effect of these intersections is to
6207 * potentially break up res^k and res_i into smaller pieces.
6208 * We want to avoid such splintering as much as possible.
6209 * In fact, an earlier implementation of this function would look for
6210 * better results in the domain of res^k and for extra results in todo^k,
6211 * but this would always result in a splintering according to todo^k,
6212 * even when the domain of basic map i is disjoint from the domains of
6213 * the previous basic maps.
6215 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6216 __isl_take isl_map *map, __isl_take isl_set *dom,
6217 __isl_give isl_set **empty, int max)
6219 int i;
6220 struct isl_map *res;
6221 struct isl_set *todo;
6223 if (!map || !dom)
6224 goto error;
6226 if (isl_map_plain_is_empty(map)) {
6227 if (empty)
6228 *empty = dom;
6229 else
6230 isl_set_free(dom);
6231 return map;
6234 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6235 isl_set_copy(dom), &todo, max);
6237 for (i = 1; i < map->n; ++i) {
6238 isl_map *lt, *le;
6239 isl_map *res_i;
6240 isl_set *todo_i;
6241 isl_space *dim = isl_space_range(isl_map_get_space(res));
6243 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6244 isl_set_copy(dom), &todo_i, max);
6246 if (max) {
6247 lt = isl_map_lex_lt(isl_space_copy(dim));
6248 le = isl_map_lex_le(dim);
6249 } else {
6250 lt = isl_map_lex_gt(isl_space_copy(dim));
6251 le = isl_map_lex_ge(dim);
6253 lt = isl_map_apply_range(isl_map_copy(res), lt);
6254 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6255 le = isl_map_apply_range(isl_map_copy(res_i), le);
6256 le = isl_map_intersect(le, isl_map_copy(res));
6258 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6259 res = isl_map_intersect_domain(res,
6260 isl_set_copy(todo_i));
6261 res_i = isl_map_intersect_domain(res_i,
6262 isl_set_copy(todo));
6265 res = isl_map_union_disjoint(res, res_i);
6266 res = isl_map_union_disjoint(res, lt);
6267 res = isl_map_union_disjoint(res, le);
6269 todo = isl_set_intersect(todo, todo_i);
6272 isl_set_free(dom);
6273 isl_map_free(map);
6275 if (empty)
6276 *empty = todo;
6277 else
6278 isl_set_free(todo);
6280 return res;
6281 error:
6282 if (empty)
6283 *empty = NULL;
6284 isl_set_free(dom);
6285 isl_map_free(map);
6286 return NULL;
6289 __isl_give isl_map *isl_map_partial_lexmax(
6290 __isl_take isl_map *map, __isl_take isl_set *dom,
6291 __isl_give isl_set **empty)
6293 return isl_map_partial_lexopt(map, dom, empty, 1);
6296 __isl_give isl_map *isl_map_partial_lexmin(
6297 __isl_take isl_map *map, __isl_take isl_set *dom,
6298 __isl_give isl_set **empty)
6300 return isl_map_partial_lexopt(map, dom, empty, 0);
6303 __isl_give isl_set *isl_set_partial_lexmin(
6304 __isl_take isl_set *set, __isl_take isl_set *dom,
6305 __isl_give isl_set **empty)
6307 return (struct isl_set *)
6308 isl_map_partial_lexmin((struct isl_map *)set,
6309 dom, empty);
6312 __isl_give isl_set *isl_set_partial_lexmax(
6313 __isl_take isl_set *set, __isl_take isl_set *dom,
6314 __isl_give isl_set **empty)
6316 return (struct isl_set *)
6317 isl_map_partial_lexmax((struct isl_map *)set,
6318 dom, empty);
6321 /* Compute the lexicographic minimum (or maximum if "max" is set)
6322 * of "bmap" over its domain.
6324 * Since we are not interested in the part of the domain space where
6325 * there is no solution, we initialize the domain to those constraints
6326 * of "bmap" that only involve the parameters and the input dimensions.
6327 * This relieves the parametric programming engine from detecting those
6328 * inequalities and transferring them to the context. More importantly,
6329 * it ensures that those inequalities are transferred first and not
6330 * intermixed with inequalities that actually split the domain.
6332 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6334 int n_div;
6335 int n_out;
6336 isl_basic_map *copy;
6337 isl_basic_set *dom;
6339 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6340 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6341 copy = isl_basic_map_copy(bmap);
6342 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6343 isl_dim_div, 0, n_div);
6344 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6345 isl_dim_out, 0, n_out);
6346 dom = isl_basic_map_domain(copy);
6347 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6350 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6352 return isl_basic_map_lexopt(bmap, 0);
6355 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6357 return isl_basic_map_lexopt(bmap, 1);
6360 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6362 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6365 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6367 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6370 /* Extract the first and only affine expression from list
6371 * and then add it to *pwaff with the given dom.
6372 * This domain is known to be disjoint from other domains
6373 * because of the way isl_basic_map_foreach_lexmax works.
6375 static int update_dim_opt(__isl_take isl_basic_set *dom,
6376 __isl_take isl_aff_list *list, void *user)
6378 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6379 isl_aff *aff;
6380 isl_pw_aff **pwaff = user;
6381 isl_pw_aff *pwaff_i;
6383 if (!list)
6384 goto error;
6385 if (isl_aff_list_n_aff(list) != 1)
6386 isl_die(ctx, isl_error_internal,
6387 "expecting single element list", goto error);
6389 aff = isl_aff_list_get_aff(list, 0);
6390 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6392 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6394 isl_aff_list_free(list);
6396 return 0;
6397 error:
6398 isl_basic_set_free(dom);
6399 isl_aff_list_free(list);
6400 return -1;
6403 /* Given a basic map with one output dimension, compute the minimum or
6404 * maximum of that dimension as an isl_pw_aff.
6406 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6407 * call update_dim_opt on each leaf of the result.
6409 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6410 int max)
6412 isl_space *dim = isl_basic_map_get_space(bmap);
6413 isl_pw_aff *pwaff;
6414 int r;
6416 dim = isl_space_from_domain(isl_space_domain(dim));
6417 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6418 pwaff = isl_pw_aff_empty(dim);
6420 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6421 if (r < 0)
6422 return isl_pw_aff_free(pwaff);
6424 return pwaff;
6427 /* Compute the minimum or maximum of the given output dimension
6428 * as a function of the parameters and the input dimensions,
6429 * but independently of the other output dimensions.
6431 * We first project out the other output dimension and then compute
6432 * the "lexicographic" maximum in each basic map, combining the results
6433 * using isl_pw_aff_union_max.
6435 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6436 int max)
6438 int i;
6439 isl_pw_aff *pwaff;
6440 unsigned n_out;
6442 n_out = isl_map_dim(map, isl_dim_out);
6443 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6444 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6445 if (!map)
6446 return NULL;
6448 if (map->n == 0) {
6449 isl_space *dim = isl_map_get_space(map);
6450 dim = isl_space_domain(isl_space_from_range(dim));
6451 isl_map_free(map);
6452 return isl_pw_aff_empty(dim);
6455 pwaff = basic_map_dim_opt(map->p[0], max);
6456 for (i = 1; i < map->n; ++i) {
6457 isl_pw_aff *pwaff_i;
6459 pwaff_i = basic_map_dim_opt(map->p[i], max);
6460 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6463 isl_map_free(map);
6465 return pwaff;
6468 /* Compute the maximum of the given output dimension as a function of the
6469 * parameters and input dimensions, but independently of
6470 * the other output dimensions.
6472 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6474 return map_dim_opt(map, pos, 1);
6477 /* Compute the minimum or maximum of the given set dimension
6478 * as a function of the parameters,
6479 * but independently of the other set dimensions.
6481 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6482 int max)
6484 return map_dim_opt(set, pos, max);
6487 /* Compute the maximum of the given set dimension as a function of the
6488 * parameters, but independently of the other set dimensions.
6490 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6492 return set_dim_opt(set, pos, 1);
6495 /* Compute the minimum of the given set dimension as a function of the
6496 * parameters, but independently of the other set dimensions.
6498 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6500 return set_dim_opt(set, pos, 0);
6503 /* Apply a preimage specified by "mat" on the parameters of "bset".
6504 * bset is assumed to have only parameters and divs.
6506 static struct isl_basic_set *basic_set_parameter_preimage(
6507 struct isl_basic_set *bset, struct isl_mat *mat)
6509 unsigned nparam;
6511 if (!bset || !mat)
6512 goto error;
6514 bset->dim = isl_space_cow(bset->dim);
6515 if (!bset->dim)
6516 goto error;
6518 nparam = isl_basic_set_dim(bset, isl_dim_param);
6520 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6522 bset->dim->nparam = 0;
6523 bset->dim->n_out = nparam;
6524 bset = isl_basic_set_preimage(bset, mat);
6525 if (bset) {
6526 bset->dim->nparam = bset->dim->n_out;
6527 bset->dim->n_out = 0;
6529 return bset;
6530 error:
6531 isl_mat_free(mat);
6532 isl_basic_set_free(bset);
6533 return NULL;
6536 /* Apply a preimage specified by "mat" on the parameters of "set".
6537 * set is assumed to have only parameters and divs.
6539 static struct isl_set *set_parameter_preimage(
6540 struct isl_set *set, struct isl_mat *mat)
6542 isl_space *dim = NULL;
6543 unsigned nparam;
6545 if (!set || !mat)
6546 goto error;
6548 dim = isl_space_copy(set->dim);
6549 dim = isl_space_cow(dim);
6550 if (!dim)
6551 goto error;
6553 nparam = isl_set_dim(set, isl_dim_param);
6555 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6557 dim->nparam = 0;
6558 dim->n_out = nparam;
6559 isl_set_reset_space(set, dim);
6560 set = isl_set_preimage(set, mat);
6561 if (!set)
6562 goto error2;
6563 dim = isl_space_copy(set->dim);
6564 dim = isl_space_cow(dim);
6565 if (!dim)
6566 goto error2;
6567 dim->nparam = dim->n_out;
6568 dim->n_out = 0;
6569 isl_set_reset_space(set, dim);
6570 return set;
6571 error:
6572 isl_space_free(dim);
6573 isl_mat_free(mat);
6574 error2:
6575 isl_set_free(set);
6576 return NULL;
6579 /* Intersect the basic set "bset" with the affine space specified by the
6580 * equalities in "eq".
6582 static struct isl_basic_set *basic_set_append_equalities(
6583 struct isl_basic_set *bset, struct isl_mat *eq)
6585 int i, k;
6586 unsigned len;
6588 if (!bset || !eq)
6589 goto error;
6591 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6592 eq->n_row, 0);
6593 if (!bset)
6594 goto error;
6596 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6597 for (i = 0; i < eq->n_row; ++i) {
6598 k = isl_basic_set_alloc_equality(bset);
6599 if (k < 0)
6600 goto error;
6601 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6602 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6604 isl_mat_free(eq);
6606 bset = isl_basic_set_gauss(bset, NULL);
6607 bset = isl_basic_set_finalize(bset);
6609 return bset;
6610 error:
6611 isl_mat_free(eq);
6612 isl_basic_set_free(bset);
6613 return NULL;
6616 /* Intersect the set "set" with the affine space specified by the
6617 * equalities in "eq".
6619 static struct isl_set *set_append_equalities(struct isl_set *set,
6620 struct isl_mat *eq)
6622 int i;
6624 if (!set || !eq)
6625 goto error;
6627 for (i = 0; i < set->n; ++i) {
6628 set->p[i] = basic_set_append_equalities(set->p[i],
6629 isl_mat_copy(eq));
6630 if (!set->p[i])
6631 goto error;
6633 isl_mat_free(eq);
6634 return set;
6635 error:
6636 isl_mat_free(eq);
6637 isl_set_free(set);
6638 return NULL;
6641 /* Given a basic set "bset" that only involves parameters and existentially
6642 * quantified variables, return the index of the first equality
6643 * that only involves parameters. If there is no such equality then
6644 * return bset->n_eq.
6646 * This function assumes that isl_basic_set_gauss has been called on "bset".
6648 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6650 int i, j;
6651 unsigned nparam, n_div;
6653 if (!bset)
6654 return -1;
6656 nparam = isl_basic_set_dim(bset, isl_dim_param);
6657 n_div = isl_basic_set_dim(bset, isl_dim_div);
6659 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6660 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6661 ++i;
6664 return i;
6667 /* Compute an explicit representation for the existentially quantified
6668 * variables in "bset" by computing the "minimal value" of the set
6669 * variables. Since there are no set variables, the computation of
6670 * the minimal value essentially computes an explicit representation
6671 * of the non-empty part(s) of "bset".
6673 * The input only involves parameters and existentially quantified variables.
6674 * All equalities among parameters have been removed.
6676 * Since the existentially quantified variables in the result are in general
6677 * going to be different from those in the input, we first replace
6678 * them by the minimal number of variables based on their equalities.
6679 * This should simplify the parametric integer programming.
6681 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6683 isl_morph *morph1, *morph2;
6684 isl_set *set;
6685 unsigned n;
6687 if (!bset)
6688 return NULL;
6689 if (bset->n_eq == 0)
6690 return isl_basic_set_lexmin(bset);
6692 morph1 = isl_basic_set_parameter_compression(bset);
6693 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6694 bset = isl_basic_set_lift(bset);
6695 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6696 bset = isl_morph_basic_set(morph2, bset);
6697 n = isl_basic_set_dim(bset, isl_dim_set);
6698 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6700 set = isl_basic_set_lexmin(bset);
6702 set = isl_morph_set(isl_morph_inverse(morph1), set);
6704 return set;
6707 /* Project the given basic set onto its parameter domain, possibly introducing
6708 * new, explicit, existential variables in the constraints.
6709 * The input has parameters and (possibly implicit) existential variables.
6710 * The output has the same parameters, but only
6711 * explicit existentially quantified variables.
6713 * The actual projection is performed by pip, but pip doesn't seem
6714 * to like equalities very much, so we first remove the equalities
6715 * among the parameters by performing a variable compression on
6716 * the parameters. Afterward, an inverse transformation is performed
6717 * and the equalities among the parameters are inserted back in.
6719 * The variable compression on the parameters may uncover additional
6720 * equalities that were only implicit before. We therefore check
6721 * if there are any new parameter equalities in the result and
6722 * if so recurse. The removal of parameter equalities is required
6723 * for the parameter compression performed by base_compute_divs.
6725 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6727 int i;
6728 struct isl_mat *eq;
6729 struct isl_mat *T, *T2;
6730 struct isl_set *set;
6731 unsigned nparam;
6733 bset = isl_basic_set_cow(bset);
6734 if (!bset)
6735 return NULL;
6737 if (bset->n_eq == 0)
6738 return base_compute_divs(bset);
6740 bset = isl_basic_set_gauss(bset, NULL);
6741 if (!bset)
6742 return NULL;
6743 if (isl_basic_set_plain_is_empty(bset))
6744 return isl_set_from_basic_set(bset);
6746 i = first_parameter_equality(bset);
6747 if (i == bset->n_eq)
6748 return base_compute_divs(bset);
6750 nparam = isl_basic_set_dim(bset, isl_dim_param);
6751 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6752 0, 1 + nparam);
6753 eq = isl_mat_cow(eq);
6754 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6755 if (T && T->n_col == 0) {
6756 isl_mat_free(T);
6757 isl_mat_free(T2);
6758 isl_mat_free(eq);
6759 bset = isl_basic_set_set_to_empty(bset);
6760 return isl_set_from_basic_set(bset);
6762 bset = basic_set_parameter_preimage(bset, T);
6764 i = first_parameter_equality(bset);
6765 if (!bset)
6766 set = NULL;
6767 else if (i == bset->n_eq)
6768 set = base_compute_divs(bset);
6769 else
6770 set = parameter_compute_divs(bset);
6771 set = set_parameter_preimage(set, T2);
6772 set = set_append_equalities(set, eq);
6773 return set;
6776 /* Insert the divs from "ls" before those of "bmap".
6778 * The number of columns is not changed, which means that the last
6779 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6780 * The caller is responsible for removing the same number of dimensions
6781 * from the space of "bmap".
6783 static __isl_give isl_basic_map *insert_divs_from_local_space(
6784 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6786 int i;
6787 int n_div;
6788 int old_n_div;
6790 n_div = isl_local_space_dim(ls, isl_dim_div);
6791 if (n_div == 0)
6792 return bmap;
6794 old_n_div = bmap->n_div;
6795 bmap = insert_div_rows(bmap, n_div);
6796 if (!bmap)
6797 return NULL;
6799 for (i = 0; i < n_div; ++i) {
6800 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6801 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6804 return bmap;
6807 /* Replace the space of "bmap" by the space and divs of "ls".
6809 * If "ls" has any divs, then we simplify the result since we may
6810 * have discovered some additional equalities that could simplify
6811 * the div expressions.
6813 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6814 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6816 int n_div;
6818 bmap = isl_basic_map_cow(bmap);
6819 if (!bmap || !ls)
6820 goto error;
6822 n_div = isl_local_space_dim(ls, isl_dim_div);
6823 bmap = insert_divs_from_local_space(bmap, ls);
6824 if (!bmap)
6825 goto error;
6827 isl_space_free(bmap->dim);
6828 bmap->dim = isl_local_space_get_space(ls);
6829 if (!bmap->dim)
6830 goto error;
6832 isl_local_space_free(ls);
6833 if (n_div > 0)
6834 bmap = isl_basic_map_simplify(bmap);
6835 bmap = isl_basic_map_finalize(bmap);
6836 return bmap;
6837 error:
6838 isl_basic_map_free(bmap);
6839 isl_local_space_free(ls);
6840 return NULL;
6843 /* Replace the space of "map" by the space and divs of "ls".
6845 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6846 __isl_take isl_local_space *ls)
6848 int i;
6850 map = isl_map_cow(map);
6851 if (!map || !ls)
6852 goto error;
6854 for (i = 0; i < map->n; ++i) {
6855 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6856 isl_local_space_copy(ls));
6857 if (!map->p[i])
6858 goto error;
6860 isl_space_free(map->dim);
6861 map->dim = isl_local_space_get_space(ls);
6862 if (!map->dim)
6863 goto error;
6865 isl_local_space_free(ls);
6866 return map;
6867 error:
6868 isl_local_space_free(ls);
6869 isl_map_free(map);
6870 return NULL;
6873 /* Compute an explicit representation for the existentially
6874 * quantified variables for which do not know any explicit representation yet.
6876 * We first sort the existentially quantified variables so that the
6877 * existentially quantified variables for which we already have an explicit
6878 * representation are placed before those for which we do not.
6879 * The input dimensions, the output dimensions and the existentially
6880 * quantified variables for which we already have an explicit
6881 * representation are then turned into parameters.
6882 * compute_divs returns a map with the same parameters and
6883 * no input or output dimensions and the dimension specification
6884 * is reset to that of the input, including the existentially quantified
6885 * variables for which we already had an explicit representation.
6887 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6889 struct isl_basic_set *bset;
6890 struct isl_set *set;
6891 struct isl_map *map;
6892 isl_space *dim;
6893 isl_local_space *ls;
6894 unsigned nparam;
6895 unsigned n_in;
6896 unsigned n_out;
6897 unsigned n_known;
6898 int i;
6900 bmap = isl_basic_map_sort_divs(bmap);
6901 bmap = isl_basic_map_cow(bmap);
6902 if (!bmap)
6903 return NULL;
6905 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6906 if (isl_int_is_zero(bmap->div[n_known][0]))
6907 break;
6909 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6910 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6911 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6912 dim = isl_space_set_alloc(bmap->ctx,
6913 nparam + n_in + n_out + n_known, 0);
6914 if (!dim)
6915 goto error;
6917 ls = isl_basic_map_get_local_space(bmap);
6918 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6919 n_known, bmap->n_div - n_known);
6920 if (n_known > 0) {
6921 for (i = n_known; i < bmap->n_div; ++i)
6922 swap_div(bmap, i - n_known, i);
6923 bmap->n_div -= n_known;
6924 bmap->extra -= n_known;
6926 bmap = isl_basic_map_reset_space(bmap, dim);
6927 bset = (struct isl_basic_set *)bmap;
6929 set = parameter_compute_divs(bset);
6930 map = (struct isl_map *)set;
6931 map = replace_space_by_local_space(map, ls);
6933 return map;
6934 error:
6935 isl_basic_map_free(bmap);
6936 return NULL;
6939 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6941 int i;
6942 unsigned off;
6944 if (!bmap)
6945 return -1;
6947 off = isl_space_dim(bmap->dim, isl_dim_all);
6948 for (i = 0; i < bmap->n_div; ++i) {
6949 if (isl_int_is_zero(bmap->div[i][0]))
6950 return 0;
6951 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6952 return -1);
6954 return 1;
6957 static int map_divs_known(__isl_keep isl_map *map)
6959 int i;
6961 if (!map)
6962 return -1;
6964 for (i = 0; i < map->n; ++i) {
6965 int known = isl_basic_map_divs_known(map->p[i]);
6966 if (known <= 0)
6967 return known;
6970 return 1;
6973 /* If bmap contains any unknown divs, then compute explicit
6974 * expressions for them. However, this computation may be
6975 * quite expensive, so first try to remove divs that aren't
6976 * strictly needed.
6978 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6980 int known;
6981 struct isl_map *map;
6983 known = isl_basic_map_divs_known(bmap);
6984 if (known < 0)
6985 goto error;
6986 if (known)
6987 return isl_map_from_basic_map(bmap);
6989 bmap = isl_basic_map_drop_redundant_divs(bmap);
6991 known = isl_basic_map_divs_known(bmap);
6992 if (known < 0)
6993 goto error;
6994 if (known)
6995 return isl_map_from_basic_map(bmap);
6997 map = compute_divs(bmap);
6998 return map;
6999 error:
7000 isl_basic_map_free(bmap);
7001 return NULL;
7004 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7006 int i;
7007 int known;
7008 struct isl_map *res;
7010 if (!map)
7011 return NULL;
7012 if (map->n == 0)
7013 return map;
7015 known = map_divs_known(map);
7016 if (known < 0) {
7017 isl_map_free(map);
7018 return NULL;
7020 if (known)
7021 return map;
7023 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7024 for (i = 1 ; i < map->n; ++i) {
7025 struct isl_map *r2;
7026 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7027 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7028 res = isl_map_union_disjoint(res, r2);
7029 else
7030 res = isl_map_union(res, r2);
7032 isl_map_free(map);
7034 return res;
7037 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7039 return (struct isl_set *)
7040 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7043 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7045 return (struct isl_set *)
7046 isl_map_compute_divs((struct isl_map *)set);
7049 struct isl_set *isl_map_domain(struct isl_map *map)
7051 int i;
7052 struct isl_set *set;
7054 if (!map)
7055 goto error;
7057 map = isl_map_cow(map);
7058 if (!map)
7059 return NULL;
7061 set = (struct isl_set *)map;
7062 set->dim = isl_space_domain(set->dim);
7063 if (!set->dim)
7064 goto error;
7065 for (i = 0; i < map->n; ++i) {
7066 set->p[i] = isl_basic_map_domain(map->p[i]);
7067 if (!set->p[i])
7068 goto error;
7070 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7071 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7072 return set;
7073 error:
7074 isl_map_free(map);
7075 return NULL;
7078 /* Return the union of "map1" and "map2", where we assume for now that
7079 * "map1" and "map2" are disjoint. Note that the basic maps inside
7080 * "map1" or "map2" may not be disjoint from each other.
7081 * Also note that this function is also called from isl_map_union,
7082 * which takes care of handling the situation where "map1" and "map2"
7083 * may not be disjoint.
7085 * If one of the inputs is empty, we can simply return the other input.
7086 * Similarly, if one of the inputs is universal, then it is equal to the union.
7088 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7089 __isl_take isl_map *map2)
7091 int i;
7092 unsigned flags = 0;
7093 struct isl_map *map = NULL;
7094 int is_universe;
7096 if (!map1 || !map2)
7097 goto error;
7099 if (map1->n == 0) {
7100 isl_map_free(map1);
7101 return map2;
7103 if (map2->n == 0) {
7104 isl_map_free(map2);
7105 return map1;
7108 is_universe = isl_map_plain_is_universe(map1);
7109 if (is_universe < 0)
7110 goto error;
7111 if (is_universe) {
7112 isl_map_free(map2);
7113 return map1;
7116 is_universe = isl_map_plain_is_universe(map2);
7117 if (is_universe < 0)
7118 goto error;
7119 if (is_universe) {
7120 isl_map_free(map1);
7121 return map2;
7124 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
7126 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7127 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7128 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7130 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7131 map1->n + map2->n, flags);
7132 if (!map)
7133 goto error;
7134 for (i = 0; i < map1->n; ++i) {
7135 map = isl_map_add_basic_map(map,
7136 isl_basic_map_copy(map1->p[i]));
7137 if (!map)
7138 goto error;
7140 for (i = 0; i < map2->n; ++i) {
7141 map = isl_map_add_basic_map(map,
7142 isl_basic_map_copy(map2->p[i]));
7143 if (!map)
7144 goto error;
7146 isl_map_free(map1);
7147 isl_map_free(map2);
7148 return map;
7149 error:
7150 isl_map_free(map);
7151 isl_map_free(map1);
7152 isl_map_free(map2);
7153 return NULL;
7156 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7157 __isl_take isl_map *map2)
7159 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7162 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
7164 map1 = isl_map_union_disjoint(map1, map2);
7165 if (!map1)
7166 return NULL;
7167 if (map1->n > 1)
7168 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7169 return map1;
7172 struct isl_set *isl_set_union_disjoint(
7173 struct isl_set *set1, struct isl_set *set2)
7175 return (struct isl_set *)
7176 isl_map_union_disjoint(
7177 (struct isl_map *)set1, (struct isl_map *)set2);
7180 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7182 return (struct isl_set *)
7183 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7186 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7187 * the results.
7189 * "map" and "set" are assumed to be compatible and non-NULL.
7191 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7192 __isl_take isl_set *set,
7193 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7194 __isl_take isl_basic_set *bset))
7196 unsigned flags = 0;
7197 struct isl_map *result;
7198 int i, j;
7200 if (isl_set_plain_is_universe(set)) {
7201 isl_set_free(set);
7202 return map;
7205 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7206 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7207 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7209 result = isl_map_alloc_space(isl_space_copy(map->dim),
7210 map->n * set->n, flags);
7211 for (i = 0; result && i < map->n; ++i)
7212 for (j = 0; j < set->n; ++j) {
7213 result = isl_map_add_basic_map(result,
7214 fn(isl_basic_map_copy(map->p[i]),
7215 isl_basic_set_copy(set->p[j])));
7216 if (!result)
7217 break;
7220 isl_map_free(map);
7221 isl_set_free(set);
7222 return result;
7225 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7226 __isl_take isl_set *set)
7228 if (!map || !set)
7229 goto error;
7231 if (!isl_map_compatible_range(map, set))
7232 isl_die(set->ctx, isl_error_invalid,
7233 "incompatible spaces", goto error);
7235 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7236 error:
7237 isl_map_free(map);
7238 isl_set_free(set);
7239 return NULL;
7242 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7243 __isl_take isl_set *set)
7245 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7248 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7249 __isl_take isl_set *set)
7251 if (!map || !set)
7252 goto error;
7254 if (!isl_map_compatible_domain(map, set))
7255 isl_die(set->ctx, isl_error_invalid,
7256 "incompatible spaces", goto error);
7258 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7259 error:
7260 isl_map_free(map);
7261 isl_set_free(set);
7262 return NULL;
7265 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7266 __isl_take isl_set *set)
7268 return isl_map_align_params_map_map_and(map, set,
7269 &map_intersect_domain);
7272 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7273 __isl_take isl_map *map2)
7275 if (!map1 || !map2)
7276 goto error;
7277 map1 = isl_map_reverse(map1);
7278 map1 = isl_map_apply_range(map1, map2);
7279 return isl_map_reverse(map1);
7280 error:
7281 isl_map_free(map1);
7282 isl_map_free(map2);
7283 return NULL;
7286 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7287 __isl_take isl_map *map2)
7289 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7292 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7293 __isl_take isl_map *map2)
7295 isl_space *dim_result;
7296 struct isl_map *result;
7297 int i, j;
7299 if (!map1 || !map2)
7300 goto error;
7302 dim_result = isl_space_join(isl_space_copy(map1->dim),
7303 isl_space_copy(map2->dim));
7305 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7306 if (!result)
7307 goto error;
7308 for (i = 0; i < map1->n; ++i)
7309 for (j = 0; j < map2->n; ++j) {
7310 result = isl_map_add_basic_map(result,
7311 isl_basic_map_apply_range(
7312 isl_basic_map_copy(map1->p[i]),
7313 isl_basic_map_copy(map2->p[j])));
7314 if (!result)
7315 goto error;
7317 isl_map_free(map1);
7318 isl_map_free(map2);
7319 if (result && result->n <= 1)
7320 ISL_F_SET(result, ISL_MAP_DISJOINT);
7321 return result;
7322 error:
7323 isl_map_free(map1);
7324 isl_map_free(map2);
7325 return NULL;
7328 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7329 __isl_take isl_map *map2)
7331 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7335 * returns range - domain
7337 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7339 isl_space *dims, *target_dim;
7340 struct isl_basic_set *bset;
7341 unsigned dim;
7342 unsigned nparam;
7343 int i;
7345 if (!bmap)
7346 goto error;
7347 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
7348 bmap->dim, isl_dim_out),
7349 goto error);
7350 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7351 dim = isl_basic_map_n_in(bmap);
7352 nparam = isl_basic_map_n_param(bmap);
7353 bset = isl_basic_set_from_basic_map(bmap);
7354 bset = isl_basic_set_cow(bset);
7355 dims = isl_basic_set_get_space(bset);
7356 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7357 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7358 bset = isl_basic_set_swap_vars(bset, 2*dim);
7359 for (i = 0; i < dim; ++i) {
7360 int j = isl_basic_map_alloc_equality(
7361 (struct isl_basic_map *)bset);
7362 if (j < 0) {
7363 bset = isl_basic_set_free(bset);
7364 break;
7366 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7367 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7368 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7369 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7371 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7372 bset = isl_basic_set_reset_space(bset, target_dim);
7373 return bset;
7374 error:
7375 isl_basic_map_free(bmap);
7376 return NULL;
7380 * returns range - domain
7382 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7384 int i;
7385 isl_space *dim;
7386 struct isl_set *result;
7388 if (!map)
7389 return NULL;
7391 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
7392 map->dim, isl_dim_out),
7393 goto error);
7394 dim = isl_map_get_space(map);
7395 dim = isl_space_domain(dim);
7396 result = isl_set_alloc_space(dim, map->n, 0);
7397 if (!result)
7398 goto error;
7399 for (i = 0; i < map->n; ++i)
7400 result = isl_set_add_basic_set(result,
7401 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7402 isl_map_free(map);
7403 return result;
7404 error:
7405 isl_map_free(map);
7406 return NULL;
7410 * returns [domain -> range] -> range - domain
7412 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7413 __isl_take isl_basic_map *bmap)
7415 int i, k;
7416 isl_space *dim;
7417 isl_basic_map *domain;
7418 int nparam, n;
7419 unsigned total;
7421 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
7422 isl_die(bmap->ctx, isl_error_invalid,
7423 "domain and range don't match", goto error);
7425 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7426 n = isl_basic_map_dim(bmap, isl_dim_in);
7428 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7429 domain = isl_basic_map_universe(dim);
7431 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7432 bmap = isl_basic_map_apply_range(bmap, domain);
7433 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7435 total = isl_basic_map_total_dim(bmap);
7437 for (i = 0; i < n; ++i) {
7438 k = isl_basic_map_alloc_equality(bmap);
7439 if (k < 0)
7440 goto error;
7441 isl_seq_clr(bmap->eq[k], 1 + total);
7442 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7443 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7444 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7447 bmap = isl_basic_map_gauss(bmap, NULL);
7448 return isl_basic_map_finalize(bmap);
7449 error:
7450 isl_basic_map_free(bmap);
7451 return NULL;
7455 * returns [domain -> range] -> range - domain
7457 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7459 int i;
7460 isl_space *domain_dim;
7462 if (!map)
7463 return NULL;
7465 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
7466 isl_die(map->ctx, isl_error_invalid,
7467 "domain and range don't match", goto error);
7469 map = isl_map_cow(map);
7470 if (!map)
7471 return NULL;
7473 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7474 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7475 map->dim = isl_space_join(map->dim, domain_dim);
7476 if (!map->dim)
7477 goto error;
7478 for (i = 0; i < map->n; ++i) {
7479 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7480 if (!map->p[i])
7481 goto error;
7483 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7484 return map;
7485 error:
7486 isl_map_free(map);
7487 return NULL;
7490 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7492 struct isl_basic_map *bmap;
7493 unsigned nparam;
7494 unsigned dim;
7495 int i;
7497 if (!dims)
7498 return NULL;
7500 nparam = dims->nparam;
7501 dim = dims->n_out;
7502 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7503 if (!bmap)
7504 goto error;
7506 for (i = 0; i < dim; ++i) {
7507 int j = isl_basic_map_alloc_equality(bmap);
7508 if (j < 0)
7509 goto error;
7510 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7511 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7512 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7514 return isl_basic_map_finalize(bmap);
7515 error:
7516 isl_basic_map_free(bmap);
7517 return NULL;
7520 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7522 if (!dim)
7523 return NULL;
7524 if (dim->n_in != dim->n_out)
7525 isl_die(dim->ctx, isl_error_invalid,
7526 "number of input and output dimensions needs to be "
7527 "the same", goto error);
7528 return basic_map_identity(dim);
7529 error:
7530 isl_space_free(dim);
7531 return NULL;
7534 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7536 if (!model || !model->dim)
7537 return NULL;
7538 return isl_basic_map_identity(isl_space_copy(model->dim));
7541 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7543 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7546 struct isl_map *isl_map_identity_like(struct isl_map *model)
7548 if (!model || !model->dim)
7549 return NULL;
7550 return isl_map_identity(isl_space_copy(model->dim));
7553 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7555 if (!model || !model->dim)
7556 return NULL;
7557 return isl_map_identity(isl_space_copy(model->dim));
7560 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7562 isl_space *dim = isl_set_get_space(set);
7563 isl_map *id;
7564 id = isl_map_identity(isl_space_map_from_set(dim));
7565 return isl_map_intersect_range(id, set);
7568 /* Construct a basic set with all set dimensions having only non-negative
7569 * values.
7571 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7572 __isl_take isl_space *space)
7574 int i;
7575 unsigned nparam;
7576 unsigned dim;
7577 struct isl_basic_set *bset;
7579 if (!space)
7580 return NULL;
7581 nparam = space->nparam;
7582 dim = space->n_out;
7583 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7584 if (!bset)
7585 return NULL;
7586 for (i = 0; i < dim; ++i) {
7587 int k = isl_basic_set_alloc_inequality(bset);
7588 if (k < 0)
7589 goto error;
7590 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7591 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7593 return bset;
7594 error:
7595 isl_basic_set_free(bset);
7596 return NULL;
7599 /* Construct the half-space x_pos >= 0.
7601 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7602 int pos)
7604 int k;
7605 isl_basic_set *nonneg;
7607 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7608 k = isl_basic_set_alloc_inequality(nonneg);
7609 if (k < 0)
7610 goto error;
7611 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7612 isl_int_set_si(nonneg->ineq[k][pos], 1);
7614 return isl_basic_set_finalize(nonneg);
7615 error:
7616 isl_basic_set_free(nonneg);
7617 return NULL;
7620 /* Construct the half-space x_pos <= -1.
7622 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7624 int k;
7625 isl_basic_set *neg;
7627 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7628 k = isl_basic_set_alloc_inequality(neg);
7629 if (k < 0)
7630 goto error;
7631 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7632 isl_int_set_si(neg->ineq[k][0], -1);
7633 isl_int_set_si(neg->ineq[k][pos], -1);
7635 return isl_basic_set_finalize(neg);
7636 error:
7637 isl_basic_set_free(neg);
7638 return NULL;
7641 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7642 enum isl_dim_type type, unsigned first, unsigned n)
7644 int i;
7645 isl_basic_set *nonneg;
7646 isl_basic_set *neg;
7648 if (!set)
7649 return NULL;
7650 if (n == 0)
7651 return set;
7653 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7655 for (i = 0; i < n; ++i) {
7656 nonneg = nonneg_halfspace(isl_set_get_space(set),
7657 pos(set->dim, type) + first + i);
7658 neg = neg_halfspace(isl_set_get_space(set),
7659 pos(set->dim, type) + first + i);
7661 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7664 return set;
7665 error:
7666 isl_set_free(set);
7667 return NULL;
7670 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7671 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7672 void *user)
7674 isl_set *half;
7676 if (!set)
7677 return -1;
7678 if (isl_set_plain_is_empty(set)) {
7679 isl_set_free(set);
7680 return 0;
7682 if (first == len)
7683 return fn(set, signs, user);
7685 signs[first] = 1;
7686 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7687 1 + first));
7688 half = isl_set_intersect(half, isl_set_copy(set));
7689 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7690 goto error;
7692 signs[first] = -1;
7693 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7694 1 + first));
7695 half = isl_set_intersect(half, set);
7696 return foreach_orthant(half, signs, first + 1, len, fn, user);
7697 error:
7698 isl_set_free(set);
7699 return -1;
7702 /* Call "fn" on the intersections of "set" with each of the orthants
7703 * (except for obviously empty intersections). The orthant is identified
7704 * by the signs array, with each entry having value 1 or -1 according
7705 * to the sign of the corresponding variable.
7707 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7708 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7709 void *user)
7711 unsigned nparam;
7712 unsigned nvar;
7713 int *signs;
7714 int r;
7716 if (!set)
7717 return -1;
7718 if (isl_set_plain_is_empty(set))
7719 return 0;
7721 nparam = isl_set_dim(set, isl_dim_param);
7722 nvar = isl_set_dim(set, isl_dim_set);
7724 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7726 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7727 fn, user);
7729 free(signs);
7731 return r;
7734 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7736 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7739 int isl_basic_map_is_subset(
7740 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7742 int is_subset;
7743 struct isl_map *map1;
7744 struct isl_map *map2;
7746 if (!bmap1 || !bmap2)
7747 return -1;
7749 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7750 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7752 is_subset = isl_map_is_subset(map1, map2);
7754 isl_map_free(map1);
7755 isl_map_free(map2);
7757 return is_subset;
7760 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7761 __isl_keep isl_basic_set *bset2)
7763 return isl_basic_map_is_subset(bset1, bset2);
7766 int isl_basic_map_is_equal(
7767 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7769 int is_subset;
7771 if (!bmap1 || !bmap2)
7772 return -1;
7773 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7774 if (is_subset != 1)
7775 return is_subset;
7776 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7777 return is_subset;
7780 int isl_basic_set_is_equal(
7781 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7783 return isl_basic_map_is_equal(
7784 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7787 int isl_map_is_empty(struct isl_map *map)
7789 int i;
7790 int is_empty;
7792 if (!map)
7793 return -1;
7794 for (i = 0; i < map->n; ++i) {
7795 is_empty = isl_basic_map_is_empty(map->p[i]);
7796 if (is_empty < 0)
7797 return -1;
7798 if (!is_empty)
7799 return 0;
7801 return 1;
7804 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7806 return map ? map->n == 0 : -1;
7809 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7811 return isl_map_plain_is_empty(map);
7814 int isl_set_plain_is_empty(struct isl_set *set)
7816 return set ? set->n == 0 : -1;
7819 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7821 return isl_set_plain_is_empty(set);
7824 int isl_set_is_empty(struct isl_set *set)
7826 return isl_map_is_empty((struct isl_map *)set);
7829 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7831 if (!map1 || !map2)
7832 return -1;
7834 return isl_space_is_equal(map1->dim, map2->dim);
7837 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7839 if (!set1 || !set2)
7840 return -1;
7842 return isl_space_is_equal(set1->dim, set2->dim);
7845 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7847 int is_subset;
7849 if (!map1 || !map2)
7850 return -1;
7851 is_subset = isl_map_is_subset(map1, map2);
7852 if (is_subset != 1)
7853 return is_subset;
7854 is_subset = isl_map_is_subset(map2, map1);
7855 return is_subset;
7858 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7860 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7863 int isl_basic_map_is_strict_subset(
7864 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7866 int is_subset;
7868 if (!bmap1 || !bmap2)
7869 return -1;
7870 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7871 if (is_subset != 1)
7872 return is_subset;
7873 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7874 if (is_subset == -1)
7875 return is_subset;
7876 return !is_subset;
7879 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7881 int is_subset;
7883 if (!map1 || !map2)
7884 return -1;
7885 is_subset = isl_map_is_subset(map1, map2);
7886 if (is_subset != 1)
7887 return is_subset;
7888 is_subset = isl_map_is_subset(map2, map1);
7889 if (is_subset == -1)
7890 return is_subset;
7891 return !is_subset;
7894 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7896 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7899 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7901 if (!bmap)
7902 return -1;
7903 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7906 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7908 if (!bset)
7909 return -1;
7910 return bset->n_eq == 0 && bset->n_ineq == 0;
7913 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7915 int i;
7917 if (!map)
7918 return -1;
7920 for (i = 0; i < map->n; ++i) {
7921 int r = isl_basic_map_is_universe(map->p[i]);
7922 if (r < 0 || r)
7923 return r;
7926 return 0;
7929 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7931 return isl_map_plain_is_universe((isl_map *) set);
7934 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7936 return isl_set_plain_is_universe(set);
7939 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7941 struct isl_basic_set *bset = NULL;
7942 struct isl_vec *sample = NULL;
7943 int empty;
7944 unsigned total;
7946 if (!bmap)
7947 return -1;
7949 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7950 return 1;
7952 if (isl_basic_map_is_universe(bmap))
7953 return 0;
7955 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7956 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7957 copy = isl_basic_map_remove_redundancies(copy);
7958 empty = isl_basic_map_plain_is_empty(copy);
7959 isl_basic_map_free(copy);
7960 return empty;
7963 total = 1 + isl_basic_map_total_dim(bmap);
7964 if (bmap->sample && bmap->sample->size == total) {
7965 int contains = isl_basic_map_contains(bmap, bmap->sample);
7966 if (contains < 0)
7967 return -1;
7968 if (contains)
7969 return 0;
7971 isl_vec_free(bmap->sample);
7972 bmap->sample = NULL;
7973 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7974 if (!bset)
7975 return -1;
7976 sample = isl_basic_set_sample_vec(bset);
7977 if (!sample)
7978 return -1;
7979 empty = sample->size == 0;
7980 isl_vec_free(bmap->sample);
7981 bmap->sample = sample;
7982 if (empty)
7983 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7985 return empty;
7988 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7990 if (!bmap)
7991 return -1;
7992 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7995 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7997 return isl_basic_map_plain_is_empty(bmap);
8000 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8002 if (!bset)
8003 return -1;
8004 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8007 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
8009 return isl_basic_set_plain_is_empty(bset);
8012 int isl_basic_set_is_empty(struct isl_basic_set *bset)
8014 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8017 struct isl_map *isl_basic_map_union(
8018 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8020 struct isl_map *map;
8021 if (!bmap1 || !bmap2)
8022 goto error;
8024 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8026 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8027 if (!map)
8028 goto error;
8029 map = isl_map_add_basic_map(map, bmap1);
8030 map = isl_map_add_basic_map(map, bmap2);
8031 return map;
8032 error:
8033 isl_basic_map_free(bmap1);
8034 isl_basic_map_free(bmap2);
8035 return NULL;
8038 struct isl_set *isl_basic_set_union(
8039 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8041 return (struct isl_set *)isl_basic_map_union(
8042 (struct isl_basic_map *)bset1,
8043 (struct isl_basic_map *)bset2);
8046 /* Order divs such that any div only depends on previous divs */
8047 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8049 int i;
8050 unsigned off;
8052 if (!bmap)
8053 return NULL;
8055 off = isl_space_dim(bmap->dim, isl_dim_all);
8057 for (i = 0; i < bmap->n_div; ++i) {
8058 int pos;
8059 if (isl_int_is_zero(bmap->div[i][0]))
8060 continue;
8061 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8062 bmap->n_div-i);
8063 if (pos == -1)
8064 continue;
8065 isl_basic_map_swap_div(bmap, i, i + pos);
8066 --i;
8068 return bmap;
8071 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8073 return (struct isl_basic_set *)
8074 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8077 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8079 int i;
8081 if (!map)
8082 return 0;
8084 for (i = 0; i < map->n; ++i) {
8085 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8086 if (!map->p[i])
8087 goto error;
8090 return map;
8091 error:
8092 isl_map_free(map);
8093 return NULL;
8096 /* Apply the expansion computed by isl_merge_divs.
8097 * The expansion itself is given by "exp" while the resulting
8098 * list of divs is given by "div".
8100 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8101 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8103 int i, j;
8104 int n_div;
8106 bset = isl_basic_set_cow(bset);
8107 if (!bset || !div)
8108 goto error;
8110 if (div->n_row < bset->n_div)
8111 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8112 "not an expansion", goto error);
8114 n_div = bset->n_div;
8115 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8116 div->n_row - n_div, 0,
8117 2 * (div->n_row - n_div));
8119 for (i = n_div; i < div->n_row; ++i)
8120 if (isl_basic_set_alloc_div(bset) < 0)
8121 goto error;
8123 j = n_div - 1;
8124 for (i = div->n_row - 1; i >= 0; --i) {
8125 if (j >= 0 && exp[j] == i) {
8126 if (i != j)
8127 isl_basic_map_swap_div(bset, i, j);
8128 j--;
8129 } else {
8130 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8131 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8132 goto error;
8136 isl_mat_free(div);
8137 return bset;
8138 error:
8139 isl_basic_set_free(bset);
8140 isl_mat_free(div);
8141 return NULL;
8144 /* Look for a div in dst that corresponds to the div "div" in src.
8145 * The divs before "div" in src and dst are assumed to be the same.
8147 * Returns -1 if no corresponding div was found and the position
8148 * of the corresponding div in dst otherwise.
8150 static int find_div(struct isl_basic_map *dst,
8151 struct isl_basic_map *src, unsigned div)
8153 int i;
8155 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8157 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8158 for (i = div; i < dst->n_div; ++i)
8159 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8160 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8161 dst->n_div - div) == -1)
8162 return i;
8163 return -1;
8166 struct isl_basic_map *isl_basic_map_align_divs(
8167 struct isl_basic_map *dst, struct isl_basic_map *src)
8169 int i;
8170 unsigned total;
8172 if (!dst || !src)
8173 goto error;
8175 if (src->n_div == 0)
8176 return dst;
8178 for (i = 0; i < src->n_div; ++i)
8179 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
8181 src = isl_basic_map_order_divs(src);
8182 dst = isl_basic_map_cow(dst);
8183 if (!dst)
8184 return NULL;
8185 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
8186 src->n_div, 0, 2 * src->n_div);
8187 if (!dst)
8188 return NULL;
8189 total = isl_space_dim(src->dim, isl_dim_all);
8190 for (i = 0; i < src->n_div; ++i) {
8191 int j = find_div(dst, src, i);
8192 if (j < 0) {
8193 j = isl_basic_map_alloc_div(dst);
8194 if (j < 0)
8195 goto error;
8196 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8197 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8198 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8199 goto error;
8201 if (j != i)
8202 isl_basic_map_swap_div(dst, i, j);
8204 return dst;
8205 error:
8206 isl_basic_map_free(dst);
8207 return NULL;
8210 struct isl_basic_set *isl_basic_set_align_divs(
8211 struct isl_basic_set *dst, struct isl_basic_set *src)
8213 return (struct isl_basic_set *)isl_basic_map_align_divs(
8214 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8217 struct isl_map *isl_map_align_divs(struct isl_map *map)
8219 int i;
8221 if (!map)
8222 return NULL;
8223 if (map->n == 0)
8224 return map;
8225 map = isl_map_compute_divs(map);
8226 map = isl_map_cow(map);
8227 if (!map)
8228 return NULL;
8230 for (i = 1; i < map->n; ++i)
8231 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8232 for (i = 1; i < map->n; ++i) {
8233 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8234 if (!map->p[i])
8235 return isl_map_free(map);
8238 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8239 return map;
8242 struct isl_set *isl_set_align_divs(struct isl_set *set)
8244 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8247 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8248 __isl_take isl_map *map)
8250 if (!set || !map)
8251 goto error;
8252 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8253 map = isl_map_intersect_domain(map, set);
8254 set = isl_map_range(map);
8255 return set;
8256 error:
8257 isl_set_free(set);
8258 isl_map_free(map);
8259 return NULL;
8262 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8263 __isl_take isl_map *map)
8265 return isl_map_align_params_map_map_and(set, map, &set_apply);
8268 /* There is no need to cow as removing empty parts doesn't change
8269 * the meaning of the set.
8271 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8273 int i;
8275 if (!map)
8276 return NULL;
8278 for (i = map->n - 1; i >= 0; --i)
8279 remove_if_empty(map, i);
8281 return map;
8284 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8286 return (struct isl_set *)
8287 isl_map_remove_empty_parts((struct isl_map *)set);
8290 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8292 struct isl_basic_map *bmap;
8293 if (!map || map->n == 0)
8294 return NULL;
8295 bmap = map->p[map->n-1];
8296 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8297 return isl_basic_map_copy(bmap);
8300 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8302 return (struct isl_basic_set *)
8303 isl_map_copy_basic_map((struct isl_map *)set);
8306 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8307 __isl_keep isl_basic_map *bmap)
8309 int i;
8311 if (!map || !bmap)
8312 goto error;
8313 for (i = map->n-1; i >= 0; --i) {
8314 if (map->p[i] != bmap)
8315 continue;
8316 map = isl_map_cow(map);
8317 if (!map)
8318 goto error;
8319 isl_basic_map_free(map->p[i]);
8320 if (i != map->n-1) {
8321 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8322 map->p[i] = map->p[map->n-1];
8324 map->n--;
8325 return map;
8327 return map;
8328 error:
8329 isl_map_free(map);
8330 return NULL;
8333 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8334 struct isl_basic_set *bset)
8336 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8337 (struct isl_basic_map *)bset);
8340 /* Given two basic sets bset1 and bset2, compute the maximal difference
8341 * between the values of dimension pos in bset1 and those in bset2
8342 * for any common value of the parameters and dimensions preceding pos.
8344 static enum isl_lp_result basic_set_maximal_difference_at(
8345 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8346 int pos, isl_int *opt)
8348 isl_space *dims;
8349 struct isl_basic_map *bmap1 = NULL;
8350 struct isl_basic_map *bmap2 = NULL;
8351 struct isl_ctx *ctx;
8352 struct isl_vec *obj;
8353 unsigned total;
8354 unsigned nparam;
8355 unsigned dim1, dim2;
8356 enum isl_lp_result res;
8358 if (!bset1 || !bset2)
8359 return isl_lp_error;
8361 nparam = isl_basic_set_n_param(bset1);
8362 dim1 = isl_basic_set_n_dim(bset1);
8363 dim2 = isl_basic_set_n_dim(bset2);
8364 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8365 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8366 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8367 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8368 if (!bmap1 || !bmap2)
8369 goto error;
8370 bmap1 = isl_basic_map_cow(bmap1);
8371 bmap1 = isl_basic_map_extend(bmap1, nparam,
8372 pos, (dim1 - pos) + (dim2 - pos),
8373 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8374 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8375 if (!bmap1)
8376 goto error;
8377 total = isl_basic_map_total_dim(bmap1);
8378 ctx = bmap1->ctx;
8379 obj = isl_vec_alloc(ctx, 1 + total);
8380 if (!obj)
8381 goto error2;
8382 isl_seq_clr(obj->block.data, 1 + total);
8383 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8384 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8385 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8386 opt, NULL, NULL);
8387 isl_basic_map_free(bmap1);
8388 isl_vec_free(obj);
8389 return res;
8390 error:
8391 isl_basic_map_free(bmap2);
8392 error2:
8393 isl_basic_map_free(bmap1);
8394 return isl_lp_error;
8397 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8398 * for any common value of the parameters and dimensions preceding pos
8399 * in both basic sets, the values of dimension pos in bset1 are
8400 * smaller or larger than those in bset2.
8402 * Returns
8403 * 1 if bset1 follows bset2
8404 * -1 if bset1 precedes bset2
8405 * 0 if bset1 and bset2 are incomparable
8406 * -2 if some error occurred.
8408 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8409 struct isl_basic_set *bset2, int pos)
8411 isl_int opt;
8412 enum isl_lp_result res;
8413 int cmp;
8415 isl_int_init(opt);
8417 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8419 if (res == isl_lp_empty)
8420 cmp = 0;
8421 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8422 res == isl_lp_unbounded)
8423 cmp = 1;
8424 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8425 cmp = -1;
8426 else
8427 cmp = -2;
8429 isl_int_clear(opt);
8430 return cmp;
8433 /* Given two basic sets bset1 and bset2, check whether
8434 * for any common value of the parameters and dimensions preceding pos
8435 * there is a value of dimension pos in bset1 that is larger
8436 * than a value of the same dimension in bset2.
8438 * Return
8439 * 1 if there exists such a pair
8440 * 0 if there is no such pair, but there is a pair of equal values
8441 * -1 otherwise
8442 * -2 if some error occurred.
8444 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8445 __isl_keep isl_basic_set *bset2, int pos)
8447 isl_int opt;
8448 enum isl_lp_result res;
8449 int cmp;
8451 isl_int_init(opt);
8453 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8455 if (res == isl_lp_empty)
8456 cmp = -1;
8457 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8458 res == isl_lp_unbounded)
8459 cmp = 1;
8460 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8461 cmp = -1;
8462 else if (res == isl_lp_ok)
8463 cmp = 0;
8464 else
8465 cmp = -2;
8467 isl_int_clear(opt);
8468 return cmp;
8471 /* Given two sets set1 and set2, check whether
8472 * for any common value of the parameters and dimensions preceding pos
8473 * there is a value of dimension pos in set1 that is larger
8474 * than a value of the same dimension in set2.
8476 * Return
8477 * 1 if there exists such a pair
8478 * 0 if there is no such pair, but there is a pair of equal values
8479 * -1 otherwise
8480 * -2 if some error occurred.
8482 int isl_set_follows_at(__isl_keep isl_set *set1,
8483 __isl_keep isl_set *set2, int pos)
8485 int i, j;
8486 int follows = -1;
8488 if (!set1 || !set2)
8489 return -2;
8491 for (i = 0; i < set1->n; ++i)
8492 for (j = 0; j < set2->n; ++j) {
8493 int f;
8494 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8495 if (f == 1 || f == -2)
8496 return f;
8497 if (f > follows)
8498 follows = f;
8501 return follows;
8504 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8505 unsigned pos, isl_int *val)
8507 int i;
8508 int d;
8509 unsigned total;
8511 if (!bmap)
8512 return -1;
8513 total = isl_basic_map_total_dim(bmap);
8514 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8515 for (; d+1 > pos; --d)
8516 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8517 break;
8518 if (d != pos)
8519 continue;
8520 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8521 return 0;
8522 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8523 return 0;
8524 if (!isl_int_is_one(bmap->eq[i][1+d]))
8525 return 0;
8526 if (val)
8527 isl_int_neg(*val, bmap->eq[i][0]);
8528 return 1;
8530 return 0;
8533 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8534 unsigned pos, isl_int *val)
8536 int i;
8537 isl_int v;
8538 isl_int tmp;
8539 int fixed;
8541 if (!map)
8542 return -1;
8543 if (map->n == 0)
8544 return 0;
8545 if (map->n == 1)
8546 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8547 isl_int_init(v);
8548 isl_int_init(tmp);
8549 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8550 for (i = 1; fixed == 1 && i < map->n; ++i) {
8551 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8552 if (fixed == 1 && isl_int_ne(tmp, v))
8553 fixed = 0;
8555 if (val)
8556 isl_int_set(*val, v);
8557 isl_int_clear(tmp);
8558 isl_int_clear(v);
8559 return fixed;
8562 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8563 unsigned pos, isl_int *val)
8565 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8566 pos, val);
8569 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8570 isl_int *val)
8572 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8575 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8576 enum isl_dim_type type, unsigned pos, isl_int *val)
8578 if (pos >= isl_basic_map_dim(bmap, type))
8579 return -1;
8580 return isl_basic_map_plain_has_fixed_var(bmap,
8581 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8584 /* If "bmap" obviously lies on a hyperplane where the given dimension
8585 * has a fixed value, then return that value.
8586 * Otherwise return NaN.
8588 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8589 __isl_keep isl_basic_map *bmap,
8590 enum isl_dim_type type, unsigned pos)
8592 isl_ctx *ctx;
8593 isl_val *v;
8594 int fixed;
8596 if (!bmap)
8597 return NULL;
8598 ctx = isl_basic_map_get_ctx(bmap);
8599 v = isl_val_alloc(ctx);
8600 if (!v)
8601 return NULL;
8602 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8603 if (fixed < 0)
8604 return isl_val_free(v);
8605 if (fixed) {
8606 isl_int_set_si(v->d, 1);
8607 return v;
8609 isl_val_free(v);
8610 return isl_val_nan(ctx);
8613 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8614 enum isl_dim_type type, unsigned pos, isl_int *val)
8616 if (pos >= isl_map_dim(map, type))
8617 return -1;
8618 return isl_map_plain_has_fixed_var(map,
8619 map_offset(map, type) - 1 + pos, val);
8622 /* If "map" obviously lies on a hyperplane where the given dimension
8623 * has a fixed value, then return that value.
8624 * Otherwise return NaN.
8626 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8627 enum isl_dim_type type, unsigned pos)
8629 isl_ctx *ctx;
8630 isl_val *v;
8631 int fixed;
8633 if (!map)
8634 return NULL;
8635 ctx = isl_map_get_ctx(map);
8636 v = isl_val_alloc(ctx);
8637 if (!v)
8638 return NULL;
8639 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8640 if (fixed < 0)
8641 return isl_val_free(v);
8642 if (fixed) {
8643 isl_int_set_si(v->d, 1);
8644 return v;
8646 isl_val_free(v);
8647 return isl_val_nan(ctx);
8650 /* If "set" obviously lies on a hyperplane where the given dimension
8651 * has a fixed value, then return that value.
8652 * Otherwise return NaN.
8654 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8655 enum isl_dim_type type, unsigned pos)
8657 return isl_map_plain_get_val_if_fixed(set, type, pos);
8660 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8661 enum isl_dim_type type, unsigned pos, isl_int *val)
8663 return isl_map_plain_is_fixed(set, type, pos, val);
8666 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8667 enum isl_dim_type type, unsigned pos, isl_int *val)
8669 return isl_map_plain_is_fixed(map, type, pos, val);
8672 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8673 * then return this fixed value in *val.
8675 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8676 unsigned dim, isl_int *val)
8678 return isl_basic_set_plain_has_fixed_var(bset,
8679 isl_basic_set_n_param(bset) + dim, val);
8682 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8683 * then return this fixed value in *val.
8685 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8686 unsigned dim, isl_int *val)
8688 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8691 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8692 unsigned dim, isl_int *val)
8694 return isl_set_plain_dim_is_fixed(set, dim, val);
8697 /* Check if input variable in has fixed value and if so and if val is not NULL,
8698 * then return this fixed value in *val.
8700 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8701 unsigned in, isl_int *val)
8703 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8706 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8707 * and if val is not NULL, then return this lower bound in *val.
8709 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8710 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8712 int i, i_eq = -1, i_ineq = -1;
8713 isl_int *c;
8714 unsigned total;
8715 unsigned nparam;
8717 if (!bset)
8718 return -1;
8719 total = isl_basic_set_total_dim(bset);
8720 nparam = isl_basic_set_n_param(bset);
8721 for (i = 0; i < bset->n_eq; ++i) {
8722 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8723 continue;
8724 if (i_eq != -1)
8725 return 0;
8726 i_eq = i;
8728 for (i = 0; i < bset->n_ineq; ++i) {
8729 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8730 continue;
8731 if (i_eq != -1 || i_ineq != -1)
8732 return 0;
8733 i_ineq = i;
8735 if (i_eq == -1 && i_ineq == -1)
8736 return 0;
8737 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8738 /* The coefficient should always be one due to normalization. */
8739 if (!isl_int_is_one(c[1+nparam+dim]))
8740 return 0;
8741 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8742 return 0;
8743 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8744 total - nparam - dim - 1) != -1)
8745 return 0;
8746 if (val)
8747 isl_int_neg(*val, c[0]);
8748 return 1;
8751 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8752 unsigned dim, isl_int *val)
8754 int i;
8755 isl_int v;
8756 isl_int tmp;
8757 int fixed;
8759 if (!set)
8760 return -1;
8761 if (set->n == 0)
8762 return 0;
8763 if (set->n == 1)
8764 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8765 dim, val);
8766 isl_int_init(v);
8767 isl_int_init(tmp);
8768 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8769 dim, &v);
8770 for (i = 1; fixed == 1 && i < set->n; ++i) {
8771 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8772 dim, &tmp);
8773 if (fixed == 1 && isl_int_ne(tmp, v))
8774 fixed = 0;
8776 if (val)
8777 isl_int_set(*val, v);
8778 isl_int_clear(tmp);
8779 isl_int_clear(v);
8780 return fixed;
8783 struct constraint {
8784 unsigned size;
8785 isl_int *c;
8788 /* uset_gist depends on constraints without existentially quantified
8789 * variables sorting first.
8791 static int qsort_constraint_cmp(const void *p1, const void *p2)
8793 const struct constraint *c1 = (const struct constraint *)p1;
8794 const struct constraint *c2 = (const struct constraint *)p2;
8795 int l1, l2;
8796 unsigned size = isl_min(c1->size, c2->size);
8798 l1 = isl_seq_last_non_zero(c1->c + 1, size);
8799 l2 = isl_seq_last_non_zero(c2->c + 1, size);
8801 if (l1 != l2)
8802 return l1 - l2;
8804 return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8807 static struct isl_basic_map *isl_basic_map_sort_constraints(
8808 struct isl_basic_map *bmap)
8810 int i;
8811 struct constraint *c;
8812 unsigned total;
8814 if (!bmap)
8815 return NULL;
8816 if (bmap->n_ineq == 0)
8817 return bmap;
8818 total = isl_basic_map_total_dim(bmap);
8819 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8820 if (!c)
8821 goto error;
8822 for (i = 0; i < bmap->n_ineq; ++i) {
8823 c[i].size = total;
8824 c[i].c = bmap->ineq[i];
8826 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8827 for (i = 0; i < bmap->n_ineq; ++i)
8828 bmap->ineq[i] = c[i].c;
8829 free(c);
8830 return bmap;
8831 error:
8832 isl_basic_map_free(bmap);
8833 return NULL;
8836 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8837 __isl_take isl_basic_set *bset)
8839 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8840 (struct isl_basic_map *)bset);
8843 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8845 if (!bmap)
8846 return NULL;
8847 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8848 return bmap;
8849 bmap = isl_basic_map_remove_redundancies(bmap);
8850 bmap = isl_basic_map_sort_constraints(bmap);
8851 if (bmap)
8852 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8853 return bmap;
8856 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8858 return (struct isl_basic_set *)isl_basic_map_normalize(
8859 (struct isl_basic_map *)bset);
8862 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8863 const __isl_keep isl_basic_map *bmap2)
8865 int i, cmp;
8866 unsigned total;
8868 if (bmap1 == bmap2)
8869 return 0;
8870 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8871 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8872 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8873 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8874 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8875 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8876 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8877 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8878 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8879 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8880 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8881 return 0;
8882 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8883 return 1;
8884 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8885 return -1;
8886 if (bmap1->n_eq != bmap2->n_eq)
8887 return bmap1->n_eq - bmap2->n_eq;
8888 if (bmap1->n_ineq != bmap2->n_ineq)
8889 return bmap1->n_ineq - bmap2->n_ineq;
8890 if (bmap1->n_div != bmap2->n_div)
8891 return bmap1->n_div - bmap2->n_div;
8892 total = isl_basic_map_total_dim(bmap1);
8893 for (i = 0; i < bmap1->n_eq; ++i) {
8894 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8895 if (cmp)
8896 return cmp;
8898 for (i = 0; i < bmap1->n_ineq; ++i) {
8899 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8900 if (cmp)
8901 return cmp;
8903 for (i = 0; i < bmap1->n_div; ++i) {
8904 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8905 if (cmp)
8906 return cmp;
8908 return 0;
8911 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8912 const __isl_keep isl_basic_set *bset2)
8914 return isl_basic_map_plain_cmp(bset1, bset2);
8917 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8919 int i, cmp;
8921 if (set1 == set2)
8922 return 0;
8923 if (set1->n != set2->n)
8924 return set1->n - set2->n;
8926 for (i = 0; i < set1->n; ++i) {
8927 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8928 if (cmp)
8929 return cmp;
8932 return 0;
8935 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8936 __isl_keep isl_basic_map *bmap2)
8938 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8941 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8942 __isl_keep isl_basic_set *bset2)
8944 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8945 (isl_basic_map *)bset2);
8948 static int qsort_bmap_cmp(const void *p1, const void *p2)
8950 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8951 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8953 return isl_basic_map_plain_cmp(bmap1, bmap2);
8956 /* We normalize in place, but if anything goes wrong we need
8957 * to return NULL, so we need to make sure we don't change the
8958 * meaning of any possible other copies of map.
8960 struct isl_map *isl_map_normalize(struct isl_map *map)
8962 int i, j;
8963 struct isl_basic_map *bmap;
8965 if (!map)
8966 return NULL;
8967 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8968 return map;
8969 for (i = 0; i < map->n; ++i) {
8970 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8971 if (!bmap)
8972 goto error;
8973 isl_basic_map_free(map->p[i]);
8974 map->p[i] = bmap;
8976 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8977 ISL_F_SET(map, ISL_MAP_NORMALIZED);
8978 map = isl_map_remove_empty_parts(map);
8979 if (!map)
8980 return NULL;
8981 for (i = map->n - 1; i >= 1; --i) {
8982 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8983 continue;
8984 isl_basic_map_free(map->p[i-1]);
8985 for (j = i; j < map->n; ++j)
8986 map->p[j-1] = map->p[j];
8987 map->n--;
8989 return map;
8990 error:
8991 isl_map_free(map);
8992 return NULL;
8996 struct isl_set *isl_set_normalize(struct isl_set *set)
8998 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9001 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9003 int i;
9004 int equal;
9006 if (!map1 || !map2)
9007 return -1;
9009 if (map1 == map2)
9010 return 1;
9011 if (!isl_space_is_equal(map1->dim, map2->dim))
9012 return 0;
9014 map1 = isl_map_copy(map1);
9015 map2 = isl_map_copy(map2);
9016 map1 = isl_map_normalize(map1);
9017 map2 = isl_map_normalize(map2);
9018 if (!map1 || !map2)
9019 goto error;
9020 equal = map1->n == map2->n;
9021 for (i = 0; equal && i < map1->n; ++i) {
9022 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9023 if (equal < 0)
9024 goto error;
9026 isl_map_free(map1);
9027 isl_map_free(map2);
9028 return equal;
9029 error:
9030 isl_map_free(map1);
9031 isl_map_free(map2);
9032 return -1;
9035 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9037 return isl_map_plain_is_equal(map1, map2);
9040 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9042 return isl_map_plain_is_equal((struct isl_map *)set1,
9043 (struct isl_map *)set2);
9046 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9048 return isl_set_plain_is_equal(set1, set2);
9051 /* Return an interval that ranges from min to max (inclusive)
9053 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9054 isl_int min, isl_int max)
9056 int k;
9057 struct isl_basic_set *bset = NULL;
9059 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9060 if (!bset)
9061 goto error;
9063 k = isl_basic_set_alloc_inequality(bset);
9064 if (k < 0)
9065 goto error;
9066 isl_int_set_si(bset->ineq[k][1], 1);
9067 isl_int_neg(bset->ineq[k][0], min);
9069 k = isl_basic_set_alloc_inequality(bset);
9070 if (k < 0)
9071 goto error;
9072 isl_int_set_si(bset->ineq[k][1], -1);
9073 isl_int_set(bset->ineq[k][0], max);
9075 return bset;
9076 error:
9077 isl_basic_set_free(bset);
9078 return NULL;
9081 /* Return the Cartesian product of the basic sets in list (in the given order).
9083 __isl_give isl_basic_set *isl_basic_set_list_product(
9084 __isl_take struct isl_basic_set_list *list)
9086 int i;
9087 unsigned dim;
9088 unsigned nparam;
9089 unsigned extra;
9090 unsigned n_eq;
9091 unsigned n_ineq;
9092 struct isl_basic_set *product = NULL;
9094 if (!list)
9095 goto error;
9096 isl_assert(list->ctx, list->n > 0, goto error);
9097 isl_assert(list->ctx, list->p[0], goto error);
9098 nparam = isl_basic_set_n_param(list->p[0]);
9099 dim = isl_basic_set_n_dim(list->p[0]);
9100 extra = list->p[0]->n_div;
9101 n_eq = list->p[0]->n_eq;
9102 n_ineq = list->p[0]->n_ineq;
9103 for (i = 1; i < list->n; ++i) {
9104 isl_assert(list->ctx, list->p[i], goto error);
9105 isl_assert(list->ctx,
9106 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9107 dim += isl_basic_set_n_dim(list->p[i]);
9108 extra += list->p[i]->n_div;
9109 n_eq += list->p[i]->n_eq;
9110 n_ineq += list->p[i]->n_ineq;
9112 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9113 n_eq, n_ineq);
9114 if (!product)
9115 goto error;
9116 dim = 0;
9117 for (i = 0; i < list->n; ++i) {
9118 isl_basic_set_add_constraints(product,
9119 isl_basic_set_copy(list->p[i]), dim);
9120 dim += isl_basic_set_n_dim(list->p[i]);
9122 isl_basic_set_list_free(list);
9123 return product;
9124 error:
9125 isl_basic_set_free(product);
9126 isl_basic_set_list_free(list);
9127 return NULL;
9130 struct isl_basic_map *isl_basic_map_product(
9131 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9133 isl_space *dim_result = NULL;
9134 struct isl_basic_map *bmap;
9135 unsigned in1, in2, out1, out2, nparam, total, pos;
9136 struct isl_dim_map *dim_map1, *dim_map2;
9138 if (!bmap1 || !bmap2)
9139 goto error;
9141 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9142 bmap2->dim, isl_dim_param), goto error);
9143 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9144 isl_space_copy(bmap2->dim));
9146 in1 = isl_basic_map_n_in(bmap1);
9147 in2 = isl_basic_map_n_in(bmap2);
9148 out1 = isl_basic_map_n_out(bmap1);
9149 out2 = isl_basic_map_n_out(bmap2);
9150 nparam = isl_basic_map_n_param(bmap1);
9152 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9153 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9154 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9155 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9156 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9157 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9158 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9159 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9160 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9161 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9162 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9164 bmap = isl_basic_map_alloc_space(dim_result,
9165 bmap1->n_div + bmap2->n_div,
9166 bmap1->n_eq + bmap2->n_eq,
9167 bmap1->n_ineq + bmap2->n_ineq);
9168 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9169 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9170 bmap = isl_basic_map_simplify(bmap);
9171 return isl_basic_map_finalize(bmap);
9172 error:
9173 isl_basic_map_free(bmap1);
9174 isl_basic_map_free(bmap2);
9175 return NULL;
9178 __isl_give isl_basic_map *isl_basic_map_flat_product(
9179 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9181 isl_basic_map *prod;
9183 prod = isl_basic_map_product(bmap1, bmap2);
9184 prod = isl_basic_map_flatten(prod);
9185 return prod;
9188 __isl_give isl_basic_set *isl_basic_set_flat_product(
9189 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9191 return isl_basic_map_flat_range_product(bset1, bset2);
9194 __isl_give isl_basic_map *isl_basic_map_domain_product(
9195 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9197 isl_space *space_result = NULL;
9198 isl_basic_map *bmap;
9199 unsigned in1, in2, out, nparam, total, pos;
9200 struct isl_dim_map *dim_map1, *dim_map2;
9202 if (!bmap1 || !bmap2)
9203 goto error;
9205 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9206 isl_space_copy(bmap2->dim));
9208 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9209 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9210 out = isl_basic_map_dim(bmap1, isl_dim_out);
9211 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9213 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9214 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9215 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9216 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9217 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9218 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9219 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9220 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9221 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9222 isl_dim_map_div(dim_map1, bmap1, pos += out);
9223 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9225 bmap = isl_basic_map_alloc_space(space_result,
9226 bmap1->n_div + bmap2->n_div,
9227 bmap1->n_eq + bmap2->n_eq,
9228 bmap1->n_ineq + bmap2->n_ineq);
9229 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9230 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9231 bmap = isl_basic_map_simplify(bmap);
9232 return isl_basic_map_finalize(bmap);
9233 error:
9234 isl_basic_map_free(bmap1);
9235 isl_basic_map_free(bmap2);
9236 return NULL;
9239 __isl_give isl_basic_map *isl_basic_map_range_product(
9240 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9242 isl_space *dim_result = NULL;
9243 isl_basic_map *bmap;
9244 unsigned in, out1, out2, nparam, total, pos;
9245 struct isl_dim_map *dim_map1, *dim_map2;
9247 if (!bmap1 || !bmap2)
9248 goto error;
9250 if (!isl_space_match(bmap1->dim, isl_dim_param,
9251 bmap2->dim, isl_dim_param))
9252 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9253 "parameters don't match", goto error);
9255 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9256 isl_space_copy(bmap2->dim));
9258 in = isl_basic_map_dim(bmap1, isl_dim_in);
9259 out1 = isl_basic_map_n_out(bmap1);
9260 out2 = isl_basic_map_n_out(bmap2);
9261 nparam = isl_basic_map_n_param(bmap1);
9263 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9264 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9265 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9266 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9267 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9268 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9269 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9270 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9271 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9272 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9273 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9275 bmap = isl_basic_map_alloc_space(dim_result,
9276 bmap1->n_div + bmap2->n_div,
9277 bmap1->n_eq + bmap2->n_eq,
9278 bmap1->n_ineq + bmap2->n_ineq);
9279 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9280 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9281 bmap = isl_basic_map_simplify(bmap);
9282 return isl_basic_map_finalize(bmap);
9283 error:
9284 isl_basic_map_free(bmap1);
9285 isl_basic_map_free(bmap2);
9286 return NULL;
9289 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9290 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9292 isl_basic_map *prod;
9294 prod = isl_basic_map_range_product(bmap1, bmap2);
9295 prod = isl_basic_map_flatten_range(prod);
9296 return prod;
9299 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9300 __isl_take isl_map *map2,
9301 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
9302 __isl_take isl_space *right),
9303 __isl_give isl_basic_map *(*basic_map_product)(
9304 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
9306 unsigned flags = 0;
9307 struct isl_map *result;
9308 int i, j;
9310 if (!map1 || !map2)
9311 goto error;
9313 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9314 map2->dim, isl_dim_param), goto error);
9316 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9317 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9318 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9320 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
9321 isl_space_copy(map2->dim)),
9322 map1->n * map2->n, flags);
9323 if (!result)
9324 goto error;
9325 for (i = 0; i < map1->n; ++i)
9326 for (j = 0; j < map2->n; ++j) {
9327 struct isl_basic_map *part;
9328 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9329 isl_basic_map_copy(map2->p[j]));
9330 if (isl_basic_map_is_empty(part))
9331 isl_basic_map_free(part);
9332 else
9333 result = isl_map_add_basic_map(result, part);
9334 if (!result)
9335 goto error;
9337 isl_map_free(map1);
9338 isl_map_free(map2);
9339 return result;
9340 error:
9341 isl_map_free(map1);
9342 isl_map_free(map2);
9343 return NULL;
9346 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9348 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9349 __isl_take isl_map *map2)
9351 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
9354 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9355 __isl_take isl_map *map2)
9357 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9360 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9362 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9363 __isl_take isl_map *map2)
9365 isl_map *prod;
9367 prod = isl_map_product(map1, map2);
9368 prod = isl_map_flatten(prod);
9369 return prod;
9372 /* Given two set A and B, construct its Cartesian product A x B.
9374 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9376 return isl_map_range_product(set1, set2);
9379 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9380 __isl_take isl_set *set2)
9382 return isl_map_flat_range_product(set1, set2);
9385 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9387 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9388 __isl_take isl_map *map2)
9390 return map_product(map1, map2, &isl_space_domain_product,
9391 &isl_basic_map_domain_product);
9394 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9396 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9397 __isl_take isl_map *map2)
9399 return map_product(map1, map2, &isl_space_range_product,
9400 &isl_basic_map_range_product);
9403 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9404 __isl_take isl_map *map2)
9406 return isl_map_align_params_map_map_and(map1, map2,
9407 &map_domain_product_aligned);
9410 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9411 __isl_take isl_map *map2)
9413 return isl_map_align_params_map_map_and(map1, map2,
9414 &map_range_product_aligned);
9417 /* Given a map A -> [B -> C], extract the map A -> B.
9419 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9421 isl_space *space;
9422 int total, keep;
9424 if (!map)
9425 return NULL;
9426 if (!isl_space_range_is_wrapping(map->dim))
9427 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9428 "range is not a product", return isl_map_free(map));
9430 space = isl_map_get_space(map);
9431 total = isl_space_dim(space, isl_dim_out);
9432 space = isl_space_range_factor_domain(space);
9433 keep = isl_space_dim(space, isl_dim_out);
9434 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9435 map = isl_map_reset_space(map, space);
9437 return map;
9440 /* Given a map A -> [B -> C], extract the map A -> C.
9442 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9444 isl_space *space;
9445 int total, keep;
9447 if (!map)
9448 return NULL;
9449 if (!isl_space_range_is_wrapping(map->dim))
9450 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9451 "range is not a product", return isl_map_free(map));
9453 space = isl_map_get_space(map);
9454 total = isl_space_dim(space, isl_dim_out);
9455 space = isl_space_range_factor_range(space);
9456 keep = isl_space_dim(space, isl_dim_out);
9457 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9458 map = isl_map_reset_space(map, space);
9460 return map;
9463 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9465 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9466 __isl_take isl_map *map2)
9468 isl_map *prod;
9470 prod = isl_map_domain_product(map1, map2);
9471 prod = isl_map_flatten_domain(prod);
9472 return prod;
9475 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9477 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9478 __isl_take isl_map *map2)
9480 isl_map *prod;
9482 prod = isl_map_range_product(map1, map2);
9483 prod = isl_map_flatten_range(prod);
9484 return prod;
9487 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9489 int i;
9490 uint32_t hash = isl_hash_init();
9491 unsigned total;
9493 if (!bmap)
9494 return 0;
9495 bmap = isl_basic_map_copy(bmap);
9496 bmap = isl_basic_map_normalize(bmap);
9497 if (!bmap)
9498 return 0;
9499 total = isl_basic_map_total_dim(bmap);
9500 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9501 for (i = 0; i < bmap->n_eq; ++i) {
9502 uint32_t c_hash;
9503 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9504 isl_hash_hash(hash, c_hash);
9506 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9507 for (i = 0; i < bmap->n_ineq; ++i) {
9508 uint32_t c_hash;
9509 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9510 isl_hash_hash(hash, c_hash);
9512 isl_hash_byte(hash, bmap->n_div & 0xFF);
9513 for (i = 0; i < bmap->n_div; ++i) {
9514 uint32_t c_hash;
9515 if (isl_int_is_zero(bmap->div[i][0]))
9516 continue;
9517 isl_hash_byte(hash, i & 0xFF);
9518 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9519 isl_hash_hash(hash, c_hash);
9521 isl_basic_map_free(bmap);
9522 return hash;
9525 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9527 return isl_basic_map_get_hash((isl_basic_map *)bset);
9530 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9532 int i;
9533 uint32_t hash;
9535 if (!map)
9536 return 0;
9537 map = isl_map_copy(map);
9538 map = isl_map_normalize(map);
9539 if (!map)
9540 return 0;
9542 hash = isl_hash_init();
9543 for (i = 0; i < map->n; ++i) {
9544 uint32_t bmap_hash;
9545 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9546 isl_hash_hash(hash, bmap_hash);
9549 isl_map_free(map);
9551 return hash;
9554 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9556 return isl_map_get_hash((isl_map *)set);
9559 /* Check if the value for dimension dim is completely determined
9560 * by the values of the other parameters and variables.
9561 * That is, check if dimension dim is involved in an equality.
9563 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9565 int i;
9566 unsigned nparam;
9568 if (!bset)
9569 return -1;
9570 nparam = isl_basic_set_n_param(bset);
9571 for (i = 0; i < bset->n_eq; ++i)
9572 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9573 return 1;
9574 return 0;
9577 /* Check if the value for dimension dim is completely determined
9578 * by the values of the other parameters and variables.
9579 * That is, check if dimension dim is involved in an equality
9580 * for each of the subsets.
9582 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9584 int i;
9586 if (!set)
9587 return -1;
9588 for (i = 0; i < set->n; ++i) {
9589 int unique;
9590 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9591 if (unique != 1)
9592 return unique;
9594 return 1;
9597 int isl_set_n_basic_set(__isl_keep isl_set *set)
9599 return set ? set->n : 0;
9602 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9603 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9605 int i;
9607 if (!map)
9608 return -1;
9610 for (i = 0; i < map->n; ++i)
9611 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9612 return -1;
9614 return 0;
9617 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9618 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9620 int i;
9622 if (!set)
9623 return -1;
9625 for (i = 0; i < set->n; ++i)
9626 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9627 return -1;
9629 return 0;
9632 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9634 isl_space *dim;
9636 if (!bset)
9637 return NULL;
9639 bset = isl_basic_set_cow(bset);
9640 if (!bset)
9641 return NULL;
9643 dim = isl_basic_set_get_space(bset);
9644 dim = isl_space_lift(dim, bset->n_div);
9645 if (!dim)
9646 goto error;
9647 isl_space_free(bset->dim);
9648 bset->dim = dim;
9649 bset->extra -= bset->n_div;
9650 bset->n_div = 0;
9652 bset = isl_basic_set_finalize(bset);
9654 return bset;
9655 error:
9656 isl_basic_set_free(bset);
9657 return NULL;
9660 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9662 int i;
9663 isl_space *dim;
9664 unsigned n_div;
9666 set = isl_set_align_divs(set);
9668 if (!set)
9669 return NULL;
9671 set = isl_set_cow(set);
9672 if (!set)
9673 return NULL;
9675 n_div = set->p[0]->n_div;
9676 dim = isl_set_get_space(set);
9677 dim = isl_space_lift(dim, n_div);
9678 if (!dim)
9679 goto error;
9680 isl_space_free(set->dim);
9681 set->dim = dim;
9683 for (i = 0; i < set->n; ++i) {
9684 set->p[i] = isl_basic_set_lift(set->p[i]);
9685 if (!set->p[i])
9686 goto error;
9689 return set;
9690 error:
9691 isl_set_free(set);
9692 return NULL;
9695 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9697 isl_space *dim;
9698 struct isl_basic_map *bmap;
9699 unsigned n_set;
9700 unsigned n_div;
9701 unsigned n_param;
9702 unsigned total;
9703 int i, k, l;
9705 set = isl_set_align_divs(set);
9707 if (!set)
9708 return NULL;
9710 dim = isl_set_get_space(set);
9711 if (set->n == 0 || set->p[0]->n_div == 0) {
9712 isl_set_free(set);
9713 return isl_map_identity(isl_space_map_from_set(dim));
9716 n_div = set->p[0]->n_div;
9717 dim = isl_space_map_from_set(dim);
9718 n_param = isl_space_dim(dim, isl_dim_param);
9719 n_set = isl_space_dim(dim, isl_dim_in);
9720 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9721 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9722 for (i = 0; i < n_set; ++i)
9723 bmap = var_equal(bmap, i);
9725 total = n_param + n_set + n_set + n_div;
9726 for (i = 0; i < n_div; ++i) {
9727 k = isl_basic_map_alloc_inequality(bmap);
9728 if (k < 0)
9729 goto error;
9730 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9731 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9732 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9733 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9734 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9735 set->p[0]->div[i][0]);
9737 l = isl_basic_map_alloc_inequality(bmap);
9738 if (l < 0)
9739 goto error;
9740 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9741 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9742 set->p[0]->div[i][0]);
9743 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9746 isl_set_free(set);
9747 bmap = isl_basic_map_simplify(bmap);
9748 bmap = isl_basic_map_finalize(bmap);
9749 return isl_map_from_basic_map(bmap);
9750 error:
9751 isl_set_free(set);
9752 isl_basic_map_free(bmap);
9753 return NULL;
9756 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9758 unsigned dim;
9759 int size = 0;
9761 if (!bset)
9762 return -1;
9764 dim = isl_basic_set_total_dim(bset);
9765 size += bset->n_eq * (1 + dim);
9766 size += bset->n_ineq * (1 + dim);
9767 size += bset->n_div * (2 + dim);
9769 return size;
9772 int isl_set_size(__isl_keep isl_set *set)
9774 int i;
9775 int size = 0;
9777 if (!set)
9778 return -1;
9780 for (i = 0; i < set->n; ++i)
9781 size += isl_basic_set_size(set->p[i]);
9783 return size;
9786 /* Check if there is any lower bound (if lower == 0) and/or upper
9787 * bound (if upper == 0) on the specified dim.
9789 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9790 enum isl_dim_type type, unsigned pos, int lower, int upper)
9792 int i;
9794 if (!bmap)
9795 return -1;
9797 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9799 pos += isl_basic_map_offset(bmap, type);
9801 for (i = 0; i < bmap->n_div; ++i) {
9802 if (isl_int_is_zero(bmap->div[i][0]))
9803 continue;
9804 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9805 return 1;
9808 for (i = 0; i < bmap->n_eq; ++i)
9809 if (!isl_int_is_zero(bmap->eq[i][pos]))
9810 return 1;
9812 for (i = 0; i < bmap->n_ineq; ++i) {
9813 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
9814 if (sgn > 0)
9815 lower = 1;
9816 if (sgn < 0)
9817 upper = 1;
9820 return lower && upper;
9823 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9824 enum isl_dim_type type, unsigned pos)
9826 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9829 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9830 enum isl_dim_type type, unsigned pos)
9832 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9835 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9836 enum isl_dim_type type, unsigned pos)
9838 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9841 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9842 enum isl_dim_type type, unsigned pos)
9844 int i;
9846 if (!map)
9847 return -1;
9849 for (i = 0; i < map->n; ++i) {
9850 int bounded;
9851 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9852 if (bounded < 0 || !bounded)
9853 return bounded;
9856 return 1;
9859 /* Return 1 if the specified dim is involved in both an upper bound
9860 * and a lower bound.
9862 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9863 enum isl_dim_type type, unsigned pos)
9865 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9868 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9870 static int has_any_bound(__isl_keep isl_map *map,
9871 enum isl_dim_type type, unsigned pos,
9872 int (*fn)(__isl_keep isl_basic_map *bmap,
9873 enum isl_dim_type type, unsigned pos))
9875 int i;
9877 if (!map)
9878 return -1;
9880 for (i = 0; i < map->n; ++i) {
9881 int bounded;
9882 bounded = fn(map->p[i], type, pos);
9883 if (bounded < 0 || bounded)
9884 return bounded;
9887 return 0;
9890 /* Return 1 if the specified dim is involved in any lower bound.
9892 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
9893 enum isl_dim_type type, unsigned pos)
9895 return has_any_bound(set, type, pos,
9896 &isl_basic_map_dim_has_lower_bound);
9899 /* Return 1 if the specified dim is involved in any upper bound.
9901 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
9902 enum isl_dim_type type, unsigned pos)
9904 return has_any_bound(set, type, pos,
9905 &isl_basic_map_dim_has_upper_bound);
9908 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9910 static int has_bound(__isl_keep isl_map *map,
9911 enum isl_dim_type type, unsigned pos,
9912 int (*fn)(__isl_keep isl_basic_map *bmap,
9913 enum isl_dim_type type, unsigned pos))
9915 int i;
9917 if (!map)
9918 return -1;
9920 for (i = 0; i < map->n; ++i) {
9921 int bounded;
9922 bounded = fn(map->p[i], type, pos);
9923 if (bounded < 0 || !bounded)
9924 return bounded;
9927 return 1;
9930 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9932 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9933 enum isl_dim_type type, unsigned pos)
9935 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9938 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9940 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9941 enum isl_dim_type type, unsigned pos)
9943 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9946 /* For each of the "n" variables starting at "first", determine
9947 * the sign of the variable and put the results in the first "n"
9948 * elements of the array "signs".
9949 * Sign
9950 * 1 means that the variable is non-negative
9951 * -1 means that the variable is non-positive
9952 * 0 means the variable attains both positive and negative values.
9954 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9955 unsigned first, unsigned n, int *signs)
9957 isl_vec *bound = NULL;
9958 struct isl_tab *tab = NULL;
9959 struct isl_tab_undo *snap;
9960 int i;
9962 if (!bset || !signs)
9963 return -1;
9965 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9966 tab = isl_tab_from_basic_set(bset, 0);
9967 if (!bound || !tab)
9968 goto error;
9970 isl_seq_clr(bound->el, bound->size);
9971 isl_int_set_si(bound->el[0], -1);
9973 snap = isl_tab_snap(tab);
9974 for (i = 0; i < n; ++i) {
9975 int empty;
9977 isl_int_set_si(bound->el[1 + first + i], -1);
9978 if (isl_tab_add_ineq(tab, bound->el) < 0)
9979 goto error;
9980 empty = tab->empty;
9981 isl_int_set_si(bound->el[1 + first + i], 0);
9982 if (isl_tab_rollback(tab, snap) < 0)
9983 goto error;
9985 if (empty) {
9986 signs[i] = 1;
9987 continue;
9990 isl_int_set_si(bound->el[1 + first + i], 1);
9991 if (isl_tab_add_ineq(tab, bound->el) < 0)
9992 goto error;
9993 empty = tab->empty;
9994 isl_int_set_si(bound->el[1 + first + i], 0);
9995 if (isl_tab_rollback(tab, snap) < 0)
9996 goto error;
9998 signs[i] = empty ? -1 : 0;
10001 isl_tab_free(tab);
10002 isl_vec_free(bound);
10003 return 0;
10004 error:
10005 isl_tab_free(tab);
10006 isl_vec_free(bound);
10007 return -1;
10010 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10011 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10013 if (!bset || !signs)
10014 return -1;
10015 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10016 return -1);
10018 first += pos(bset->dim, type) - 1;
10019 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10022 /* Check if the given basic map is obviously single-valued.
10023 * In particular, for each output dimension, check that there is
10024 * an equality that defines the output dimension in terms of
10025 * earlier dimensions.
10027 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10029 int i, j;
10030 unsigned total;
10031 unsigned n_out;
10032 unsigned o_out;
10034 if (!bmap)
10035 return -1;
10037 total = 1 + isl_basic_map_total_dim(bmap);
10038 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10039 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10041 for (i = 0; i < n_out; ++i) {
10042 for (j = 0; j < bmap->n_eq; ++j) {
10043 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
10044 continue;
10045 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
10046 total - (o_out + i + 1)) == -1)
10047 break;
10049 if (j >= bmap->n_eq)
10050 return 0;
10053 return 1;
10056 /* Check if the given basic map is single-valued.
10057 * We simply compute
10059 * M \circ M^-1
10061 * and check if the result is a subset of the identity mapping.
10063 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10065 isl_space *space;
10066 isl_basic_map *test;
10067 isl_basic_map *id;
10068 int sv;
10070 sv = isl_basic_map_plain_is_single_valued(bmap);
10071 if (sv < 0 || sv)
10072 return sv;
10074 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10075 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10077 space = isl_basic_map_get_space(bmap);
10078 space = isl_space_map_from_set(isl_space_range(space));
10079 id = isl_basic_map_identity(space);
10081 sv = isl_basic_map_is_subset(test, id);
10083 isl_basic_map_free(test);
10084 isl_basic_map_free(id);
10086 return sv;
10089 /* Check if the given map is obviously single-valued.
10091 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10093 if (!map)
10094 return -1;
10095 if (map->n == 0)
10096 return 1;
10097 if (map->n >= 2)
10098 return 0;
10100 return isl_basic_map_plain_is_single_valued(map->p[0]);
10103 /* Check if the given map is single-valued.
10104 * We simply compute
10106 * M \circ M^-1
10108 * and check if the result is a subset of the identity mapping.
10110 int isl_map_is_single_valued(__isl_keep isl_map *map)
10112 isl_space *dim;
10113 isl_map *test;
10114 isl_map *id;
10115 int sv;
10117 sv = isl_map_plain_is_single_valued(map);
10118 if (sv < 0 || sv)
10119 return sv;
10121 test = isl_map_reverse(isl_map_copy(map));
10122 test = isl_map_apply_range(test, isl_map_copy(map));
10124 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10125 id = isl_map_identity(dim);
10127 sv = isl_map_is_subset(test, id);
10129 isl_map_free(test);
10130 isl_map_free(id);
10132 return sv;
10135 int isl_map_is_injective(__isl_keep isl_map *map)
10137 int in;
10139 map = isl_map_copy(map);
10140 map = isl_map_reverse(map);
10141 in = isl_map_is_single_valued(map);
10142 isl_map_free(map);
10144 return in;
10147 /* Check if the given map is obviously injective.
10149 int isl_map_plain_is_injective(__isl_keep isl_map *map)
10151 int in;
10153 map = isl_map_copy(map);
10154 map = isl_map_reverse(map);
10155 in = isl_map_plain_is_single_valued(map);
10156 isl_map_free(map);
10158 return in;
10161 int isl_map_is_bijective(__isl_keep isl_map *map)
10163 int sv;
10165 sv = isl_map_is_single_valued(map);
10166 if (sv < 0 || !sv)
10167 return sv;
10169 return isl_map_is_injective(map);
10172 int isl_set_is_singleton(__isl_keep isl_set *set)
10174 return isl_map_is_single_valued((isl_map *)set);
10177 int isl_map_is_translation(__isl_keep isl_map *map)
10179 int ok;
10180 isl_set *delta;
10182 delta = isl_map_deltas(isl_map_copy(map));
10183 ok = isl_set_is_singleton(delta);
10184 isl_set_free(delta);
10186 return ok;
10189 static int unique(isl_int *p, unsigned pos, unsigned len)
10191 if (isl_seq_first_non_zero(p, pos) != -1)
10192 return 0;
10193 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10194 return 0;
10195 return 1;
10198 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10200 int i, j;
10201 unsigned nvar;
10202 unsigned ovar;
10204 if (!bset)
10205 return -1;
10207 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10208 return 0;
10210 nvar = isl_basic_set_dim(bset, isl_dim_set);
10211 ovar = isl_space_offset(bset->dim, isl_dim_set);
10212 for (j = 0; j < nvar; ++j) {
10213 int lower = 0, upper = 0;
10214 for (i = 0; i < bset->n_eq; ++i) {
10215 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10216 continue;
10217 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10218 return 0;
10219 break;
10221 if (i < bset->n_eq)
10222 continue;
10223 for (i = 0; i < bset->n_ineq; ++i) {
10224 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10225 continue;
10226 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10227 return 0;
10228 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10229 lower = 1;
10230 else
10231 upper = 1;
10233 if (!lower || !upper)
10234 return 0;
10237 return 1;
10240 int isl_set_is_box(__isl_keep isl_set *set)
10242 if (!set)
10243 return -1;
10244 if (set->n != 1)
10245 return 0;
10247 return isl_basic_set_is_box(set->p[0]);
10250 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10252 if (!bset)
10253 return -1;
10255 return isl_space_is_wrapping(bset->dim);
10258 int isl_set_is_wrapping(__isl_keep isl_set *set)
10260 if (!set)
10261 return -1;
10263 return isl_space_is_wrapping(set->dim);
10266 /* Is the domain of "map" a wrapped relation?
10268 int isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10270 if (!map)
10271 return -1;
10273 return isl_space_domain_is_wrapping(map->dim);
10276 /* Is the range of "map" a wrapped relation?
10278 int isl_map_range_is_wrapping(__isl_keep isl_map *map)
10280 if (!map)
10281 return -1;
10283 return isl_space_range_is_wrapping(map->dim);
10286 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10288 bmap = isl_basic_map_cow(bmap);
10289 if (!bmap)
10290 return NULL;
10292 bmap->dim = isl_space_wrap(bmap->dim);
10293 if (!bmap->dim)
10294 goto error;
10296 bmap = isl_basic_map_finalize(bmap);
10298 return (isl_basic_set *)bmap;
10299 error:
10300 isl_basic_map_free(bmap);
10301 return NULL;
10304 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10306 int i;
10308 map = isl_map_cow(map);
10309 if (!map)
10310 return NULL;
10312 for (i = 0; i < map->n; ++i) {
10313 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10314 if (!map->p[i])
10315 goto error;
10317 map->dim = isl_space_wrap(map->dim);
10318 if (!map->dim)
10319 goto error;
10321 return (isl_set *)map;
10322 error:
10323 isl_map_free(map);
10324 return NULL;
10327 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10329 bset = isl_basic_set_cow(bset);
10330 if (!bset)
10331 return NULL;
10333 bset->dim = isl_space_unwrap(bset->dim);
10334 if (!bset->dim)
10335 goto error;
10337 bset = isl_basic_set_finalize(bset);
10339 return (isl_basic_map *)bset;
10340 error:
10341 isl_basic_set_free(bset);
10342 return NULL;
10345 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10347 int i;
10349 if (!set)
10350 return NULL;
10352 if (!isl_set_is_wrapping(set))
10353 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10354 goto error);
10356 set = isl_set_cow(set);
10357 if (!set)
10358 return NULL;
10360 for (i = 0; i < set->n; ++i) {
10361 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10362 if (!set->p[i])
10363 goto error;
10366 set->dim = isl_space_unwrap(set->dim);
10367 if (!set->dim)
10368 goto error;
10370 return (isl_map *)set;
10371 error:
10372 isl_set_free(set);
10373 return NULL;
10376 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10377 enum isl_dim_type type)
10379 if (!bmap)
10380 return NULL;
10382 if (!isl_space_is_named_or_nested(bmap->dim, type))
10383 return bmap;
10385 bmap = isl_basic_map_cow(bmap);
10386 if (!bmap)
10387 return NULL;
10389 bmap->dim = isl_space_reset(bmap->dim, type);
10390 if (!bmap->dim)
10391 goto error;
10393 bmap = isl_basic_map_finalize(bmap);
10395 return bmap;
10396 error:
10397 isl_basic_map_free(bmap);
10398 return NULL;
10401 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10402 enum isl_dim_type type)
10404 int i;
10406 if (!map)
10407 return NULL;
10409 if (!isl_space_is_named_or_nested(map->dim, type))
10410 return map;
10412 map = isl_map_cow(map);
10413 if (!map)
10414 return NULL;
10416 for (i = 0; i < map->n; ++i) {
10417 map->p[i] = isl_basic_map_reset(map->p[i], type);
10418 if (!map->p[i])
10419 goto error;
10421 map->dim = isl_space_reset(map->dim, type);
10422 if (!map->dim)
10423 goto error;
10425 return map;
10426 error:
10427 isl_map_free(map);
10428 return NULL;
10431 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10433 if (!bmap)
10434 return NULL;
10436 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10437 return bmap;
10439 bmap = isl_basic_map_cow(bmap);
10440 if (!bmap)
10441 return NULL;
10443 bmap->dim = isl_space_flatten(bmap->dim);
10444 if (!bmap->dim)
10445 goto error;
10447 bmap = isl_basic_map_finalize(bmap);
10449 return bmap;
10450 error:
10451 isl_basic_map_free(bmap);
10452 return NULL;
10455 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10457 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10460 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10461 __isl_take isl_basic_map *bmap)
10463 if (!bmap)
10464 return NULL;
10466 if (!bmap->dim->nested[0])
10467 return bmap;
10469 bmap = isl_basic_map_cow(bmap);
10470 if (!bmap)
10471 return NULL;
10473 bmap->dim = isl_space_flatten_domain(bmap->dim);
10474 if (!bmap->dim)
10475 goto error;
10477 bmap = isl_basic_map_finalize(bmap);
10479 return bmap;
10480 error:
10481 isl_basic_map_free(bmap);
10482 return NULL;
10485 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10486 __isl_take isl_basic_map *bmap)
10488 if (!bmap)
10489 return NULL;
10491 if (!bmap->dim->nested[1])
10492 return bmap;
10494 bmap = isl_basic_map_cow(bmap);
10495 if (!bmap)
10496 return NULL;
10498 bmap->dim = isl_space_flatten_range(bmap->dim);
10499 if (!bmap->dim)
10500 goto error;
10502 bmap = isl_basic_map_finalize(bmap);
10504 return bmap;
10505 error:
10506 isl_basic_map_free(bmap);
10507 return NULL;
10510 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10512 int i;
10514 if (!map)
10515 return NULL;
10517 if (!map->dim->nested[0] && !map->dim->nested[1])
10518 return map;
10520 map = isl_map_cow(map);
10521 if (!map)
10522 return NULL;
10524 for (i = 0; i < map->n; ++i) {
10525 map->p[i] = isl_basic_map_flatten(map->p[i]);
10526 if (!map->p[i])
10527 goto error;
10529 map->dim = isl_space_flatten(map->dim);
10530 if (!map->dim)
10531 goto error;
10533 return map;
10534 error:
10535 isl_map_free(map);
10536 return NULL;
10539 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10541 return (isl_set *)isl_map_flatten((isl_map *)set);
10544 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10546 isl_space *dim, *flat_dim;
10547 isl_map *map;
10549 dim = isl_set_get_space(set);
10550 flat_dim = isl_space_flatten(isl_space_copy(dim));
10551 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10552 map = isl_map_intersect_domain(map, set);
10554 return map;
10557 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10559 int i;
10561 if (!map)
10562 return NULL;
10564 if (!map->dim->nested[0])
10565 return map;
10567 map = isl_map_cow(map);
10568 if (!map)
10569 return NULL;
10571 for (i = 0; i < map->n; ++i) {
10572 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10573 if (!map->p[i])
10574 goto error;
10576 map->dim = isl_space_flatten_domain(map->dim);
10577 if (!map->dim)
10578 goto error;
10580 return map;
10581 error:
10582 isl_map_free(map);
10583 return NULL;
10586 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10588 int i;
10590 if (!map)
10591 return NULL;
10593 if (!map->dim->nested[1])
10594 return map;
10596 map = isl_map_cow(map);
10597 if (!map)
10598 return NULL;
10600 for (i = 0; i < map->n; ++i) {
10601 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10602 if (!map->p[i])
10603 goto error;
10605 map->dim = isl_space_flatten_range(map->dim);
10606 if (!map->dim)
10607 goto error;
10609 return map;
10610 error:
10611 isl_map_free(map);
10612 return NULL;
10615 /* Reorder the dimensions of "bmap" according to the given dim_map
10616 * and set the dimension specification to "dim".
10618 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10619 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10621 isl_basic_map *res;
10622 unsigned flags;
10624 bmap = isl_basic_map_cow(bmap);
10625 if (!bmap || !dim || !dim_map)
10626 goto error;
10628 flags = bmap->flags;
10629 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10630 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10631 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10632 res = isl_basic_map_alloc_space(dim,
10633 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10634 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10635 if (res)
10636 res->flags = flags;
10637 res = isl_basic_map_finalize(res);
10638 return res;
10639 error:
10640 free(dim_map);
10641 isl_basic_map_free(bmap);
10642 isl_space_free(dim);
10643 return NULL;
10646 /* Reorder the dimensions of "map" according to given reordering.
10648 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10649 __isl_take isl_reordering *r)
10651 int i;
10652 struct isl_dim_map *dim_map;
10654 map = isl_map_cow(map);
10655 dim_map = isl_dim_map_from_reordering(r);
10656 if (!map || !r || !dim_map)
10657 goto error;
10659 for (i = 0; i < map->n; ++i) {
10660 struct isl_dim_map *dim_map_i;
10662 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10664 map->p[i] = isl_basic_map_realign(map->p[i],
10665 isl_space_copy(r->dim), dim_map_i);
10667 if (!map->p[i])
10668 goto error;
10671 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10673 isl_reordering_free(r);
10674 free(dim_map);
10675 return map;
10676 error:
10677 free(dim_map);
10678 isl_map_free(map);
10679 isl_reordering_free(r);
10680 return NULL;
10683 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10684 __isl_take isl_reordering *r)
10686 return (isl_set *)isl_map_realign((isl_map *)set, r);
10689 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10690 __isl_take isl_space *model)
10692 isl_ctx *ctx;
10694 if (!map || !model)
10695 goto error;
10697 ctx = isl_space_get_ctx(model);
10698 if (!isl_space_has_named_params(model))
10699 isl_die(ctx, isl_error_invalid,
10700 "model has unnamed parameters", goto error);
10701 if (!isl_space_has_named_params(map->dim))
10702 isl_die(ctx, isl_error_invalid,
10703 "relation has unnamed parameters", goto error);
10704 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10705 isl_reordering *exp;
10707 model = isl_space_drop_dims(model, isl_dim_in,
10708 0, isl_space_dim(model, isl_dim_in));
10709 model = isl_space_drop_dims(model, isl_dim_out,
10710 0, isl_space_dim(model, isl_dim_out));
10711 exp = isl_parameter_alignment_reordering(map->dim, model);
10712 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10713 map = isl_map_realign(map, exp);
10716 isl_space_free(model);
10717 return map;
10718 error:
10719 isl_space_free(model);
10720 isl_map_free(map);
10721 return NULL;
10724 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10725 __isl_take isl_space *model)
10727 return isl_map_align_params(set, model);
10730 /* Align the parameters of "bmap" to those of "model", introducing
10731 * additional parameters if needed.
10733 __isl_give isl_basic_map *isl_basic_map_align_params(
10734 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10736 isl_ctx *ctx;
10738 if (!bmap || !model)
10739 goto error;
10741 ctx = isl_space_get_ctx(model);
10742 if (!isl_space_has_named_params(model))
10743 isl_die(ctx, isl_error_invalid,
10744 "model has unnamed parameters", goto error);
10745 if (!isl_space_has_named_params(bmap->dim))
10746 isl_die(ctx, isl_error_invalid,
10747 "relation has unnamed parameters", goto error);
10748 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10749 isl_reordering *exp;
10750 struct isl_dim_map *dim_map;
10752 model = isl_space_drop_dims(model, isl_dim_in,
10753 0, isl_space_dim(model, isl_dim_in));
10754 model = isl_space_drop_dims(model, isl_dim_out,
10755 0, isl_space_dim(model, isl_dim_out));
10756 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10757 exp = isl_reordering_extend_space(exp,
10758 isl_basic_map_get_space(bmap));
10759 dim_map = isl_dim_map_from_reordering(exp);
10760 bmap = isl_basic_map_realign(bmap,
10761 exp ? isl_space_copy(exp->dim) : NULL,
10762 isl_dim_map_extend(dim_map, bmap));
10763 isl_reordering_free(exp);
10764 free(dim_map);
10767 isl_space_free(model);
10768 return bmap;
10769 error:
10770 isl_space_free(model);
10771 isl_basic_map_free(bmap);
10772 return NULL;
10775 /* Align the parameters of "bset" to those of "model", introducing
10776 * additional parameters if needed.
10778 __isl_give isl_basic_set *isl_basic_set_align_params(
10779 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10781 return isl_basic_map_align_params(bset, model);
10784 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10785 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10786 enum isl_dim_type c2, enum isl_dim_type c3,
10787 enum isl_dim_type c4, enum isl_dim_type c5)
10789 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10790 struct isl_mat *mat;
10791 int i, j, k;
10792 int pos;
10794 if (!bmap)
10795 return NULL;
10796 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
10797 isl_basic_map_total_dim(bmap) + 1);
10798 if (!mat)
10799 return NULL;
10800 for (i = 0; i < bmap->n_eq; ++i)
10801 for (j = 0, pos = 0; j < 5; ++j) {
10802 int off = isl_basic_map_offset(bmap, c[j]);
10803 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10804 isl_int_set(mat->row[i][pos],
10805 bmap->eq[i][off + k]);
10806 ++pos;
10810 return mat;
10813 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
10814 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10815 enum isl_dim_type c2, enum isl_dim_type c3,
10816 enum isl_dim_type c4, enum isl_dim_type c5)
10818 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10819 struct isl_mat *mat;
10820 int i, j, k;
10821 int pos;
10823 if (!bmap)
10824 return NULL;
10825 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
10826 isl_basic_map_total_dim(bmap) + 1);
10827 if (!mat)
10828 return NULL;
10829 for (i = 0; i < bmap->n_ineq; ++i)
10830 for (j = 0, pos = 0; j < 5; ++j) {
10831 int off = isl_basic_map_offset(bmap, c[j]);
10832 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10833 isl_int_set(mat->row[i][pos],
10834 bmap->ineq[i][off + k]);
10835 ++pos;
10839 return mat;
10842 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
10843 __isl_take isl_space *dim,
10844 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10845 enum isl_dim_type c2, enum isl_dim_type c3,
10846 enum isl_dim_type c4, enum isl_dim_type c5)
10848 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10849 isl_basic_map *bmap;
10850 unsigned total;
10851 unsigned extra;
10852 int i, j, k, l;
10853 int pos;
10855 if (!dim || !eq || !ineq)
10856 goto error;
10858 if (eq->n_col != ineq->n_col)
10859 isl_die(dim->ctx, isl_error_invalid,
10860 "equalities and inequalities matrices should have "
10861 "same number of columns", goto error);
10863 total = 1 + isl_space_dim(dim, isl_dim_all);
10865 if (eq->n_col < total)
10866 isl_die(dim->ctx, isl_error_invalid,
10867 "number of columns too small", goto error);
10869 extra = eq->n_col - total;
10871 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
10872 eq->n_row, ineq->n_row);
10873 if (!bmap)
10874 goto error;
10875 for (i = 0; i < extra; ++i) {
10876 k = isl_basic_map_alloc_div(bmap);
10877 if (k < 0)
10878 goto error;
10879 isl_int_set_si(bmap->div[k][0], 0);
10881 for (i = 0; i < eq->n_row; ++i) {
10882 l = isl_basic_map_alloc_equality(bmap);
10883 if (l < 0)
10884 goto error;
10885 for (j = 0, pos = 0; j < 5; ++j) {
10886 int off = isl_basic_map_offset(bmap, c[j]);
10887 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10888 isl_int_set(bmap->eq[l][off + k],
10889 eq->row[i][pos]);
10890 ++pos;
10894 for (i = 0; i < ineq->n_row; ++i) {
10895 l = isl_basic_map_alloc_inequality(bmap);
10896 if (l < 0)
10897 goto error;
10898 for (j = 0, pos = 0; j < 5; ++j) {
10899 int off = isl_basic_map_offset(bmap, c[j]);
10900 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10901 isl_int_set(bmap->ineq[l][off + k],
10902 ineq->row[i][pos]);
10903 ++pos;
10908 isl_space_free(dim);
10909 isl_mat_free(eq);
10910 isl_mat_free(ineq);
10912 bmap = isl_basic_map_simplify(bmap);
10913 return isl_basic_map_finalize(bmap);
10914 error:
10915 isl_space_free(dim);
10916 isl_mat_free(eq);
10917 isl_mat_free(ineq);
10918 return NULL;
10921 __isl_give isl_mat *isl_basic_set_equalities_matrix(
10922 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10923 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10925 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
10926 c1, c2, c3, c4, isl_dim_in);
10929 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
10930 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10931 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10933 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
10934 c1, c2, c3, c4, isl_dim_in);
10937 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
10938 __isl_take isl_space *dim,
10939 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10940 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10942 return (isl_basic_set*)
10943 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
10944 c1, c2, c3, c4, isl_dim_in);
10947 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
10949 if (!bmap)
10950 return -1;
10952 return isl_space_can_zip(bmap->dim);
10955 int isl_map_can_zip(__isl_keep isl_map *map)
10957 if (!map)
10958 return -1;
10960 return isl_space_can_zip(map->dim);
10963 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10964 * (A -> C) -> (B -> D).
10966 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10968 unsigned pos;
10969 unsigned n1;
10970 unsigned n2;
10972 if (!bmap)
10973 return NULL;
10975 if (!isl_basic_map_can_zip(bmap))
10976 isl_die(bmap->ctx, isl_error_invalid,
10977 "basic map cannot be zipped", goto error);
10978 pos = isl_basic_map_offset(bmap, isl_dim_in) +
10979 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10980 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10981 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10982 bmap = isl_basic_map_cow(bmap);
10983 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10984 if (!bmap)
10985 return NULL;
10986 bmap->dim = isl_space_zip(bmap->dim);
10987 if (!bmap->dim)
10988 goto error;
10989 return bmap;
10990 error:
10991 isl_basic_map_free(bmap);
10992 return NULL;
10995 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10996 * (A -> C) -> (B -> D).
10998 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11000 int i;
11002 if (!map)
11003 return NULL;
11005 if (!isl_map_can_zip(map))
11006 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11007 goto error);
11009 map = isl_map_cow(map);
11010 if (!map)
11011 return NULL;
11013 for (i = 0; i < map->n; ++i) {
11014 map->p[i] = isl_basic_map_zip(map->p[i]);
11015 if (!map->p[i])
11016 goto error;
11019 map->dim = isl_space_zip(map->dim);
11020 if (!map->dim)
11021 goto error;
11023 return map;
11024 error:
11025 isl_map_free(map);
11026 return NULL;
11029 /* Can we apply isl_basic_map_curry to "bmap"?
11030 * That is, does it have a nested relation in its domain?
11032 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11034 if (!bmap)
11035 return -1;
11037 return isl_space_can_curry(bmap->dim);
11040 /* Can we apply isl_map_curry to "map"?
11041 * That is, does it have a nested relation in its domain?
11043 int isl_map_can_curry(__isl_keep isl_map *map)
11045 if (!map)
11046 return -1;
11048 return isl_space_can_curry(map->dim);
11051 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11052 * A -> (B -> C).
11054 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11057 if (!bmap)
11058 return NULL;
11060 if (!isl_basic_map_can_curry(bmap))
11061 isl_die(bmap->ctx, isl_error_invalid,
11062 "basic map cannot be curried", goto error);
11063 bmap = isl_basic_map_cow(bmap);
11064 if (!bmap)
11065 return NULL;
11066 bmap->dim = isl_space_curry(bmap->dim);
11067 if (!bmap->dim)
11068 goto error;
11069 return bmap;
11070 error:
11071 isl_basic_map_free(bmap);
11072 return NULL;
11075 /* Given a map (A -> B) -> C, return the corresponding map
11076 * A -> (B -> C).
11078 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11080 int i;
11082 if (!map)
11083 return NULL;
11085 if (!isl_map_can_curry(map))
11086 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11087 goto error);
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_curry(map->p[i]);
11095 if (!map->p[i])
11096 goto error;
11099 map->dim = isl_space_curry(map->dim);
11100 if (!map->dim)
11101 goto error;
11103 return map;
11104 error:
11105 isl_map_free(map);
11106 return NULL;
11109 /* Can we apply isl_basic_map_uncurry to "bmap"?
11110 * That is, does it have a nested relation in its domain?
11112 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11114 if (!bmap)
11115 return -1;
11117 return isl_space_can_uncurry(bmap->dim);
11120 /* Can we apply isl_map_uncurry to "map"?
11121 * That is, does it have a nested relation in its domain?
11123 int isl_map_can_uncurry(__isl_keep isl_map *map)
11125 if (!map)
11126 return -1;
11128 return isl_space_can_uncurry(map->dim);
11131 /* Given a basic map A -> (B -> C), return the corresponding basic map
11132 * (A -> B) -> C.
11134 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11137 if (!bmap)
11138 return NULL;
11140 if (!isl_basic_map_can_uncurry(bmap))
11141 isl_die(bmap->ctx, isl_error_invalid,
11142 "basic map cannot be uncurried",
11143 return isl_basic_map_free(bmap));
11144 bmap = isl_basic_map_cow(bmap);
11145 if (!bmap)
11146 return NULL;
11147 bmap->dim = isl_space_uncurry(bmap->dim);
11148 if (!bmap->dim)
11149 return isl_basic_map_free(bmap);
11150 return bmap;
11153 /* Given a map A -> (B -> C), return the corresponding map
11154 * (A -> B) -> C.
11156 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11158 int i;
11160 if (!map)
11161 return NULL;
11163 if (!isl_map_can_uncurry(map))
11164 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11165 return isl_map_free(map));
11167 map = isl_map_cow(map);
11168 if (!map)
11169 return NULL;
11171 for (i = 0; i < map->n; ++i) {
11172 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11173 if (!map->p[i])
11174 return isl_map_free(map);
11177 map->dim = isl_space_uncurry(map->dim);
11178 if (!map->dim)
11179 return isl_map_free(map);
11181 return map;
11184 /* Construct a basic map mapping the domain of the affine expression
11185 * to a one-dimensional range prescribed by the affine expression.
11187 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11189 int k;
11190 int pos;
11191 isl_local_space *ls;
11192 isl_basic_map *bmap;
11194 if (!aff)
11195 return NULL;
11197 ls = isl_aff_get_local_space(aff);
11198 bmap = isl_basic_map_from_local_space(ls);
11199 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11200 k = isl_basic_map_alloc_equality(bmap);
11201 if (k < 0)
11202 goto error;
11204 pos = isl_basic_map_offset(bmap, isl_dim_out);
11205 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11206 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11207 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11208 aff->v->size - (pos + 1));
11210 isl_aff_free(aff);
11211 bmap = isl_basic_map_finalize(bmap);
11212 return bmap;
11213 error:
11214 isl_aff_free(aff);
11215 isl_basic_map_free(bmap);
11216 return NULL;
11219 /* Construct a map mapping the domain of the affine expression
11220 * to a one-dimensional range prescribed by the affine expression.
11222 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11224 isl_basic_map *bmap;
11226 bmap = isl_basic_map_from_aff(aff);
11227 return isl_map_from_basic_map(bmap);
11230 /* Construct a basic map mapping the domain the multi-affine expression
11231 * to its range, with each dimension in the range equated to the
11232 * corresponding affine expression.
11234 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11235 __isl_take isl_multi_aff *maff)
11237 int i;
11238 isl_space *space;
11239 isl_basic_map *bmap;
11241 if (!maff)
11242 return NULL;
11244 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11245 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11246 "invalid space", return isl_multi_aff_free(maff));
11248 space = isl_space_domain(isl_multi_aff_get_space(maff));
11249 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11251 for (i = 0; i < maff->n; ++i) {
11252 isl_aff *aff;
11253 isl_basic_map *bmap_i;
11255 aff = isl_aff_copy(maff->p[i]);
11256 bmap_i = isl_basic_map_from_aff(aff);
11258 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11261 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11263 isl_multi_aff_free(maff);
11264 return bmap;
11267 /* Construct a map mapping the domain the multi-affine expression
11268 * to its range, with each dimension in the range equated to the
11269 * corresponding affine expression.
11271 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11273 isl_basic_map *bmap;
11275 bmap = isl_basic_map_from_multi_aff(maff);
11276 return isl_map_from_basic_map(bmap);
11279 /* Construct a basic map mapping a domain in the given space to
11280 * to an n-dimensional range, with n the number of elements in the list,
11281 * where each coordinate in the range is prescribed by the
11282 * corresponding affine expression.
11283 * The domains of all affine expressions in the list are assumed to match
11284 * domain_dim.
11286 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11287 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11289 int i;
11290 isl_space *dim;
11291 isl_basic_map *bmap;
11293 if (!list)
11294 return NULL;
11296 dim = isl_space_from_domain(domain_dim);
11297 bmap = isl_basic_map_universe(dim);
11299 for (i = 0; i < list->n; ++i) {
11300 isl_aff *aff;
11301 isl_basic_map *bmap_i;
11303 aff = isl_aff_copy(list->p[i]);
11304 bmap_i = isl_basic_map_from_aff(aff);
11306 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11309 isl_aff_list_free(list);
11310 return bmap;
11313 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11314 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11316 return isl_map_equate(set, type1, pos1, type2, pos2);
11319 /* Construct a basic map where the given dimensions are equal to each other.
11321 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11322 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11324 isl_basic_map *bmap = NULL;
11325 int i;
11327 if (!space)
11328 return NULL;
11330 if (pos1 >= isl_space_dim(space, type1))
11331 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11332 "index out of bounds", goto error);
11333 if (pos2 >= isl_space_dim(space, type2))
11334 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11335 "index out of bounds", goto error);
11337 if (type1 == type2 && pos1 == pos2)
11338 return isl_basic_map_universe(space);
11340 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11341 i = isl_basic_map_alloc_equality(bmap);
11342 if (i < 0)
11343 goto error;
11344 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11345 pos1 += isl_basic_map_offset(bmap, type1);
11346 pos2 += isl_basic_map_offset(bmap, type2);
11347 isl_int_set_si(bmap->eq[i][pos1], -1);
11348 isl_int_set_si(bmap->eq[i][pos2], 1);
11349 bmap = isl_basic_map_finalize(bmap);
11350 isl_space_free(space);
11351 return bmap;
11352 error:
11353 isl_space_free(space);
11354 isl_basic_map_free(bmap);
11355 return NULL;
11358 /* Add a constraint imposing that the given two dimensions are equal.
11360 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11361 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11363 isl_basic_map *eq;
11365 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11367 bmap = isl_basic_map_intersect(bmap, eq);
11369 return bmap;
11372 /* Add a constraint imposing that the given two dimensions are equal.
11374 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11375 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11377 isl_basic_map *bmap;
11379 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11381 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11383 return map;
11386 /* Add a constraint imposing that the given two dimensions have opposite values.
11388 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11389 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11391 isl_basic_map *bmap = NULL;
11392 int i;
11394 if (!map)
11395 return NULL;
11397 if (pos1 >= isl_map_dim(map, type1))
11398 isl_die(map->ctx, isl_error_invalid,
11399 "index out of bounds", goto error);
11400 if (pos2 >= isl_map_dim(map, type2))
11401 isl_die(map->ctx, isl_error_invalid,
11402 "index out of bounds", goto error);
11404 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11405 i = isl_basic_map_alloc_equality(bmap);
11406 if (i < 0)
11407 goto error;
11408 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11409 pos1 += isl_basic_map_offset(bmap, type1);
11410 pos2 += isl_basic_map_offset(bmap, type2);
11411 isl_int_set_si(bmap->eq[i][pos1], 1);
11412 isl_int_set_si(bmap->eq[i][pos2], 1);
11413 bmap = isl_basic_map_finalize(bmap);
11415 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11417 return map;
11418 error:
11419 isl_basic_map_free(bmap);
11420 isl_map_free(map);
11421 return NULL;
11424 /* Construct a constraint imposing that the value of the first dimension is
11425 * greater than or equal to that of the second.
11427 static __isl_give isl_constraint *constraint_order_ge(
11428 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11429 enum isl_dim_type type2, int pos2)
11431 isl_constraint *c;
11433 if (!space)
11434 return NULL;
11436 c = isl_inequality_alloc(isl_local_space_from_space(space));
11438 if (pos1 >= isl_constraint_dim(c, type1))
11439 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11440 "index out of bounds", return isl_constraint_free(c));
11441 if (pos2 >= isl_constraint_dim(c, type2))
11442 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11443 "index out of bounds", return isl_constraint_free(c));
11445 if (type1 == type2 && pos1 == pos2)
11446 return c;
11448 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11449 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11451 return c;
11454 /* Add a constraint imposing that the value of the first dimension is
11455 * greater than or equal to that of the second.
11457 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11458 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11460 isl_constraint *c;
11461 isl_space *space;
11463 if (type1 == type2 && pos1 == pos2)
11464 return bmap;
11465 space = isl_basic_map_get_space(bmap);
11466 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11467 bmap = isl_basic_map_add_constraint(bmap, c);
11469 return bmap;
11472 /* Add a constraint imposing that the value of the first dimension is
11473 * greater than or equal to that of the second.
11475 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11476 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11478 isl_constraint *c;
11479 isl_space *space;
11481 if (type1 == type2 && pos1 == pos2)
11482 return map;
11483 space = isl_map_get_space(map);
11484 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11485 map = isl_map_add_constraint(map, c);
11487 return map;
11490 /* Add a constraint imposing that the value of the first dimension is
11491 * less than or equal to that of the second.
11493 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11494 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11496 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11499 /* Construct a basic map where the value of the first dimension is
11500 * greater than that of the second.
11502 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11503 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11505 isl_basic_map *bmap = NULL;
11506 int i;
11508 if (!space)
11509 return NULL;
11511 if (pos1 >= isl_space_dim(space, type1))
11512 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11513 "index out of bounds", goto error);
11514 if (pos2 >= isl_space_dim(space, type2))
11515 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11516 "index out of bounds", goto error);
11518 if (type1 == type2 && pos1 == pos2)
11519 return isl_basic_map_empty(space);
11521 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11522 i = isl_basic_map_alloc_inequality(bmap);
11523 if (i < 0)
11524 goto error;
11525 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11526 pos1 += isl_basic_map_offset(bmap, type1);
11527 pos2 += isl_basic_map_offset(bmap, type2);
11528 isl_int_set_si(bmap->ineq[i][pos1], 1);
11529 isl_int_set_si(bmap->ineq[i][pos2], -1);
11530 isl_int_set_si(bmap->ineq[i][0], -1);
11531 bmap = isl_basic_map_finalize(bmap);
11533 return bmap;
11534 error:
11535 isl_space_free(space);
11536 isl_basic_map_free(bmap);
11537 return NULL;
11540 /* Add a constraint imposing that the value of the first dimension is
11541 * greater than that of the second.
11543 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11544 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11546 isl_basic_map *gt;
11548 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11550 bmap = isl_basic_map_intersect(bmap, gt);
11552 return bmap;
11555 /* Add a constraint imposing that the value of the first dimension is
11556 * greater than that of the second.
11558 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11559 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11561 isl_basic_map *bmap;
11563 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11565 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11567 return map;
11570 /* Add a constraint imposing that the value of the first dimension is
11571 * smaller than that of the second.
11573 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11574 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11576 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11579 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11580 int pos)
11582 isl_aff *div;
11583 isl_local_space *ls;
11585 if (!bmap)
11586 return NULL;
11588 if (!isl_basic_map_divs_known(bmap))
11589 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11590 "some divs are unknown", return NULL);
11592 ls = isl_basic_map_get_local_space(bmap);
11593 div = isl_local_space_get_div(ls, pos);
11594 isl_local_space_free(ls);
11596 return div;
11599 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11600 int pos)
11602 return isl_basic_map_get_div(bset, pos);
11605 /* Plug in "subs" for dimension "type", "pos" of "bset".
11607 * Let i be the dimension to replace and let "subs" be of the form
11609 * f/d
11611 * Any integer division with a non-zero coefficient for i,
11613 * floor((a i + g)/m)
11615 * is replaced by
11617 * floor((a f + d g)/(m d))
11619 * Constraints of the form
11621 * a i + g
11623 * are replaced by
11625 * a f + d g
11627 * We currently require that "subs" is an integral expression.
11628 * Handling rational expressions may require us to add stride constraints
11629 * as we do in isl_basic_set_preimage_multi_aff.
11631 __isl_give isl_basic_set *isl_basic_set_substitute(
11632 __isl_take isl_basic_set *bset,
11633 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11635 int i;
11636 isl_int v;
11637 isl_ctx *ctx;
11639 if (bset && isl_basic_set_plain_is_empty(bset))
11640 return bset;
11642 bset = isl_basic_set_cow(bset);
11643 if (!bset || !subs)
11644 goto error;
11646 ctx = isl_basic_set_get_ctx(bset);
11647 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11648 isl_die(ctx, isl_error_invalid,
11649 "spaces don't match", goto error);
11650 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11651 isl_die(ctx, isl_error_unsupported,
11652 "cannot handle divs yet", goto error);
11653 if (!isl_int_is_one(subs->v->el[0]))
11654 isl_die(ctx, isl_error_invalid,
11655 "can only substitute integer expressions", goto error);
11657 pos += isl_basic_set_offset(bset, type);
11659 isl_int_init(v);
11661 for (i = 0; i < bset->n_eq; ++i) {
11662 if (isl_int_is_zero(bset->eq[i][pos]))
11663 continue;
11664 isl_int_set(v, bset->eq[i][pos]);
11665 isl_int_set_si(bset->eq[i][pos], 0);
11666 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11667 v, subs->v->el + 1, subs->v->size - 1);
11670 for (i = 0; i < bset->n_ineq; ++i) {
11671 if (isl_int_is_zero(bset->ineq[i][pos]))
11672 continue;
11673 isl_int_set(v, bset->ineq[i][pos]);
11674 isl_int_set_si(bset->ineq[i][pos], 0);
11675 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11676 v, subs->v->el + 1, subs->v->size - 1);
11679 for (i = 0; i < bset->n_div; ++i) {
11680 if (isl_int_is_zero(bset->div[i][1 + pos]))
11681 continue;
11682 isl_int_set(v, bset->div[i][1 + pos]);
11683 isl_int_set_si(bset->div[i][1 + pos], 0);
11684 isl_seq_combine(bset->div[i] + 1,
11685 subs->v->el[0], bset->div[i] + 1,
11686 v, subs->v->el + 1, subs->v->size - 1);
11687 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11690 isl_int_clear(v);
11692 bset = isl_basic_set_simplify(bset);
11693 return isl_basic_set_finalize(bset);
11694 error:
11695 isl_basic_set_free(bset);
11696 return NULL;
11699 /* Plug in "subs" for dimension "type", "pos" of "set".
11701 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11702 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11704 int i;
11706 if (set && isl_set_plain_is_empty(set))
11707 return set;
11709 set = isl_set_cow(set);
11710 if (!set || !subs)
11711 goto error;
11713 for (i = set->n - 1; i >= 0; --i) {
11714 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11715 if (remove_if_empty(set, i) < 0)
11716 goto error;
11719 return set;
11720 error:
11721 isl_set_free(set);
11722 return NULL;
11725 /* Check if the range of "ma" is compatible with the domain or range
11726 * (depending on "type") of "bmap".
11727 * Return -1 if anything is wrong.
11729 static int check_basic_map_compatible_range_multi_aff(
11730 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11731 __isl_keep isl_multi_aff *ma)
11733 int m;
11734 isl_space *ma_space;
11736 ma_space = isl_multi_aff_get_space(ma);
11737 m = isl_space_tuple_match(bmap->dim, type, ma_space, isl_dim_out);
11738 isl_space_free(ma_space);
11739 if (m >= 0 && !m)
11740 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11741 "spaces don't match", return -1);
11742 return m;
11745 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11746 * coefficients before the transformed range of dimensions,
11747 * the "n_after" coefficients after the transformed range of dimensions
11748 * and the coefficients of the other divs in "bmap".
11750 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
11751 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
11753 int i;
11754 int n_param;
11755 int n_set;
11756 isl_local_space *ls;
11758 if (n_div == 0)
11759 return 0;
11761 ls = isl_aff_get_domain_local_space(ma->p[0]);
11762 if (!ls)
11763 return -1;
11765 n_param = isl_local_space_dim(ls, isl_dim_param);
11766 n_set = isl_local_space_dim(ls, isl_dim_set);
11767 for (i = 0; i < n_div; ++i) {
11768 int o_bmap = 0, o_ls = 0;
11770 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
11771 o_bmap += 1 + 1 + n_param;
11772 o_ls += 1 + 1 + n_param;
11773 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
11774 o_bmap += n_before;
11775 isl_seq_cpy(bmap->div[i] + o_bmap,
11776 ls->div->row[i] + o_ls, n_set);
11777 o_bmap += n_set;
11778 o_ls += n_set;
11779 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
11780 o_bmap += n_after;
11781 isl_seq_cpy(bmap->div[i] + o_bmap,
11782 ls->div->row[i] + o_ls, n_div);
11783 o_bmap += n_div;
11784 o_ls += n_div;
11785 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
11786 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
11787 goto error;
11790 isl_local_space_free(ls);
11791 return 0;
11792 error:
11793 isl_local_space_free(ls);
11794 return -1;
11797 /* How many stride constraints does "ma" enforce?
11798 * That is, how many of the affine expressions have a denominator
11799 * different from one?
11801 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
11803 int i;
11804 int strides = 0;
11806 for (i = 0; i < ma->n; ++i)
11807 if (!isl_int_is_one(ma->p[i]->v->el[0]))
11808 strides++;
11810 return strides;
11813 /* For each affine expression in ma of the form
11815 * x_i = (f_i y + h_i)/m_i
11817 * with m_i different from one, add a constraint to "bmap"
11818 * of the form
11820 * f_i y + h_i = m_i alpha_i
11822 * with alpha_i an additional existentially quantified variable.
11824 static __isl_give isl_basic_map *add_ma_strides(
11825 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
11826 int n_before, int n_after)
11828 int i, k;
11829 int div;
11830 int total;
11831 int n_param;
11832 int n_in;
11833 int n_div;
11835 total = isl_basic_map_total_dim(bmap);
11836 n_param = isl_multi_aff_dim(ma, isl_dim_param);
11837 n_in = isl_multi_aff_dim(ma, isl_dim_in);
11838 n_div = isl_multi_aff_dim(ma, isl_dim_div);
11839 for (i = 0; i < ma->n; ++i) {
11840 int o_bmap = 0, o_ma = 1;
11842 if (isl_int_is_one(ma->p[i]->v->el[0]))
11843 continue;
11844 div = isl_basic_map_alloc_div(bmap);
11845 k = isl_basic_map_alloc_equality(bmap);
11846 if (div < 0 || k < 0)
11847 goto error;
11848 isl_int_set_si(bmap->div[div][0], 0);
11849 isl_seq_cpy(bmap->eq[k] + o_bmap,
11850 ma->p[i]->v->el + o_ma, 1 + n_param);
11851 o_bmap += 1 + n_param;
11852 o_ma += 1 + n_param;
11853 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
11854 o_bmap += n_before;
11855 isl_seq_cpy(bmap->eq[k] + o_bmap,
11856 ma->p[i]->v->el + o_ma, n_in);
11857 o_bmap += n_in;
11858 o_ma += n_in;
11859 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
11860 o_bmap += n_after;
11861 isl_seq_cpy(bmap->eq[k] + o_bmap,
11862 ma->p[i]->v->el + o_ma, n_div);
11863 o_bmap += n_div;
11864 o_ma += n_div;
11865 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
11866 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
11867 total++;
11870 return bmap;
11871 error:
11872 isl_basic_map_free(bmap);
11873 return NULL;
11876 /* Replace the domain or range space (depending on "type) of "space" by "set".
11878 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
11879 enum isl_dim_type type, __isl_take isl_space *set)
11881 if (type == isl_dim_in) {
11882 space = isl_space_range(space);
11883 space = isl_space_map_from_domain_and_range(set, space);
11884 } else {
11885 space = isl_space_domain(space);
11886 space = isl_space_map_from_domain_and_range(space, set);
11889 return space;
11892 /* Compute the preimage of the domain or range (depending on "type")
11893 * of "bmap" under the function represented by "ma".
11894 * In other words, plug in "ma" in the domain or range of "bmap".
11895 * The result is a basic map that lives in the same space as "bmap"
11896 * except that the domain or range has been replaced by
11897 * the domain space of "ma".
11899 * If bmap is represented by
11901 * A(p) + S u + B x + T v + C(divs) >= 0,
11903 * where u and x are input and output dimensions if type == isl_dim_out
11904 * while x and v are input and output dimensions if type == isl_dim_in,
11905 * and ma is represented by
11907 * x = D(p) + F(y) + G(divs')
11909 * then the result is
11911 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11913 * The divs in the input set are similarly adjusted.
11914 * In particular
11916 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11918 * becomes
11920 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11921 * B_i G(divs') + c_i(divs))/n_i)
11923 * If bmap is not a rational map and if F(y) involves any denominators
11925 * x_i = (f_i y + h_i)/m_i
11927 * then additional constraints are added to ensure that we only
11928 * map back integer points. That is we enforce
11930 * f_i y + h_i = m_i alpha_i
11932 * with alpha_i an additional existentially quantified variable.
11934 * We first copy over the divs from "ma".
11935 * Then we add the modified constraints and divs from "bmap".
11936 * Finally, we add the stride constraints, if needed.
11938 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
11939 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
11940 __isl_take isl_multi_aff *ma)
11942 int i, k;
11943 isl_space *space;
11944 isl_basic_map *res = NULL;
11945 int n_before, n_after, n_div_bmap, n_div_ma;
11946 isl_int f, c1, c2, g;
11947 int rational, strides;
11949 isl_int_init(f);
11950 isl_int_init(c1);
11951 isl_int_init(c2);
11952 isl_int_init(g);
11954 ma = isl_multi_aff_align_divs(ma);
11955 if (!bmap || !ma)
11956 goto error;
11957 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
11958 goto error;
11960 if (type == isl_dim_in) {
11961 n_before = 0;
11962 n_after = isl_basic_map_dim(bmap, isl_dim_out);
11963 } else {
11964 n_before = isl_basic_map_dim(bmap, isl_dim_in);
11965 n_after = 0;
11967 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
11968 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
11970 space = isl_multi_aff_get_domain_space(ma);
11971 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
11972 rational = isl_basic_map_is_rational(bmap);
11973 strides = rational ? 0 : multi_aff_strides(ma);
11974 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
11975 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
11976 if (rational)
11977 res = isl_basic_map_set_rational(res);
11979 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
11980 if (isl_basic_map_alloc_div(res) < 0)
11981 goto error;
11983 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
11984 goto error;
11986 for (i = 0; i < bmap->n_eq; ++i) {
11987 k = isl_basic_map_alloc_equality(res);
11988 if (k < 0)
11989 goto error;
11990 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
11991 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11994 for (i = 0; i < bmap->n_ineq; ++i) {
11995 k = isl_basic_map_alloc_inequality(res);
11996 if (k < 0)
11997 goto error;
11998 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
11999 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12002 for (i = 0; i < bmap->n_div; ++i) {
12003 if (isl_int_is_zero(bmap->div[i][0])) {
12004 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12005 continue;
12007 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12008 n_before, n_after, n_div_ma, n_div_bmap,
12009 f, c1, c2, g, 1);
12012 if (strides)
12013 res = add_ma_strides(res, ma, n_before, n_after);
12015 isl_int_clear(f);
12016 isl_int_clear(c1);
12017 isl_int_clear(c2);
12018 isl_int_clear(g);
12019 isl_basic_map_free(bmap);
12020 isl_multi_aff_free(ma);
12021 res = isl_basic_set_simplify(res);
12022 return isl_basic_map_finalize(res);
12023 error:
12024 isl_int_clear(f);
12025 isl_int_clear(c1);
12026 isl_int_clear(c2);
12027 isl_int_clear(g);
12028 isl_basic_map_free(bmap);
12029 isl_multi_aff_free(ma);
12030 isl_basic_map_free(res);
12031 return NULL;
12034 /* Compute the preimage of "bset" under the function represented by "ma".
12035 * In other words, plug in "ma" in "bset". The result is a basic set
12036 * that lives in the domain space of "ma".
12038 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12039 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12041 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12044 /* Compute the preimage of the domain of "bmap" under the function
12045 * represented by "ma".
12046 * In other words, plug in "ma" in the domain of "bmap".
12047 * The result is a basic map that lives in the same space as "bmap"
12048 * except that the domain has been replaced by the domain space of "ma".
12050 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12051 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12053 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12056 /* Compute the preimage of the range of "bmap" under the function
12057 * represented by "ma".
12058 * In other words, plug in "ma" in the range of "bmap".
12059 * The result is a basic map that lives in the same space as "bmap"
12060 * except that the range has been replaced by the domain space of "ma".
12062 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12063 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12065 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12068 /* Check if the range of "ma" is compatible with the domain or range
12069 * (depending on "type") of "map".
12070 * Return -1 if anything is wrong.
12072 static int check_map_compatible_range_multi_aff(
12073 __isl_keep isl_map *map, enum isl_dim_type type,
12074 __isl_keep isl_multi_aff *ma)
12076 int m;
12077 isl_space *ma_space;
12079 ma_space = isl_multi_aff_get_space(ma);
12080 m = isl_space_tuple_match(map->dim, type, ma_space, isl_dim_out);
12081 isl_space_free(ma_space);
12082 if (m >= 0 && !m)
12083 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12084 "spaces don't match", return -1);
12085 return m;
12088 /* Compute the preimage of the domain or range (depending on "type")
12089 * of "map" under the function represented by "ma".
12090 * In other words, plug in "ma" in the domain or range of "map".
12091 * The result is a map that lives in the same space as "map"
12092 * except that the domain or range has been replaced by
12093 * the domain space of "ma".
12095 * The parameters are assumed to have been aligned.
12097 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12098 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12100 int i;
12101 isl_space *space;
12103 map = isl_map_cow(map);
12104 ma = isl_multi_aff_align_divs(ma);
12105 if (!map || !ma)
12106 goto error;
12107 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12108 goto error;
12110 for (i = 0; i < map->n; ++i) {
12111 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12112 isl_multi_aff_copy(ma));
12113 if (!map->p[i])
12114 goto error;
12117 space = isl_multi_aff_get_domain_space(ma);
12118 space = isl_space_set(isl_map_get_space(map), type, space);
12120 isl_space_free(map->dim);
12121 map->dim = space;
12122 if (!map->dim)
12123 goto error;
12125 isl_multi_aff_free(ma);
12126 if (map->n > 1)
12127 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12128 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12129 return map;
12130 error:
12131 isl_multi_aff_free(ma);
12132 isl_map_free(map);
12133 return NULL;
12136 /* Compute the preimage of the domain or range (depending on "type")
12137 * of "map" under the function represented by "ma".
12138 * In other words, plug in "ma" in the domain or range of "map".
12139 * The result is a map that lives in the same space as "map"
12140 * except that the domain or range has been replaced by
12141 * the domain space of "ma".
12143 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12144 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12146 if (!map || !ma)
12147 goto error;
12149 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12150 return map_preimage_multi_aff(map, type, ma);
12152 if (!isl_space_has_named_params(map->dim) ||
12153 !isl_space_has_named_params(ma->space))
12154 isl_die(map->ctx, isl_error_invalid,
12155 "unaligned unnamed parameters", goto error);
12156 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12157 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12159 return map_preimage_multi_aff(map, type, ma);
12160 error:
12161 isl_multi_aff_free(ma);
12162 return isl_map_free(map);
12165 /* Compute the preimage of "set" under the function represented by "ma".
12166 * In other words, plug in "ma" "set". The result is a set
12167 * that lives in the domain space of "ma".
12169 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12170 __isl_take isl_multi_aff *ma)
12172 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12175 /* Compute the preimage of the domain of "map" under the function
12176 * represented by "ma".
12177 * In other words, plug in "ma" in the domain of "map".
12178 * The result is a map that lives in the same space as "map"
12179 * except that the domain has been replaced by the domain space of "ma".
12181 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12182 __isl_take isl_multi_aff *ma)
12184 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12187 /* Compute the preimage of the range of "map" under the function
12188 * represented by "ma".
12189 * In other words, plug in "ma" in the range of "map".
12190 * The result is a map that lives in the same space as "map"
12191 * except that the range has been replaced by the domain space of "ma".
12193 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12194 __isl_take isl_multi_aff *ma)
12196 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12199 /* Compute the preimage of "map" under the function represented by "pma".
12200 * In other words, plug in "pma" in the domain or range of "map".
12201 * The result is a map that lives in the same space as "map",
12202 * except that the space of type "type" has been replaced by
12203 * the domain space of "pma".
12205 * The parameters of "map" and "pma" are assumed to have been aligned.
12207 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12208 __isl_take isl_map *map, enum isl_dim_type type,
12209 __isl_take isl_pw_multi_aff *pma)
12211 int i;
12212 isl_map *res;
12214 if (!pma)
12215 goto error;
12217 if (pma->n == 0) {
12218 isl_pw_multi_aff_free(pma);
12219 res = isl_map_empty(isl_map_get_space(map));
12220 isl_map_free(map);
12221 return res;
12224 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12225 isl_multi_aff_copy(pma->p[0].maff));
12226 if (type == isl_dim_in)
12227 res = isl_map_intersect_domain(res,
12228 isl_map_copy(pma->p[0].set));
12229 else
12230 res = isl_map_intersect_range(res,
12231 isl_map_copy(pma->p[0].set));
12233 for (i = 1; i < pma->n; ++i) {
12234 isl_map *res_i;
12236 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12237 isl_multi_aff_copy(pma->p[i].maff));
12238 if (type == isl_dim_in)
12239 res_i = isl_map_intersect_domain(res_i,
12240 isl_map_copy(pma->p[i].set));
12241 else
12242 res_i = isl_map_intersect_range(res_i,
12243 isl_map_copy(pma->p[i].set));
12244 res = isl_map_union(res, res_i);
12247 isl_pw_multi_aff_free(pma);
12248 isl_map_free(map);
12249 return res;
12250 error:
12251 isl_pw_multi_aff_free(pma);
12252 isl_map_free(map);
12253 return NULL;
12256 /* Compute the preimage of "map" under the function represented by "pma".
12257 * In other words, plug in "pma" in the domain or range of "map".
12258 * The result is a map that lives in the same space as "map",
12259 * except that the space of type "type" has been replaced by
12260 * the domain space of "pma".
12262 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12263 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12265 if (!map || !pma)
12266 goto error;
12268 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12269 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12271 if (!isl_space_has_named_params(map->dim) ||
12272 !isl_space_has_named_params(pma->dim))
12273 isl_die(map->ctx, isl_error_invalid,
12274 "unaligned unnamed parameters", goto error);
12275 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12276 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12278 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12279 error:
12280 isl_pw_multi_aff_free(pma);
12281 return isl_map_free(map);
12284 /* Compute the preimage of "set" under the function represented by "pma".
12285 * In other words, plug in "pma" in "set". The result is a set
12286 * that lives in the domain space of "pma".
12288 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12289 __isl_take isl_pw_multi_aff *pma)
12291 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12294 /* Compute the preimage of the domain of "map" under the function
12295 * represented by "pma".
12296 * In other words, plug in "pma" in the domain of "map".
12297 * The result is a map that lives in the same space as "map",
12298 * except that domain space has been replaced by the domain space of "pma".
12300 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12301 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12303 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12306 /* Compute the preimage of the range of "map" under the function
12307 * represented by "pma".
12308 * In other words, plug in "pma" in the range of "map".
12309 * The result is a map that lives in the same space as "map",
12310 * except that range space has been replaced by the domain space of "pma".
12312 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12313 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12315 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12318 /* Compute the preimage of "map" under the function represented by "mpa".
12319 * In other words, plug in "mpa" in the domain or range of "map".
12320 * The result is a map that lives in the same space as "map",
12321 * except that the space of type "type" has been replaced by
12322 * the domain space of "mpa".
12324 * If the map does not involve any constraints that refer to the
12325 * dimensions of the substituted space, then the only possible
12326 * effect of "mpa" on the map is to map the space to a different space.
12327 * We create a separate isl_multi_aff to effectuate this change
12328 * in order to avoid spurious splitting of the map along the pieces
12329 * of "mpa".
12331 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12332 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12334 int n;
12335 isl_pw_multi_aff *pma;
12337 if (!map || !mpa)
12338 goto error;
12340 n = isl_map_dim(map, type);
12341 if (!isl_map_involves_dims(map, type, 0, n)) {
12342 isl_space *space;
12343 isl_multi_aff *ma;
12345 space = isl_multi_pw_aff_get_space(mpa);
12346 isl_multi_pw_aff_free(mpa);
12347 ma = isl_multi_aff_zero(space);
12348 return isl_map_preimage_multi_aff(map, type, ma);
12351 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12352 return isl_map_preimage_pw_multi_aff(map, type, pma);
12353 error:
12354 isl_map_free(map);
12355 isl_multi_pw_aff_free(mpa);
12356 return NULL;
12359 /* Compute the preimage of "map" under the function represented by "mpa".
12360 * In other words, plug in "mpa" in the domain "map".
12361 * The result is a map that lives in the same space as "map",
12362 * except that domain space has been replaced by the domain space of "mpa".
12364 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12365 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12367 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12370 /* Compute the preimage of "set" by the function represented by "mpa".
12371 * In other words, plug in "mpa" in "set".
12373 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12374 __isl_take isl_multi_pw_aff *mpa)
12376 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);