isl_ast_build_expr_from_basic_set: operate on list of constraints
[isl.git] / isl_map.c
blob573fb2416d154861e95b3207457494f49e6ed245
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 goto error;
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;
435 error:
436 isl_id_free(id);
437 return NULL;
440 /* Replace the identifier of the tuple by "id".
442 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
443 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
445 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
448 /* Does the input or output tuple have a name?
450 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
452 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
455 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
456 enum isl_dim_type type)
458 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
461 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
462 const char *s)
464 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
467 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
468 enum isl_dim_type type, __isl_take isl_id *id)
470 map = isl_map_cow(map);
471 if (!map)
472 goto error;
474 map->dim = isl_space_set_tuple_id(map->dim, type, id);
476 return isl_map_reset_space(map, isl_space_copy(map->dim));
477 error:
478 isl_id_free(id);
479 return NULL;
482 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
483 __isl_take isl_id *id)
485 return isl_map_set_tuple_id(set, isl_dim_set, id);
488 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
489 enum isl_dim_type type)
491 map = isl_map_cow(map);
492 if (!map)
493 return NULL;
495 map->dim = isl_space_reset_tuple_id(map->dim, type);
497 return isl_map_reset_space(map, isl_space_copy(map->dim));
500 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
502 return isl_map_reset_tuple_id(set, isl_dim_set);
505 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
507 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
510 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
511 enum isl_dim_type type)
513 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
516 int isl_set_has_tuple_id(__isl_keep isl_set *set)
518 return isl_map_has_tuple_id(set, isl_dim_set);
521 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
523 return isl_map_get_tuple_id(set, isl_dim_set);
526 /* Does the set tuple have a name?
528 int isl_set_has_tuple_name(__isl_keep isl_set *set)
530 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
534 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
536 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
539 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
541 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
544 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
545 enum isl_dim_type type, unsigned pos)
547 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
550 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
551 enum isl_dim_type type, unsigned pos)
553 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
556 /* Does the given dimension have a name?
558 int isl_map_has_dim_name(__isl_keep isl_map *map,
559 enum isl_dim_type type, unsigned pos)
561 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
564 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
565 enum isl_dim_type type, unsigned pos)
567 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
570 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
571 enum isl_dim_type type, unsigned pos)
573 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
576 /* Does the given dimension have a name?
578 int isl_set_has_dim_name(__isl_keep isl_set *set,
579 enum isl_dim_type type, unsigned pos)
581 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
584 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
585 __isl_take isl_basic_map *bmap,
586 enum isl_dim_type type, unsigned pos, const char *s)
588 bmap = isl_basic_map_cow(bmap);
589 if (!bmap)
590 return NULL;
591 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
592 if (!bmap->dim)
593 goto error;
594 return isl_basic_map_finalize(bmap);
595 error:
596 isl_basic_map_free(bmap);
597 return NULL;
600 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
601 enum isl_dim_type type, unsigned pos, const char *s)
603 int i;
605 map = isl_map_cow(map);
606 if (!map)
607 return NULL;
609 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
610 if (!map->dim)
611 goto error;
613 for (i = 0; i < map->n; ++i) {
614 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
615 if (!map->p[i])
616 goto error;
619 return map;
620 error:
621 isl_map_free(map);
622 return NULL;
625 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
626 __isl_take isl_basic_set *bset,
627 enum isl_dim_type type, unsigned pos, const char *s)
629 return (isl_basic_set *)isl_basic_map_set_dim_name(
630 (isl_basic_map *)bset, type, pos, s);
633 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
634 enum isl_dim_type type, unsigned pos, const char *s)
636 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
639 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
640 enum isl_dim_type type, unsigned pos)
642 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
645 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
646 enum isl_dim_type type, unsigned pos)
648 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
651 int isl_map_has_dim_id(__isl_keep isl_map *map,
652 enum isl_dim_type type, unsigned pos)
654 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
657 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
658 enum isl_dim_type type, unsigned pos)
660 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
663 int isl_set_has_dim_id(__isl_keep isl_set *set,
664 enum isl_dim_type type, unsigned pos)
666 return isl_map_has_dim_id(set, type, pos);
669 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
670 enum isl_dim_type type, unsigned pos)
672 return isl_map_get_dim_id(set, type, pos);
675 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
676 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
678 map = isl_map_cow(map);
679 if (!map)
680 goto error;
682 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
684 return isl_map_reset_space(map, isl_space_copy(map->dim));
685 error:
686 isl_id_free(id);
687 return NULL;
690 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
691 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
693 return isl_map_set_dim_id(set, type, pos, id);
696 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
697 __isl_keep isl_id *id)
699 if (!map)
700 return -1;
701 return isl_space_find_dim_by_id(map->dim, type, id);
704 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
705 __isl_keep isl_id *id)
707 return isl_map_find_dim_by_id(set, type, id);
710 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
711 const char *name)
713 if (!map)
714 return -1;
715 return isl_space_find_dim_by_name(map->dim, type, name);
718 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
719 const char *name)
721 return isl_map_find_dim_by_name(set, type, name);
724 /* Reset the user pointer on all identifiers of parameters and tuples
725 * of the space of "map".
727 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
729 isl_space *space;
731 space = isl_map_get_space(map);
732 space = isl_space_reset_user(space);
733 map = isl_map_reset_space(map, space);
735 return map;
738 /* Reset the user pointer on all identifiers of parameters and tuples
739 * of the space of "set".
741 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
743 return isl_map_reset_user(set);
746 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
748 if (!bmap)
749 return -1;
750 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
753 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
755 return isl_basic_map_is_rational(bset);
758 /* Does "bmap" contain any rational points?
760 * If "bmap" has an equality for each dimension, equating the dimension
761 * to an integer constant, then it has no rational points, even if it
762 * is marked as rational.
764 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
766 int has_rational = 1;
767 unsigned total;
769 if (!bmap)
770 return -1;
771 if (isl_basic_map_plain_is_empty(bmap))
772 return 0;
773 if (!isl_basic_map_is_rational(bmap))
774 return 0;
775 bmap = isl_basic_map_copy(bmap);
776 bmap = isl_basic_map_implicit_equalities(bmap);
777 if (!bmap)
778 return -1;
779 total = isl_basic_map_total_dim(bmap);
780 if (bmap->n_eq == total) {
781 int i, j;
782 for (i = 0; i < bmap->n_eq; ++i) {
783 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
784 if (j < 0)
785 break;
786 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
787 !isl_int_is_negone(bmap->eq[i][1 + j]))
788 break;
789 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
790 total - j - 1);
791 if (j >= 0)
792 break;
794 if (i == bmap->n_eq)
795 has_rational = 0;
797 isl_basic_map_free(bmap);
799 return has_rational;
802 /* Does "map" contain any rational points?
804 int isl_map_has_rational(__isl_keep isl_map *map)
806 int i;
807 int has_rational;
809 if (!map)
810 return -1;
811 for (i = 0; i < map->n; ++i) {
812 has_rational = isl_basic_map_has_rational(map->p[i]);
813 if (has_rational < 0)
814 return -1;
815 if (has_rational)
816 return 1;
818 return 0;
821 /* Does "set" contain any rational points?
823 int isl_set_has_rational(__isl_keep isl_set *set)
825 return isl_map_has_rational(set);
828 /* Is this basic set a parameter domain?
830 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
832 if (!bset)
833 return -1;
834 return isl_space_is_params(bset->dim);
837 /* Is this set a parameter domain?
839 int isl_set_is_params(__isl_keep isl_set *set)
841 if (!set)
842 return -1;
843 return isl_space_is_params(set->dim);
846 /* Is this map actually a parameter domain?
847 * Users should never call this function. Outside of isl,
848 * a map can never be a parameter domain.
850 int isl_map_is_params(__isl_keep isl_map *map)
852 if (!map)
853 return -1;
854 return isl_space_is_params(map->dim);
857 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
858 struct isl_basic_map *bmap, unsigned extra,
859 unsigned n_eq, unsigned n_ineq)
861 int i;
862 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
864 bmap->ctx = ctx;
865 isl_ctx_ref(ctx);
867 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
868 if (isl_blk_is_error(bmap->block))
869 goto error;
871 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
872 if ((n_ineq + n_eq) && !bmap->ineq)
873 goto error;
875 if (extra == 0) {
876 bmap->block2 = isl_blk_empty();
877 bmap->div = NULL;
878 } else {
879 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
880 if (isl_blk_is_error(bmap->block2))
881 goto error;
883 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
884 if (!bmap->div)
885 goto error;
888 for (i = 0; i < n_ineq + n_eq; ++i)
889 bmap->ineq[i] = bmap->block.data + i * row_size;
891 for (i = 0; i < extra; ++i)
892 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
894 bmap->ref = 1;
895 bmap->flags = 0;
896 bmap->c_size = n_eq + n_ineq;
897 bmap->eq = bmap->ineq + n_ineq;
898 bmap->extra = extra;
899 bmap->n_eq = 0;
900 bmap->n_ineq = 0;
901 bmap->n_div = 0;
902 bmap->sample = NULL;
904 return bmap;
905 error:
906 isl_basic_map_free(bmap);
907 return NULL;
910 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
911 unsigned nparam, unsigned dim, unsigned extra,
912 unsigned n_eq, unsigned n_ineq)
914 struct isl_basic_map *bmap;
915 isl_space *space;
917 space = isl_space_set_alloc(ctx, nparam, dim);
918 if (!space)
919 return NULL;
921 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
922 return (struct isl_basic_set *)bmap;
925 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
926 unsigned extra, unsigned n_eq, unsigned n_ineq)
928 struct isl_basic_map *bmap;
929 if (!dim)
930 return NULL;
931 isl_assert(dim->ctx, dim->n_in == 0, goto error);
932 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
933 return (struct isl_basic_set *)bmap;
934 error:
935 isl_space_free(dim);
936 return NULL;
939 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
940 unsigned extra, unsigned n_eq, unsigned n_ineq)
942 struct isl_basic_map *bmap;
944 if (!dim)
945 return NULL;
946 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
947 if (!bmap)
948 goto error;
949 bmap->dim = dim;
951 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
952 error:
953 isl_space_free(dim);
954 return NULL;
957 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
958 unsigned nparam, unsigned in, unsigned out, unsigned extra,
959 unsigned n_eq, unsigned n_ineq)
961 struct isl_basic_map *bmap;
962 isl_space *dim;
964 dim = isl_space_alloc(ctx, nparam, in, out);
965 if (!dim)
966 return NULL;
968 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
969 return bmap;
972 static void dup_constraints(
973 struct isl_basic_map *dst, struct isl_basic_map *src)
975 int i;
976 unsigned total = isl_basic_map_total_dim(src);
978 for (i = 0; i < src->n_eq; ++i) {
979 int j = isl_basic_map_alloc_equality(dst);
980 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
983 for (i = 0; i < src->n_ineq; ++i) {
984 int j = isl_basic_map_alloc_inequality(dst);
985 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
988 for (i = 0; i < src->n_div; ++i) {
989 int j = isl_basic_map_alloc_div(dst);
990 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
992 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
995 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
997 struct isl_basic_map *dup;
999 if (!bmap)
1000 return NULL;
1001 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1002 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1003 if (!dup)
1004 return NULL;
1005 dup_constraints(dup, bmap);
1006 dup->flags = bmap->flags;
1007 dup->sample = isl_vec_copy(bmap->sample);
1008 return dup;
1011 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1013 struct isl_basic_map *dup;
1015 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1016 return (struct isl_basic_set *)dup;
1019 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1021 if (!bset)
1022 return NULL;
1024 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1025 bset->ref++;
1026 return bset;
1028 return isl_basic_set_dup(bset);
1031 struct isl_set *isl_set_copy(struct isl_set *set)
1033 if (!set)
1034 return NULL;
1036 set->ref++;
1037 return set;
1040 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1042 if (!bmap)
1043 return NULL;
1045 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1046 bmap->ref++;
1047 return bmap;
1049 bmap = isl_basic_map_dup(bmap);
1050 if (bmap)
1051 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1052 return bmap;
1055 struct isl_map *isl_map_copy(struct isl_map *map)
1057 if (!map)
1058 return NULL;
1060 map->ref++;
1061 return map;
1064 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1066 if (!bmap)
1067 return NULL;
1069 if (--bmap->ref > 0)
1070 return NULL;
1072 isl_ctx_deref(bmap->ctx);
1073 free(bmap->div);
1074 isl_blk_free(bmap->ctx, bmap->block2);
1075 free(bmap->ineq);
1076 isl_blk_free(bmap->ctx, bmap->block);
1077 isl_vec_free(bmap->sample);
1078 isl_space_free(bmap->dim);
1079 free(bmap);
1081 return NULL;
1084 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1086 return isl_basic_map_free((struct isl_basic_map *)bset);
1089 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1091 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1094 __isl_give isl_map *isl_map_align_params_map_map_and(
1095 __isl_take isl_map *map1, __isl_take isl_map *map2,
1096 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1097 __isl_take isl_map *map2))
1099 if (!map1 || !map2)
1100 goto error;
1101 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1102 return fn(map1, map2);
1103 if (!isl_space_has_named_params(map1->dim) ||
1104 !isl_space_has_named_params(map2->dim))
1105 isl_die(map1->ctx, isl_error_invalid,
1106 "unaligned unnamed parameters", goto error);
1107 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1108 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1109 return fn(map1, map2);
1110 error:
1111 isl_map_free(map1);
1112 isl_map_free(map2);
1113 return NULL;
1116 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1117 __isl_keep isl_map *map2,
1118 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1120 int r;
1122 if (!map1 || !map2)
1123 return -1;
1124 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1125 return fn(map1, map2);
1126 if (!isl_space_has_named_params(map1->dim) ||
1127 !isl_space_has_named_params(map2->dim))
1128 isl_die(map1->ctx, isl_error_invalid,
1129 "unaligned unnamed parameters", return -1);
1130 map1 = isl_map_copy(map1);
1131 map2 = isl_map_copy(map2);
1132 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1133 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1134 r = fn(map1, map2);
1135 isl_map_free(map1);
1136 isl_map_free(map2);
1137 return r;
1140 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1142 struct isl_ctx *ctx;
1143 if (!bmap)
1144 return -1;
1145 ctx = bmap->ctx;
1146 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1147 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1148 return -1);
1149 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1150 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1151 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1152 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1153 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1154 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1155 isl_int *t;
1156 int j = isl_basic_map_alloc_inequality(bmap);
1157 if (j < 0)
1158 return -1;
1159 t = bmap->ineq[j];
1160 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1161 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1162 bmap->eq[-1] = t;
1163 bmap->n_eq++;
1164 bmap->n_ineq--;
1165 bmap->eq--;
1166 return 0;
1168 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1169 bmap->extra - bmap->n_div);
1170 return bmap->n_eq++;
1173 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1175 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1178 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1180 if (!bmap)
1181 return -1;
1182 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1183 bmap->n_eq -= n;
1184 return 0;
1187 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1189 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1192 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1194 isl_int *t;
1195 if (!bmap)
1196 return -1;
1197 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1199 if (pos != bmap->n_eq - 1) {
1200 t = bmap->eq[pos];
1201 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1202 bmap->eq[bmap->n_eq - 1] = t;
1204 bmap->n_eq--;
1205 return 0;
1208 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1210 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1213 /* Turn inequality "pos" of "bmap" into an equality.
1215 * In particular, we move the inequality in front of the equalities
1216 * and move the last inequality in the position of the moved inequality.
1217 * Note that isl_tab_make_equalities_explicit depends on this particular
1218 * change in the ordering of the constraints.
1220 void isl_basic_map_inequality_to_equality(
1221 struct isl_basic_map *bmap, unsigned pos)
1223 isl_int *t;
1225 t = bmap->ineq[pos];
1226 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1227 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1228 bmap->eq[-1] = t;
1229 bmap->n_eq++;
1230 bmap->n_ineq--;
1231 bmap->eq--;
1232 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1233 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1234 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1235 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1238 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1240 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1243 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1245 struct isl_ctx *ctx;
1246 if (!bmap)
1247 return -1;
1248 ctx = bmap->ctx;
1249 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1250 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1251 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1252 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1253 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1254 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1255 1 + isl_basic_map_total_dim(bmap),
1256 bmap->extra - bmap->n_div);
1257 return bmap->n_ineq++;
1260 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1262 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1265 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1267 if (!bmap)
1268 return -1;
1269 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1270 bmap->n_ineq -= n;
1271 return 0;
1274 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1276 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1279 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1281 isl_int *t;
1282 if (!bmap)
1283 return -1;
1284 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1286 if (pos != bmap->n_ineq - 1) {
1287 t = bmap->ineq[pos];
1288 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1289 bmap->ineq[bmap->n_ineq - 1] = t;
1290 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1292 bmap->n_ineq--;
1293 return 0;
1296 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1298 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1301 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1302 isl_int *eq)
1304 int k;
1306 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1307 if (!bmap)
1308 return NULL;
1309 k = isl_basic_map_alloc_equality(bmap);
1310 if (k < 0)
1311 goto error;
1312 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1313 return bmap;
1314 error:
1315 isl_basic_map_free(bmap);
1316 return NULL;
1319 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1320 isl_int *eq)
1322 return (isl_basic_set *)
1323 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1326 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1327 isl_int *ineq)
1329 int k;
1331 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1332 if (!bmap)
1333 return NULL;
1334 k = isl_basic_map_alloc_inequality(bmap);
1335 if (k < 0)
1336 goto error;
1337 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1338 return bmap;
1339 error:
1340 isl_basic_map_free(bmap);
1341 return NULL;
1344 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1345 isl_int *ineq)
1347 return (isl_basic_set *)
1348 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1351 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1353 if (!bmap)
1354 return -1;
1355 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1356 isl_seq_clr(bmap->div[bmap->n_div] +
1357 1 + 1 + isl_basic_map_total_dim(bmap),
1358 bmap->extra - bmap->n_div);
1359 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1360 return bmap->n_div++;
1363 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1365 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1368 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1370 if (!bmap)
1371 return -1;
1372 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1373 bmap->n_div -= n;
1374 return 0;
1377 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1379 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1382 /* Copy constraint from src to dst, putting the vars of src at offset
1383 * dim_off in dst and the divs of src at offset div_off in dst.
1384 * If both sets are actually map, then dim_off applies to the input
1385 * variables.
1387 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1388 struct isl_basic_map *src_map, isl_int *src,
1389 unsigned in_off, unsigned out_off, unsigned div_off)
1391 unsigned src_nparam = isl_basic_map_n_param(src_map);
1392 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1393 unsigned src_in = isl_basic_map_n_in(src_map);
1394 unsigned dst_in = isl_basic_map_n_in(dst_map);
1395 unsigned src_out = isl_basic_map_n_out(src_map);
1396 unsigned dst_out = isl_basic_map_n_out(dst_map);
1397 isl_int_set(dst[0], src[0]);
1398 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1399 if (dst_nparam > src_nparam)
1400 isl_seq_clr(dst+1+src_nparam,
1401 dst_nparam - src_nparam);
1402 isl_seq_clr(dst+1+dst_nparam, in_off);
1403 isl_seq_cpy(dst+1+dst_nparam+in_off,
1404 src+1+src_nparam,
1405 isl_min(dst_in-in_off, src_in));
1406 if (dst_in-in_off > src_in)
1407 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1408 dst_in - in_off - src_in);
1409 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1410 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1411 src+1+src_nparam+src_in,
1412 isl_min(dst_out-out_off, src_out));
1413 if (dst_out-out_off > src_out)
1414 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1415 dst_out - out_off - src_out);
1416 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1417 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1418 src+1+src_nparam+src_in+src_out,
1419 isl_min(dst_map->extra-div_off, src_map->n_div));
1420 if (dst_map->n_div-div_off > src_map->n_div)
1421 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1422 div_off+src_map->n_div,
1423 dst_map->n_div - div_off - src_map->n_div);
1426 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1427 struct isl_basic_map *src_map, isl_int *src,
1428 unsigned in_off, unsigned out_off, unsigned div_off)
1430 isl_int_set(dst[0], src[0]);
1431 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1434 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1435 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1437 int i;
1438 unsigned div_off;
1440 if (!bmap1 || !bmap2)
1441 goto error;
1443 div_off = bmap1->n_div;
1445 for (i = 0; i < bmap2->n_eq; ++i) {
1446 int i1 = isl_basic_map_alloc_equality(bmap1);
1447 if (i1 < 0)
1448 goto error;
1449 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1450 i_pos, o_pos, div_off);
1453 for (i = 0; i < bmap2->n_ineq; ++i) {
1454 int i1 = isl_basic_map_alloc_inequality(bmap1);
1455 if (i1 < 0)
1456 goto error;
1457 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1458 i_pos, o_pos, div_off);
1461 for (i = 0; i < bmap2->n_div; ++i) {
1462 int i1 = isl_basic_map_alloc_div(bmap1);
1463 if (i1 < 0)
1464 goto error;
1465 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1466 i_pos, o_pos, div_off);
1469 isl_basic_map_free(bmap2);
1471 return bmap1;
1473 error:
1474 isl_basic_map_free(bmap1);
1475 isl_basic_map_free(bmap2);
1476 return NULL;
1479 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1480 struct isl_basic_set *bset2, unsigned pos)
1482 return (struct isl_basic_set *)
1483 add_constraints((struct isl_basic_map *)bset1,
1484 (struct isl_basic_map *)bset2, 0, pos);
1487 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1488 __isl_take isl_space *dim, unsigned extra,
1489 unsigned n_eq, unsigned n_ineq)
1491 struct isl_basic_map *ext;
1492 unsigned flags;
1493 int dims_ok;
1495 if (!dim)
1496 goto error;
1498 if (!base)
1499 goto error;
1501 dims_ok = isl_space_is_equal(base->dim, dim) &&
1502 base->extra >= base->n_div + extra;
1504 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1505 room_for_ineq(base, n_ineq)) {
1506 isl_space_free(dim);
1507 return base;
1510 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1511 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1512 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1513 extra += base->extra;
1514 n_eq += base->n_eq;
1515 n_ineq += base->n_ineq;
1517 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1518 dim = NULL;
1519 if (!ext)
1520 goto error;
1522 if (dims_ok)
1523 ext->sample = isl_vec_copy(base->sample);
1524 flags = base->flags;
1525 ext = add_constraints(ext, base, 0, 0);
1526 if (ext) {
1527 ext->flags = flags;
1528 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1531 return ext;
1533 error:
1534 isl_space_free(dim);
1535 isl_basic_map_free(base);
1536 return NULL;
1539 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1540 __isl_take isl_space *dim, unsigned extra,
1541 unsigned n_eq, unsigned n_ineq)
1543 return (struct isl_basic_set *)
1544 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1545 extra, n_eq, n_ineq);
1548 struct isl_basic_map *isl_basic_map_extend_constraints(
1549 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1551 if (!base)
1552 return NULL;
1553 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1554 0, n_eq, n_ineq);
1557 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1558 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1559 unsigned n_eq, unsigned n_ineq)
1561 struct isl_basic_map *bmap;
1562 isl_space *dim;
1564 if (!base)
1565 return NULL;
1566 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1567 if (!dim)
1568 goto error;
1570 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1571 return bmap;
1572 error:
1573 isl_basic_map_free(base);
1574 return NULL;
1577 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1578 unsigned nparam, unsigned dim, unsigned extra,
1579 unsigned n_eq, unsigned n_ineq)
1581 return (struct isl_basic_set *)
1582 isl_basic_map_extend((struct isl_basic_map *)base,
1583 nparam, 0, dim, extra, n_eq, n_ineq);
1586 struct isl_basic_set *isl_basic_set_extend_constraints(
1587 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1589 return (struct isl_basic_set *)
1590 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1591 n_eq, n_ineq);
1594 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1596 return (struct isl_basic_set *)
1597 isl_basic_map_cow((struct isl_basic_map *)bset);
1600 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1602 if (!bmap)
1603 return NULL;
1605 if (bmap->ref > 1) {
1606 bmap->ref--;
1607 bmap = isl_basic_map_dup(bmap);
1609 if (bmap)
1610 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1611 return bmap;
1614 struct isl_set *isl_set_cow(struct isl_set *set)
1616 if (!set)
1617 return NULL;
1619 if (set->ref == 1)
1620 return set;
1621 set->ref--;
1622 return isl_set_dup(set);
1625 struct isl_map *isl_map_cow(struct isl_map *map)
1627 if (!map)
1628 return NULL;
1630 if (map->ref == 1)
1631 return map;
1632 map->ref--;
1633 return isl_map_dup(map);
1636 static void swap_vars(struct isl_blk blk, isl_int *a,
1637 unsigned a_len, unsigned b_len)
1639 isl_seq_cpy(blk.data, a+a_len, b_len);
1640 isl_seq_cpy(blk.data+b_len, a, a_len);
1641 isl_seq_cpy(a, blk.data, b_len+a_len);
1644 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1645 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1647 int i;
1648 struct isl_blk blk;
1650 if (!bmap)
1651 goto error;
1653 isl_assert(bmap->ctx,
1654 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1656 if (n1 == 0 || n2 == 0)
1657 return bmap;
1659 bmap = isl_basic_map_cow(bmap);
1660 if (!bmap)
1661 return NULL;
1663 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1664 if (isl_blk_is_error(blk))
1665 goto error;
1667 for (i = 0; i < bmap->n_eq; ++i)
1668 swap_vars(blk,
1669 bmap->eq[i] + pos, n1, n2);
1671 for (i = 0; i < bmap->n_ineq; ++i)
1672 swap_vars(blk,
1673 bmap->ineq[i] + pos, n1, n2);
1675 for (i = 0; i < bmap->n_div; ++i)
1676 swap_vars(blk,
1677 bmap->div[i]+1 + pos, n1, n2);
1679 isl_blk_free(bmap->ctx, blk);
1681 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1682 bmap = isl_basic_map_gauss(bmap, NULL);
1683 return isl_basic_map_finalize(bmap);
1684 error:
1685 isl_basic_map_free(bmap);
1686 return NULL;
1689 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1690 __isl_take isl_basic_set *bset, unsigned n)
1692 unsigned dim;
1693 unsigned nparam;
1695 if (!bset)
1696 return NULL;
1698 nparam = isl_basic_set_n_param(bset);
1699 dim = isl_basic_set_n_dim(bset);
1700 isl_assert(bset->ctx, n <= dim, goto error);
1702 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1703 error:
1704 isl_basic_set_free(bset);
1705 return NULL;
1708 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1710 int i = 0;
1711 unsigned total;
1712 if (!bmap)
1713 goto error;
1714 total = isl_basic_map_total_dim(bmap);
1715 isl_basic_map_free_div(bmap, bmap->n_div);
1716 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1717 if (bmap->n_eq > 0)
1718 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1719 else {
1720 i = isl_basic_map_alloc_equality(bmap);
1721 if (i < 0)
1722 goto error;
1724 isl_int_set_si(bmap->eq[i][0], 1);
1725 isl_seq_clr(bmap->eq[i]+1, total);
1726 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1727 isl_vec_free(bmap->sample);
1728 bmap->sample = NULL;
1729 return isl_basic_map_finalize(bmap);
1730 error:
1731 isl_basic_map_free(bmap);
1732 return NULL;
1735 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1737 return (struct isl_basic_set *)
1738 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1741 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1742 * of "bmap").
1744 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1746 isl_int *t = bmap->div[a];
1747 bmap->div[a] = bmap->div[b];
1748 bmap->div[b] = t;
1751 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1752 * div definitions accordingly.
1754 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1756 int i;
1757 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1759 swap_div(bmap, a, b);
1761 for (i = 0; i < bmap->n_eq; ++i)
1762 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1764 for (i = 0; i < bmap->n_ineq; ++i)
1765 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1767 for (i = 0; i < bmap->n_div; ++i)
1768 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1769 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1772 /* Eliminate the specified n dimensions starting at first from the
1773 * constraints, without removing the dimensions from the space.
1774 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1776 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1777 enum isl_dim_type type, unsigned first, unsigned n)
1779 int i;
1781 if (!map)
1782 return NULL;
1783 if (n == 0)
1784 return map;
1786 if (first + n > isl_map_dim(map, type) || first + n < first)
1787 isl_die(map->ctx, isl_error_invalid,
1788 "index out of bounds", goto error);
1790 map = isl_map_cow(map);
1791 if (!map)
1792 return NULL;
1794 for (i = 0; i < map->n; ++i) {
1795 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1796 if (!map->p[i])
1797 goto error;
1799 return map;
1800 error:
1801 isl_map_free(map);
1802 return NULL;
1805 /* Eliminate the specified n dimensions starting at first from the
1806 * constraints, without removing the dimensions from the space.
1807 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1809 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1810 enum isl_dim_type type, unsigned first, unsigned n)
1812 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1815 /* Eliminate the specified n dimensions starting at first from the
1816 * constraints, without removing the dimensions from the space.
1817 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1819 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1820 unsigned first, unsigned n)
1822 return isl_set_eliminate(set, isl_dim_set, first, n);
1825 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1826 __isl_take isl_basic_map *bmap)
1828 if (!bmap)
1829 return NULL;
1830 bmap = isl_basic_map_eliminate_vars(bmap,
1831 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1832 if (!bmap)
1833 return NULL;
1834 bmap->n_div = 0;
1835 return isl_basic_map_finalize(bmap);
1838 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1839 __isl_take isl_basic_set *bset)
1841 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1842 (struct isl_basic_map *)bset);
1845 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1847 int i;
1849 if (!map)
1850 return NULL;
1851 if (map->n == 0)
1852 return map;
1854 map = isl_map_cow(map);
1855 if (!map)
1856 return NULL;
1858 for (i = 0; i < map->n; ++i) {
1859 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1860 if (!map->p[i])
1861 goto error;
1863 return map;
1864 error:
1865 isl_map_free(map);
1866 return NULL;
1869 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1871 return isl_map_remove_divs(set);
1874 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1875 enum isl_dim_type type, unsigned first, unsigned n)
1877 if (!bmap)
1878 return NULL;
1879 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1880 goto error);
1881 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1882 return bmap;
1883 bmap = isl_basic_map_eliminate_vars(bmap,
1884 isl_basic_map_offset(bmap, type) - 1 + first, n);
1885 if (!bmap)
1886 return bmap;
1887 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1888 return bmap;
1889 bmap = isl_basic_map_drop(bmap, type, first, n);
1890 return bmap;
1891 error:
1892 isl_basic_map_free(bmap);
1893 return NULL;
1896 /* Return true if the definition of the given div (recursively) involves
1897 * any of the given variables.
1899 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1900 unsigned first, unsigned n)
1902 int i;
1903 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1905 if (isl_int_is_zero(bmap->div[div][0]))
1906 return 0;
1907 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1908 return 1;
1910 for (i = bmap->n_div - 1; i >= 0; --i) {
1911 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1912 continue;
1913 if (div_involves_vars(bmap, i, first, n))
1914 return 1;
1917 return 0;
1920 /* Try and add a lower and/or upper bound on "div" to "bmap"
1921 * based on inequality "i".
1922 * "total" is the total number of variables (excluding the divs).
1923 * "v" is a temporary object that can be used during the calculations.
1924 * If "lb" is set, then a lower bound should be constructed.
1925 * If "ub" is set, then an upper bound should be constructed.
1927 * The calling function has already checked that the inequality does not
1928 * reference "div", but we still need to check that the inequality is
1929 * of the right form. We'll consider the case where we want to construct
1930 * a lower bound. The construction of upper bounds is similar.
1932 * Let "div" be of the form
1934 * q = floor((a + f(x))/d)
1936 * We essentially check if constraint "i" is of the form
1938 * b + f(x) >= 0
1940 * so that we can use it to derive a lower bound on "div".
1941 * However, we allow a slightly more general form
1943 * b + g(x) >= 0
1945 * with the condition that the coefficients of g(x) - f(x) are all
1946 * divisible by d.
1947 * Rewriting this constraint as
1949 * 0 >= -b - g(x)
1951 * adding a + f(x) to both sides and dividing by d, we obtain
1953 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1955 * Taking the floor on both sides, we obtain
1957 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1959 * or
1961 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1963 * In the case of an upper bound, we construct the constraint
1965 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1968 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1969 __isl_take isl_basic_map *bmap, int div, int i,
1970 unsigned total, isl_int v, int lb, int ub)
1972 int j;
1974 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1975 if (lb) {
1976 isl_int_sub(v, bmap->ineq[i][1 + j],
1977 bmap->div[div][1 + 1 + j]);
1978 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1980 if (ub) {
1981 isl_int_add(v, bmap->ineq[i][1 + j],
1982 bmap->div[div][1 + 1 + j]);
1983 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1986 if (!lb && !ub)
1987 return bmap;
1989 bmap = isl_basic_map_cow(bmap);
1990 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1991 if (lb) {
1992 int k = isl_basic_map_alloc_inequality(bmap);
1993 if (k < 0)
1994 goto error;
1995 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1996 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
1997 bmap->div[div][1 + j]);
1998 isl_int_cdiv_q(bmap->ineq[k][j],
1999 bmap->ineq[k][j], bmap->div[div][0]);
2001 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2003 if (ub) {
2004 int k = isl_basic_map_alloc_inequality(bmap);
2005 if (k < 0)
2006 goto error;
2007 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2008 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2009 bmap->div[div][1 + j]);
2010 isl_int_fdiv_q(bmap->ineq[k][j],
2011 bmap->ineq[k][j], bmap->div[div][0]);
2013 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2016 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2017 return bmap;
2018 error:
2019 isl_basic_map_free(bmap);
2020 return NULL;
2023 /* This function is called right before "div" is eliminated from "bmap"
2024 * using Fourier-Motzkin.
2025 * Look through the constraints of "bmap" for constraints on the argument
2026 * of the integer division and use them to construct constraints on the
2027 * integer division itself. These constraints can then be combined
2028 * during the Fourier-Motzkin elimination.
2029 * Note that it is only useful to introduce lower bounds on "div"
2030 * if "bmap" already contains upper bounds on "div" as the newly
2031 * introduce lower bounds can then be combined with the pre-existing
2032 * upper bounds. Similarly for upper bounds.
2033 * We therefore first check if "bmap" contains any lower and/or upper bounds
2034 * on "div".
2036 * It is interesting to note that the introduction of these constraints
2037 * can indeed lead to more accurate results, even when compared to
2038 * deriving constraints on the argument of "div" from constraints on "div".
2039 * Consider, for example, the set
2041 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2043 * The second constraint can be rewritten as
2045 * 2 * [(-i-2j+3)/4] + k >= 0
2047 * from which we can derive
2049 * -i - 2j + 3 >= -2k
2051 * or
2053 * i + 2j <= 3 + 2k
2055 * Combined with the first constraint, we obtain
2057 * -3 <= 3 + 2k or k >= -3
2059 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2060 * the first constraint, we obtain
2062 * [(i + 2j)/4] >= [-3/4] = -1
2064 * Combining this constraint with the second constraint, we obtain
2066 * k >= -2
2068 static __isl_give isl_basic_map *insert_bounds_on_div(
2069 __isl_take isl_basic_map *bmap, int div)
2071 int i;
2072 int check_lb, check_ub;
2073 isl_int v;
2074 unsigned total;
2076 if (!bmap)
2077 return NULL;
2079 if (isl_int_is_zero(bmap->div[div][0]))
2080 return bmap;
2082 total = isl_space_dim(bmap->dim, isl_dim_all);
2084 check_lb = 0;
2085 check_ub = 0;
2086 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2087 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2088 if (s > 0)
2089 check_ub = 1;
2090 if (s < 0)
2091 check_lb = 1;
2094 if (!check_lb && !check_ub)
2095 return bmap;
2097 isl_int_init(v);
2099 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2100 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2101 continue;
2103 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2104 check_lb, check_ub);
2107 isl_int_clear(v);
2109 return bmap;
2112 /* Remove all divs (recursively) involving any of the given dimensions
2113 * in their definitions.
2115 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2116 __isl_take isl_basic_map *bmap,
2117 enum isl_dim_type type, unsigned first, unsigned n)
2119 int i;
2121 if (!bmap)
2122 return NULL;
2123 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2124 goto error);
2125 first += isl_basic_map_offset(bmap, type);
2127 for (i = bmap->n_div - 1; i >= 0; --i) {
2128 if (!div_involves_vars(bmap, i, first, n))
2129 continue;
2130 bmap = insert_bounds_on_div(bmap, i);
2131 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2132 if (!bmap)
2133 return NULL;
2134 i = bmap->n_div;
2137 return bmap;
2138 error:
2139 isl_basic_map_free(bmap);
2140 return NULL;
2143 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2144 __isl_take isl_basic_set *bset,
2145 enum isl_dim_type type, unsigned first, unsigned n)
2147 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2150 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2151 enum isl_dim_type type, unsigned first, unsigned n)
2153 int i;
2155 if (!map)
2156 return NULL;
2157 if (map->n == 0)
2158 return map;
2160 map = isl_map_cow(map);
2161 if (!map)
2162 return NULL;
2164 for (i = 0; i < map->n; ++i) {
2165 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2166 type, first, n);
2167 if (!map->p[i])
2168 goto error;
2170 return map;
2171 error:
2172 isl_map_free(map);
2173 return NULL;
2176 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2177 enum isl_dim_type type, unsigned first, unsigned n)
2179 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2180 type, first, n);
2183 /* Does the desciption of "bmap" depend on the specified dimensions?
2184 * We also check whether the dimensions appear in any of the div definitions.
2185 * In principle there is no need for this check. If the dimensions appear
2186 * in a div definition, they also appear in the defining constraints of that
2187 * div.
2189 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2190 enum isl_dim_type type, unsigned first, unsigned n)
2192 int i;
2194 if (!bmap)
2195 return -1;
2197 if (first + n > isl_basic_map_dim(bmap, type))
2198 isl_die(bmap->ctx, isl_error_invalid,
2199 "index out of bounds", return -1);
2201 first += isl_basic_map_offset(bmap, type);
2202 for (i = 0; i < bmap->n_eq; ++i)
2203 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2204 return 1;
2205 for (i = 0; i < bmap->n_ineq; ++i)
2206 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2207 return 1;
2208 for (i = 0; i < bmap->n_div; ++i) {
2209 if (isl_int_is_zero(bmap->div[i][0]))
2210 continue;
2211 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2212 return 1;
2215 return 0;
2218 int isl_map_involves_dims(__isl_keep isl_map *map,
2219 enum isl_dim_type type, unsigned first, unsigned n)
2221 int i;
2223 if (!map)
2224 return -1;
2226 if (first + n > isl_map_dim(map, type))
2227 isl_die(map->ctx, isl_error_invalid,
2228 "index out of bounds", return -1);
2230 for (i = 0; i < map->n; ++i) {
2231 int involves = isl_basic_map_involves_dims(map->p[i],
2232 type, first, n);
2233 if (involves < 0 || involves)
2234 return involves;
2237 return 0;
2240 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2241 enum isl_dim_type type, unsigned first, unsigned n)
2243 return isl_basic_map_involves_dims(bset, type, first, n);
2246 int isl_set_involves_dims(__isl_keep isl_set *set,
2247 enum isl_dim_type type, unsigned first, unsigned n)
2249 return isl_map_involves_dims(set, type, first, n);
2252 /* Return true if the definition of the given div is unknown or depends
2253 * on unknown divs.
2255 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2257 int i;
2258 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2260 if (isl_int_is_zero(bmap->div[div][0]))
2261 return 1;
2263 for (i = bmap->n_div - 1; i >= 0; --i) {
2264 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2265 continue;
2266 if (div_is_unknown(bmap, i))
2267 return 1;
2270 return 0;
2273 /* Remove all divs that are unknown or defined in terms of unknown divs.
2275 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2276 __isl_take isl_basic_map *bmap)
2278 int i;
2280 if (!bmap)
2281 return NULL;
2283 for (i = bmap->n_div - 1; i >= 0; --i) {
2284 if (!div_is_unknown(bmap, i))
2285 continue;
2286 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2287 if (!bmap)
2288 return NULL;
2289 i = bmap->n_div;
2292 return bmap;
2295 /* Remove all divs that are unknown or defined in terms of unknown divs.
2297 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2298 __isl_take isl_basic_set *bset)
2300 return isl_basic_map_remove_unknown_divs(bset);
2303 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2305 int i;
2307 if (!map)
2308 return NULL;
2309 if (map->n == 0)
2310 return map;
2312 map = isl_map_cow(map);
2313 if (!map)
2314 return NULL;
2316 for (i = 0; i < map->n; ++i) {
2317 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2318 if (!map->p[i])
2319 goto error;
2321 return map;
2322 error:
2323 isl_map_free(map);
2324 return NULL;
2327 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2329 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2332 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2333 __isl_take isl_basic_set *bset,
2334 enum isl_dim_type type, unsigned first, unsigned n)
2336 return (isl_basic_set *)
2337 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2340 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2341 enum isl_dim_type type, unsigned first, unsigned n)
2343 int i;
2345 if (n == 0)
2346 return map;
2348 map = isl_map_cow(map);
2349 if (!map)
2350 return NULL;
2351 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2353 for (i = 0; i < map->n; ++i) {
2354 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2355 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2356 if (!map->p[i])
2357 goto error;
2359 map = isl_map_drop(map, type, first, n);
2360 return map;
2361 error:
2362 isl_map_free(map);
2363 return NULL;
2366 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2367 enum isl_dim_type type, unsigned first, unsigned n)
2369 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2372 /* Project out n inputs starting at first using Fourier-Motzkin */
2373 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2374 unsigned first, unsigned n)
2376 return isl_map_remove_dims(map, isl_dim_in, first, n);
2379 static void dump_term(struct isl_basic_map *bmap,
2380 isl_int c, int pos, FILE *out)
2382 const char *name;
2383 unsigned in = isl_basic_map_n_in(bmap);
2384 unsigned dim = in + isl_basic_map_n_out(bmap);
2385 unsigned nparam = isl_basic_map_n_param(bmap);
2386 if (!pos)
2387 isl_int_print(out, c, 0);
2388 else {
2389 if (!isl_int_is_one(c))
2390 isl_int_print(out, c, 0);
2391 if (pos < 1 + nparam) {
2392 name = isl_space_get_dim_name(bmap->dim,
2393 isl_dim_param, pos - 1);
2394 if (name)
2395 fprintf(out, "%s", name);
2396 else
2397 fprintf(out, "p%d", pos - 1);
2398 } else if (pos < 1 + nparam + in)
2399 fprintf(out, "i%d", pos - 1 - nparam);
2400 else if (pos < 1 + nparam + dim)
2401 fprintf(out, "o%d", pos - 1 - nparam - in);
2402 else
2403 fprintf(out, "e%d", pos - 1 - nparam - dim);
2407 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2408 int sign, FILE *out)
2410 int i;
2411 int first;
2412 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2413 isl_int v;
2415 isl_int_init(v);
2416 for (i = 0, first = 1; i < len; ++i) {
2417 if (isl_int_sgn(c[i]) * sign <= 0)
2418 continue;
2419 if (!first)
2420 fprintf(out, " + ");
2421 first = 0;
2422 isl_int_abs(v, c[i]);
2423 dump_term(bmap, v, i, out);
2425 isl_int_clear(v);
2426 if (first)
2427 fprintf(out, "0");
2430 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2431 const char *op, FILE *out, int indent)
2433 int i;
2435 fprintf(out, "%*s", indent, "");
2437 dump_constraint_sign(bmap, c, 1, out);
2438 fprintf(out, " %s ", op);
2439 dump_constraint_sign(bmap, c, -1, out);
2441 fprintf(out, "\n");
2443 for (i = bmap->n_div; i < bmap->extra; ++i) {
2444 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2445 continue;
2446 fprintf(out, "%*s", indent, "");
2447 fprintf(out, "ERROR: unused div coefficient not zero\n");
2448 abort();
2452 static void dump_constraints(struct isl_basic_map *bmap,
2453 isl_int **c, unsigned n,
2454 const char *op, FILE *out, int indent)
2456 int i;
2458 for (i = 0; i < n; ++i)
2459 dump_constraint(bmap, c[i], op, out, indent);
2462 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2464 int j;
2465 int first = 1;
2466 unsigned total = isl_basic_map_total_dim(bmap);
2468 for (j = 0; j < 1 + total; ++j) {
2469 if (isl_int_is_zero(exp[j]))
2470 continue;
2471 if (!first && isl_int_is_pos(exp[j]))
2472 fprintf(out, "+");
2473 dump_term(bmap, exp[j], j, out);
2474 first = 0;
2478 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2480 int i;
2482 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2483 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2485 for (i = 0; i < bmap->n_div; ++i) {
2486 fprintf(out, "%*s", indent, "");
2487 fprintf(out, "e%d = [(", i);
2488 dump_affine(bmap, bmap->div[i]+1, out);
2489 fprintf(out, ")/");
2490 isl_int_print(out, bmap->div[i][0], 0);
2491 fprintf(out, "]\n");
2495 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2496 FILE *out, int indent)
2498 if (!bset) {
2499 fprintf(out, "null basic set\n");
2500 return;
2503 fprintf(out, "%*s", indent, "");
2504 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2505 bset->ref, bset->dim->nparam, bset->dim->n_out,
2506 bset->extra, bset->flags);
2507 dump((struct isl_basic_map *)bset, out, indent);
2510 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2511 FILE *out, int indent)
2513 if (!bmap) {
2514 fprintf(out, "null basic map\n");
2515 return;
2518 fprintf(out, "%*s", indent, "");
2519 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2520 "flags: %x, n_name: %d\n",
2521 bmap->ref,
2522 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2523 bmap->extra, bmap->flags, bmap->dim->n_id);
2524 dump(bmap, out, indent);
2527 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2529 unsigned total;
2530 if (!bmap)
2531 return -1;
2532 total = isl_basic_map_total_dim(bmap);
2533 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2534 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2535 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2536 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2537 return 0;
2540 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2541 unsigned flags)
2543 struct isl_set *set;
2545 if (!dim)
2546 return NULL;
2547 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2548 isl_assert(dim->ctx, n >= 0, goto error);
2549 set = isl_alloc(dim->ctx, struct isl_set,
2550 sizeof(struct isl_set) +
2551 (n - 1) * sizeof(struct isl_basic_set *));
2552 if (!set)
2553 goto error;
2555 set->ctx = dim->ctx;
2556 isl_ctx_ref(set->ctx);
2557 set->ref = 1;
2558 set->size = n;
2559 set->n = 0;
2560 set->dim = dim;
2561 set->flags = flags;
2562 return set;
2563 error:
2564 isl_space_free(dim);
2565 return NULL;
2568 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2569 unsigned nparam, unsigned dim, int n, unsigned flags)
2571 struct isl_set *set;
2572 isl_space *dims;
2574 dims = isl_space_alloc(ctx, nparam, 0, dim);
2575 if (!dims)
2576 return NULL;
2578 set = isl_set_alloc_space(dims, n, flags);
2579 return set;
2582 /* Make sure "map" has room for at least "n" more basic maps.
2584 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2586 int i;
2587 struct isl_map *grown = NULL;
2589 if (!map)
2590 return NULL;
2591 isl_assert(map->ctx, n >= 0, goto error);
2592 if (map->n + n <= map->size)
2593 return map;
2594 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2595 if (!grown)
2596 goto error;
2597 for (i = 0; i < map->n; ++i) {
2598 grown->p[i] = isl_basic_map_copy(map->p[i]);
2599 if (!grown->p[i])
2600 goto error;
2601 grown->n++;
2603 isl_map_free(map);
2604 return grown;
2605 error:
2606 isl_map_free(grown);
2607 isl_map_free(map);
2608 return NULL;
2611 /* Make sure "set" has room for at least "n" more basic sets.
2613 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2615 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2618 struct isl_set *isl_set_dup(struct isl_set *set)
2620 int i;
2621 struct isl_set *dup;
2623 if (!set)
2624 return NULL;
2626 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2627 if (!dup)
2628 return NULL;
2629 for (i = 0; i < set->n; ++i)
2630 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2631 return dup;
2634 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2636 return isl_map_from_basic_map(bset);
2639 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2641 struct isl_map *map;
2643 if (!bmap)
2644 return NULL;
2646 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2647 return isl_map_add_basic_map(map, bmap);
2650 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2651 __isl_take isl_basic_set *bset)
2653 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2654 (struct isl_basic_map *)bset);
2657 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2659 int i;
2661 if (!set)
2662 return NULL;
2664 if (--set->ref > 0)
2665 return NULL;
2667 isl_ctx_deref(set->ctx);
2668 for (i = 0; i < set->n; ++i)
2669 isl_basic_set_free(set->p[i]);
2670 isl_space_free(set->dim);
2671 free(set);
2673 return NULL;
2676 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2678 int i;
2680 if (!set) {
2681 fprintf(out, "null set\n");
2682 return;
2685 fprintf(out, "%*s", indent, "");
2686 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2687 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2688 set->flags);
2689 for (i = 0; i < set->n; ++i) {
2690 fprintf(out, "%*s", indent, "");
2691 fprintf(out, "basic set %d:\n", i);
2692 isl_basic_set_print_internal(set->p[i], out, indent+4);
2696 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2698 int i;
2700 if (!map) {
2701 fprintf(out, "null map\n");
2702 return;
2705 fprintf(out, "%*s", indent, "");
2706 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2707 "flags: %x, n_name: %d\n",
2708 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2709 map->dim->n_out, map->flags, map->dim->n_id);
2710 for (i = 0; i < map->n; ++i) {
2711 fprintf(out, "%*s", indent, "");
2712 fprintf(out, "basic map %d:\n", i);
2713 isl_basic_map_print_internal(map->p[i], out, indent+4);
2717 struct isl_basic_map *isl_basic_map_intersect_domain(
2718 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2720 struct isl_basic_map *bmap_domain;
2722 if (!bmap || !bset)
2723 goto error;
2725 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2726 bset->dim, isl_dim_param), goto error);
2728 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2729 isl_assert(bset->ctx,
2730 isl_basic_map_compatible_domain(bmap, bset), goto error);
2732 bmap = isl_basic_map_cow(bmap);
2733 if (!bmap)
2734 goto error;
2735 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2736 bset->n_div, bset->n_eq, bset->n_ineq);
2737 bmap_domain = isl_basic_map_from_domain(bset);
2738 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2740 bmap = isl_basic_map_simplify(bmap);
2741 return isl_basic_map_finalize(bmap);
2742 error:
2743 isl_basic_map_free(bmap);
2744 isl_basic_set_free(bset);
2745 return NULL;
2748 struct isl_basic_map *isl_basic_map_intersect_range(
2749 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2751 struct isl_basic_map *bmap_range;
2753 if (!bmap || !bset)
2754 goto error;
2756 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2757 bset->dim, isl_dim_param), goto error);
2759 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2760 isl_assert(bset->ctx,
2761 isl_basic_map_compatible_range(bmap, bset), goto error);
2763 if (isl_basic_set_is_universe(bset)) {
2764 isl_basic_set_free(bset);
2765 return bmap;
2768 bmap = isl_basic_map_cow(bmap);
2769 if (!bmap)
2770 goto error;
2771 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2772 bset->n_div, bset->n_eq, bset->n_ineq);
2773 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2774 bmap = add_constraints(bmap, bmap_range, 0, 0);
2776 bmap = isl_basic_map_simplify(bmap);
2777 return isl_basic_map_finalize(bmap);
2778 error:
2779 isl_basic_map_free(bmap);
2780 isl_basic_set_free(bset);
2781 return NULL;
2784 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2786 int i;
2787 unsigned total;
2788 isl_int s;
2790 if (!bmap || !vec)
2791 return -1;
2793 total = 1 + isl_basic_map_total_dim(bmap);
2794 if (total != vec->size)
2795 return -1;
2797 isl_int_init(s);
2799 for (i = 0; i < bmap->n_eq; ++i) {
2800 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2801 if (!isl_int_is_zero(s)) {
2802 isl_int_clear(s);
2803 return 0;
2807 for (i = 0; i < bmap->n_ineq; ++i) {
2808 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2809 if (isl_int_is_neg(s)) {
2810 isl_int_clear(s);
2811 return 0;
2815 isl_int_clear(s);
2817 return 1;
2820 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2822 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2825 struct isl_basic_map *isl_basic_map_intersect(
2826 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2828 struct isl_vec *sample = NULL;
2830 if (!bmap1 || !bmap2)
2831 goto error;
2833 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2834 bmap2->dim, isl_dim_param), goto error);
2835 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2836 isl_space_dim(bmap1->dim, isl_dim_param) &&
2837 isl_space_dim(bmap2->dim, isl_dim_all) !=
2838 isl_space_dim(bmap2->dim, isl_dim_param))
2839 return isl_basic_map_intersect(bmap2, bmap1);
2841 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2842 isl_space_dim(bmap2->dim, isl_dim_param))
2843 isl_assert(bmap1->ctx,
2844 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2846 if (bmap1->sample &&
2847 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2848 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2849 sample = isl_vec_copy(bmap1->sample);
2850 else if (bmap2->sample &&
2851 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2852 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2853 sample = isl_vec_copy(bmap2->sample);
2855 bmap1 = isl_basic_map_cow(bmap1);
2856 if (!bmap1)
2857 goto error;
2858 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2859 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2860 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2862 if (!bmap1)
2863 isl_vec_free(sample);
2864 else if (sample) {
2865 isl_vec_free(bmap1->sample);
2866 bmap1->sample = sample;
2869 bmap1 = isl_basic_map_simplify(bmap1);
2870 return isl_basic_map_finalize(bmap1);
2871 error:
2872 if (sample)
2873 isl_vec_free(sample);
2874 isl_basic_map_free(bmap1);
2875 isl_basic_map_free(bmap2);
2876 return NULL;
2879 struct isl_basic_set *isl_basic_set_intersect(
2880 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2882 return (struct isl_basic_set *)
2883 isl_basic_map_intersect(
2884 (struct isl_basic_map *)bset1,
2885 (struct isl_basic_map *)bset2);
2888 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2889 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2891 return isl_basic_set_intersect(bset1, bset2);
2894 /* Special case of isl_map_intersect, where both map1 and map2
2895 * are convex, without any divs and such that either map1 or map2
2896 * contains a single constraint. This constraint is then simply
2897 * added to the other map.
2899 static __isl_give isl_map *map_intersect_add_constraint(
2900 __isl_take isl_map *map1, __isl_take isl_map *map2)
2902 isl_assert(map1->ctx, map1->n == 1, goto error);
2903 isl_assert(map2->ctx, map1->n == 1, goto error);
2904 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2905 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2907 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2908 return isl_map_intersect(map2, map1);
2910 isl_assert(map2->ctx,
2911 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2913 map1 = isl_map_cow(map1);
2914 if (!map1)
2915 goto error;
2916 if (isl_map_plain_is_empty(map1)) {
2917 isl_map_free(map2);
2918 return map1;
2920 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2921 if (map2->p[0]->n_eq == 1)
2922 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2923 else
2924 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2925 map2->p[0]->ineq[0]);
2927 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2928 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2929 if (!map1->p[0])
2930 goto error;
2932 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2933 isl_basic_map_free(map1->p[0]);
2934 map1->n = 0;
2937 isl_map_free(map2);
2939 return map1;
2940 error:
2941 isl_map_free(map1);
2942 isl_map_free(map2);
2943 return NULL;
2946 /* map2 may be either a parameter domain or a map living in the same
2947 * space as map1.
2949 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2950 __isl_take isl_map *map2)
2952 unsigned flags = 0;
2953 isl_map *result;
2954 int i, j;
2956 if (!map1 || !map2)
2957 goto error;
2959 if ((isl_map_plain_is_empty(map1) ||
2960 isl_map_plain_is_universe(map2)) &&
2961 isl_space_is_equal(map1->dim, map2->dim)) {
2962 isl_map_free(map2);
2963 return map1;
2965 if ((isl_map_plain_is_empty(map2) ||
2966 isl_map_plain_is_universe(map1)) &&
2967 isl_space_is_equal(map1->dim, map2->dim)) {
2968 isl_map_free(map1);
2969 return map2;
2972 if (map1->n == 1 && map2->n == 1 &&
2973 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2974 isl_space_is_equal(map1->dim, map2->dim) &&
2975 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2976 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2977 return map_intersect_add_constraint(map1, map2);
2979 if (isl_space_dim(map2->dim, isl_dim_all) !=
2980 isl_space_dim(map2->dim, isl_dim_param))
2981 isl_assert(map1->ctx,
2982 isl_space_is_equal(map1->dim, map2->dim), goto error);
2984 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2985 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2986 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2988 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2989 map1->n * map2->n, flags);
2990 if (!result)
2991 goto error;
2992 for (i = 0; i < map1->n; ++i)
2993 for (j = 0; j < map2->n; ++j) {
2994 struct isl_basic_map *part;
2995 part = isl_basic_map_intersect(
2996 isl_basic_map_copy(map1->p[i]),
2997 isl_basic_map_copy(map2->p[j]));
2998 if (isl_basic_map_is_empty(part) < 0)
2999 part = isl_basic_map_free(part);
3000 result = isl_map_add_basic_map(result, part);
3001 if (!result)
3002 goto error;
3004 isl_map_free(map1);
3005 isl_map_free(map2);
3006 return result;
3007 error:
3008 isl_map_free(map1);
3009 isl_map_free(map2);
3010 return NULL;
3013 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3014 __isl_take isl_map *map2)
3016 if (!map1 || !map2)
3017 goto error;
3018 if (!isl_space_is_equal(map1->dim, map2->dim))
3019 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3020 "spaces don't match", goto error);
3021 return map_intersect_internal(map1, map2);
3022 error:
3023 isl_map_free(map1);
3024 isl_map_free(map2);
3025 return NULL;
3028 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3029 __isl_take isl_map *map2)
3031 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3034 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3036 return (struct isl_set *)
3037 isl_map_intersect((struct isl_map *)set1,
3038 (struct isl_map *)set2);
3041 /* map_intersect_internal accepts intersections
3042 * with parameter domains, so we can just call that function.
3044 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3045 __isl_take isl_set *params)
3047 return map_intersect_internal(map, params);
3050 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3051 __isl_take isl_map *map2)
3053 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3056 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3057 __isl_take isl_set *params)
3059 return isl_map_intersect_params(set, params);
3062 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3064 isl_space *dim;
3065 struct isl_basic_set *bset;
3066 unsigned in;
3068 if (!bmap)
3069 return NULL;
3070 bmap = isl_basic_map_cow(bmap);
3071 if (!bmap)
3072 return NULL;
3073 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3074 in = isl_basic_map_n_in(bmap);
3075 bset = isl_basic_set_from_basic_map(bmap);
3076 bset = isl_basic_set_swap_vars(bset, in);
3077 return isl_basic_map_from_basic_set(bset, dim);
3080 static __isl_give isl_basic_map *basic_map_space_reset(
3081 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3083 isl_space *space;
3085 if (!bmap)
3086 return NULL;
3087 if (!isl_space_is_named_or_nested(bmap->dim, type))
3088 return bmap;
3090 space = isl_basic_map_get_space(bmap);
3091 space = isl_space_reset(space, type);
3092 bmap = isl_basic_map_reset_space(bmap, space);
3093 return bmap;
3096 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3097 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3098 unsigned pos, unsigned n)
3100 isl_space *res_dim;
3101 struct isl_basic_map *res;
3102 struct isl_dim_map *dim_map;
3103 unsigned total, off;
3104 enum isl_dim_type t;
3106 if (n == 0)
3107 return basic_map_space_reset(bmap, type);
3109 if (!bmap)
3110 return NULL;
3112 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3114 total = isl_basic_map_total_dim(bmap) + n;
3115 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3116 off = 0;
3117 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3118 if (t != type) {
3119 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3120 } else {
3121 unsigned size = isl_basic_map_dim(bmap, t);
3122 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3123 0, pos, off);
3124 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3125 pos, size - pos, off + pos + n);
3127 off += isl_space_dim(res_dim, t);
3129 isl_dim_map_div(dim_map, bmap, off);
3131 res = isl_basic_map_alloc_space(res_dim,
3132 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3133 if (isl_basic_map_is_rational(bmap))
3134 res = isl_basic_map_set_rational(res);
3135 if (isl_basic_map_plain_is_empty(bmap)) {
3136 isl_basic_map_free(bmap);
3137 free(dim_map);
3138 return isl_basic_map_set_to_empty(res);
3140 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3141 return isl_basic_map_finalize(res);
3144 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3145 __isl_take isl_basic_set *bset,
3146 enum isl_dim_type type, unsigned pos, unsigned n)
3148 return isl_basic_map_insert_dims(bset, type, pos, n);
3151 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3152 enum isl_dim_type type, unsigned n)
3154 if (!bmap)
3155 return NULL;
3156 return isl_basic_map_insert_dims(bmap, type,
3157 isl_basic_map_dim(bmap, type), n);
3160 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3161 enum isl_dim_type type, unsigned n)
3163 if (!bset)
3164 return NULL;
3165 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3166 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3167 error:
3168 isl_basic_set_free(bset);
3169 return NULL;
3172 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3173 enum isl_dim_type type)
3175 isl_space *space;
3177 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3178 return map;
3180 space = isl_map_get_space(map);
3181 space = isl_space_reset(space, type);
3182 map = isl_map_reset_space(map, space);
3183 return map;
3186 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3187 enum isl_dim_type type, unsigned pos, unsigned n)
3189 int i;
3191 if (n == 0)
3192 return map_space_reset(map, type);
3194 map = isl_map_cow(map);
3195 if (!map)
3196 return NULL;
3198 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3199 if (!map->dim)
3200 goto error;
3202 for (i = 0; i < map->n; ++i) {
3203 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3204 if (!map->p[i])
3205 goto error;
3208 return map;
3209 error:
3210 isl_map_free(map);
3211 return NULL;
3214 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3215 enum isl_dim_type type, unsigned pos, unsigned n)
3217 return isl_map_insert_dims(set, type, pos, n);
3220 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3221 enum isl_dim_type type, unsigned n)
3223 if (!map)
3224 return NULL;
3225 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3228 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3229 enum isl_dim_type type, unsigned n)
3231 if (!set)
3232 return NULL;
3233 isl_assert(set->ctx, type != isl_dim_in, goto error);
3234 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3235 error:
3236 isl_set_free(set);
3237 return NULL;
3240 __isl_give isl_basic_map *isl_basic_map_move_dims(
3241 __isl_take isl_basic_map *bmap,
3242 enum isl_dim_type dst_type, unsigned dst_pos,
3243 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3245 struct isl_dim_map *dim_map;
3246 struct isl_basic_map *res;
3247 enum isl_dim_type t;
3248 unsigned total, off;
3250 if (!bmap)
3251 return NULL;
3252 if (n == 0)
3253 return bmap;
3255 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3256 goto error);
3258 if (dst_type == src_type && dst_pos == src_pos)
3259 return bmap;
3261 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3263 if (pos(bmap->dim, dst_type) + dst_pos ==
3264 pos(bmap->dim, src_type) + src_pos +
3265 ((src_type < dst_type) ? n : 0)) {
3266 bmap = isl_basic_map_cow(bmap);
3267 if (!bmap)
3268 return NULL;
3270 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3271 src_type, src_pos, n);
3272 if (!bmap->dim)
3273 goto error;
3275 bmap = isl_basic_map_finalize(bmap);
3277 return bmap;
3280 total = isl_basic_map_total_dim(bmap);
3281 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3283 off = 0;
3284 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3285 unsigned size = isl_space_dim(bmap->dim, t);
3286 if (t == dst_type) {
3287 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3288 0, dst_pos, off);
3289 off += dst_pos;
3290 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3291 src_pos, n, off);
3292 off += n;
3293 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3294 dst_pos, size - dst_pos, off);
3295 off += size - dst_pos;
3296 } else if (t == src_type) {
3297 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3298 0, src_pos, off);
3299 off += src_pos;
3300 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3301 src_pos + n, size - src_pos - n, off);
3302 off += size - src_pos - n;
3303 } else {
3304 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3305 off += size;
3308 isl_dim_map_div(dim_map, bmap, off);
3310 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3311 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3312 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3313 if (!bmap)
3314 goto error;
3316 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3317 src_type, src_pos, n);
3318 if (!bmap->dim)
3319 goto error;
3321 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3322 bmap = isl_basic_map_gauss(bmap, NULL);
3323 bmap = isl_basic_map_finalize(bmap);
3325 return bmap;
3326 error:
3327 isl_basic_map_free(bmap);
3328 return NULL;
3331 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3332 enum isl_dim_type dst_type, unsigned dst_pos,
3333 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3335 return (isl_basic_set *)isl_basic_map_move_dims(
3336 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3339 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3340 enum isl_dim_type dst_type, unsigned dst_pos,
3341 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3343 if (!set)
3344 return NULL;
3345 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3346 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3347 src_type, src_pos, n);
3348 error:
3349 isl_set_free(set);
3350 return NULL;
3353 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3354 enum isl_dim_type dst_type, unsigned dst_pos,
3355 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3357 int i;
3359 if (!map)
3360 return NULL;
3361 if (n == 0)
3362 return map;
3364 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3365 goto error);
3367 if (dst_type == src_type && dst_pos == src_pos)
3368 return map;
3370 isl_assert(map->ctx, dst_type != src_type, goto error);
3372 map = isl_map_cow(map);
3373 if (!map)
3374 return NULL;
3376 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3377 if (!map->dim)
3378 goto error;
3380 for (i = 0; i < map->n; ++i) {
3381 map->p[i] = isl_basic_map_move_dims(map->p[i],
3382 dst_type, dst_pos,
3383 src_type, src_pos, n);
3384 if (!map->p[i])
3385 goto error;
3388 return map;
3389 error:
3390 isl_map_free(map);
3391 return NULL;
3394 /* Move the specified dimensions to the last columns right before
3395 * the divs. Don't change the dimension specification of bmap.
3396 * That's the responsibility of the caller.
3398 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3399 enum isl_dim_type type, unsigned first, unsigned n)
3401 struct isl_dim_map *dim_map;
3402 struct isl_basic_map *res;
3403 enum isl_dim_type t;
3404 unsigned total, off;
3406 if (!bmap)
3407 return NULL;
3408 if (pos(bmap->dim, type) + first + n ==
3409 1 + isl_space_dim(bmap->dim, isl_dim_all))
3410 return bmap;
3412 total = isl_basic_map_total_dim(bmap);
3413 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3415 off = 0;
3416 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3417 unsigned size = isl_space_dim(bmap->dim, t);
3418 if (t == type) {
3419 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3420 0, first, off);
3421 off += first;
3422 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3423 first, n, total - bmap->n_div - n);
3424 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3425 first + n, size - (first + n), off);
3426 off += size - (first + n);
3427 } else {
3428 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3429 off += size;
3432 isl_dim_map_div(dim_map, bmap, off + n);
3434 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3435 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3436 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3437 return res;
3440 /* Insert "n" rows in the divs of "bmap".
3442 * The number of columns is not changed, which means that the last
3443 * dimensions of "bmap" are being reintepreted as the new divs.
3444 * The space of "bmap" is not adjusted, however, which means
3445 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3446 * from the space of "bmap" is the responsibility of the caller.
3448 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3449 int n)
3451 int i;
3452 size_t row_size;
3453 isl_int **new_div;
3454 isl_int *old;
3456 bmap = isl_basic_map_cow(bmap);
3457 if (!bmap)
3458 return NULL;
3460 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3461 old = bmap->block2.data;
3462 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3463 (bmap->extra + n) * (1 + row_size));
3464 if (!bmap->block2.data)
3465 return isl_basic_map_free(bmap);
3466 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3467 if (!new_div)
3468 return isl_basic_map_free(bmap);
3469 for (i = 0; i < n; ++i) {
3470 new_div[i] = bmap->block2.data +
3471 (bmap->extra + i) * (1 + row_size);
3472 isl_seq_clr(new_div[i], 1 + row_size);
3474 for (i = 0; i < bmap->extra; ++i)
3475 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3476 free(bmap->div);
3477 bmap->div = new_div;
3478 bmap->n_div += n;
3479 bmap->extra += n;
3481 return bmap;
3484 /* Turn the n dimensions of type type, starting at first
3485 * into existentially quantified variables.
3487 __isl_give isl_basic_map *isl_basic_map_project_out(
3488 __isl_take isl_basic_map *bmap,
3489 enum isl_dim_type type, unsigned first, unsigned n)
3491 if (n == 0)
3492 return basic_map_space_reset(bmap, type);
3494 if (!bmap)
3495 return NULL;
3497 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3498 return isl_basic_map_remove_dims(bmap, type, first, n);
3500 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3501 goto error);
3503 bmap = move_last(bmap, type, first, n);
3504 bmap = isl_basic_map_cow(bmap);
3505 bmap = insert_div_rows(bmap, n);
3506 if (!bmap)
3507 return NULL;
3509 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3510 if (!bmap->dim)
3511 goto error;
3512 bmap = isl_basic_map_simplify(bmap);
3513 bmap = isl_basic_map_drop_redundant_divs(bmap);
3514 return isl_basic_map_finalize(bmap);
3515 error:
3516 isl_basic_map_free(bmap);
3517 return NULL;
3520 /* Turn the n dimensions of type type, starting at first
3521 * into existentially quantified variables.
3523 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3524 enum isl_dim_type type, unsigned first, unsigned n)
3526 return (isl_basic_set *)isl_basic_map_project_out(
3527 (isl_basic_map *)bset, type, first, n);
3530 /* Turn the n dimensions of type type, starting at first
3531 * into existentially quantified variables.
3533 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3534 enum isl_dim_type type, unsigned first, unsigned n)
3536 int i;
3538 if (!map)
3539 return NULL;
3541 if (n == 0)
3542 return map_space_reset(map, type);
3544 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3546 map = isl_map_cow(map);
3547 if (!map)
3548 return NULL;
3550 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3551 if (!map->dim)
3552 goto error;
3554 for (i = 0; i < map->n; ++i) {
3555 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3556 if (!map->p[i])
3557 goto error;
3560 return map;
3561 error:
3562 isl_map_free(map);
3563 return NULL;
3566 /* Turn the n dimensions of type type, starting at first
3567 * into existentially quantified variables.
3569 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3570 enum isl_dim_type type, unsigned first, unsigned n)
3572 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3575 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3577 int i, j;
3579 for (i = 0; i < n; ++i) {
3580 j = isl_basic_map_alloc_div(bmap);
3581 if (j < 0)
3582 goto error;
3583 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3585 return bmap;
3586 error:
3587 isl_basic_map_free(bmap);
3588 return NULL;
3591 struct isl_basic_map *isl_basic_map_apply_range(
3592 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3594 isl_space *dim_result = NULL;
3595 struct isl_basic_map *bmap;
3596 unsigned n_in, n_out, n, nparam, total, pos;
3597 struct isl_dim_map *dim_map1, *dim_map2;
3599 if (!bmap1 || !bmap2)
3600 goto error;
3601 if (!isl_space_match(bmap1->dim, isl_dim_param,
3602 bmap2->dim, isl_dim_param))
3603 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3604 "parameters don't match", goto error);
3605 if (!isl_space_tuple_match(bmap1->dim, isl_dim_out,
3606 bmap2->dim, isl_dim_in))
3607 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3608 "spaces don't match", goto error);
3610 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3611 isl_space_copy(bmap2->dim));
3613 n_in = isl_basic_map_n_in(bmap1);
3614 n_out = isl_basic_map_n_out(bmap2);
3615 n = isl_basic_map_n_out(bmap1);
3616 nparam = isl_basic_map_n_param(bmap1);
3618 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3619 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3620 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3621 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3622 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3623 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3624 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3625 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3626 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3627 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3628 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3630 bmap = isl_basic_map_alloc_space(dim_result,
3631 bmap1->n_div + bmap2->n_div + n,
3632 bmap1->n_eq + bmap2->n_eq,
3633 bmap1->n_ineq + bmap2->n_ineq);
3634 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3635 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3636 bmap = add_divs(bmap, n);
3637 bmap = isl_basic_map_simplify(bmap);
3638 bmap = isl_basic_map_drop_redundant_divs(bmap);
3639 return isl_basic_map_finalize(bmap);
3640 error:
3641 isl_basic_map_free(bmap1);
3642 isl_basic_map_free(bmap2);
3643 return NULL;
3646 struct isl_basic_set *isl_basic_set_apply(
3647 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3649 if (!bset || !bmap)
3650 goto error;
3652 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3653 goto error);
3655 return (struct isl_basic_set *)
3656 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3657 error:
3658 isl_basic_set_free(bset);
3659 isl_basic_map_free(bmap);
3660 return NULL;
3663 struct isl_basic_map *isl_basic_map_apply_domain(
3664 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3666 if (!bmap1 || !bmap2)
3667 goto error;
3669 isl_assert(bmap1->ctx,
3670 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3671 isl_assert(bmap1->ctx,
3672 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3673 goto error);
3675 bmap1 = isl_basic_map_reverse(bmap1);
3676 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3677 return isl_basic_map_reverse(bmap1);
3678 error:
3679 isl_basic_map_free(bmap1);
3680 isl_basic_map_free(bmap2);
3681 return NULL;
3684 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3685 * A \cap B -> f(A) + f(B)
3687 struct isl_basic_map *isl_basic_map_sum(
3688 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3690 unsigned n_in, n_out, nparam, total, pos;
3691 struct isl_basic_map *bmap = NULL;
3692 struct isl_dim_map *dim_map1, *dim_map2;
3693 int i;
3695 if (!bmap1 || !bmap2)
3696 goto error;
3698 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3699 goto error);
3701 nparam = isl_basic_map_n_param(bmap1);
3702 n_in = isl_basic_map_n_in(bmap1);
3703 n_out = isl_basic_map_n_out(bmap1);
3705 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3706 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3707 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3708 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3709 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3710 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3711 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3712 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3713 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3714 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3715 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3717 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3718 bmap1->n_div + bmap2->n_div + 2 * n_out,
3719 bmap1->n_eq + bmap2->n_eq + n_out,
3720 bmap1->n_ineq + bmap2->n_ineq);
3721 for (i = 0; i < n_out; ++i) {
3722 int j = isl_basic_map_alloc_equality(bmap);
3723 if (j < 0)
3724 goto error;
3725 isl_seq_clr(bmap->eq[j], 1+total);
3726 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3727 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3728 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3730 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3731 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3732 bmap = add_divs(bmap, 2 * n_out);
3734 bmap = isl_basic_map_simplify(bmap);
3735 return isl_basic_map_finalize(bmap);
3736 error:
3737 isl_basic_map_free(bmap);
3738 isl_basic_map_free(bmap1);
3739 isl_basic_map_free(bmap2);
3740 return NULL;
3743 /* Given two maps A -> f(A) and B -> g(B), construct a map
3744 * A \cap B -> f(A) + f(B)
3746 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3748 struct isl_map *result;
3749 int i, j;
3751 if (!map1 || !map2)
3752 goto error;
3754 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3756 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3757 map1->n * map2->n, 0);
3758 if (!result)
3759 goto error;
3760 for (i = 0; i < map1->n; ++i)
3761 for (j = 0; j < map2->n; ++j) {
3762 struct isl_basic_map *part;
3763 part = isl_basic_map_sum(
3764 isl_basic_map_copy(map1->p[i]),
3765 isl_basic_map_copy(map2->p[j]));
3766 if (isl_basic_map_is_empty(part))
3767 isl_basic_map_free(part);
3768 else
3769 result = isl_map_add_basic_map(result, part);
3770 if (!result)
3771 goto error;
3773 isl_map_free(map1);
3774 isl_map_free(map2);
3775 return result;
3776 error:
3777 isl_map_free(map1);
3778 isl_map_free(map2);
3779 return NULL;
3782 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3783 __isl_take isl_set *set2)
3785 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3788 /* Given a basic map A -> f(A), construct A -> -f(A).
3790 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3792 int i, j;
3793 unsigned off, n;
3795 bmap = isl_basic_map_cow(bmap);
3796 if (!bmap)
3797 return NULL;
3799 n = isl_basic_map_dim(bmap, isl_dim_out);
3800 off = isl_basic_map_offset(bmap, isl_dim_out);
3801 for (i = 0; i < bmap->n_eq; ++i)
3802 for (j = 0; j < n; ++j)
3803 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3804 for (i = 0; i < bmap->n_ineq; ++i)
3805 for (j = 0; j < n; ++j)
3806 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3807 for (i = 0; i < bmap->n_div; ++i)
3808 for (j = 0; j < n; ++j)
3809 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3810 bmap = isl_basic_map_gauss(bmap, NULL);
3811 return isl_basic_map_finalize(bmap);
3814 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3816 return isl_basic_map_neg(bset);
3819 /* Given a map A -> f(A), construct A -> -f(A).
3821 struct isl_map *isl_map_neg(struct isl_map *map)
3823 int i;
3825 map = isl_map_cow(map);
3826 if (!map)
3827 return NULL;
3829 for (i = 0; i < map->n; ++i) {
3830 map->p[i] = isl_basic_map_neg(map->p[i]);
3831 if (!map->p[i])
3832 goto error;
3835 return map;
3836 error:
3837 isl_map_free(map);
3838 return NULL;
3841 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3843 return (isl_set *)isl_map_neg((isl_map *)set);
3846 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3847 * A -> floor(f(A)/d).
3849 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3850 isl_int d)
3852 unsigned n_in, n_out, nparam, total, pos;
3853 struct isl_basic_map *result = NULL;
3854 struct isl_dim_map *dim_map;
3855 int i;
3857 if (!bmap)
3858 return NULL;
3860 nparam = isl_basic_map_n_param(bmap);
3861 n_in = isl_basic_map_n_in(bmap);
3862 n_out = isl_basic_map_n_out(bmap);
3864 total = nparam + n_in + n_out + bmap->n_div + n_out;
3865 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3866 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3867 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3868 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3869 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3871 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3872 bmap->n_div + n_out,
3873 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3874 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3875 result = add_divs(result, n_out);
3876 for (i = 0; i < n_out; ++i) {
3877 int j;
3878 j = isl_basic_map_alloc_inequality(result);
3879 if (j < 0)
3880 goto error;
3881 isl_seq_clr(result->ineq[j], 1+total);
3882 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3883 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3884 j = isl_basic_map_alloc_inequality(result);
3885 if (j < 0)
3886 goto error;
3887 isl_seq_clr(result->ineq[j], 1+total);
3888 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3889 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3890 isl_int_sub_ui(result->ineq[j][0], d, 1);
3893 result = isl_basic_map_simplify(result);
3894 return isl_basic_map_finalize(result);
3895 error:
3896 isl_basic_map_free(result);
3897 return NULL;
3900 /* Given a map A -> f(A) and an integer d, construct a map
3901 * A -> floor(f(A)/d).
3903 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3905 int i;
3907 map = isl_map_cow(map);
3908 if (!map)
3909 return NULL;
3911 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3912 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3913 for (i = 0; i < map->n; ++i) {
3914 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3915 if (!map->p[i])
3916 goto error;
3919 return map;
3920 error:
3921 isl_map_free(map);
3922 return NULL;
3925 /* Given a map A -> f(A) and an integer d, construct a map
3926 * A -> floor(f(A)/d).
3928 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3929 __isl_take isl_val *d)
3931 if (!map || !d)
3932 goto error;
3933 if (!isl_val_is_int(d))
3934 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3935 "expecting integer denominator", goto error);
3936 map = isl_map_floordiv(map, d->n);
3937 isl_val_free(d);
3938 return map;
3939 error:
3940 isl_map_free(map);
3941 isl_val_free(d);
3942 return NULL;
3945 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3947 int i;
3948 unsigned nparam;
3949 unsigned n_in;
3951 i = isl_basic_map_alloc_equality(bmap);
3952 if (i < 0)
3953 goto error;
3954 nparam = isl_basic_map_n_param(bmap);
3955 n_in = isl_basic_map_n_in(bmap);
3956 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3957 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3958 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3959 return isl_basic_map_finalize(bmap);
3960 error:
3961 isl_basic_map_free(bmap);
3962 return NULL;
3965 /* Add a constraints to "bmap" expressing i_pos < o_pos
3967 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3969 int i;
3970 unsigned nparam;
3971 unsigned n_in;
3973 i = isl_basic_map_alloc_inequality(bmap);
3974 if (i < 0)
3975 goto error;
3976 nparam = isl_basic_map_n_param(bmap);
3977 n_in = isl_basic_map_n_in(bmap);
3978 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3979 isl_int_set_si(bmap->ineq[i][0], -1);
3980 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3981 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3982 return isl_basic_map_finalize(bmap);
3983 error:
3984 isl_basic_map_free(bmap);
3985 return NULL;
3988 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3990 static __isl_give isl_basic_map *var_less_or_equal(
3991 __isl_take isl_basic_map *bmap, unsigned pos)
3993 int i;
3994 unsigned nparam;
3995 unsigned n_in;
3997 i = isl_basic_map_alloc_inequality(bmap);
3998 if (i < 0)
3999 goto error;
4000 nparam = isl_basic_map_n_param(bmap);
4001 n_in = isl_basic_map_n_in(bmap);
4002 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4003 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4004 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4005 return isl_basic_map_finalize(bmap);
4006 error:
4007 isl_basic_map_free(bmap);
4008 return NULL;
4011 /* Add a constraints to "bmap" expressing i_pos > o_pos
4013 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4015 int i;
4016 unsigned nparam;
4017 unsigned n_in;
4019 i = isl_basic_map_alloc_inequality(bmap);
4020 if (i < 0)
4021 goto error;
4022 nparam = isl_basic_map_n_param(bmap);
4023 n_in = isl_basic_map_n_in(bmap);
4024 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4025 isl_int_set_si(bmap->ineq[i][0], -1);
4026 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4027 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4028 return isl_basic_map_finalize(bmap);
4029 error:
4030 isl_basic_map_free(bmap);
4031 return NULL;
4034 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4036 static __isl_give isl_basic_map *var_more_or_equal(
4037 __isl_take isl_basic_map *bmap, unsigned pos)
4039 int i;
4040 unsigned nparam;
4041 unsigned n_in;
4043 i = isl_basic_map_alloc_inequality(bmap);
4044 if (i < 0)
4045 goto error;
4046 nparam = isl_basic_map_n_param(bmap);
4047 n_in = isl_basic_map_n_in(bmap);
4048 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4049 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4050 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4051 return isl_basic_map_finalize(bmap);
4052 error:
4053 isl_basic_map_free(bmap);
4054 return NULL;
4057 __isl_give isl_basic_map *isl_basic_map_equal(
4058 __isl_take isl_space *dim, unsigned n_equal)
4060 int i;
4061 struct isl_basic_map *bmap;
4062 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4063 if (!bmap)
4064 return NULL;
4065 for (i = 0; i < n_equal && bmap; ++i)
4066 bmap = var_equal(bmap, i);
4067 return isl_basic_map_finalize(bmap);
4070 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4072 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4073 unsigned pos)
4075 int i;
4076 struct isl_basic_map *bmap;
4077 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4078 if (!bmap)
4079 return NULL;
4080 for (i = 0; i < pos && bmap; ++i)
4081 bmap = var_equal(bmap, i);
4082 if (bmap)
4083 bmap = var_less(bmap, pos);
4084 return isl_basic_map_finalize(bmap);
4087 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4089 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4090 __isl_take isl_space *dim, unsigned pos)
4092 int i;
4093 isl_basic_map *bmap;
4095 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4096 for (i = 0; i < pos; ++i)
4097 bmap = var_equal(bmap, i);
4098 bmap = var_less_or_equal(bmap, pos);
4099 return isl_basic_map_finalize(bmap);
4102 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4104 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4105 unsigned pos)
4107 int i;
4108 struct isl_basic_map *bmap;
4109 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4110 if (!bmap)
4111 return NULL;
4112 for (i = 0; i < pos && bmap; ++i)
4113 bmap = var_equal(bmap, i);
4114 if (bmap)
4115 bmap = var_more(bmap, pos);
4116 return isl_basic_map_finalize(bmap);
4119 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4121 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4122 __isl_take isl_space *dim, unsigned pos)
4124 int i;
4125 isl_basic_map *bmap;
4127 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4128 for (i = 0; i < pos; ++i)
4129 bmap = var_equal(bmap, i);
4130 bmap = var_more_or_equal(bmap, pos);
4131 return isl_basic_map_finalize(bmap);
4134 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4135 unsigned n, int equal)
4137 struct isl_map *map;
4138 int i;
4140 if (n == 0 && equal)
4141 return isl_map_universe(dims);
4143 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4145 for (i = 0; i + 1 < n; ++i)
4146 map = isl_map_add_basic_map(map,
4147 isl_basic_map_less_at(isl_space_copy(dims), i));
4148 if (n > 0) {
4149 if (equal)
4150 map = isl_map_add_basic_map(map,
4151 isl_basic_map_less_or_equal_at(dims, n - 1));
4152 else
4153 map = isl_map_add_basic_map(map,
4154 isl_basic_map_less_at(dims, n - 1));
4155 } else
4156 isl_space_free(dims);
4158 return map;
4161 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4163 if (!dims)
4164 return NULL;
4165 return map_lex_lte_first(dims, dims->n_out, equal);
4168 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4170 return map_lex_lte_first(dim, n, 0);
4173 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4175 return map_lex_lte_first(dim, n, 1);
4178 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4180 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4183 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4185 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4188 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4189 unsigned n, int equal)
4191 struct isl_map *map;
4192 int i;
4194 if (n == 0 && equal)
4195 return isl_map_universe(dims);
4197 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4199 for (i = 0; i + 1 < n; ++i)
4200 map = isl_map_add_basic_map(map,
4201 isl_basic_map_more_at(isl_space_copy(dims), i));
4202 if (n > 0) {
4203 if (equal)
4204 map = isl_map_add_basic_map(map,
4205 isl_basic_map_more_or_equal_at(dims, n - 1));
4206 else
4207 map = isl_map_add_basic_map(map,
4208 isl_basic_map_more_at(dims, n - 1));
4209 } else
4210 isl_space_free(dims);
4212 return map;
4215 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4217 if (!dims)
4218 return NULL;
4219 return map_lex_gte_first(dims, dims->n_out, equal);
4222 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4224 return map_lex_gte_first(dim, n, 0);
4227 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4229 return map_lex_gte_first(dim, n, 1);
4232 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4234 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4237 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4239 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4242 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4243 __isl_take isl_set *set2)
4245 isl_map *map;
4246 map = isl_map_lex_le(isl_set_get_space(set1));
4247 map = isl_map_intersect_domain(map, set1);
4248 map = isl_map_intersect_range(map, set2);
4249 return map;
4252 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4253 __isl_take isl_set *set2)
4255 isl_map *map;
4256 map = isl_map_lex_lt(isl_set_get_space(set1));
4257 map = isl_map_intersect_domain(map, set1);
4258 map = isl_map_intersect_range(map, set2);
4259 return map;
4262 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4263 __isl_take isl_set *set2)
4265 isl_map *map;
4266 map = isl_map_lex_ge(isl_set_get_space(set1));
4267 map = isl_map_intersect_domain(map, set1);
4268 map = isl_map_intersect_range(map, set2);
4269 return map;
4272 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4273 __isl_take isl_set *set2)
4275 isl_map *map;
4276 map = isl_map_lex_gt(isl_set_get_space(set1));
4277 map = isl_map_intersect_domain(map, set1);
4278 map = isl_map_intersect_range(map, set2);
4279 return map;
4282 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4283 __isl_take isl_map *map2)
4285 isl_map *map;
4286 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4287 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4288 map = isl_map_apply_range(map, isl_map_reverse(map2));
4289 return map;
4292 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4293 __isl_take isl_map *map2)
4295 isl_map *map;
4296 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4297 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4298 map = isl_map_apply_range(map, isl_map_reverse(map2));
4299 return map;
4302 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4303 __isl_take isl_map *map2)
4305 isl_map *map;
4306 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4307 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4308 map = isl_map_apply_range(map, isl_map_reverse(map2));
4309 return map;
4312 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4313 __isl_take isl_map *map2)
4315 isl_map *map;
4316 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4317 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4318 map = isl_map_apply_range(map, isl_map_reverse(map2));
4319 return map;
4322 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4323 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4325 struct isl_basic_map *bmap;
4327 bset = isl_basic_set_cow(bset);
4328 if (!bset || !dim)
4329 goto error;
4331 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4332 isl_space_free(bset->dim);
4333 bmap = (struct isl_basic_map *) bset;
4334 bmap->dim = dim;
4335 return isl_basic_map_finalize(bmap);
4336 error:
4337 isl_basic_set_free(bset);
4338 isl_space_free(dim);
4339 return NULL;
4342 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4344 if (!bmap)
4345 goto error;
4346 if (bmap->dim->n_in == 0)
4347 return (struct isl_basic_set *)bmap;
4348 bmap = isl_basic_map_cow(bmap);
4349 if (!bmap)
4350 goto error;
4351 bmap->dim = isl_space_as_set_space(bmap->dim);
4352 if (!bmap->dim)
4353 goto error;
4354 bmap = isl_basic_map_finalize(bmap);
4355 return (struct isl_basic_set *)bmap;
4356 error:
4357 isl_basic_map_free(bmap);
4358 return NULL;
4361 /* For a div d = floor(f/m), add the constraint
4363 * f - m d >= 0
4365 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4366 unsigned pos, isl_int *div)
4368 int i;
4369 unsigned total = isl_basic_map_total_dim(bmap);
4371 i = isl_basic_map_alloc_inequality(bmap);
4372 if (i < 0)
4373 return -1;
4374 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4375 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4377 return 0;
4380 /* For a div d = floor(f/m), add the constraint
4382 * -(f-(n-1)) + m d >= 0
4384 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4385 unsigned pos, isl_int *div)
4387 int i;
4388 unsigned total = isl_basic_map_total_dim(bmap);
4390 i = isl_basic_map_alloc_inequality(bmap);
4391 if (i < 0)
4392 return -1;
4393 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4394 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4395 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4396 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4398 return 0;
4401 /* For a div d = floor(f/m), add the constraints
4403 * f - m d >= 0
4404 * -(f-(n-1)) + m d >= 0
4406 * Note that the second constraint is the negation of
4408 * f - m d >= n
4410 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4411 unsigned pos, isl_int *div)
4413 if (add_upper_div_constraint(bmap, pos, div) < 0)
4414 return -1;
4415 if (add_lower_div_constraint(bmap, pos, div) < 0)
4416 return -1;
4417 return 0;
4420 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4421 unsigned pos, isl_int *div)
4423 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4424 pos, div);
4427 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4429 unsigned total = isl_basic_map_total_dim(bmap);
4430 unsigned div_pos = total - bmap->n_div + div;
4432 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4433 bmap->div[div]);
4436 /* For each known div d = floor(f/m), add the constraints
4438 * f - m d >= 0
4439 * -(f-(n-1)) + m d >= 0
4441 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4442 __isl_take isl_basic_map *bmap)
4444 int i;
4445 unsigned n_div;
4447 if (!bmap)
4448 return NULL;
4449 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4450 if (n_div == 0)
4451 return bmap;
4452 bmap = isl_basic_map_cow(bmap);
4453 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
4454 if (!bmap)
4455 return NULL;
4456 for (i = 0; i < n_div; ++i) {
4457 if (isl_int_is_zero(bmap->div[i][0]))
4458 continue;
4459 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4460 return isl_basic_map_free(bmap);
4463 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4464 bmap = isl_basic_map_finalize(bmap);
4465 return bmap;
4468 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4470 * In particular, if this div is of the form d = floor(f/m),
4471 * then add the constraint
4473 * f - m d >= 0
4475 * if sign < 0 or the constraint
4477 * -(f-(n-1)) + m d >= 0
4479 * if sign > 0.
4481 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4482 unsigned div, int sign)
4484 unsigned total;
4485 unsigned div_pos;
4487 if (!bmap)
4488 return -1;
4490 total = isl_basic_map_total_dim(bmap);
4491 div_pos = total - bmap->n_div + div;
4493 if (sign < 0)
4494 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4495 else
4496 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4499 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4501 return isl_basic_map_add_div_constraints(bset, div);
4504 struct isl_basic_set *isl_basic_map_underlying_set(
4505 struct isl_basic_map *bmap)
4507 if (!bmap)
4508 goto error;
4509 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4510 bmap->n_div == 0 &&
4511 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4512 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4513 return (struct isl_basic_set *)bmap;
4514 bmap = isl_basic_map_cow(bmap);
4515 if (!bmap)
4516 goto error;
4517 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4518 if (!bmap->dim)
4519 goto error;
4520 bmap->extra -= bmap->n_div;
4521 bmap->n_div = 0;
4522 bmap = isl_basic_map_finalize(bmap);
4523 return (struct isl_basic_set *)bmap;
4524 error:
4525 isl_basic_map_free(bmap);
4526 return NULL;
4529 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4530 __isl_take isl_basic_set *bset)
4532 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4535 struct isl_basic_map *isl_basic_map_overlying_set(
4536 struct isl_basic_set *bset, struct isl_basic_map *like)
4538 struct isl_basic_map *bmap;
4539 struct isl_ctx *ctx;
4540 unsigned total;
4541 int i;
4543 if (!bset || !like)
4544 goto error;
4545 ctx = bset->ctx;
4546 isl_assert(ctx, bset->n_div == 0, goto error);
4547 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4548 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4549 goto error);
4550 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4551 isl_basic_map_free(like);
4552 return (struct isl_basic_map *)bset;
4554 bset = isl_basic_set_cow(bset);
4555 if (!bset)
4556 goto error;
4557 total = bset->dim->n_out + bset->extra;
4558 bmap = (struct isl_basic_map *)bset;
4559 isl_space_free(bmap->dim);
4560 bmap->dim = isl_space_copy(like->dim);
4561 if (!bmap->dim)
4562 goto error;
4563 bmap->n_div = like->n_div;
4564 bmap->extra += like->n_div;
4565 if (bmap->extra) {
4566 unsigned ltotal;
4567 isl_int **div;
4568 ltotal = total - bmap->extra + like->extra;
4569 if (ltotal > total)
4570 ltotal = total;
4571 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4572 bmap->extra * (1 + 1 + total));
4573 if (isl_blk_is_error(bmap->block2))
4574 goto error;
4575 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4576 if (!div)
4577 goto error;
4578 bmap->div = div;
4579 for (i = 0; i < bmap->extra; ++i)
4580 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4581 for (i = 0; i < like->n_div; ++i) {
4582 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4583 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4585 bmap = isl_basic_map_add_known_div_constraints(bmap);
4587 isl_basic_map_free(like);
4588 bmap = isl_basic_map_simplify(bmap);
4589 bmap = isl_basic_map_finalize(bmap);
4590 return bmap;
4591 error:
4592 isl_basic_map_free(like);
4593 isl_basic_set_free(bset);
4594 return NULL;
4597 struct isl_basic_set *isl_basic_set_from_underlying_set(
4598 struct isl_basic_set *bset, struct isl_basic_set *like)
4600 return (struct isl_basic_set *)
4601 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4604 struct isl_set *isl_set_from_underlying_set(
4605 struct isl_set *set, struct isl_basic_set *like)
4607 int i;
4609 if (!set || !like)
4610 goto error;
4611 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4612 goto error);
4613 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4614 isl_basic_set_free(like);
4615 return set;
4617 set = isl_set_cow(set);
4618 if (!set)
4619 goto error;
4620 for (i = 0; i < set->n; ++i) {
4621 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4622 isl_basic_set_copy(like));
4623 if (!set->p[i])
4624 goto error;
4626 isl_space_free(set->dim);
4627 set->dim = isl_space_copy(like->dim);
4628 if (!set->dim)
4629 goto error;
4630 isl_basic_set_free(like);
4631 return set;
4632 error:
4633 isl_basic_set_free(like);
4634 isl_set_free(set);
4635 return NULL;
4638 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4640 int i;
4642 map = isl_map_cow(map);
4643 if (!map)
4644 return NULL;
4645 map->dim = isl_space_cow(map->dim);
4646 if (!map->dim)
4647 goto error;
4649 for (i = 1; i < map->n; ++i)
4650 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4651 goto error);
4652 for (i = 0; i < map->n; ++i) {
4653 map->p[i] = (struct isl_basic_map *)
4654 isl_basic_map_underlying_set(map->p[i]);
4655 if (!map->p[i])
4656 goto error;
4658 if (map->n == 0)
4659 map->dim = isl_space_underlying(map->dim, 0);
4660 else {
4661 isl_space_free(map->dim);
4662 map->dim = isl_space_copy(map->p[0]->dim);
4664 if (!map->dim)
4665 goto error;
4666 return (struct isl_set *)map;
4667 error:
4668 isl_map_free(map);
4669 return NULL;
4672 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4674 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4677 __isl_give isl_basic_map *isl_basic_map_reset_space(
4678 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4680 bmap = isl_basic_map_cow(bmap);
4681 if (!bmap || !dim)
4682 goto error;
4684 isl_space_free(bmap->dim);
4685 bmap->dim = dim;
4687 bmap = isl_basic_map_finalize(bmap);
4689 return bmap;
4690 error:
4691 isl_basic_map_free(bmap);
4692 isl_space_free(dim);
4693 return NULL;
4696 __isl_give isl_basic_set *isl_basic_set_reset_space(
4697 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4699 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4700 dim);
4703 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4704 __isl_take isl_space *dim)
4706 int i;
4708 map = isl_map_cow(map);
4709 if (!map || !dim)
4710 goto error;
4712 for (i = 0; i < map->n; ++i) {
4713 map->p[i] = isl_basic_map_reset_space(map->p[i],
4714 isl_space_copy(dim));
4715 if (!map->p[i])
4716 goto error;
4718 isl_space_free(map->dim);
4719 map->dim = dim;
4721 return map;
4722 error:
4723 isl_map_free(map);
4724 isl_space_free(dim);
4725 return NULL;
4728 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4729 __isl_take isl_space *dim)
4731 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4734 /* Compute the parameter domain of the given basic set.
4736 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4738 isl_space *space;
4739 unsigned n;
4741 if (isl_basic_set_is_params(bset))
4742 return bset;
4744 n = isl_basic_set_dim(bset, isl_dim_set);
4745 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4746 space = isl_basic_set_get_space(bset);
4747 space = isl_space_params(space);
4748 bset = isl_basic_set_reset_space(bset, space);
4749 return bset;
4752 /* Construct a zero-dimensional basic set with the given parameter domain.
4754 __isl_give isl_basic_set *isl_basic_set_from_params(
4755 __isl_take isl_basic_set *bset)
4757 isl_space *space;
4758 space = isl_basic_set_get_space(bset);
4759 space = isl_space_set_from_params(space);
4760 bset = isl_basic_set_reset_space(bset, space);
4761 return bset;
4764 /* Compute the parameter domain of the given set.
4766 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4768 isl_space *space;
4769 unsigned n;
4771 if (isl_set_is_params(set))
4772 return set;
4774 n = isl_set_dim(set, isl_dim_set);
4775 set = isl_set_project_out(set, isl_dim_set, 0, n);
4776 space = isl_set_get_space(set);
4777 space = isl_space_params(space);
4778 set = isl_set_reset_space(set, space);
4779 return set;
4782 /* Construct a zero-dimensional set with the given parameter domain.
4784 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4786 isl_space *space;
4787 space = isl_set_get_space(set);
4788 space = isl_space_set_from_params(space);
4789 set = isl_set_reset_space(set, space);
4790 return set;
4793 /* Compute the parameter domain of the given map.
4795 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4797 isl_space *space;
4798 unsigned n;
4800 n = isl_map_dim(map, isl_dim_in);
4801 map = isl_map_project_out(map, isl_dim_in, 0, n);
4802 n = isl_map_dim(map, isl_dim_out);
4803 map = isl_map_project_out(map, isl_dim_out, 0, n);
4804 space = isl_map_get_space(map);
4805 space = isl_space_params(space);
4806 map = isl_map_reset_space(map, space);
4807 return map;
4810 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4812 isl_space *dim;
4813 struct isl_basic_set *domain;
4814 unsigned n_in;
4815 unsigned n_out;
4817 if (!bmap)
4818 return NULL;
4819 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4821 n_in = isl_basic_map_n_in(bmap);
4822 n_out = isl_basic_map_n_out(bmap);
4823 domain = isl_basic_set_from_basic_map(bmap);
4824 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4826 domain = isl_basic_set_reset_space(domain, dim);
4828 return domain;
4831 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4833 if (!bmap)
4834 return -1;
4835 return isl_space_may_be_set(bmap->dim);
4838 /* Is this basic map actually a set?
4839 * Users should never call this function. Outside of isl,
4840 * the type should indicate whether something is a set or a map.
4842 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4844 if (!bmap)
4845 return -1;
4846 return isl_space_is_set(bmap->dim);
4849 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4851 if (!bmap)
4852 return NULL;
4853 if (isl_basic_map_is_set(bmap))
4854 return bmap;
4855 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4858 __isl_give isl_basic_map *isl_basic_map_domain_map(
4859 __isl_take isl_basic_map *bmap)
4861 int i, k;
4862 isl_space *dim;
4863 isl_basic_map *domain;
4864 int nparam, n_in, n_out;
4865 unsigned total;
4867 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4868 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4869 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4871 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4872 domain = isl_basic_map_universe(dim);
4874 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4875 bmap = isl_basic_map_apply_range(bmap, domain);
4876 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4878 total = isl_basic_map_total_dim(bmap);
4880 for (i = 0; i < n_in; ++i) {
4881 k = isl_basic_map_alloc_equality(bmap);
4882 if (k < 0)
4883 goto error;
4884 isl_seq_clr(bmap->eq[k], 1 + total);
4885 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4886 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4889 bmap = isl_basic_map_gauss(bmap, NULL);
4890 return isl_basic_map_finalize(bmap);
4891 error:
4892 isl_basic_map_free(bmap);
4893 return NULL;
4896 __isl_give isl_basic_map *isl_basic_map_range_map(
4897 __isl_take isl_basic_map *bmap)
4899 int i, k;
4900 isl_space *dim;
4901 isl_basic_map *range;
4902 int nparam, n_in, n_out;
4903 unsigned total;
4905 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4906 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4907 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4909 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4910 range = isl_basic_map_universe(dim);
4912 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4913 bmap = isl_basic_map_apply_range(bmap, range);
4914 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4916 total = isl_basic_map_total_dim(bmap);
4918 for (i = 0; i < n_out; ++i) {
4919 k = isl_basic_map_alloc_equality(bmap);
4920 if (k < 0)
4921 goto error;
4922 isl_seq_clr(bmap->eq[k], 1 + total);
4923 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4924 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4927 bmap = isl_basic_map_gauss(bmap, NULL);
4928 return isl_basic_map_finalize(bmap);
4929 error:
4930 isl_basic_map_free(bmap);
4931 return NULL;
4934 int isl_map_may_be_set(__isl_keep isl_map *map)
4936 if (!map)
4937 return -1;
4938 return isl_space_may_be_set(map->dim);
4941 /* Is this map actually a set?
4942 * Users should never call this function. Outside of isl,
4943 * the type should indicate whether something is a set or a map.
4945 int isl_map_is_set(__isl_keep isl_map *map)
4947 if (!map)
4948 return -1;
4949 return isl_space_is_set(map->dim);
4952 struct isl_set *isl_map_range(struct isl_map *map)
4954 int i;
4955 struct isl_set *set;
4957 if (!map)
4958 goto error;
4959 if (isl_map_is_set(map))
4960 return (isl_set *)map;
4962 map = isl_map_cow(map);
4963 if (!map)
4964 goto error;
4966 set = (struct isl_set *) map;
4967 set->dim = isl_space_range(set->dim);
4968 if (!set->dim)
4969 goto error;
4970 for (i = 0; i < map->n; ++i) {
4971 set->p[i] = isl_basic_map_range(map->p[i]);
4972 if (!set->p[i])
4973 goto error;
4975 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4976 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4977 return set;
4978 error:
4979 isl_map_free(map);
4980 return NULL;
4983 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4985 int i;
4987 map = isl_map_cow(map);
4988 if (!map)
4989 return NULL;
4991 map->dim = isl_space_domain_map(map->dim);
4992 if (!map->dim)
4993 goto error;
4994 for (i = 0; i < map->n; ++i) {
4995 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4996 if (!map->p[i])
4997 goto error;
4999 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5000 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5001 return map;
5002 error:
5003 isl_map_free(map);
5004 return NULL;
5007 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5009 int i;
5010 isl_space *range_dim;
5012 map = isl_map_cow(map);
5013 if (!map)
5014 return NULL;
5016 range_dim = isl_space_range(isl_map_get_space(map));
5017 range_dim = isl_space_from_range(range_dim);
5018 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5019 map->dim = isl_space_join(map->dim, range_dim);
5020 if (!map->dim)
5021 goto error;
5022 for (i = 0; i < map->n; ++i) {
5023 map->p[i] = isl_basic_map_range_map(map->p[i]);
5024 if (!map->p[i])
5025 goto error;
5027 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5028 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5029 return map;
5030 error:
5031 isl_map_free(map);
5032 return NULL;
5035 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5036 __isl_take isl_space *dim)
5038 int i;
5039 struct isl_map *map = NULL;
5041 set = isl_set_cow(set);
5042 if (!set || !dim)
5043 goto error;
5044 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5045 map = (struct isl_map *)set;
5046 for (i = 0; i < set->n; ++i) {
5047 map->p[i] = isl_basic_map_from_basic_set(
5048 set->p[i], isl_space_copy(dim));
5049 if (!map->p[i])
5050 goto error;
5052 isl_space_free(map->dim);
5053 map->dim = dim;
5054 return map;
5055 error:
5056 isl_space_free(dim);
5057 isl_set_free(set);
5058 return NULL;
5061 __isl_give isl_basic_map *isl_basic_map_from_domain(
5062 __isl_take isl_basic_set *bset)
5064 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5067 __isl_give isl_basic_map *isl_basic_map_from_range(
5068 __isl_take isl_basic_set *bset)
5070 isl_space *space;
5071 space = isl_basic_set_get_space(bset);
5072 space = isl_space_from_range(space);
5073 bset = isl_basic_set_reset_space(bset, space);
5074 return (isl_basic_map *)bset;
5077 /* Create a relation with the given set as range.
5078 * The domain of the created relation is a zero-dimensional
5079 * flat anonymous space.
5081 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5083 isl_space *space;
5084 space = isl_set_get_space(set);
5085 space = isl_space_from_range(space);
5086 set = isl_set_reset_space(set, space);
5087 return (struct isl_map *)set;
5090 /* Create a relation with the given set as domain.
5091 * The range of the created relation is a zero-dimensional
5092 * flat anonymous space.
5094 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5096 return isl_map_reverse(isl_map_from_range(set));
5099 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5100 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5102 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5105 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5106 __isl_take isl_set *range)
5108 return isl_map_apply_range(isl_map_reverse(domain), range);
5111 struct isl_set *isl_set_from_map(struct isl_map *map)
5113 int i;
5114 struct isl_set *set = NULL;
5116 if (!map)
5117 return NULL;
5118 map = isl_map_cow(map);
5119 if (!map)
5120 return NULL;
5121 map->dim = isl_space_as_set_space(map->dim);
5122 if (!map->dim)
5123 goto error;
5124 set = (struct isl_set *)map;
5125 for (i = 0; i < map->n; ++i) {
5126 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
5127 if (!set->p[i])
5128 goto error;
5130 return set;
5131 error:
5132 isl_map_free(map);
5133 return NULL;
5136 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5137 unsigned flags)
5139 struct isl_map *map;
5141 if (!dim)
5142 return NULL;
5143 if (n < 0)
5144 isl_die(dim->ctx, isl_error_internal,
5145 "negative number of basic maps", goto error);
5146 map = isl_alloc(dim->ctx, struct isl_map,
5147 sizeof(struct isl_map) +
5148 (n - 1) * sizeof(struct isl_basic_map *));
5149 if (!map)
5150 goto error;
5152 map->ctx = dim->ctx;
5153 isl_ctx_ref(map->ctx);
5154 map->ref = 1;
5155 map->size = n;
5156 map->n = 0;
5157 map->dim = dim;
5158 map->flags = flags;
5159 return map;
5160 error:
5161 isl_space_free(dim);
5162 return NULL;
5165 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5166 unsigned nparam, unsigned in, unsigned out, int n,
5167 unsigned flags)
5169 struct isl_map *map;
5170 isl_space *dims;
5172 dims = isl_space_alloc(ctx, nparam, in, out);
5173 if (!dims)
5174 return NULL;
5176 map = isl_map_alloc_space(dims, n, flags);
5177 return map;
5180 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5182 struct isl_basic_map *bmap;
5183 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5184 bmap = isl_basic_map_set_to_empty(bmap);
5185 return bmap;
5188 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5190 struct isl_basic_set *bset;
5191 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5192 bset = isl_basic_set_set_to_empty(bset);
5193 return bset;
5196 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5198 struct isl_basic_map *bmap;
5199 if (!model)
5200 return NULL;
5201 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5202 bmap = isl_basic_map_set_to_empty(bmap);
5203 return bmap;
5206 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5208 struct isl_basic_map *bmap;
5209 if (!model)
5210 return NULL;
5211 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5212 bmap = isl_basic_map_set_to_empty(bmap);
5213 return bmap;
5216 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5218 struct isl_basic_set *bset;
5219 if (!model)
5220 return NULL;
5221 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5222 bset = isl_basic_set_set_to_empty(bset);
5223 return bset;
5226 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5228 struct isl_basic_map *bmap;
5229 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5230 bmap = isl_basic_map_finalize(bmap);
5231 return bmap;
5234 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5236 struct isl_basic_set *bset;
5237 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5238 bset = isl_basic_set_finalize(bset);
5239 return bset;
5242 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5244 int i;
5245 unsigned total = isl_space_dim(dim, isl_dim_all);
5246 isl_basic_map *bmap;
5248 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5249 for (i = 0; i < total; ++i) {
5250 int k = isl_basic_map_alloc_inequality(bmap);
5251 if (k < 0)
5252 goto error;
5253 isl_seq_clr(bmap->ineq[k], 1 + total);
5254 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5256 return bmap;
5257 error:
5258 isl_basic_map_free(bmap);
5259 return NULL;
5262 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5264 return isl_basic_map_nat_universe(dim);
5267 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5269 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5272 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5274 return isl_map_nat_universe(dim);
5277 __isl_give isl_basic_map *isl_basic_map_universe_like(
5278 __isl_keep isl_basic_map *model)
5280 if (!model)
5281 return NULL;
5282 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5285 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5287 if (!model)
5288 return NULL;
5289 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5292 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5293 __isl_keep isl_set *model)
5295 if (!model)
5296 return NULL;
5297 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5300 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5302 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5305 struct isl_map *isl_map_empty_like(struct isl_map *model)
5307 if (!model)
5308 return NULL;
5309 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5312 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5314 if (!model)
5315 return NULL;
5316 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5319 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5321 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5324 struct isl_set *isl_set_empty_like(struct isl_set *model)
5326 if (!model)
5327 return NULL;
5328 return isl_set_empty(isl_space_copy(model->dim));
5331 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5333 struct isl_map *map;
5334 if (!dim)
5335 return NULL;
5336 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5337 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5338 return map;
5341 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5343 struct isl_set *set;
5344 if (!dim)
5345 return NULL;
5346 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5347 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5348 return set;
5351 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5353 if (!model)
5354 return NULL;
5355 return isl_set_universe(isl_space_copy(model->dim));
5358 struct isl_map *isl_map_dup(struct isl_map *map)
5360 int i;
5361 struct isl_map *dup;
5363 if (!map)
5364 return NULL;
5365 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5366 for (i = 0; i < map->n; ++i)
5367 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5368 return dup;
5371 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5372 __isl_take isl_basic_map *bmap)
5374 if (!bmap || !map)
5375 goto error;
5376 if (isl_basic_map_plain_is_empty(bmap)) {
5377 isl_basic_map_free(bmap);
5378 return map;
5380 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5381 isl_assert(map->ctx, map->n < map->size, goto error);
5382 map->p[map->n] = bmap;
5383 map->n++;
5384 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5385 return map;
5386 error:
5387 if (map)
5388 isl_map_free(map);
5389 if (bmap)
5390 isl_basic_map_free(bmap);
5391 return NULL;
5394 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5396 int i;
5398 if (!map)
5399 return NULL;
5401 if (--map->ref > 0)
5402 return NULL;
5404 isl_ctx_deref(map->ctx);
5405 for (i = 0; i < map->n; ++i)
5406 isl_basic_map_free(map->p[i]);
5407 isl_space_free(map->dim);
5408 free(map);
5410 return NULL;
5413 struct isl_map *isl_map_extend(struct isl_map *base,
5414 unsigned nparam, unsigned n_in, unsigned n_out)
5416 int i;
5418 base = isl_map_cow(base);
5419 if (!base)
5420 return NULL;
5422 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5423 if (!base->dim)
5424 goto error;
5425 for (i = 0; i < base->n; ++i) {
5426 base->p[i] = isl_basic_map_extend_space(base->p[i],
5427 isl_space_copy(base->dim), 0, 0, 0);
5428 if (!base->p[i])
5429 goto error;
5431 return base;
5432 error:
5433 isl_map_free(base);
5434 return NULL;
5437 struct isl_set *isl_set_extend(struct isl_set *base,
5438 unsigned nparam, unsigned dim)
5440 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5441 nparam, 0, dim);
5444 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5445 struct isl_basic_map *bmap, unsigned pos, int value)
5447 int j;
5449 bmap = isl_basic_map_cow(bmap);
5450 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5451 j = isl_basic_map_alloc_equality(bmap);
5452 if (j < 0)
5453 goto error;
5454 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5455 isl_int_set_si(bmap->eq[j][pos], -1);
5456 isl_int_set_si(bmap->eq[j][0], value);
5457 bmap = isl_basic_map_simplify(bmap);
5458 return isl_basic_map_finalize(bmap);
5459 error:
5460 isl_basic_map_free(bmap);
5461 return NULL;
5464 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5465 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5467 int j;
5469 bmap = isl_basic_map_cow(bmap);
5470 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5471 j = isl_basic_map_alloc_equality(bmap);
5472 if (j < 0)
5473 goto error;
5474 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5475 isl_int_set_si(bmap->eq[j][pos], -1);
5476 isl_int_set(bmap->eq[j][0], value);
5477 bmap = isl_basic_map_simplify(bmap);
5478 return isl_basic_map_finalize(bmap);
5479 error:
5480 isl_basic_map_free(bmap);
5481 return NULL;
5484 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5485 enum isl_dim_type type, unsigned pos, int value)
5487 if (!bmap)
5488 return NULL;
5489 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5490 return isl_basic_map_fix_pos_si(bmap,
5491 isl_basic_map_offset(bmap, type) + pos, value);
5492 error:
5493 isl_basic_map_free(bmap);
5494 return NULL;
5497 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5498 enum isl_dim_type type, unsigned pos, isl_int value)
5500 if (!bmap)
5501 return NULL;
5502 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5503 return isl_basic_map_fix_pos(bmap,
5504 isl_basic_map_offset(bmap, type) + pos, value);
5505 error:
5506 isl_basic_map_free(bmap);
5507 return NULL;
5510 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5511 * to be equal to "v".
5513 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5514 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5516 if (!bmap || !v)
5517 goto error;
5518 if (!isl_val_is_int(v))
5519 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5520 "expecting integer value", goto error);
5521 if (pos >= isl_basic_map_dim(bmap, type))
5522 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5523 "index out of bounds", goto error);
5524 pos += isl_basic_map_offset(bmap, type);
5525 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5526 isl_val_free(v);
5527 return bmap;
5528 error:
5529 isl_basic_map_free(bmap);
5530 isl_val_free(v);
5531 return NULL;
5534 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5535 * to be equal to "v".
5537 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5538 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5540 return isl_basic_map_fix_val(bset, type, pos, v);
5543 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5544 enum isl_dim_type type, unsigned pos, int value)
5546 return (struct isl_basic_set *)
5547 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5548 type, pos, value);
5551 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5552 enum isl_dim_type type, unsigned pos, isl_int value)
5554 return (struct isl_basic_set *)
5555 isl_basic_map_fix((struct isl_basic_map *)bset,
5556 type, pos, value);
5559 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5560 unsigned input, int value)
5562 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5565 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5566 unsigned dim, int value)
5568 return (struct isl_basic_set *)
5569 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5570 isl_dim_set, dim, value);
5573 static int remove_if_empty(__isl_keep isl_map *map, int i)
5575 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5577 if (empty < 0)
5578 return -1;
5579 if (!empty)
5580 return 0;
5582 isl_basic_map_free(map->p[i]);
5583 if (i != map->n - 1) {
5584 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5585 map->p[i] = map->p[map->n - 1];
5587 map->n--;
5589 return 0;
5592 /* Perform "fn" on each basic map of "map", where we may not be holding
5593 * the only reference to "map".
5594 * In particular, "fn" should be a semantics preserving operation
5595 * that we want to apply to all copies of "map". We therefore need
5596 * to be careful not to modify "map" in a way that breaks "map"
5597 * in case anything goes wrong.
5599 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5600 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5602 struct isl_basic_map *bmap;
5603 int i;
5605 if (!map)
5606 return NULL;
5608 for (i = map->n - 1; i >= 0; --i) {
5609 bmap = isl_basic_map_copy(map->p[i]);
5610 bmap = fn(bmap);
5611 if (!bmap)
5612 goto error;
5613 isl_basic_map_free(map->p[i]);
5614 map->p[i] = bmap;
5615 if (remove_if_empty(map, i) < 0)
5616 goto error;
5619 return map;
5620 error:
5621 isl_map_free(map);
5622 return NULL;
5625 struct isl_map *isl_map_fix_si(struct isl_map *map,
5626 enum isl_dim_type type, unsigned pos, int value)
5628 int i;
5630 map = isl_map_cow(map);
5631 if (!map)
5632 return NULL;
5634 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5635 for (i = map->n - 1; i >= 0; --i) {
5636 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5637 if (remove_if_empty(map, i) < 0)
5638 goto error;
5640 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5641 return map;
5642 error:
5643 isl_map_free(map);
5644 return NULL;
5647 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5648 enum isl_dim_type type, unsigned pos, int value)
5650 return (struct isl_set *)
5651 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5654 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5655 enum isl_dim_type type, unsigned pos, isl_int value)
5657 int i;
5659 map = isl_map_cow(map);
5660 if (!map)
5661 return NULL;
5663 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5664 for (i = 0; i < map->n; ++i) {
5665 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5666 if (!map->p[i])
5667 goto error;
5669 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5670 return map;
5671 error:
5672 isl_map_free(map);
5673 return NULL;
5676 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5677 enum isl_dim_type type, unsigned pos, isl_int value)
5679 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5682 /* Fix the value of the variable at position "pos" of type "type" of "map"
5683 * to be equal to "v".
5685 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5686 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5688 int i;
5690 map = isl_map_cow(map);
5691 if (!map || !v)
5692 goto error;
5694 if (!isl_val_is_int(v))
5695 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5696 "expecting integer value", goto error);
5697 if (pos >= isl_map_dim(map, type))
5698 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5699 "index out of bounds", goto error);
5700 for (i = map->n - 1; i >= 0; --i) {
5701 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5702 isl_val_copy(v));
5703 if (remove_if_empty(map, i) < 0)
5704 goto error;
5706 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5707 isl_val_free(v);
5708 return map;
5709 error:
5710 isl_map_free(map);
5711 isl_val_free(v);
5712 return NULL;
5715 /* Fix the value of the variable at position "pos" of type "type" of "set"
5716 * to be equal to "v".
5718 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5719 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5721 return isl_map_fix_val(set, type, pos, v);
5724 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5725 unsigned input, int value)
5727 return isl_map_fix_si(map, isl_dim_in, input, value);
5730 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5732 return (struct isl_set *)
5733 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5736 static __isl_give isl_basic_map *basic_map_bound_si(
5737 __isl_take isl_basic_map *bmap,
5738 enum isl_dim_type type, unsigned pos, int value, int upper)
5740 int j;
5742 if (!bmap)
5743 return NULL;
5744 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5745 pos += isl_basic_map_offset(bmap, type);
5746 bmap = isl_basic_map_cow(bmap);
5747 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5748 j = isl_basic_map_alloc_inequality(bmap);
5749 if (j < 0)
5750 goto error;
5751 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5752 if (upper) {
5753 isl_int_set_si(bmap->ineq[j][pos], -1);
5754 isl_int_set_si(bmap->ineq[j][0], value);
5755 } else {
5756 isl_int_set_si(bmap->ineq[j][pos], 1);
5757 isl_int_set_si(bmap->ineq[j][0], -value);
5759 bmap = isl_basic_map_simplify(bmap);
5760 return isl_basic_map_finalize(bmap);
5761 error:
5762 isl_basic_map_free(bmap);
5763 return NULL;
5766 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5767 __isl_take isl_basic_map *bmap,
5768 enum isl_dim_type type, unsigned pos, int value)
5770 return basic_map_bound_si(bmap, type, pos, value, 0);
5773 /* Constrain the values of the given dimension to be no greater than "value".
5775 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5776 __isl_take isl_basic_map *bmap,
5777 enum isl_dim_type type, unsigned pos, int value)
5779 return basic_map_bound_si(bmap, type, pos, value, 1);
5782 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5783 unsigned dim, isl_int value)
5785 int j;
5787 bset = isl_basic_set_cow(bset);
5788 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5789 j = isl_basic_set_alloc_inequality(bset);
5790 if (j < 0)
5791 goto error;
5792 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5793 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5794 isl_int_neg(bset->ineq[j][0], value);
5795 bset = isl_basic_set_simplify(bset);
5796 return isl_basic_set_finalize(bset);
5797 error:
5798 isl_basic_set_free(bset);
5799 return NULL;
5802 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5803 enum isl_dim_type type, unsigned pos, int value, int upper)
5805 int i;
5807 map = isl_map_cow(map);
5808 if (!map)
5809 return NULL;
5811 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5812 for (i = 0; i < map->n; ++i) {
5813 map->p[i] = basic_map_bound_si(map->p[i],
5814 type, pos, value, upper);
5815 if (!map->p[i])
5816 goto error;
5818 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5819 return map;
5820 error:
5821 isl_map_free(map);
5822 return NULL;
5825 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5826 enum isl_dim_type type, unsigned pos, int value)
5828 return map_bound_si(map, type, pos, value, 0);
5831 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5832 enum isl_dim_type type, unsigned pos, int value)
5834 return map_bound_si(map, type, pos, value, 1);
5837 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5838 enum isl_dim_type type, unsigned pos, int value)
5840 return (struct isl_set *)
5841 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5844 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5845 enum isl_dim_type type, unsigned pos, int value)
5847 return isl_map_upper_bound_si(set, type, pos, value);
5850 /* Bound the given variable of "bmap" from below (or above is "upper"
5851 * is set) to "value".
5853 static __isl_give isl_basic_map *basic_map_bound(
5854 __isl_take isl_basic_map *bmap,
5855 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5857 int j;
5859 if (!bmap)
5860 return NULL;
5861 if (pos >= isl_basic_map_dim(bmap, type))
5862 isl_die(bmap->ctx, isl_error_invalid,
5863 "index out of bounds", goto error);
5864 pos += isl_basic_map_offset(bmap, type);
5865 bmap = isl_basic_map_cow(bmap);
5866 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5867 j = isl_basic_map_alloc_inequality(bmap);
5868 if (j < 0)
5869 goto error;
5870 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5871 if (upper) {
5872 isl_int_set_si(bmap->ineq[j][pos], -1);
5873 isl_int_set(bmap->ineq[j][0], value);
5874 } else {
5875 isl_int_set_si(bmap->ineq[j][pos], 1);
5876 isl_int_neg(bmap->ineq[j][0], value);
5878 bmap = isl_basic_map_simplify(bmap);
5879 return isl_basic_map_finalize(bmap);
5880 error:
5881 isl_basic_map_free(bmap);
5882 return NULL;
5885 /* Bound the given variable of "map" from below (or above is "upper"
5886 * is set) to "value".
5888 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5889 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5891 int i;
5893 map = isl_map_cow(map);
5894 if (!map)
5895 return NULL;
5897 if (pos >= isl_map_dim(map, type))
5898 isl_die(map->ctx, isl_error_invalid,
5899 "index out of bounds", goto error);
5900 for (i = map->n - 1; i >= 0; --i) {
5901 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5902 if (remove_if_empty(map, i) < 0)
5903 goto error;
5905 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5906 return map;
5907 error:
5908 isl_map_free(map);
5909 return NULL;
5912 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5913 enum isl_dim_type type, unsigned pos, isl_int value)
5915 return map_bound(map, type, pos, value, 0);
5918 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5919 enum isl_dim_type type, unsigned pos, isl_int value)
5921 return map_bound(map, type, pos, value, 1);
5924 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5925 enum isl_dim_type type, unsigned pos, isl_int value)
5927 return isl_map_lower_bound(set, type, pos, value);
5930 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5931 enum isl_dim_type type, unsigned pos, isl_int value)
5933 return isl_map_upper_bound(set, type, pos, value);
5936 /* Force the values of the variable at position "pos" of type "type" of "set"
5937 * to be no smaller than "value".
5939 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5940 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5942 if (!value)
5943 goto error;
5944 if (!isl_val_is_int(value))
5945 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5946 "expecting integer value", goto error);
5947 set = isl_set_lower_bound(set, type, pos, value->n);
5948 isl_val_free(value);
5949 return set;
5950 error:
5951 isl_val_free(value);
5952 isl_set_free(set);
5953 return NULL;
5956 /* Force the values of the variable at position "pos" of type "type" of "set"
5957 * to be no greater than "value".
5959 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5960 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5962 if (!value)
5963 goto error;
5964 if (!isl_val_is_int(value))
5965 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5966 "expecting integer value", goto error);
5967 set = isl_set_upper_bound(set, type, pos, value->n);
5968 isl_val_free(value);
5969 return set;
5970 error:
5971 isl_val_free(value);
5972 isl_set_free(set);
5973 return NULL;
5976 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5977 isl_int value)
5979 int i;
5981 set = isl_set_cow(set);
5982 if (!set)
5983 return NULL;
5985 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5986 for (i = 0; i < set->n; ++i) {
5987 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5988 if (!set->p[i])
5989 goto error;
5991 return set;
5992 error:
5993 isl_set_free(set);
5994 return NULL;
5997 struct isl_map *isl_map_reverse(struct isl_map *map)
5999 int i;
6001 map = isl_map_cow(map);
6002 if (!map)
6003 return NULL;
6005 map->dim = isl_space_reverse(map->dim);
6006 if (!map->dim)
6007 goto error;
6008 for (i = 0; i < map->n; ++i) {
6009 map->p[i] = isl_basic_map_reverse(map->p[i]);
6010 if (!map->p[i])
6011 goto error;
6013 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6014 return map;
6015 error:
6016 isl_map_free(map);
6017 return NULL;
6020 static struct isl_map *isl_basic_map_partial_lexopt(
6021 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6022 struct isl_set **empty, int max)
6024 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6027 struct isl_map *isl_basic_map_partial_lexmax(
6028 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6029 struct isl_set **empty)
6031 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6034 struct isl_map *isl_basic_map_partial_lexmin(
6035 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6036 struct isl_set **empty)
6038 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6041 struct isl_set *isl_basic_set_partial_lexmin(
6042 struct isl_basic_set *bset, struct isl_basic_set *dom,
6043 struct isl_set **empty)
6045 return (struct isl_set *)
6046 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6047 dom, empty);
6050 struct isl_set *isl_basic_set_partial_lexmax(
6051 struct isl_basic_set *bset, struct isl_basic_set *dom,
6052 struct isl_set **empty)
6054 return (struct isl_set *)
6055 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6056 dom, empty);
6059 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6060 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6061 __isl_give isl_set **empty)
6063 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6066 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6067 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6068 __isl_give isl_set **empty)
6070 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6073 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6074 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6075 __isl_give isl_set **empty)
6077 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6080 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6081 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6082 __isl_give isl_set **empty)
6084 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6087 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6088 __isl_take isl_basic_map *bmap, int max)
6090 isl_basic_set *dom = NULL;
6091 isl_space *dom_space;
6093 if (!bmap)
6094 goto error;
6095 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6096 dom = isl_basic_set_universe(dom_space);
6097 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6098 error:
6099 isl_basic_map_free(bmap);
6100 return NULL;
6103 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6104 __isl_take isl_basic_map *bmap)
6106 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6109 #undef TYPE
6110 #define TYPE isl_pw_multi_aff
6111 #undef SUFFIX
6112 #define SUFFIX _pw_multi_aff
6113 #undef EMPTY
6114 #define EMPTY isl_pw_multi_aff_empty
6115 #undef ADD
6116 #define ADD isl_pw_multi_aff_union_add
6117 #include "isl_map_lexopt_templ.c"
6119 /* Given a map "map", compute the lexicographically minimal
6120 * (or maximal) image element for each domain element in dom,
6121 * in the form of an isl_pw_multi_aff.
6122 * Set *empty to those elements in dom that do not have an image element.
6124 * We first compute the lexicographically minimal or maximal element
6125 * in the first basic map. This results in a partial solution "res"
6126 * and a subset "todo" of dom that still need to be handled.
6127 * We then consider each of the remaining maps in "map" and successively
6128 * update both "res" and "todo".
6130 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6131 __isl_take isl_map *map, __isl_take isl_set *dom,
6132 __isl_give isl_set **empty, int max)
6134 int i;
6135 isl_pw_multi_aff *res;
6136 isl_set *todo;
6138 if (!map || !dom)
6139 goto error;
6141 if (isl_map_plain_is_empty(map)) {
6142 if (empty)
6143 *empty = dom;
6144 else
6145 isl_set_free(dom);
6146 return isl_pw_multi_aff_from_map(map);
6149 res = basic_map_partial_lexopt_pw_multi_aff(
6150 isl_basic_map_copy(map->p[0]),
6151 isl_set_copy(dom), &todo, max);
6153 for (i = 1; i < map->n; ++i) {
6154 isl_pw_multi_aff *res_i;
6155 isl_set *todo_i;
6157 res_i = basic_map_partial_lexopt_pw_multi_aff(
6158 isl_basic_map_copy(map->p[i]),
6159 isl_set_copy(dom), &todo_i, max);
6161 if (max)
6162 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6163 else
6164 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6166 todo = isl_set_intersect(todo, todo_i);
6169 isl_set_free(dom);
6170 isl_map_free(map);
6172 if (empty)
6173 *empty = todo;
6174 else
6175 isl_set_free(todo);
6177 return res;
6178 error:
6179 if (empty)
6180 *empty = NULL;
6181 isl_set_free(dom);
6182 isl_map_free(map);
6183 return NULL;
6186 #undef TYPE
6187 #define TYPE isl_map
6188 #undef SUFFIX
6189 #define SUFFIX
6190 #undef EMPTY
6191 #define EMPTY isl_map_empty
6192 #undef ADD
6193 #define ADD isl_map_union_disjoint
6194 #include "isl_map_lexopt_templ.c"
6196 /* Given a map "map", compute the lexicographically minimal
6197 * (or maximal) image element for each domain element in dom.
6198 * Set *empty to those elements in dom that do not have an image element.
6200 * We first compute the lexicographically minimal or maximal element
6201 * in the first basic map. This results in a partial solution "res"
6202 * and a subset "todo" of dom that still need to be handled.
6203 * We then consider each of the remaining maps in "map" and successively
6204 * update both "res" and "todo".
6206 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6207 * Assume we are computing the lexicographical maximum.
6208 * We first compute the lexicographically maximal element in basic map i.
6209 * This results in a partial solution res_i and a subset todo_i.
6210 * Then we combine these results with those obtain for the first k basic maps
6211 * to obtain a result that is valid for the first k+1 basic maps.
6212 * In particular, the set where there is no solution is the set where
6213 * there is no solution for the first k basic maps and also no solution
6214 * for the ith basic map, i.e.,
6216 * todo^i = todo^k * todo_i
6218 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6219 * solutions, arbitrarily breaking ties in favor of res^k.
6220 * That is, when res^k(a) >= res_i(a), we pick res^k and
6221 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6222 * the lexicographic order.)
6223 * In practice, we compute
6225 * res^k * (res_i . "<=")
6227 * and
6229 * res_i * (res^k . "<")
6231 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6232 * where only one of res^k and res_i provides a solution and we simply pick
6233 * that one, i.e.,
6235 * res^k * todo_i
6236 * and
6237 * res_i * todo^k
6239 * Note that we only compute these intersections when dom(res^k) intersects
6240 * dom(res_i). Otherwise, the only effect of these intersections is to
6241 * potentially break up res^k and res_i into smaller pieces.
6242 * We want to avoid such splintering as much as possible.
6243 * In fact, an earlier implementation of this function would look for
6244 * better results in the domain of res^k and for extra results in todo^k,
6245 * but this would always result in a splintering according to todo^k,
6246 * even when the domain of basic map i is disjoint from the domains of
6247 * the previous basic maps.
6249 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6250 __isl_take isl_map *map, __isl_take isl_set *dom,
6251 __isl_give isl_set **empty, int max)
6253 int i;
6254 struct isl_map *res;
6255 struct isl_set *todo;
6257 if (!map || !dom)
6258 goto error;
6260 if (isl_map_plain_is_empty(map)) {
6261 if (empty)
6262 *empty = dom;
6263 else
6264 isl_set_free(dom);
6265 return map;
6268 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6269 isl_set_copy(dom), &todo, max);
6271 for (i = 1; i < map->n; ++i) {
6272 isl_map *lt, *le;
6273 isl_map *res_i;
6274 isl_set *todo_i;
6275 isl_space *dim = isl_space_range(isl_map_get_space(res));
6277 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6278 isl_set_copy(dom), &todo_i, max);
6280 if (max) {
6281 lt = isl_map_lex_lt(isl_space_copy(dim));
6282 le = isl_map_lex_le(dim);
6283 } else {
6284 lt = isl_map_lex_gt(isl_space_copy(dim));
6285 le = isl_map_lex_ge(dim);
6287 lt = isl_map_apply_range(isl_map_copy(res), lt);
6288 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6289 le = isl_map_apply_range(isl_map_copy(res_i), le);
6290 le = isl_map_intersect(le, isl_map_copy(res));
6292 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6293 res = isl_map_intersect_domain(res,
6294 isl_set_copy(todo_i));
6295 res_i = isl_map_intersect_domain(res_i,
6296 isl_set_copy(todo));
6299 res = isl_map_union_disjoint(res, res_i);
6300 res = isl_map_union_disjoint(res, lt);
6301 res = isl_map_union_disjoint(res, le);
6303 todo = isl_set_intersect(todo, todo_i);
6306 isl_set_free(dom);
6307 isl_map_free(map);
6309 if (empty)
6310 *empty = todo;
6311 else
6312 isl_set_free(todo);
6314 return res;
6315 error:
6316 if (empty)
6317 *empty = NULL;
6318 isl_set_free(dom);
6319 isl_map_free(map);
6320 return NULL;
6323 __isl_give isl_map *isl_map_partial_lexmax(
6324 __isl_take isl_map *map, __isl_take isl_set *dom,
6325 __isl_give isl_set **empty)
6327 return isl_map_partial_lexopt(map, dom, empty, 1);
6330 __isl_give isl_map *isl_map_partial_lexmin(
6331 __isl_take isl_map *map, __isl_take isl_set *dom,
6332 __isl_give isl_set **empty)
6334 return isl_map_partial_lexopt(map, dom, empty, 0);
6337 __isl_give isl_set *isl_set_partial_lexmin(
6338 __isl_take isl_set *set, __isl_take isl_set *dom,
6339 __isl_give isl_set **empty)
6341 return (struct isl_set *)
6342 isl_map_partial_lexmin((struct isl_map *)set,
6343 dom, empty);
6346 __isl_give isl_set *isl_set_partial_lexmax(
6347 __isl_take isl_set *set, __isl_take isl_set *dom,
6348 __isl_give isl_set **empty)
6350 return (struct isl_set *)
6351 isl_map_partial_lexmax((struct isl_map *)set,
6352 dom, empty);
6355 /* Compute the lexicographic minimum (or maximum if "max" is set)
6356 * of "bmap" over its domain.
6358 * Since we are not interested in the part of the domain space where
6359 * there is no solution, we initialize the domain to those constraints
6360 * of "bmap" that only involve the parameters and the input dimensions.
6361 * This relieves the parametric programming engine from detecting those
6362 * inequalities and transferring them to the context. More importantly,
6363 * it ensures that those inequalities are transferred first and not
6364 * intermixed with inequalities that actually split the domain.
6366 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6368 int n_div;
6369 int n_out;
6370 isl_basic_map *copy;
6371 isl_basic_set *dom;
6373 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6374 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6375 copy = isl_basic_map_copy(bmap);
6376 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6377 isl_dim_div, 0, n_div);
6378 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6379 isl_dim_out, 0, n_out);
6380 dom = isl_basic_map_domain(copy);
6381 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6384 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6386 return isl_basic_map_lexopt(bmap, 0);
6389 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6391 return isl_basic_map_lexopt(bmap, 1);
6394 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6396 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6399 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6401 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6404 /* Extract the first and only affine expression from list
6405 * and then add it to *pwaff with the given dom.
6406 * This domain is known to be disjoint from other domains
6407 * because of the way isl_basic_map_foreach_lexmax works.
6409 static int update_dim_opt(__isl_take isl_basic_set *dom,
6410 __isl_take isl_aff_list *list, void *user)
6412 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6413 isl_aff *aff;
6414 isl_pw_aff **pwaff = user;
6415 isl_pw_aff *pwaff_i;
6417 if (!list)
6418 goto error;
6419 if (isl_aff_list_n_aff(list) != 1)
6420 isl_die(ctx, isl_error_internal,
6421 "expecting single element list", goto error);
6423 aff = isl_aff_list_get_aff(list, 0);
6424 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6426 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6428 isl_aff_list_free(list);
6430 return 0;
6431 error:
6432 isl_basic_set_free(dom);
6433 isl_aff_list_free(list);
6434 return -1;
6437 /* Given a basic map with one output dimension, compute the minimum or
6438 * maximum of that dimension as an isl_pw_aff.
6440 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6441 * call update_dim_opt on each leaf of the result.
6443 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6444 int max)
6446 isl_space *dim = isl_basic_map_get_space(bmap);
6447 isl_pw_aff *pwaff;
6448 int r;
6450 dim = isl_space_from_domain(isl_space_domain(dim));
6451 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6452 pwaff = isl_pw_aff_empty(dim);
6454 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6455 if (r < 0)
6456 return isl_pw_aff_free(pwaff);
6458 return pwaff;
6461 /* Compute the minimum or maximum of the given output dimension
6462 * as a function of the parameters and the input dimensions,
6463 * but independently of the other output dimensions.
6465 * We first project out the other output dimension and then compute
6466 * the "lexicographic" maximum in each basic map, combining the results
6467 * using isl_pw_aff_union_max.
6469 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6470 int max)
6472 int i;
6473 isl_pw_aff *pwaff;
6474 unsigned n_out;
6476 n_out = isl_map_dim(map, isl_dim_out);
6477 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6478 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6479 if (!map)
6480 return NULL;
6482 if (map->n == 0) {
6483 isl_space *dim = isl_map_get_space(map);
6484 isl_map_free(map);
6485 return isl_pw_aff_empty(dim);
6488 pwaff = basic_map_dim_opt(map->p[0], max);
6489 for (i = 1; i < map->n; ++i) {
6490 isl_pw_aff *pwaff_i;
6492 pwaff_i = basic_map_dim_opt(map->p[i], max);
6493 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6496 isl_map_free(map);
6498 return pwaff;
6501 /* Compute the maximum of the given output dimension as a function of the
6502 * parameters and input dimensions, but independently of
6503 * the other output dimensions.
6505 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6507 return map_dim_opt(map, pos, 1);
6510 /* Compute the minimum or maximum of the given set dimension
6511 * as a function of the parameters,
6512 * but independently of the other set dimensions.
6514 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6515 int max)
6517 return map_dim_opt(set, pos, max);
6520 /* Compute the maximum of the given set dimension as a function of the
6521 * parameters, but independently of the other set dimensions.
6523 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6525 return set_dim_opt(set, pos, 1);
6528 /* Compute the minimum of the given set dimension as a function of the
6529 * parameters, but independently of the other set dimensions.
6531 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6533 return set_dim_opt(set, pos, 0);
6536 /* Apply a preimage specified by "mat" on the parameters of "bset".
6537 * bset is assumed to have only parameters and divs.
6539 static struct isl_basic_set *basic_set_parameter_preimage(
6540 struct isl_basic_set *bset, struct isl_mat *mat)
6542 unsigned nparam;
6544 if (!bset || !mat)
6545 goto error;
6547 bset->dim = isl_space_cow(bset->dim);
6548 if (!bset->dim)
6549 goto error;
6551 nparam = isl_basic_set_dim(bset, isl_dim_param);
6553 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6555 bset->dim->nparam = 0;
6556 bset->dim->n_out = nparam;
6557 bset = isl_basic_set_preimage(bset, mat);
6558 if (bset) {
6559 bset->dim->nparam = bset->dim->n_out;
6560 bset->dim->n_out = 0;
6562 return bset;
6563 error:
6564 isl_mat_free(mat);
6565 isl_basic_set_free(bset);
6566 return NULL;
6569 /* Apply a preimage specified by "mat" on the parameters of "set".
6570 * set is assumed to have only parameters and divs.
6572 static struct isl_set *set_parameter_preimage(
6573 struct isl_set *set, struct isl_mat *mat)
6575 isl_space *dim = NULL;
6576 unsigned nparam;
6578 if (!set || !mat)
6579 goto error;
6581 dim = isl_space_copy(set->dim);
6582 dim = isl_space_cow(dim);
6583 if (!dim)
6584 goto error;
6586 nparam = isl_set_dim(set, isl_dim_param);
6588 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6590 dim->nparam = 0;
6591 dim->n_out = nparam;
6592 isl_set_reset_space(set, dim);
6593 set = isl_set_preimage(set, mat);
6594 if (!set)
6595 goto error2;
6596 dim = isl_space_copy(set->dim);
6597 dim = isl_space_cow(dim);
6598 if (!dim)
6599 goto error2;
6600 dim->nparam = dim->n_out;
6601 dim->n_out = 0;
6602 isl_set_reset_space(set, dim);
6603 return set;
6604 error:
6605 isl_space_free(dim);
6606 isl_mat_free(mat);
6607 error2:
6608 isl_set_free(set);
6609 return NULL;
6612 /* Intersect the basic set "bset" with the affine space specified by the
6613 * equalities in "eq".
6615 static struct isl_basic_set *basic_set_append_equalities(
6616 struct isl_basic_set *bset, struct isl_mat *eq)
6618 int i, k;
6619 unsigned len;
6621 if (!bset || !eq)
6622 goto error;
6624 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6625 eq->n_row, 0);
6626 if (!bset)
6627 goto error;
6629 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6630 for (i = 0; i < eq->n_row; ++i) {
6631 k = isl_basic_set_alloc_equality(bset);
6632 if (k < 0)
6633 goto error;
6634 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6635 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6637 isl_mat_free(eq);
6639 bset = isl_basic_set_gauss(bset, NULL);
6640 bset = isl_basic_set_finalize(bset);
6642 return bset;
6643 error:
6644 isl_mat_free(eq);
6645 isl_basic_set_free(bset);
6646 return NULL;
6649 /* Intersect the set "set" with the affine space specified by the
6650 * equalities in "eq".
6652 static struct isl_set *set_append_equalities(struct isl_set *set,
6653 struct isl_mat *eq)
6655 int i;
6657 if (!set || !eq)
6658 goto error;
6660 for (i = 0; i < set->n; ++i) {
6661 set->p[i] = basic_set_append_equalities(set->p[i],
6662 isl_mat_copy(eq));
6663 if (!set->p[i])
6664 goto error;
6666 isl_mat_free(eq);
6667 return set;
6668 error:
6669 isl_mat_free(eq);
6670 isl_set_free(set);
6671 return NULL;
6674 /* Given a basic set "bset" that only involves parameters and existentially
6675 * quantified variables, return the index of the first equality
6676 * that only involves parameters. If there is no such equality then
6677 * return bset->n_eq.
6679 * This function assumes that isl_basic_set_gauss has been called on "bset".
6681 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6683 int i, j;
6684 unsigned nparam, n_div;
6686 if (!bset)
6687 return -1;
6689 nparam = isl_basic_set_dim(bset, isl_dim_param);
6690 n_div = isl_basic_set_dim(bset, isl_dim_div);
6692 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6693 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6694 ++i;
6697 return i;
6700 /* Compute an explicit representation for the existentially quantified
6701 * variables in "bset" by computing the "minimal value" of the set
6702 * variables. Since there are no set variables, the computation of
6703 * the minimal value essentially computes an explicit representation
6704 * of the non-empty part(s) of "bset".
6706 * The input only involves parameters and existentially quantified variables.
6707 * All equalities among parameters have been removed.
6709 * Since the existentially quantified variables in the result are in general
6710 * going to be different from those in the input, we first replace
6711 * them by the minimal number of variables based on their equalities.
6712 * This should simplify the parametric integer programming.
6714 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6716 isl_morph *morph1, *morph2;
6717 isl_set *set;
6718 unsigned n;
6720 if (!bset)
6721 return NULL;
6722 if (bset->n_eq == 0)
6723 return isl_basic_set_lexmin(bset);
6725 morph1 = isl_basic_set_parameter_compression(bset);
6726 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6727 bset = isl_basic_set_lift(bset);
6728 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6729 bset = isl_morph_basic_set(morph2, bset);
6730 n = isl_basic_set_dim(bset, isl_dim_set);
6731 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6733 set = isl_basic_set_lexmin(bset);
6735 set = isl_morph_set(isl_morph_inverse(morph1), set);
6737 return set;
6740 /* Project the given basic set onto its parameter domain, possibly introducing
6741 * new, explicit, existential variables in the constraints.
6742 * The input has parameters and (possibly implicit) existential variables.
6743 * The output has the same parameters, but only
6744 * explicit existentially quantified variables.
6746 * The actual projection is performed by pip, but pip doesn't seem
6747 * to like equalities very much, so we first remove the equalities
6748 * among the parameters by performing a variable compression on
6749 * the parameters. Afterward, an inverse transformation is performed
6750 * and the equalities among the parameters are inserted back in.
6752 * The variable compression on the parameters may uncover additional
6753 * equalities that were only implicit before. We therefore check
6754 * if there are any new parameter equalities in the result and
6755 * if so recurse. The removal of parameter equalities is required
6756 * for the parameter compression performed by base_compute_divs.
6758 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6760 int i;
6761 struct isl_mat *eq;
6762 struct isl_mat *T, *T2;
6763 struct isl_set *set;
6764 unsigned nparam;
6766 bset = isl_basic_set_cow(bset);
6767 if (!bset)
6768 return NULL;
6770 if (bset->n_eq == 0)
6771 return base_compute_divs(bset);
6773 bset = isl_basic_set_gauss(bset, NULL);
6774 if (!bset)
6775 return NULL;
6776 if (isl_basic_set_plain_is_empty(bset))
6777 return isl_set_from_basic_set(bset);
6779 i = first_parameter_equality(bset);
6780 if (i == bset->n_eq)
6781 return base_compute_divs(bset);
6783 nparam = isl_basic_set_dim(bset, isl_dim_param);
6784 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6785 0, 1 + nparam);
6786 eq = isl_mat_cow(eq);
6787 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6788 if (T && T->n_col == 0) {
6789 isl_mat_free(T);
6790 isl_mat_free(T2);
6791 isl_mat_free(eq);
6792 bset = isl_basic_set_set_to_empty(bset);
6793 return isl_set_from_basic_set(bset);
6795 bset = basic_set_parameter_preimage(bset, T);
6797 i = first_parameter_equality(bset);
6798 if (!bset)
6799 set = NULL;
6800 else if (i == bset->n_eq)
6801 set = base_compute_divs(bset);
6802 else
6803 set = parameter_compute_divs(bset);
6804 set = set_parameter_preimage(set, T2);
6805 set = set_append_equalities(set, eq);
6806 return set;
6809 /* Insert the divs from "ls" before those of "bmap".
6811 * The number of columns is not changed, which means that the last
6812 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6813 * The caller is responsible for removing the same number of dimensions
6814 * from the space of "bmap".
6816 static __isl_give isl_basic_map *insert_divs_from_local_space(
6817 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6819 int i;
6820 int n_div;
6821 int old_n_div;
6823 n_div = isl_local_space_dim(ls, isl_dim_div);
6824 if (n_div == 0)
6825 return bmap;
6827 old_n_div = bmap->n_div;
6828 bmap = insert_div_rows(bmap, n_div);
6829 if (!bmap)
6830 return NULL;
6832 for (i = 0; i < n_div; ++i) {
6833 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6834 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6837 return bmap;
6840 /* Replace the space of "bmap" by the space and divs of "ls".
6842 * If "ls" has any divs, then we simplify the result since we may
6843 * have discovered some additional equalities that could simplify
6844 * the div expressions.
6846 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6847 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6849 int n_div;
6851 bmap = isl_basic_map_cow(bmap);
6852 if (!bmap || !ls)
6853 goto error;
6855 n_div = isl_local_space_dim(ls, isl_dim_div);
6856 bmap = insert_divs_from_local_space(bmap, ls);
6857 if (!bmap)
6858 goto error;
6860 isl_space_free(bmap->dim);
6861 bmap->dim = isl_local_space_get_space(ls);
6862 if (!bmap->dim)
6863 goto error;
6865 isl_local_space_free(ls);
6866 if (n_div > 0)
6867 bmap = isl_basic_map_simplify(bmap);
6868 bmap = isl_basic_map_finalize(bmap);
6869 return bmap;
6870 error:
6871 isl_basic_map_free(bmap);
6872 isl_local_space_free(ls);
6873 return NULL;
6876 /* Replace the space of "map" by the space and divs of "ls".
6878 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6879 __isl_take isl_local_space *ls)
6881 int i;
6883 map = isl_map_cow(map);
6884 if (!map || !ls)
6885 goto error;
6887 for (i = 0; i < map->n; ++i) {
6888 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6889 isl_local_space_copy(ls));
6890 if (!map->p[i])
6891 goto error;
6893 isl_space_free(map->dim);
6894 map->dim = isl_local_space_get_space(ls);
6895 if (!map->dim)
6896 goto error;
6898 isl_local_space_free(ls);
6899 return map;
6900 error:
6901 isl_local_space_free(ls);
6902 isl_map_free(map);
6903 return NULL;
6906 /* Compute an explicit representation for the existentially
6907 * quantified variables for which do not know any explicit representation yet.
6909 * We first sort the existentially quantified variables so that the
6910 * existentially quantified variables for which we already have an explicit
6911 * representation are placed before those for which we do not.
6912 * The input dimensions, the output dimensions and the existentially
6913 * quantified variables for which we already have an explicit
6914 * representation are then turned into parameters.
6915 * compute_divs returns a map with the same parameters and
6916 * no input or output dimensions and the dimension specification
6917 * is reset to that of the input, including the existentially quantified
6918 * variables for which we already had an explicit representation.
6920 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6922 struct isl_basic_set *bset;
6923 struct isl_set *set;
6924 struct isl_map *map;
6925 isl_space *dim;
6926 isl_local_space *ls;
6927 unsigned nparam;
6928 unsigned n_in;
6929 unsigned n_out;
6930 unsigned n_known;
6931 int i;
6933 bmap = isl_basic_map_sort_divs(bmap);
6934 bmap = isl_basic_map_cow(bmap);
6935 if (!bmap)
6936 return NULL;
6938 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6939 if (isl_int_is_zero(bmap->div[n_known][0]))
6940 break;
6942 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6943 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6944 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6945 dim = isl_space_set_alloc(bmap->ctx,
6946 nparam + n_in + n_out + n_known, 0);
6947 if (!dim)
6948 goto error;
6950 ls = isl_basic_map_get_local_space(bmap);
6951 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6952 n_known, bmap->n_div - n_known);
6953 if (n_known > 0) {
6954 for (i = n_known; i < bmap->n_div; ++i)
6955 swap_div(bmap, i - n_known, i);
6956 bmap->n_div -= n_known;
6957 bmap->extra -= n_known;
6959 bmap = isl_basic_map_reset_space(bmap, dim);
6960 bset = (struct isl_basic_set *)bmap;
6962 set = parameter_compute_divs(bset);
6963 map = (struct isl_map *)set;
6964 map = replace_space_by_local_space(map, ls);
6966 return map;
6967 error:
6968 isl_basic_map_free(bmap);
6969 return NULL;
6972 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6974 int i;
6975 unsigned off;
6977 if (!bmap)
6978 return -1;
6980 off = isl_space_dim(bmap->dim, isl_dim_all);
6981 for (i = 0; i < bmap->n_div; ++i) {
6982 if (isl_int_is_zero(bmap->div[i][0]))
6983 return 0;
6984 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6985 return -1);
6987 return 1;
6990 static int map_divs_known(__isl_keep isl_map *map)
6992 int i;
6994 if (!map)
6995 return -1;
6997 for (i = 0; i < map->n; ++i) {
6998 int known = isl_basic_map_divs_known(map->p[i]);
6999 if (known <= 0)
7000 return known;
7003 return 1;
7006 /* If bmap contains any unknown divs, then compute explicit
7007 * expressions for them. However, this computation may be
7008 * quite expensive, so first try to remove divs that aren't
7009 * strictly needed.
7011 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7013 int known;
7014 struct isl_map *map;
7016 known = isl_basic_map_divs_known(bmap);
7017 if (known < 0)
7018 goto error;
7019 if (known)
7020 return isl_map_from_basic_map(bmap);
7022 bmap = isl_basic_map_drop_redundant_divs(bmap);
7024 known = isl_basic_map_divs_known(bmap);
7025 if (known < 0)
7026 goto error;
7027 if (known)
7028 return isl_map_from_basic_map(bmap);
7030 map = compute_divs(bmap);
7031 return map;
7032 error:
7033 isl_basic_map_free(bmap);
7034 return NULL;
7037 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7039 int i;
7040 int known;
7041 struct isl_map *res;
7043 if (!map)
7044 return NULL;
7045 if (map->n == 0)
7046 return map;
7048 known = map_divs_known(map);
7049 if (known < 0) {
7050 isl_map_free(map);
7051 return NULL;
7053 if (known)
7054 return map;
7056 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7057 for (i = 1 ; i < map->n; ++i) {
7058 struct isl_map *r2;
7059 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7060 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7061 res = isl_map_union_disjoint(res, r2);
7062 else
7063 res = isl_map_union(res, r2);
7065 isl_map_free(map);
7067 return res;
7070 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7072 return (struct isl_set *)
7073 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7076 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7078 return (struct isl_set *)
7079 isl_map_compute_divs((struct isl_map *)set);
7082 struct isl_set *isl_map_domain(struct isl_map *map)
7084 int i;
7085 struct isl_set *set;
7087 if (!map)
7088 goto error;
7090 map = isl_map_cow(map);
7091 if (!map)
7092 return NULL;
7094 set = (struct isl_set *)map;
7095 set->dim = isl_space_domain(set->dim);
7096 if (!set->dim)
7097 goto error;
7098 for (i = 0; i < map->n; ++i) {
7099 set->p[i] = isl_basic_map_domain(map->p[i]);
7100 if (!set->p[i])
7101 goto error;
7103 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7104 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7105 return set;
7106 error:
7107 isl_map_free(map);
7108 return NULL;
7111 /* Return the union of "map1" and "map2", where we assume for now that
7112 * "map1" and "map2" are disjoint. Note that the basic maps inside
7113 * "map1" or "map2" may not be disjoint from each other.
7114 * Also note that this function is also called from isl_map_union,
7115 * which takes care of handling the situation where "map1" and "map2"
7116 * may not be disjoint.
7118 * If one of the inputs is empty, we can simply return the other input.
7119 * Similarly, if one of the inputs is universal, then it is equal to the union.
7121 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7122 __isl_take isl_map *map2)
7124 int i;
7125 unsigned flags = 0;
7126 struct isl_map *map = NULL;
7127 int is_universe;
7129 if (!map1 || !map2)
7130 goto error;
7132 if (!isl_space_is_equal(map1->dim, map2->dim))
7133 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7134 "spaces don't match", goto error);
7136 if (map1->n == 0) {
7137 isl_map_free(map1);
7138 return map2;
7140 if (map2->n == 0) {
7141 isl_map_free(map2);
7142 return map1;
7145 is_universe = isl_map_plain_is_universe(map1);
7146 if (is_universe < 0)
7147 goto error;
7148 if (is_universe) {
7149 isl_map_free(map2);
7150 return map1;
7153 is_universe = isl_map_plain_is_universe(map2);
7154 if (is_universe < 0)
7155 goto error;
7156 if (is_universe) {
7157 isl_map_free(map1);
7158 return map2;
7161 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7162 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7163 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7165 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7166 map1->n + map2->n, flags);
7167 if (!map)
7168 goto error;
7169 for (i = 0; i < map1->n; ++i) {
7170 map = isl_map_add_basic_map(map,
7171 isl_basic_map_copy(map1->p[i]));
7172 if (!map)
7173 goto error;
7175 for (i = 0; i < map2->n; ++i) {
7176 map = isl_map_add_basic_map(map,
7177 isl_basic_map_copy(map2->p[i]));
7178 if (!map)
7179 goto error;
7181 isl_map_free(map1);
7182 isl_map_free(map2);
7183 return map;
7184 error:
7185 isl_map_free(map);
7186 isl_map_free(map1);
7187 isl_map_free(map2);
7188 return NULL;
7191 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7192 * guaranteed to be disjoint by the caller.
7194 * Note that this functions is called from within isl_map_make_disjoint,
7195 * so we have to be careful not to touch the constraints of the inputs
7196 * in any way.
7198 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7199 __isl_take isl_map *map2)
7201 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7204 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7205 * not be disjoint. The parameters are assumed to have been aligned.
7207 * We currently simply call map_union_disjoint, the internal operation
7208 * of which does not really depend on the inputs being disjoint.
7209 * If the result contains more than one basic map, then we clear
7210 * the disjoint flag since the result may contain basic maps from
7211 * both inputs and these are not guaranteed to be disjoint.
7213 * As a special case, if "map1" and "map2" are obviously equal,
7214 * then we simply return "map1".
7216 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7217 __isl_take isl_map *map2)
7219 int equal;
7221 if (!map1 || !map2)
7222 goto error;
7224 equal = isl_map_plain_is_equal(map1, map2);
7225 if (equal < 0)
7226 goto error;
7227 if (equal) {
7228 isl_map_free(map2);
7229 return map1;
7232 map1 = map_union_disjoint(map1, map2);
7233 if (!map1)
7234 return NULL;
7235 if (map1->n > 1)
7236 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7237 return map1;
7238 error:
7239 isl_map_free(map1);
7240 isl_map_free(map2);
7241 return NULL;
7244 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7245 * not be disjoint.
7247 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7248 __isl_take isl_map *map2)
7250 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7253 struct isl_set *isl_set_union_disjoint(
7254 struct isl_set *set1, struct isl_set *set2)
7256 return (struct isl_set *)
7257 isl_map_union_disjoint(
7258 (struct isl_map *)set1, (struct isl_map *)set2);
7261 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7263 return (struct isl_set *)
7264 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7267 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7268 * the results.
7270 * "map" and "set" are assumed to be compatible and non-NULL.
7272 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7273 __isl_take isl_set *set,
7274 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7275 __isl_take isl_basic_set *bset))
7277 unsigned flags = 0;
7278 struct isl_map *result;
7279 int i, j;
7281 if (isl_set_plain_is_universe(set)) {
7282 isl_set_free(set);
7283 return map;
7286 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7287 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7288 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7290 result = isl_map_alloc_space(isl_space_copy(map->dim),
7291 map->n * set->n, flags);
7292 for (i = 0; result && i < map->n; ++i)
7293 for (j = 0; j < set->n; ++j) {
7294 result = isl_map_add_basic_map(result,
7295 fn(isl_basic_map_copy(map->p[i]),
7296 isl_basic_set_copy(set->p[j])));
7297 if (!result)
7298 break;
7301 isl_map_free(map);
7302 isl_set_free(set);
7303 return result;
7306 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7307 __isl_take isl_set *set)
7309 if (!map || !set)
7310 goto error;
7312 if (!isl_map_compatible_range(map, set))
7313 isl_die(set->ctx, isl_error_invalid,
7314 "incompatible spaces", goto error);
7316 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7317 error:
7318 isl_map_free(map);
7319 isl_set_free(set);
7320 return NULL;
7323 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7324 __isl_take isl_set *set)
7326 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7329 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7330 __isl_take isl_set *set)
7332 if (!map || !set)
7333 goto error;
7335 if (!isl_map_compatible_domain(map, set))
7336 isl_die(set->ctx, isl_error_invalid,
7337 "incompatible spaces", goto error);
7339 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7340 error:
7341 isl_map_free(map);
7342 isl_set_free(set);
7343 return NULL;
7346 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7347 __isl_take isl_set *set)
7349 return isl_map_align_params_map_map_and(map, set,
7350 &map_intersect_domain);
7353 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7354 __isl_take isl_map *map2)
7356 if (!map1 || !map2)
7357 goto error;
7358 map1 = isl_map_reverse(map1);
7359 map1 = isl_map_apply_range(map1, map2);
7360 return isl_map_reverse(map1);
7361 error:
7362 isl_map_free(map1);
7363 isl_map_free(map2);
7364 return NULL;
7367 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7368 __isl_take isl_map *map2)
7370 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7373 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7374 __isl_take isl_map *map2)
7376 isl_space *dim_result;
7377 struct isl_map *result;
7378 int i, j;
7380 if (!map1 || !map2)
7381 goto error;
7383 dim_result = isl_space_join(isl_space_copy(map1->dim),
7384 isl_space_copy(map2->dim));
7386 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7387 if (!result)
7388 goto error;
7389 for (i = 0; i < map1->n; ++i)
7390 for (j = 0; j < map2->n; ++j) {
7391 result = isl_map_add_basic_map(result,
7392 isl_basic_map_apply_range(
7393 isl_basic_map_copy(map1->p[i]),
7394 isl_basic_map_copy(map2->p[j])));
7395 if (!result)
7396 goto error;
7398 isl_map_free(map1);
7399 isl_map_free(map2);
7400 if (result && result->n <= 1)
7401 ISL_F_SET(result, ISL_MAP_DISJOINT);
7402 return result;
7403 error:
7404 isl_map_free(map1);
7405 isl_map_free(map2);
7406 return NULL;
7409 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7410 __isl_take isl_map *map2)
7412 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7416 * returns range - domain
7418 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7420 isl_space *dims, *target_dim;
7421 struct isl_basic_set *bset;
7422 unsigned dim;
7423 unsigned nparam;
7424 int i;
7426 if (!bmap)
7427 goto error;
7428 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
7429 bmap->dim, isl_dim_out),
7430 goto error);
7431 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7432 dim = isl_basic_map_n_in(bmap);
7433 nparam = isl_basic_map_n_param(bmap);
7434 bset = isl_basic_set_from_basic_map(bmap);
7435 bset = isl_basic_set_cow(bset);
7436 dims = isl_basic_set_get_space(bset);
7437 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7438 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7439 bset = isl_basic_set_swap_vars(bset, 2*dim);
7440 for (i = 0; i < dim; ++i) {
7441 int j = isl_basic_map_alloc_equality(
7442 (struct isl_basic_map *)bset);
7443 if (j < 0) {
7444 bset = isl_basic_set_free(bset);
7445 break;
7447 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7448 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7449 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7450 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7452 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7453 bset = isl_basic_set_reset_space(bset, target_dim);
7454 return bset;
7455 error:
7456 isl_basic_map_free(bmap);
7457 return NULL;
7461 * returns range - domain
7463 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7465 int i;
7466 isl_space *dim;
7467 struct isl_set *result;
7469 if (!map)
7470 return NULL;
7472 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
7473 map->dim, isl_dim_out),
7474 goto error);
7475 dim = isl_map_get_space(map);
7476 dim = isl_space_domain(dim);
7477 result = isl_set_alloc_space(dim, map->n, 0);
7478 if (!result)
7479 goto error;
7480 for (i = 0; i < map->n; ++i)
7481 result = isl_set_add_basic_set(result,
7482 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7483 isl_map_free(map);
7484 return result;
7485 error:
7486 isl_map_free(map);
7487 return NULL;
7491 * returns [domain -> range] -> range - domain
7493 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7494 __isl_take isl_basic_map *bmap)
7496 int i, k;
7497 isl_space *dim;
7498 isl_basic_map *domain;
7499 int nparam, n;
7500 unsigned total;
7502 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
7503 isl_die(bmap->ctx, isl_error_invalid,
7504 "domain and range don't match", goto error);
7506 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7507 n = isl_basic_map_dim(bmap, isl_dim_in);
7509 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7510 domain = isl_basic_map_universe(dim);
7512 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7513 bmap = isl_basic_map_apply_range(bmap, domain);
7514 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7516 total = isl_basic_map_total_dim(bmap);
7518 for (i = 0; i < n; ++i) {
7519 k = isl_basic_map_alloc_equality(bmap);
7520 if (k < 0)
7521 goto error;
7522 isl_seq_clr(bmap->eq[k], 1 + total);
7523 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7524 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7525 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7528 bmap = isl_basic_map_gauss(bmap, NULL);
7529 return isl_basic_map_finalize(bmap);
7530 error:
7531 isl_basic_map_free(bmap);
7532 return NULL;
7536 * returns [domain -> range] -> range - domain
7538 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7540 int i;
7541 isl_space *domain_dim;
7543 if (!map)
7544 return NULL;
7546 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
7547 isl_die(map->ctx, isl_error_invalid,
7548 "domain and range don't match", goto error);
7550 map = isl_map_cow(map);
7551 if (!map)
7552 return NULL;
7554 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7555 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7556 map->dim = isl_space_join(map->dim, domain_dim);
7557 if (!map->dim)
7558 goto error;
7559 for (i = 0; i < map->n; ++i) {
7560 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7561 if (!map->p[i])
7562 goto error;
7564 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7565 return map;
7566 error:
7567 isl_map_free(map);
7568 return NULL;
7571 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7573 struct isl_basic_map *bmap;
7574 unsigned nparam;
7575 unsigned dim;
7576 int i;
7578 if (!dims)
7579 return NULL;
7581 nparam = dims->nparam;
7582 dim = dims->n_out;
7583 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7584 if (!bmap)
7585 goto error;
7587 for (i = 0; i < dim; ++i) {
7588 int j = isl_basic_map_alloc_equality(bmap);
7589 if (j < 0)
7590 goto error;
7591 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7592 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7593 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7595 return isl_basic_map_finalize(bmap);
7596 error:
7597 isl_basic_map_free(bmap);
7598 return NULL;
7601 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7603 if (!dim)
7604 return NULL;
7605 if (dim->n_in != dim->n_out)
7606 isl_die(dim->ctx, isl_error_invalid,
7607 "number of input and output dimensions needs to be "
7608 "the same", goto error);
7609 return basic_map_identity(dim);
7610 error:
7611 isl_space_free(dim);
7612 return NULL;
7615 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7617 if (!model || !model->dim)
7618 return NULL;
7619 return isl_basic_map_identity(isl_space_copy(model->dim));
7622 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7624 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7627 struct isl_map *isl_map_identity_like(struct isl_map *model)
7629 if (!model || !model->dim)
7630 return NULL;
7631 return isl_map_identity(isl_space_copy(model->dim));
7634 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7636 if (!model || !model->dim)
7637 return NULL;
7638 return isl_map_identity(isl_space_copy(model->dim));
7641 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7643 isl_space *dim = isl_set_get_space(set);
7644 isl_map *id;
7645 id = isl_map_identity(isl_space_map_from_set(dim));
7646 return isl_map_intersect_range(id, set);
7649 /* Construct a basic set with all set dimensions having only non-negative
7650 * values.
7652 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7653 __isl_take isl_space *space)
7655 int i;
7656 unsigned nparam;
7657 unsigned dim;
7658 struct isl_basic_set *bset;
7660 if (!space)
7661 return NULL;
7662 nparam = space->nparam;
7663 dim = space->n_out;
7664 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7665 if (!bset)
7666 return NULL;
7667 for (i = 0; i < dim; ++i) {
7668 int k = isl_basic_set_alloc_inequality(bset);
7669 if (k < 0)
7670 goto error;
7671 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7672 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7674 return bset;
7675 error:
7676 isl_basic_set_free(bset);
7677 return NULL;
7680 /* Construct the half-space x_pos >= 0.
7682 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7683 int pos)
7685 int k;
7686 isl_basic_set *nonneg;
7688 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7689 k = isl_basic_set_alloc_inequality(nonneg);
7690 if (k < 0)
7691 goto error;
7692 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7693 isl_int_set_si(nonneg->ineq[k][pos], 1);
7695 return isl_basic_set_finalize(nonneg);
7696 error:
7697 isl_basic_set_free(nonneg);
7698 return NULL;
7701 /* Construct the half-space x_pos <= -1.
7703 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7705 int k;
7706 isl_basic_set *neg;
7708 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7709 k = isl_basic_set_alloc_inequality(neg);
7710 if (k < 0)
7711 goto error;
7712 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7713 isl_int_set_si(neg->ineq[k][0], -1);
7714 isl_int_set_si(neg->ineq[k][pos], -1);
7716 return isl_basic_set_finalize(neg);
7717 error:
7718 isl_basic_set_free(neg);
7719 return NULL;
7722 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7723 enum isl_dim_type type, unsigned first, unsigned n)
7725 int i;
7726 isl_basic_set *nonneg;
7727 isl_basic_set *neg;
7729 if (!set)
7730 return NULL;
7731 if (n == 0)
7732 return set;
7734 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7736 for (i = 0; i < n; ++i) {
7737 nonneg = nonneg_halfspace(isl_set_get_space(set),
7738 pos(set->dim, type) + first + i);
7739 neg = neg_halfspace(isl_set_get_space(set),
7740 pos(set->dim, type) + first + i);
7742 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7745 return set;
7746 error:
7747 isl_set_free(set);
7748 return NULL;
7751 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7752 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7753 void *user)
7755 isl_set *half;
7757 if (!set)
7758 return -1;
7759 if (isl_set_plain_is_empty(set)) {
7760 isl_set_free(set);
7761 return 0;
7763 if (first == len)
7764 return fn(set, signs, user);
7766 signs[first] = 1;
7767 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7768 1 + first));
7769 half = isl_set_intersect(half, isl_set_copy(set));
7770 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7771 goto error;
7773 signs[first] = -1;
7774 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7775 1 + first));
7776 half = isl_set_intersect(half, set);
7777 return foreach_orthant(half, signs, first + 1, len, fn, user);
7778 error:
7779 isl_set_free(set);
7780 return -1;
7783 /* Call "fn" on the intersections of "set" with each of the orthants
7784 * (except for obviously empty intersections). The orthant is identified
7785 * by the signs array, with each entry having value 1 or -1 according
7786 * to the sign of the corresponding variable.
7788 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7789 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7790 void *user)
7792 unsigned nparam;
7793 unsigned nvar;
7794 int *signs;
7795 int r;
7797 if (!set)
7798 return -1;
7799 if (isl_set_plain_is_empty(set))
7800 return 0;
7802 nparam = isl_set_dim(set, isl_dim_param);
7803 nvar = isl_set_dim(set, isl_dim_set);
7805 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7807 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7808 fn, user);
7810 free(signs);
7812 return r;
7815 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7817 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7820 int isl_basic_map_is_subset(
7821 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7823 int is_subset;
7824 struct isl_map *map1;
7825 struct isl_map *map2;
7827 if (!bmap1 || !bmap2)
7828 return -1;
7830 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7831 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7833 is_subset = isl_map_is_subset(map1, map2);
7835 isl_map_free(map1);
7836 isl_map_free(map2);
7838 return is_subset;
7841 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7842 __isl_keep isl_basic_set *bset2)
7844 return isl_basic_map_is_subset(bset1, bset2);
7847 int isl_basic_map_is_equal(
7848 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7850 int is_subset;
7852 if (!bmap1 || !bmap2)
7853 return -1;
7854 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7855 if (is_subset != 1)
7856 return is_subset;
7857 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7858 return is_subset;
7861 int isl_basic_set_is_equal(
7862 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7864 return isl_basic_map_is_equal(
7865 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7868 int isl_map_is_empty(struct isl_map *map)
7870 int i;
7871 int is_empty;
7873 if (!map)
7874 return -1;
7875 for (i = 0; i < map->n; ++i) {
7876 is_empty = isl_basic_map_is_empty(map->p[i]);
7877 if (is_empty < 0)
7878 return -1;
7879 if (!is_empty)
7880 return 0;
7882 return 1;
7885 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7887 return map ? map->n == 0 : -1;
7890 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7892 return isl_map_plain_is_empty(map);
7895 int isl_set_plain_is_empty(struct isl_set *set)
7897 return set ? set->n == 0 : -1;
7900 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7902 return isl_set_plain_is_empty(set);
7905 int isl_set_is_empty(struct isl_set *set)
7907 return isl_map_is_empty((struct isl_map *)set);
7910 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7912 if (!map1 || !map2)
7913 return -1;
7915 return isl_space_is_equal(map1->dim, map2->dim);
7918 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7920 if (!set1 || !set2)
7921 return -1;
7923 return isl_space_is_equal(set1->dim, set2->dim);
7926 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7928 int is_subset;
7930 if (!map1 || !map2)
7931 return -1;
7932 is_subset = isl_map_is_subset(map1, map2);
7933 if (is_subset != 1)
7934 return is_subset;
7935 is_subset = isl_map_is_subset(map2, map1);
7936 return is_subset;
7939 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7941 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7944 int isl_basic_map_is_strict_subset(
7945 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7947 int is_subset;
7949 if (!bmap1 || !bmap2)
7950 return -1;
7951 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7952 if (is_subset != 1)
7953 return is_subset;
7954 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7955 if (is_subset == -1)
7956 return is_subset;
7957 return !is_subset;
7960 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7962 int is_subset;
7964 if (!map1 || !map2)
7965 return -1;
7966 is_subset = isl_map_is_subset(map1, map2);
7967 if (is_subset != 1)
7968 return is_subset;
7969 is_subset = isl_map_is_subset(map2, map1);
7970 if (is_subset == -1)
7971 return is_subset;
7972 return !is_subset;
7975 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7977 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7980 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7982 if (!bmap)
7983 return -1;
7984 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7987 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7989 if (!bset)
7990 return -1;
7991 return bset->n_eq == 0 && bset->n_ineq == 0;
7994 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7996 int i;
7998 if (!map)
7999 return -1;
8001 for (i = 0; i < map->n; ++i) {
8002 int r = isl_basic_map_is_universe(map->p[i]);
8003 if (r < 0 || r)
8004 return r;
8007 return 0;
8010 int isl_set_plain_is_universe(__isl_keep isl_set *set)
8012 return isl_map_plain_is_universe((isl_map *) set);
8015 int isl_set_fast_is_universe(__isl_keep isl_set *set)
8017 return isl_set_plain_is_universe(set);
8020 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
8022 struct isl_basic_set *bset = NULL;
8023 struct isl_vec *sample = NULL;
8024 int empty;
8025 unsigned total;
8027 if (!bmap)
8028 return -1;
8030 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8031 return 1;
8033 if (isl_basic_map_is_universe(bmap))
8034 return 0;
8036 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8037 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8038 copy = isl_basic_map_remove_redundancies(copy);
8039 empty = isl_basic_map_plain_is_empty(copy);
8040 isl_basic_map_free(copy);
8041 return empty;
8044 total = 1 + isl_basic_map_total_dim(bmap);
8045 if (bmap->sample && bmap->sample->size == total) {
8046 int contains = isl_basic_map_contains(bmap, bmap->sample);
8047 if (contains < 0)
8048 return -1;
8049 if (contains)
8050 return 0;
8052 isl_vec_free(bmap->sample);
8053 bmap->sample = NULL;
8054 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8055 if (!bset)
8056 return -1;
8057 sample = isl_basic_set_sample_vec(bset);
8058 if (!sample)
8059 return -1;
8060 empty = sample->size == 0;
8061 isl_vec_free(bmap->sample);
8062 bmap->sample = sample;
8063 if (empty)
8064 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8066 return empty;
8069 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8071 if (!bmap)
8072 return -1;
8073 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8076 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
8078 return isl_basic_map_plain_is_empty(bmap);
8081 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8083 if (!bset)
8084 return -1;
8085 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8088 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
8090 return isl_basic_set_plain_is_empty(bset);
8093 int isl_basic_set_is_empty(struct isl_basic_set *bset)
8095 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8098 struct isl_map *isl_basic_map_union(
8099 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8101 struct isl_map *map;
8102 if (!bmap1 || !bmap2)
8103 goto error;
8105 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8107 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8108 if (!map)
8109 goto error;
8110 map = isl_map_add_basic_map(map, bmap1);
8111 map = isl_map_add_basic_map(map, bmap2);
8112 return map;
8113 error:
8114 isl_basic_map_free(bmap1);
8115 isl_basic_map_free(bmap2);
8116 return NULL;
8119 struct isl_set *isl_basic_set_union(
8120 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8122 return (struct isl_set *)isl_basic_map_union(
8123 (struct isl_basic_map *)bset1,
8124 (struct isl_basic_map *)bset2);
8127 /* Order divs such that any div only depends on previous divs */
8128 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8130 int i;
8131 unsigned off;
8133 if (!bmap)
8134 return NULL;
8136 off = isl_space_dim(bmap->dim, isl_dim_all);
8138 for (i = 0; i < bmap->n_div; ++i) {
8139 int pos;
8140 if (isl_int_is_zero(bmap->div[i][0]))
8141 continue;
8142 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8143 bmap->n_div-i);
8144 if (pos == -1)
8145 continue;
8146 isl_basic_map_swap_div(bmap, i, i + pos);
8147 --i;
8149 return bmap;
8152 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8154 return (struct isl_basic_set *)
8155 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8158 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8160 int i;
8162 if (!map)
8163 return 0;
8165 for (i = 0; i < map->n; ++i) {
8166 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8167 if (!map->p[i])
8168 goto error;
8171 return map;
8172 error:
8173 isl_map_free(map);
8174 return NULL;
8177 /* Apply the expansion computed by isl_merge_divs.
8178 * The expansion itself is given by "exp" while the resulting
8179 * list of divs is given by "div".
8181 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8182 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8184 int i, j;
8185 int n_div;
8187 bset = isl_basic_set_cow(bset);
8188 if (!bset || !div)
8189 goto error;
8191 if (div->n_row < bset->n_div)
8192 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8193 "not an expansion", goto error);
8195 n_div = bset->n_div;
8196 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8197 div->n_row - n_div, 0,
8198 2 * (div->n_row - n_div));
8200 for (i = n_div; i < div->n_row; ++i)
8201 if (isl_basic_set_alloc_div(bset) < 0)
8202 goto error;
8204 j = n_div - 1;
8205 for (i = div->n_row - 1; i >= 0; --i) {
8206 if (j >= 0 && exp[j] == i) {
8207 if (i != j)
8208 isl_basic_map_swap_div(bset, i, j);
8209 j--;
8210 } else {
8211 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8212 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8213 goto error;
8217 isl_mat_free(div);
8218 return bset;
8219 error:
8220 isl_basic_set_free(bset);
8221 isl_mat_free(div);
8222 return NULL;
8225 /* Look for a div in dst that corresponds to the div "div" in src.
8226 * The divs before "div" in src and dst are assumed to be the same.
8228 * Returns -1 if no corresponding div was found and the position
8229 * of the corresponding div in dst otherwise.
8231 static int find_div(struct isl_basic_map *dst,
8232 struct isl_basic_map *src, unsigned div)
8234 int i;
8236 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8238 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8239 for (i = div; i < dst->n_div; ++i)
8240 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8241 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8242 dst->n_div - div) == -1)
8243 return i;
8244 return -1;
8247 struct isl_basic_map *isl_basic_map_align_divs(
8248 struct isl_basic_map *dst, struct isl_basic_map *src)
8250 int i;
8251 unsigned total;
8253 if (!dst || !src)
8254 goto error;
8256 if (src->n_div == 0)
8257 return dst;
8259 for (i = 0; i < src->n_div; ++i)
8260 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
8262 src = isl_basic_map_order_divs(src);
8263 dst = isl_basic_map_cow(dst);
8264 if (!dst)
8265 return NULL;
8266 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
8267 src->n_div, 0, 2 * src->n_div);
8268 if (!dst)
8269 return NULL;
8270 total = isl_space_dim(src->dim, isl_dim_all);
8271 for (i = 0; i < src->n_div; ++i) {
8272 int j = find_div(dst, src, i);
8273 if (j < 0) {
8274 j = isl_basic_map_alloc_div(dst);
8275 if (j < 0)
8276 goto error;
8277 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8278 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8279 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8280 goto error;
8282 if (j != i)
8283 isl_basic_map_swap_div(dst, i, j);
8285 return dst;
8286 error:
8287 isl_basic_map_free(dst);
8288 return NULL;
8291 struct isl_basic_set *isl_basic_set_align_divs(
8292 struct isl_basic_set *dst, struct isl_basic_set *src)
8294 return (struct isl_basic_set *)isl_basic_map_align_divs(
8295 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8298 struct isl_map *isl_map_align_divs(struct isl_map *map)
8300 int i;
8302 if (!map)
8303 return NULL;
8304 if (map->n == 0)
8305 return map;
8306 map = isl_map_compute_divs(map);
8307 map = isl_map_cow(map);
8308 if (!map)
8309 return NULL;
8311 for (i = 1; i < map->n; ++i)
8312 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8313 for (i = 1; i < map->n; ++i) {
8314 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8315 if (!map->p[i])
8316 return isl_map_free(map);
8319 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8320 return map;
8323 struct isl_set *isl_set_align_divs(struct isl_set *set)
8325 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8328 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8329 __isl_take isl_map *map)
8331 if (!set || !map)
8332 goto error;
8333 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8334 map = isl_map_intersect_domain(map, set);
8335 set = isl_map_range(map);
8336 return set;
8337 error:
8338 isl_set_free(set);
8339 isl_map_free(map);
8340 return NULL;
8343 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8344 __isl_take isl_map *map)
8346 return isl_map_align_params_map_map_and(set, map, &set_apply);
8349 /* There is no need to cow as removing empty parts doesn't change
8350 * the meaning of the set.
8352 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8354 int i;
8356 if (!map)
8357 return NULL;
8359 for (i = map->n - 1; i >= 0; --i)
8360 remove_if_empty(map, i);
8362 return map;
8365 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8367 return (struct isl_set *)
8368 isl_map_remove_empty_parts((struct isl_map *)set);
8371 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8373 struct isl_basic_map *bmap;
8374 if (!map || map->n == 0)
8375 return NULL;
8376 bmap = map->p[map->n-1];
8377 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8378 return isl_basic_map_copy(bmap);
8381 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8383 return (struct isl_basic_set *)
8384 isl_map_copy_basic_map((struct isl_map *)set);
8387 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8388 __isl_keep isl_basic_map *bmap)
8390 int i;
8392 if (!map || !bmap)
8393 goto error;
8394 for (i = map->n-1; i >= 0; --i) {
8395 if (map->p[i] != bmap)
8396 continue;
8397 map = isl_map_cow(map);
8398 if (!map)
8399 goto error;
8400 isl_basic_map_free(map->p[i]);
8401 if (i != map->n-1) {
8402 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8403 map->p[i] = map->p[map->n-1];
8405 map->n--;
8406 return map;
8408 return map;
8409 error:
8410 isl_map_free(map);
8411 return NULL;
8414 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8415 struct isl_basic_set *bset)
8417 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8418 (struct isl_basic_map *)bset);
8421 /* Given two basic sets bset1 and bset2, compute the maximal difference
8422 * between the values of dimension pos in bset1 and those in bset2
8423 * for any common value of the parameters and dimensions preceding pos.
8425 static enum isl_lp_result basic_set_maximal_difference_at(
8426 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8427 int pos, isl_int *opt)
8429 isl_space *dims;
8430 struct isl_basic_map *bmap1 = NULL;
8431 struct isl_basic_map *bmap2 = NULL;
8432 struct isl_ctx *ctx;
8433 struct isl_vec *obj;
8434 unsigned total;
8435 unsigned nparam;
8436 unsigned dim1, dim2;
8437 enum isl_lp_result res;
8439 if (!bset1 || !bset2)
8440 return isl_lp_error;
8442 nparam = isl_basic_set_n_param(bset1);
8443 dim1 = isl_basic_set_n_dim(bset1);
8444 dim2 = isl_basic_set_n_dim(bset2);
8445 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8446 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8447 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8448 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8449 if (!bmap1 || !bmap2)
8450 goto error;
8451 bmap1 = isl_basic_map_cow(bmap1);
8452 bmap1 = isl_basic_map_extend(bmap1, nparam,
8453 pos, (dim1 - pos) + (dim2 - pos),
8454 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8455 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8456 if (!bmap1)
8457 goto error2;
8458 total = isl_basic_map_total_dim(bmap1);
8459 ctx = bmap1->ctx;
8460 obj = isl_vec_alloc(ctx, 1 + total);
8461 if (!obj)
8462 goto error2;
8463 isl_seq_clr(obj->block.data, 1 + total);
8464 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8465 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8466 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8467 opt, NULL, NULL);
8468 isl_basic_map_free(bmap1);
8469 isl_vec_free(obj);
8470 return res;
8471 error:
8472 isl_basic_map_free(bmap2);
8473 error2:
8474 isl_basic_map_free(bmap1);
8475 return isl_lp_error;
8478 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8479 * for any common value of the parameters and dimensions preceding pos
8480 * in both basic sets, the values of dimension pos in bset1 are
8481 * smaller or larger than those in bset2.
8483 * Returns
8484 * 1 if bset1 follows bset2
8485 * -1 if bset1 precedes bset2
8486 * 0 if bset1 and bset2 are incomparable
8487 * -2 if some error occurred.
8489 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8490 struct isl_basic_set *bset2, int pos)
8492 isl_int opt;
8493 enum isl_lp_result res;
8494 int cmp;
8496 isl_int_init(opt);
8498 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8500 if (res == isl_lp_empty)
8501 cmp = 0;
8502 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8503 res == isl_lp_unbounded)
8504 cmp = 1;
8505 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8506 cmp = -1;
8507 else
8508 cmp = -2;
8510 isl_int_clear(opt);
8511 return cmp;
8514 /* Given two basic sets bset1 and bset2, check whether
8515 * for any common value of the parameters and dimensions preceding pos
8516 * there is a value of dimension pos in bset1 that is larger
8517 * than a value of the same dimension in bset2.
8519 * Return
8520 * 1 if there exists such a pair
8521 * 0 if there is no such pair, but there is a pair of equal values
8522 * -1 otherwise
8523 * -2 if some error occurred.
8525 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8526 __isl_keep isl_basic_set *bset2, int pos)
8528 isl_int opt;
8529 enum isl_lp_result res;
8530 int cmp;
8532 isl_int_init(opt);
8534 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8536 if (res == isl_lp_empty)
8537 cmp = -1;
8538 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8539 res == isl_lp_unbounded)
8540 cmp = 1;
8541 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8542 cmp = -1;
8543 else if (res == isl_lp_ok)
8544 cmp = 0;
8545 else
8546 cmp = -2;
8548 isl_int_clear(opt);
8549 return cmp;
8552 /* Given two sets set1 and set2, check whether
8553 * for any common value of the parameters and dimensions preceding pos
8554 * there is a value of dimension pos in set1 that is larger
8555 * than a value of the same dimension in set2.
8557 * Return
8558 * 1 if there exists such a pair
8559 * 0 if there is no such pair, but there is a pair of equal values
8560 * -1 otherwise
8561 * -2 if some error occurred.
8563 int isl_set_follows_at(__isl_keep isl_set *set1,
8564 __isl_keep isl_set *set2, int pos)
8566 int i, j;
8567 int follows = -1;
8569 if (!set1 || !set2)
8570 return -2;
8572 for (i = 0; i < set1->n; ++i)
8573 for (j = 0; j < set2->n; ++j) {
8574 int f;
8575 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8576 if (f == 1 || f == -2)
8577 return f;
8578 if (f > follows)
8579 follows = f;
8582 return follows;
8585 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8586 unsigned pos, isl_int *val)
8588 int i;
8589 int d;
8590 unsigned total;
8592 if (!bmap)
8593 return -1;
8594 total = isl_basic_map_total_dim(bmap);
8595 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8596 for (; d+1 > pos; --d)
8597 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8598 break;
8599 if (d != pos)
8600 continue;
8601 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8602 return 0;
8603 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8604 return 0;
8605 if (!isl_int_is_one(bmap->eq[i][1+d]))
8606 return 0;
8607 if (val)
8608 isl_int_neg(*val, bmap->eq[i][0]);
8609 return 1;
8611 return 0;
8614 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8615 unsigned pos, isl_int *val)
8617 int i;
8618 isl_int v;
8619 isl_int tmp;
8620 int fixed;
8622 if (!map)
8623 return -1;
8624 if (map->n == 0)
8625 return 0;
8626 if (map->n == 1)
8627 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8628 isl_int_init(v);
8629 isl_int_init(tmp);
8630 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8631 for (i = 1; fixed == 1 && i < map->n; ++i) {
8632 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8633 if (fixed == 1 && isl_int_ne(tmp, v))
8634 fixed = 0;
8636 if (val)
8637 isl_int_set(*val, v);
8638 isl_int_clear(tmp);
8639 isl_int_clear(v);
8640 return fixed;
8643 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8644 unsigned pos, isl_int *val)
8646 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8647 pos, val);
8650 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8651 isl_int *val)
8653 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8656 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8657 enum isl_dim_type type, unsigned pos, isl_int *val)
8659 if (pos >= isl_basic_map_dim(bmap, type))
8660 return -1;
8661 return isl_basic_map_plain_has_fixed_var(bmap,
8662 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8665 /* If "bmap" obviously lies on a hyperplane where the given dimension
8666 * has a fixed value, then return that value.
8667 * Otherwise return NaN.
8669 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8670 __isl_keep isl_basic_map *bmap,
8671 enum isl_dim_type type, unsigned pos)
8673 isl_ctx *ctx;
8674 isl_val *v;
8675 int fixed;
8677 if (!bmap)
8678 return NULL;
8679 ctx = isl_basic_map_get_ctx(bmap);
8680 v = isl_val_alloc(ctx);
8681 if (!v)
8682 return NULL;
8683 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8684 if (fixed < 0)
8685 return isl_val_free(v);
8686 if (fixed) {
8687 isl_int_set_si(v->d, 1);
8688 return v;
8690 isl_val_free(v);
8691 return isl_val_nan(ctx);
8694 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8695 enum isl_dim_type type, unsigned pos, isl_int *val)
8697 if (pos >= isl_map_dim(map, type))
8698 return -1;
8699 return isl_map_plain_has_fixed_var(map,
8700 map_offset(map, type) - 1 + pos, val);
8703 /* If "map" obviously lies on a hyperplane where the given dimension
8704 * has a fixed value, then return that value.
8705 * Otherwise return NaN.
8707 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8708 enum isl_dim_type type, unsigned pos)
8710 isl_ctx *ctx;
8711 isl_val *v;
8712 int fixed;
8714 if (!map)
8715 return NULL;
8716 ctx = isl_map_get_ctx(map);
8717 v = isl_val_alloc(ctx);
8718 if (!v)
8719 return NULL;
8720 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8721 if (fixed < 0)
8722 return isl_val_free(v);
8723 if (fixed) {
8724 isl_int_set_si(v->d, 1);
8725 return v;
8727 isl_val_free(v);
8728 return isl_val_nan(ctx);
8731 /* If "set" obviously lies on a hyperplane where the given dimension
8732 * has a fixed value, then return that value.
8733 * Otherwise return NaN.
8735 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8736 enum isl_dim_type type, unsigned pos)
8738 return isl_map_plain_get_val_if_fixed(set, type, pos);
8741 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8742 enum isl_dim_type type, unsigned pos, isl_int *val)
8744 return isl_map_plain_is_fixed(set, type, pos, val);
8747 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8748 enum isl_dim_type type, unsigned pos, isl_int *val)
8750 return isl_map_plain_is_fixed(map, type, pos, val);
8753 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8754 * then return this fixed value in *val.
8756 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8757 unsigned dim, isl_int *val)
8759 return isl_basic_set_plain_has_fixed_var(bset,
8760 isl_basic_set_n_param(bset) + dim, val);
8763 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8764 * then return this fixed value in *val.
8766 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8767 unsigned dim, isl_int *val)
8769 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8772 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8773 unsigned dim, isl_int *val)
8775 return isl_set_plain_dim_is_fixed(set, dim, val);
8778 /* Check if input variable in has fixed value and if so and if val is not NULL,
8779 * then return this fixed value in *val.
8781 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8782 unsigned in, isl_int *val)
8784 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8787 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8788 * and if val is not NULL, then return this lower bound in *val.
8790 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8791 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8793 int i, i_eq = -1, i_ineq = -1;
8794 isl_int *c;
8795 unsigned total;
8796 unsigned nparam;
8798 if (!bset)
8799 return -1;
8800 total = isl_basic_set_total_dim(bset);
8801 nparam = isl_basic_set_n_param(bset);
8802 for (i = 0; i < bset->n_eq; ++i) {
8803 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8804 continue;
8805 if (i_eq != -1)
8806 return 0;
8807 i_eq = i;
8809 for (i = 0; i < bset->n_ineq; ++i) {
8810 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8811 continue;
8812 if (i_eq != -1 || i_ineq != -1)
8813 return 0;
8814 i_ineq = i;
8816 if (i_eq == -1 && i_ineq == -1)
8817 return 0;
8818 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8819 /* The coefficient should always be one due to normalization. */
8820 if (!isl_int_is_one(c[1+nparam+dim]))
8821 return 0;
8822 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8823 return 0;
8824 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8825 total - nparam - dim - 1) != -1)
8826 return 0;
8827 if (val)
8828 isl_int_neg(*val, c[0]);
8829 return 1;
8832 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8833 unsigned dim, isl_int *val)
8835 int i;
8836 isl_int v;
8837 isl_int tmp;
8838 int fixed;
8840 if (!set)
8841 return -1;
8842 if (set->n == 0)
8843 return 0;
8844 if (set->n == 1)
8845 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8846 dim, val);
8847 isl_int_init(v);
8848 isl_int_init(tmp);
8849 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8850 dim, &v);
8851 for (i = 1; fixed == 1 && i < set->n; ++i) {
8852 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8853 dim, &tmp);
8854 if (fixed == 1 && isl_int_ne(tmp, v))
8855 fixed = 0;
8857 if (val)
8858 isl_int_set(*val, v);
8859 isl_int_clear(tmp);
8860 isl_int_clear(v);
8861 return fixed;
8864 struct constraint {
8865 unsigned size;
8866 isl_int *c;
8869 /* uset_gist depends on constraints without existentially quantified
8870 * variables sorting first.
8872 static int qsort_constraint_cmp(const void *p1, const void *p2)
8874 const struct constraint *c1 = (const struct constraint *)p1;
8875 const struct constraint *c2 = (const struct constraint *)p2;
8876 int l1, l2;
8877 unsigned size = isl_min(c1->size, c2->size);
8879 l1 = isl_seq_last_non_zero(c1->c + 1, size);
8880 l2 = isl_seq_last_non_zero(c2->c + 1, size);
8882 if (l1 != l2)
8883 return l1 - l2;
8885 return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8888 static struct isl_basic_map *isl_basic_map_sort_constraints(
8889 struct isl_basic_map *bmap)
8891 int i;
8892 struct constraint *c;
8893 unsigned total;
8895 if (!bmap)
8896 return NULL;
8897 if (bmap->n_ineq == 0)
8898 return bmap;
8899 total = isl_basic_map_total_dim(bmap);
8900 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8901 if (!c)
8902 goto error;
8903 for (i = 0; i < bmap->n_ineq; ++i) {
8904 c[i].size = total;
8905 c[i].c = bmap->ineq[i];
8907 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8908 for (i = 0; i < bmap->n_ineq; ++i)
8909 bmap->ineq[i] = c[i].c;
8910 free(c);
8911 return bmap;
8912 error:
8913 isl_basic_map_free(bmap);
8914 return NULL;
8917 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8918 __isl_take isl_basic_set *bset)
8920 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8921 (struct isl_basic_map *)bset);
8924 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8926 if (!bmap)
8927 return NULL;
8928 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8929 return bmap;
8930 bmap = isl_basic_map_remove_redundancies(bmap);
8931 bmap = isl_basic_map_sort_constraints(bmap);
8932 if (bmap)
8933 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8934 return bmap;
8937 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8939 return (struct isl_basic_set *)isl_basic_map_normalize(
8940 (struct isl_basic_map *)bset);
8943 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8944 const __isl_keep isl_basic_map *bmap2)
8946 int i, cmp;
8947 unsigned total;
8949 if (bmap1 == bmap2)
8950 return 0;
8951 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8952 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8953 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8954 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8955 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8956 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8957 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8958 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8959 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8960 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8961 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8962 return 0;
8963 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8964 return 1;
8965 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8966 return -1;
8967 if (bmap1->n_eq != bmap2->n_eq)
8968 return bmap1->n_eq - bmap2->n_eq;
8969 if (bmap1->n_ineq != bmap2->n_ineq)
8970 return bmap1->n_ineq - bmap2->n_ineq;
8971 if (bmap1->n_div != bmap2->n_div)
8972 return bmap1->n_div - bmap2->n_div;
8973 total = isl_basic_map_total_dim(bmap1);
8974 for (i = 0; i < bmap1->n_eq; ++i) {
8975 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8976 if (cmp)
8977 return cmp;
8979 for (i = 0; i < bmap1->n_ineq; ++i) {
8980 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8981 if (cmp)
8982 return cmp;
8984 for (i = 0; i < bmap1->n_div; ++i) {
8985 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8986 if (cmp)
8987 return cmp;
8989 return 0;
8992 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8993 const __isl_keep isl_basic_set *bset2)
8995 return isl_basic_map_plain_cmp(bset1, bset2);
8998 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9000 int i, cmp;
9002 if (set1 == set2)
9003 return 0;
9004 if (set1->n != set2->n)
9005 return set1->n - set2->n;
9007 for (i = 0; i < set1->n; ++i) {
9008 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9009 if (cmp)
9010 return cmp;
9013 return 0;
9016 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9017 __isl_keep isl_basic_map *bmap2)
9019 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9022 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9023 __isl_keep isl_basic_set *bset2)
9025 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9026 (isl_basic_map *)bset2);
9029 static int qsort_bmap_cmp(const void *p1, const void *p2)
9031 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9032 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9034 return isl_basic_map_plain_cmp(bmap1, bmap2);
9037 /* We normalize in place, but if anything goes wrong we need
9038 * to return NULL, so we need to make sure we don't change the
9039 * meaning of any possible other copies of map.
9041 struct isl_map *isl_map_normalize(struct isl_map *map)
9043 int i, j;
9044 struct isl_basic_map *bmap;
9046 if (!map)
9047 return NULL;
9048 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9049 return map;
9050 for (i = 0; i < map->n; ++i) {
9051 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9052 if (!bmap)
9053 goto error;
9054 isl_basic_map_free(map->p[i]);
9055 map->p[i] = bmap;
9057 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9058 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9059 map = isl_map_remove_empty_parts(map);
9060 if (!map)
9061 return NULL;
9062 for (i = map->n - 1; i >= 1; --i) {
9063 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
9064 continue;
9065 isl_basic_map_free(map->p[i-1]);
9066 for (j = i; j < map->n; ++j)
9067 map->p[j-1] = map->p[j];
9068 map->n--;
9070 return map;
9071 error:
9072 isl_map_free(map);
9073 return NULL;
9076 struct isl_set *isl_set_normalize(struct isl_set *set)
9078 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9081 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9083 int i;
9084 int equal;
9086 if (!map1 || !map2)
9087 return -1;
9089 if (map1 == map2)
9090 return 1;
9091 if (!isl_space_is_equal(map1->dim, map2->dim))
9092 return 0;
9094 map1 = isl_map_copy(map1);
9095 map2 = isl_map_copy(map2);
9096 map1 = isl_map_normalize(map1);
9097 map2 = isl_map_normalize(map2);
9098 if (!map1 || !map2)
9099 goto error;
9100 equal = map1->n == map2->n;
9101 for (i = 0; equal && i < map1->n; ++i) {
9102 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9103 if (equal < 0)
9104 goto error;
9106 isl_map_free(map1);
9107 isl_map_free(map2);
9108 return equal;
9109 error:
9110 isl_map_free(map1);
9111 isl_map_free(map2);
9112 return -1;
9115 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9117 return isl_map_plain_is_equal(map1, map2);
9120 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9122 return isl_map_plain_is_equal((struct isl_map *)set1,
9123 (struct isl_map *)set2);
9126 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9128 return isl_set_plain_is_equal(set1, set2);
9131 /* Return an interval that ranges from min to max (inclusive)
9133 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9134 isl_int min, isl_int max)
9136 int k;
9137 struct isl_basic_set *bset = NULL;
9139 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9140 if (!bset)
9141 goto error;
9143 k = isl_basic_set_alloc_inequality(bset);
9144 if (k < 0)
9145 goto error;
9146 isl_int_set_si(bset->ineq[k][1], 1);
9147 isl_int_neg(bset->ineq[k][0], min);
9149 k = isl_basic_set_alloc_inequality(bset);
9150 if (k < 0)
9151 goto error;
9152 isl_int_set_si(bset->ineq[k][1], -1);
9153 isl_int_set(bset->ineq[k][0], max);
9155 return bset;
9156 error:
9157 isl_basic_set_free(bset);
9158 return NULL;
9161 /* Return the Cartesian product of the basic sets in list (in the given order).
9163 __isl_give isl_basic_set *isl_basic_set_list_product(
9164 __isl_take struct isl_basic_set_list *list)
9166 int i;
9167 unsigned dim;
9168 unsigned nparam;
9169 unsigned extra;
9170 unsigned n_eq;
9171 unsigned n_ineq;
9172 struct isl_basic_set *product = NULL;
9174 if (!list)
9175 goto error;
9176 isl_assert(list->ctx, list->n > 0, goto error);
9177 isl_assert(list->ctx, list->p[0], goto error);
9178 nparam = isl_basic_set_n_param(list->p[0]);
9179 dim = isl_basic_set_n_dim(list->p[0]);
9180 extra = list->p[0]->n_div;
9181 n_eq = list->p[0]->n_eq;
9182 n_ineq = list->p[0]->n_ineq;
9183 for (i = 1; i < list->n; ++i) {
9184 isl_assert(list->ctx, list->p[i], goto error);
9185 isl_assert(list->ctx,
9186 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9187 dim += isl_basic_set_n_dim(list->p[i]);
9188 extra += list->p[i]->n_div;
9189 n_eq += list->p[i]->n_eq;
9190 n_ineq += list->p[i]->n_ineq;
9192 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9193 n_eq, n_ineq);
9194 if (!product)
9195 goto error;
9196 dim = 0;
9197 for (i = 0; i < list->n; ++i) {
9198 isl_basic_set_add_constraints(product,
9199 isl_basic_set_copy(list->p[i]), dim);
9200 dim += isl_basic_set_n_dim(list->p[i]);
9202 isl_basic_set_list_free(list);
9203 return product;
9204 error:
9205 isl_basic_set_free(product);
9206 isl_basic_set_list_free(list);
9207 return NULL;
9210 struct isl_basic_map *isl_basic_map_product(
9211 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9213 isl_space *dim_result = NULL;
9214 struct isl_basic_map *bmap;
9215 unsigned in1, in2, out1, out2, nparam, total, pos;
9216 struct isl_dim_map *dim_map1, *dim_map2;
9218 if (!bmap1 || !bmap2)
9219 goto error;
9221 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9222 bmap2->dim, isl_dim_param), goto error);
9223 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9224 isl_space_copy(bmap2->dim));
9226 in1 = isl_basic_map_n_in(bmap1);
9227 in2 = isl_basic_map_n_in(bmap2);
9228 out1 = isl_basic_map_n_out(bmap1);
9229 out2 = isl_basic_map_n_out(bmap2);
9230 nparam = isl_basic_map_n_param(bmap1);
9232 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9233 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9234 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9235 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9236 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9237 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9238 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9239 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9240 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9241 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9242 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9244 bmap = isl_basic_map_alloc_space(dim_result,
9245 bmap1->n_div + bmap2->n_div,
9246 bmap1->n_eq + bmap2->n_eq,
9247 bmap1->n_ineq + bmap2->n_ineq);
9248 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9249 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9250 bmap = isl_basic_map_simplify(bmap);
9251 return isl_basic_map_finalize(bmap);
9252 error:
9253 isl_basic_map_free(bmap1);
9254 isl_basic_map_free(bmap2);
9255 return NULL;
9258 __isl_give isl_basic_map *isl_basic_map_flat_product(
9259 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9261 isl_basic_map *prod;
9263 prod = isl_basic_map_product(bmap1, bmap2);
9264 prod = isl_basic_map_flatten(prod);
9265 return prod;
9268 __isl_give isl_basic_set *isl_basic_set_flat_product(
9269 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9271 return isl_basic_map_flat_range_product(bset1, bset2);
9274 __isl_give isl_basic_map *isl_basic_map_domain_product(
9275 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9277 isl_space *space_result = NULL;
9278 isl_basic_map *bmap;
9279 unsigned in1, in2, out, nparam, total, pos;
9280 struct isl_dim_map *dim_map1, *dim_map2;
9282 if (!bmap1 || !bmap2)
9283 goto error;
9285 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9286 isl_space_copy(bmap2->dim));
9288 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9289 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9290 out = isl_basic_map_dim(bmap1, isl_dim_out);
9291 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9293 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9294 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9295 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9296 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9297 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9298 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9299 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9300 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9301 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9302 isl_dim_map_div(dim_map1, bmap1, pos += out);
9303 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9305 bmap = isl_basic_map_alloc_space(space_result,
9306 bmap1->n_div + bmap2->n_div,
9307 bmap1->n_eq + bmap2->n_eq,
9308 bmap1->n_ineq + bmap2->n_ineq);
9309 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9310 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9311 bmap = isl_basic_map_simplify(bmap);
9312 return isl_basic_map_finalize(bmap);
9313 error:
9314 isl_basic_map_free(bmap1);
9315 isl_basic_map_free(bmap2);
9316 return NULL;
9319 __isl_give isl_basic_map *isl_basic_map_range_product(
9320 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9322 isl_space *dim_result = NULL;
9323 isl_basic_map *bmap;
9324 unsigned in, out1, out2, nparam, total, pos;
9325 struct isl_dim_map *dim_map1, *dim_map2;
9327 if (!bmap1 || !bmap2)
9328 goto error;
9330 if (!isl_space_match(bmap1->dim, isl_dim_param,
9331 bmap2->dim, isl_dim_param))
9332 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9333 "parameters don't match", goto error);
9335 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9336 isl_space_copy(bmap2->dim));
9338 in = isl_basic_map_dim(bmap1, isl_dim_in);
9339 out1 = isl_basic_map_n_out(bmap1);
9340 out2 = isl_basic_map_n_out(bmap2);
9341 nparam = isl_basic_map_n_param(bmap1);
9343 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9344 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9345 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9346 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9347 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9348 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9349 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9350 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9351 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9352 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9353 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9355 bmap = isl_basic_map_alloc_space(dim_result,
9356 bmap1->n_div + bmap2->n_div,
9357 bmap1->n_eq + bmap2->n_eq,
9358 bmap1->n_ineq + bmap2->n_ineq);
9359 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9360 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9361 bmap = isl_basic_map_simplify(bmap);
9362 return isl_basic_map_finalize(bmap);
9363 error:
9364 isl_basic_map_free(bmap1);
9365 isl_basic_map_free(bmap2);
9366 return NULL;
9369 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9370 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9372 isl_basic_map *prod;
9374 prod = isl_basic_map_range_product(bmap1, bmap2);
9375 prod = isl_basic_map_flatten_range(prod);
9376 return prod;
9379 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9380 __isl_take isl_map *map2,
9381 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
9382 __isl_take isl_space *right),
9383 __isl_give isl_basic_map *(*basic_map_product)(
9384 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
9386 unsigned flags = 0;
9387 struct isl_map *result;
9388 int i, j;
9390 if (!map1 || !map2)
9391 goto error;
9393 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9394 map2->dim, isl_dim_param), goto error);
9396 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9397 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9398 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9400 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
9401 isl_space_copy(map2->dim)),
9402 map1->n * map2->n, flags);
9403 if (!result)
9404 goto error;
9405 for (i = 0; i < map1->n; ++i)
9406 for (j = 0; j < map2->n; ++j) {
9407 struct isl_basic_map *part;
9408 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9409 isl_basic_map_copy(map2->p[j]));
9410 if (isl_basic_map_is_empty(part))
9411 isl_basic_map_free(part);
9412 else
9413 result = isl_map_add_basic_map(result, part);
9414 if (!result)
9415 goto error;
9417 isl_map_free(map1);
9418 isl_map_free(map2);
9419 return result;
9420 error:
9421 isl_map_free(map1);
9422 isl_map_free(map2);
9423 return NULL;
9426 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9428 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9429 __isl_take isl_map *map2)
9431 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
9434 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9435 __isl_take isl_map *map2)
9437 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9440 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9442 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9443 __isl_take isl_map *map2)
9445 isl_map *prod;
9447 prod = isl_map_product(map1, map2);
9448 prod = isl_map_flatten(prod);
9449 return prod;
9452 /* Given two set A and B, construct its Cartesian product A x B.
9454 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9456 return isl_map_range_product(set1, set2);
9459 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9460 __isl_take isl_set *set2)
9462 return isl_map_flat_range_product(set1, set2);
9465 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9467 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9468 __isl_take isl_map *map2)
9470 return map_product(map1, map2, &isl_space_domain_product,
9471 &isl_basic_map_domain_product);
9474 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9476 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9477 __isl_take isl_map *map2)
9479 return map_product(map1, map2, &isl_space_range_product,
9480 &isl_basic_map_range_product);
9483 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9484 __isl_take isl_map *map2)
9486 return isl_map_align_params_map_map_and(map1, map2,
9487 &map_domain_product_aligned);
9490 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9491 __isl_take isl_map *map2)
9493 return isl_map_align_params_map_map_and(map1, map2,
9494 &map_range_product_aligned);
9497 /* Given a map A -> [B -> C], extract the map A -> B.
9499 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9501 isl_space *space;
9502 int total, keep;
9504 if (!map)
9505 return NULL;
9506 if (!isl_space_range_is_wrapping(map->dim))
9507 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9508 "range is not a product", return isl_map_free(map));
9510 space = isl_map_get_space(map);
9511 total = isl_space_dim(space, isl_dim_out);
9512 space = isl_space_range_factor_domain(space);
9513 keep = isl_space_dim(space, isl_dim_out);
9514 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9515 map = isl_map_reset_space(map, space);
9517 return map;
9520 /* Given a map A -> [B -> C], extract the map A -> C.
9522 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9524 isl_space *space;
9525 int total, keep;
9527 if (!map)
9528 return NULL;
9529 if (!isl_space_range_is_wrapping(map->dim))
9530 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9531 "range is not a product", return isl_map_free(map));
9533 space = isl_map_get_space(map);
9534 total = isl_space_dim(space, isl_dim_out);
9535 space = isl_space_range_factor_range(space);
9536 keep = isl_space_dim(space, isl_dim_out);
9537 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9538 map = isl_map_reset_space(map, space);
9540 return map;
9543 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9545 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9546 __isl_take isl_map *map2)
9548 isl_map *prod;
9550 prod = isl_map_domain_product(map1, map2);
9551 prod = isl_map_flatten_domain(prod);
9552 return prod;
9555 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9557 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9558 __isl_take isl_map *map2)
9560 isl_map *prod;
9562 prod = isl_map_range_product(map1, map2);
9563 prod = isl_map_flatten_range(prod);
9564 return prod;
9567 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9569 int i;
9570 uint32_t hash = isl_hash_init();
9571 unsigned total;
9573 if (!bmap)
9574 return 0;
9575 bmap = isl_basic_map_copy(bmap);
9576 bmap = isl_basic_map_normalize(bmap);
9577 if (!bmap)
9578 return 0;
9579 total = isl_basic_map_total_dim(bmap);
9580 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9581 for (i = 0; i < bmap->n_eq; ++i) {
9582 uint32_t c_hash;
9583 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9584 isl_hash_hash(hash, c_hash);
9586 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9587 for (i = 0; i < bmap->n_ineq; ++i) {
9588 uint32_t c_hash;
9589 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9590 isl_hash_hash(hash, c_hash);
9592 isl_hash_byte(hash, bmap->n_div & 0xFF);
9593 for (i = 0; i < bmap->n_div; ++i) {
9594 uint32_t c_hash;
9595 if (isl_int_is_zero(bmap->div[i][0]))
9596 continue;
9597 isl_hash_byte(hash, i & 0xFF);
9598 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9599 isl_hash_hash(hash, c_hash);
9601 isl_basic_map_free(bmap);
9602 return hash;
9605 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9607 return isl_basic_map_get_hash((isl_basic_map *)bset);
9610 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9612 int i;
9613 uint32_t hash;
9615 if (!map)
9616 return 0;
9617 map = isl_map_copy(map);
9618 map = isl_map_normalize(map);
9619 if (!map)
9620 return 0;
9622 hash = isl_hash_init();
9623 for (i = 0; i < map->n; ++i) {
9624 uint32_t bmap_hash;
9625 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9626 isl_hash_hash(hash, bmap_hash);
9629 isl_map_free(map);
9631 return hash;
9634 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9636 return isl_map_get_hash((isl_map *)set);
9639 /* Check if the value for dimension dim is completely determined
9640 * by the values of the other parameters and variables.
9641 * That is, check if dimension dim is involved in an equality.
9643 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9645 int i;
9646 unsigned nparam;
9648 if (!bset)
9649 return -1;
9650 nparam = isl_basic_set_n_param(bset);
9651 for (i = 0; i < bset->n_eq; ++i)
9652 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9653 return 1;
9654 return 0;
9657 /* Check if the value for dimension dim is completely determined
9658 * by the values of the other parameters and variables.
9659 * That is, check if dimension dim is involved in an equality
9660 * for each of the subsets.
9662 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9664 int i;
9666 if (!set)
9667 return -1;
9668 for (i = 0; i < set->n; ++i) {
9669 int unique;
9670 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9671 if (unique != 1)
9672 return unique;
9674 return 1;
9677 int isl_set_n_basic_set(__isl_keep isl_set *set)
9679 return set ? set->n : 0;
9682 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9683 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9685 int i;
9687 if (!map)
9688 return -1;
9690 for (i = 0; i < map->n; ++i)
9691 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9692 return -1;
9694 return 0;
9697 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9698 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9700 int i;
9702 if (!set)
9703 return -1;
9705 for (i = 0; i < set->n; ++i)
9706 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9707 return -1;
9709 return 0;
9712 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9714 isl_space *dim;
9716 if (!bset)
9717 return NULL;
9719 bset = isl_basic_set_cow(bset);
9720 if (!bset)
9721 return NULL;
9723 dim = isl_basic_set_get_space(bset);
9724 dim = isl_space_lift(dim, bset->n_div);
9725 if (!dim)
9726 goto error;
9727 isl_space_free(bset->dim);
9728 bset->dim = dim;
9729 bset->extra -= bset->n_div;
9730 bset->n_div = 0;
9732 bset = isl_basic_set_finalize(bset);
9734 return bset;
9735 error:
9736 isl_basic_set_free(bset);
9737 return NULL;
9740 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9742 int i;
9743 isl_space *dim;
9744 unsigned n_div;
9746 set = isl_set_align_divs(set);
9748 if (!set)
9749 return NULL;
9751 set = isl_set_cow(set);
9752 if (!set)
9753 return NULL;
9755 n_div = set->p[0]->n_div;
9756 dim = isl_set_get_space(set);
9757 dim = isl_space_lift(dim, n_div);
9758 if (!dim)
9759 goto error;
9760 isl_space_free(set->dim);
9761 set->dim = dim;
9763 for (i = 0; i < set->n; ++i) {
9764 set->p[i] = isl_basic_set_lift(set->p[i]);
9765 if (!set->p[i])
9766 goto error;
9769 return set;
9770 error:
9771 isl_set_free(set);
9772 return NULL;
9775 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9777 isl_space *dim;
9778 struct isl_basic_map *bmap;
9779 unsigned n_set;
9780 unsigned n_div;
9781 unsigned n_param;
9782 unsigned total;
9783 int i, k, l;
9785 set = isl_set_align_divs(set);
9787 if (!set)
9788 return NULL;
9790 dim = isl_set_get_space(set);
9791 if (set->n == 0 || set->p[0]->n_div == 0) {
9792 isl_set_free(set);
9793 return isl_map_identity(isl_space_map_from_set(dim));
9796 n_div = set->p[0]->n_div;
9797 dim = isl_space_map_from_set(dim);
9798 n_param = isl_space_dim(dim, isl_dim_param);
9799 n_set = isl_space_dim(dim, isl_dim_in);
9800 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9801 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9802 for (i = 0; i < n_set; ++i)
9803 bmap = var_equal(bmap, i);
9805 total = n_param + n_set + n_set + n_div;
9806 for (i = 0; i < n_div; ++i) {
9807 k = isl_basic_map_alloc_inequality(bmap);
9808 if (k < 0)
9809 goto error;
9810 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9811 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9812 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9813 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9814 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9815 set->p[0]->div[i][0]);
9817 l = isl_basic_map_alloc_inequality(bmap);
9818 if (l < 0)
9819 goto error;
9820 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9821 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9822 set->p[0]->div[i][0]);
9823 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9826 isl_set_free(set);
9827 bmap = isl_basic_map_simplify(bmap);
9828 bmap = isl_basic_map_finalize(bmap);
9829 return isl_map_from_basic_map(bmap);
9830 error:
9831 isl_set_free(set);
9832 isl_basic_map_free(bmap);
9833 return NULL;
9836 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9838 unsigned dim;
9839 int size = 0;
9841 if (!bset)
9842 return -1;
9844 dim = isl_basic_set_total_dim(bset);
9845 size += bset->n_eq * (1 + dim);
9846 size += bset->n_ineq * (1 + dim);
9847 size += bset->n_div * (2 + dim);
9849 return size;
9852 int isl_set_size(__isl_keep isl_set *set)
9854 int i;
9855 int size = 0;
9857 if (!set)
9858 return -1;
9860 for (i = 0; i < set->n; ++i)
9861 size += isl_basic_set_size(set->p[i]);
9863 return size;
9866 /* Check if there is any lower bound (if lower == 0) and/or upper
9867 * bound (if upper == 0) on the specified dim.
9869 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9870 enum isl_dim_type type, unsigned pos, int lower, int upper)
9872 int i;
9874 if (!bmap)
9875 return -1;
9877 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9879 pos += isl_basic_map_offset(bmap, type);
9881 for (i = 0; i < bmap->n_div; ++i) {
9882 if (isl_int_is_zero(bmap->div[i][0]))
9883 continue;
9884 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9885 return 1;
9888 for (i = 0; i < bmap->n_eq; ++i)
9889 if (!isl_int_is_zero(bmap->eq[i][pos]))
9890 return 1;
9892 for (i = 0; i < bmap->n_ineq; ++i) {
9893 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
9894 if (sgn > 0)
9895 lower = 1;
9896 if (sgn < 0)
9897 upper = 1;
9900 return lower && upper;
9903 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9904 enum isl_dim_type type, unsigned pos)
9906 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9909 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9910 enum isl_dim_type type, unsigned pos)
9912 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9915 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9916 enum isl_dim_type type, unsigned pos)
9918 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9921 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9922 enum isl_dim_type type, unsigned pos)
9924 int i;
9926 if (!map)
9927 return -1;
9929 for (i = 0; i < map->n; ++i) {
9930 int bounded;
9931 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9932 if (bounded < 0 || !bounded)
9933 return bounded;
9936 return 1;
9939 /* Return 1 if the specified dim is involved in both an upper bound
9940 * and a lower bound.
9942 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9943 enum isl_dim_type type, unsigned pos)
9945 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9948 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9950 static int has_any_bound(__isl_keep isl_map *map,
9951 enum isl_dim_type type, unsigned pos,
9952 int (*fn)(__isl_keep isl_basic_map *bmap,
9953 enum isl_dim_type type, unsigned pos))
9955 int i;
9957 if (!map)
9958 return -1;
9960 for (i = 0; i < map->n; ++i) {
9961 int bounded;
9962 bounded = fn(map->p[i], type, pos);
9963 if (bounded < 0 || bounded)
9964 return bounded;
9967 return 0;
9970 /* Return 1 if the specified dim is involved in any lower bound.
9972 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
9973 enum isl_dim_type type, unsigned pos)
9975 return has_any_bound(set, type, pos,
9976 &isl_basic_map_dim_has_lower_bound);
9979 /* Return 1 if the specified dim is involved in any upper bound.
9981 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
9982 enum isl_dim_type type, unsigned pos)
9984 return has_any_bound(set, type, pos,
9985 &isl_basic_map_dim_has_upper_bound);
9988 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9990 static int has_bound(__isl_keep isl_map *map,
9991 enum isl_dim_type type, unsigned pos,
9992 int (*fn)(__isl_keep isl_basic_map *bmap,
9993 enum isl_dim_type type, unsigned pos))
9995 int i;
9997 if (!map)
9998 return -1;
10000 for (i = 0; i < map->n; ++i) {
10001 int bounded;
10002 bounded = fn(map->p[i], type, pos);
10003 if (bounded < 0 || !bounded)
10004 return bounded;
10007 return 1;
10010 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10012 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10013 enum isl_dim_type type, unsigned pos)
10015 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10018 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10020 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10021 enum isl_dim_type type, unsigned pos)
10023 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10026 /* For each of the "n" variables starting at "first", determine
10027 * the sign of the variable and put the results in the first "n"
10028 * elements of the array "signs".
10029 * Sign
10030 * 1 means that the variable is non-negative
10031 * -1 means that the variable is non-positive
10032 * 0 means the variable attains both positive and negative values.
10034 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10035 unsigned first, unsigned n, int *signs)
10037 isl_vec *bound = NULL;
10038 struct isl_tab *tab = NULL;
10039 struct isl_tab_undo *snap;
10040 int i;
10042 if (!bset || !signs)
10043 return -1;
10045 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10046 tab = isl_tab_from_basic_set(bset, 0);
10047 if (!bound || !tab)
10048 goto error;
10050 isl_seq_clr(bound->el, bound->size);
10051 isl_int_set_si(bound->el[0], -1);
10053 snap = isl_tab_snap(tab);
10054 for (i = 0; i < n; ++i) {
10055 int empty;
10057 isl_int_set_si(bound->el[1 + first + i], -1);
10058 if (isl_tab_add_ineq(tab, bound->el) < 0)
10059 goto error;
10060 empty = tab->empty;
10061 isl_int_set_si(bound->el[1 + first + i], 0);
10062 if (isl_tab_rollback(tab, snap) < 0)
10063 goto error;
10065 if (empty) {
10066 signs[i] = 1;
10067 continue;
10070 isl_int_set_si(bound->el[1 + first + i], 1);
10071 if (isl_tab_add_ineq(tab, bound->el) < 0)
10072 goto error;
10073 empty = tab->empty;
10074 isl_int_set_si(bound->el[1 + first + i], 0);
10075 if (isl_tab_rollback(tab, snap) < 0)
10076 goto error;
10078 signs[i] = empty ? -1 : 0;
10081 isl_tab_free(tab);
10082 isl_vec_free(bound);
10083 return 0;
10084 error:
10085 isl_tab_free(tab);
10086 isl_vec_free(bound);
10087 return -1;
10090 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10091 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10093 if (!bset || !signs)
10094 return -1;
10095 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10096 return -1);
10098 first += pos(bset->dim, type) - 1;
10099 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10102 /* Check if the given basic map is obviously single-valued.
10103 * In particular, for each output dimension, check that there is
10104 * an equality that defines the output dimension in terms of
10105 * earlier dimensions.
10107 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10109 int i, j;
10110 unsigned total;
10111 unsigned n_out;
10112 unsigned o_out;
10114 if (!bmap)
10115 return -1;
10117 total = 1 + isl_basic_map_total_dim(bmap);
10118 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10119 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10121 for (i = 0; i < n_out; ++i) {
10122 for (j = 0; j < bmap->n_eq; ++j) {
10123 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
10124 continue;
10125 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
10126 total - (o_out + i + 1)) == -1)
10127 break;
10129 if (j >= bmap->n_eq)
10130 return 0;
10133 return 1;
10136 /* Check if the given basic map is single-valued.
10137 * We simply compute
10139 * M \circ M^-1
10141 * and check if the result is a subset of the identity mapping.
10143 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10145 isl_space *space;
10146 isl_basic_map *test;
10147 isl_basic_map *id;
10148 int sv;
10150 sv = isl_basic_map_plain_is_single_valued(bmap);
10151 if (sv < 0 || sv)
10152 return sv;
10154 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10155 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10157 space = isl_basic_map_get_space(bmap);
10158 space = isl_space_map_from_set(isl_space_range(space));
10159 id = isl_basic_map_identity(space);
10161 sv = isl_basic_map_is_subset(test, id);
10163 isl_basic_map_free(test);
10164 isl_basic_map_free(id);
10166 return sv;
10169 /* Check if the given map is obviously single-valued.
10171 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10173 if (!map)
10174 return -1;
10175 if (map->n == 0)
10176 return 1;
10177 if (map->n >= 2)
10178 return 0;
10180 return isl_basic_map_plain_is_single_valued(map->p[0]);
10183 /* Check if the given map is single-valued.
10184 * We simply compute
10186 * M \circ M^-1
10188 * and check if the result is a subset of the identity mapping.
10190 int isl_map_is_single_valued(__isl_keep isl_map *map)
10192 isl_space *dim;
10193 isl_map *test;
10194 isl_map *id;
10195 int sv;
10197 sv = isl_map_plain_is_single_valued(map);
10198 if (sv < 0 || sv)
10199 return sv;
10201 test = isl_map_reverse(isl_map_copy(map));
10202 test = isl_map_apply_range(test, isl_map_copy(map));
10204 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10205 id = isl_map_identity(dim);
10207 sv = isl_map_is_subset(test, id);
10209 isl_map_free(test);
10210 isl_map_free(id);
10212 return sv;
10215 int isl_map_is_injective(__isl_keep isl_map *map)
10217 int in;
10219 map = isl_map_copy(map);
10220 map = isl_map_reverse(map);
10221 in = isl_map_is_single_valued(map);
10222 isl_map_free(map);
10224 return in;
10227 /* Check if the given map is obviously injective.
10229 int isl_map_plain_is_injective(__isl_keep isl_map *map)
10231 int in;
10233 map = isl_map_copy(map);
10234 map = isl_map_reverse(map);
10235 in = isl_map_plain_is_single_valued(map);
10236 isl_map_free(map);
10238 return in;
10241 int isl_map_is_bijective(__isl_keep isl_map *map)
10243 int sv;
10245 sv = isl_map_is_single_valued(map);
10246 if (sv < 0 || !sv)
10247 return sv;
10249 return isl_map_is_injective(map);
10252 int isl_set_is_singleton(__isl_keep isl_set *set)
10254 return isl_map_is_single_valued((isl_map *)set);
10257 int isl_map_is_translation(__isl_keep isl_map *map)
10259 int ok;
10260 isl_set *delta;
10262 delta = isl_map_deltas(isl_map_copy(map));
10263 ok = isl_set_is_singleton(delta);
10264 isl_set_free(delta);
10266 return ok;
10269 static int unique(isl_int *p, unsigned pos, unsigned len)
10271 if (isl_seq_first_non_zero(p, pos) != -1)
10272 return 0;
10273 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10274 return 0;
10275 return 1;
10278 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10280 int i, j;
10281 unsigned nvar;
10282 unsigned ovar;
10284 if (!bset)
10285 return -1;
10287 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10288 return 0;
10290 nvar = isl_basic_set_dim(bset, isl_dim_set);
10291 ovar = isl_space_offset(bset->dim, isl_dim_set);
10292 for (j = 0; j < nvar; ++j) {
10293 int lower = 0, upper = 0;
10294 for (i = 0; i < bset->n_eq; ++i) {
10295 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10296 continue;
10297 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10298 return 0;
10299 break;
10301 if (i < bset->n_eq)
10302 continue;
10303 for (i = 0; i < bset->n_ineq; ++i) {
10304 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10305 continue;
10306 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10307 return 0;
10308 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10309 lower = 1;
10310 else
10311 upper = 1;
10313 if (!lower || !upper)
10314 return 0;
10317 return 1;
10320 int isl_set_is_box(__isl_keep isl_set *set)
10322 if (!set)
10323 return -1;
10324 if (set->n != 1)
10325 return 0;
10327 return isl_basic_set_is_box(set->p[0]);
10330 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10332 if (!bset)
10333 return -1;
10335 return isl_space_is_wrapping(bset->dim);
10338 int isl_set_is_wrapping(__isl_keep isl_set *set)
10340 if (!set)
10341 return -1;
10343 return isl_space_is_wrapping(set->dim);
10346 /* Is the domain of "map" a wrapped relation?
10348 int isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10350 if (!map)
10351 return -1;
10353 return isl_space_domain_is_wrapping(map->dim);
10356 /* Is the range of "map" a wrapped relation?
10358 int isl_map_range_is_wrapping(__isl_keep isl_map *map)
10360 if (!map)
10361 return -1;
10363 return isl_space_range_is_wrapping(map->dim);
10366 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10368 bmap = isl_basic_map_cow(bmap);
10369 if (!bmap)
10370 return NULL;
10372 bmap->dim = isl_space_wrap(bmap->dim);
10373 if (!bmap->dim)
10374 goto error;
10376 bmap = isl_basic_map_finalize(bmap);
10378 return (isl_basic_set *)bmap;
10379 error:
10380 isl_basic_map_free(bmap);
10381 return NULL;
10384 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10386 int i;
10388 map = isl_map_cow(map);
10389 if (!map)
10390 return NULL;
10392 for (i = 0; i < map->n; ++i) {
10393 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10394 if (!map->p[i])
10395 goto error;
10397 map->dim = isl_space_wrap(map->dim);
10398 if (!map->dim)
10399 goto error;
10401 return (isl_set *)map;
10402 error:
10403 isl_map_free(map);
10404 return NULL;
10407 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10409 bset = isl_basic_set_cow(bset);
10410 if (!bset)
10411 return NULL;
10413 bset->dim = isl_space_unwrap(bset->dim);
10414 if (!bset->dim)
10415 goto error;
10417 bset = isl_basic_set_finalize(bset);
10419 return (isl_basic_map *)bset;
10420 error:
10421 isl_basic_set_free(bset);
10422 return NULL;
10425 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10427 int i;
10429 if (!set)
10430 return NULL;
10432 if (!isl_set_is_wrapping(set))
10433 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10434 goto error);
10436 set = isl_set_cow(set);
10437 if (!set)
10438 return NULL;
10440 for (i = 0; i < set->n; ++i) {
10441 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10442 if (!set->p[i])
10443 goto error;
10446 set->dim = isl_space_unwrap(set->dim);
10447 if (!set->dim)
10448 goto error;
10450 return (isl_map *)set;
10451 error:
10452 isl_set_free(set);
10453 return NULL;
10456 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10457 enum isl_dim_type type)
10459 if (!bmap)
10460 return NULL;
10462 if (!isl_space_is_named_or_nested(bmap->dim, type))
10463 return bmap;
10465 bmap = isl_basic_map_cow(bmap);
10466 if (!bmap)
10467 return NULL;
10469 bmap->dim = isl_space_reset(bmap->dim, type);
10470 if (!bmap->dim)
10471 goto error;
10473 bmap = isl_basic_map_finalize(bmap);
10475 return bmap;
10476 error:
10477 isl_basic_map_free(bmap);
10478 return NULL;
10481 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10482 enum isl_dim_type type)
10484 int i;
10486 if (!map)
10487 return NULL;
10489 if (!isl_space_is_named_or_nested(map->dim, type))
10490 return map;
10492 map = isl_map_cow(map);
10493 if (!map)
10494 return NULL;
10496 for (i = 0; i < map->n; ++i) {
10497 map->p[i] = isl_basic_map_reset(map->p[i], type);
10498 if (!map->p[i])
10499 goto error;
10501 map->dim = isl_space_reset(map->dim, type);
10502 if (!map->dim)
10503 goto error;
10505 return map;
10506 error:
10507 isl_map_free(map);
10508 return NULL;
10511 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10513 if (!bmap)
10514 return NULL;
10516 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10517 return bmap;
10519 bmap = isl_basic_map_cow(bmap);
10520 if (!bmap)
10521 return NULL;
10523 bmap->dim = isl_space_flatten(bmap->dim);
10524 if (!bmap->dim)
10525 goto error;
10527 bmap = isl_basic_map_finalize(bmap);
10529 return bmap;
10530 error:
10531 isl_basic_map_free(bmap);
10532 return NULL;
10535 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10537 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10540 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10541 __isl_take isl_basic_map *bmap)
10543 if (!bmap)
10544 return NULL;
10546 if (!bmap->dim->nested[0])
10547 return bmap;
10549 bmap = isl_basic_map_cow(bmap);
10550 if (!bmap)
10551 return NULL;
10553 bmap->dim = isl_space_flatten_domain(bmap->dim);
10554 if (!bmap->dim)
10555 goto error;
10557 bmap = isl_basic_map_finalize(bmap);
10559 return bmap;
10560 error:
10561 isl_basic_map_free(bmap);
10562 return NULL;
10565 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10566 __isl_take isl_basic_map *bmap)
10568 if (!bmap)
10569 return NULL;
10571 if (!bmap->dim->nested[1])
10572 return bmap;
10574 bmap = isl_basic_map_cow(bmap);
10575 if (!bmap)
10576 return NULL;
10578 bmap->dim = isl_space_flatten_range(bmap->dim);
10579 if (!bmap->dim)
10580 goto error;
10582 bmap = isl_basic_map_finalize(bmap);
10584 return bmap;
10585 error:
10586 isl_basic_map_free(bmap);
10587 return NULL;
10590 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10592 int i;
10594 if (!map)
10595 return NULL;
10597 if (!map->dim->nested[0] && !map->dim->nested[1])
10598 return map;
10600 map = isl_map_cow(map);
10601 if (!map)
10602 return NULL;
10604 for (i = 0; i < map->n; ++i) {
10605 map->p[i] = isl_basic_map_flatten(map->p[i]);
10606 if (!map->p[i])
10607 goto error;
10609 map->dim = isl_space_flatten(map->dim);
10610 if (!map->dim)
10611 goto error;
10613 return map;
10614 error:
10615 isl_map_free(map);
10616 return NULL;
10619 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10621 return (isl_set *)isl_map_flatten((isl_map *)set);
10624 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10626 isl_space *dim, *flat_dim;
10627 isl_map *map;
10629 dim = isl_set_get_space(set);
10630 flat_dim = isl_space_flatten(isl_space_copy(dim));
10631 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10632 map = isl_map_intersect_domain(map, set);
10634 return map;
10637 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10639 int i;
10641 if (!map)
10642 return NULL;
10644 if (!map->dim->nested[0])
10645 return map;
10647 map = isl_map_cow(map);
10648 if (!map)
10649 return NULL;
10651 for (i = 0; i < map->n; ++i) {
10652 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10653 if (!map->p[i])
10654 goto error;
10656 map->dim = isl_space_flatten_domain(map->dim);
10657 if (!map->dim)
10658 goto error;
10660 return map;
10661 error:
10662 isl_map_free(map);
10663 return NULL;
10666 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10668 int i;
10670 if (!map)
10671 return NULL;
10673 if (!map->dim->nested[1])
10674 return map;
10676 map = isl_map_cow(map);
10677 if (!map)
10678 return NULL;
10680 for (i = 0; i < map->n; ++i) {
10681 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10682 if (!map->p[i])
10683 goto error;
10685 map->dim = isl_space_flatten_range(map->dim);
10686 if (!map->dim)
10687 goto error;
10689 return map;
10690 error:
10691 isl_map_free(map);
10692 return NULL;
10695 /* Reorder the dimensions of "bmap" according to the given dim_map
10696 * and set the dimension specification to "dim".
10698 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10699 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10701 isl_basic_map *res;
10702 unsigned flags;
10704 bmap = isl_basic_map_cow(bmap);
10705 if (!bmap || !dim || !dim_map)
10706 goto error;
10708 flags = bmap->flags;
10709 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10710 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10711 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10712 res = isl_basic_map_alloc_space(dim,
10713 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10714 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10715 if (res)
10716 res->flags = flags;
10717 res = isl_basic_map_finalize(res);
10718 return res;
10719 error:
10720 free(dim_map);
10721 isl_basic_map_free(bmap);
10722 isl_space_free(dim);
10723 return NULL;
10726 /* Reorder the dimensions of "map" according to given reordering.
10728 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10729 __isl_take isl_reordering *r)
10731 int i;
10732 struct isl_dim_map *dim_map;
10734 map = isl_map_cow(map);
10735 dim_map = isl_dim_map_from_reordering(r);
10736 if (!map || !r || !dim_map)
10737 goto error;
10739 for (i = 0; i < map->n; ++i) {
10740 struct isl_dim_map *dim_map_i;
10742 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10744 map->p[i] = isl_basic_map_realign(map->p[i],
10745 isl_space_copy(r->dim), dim_map_i);
10747 if (!map->p[i])
10748 goto error;
10751 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10753 isl_reordering_free(r);
10754 free(dim_map);
10755 return map;
10756 error:
10757 free(dim_map);
10758 isl_map_free(map);
10759 isl_reordering_free(r);
10760 return NULL;
10763 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10764 __isl_take isl_reordering *r)
10766 return (isl_set *)isl_map_realign((isl_map *)set, r);
10769 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10770 __isl_take isl_space *model)
10772 isl_ctx *ctx;
10774 if (!map || !model)
10775 goto error;
10777 ctx = isl_space_get_ctx(model);
10778 if (!isl_space_has_named_params(model))
10779 isl_die(ctx, isl_error_invalid,
10780 "model has unnamed parameters", goto error);
10781 if (!isl_space_has_named_params(map->dim))
10782 isl_die(ctx, isl_error_invalid,
10783 "relation has unnamed parameters", goto error);
10784 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10785 isl_reordering *exp;
10787 model = isl_space_drop_dims(model, isl_dim_in,
10788 0, isl_space_dim(model, isl_dim_in));
10789 model = isl_space_drop_dims(model, isl_dim_out,
10790 0, isl_space_dim(model, isl_dim_out));
10791 exp = isl_parameter_alignment_reordering(map->dim, model);
10792 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10793 map = isl_map_realign(map, exp);
10796 isl_space_free(model);
10797 return map;
10798 error:
10799 isl_space_free(model);
10800 isl_map_free(map);
10801 return NULL;
10804 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10805 __isl_take isl_space *model)
10807 return isl_map_align_params(set, model);
10810 /* Align the parameters of "bmap" to those of "model", introducing
10811 * additional parameters if needed.
10813 __isl_give isl_basic_map *isl_basic_map_align_params(
10814 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10816 isl_ctx *ctx;
10818 if (!bmap || !model)
10819 goto error;
10821 ctx = isl_space_get_ctx(model);
10822 if (!isl_space_has_named_params(model))
10823 isl_die(ctx, isl_error_invalid,
10824 "model has unnamed parameters", goto error);
10825 if (!isl_space_has_named_params(bmap->dim))
10826 isl_die(ctx, isl_error_invalid,
10827 "relation has unnamed parameters", goto error);
10828 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10829 isl_reordering *exp;
10830 struct isl_dim_map *dim_map;
10832 model = isl_space_drop_dims(model, isl_dim_in,
10833 0, isl_space_dim(model, isl_dim_in));
10834 model = isl_space_drop_dims(model, isl_dim_out,
10835 0, isl_space_dim(model, isl_dim_out));
10836 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10837 exp = isl_reordering_extend_space(exp,
10838 isl_basic_map_get_space(bmap));
10839 dim_map = isl_dim_map_from_reordering(exp);
10840 bmap = isl_basic_map_realign(bmap,
10841 exp ? isl_space_copy(exp->dim) : NULL,
10842 isl_dim_map_extend(dim_map, bmap));
10843 isl_reordering_free(exp);
10844 free(dim_map);
10847 isl_space_free(model);
10848 return bmap;
10849 error:
10850 isl_space_free(model);
10851 isl_basic_map_free(bmap);
10852 return NULL;
10855 /* Align the parameters of "bset" to those of "model", introducing
10856 * additional parameters if needed.
10858 __isl_give isl_basic_set *isl_basic_set_align_params(
10859 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10861 return isl_basic_map_align_params(bset, model);
10864 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10865 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10866 enum isl_dim_type c2, enum isl_dim_type c3,
10867 enum isl_dim_type c4, enum isl_dim_type c5)
10869 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10870 struct isl_mat *mat;
10871 int i, j, k;
10872 int pos;
10874 if (!bmap)
10875 return NULL;
10876 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
10877 isl_basic_map_total_dim(bmap) + 1);
10878 if (!mat)
10879 return NULL;
10880 for (i = 0; i < bmap->n_eq; ++i)
10881 for (j = 0, pos = 0; j < 5; ++j) {
10882 int off = isl_basic_map_offset(bmap, c[j]);
10883 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10884 isl_int_set(mat->row[i][pos],
10885 bmap->eq[i][off + k]);
10886 ++pos;
10890 return mat;
10893 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
10894 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10895 enum isl_dim_type c2, enum isl_dim_type c3,
10896 enum isl_dim_type c4, enum isl_dim_type c5)
10898 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10899 struct isl_mat *mat;
10900 int i, j, k;
10901 int pos;
10903 if (!bmap)
10904 return NULL;
10905 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
10906 isl_basic_map_total_dim(bmap) + 1);
10907 if (!mat)
10908 return NULL;
10909 for (i = 0; i < bmap->n_ineq; ++i)
10910 for (j = 0, pos = 0; j < 5; ++j) {
10911 int off = isl_basic_map_offset(bmap, c[j]);
10912 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10913 isl_int_set(mat->row[i][pos],
10914 bmap->ineq[i][off + k]);
10915 ++pos;
10919 return mat;
10922 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
10923 __isl_take isl_space *dim,
10924 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10925 enum isl_dim_type c2, enum isl_dim_type c3,
10926 enum isl_dim_type c4, enum isl_dim_type c5)
10928 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10929 isl_basic_map *bmap;
10930 unsigned total;
10931 unsigned extra;
10932 int i, j, k, l;
10933 int pos;
10935 if (!dim || !eq || !ineq)
10936 goto error;
10938 if (eq->n_col != ineq->n_col)
10939 isl_die(dim->ctx, isl_error_invalid,
10940 "equalities and inequalities matrices should have "
10941 "same number of columns", goto error);
10943 total = 1 + isl_space_dim(dim, isl_dim_all);
10945 if (eq->n_col < total)
10946 isl_die(dim->ctx, isl_error_invalid,
10947 "number of columns too small", goto error);
10949 extra = eq->n_col - total;
10951 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
10952 eq->n_row, ineq->n_row);
10953 if (!bmap)
10954 goto error;
10955 for (i = 0; i < extra; ++i) {
10956 k = isl_basic_map_alloc_div(bmap);
10957 if (k < 0)
10958 goto error;
10959 isl_int_set_si(bmap->div[k][0], 0);
10961 for (i = 0; i < eq->n_row; ++i) {
10962 l = isl_basic_map_alloc_equality(bmap);
10963 if (l < 0)
10964 goto error;
10965 for (j = 0, pos = 0; j < 5; ++j) {
10966 int off = isl_basic_map_offset(bmap, c[j]);
10967 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10968 isl_int_set(bmap->eq[l][off + k],
10969 eq->row[i][pos]);
10970 ++pos;
10974 for (i = 0; i < ineq->n_row; ++i) {
10975 l = isl_basic_map_alloc_inequality(bmap);
10976 if (l < 0)
10977 goto error;
10978 for (j = 0, pos = 0; j < 5; ++j) {
10979 int off = isl_basic_map_offset(bmap, c[j]);
10980 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10981 isl_int_set(bmap->ineq[l][off + k],
10982 ineq->row[i][pos]);
10983 ++pos;
10988 isl_space_free(dim);
10989 isl_mat_free(eq);
10990 isl_mat_free(ineq);
10992 bmap = isl_basic_map_simplify(bmap);
10993 return isl_basic_map_finalize(bmap);
10994 error:
10995 isl_space_free(dim);
10996 isl_mat_free(eq);
10997 isl_mat_free(ineq);
10998 return NULL;
11001 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11002 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11003 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11005 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11006 c1, c2, c3, c4, isl_dim_in);
11009 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11010 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11011 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11013 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11014 c1, c2, c3, c4, isl_dim_in);
11017 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11018 __isl_take isl_space *dim,
11019 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11020 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11022 return (isl_basic_set*)
11023 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11024 c1, c2, c3, c4, isl_dim_in);
11027 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11029 if (!bmap)
11030 return -1;
11032 return isl_space_can_zip(bmap->dim);
11035 int isl_map_can_zip(__isl_keep isl_map *map)
11037 if (!map)
11038 return -1;
11040 return isl_space_can_zip(map->dim);
11043 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11044 * (A -> C) -> (B -> D).
11046 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11048 unsigned pos;
11049 unsigned n1;
11050 unsigned n2;
11052 if (!bmap)
11053 return NULL;
11055 if (!isl_basic_map_can_zip(bmap))
11056 isl_die(bmap->ctx, isl_error_invalid,
11057 "basic map cannot be zipped", goto error);
11058 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11059 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11060 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11061 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11062 bmap = isl_basic_map_cow(bmap);
11063 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11064 if (!bmap)
11065 return NULL;
11066 bmap->dim = isl_space_zip(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 -> D), return the corresponding map
11076 * (A -> C) -> (B -> D).
11078 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11080 int i;
11082 if (!map)
11083 return NULL;
11085 if (!isl_map_can_zip(map))
11086 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
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_zip(map->p[i]);
11095 if (!map->p[i])
11096 goto error;
11099 map->dim = isl_space_zip(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_curry to "bmap"?
11110 * That is, does it have a nested relation in its domain?
11112 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11114 if (!bmap)
11115 return -1;
11117 return isl_space_can_curry(bmap->dim);
11120 /* Can we apply isl_map_curry to "map"?
11121 * That is, does it have a nested relation in its domain?
11123 int isl_map_can_curry(__isl_keep isl_map *map)
11125 if (!map)
11126 return -1;
11128 return isl_space_can_curry(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_curry(__isl_take isl_basic_map *bmap)
11137 if (!bmap)
11138 return NULL;
11140 if (!isl_basic_map_can_curry(bmap))
11141 isl_die(bmap->ctx, isl_error_invalid,
11142 "basic map cannot be curried", goto error);
11143 bmap = isl_basic_map_cow(bmap);
11144 if (!bmap)
11145 return NULL;
11146 bmap->dim = isl_space_curry(bmap->dim);
11147 if (!bmap->dim)
11148 goto error;
11149 return bmap;
11150 error:
11151 isl_basic_map_free(bmap);
11152 return NULL;
11155 /* Given a map (A -> B) -> C, return the corresponding map
11156 * A -> (B -> C).
11158 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11160 int i;
11162 if (!map)
11163 return NULL;
11165 if (!isl_map_can_curry(map))
11166 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11167 goto error);
11169 map = isl_map_cow(map);
11170 if (!map)
11171 return NULL;
11173 for (i = 0; i < map->n; ++i) {
11174 map->p[i] = isl_basic_map_curry(map->p[i]);
11175 if (!map->p[i])
11176 goto error;
11179 map->dim = isl_space_curry(map->dim);
11180 if (!map->dim)
11181 goto error;
11183 return map;
11184 error:
11185 isl_map_free(map);
11186 return NULL;
11189 /* Can we apply isl_basic_map_uncurry to "bmap"?
11190 * That is, does it have a nested relation in its domain?
11192 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11194 if (!bmap)
11195 return -1;
11197 return isl_space_can_uncurry(bmap->dim);
11200 /* Can we apply isl_map_uncurry to "map"?
11201 * That is, does it have a nested relation in its domain?
11203 int isl_map_can_uncurry(__isl_keep isl_map *map)
11205 if (!map)
11206 return -1;
11208 return isl_space_can_uncurry(map->dim);
11211 /* Given a basic map A -> (B -> C), return the corresponding basic map
11212 * (A -> B) -> C.
11214 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11217 if (!bmap)
11218 return NULL;
11220 if (!isl_basic_map_can_uncurry(bmap))
11221 isl_die(bmap->ctx, isl_error_invalid,
11222 "basic map cannot be uncurried",
11223 return isl_basic_map_free(bmap));
11224 bmap = isl_basic_map_cow(bmap);
11225 if (!bmap)
11226 return NULL;
11227 bmap->dim = isl_space_uncurry(bmap->dim);
11228 if (!bmap->dim)
11229 return isl_basic_map_free(bmap);
11230 return bmap;
11233 /* Given a map A -> (B -> C), return the corresponding map
11234 * (A -> B) -> C.
11236 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11238 int i;
11240 if (!map)
11241 return NULL;
11243 if (!isl_map_can_uncurry(map))
11244 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11245 return isl_map_free(map));
11247 map = isl_map_cow(map);
11248 if (!map)
11249 return NULL;
11251 for (i = 0; i < map->n; ++i) {
11252 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11253 if (!map->p[i])
11254 return isl_map_free(map);
11257 map->dim = isl_space_uncurry(map->dim);
11258 if (!map->dim)
11259 return isl_map_free(map);
11261 return map;
11264 /* Construct a basic map mapping the domain of the affine expression
11265 * to a one-dimensional range prescribed by the affine expression.
11267 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11269 int k;
11270 int pos;
11271 isl_local_space *ls;
11272 isl_basic_map *bmap;
11274 if (!aff)
11275 return NULL;
11277 ls = isl_aff_get_local_space(aff);
11278 bmap = isl_basic_map_from_local_space(ls);
11279 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11280 k = isl_basic_map_alloc_equality(bmap);
11281 if (k < 0)
11282 goto error;
11284 pos = isl_basic_map_offset(bmap, isl_dim_out);
11285 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11286 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11287 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11288 aff->v->size - (pos + 1));
11290 isl_aff_free(aff);
11291 bmap = isl_basic_map_finalize(bmap);
11292 return bmap;
11293 error:
11294 isl_aff_free(aff);
11295 isl_basic_map_free(bmap);
11296 return NULL;
11299 /* Construct a map mapping the domain of the affine expression
11300 * to a one-dimensional range prescribed by the affine expression.
11302 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11304 isl_basic_map *bmap;
11306 bmap = isl_basic_map_from_aff(aff);
11307 return isl_map_from_basic_map(bmap);
11310 /* Construct a basic map mapping the domain the multi-affine expression
11311 * to its range, with each dimension in the range equated to the
11312 * corresponding affine expression.
11314 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11315 __isl_take isl_multi_aff *maff)
11317 int i;
11318 isl_space *space;
11319 isl_basic_map *bmap;
11321 if (!maff)
11322 return NULL;
11324 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11325 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11326 "invalid space", goto error);
11328 space = isl_space_domain(isl_multi_aff_get_space(maff));
11329 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11331 for (i = 0; i < maff->n; ++i) {
11332 isl_aff *aff;
11333 isl_basic_map *bmap_i;
11335 aff = isl_aff_copy(maff->p[i]);
11336 bmap_i = isl_basic_map_from_aff(aff);
11338 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11341 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11343 isl_multi_aff_free(maff);
11344 return bmap;
11345 error:
11346 isl_multi_aff_free(maff);
11347 return NULL;
11350 /* Construct a map mapping the domain the multi-affine expression
11351 * to its range, with each dimension in the range equated to the
11352 * corresponding affine expression.
11354 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11356 isl_basic_map *bmap;
11358 bmap = isl_basic_map_from_multi_aff(maff);
11359 return isl_map_from_basic_map(bmap);
11362 /* Construct a basic map mapping a domain in the given space to
11363 * to an n-dimensional range, with n the number of elements in the list,
11364 * where each coordinate in the range is prescribed by the
11365 * corresponding affine expression.
11366 * The domains of all affine expressions in the list are assumed to match
11367 * domain_dim.
11369 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11370 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11372 int i;
11373 isl_space *dim;
11374 isl_basic_map *bmap;
11376 if (!list)
11377 return NULL;
11379 dim = isl_space_from_domain(domain_dim);
11380 bmap = isl_basic_map_universe(dim);
11382 for (i = 0; i < list->n; ++i) {
11383 isl_aff *aff;
11384 isl_basic_map *bmap_i;
11386 aff = isl_aff_copy(list->p[i]);
11387 bmap_i = isl_basic_map_from_aff(aff);
11389 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11392 isl_aff_list_free(list);
11393 return bmap;
11396 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11397 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11399 return isl_map_equate(set, type1, pos1, type2, pos2);
11402 /* Construct a basic map where the given dimensions are equal to each other.
11404 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11405 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11407 isl_basic_map *bmap = NULL;
11408 int i;
11410 if (!space)
11411 return NULL;
11413 if (pos1 >= isl_space_dim(space, type1))
11414 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11415 "index out of bounds", goto error);
11416 if (pos2 >= isl_space_dim(space, type2))
11417 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11418 "index out of bounds", goto error);
11420 if (type1 == type2 && pos1 == pos2)
11421 return isl_basic_map_universe(space);
11423 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11424 i = isl_basic_map_alloc_equality(bmap);
11425 if (i < 0)
11426 goto error;
11427 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11428 pos1 += isl_basic_map_offset(bmap, type1);
11429 pos2 += isl_basic_map_offset(bmap, type2);
11430 isl_int_set_si(bmap->eq[i][pos1], -1);
11431 isl_int_set_si(bmap->eq[i][pos2], 1);
11432 bmap = isl_basic_map_finalize(bmap);
11433 isl_space_free(space);
11434 return bmap;
11435 error:
11436 isl_space_free(space);
11437 isl_basic_map_free(bmap);
11438 return NULL;
11441 /* Add a constraint imposing that the given two dimensions are equal.
11443 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11444 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11446 isl_basic_map *eq;
11448 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11450 bmap = isl_basic_map_intersect(bmap, eq);
11452 return bmap;
11455 /* Add a constraint imposing that the given two dimensions are equal.
11457 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11458 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11460 isl_basic_map *bmap;
11462 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11464 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11466 return map;
11469 /* Add a constraint imposing that the given two dimensions have opposite values.
11471 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11472 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11474 isl_basic_map *bmap = NULL;
11475 int i;
11477 if (!map)
11478 return NULL;
11480 if (pos1 >= isl_map_dim(map, type1))
11481 isl_die(map->ctx, isl_error_invalid,
11482 "index out of bounds", goto error);
11483 if (pos2 >= isl_map_dim(map, type2))
11484 isl_die(map->ctx, isl_error_invalid,
11485 "index out of bounds", goto error);
11487 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11488 i = isl_basic_map_alloc_equality(bmap);
11489 if (i < 0)
11490 goto error;
11491 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11492 pos1 += isl_basic_map_offset(bmap, type1);
11493 pos2 += isl_basic_map_offset(bmap, type2);
11494 isl_int_set_si(bmap->eq[i][pos1], 1);
11495 isl_int_set_si(bmap->eq[i][pos2], 1);
11496 bmap = isl_basic_map_finalize(bmap);
11498 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11500 return map;
11501 error:
11502 isl_basic_map_free(bmap);
11503 isl_map_free(map);
11504 return NULL;
11507 /* Construct a constraint imposing that the value of the first dimension is
11508 * greater than or equal to that of the second.
11510 static __isl_give isl_constraint *constraint_order_ge(
11511 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11512 enum isl_dim_type type2, int pos2)
11514 isl_constraint *c;
11516 if (!space)
11517 return NULL;
11519 c = isl_inequality_alloc(isl_local_space_from_space(space));
11521 if (pos1 >= isl_constraint_dim(c, type1))
11522 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11523 "index out of bounds", return isl_constraint_free(c));
11524 if (pos2 >= isl_constraint_dim(c, type2))
11525 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11526 "index out of bounds", return isl_constraint_free(c));
11528 if (type1 == type2 && pos1 == pos2)
11529 return c;
11531 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11532 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11534 return c;
11537 /* Add a constraint imposing that the value of the first dimension is
11538 * greater than or equal to that of the second.
11540 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11541 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11543 isl_constraint *c;
11544 isl_space *space;
11546 if (type1 == type2 && pos1 == pos2)
11547 return bmap;
11548 space = isl_basic_map_get_space(bmap);
11549 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11550 bmap = isl_basic_map_add_constraint(bmap, c);
11552 return bmap;
11555 /* Add a constraint imposing that the value of the first dimension is
11556 * greater than or equal to that of the second.
11558 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11559 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11561 isl_constraint *c;
11562 isl_space *space;
11564 if (type1 == type2 && pos1 == pos2)
11565 return map;
11566 space = isl_map_get_space(map);
11567 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11568 map = isl_map_add_constraint(map, c);
11570 return map;
11573 /* Add a constraint imposing that the value of the first dimension is
11574 * less than or equal to that of the second.
11576 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11577 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11579 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11582 /* Construct a basic map where the value of the first dimension is
11583 * greater than that of the second.
11585 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11586 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11588 isl_basic_map *bmap = NULL;
11589 int i;
11591 if (!space)
11592 return NULL;
11594 if (pos1 >= isl_space_dim(space, type1))
11595 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11596 "index out of bounds", goto error);
11597 if (pos2 >= isl_space_dim(space, type2))
11598 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11599 "index out of bounds", goto error);
11601 if (type1 == type2 && pos1 == pos2)
11602 return isl_basic_map_empty(space);
11604 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11605 i = isl_basic_map_alloc_inequality(bmap);
11606 if (i < 0)
11607 return isl_basic_map_free(bmap);
11608 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11609 pos1 += isl_basic_map_offset(bmap, type1);
11610 pos2 += isl_basic_map_offset(bmap, type2);
11611 isl_int_set_si(bmap->ineq[i][pos1], 1);
11612 isl_int_set_si(bmap->ineq[i][pos2], -1);
11613 isl_int_set_si(bmap->ineq[i][0], -1);
11614 bmap = isl_basic_map_finalize(bmap);
11616 return bmap;
11617 error:
11618 isl_space_free(space);
11619 isl_basic_map_free(bmap);
11620 return NULL;
11623 /* Add a constraint imposing that the value of the first dimension is
11624 * greater than that of the second.
11626 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11627 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11629 isl_basic_map *gt;
11631 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11633 bmap = isl_basic_map_intersect(bmap, gt);
11635 return bmap;
11638 /* Add a constraint imposing that the value of the first dimension is
11639 * greater than that of the second.
11641 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11642 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11644 isl_basic_map *bmap;
11646 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11648 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11650 return map;
11653 /* Add a constraint imposing that the value of the first dimension is
11654 * smaller than that of the second.
11656 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11657 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11659 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11662 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11663 int pos)
11665 isl_aff *div;
11666 isl_local_space *ls;
11668 if (!bmap)
11669 return NULL;
11671 if (!isl_basic_map_divs_known(bmap))
11672 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11673 "some divs are unknown", return NULL);
11675 ls = isl_basic_map_get_local_space(bmap);
11676 div = isl_local_space_get_div(ls, pos);
11677 isl_local_space_free(ls);
11679 return div;
11682 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11683 int pos)
11685 return isl_basic_map_get_div(bset, pos);
11688 /* Plug in "subs" for dimension "type", "pos" of "bset".
11690 * Let i be the dimension to replace and let "subs" be of the form
11692 * f/d
11694 * Any integer division with a non-zero coefficient for i,
11696 * floor((a i + g)/m)
11698 * is replaced by
11700 * floor((a f + d g)/(m d))
11702 * Constraints of the form
11704 * a i + g
11706 * are replaced by
11708 * a f + d g
11710 * We currently require that "subs" is an integral expression.
11711 * Handling rational expressions may require us to add stride constraints
11712 * as we do in isl_basic_set_preimage_multi_aff.
11714 __isl_give isl_basic_set *isl_basic_set_substitute(
11715 __isl_take isl_basic_set *bset,
11716 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11718 int i;
11719 isl_int v;
11720 isl_ctx *ctx;
11722 if (bset && isl_basic_set_plain_is_empty(bset))
11723 return bset;
11725 bset = isl_basic_set_cow(bset);
11726 if (!bset || !subs)
11727 goto error;
11729 ctx = isl_basic_set_get_ctx(bset);
11730 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11731 isl_die(ctx, isl_error_invalid,
11732 "spaces don't match", goto error);
11733 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11734 isl_die(ctx, isl_error_unsupported,
11735 "cannot handle divs yet", goto error);
11736 if (!isl_int_is_one(subs->v->el[0]))
11737 isl_die(ctx, isl_error_invalid,
11738 "can only substitute integer expressions", goto error);
11740 pos += isl_basic_set_offset(bset, type);
11742 isl_int_init(v);
11744 for (i = 0; i < bset->n_eq; ++i) {
11745 if (isl_int_is_zero(bset->eq[i][pos]))
11746 continue;
11747 isl_int_set(v, bset->eq[i][pos]);
11748 isl_int_set_si(bset->eq[i][pos], 0);
11749 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11750 v, subs->v->el + 1, subs->v->size - 1);
11753 for (i = 0; i < bset->n_ineq; ++i) {
11754 if (isl_int_is_zero(bset->ineq[i][pos]))
11755 continue;
11756 isl_int_set(v, bset->ineq[i][pos]);
11757 isl_int_set_si(bset->ineq[i][pos], 0);
11758 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11759 v, subs->v->el + 1, subs->v->size - 1);
11762 for (i = 0; i < bset->n_div; ++i) {
11763 if (isl_int_is_zero(bset->div[i][1 + pos]))
11764 continue;
11765 isl_int_set(v, bset->div[i][1 + pos]);
11766 isl_int_set_si(bset->div[i][1 + pos], 0);
11767 isl_seq_combine(bset->div[i] + 1,
11768 subs->v->el[0], bset->div[i] + 1,
11769 v, subs->v->el + 1, subs->v->size - 1);
11770 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11773 isl_int_clear(v);
11775 bset = isl_basic_set_simplify(bset);
11776 return isl_basic_set_finalize(bset);
11777 error:
11778 isl_basic_set_free(bset);
11779 return NULL;
11782 /* Plug in "subs" for dimension "type", "pos" of "set".
11784 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11785 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11787 int i;
11789 if (set && isl_set_plain_is_empty(set))
11790 return set;
11792 set = isl_set_cow(set);
11793 if (!set || !subs)
11794 goto error;
11796 for (i = set->n - 1; i >= 0; --i) {
11797 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11798 if (remove_if_empty(set, i) < 0)
11799 goto error;
11802 return set;
11803 error:
11804 isl_set_free(set);
11805 return NULL;
11808 /* Check if the range of "ma" is compatible with the domain or range
11809 * (depending on "type") of "bmap".
11810 * Return -1 if anything is wrong.
11812 static int check_basic_map_compatible_range_multi_aff(
11813 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11814 __isl_keep isl_multi_aff *ma)
11816 int m;
11817 isl_space *ma_space;
11819 ma_space = isl_multi_aff_get_space(ma);
11821 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
11822 if (m < 0)
11823 goto error;
11824 if (!m)
11825 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11826 "parameters don't match", goto error);
11827 m = isl_space_tuple_match(bmap->dim, type, ma_space, isl_dim_out);
11828 if (m < 0)
11829 goto error;
11830 if (!m)
11831 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11832 "spaces don't match", goto error);
11834 isl_space_free(ma_space);
11835 return m;
11836 error:
11837 isl_space_free(ma_space);
11838 return -1;
11841 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11842 * coefficients before the transformed range of dimensions,
11843 * the "n_after" coefficients after the transformed range of dimensions
11844 * and the coefficients of the other divs in "bmap".
11846 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
11847 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
11849 int i;
11850 int n_param;
11851 int n_set;
11852 isl_local_space *ls;
11854 if (n_div == 0)
11855 return 0;
11857 ls = isl_aff_get_domain_local_space(ma->p[0]);
11858 if (!ls)
11859 return -1;
11861 n_param = isl_local_space_dim(ls, isl_dim_param);
11862 n_set = isl_local_space_dim(ls, isl_dim_set);
11863 for (i = 0; i < n_div; ++i) {
11864 int o_bmap = 0, o_ls = 0;
11866 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
11867 o_bmap += 1 + 1 + n_param;
11868 o_ls += 1 + 1 + n_param;
11869 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
11870 o_bmap += n_before;
11871 isl_seq_cpy(bmap->div[i] + o_bmap,
11872 ls->div->row[i] + o_ls, n_set);
11873 o_bmap += n_set;
11874 o_ls += n_set;
11875 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
11876 o_bmap += n_after;
11877 isl_seq_cpy(bmap->div[i] + o_bmap,
11878 ls->div->row[i] + o_ls, n_div);
11879 o_bmap += n_div;
11880 o_ls += n_div;
11881 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
11882 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
11883 goto error;
11886 isl_local_space_free(ls);
11887 return 0;
11888 error:
11889 isl_local_space_free(ls);
11890 return -1;
11893 /* How many stride constraints does "ma" enforce?
11894 * That is, how many of the affine expressions have a denominator
11895 * different from one?
11897 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
11899 int i;
11900 int strides = 0;
11902 for (i = 0; i < ma->n; ++i)
11903 if (!isl_int_is_one(ma->p[i]->v->el[0]))
11904 strides++;
11906 return strides;
11909 /* For each affine expression in ma of the form
11911 * x_i = (f_i y + h_i)/m_i
11913 * with m_i different from one, add a constraint to "bmap"
11914 * of the form
11916 * f_i y + h_i = m_i alpha_i
11918 * with alpha_i an additional existentially quantified variable.
11920 static __isl_give isl_basic_map *add_ma_strides(
11921 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
11922 int n_before, int n_after)
11924 int i, k;
11925 int div;
11926 int total;
11927 int n_param;
11928 int n_in;
11929 int n_div;
11931 total = isl_basic_map_total_dim(bmap);
11932 n_param = isl_multi_aff_dim(ma, isl_dim_param);
11933 n_in = isl_multi_aff_dim(ma, isl_dim_in);
11934 n_div = isl_multi_aff_dim(ma, isl_dim_div);
11935 for (i = 0; i < ma->n; ++i) {
11936 int o_bmap = 0, o_ma = 1;
11938 if (isl_int_is_one(ma->p[i]->v->el[0]))
11939 continue;
11940 div = isl_basic_map_alloc_div(bmap);
11941 k = isl_basic_map_alloc_equality(bmap);
11942 if (div < 0 || k < 0)
11943 goto error;
11944 isl_int_set_si(bmap->div[div][0], 0);
11945 isl_seq_cpy(bmap->eq[k] + o_bmap,
11946 ma->p[i]->v->el + o_ma, 1 + n_param);
11947 o_bmap += 1 + n_param;
11948 o_ma += 1 + n_param;
11949 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
11950 o_bmap += n_before;
11951 isl_seq_cpy(bmap->eq[k] + o_bmap,
11952 ma->p[i]->v->el + o_ma, n_in);
11953 o_bmap += n_in;
11954 o_ma += n_in;
11955 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
11956 o_bmap += n_after;
11957 isl_seq_cpy(bmap->eq[k] + o_bmap,
11958 ma->p[i]->v->el + o_ma, n_div);
11959 o_bmap += n_div;
11960 o_ma += n_div;
11961 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
11962 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
11963 total++;
11966 return bmap;
11967 error:
11968 isl_basic_map_free(bmap);
11969 return NULL;
11972 /* Replace the domain or range space (depending on "type) of "space" by "set".
11974 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
11975 enum isl_dim_type type, __isl_take isl_space *set)
11977 if (type == isl_dim_in) {
11978 space = isl_space_range(space);
11979 space = isl_space_map_from_domain_and_range(set, space);
11980 } else {
11981 space = isl_space_domain(space);
11982 space = isl_space_map_from_domain_and_range(space, set);
11985 return space;
11988 /* Compute the preimage of the domain or range (depending on "type")
11989 * of "bmap" under the function represented by "ma".
11990 * In other words, plug in "ma" in the domain or range of "bmap".
11991 * The result is a basic map that lives in the same space as "bmap"
11992 * except that the domain or range has been replaced by
11993 * the domain space of "ma".
11995 * If bmap is represented by
11997 * A(p) + S u + B x + T v + C(divs) >= 0,
11999 * where u and x are input and output dimensions if type == isl_dim_out
12000 * while x and v are input and output dimensions if type == isl_dim_in,
12001 * and ma is represented by
12003 * x = D(p) + F(y) + G(divs')
12005 * then the result is
12007 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12009 * The divs in the input set are similarly adjusted.
12010 * In particular
12012 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12014 * becomes
12016 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12017 * B_i G(divs') + c_i(divs))/n_i)
12019 * If bmap is not a rational map and if F(y) involves any denominators
12021 * x_i = (f_i y + h_i)/m_i
12023 * then additional constraints are added to ensure that we only
12024 * map back integer points. That is we enforce
12026 * f_i y + h_i = m_i alpha_i
12028 * with alpha_i an additional existentially quantified variable.
12030 * We first copy over the divs from "ma".
12031 * Then we add the modified constraints and divs from "bmap".
12032 * Finally, we add the stride constraints, if needed.
12034 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12035 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12036 __isl_take isl_multi_aff *ma)
12038 int i, k;
12039 isl_space *space;
12040 isl_basic_map *res = NULL;
12041 int n_before, n_after, n_div_bmap, n_div_ma;
12042 isl_int f, c1, c2, g;
12043 int rational, strides;
12045 isl_int_init(f);
12046 isl_int_init(c1);
12047 isl_int_init(c2);
12048 isl_int_init(g);
12050 ma = isl_multi_aff_align_divs(ma);
12051 if (!bmap || !ma)
12052 goto error;
12053 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12054 goto error;
12056 if (type == isl_dim_in) {
12057 n_before = 0;
12058 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12059 } else {
12060 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12061 n_after = 0;
12063 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12064 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12066 space = isl_multi_aff_get_domain_space(ma);
12067 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12068 rational = isl_basic_map_is_rational(bmap);
12069 strides = rational ? 0 : multi_aff_strides(ma);
12070 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12071 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12072 if (rational)
12073 res = isl_basic_map_set_rational(res);
12075 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12076 if (isl_basic_map_alloc_div(res) < 0)
12077 goto error;
12079 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12080 goto error;
12082 for (i = 0; i < bmap->n_eq; ++i) {
12083 k = isl_basic_map_alloc_equality(res);
12084 if (k < 0)
12085 goto error;
12086 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12087 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12090 for (i = 0; i < bmap->n_ineq; ++i) {
12091 k = isl_basic_map_alloc_inequality(res);
12092 if (k < 0)
12093 goto error;
12094 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12095 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12098 for (i = 0; i < bmap->n_div; ++i) {
12099 if (isl_int_is_zero(bmap->div[i][0])) {
12100 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12101 continue;
12103 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12104 n_before, n_after, n_div_ma, n_div_bmap,
12105 f, c1, c2, g, 1);
12108 if (strides)
12109 res = add_ma_strides(res, ma, n_before, n_after);
12111 isl_int_clear(f);
12112 isl_int_clear(c1);
12113 isl_int_clear(c2);
12114 isl_int_clear(g);
12115 isl_basic_map_free(bmap);
12116 isl_multi_aff_free(ma);
12117 res = isl_basic_set_simplify(res);
12118 return isl_basic_map_finalize(res);
12119 error:
12120 isl_int_clear(f);
12121 isl_int_clear(c1);
12122 isl_int_clear(c2);
12123 isl_int_clear(g);
12124 isl_basic_map_free(bmap);
12125 isl_multi_aff_free(ma);
12126 isl_basic_map_free(res);
12127 return NULL;
12130 /* Compute the preimage of "bset" under the function represented by "ma".
12131 * In other words, plug in "ma" in "bset". The result is a basic set
12132 * that lives in the domain space of "ma".
12134 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12135 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12137 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12140 /* Compute the preimage of the domain of "bmap" under the function
12141 * represented by "ma".
12142 * In other words, plug in "ma" in the domain of "bmap".
12143 * The result is a basic map that lives in the same space as "bmap"
12144 * except that the domain has been replaced by the domain space of "ma".
12146 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12147 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12149 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12152 /* Compute the preimage of the range of "bmap" under the function
12153 * represented by "ma".
12154 * In other words, plug in "ma" in the range of "bmap".
12155 * The result is a basic map that lives in the same space as "bmap"
12156 * except that the range has been replaced by the domain space of "ma".
12158 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12159 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12161 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12164 /* Check if the range of "ma" is compatible with the domain or range
12165 * (depending on "type") of "map".
12166 * Return -1 if anything is wrong.
12168 static int check_map_compatible_range_multi_aff(
12169 __isl_keep isl_map *map, enum isl_dim_type type,
12170 __isl_keep isl_multi_aff *ma)
12172 int m;
12173 isl_space *ma_space;
12175 ma_space = isl_multi_aff_get_space(ma);
12176 m = isl_space_tuple_match(map->dim, type, ma_space, isl_dim_out);
12177 isl_space_free(ma_space);
12178 if (m >= 0 && !m)
12179 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12180 "spaces don't match", return -1);
12181 return m;
12184 /* Compute the preimage of the domain or range (depending on "type")
12185 * of "map" under the function represented by "ma".
12186 * In other words, plug in "ma" in the domain or range of "map".
12187 * The result is a map that lives in the same space as "map"
12188 * except that the domain or range has been replaced by
12189 * the domain space of "ma".
12191 * The parameters are assumed to have been aligned.
12193 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12194 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12196 int i;
12197 isl_space *space;
12199 map = isl_map_cow(map);
12200 ma = isl_multi_aff_align_divs(ma);
12201 if (!map || !ma)
12202 goto error;
12203 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12204 goto error;
12206 for (i = 0; i < map->n; ++i) {
12207 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12208 isl_multi_aff_copy(ma));
12209 if (!map->p[i])
12210 goto error;
12213 space = isl_multi_aff_get_domain_space(ma);
12214 space = isl_space_set(isl_map_get_space(map), type, space);
12216 isl_space_free(map->dim);
12217 map->dim = space;
12218 if (!map->dim)
12219 goto error;
12221 isl_multi_aff_free(ma);
12222 if (map->n > 1)
12223 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12224 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12225 return map;
12226 error:
12227 isl_multi_aff_free(ma);
12228 isl_map_free(map);
12229 return NULL;
12232 /* Compute the preimage of the domain or range (depending on "type")
12233 * of "map" under the function represented by "ma".
12234 * In other words, plug in "ma" in the domain or range of "map".
12235 * The result is a map that lives in the same space as "map"
12236 * except that the domain or range has been replaced by
12237 * the domain space of "ma".
12239 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12240 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12242 if (!map || !ma)
12243 goto error;
12245 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12246 return map_preimage_multi_aff(map, type, ma);
12248 if (!isl_space_has_named_params(map->dim) ||
12249 !isl_space_has_named_params(ma->space))
12250 isl_die(map->ctx, isl_error_invalid,
12251 "unaligned unnamed parameters", goto error);
12252 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12253 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12255 return map_preimage_multi_aff(map, type, ma);
12256 error:
12257 isl_multi_aff_free(ma);
12258 return isl_map_free(map);
12261 /* Compute the preimage of "set" under the function represented by "ma".
12262 * In other words, plug in "ma" in "set". The result is a set
12263 * that lives in the domain space of "ma".
12265 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12266 __isl_take isl_multi_aff *ma)
12268 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12271 /* Compute the preimage of the domain of "map" under the function
12272 * represented by "ma".
12273 * In other words, plug in "ma" in the domain of "map".
12274 * The result is a map that lives in the same space as "map"
12275 * except that the domain has been replaced by the domain space of "ma".
12277 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12278 __isl_take isl_multi_aff *ma)
12280 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12283 /* Compute the preimage of the range of "map" under the function
12284 * represented by "ma".
12285 * In other words, plug in "ma" in the range of "map".
12286 * The result is a map that lives in the same space as "map"
12287 * except that the range has been replaced by the domain space of "ma".
12289 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12290 __isl_take isl_multi_aff *ma)
12292 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12295 /* Compute the preimage of "map" under the function represented by "pma".
12296 * In other words, plug in "pma" in the domain or range of "map".
12297 * The result is a map that lives in the same space as "map",
12298 * except that the space of type "type" has been replaced by
12299 * the domain space of "pma".
12301 * The parameters of "map" and "pma" are assumed to have been aligned.
12303 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12304 __isl_take isl_map *map, enum isl_dim_type type,
12305 __isl_take isl_pw_multi_aff *pma)
12307 int i;
12308 isl_map *res;
12310 if (!pma)
12311 goto error;
12313 if (pma->n == 0) {
12314 isl_pw_multi_aff_free(pma);
12315 res = isl_map_empty(isl_map_get_space(map));
12316 isl_map_free(map);
12317 return res;
12320 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12321 isl_multi_aff_copy(pma->p[0].maff));
12322 if (type == isl_dim_in)
12323 res = isl_map_intersect_domain(res,
12324 isl_map_copy(pma->p[0].set));
12325 else
12326 res = isl_map_intersect_range(res,
12327 isl_map_copy(pma->p[0].set));
12329 for (i = 1; i < pma->n; ++i) {
12330 isl_map *res_i;
12332 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12333 isl_multi_aff_copy(pma->p[i].maff));
12334 if (type == isl_dim_in)
12335 res_i = isl_map_intersect_domain(res_i,
12336 isl_map_copy(pma->p[i].set));
12337 else
12338 res_i = isl_map_intersect_range(res_i,
12339 isl_map_copy(pma->p[i].set));
12340 res = isl_map_union(res, res_i);
12343 isl_pw_multi_aff_free(pma);
12344 isl_map_free(map);
12345 return res;
12346 error:
12347 isl_pw_multi_aff_free(pma);
12348 isl_map_free(map);
12349 return NULL;
12352 /* Compute the preimage of "map" under the function represented by "pma".
12353 * In other words, plug in "pma" in the domain or range of "map".
12354 * The result is a map that lives in the same space as "map",
12355 * except that the space of type "type" has been replaced by
12356 * the domain space of "pma".
12358 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12359 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12361 if (!map || !pma)
12362 goto error;
12364 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12365 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12367 if (!isl_space_has_named_params(map->dim) ||
12368 !isl_space_has_named_params(pma->dim))
12369 isl_die(map->ctx, isl_error_invalid,
12370 "unaligned unnamed parameters", goto error);
12371 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12372 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12374 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12375 error:
12376 isl_pw_multi_aff_free(pma);
12377 return isl_map_free(map);
12380 /* Compute the preimage of "set" under the function represented by "pma".
12381 * In other words, plug in "pma" in "set". The result is a set
12382 * that lives in the domain space of "pma".
12384 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12385 __isl_take isl_pw_multi_aff *pma)
12387 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12390 /* Compute the preimage of the domain of "map" under the function
12391 * represented by "pma".
12392 * In other words, plug in "pma" in the domain of "map".
12393 * The result is a map that lives in the same space as "map",
12394 * except that domain space has been replaced by the domain space of "pma".
12396 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12397 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12399 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12402 /* Compute the preimage of the range of "map" under the function
12403 * represented by "pma".
12404 * In other words, plug in "pma" in the range of "map".
12405 * The result is a map that lives in the same space as "map",
12406 * except that range space has been replaced by the domain space of "pma".
12408 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12409 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12411 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12414 /* Compute the preimage of "map" under the function represented by "mpa".
12415 * In other words, plug in "mpa" in the domain or range of "map".
12416 * The result is a map that lives in the same space as "map",
12417 * except that the space of type "type" has been replaced by
12418 * the domain space of "mpa".
12420 * If the map does not involve any constraints that refer to the
12421 * dimensions of the substituted space, then the only possible
12422 * effect of "mpa" on the map is to map the space to a different space.
12423 * We create a separate isl_multi_aff to effectuate this change
12424 * in order to avoid spurious splitting of the map along the pieces
12425 * of "mpa".
12427 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12428 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12430 int n;
12431 isl_pw_multi_aff *pma;
12433 if (!map || !mpa)
12434 goto error;
12436 n = isl_map_dim(map, type);
12437 if (!isl_map_involves_dims(map, type, 0, n)) {
12438 isl_space *space;
12439 isl_multi_aff *ma;
12441 space = isl_multi_pw_aff_get_space(mpa);
12442 isl_multi_pw_aff_free(mpa);
12443 ma = isl_multi_aff_zero(space);
12444 return isl_map_preimage_multi_aff(map, type, ma);
12447 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12448 return isl_map_preimage_pw_multi_aff(map, type, pma);
12449 error:
12450 isl_map_free(map);
12451 isl_multi_pw_aff_free(mpa);
12452 return NULL;
12455 /* Compute the preimage of "map" under the function represented by "mpa".
12456 * In other words, plug in "mpa" in the domain "map".
12457 * The result is a map that lives in the same space as "map",
12458 * except that domain space has been replaced by the domain space of "mpa".
12460 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12461 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12463 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12466 /* Compute the preimage of "set" by the function represented by "mpa".
12467 * In other words, plug in "mpa" in "set".
12469 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12470 __isl_take isl_multi_pw_aff *mpa)
12472 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);