isl_ast_build_expr_from_set: return valid result on empty set
[isl.git] / isl_map.c
blob9014b2ea96154abc69e14ff9855518643b5ed83d
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_extend_constraints(bmap, 0, lb + ub);
1990 if (lb) {
1991 int k = isl_basic_map_alloc_inequality(bmap);
1992 if (k < 0)
1993 goto error;
1994 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1995 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
1996 bmap->div[div][1 + j]);
1997 isl_int_cdiv_q(bmap->ineq[k][j],
1998 bmap->ineq[k][j], bmap->div[div][0]);
2000 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2002 if (ub) {
2003 int k = isl_basic_map_alloc_inequality(bmap);
2004 if (k < 0)
2005 goto error;
2006 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2007 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2008 bmap->div[div][1 + j]);
2009 isl_int_fdiv_q(bmap->ineq[k][j],
2010 bmap->ineq[k][j], bmap->div[div][0]);
2012 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2015 return bmap;
2016 error:
2017 isl_basic_map_free(bmap);
2018 return NULL;
2021 /* This function is called right before "div" is eliminated from "bmap"
2022 * using Fourier-Motzkin.
2023 * Look through the constraints of "bmap" for constraints on the argument
2024 * of the integer division and use them to construct constraints on the
2025 * integer division itself. These constraints can then be combined
2026 * during the Fourier-Motzkin elimination.
2027 * Note that it is only useful to introduce lower bounds on "div"
2028 * if "bmap" already contains upper bounds on "div" as the newly
2029 * introduce lower bounds can then be combined with the pre-existing
2030 * upper bounds. Similarly for upper bounds.
2031 * We therefore first check if "bmap" contains any lower and/or upper bounds
2032 * on "div".
2034 * It is interesting to note that the introduction of these constraints
2035 * can indeed lead to more accurate results, even when compared to
2036 * deriving constraints on the argument of "div" from constraints on "div".
2037 * Consider, for example, the set
2039 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2041 * The second constraint can be rewritten as
2043 * 2 * [(-i-2j+3)/4] + k >= 0
2045 * from which we can derive
2047 * -i - 2j + 3 >= -2k
2049 * or
2051 * i + 2j <= 3 + 2k
2053 * Combined with the first constraint, we obtain
2055 * -3 <= 3 + 2k or k >= -3
2057 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2058 * the first constraint, we obtain
2060 * [(i + 2j)/4] >= [-3/4] = -1
2062 * Combining this constraint with the second constraint, we obtain
2064 * k >= -2
2066 static __isl_give isl_basic_map *insert_bounds_on_div(
2067 __isl_take isl_basic_map *bmap, int div)
2069 int i;
2070 int check_lb, check_ub;
2071 isl_int v;
2072 unsigned total;
2074 if (!bmap)
2075 return NULL;
2077 if (isl_int_is_zero(bmap->div[div][0]))
2078 return bmap;
2080 total = isl_space_dim(bmap->dim, isl_dim_all);
2082 check_lb = 0;
2083 check_ub = 0;
2084 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2085 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2086 if (s > 0)
2087 check_ub = 1;
2088 if (s < 0)
2089 check_lb = 1;
2092 if (!check_lb && !check_ub)
2093 return bmap;
2095 isl_int_init(v);
2097 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2098 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2099 continue;
2101 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2102 check_lb, check_ub);
2105 isl_int_clear(v);
2107 return bmap;
2110 /* Remove all divs (recursively) involving any of the given dimensions
2111 * in their definitions.
2113 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2114 __isl_take isl_basic_map *bmap,
2115 enum isl_dim_type type, unsigned first, unsigned n)
2117 int i;
2119 if (!bmap)
2120 return NULL;
2121 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2122 goto error);
2123 first += isl_basic_map_offset(bmap, type);
2125 for (i = bmap->n_div - 1; i >= 0; --i) {
2126 if (!div_involves_vars(bmap, i, first, n))
2127 continue;
2128 bmap = insert_bounds_on_div(bmap, i);
2129 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2130 if (!bmap)
2131 return NULL;
2132 i = bmap->n_div;
2135 return bmap;
2136 error:
2137 isl_basic_map_free(bmap);
2138 return NULL;
2141 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2142 __isl_take isl_basic_set *bset,
2143 enum isl_dim_type type, unsigned first, unsigned n)
2145 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2148 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2149 enum isl_dim_type type, unsigned first, unsigned n)
2151 int i;
2153 if (!map)
2154 return NULL;
2155 if (map->n == 0)
2156 return map;
2158 map = isl_map_cow(map);
2159 if (!map)
2160 return NULL;
2162 for (i = 0; i < map->n; ++i) {
2163 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2164 type, first, n);
2165 if (!map->p[i])
2166 goto error;
2168 return map;
2169 error:
2170 isl_map_free(map);
2171 return NULL;
2174 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2175 enum isl_dim_type type, unsigned first, unsigned n)
2177 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2178 type, first, n);
2181 /* Does the desciption of "bmap" depend on the specified dimensions?
2182 * We also check whether the dimensions appear in any of the div definitions.
2183 * In principle there is no need for this check. If the dimensions appear
2184 * in a div definition, they also appear in the defining constraints of that
2185 * div.
2187 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2188 enum isl_dim_type type, unsigned first, unsigned n)
2190 int i;
2192 if (!bmap)
2193 return -1;
2195 if (first + n > isl_basic_map_dim(bmap, type))
2196 isl_die(bmap->ctx, isl_error_invalid,
2197 "index out of bounds", return -1);
2199 first += isl_basic_map_offset(bmap, type);
2200 for (i = 0; i < bmap->n_eq; ++i)
2201 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2202 return 1;
2203 for (i = 0; i < bmap->n_ineq; ++i)
2204 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2205 return 1;
2206 for (i = 0; i < bmap->n_div; ++i) {
2207 if (isl_int_is_zero(bmap->div[i][0]))
2208 continue;
2209 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2210 return 1;
2213 return 0;
2216 int isl_map_involves_dims(__isl_keep isl_map *map,
2217 enum isl_dim_type type, unsigned first, unsigned n)
2219 int i;
2221 if (!map)
2222 return -1;
2224 if (first + n > isl_map_dim(map, type))
2225 isl_die(map->ctx, isl_error_invalid,
2226 "index out of bounds", return -1);
2228 for (i = 0; i < map->n; ++i) {
2229 int involves = isl_basic_map_involves_dims(map->p[i],
2230 type, first, n);
2231 if (involves < 0 || involves)
2232 return involves;
2235 return 0;
2238 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2239 enum isl_dim_type type, unsigned first, unsigned n)
2241 return isl_basic_map_involves_dims(bset, type, first, n);
2244 int isl_set_involves_dims(__isl_keep isl_set *set,
2245 enum isl_dim_type type, unsigned first, unsigned n)
2247 return isl_map_involves_dims(set, type, first, n);
2250 /* Return true if the definition of the given div is unknown or depends
2251 * on unknown divs.
2253 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2255 int i;
2256 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2258 if (isl_int_is_zero(bmap->div[div][0]))
2259 return 1;
2261 for (i = bmap->n_div - 1; i >= 0; --i) {
2262 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2263 continue;
2264 if (div_is_unknown(bmap, i))
2265 return 1;
2268 return 0;
2271 /* Remove all divs that are unknown or defined in terms of unknown divs.
2273 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2274 __isl_take isl_basic_map *bmap)
2276 int i;
2278 if (!bmap)
2279 return NULL;
2281 for (i = bmap->n_div - 1; i >= 0; --i) {
2282 if (!div_is_unknown(bmap, i))
2283 continue;
2284 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2285 if (!bmap)
2286 return NULL;
2287 i = bmap->n_div;
2290 return bmap;
2293 /* Remove all divs that are unknown or defined in terms of unknown divs.
2295 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2296 __isl_take isl_basic_set *bset)
2298 return isl_basic_map_remove_unknown_divs(bset);
2301 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2303 int i;
2305 if (!map)
2306 return NULL;
2307 if (map->n == 0)
2308 return map;
2310 map = isl_map_cow(map);
2311 if (!map)
2312 return NULL;
2314 for (i = 0; i < map->n; ++i) {
2315 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2316 if (!map->p[i])
2317 goto error;
2319 return map;
2320 error:
2321 isl_map_free(map);
2322 return NULL;
2325 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2327 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2330 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2331 __isl_take isl_basic_set *bset,
2332 enum isl_dim_type type, unsigned first, unsigned n)
2334 return (isl_basic_set *)
2335 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2338 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2339 enum isl_dim_type type, unsigned first, unsigned n)
2341 int i;
2343 if (n == 0)
2344 return map;
2346 map = isl_map_cow(map);
2347 if (!map)
2348 return NULL;
2349 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2351 for (i = 0; i < map->n; ++i) {
2352 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2353 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2354 if (!map->p[i])
2355 goto error;
2357 map = isl_map_drop(map, type, first, n);
2358 return map;
2359 error:
2360 isl_map_free(map);
2361 return NULL;
2364 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2365 enum isl_dim_type type, unsigned first, unsigned n)
2367 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2370 /* Project out n inputs starting at first using Fourier-Motzkin */
2371 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2372 unsigned first, unsigned n)
2374 return isl_map_remove_dims(map, isl_dim_in, first, n);
2377 static void dump_term(struct isl_basic_map *bmap,
2378 isl_int c, int pos, FILE *out)
2380 const char *name;
2381 unsigned in = isl_basic_map_n_in(bmap);
2382 unsigned dim = in + isl_basic_map_n_out(bmap);
2383 unsigned nparam = isl_basic_map_n_param(bmap);
2384 if (!pos)
2385 isl_int_print(out, c, 0);
2386 else {
2387 if (!isl_int_is_one(c))
2388 isl_int_print(out, c, 0);
2389 if (pos < 1 + nparam) {
2390 name = isl_space_get_dim_name(bmap->dim,
2391 isl_dim_param, pos - 1);
2392 if (name)
2393 fprintf(out, "%s", name);
2394 else
2395 fprintf(out, "p%d", pos - 1);
2396 } else if (pos < 1 + nparam + in)
2397 fprintf(out, "i%d", pos - 1 - nparam);
2398 else if (pos < 1 + nparam + dim)
2399 fprintf(out, "o%d", pos - 1 - nparam - in);
2400 else
2401 fprintf(out, "e%d", pos - 1 - nparam - dim);
2405 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2406 int sign, FILE *out)
2408 int i;
2409 int first;
2410 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2411 isl_int v;
2413 isl_int_init(v);
2414 for (i = 0, first = 1; i < len; ++i) {
2415 if (isl_int_sgn(c[i]) * sign <= 0)
2416 continue;
2417 if (!first)
2418 fprintf(out, " + ");
2419 first = 0;
2420 isl_int_abs(v, c[i]);
2421 dump_term(bmap, v, i, out);
2423 isl_int_clear(v);
2424 if (first)
2425 fprintf(out, "0");
2428 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2429 const char *op, FILE *out, int indent)
2431 int i;
2433 fprintf(out, "%*s", indent, "");
2435 dump_constraint_sign(bmap, c, 1, out);
2436 fprintf(out, " %s ", op);
2437 dump_constraint_sign(bmap, c, -1, out);
2439 fprintf(out, "\n");
2441 for (i = bmap->n_div; i < bmap->extra; ++i) {
2442 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2443 continue;
2444 fprintf(out, "%*s", indent, "");
2445 fprintf(out, "ERROR: unused div coefficient not zero\n");
2446 abort();
2450 static void dump_constraints(struct isl_basic_map *bmap,
2451 isl_int **c, unsigned n,
2452 const char *op, FILE *out, int indent)
2454 int i;
2456 for (i = 0; i < n; ++i)
2457 dump_constraint(bmap, c[i], op, out, indent);
2460 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2462 int j;
2463 int first = 1;
2464 unsigned total = isl_basic_map_total_dim(bmap);
2466 for (j = 0; j < 1 + total; ++j) {
2467 if (isl_int_is_zero(exp[j]))
2468 continue;
2469 if (!first && isl_int_is_pos(exp[j]))
2470 fprintf(out, "+");
2471 dump_term(bmap, exp[j], j, out);
2472 first = 0;
2476 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2478 int i;
2480 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2481 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2483 for (i = 0; i < bmap->n_div; ++i) {
2484 fprintf(out, "%*s", indent, "");
2485 fprintf(out, "e%d = [(", i);
2486 dump_affine(bmap, bmap->div[i]+1, out);
2487 fprintf(out, ")/");
2488 isl_int_print(out, bmap->div[i][0], 0);
2489 fprintf(out, "]\n");
2493 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2494 FILE *out, int indent)
2496 if (!bset) {
2497 fprintf(out, "null basic set\n");
2498 return;
2501 fprintf(out, "%*s", indent, "");
2502 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2503 bset->ref, bset->dim->nparam, bset->dim->n_out,
2504 bset->extra, bset->flags);
2505 dump((struct isl_basic_map *)bset, out, indent);
2508 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2509 FILE *out, int indent)
2511 if (!bmap) {
2512 fprintf(out, "null basic map\n");
2513 return;
2516 fprintf(out, "%*s", indent, "");
2517 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2518 "flags: %x, n_name: %d\n",
2519 bmap->ref,
2520 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2521 bmap->extra, bmap->flags, bmap->dim->n_id);
2522 dump(bmap, out, indent);
2525 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2527 unsigned total;
2528 if (!bmap)
2529 return -1;
2530 total = isl_basic_map_total_dim(bmap);
2531 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2532 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2533 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2534 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2535 return 0;
2538 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2539 unsigned flags)
2541 struct isl_set *set;
2543 if (!dim)
2544 return NULL;
2545 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2546 isl_assert(dim->ctx, n >= 0, goto error);
2547 set = isl_alloc(dim->ctx, struct isl_set,
2548 sizeof(struct isl_set) +
2549 (n - 1) * sizeof(struct isl_basic_set *));
2550 if (!set)
2551 goto error;
2553 set->ctx = dim->ctx;
2554 isl_ctx_ref(set->ctx);
2555 set->ref = 1;
2556 set->size = n;
2557 set->n = 0;
2558 set->dim = dim;
2559 set->flags = flags;
2560 return set;
2561 error:
2562 isl_space_free(dim);
2563 return NULL;
2566 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2567 unsigned nparam, unsigned dim, int n, unsigned flags)
2569 struct isl_set *set;
2570 isl_space *dims;
2572 dims = isl_space_alloc(ctx, nparam, 0, dim);
2573 if (!dims)
2574 return NULL;
2576 set = isl_set_alloc_space(dims, n, flags);
2577 return set;
2580 /* Make sure "map" has room for at least "n" more basic maps.
2582 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2584 int i;
2585 struct isl_map *grown = NULL;
2587 if (!map)
2588 return NULL;
2589 isl_assert(map->ctx, n >= 0, goto error);
2590 if (map->n + n <= map->size)
2591 return map;
2592 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2593 if (!grown)
2594 goto error;
2595 for (i = 0; i < map->n; ++i) {
2596 grown->p[i] = isl_basic_map_copy(map->p[i]);
2597 if (!grown->p[i])
2598 goto error;
2599 grown->n++;
2601 isl_map_free(map);
2602 return grown;
2603 error:
2604 isl_map_free(grown);
2605 isl_map_free(map);
2606 return NULL;
2609 /* Make sure "set" has room for at least "n" more basic sets.
2611 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2613 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2616 struct isl_set *isl_set_dup(struct isl_set *set)
2618 int i;
2619 struct isl_set *dup;
2621 if (!set)
2622 return NULL;
2624 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2625 if (!dup)
2626 return NULL;
2627 for (i = 0; i < set->n; ++i)
2628 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2629 return dup;
2632 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2634 return isl_map_from_basic_map(bset);
2637 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2639 struct isl_map *map;
2641 if (!bmap)
2642 return NULL;
2644 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2645 return isl_map_add_basic_map(map, bmap);
2648 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2649 __isl_take isl_basic_set *bset)
2651 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2652 (struct isl_basic_map *)bset);
2655 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2657 int i;
2659 if (!set)
2660 return NULL;
2662 if (--set->ref > 0)
2663 return NULL;
2665 isl_ctx_deref(set->ctx);
2666 for (i = 0; i < set->n; ++i)
2667 isl_basic_set_free(set->p[i]);
2668 isl_space_free(set->dim);
2669 free(set);
2671 return NULL;
2674 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2676 int i;
2678 if (!set) {
2679 fprintf(out, "null set\n");
2680 return;
2683 fprintf(out, "%*s", indent, "");
2684 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2685 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2686 set->flags);
2687 for (i = 0; i < set->n; ++i) {
2688 fprintf(out, "%*s", indent, "");
2689 fprintf(out, "basic set %d:\n", i);
2690 isl_basic_set_print_internal(set->p[i], out, indent+4);
2694 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2696 int i;
2698 if (!map) {
2699 fprintf(out, "null map\n");
2700 return;
2703 fprintf(out, "%*s", indent, "");
2704 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2705 "flags: %x, n_name: %d\n",
2706 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2707 map->dim->n_out, map->flags, map->dim->n_id);
2708 for (i = 0; i < map->n; ++i) {
2709 fprintf(out, "%*s", indent, "");
2710 fprintf(out, "basic map %d:\n", i);
2711 isl_basic_map_print_internal(map->p[i], out, indent+4);
2715 struct isl_basic_map *isl_basic_map_intersect_domain(
2716 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2718 struct isl_basic_map *bmap_domain;
2720 if (!bmap || !bset)
2721 goto error;
2723 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2724 bset->dim, isl_dim_param), goto error);
2726 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2727 isl_assert(bset->ctx,
2728 isl_basic_map_compatible_domain(bmap, bset), goto error);
2730 bmap = isl_basic_map_cow(bmap);
2731 if (!bmap)
2732 goto error;
2733 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2734 bset->n_div, bset->n_eq, bset->n_ineq);
2735 bmap_domain = isl_basic_map_from_domain(bset);
2736 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2738 bmap = isl_basic_map_simplify(bmap);
2739 return isl_basic_map_finalize(bmap);
2740 error:
2741 isl_basic_map_free(bmap);
2742 isl_basic_set_free(bset);
2743 return NULL;
2746 struct isl_basic_map *isl_basic_map_intersect_range(
2747 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2749 struct isl_basic_map *bmap_range;
2751 if (!bmap || !bset)
2752 goto error;
2754 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2755 bset->dim, isl_dim_param), goto error);
2757 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2758 isl_assert(bset->ctx,
2759 isl_basic_map_compatible_range(bmap, bset), goto error);
2761 if (isl_basic_set_is_universe(bset)) {
2762 isl_basic_set_free(bset);
2763 return bmap;
2766 bmap = isl_basic_map_cow(bmap);
2767 if (!bmap)
2768 goto error;
2769 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2770 bset->n_div, bset->n_eq, bset->n_ineq);
2771 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2772 bmap = add_constraints(bmap, bmap_range, 0, 0);
2774 bmap = isl_basic_map_simplify(bmap);
2775 return isl_basic_map_finalize(bmap);
2776 error:
2777 isl_basic_map_free(bmap);
2778 isl_basic_set_free(bset);
2779 return NULL;
2782 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2784 int i;
2785 unsigned total;
2786 isl_int s;
2788 if (!bmap || !vec)
2789 return -1;
2791 total = 1 + isl_basic_map_total_dim(bmap);
2792 if (total != vec->size)
2793 return -1;
2795 isl_int_init(s);
2797 for (i = 0; i < bmap->n_eq; ++i) {
2798 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2799 if (!isl_int_is_zero(s)) {
2800 isl_int_clear(s);
2801 return 0;
2805 for (i = 0; i < bmap->n_ineq; ++i) {
2806 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2807 if (isl_int_is_neg(s)) {
2808 isl_int_clear(s);
2809 return 0;
2813 isl_int_clear(s);
2815 return 1;
2818 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2820 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2823 struct isl_basic_map *isl_basic_map_intersect(
2824 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2826 struct isl_vec *sample = NULL;
2828 if (!bmap1 || !bmap2)
2829 goto error;
2831 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2832 bmap2->dim, isl_dim_param), goto error);
2833 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2834 isl_space_dim(bmap1->dim, isl_dim_param) &&
2835 isl_space_dim(bmap2->dim, isl_dim_all) !=
2836 isl_space_dim(bmap2->dim, isl_dim_param))
2837 return isl_basic_map_intersect(bmap2, bmap1);
2839 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2840 isl_space_dim(bmap2->dim, isl_dim_param))
2841 isl_assert(bmap1->ctx,
2842 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2844 if (bmap1->sample &&
2845 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2846 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2847 sample = isl_vec_copy(bmap1->sample);
2848 else if (bmap2->sample &&
2849 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2850 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2851 sample = isl_vec_copy(bmap2->sample);
2853 bmap1 = isl_basic_map_cow(bmap1);
2854 if (!bmap1)
2855 goto error;
2856 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2857 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2858 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2860 if (!bmap1)
2861 isl_vec_free(sample);
2862 else if (sample) {
2863 isl_vec_free(bmap1->sample);
2864 bmap1->sample = sample;
2867 bmap1 = isl_basic_map_simplify(bmap1);
2868 return isl_basic_map_finalize(bmap1);
2869 error:
2870 if (sample)
2871 isl_vec_free(sample);
2872 isl_basic_map_free(bmap1);
2873 isl_basic_map_free(bmap2);
2874 return NULL;
2877 struct isl_basic_set *isl_basic_set_intersect(
2878 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2880 return (struct isl_basic_set *)
2881 isl_basic_map_intersect(
2882 (struct isl_basic_map *)bset1,
2883 (struct isl_basic_map *)bset2);
2886 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2887 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2889 return isl_basic_set_intersect(bset1, bset2);
2892 /* Special case of isl_map_intersect, where both map1 and map2
2893 * are convex, without any divs and such that either map1 or map2
2894 * contains a single constraint. This constraint is then simply
2895 * added to the other map.
2897 static __isl_give isl_map *map_intersect_add_constraint(
2898 __isl_take isl_map *map1, __isl_take isl_map *map2)
2900 isl_assert(map1->ctx, map1->n == 1, goto error);
2901 isl_assert(map2->ctx, map1->n == 1, goto error);
2902 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2903 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2905 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2906 return isl_map_intersect(map2, map1);
2908 isl_assert(map2->ctx,
2909 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2911 map1 = isl_map_cow(map1);
2912 if (!map1)
2913 goto error;
2914 if (isl_map_plain_is_empty(map1)) {
2915 isl_map_free(map2);
2916 return map1;
2918 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2919 if (map2->p[0]->n_eq == 1)
2920 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2921 else
2922 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2923 map2->p[0]->ineq[0]);
2925 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2926 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2927 if (!map1->p[0])
2928 goto error;
2930 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2931 isl_basic_map_free(map1->p[0]);
2932 map1->n = 0;
2935 isl_map_free(map2);
2937 return map1;
2938 error:
2939 isl_map_free(map1);
2940 isl_map_free(map2);
2941 return NULL;
2944 /* map2 may be either a parameter domain or a map living in the same
2945 * space as map1.
2947 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2948 __isl_take isl_map *map2)
2950 unsigned flags = 0;
2951 isl_map *result;
2952 int i, j;
2954 if (!map1 || !map2)
2955 goto error;
2957 if ((isl_map_plain_is_empty(map1) ||
2958 isl_map_plain_is_universe(map2)) &&
2959 isl_space_is_equal(map1->dim, map2->dim)) {
2960 isl_map_free(map2);
2961 return map1;
2963 if ((isl_map_plain_is_empty(map2) ||
2964 isl_map_plain_is_universe(map1)) &&
2965 isl_space_is_equal(map1->dim, map2->dim)) {
2966 isl_map_free(map1);
2967 return map2;
2970 if (map1->n == 1 && map2->n == 1 &&
2971 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2972 isl_space_is_equal(map1->dim, map2->dim) &&
2973 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2974 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2975 return map_intersect_add_constraint(map1, map2);
2977 if (isl_space_dim(map2->dim, isl_dim_all) !=
2978 isl_space_dim(map2->dim, isl_dim_param))
2979 isl_assert(map1->ctx,
2980 isl_space_is_equal(map1->dim, map2->dim), goto error);
2982 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2983 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2984 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2986 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2987 map1->n * map2->n, flags);
2988 if (!result)
2989 goto error;
2990 for (i = 0; i < map1->n; ++i)
2991 for (j = 0; j < map2->n; ++j) {
2992 struct isl_basic_map *part;
2993 part = isl_basic_map_intersect(
2994 isl_basic_map_copy(map1->p[i]),
2995 isl_basic_map_copy(map2->p[j]));
2996 if (isl_basic_map_is_empty(part) < 0)
2997 part = isl_basic_map_free(part);
2998 result = isl_map_add_basic_map(result, part);
2999 if (!result)
3000 goto error;
3002 isl_map_free(map1);
3003 isl_map_free(map2);
3004 return result;
3005 error:
3006 isl_map_free(map1);
3007 isl_map_free(map2);
3008 return NULL;
3011 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3012 __isl_take isl_map *map2)
3014 if (!map1 || !map2)
3015 goto error;
3016 if (!isl_space_is_equal(map1->dim, map2->dim))
3017 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3018 "spaces don't match", goto error);
3019 return map_intersect_internal(map1, map2);
3020 error:
3021 isl_map_free(map1);
3022 isl_map_free(map2);
3023 return NULL;
3026 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3027 __isl_take isl_map *map2)
3029 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3032 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3034 return (struct isl_set *)
3035 isl_map_intersect((struct isl_map *)set1,
3036 (struct isl_map *)set2);
3039 /* map_intersect_internal accepts intersections
3040 * with parameter domains, so we can just call that function.
3042 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3043 __isl_take isl_set *params)
3045 return map_intersect_internal(map, params);
3048 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3049 __isl_take isl_map *map2)
3051 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3054 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3055 __isl_take isl_set *params)
3057 return isl_map_intersect_params(set, params);
3060 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3062 isl_space *dim;
3063 struct isl_basic_set *bset;
3064 unsigned in;
3066 if (!bmap)
3067 return NULL;
3068 bmap = isl_basic_map_cow(bmap);
3069 if (!bmap)
3070 return NULL;
3071 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3072 in = isl_basic_map_n_in(bmap);
3073 bset = isl_basic_set_from_basic_map(bmap);
3074 bset = isl_basic_set_swap_vars(bset, in);
3075 return isl_basic_map_from_basic_set(bset, dim);
3078 static __isl_give isl_basic_map *basic_map_space_reset(
3079 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3081 isl_space *space;
3083 if (!bmap)
3084 return NULL;
3085 if (!isl_space_is_named_or_nested(bmap->dim, type))
3086 return bmap;
3088 space = isl_basic_map_get_space(bmap);
3089 space = isl_space_reset(space, type);
3090 bmap = isl_basic_map_reset_space(bmap, space);
3091 return bmap;
3094 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3095 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3096 unsigned pos, unsigned n)
3098 isl_space *res_dim;
3099 struct isl_basic_map *res;
3100 struct isl_dim_map *dim_map;
3101 unsigned total, off;
3102 enum isl_dim_type t;
3104 if (n == 0)
3105 return basic_map_space_reset(bmap, type);
3107 if (!bmap)
3108 return NULL;
3110 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3112 total = isl_basic_map_total_dim(bmap) + n;
3113 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3114 off = 0;
3115 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3116 if (t != type) {
3117 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3118 } else {
3119 unsigned size = isl_basic_map_dim(bmap, t);
3120 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3121 0, pos, off);
3122 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3123 pos, size - pos, off + pos + n);
3125 off += isl_space_dim(res_dim, t);
3127 isl_dim_map_div(dim_map, bmap, off);
3129 res = isl_basic_map_alloc_space(res_dim,
3130 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3131 if (isl_basic_map_is_rational(bmap))
3132 res = isl_basic_map_set_rational(res);
3133 if (isl_basic_map_plain_is_empty(bmap)) {
3134 isl_basic_map_free(bmap);
3135 free(dim_map);
3136 return isl_basic_map_set_to_empty(res);
3138 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3139 return isl_basic_map_finalize(res);
3142 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3143 __isl_take isl_basic_set *bset,
3144 enum isl_dim_type type, unsigned pos, unsigned n)
3146 return isl_basic_map_insert_dims(bset, type, pos, n);
3149 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3150 enum isl_dim_type type, unsigned n)
3152 if (!bmap)
3153 return NULL;
3154 return isl_basic_map_insert_dims(bmap, type,
3155 isl_basic_map_dim(bmap, type), n);
3158 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3159 enum isl_dim_type type, unsigned n)
3161 if (!bset)
3162 return NULL;
3163 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3164 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3165 error:
3166 isl_basic_set_free(bset);
3167 return NULL;
3170 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3171 enum isl_dim_type type)
3173 isl_space *space;
3175 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3176 return map;
3178 space = isl_map_get_space(map);
3179 space = isl_space_reset(space, type);
3180 map = isl_map_reset_space(map, space);
3181 return map;
3184 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3185 enum isl_dim_type type, unsigned pos, unsigned n)
3187 int i;
3189 if (n == 0)
3190 return map_space_reset(map, type);
3192 map = isl_map_cow(map);
3193 if (!map)
3194 return NULL;
3196 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3197 if (!map->dim)
3198 goto error;
3200 for (i = 0; i < map->n; ++i) {
3201 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3202 if (!map->p[i])
3203 goto error;
3206 return map;
3207 error:
3208 isl_map_free(map);
3209 return NULL;
3212 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3213 enum isl_dim_type type, unsigned pos, unsigned n)
3215 return isl_map_insert_dims(set, type, pos, n);
3218 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3219 enum isl_dim_type type, unsigned n)
3221 if (!map)
3222 return NULL;
3223 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3226 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3227 enum isl_dim_type type, unsigned n)
3229 if (!set)
3230 return NULL;
3231 isl_assert(set->ctx, type != isl_dim_in, goto error);
3232 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3233 error:
3234 isl_set_free(set);
3235 return NULL;
3238 __isl_give isl_basic_map *isl_basic_map_move_dims(
3239 __isl_take isl_basic_map *bmap,
3240 enum isl_dim_type dst_type, unsigned dst_pos,
3241 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3243 struct isl_dim_map *dim_map;
3244 struct isl_basic_map *res;
3245 enum isl_dim_type t;
3246 unsigned total, off;
3248 if (!bmap)
3249 return NULL;
3250 if (n == 0)
3251 return bmap;
3253 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3254 goto error);
3256 if (dst_type == src_type && dst_pos == src_pos)
3257 return bmap;
3259 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3261 if (pos(bmap->dim, dst_type) + dst_pos ==
3262 pos(bmap->dim, src_type) + src_pos +
3263 ((src_type < dst_type) ? n : 0)) {
3264 bmap = isl_basic_map_cow(bmap);
3265 if (!bmap)
3266 return NULL;
3268 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3269 src_type, src_pos, n);
3270 if (!bmap->dim)
3271 goto error;
3273 bmap = isl_basic_map_finalize(bmap);
3275 return bmap;
3278 total = isl_basic_map_total_dim(bmap);
3279 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3281 off = 0;
3282 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3283 unsigned size = isl_space_dim(bmap->dim, t);
3284 if (t == dst_type) {
3285 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3286 0, dst_pos, off);
3287 off += dst_pos;
3288 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3289 src_pos, n, off);
3290 off += n;
3291 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3292 dst_pos, size - dst_pos, off);
3293 off += size - dst_pos;
3294 } else if (t == src_type) {
3295 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3296 0, src_pos, off);
3297 off += src_pos;
3298 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3299 src_pos + n, size - src_pos - n, off);
3300 off += size - src_pos - n;
3301 } else {
3302 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3303 off += size;
3306 isl_dim_map_div(dim_map, bmap, off);
3308 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3309 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3310 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3311 if (!bmap)
3312 goto error;
3314 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3315 src_type, src_pos, n);
3316 if (!bmap->dim)
3317 goto error;
3319 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3320 bmap = isl_basic_map_gauss(bmap, NULL);
3321 bmap = isl_basic_map_finalize(bmap);
3323 return bmap;
3324 error:
3325 isl_basic_map_free(bmap);
3326 return NULL;
3329 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3330 enum isl_dim_type dst_type, unsigned dst_pos,
3331 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3333 return (isl_basic_set *)isl_basic_map_move_dims(
3334 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3337 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3338 enum isl_dim_type dst_type, unsigned dst_pos,
3339 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3341 if (!set)
3342 return NULL;
3343 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3344 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3345 src_type, src_pos, n);
3346 error:
3347 isl_set_free(set);
3348 return NULL;
3351 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3352 enum isl_dim_type dst_type, unsigned dst_pos,
3353 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3355 int i;
3357 if (!map)
3358 return NULL;
3359 if (n == 0)
3360 return map;
3362 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3363 goto error);
3365 if (dst_type == src_type && dst_pos == src_pos)
3366 return map;
3368 isl_assert(map->ctx, dst_type != src_type, goto error);
3370 map = isl_map_cow(map);
3371 if (!map)
3372 return NULL;
3374 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3375 if (!map->dim)
3376 goto error;
3378 for (i = 0; i < map->n; ++i) {
3379 map->p[i] = isl_basic_map_move_dims(map->p[i],
3380 dst_type, dst_pos,
3381 src_type, src_pos, n);
3382 if (!map->p[i])
3383 goto error;
3386 return map;
3387 error:
3388 isl_map_free(map);
3389 return NULL;
3392 /* Move the specified dimensions to the last columns right before
3393 * the divs. Don't change the dimension specification of bmap.
3394 * That's the responsibility of the caller.
3396 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3397 enum isl_dim_type type, unsigned first, unsigned n)
3399 struct isl_dim_map *dim_map;
3400 struct isl_basic_map *res;
3401 enum isl_dim_type t;
3402 unsigned total, off;
3404 if (!bmap)
3405 return NULL;
3406 if (pos(bmap->dim, type) + first + n ==
3407 1 + isl_space_dim(bmap->dim, isl_dim_all))
3408 return bmap;
3410 total = isl_basic_map_total_dim(bmap);
3411 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3413 off = 0;
3414 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3415 unsigned size = isl_space_dim(bmap->dim, t);
3416 if (t == type) {
3417 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3418 0, first, off);
3419 off += first;
3420 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3421 first, n, total - bmap->n_div - n);
3422 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3423 first + n, size - (first + n), off);
3424 off += size - (first + n);
3425 } else {
3426 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3427 off += size;
3430 isl_dim_map_div(dim_map, bmap, off + n);
3432 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3433 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3434 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3435 return res;
3438 /* Insert "n" rows in the divs of "bmap".
3440 * The number of columns is not changed, which means that the last
3441 * dimensions of "bmap" are being reintepreted as the new divs.
3442 * The space of "bmap" is not adjusted, however, which means
3443 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3444 * from the space of "bmap" is the responsibility of the caller.
3446 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3447 int n)
3449 int i;
3450 size_t row_size;
3451 isl_int **new_div;
3452 isl_int *old;
3454 bmap = isl_basic_map_cow(bmap);
3455 if (!bmap)
3456 return NULL;
3458 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3459 old = bmap->block2.data;
3460 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3461 (bmap->extra + n) * (1 + row_size));
3462 if (!bmap->block2.data)
3463 return isl_basic_map_free(bmap);
3464 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3465 if (!new_div)
3466 return isl_basic_map_free(bmap);
3467 for (i = 0; i < n; ++i) {
3468 new_div[i] = bmap->block2.data +
3469 (bmap->extra + i) * (1 + row_size);
3470 isl_seq_clr(new_div[i], 1 + row_size);
3472 for (i = 0; i < bmap->extra; ++i)
3473 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3474 free(bmap->div);
3475 bmap->div = new_div;
3476 bmap->n_div += n;
3477 bmap->extra += n;
3479 return bmap;
3482 /* Turn the n dimensions of type type, starting at first
3483 * into existentially quantified variables.
3485 __isl_give isl_basic_map *isl_basic_map_project_out(
3486 __isl_take isl_basic_map *bmap,
3487 enum isl_dim_type type, unsigned first, unsigned n)
3489 if (n == 0)
3490 return basic_map_space_reset(bmap, type);
3492 if (!bmap)
3493 return NULL;
3495 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3496 return isl_basic_map_remove_dims(bmap, type, first, n);
3498 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3499 goto error);
3501 bmap = move_last(bmap, type, first, n);
3502 bmap = isl_basic_map_cow(bmap);
3503 bmap = insert_div_rows(bmap, n);
3504 if (!bmap)
3505 return NULL;
3507 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3508 if (!bmap->dim)
3509 goto error;
3510 bmap = isl_basic_map_simplify(bmap);
3511 bmap = isl_basic_map_drop_redundant_divs(bmap);
3512 return isl_basic_map_finalize(bmap);
3513 error:
3514 isl_basic_map_free(bmap);
3515 return NULL;
3518 /* Turn the n dimensions of type type, starting at first
3519 * into existentially quantified variables.
3521 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3522 enum isl_dim_type type, unsigned first, unsigned n)
3524 return (isl_basic_set *)isl_basic_map_project_out(
3525 (isl_basic_map *)bset, type, first, n);
3528 /* Turn the n dimensions of type type, starting at first
3529 * into existentially quantified variables.
3531 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3532 enum isl_dim_type type, unsigned first, unsigned n)
3534 int i;
3536 if (!map)
3537 return NULL;
3539 if (n == 0)
3540 return map_space_reset(map, type);
3542 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3544 map = isl_map_cow(map);
3545 if (!map)
3546 return NULL;
3548 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3549 if (!map->dim)
3550 goto error;
3552 for (i = 0; i < map->n; ++i) {
3553 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3554 if (!map->p[i])
3555 goto error;
3558 return map;
3559 error:
3560 isl_map_free(map);
3561 return NULL;
3564 /* Turn the n dimensions of type type, starting at first
3565 * into existentially quantified variables.
3567 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3568 enum isl_dim_type type, unsigned first, unsigned n)
3570 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3573 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3575 int i, j;
3577 for (i = 0; i < n; ++i) {
3578 j = isl_basic_map_alloc_div(bmap);
3579 if (j < 0)
3580 goto error;
3581 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3583 return bmap;
3584 error:
3585 isl_basic_map_free(bmap);
3586 return NULL;
3589 struct isl_basic_map *isl_basic_map_apply_range(
3590 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3592 isl_space *dim_result = NULL;
3593 struct isl_basic_map *bmap;
3594 unsigned n_in, n_out, n, nparam, total, pos;
3595 struct isl_dim_map *dim_map1, *dim_map2;
3597 if (!bmap1 || !bmap2)
3598 goto error;
3599 if (!isl_space_match(bmap1->dim, isl_dim_param,
3600 bmap2->dim, isl_dim_param))
3601 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3602 "parameters don't match", goto error);
3603 if (!isl_space_tuple_match(bmap1->dim, isl_dim_out,
3604 bmap2->dim, isl_dim_in))
3605 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3606 "spaces don't match", goto error);
3608 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3609 isl_space_copy(bmap2->dim));
3611 n_in = isl_basic_map_n_in(bmap1);
3612 n_out = isl_basic_map_n_out(bmap2);
3613 n = isl_basic_map_n_out(bmap1);
3614 nparam = isl_basic_map_n_param(bmap1);
3616 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3617 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3618 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3619 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3620 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3621 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3622 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3623 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3624 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3625 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3626 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3628 bmap = isl_basic_map_alloc_space(dim_result,
3629 bmap1->n_div + bmap2->n_div + n,
3630 bmap1->n_eq + bmap2->n_eq,
3631 bmap1->n_ineq + bmap2->n_ineq);
3632 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3633 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3634 bmap = add_divs(bmap, n);
3635 bmap = isl_basic_map_simplify(bmap);
3636 bmap = isl_basic_map_drop_redundant_divs(bmap);
3637 return isl_basic_map_finalize(bmap);
3638 error:
3639 isl_basic_map_free(bmap1);
3640 isl_basic_map_free(bmap2);
3641 return NULL;
3644 struct isl_basic_set *isl_basic_set_apply(
3645 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3647 if (!bset || !bmap)
3648 goto error;
3650 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3651 goto error);
3653 return (struct isl_basic_set *)
3654 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3655 error:
3656 isl_basic_set_free(bset);
3657 isl_basic_map_free(bmap);
3658 return NULL;
3661 struct isl_basic_map *isl_basic_map_apply_domain(
3662 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3664 if (!bmap1 || !bmap2)
3665 goto error;
3667 isl_assert(bmap1->ctx,
3668 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3669 isl_assert(bmap1->ctx,
3670 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3671 goto error);
3673 bmap1 = isl_basic_map_reverse(bmap1);
3674 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3675 return isl_basic_map_reverse(bmap1);
3676 error:
3677 isl_basic_map_free(bmap1);
3678 isl_basic_map_free(bmap2);
3679 return NULL;
3682 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3683 * A \cap B -> f(A) + f(B)
3685 struct isl_basic_map *isl_basic_map_sum(
3686 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3688 unsigned n_in, n_out, nparam, total, pos;
3689 struct isl_basic_map *bmap = NULL;
3690 struct isl_dim_map *dim_map1, *dim_map2;
3691 int i;
3693 if (!bmap1 || !bmap2)
3694 goto error;
3696 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3697 goto error);
3699 nparam = isl_basic_map_n_param(bmap1);
3700 n_in = isl_basic_map_n_in(bmap1);
3701 n_out = isl_basic_map_n_out(bmap1);
3703 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3704 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3705 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3706 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3707 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3708 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3709 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3710 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3711 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3712 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3713 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3715 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3716 bmap1->n_div + bmap2->n_div + 2 * n_out,
3717 bmap1->n_eq + bmap2->n_eq + n_out,
3718 bmap1->n_ineq + bmap2->n_ineq);
3719 for (i = 0; i < n_out; ++i) {
3720 int j = isl_basic_map_alloc_equality(bmap);
3721 if (j < 0)
3722 goto error;
3723 isl_seq_clr(bmap->eq[j], 1+total);
3724 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3725 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3726 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3728 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3729 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3730 bmap = add_divs(bmap, 2 * n_out);
3732 bmap = isl_basic_map_simplify(bmap);
3733 return isl_basic_map_finalize(bmap);
3734 error:
3735 isl_basic_map_free(bmap);
3736 isl_basic_map_free(bmap1);
3737 isl_basic_map_free(bmap2);
3738 return NULL;
3741 /* Given two maps A -> f(A) and B -> g(B), construct a map
3742 * A \cap B -> f(A) + f(B)
3744 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3746 struct isl_map *result;
3747 int i, j;
3749 if (!map1 || !map2)
3750 goto error;
3752 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3754 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3755 map1->n * map2->n, 0);
3756 if (!result)
3757 goto error;
3758 for (i = 0; i < map1->n; ++i)
3759 for (j = 0; j < map2->n; ++j) {
3760 struct isl_basic_map *part;
3761 part = isl_basic_map_sum(
3762 isl_basic_map_copy(map1->p[i]),
3763 isl_basic_map_copy(map2->p[j]));
3764 if (isl_basic_map_is_empty(part))
3765 isl_basic_map_free(part);
3766 else
3767 result = isl_map_add_basic_map(result, part);
3768 if (!result)
3769 goto error;
3771 isl_map_free(map1);
3772 isl_map_free(map2);
3773 return result;
3774 error:
3775 isl_map_free(map1);
3776 isl_map_free(map2);
3777 return NULL;
3780 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3781 __isl_take isl_set *set2)
3783 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3786 /* Given a basic map A -> f(A), construct A -> -f(A).
3788 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3790 int i, j;
3791 unsigned off, n;
3793 bmap = isl_basic_map_cow(bmap);
3794 if (!bmap)
3795 return NULL;
3797 n = isl_basic_map_dim(bmap, isl_dim_out);
3798 off = isl_basic_map_offset(bmap, isl_dim_out);
3799 for (i = 0; i < bmap->n_eq; ++i)
3800 for (j = 0; j < n; ++j)
3801 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3802 for (i = 0; i < bmap->n_ineq; ++i)
3803 for (j = 0; j < n; ++j)
3804 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3805 for (i = 0; i < bmap->n_div; ++i)
3806 for (j = 0; j < n; ++j)
3807 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3808 bmap = isl_basic_map_gauss(bmap, NULL);
3809 return isl_basic_map_finalize(bmap);
3812 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3814 return isl_basic_map_neg(bset);
3817 /* Given a map A -> f(A), construct A -> -f(A).
3819 struct isl_map *isl_map_neg(struct isl_map *map)
3821 int i;
3823 map = isl_map_cow(map);
3824 if (!map)
3825 return NULL;
3827 for (i = 0; i < map->n; ++i) {
3828 map->p[i] = isl_basic_map_neg(map->p[i]);
3829 if (!map->p[i])
3830 goto error;
3833 return map;
3834 error:
3835 isl_map_free(map);
3836 return NULL;
3839 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3841 return (isl_set *)isl_map_neg((isl_map *)set);
3844 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3845 * A -> floor(f(A)/d).
3847 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3848 isl_int d)
3850 unsigned n_in, n_out, nparam, total, pos;
3851 struct isl_basic_map *result = NULL;
3852 struct isl_dim_map *dim_map;
3853 int i;
3855 if (!bmap)
3856 return NULL;
3858 nparam = isl_basic_map_n_param(bmap);
3859 n_in = isl_basic_map_n_in(bmap);
3860 n_out = isl_basic_map_n_out(bmap);
3862 total = nparam + n_in + n_out + bmap->n_div + n_out;
3863 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3864 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3865 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3866 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3867 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3869 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3870 bmap->n_div + n_out,
3871 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3872 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3873 result = add_divs(result, n_out);
3874 for (i = 0; i < n_out; ++i) {
3875 int j;
3876 j = isl_basic_map_alloc_inequality(result);
3877 if (j < 0)
3878 goto error;
3879 isl_seq_clr(result->ineq[j], 1+total);
3880 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3881 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3882 j = isl_basic_map_alloc_inequality(result);
3883 if (j < 0)
3884 goto error;
3885 isl_seq_clr(result->ineq[j], 1+total);
3886 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3887 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3888 isl_int_sub_ui(result->ineq[j][0], d, 1);
3891 result = isl_basic_map_simplify(result);
3892 return isl_basic_map_finalize(result);
3893 error:
3894 isl_basic_map_free(result);
3895 return NULL;
3898 /* Given a map A -> f(A) and an integer d, construct a map
3899 * A -> floor(f(A)/d).
3901 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3903 int i;
3905 map = isl_map_cow(map);
3906 if (!map)
3907 return NULL;
3909 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3910 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3911 for (i = 0; i < map->n; ++i) {
3912 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3913 if (!map->p[i])
3914 goto error;
3917 return map;
3918 error:
3919 isl_map_free(map);
3920 return NULL;
3923 /* Given a map A -> f(A) and an integer d, construct a map
3924 * A -> floor(f(A)/d).
3926 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3927 __isl_take isl_val *d)
3929 if (!map || !d)
3930 goto error;
3931 if (!isl_val_is_int(d))
3932 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3933 "expecting integer denominator", goto error);
3934 map = isl_map_floordiv(map, d->n);
3935 isl_val_free(d);
3936 return map;
3937 error:
3938 isl_map_free(map);
3939 isl_val_free(d);
3940 return NULL;
3943 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3945 int i;
3946 unsigned nparam;
3947 unsigned n_in;
3949 i = isl_basic_map_alloc_equality(bmap);
3950 if (i < 0)
3951 goto error;
3952 nparam = isl_basic_map_n_param(bmap);
3953 n_in = isl_basic_map_n_in(bmap);
3954 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3955 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3956 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3957 return isl_basic_map_finalize(bmap);
3958 error:
3959 isl_basic_map_free(bmap);
3960 return NULL;
3963 /* Add a constraints to "bmap" expressing i_pos < o_pos
3965 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3967 int i;
3968 unsigned nparam;
3969 unsigned n_in;
3971 i = isl_basic_map_alloc_inequality(bmap);
3972 if (i < 0)
3973 goto error;
3974 nparam = isl_basic_map_n_param(bmap);
3975 n_in = isl_basic_map_n_in(bmap);
3976 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3977 isl_int_set_si(bmap->ineq[i][0], -1);
3978 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3979 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3980 return isl_basic_map_finalize(bmap);
3981 error:
3982 isl_basic_map_free(bmap);
3983 return NULL;
3986 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3988 static __isl_give isl_basic_map *var_less_or_equal(
3989 __isl_take isl_basic_map *bmap, unsigned pos)
3991 int i;
3992 unsigned nparam;
3993 unsigned n_in;
3995 i = isl_basic_map_alloc_inequality(bmap);
3996 if (i < 0)
3997 goto error;
3998 nparam = isl_basic_map_n_param(bmap);
3999 n_in = isl_basic_map_n_in(bmap);
4000 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4001 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4002 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4003 return isl_basic_map_finalize(bmap);
4004 error:
4005 isl_basic_map_free(bmap);
4006 return NULL;
4009 /* Add a constraints to "bmap" expressing i_pos > o_pos
4011 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4013 int i;
4014 unsigned nparam;
4015 unsigned n_in;
4017 i = isl_basic_map_alloc_inequality(bmap);
4018 if (i < 0)
4019 goto error;
4020 nparam = isl_basic_map_n_param(bmap);
4021 n_in = isl_basic_map_n_in(bmap);
4022 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4023 isl_int_set_si(bmap->ineq[i][0], -1);
4024 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4025 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4026 return isl_basic_map_finalize(bmap);
4027 error:
4028 isl_basic_map_free(bmap);
4029 return NULL;
4032 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4034 static __isl_give isl_basic_map *var_more_or_equal(
4035 __isl_take isl_basic_map *bmap, unsigned pos)
4037 int i;
4038 unsigned nparam;
4039 unsigned n_in;
4041 i = isl_basic_map_alloc_inequality(bmap);
4042 if (i < 0)
4043 goto error;
4044 nparam = isl_basic_map_n_param(bmap);
4045 n_in = isl_basic_map_n_in(bmap);
4046 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4047 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4048 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4049 return isl_basic_map_finalize(bmap);
4050 error:
4051 isl_basic_map_free(bmap);
4052 return NULL;
4055 __isl_give isl_basic_map *isl_basic_map_equal(
4056 __isl_take isl_space *dim, unsigned n_equal)
4058 int i;
4059 struct isl_basic_map *bmap;
4060 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4061 if (!bmap)
4062 return NULL;
4063 for (i = 0; i < n_equal && bmap; ++i)
4064 bmap = var_equal(bmap, i);
4065 return isl_basic_map_finalize(bmap);
4068 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4070 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4071 unsigned pos)
4073 int i;
4074 struct isl_basic_map *bmap;
4075 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4076 if (!bmap)
4077 return NULL;
4078 for (i = 0; i < pos && bmap; ++i)
4079 bmap = var_equal(bmap, i);
4080 if (bmap)
4081 bmap = var_less(bmap, pos);
4082 return isl_basic_map_finalize(bmap);
4085 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4087 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4088 __isl_take isl_space *dim, unsigned pos)
4090 int i;
4091 isl_basic_map *bmap;
4093 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4094 for (i = 0; i < pos; ++i)
4095 bmap = var_equal(bmap, i);
4096 bmap = var_less_or_equal(bmap, pos);
4097 return isl_basic_map_finalize(bmap);
4100 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4102 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4103 unsigned pos)
4105 int i;
4106 struct isl_basic_map *bmap;
4107 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4108 if (!bmap)
4109 return NULL;
4110 for (i = 0; i < pos && bmap; ++i)
4111 bmap = var_equal(bmap, i);
4112 if (bmap)
4113 bmap = var_more(bmap, pos);
4114 return isl_basic_map_finalize(bmap);
4117 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4119 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4120 __isl_take isl_space *dim, unsigned pos)
4122 int i;
4123 isl_basic_map *bmap;
4125 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4126 for (i = 0; i < pos; ++i)
4127 bmap = var_equal(bmap, i);
4128 bmap = var_more_or_equal(bmap, pos);
4129 return isl_basic_map_finalize(bmap);
4132 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4133 unsigned n, int equal)
4135 struct isl_map *map;
4136 int i;
4138 if (n == 0 && equal)
4139 return isl_map_universe(dims);
4141 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4143 for (i = 0; i + 1 < n; ++i)
4144 map = isl_map_add_basic_map(map,
4145 isl_basic_map_less_at(isl_space_copy(dims), i));
4146 if (n > 0) {
4147 if (equal)
4148 map = isl_map_add_basic_map(map,
4149 isl_basic_map_less_or_equal_at(dims, n - 1));
4150 else
4151 map = isl_map_add_basic_map(map,
4152 isl_basic_map_less_at(dims, n - 1));
4153 } else
4154 isl_space_free(dims);
4156 return map;
4159 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4161 if (!dims)
4162 return NULL;
4163 return map_lex_lte_first(dims, dims->n_out, equal);
4166 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4168 return map_lex_lte_first(dim, n, 0);
4171 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4173 return map_lex_lte_first(dim, n, 1);
4176 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4178 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4181 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4183 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4186 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4187 unsigned n, int equal)
4189 struct isl_map *map;
4190 int i;
4192 if (n == 0 && equal)
4193 return isl_map_universe(dims);
4195 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4197 for (i = 0; i + 1 < n; ++i)
4198 map = isl_map_add_basic_map(map,
4199 isl_basic_map_more_at(isl_space_copy(dims), i));
4200 if (n > 0) {
4201 if (equal)
4202 map = isl_map_add_basic_map(map,
4203 isl_basic_map_more_or_equal_at(dims, n - 1));
4204 else
4205 map = isl_map_add_basic_map(map,
4206 isl_basic_map_more_at(dims, n - 1));
4207 } else
4208 isl_space_free(dims);
4210 return map;
4213 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4215 if (!dims)
4216 return NULL;
4217 return map_lex_gte_first(dims, dims->n_out, equal);
4220 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4222 return map_lex_gte_first(dim, n, 0);
4225 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4227 return map_lex_gte_first(dim, n, 1);
4230 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4232 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4235 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4237 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4240 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4241 __isl_take isl_set *set2)
4243 isl_map *map;
4244 map = isl_map_lex_le(isl_set_get_space(set1));
4245 map = isl_map_intersect_domain(map, set1);
4246 map = isl_map_intersect_range(map, set2);
4247 return map;
4250 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4251 __isl_take isl_set *set2)
4253 isl_map *map;
4254 map = isl_map_lex_lt(isl_set_get_space(set1));
4255 map = isl_map_intersect_domain(map, set1);
4256 map = isl_map_intersect_range(map, set2);
4257 return map;
4260 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4261 __isl_take isl_set *set2)
4263 isl_map *map;
4264 map = isl_map_lex_ge(isl_set_get_space(set1));
4265 map = isl_map_intersect_domain(map, set1);
4266 map = isl_map_intersect_range(map, set2);
4267 return map;
4270 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4271 __isl_take isl_set *set2)
4273 isl_map *map;
4274 map = isl_map_lex_gt(isl_set_get_space(set1));
4275 map = isl_map_intersect_domain(map, set1);
4276 map = isl_map_intersect_range(map, set2);
4277 return map;
4280 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4281 __isl_take isl_map *map2)
4283 isl_map *map;
4284 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4285 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4286 map = isl_map_apply_range(map, isl_map_reverse(map2));
4287 return map;
4290 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4291 __isl_take isl_map *map2)
4293 isl_map *map;
4294 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4295 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4296 map = isl_map_apply_range(map, isl_map_reverse(map2));
4297 return map;
4300 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4301 __isl_take isl_map *map2)
4303 isl_map *map;
4304 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4305 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4306 map = isl_map_apply_range(map, isl_map_reverse(map2));
4307 return map;
4310 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4311 __isl_take isl_map *map2)
4313 isl_map *map;
4314 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4315 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4316 map = isl_map_apply_range(map, isl_map_reverse(map2));
4317 return map;
4320 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4321 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4323 struct isl_basic_map *bmap;
4325 bset = isl_basic_set_cow(bset);
4326 if (!bset || !dim)
4327 goto error;
4329 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4330 isl_space_free(bset->dim);
4331 bmap = (struct isl_basic_map *) bset;
4332 bmap->dim = dim;
4333 return isl_basic_map_finalize(bmap);
4334 error:
4335 isl_basic_set_free(bset);
4336 isl_space_free(dim);
4337 return NULL;
4340 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4342 if (!bmap)
4343 goto error;
4344 if (bmap->dim->n_in == 0)
4345 return (struct isl_basic_set *)bmap;
4346 bmap = isl_basic_map_cow(bmap);
4347 if (!bmap)
4348 goto error;
4349 bmap->dim = isl_space_as_set_space(bmap->dim);
4350 if (!bmap->dim)
4351 goto error;
4352 bmap = isl_basic_map_finalize(bmap);
4353 return (struct isl_basic_set *)bmap;
4354 error:
4355 isl_basic_map_free(bmap);
4356 return NULL;
4359 /* For a div d = floor(f/m), add the constraint
4361 * f - m d >= 0
4363 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4364 unsigned pos, isl_int *div)
4366 int i;
4367 unsigned total = isl_basic_map_total_dim(bmap);
4369 i = isl_basic_map_alloc_inequality(bmap);
4370 if (i < 0)
4371 return -1;
4372 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4373 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4375 return 0;
4378 /* For a div d = floor(f/m), add the constraint
4380 * -(f-(n-1)) + m d >= 0
4382 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4383 unsigned pos, isl_int *div)
4385 int i;
4386 unsigned total = isl_basic_map_total_dim(bmap);
4388 i = isl_basic_map_alloc_inequality(bmap);
4389 if (i < 0)
4390 return -1;
4391 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4392 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4393 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4394 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4396 return 0;
4399 /* For a div d = floor(f/m), add the constraints
4401 * f - m d >= 0
4402 * -(f-(n-1)) + m d >= 0
4404 * Note that the second constraint is the negation of
4406 * f - m d >= n
4408 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4409 unsigned pos, isl_int *div)
4411 if (add_upper_div_constraint(bmap, pos, div) < 0)
4412 return -1;
4413 if (add_lower_div_constraint(bmap, pos, div) < 0)
4414 return -1;
4415 return 0;
4418 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4419 unsigned pos, isl_int *div)
4421 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4422 pos, div);
4425 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4427 unsigned total = isl_basic_map_total_dim(bmap);
4428 unsigned div_pos = total - bmap->n_div + div;
4430 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4431 bmap->div[div]);
4434 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4436 * In particular, if this div is of the form d = floor(f/m),
4437 * then add the constraint
4439 * f - m d >= 0
4441 * if sign < 0 or the constraint
4443 * -(f-(n-1)) + m d >= 0
4445 * if sign > 0.
4447 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4448 unsigned div, int sign)
4450 unsigned total;
4451 unsigned div_pos;
4453 if (!bmap)
4454 return -1;
4456 total = isl_basic_map_total_dim(bmap);
4457 div_pos = total - bmap->n_div + div;
4459 if (sign < 0)
4460 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4461 else
4462 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4465 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4467 return isl_basic_map_add_div_constraints(bset, div);
4470 struct isl_basic_set *isl_basic_map_underlying_set(
4471 struct isl_basic_map *bmap)
4473 if (!bmap)
4474 goto error;
4475 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4476 bmap->n_div == 0 &&
4477 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4478 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4479 return (struct isl_basic_set *)bmap;
4480 bmap = isl_basic_map_cow(bmap);
4481 if (!bmap)
4482 goto error;
4483 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4484 if (!bmap->dim)
4485 goto error;
4486 bmap->extra -= bmap->n_div;
4487 bmap->n_div = 0;
4488 bmap = isl_basic_map_finalize(bmap);
4489 return (struct isl_basic_set *)bmap;
4490 error:
4491 isl_basic_map_free(bmap);
4492 return NULL;
4495 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4496 __isl_take isl_basic_set *bset)
4498 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4501 struct isl_basic_map *isl_basic_map_overlying_set(
4502 struct isl_basic_set *bset, struct isl_basic_map *like)
4504 struct isl_basic_map *bmap;
4505 struct isl_ctx *ctx;
4506 unsigned total;
4507 int i;
4509 if (!bset || !like)
4510 goto error;
4511 ctx = bset->ctx;
4512 isl_assert(ctx, bset->n_div == 0, goto error);
4513 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4514 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4515 goto error);
4516 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4517 isl_basic_map_free(like);
4518 return (struct isl_basic_map *)bset;
4520 bset = isl_basic_set_cow(bset);
4521 if (!bset)
4522 goto error;
4523 total = bset->dim->n_out + bset->extra;
4524 bmap = (struct isl_basic_map *)bset;
4525 isl_space_free(bmap->dim);
4526 bmap->dim = isl_space_copy(like->dim);
4527 if (!bmap->dim)
4528 goto error;
4529 bmap->n_div = like->n_div;
4530 bmap->extra += like->n_div;
4531 if (bmap->extra) {
4532 unsigned ltotal;
4533 isl_int **div;
4534 ltotal = total - bmap->extra + like->extra;
4535 if (ltotal > total)
4536 ltotal = total;
4537 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4538 bmap->extra * (1 + 1 + total));
4539 if (isl_blk_is_error(bmap->block2))
4540 goto error;
4541 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4542 if (!div)
4543 goto error;
4544 bmap->div = div;
4545 for (i = 0; i < bmap->extra; ++i)
4546 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4547 for (i = 0; i < like->n_div; ++i) {
4548 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4549 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4551 bmap = isl_basic_map_extend_constraints(bmap,
4552 0, 2 * like->n_div);
4553 for (i = 0; i < like->n_div; ++i) {
4554 if (!bmap)
4555 break;
4556 if (isl_int_is_zero(bmap->div[i][0]))
4557 continue;
4558 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4559 bmap = isl_basic_map_free(bmap);
4562 isl_basic_map_free(like);
4563 bmap = isl_basic_map_simplify(bmap);
4564 bmap = isl_basic_map_finalize(bmap);
4565 return bmap;
4566 error:
4567 isl_basic_map_free(like);
4568 isl_basic_set_free(bset);
4569 return NULL;
4572 struct isl_basic_set *isl_basic_set_from_underlying_set(
4573 struct isl_basic_set *bset, struct isl_basic_set *like)
4575 return (struct isl_basic_set *)
4576 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4579 struct isl_set *isl_set_from_underlying_set(
4580 struct isl_set *set, struct isl_basic_set *like)
4582 int i;
4584 if (!set || !like)
4585 goto error;
4586 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4587 goto error);
4588 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4589 isl_basic_set_free(like);
4590 return set;
4592 set = isl_set_cow(set);
4593 if (!set)
4594 goto error;
4595 for (i = 0; i < set->n; ++i) {
4596 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4597 isl_basic_set_copy(like));
4598 if (!set->p[i])
4599 goto error;
4601 isl_space_free(set->dim);
4602 set->dim = isl_space_copy(like->dim);
4603 if (!set->dim)
4604 goto error;
4605 isl_basic_set_free(like);
4606 return set;
4607 error:
4608 isl_basic_set_free(like);
4609 isl_set_free(set);
4610 return NULL;
4613 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4615 int i;
4617 map = isl_map_cow(map);
4618 if (!map)
4619 return NULL;
4620 map->dim = isl_space_cow(map->dim);
4621 if (!map->dim)
4622 goto error;
4624 for (i = 1; i < map->n; ++i)
4625 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4626 goto error);
4627 for (i = 0; i < map->n; ++i) {
4628 map->p[i] = (struct isl_basic_map *)
4629 isl_basic_map_underlying_set(map->p[i]);
4630 if (!map->p[i])
4631 goto error;
4633 if (map->n == 0)
4634 map->dim = isl_space_underlying(map->dim, 0);
4635 else {
4636 isl_space_free(map->dim);
4637 map->dim = isl_space_copy(map->p[0]->dim);
4639 if (!map->dim)
4640 goto error;
4641 return (struct isl_set *)map;
4642 error:
4643 isl_map_free(map);
4644 return NULL;
4647 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4649 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4652 __isl_give isl_basic_map *isl_basic_map_reset_space(
4653 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4655 bmap = isl_basic_map_cow(bmap);
4656 if (!bmap || !dim)
4657 goto error;
4659 isl_space_free(bmap->dim);
4660 bmap->dim = dim;
4662 bmap = isl_basic_map_finalize(bmap);
4664 return bmap;
4665 error:
4666 isl_basic_map_free(bmap);
4667 isl_space_free(dim);
4668 return NULL;
4671 __isl_give isl_basic_set *isl_basic_set_reset_space(
4672 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4674 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4675 dim);
4678 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4679 __isl_take isl_space *dim)
4681 int i;
4683 map = isl_map_cow(map);
4684 if (!map || !dim)
4685 goto error;
4687 for (i = 0; i < map->n; ++i) {
4688 map->p[i] = isl_basic_map_reset_space(map->p[i],
4689 isl_space_copy(dim));
4690 if (!map->p[i])
4691 goto error;
4693 isl_space_free(map->dim);
4694 map->dim = dim;
4696 return map;
4697 error:
4698 isl_map_free(map);
4699 isl_space_free(dim);
4700 return NULL;
4703 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4704 __isl_take isl_space *dim)
4706 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4709 /* Compute the parameter domain of the given basic set.
4711 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4713 isl_space *space;
4714 unsigned n;
4716 if (isl_basic_set_is_params(bset))
4717 return bset;
4719 n = isl_basic_set_dim(bset, isl_dim_set);
4720 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4721 space = isl_basic_set_get_space(bset);
4722 space = isl_space_params(space);
4723 bset = isl_basic_set_reset_space(bset, space);
4724 return bset;
4727 /* Construct a zero-dimensional basic set with the given parameter domain.
4729 __isl_give isl_basic_set *isl_basic_set_from_params(
4730 __isl_take isl_basic_set *bset)
4732 isl_space *space;
4733 space = isl_basic_set_get_space(bset);
4734 space = isl_space_set_from_params(space);
4735 bset = isl_basic_set_reset_space(bset, space);
4736 return bset;
4739 /* Compute the parameter domain of the given set.
4741 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4743 isl_space *space;
4744 unsigned n;
4746 if (isl_set_is_params(set))
4747 return set;
4749 n = isl_set_dim(set, isl_dim_set);
4750 set = isl_set_project_out(set, isl_dim_set, 0, n);
4751 space = isl_set_get_space(set);
4752 space = isl_space_params(space);
4753 set = isl_set_reset_space(set, space);
4754 return set;
4757 /* Construct a zero-dimensional set with the given parameter domain.
4759 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4761 isl_space *space;
4762 space = isl_set_get_space(set);
4763 space = isl_space_set_from_params(space);
4764 set = isl_set_reset_space(set, space);
4765 return set;
4768 /* Compute the parameter domain of the given map.
4770 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4772 isl_space *space;
4773 unsigned n;
4775 n = isl_map_dim(map, isl_dim_in);
4776 map = isl_map_project_out(map, isl_dim_in, 0, n);
4777 n = isl_map_dim(map, isl_dim_out);
4778 map = isl_map_project_out(map, isl_dim_out, 0, n);
4779 space = isl_map_get_space(map);
4780 space = isl_space_params(space);
4781 map = isl_map_reset_space(map, space);
4782 return map;
4785 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4787 isl_space *dim;
4788 struct isl_basic_set *domain;
4789 unsigned n_in;
4790 unsigned n_out;
4792 if (!bmap)
4793 return NULL;
4794 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4796 n_in = isl_basic_map_n_in(bmap);
4797 n_out = isl_basic_map_n_out(bmap);
4798 domain = isl_basic_set_from_basic_map(bmap);
4799 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4801 domain = isl_basic_set_reset_space(domain, dim);
4803 return domain;
4806 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4808 if (!bmap)
4809 return -1;
4810 return isl_space_may_be_set(bmap->dim);
4813 /* Is this basic map actually a set?
4814 * Users should never call this function. Outside of isl,
4815 * the type should indicate whether something is a set or a map.
4817 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4819 if (!bmap)
4820 return -1;
4821 return isl_space_is_set(bmap->dim);
4824 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4826 if (!bmap)
4827 return NULL;
4828 if (isl_basic_map_is_set(bmap))
4829 return bmap;
4830 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4833 __isl_give isl_basic_map *isl_basic_map_domain_map(
4834 __isl_take isl_basic_map *bmap)
4836 int i, k;
4837 isl_space *dim;
4838 isl_basic_map *domain;
4839 int nparam, n_in, n_out;
4840 unsigned total;
4842 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4843 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4844 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4846 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4847 domain = isl_basic_map_universe(dim);
4849 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4850 bmap = isl_basic_map_apply_range(bmap, domain);
4851 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4853 total = isl_basic_map_total_dim(bmap);
4855 for (i = 0; i < n_in; ++i) {
4856 k = isl_basic_map_alloc_equality(bmap);
4857 if (k < 0)
4858 goto error;
4859 isl_seq_clr(bmap->eq[k], 1 + total);
4860 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4861 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4864 bmap = isl_basic_map_gauss(bmap, NULL);
4865 return isl_basic_map_finalize(bmap);
4866 error:
4867 isl_basic_map_free(bmap);
4868 return NULL;
4871 __isl_give isl_basic_map *isl_basic_map_range_map(
4872 __isl_take isl_basic_map *bmap)
4874 int i, k;
4875 isl_space *dim;
4876 isl_basic_map *range;
4877 int nparam, n_in, n_out;
4878 unsigned total;
4880 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4881 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4882 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4884 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4885 range = isl_basic_map_universe(dim);
4887 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4888 bmap = isl_basic_map_apply_range(bmap, range);
4889 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4891 total = isl_basic_map_total_dim(bmap);
4893 for (i = 0; i < n_out; ++i) {
4894 k = isl_basic_map_alloc_equality(bmap);
4895 if (k < 0)
4896 goto error;
4897 isl_seq_clr(bmap->eq[k], 1 + total);
4898 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4899 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4902 bmap = isl_basic_map_gauss(bmap, NULL);
4903 return isl_basic_map_finalize(bmap);
4904 error:
4905 isl_basic_map_free(bmap);
4906 return NULL;
4909 int isl_map_may_be_set(__isl_keep isl_map *map)
4911 if (!map)
4912 return -1;
4913 return isl_space_may_be_set(map->dim);
4916 /* Is this map actually a set?
4917 * Users should never call this function. Outside of isl,
4918 * the type should indicate whether something is a set or a map.
4920 int isl_map_is_set(__isl_keep isl_map *map)
4922 if (!map)
4923 return -1;
4924 return isl_space_is_set(map->dim);
4927 struct isl_set *isl_map_range(struct isl_map *map)
4929 int i;
4930 struct isl_set *set;
4932 if (!map)
4933 goto error;
4934 if (isl_map_is_set(map))
4935 return (isl_set *)map;
4937 map = isl_map_cow(map);
4938 if (!map)
4939 goto error;
4941 set = (struct isl_set *) map;
4942 set->dim = isl_space_range(set->dim);
4943 if (!set->dim)
4944 goto error;
4945 for (i = 0; i < map->n; ++i) {
4946 set->p[i] = isl_basic_map_range(map->p[i]);
4947 if (!set->p[i])
4948 goto error;
4950 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4951 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4952 return set;
4953 error:
4954 isl_map_free(map);
4955 return NULL;
4958 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4960 int i;
4962 map = isl_map_cow(map);
4963 if (!map)
4964 return NULL;
4966 map->dim = isl_space_domain_map(map->dim);
4967 if (!map->dim)
4968 goto error;
4969 for (i = 0; i < map->n; ++i) {
4970 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4971 if (!map->p[i])
4972 goto error;
4974 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4975 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4976 return map;
4977 error:
4978 isl_map_free(map);
4979 return NULL;
4982 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4984 int i;
4985 isl_space *range_dim;
4987 map = isl_map_cow(map);
4988 if (!map)
4989 return NULL;
4991 range_dim = isl_space_range(isl_map_get_space(map));
4992 range_dim = isl_space_from_range(range_dim);
4993 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4994 map->dim = isl_space_join(map->dim, range_dim);
4995 if (!map->dim)
4996 goto error;
4997 for (i = 0; i < map->n; ++i) {
4998 map->p[i] = isl_basic_map_range_map(map->p[i]);
4999 if (!map->p[i])
5000 goto error;
5002 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5003 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5004 return map;
5005 error:
5006 isl_map_free(map);
5007 return NULL;
5010 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5011 __isl_take isl_space *dim)
5013 int i;
5014 struct isl_map *map = NULL;
5016 set = isl_set_cow(set);
5017 if (!set || !dim)
5018 goto error;
5019 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5020 map = (struct isl_map *)set;
5021 for (i = 0; i < set->n; ++i) {
5022 map->p[i] = isl_basic_map_from_basic_set(
5023 set->p[i], isl_space_copy(dim));
5024 if (!map->p[i])
5025 goto error;
5027 isl_space_free(map->dim);
5028 map->dim = dim;
5029 return map;
5030 error:
5031 isl_space_free(dim);
5032 isl_set_free(set);
5033 return NULL;
5036 __isl_give isl_basic_map *isl_basic_map_from_domain(
5037 __isl_take isl_basic_set *bset)
5039 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5042 __isl_give isl_basic_map *isl_basic_map_from_range(
5043 __isl_take isl_basic_set *bset)
5045 isl_space *space;
5046 space = isl_basic_set_get_space(bset);
5047 space = isl_space_from_range(space);
5048 bset = isl_basic_set_reset_space(bset, space);
5049 return (isl_basic_map *)bset;
5052 /* Create a relation with the given set as range.
5053 * The domain of the created relation is a zero-dimensional
5054 * flat anonymous space.
5056 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5058 isl_space *space;
5059 space = isl_set_get_space(set);
5060 space = isl_space_from_range(space);
5061 set = isl_set_reset_space(set, space);
5062 return (struct isl_map *)set;
5065 /* Create a relation with the given set as domain.
5066 * The range of the created relation is a zero-dimensional
5067 * flat anonymous space.
5069 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5071 return isl_map_reverse(isl_map_from_range(set));
5074 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5075 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5077 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5080 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5081 __isl_take isl_set *range)
5083 return isl_map_apply_range(isl_map_reverse(domain), range);
5086 struct isl_set *isl_set_from_map(struct isl_map *map)
5088 int i;
5089 struct isl_set *set = NULL;
5091 if (!map)
5092 return NULL;
5093 map = isl_map_cow(map);
5094 if (!map)
5095 return NULL;
5096 map->dim = isl_space_as_set_space(map->dim);
5097 if (!map->dim)
5098 goto error;
5099 set = (struct isl_set *)map;
5100 for (i = 0; i < map->n; ++i) {
5101 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
5102 if (!set->p[i])
5103 goto error;
5105 return set;
5106 error:
5107 isl_map_free(map);
5108 return NULL;
5111 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5112 unsigned flags)
5114 struct isl_map *map;
5116 if (!dim)
5117 return NULL;
5118 if (n < 0)
5119 isl_die(dim->ctx, isl_error_internal,
5120 "negative number of basic maps", goto error);
5121 map = isl_alloc(dim->ctx, struct isl_map,
5122 sizeof(struct isl_map) +
5123 (n - 1) * sizeof(struct isl_basic_map *));
5124 if (!map)
5125 goto error;
5127 map->ctx = dim->ctx;
5128 isl_ctx_ref(map->ctx);
5129 map->ref = 1;
5130 map->size = n;
5131 map->n = 0;
5132 map->dim = dim;
5133 map->flags = flags;
5134 return map;
5135 error:
5136 isl_space_free(dim);
5137 return NULL;
5140 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5141 unsigned nparam, unsigned in, unsigned out, int n,
5142 unsigned flags)
5144 struct isl_map *map;
5145 isl_space *dims;
5147 dims = isl_space_alloc(ctx, nparam, in, out);
5148 if (!dims)
5149 return NULL;
5151 map = isl_map_alloc_space(dims, n, flags);
5152 return map;
5155 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5157 struct isl_basic_map *bmap;
5158 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5159 bmap = isl_basic_map_set_to_empty(bmap);
5160 return bmap;
5163 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5165 struct isl_basic_set *bset;
5166 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5167 bset = isl_basic_set_set_to_empty(bset);
5168 return bset;
5171 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5173 struct isl_basic_map *bmap;
5174 if (!model)
5175 return NULL;
5176 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5177 bmap = isl_basic_map_set_to_empty(bmap);
5178 return bmap;
5181 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5183 struct isl_basic_map *bmap;
5184 if (!model)
5185 return NULL;
5186 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5187 bmap = isl_basic_map_set_to_empty(bmap);
5188 return bmap;
5191 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5193 struct isl_basic_set *bset;
5194 if (!model)
5195 return NULL;
5196 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5197 bset = isl_basic_set_set_to_empty(bset);
5198 return bset;
5201 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5203 struct isl_basic_map *bmap;
5204 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5205 bmap = isl_basic_map_finalize(bmap);
5206 return bmap;
5209 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5211 struct isl_basic_set *bset;
5212 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5213 bset = isl_basic_set_finalize(bset);
5214 return bset;
5217 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5219 int i;
5220 unsigned total = isl_space_dim(dim, isl_dim_all);
5221 isl_basic_map *bmap;
5223 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5224 for (i = 0; i < total; ++i) {
5225 int k = isl_basic_map_alloc_inequality(bmap);
5226 if (k < 0)
5227 goto error;
5228 isl_seq_clr(bmap->ineq[k], 1 + total);
5229 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5231 return bmap;
5232 error:
5233 isl_basic_map_free(bmap);
5234 return NULL;
5237 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5239 return isl_basic_map_nat_universe(dim);
5242 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5244 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5247 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5249 return isl_map_nat_universe(dim);
5252 __isl_give isl_basic_map *isl_basic_map_universe_like(
5253 __isl_keep isl_basic_map *model)
5255 if (!model)
5256 return NULL;
5257 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5260 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5262 if (!model)
5263 return NULL;
5264 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5267 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5268 __isl_keep isl_set *model)
5270 if (!model)
5271 return NULL;
5272 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5275 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5277 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5280 struct isl_map *isl_map_empty_like(struct isl_map *model)
5282 if (!model)
5283 return NULL;
5284 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5287 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5289 if (!model)
5290 return NULL;
5291 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5294 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5296 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5299 struct isl_set *isl_set_empty_like(struct isl_set *model)
5301 if (!model)
5302 return NULL;
5303 return isl_set_empty(isl_space_copy(model->dim));
5306 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5308 struct isl_map *map;
5309 if (!dim)
5310 return NULL;
5311 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5312 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5313 return map;
5316 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5318 struct isl_set *set;
5319 if (!dim)
5320 return NULL;
5321 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5322 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5323 return set;
5326 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5328 if (!model)
5329 return NULL;
5330 return isl_set_universe(isl_space_copy(model->dim));
5333 struct isl_map *isl_map_dup(struct isl_map *map)
5335 int i;
5336 struct isl_map *dup;
5338 if (!map)
5339 return NULL;
5340 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5341 for (i = 0; i < map->n; ++i)
5342 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5343 return dup;
5346 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5347 __isl_take isl_basic_map *bmap)
5349 if (!bmap || !map)
5350 goto error;
5351 if (isl_basic_map_plain_is_empty(bmap)) {
5352 isl_basic_map_free(bmap);
5353 return map;
5355 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5356 isl_assert(map->ctx, map->n < map->size, goto error);
5357 map->p[map->n] = bmap;
5358 map->n++;
5359 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5360 return map;
5361 error:
5362 if (map)
5363 isl_map_free(map);
5364 if (bmap)
5365 isl_basic_map_free(bmap);
5366 return NULL;
5369 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5371 int i;
5373 if (!map)
5374 return NULL;
5376 if (--map->ref > 0)
5377 return NULL;
5379 isl_ctx_deref(map->ctx);
5380 for (i = 0; i < map->n; ++i)
5381 isl_basic_map_free(map->p[i]);
5382 isl_space_free(map->dim);
5383 free(map);
5385 return NULL;
5388 struct isl_map *isl_map_extend(struct isl_map *base,
5389 unsigned nparam, unsigned n_in, unsigned n_out)
5391 int i;
5393 base = isl_map_cow(base);
5394 if (!base)
5395 return NULL;
5397 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5398 if (!base->dim)
5399 goto error;
5400 for (i = 0; i < base->n; ++i) {
5401 base->p[i] = isl_basic_map_extend_space(base->p[i],
5402 isl_space_copy(base->dim), 0, 0, 0);
5403 if (!base->p[i])
5404 goto error;
5406 return base;
5407 error:
5408 isl_map_free(base);
5409 return NULL;
5412 struct isl_set *isl_set_extend(struct isl_set *base,
5413 unsigned nparam, unsigned dim)
5415 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5416 nparam, 0, dim);
5419 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5420 struct isl_basic_map *bmap, unsigned pos, int value)
5422 int j;
5424 bmap = isl_basic_map_cow(bmap);
5425 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5426 j = isl_basic_map_alloc_equality(bmap);
5427 if (j < 0)
5428 goto error;
5429 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5430 isl_int_set_si(bmap->eq[j][pos], -1);
5431 isl_int_set_si(bmap->eq[j][0], value);
5432 bmap = isl_basic_map_simplify(bmap);
5433 return isl_basic_map_finalize(bmap);
5434 error:
5435 isl_basic_map_free(bmap);
5436 return NULL;
5439 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5440 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5442 int j;
5444 bmap = isl_basic_map_cow(bmap);
5445 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5446 j = isl_basic_map_alloc_equality(bmap);
5447 if (j < 0)
5448 goto error;
5449 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5450 isl_int_set_si(bmap->eq[j][pos], -1);
5451 isl_int_set(bmap->eq[j][0], value);
5452 bmap = isl_basic_map_simplify(bmap);
5453 return isl_basic_map_finalize(bmap);
5454 error:
5455 isl_basic_map_free(bmap);
5456 return NULL;
5459 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5460 enum isl_dim_type type, unsigned pos, int value)
5462 if (!bmap)
5463 return NULL;
5464 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5465 return isl_basic_map_fix_pos_si(bmap,
5466 isl_basic_map_offset(bmap, type) + pos, value);
5467 error:
5468 isl_basic_map_free(bmap);
5469 return NULL;
5472 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5473 enum isl_dim_type type, unsigned pos, isl_int value)
5475 if (!bmap)
5476 return NULL;
5477 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5478 return isl_basic_map_fix_pos(bmap,
5479 isl_basic_map_offset(bmap, type) + pos, value);
5480 error:
5481 isl_basic_map_free(bmap);
5482 return NULL;
5485 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5486 * to be equal to "v".
5488 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5489 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5491 if (!bmap || !v)
5492 goto error;
5493 if (!isl_val_is_int(v))
5494 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5495 "expecting integer value", goto error);
5496 if (pos >= isl_basic_map_dim(bmap, type))
5497 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5498 "index out of bounds", goto error);
5499 pos += isl_basic_map_offset(bmap, type);
5500 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5501 isl_val_free(v);
5502 return bmap;
5503 error:
5504 isl_basic_map_free(bmap);
5505 isl_val_free(v);
5506 return NULL;
5509 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5510 * to be equal to "v".
5512 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5513 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5515 return isl_basic_map_fix_val(bset, type, pos, v);
5518 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5519 enum isl_dim_type type, unsigned pos, int value)
5521 return (struct isl_basic_set *)
5522 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5523 type, pos, value);
5526 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5527 enum isl_dim_type type, unsigned pos, isl_int value)
5529 return (struct isl_basic_set *)
5530 isl_basic_map_fix((struct isl_basic_map *)bset,
5531 type, pos, value);
5534 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5535 unsigned input, int value)
5537 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5540 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5541 unsigned dim, int value)
5543 return (struct isl_basic_set *)
5544 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5545 isl_dim_set, dim, value);
5548 static int remove_if_empty(__isl_keep isl_map *map, int i)
5550 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5552 if (empty < 0)
5553 return -1;
5554 if (!empty)
5555 return 0;
5557 isl_basic_map_free(map->p[i]);
5558 if (i != map->n - 1) {
5559 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5560 map->p[i] = map->p[map->n - 1];
5562 map->n--;
5564 return 0;
5567 /* Perform "fn" on each basic map of "map", where we may not be holding
5568 * the only reference to "map".
5569 * In particular, "fn" should be a semantics preserving operation
5570 * that we want to apply to all copies of "map". We therefore need
5571 * to be careful not to modify "map" in a way that breaks "map"
5572 * in case anything goes wrong.
5574 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5575 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5577 struct isl_basic_map *bmap;
5578 int i;
5580 if (!map)
5581 return NULL;
5583 for (i = map->n - 1; i >= 0; --i) {
5584 bmap = isl_basic_map_copy(map->p[i]);
5585 bmap = fn(bmap);
5586 if (!bmap)
5587 goto error;
5588 isl_basic_map_free(map->p[i]);
5589 map->p[i] = bmap;
5590 if (remove_if_empty(map, i) < 0)
5591 goto error;
5594 return map;
5595 error:
5596 isl_map_free(map);
5597 return NULL;
5600 struct isl_map *isl_map_fix_si(struct isl_map *map,
5601 enum isl_dim_type type, unsigned pos, int value)
5603 int i;
5605 map = isl_map_cow(map);
5606 if (!map)
5607 return NULL;
5609 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5610 for (i = map->n - 1; i >= 0; --i) {
5611 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5612 if (remove_if_empty(map, i) < 0)
5613 goto error;
5615 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5616 return map;
5617 error:
5618 isl_map_free(map);
5619 return NULL;
5622 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5623 enum isl_dim_type type, unsigned pos, int value)
5625 return (struct isl_set *)
5626 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5629 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5630 enum isl_dim_type type, unsigned pos, isl_int value)
5632 int i;
5634 map = isl_map_cow(map);
5635 if (!map)
5636 return NULL;
5638 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5639 for (i = 0; i < map->n; ++i) {
5640 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5641 if (!map->p[i])
5642 goto error;
5644 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5645 return map;
5646 error:
5647 isl_map_free(map);
5648 return NULL;
5651 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5652 enum isl_dim_type type, unsigned pos, isl_int value)
5654 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5657 /* Fix the value of the variable at position "pos" of type "type" of "map"
5658 * to be equal to "v".
5660 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5661 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5663 int i;
5665 map = isl_map_cow(map);
5666 if (!map || !v)
5667 goto error;
5669 if (!isl_val_is_int(v))
5670 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5671 "expecting integer value", goto error);
5672 if (pos >= isl_map_dim(map, type))
5673 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5674 "index out of bounds", goto error);
5675 for (i = map->n - 1; i >= 0; --i) {
5676 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5677 isl_val_copy(v));
5678 if (remove_if_empty(map, i) < 0)
5679 goto error;
5681 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5682 isl_val_free(v);
5683 return map;
5684 error:
5685 isl_map_free(map);
5686 isl_val_free(v);
5687 return NULL;
5690 /* Fix the value of the variable at position "pos" of type "type" of "set"
5691 * to be equal to "v".
5693 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5694 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5696 return isl_map_fix_val(set, type, pos, v);
5699 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5700 unsigned input, int value)
5702 return isl_map_fix_si(map, isl_dim_in, input, value);
5705 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5707 return (struct isl_set *)
5708 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5711 static __isl_give isl_basic_map *basic_map_bound_si(
5712 __isl_take isl_basic_map *bmap,
5713 enum isl_dim_type type, unsigned pos, int value, int upper)
5715 int j;
5717 if (!bmap)
5718 return NULL;
5719 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5720 pos += isl_basic_map_offset(bmap, type);
5721 bmap = isl_basic_map_cow(bmap);
5722 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5723 j = isl_basic_map_alloc_inequality(bmap);
5724 if (j < 0)
5725 goto error;
5726 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5727 if (upper) {
5728 isl_int_set_si(bmap->ineq[j][pos], -1);
5729 isl_int_set_si(bmap->ineq[j][0], value);
5730 } else {
5731 isl_int_set_si(bmap->ineq[j][pos], 1);
5732 isl_int_set_si(bmap->ineq[j][0], -value);
5734 bmap = isl_basic_map_simplify(bmap);
5735 return isl_basic_map_finalize(bmap);
5736 error:
5737 isl_basic_map_free(bmap);
5738 return NULL;
5741 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5742 __isl_take isl_basic_map *bmap,
5743 enum isl_dim_type type, unsigned pos, int value)
5745 return basic_map_bound_si(bmap, type, pos, value, 0);
5748 /* Constrain the values of the given dimension to be no greater than "value".
5750 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5751 __isl_take isl_basic_map *bmap,
5752 enum isl_dim_type type, unsigned pos, int value)
5754 return basic_map_bound_si(bmap, type, pos, value, 1);
5757 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5758 unsigned dim, isl_int value)
5760 int j;
5762 bset = isl_basic_set_cow(bset);
5763 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5764 j = isl_basic_set_alloc_inequality(bset);
5765 if (j < 0)
5766 goto error;
5767 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5768 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5769 isl_int_neg(bset->ineq[j][0], value);
5770 bset = isl_basic_set_simplify(bset);
5771 return isl_basic_set_finalize(bset);
5772 error:
5773 isl_basic_set_free(bset);
5774 return NULL;
5777 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5778 enum isl_dim_type type, unsigned pos, int value, int upper)
5780 int i;
5782 map = isl_map_cow(map);
5783 if (!map)
5784 return NULL;
5786 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5787 for (i = 0; i < map->n; ++i) {
5788 map->p[i] = basic_map_bound_si(map->p[i],
5789 type, pos, value, upper);
5790 if (!map->p[i])
5791 goto error;
5793 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5794 return map;
5795 error:
5796 isl_map_free(map);
5797 return NULL;
5800 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5801 enum isl_dim_type type, unsigned pos, int value)
5803 return map_bound_si(map, type, pos, value, 0);
5806 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5807 enum isl_dim_type type, unsigned pos, int value)
5809 return map_bound_si(map, type, pos, value, 1);
5812 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5813 enum isl_dim_type type, unsigned pos, int value)
5815 return (struct isl_set *)
5816 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5819 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5820 enum isl_dim_type type, unsigned pos, int value)
5822 return isl_map_upper_bound_si(set, type, pos, value);
5825 /* Bound the given variable of "bmap" from below (or above is "upper"
5826 * is set) to "value".
5828 static __isl_give isl_basic_map *basic_map_bound(
5829 __isl_take isl_basic_map *bmap,
5830 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5832 int j;
5834 if (!bmap)
5835 return NULL;
5836 if (pos >= isl_basic_map_dim(bmap, type))
5837 isl_die(bmap->ctx, isl_error_invalid,
5838 "index out of bounds", goto error);
5839 pos += isl_basic_map_offset(bmap, type);
5840 bmap = isl_basic_map_cow(bmap);
5841 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5842 j = isl_basic_map_alloc_inequality(bmap);
5843 if (j < 0)
5844 goto error;
5845 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5846 if (upper) {
5847 isl_int_set_si(bmap->ineq[j][pos], -1);
5848 isl_int_set(bmap->ineq[j][0], value);
5849 } else {
5850 isl_int_set_si(bmap->ineq[j][pos], 1);
5851 isl_int_neg(bmap->ineq[j][0], value);
5853 bmap = isl_basic_map_simplify(bmap);
5854 return isl_basic_map_finalize(bmap);
5855 error:
5856 isl_basic_map_free(bmap);
5857 return NULL;
5860 /* Bound the given variable of "map" from below (or above is "upper"
5861 * is set) to "value".
5863 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5864 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5866 int i;
5868 map = isl_map_cow(map);
5869 if (!map)
5870 return NULL;
5872 if (pos >= isl_map_dim(map, type))
5873 isl_die(map->ctx, isl_error_invalid,
5874 "index out of bounds", goto error);
5875 for (i = map->n - 1; i >= 0; --i) {
5876 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5877 if (remove_if_empty(map, i) < 0)
5878 goto error;
5880 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5881 return map;
5882 error:
5883 isl_map_free(map);
5884 return NULL;
5887 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5888 enum isl_dim_type type, unsigned pos, isl_int value)
5890 return map_bound(map, type, pos, value, 0);
5893 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5894 enum isl_dim_type type, unsigned pos, isl_int value)
5896 return map_bound(map, type, pos, value, 1);
5899 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5900 enum isl_dim_type type, unsigned pos, isl_int value)
5902 return isl_map_lower_bound(set, type, pos, value);
5905 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5906 enum isl_dim_type type, unsigned pos, isl_int value)
5908 return isl_map_upper_bound(set, type, pos, value);
5911 /* Force the values of the variable at position "pos" of type "type" of "set"
5912 * to be no smaller than "value".
5914 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5915 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5917 if (!value)
5918 goto error;
5919 if (!isl_val_is_int(value))
5920 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5921 "expecting integer value", goto error);
5922 set = isl_set_lower_bound(set, type, pos, value->n);
5923 isl_val_free(value);
5924 return set;
5925 error:
5926 isl_val_free(value);
5927 isl_set_free(set);
5928 return NULL;
5931 /* Force the values of the variable at position "pos" of type "type" of "set"
5932 * to be no greater than "value".
5934 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5935 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5937 if (!value)
5938 goto error;
5939 if (!isl_val_is_int(value))
5940 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5941 "expecting integer value", goto error);
5942 set = isl_set_upper_bound(set, type, pos, value->n);
5943 isl_val_free(value);
5944 return set;
5945 error:
5946 isl_val_free(value);
5947 isl_set_free(set);
5948 return NULL;
5951 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5952 isl_int value)
5954 int i;
5956 set = isl_set_cow(set);
5957 if (!set)
5958 return NULL;
5960 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5961 for (i = 0; i < set->n; ++i) {
5962 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5963 if (!set->p[i])
5964 goto error;
5966 return set;
5967 error:
5968 isl_set_free(set);
5969 return NULL;
5972 struct isl_map *isl_map_reverse(struct isl_map *map)
5974 int i;
5976 map = isl_map_cow(map);
5977 if (!map)
5978 return NULL;
5980 map->dim = isl_space_reverse(map->dim);
5981 if (!map->dim)
5982 goto error;
5983 for (i = 0; i < map->n; ++i) {
5984 map->p[i] = isl_basic_map_reverse(map->p[i]);
5985 if (!map->p[i])
5986 goto error;
5988 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5989 return map;
5990 error:
5991 isl_map_free(map);
5992 return NULL;
5995 static struct isl_map *isl_basic_map_partial_lexopt(
5996 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5997 struct isl_set **empty, int max)
5999 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6002 struct isl_map *isl_basic_map_partial_lexmax(
6003 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6004 struct isl_set **empty)
6006 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6009 struct isl_map *isl_basic_map_partial_lexmin(
6010 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6011 struct isl_set **empty)
6013 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6016 struct isl_set *isl_basic_set_partial_lexmin(
6017 struct isl_basic_set *bset, struct isl_basic_set *dom,
6018 struct isl_set **empty)
6020 return (struct isl_set *)
6021 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6022 dom, empty);
6025 struct isl_set *isl_basic_set_partial_lexmax(
6026 struct isl_basic_set *bset, struct isl_basic_set *dom,
6027 struct isl_set **empty)
6029 return (struct isl_set *)
6030 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6031 dom, empty);
6034 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6035 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6036 __isl_give isl_set **empty)
6038 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6041 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6042 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6043 __isl_give isl_set **empty)
6045 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6048 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6049 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6050 __isl_give isl_set **empty)
6052 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6055 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6056 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6057 __isl_give isl_set **empty)
6059 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6062 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6063 __isl_take isl_basic_map *bmap, int max)
6065 isl_basic_set *dom = NULL;
6066 isl_space *dom_space;
6068 if (!bmap)
6069 goto error;
6070 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6071 dom = isl_basic_set_universe(dom_space);
6072 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6073 error:
6074 isl_basic_map_free(bmap);
6075 return NULL;
6078 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6079 __isl_take isl_basic_map *bmap)
6081 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6084 #undef TYPE
6085 #define TYPE isl_pw_multi_aff
6086 #undef SUFFIX
6087 #define SUFFIX _pw_multi_aff
6088 #undef EMPTY
6089 #define EMPTY isl_pw_multi_aff_empty
6090 #undef ADD
6091 #define ADD isl_pw_multi_aff_union_add
6092 #include "isl_map_lexopt_templ.c"
6094 /* Given a map "map", compute the lexicographically minimal
6095 * (or maximal) image element for each domain element in dom,
6096 * in the form of an isl_pw_multi_aff.
6097 * Set *empty to those elements in dom that do not have an image element.
6099 * We first compute the lexicographically minimal or maximal element
6100 * in the first basic map. This results in a partial solution "res"
6101 * and a subset "todo" of dom that still need to be handled.
6102 * We then consider each of the remaining maps in "map" and successively
6103 * update both "res" and "todo".
6105 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6106 __isl_take isl_map *map, __isl_take isl_set *dom,
6107 __isl_give isl_set **empty, int max)
6109 int i;
6110 isl_pw_multi_aff *res;
6111 isl_set *todo;
6113 if (!map || !dom)
6114 goto error;
6116 if (isl_map_plain_is_empty(map)) {
6117 if (empty)
6118 *empty = dom;
6119 else
6120 isl_set_free(dom);
6121 return isl_pw_multi_aff_from_map(map);
6124 res = basic_map_partial_lexopt_pw_multi_aff(
6125 isl_basic_map_copy(map->p[0]),
6126 isl_set_copy(dom), &todo, max);
6128 for (i = 1; i < map->n; ++i) {
6129 isl_pw_multi_aff *res_i;
6130 isl_set *todo_i;
6132 res_i = basic_map_partial_lexopt_pw_multi_aff(
6133 isl_basic_map_copy(map->p[i]),
6134 isl_set_copy(dom), &todo_i, max);
6136 if (max)
6137 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6138 else
6139 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6141 todo = isl_set_intersect(todo, todo_i);
6144 isl_set_free(dom);
6145 isl_map_free(map);
6147 if (empty)
6148 *empty = todo;
6149 else
6150 isl_set_free(todo);
6152 return res;
6153 error:
6154 if (empty)
6155 *empty = NULL;
6156 isl_set_free(dom);
6157 isl_map_free(map);
6158 return NULL;
6161 #undef TYPE
6162 #define TYPE isl_map
6163 #undef SUFFIX
6164 #define SUFFIX
6165 #undef EMPTY
6166 #define EMPTY isl_map_empty
6167 #undef ADD
6168 #define ADD isl_map_union_disjoint
6169 #include "isl_map_lexopt_templ.c"
6171 /* Given a map "map", compute the lexicographically minimal
6172 * (or maximal) image element for each domain element in dom.
6173 * Set *empty to those elements in dom that do not have an image element.
6175 * We first compute the lexicographically minimal or maximal element
6176 * in the first basic map. This results in a partial solution "res"
6177 * and a subset "todo" of dom that still need to be handled.
6178 * We then consider each of the remaining maps in "map" and successively
6179 * update both "res" and "todo".
6181 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6182 * Assume we are computing the lexicographical maximum.
6183 * We first compute the lexicographically maximal element in basic map i.
6184 * This results in a partial solution res_i and a subset todo_i.
6185 * Then we combine these results with those obtain for the first k basic maps
6186 * to obtain a result that is valid for the first k+1 basic maps.
6187 * In particular, the set where there is no solution is the set where
6188 * there is no solution for the first k basic maps and also no solution
6189 * for the ith basic map, i.e.,
6191 * todo^i = todo^k * todo_i
6193 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6194 * solutions, arbitrarily breaking ties in favor of res^k.
6195 * That is, when res^k(a) >= res_i(a), we pick res^k and
6196 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6197 * the lexicographic order.)
6198 * In practice, we compute
6200 * res^k * (res_i . "<=")
6202 * and
6204 * res_i * (res^k . "<")
6206 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6207 * where only one of res^k and res_i provides a solution and we simply pick
6208 * that one, i.e.,
6210 * res^k * todo_i
6211 * and
6212 * res_i * todo^k
6214 * Note that we only compute these intersections when dom(res^k) intersects
6215 * dom(res_i). Otherwise, the only effect of these intersections is to
6216 * potentially break up res^k and res_i into smaller pieces.
6217 * We want to avoid such splintering as much as possible.
6218 * In fact, an earlier implementation of this function would look for
6219 * better results in the domain of res^k and for extra results in todo^k,
6220 * but this would always result in a splintering according to todo^k,
6221 * even when the domain of basic map i is disjoint from the domains of
6222 * the previous basic maps.
6224 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6225 __isl_take isl_map *map, __isl_take isl_set *dom,
6226 __isl_give isl_set **empty, int max)
6228 int i;
6229 struct isl_map *res;
6230 struct isl_set *todo;
6232 if (!map || !dom)
6233 goto error;
6235 if (isl_map_plain_is_empty(map)) {
6236 if (empty)
6237 *empty = dom;
6238 else
6239 isl_set_free(dom);
6240 return map;
6243 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6244 isl_set_copy(dom), &todo, max);
6246 for (i = 1; i < map->n; ++i) {
6247 isl_map *lt, *le;
6248 isl_map *res_i;
6249 isl_set *todo_i;
6250 isl_space *dim = isl_space_range(isl_map_get_space(res));
6252 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6253 isl_set_copy(dom), &todo_i, max);
6255 if (max) {
6256 lt = isl_map_lex_lt(isl_space_copy(dim));
6257 le = isl_map_lex_le(dim);
6258 } else {
6259 lt = isl_map_lex_gt(isl_space_copy(dim));
6260 le = isl_map_lex_ge(dim);
6262 lt = isl_map_apply_range(isl_map_copy(res), lt);
6263 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6264 le = isl_map_apply_range(isl_map_copy(res_i), le);
6265 le = isl_map_intersect(le, isl_map_copy(res));
6267 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6268 res = isl_map_intersect_domain(res,
6269 isl_set_copy(todo_i));
6270 res_i = isl_map_intersect_domain(res_i,
6271 isl_set_copy(todo));
6274 res = isl_map_union_disjoint(res, res_i);
6275 res = isl_map_union_disjoint(res, lt);
6276 res = isl_map_union_disjoint(res, le);
6278 todo = isl_set_intersect(todo, todo_i);
6281 isl_set_free(dom);
6282 isl_map_free(map);
6284 if (empty)
6285 *empty = todo;
6286 else
6287 isl_set_free(todo);
6289 return res;
6290 error:
6291 if (empty)
6292 *empty = NULL;
6293 isl_set_free(dom);
6294 isl_map_free(map);
6295 return NULL;
6298 __isl_give isl_map *isl_map_partial_lexmax(
6299 __isl_take isl_map *map, __isl_take isl_set *dom,
6300 __isl_give isl_set **empty)
6302 return isl_map_partial_lexopt(map, dom, empty, 1);
6305 __isl_give isl_map *isl_map_partial_lexmin(
6306 __isl_take isl_map *map, __isl_take isl_set *dom,
6307 __isl_give isl_set **empty)
6309 return isl_map_partial_lexopt(map, dom, empty, 0);
6312 __isl_give isl_set *isl_set_partial_lexmin(
6313 __isl_take isl_set *set, __isl_take isl_set *dom,
6314 __isl_give isl_set **empty)
6316 return (struct isl_set *)
6317 isl_map_partial_lexmin((struct isl_map *)set,
6318 dom, empty);
6321 __isl_give isl_set *isl_set_partial_lexmax(
6322 __isl_take isl_set *set, __isl_take isl_set *dom,
6323 __isl_give isl_set **empty)
6325 return (struct isl_set *)
6326 isl_map_partial_lexmax((struct isl_map *)set,
6327 dom, empty);
6330 /* Compute the lexicographic minimum (or maximum if "max" is set)
6331 * of "bmap" over its domain.
6333 * Since we are not interested in the part of the domain space where
6334 * there is no solution, we initialize the domain to those constraints
6335 * of "bmap" that only involve the parameters and the input dimensions.
6336 * This relieves the parametric programming engine from detecting those
6337 * inequalities and transferring them to the context. More importantly,
6338 * it ensures that those inequalities are transferred first and not
6339 * intermixed with inequalities that actually split the domain.
6341 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6343 int n_div;
6344 int n_out;
6345 isl_basic_map *copy;
6346 isl_basic_set *dom;
6348 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6349 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6350 copy = isl_basic_map_copy(bmap);
6351 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6352 isl_dim_div, 0, n_div);
6353 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6354 isl_dim_out, 0, n_out);
6355 dom = isl_basic_map_domain(copy);
6356 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6359 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6361 return isl_basic_map_lexopt(bmap, 0);
6364 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6366 return isl_basic_map_lexopt(bmap, 1);
6369 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6371 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6374 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6376 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6379 /* Extract the first and only affine expression from list
6380 * and then add it to *pwaff with the given dom.
6381 * This domain is known to be disjoint from other domains
6382 * because of the way isl_basic_map_foreach_lexmax works.
6384 static int update_dim_opt(__isl_take isl_basic_set *dom,
6385 __isl_take isl_aff_list *list, void *user)
6387 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6388 isl_aff *aff;
6389 isl_pw_aff **pwaff = user;
6390 isl_pw_aff *pwaff_i;
6392 if (!list)
6393 goto error;
6394 if (isl_aff_list_n_aff(list) != 1)
6395 isl_die(ctx, isl_error_internal,
6396 "expecting single element list", goto error);
6398 aff = isl_aff_list_get_aff(list, 0);
6399 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6401 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6403 isl_aff_list_free(list);
6405 return 0;
6406 error:
6407 isl_basic_set_free(dom);
6408 isl_aff_list_free(list);
6409 return -1;
6412 /* Given a basic map with one output dimension, compute the minimum or
6413 * maximum of that dimension as an isl_pw_aff.
6415 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6416 * call update_dim_opt on each leaf of the result.
6418 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6419 int max)
6421 isl_space *dim = isl_basic_map_get_space(bmap);
6422 isl_pw_aff *pwaff;
6423 int r;
6425 dim = isl_space_from_domain(isl_space_domain(dim));
6426 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6427 pwaff = isl_pw_aff_empty(dim);
6429 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6430 if (r < 0)
6431 return isl_pw_aff_free(pwaff);
6433 return pwaff;
6436 /* Compute the minimum or maximum of the given output dimension
6437 * as a function of the parameters and the input dimensions,
6438 * but independently of the other output dimensions.
6440 * We first project out the other output dimension and then compute
6441 * the "lexicographic" maximum in each basic map, combining the results
6442 * using isl_pw_aff_union_max.
6444 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6445 int max)
6447 int i;
6448 isl_pw_aff *pwaff;
6449 unsigned n_out;
6451 n_out = isl_map_dim(map, isl_dim_out);
6452 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6453 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6454 if (!map)
6455 return NULL;
6457 if (map->n == 0) {
6458 isl_space *dim = isl_map_get_space(map);
6459 isl_map_free(map);
6460 return isl_pw_aff_empty(dim);
6463 pwaff = basic_map_dim_opt(map->p[0], max);
6464 for (i = 1; i < map->n; ++i) {
6465 isl_pw_aff *pwaff_i;
6467 pwaff_i = basic_map_dim_opt(map->p[i], max);
6468 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6471 isl_map_free(map);
6473 return pwaff;
6476 /* Compute the maximum of the given output dimension as a function of the
6477 * parameters and input dimensions, but independently of
6478 * the other output dimensions.
6480 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6482 return map_dim_opt(map, pos, 1);
6485 /* Compute the minimum or maximum of the given set dimension
6486 * as a function of the parameters,
6487 * but independently of the other set dimensions.
6489 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6490 int max)
6492 return map_dim_opt(set, pos, max);
6495 /* Compute the maximum of the given set dimension as a function of the
6496 * parameters, but independently of the other set dimensions.
6498 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6500 return set_dim_opt(set, pos, 1);
6503 /* Compute the minimum of the given set dimension as a function of the
6504 * parameters, but independently of the other set dimensions.
6506 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6508 return set_dim_opt(set, pos, 0);
6511 /* Apply a preimage specified by "mat" on the parameters of "bset".
6512 * bset is assumed to have only parameters and divs.
6514 static struct isl_basic_set *basic_set_parameter_preimage(
6515 struct isl_basic_set *bset, struct isl_mat *mat)
6517 unsigned nparam;
6519 if (!bset || !mat)
6520 goto error;
6522 bset->dim = isl_space_cow(bset->dim);
6523 if (!bset->dim)
6524 goto error;
6526 nparam = isl_basic_set_dim(bset, isl_dim_param);
6528 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6530 bset->dim->nparam = 0;
6531 bset->dim->n_out = nparam;
6532 bset = isl_basic_set_preimage(bset, mat);
6533 if (bset) {
6534 bset->dim->nparam = bset->dim->n_out;
6535 bset->dim->n_out = 0;
6537 return bset;
6538 error:
6539 isl_mat_free(mat);
6540 isl_basic_set_free(bset);
6541 return NULL;
6544 /* Apply a preimage specified by "mat" on the parameters of "set".
6545 * set is assumed to have only parameters and divs.
6547 static struct isl_set *set_parameter_preimage(
6548 struct isl_set *set, struct isl_mat *mat)
6550 isl_space *dim = NULL;
6551 unsigned nparam;
6553 if (!set || !mat)
6554 goto error;
6556 dim = isl_space_copy(set->dim);
6557 dim = isl_space_cow(dim);
6558 if (!dim)
6559 goto error;
6561 nparam = isl_set_dim(set, isl_dim_param);
6563 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6565 dim->nparam = 0;
6566 dim->n_out = nparam;
6567 isl_set_reset_space(set, dim);
6568 set = isl_set_preimage(set, mat);
6569 if (!set)
6570 goto error2;
6571 dim = isl_space_copy(set->dim);
6572 dim = isl_space_cow(dim);
6573 if (!dim)
6574 goto error2;
6575 dim->nparam = dim->n_out;
6576 dim->n_out = 0;
6577 isl_set_reset_space(set, dim);
6578 return set;
6579 error:
6580 isl_space_free(dim);
6581 isl_mat_free(mat);
6582 error2:
6583 isl_set_free(set);
6584 return NULL;
6587 /* Intersect the basic set "bset" with the affine space specified by the
6588 * equalities in "eq".
6590 static struct isl_basic_set *basic_set_append_equalities(
6591 struct isl_basic_set *bset, struct isl_mat *eq)
6593 int i, k;
6594 unsigned len;
6596 if (!bset || !eq)
6597 goto error;
6599 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6600 eq->n_row, 0);
6601 if (!bset)
6602 goto error;
6604 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6605 for (i = 0; i < eq->n_row; ++i) {
6606 k = isl_basic_set_alloc_equality(bset);
6607 if (k < 0)
6608 goto error;
6609 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6610 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6612 isl_mat_free(eq);
6614 bset = isl_basic_set_gauss(bset, NULL);
6615 bset = isl_basic_set_finalize(bset);
6617 return bset;
6618 error:
6619 isl_mat_free(eq);
6620 isl_basic_set_free(bset);
6621 return NULL;
6624 /* Intersect the set "set" with the affine space specified by the
6625 * equalities in "eq".
6627 static struct isl_set *set_append_equalities(struct isl_set *set,
6628 struct isl_mat *eq)
6630 int i;
6632 if (!set || !eq)
6633 goto error;
6635 for (i = 0; i < set->n; ++i) {
6636 set->p[i] = basic_set_append_equalities(set->p[i],
6637 isl_mat_copy(eq));
6638 if (!set->p[i])
6639 goto error;
6641 isl_mat_free(eq);
6642 return set;
6643 error:
6644 isl_mat_free(eq);
6645 isl_set_free(set);
6646 return NULL;
6649 /* Given a basic set "bset" that only involves parameters and existentially
6650 * quantified variables, return the index of the first equality
6651 * that only involves parameters. If there is no such equality then
6652 * return bset->n_eq.
6654 * This function assumes that isl_basic_set_gauss has been called on "bset".
6656 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6658 int i, j;
6659 unsigned nparam, n_div;
6661 if (!bset)
6662 return -1;
6664 nparam = isl_basic_set_dim(bset, isl_dim_param);
6665 n_div = isl_basic_set_dim(bset, isl_dim_div);
6667 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6668 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6669 ++i;
6672 return i;
6675 /* Compute an explicit representation for the existentially quantified
6676 * variables in "bset" by computing the "minimal value" of the set
6677 * variables. Since there are no set variables, the computation of
6678 * the minimal value essentially computes an explicit representation
6679 * of the non-empty part(s) of "bset".
6681 * The input only involves parameters and existentially quantified variables.
6682 * All equalities among parameters have been removed.
6684 * Since the existentially quantified variables in the result are in general
6685 * going to be different from those in the input, we first replace
6686 * them by the minimal number of variables based on their equalities.
6687 * This should simplify the parametric integer programming.
6689 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6691 isl_morph *morph1, *morph2;
6692 isl_set *set;
6693 unsigned n;
6695 if (!bset)
6696 return NULL;
6697 if (bset->n_eq == 0)
6698 return isl_basic_set_lexmin(bset);
6700 morph1 = isl_basic_set_parameter_compression(bset);
6701 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6702 bset = isl_basic_set_lift(bset);
6703 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6704 bset = isl_morph_basic_set(morph2, bset);
6705 n = isl_basic_set_dim(bset, isl_dim_set);
6706 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6708 set = isl_basic_set_lexmin(bset);
6710 set = isl_morph_set(isl_morph_inverse(morph1), set);
6712 return set;
6715 /* Project the given basic set onto its parameter domain, possibly introducing
6716 * new, explicit, existential variables in the constraints.
6717 * The input has parameters and (possibly implicit) existential variables.
6718 * The output has the same parameters, but only
6719 * explicit existentially quantified variables.
6721 * The actual projection is performed by pip, but pip doesn't seem
6722 * to like equalities very much, so we first remove the equalities
6723 * among the parameters by performing a variable compression on
6724 * the parameters. Afterward, an inverse transformation is performed
6725 * and the equalities among the parameters are inserted back in.
6727 * The variable compression on the parameters may uncover additional
6728 * equalities that were only implicit before. We therefore check
6729 * if there are any new parameter equalities in the result and
6730 * if so recurse. The removal of parameter equalities is required
6731 * for the parameter compression performed by base_compute_divs.
6733 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6735 int i;
6736 struct isl_mat *eq;
6737 struct isl_mat *T, *T2;
6738 struct isl_set *set;
6739 unsigned nparam;
6741 bset = isl_basic_set_cow(bset);
6742 if (!bset)
6743 return NULL;
6745 if (bset->n_eq == 0)
6746 return base_compute_divs(bset);
6748 bset = isl_basic_set_gauss(bset, NULL);
6749 if (!bset)
6750 return NULL;
6751 if (isl_basic_set_plain_is_empty(bset))
6752 return isl_set_from_basic_set(bset);
6754 i = first_parameter_equality(bset);
6755 if (i == bset->n_eq)
6756 return base_compute_divs(bset);
6758 nparam = isl_basic_set_dim(bset, isl_dim_param);
6759 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6760 0, 1 + nparam);
6761 eq = isl_mat_cow(eq);
6762 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6763 if (T && T->n_col == 0) {
6764 isl_mat_free(T);
6765 isl_mat_free(T2);
6766 isl_mat_free(eq);
6767 bset = isl_basic_set_set_to_empty(bset);
6768 return isl_set_from_basic_set(bset);
6770 bset = basic_set_parameter_preimage(bset, T);
6772 i = first_parameter_equality(bset);
6773 if (!bset)
6774 set = NULL;
6775 else if (i == bset->n_eq)
6776 set = base_compute_divs(bset);
6777 else
6778 set = parameter_compute_divs(bset);
6779 set = set_parameter_preimage(set, T2);
6780 set = set_append_equalities(set, eq);
6781 return set;
6784 /* Insert the divs from "ls" before those of "bmap".
6786 * The number of columns is not changed, which means that the last
6787 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6788 * The caller is responsible for removing the same number of dimensions
6789 * from the space of "bmap".
6791 static __isl_give isl_basic_map *insert_divs_from_local_space(
6792 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6794 int i;
6795 int n_div;
6796 int old_n_div;
6798 n_div = isl_local_space_dim(ls, isl_dim_div);
6799 if (n_div == 0)
6800 return bmap;
6802 old_n_div = bmap->n_div;
6803 bmap = insert_div_rows(bmap, n_div);
6804 if (!bmap)
6805 return NULL;
6807 for (i = 0; i < n_div; ++i) {
6808 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6809 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6812 return bmap;
6815 /* Replace the space of "bmap" by the space and divs of "ls".
6817 * If "ls" has any divs, then we simplify the result since we may
6818 * have discovered some additional equalities that could simplify
6819 * the div expressions.
6821 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6822 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6824 int n_div;
6826 bmap = isl_basic_map_cow(bmap);
6827 if (!bmap || !ls)
6828 goto error;
6830 n_div = isl_local_space_dim(ls, isl_dim_div);
6831 bmap = insert_divs_from_local_space(bmap, ls);
6832 if (!bmap)
6833 goto error;
6835 isl_space_free(bmap->dim);
6836 bmap->dim = isl_local_space_get_space(ls);
6837 if (!bmap->dim)
6838 goto error;
6840 isl_local_space_free(ls);
6841 if (n_div > 0)
6842 bmap = isl_basic_map_simplify(bmap);
6843 bmap = isl_basic_map_finalize(bmap);
6844 return bmap;
6845 error:
6846 isl_basic_map_free(bmap);
6847 isl_local_space_free(ls);
6848 return NULL;
6851 /* Replace the space of "map" by the space and divs of "ls".
6853 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6854 __isl_take isl_local_space *ls)
6856 int i;
6858 map = isl_map_cow(map);
6859 if (!map || !ls)
6860 goto error;
6862 for (i = 0; i < map->n; ++i) {
6863 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6864 isl_local_space_copy(ls));
6865 if (!map->p[i])
6866 goto error;
6868 isl_space_free(map->dim);
6869 map->dim = isl_local_space_get_space(ls);
6870 if (!map->dim)
6871 goto error;
6873 isl_local_space_free(ls);
6874 return map;
6875 error:
6876 isl_local_space_free(ls);
6877 isl_map_free(map);
6878 return NULL;
6881 /* Compute an explicit representation for the existentially
6882 * quantified variables for which do not know any explicit representation yet.
6884 * We first sort the existentially quantified variables so that the
6885 * existentially quantified variables for which we already have an explicit
6886 * representation are placed before those for which we do not.
6887 * The input dimensions, the output dimensions and the existentially
6888 * quantified variables for which we already have an explicit
6889 * representation are then turned into parameters.
6890 * compute_divs returns a map with the same parameters and
6891 * no input or output dimensions and the dimension specification
6892 * is reset to that of the input, including the existentially quantified
6893 * variables for which we already had an explicit representation.
6895 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6897 struct isl_basic_set *bset;
6898 struct isl_set *set;
6899 struct isl_map *map;
6900 isl_space *dim;
6901 isl_local_space *ls;
6902 unsigned nparam;
6903 unsigned n_in;
6904 unsigned n_out;
6905 unsigned n_known;
6906 int i;
6908 bmap = isl_basic_map_sort_divs(bmap);
6909 bmap = isl_basic_map_cow(bmap);
6910 if (!bmap)
6911 return NULL;
6913 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6914 if (isl_int_is_zero(bmap->div[n_known][0]))
6915 break;
6917 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6918 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6919 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6920 dim = isl_space_set_alloc(bmap->ctx,
6921 nparam + n_in + n_out + n_known, 0);
6922 if (!dim)
6923 goto error;
6925 ls = isl_basic_map_get_local_space(bmap);
6926 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6927 n_known, bmap->n_div - n_known);
6928 if (n_known > 0) {
6929 for (i = n_known; i < bmap->n_div; ++i)
6930 swap_div(bmap, i - n_known, i);
6931 bmap->n_div -= n_known;
6932 bmap->extra -= n_known;
6934 bmap = isl_basic_map_reset_space(bmap, dim);
6935 bset = (struct isl_basic_set *)bmap;
6937 set = parameter_compute_divs(bset);
6938 map = (struct isl_map *)set;
6939 map = replace_space_by_local_space(map, ls);
6941 return map;
6942 error:
6943 isl_basic_map_free(bmap);
6944 return NULL;
6947 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6949 int i;
6950 unsigned off;
6952 if (!bmap)
6953 return -1;
6955 off = isl_space_dim(bmap->dim, isl_dim_all);
6956 for (i = 0; i < bmap->n_div; ++i) {
6957 if (isl_int_is_zero(bmap->div[i][0]))
6958 return 0;
6959 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6960 return -1);
6962 return 1;
6965 static int map_divs_known(__isl_keep isl_map *map)
6967 int i;
6969 if (!map)
6970 return -1;
6972 for (i = 0; i < map->n; ++i) {
6973 int known = isl_basic_map_divs_known(map->p[i]);
6974 if (known <= 0)
6975 return known;
6978 return 1;
6981 /* If bmap contains any unknown divs, then compute explicit
6982 * expressions for them. However, this computation may be
6983 * quite expensive, so first try to remove divs that aren't
6984 * strictly needed.
6986 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6988 int known;
6989 struct isl_map *map;
6991 known = isl_basic_map_divs_known(bmap);
6992 if (known < 0)
6993 goto error;
6994 if (known)
6995 return isl_map_from_basic_map(bmap);
6997 bmap = isl_basic_map_drop_redundant_divs(bmap);
6999 known = isl_basic_map_divs_known(bmap);
7000 if (known < 0)
7001 goto error;
7002 if (known)
7003 return isl_map_from_basic_map(bmap);
7005 map = compute_divs(bmap);
7006 return map;
7007 error:
7008 isl_basic_map_free(bmap);
7009 return NULL;
7012 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7014 int i;
7015 int known;
7016 struct isl_map *res;
7018 if (!map)
7019 return NULL;
7020 if (map->n == 0)
7021 return map;
7023 known = map_divs_known(map);
7024 if (known < 0) {
7025 isl_map_free(map);
7026 return NULL;
7028 if (known)
7029 return map;
7031 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7032 for (i = 1 ; i < map->n; ++i) {
7033 struct isl_map *r2;
7034 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7035 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7036 res = isl_map_union_disjoint(res, r2);
7037 else
7038 res = isl_map_union(res, r2);
7040 isl_map_free(map);
7042 return res;
7045 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7047 return (struct isl_set *)
7048 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7051 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7053 return (struct isl_set *)
7054 isl_map_compute_divs((struct isl_map *)set);
7057 struct isl_set *isl_map_domain(struct isl_map *map)
7059 int i;
7060 struct isl_set *set;
7062 if (!map)
7063 goto error;
7065 map = isl_map_cow(map);
7066 if (!map)
7067 return NULL;
7069 set = (struct isl_set *)map;
7070 set->dim = isl_space_domain(set->dim);
7071 if (!set->dim)
7072 goto error;
7073 for (i = 0; i < map->n; ++i) {
7074 set->p[i] = isl_basic_map_domain(map->p[i]);
7075 if (!set->p[i])
7076 goto error;
7078 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7079 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7080 return set;
7081 error:
7082 isl_map_free(map);
7083 return NULL;
7086 /* Return the union of "map1" and "map2", where we assume for now that
7087 * "map1" and "map2" are disjoint. Note that the basic maps inside
7088 * "map1" or "map2" may not be disjoint from each other.
7089 * Also note that this function is also called from isl_map_union,
7090 * which takes care of handling the situation where "map1" and "map2"
7091 * may not be disjoint.
7093 * If one of the inputs is empty, we can simply return the other input.
7094 * Similarly, if one of the inputs is universal, then it is equal to the union.
7096 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7097 __isl_take isl_map *map2)
7099 int i;
7100 unsigned flags = 0;
7101 struct isl_map *map = NULL;
7102 int is_universe;
7104 if (!map1 || !map2)
7105 goto error;
7107 if (map1->n == 0) {
7108 isl_map_free(map1);
7109 return map2;
7111 if (map2->n == 0) {
7112 isl_map_free(map2);
7113 return map1;
7116 is_universe = isl_map_plain_is_universe(map1);
7117 if (is_universe < 0)
7118 goto error;
7119 if (is_universe) {
7120 isl_map_free(map2);
7121 return map1;
7124 is_universe = isl_map_plain_is_universe(map2);
7125 if (is_universe < 0)
7126 goto error;
7127 if (is_universe) {
7128 isl_map_free(map1);
7129 return map2;
7132 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
7134 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7135 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7136 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7138 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7139 map1->n + map2->n, flags);
7140 if (!map)
7141 goto error;
7142 for (i = 0; i < map1->n; ++i) {
7143 map = isl_map_add_basic_map(map,
7144 isl_basic_map_copy(map1->p[i]));
7145 if (!map)
7146 goto error;
7148 for (i = 0; i < map2->n; ++i) {
7149 map = isl_map_add_basic_map(map,
7150 isl_basic_map_copy(map2->p[i]));
7151 if (!map)
7152 goto error;
7154 isl_map_free(map1);
7155 isl_map_free(map2);
7156 return map;
7157 error:
7158 isl_map_free(map);
7159 isl_map_free(map1);
7160 isl_map_free(map2);
7161 return NULL;
7164 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7165 * guaranteed to be disjoint by the caller.
7167 * Note that this functions is called from within isl_map_make_disjoint,
7168 * so we have to be careful not to touch the constraints of the inputs
7169 * in any way.
7171 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7172 __isl_take isl_map *map2)
7174 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7177 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7178 * not be disjoint. The parameters are assumed to have been aligned.
7180 * We currently simply call map_union_disjoint, the internal operation
7181 * of which does not really depend on the inputs being disjoint.
7182 * If the result contains more than one basic map, then we clear
7183 * the disjoint flag since the result may contain basic maps from
7184 * both inputs and these are not guaranteed to be disjoint.
7186 * As a special case, if "map1" and "map2" are obviously equal,
7187 * then we simply return "map1".
7189 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7190 __isl_take isl_map *map2)
7192 int equal;
7194 if (!map1 || !map2)
7195 goto error;
7197 equal = isl_map_plain_is_equal(map1, map2);
7198 if (equal < 0)
7199 goto error;
7200 if (equal) {
7201 isl_map_free(map2);
7202 return map1;
7205 map1 = map_union_disjoint(map1, map2);
7206 if (!map1)
7207 return NULL;
7208 if (map1->n > 1)
7209 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7210 return map1;
7211 error:
7212 isl_map_free(map1);
7213 isl_map_free(map2);
7214 return NULL;
7217 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7218 * not be disjoint.
7220 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7221 __isl_take isl_map *map2)
7223 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7226 struct isl_set *isl_set_union_disjoint(
7227 struct isl_set *set1, struct isl_set *set2)
7229 return (struct isl_set *)
7230 isl_map_union_disjoint(
7231 (struct isl_map *)set1, (struct isl_map *)set2);
7234 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7236 return (struct isl_set *)
7237 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7240 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7241 * the results.
7243 * "map" and "set" are assumed to be compatible and non-NULL.
7245 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7246 __isl_take isl_set *set,
7247 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7248 __isl_take isl_basic_set *bset))
7250 unsigned flags = 0;
7251 struct isl_map *result;
7252 int i, j;
7254 if (isl_set_plain_is_universe(set)) {
7255 isl_set_free(set);
7256 return map;
7259 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7260 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7261 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7263 result = isl_map_alloc_space(isl_space_copy(map->dim),
7264 map->n * set->n, flags);
7265 for (i = 0; result && i < map->n; ++i)
7266 for (j = 0; j < set->n; ++j) {
7267 result = isl_map_add_basic_map(result,
7268 fn(isl_basic_map_copy(map->p[i]),
7269 isl_basic_set_copy(set->p[j])));
7270 if (!result)
7271 break;
7274 isl_map_free(map);
7275 isl_set_free(set);
7276 return result;
7279 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7280 __isl_take isl_set *set)
7282 if (!map || !set)
7283 goto error;
7285 if (!isl_map_compatible_range(map, set))
7286 isl_die(set->ctx, isl_error_invalid,
7287 "incompatible spaces", goto error);
7289 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7290 error:
7291 isl_map_free(map);
7292 isl_set_free(set);
7293 return NULL;
7296 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7297 __isl_take isl_set *set)
7299 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7302 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7303 __isl_take isl_set *set)
7305 if (!map || !set)
7306 goto error;
7308 if (!isl_map_compatible_domain(map, set))
7309 isl_die(set->ctx, isl_error_invalid,
7310 "incompatible spaces", goto error);
7312 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7313 error:
7314 isl_map_free(map);
7315 isl_set_free(set);
7316 return NULL;
7319 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7320 __isl_take isl_set *set)
7322 return isl_map_align_params_map_map_and(map, set,
7323 &map_intersect_domain);
7326 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7327 __isl_take isl_map *map2)
7329 if (!map1 || !map2)
7330 goto error;
7331 map1 = isl_map_reverse(map1);
7332 map1 = isl_map_apply_range(map1, map2);
7333 return isl_map_reverse(map1);
7334 error:
7335 isl_map_free(map1);
7336 isl_map_free(map2);
7337 return NULL;
7340 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7341 __isl_take isl_map *map2)
7343 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7346 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7347 __isl_take isl_map *map2)
7349 isl_space *dim_result;
7350 struct isl_map *result;
7351 int i, j;
7353 if (!map1 || !map2)
7354 goto error;
7356 dim_result = isl_space_join(isl_space_copy(map1->dim),
7357 isl_space_copy(map2->dim));
7359 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7360 if (!result)
7361 goto error;
7362 for (i = 0; i < map1->n; ++i)
7363 for (j = 0; j < map2->n; ++j) {
7364 result = isl_map_add_basic_map(result,
7365 isl_basic_map_apply_range(
7366 isl_basic_map_copy(map1->p[i]),
7367 isl_basic_map_copy(map2->p[j])));
7368 if (!result)
7369 goto error;
7371 isl_map_free(map1);
7372 isl_map_free(map2);
7373 if (result && result->n <= 1)
7374 ISL_F_SET(result, ISL_MAP_DISJOINT);
7375 return result;
7376 error:
7377 isl_map_free(map1);
7378 isl_map_free(map2);
7379 return NULL;
7382 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7383 __isl_take isl_map *map2)
7385 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7389 * returns range - domain
7391 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7393 isl_space *dims, *target_dim;
7394 struct isl_basic_set *bset;
7395 unsigned dim;
7396 unsigned nparam;
7397 int i;
7399 if (!bmap)
7400 goto error;
7401 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
7402 bmap->dim, isl_dim_out),
7403 goto error);
7404 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7405 dim = isl_basic_map_n_in(bmap);
7406 nparam = isl_basic_map_n_param(bmap);
7407 bset = isl_basic_set_from_basic_map(bmap);
7408 bset = isl_basic_set_cow(bset);
7409 dims = isl_basic_set_get_space(bset);
7410 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7411 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7412 bset = isl_basic_set_swap_vars(bset, 2*dim);
7413 for (i = 0; i < dim; ++i) {
7414 int j = isl_basic_map_alloc_equality(
7415 (struct isl_basic_map *)bset);
7416 if (j < 0) {
7417 bset = isl_basic_set_free(bset);
7418 break;
7420 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7421 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7422 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7423 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7425 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7426 bset = isl_basic_set_reset_space(bset, target_dim);
7427 return bset;
7428 error:
7429 isl_basic_map_free(bmap);
7430 return NULL;
7434 * returns range - domain
7436 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7438 int i;
7439 isl_space *dim;
7440 struct isl_set *result;
7442 if (!map)
7443 return NULL;
7445 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
7446 map->dim, isl_dim_out),
7447 goto error);
7448 dim = isl_map_get_space(map);
7449 dim = isl_space_domain(dim);
7450 result = isl_set_alloc_space(dim, map->n, 0);
7451 if (!result)
7452 goto error;
7453 for (i = 0; i < map->n; ++i)
7454 result = isl_set_add_basic_set(result,
7455 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7456 isl_map_free(map);
7457 return result;
7458 error:
7459 isl_map_free(map);
7460 return NULL;
7464 * returns [domain -> range] -> range - domain
7466 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7467 __isl_take isl_basic_map *bmap)
7469 int i, k;
7470 isl_space *dim;
7471 isl_basic_map *domain;
7472 int nparam, n;
7473 unsigned total;
7475 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
7476 isl_die(bmap->ctx, isl_error_invalid,
7477 "domain and range don't match", goto error);
7479 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7480 n = isl_basic_map_dim(bmap, isl_dim_in);
7482 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7483 domain = isl_basic_map_universe(dim);
7485 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7486 bmap = isl_basic_map_apply_range(bmap, domain);
7487 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7489 total = isl_basic_map_total_dim(bmap);
7491 for (i = 0; i < n; ++i) {
7492 k = isl_basic_map_alloc_equality(bmap);
7493 if (k < 0)
7494 goto error;
7495 isl_seq_clr(bmap->eq[k], 1 + total);
7496 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7497 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7498 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7501 bmap = isl_basic_map_gauss(bmap, NULL);
7502 return isl_basic_map_finalize(bmap);
7503 error:
7504 isl_basic_map_free(bmap);
7505 return NULL;
7509 * returns [domain -> range] -> range - domain
7511 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7513 int i;
7514 isl_space *domain_dim;
7516 if (!map)
7517 return NULL;
7519 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
7520 isl_die(map->ctx, isl_error_invalid,
7521 "domain and range don't match", goto error);
7523 map = isl_map_cow(map);
7524 if (!map)
7525 return NULL;
7527 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7528 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7529 map->dim = isl_space_join(map->dim, domain_dim);
7530 if (!map->dim)
7531 goto error;
7532 for (i = 0; i < map->n; ++i) {
7533 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7534 if (!map->p[i])
7535 goto error;
7537 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7538 return map;
7539 error:
7540 isl_map_free(map);
7541 return NULL;
7544 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7546 struct isl_basic_map *bmap;
7547 unsigned nparam;
7548 unsigned dim;
7549 int i;
7551 if (!dims)
7552 return NULL;
7554 nparam = dims->nparam;
7555 dim = dims->n_out;
7556 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7557 if (!bmap)
7558 goto error;
7560 for (i = 0; i < dim; ++i) {
7561 int j = isl_basic_map_alloc_equality(bmap);
7562 if (j < 0)
7563 goto error;
7564 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7565 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7566 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7568 return isl_basic_map_finalize(bmap);
7569 error:
7570 isl_basic_map_free(bmap);
7571 return NULL;
7574 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7576 if (!dim)
7577 return NULL;
7578 if (dim->n_in != dim->n_out)
7579 isl_die(dim->ctx, isl_error_invalid,
7580 "number of input and output dimensions needs to be "
7581 "the same", goto error);
7582 return basic_map_identity(dim);
7583 error:
7584 isl_space_free(dim);
7585 return NULL;
7588 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7590 if (!model || !model->dim)
7591 return NULL;
7592 return isl_basic_map_identity(isl_space_copy(model->dim));
7595 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7597 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7600 struct isl_map *isl_map_identity_like(struct isl_map *model)
7602 if (!model || !model->dim)
7603 return NULL;
7604 return isl_map_identity(isl_space_copy(model->dim));
7607 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7609 if (!model || !model->dim)
7610 return NULL;
7611 return isl_map_identity(isl_space_copy(model->dim));
7614 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7616 isl_space *dim = isl_set_get_space(set);
7617 isl_map *id;
7618 id = isl_map_identity(isl_space_map_from_set(dim));
7619 return isl_map_intersect_range(id, set);
7622 /* Construct a basic set with all set dimensions having only non-negative
7623 * values.
7625 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7626 __isl_take isl_space *space)
7628 int i;
7629 unsigned nparam;
7630 unsigned dim;
7631 struct isl_basic_set *bset;
7633 if (!space)
7634 return NULL;
7635 nparam = space->nparam;
7636 dim = space->n_out;
7637 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7638 if (!bset)
7639 return NULL;
7640 for (i = 0; i < dim; ++i) {
7641 int k = isl_basic_set_alloc_inequality(bset);
7642 if (k < 0)
7643 goto error;
7644 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7645 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7647 return bset;
7648 error:
7649 isl_basic_set_free(bset);
7650 return NULL;
7653 /* Construct the half-space x_pos >= 0.
7655 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7656 int pos)
7658 int k;
7659 isl_basic_set *nonneg;
7661 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7662 k = isl_basic_set_alloc_inequality(nonneg);
7663 if (k < 0)
7664 goto error;
7665 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7666 isl_int_set_si(nonneg->ineq[k][pos], 1);
7668 return isl_basic_set_finalize(nonneg);
7669 error:
7670 isl_basic_set_free(nonneg);
7671 return NULL;
7674 /* Construct the half-space x_pos <= -1.
7676 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7678 int k;
7679 isl_basic_set *neg;
7681 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7682 k = isl_basic_set_alloc_inequality(neg);
7683 if (k < 0)
7684 goto error;
7685 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7686 isl_int_set_si(neg->ineq[k][0], -1);
7687 isl_int_set_si(neg->ineq[k][pos], -1);
7689 return isl_basic_set_finalize(neg);
7690 error:
7691 isl_basic_set_free(neg);
7692 return NULL;
7695 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7696 enum isl_dim_type type, unsigned first, unsigned n)
7698 int i;
7699 isl_basic_set *nonneg;
7700 isl_basic_set *neg;
7702 if (!set)
7703 return NULL;
7704 if (n == 0)
7705 return set;
7707 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7709 for (i = 0; i < n; ++i) {
7710 nonneg = nonneg_halfspace(isl_set_get_space(set),
7711 pos(set->dim, type) + first + i);
7712 neg = neg_halfspace(isl_set_get_space(set),
7713 pos(set->dim, type) + first + i);
7715 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7718 return set;
7719 error:
7720 isl_set_free(set);
7721 return NULL;
7724 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7725 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7726 void *user)
7728 isl_set *half;
7730 if (!set)
7731 return -1;
7732 if (isl_set_plain_is_empty(set)) {
7733 isl_set_free(set);
7734 return 0;
7736 if (first == len)
7737 return fn(set, signs, user);
7739 signs[first] = 1;
7740 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7741 1 + first));
7742 half = isl_set_intersect(half, isl_set_copy(set));
7743 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7744 goto error;
7746 signs[first] = -1;
7747 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7748 1 + first));
7749 half = isl_set_intersect(half, set);
7750 return foreach_orthant(half, signs, first + 1, len, fn, user);
7751 error:
7752 isl_set_free(set);
7753 return -1;
7756 /* Call "fn" on the intersections of "set" with each of the orthants
7757 * (except for obviously empty intersections). The orthant is identified
7758 * by the signs array, with each entry having value 1 or -1 according
7759 * to the sign of the corresponding variable.
7761 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7762 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7763 void *user)
7765 unsigned nparam;
7766 unsigned nvar;
7767 int *signs;
7768 int r;
7770 if (!set)
7771 return -1;
7772 if (isl_set_plain_is_empty(set))
7773 return 0;
7775 nparam = isl_set_dim(set, isl_dim_param);
7776 nvar = isl_set_dim(set, isl_dim_set);
7778 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7780 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7781 fn, user);
7783 free(signs);
7785 return r;
7788 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7790 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7793 int isl_basic_map_is_subset(
7794 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7796 int is_subset;
7797 struct isl_map *map1;
7798 struct isl_map *map2;
7800 if (!bmap1 || !bmap2)
7801 return -1;
7803 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7804 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7806 is_subset = isl_map_is_subset(map1, map2);
7808 isl_map_free(map1);
7809 isl_map_free(map2);
7811 return is_subset;
7814 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7815 __isl_keep isl_basic_set *bset2)
7817 return isl_basic_map_is_subset(bset1, bset2);
7820 int isl_basic_map_is_equal(
7821 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7823 int is_subset;
7825 if (!bmap1 || !bmap2)
7826 return -1;
7827 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7828 if (is_subset != 1)
7829 return is_subset;
7830 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7831 return is_subset;
7834 int isl_basic_set_is_equal(
7835 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7837 return isl_basic_map_is_equal(
7838 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7841 int isl_map_is_empty(struct isl_map *map)
7843 int i;
7844 int is_empty;
7846 if (!map)
7847 return -1;
7848 for (i = 0; i < map->n; ++i) {
7849 is_empty = isl_basic_map_is_empty(map->p[i]);
7850 if (is_empty < 0)
7851 return -1;
7852 if (!is_empty)
7853 return 0;
7855 return 1;
7858 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7860 return map ? map->n == 0 : -1;
7863 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7865 return isl_map_plain_is_empty(map);
7868 int isl_set_plain_is_empty(struct isl_set *set)
7870 return set ? set->n == 0 : -1;
7873 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7875 return isl_set_plain_is_empty(set);
7878 int isl_set_is_empty(struct isl_set *set)
7880 return isl_map_is_empty((struct isl_map *)set);
7883 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7885 if (!map1 || !map2)
7886 return -1;
7888 return isl_space_is_equal(map1->dim, map2->dim);
7891 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7893 if (!set1 || !set2)
7894 return -1;
7896 return isl_space_is_equal(set1->dim, set2->dim);
7899 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7901 int is_subset;
7903 if (!map1 || !map2)
7904 return -1;
7905 is_subset = isl_map_is_subset(map1, map2);
7906 if (is_subset != 1)
7907 return is_subset;
7908 is_subset = isl_map_is_subset(map2, map1);
7909 return is_subset;
7912 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7914 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7917 int isl_basic_map_is_strict_subset(
7918 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7920 int is_subset;
7922 if (!bmap1 || !bmap2)
7923 return -1;
7924 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7925 if (is_subset != 1)
7926 return is_subset;
7927 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7928 if (is_subset == -1)
7929 return is_subset;
7930 return !is_subset;
7933 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7935 int is_subset;
7937 if (!map1 || !map2)
7938 return -1;
7939 is_subset = isl_map_is_subset(map1, map2);
7940 if (is_subset != 1)
7941 return is_subset;
7942 is_subset = isl_map_is_subset(map2, map1);
7943 if (is_subset == -1)
7944 return is_subset;
7945 return !is_subset;
7948 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7950 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7953 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7955 if (!bmap)
7956 return -1;
7957 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7960 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7962 if (!bset)
7963 return -1;
7964 return bset->n_eq == 0 && bset->n_ineq == 0;
7967 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7969 int i;
7971 if (!map)
7972 return -1;
7974 for (i = 0; i < map->n; ++i) {
7975 int r = isl_basic_map_is_universe(map->p[i]);
7976 if (r < 0 || r)
7977 return r;
7980 return 0;
7983 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7985 return isl_map_plain_is_universe((isl_map *) set);
7988 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7990 return isl_set_plain_is_universe(set);
7993 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7995 struct isl_basic_set *bset = NULL;
7996 struct isl_vec *sample = NULL;
7997 int empty;
7998 unsigned total;
8000 if (!bmap)
8001 return -1;
8003 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8004 return 1;
8006 if (isl_basic_map_is_universe(bmap))
8007 return 0;
8009 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8010 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8011 copy = isl_basic_map_remove_redundancies(copy);
8012 empty = isl_basic_map_plain_is_empty(copy);
8013 isl_basic_map_free(copy);
8014 return empty;
8017 total = 1 + isl_basic_map_total_dim(bmap);
8018 if (bmap->sample && bmap->sample->size == total) {
8019 int contains = isl_basic_map_contains(bmap, bmap->sample);
8020 if (contains < 0)
8021 return -1;
8022 if (contains)
8023 return 0;
8025 isl_vec_free(bmap->sample);
8026 bmap->sample = NULL;
8027 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8028 if (!bset)
8029 return -1;
8030 sample = isl_basic_set_sample_vec(bset);
8031 if (!sample)
8032 return -1;
8033 empty = sample->size == 0;
8034 isl_vec_free(bmap->sample);
8035 bmap->sample = sample;
8036 if (empty)
8037 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8039 return empty;
8042 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8044 if (!bmap)
8045 return -1;
8046 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8049 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
8051 return isl_basic_map_plain_is_empty(bmap);
8054 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8056 if (!bset)
8057 return -1;
8058 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8061 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
8063 return isl_basic_set_plain_is_empty(bset);
8066 int isl_basic_set_is_empty(struct isl_basic_set *bset)
8068 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8071 struct isl_map *isl_basic_map_union(
8072 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8074 struct isl_map *map;
8075 if (!bmap1 || !bmap2)
8076 goto error;
8078 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8080 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8081 if (!map)
8082 goto error;
8083 map = isl_map_add_basic_map(map, bmap1);
8084 map = isl_map_add_basic_map(map, bmap2);
8085 return map;
8086 error:
8087 isl_basic_map_free(bmap1);
8088 isl_basic_map_free(bmap2);
8089 return NULL;
8092 struct isl_set *isl_basic_set_union(
8093 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8095 return (struct isl_set *)isl_basic_map_union(
8096 (struct isl_basic_map *)bset1,
8097 (struct isl_basic_map *)bset2);
8100 /* Order divs such that any div only depends on previous divs */
8101 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8103 int i;
8104 unsigned off;
8106 if (!bmap)
8107 return NULL;
8109 off = isl_space_dim(bmap->dim, isl_dim_all);
8111 for (i = 0; i < bmap->n_div; ++i) {
8112 int pos;
8113 if (isl_int_is_zero(bmap->div[i][0]))
8114 continue;
8115 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8116 bmap->n_div-i);
8117 if (pos == -1)
8118 continue;
8119 isl_basic_map_swap_div(bmap, i, i + pos);
8120 --i;
8122 return bmap;
8125 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8127 return (struct isl_basic_set *)
8128 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8131 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8133 int i;
8135 if (!map)
8136 return 0;
8138 for (i = 0; i < map->n; ++i) {
8139 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8140 if (!map->p[i])
8141 goto error;
8144 return map;
8145 error:
8146 isl_map_free(map);
8147 return NULL;
8150 /* Apply the expansion computed by isl_merge_divs.
8151 * The expansion itself is given by "exp" while the resulting
8152 * list of divs is given by "div".
8154 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8155 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8157 int i, j;
8158 int n_div;
8160 bset = isl_basic_set_cow(bset);
8161 if (!bset || !div)
8162 goto error;
8164 if (div->n_row < bset->n_div)
8165 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8166 "not an expansion", goto error);
8168 n_div = bset->n_div;
8169 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8170 div->n_row - n_div, 0,
8171 2 * (div->n_row - n_div));
8173 for (i = n_div; i < div->n_row; ++i)
8174 if (isl_basic_set_alloc_div(bset) < 0)
8175 goto error;
8177 j = n_div - 1;
8178 for (i = div->n_row - 1; i >= 0; --i) {
8179 if (j >= 0 && exp[j] == i) {
8180 if (i != j)
8181 isl_basic_map_swap_div(bset, i, j);
8182 j--;
8183 } else {
8184 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8185 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8186 goto error;
8190 isl_mat_free(div);
8191 return bset;
8192 error:
8193 isl_basic_set_free(bset);
8194 isl_mat_free(div);
8195 return NULL;
8198 /* Look for a div in dst that corresponds to the div "div" in src.
8199 * The divs before "div" in src and dst are assumed to be the same.
8201 * Returns -1 if no corresponding div was found and the position
8202 * of the corresponding div in dst otherwise.
8204 static int find_div(struct isl_basic_map *dst,
8205 struct isl_basic_map *src, unsigned div)
8207 int i;
8209 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8211 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8212 for (i = div; i < dst->n_div; ++i)
8213 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8214 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8215 dst->n_div - div) == -1)
8216 return i;
8217 return -1;
8220 struct isl_basic_map *isl_basic_map_align_divs(
8221 struct isl_basic_map *dst, struct isl_basic_map *src)
8223 int i;
8224 unsigned total;
8226 if (!dst || !src)
8227 goto error;
8229 if (src->n_div == 0)
8230 return dst;
8232 for (i = 0; i < src->n_div; ++i)
8233 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
8235 src = isl_basic_map_order_divs(src);
8236 dst = isl_basic_map_cow(dst);
8237 if (!dst)
8238 return NULL;
8239 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
8240 src->n_div, 0, 2 * src->n_div);
8241 if (!dst)
8242 return NULL;
8243 total = isl_space_dim(src->dim, isl_dim_all);
8244 for (i = 0; i < src->n_div; ++i) {
8245 int j = find_div(dst, src, i);
8246 if (j < 0) {
8247 j = isl_basic_map_alloc_div(dst);
8248 if (j < 0)
8249 goto error;
8250 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8251 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8252 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8253 goto error;
8255 if (j != i)
8256 isl_basic_map_swap_div(dst, i, j);
8258 return dst;
8259 error:
8260 isl_basic_map_free(dst);
8261 return NULL;
8264 struct isl_basic_set *isl_basic_set_align_divs(
8265 struct isl_basic_set *dst, struct isl_basic_set *src)
8267 return (struct isl_basic_set *)isl_basic_map_align_divs(
8268 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8271 struct isl_map *isl_map_align_divs(struct isl_map *map)
8273 int i;
8275 if (!map)
8276 return NULL;
8277 if (map->n == 0)
8278 return map;
8279 map = isl_map_compute_divs(map);
8280 map = isl_map_cow(map);
8281 if (!map)
8282 return NULL;
8284 for (i = 1; i < map->n; ++i)
8285 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8286 for (i = 1; i < map->n; ++i) {
8287 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8288 if (!map->p[i])
8289 return isl_map_free(map);
8292 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8293 return map;
8296 struct isl_set *isl_set_align_divs(struct isl_set *set)
8298 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8301 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8302 __isl_take isl_map *map)
8304 if (!set || !map)
8305 goto error;
8306 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8307 map = isl_map_intersect_domain(map, set);
8308 set = isl_map_range(map);
8309 return set;
8310 error:
8311 isl_set_free(set);
8312 isl_map_free(map);
8313 return NULL;
8316 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8317 __isl_take isl_map *map)
8319 return isl_map_align_params_map_map_and(set, map, &set_apply);
8322 /* There is no need to cow as removing empty parts doesn't change
8323 * the meaning of the set.
8325 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8327 int i;
8329 if (!map)
8330 return NULL;
8332 for (i = map->n - 1; i >= 0; --i)
8333 remove_if_empty(map, i);
8335 return map;
8338 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8340 return (struct isl_set *)
8341 isl_map_remove_empty_parts((struct isl_map *)set);
8344 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8346 struct isl_basic_map *bmap;
8347 if (!map || map->n == 0)
8348 return NULL;
8349 bmap = map->p[map->n-1];
8350 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8351 return isl_basic_map_copy(bmap);
8354 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8356 return (struct isl_basic_set *)
8357 isl_map_copy_basic_map((struct isl_map *)set);
8360 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8361 __isl_keep isl_basic_map *bmap)
8363 int i;
8365 if (!map || !bmap)
8366 goto error;
8367 for (i = map->n-1; i >= 0; --i) {
8368 if (map->p[i] != bmap)
8369 continue;
8370 map = isl_map_cow(map);
8371 if (!map)
8372 goto error;
8373 isl_basic_map_free(map->p[i]);
8374 if (i != map->n-1) {
8375 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8376 map->p[i] = map->p[map->n-1];
8378 map->n--;
8379 return map;
8381 return map;
8382 error:
8383 isl_map_free(map);
8384 return NULL;
8387 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8388 struct isl_basic_set *bset)
8390 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8391 (struct isl_basic_map *)bset);
8394 /* Given two basic sets bset1 and bset2, compute the maximal difference
8395 * between the values of dimension pos in bset1 and those in bset2
8396 * for any common value of the parameters and dimensions preceding pos.
8398 static enum isl_lp_result basic_set_maximal_difference_at(
8399 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8400 int pos, isl_int *opt)
8402 isl_space *dims;
8403 struct isl_basic_map *bmap1 = NULL;
8404 struct isl_basic_map *bmap2 = NULL;
8405 struct isl_ctx *ctx;
8406 struct isl_vec *obj;
8407 unsigned total;
8408 unsigned nparam;
8409 unsigned dim1, dim2;
8410 enum isl_lp_result res;
8412 if (!bset1 || !bset2)
8413 return isl_lp_error;
8415 nparam = isl_basic_set_n_param(bset1);
8416 dim1 = isl_basic_set_n_dim(bset1);
8417 dim2 = isl_basic_set_n_dim(bset2);
8418 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8419 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8420 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8421 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8422 if (!bmap1 || !bmap2)
8423 goto error;
8424 bmap1 = isl_basic_map_cow(bmap1);
8425 bmap1 = isl_basic_map_extend(bmap1, nparam,
8426 pos, (dim1 - pos) + (dim2 - pos),
8427 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8428 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8429 if (!bmap1)
8430 goto error2;
8431 total = isl_basic_map_total_dim(bmap1);
8432 ctx = bmap1->ctx;
8433 obj = isl_vec_alloc(ctx, 1 + total);
8434 if (!obj)
8435 goto error2;
8436 isl_seq_clr(obj->block.data, 1 + total);
8437 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8438 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8439 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8440 opt, NULL, NULL);
8441 isl_basic_map_free(bmap1);
8442 isl_vec_free(obj);
8443 return res;
8444 error:
8445 isl_basic_map_free(bmap2);
8446 error2:
8447 isl_basic_map_free(bmap1);
8448 return isl_lp_error;
8451 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8452 * for any common value of the parameters and dimensions preceding pos
8453 * in both basic sets, the values of dimension pos in bset1 are
8454 * smaller or larger than those in bset2.
8456 * Returns
8457 * 1 if bset1 follows bset2
8458 * -1 if bset1 precedes bset2
8459 * 0 if bset1 and bset2 are incomparable
8460 * -2 if some error occurred.
8462 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8463 struct isl_basic_set *bset2, int pos)
8465 isl_int opt;
8466 enum isl_lp_result res;
8467 int cmp;
8469 isl_int_init(opt);
8471 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8473 if (res == isl_lp_empty)
8474 cmp = 0;
8475 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8476 res == isl_lp_unbounded)
8477 cmp = 1;
8478 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8479 cmp = -1;
8480 else
8481 cmp = -2;
8483 isl_int_clear(opt);
8484 return cmp;
8487 /* Given two basic sets bset1 and bset2, check whether
8488 * for any common value of the parameters and dimensions preceding pos
8489 * there is a value of dimension pos in bset1 that is larger
8490 * than a value of the same dimension in bset2.
8492 * Return
8493 * 1 if there exists such a pair
8494 * 0 if there is no such pair, but there is a pair of equal values
8495 * -1 otherwise
8496 * -2 if some error occurred.
8498 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8499 __isl_keep isl_basic_set *bset2, int pos)
8501 isl_int opt;
8502 enum isl_lp_result res;
8503 int cmp;
8505 isl_int_init(opt);
8507 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8509 if (res == isl_lp_empty)
8510 cmp = -1;
8511 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8512 res == isl_lp_unbounded)
8513 cmp = 1;
8514 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8515 cmp = -1;
8516 else if (res == isl_lp_ok)
8517 cmp = 0;
8518 else
8519 cmp = -2;
8521 isl_int_clear(opt);
8522 return cmp;
8525 /* Given two sets set1 and set2, check whether
8526 * for any common value of the parameters and dimensions preceding pos
8527 * there is a value of dimension pos in set1 that is larger
8528 * than a value of the same dimension in set2.
8530 * Return
8531 * 1 if there exists such a pair
8532 * 0 if there is no such pair, but there is a pair of equal values
8533 * -1 otherwise
8534 * -2 if some error occurred.
8536 int isl_set_follows_at(__isl_keep isl_set *set1,
8537 __isl_keep isl_set *set2, int pos)
8539 int i, j;
8540 int follows = -1;
8542 if (!set1 || !set2)
8543 return -2;
8545 for (i = 0; i < set1->n; ++i)
8546 for (j = 0; j < set2->n; ++j) {
8547 int f;
8548 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8549 if (f == 1 || f == -2)
8550 return f;
8551 if (f > follows)
8552 follows = f;
8555 return follows;
8558 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8559 unsigned pos, isl_int *val)
8561 int i;
8562 int d;
8563 unsigned total;
8565 if (!bmap)
8566 return -1;
8567 total = isl_basic_map_total_dim(bmap);
8568 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8569 for (; d+1 > pos; --d)
8570 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8571 break;
8572 if (d != pos)
8573 continue;
8574 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8575 return 0;
8576 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8577 return 0;
8578 if (!isl_int_is_one(bmap->eq[i][1+d]))
8579 return 0;
8580 if (val)
8581 isl_int_neg(*val, bmap->eq[i][0]);
8582 return 1;
8584 return 0;
8587 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8588 unsigned pos, isl_int *val)
8590 int i;
8591 isl_int v;
8592 isl_int tmp;
8593 int fixed;
8595 if (!map)
8596 return -1;
8597 if (map->n == 0)
8598 return 0;
8599 if (map->n == 1)
8600 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8601 isl_int_init(v);
8602 isl_int_init(tmp);
8603 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8604 for (i = 1; fixed == 1 && i < map->n; ++i) {
8605 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8606 if (fixed == 1 && isl_int_ne(tmp, v))
8607 fixed = 0;
8609 if (val)
8610 isl_int_set(*val, v);
8611 isl_int_clear(tmp);
8612 isl_int_clear(v);
8613 return fixed;
8616 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8617 unsigned pos, isl_int *val)
8619 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8620 pos, val);
8623 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8624 isl_int *val)
8626 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8629 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8630 enum isl_dim_type type, unsigned pos, isl_int *val)
8632 if (pos >= isl_basic_map_dim(bmap, type))
8633 return -1;
8634 return isl_basic_map_plain_has_fixed_var(bmap,
8635 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8638 /* If "bmap" obviously lies on a hyperplane where the given dimension
8639 * has a fixed value, then return that value.
8640 * Otherwise return NaN.
8642 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8643 __isl_keep isl_basic_map *bmap,
8644 enum isl_dim_type type, unsigned pos)
8646 isl_ctx *ctx;
8647 isl_val *v;
8648 int fixed;
8650 if (!bmap)
8651 return NULL;
8652 ctx = isl_basic_map_get_ctx(bmap);
8653 v = isl_val_alloc(ctx);
8654 if (!v)
8655 return NULL;
8656 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8657 if (fixed < 0)
8658 return isl_val_free(v);
8659 if (fixed) {
8660 isl_int_set_si(v->d, 1);
8661 return v;
8663 isl_val_free(v);
8664 return isl_val_nan(ctx);
8667 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8668 enum isl_dim_type type, unsigned pos, isl_int *val)
8670 if (pos >= isl_map_dim(map, type))
8671 return -1;
8672 return isl_map_plain_has_fixed_var(map,
8673 map_offset(map, type) - 1 + pos, val);
8676 /* If "map" obviously lies on a hyperplane where the given dimension
8677 * has a fixed value, then return that value.
8678 * Otherwise return NaN.
8680 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8681 enum isl_dim_type type, unsigned pos)
8683 isl_ctx *ctx;
8684 isl_val *v;
8685 int fixed;
8687 if (!map)
8688 return NULL;
8689 ctx = isl_map_get_ctx(map);
8690 v = isl_val_alloc(ctx);
8691 if (!v)
8692 return NULL;
8693 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8694 if (fixed < 0)
8695 return isl_val_free(v);
8696 if (fixed) {
8697 isl_int_set_si(v->d, 1);
8698 return v;
8700 isl_val_free(v);
8701 return isl_val_nan(ctx);
8704 /* If "set" obviously lies on a hyperplane where the given dimension
8705 * has a fixed value, then return that value.
8706 * Otherwise return NaN.
8708 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8709 enum isl_dim_type type, unsigned pos)
8711 return isl_map_plain_get_val_if_fixed(set, type, pos);
8714 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8715 enum isl_dim_type type, unsigned pos, isl_int *val)
8717 return isl_map_plain_is_fixed(set, type, pos, val);
8720 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8721 enum isl_dim_type type, unsigned pos, isl_int *val)
8723 return isl_map_plain_is_fixed(map, type, pos, val);
8726 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8727 * then return this fixed value in *val.
8729 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8730 unsigned dim, isl_int *val)
8732 return isl_basic_set_plain_has_fixed_var(bset,
8733 isl_basic_set_n_param(bset) + dim, val);
8736 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8737 * then return this fixed value in *val.
8739 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8740 unsigned dim, isl_int *val)
8742 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8745 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8746 unsigned dim, isl_int *val)
8748 return isl_set_plain_dim_is_fixed(set, dim, val);
8751 /* Check if input variable in has fixed value and if so and if val is not NULL,
8752 * then return this fixed value in *val.
8754 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8755 unsigned in, isl_int *val)
8757 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8760 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8761 * and if val is not NULL, then return this lower bound in *val.
8763 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8764 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8766 int i, i_eq = -1, i_ineq = -1;
8767 isl_int *c;
8768 unsigned total;
8769 unsigned nparam;
8771 if (!bset)
8772 return -1;
8773 total = isl_basic_set_total_dim(bset);
8774 nparam = isl_basic_set_n_param(bset);
8775 for (i = 0; i < bset->n_eq; ++i) {
8776 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8777 continue;
8778 if (i_eq != -1)
8779 return 0;
8780 i_eq = i;
8782 for (i = 0; i < bset->n_ineq; ++i) {
8783 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8784 continue;
8785 if (i_eq != -1 || i_ineq != -1)
8786 return 0;
8787 i_ineq = i;
8789 if (i_eq == -1 && i_ineq == -1)
8790 return 0;
8791 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8792 /* The coefficient should always be one due to normalization. */
8793 if (!isl_int_is_one(c[1+nparam+dim]))
8794 return 0;
8795 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8796 return 0;
8797 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8798 total - nparam - dim - 1) != -1)
8799 return 0;
8800 if (val)
8801 isl_int_neg(*val, c[0]);
8802 return 1;
8805 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8806 unsigned dim, isl_int *val)
8808 int i;
8809 isl_int v;
8810 isl_int tmp;
8811 int fixed;
8813 if (!set)
8814 return -1;
8815 if (set->n == 0)
8816 return 0;
8817 if (set->n == 1)
8818 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8819 dim, val);
8820 isl_int_init(v);
8821 isl_int_init(tmp);
8822 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8823 dim, &v);
8824 for (i = 1; fixed == 1 && i < set->n; ++i) {
8825 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8826 dim, &tmp);
8827 if (fixed == 1 && isl_int_ne(tmp, v))
8828 fixed = 0;
8830 if (val)
8831 isl_int_set(*val, v);
8832 isl_int_clear(tmp);
8833 isl_int_clear(v);
8834 return fixed;
8837 struct constraint {
8838 unsigned size;
8839 isl_int *c;
8842 /* uset_gist depends on constraints without existentially quantified
8843 * variables sorting first.
8845 static int qsort_constraint_cmp(const void *p1, const void *p2)
8847 const struct constraint *c1 = (const struct constraint *)p1;
8848 const struct constraint *c2 = (const struct constraint *)p2;
8849 int l1, l2;
8850 unsigned size = isl_min(c1->size, c2->size);
8852 l1 = isl_seq_last_non_zero(c1->c + 1, size);
8853 l2 = isl_seq_last_non_zero(c2->c + 1, size);
8855 if (l1 != l2)
8856 return l1 - l2;
8858 return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8861 static struct isl_basic_map *isl_basic_map_sort_constraints(
8862 struct isl_basic_map *bmap)
8864 int i;
8865 struct constraint *c;
8866 unsigned total;
8868 if (!bmap)
8869 return NULL;
8870 if (bmap->n_ineq == 0)
8871 return bmap;
8872 total = isl_basic_map_total_dim(bmap);
8873 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8874 if (!c)
8875 goto error;
8876 for (i = 0; i < bmap->n_ineq; ++i) {
8877 c[i].size = total;
8878 c[i].c = bmap->ineq[i];
8880 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8881 for (i = 0; i < bmap->n_ineq; ++i)
8882 bmap->ineq[i] = c[i].c;
8883 free(c);
8884 return bmap;
8885 error:
8886 isl_basic_map_free(bmap);
8887 return NULL;
8890 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8891 __isl_take isl_basic_set *bset)
8893 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8894 (struct isl_basic_map *)bset);
8897 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8899 if (!bmap)
8900 return NULL;
8901 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8902 return bmap;
8903 bmap = isl_basic_map_remove_redundancies(bmap);
8904 bmap = isl_basic_map_sort_constraints(bmap);
8905 if (bmap)
8906 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8907 return bmap;
8910 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8912 return (struct isl_basic_set *)isl_basic_map_normalize(
8913 (struct isl_basic_map *)bset);
8916 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8917 const __isl_keep isl_basic_map *bmap2)
8919 int i, cmp;
8920 unsigned total;
8922 if (bmap1 == bmap2)
8923 return 0;
8924 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8925 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8926 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8927 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8928 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8929 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8930 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8931 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8932 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8933 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8934 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8935 return 0;
8936 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8937 return 1;
8938 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8939 return -1;
8940 if (bmap1->n_eq != bmap2->n_eq)
8941 return bmap1->n_eq - bmap2->n_eq;
8942 if (bmap1->n_ineq != bmap2->n_ineq)
8943 return bmap1->n_ineq - bmap2->n_ineq;
8944 if (bmap1->n_div != bmap2->n_div)
8945 return bmap1->n_div - bmap2->n_div;
8946 total = isl_basic_map_total_dim(bmap1);
8947 for (i = 0; i < bmap1->n_eq; ++i) {
8948 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8949 if (cmp)
8950 return cmp;
8952 for (i = 0; i < bmap1->n_ineq; ++i) {
8953 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8954 if (cmp)
8955 return cmp;
8957 for (i = 0; i < bmap1->n_div; ++i) {
8958 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8959 if (cmp)
8960 return cmp;
8962 return 0;
8965 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8966 const __isl_keep isl_basic_set *bset2)
8968 return isl_basic_map_plain_cmp(bset1, bset2);
8971 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8973 int i, cmp;
8975 if (set1 == set2)
8976 return 0;
8977 if (set1->n != set2->n)
8978 return set1->n - set2->n;
8980 for (i = 0; i < set1->n; ++i) {
8981 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8982 if (cmp)
8983 return cmp;
8986 return 0;
8989 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8990 __isl_keep isl_basic_map *bmap2)
8992 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8995 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8996 __isl_keep isl_basic_set *bset2)
8998 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8999 (isl_basic_map *)bset2);
9002 static int qsort_bmap_cmp(const void *p1, const void *p2)
9004 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9005 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9007 return isl_basic_map_plain_cmp(bmap1, bmap2);
9010 /* We normalize in place, but if anything goes wrong we need
9011 * to return NULL, so we need to make sure we don't change the
9012 * meaning of any possible other copies of map.
9014 struct isl_map *isl_map_normalize(struct isl_map *map)
9016 int i, j;
9017 struct isl_basic_map *bmap;
9019 if (!map)
9020 return NULL;
9021 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9022 return map;
9023 for (i = 0; i < map->n; ++i) {
9024 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9025 if (!bmap)
9026 goto error;
9027 isl_basic_map_free(map->p[i]);
9028 map->p[i] = bmap;
9030 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9031 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9032 map = isl_map_remove_empty_parts(map);
9033 if (!map)
9034 return NULL;
9035 for (i = map->n - 1; i >= 1; --i) {
9036 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
9037 continue;
9038 isl_basic_map_free(map->p[i-1]);
9039 for (j = i; j < map->n; ++j)
9040 map->p[j-1] = map->p[j];
9041 map->n--;
9043 return map;
9044 error:
9045 isl_map_free(map);
9046 return NULL;
9050 struct isl_set *isl_set_normalize(struct isl_set *set)
9052 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9055 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9057 int i;
9058 int equal;
9060 if (!map1 || !map2)
9061 return -1;
9063 if (map1 == map2)
9064 return 1;
9065 if (!isl_space_is_equal(map1->dim, map2->dim))
9066 return 0;
9068 map1 = isl_map_copy(map1);
9069 map2 = isl_map_copy(map2);
9070 map1 = isl_map_normalize(map1);
9071 map2 = isl_map_normalize(map2);
9072 if (!map1 || !map2)
9073 goto error;
9074 equal = map1->n == map2->n;
9075 for (i = 0; equal && i < map1->n; ++i) {
9076 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9077 if (equal < 0)
9078 goto error;
9080 isl_map_free(map1);
9081 isl_map_free(map2);
9082 return equal;
9083 error:
9084 isl_map_free(map1);
9085 isl_map_free(map2);
9086 return -1;
9089 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9091 return isl_map_plain_is_equal(map1, map2);
9094 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9096 return isl_map_plain_is_equal((struct isl_map *)set1,
9097 (struct isl_map *)set2);
9100 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9102 return isl_set_plain_is_equal(set1, set2);
9105 /* Return an interval that ranges from min to max (inclusive)
9107 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9108 isl_int min, isl_int max)
9110 int k;
9111 struct isl_basic_set *bset = NULL;
9113 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9114 if (!bset)
9115 goto error;
9117 k = isl_basic_set_alloc_inequality(bset);
9118 if (k < 0)
9119 goto error;
9120 isl_int_set_si(bset->ineq[k][1], 1);
9121 isl_int_neg(bset->ineq[k][0], min);
9123 k = isl_basic_set_alloc_inequality(bset);
9124 if (k < 0)
9125 goto error;
9126 isl_int_set_si(bset->ineq[k][1], -1);
9127 isl_int_set(bset->ineq[k][0], max);
9129 return bset;
9130 error:
9131 isl_basic_set_free(bset);
9132 return NULL;
9135 /* Return the Cartesian product of the basic sets in list (in the given order).
9137 __isl_give isl_basic_set *isl_basic_set_list_product(
9138 __isl_take struct isl_basic_set_list *list)
9140 int i;
9141 unsigned dim;
9142 unsigned nparam;
9143 unsigned extra;
9144 unsigned n_eq;
9145 unsigned n_ineq;
9146 struct isl_basic_set *product = NULL;
9148 if (!list)
9149 goto error;
9150 isl_assert(list->ctx, list->n > 0, goto error);
9151 isl_assert(list->ctx, list->p[0], goto error);
9152 nparam = isl_basic_set_n_param(list->p[0]);
9153 dim = isl_basic_set_n_dim(list->p[0]);
9154 extra = list->p[0]->n_div;
9155 n_eq = list->p[0]->n_eq;
9156 n_ineq = list->p[0]->n_ineq;
9157 for (i = 1; i < list->n; ++i) {
9158 isl_assert(list->ctx, list->p[i], goto error);
9159 isl_assert(list->ctx,
9160 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9161 dim += isl_basic_set_n_dim(list->p[i]);
9162 extra += list->p[i]->n_div;
9163 n_eq += list->p[i]->n_eq;
9164 n_ineq += list->p[i]->n_ineq;
9166 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9167 n_eq, n_ineq);
9168 if (!product)
9169 goto error;
9170 dim = 0;
9171 for (i = 0; i < list->n; ++i) {
9172 isl_basic_set_add_constraints(product,
9173 isl_basic_set_copy(list->p[i]), dim);
9174 dim += isl_basic_set_n_dim(list->p[i]);
9176 isl_basic_set_list_free(list);
9177 return product;
9178 error:
9179 isl_basic_set_free(product);
9180 isl_basic_set_list_free(list);
9181 return NULL;
9184 struct isl_basic_map *isl_basic_map_product(
9185 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9187 isl_space *dim_result = NULL;
9188 struct isl_basic_map *bmap;
9189 unsigned in1, in2, out1, out2, nparam, total, pos;
9190 struct isl_dim_map *dim_map1, *dim_map2;
9192 if (!bmap1 || !bmap2)
9193 goto error;
9195 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9196 bmap2->dim, isl_dim_param), goto error);
9197 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9198 isl_space_copy(bmap2->dim));
9200 in1 = isl_basic_map_n_in(bmap1);
9201 in2 = isl_basic_map_n_in(bmap2);
9202 out1 = isl_basic_map_n_out(bmap1);
9203 out2 = isl_basic_map_n_out(bmap2);
9204 nparam = isl_basic_map_n_param(bmap1);
9206 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9207 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9208 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9209 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9210 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9211 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9212 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9213 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9214 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9215 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9216 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9218 bmap = isl_basic_map_alloc_space(dim_result,
9219 bmap1->n_div + bmap2->n_div,
9220 bmap1->n_eq + bmap2->n_eq,
9221 bmap1->n_ineq + bmap2->n_ineq);
9222 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9223 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9224 bmap = isl_basic_map_simplify(bmap);
9225 return isl_basic_map_finalize(bmap);
9226 error:
9227 isl_basic_map_free(bmap1);
9228 isl_basic_map_free(bmap2);
9229 return NULL;
9232 __isl_give isl_basic_map *isl_basic_map_flat_product(
9233 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9235 isl_basic_map *prod;
9237 prod = isl_basic_map_product(bmap1, bmap2);
9238 prod = isl_basic_map_flatten(prod);
9239 return prod;
9242 __isl_give isl_basic_set *isl_basic_set_flat_product(
9243 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9245 return isl_basic_map_flat_range_product(bset1, bset2);
9248 __isl_give isl_basic_map *isl_basic_map_domain_product(
9249 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9251 isl_space *space_result = NULL;
9252 isl_basic_map *bmap;
9253 unsigned in1, in2, out, nparam, total, pos;
9254 struct isl_dim_map *dim_map1, *dim_map2;
9256 if (!bmap1 || !bmap2)
9257 goto error;
9259 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9260 isl_space_copy(bmap2->dim));
9262 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9263 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9264 out = isl_basic_map_dim(bmap1, isl_dim_out);
9265 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9267 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9268 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9269 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9270 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9271 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9272 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9273 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9274 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9275 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9276 isl_dim_map_div(dim_map1, bmap1, pos += out);
9277 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9279 bmap = isl_basic_map_alloc_space(space_result,
9280 bmap1->n_div + bmap2->n_div,
9281 bmap1->n_eq + bmap2->n_eq,
9282 bmap1->n_ineq + bmap2->n_ineq);
9283 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9284 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9285 bmap = isl_basic_map_simplify(bmap);
9286 return isl_basic_map_finalize(bmap);
9287 error:
9288 isl_basic_map_free(bmap1);
9289 isl_basic_map_free(bmap2);
9290 return NULL;
9293 __isl_give isl_basic_map *isl_basic_map_range_product(
9294 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9296 isl_space *dim_result = NULL;
9297 isl_basic_map *bmap;
9298 unsigned in, out1, out2, nparam, total, pos;
9299 struct isl_dim_map *dim_map1, *dim_map2;
9301 if (!bmap1 || !bmap2)
9302 goto error;
9304 if (!isl_space_match(bmap1->dim, isl_dim_param,
9305 bmap2->dim, isl_dim_param))
9306 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9307 "parameters don't match", goto error);
9309 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9310 isl_space_copy(bmap2->dim));
9312 in = isl_basic_map_dim(bmap1, isl_dim_in);
9313 out1 = isl_basic_map_n_out(bmap1);
9314 out2 = isl_basic_map_n_out(bmap2);
9315 nparam = isl_basic_map_n_param(bmap1);
9317 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9318 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9319 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9320 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9321 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9322 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9323 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9324 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9325 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9326 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9327 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9329 bmap = isl_basic_map_alloc_space(dim_result,
9330 bmap1->n_div + bmap2->n_div,
9331 bmap1->n_eq + bmap2->n_eq,
9332 bmap1->n_ineq + bmap2->n_ineq);
9333 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9334 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9335 bmap = isl_basic_map_simplify(bmap);
9336 return isl_basic_map_finalize(bmap);
9337 error:
9338 isl_basic_map_free(bmap1);
9339 isl_basic_map_free(bmap2);
9340 return NULL;
9343 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9344 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9346 isl_basic_map *prod;
9348 prod = isl_basic_map_range_product(bmap1, bmap2);
9349 prod = isl_basic_map_flatten_range(prod);
9350 return prod;
9353 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9354 __isl_take isl_map *map2,
9355 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
9356 __isl_take isl_space *right),
9357 __isl_give isl_basic_map *(*basic_map_product)(
9358 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
9360 unsigned flags = 0;
9361 struct isl_map *result;
9362 int i, j;
9364 if (!map1 || !map2)
9365 goto error;
9367 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9368 map2->dim, isl_dim_param), goto error);
9370 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9371 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9372 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9374 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
9375 isl_space_copy(map2->dim)),
9376 map1->n * map2->n, flags);
9377 if (!result)
9378 goto error;
9379 for (i = 0; i < map1->n; ++i)
9380 for (j = 0; j < map2->n; ++j) {
9381 struct isl_basic_map *part;
9382 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9383 isl_basic_map_copy(map2->p[j]));
9384 if (isl_basic_map_is_empty(part))
9385 isl_basic_map_free(part);
9386 else
9387 result = isl_map_add_basic_map(result, part);
9388 if (!result)
9389 goto error;
9391 isl_map_free(map1);
9392 isl_map_free(map2);
9393 return result;
9394 error:
9395 isl_map_free(map1);
9396 isl_map_free(map2);
9397 return NULL;
9400 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9402 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9403 __isl_take isl_map *map2)
9405 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
9408 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9409 __isl_take isl_map *map2)
9411 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9414 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9416 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9417 __isl_take isl_map *map2)
9419 isl_map *prod;
9421 prod = isl_map_product(map1, map2);
9422 prod = isl_map_flatten(prod);
9423 return prod;
9426 /* Given two set A and B, construct its Cartesian product A x B.
9428 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9430 return isl_map_range_product(set1, set2);
9433 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9434 __isl_take isl_set *set2)
9436 return isl_map_flat_range_product(set1, set2);
9439 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9441 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9442 __isl_take isl_map *map2)
9444 return map_product(map1, map2, &isl_space_domain_product,
9445 &isl_basic_map_domain_product);
9448 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9450 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9451 __isl_take isl_map *map2)
9453 return map_product(map1, map2, &isl_space_range_product,
9454 &isl_basic_map_range_product);
9457 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9458 __isl_take isl_map *map2)
9460 return isl_map_align_params_map_map_and(map1, map2,
9461 &map_domain_product_aligned);
9464 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9465 __isl_take isl_map *map2)
9467 return isl_map_align_params_map_map_and(map1, map2,
9468 &map_range_product_aligned);
9471 /* Given a map A -> [B -> C], extract the map A -> B.
9473 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9475 isl_space *space;
9476 int total, keep;
9478 if (!map)
9479 return NULL;
9480 if (!isl_space_range_is_wrapping(map->dim))
9481 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9482 "range is not a product", return isl_map_free(map));
9484 space = isl_map_get_space(map);
9485 total = isl_space_dim(space, isl_dim_out);
9486 space = isl_space_range_factor_domain(space);
9487 keep = isl_space_dim(space, isl_dim_out);
9488 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9489 map = isl_map_reset_space(map, space);
9491 return map;
9494 /* Given a map A -> [B -> C], extract the map A -> C.
9496 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9498 isl_space *space;
9499 int total, keep;
9501 if (!map)
9502 return NULL;
9503 if (!isl_space_range_is_wrapping(map->dim))
9504 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9505 "range is not a product", return isl_map_free(map));
9507 space = isl_map_get_space(map);
9508 total = isl_space_dim(space, isl_dim_out);
9509 space = isl_space_range_factor_range(space);
9510 keep = isl_space_dim(space, isl_dim_out);
9511 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9512 map = isl_map_reset_space(map, space);
9514 return map;
9517 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9519 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9520 __isl_take isl_map *map2)
9522 isl_map *prod;
9524 prod = isl_map_domain_product(map1, map2);
9525 prod = isl_map_flatten_domain(prod);
9526 return prod;
9529 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9531 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9532 __isl_take isl_map *map2)
9534 isl_map *prod;
9536 prod = isl_map_range_product(map1, map2);
9537 prod = isl_map_flatten_range(prod);
9538 return prod;
9541 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9543 int i;
9544 uint32_t hash = isl_hash_init();
9545 unsigned total;
9547 if (!bmap)
9548 return 0;
9549 bmap = isl_basic_map_copy(bmap);
9550 bmap = isl_basic_map_normalize(bmap);
9551 if (!bmap)
9552 return 0;
9553 total = isl_basic_map_total_dim(bmap);
9554 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9555 for (i = 0; i < bmap->n_eq; ++i) {
9556 uint32_t c_hash;
9557 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9558 isl_hash_hash(hash, c_hash);
9560 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9561 for (i = 0; i < bmap->n_ineq; ++i) {
9562 uint32_t c_hash;
9563 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9564 isl_hash_hash(hash, c_hash);
9566 isl_hash_byte(hash, bmap->n_div & 0xFF);
9567 for (i = 0; i < bmap->n_div; ++i) {
9568 uint32_t c_hash;
9569 if (isl_int_is_zero(bmap->div[i][0]))
9570 continue;
9571 isl_hash_byte(hash, i & 0xFF);
9572 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9573 isl_hash_hash(hash, c_hash);
9575 isl_basic_map_free(bmap);
9576 return hash;
9579 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9581 return isl_basic_map_get_hash((isl_basic_map *)bset);
9584 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9586 int i;
9587 uint32_t hash;
9589 if (!map)
9590 return 0;
9591 map = isl_map_copy(map);
9592 map = isl_map_normalize(map);
9593 if (!map)
9594 return 0;
9596 hash = isl_hash_init();
9597 for (i = 0; i < map->n; ++i) {
9598 uint32_t bmap_hash;
9599 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9600 isl_hash_hash(hash, bmap_hash);
9603 isl_map_free(map);
9605 return hash;
9608 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9610 return isl_map_get_hash((isl_map *)set);
9613 /* Check if the value for dimension dim is completely determined
9614 * by the values of the other parameters and variables.
9615 * That is, check if dimension dim is involved in an equality.
9617 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9619 int i;
9620 unsigned nparam;
9622 if (!bset)
9623 return -1;
9624 nparam = isl_basic_set_n_param(bset);
9625 for (i = 0; i < bset->n_eq; ++i)
9626 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9627 return 1;
9628 return 0;
9631 /* Check if the value for dimension dim is completely determined
9632 * by the values of the other parameters and variables.
9633 * That is, check if dimension dim is involved in an equality
9634 * for each of the subsets.
9636 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9638 int i;
9640 if (!set)
9641 return -1;
9642 for (i = 0; i < set->n; ++i) {
9643 int unique;
9644 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9645 if (unique != 1)
9646 return unique;
9648 return 1;
9651 int isl_set_n_basic_set(__isl_keep isl_set *set)
9653 return set ? set->n : 0;
9656 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9657 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9659 int i;
9661 if (!map)
9662 return -1;
9664 for (i = 0; i < map->n; ++i)
9665 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9666 return -1;
9668 return 0;
9671 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9672 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9674 int i;
9676 if (!set)
9677 return -1;
9679 for (i = 0; i < set->n; ++i)
9680 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9681 return -1;
9683 return 0;
9686 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9688 isl_space *dim;
9690 if (!bset)
9691 return NULL;
9693 bset = isl_basic_set_cow(bset);
9694 if (!bset)
9695 return NULL;
9697 dim = isl_basic_set_get_space(bset);
9698 dim = isl_space_lift(dim, bset->n_div);
9699 if (!dim)
9700 goto error;
9701 isl_space_free(bset->dim);
9702 bset->dim = dim;
9703 bset->extra -= bset->n_div;
9704 bset->n_div = 0;
9706 bset = isl_basic_set_finalize(bset);
9708 return bset;
9709 error:
9710 isl_basic_set_free(bset);
9711 return NULL;
9714 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9716 int i;
9717 isl_space *dim;
9718 unsigned n_div;
9720 set = isl_set_align_divs(set);
9722 if (!set)
9723 return NULL;
9725 set = isl_set_cow(set);
9726 if (!set)
9727 return NULL;
9729 n_div = set->p[0]->n_div;
9730 dim = isl_set_get_space(set);
9731 dim = isl_space_lift(dim, n_div);
9732 if (!dim)
9733 goto error;
9734 isl_space_free(set->dim);
9735 set->dim = dim;
9737 for (i = 0; i < set->n; ++i) {
9738 set->p[i] = isl_basic_set_lift(set->p[i]);
9739 if (!set->p[i])
9740 goto error;
9743 return set;
9744 error:
9745 isl_set_free(set);
9746 return NULL;
9749 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9751 isl_space *dim;
9752 struct isl_basic_map *bmap;
9753 unsigned n_set;
9754 unsigned n_div;
9755 unsigned n_param;
9756 unsigned total;
9757 int i, k, l;
9759 set = isl_set_align_divs(set);
9761 if (!set)
9762 return NULL;
9764 dim = isl_set_get_space(set);
9765 if (set->n == 0 || set->p[0]->n_div == 0) {
9766 isl_set_free(set);
9767 return isl_map_identity(isl_space_map_from_set(dim));
9770 n_div = set->p[0]->n_div;
9771 dim = isl_space_map_from_set(dim);
9772 n_param = isl_space_dim(dim, isl_dim_param);
9773 n_set = isl_space_dim(dim, isl_dim_in);
9774 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9775 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9776 for (i = 0; i < n_set; ++i)
9777 bmap = var_equal(bmap, i);
9779 total = n_param + n_set + n_set + n_div;
9780 for (i = 0; i < n_div; ++i) {
9781 k = isl_basic_map_alloc_inequality(bmap);
9782 if (k < 0)
9783 goto error;
9784 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9785 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9786 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9787 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9788 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9789 set->p[0]->div[i][0]);
9791 l = isl_basic_map_alloc_inequality(bmap);
9792 if (l < 0)
9793 goto error;
9794 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9795 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9796 set->p[0]->div[i][0]);
9797 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9800 isl_set_free(set);
9801 bmap = isl_basic_map_simplify(bmap);
9802 bmap = isl_basic_map_finalize(bmap);
9803 return isl_map_from_basic_map(bmap);
9804 error:
9805 isl_set_free(set);
9806 isl_basic_map_free(bmap);
9807 return NULL;
9810 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9812 unsigned dim;
9813 int size = 0;
9815 if (!bset)
9816 return -1;
9818 dim = isl_basic_set_total_dim(bset);
9819 size += bset->n_eq * (1 + dim);
9820 size += bset->n_ineq * (1 + dim);
9821 size += bset->n_div * (2 + dim);
9823 return size;
9826 int isl_set_size(__isl_keep isl_set *set)
9828 int i;
9829 int size = 0;
9831 if (!set)
9832 return -1;
9834 for (i = 0; i < set->n; ++i)
9835 size += isl_basic_set_size(set->p[i]);
9837 return size;
9840 /* Check if there is any lower bound (if lower == 0) and/or upper
9841 * bound (if upper == 0) on the specified dim.
9843 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9844 enum isl_dim_type type, unsigned pos, int lower, int upper)
9846 int i;
9848 if (!bmap)
9849 return -1;
9851 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9853 pos += isl_basic_map_offset(bmap, type);
9855 for (i = 0; i < bmap->n_div; ++i) {
9856 if (isl_int_is_zero(bmap->div[i][0]))
9857 continue;
9858 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9859 return 1;
9862 for (i = 0; i < bmap->n_eq; ++i)
9863 if (!isl_int_is_zero(bmap->eq[i][pos]))
9864 return 1;
9866 for (i = 0; i < bmap->n_ineq; ++i) {
9867 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
9868 if (sgn > 0)
9869 lower = 1;
9870 if (sgn < 0)
9871 upper = 1;
9874 return lower && upper;
9877 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9878 enum isl_dim_type type, unsigned pos)
9880 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9883 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9884 enum isl_dim_type type, unsigned pos)
9886 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9889 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9890 enum isl_dim_type type, unsigned pos)
9892 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9895 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9896 enum isl_dim_type type, unsigned pos)
9898 int i;
9900 if (!map)
9901 return -1;
9903 for (i = 0; i < map->n; ++i) {
9904 int bounded;
9905 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9906 if (bounded < 0 || !bounded)
9907 return bounded;
9910 return 1;
9913 /* Return 1 if the specified dim is involved in both an upper bound
9914 * and a lower bound.
9916 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9917 enum isl_dim_type type, unsigned pos)
9919 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9922 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9924 static int has_any_bound(__isl_keep isl_map *map,
9925 enum isl_dim_type type, unsigned pos,
9926 int (*fn)(__isl_keep isl_basic_map *bmap,
9927 enum isl_dim_type type, unsigned pos))
9929 int i;
9931 if (!map)
9932 return -1;
9934 for (i = 0; i < map->n; ++i) {
9935 int bounded;
9936 bounded = fn(map->p[i], type, pos);
9937 if (bounded < 0 || bounded)
9938 return bounded;
9941 return 0;
9944 /* Return 1 if the specified dim is involved in any lower bound.
9946 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
9947 enum isl_dim_type type, unsigned pos)
9949 return has_any_bound(set, type, pos,
9950 &isl_basic_map_dim_has_lower_bound);
9953 /* Return 1 if the specified dim is involved in any upper bound.
9955 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
9956 enum isl_dim_type type, unsigned pos)
9958 return has_any_bound(set, type, pos,
9959 &isl_basic_map_dim_has_upper_bound);
9962 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9964 static int has_bound(__isl_keep isl_map *map,
9965 enum isl_dim_type type, unsigned pos,
9966 int (*fn)(__isl_keep isl_basic_map *bmap,
9967 enum isl_dim_type type, unsigned pos))
9969 int i;
9971 if (!map)
9972 return -1;
9974 for (i = 0; i < map->n; ++i) {
9975 int bounded;
9976 bounded = fn(map->p[i], type, pos);
9977 if (bounded < 0 || !bounded)
9978 return bounded;
9981 return 1;
9984 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9986 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9987 enum isl_dim_type type, unsigned pos)
9989 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9992 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9994 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9995 enum isl_dim_type type, unsigned pos)
9997 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10000 /* For each of the "n" variables starting at "first", determine
10001 * the sign of the variable and put the results in the first "n"
10002 * elements of the array "signs".
10003 * Sign
10004 * 1 means that the variable is non-negative
10005 * -1 means that the variable is non-positive
10006 * 0 means the variable attains both positive and negative values.
10008 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10009 unsigned first, unsigned n, int *signs)
10011 isl_vec *bound = NULL;
10012 struct isl_tab *tab = NULL;
10013 struct isl_tab_undo *snap;
10014 int i;
10016 if (!bset || !signs)
10017 return -1;
10019 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10020 tab = isl_tab_from_basic_set(bset, 0);
10021 if (!bound || !tab)
10022 goto error;
10024 isl_seq_clr(bound->el, bound->size);
10025 isl_int_set_si(bound->el[0], -1);
10027 snap = isl_tab_snap(tab);
10028 for (i = 0; i < n; ++i) {
10029 int empty;
10031 isl_int_set_si(bound->el[1 + first + i], -1);
10032 if (isl_tab_add_ineq(tab, bound->el) < 0)
10033 goto error;
10034 empty = tab->empty;
10035 isl_int_set_si(bound->el[1 + first + i], 0);
10036 if (isl_tab_rollback(tab, snap) < 0)
10037 goto error;
10039 if (empty) {
10040 signs[i] = 1;
10041 continue;
10044 isl_int_set_si(bound->el[1 + first + i], 1);
10045 if (isl_tab_add_ineq(tab, bound->el) < 0)
10046 goto error;
10047 empty = tab->empty;
10048 isl_int_set_si(bound->el[1 + first + i], 0);
10049 if (isl_tab_rollback(tab, snap) < 0)
10050 goto error;
10052 signs[i] = empty ? -1 : 0;
10055 isl_tab_free(tab);
10056 isl_vec_free(bound);
10057 return 0;
10058 error:
10059 isl_tab_free(tab);
10060 isl_vec_free(bound);
10061 return -1;
10064 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10065 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10067 if (!bset || !signs)
10068 return -1;
10069 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10070 return -1);
10072 first += pos(bset->dim, type) - 1;
10073 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10076 /* Check if the given basic map is obviously single-valued.
10077 * In particular, for each output dimension, check that there is
10078 * an equality that defines the output dimension in terms of
10079 * earlier dimensions.
10081 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10083 int i, j;
10084 unsigned total;
10085 unsigned n_out;
10086 unsigned o_out;
10088 if (!bmap)
10089 return -1;
10091 total = 1 + isl_basic_map_total_dim(bmap);
10092 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10093 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10095 for (i = 0; i < n_out; ++i) {
10096 for (j = 0; j < bmap->n_eq; ++j) {
10097 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
10098 continue;
10099 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
10100 total - (o_out + i + 1)) == -1)
10101 break;
10103 if (j >= bmap->n_eq)
10104 return 0;
10107 return 1;
10110 /* Check if the given basic map is single-valued.
10111 * We simply compute
10113 * M \circ M^-1
10115 * and check if the result is a subset of the identity mapping.
10117 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10119 isl_space *space;
10120 isl_basic_map *test;
10121 isl_basic_map *id;
10122 int sv;
10124 sv = isl_basic_map_plain_is_single_valued(bmap);
10125 if (sv < 0 || sv)
10126 return sv;
10128 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10129 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10131 space = isl_basic_map_get_space(bmap);
10132 space = isl_space_map_from_set(isl_space_range(space));
10133 id = isl_basic_map_identity(space);
10135 sv = isl_basic_map_is_subset(test, id);
10137 isl_basic_map_free(test);
10138 isl_basic_map_free(id);
10140 return sv;
10143 /* Check if the given map is obviously single-valued.
10145 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10147 if (!map)
10148 return -1;
10149 if (map->n == 0)
10150 return 1;
10151 if (map->n >= 2)
10152 return 0;
10154 return isl_basic_map_plain_is_single_valued(map->p[0]);
10157 /* Check if the given map is single-valued.
10158 * We simply compute
10160 * M \circ M^-1
10162 * and check if the result is a subset of the identity mapping.
10164 int isl_map_is_single_valued(__isl_keep isl_map *map)
10166 isl_space *dim;
10167 isl_map *test;
10168 isl_map *id;
10169 int sv;
10171 sv = isl_map_plain_is_single_valued(map);
10172 if (sv < 0 || sv)
10173 return sv;
10175 test = isl_map_reverse(isl_map_copy(map));
10176 test = isl_map_apply_range(test, isl_map_copy(map));
10178 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10179 id = isl_map_identity(dim);
10181 sv = isl_map_is_subset(test, id);
10183 isl_map_free(test);
10184 isl_map_free(id);
10186 return sv;
10189 int isl_map_is_injective(__isl_keep isl_map *map)
10191 int in;
10193 map = isl_map_copy(map);
10194 map = isl_map_reverse(map);
10195 in = isl_map_is_single_valued(map);
10196 isl_map_free(map);
10198 return in;
10201 /* Check if the given map is obviously injective.
10203 int isl_map_plain_is_injective(__isl_keep isl_map *map)
10205 int in;
10207 map = isl_map_copy(map);
10208 map = isl_map_reverse(map);
10209 in = isl_map_plain_is_single_valued(map);
10210 isl_map_free(map);
10212 return in;
10215 int isl_map_is_bijective(__isl_keep isl_map *map)
10217 int sv;
10219 sv = isl_map_is_single_valued(map);
10220 if (sv < 0 || !sv)
10221 return sv;
10223 return isl_map_is_injective(map);
10226 int isl_set_is_singleton(__isl_keep isl_set *set)
10228 return isl_map_is_single_valued((isl_map *)set);
10231 int isl_map_is_translation(__isl_keep isl_map *map)
10233 int ok;
10234 isl_set *delta;
10236 delta = isl_map_deltas(isl_map_copy(map));
10237 ok = isl_set_is_singleton(delta);
10238 isl_set_free(delta);
10240 return ok;
10243 static int unique(isl_int *p, unsigned pos, unsigned len)
10245 if (isl_seq_first_non_zero(p, pos) != -1)
10246 return 0;
10247 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10248 return 0;
10249 return 1;
10252 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10254 int i, j;
10255 unsigned nvar;
10256 unsigned ovar;
10258 if (!bset)
10259 return -1;
10261 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10262 return 0;
10264 nvar = isl_basic_set_dim(bset, isl_dim_set);
10265 ovar = isl_space_offset(bset->dim, isl_dim_set);
10266 for (j = 0; j < nvar; ++j) {
10267 int lower = 0, upper = 0;
10268 for (i = 0; i < bset->n_eq; ++i) {
10269 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10270 continue;
10271 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10272 return 0;
10273 break;
10275 if (i < bset->n_eq)
10276 continue;
10277 for (i = 0; i < bset->n_ineq; ++i) {
10278 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10279 continue;
10280 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10281 return 0;
10282 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10283 lower = 1;
10284 else
10285 upper = 1;
10287 if (!lower || !upper)
10288 return 0;
10291 return 1;
10294 int isl_set_is_box(__isl_keep isl_set *set)
10296 if (!set)
10297 return -1;
10298 if (set->n != 1)
10299 return 0;
10301 return isl_basic_set_is_box(set->p[0]);
10304 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10306 if (!bset)
10307 return -1;
10309 return isl_space_is_wrapping(bset->dim);
10312 int isl_set_is_wrapping(__isl_keep isl_set *set)
10314 if (!set)
10315 return -1;
10317 return isl_space_is_wrapping(set->dim);
10320 /* Is the domain of "map" a wrapped relation?
10322 int isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10324 if (!map)
10325 return -1;
10327 return isl_space_domain_is_wrapping(map->dim);
10330 /* Is the range of "map" a wrapped relation?
10332 int isl_map_range_is_wrapping(__isl_keep isl_map *map)
10334 if (!map)
10335 return -1;
10337 return isl_space_range_is_wrapping(map->dim);
10340 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10342 bmap = isl_basic_map_cow(bmap);
10343 if (!bmap)
10344 return NULL;
10346 bmap->dim = isl_space_wrap(bmap->dim);
10347 if (!bmap->dim)
10348 goto error;
10350 bmap = isl_basic_map_finalize(bmap);
10352 return (isl_basic_set *)bmap;
10353 error:
10354 isl_basic_map_free(bmap);
10355 return NULL;
10358 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10360 int i;
10362 map = isl_map_cow(map);
10363 if (!map)
10364 return NULL;
10366 for (i = 0; i < map->n; ++i) {
10367 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10368 if (!map->p[i])
10369 goto error;
10371 map->dim = isl_space_wrap(map->dim);
10372 if (!map->dim)
10373 goto error;
10375 return (isl_set *)map;
10376 error:
10377 isl_map_free(map);
10378 return NULL;
10381 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10383 bset = isl_basic_set_cow(bset);
10384 if (!bset)
10385 return NULL;
10387 bset->dim = isl_space_unwrap(bset->dim);
10388 if (!bset->dim)
10389 goto error;
10391 bset = isl_basic_set_finalize(bset);
10393 return (isl_basic_map *)bset;
10394 error:
10395 isl_basic_set_free(bset);
10396 return NULL;
10399 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10401 int i;
10403 if (!set)
10404 return NULL;
10406 if (!isl_set_is_wrapping(set))
10407 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10408 goto error);
10410 set = isl_set_cow(set);
10411 if (!set)
10412 return NULL;
10414 for (i = 0; i < set->n; ++i) {
10415 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10416 if (!set->p[i])
10417 goto error;
10420 set->dim = isl_space_unwrap(set->dim);
10421 if (!set->dim)
10422 goto error;
10424 return (isl_map *)set;
10425 error:
10426 isl_set_free(set);
10427 return NULL;
10430 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10431 enum isl_dim_type type)
10433 if (!bmap)
10434 return NULL;
10436 if (!isl_space_is_named_or_nested(bmap->dim, type))
10437 return bmap;
10439 bmap = isl_basic_map_cow(bmap);
10440 if (!bmap)
10441 return NULL;
10443 bmap->dim = isl_space_reset(bmap->dim, type);
10444 if (!bmap->dim)
10445 goto error;
10447 bmap = isl_basic_map_finalize(bmap);
10449 return bmap;
10450 error:
10451 isl_basic_map_free(bmap);
10452 return NULL;
10455 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10456 enum isl_dim_type type)
10458 int i;
10460 if (!map)
10461 return NULL;
10463 if (!isl_space_is_named_or_nested(map->dim, type))
10464 return map;
10466 map = isl_map_cow(map);
10467 if (!map)
10468 return NULL;
10470 for (i = 0; i < map->n; ++i) {
10471 map->p[i] = isl_basic_map_reset(map->p[i], type);
10472 if (!map->p[i])
10473 goto error;
10475 map->dim = isl_space_reset(map->dim, type);
10476 if (!map->dim)
10477 goto error;
10479 return map;
10480 error:
10481 isl_map_free(map);
10482 return NULL;
10485 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10487 if (!bmap)
10488 return NULL;
10490 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10491 return bmap;
10493 bmap = isl_basic_map_cow(bmap);
10494 if (!bmap)
10495 return NULL;
10497 bmap->dim = isl_space_flatten(bmap->dim);
10498 if (!bmap->dim)
10499 goto error;
10501 bmap = isl_basic_map_finalize(bmap);
10503 return bmap;
10504 error:
10505 isl_basic_map_free(bmap);
10506 return NULL;
10509 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10511 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10514 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10515 __isl_take isl_basic_map *bmap)
10517 if (!bmap)
10518 return NULL;
10520 if (!bmap->dim->nested[0])
10521 return bmap;
10523 bmap = isl_basic_map_cow(bmap);
10524 if (!bmap)
10525 return NULL;
10527 bmap->dim = isl_space_flatten_domain(bmap->dim);
10528 if (!bmap->dim)
10529 goto error;
10531 bmap = isl_basic_map_finalize(bmap);
10533 return bmap;
10534 error:
10535 isl_basic_map_free(bmap);
10536 return NULL;
10539 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10540 __isl_take isl_basic_map *bmap)
10542 if (!bmap)
10543 return NULL;
10545 if (!bmap->dim->nested[1])
10546 return bmap;
10548 bmap = isl_basic_map_cow(bmap);
10549 if (!bmap)
10550 return NULL;
10552 bmap->dim = isl_space_flatten_range(bmap->dim);
10553 if (!bmap->dim)
10554 goto error;
10556 bmap = isl_basic_map_finalize(bmap);
10558 return bmap;
10559 error:
10560 isl_basic_map_free(bmap);
10561 return NULL;
10564 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10566 int i;
10568 if (!map)
10569 return NULL;
10571 if (!map->dim->nested[0] && !map->dim->nested[1])
10572 return map;
10574 map = isl_map_cow(map);
10575 if (!map)
10576 return NULL;
10578 for (i = 0; i < map->n; ++i) {
10579 map->p[i] = isl_basic_map_flatten(map->p[i]);
10580 if (!map->p[i])
10581 goto error;
10583 map->dim = isl_space_flatten(map->dim);
10584 if (!map->dim)
10585 goto error;
10587 return map;
10588 error:
10589 isl_map_free(map);
10590 return NULL;
10593 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10595 return (isl_set *)isl_map_flatten((isl_map *)set);
10598 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10600 isl_space *dim, *flat_dim;
10601 isl_map *map;
10603 dim = isl_set_get_space(set);
10604 flat_dim = isl_space_flatten(isl_space_copy(dim));
10605 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10606 map = isl_map_intersect_domain(map, set);
10608 return map;
10611 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10613 int i;
10615 if (!map)
10616 return NULL;
10618 if (!map->dim->nested[0])
10619 return map;
10621 map = isl_map_cow(map);
10622 if (!map)
10623 return NULL;
10625 for (i = 0; i < map->n; ++i) {
10626 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10627 if (!map->p[i])
10628 goto error;
10630 map->dim = isl_space_flatten_domain(map->dim);
10631 if (!map->dim)
10632 goto error;
10634 return map;
10635 error:
10636 isl_map_free(map);
10637 return NULL;
10640 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10642 int i;
10644 if (!map)
10645 return NULL;
10647 if (!map->dim->nested[1])
10648 return map;
10650 map = isl_map_cow(map);
10651 if (!map)
10652 return NULL;
10654 for (i = 0; i < map->n; ++i) {
10655 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10656 if (!map->p[i])
10657 goto error;
10659 map->dim = isl_space_flatten_range(map->dim);
10660 if (!map->dim)
10661 goto error;
10663 return map;
10664 error:
10665 isl_map_free(map);
10666 return NULL;
10669 /* Reorder the dimensions of "bmap" according to the given dim_map
10670 * and set the dimension specification to "dim".
10672 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10673 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10675 isl_basic_map *res;
10676 unsigned flags;
10678 bmap = isl_basic_map_cow(bmap);
10679 if (!bmap || !dim || !dim_map)
10680 goto error;
10682 flags = bmap->flags;
10683 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10684 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10685 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10686 res = isl_basic_map_alloc_space(dim,
10687 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10688 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10689 if (res)
10690 res->flags = flags;
10691 res = isl_basic_map_finalize(res);
10692 return res;
10693 error:
10694 free(dim_map);
10695 isl_basic_map_free(bmap);
10696 isl_space_free(dim);
10697 return NULL;
10700 /* Reorder the dimensions of "map" according to given reordering.
10702 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10703 __isl_take isl_reordering *r)
10705 int i;
10706 struct isl_dim_map *dim_map;
10708 map = isl_map_cow(map);
10709 dim_map = isl_dim_map_from_reordering(r);
10710 if (!map || !r || !dim_map)
10711 goto error;
10713 for (i = 0; i < map->n; ++i) {
10714 struct isl_dim_map *dim_map_i;
10716 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10718 map->p[i] = isl_basic_map_realign(map->p[i],
10719 isl_space_copy(r->dim), dim_map_i);
10721 if (!map->p[i])
10722 goto error;
10725 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10727 isl_reordering_free(r);
10728 free(dim_map);
10729 return map;
10730 error:
10731 free(dim_map);
10732 isl_map_free(map);
10733 isl_reordering_free(r);
10734 return NULL;
10737 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10738 __isl_take isl_reordering *r)
10740 return (isl_set *)isl_map_realign((isl_map *)set, r);
10743 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10744 __isl_take isl_space *model)
10746 isl_ctx *ctx;
10748 if (!map || !model)
10749 goto error;
10751 ctx = isl_space_get_ctx(model);
10752 if (!isl_space_has_named_params(model))
10753 isl_die(ctx, isl_error_invalid,
10754 "model has unnamed parameters", goto error);
10755 if (!isl_space_has_named_params(map->dim))
10756 isl_die(ctx, isl_error_invalid,
10757 "relation has unnamed parameters", goto error);
10758 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10759 isl_reordering *exp;
10761 model = isl_space_drop_dims(model, isl_dim_in,
10762 0, isl_space_dim(model, isl_dim_in));
10763 model = isl_space_drop_dims(model, isl_dim_out,
10764 0, isl_space_dim(model, isl_dim_out));
10765 exp = isl_parameter_alignment_reordering(map->dim, model);
10766 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10767 map = isl_map_realign(map, exp);
10770 isl_space_free(model);
10771 return map;
10772 error:
10773 isl_space_free(model);
10774 isl_map_free(map);
10775 return NULL;
10778 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10779 __isl_take isl_space *model)
10781 return isl_map_align_params(set, model);
10784 /* Align the parameters of "bmap" to those of "model", introducing
10785 * additional parameters if needed.
10787 __isl_give isl_basic_map *isl_basic_map_align_params(
10788 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10790 isl_ctx *ctx;
10792 if (!bmap || !model)
10793 goto error;
10795 ctx = isl_space_get_ctx(model);
10796 if (!isl_space_has_named_params(model))
10797 isl_die(ctx, isl_error_invalid,
10798 "model has unnamed parameters", goto error);
10799 if (!isl_space_has_named_params(bmap->dim))
10800 isl_die(ctx, isl_error_invalid,
10801 "relation has unnamed parameters", goto error);
10802 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10803 isl_reordering *exp;
10804 struct isl_dim_map *dim_map;
10806 model = isl_space_drop_dims(model, isl_dim_in,
10807 0, isl_space_dim(model, isl_dim_in));
10808 model = isl_space_drop_dims(model, isl_dim_out,
10809 0, isl_space_dim(model, isl_dim_out));
10810 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10811 exp = isl_reordering_extend_space(exp,
10812 isl_basic_map_get_space(bmap));
10813 dim_map = isl_dim_map_from_reordering(exp);
10814 bmap = isl_basic_map_realign(bmap,
10815 exp ? isl_space_copy(exp->dim) : NULL,
10816 isl_dim_map_extend(dim_map, bmap));
10817 isl_reordering_free(exp);
10818 free(dim_map);
10821 isl_space_free(model);
10822 return bmap;
10823 error:
10824 isl_space_free(model);
10825 isl_basic_map_free(bmap);
10826 return NULL;
10829 /* Align the parameters of "bset" to those of "model", introducing
10830 * additional parameters if needed.
10832 __isl_give isl_basic_set *isl_basic_set_align_params(
10833 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10835 return isl_basic_map_align_params(bset, model);
10838 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10839 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10840 enum isl_dim_type c2, enum isl_dim_type c3,
10841 enum isl_dim_type c4, enum isl_dim_type c5)
10843 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10844 struct isl_mat *mat;
10845 int i, j, k;
10846 int pos;
10848 if (!bmap)
10849 return NULL;
10850 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
10851 isl_basic_map_total_dim(bmap) + 1);
10852 if (!mat)
10853 return NULL;
10854 for (i = 0; i < bmap->n_eq; ++i)
10855 for (j = 0, pos = 0; j < 5; ++j) {
10856 int off = isl_basic_map_offset(bmap, c[j]);
10857 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10858 isl_int_set(mat->row[i][pos],
10859 bmap->eq[i][off + k]);
10860 ++pos;
10864 return mat;
10867 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
10868 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10869 enum isl_dim_type c2, enum isl_dim_type c3,
10870 enum isl_dim_type c4, enum isl_dim_type c5)
10872 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10873 struct isl_mat *mat;
10874 int i, j, k;
10875 int pos;
10877 if (!bmap)
10878 return NULL;
10879 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
10880 isl_basic_map_total_dim(bmap) + 1);
10881 if (!mat)
10882 return NULL;
10883 for (i = 0; i < bmap->n_ineq; ++i)
10884 for (j = 0, pos = 0; j < 5; ++j) {
10885 int off = isl_basic_map_offset(bmap, c[j]);
10886 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10887 isl_int_set(mat->row[i][pos],
10888 bmap->ineq[i][off + k]);
10889 ++pos;
10893 return mat;
10896 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
10897 __isl_take isl_space *dim,
10898 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10899 enum isl_dim_type c2, enum isl_dim_type c3,
10900 enum isl_dim_type c4, enum isl_dim_type c5)
10902 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10903 isl_basic_map *bmap;
10904 unsigned total;
10905 unsigned extra;
10906 int i, j, k, l;
10907 int pos;
10909 if (!dim || !eq || !ineq)
10910 goto error;
10912 if (eq->n_col != ineq->n_col)
10913 isl_die(dim->ctx, isl_error_invalid,
10914 "equalities and inequalities matrices should have "
10915 "same number of columns", goto error);
10917 total = 1 + isl_space_dim(dim, isl_dim_all);
10919 if (eq->n_col < total)
10920 isl_die(dim->ctx, isl_error_invalid,
10921 "number of columns too small", goto error);
10923 extra = eq->n_col - total;
10925 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
10926 eq->n_row, ineq->n_row);
10927 if (!bmap)
10928 goto error;
10929 for (i = 0; i < extra; ++i) {
10930 k = isl_basic_map_alloc_div(bmap);
10931 if (k < 0)
10932 goto error;
10933 isl_int_set_si(bmap->div[k][0], 0);
10935 for (i = 0; i < eq->n_row; ++i) {
10936 l = isl_basic_map_alloc_equality(bmap);
10937 if (l < 0)
10938 goto error;
10939 for (j = 0, pos = 0; j < 5; ++j) {
10940 int off = isl_basic_map_offset(bmap, c[j]);
10941 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10942 isl_int_set(bmap->eq[l][off + k],
10943 eq->row[i][pos]);
10944 ++pos;
10948 for (i = 0; i < ineq->n_row; ++i) {
10949 l = isl_basic_map_alloc_inequality(bmap);
10950 if (l < 0)
10951 goto error;
10952 for (j = 0, pos = 0; j < 5; ++j) {
10953 int off = isl_basic_map_offset(bmap, c[j]);
10954 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10955 isl_int_set(bmap->ineq[l][off + k],
10956 ineq->row[i][pos]);
10957 ++pos;
10962 isl_space_free(dim);
10963 isl_mat_free(eq);
10964 isl_mat_free(ineq);
10966 bmap = isl_basic_map_simplify(bmap);
10967 return isl_basic_map_finalize(bmap);
10968 error:
10969 isl_space_free(dim);
10970 isl_mat_free(eq);
10971 isl_mat_free(ineq);
10972 return NULL;
10975 __isl_give isl_mat *isl_basic_set_equalities_matrix(
10976 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10977 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10979 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
10980 c1, c2, c3, c4, isl_dim_in);
10983 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
10984 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10985 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10987 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
10988 c1, c2, c3, c4, isl_dim_in);
10991 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
10992 __isl_take isl_space *dim,
10993 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10994 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10996 return (isl_basic_set*)
10997 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
10998 c1, c2, c3, c4, isl_dim_in);
11001 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11003 if (!bmap)
11004 return -1;
11006 return isl_space_can_zip(bmap->dim);
11009 int isl_map_can_zip(__isl_keep isl_map *map)
11011 if (!map)
11012 return -1;
11014 return isl_space_can_zip(map->dim);
11017 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11018 * (A -> C) -> (B -> D).
11020 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11022 unsigned pos;
11023 unsigned n1;
11024 unsigned n2;
11026 if (!bmap)
11027 return NULL;
11029 if (!isl_basic_map_can_zip(bmap))
11030 isl_die(bmap->ctx, isl_error_invalid,
11031 "basic map cannot be zipped", goto error);
11032 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11033 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11034 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11035 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11036 bmap = isl_basic_map_cow(bmap);
11037 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11038 if (!bmap)
11039 return NULL;
11040 bmap->dim = isl_space_zip(bmap->dim);
11041 if (!bmap->dim)
11042 goto error;
11043 return bmap;
11044 error:
11045 isl_basic_map_free(bmap);
11046 return NULL;
11049 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11050 * (A -> C) -> (B -> D).
11052 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11054 int i;
11056 if (!map)
11057 return NULL;
11059 if (!isl_map_can_zip(map))
11060 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11061 goto error);
11063 map = isl_map_cow(map);
11064 if (!map)
11065 return NULL;
11067 for (i = 0; i < map->n; ++i) {
11068 map->p[i] = isl_basic_map_zip(map->p[i]);
11069 if (!map->p[i])
11070 goto error;
11073 map->dim = isl_space_zip(map->dim);
11074 if (!map->dim)
11075 goto error;
11077 return map;
11078 error:
11079 isl_map_free(map);
11080 return NULL;
11083 /* Can we apply isl_basic_map_curry to "bmap"?
11084 * That is, does it have a nested relation in its domain?
11086 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11088 if (!bmap)
11089 return -1;
11091 return isl_space_can_curry(bmap->dim);
11094 /* Can we apply isl_map_curry to "map"?
11095 * That is, does it have a nested relation in its domain?
11097 int isl_map_can_curry(__isl_keep isl_map *map)
11099 if (!map)
11100 return -1;
11102 return isl_space_can_curry(map->dim);
11105 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11106 * A -> (B -> C).
11108 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11111 if (!bmap)
11112 return NULL;
11114 if (!isl_basic_map_can_curry(bmap))
11115 isl_die(bmap->ctx, isl_error_invalid,
11116 "basic map cannot be curried", goto error);
11117 bmap = isl_basic_map_cow(bmap);
11118 if (!bmap)
11119 return NULL;
11120 bmap->dim = isl_space_curry(bmap->dim);
11121 if (!bmap->dim)
11122 goto error;
11123 return bmap;
11124 error:
11125 isl_basic_map_free(bmap);
11126 return NULL;
11129 /* Given a map (A -> B) -> C, return the corresponding map
11130 * A -> (B -> C).
11132 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11134 int i;
11136 if (!map)
11137 return NULL;
11139 if (!isl_map_can_curry(map))
11140 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11141 goto error);
11143 map = isl_map_cow(map);
11144 if (!map)
11145 return NULL;
11147 for (i = 0; i < map->n; ++i) {
11148 map->p[i] = isl_basic_map_curry(map->p[i]);
11149 if (!map->p[i])
11150 goto error;
11153 map->dim = isl_space_curry(map->dim);
11154 if (!map->dim)
11155 goto error;
11157 return map;
11158 error:
11159 isl_map_free(map);
11160 return NULL;
11163 /* Can we apply isl_basic_map_uncurry to "bmap"?
11164 * That is, does it have a nested relation in its domain?
11166 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11168 if (!bmap)
11169 return -1;
11171 return isl_space_can_uncurry(bmap->dim);
11174 /* Can we apply isl_map_uncurry to "map"?
11175 * That is, does it have a nested relation in its domain?
11177 int isl_map_can_uncurry(__isl_keep isl_map *map)
11179 if (!map)
11180 return -1;
11182 return isl_space_can_uncurry(map->dim);
11185 /* Given a basic map A -> (B -> C), return the corresponding basic map
11186 * (A -> B) -> C.
11188 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11191 if (!bmap)
11192 return NULL;
11194 if (!isl_basic_map_can_uncurry(bmap))
11195 isl_die(bmap->ctx, isl_error_invalid,
11196 "basic map cannot be uncurried",
11197 return isl_basic_map_free(bmap));
11198 bmap = isl_basic_map_cow(bmap);
11199 if (!bmap)
11200 return NULL;
11201 bmap->dim = isl_space_uncurry(bmap->dim);
11202 if (!bmap->dim)
11203 return isl_basic_map_free(bmap);
11204 return bmap;
11207 /* Given a map A -> (B -> C), return the corresponding map
11208 * (A -> B) -> C.
11210 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11212 int i;
11214 if (!map)
11215 return NULL;
11217 if (!isl_map_can_uncurry(map))
11218 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11219 return isl_map_free(map));
11221 map = isl_map_cow(map);
11222 if (!map)
11223 return NULL;
11225 for (i = 0; i < map->n; ++i) {
11226 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11227 if (!map->p[i])
11228 return isl_map_free(map);
11231 map->dim = isl_space_uncurry(map->dim);
11232 if (!map->dim)
11233 return isl_map_free(map);
11235 return map;
11238 /* Construct a basic map mapping the domain of the affine expression
11239 * to a one-dimensional range prescribed by the affine expression.
11241 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11243 int k;
11244 int pos;
11245 isl_local_space *ls;
11246 isl_basic_map *bmap;
11248 if (!aff)
11249 return NULL;
11251 ls = isl_aff_get_local_space(aff);
11252 bmap = isl_basic_map_from_local_space(ls);
11253 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11254 k = isl_basic_map_alloc_equality(bmap);
11255 if (k < 0)
11256 goto error;
11258 pos = isl_basic_map_offset(bmap, isl_dim_out);
11259 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11260 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11261 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11262 aff->v->size - (pos + 1));
11264 isl_aff_free(aff);
11265 bmap = isl_basic_map_finalize(bmap);
11266 return bmap;
11267 error:
11268 isl_aff_free(aff);
11269 isl_basic_map_free(bmap);
11270 return NULL;
11273 /* Construct a map mapping the domain of the affine expression
11274 * to a one-dimensional range prescribed by the affine expression.
11276 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11278 isl_basic_map *bmap;
11280 bmap = isl_basic_map_from_aff(aff);
11281 return isl_map_from_basic_map(bmap);
11284 /* Construct a basic map mapping the domain the multi-affine expression
11285 * to its range, with each dimension in the range equated to the
11286 * corresponding affine expression.
11288 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11289 __isl_take isl_multi_aff *maff)
11291 int i;
11292 isl_space *space;
11293 isl_basic_map *bmap;
11295 if (!maff)
11296 return NULL;
11298 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11299 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11300 "invalid space", goto error);
11302 space = isl_space_domain(isl_multi_aff_get_space(maff));
11303 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11305 for (i = 0; i < maff->n; ++i) {
11306 isl_aff *aff;
11307 isl_basic_map *bmap_i;
11309 aff = isl_aff_copy(maff->p[i]);
11310 bmap_i = isl_basic_map_from_aff(aff);
11312 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11315 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11317 isl_multi_aff_free(maff);
11318 return bmap;
11319 error:
11320 isl_multi_aff_free(maff);
11321 return NULL;
11324 /* Construct a map mapping the domain the multi-affine expression
11325 * to its range, with each dimension in the range equated to the
11326 * corresponding affine expression.
11328 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11330 isl_basic_map *bmap;
11332 bmap = isl_basic_map_from_multi_aff(maff);
11333 return isl_map_from_basic_map(bmap);
11336 /* Construct a basic map mapping a domain in the given space to
11337 * to an n-dimensional range, with n the number of elements in the list,
11338 * where each coordinate in the range is prescribed by the
11339 * corresponding affine expression.
11340 * The domains of all affine expressions in the list are assumed to match
11341 * domain_dim.
11343 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11344 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11346 int i;
11347 isl_space *dim;
11348 isl_basic_map *bmap;
11350 if (!list)
11351 return NULL;
11353 dim = isl_space_from_domain(domain_dim);
11354 bmap = isl_basic_map_universe(dim);
11356 for (i = 0; i < list->n; ++i) {
11357 isl_aff *aff;
11358 isl_basic_map *bmap_i;
11360 aff = isl_aff_copy(list->p[i]);
11361 bmap_i = isl_basic_map_from_aff(aff);
11363 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11366 isl_aff_list_free(list);
11367 return bmap;
11370 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11371 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11373 return isl_map_equate(set, type1, pos1, type2, pos2);
11376 /* Construct a basic map where the given dimensions are equal to each other.
11378 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11379 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11381 isl_basic_map *bmap = NULL;
11382 int i;
11384 if (!space)
11385 return NULL;
11387 if (pos1 >= isl_space_dim(space, type1))
11388 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11389 "index out of bounds", goto error);
11390 if (pos2 >= isl_space_dim(space, type2))
11391 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11392 "index out of bounds", goto error);
11394 if (type1 == type2 && pos1 == pos2)
11395 return isl_basic_map_universe(space);
11397 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11398 i = isl_basic_map_alloc_equality(bmap);
11399 if (i < 0)
11400 goto error;
11401 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11402 pos1 += isl_basic_map_offset(bmap, type1);
11403 pos2 += isl_basic_map_offset(bmap, type2);
11404 isl_int_set_si(bmap->eq[i][pos1], -1);
11405 isl_int_set_si(bmap->eq[i][pos2], 1);
11406 bmap = isl_basic_map_finalize(bmap);
11407 isl_space_free(space);
11408 return bmap;
11409 error:
11410 isl_space_free(space);
11411 isl_basic_map_free(bmap);
11412 return NULL;
11415 /* Add a constraint imposing that the given two dimensions are equal.
11417 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11418 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11420 isl_basic_map *eq;
11422 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11424 bmap = isl_basic_map_intersect(bmap, eq);
11426 return bmap;
11429 /* Add a constraint imposing that the given two dimensions are equal.
11431 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11432 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11434 isl_basic_map *bmap;
11436 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11438 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11440 return map;
11443 /* Add a constraint imposing that the given two dimensions have opposite values.
11445 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11446 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11448 isl_basic_map *bmap = NULL;
11449 int i;
11451 if (!map)
11452 return NULL;
11454 if (pos1 >= isl_map_dim(map, type1))
11455 isl_die(map->ctx, isl_error_invalid,
11456 "index out of bounds", goto error);
11457 if (pos2 >= isl_map_dim(map, type2))
11458 isl_die(map->ctx, isl_error_invalid,
11459 "index out of bounds", goto error);
11461 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11462 i = isl_basic_map_alloc_equality(bmap);
11463 if (i < 0)
11464 goto error;
11465 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11466 pos1 += isl_basic_map_offset(bmap, type1);
11467 pos2 += isl_basic_map_offset(bmap, type2);
11468 isl_int_set_si(bmap->eq[i][pos1], 1);
11469 isl_int_set_si(bmap->eq[i][pos2], 1);
11470 bmap = isl_basic_map_finalize(bmap);
11472 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11474 return map;
11475 error:
11476 isl_basic_map_free(bmap);
11477 isl_map_free(map);
11478 return NULL;
11481 /* Construct a constraint imposing that the value of the first dimension is
11482 * greater than or equal to that of the second.
11484 static __isl_give isl_constraint *constraint_order_ge(
11485 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11486 enum isl_dim_type type2, int pos2)
11488 isl_constraint *c;
11490 if (!space)
11491 return NULL;
11493 c = isl_inequality_alloc(isl_local_space_from_space(space));
11495 if (pos1 >= isl_constraint_dim(c, type1))
11496 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11497 "index out of bounds", return isl_constraint_free(c));
11498 if (pos2 >= isl_constraint_dim(c, type2))
11499 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11500 "index out of bounds", return isl_constraint_free(c));
11502 if (type1 == type2 && pos1 == pos2)
11503 return c;
11505 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11506 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11508 return c;
11511 /* Add a constraint imposing that the value of the first dimension is
11512 * greater than or equal to that of the second.
11514 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11515 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11517 isl_constraint *c;
11518 isl_space *space;
11520 if (type1 == type2 && pos1 == pos2)
11521 return bmap;
11522 space = isl_basic_map_get_space(bmap);
11523 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11524 bmap = isl_basic_map_add_constraint(bmap, c);
11526 return bmap;
11529 /* Add a constraint imposing that the value of the first dimension is
11530 * greater than or equal to that of the second.
11532 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11533 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11535 isl_constraint *c;
11536 isl_space *space;
11538 if (type1 == type2 && pos1 == pos2)
11539 return map;
11540 space = isl_map_get_space(map);
11541 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11542 map = isl_map_add_constraint(map, c);
11544 return map;
11547 /* Add a constraint imposing that the value of the first dimension is
11548 * less than or equal to that of the second.
11550 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11551 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11553 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11556 /* Construct a basic map where the value of the first dimension is
11557 * greater than that of the second.
11559 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11560 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11562 isl_basic_map *bmap = NULL;
11563 int i;
11565 if (!space)
11566 return NULL;
11568 if (pos1 >= isl_space_dim(space, type1))
11569 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11570 "index out of bounds", goto error);
11571 if (pos2 >= isl_space_dim(space, type2))
11572 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11573 "index out of bounds", goto error);
11575 if (type1 == type2 && pos1 == pos2)
11576 return isl_basic_map_empty(space);
11578 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11579 i = isl_basic_map_alloc_inequality(bmap);
11580 if (i < 0)
11581 return isl_basic_map_free(bmap);
11582 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11583 pos1 += isl_basic_map_offset(bmap, type1);
11584 pos2 += isl_basic_map_offset(bmap, type2);
11585 isl_int_set_si(bmap->ineq[i][pos1], 1);
11586 isl_int_set_si(bmap->ineq[i][pos2], -1);
11587 isl_int_set_si(bmap->ineq[i][0], -1);
11588 bmap = isl_basic_map_finalize(bmap);
11590 return bmap;
11591 error:
11592 isl_space_free(space);
11593 isl_basic_map_free(bmap);
11594 return NULL;
11597 /* Add a constraint imposing that the value of the first dimension is
11598 * greater than that of the second.
11600 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11601 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11603 isl_basic_map *gt;
11605 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11607 bmap = isl_basic_map_intersect(bmap, gt);
11609 return bmap;
11612 /* Add a constraint imposing that the value of the first dimension is
11613 * greater than that of the second.
11615 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11616 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11618 isl_basic_map *bmap;
11620 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11622 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11624 return map;
11627 /* Add a constraint imposing that the value of the first dimension is
11628 * smaller than that of the second.
11630 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11631 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11633 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11636 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11637 int pos)
11639 isl_aff *div;
11640 isl_local_space *ls;
11642 if (!bmap)
11643 return NULL;
11645 if (!isl_basic_map_divs_known(bmap))
11646 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11647 "some divs are unknown", return NULL);
11649 ls = isl_basic_map_get_local_space(bmap);
11650 div = isl_local_space_get_div(ls, pos);
11651 isl_local_space_free(ls);
11653 return div;
11656 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11657 int pos)
11659 return isl_basic_map_get_div(bset, pos);
11662 /* Plug in "subs" for dimension "type", "pos" of "bset".
11664 * Let i be the dimension to replace and let "subs" be of the form
11666 * f/d
11668 * Any integer division with a non-zero coefficient for i,
11670 * floor((a i + g)/m)
11672 * is replaced by
11674 * floor((a f + d g)/(m d))
11676 * Constraints of the form
11678 * a i + g
11680 * are replaced by
11682 * a f + d g
11684 * We currently require that "subs" is an integral expression.
11685 * Handling rational expressions may require us to add stride constraints
11686 * as we do in isl_basic_set_preimage_multi_aff.
11688 __isl_give isl_basic_set *isl_basic_set_substitute(
11689 __isl_take isl_basic_set *bset,
11690 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11692 int i;
11693 isl_int v;
11694 isl_ctx *ctx;
11696 if (bset && isl_basic_set_plain_is_empty(bset))
11697 return bset;
11699 bset = isl_basic_set_cow(bset);
11700 if (!bset || !subs)
11701 goto error;
11703 ctx = isl_basic_set_get_ctx(bset);
11704 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11705 isl_die(ctx, isl_error_invalid,
11706 "spaces don't match", goto error);
11707 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11708 isl_die(ctx, isl_error_unsupported,
11709 "cannot handle divs yet", goto error);
11710 if (!isl_int_is_one(subs->v->el[0]))
11711 isl_die(ctx, isl_error_invalid,
11712 "can only substitute integer expressions", goto error);
11714 pos += isl_basic_set_offset(bset, type);
11716 isl_int_init(v);
11718 for (i = 0; i < bset->n_eq; ++i) {
11719 if (isl_int_is_zero(bset->eq[i][pos]))
11720 continue;
11721 isl_int_set(v, bset->eq[i][pos]);
11722 isl_int_set_si(bset->eq[i][pos], 0);
11723 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11724 v, subs->v->el + 1, subs->v->size - 1);
11727 for (i = 0; i < bset->n_ineq; ++i) {
11728 if (isl_int_is_zero(bset->ineq[i][pos]))
11729 continue;
11730 isl_int_set(v, bset->ineq[i][pos]);
11731 isl_int_set_si(bset->ineq[i][pos], 0);
11732 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11733 v, subs->v->el + 1, subs->v->size - 1);
11736 for (i = 0; i < bset->n_div; ++i) {
11737 if (isl_int_is_zero(bset->div[i][1 + pos]))
11738 continue;
11739 isl_int_set(v, bset->div[i][1 + pos]);
11740 isl_int_set_si(bset->div[i][1 + pos], 0);
11741 isl_seq_combine(bset->div[i] + 1,
11742 subs->v->el[0], bset->div[i] + 1,
11743 v, subs->v->el + 1, subs->v->size - 1);
11744 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11747 isl_int_clear(v);
11749 bset = isl_basic_set_simplify(bset);
11750 return isl_basic_set_finalize(bset);
11751 error:
11752 isl_basic_set_free(bset);
11753 return NULL;
11756 /* Plug in "subs" for dimension "type", "pos" of "set".
11758 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11759 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11761 int i;
11763 if (set && isl_set_plain_is_empty(set))
11764 return set;
11766 set = isl_set_cow(set);
11767 if (!set || !subs)
11768 goto error;
11770 for (i = set->n - 1; i >= 0; --i) {
11771 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11772 if (remove_if_empty(set, i) < 0)
11773 goto error;
11776 return set;
11777 error:
11778 isl_set_free(set);
11779 return NULL;
11782 /* Check if the range of "ma" is compatible with the domain or range
11783 * (depending on "type") of "bmap".
11784 * Return -1 if anything is wrong.
11786 static int check_basic_map_compatible_range_multi_aff(
11787 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11788 __isl_keep isl_multi_aff *ma)
11790 int m;
11791 isl_space *ma_space;
11793 ma_space = isl_multi_aff_get_space(ma);
11794 m = isl_space_tuple_match(bmap->dim, type, ma_space, isl_dim_out);
11795 isl_space_free(ma_space);
11796 if (m >= 0 && !m)
11797 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11798 "spaces don't match", return -1);
11799 return m;
11802 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11803 * coefficients before the transformed range of dimensions,
11804 * the "n_after" coefficients after the transformed range of dimensions
11805 * and the coefficients of the other divs in "bmap".
11807 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
11808 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
11810 int i;
11811 int n_param;
11812 int n_set;
11813 isl_local_space *ls;
11815 if (n_div == 0)
11816 return 0;
11818 ls = isl_aff_get_domain_local_space(ma->p[0]);
11819 if (!ls)
11820 return -1;
11822 n_param = isl_local_space_dim(ls, isl_dim_param);
11823 n_set = isl_local_space_dim(ls, isl_dim_set);
11824 for (i = 0; i < n_div; ++i) {
11825 int o_bmap = 0, o_ls = 0;
11827 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
11828 o_bmap += 1 + 1 + n_param;
11829 o_ls += 1 + 1 + n_param;
11830 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
11831 o_bmap += n_before;
11832 isl_seq_cpy(bmap->div[i] + o_bmap,
11833 ls->div->row[i] + o_ls, n_set);
11834 o_bmap += n_set;
11835 o_ls += n_set;
11836 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
11837 o_bmap += n_after;
11838 isl_seq_cpy(bmap->div[i] + o_bmap,
11839 ls->div->row[i] + o_ls, n_div);
11840 o_bmap += n_div;
11841 o_ls += n_div;
11842 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
11843 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
11844 goto error;
11847 isl_local_space_free(ls);
11848 return 0;
11849 error:
11850 isl_local_space_free(ls);
11851 return -1;
11854 /* How many stride constraints does "ma" enforce?
11855 * That is, how many of the affine expressions have a denominator
11856 * different from one?
11858 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
11860 int i;
11861 int strides = 0;
11863 for (i = 0; i < ma->n; ++i)
11864 if (!isl_int_is_one(ma->p[i]->v->el[0]))
11865 strides++;
11867 return strides;
11870 /* For each affine expression in ma of the form
11872 * x_i = (f_i y + h_i)/m_i
11874 * with m_i different from one, add a constraint to "bmap"
11875 * of the form
11877 * f_i y + h_i = m_i alpha_i
11879 * with alpha_i an additional existentially quantified variable.
11881 static __isl_give isl_basic_map *add_ma_strides(
11882 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
11883 int n_before, int n_after)
11885 int i, k;
11886 int div;
11887 int total;
11888 int n_param;
11889 int n_in;
11890 int n_div;
11892 total = isl_basic_map_total_dim(bmap);
11893 n_param = isl_multi_aff_dim(ma, isl_dim_param);
11894 n_in = isl_multi_aff_dim(ma, isl_dim_in);
11895 n_div = isl_multi_aff_dim(ma, isl_dim_div);
11896 for (i = 0; i < ma->n; ++i) {
11897 int o_bmap = 0, o_ma = 1;
11899 if (isl_int_is_one(ma->p[i]->v->el[0]))
11900 continue;
11901 div = isl_basic_map_alloc_div(bmap);
11902 k = isl_basic_map_alloc_equality(bmap);
11903 if (div < 0 || k < 0)
11904 goto error;
11905 isl_int_set_si(bmap->div[div][0], 0);
11906 isl_seq_cpy(bmap->eq[k] + o_bmap,
11907 ma->p[i]->v->el + o_ma, 1 + n_param);
11908 o_bmap += 1 + n_param;
11909 o_ma += 1 + n_param;
11910 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
11911 o_bmap += n_before;
11912 isl_seq_cpy(bmap->eq[k] + o_bmap,
11913 ma->p[i]->v->el + o_ma, n_in);
11914 o_bmap += n_in;
11915 o_ma += n_in;
11916 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
11917 o_bmap += n_after;
11918 isl_seq_cpy(bmap->eq[k] + o_bmap,
11919 ma->p[i]->v->el + o_ma, n_div);
11920 o_bmap += n_div;
11921 o_ma += n_div;
11922 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
11923 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
11924 total++;
11927 return bmap;
11928 error:
11929 isl_basic_map_free(bmap);
11930 return NULL;
11933 /* Replace the domain or range space (depending on "type) of "space" by "set".
11935 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
11936 enum isl_dim_type type, __isl_take isl_space *set)
11938 if (type == isl_dim_in) {
11939 space = isl_space_range(space);
11940 space = isl_space_map_from_domain_and_range(set, space);
11941 } else {
11942 space = isl_space_domain(space);
11943 space = isl_space_map_from_domain_and_range(space, set);
11946 return space;
11949 /* Compute the preimage of the domain or range (depending on "type")
11950 * of "bmap" under the function represented by "ma".
11951 * In other words, plug in "ma" in the domain or range of "bmap".
11952 * The result is a basic map that lives in the same space as "bmap"
11953 * except that the domain or range has been replaced by
11954 * the domain space of "ma".
11956 * If bmap is represented by
11958 * A(p) + S u + B x + T v + C(divs) >= 0,
11960 * where u and x are input and output dimensions if type == isl_dim_out
11961 * while x and v are input and output dimensions if type == isl_dim_in,
11962 * and ma is represented by
11964 * x = D(p) + F(y) + G(divs')
11966 * then the result is
11968 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11970 * The divs in the input set are similarly adjusted.
11971 * In particular
11973 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11975 * becomes
11977 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11978 * B_i G(divs') + c_i(divs))/n_i)
11980 * If bmap is not a rational map and if F(y) involves any denominators
11982 * x_i = (f_i y + h_i)/m_i
11984 * then additional constraints are added to ensure that we only
11985 * map back integer points. That is we enforce
11987 * f_i y + h_i = m_i alpha_i
11989 * with alpha_i an additional existentially quantified variable.
11991 * We first copy over the divs from "ma".
11992 * Then we add the modified constraints and divs from "bmap".
11993 * Finally, we add the stride constraints, if needed.
11995 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
11996 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
11997 __isl_take isl_multi_aff *ma)
11999 int i, k;
12000 isl_space *space;
12001 isl_basic_map *res = NULL;
12002 int n_before, n_after, n_div_bmap, n_div_ma;
12003 isl_int f, c1, c2, g;
12004 int rational, strides;
12006 isl_int_init(f);
12007 isl_int_init(c1);
12008 isl_int_init(c2);
12009 isl_int_init(g);
12011 ma = isl_multi_aff_align_divs(ma);
12012 if (!bmap || !ma)
12013 goto error;
12014 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12015 goto error;
12017 if (type == isl_dim_in) {
12018 n_before = 0;
12019 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12020 } else {
12021 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12022 n_after = 0;
12024 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12025 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12027 space = isl_multi_aff_get_domain_space(ma);
12028 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12029 rational = isl_basic_map_is_rational(bmap);
12030 strides = rational ? 0 : multi_aff_strides(ma);
12031 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12032 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12033 if (rational)
12034 res = isl_basic_map_set_rational(res);
12036 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12037 if (isl_basic_map_alloc_div(res) < 0)
12038 goto error;
12040 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12041 goto error;
12043 for (i = 0; i < bmap->n_eq; ++i) {
12044 k = isl_basic_map_alloc_equality(res);
12045 if (k < 0)
12046 goto error;
12047 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12048 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12051 for (i = 0; i < bmap->n_ineq; ++i) {
12052 k = isl_basic_map_alloc_inequality(res);
12053 if (k < 0)
12054 goto error;
12055 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12056 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12059 for (i = 0; i < bmap->n_div; ++i) {
12060 if (isl_int_is_zero(bmap->div[i][0])) {
12061 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12062 continue;
12064 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12065 n_before, n_after, n_div_ma, n_div_bmap,
12066 f, c1, c2, g, 1);
12069 if (strides)
12070 res = add_ma_strides(res, ma, n_before, n_after);
12072 isl_int_clear(f);
12073 isl_int_clear(c1);
12074 isl_int_clear(c2);
12075 isl_int_clear(g);
12076 isl_basic_map_free(bmap);
12077 isl_multi_aff_free(ma);
12078 res = isl_basic_set_simplify(res);
12079 return isl_basic_map_finalize(res);
12080 error:
12081 isl_int_clear(f);
12082 isl_int_clear(c1);
12083 isl_int_clear(c2);
12084 isl_int_clear(g);
12085 isl_basic_map_free(bmap);
12086 isl_multi_aff_free(ma);
12087 isl_basic_map_free(res);
12088 return NULL;
12091 /* Compute the preimage of "bset" under the function represented by "ma".
12092 * In other words, plug in "ma" in "bset". The result is a basic set
12093 * that lives in the domain space of "ma".
12095 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12096 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12098 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12101 /* Compute the preimage of the domain of "bmap" under the function
12102 * represented by "ma".
12103 * In other words, plug in "ma" in the domain of "bmap".
12104 * The result is a basic map that lives in the same space as "bmap"
12105 * except that the domain has been replaced by the domain space of "ma".
12107 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12108 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12110 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12113 /* Compute the preimage of the range of "bmap" under the function
12114 * represented by "ma".
12115 * In other words, plug in "ma" in the range of "bmap".
12116 * The result is a basic map that lives in the same space as "bmap"
12117 * except that the range has been replaced by the domain space of "ma".
12119 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12120 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12122 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12125 /* Check if the range of "ma" is compatible with the domain or range
12126 * (depending on "type") of "map".
12127 * Return -1 if anything is wrong.
12129 static int check_map_compatible_range_multi_aff(
12130 __isl_keep isl_map *map, enum isl_dim_type type,
12131 __isl_keep isl_multi_aff *ma)
12133 int m;
12134 isl_space *ma_space;
12136 ma_space = isl_multi_aff_get_space(ma);
12137 m = isl_space_tuple_match(map->dim, type, ma_space, isl_dim_out);
12138 isl_space_free(ma_space);
12139 if (m >= 0 && !m)
12140 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12141 "spaces don't match", return -1);
12142 return m;
12145 /* Compute the preimage of the domain or range (depending on "type")
12146 * of "map" under the function represented by "ma".
12147 * In other words, plug in "ma" in the domain or range of "map".
12148 * The result is a map that lives in the same space as "map"
12149 * except that the domain or range has been replaced by
12150 * the domain space of "ma".
12152 * The parameters are assumed to have been aligned.
12154 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12155 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12157 int i;
12158 isl_space *space;
12160 map = isl_map_cow(map);
12161 ma = isl_multi_aff_align_divs(ma);
12162 if (!map || !ma)
12163 goto error;
12164 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12165 goto error;
12167 for (i = 0; i < map->n; ++i) {
12168 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12169 isl_multi_aff_copy(ma));
12170 if (!map->p[i])
12171 goto error;
12174 space = isl_multi_aff_get_domain_space(ma);
12175 space = isl_space_set(isl_map_get_space(map), type, space);
12177 isl_space_free(map->dim);
12178 map->dim = space;
12179 if (!map->dim)
12180 goto error;
12182 isl_multi_aff_free(ma);
12183 if (map->n > 1)
12184 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12185 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12186 return map;
12187 error:
12188 isl_multi_aff_free(ma);
12189 isl_map_free(map);
12190 return NULL;
12193 /* Compute the preimage of the domain or range (depending on "type")
12194 * of "map" under the function represented by "ma".
12195 * In other words, plug in "ma" in the domain or range of "map".
12196 * The result is a map that lives in the same space as "map"
12197 * except that the domain or range has been replaced by
12198 * the domain space of "ma".
12200 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12201 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12203 if (!map || !ma)
12204 goto error;
12206 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12207 return map_preimage_multi_aff(map, type, ma);
12209 if (!isl_space_has_named_params(map->dim) ||
12210 !isl_space_has_named_params(ma->space))
12211 isl_die(map->ctx, isl_error_invalid,
12212 "unaligned unnamed parameters", goto error);
12213 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12214 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12216 return map_preimage_multi_aff(map, type, ma);
12217 error:
12218 isl_multi_aff_free(ma);
12219 return isl_map_free(map);
12222 /* Compute the preimage of "set" under the function represented by "ma".
12223 * In other words, plug in "ma" in "set". The result is a set
12224 * that lives in the domain space of "ma".
12226 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12227 __isl_take isl_multi_aff *ma)
12229 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12232 /* Compute the preimage of the domain of "map" under the function
12233 * represented by "ma".
12234 * In other words, plug in "ma" in the domain of "map".
12235 * The result is a map that lives in the same space as "map"
12236 * except that the domain has been replaced by the domain space of "ma".
12238 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12239 __isl_take isl_multi_aff *ma)
12241 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12244 /* Compute the preimage of the range of "map" under the function
12245 * represented by "ma".
12246 * In other words, plug in "ma" in the range of "map".
12247 * The result is a map that lives in the same space as "map"
12248 * except that the range has been replaced by the domain space of "ma".
12250 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12251 __isl_take isl_multi_aff *ma)
12253 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12256 /* Compute the preimage of "map" under the function represented by "pma".
12257 * In other words, plug in "pma" in the domain or range of "map".
12258 * The result is a map that lives in the same space as "map",
12259 * except that the space of type "type" has been replaced by
12260 * the domain space of "pma".
12262 * The parameters of "map" and "pma" are assumed to have been aligned.
12264 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12265 __isl_take isl_map *map, enum isl_dim_type type,
12266 __isl_take isl_pw_multi_aff *pma)
12268 int i;
12269 isl_map *res;
12271 if (!pma)
12272 goto error;
12274 if (pma->n == 0) {
12275 isl_pw_multi_aff_free(pma);
12276 res = isl_map_empty(isl_map_get_space(map));
12277 isl_map_free(map);
12278 return res;
12281 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12282 isl_multi_aff_copy(pma->p[0].maff));
12283 if (type == isl_dim_in)
12284 res = isl_map_intersect_domain(res,
12285 isl_map_copy(pma->p[0].set));
12286 else
12287 res = isl_map_intersect_range(res,
12288 isl_map_copy(pma->p[0].set));
12290 for (i = 1; i < pma->n; ++i) {
12291 isl_map *res_i;
12293 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12294 isl_multi_aff_copy(pma->p[i].maff));
12295 if (type == isl_dim_in)
12296 res_i = isl_map_intersect_domain(res_i,
12297 isl_map_copy(pma->p[i].set));
12298 else
12299 res_i = isl_map_intersect_range(res_i,
12300 isl_map_copy(pma->p[i].set));
12301 res = isl_map_union(res, res_i);
12304 isl_pw_multi_aff_free(pma);
12305 isl_map_free(map);
12306 return res;
12307 error:
12308 isl_pw_multi_aff_free(pma);
12309 isl_map_free(map);
12310 return NULL;
12313 /* Compute the preimage of "map" under the function represented by "pma".
12314 * In other words, plug in "pma" in the domain or range of "map".
12315 * The result is a map that lives in the same space as "map",
12316 * except that the space of type "type" has been replaced by
12317 * the domain space of "pma".
12319 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12320 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12322 if (!map || !pma)
12323 goto error;
12325 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12326 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12328 if (!isl_space_has_named_params(map->dim) ||
12329 !isl_space_has_named_params(pma->dim))
12330 isl_die(map->ctx, isl_error_invalid,
12331 "unaligned unnamed parameters", goto error);
12332 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12333 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12335 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12336 error:
12337 isl_pw_multi_aff_free(pma);
12338 return isl_map_free(map);
12341 /* Compute the preimage of "set" under the function represented by "pma".
12342 * In other words, plug in "pma" in "set". The result is a set
12343 * that lives in the domain space of "pma".
12345 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12346 __isl_take isl_pw_multi_aff *pma)
12348 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12351 /* Compute the preimage of the domain of "map" under the function
12352 * represented by "pma".
12353 * In other words, plug in "pma" in the domain of "map".
12354 * The result is a map that lives in the same space as "map",
12355 * except that domain space has been replaced by the domain space of "pma".
12357 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12358 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12360 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12363 /* Compute the preimage of the range of "map" under the function
12364 * represented by "pma".
12365 * In other words, plug in "pma" in the range of "map".
12366 * The result is a map that lives in the same space as "map",
12367 * except that range space has been replaced by the domain space of "pma".
12369 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12370 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12372 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12375 /* Compute the preimage of "map" under the function represented by "mpa".
12376 * In other words, plug in "mpa" in the domain or range of "map".
12377 * The result is a map that lives in the same space as "map",
12378 * except that the space of type "type" has been replaced by
12379 * the domain space of "mpa".
12381 * If the map does not involve any constraints that refer to the
12382 * dimensions of the substituted space, then the only possible
12383 * effect of "mpa" on the map is to map the space to a different space.
12384 * We create a separate isl_multi_aff to effectuate this change
12385 * in order to avoid spurious splitting of the map along the pieces
12386 * of "mpa".
12388 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12389 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12391 int n;
12392 isl_pw_multi_aff *pma;
12394 if (!map || !mpa)
12395 goto error;
12397 n = isl_map_dim(map, type);
12398 if (!isl_map_involves_dims(map, type, 0, n)) {
12399 isl_space *space;
12400 isl_multi_aff *ma;
12402 space = isl_multi_pw_aff_get_space(mpa);
12403 isl_multi_pw_aff_free(mpa);
12404 ma = isl_multi_aff_zero(space);
12405 return isl_map_preimage_multi_aff(map, type, ma);
12408 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12409 return isl_map_preimage_pw_multi_aff(map, type, pma);
12410 error:
12411 isl_map_free(map);
12412 isl_multi_pw_aff_free(mpa);
12413 return NULL;
12416 /* Compute the preimage of "map" under the function represented by "mpa".
12417 * In other words, plug in "mpa" in the domain "map".
12418 * The result is a map that lives in the same space as "map",
12419 * except that domain space has been replaced by the domain space of "mpa".
12421 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12422 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12424 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12427 /* Compute the preimage of "set" by the function represented by "mpa".
12428 * In other words, plug in "mpa" in "set".
12430 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12431 __isl_take isl_multi_pw_aff *mpa)
12433 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);