add isl_basic_set_peek_space
[isl.git] / isl_map.c
blobfafebf3be9caa92bcc19e63dad2f66dda67e8082
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
22 #include <string.h>
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_blk.h>
26 #include <isl/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
30 #include <isl_seq.h>
31 #include <isl/set.h>
32 #include <isl/map.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
35 #include <isl_sort.h>
36 #include "isl_tab.h"
37 #include <isl/vec.h>
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
56 switch (type) {
57 case isl_dim_param: return dim->nparam;
58 case isl_dim_in: return dim->n_in;
59 case isl_dim_out: return dim->n_out;
60 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
61 default: return 0;
65 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
67 switch (type) {
68 case isl_dim_param: return 1;
69 case isl_dim_in: return 1 + dim->nparam;
70 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
71 default: return 0;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
76 enum isl_dim_type type)
78 if (!bmap)
79 return 0;
80 switch (type) {
81 case isl_dim_cst: return 1;
82 case isl_dim_param:
83 case isl_dim_in:
84 case isl_dim_out: return isl_space_dim(bmap->dim, type);
85 case isl_dim_div: return bmap->n_div;
86 case isl_dim_all: return isl_basic_map_total_dim(bmap);
87 default: return 0;
91 /* Return the space of "map".
93 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
95 return map ? map->dim : NULL;
98 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
100 return map ? n(map->dim, type) : 0;
103 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
105 return set ? n(set->dim, type) : 0;
108 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
109 enum isl_dim_type type)
111 isl_space *space;
113 if (!bmap)
114 return 0;
116 space = bmap->dim;
117 switch (type) {
118 case isl_dim_cst: return 0;
119 case isl_dim_param: return 1;
120 case isl_dim_in: return 1 + space->nparam;
121 case isl_dim_out: return 1 + space->nparam + space->n_in;
122 case isl_dim_div: return 1 + space->nparam + space->n_in +
123 space->n_out;
124 default: return 0;
128 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
129 enum isl_dim_type type)
131 return isl_basic_map_offset(bset, type);
134 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
136 return pos(map->dim, type);
139 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
140 enum isl_dim_type type)
142 return isl_basic_map_dim(bset, type);
145 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
147 return isl_basic_set_dim(bset, isl_dim_set);
150 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
152 return isl_basic_set_dim(bset, isl_dim_param);
155 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
157 if (!bset)
158 return 0;
159 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
162 unsigned isl_set_n_dim(__isl_keep isl_set *set)
164 return isl_set_dim(set, isl_dim_set);
167 unsigned isl_set_n_param(__isl_keep isl_set *set)
169 return isl_set_dim(set, isl_dim_param);
172 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
174 return bmap ? bmap->dim->n_in : 0;
177 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
179 return bmap ? bmap->dim->n_out : 0;
182 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
184 return bmap ? bmap->dim->nparam : 0;
187 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
189 return bmap ? bmap->n_div : 0;
192 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
194 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
197 unsigned isl_map_n_in(const struct isl_map *map)
199 return map ? map->dim->n_in : 0;
202 unsigned isl_map_n_out(const struct isl_map *map)
204 return map ? map->dim->n_out : 0;
207 unsigned isl_map_n_param(const struct isl_map *map)
209 return map ? map->dim->nparam : 0;
212 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
213 __isl_keep isl_set *set)
215 isl_bool m;
216 if (!map || !set)
217 return isl_bool_error;
218 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
219 if (m < 0 || !m)
220 return m;
221 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
222 set->dim, isl_dim_set);
225 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
226 __isl_keep isl_basic_set *bset)
228 isl_bool m;
229 if (!bmap || !bset)
230 return isl_bool_error;
231 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
232 if (m < 0 || !m)
233 return m;
234 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
235 bset->dim, isl_dim_set);
238 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
239 __isl_keep isl_set *set)
241 isl_bool m;
242 if (!map || !set)
243 return isl_bool_error;
244 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
245 if (m < 0 || !m)
246 return m;
247 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
248 set->dim, isl_dim_set);
251 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
252 __isl_keep isl_basic_set *bset)
254 isl_bool m;
255 if (!bmap || !bset)
256 return isl_bool_error;
257 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
258 if (m < 0 || !m)
259 return m;
260 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
261 bset->dim, isl_dim_set);
264 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
266 return bmap ? bmap->ctx : NULL;
269 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
271 return bset ? bset->ctx : NULL;
274 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
276 return map ? map->ctx : NULL;
279 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
281 return set ? set->ctx : NULL;
284 /* Return the space of "bmap".
286 __isl_keep isl_space *isl_basic_map_peek_space(
287 __isl_keep const isl_basic_map *bmap)
289 return bmap ? bmap->dim : NULL;
292 /* Return the space of "bset".
294 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
296 return isl_basic_map_peek_space(bset_to_bmap(bset));
299 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
301 return isl_space_copy(isl_basic_map_peek_space(bmap));
304 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
306 return isl_basic_map_get_space(bset_to_bmap(bset));
309 /* Extract the divs in "bmap" as a matrix.
311 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
313 int i;
314 isl_ctx *ctx;
315 isl_mat *div;
316 unsigned total;
317 unsigned cols;
319 if (!bmap)
320 return NULL;
322 ctx = isl_basic_map_get_ctx(bmap);
323 total = isl_space_dim(bmap->dim, isl_dim_all);
324 cols = 1 + 1 + total + bmap->n_div;
325 div = isl_mat_alloc(ctx, bmap->n_div, cols);
326 if (!div)
327 return NULL;
329 for (i = 0; i < bmap->n_div; ++i)
330 isl_seq_cpy(div->row[i], bmap->div[i], cols);
332 return div;
335 /* Extract the divs in "bset" as a matrix.
337 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
339 return isl_basic_map_get_divs(bset);
342 __isl_give isl_local_space *isl_basic_map_get_local_space(
343 __isl_keep isl_basic_map *bmap)
345 isl_mat *div;
347 if (!bmap)
348 return NULL;
350 div = isl_basic_map_get_divs(bmap);
351 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
354 __isl_give isl_local_space *isl_basic_set_get_local_space(
355 __isl_keep isl_basic_set *bset)
357 return isl_basic_map_get_local_space(bset);
360 /* For each known div d = floor(f/m), add the constraints
362 * f - m d >= 0
363 * -(f-(m-1)) + m d >= 0
365 * Do not finalize the result.
367 static __isl_give isl_basic_map *add_known_div_constraints(
368 __isl_take isl_basic_map *bmap)
370 int i;
371 unsigned n_div;
373 if (!bmap)
374 return NULL;
375 n_div = isl_basic_map_dim(bmap, isl_dim_div);
376 if (n_div == 0)
377 return bmap;
378 bmap = isl_basic_map_cow(bmap);
379 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
380 if (!bmap)
381 return NULL;
382 for (i = 0; i < n_div; ++i) {
383 if (isl_int_is_zero(bmap->div[i][0]))
384 continue;
385 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
386 return isl_basic_map_free(bmap);
389 return bmap;
392 __isl_give isl_basic_map *isl_basic_map_from_local_space(
393 __isl_take isl_local_space *ls)
395 int i;
396 int n_div;
397 isl_basic_map *bmap;
399 if (!ls)
400 return NULL;
402 n_div = isl_local_space_dim(ls, isl_dim_div);
403 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
404 n_div, 0, 2 * n_div);
406 for (i = 0; i < n_div; ++i)
407 if (isl_basic_map_alloc_div(bmap) < 0)
408 goto error;
410 for (i = 0; i < n_div; ++i)
411 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
412 bmap = add_known_div_constraints(bmap);
414 isl_local_space_free(ls);
415 return bmap;
416 error:
417 isl_local_space_free(ls);
418 isl_basic_map_free(bmap);
419 return NULL;
422 __isl_give isl_basic_set *isl_basic_set_from_local_space(
423 __isl_take isl_local_space *ls)
425 return isl_basic_map_from_local_space(ls);
428 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
430 return isl_space_copy(isl_map_peek_space(map));
433 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
435 if (!set)
436 return NULL;
437 return isl_space_copy(set->dim);
440 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
441 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
443 bmap = isl_basic_map_cow(bmap);
444 if (!bmap)
445 return NULL;
446 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
447 if (!bmap->dim)
448 goto error;
449 bmap = isl_basic_map_finalize(bmap);
450 return bmap;
451 error:
452 isl_basic_map_free(bmap);
453 return NULL;
456 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
457 __isl_take isl_basic_set *bset, const char *s)
459 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
462 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
463 enum isl_dim_type type)
465 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
468 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
469 enum isl_dim_type type, const char *s)
471 int i;
473 map = isl_map_cow(map);
474 if (!map)
475 return NULL;
477 map->dim = isl_space_set_tuple_name(map->dim, type, s);
478 if (!map->dim)
479 goto error;
481 for (i = 0; i < map->n; ++i) {
482 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
483 if (!map->p[i])
484 goto error;
487 return map;
488 error:
489 isl_map_free(map);
490 return NULL;
493 /* Replace the identifier of the tuple of type "type" by "id".
495 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
496 __isl_take isl_basic_map *bmap,
497 enum isl_dim_type type, __isl_take isl_id *id)
499 bmap = isl_basic_map_cow(bmap);
500 if (!bmap)
501 goto error;
502 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
503 if (!bmap->dim)
504 return isl_basic_map_free(bmap);
505 bmap = isl_basic_map_finalize(bmap);
506 return bmap;
507 error:
508 isl_id_free(id);
509 return NULL;
512 /* Replace the identifier of the tuple by "id".
514 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
515 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
517 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
520 /* Does the input or output tuple have a name?
522 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
524 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
527 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
528 enum isl_dim_type type)
530 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
533 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
534 const char *s)
536 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
537 isl_dim_set, s));
540 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
541 enum isl_dim_type type, __isl_take isl_id *id)
543 map = isl_map_cow(map);
544 if (!map)
545 goto error;
547 map->dim = isl_space_set_tuple_id(map->dim, type, id);
549 return isl_map_reset_space(map, isl_space_copy(map->dim));
550 error:
551 isl_id_free(id);
552 return NULL;
555 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
556 __isl_take isl_id *id)
558 return isl_map_set_tuple_id(set, isl_dim_set, id);
561 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
562 enum isl_dim_type type)
564 map = isl_map_cow(map);
565 if (!map)
566 return NULL;
568 map->dim = isl_space_reset_tuple_id(map->dim, type);
570 return isl_map_reset_space(map, isl_space_copy(map->dim));
573 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
575 return isl_map_reset_tuple_id(set, isl_dim_set);
578 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
580 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
583 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
584 enum isl_dim_type type)
586 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
589 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
591 return isl_map_has_tuple_id(set, isl_dim_set);
594 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
596 return isl_map_get_tuple_id(set, isl_dim_set);
599 /* Does the set tuple have a name?
601 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
603 if (!set)
604 return isl_bool_error;
605 return isl_space_has_tuple_name(set->dim, isl_dim_set);
609 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
611 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
614 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
616 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
619 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
620 enum isl_dim_type type, unsigned pos)
622 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
625 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
626 enum isl_dim_type type, unsigned pos)
628 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
631 /* Does the given dimension have a name?
633 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
634 enum isl_dim_type type, unsigned pos)
636 if (!map)
637 return isl_bool_error;
638 return isl_space_has_dim_name(map->dim, type, pos);
641 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
642 enum isl_dim_type type, unsigned pos)
644 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
647 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
648 enum isl_dim_type type, unsigned pos)
650 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
653 /* Does the given dimension have a name?
655 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
656 enum isl_dim_type type, unsigned pos)
658 if (!set)
659 return isl_bool_error;
660 return isl_space_has_dim_name(set->dim, type, pos);
663 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
664 __isl_take isl_basic_map *bmap,
665 enum isl_dim_type type, unsigned pos, const char *s)
667 bmap = isl_basic_map_cow(bmap);
668 if (!bmap)
669 return NULL;
670 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
671 if (!bmap->dim)
672 goto error;
673 return isl_basic_map_finalize(bmap);
674 error:
675 isl_basic_map_free(bmap);
676 return NULL;
679 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
680 enum isl_dim_type type, unsigned pos, const char *s)
682 int i;
684 map = isl_map_cow(map);
685 if (!map)
686 return NULL;
688 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
689 if (!map->dim)
690 goto error;
692 for (i = 0; i < map->n; ++i) {
693 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
694 if (!map->p[i])
695 goto error;
698 return map;
699 error:
700 isl_map_free(map);
701 return NULL;
704 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
705 __isl_take isl_basic_set *bset,
706 enum isl_dim_type type, unsigned pos, const char *s)
708 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
709 type, pos, s));
712 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
713 enum isl_dim_type type, unsigned pos, const char *s)
715 return set_from_map(isl_map_set_dim_name(set_to_map(set),
716 type, pos, s));
719 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
720 enum isl_dim_type type, unsigned pos)
722 if (!bmap)
723 return isl_bool_error;
724 return isl_space_has_dim_id(bmap->dim, type, pos);
727 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
728 enum isl_dim_type type, unsigned pos)
730 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
733 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
734 enum isl_dim_type type, unsigned pos)
736 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
739 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
740 enum isl_dim_type type, unsigned pos)
742 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
745 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
746 enum isl_dim_type type, unsigned pos)
748 return isl_map_has_dim_id(set, type, pos);
751 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
752 enum isl_dim_type type, unsigned pos)
754 return isl_map_get_dim_id(set, type, pos);
757 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
758 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
760 map = isl_map_cow(map);
761 if (!map)
762 goto error;
764 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
766 return isl_map_reset_space(map, isl_space_copy(map->dim));
767 error:
768 isl_id_free(id);
769 return NULL;
772 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
773 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
775 return isl_map_set_dim_id(set, type, pos, id);
778 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
779 __isl_keep isl_id *id)
781 if (!map)
782 return -1;
783 return isl_space_find_dim_by_id(map->dim, type, id);
786 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
787 __isl_keep isl_id *id)
789 return isl_map_find_dim_by_id(set, type, id);
792 /* Return the position of the dimension of the given type and name
793 * in "bmap".
794 * Return -1 if no such dimension can be found.
796 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
797 enum isl_dim_type type, const char *name)
799 if (!bmap)
800 return -1;
801 return isl_space_find_dim_by_name(bmap->dim, type, name);
804 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
805 const char *name)
807 if (!map)
808 return -1;
809 return isl_space_find_dim_by_name(map->dim, type, name);
812 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
813 const char *name)
815 return isl_map_find_dim_by_name(set, type, name);
818 /* Reset the user pointer on all identifiers of parameters and tuples
819 * of the space of "map".
821 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
823 isl_space *space;
825 space = isl_map_get_space(map);
826 space = isl_space_reset_user(space);
827 map = isl_map_reset_space(map, space);
829 return map;
832 /* Reset the user pointer on all identifiers of parameters and tuples
833 * of the space of "set".
835 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
837 return isl_map_reset_user(set);
840 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
842 if (!bmap)
843 return isl_bool_error;
844 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
847 /* Has "map" been marked as a rational map?
848 * In particular, have all basic maps in "map" been marked this way?
849 * An empty map is not considered to be rational.
850 * Maps where only some of the basic maps are marked rational
851 * are not allowed.
853 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
855 int i;
856 isl_bool rational;
858 if (!map)
859 return isl_bool_error;
860 if (map->n == 0)
861 return isl_bool_false;
862 rational = isl_basic_map_is_rational(map->p[0]);
863 if (rational < 0)
864 return rational;
865 for (i = 1; i < map->n; ++i) {
866 isl_bool rational_i;
868 rational_i = isl_basic_map_is_rational(map->p[i]);
869 if (rational_i < 0)
870 return rational_i;
871 if (rational != rational_i)
872 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
873 "mixed rational and integer basic maps "
874 "not supported", return isl_bool_error);
877 return rational;
880 /* Has "set" been marked as a rational set?
881 * In particular, have all basic set in "set" been marked this way?
882 * An empty set is not considered to be rational.
883 * Sets where only some of the basic sets are marked rational
884 * are not allowed.
886 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
888 return isl_map_is_rational(set);
891 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
893 return isl_basic_map_is_rational(bset);
896 /* Does "bmap" contain any rational points?
898 * If "bmap" has an equality for each dimension, equating the dimension
899 * to an integer constant, then it has no rational points, even if it
900 * is marked as rational.
902 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
904 isl_bool has_rational = isl_bool_true;
905 unsigned total;
907 if (!bmap)
908 return isl_bool_error;
909 if (isl_basic_map_plain_is_empty(bmap))
910 return isl_bool_false;
911 if (!isl_basic_map_is_rational(bmap))
912 return isl_bool_false;
913 bmap = isl_basic_map_copy(bmap);
914 bmap = isl_basic_map_implicit_equalities(bmap);
915 if (!bmap)
916 return isl_bool_error;
917 total = isl_basic_map_total_dim(bmap);
918 if (bmap->n_eq == total) {
919 int i, j;
920 for (i = 0; i < bmap->n_eq; ++i) {
921 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
922 if (j < 0)
923 break;
924 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
925 !isl_int_is_negone(bmap->eq[i][1 + j]))
926 break;
927 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
928 total - j - 1);
929 if (j >= 0)
930 break;
932 if (i == bmap->n_eq)
933 has_rational = isl_bool_false;
935 isl_basic_map_free(bmap);
937 return has_rational;
940 /* Does "map" contain any rational points?
942 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
944 int i;
945 isl_bool has_rational;
947 if (!map)
948 return isl_bool_error;
949 for (i = 0; i < map->n; ++i) {
950 has_rational = isl_basic_map_has_rational(map->p[i]);
951 if (has_rational < 0 || has_rational)
952 return has_rational;
954 return isl_bool_false;
957 /* Does "set" contain any rational points?
959 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
961 return isl_map_has_rational(set);
964 /* Is this basic set a parameter domain?
966 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
968 if (!bset)
969 return isl_bool_error;
970 return isl_space_is_params(bset->dim);
973 /* Is this set a parameter domain?
975 isl_bool isl_set_is_params(__isl_keep isl_set *set)
977 if (!set)
978 return isl_bool_error;
979 return isl_space_is_params(set->dim);
982 /* Is this map actually a parameter domain?
983 * Users should never call this function. Outside of isl,
984 * a map can never be a parameter domain.
986 isl_bool isl_map_is_params(__isl_keep isl_map *map)
988 if (!map)
989 return isl_bool_error;
990 return isl_space_is_params(map->dim);
993 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
994 struct isl_basic_map *bmap, unsigned extra,
995 unsigned n_eq, unsigned n_ineq)
997 int i;
998 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1000 bmap->ctx = ctx;
1001 isl_ctx_ref(ctx);
1003 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1004 if (isl_blk_is_error(bmap->block))
1005 goto error;
1007 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1008 if ((n_ineq + n_eq) && !bmap->ineq)
1009 goto error;
1011 if (extra == 0) {
1012 bmap->block2 = isl_blk_empty();
1013 bmap->div = NULL;
1014 } else {
1015 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1016 if (isl_blk_is_error(bmap->block2))
1017 goto error;
1019 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1020 if (!bmap->div)
1021 goto error;
1024 for (i = 0; i < n_ineq + n_eq; ++i)
1025 bmap->ineq[i] = bmap->block.data + i * row_size;
1027 for (i = 0; i < extra; ++i)
1028 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1030 bmap->ref = 1;
1031 bmap->flags = 0;
1032 bmap->c_size = n_eq + n_ineq;
1033 bmap->eq = bmap->ineq + n_ineq;
1034 bmap->extra = extra;
1035 bmap->n_eq = 0;
1036 bmap->n_ineq = 0;
1037 bmap->n_div = 0;
1038 bmap->sample = NULL;
1040 return bmap;
1041 error:
1042 isl_basic_map_free(bmap);
1043 return NULL;
1046 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1047 unsigned nparam, unsigned dim, unsigned extra,
1048 unsigned n_eq, unsigned n_ineq)
1050 struct isl_basic_map *bmap;
1051 isl_space *space;
1053 space = isl_space_set_alloc(ctx, nparam, dim);
1054 if (!space)
1055 return NULL;
1057 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1058 return bset_from_bmap(bmap);
1061 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1062 unsigned extra, unsigned n_eq, unsigned n_ineq)
1064 struct isl_basic_map *bmap;
1065 if (!dim)
1066 return NULL;
1067 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1068 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1069 return bset_from_bmap(bmap);
1070 error:
1071 isl_space_free(dim);
1072 return NULL;
1075 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1076 unsigned extra, unsigned n_eq, unsigned n_ineq)
1078 struct isl_basic_map *bmap;
1080 if (!dim)
1081 return NULL;
1082 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1083 if (!bmap)
1084 goto error;
1085 bmap->dim = dim;
1087 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1088 error:
1089 isl_space_free(dim);
1090 return NULL;
1093 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1094 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1095 unsigned n_eq, unsigned n_ineq)
1097 struct isl_basic_map *bmap;
1098 isl_space *dim;
1100 dim = isl_space_alloc(ctx, nparam, in, out);
1101 if (!dim)
1102 return NULL;
1104 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1105 return bmap;
1108 static void dup_constraints(
1109 struct isl_basic_map *dst, struct isl_basic_map *src)
1111 int i;
1112 unsigned total = isl_basic_map_total_dim(src);
1114 for (i = 0; i < src->n_eq; ++i) {
1115 int j = isl_basic_map_alloc_equality(dst);
1116 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1119 for (i = 0; i < src->n_ineq; ++i) {
1120 int j = isl_basic_map_alloc_inequality(dst);
1121 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1124 for (i = 0; i < src->n_div; ++i) {
1125 int j = isl_basic_map_alloc_div(dst);
1126 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1128 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1131 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1133 struct isl_basic_map *dup;
1135 if (!bmap)
1136 return NULL;
1137 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1138 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1139 if (!dup)
1140 return NULL;
1141 dup_constraints(dup, bmap);
1142 dup->flags = bmap->flags;
1143 dup->sample = isl_vec_copy(bmap->sample);
1144 return dup;
1147 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1149 struct isl_basic_map *dup;
1151 dup = isl_basic_map_dup(bset_to_bmap(bset));
1152 return bset_from_bmap(dup);
1155 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1157 if (!bset)
1158 return NULL;
1160 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1161 bset->ref++;
1162 return bset;
1164 return isl_basic_set_dup(bset);
1167 struct isl_set *isl_set_copy(struct isl_set *set)
1169 if (!set)
1170 return NULL;
1172 set->ref++;
1173 return set;
1176 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1178 if (!bmap)
1179 return NULL;
1181 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1182 bmap->ref++;
1183 return bmap;
1185 bmap = isl_basic_map_dup(bmap);
1186 if (bmap)
1187 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1188 return bmap;
1191 struct isl_map *isl_map_copy(struct isl_map *map)
1193 if (!map)
1194 return NULL;
1196 map->ref++;
1197 return map;
1200 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1202 if (!bmap)
1203 return NULL;
1205 if (--bmap->ref > 0)
1206 return NULL;
1208 isl_ctx_deref(bmap->ctx);
1209 free(bmap->div);
1210 isl_blk_free(bmap->ctx, bmap->block2);
1211 free(bmap->ineq);
1212 isl_blk_free(bmap->ctx, bmap->block);
1213 isl_vec_free(bmap->sample);
1214 isl_space_free(bmap->dim);
1215 free(bmap);
1217 return NULL;
1220 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1222 return isl_basic_map_free(bset_to_bmap(bset));
1225 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1227 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1230 __isl_give isl_map *isl_map_align_params_map_map_and(
1231 __isl_take isl_map *map1, __isl_take isl_map *map2,
1232 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1233 __isl_take isl_map *map2))
1235 if (!map1 || !map2)
1236 goto error;
1237 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1238 return fn(map1, map2);
1239 if (!isl_space_has_named_params(map1->dim) ||
1240 !isl_space_has_named_params(map2->dim))
1241 isl_die(map1->ctx, isl_error_invalid,
1242 "unaligned unnamed parameters", goto error);
1243 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1244 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1245 return fn(map1, map2);
1246 error:
1247 isl_map_free(map1);
1248 isl_map_free(map2);
1249 return NULL;
1252 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1253 __isl_keep isl_map *map2,
1254 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1256 isl_bool r;
1258 if (!map1 || !map2)
1259 return isl_bool_error;
1260 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1261 return fn(map1, map2);
1262 if (!isl_space_has_named_params(map1->dim) ||
1263 !isl_space_has_named_params(map2->dim))
1264 isl_die(map1->ctx, isl_error_invalid,
1265 "unaligned unnamed parameters", return isl_bool_error);
1266 map1 = isl_map_copy(map1);
1267 map2 = isl_map_copy(map2);
1268 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1269 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1270 r = fn(map1, map2);
1271 isl_map_free(map1);
1272 isl_map_free(map2);
1273 return r;
1276 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1278 struct isl_ctx *ctx;
1279 if (!bmap)
1280 return -1;
1281 ctx = bmap->ctx;
1282 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1283 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1284 return -1);
1285 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1286 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1287 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1288 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1289 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1290 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1291 isl_int *t;
1292 int j = isl_basic_map_alloc_inequality(bmap);
1293 if (j < 0)
1294 return -1;
1295 t = bmap->ineq[j];
1296 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1297 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1298 bmap->eq[-1] = t;
1299 bmap->n_eq++;
1300 bmap->n_ineq--;
1301 bmap->eq--;
1302 return 0;
1304 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1305 bmap->extra - bmap->n_div);
1306 return bmap->n_eq++;
1309 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1311 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1314 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1316 if (!bmap)
1317 return -1;
1318 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1319 bmap->n_eq -= n;
1320 return 0;
1323 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1325 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1328 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1330 isl_int *t;
1331 if (!bmap)
1332 return -1;
1333 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1335 if (pos != bmap->n_eq - 1) {
1336 t = bmap->eq[pos];
1337 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1338 bmap->eq[bmap->n_eq - 1] = t;
1340 bmap->n_eq--;
1341 return 0;
1344 /* Turn inequality "pos" of "bmap" into an equality.
1346 * In particular, we move the inequality in front of the equalities
1347 * and move the last inequality in the position of the moved inequality.
1348 * Note that isl_tab_make_equalities_explicit depends on this particular
1349 * change in the ordering of the constraints.
1351 void isl_basic_map_inequality_to_equality(
1352 struct isl_basic_map *bmap, unsigned pos)
1354 isl_int *t;
1356 t = bmap->ineq[pos];
1357 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1358 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1359 bmap->eq[-1] = t;
1360 bmap->n_eq++;
1361 bmap->n_ineq--;
1362 bmap->eq--;
1363 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1364 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1365 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1366 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1369 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1371 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1374 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1376 struct isl_ctx *ctx;
1377 if (!bmap)
1378 return -1;
1379 ctx = bmap->ctx;
1380 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1381 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1382 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1383 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1384 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1385 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1386 1 + isl_basic_map_total_dim(bmap),
1387 bmap->extra - bmap->n_div);
1388 return bmap->n_ineq++;
1391 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1393 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1396 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1398 if (!bmap)
1399 return -1;
1400 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1401 bmap->n_ineq -= n;
1402 return 0;
1405 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1407 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1410 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1412 isl_int *t;
1413 if (!bmap)
1414 return -1;
1415 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1417 if (pos != bmap->n_ineq - 1) {
1418 t = bmap->ineq[pos];
1419 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1420 bmap->ineq[bmap->n_ineq - 1] = t;
1421 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1423 bmap->n_ineq--;
1424 return 0;
1427 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1429 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1432 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1433 isl_int *eq)
1435 int k;
1437 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1438 if (!bmap)
1439 return NULL;
1440 k = isl_basic_map_alloc_equality(bmap);
1441 if (k < 0)
1442 goto error;
1443 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1444 return bmap;
1445 error:
1446 isl_basic_map_free(bmap);
1447 return NULL;
1450 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1451 isl_int *eq)
1453 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1456 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1457 isl_int *ineq)
1459 int k;
1461 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1462 if (!bmap)
1463 return NULL;
1464 k = isl_basic_map_alloc_inequality(bmap);
1465 if (k < 0)
1466 goto error;
1467 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1468 return bmap;
1469 error:
1470 isl_basic_map_free(bmap);
1471 return NULL;
1474 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1475 isl_int *ineq)
1477 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1480 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1482 if (!bmap)
1483 return -1;
1484 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1485 isl_seq_clr(bmap->div[bmap->n_div] +
1486 1 + 1 + isl_basic_map_total_dim(bmap),
1487 bmap->extra - bmap->n_div);
1488 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1489 return bmap->n_div++;
1492 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1494 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1497 /* Check that there are "n" dimensions of type "type" starting at "first"
1498 * in "bmap".
1500 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1501 enum isl_dim_type type, unsigned first, unsigned n)
1503 unsigned dim;
1505 if (!bmap)
1506 return isl_stat_error;
1507 dim = isl_basic_map_dim(bmap, type);
1508 if (first + n > dim || first + n < first)
1509 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1510 "position or range out of bounds",
1511 return isl_stat_error);
1512 return isl_stat_ok;
1515 /* Insert an extra integer division, prescribed by "div", to "bmap"
1516 * at (integer division) position "pos".
1518 * The integer division is first added at the end and then moved
1519 * into the right position.
1521 __isl_give isl_basic_map *isl_basic_map_insert_div(
1522 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1524 int i, k;
1526 bmap = isl_basic_map_cow(bmap);
1527 if (!bmap || !div)
1528 return isl_basic_map_free(bmap);
1530 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1531 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1532 "unexpected size", return isl_basic_map_free(bmap));
1533 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1534 return isl_basic_map_free(bmap);
1536 bmap = isl_basic_map_extend_space(bmap,
1537 isl_basic_map_get_space(bmap), 1, 0, 2);
1538 k = isl_basic_map_alloc_div(bmap);
1539 if (k < 0)
1540 return isl_basic_map_free(bmap);
1541 isl_seq_cpy(bmap->div[k], div->el, div->size);
1542 isl_int_set_si(bmap->div[k][div->size], 0);
1544 for (i = k; i > pos; --i)
1545 isl_basic_map_swap_div(bmap, i, i - 1);
1547 return bmap;
1550 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1552 if (!bmap)
1553 return isl_stat_error;
1554 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1555 bmap->n_div -= n;
1556 return isl_stat_ok;
1559 /* Copy constraint from src to dst, putting the vars of src at offset
1560 * dim_off in dst and the divs of src at offset div_off in dst.
1561 * If both sets are actually map, then dim_off applies to the input
1562 * variables.
1564 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1565 struct isl_basic_map *src_map, isl_int *src,
1566 unsigned in_off, unsigned out_off, unsigned div_off)
1568 unsigned src_nparam = isl_basic_map_n_param(src_map);
1569 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1570 unsigned src_in = isl_basic_map_n_in(src_map);
1571 unsigned dst_in = isl_basic_map_n_in(dst_map);
1572 unsigned src_out = isl_basic_map_n_out(src_map);
1573 unsigned dst_out = isl_basic_map_n_out(dst_map);
1574 isl_int_set(dst[0], src[0]);
1575 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1576 if (dst_nparam > src_nparam)
1577 isl_seq_clr(dst+1+src_nparam,
1578 dst_nparam - src_nparam);
1579 isl_seq_clr(dst+1+dst_nparam, in_off);
1580 isl_seq_cpy(dst+1+dst_nparam+in_off,
1581 src+1+src_nparam,
1582 isl_min(dst_in-in_off, src_in));
1583 if (dst_in-in_off > src_in)
1584 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1585 dst_in - in_off - src_in);
1586 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1587 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1588 src+1+src_nparam+src_in,
1589 isl_min(dst_out-out_off, src_out));
1590 if (dst_out-out_off > src_out)
1591 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1592 dst_out - out_off - src_out);
1593 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1594 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1595 src+1+src_nparam+src_in+src_out,
1596 isl_min(dst_map->extra-div_off, src_map->n_div));
1597 if (dst_map->n_div-div_off > src_map->n_div)
1598 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1599 div_off+src_map->n_div,
1600 dst_map->n_div - div_off - src_map->n_div);
1603 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1604 struct isl_basic_map *src_map, isl_int *src,
1605 unsigned in_off, unsigned out_off, unsigned div_off)
1607 isl_int_set(dst[0], src[0]);
1608 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1611 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1612 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1614 int i;
1615 unsigned div_off;
1617 if (!bmap1 || !bmap2)
1618 goto error;
1620 div_off = bmap1->n_div;
1622 for (i = 0; i < bmap2->n_eq; ++i) {
1623 int i1 = isl_basic_map_alloc_equality(bmap1);
1624 if (i1 < 0)
1625 goto error;
1626 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1627 i_pos, o_pos, div_off);
1630 for (i = 0; i < bmap2->n_ineq; ++i) {
1631 int i1 = isl_basic_map_alloc_inequality(bmap1);
1632 if (i1 < 0)
1633 goto error;
1634 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1635 i_pos, o_pos, div_off);
1638 for (i = 0; i < bmap2->n_div; ++i) {
1639 int i1 = isl_basic_map_alloc_div(bmap1);
1640 if (i1 < 0)
1641 goto error;
1642 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1643 i_pos, o_pos, div_off);
1646 isl_basic_map_free(bmap2);
1648 return bmap1;
1650 error:
1651 isl_basic_map_free(bmap1);
1652 isl_basic_map_free(bmap2);
1653 return NULL;
1656 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1657 struct isl_basic_set *bset2, unsigned pos)
1659 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1660 bset_to_bmap(bset2), 0, pos));
1663 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1664 __isl_take isl_space *dim, unsigned extra,
1665 unsigned n_eq, unsigned n_ineq)
1667 struct isl_basic_map *ext;
1668 unsigned flags;
1669 int dims_ok;
1671 if (!dim)
1672 goto error;
1674 if (!base)
1675 goto error;
1677 dims_ok = isl_space_is_equal(base->dim, dim) &&
1678 base->extra >= base->n_div + extra;
1680 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1681 room_for_ineq(base, n_ineq)) {
1682 isl_space_free(dim);
1683 return base;
1686 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1687 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1688 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1689 extra += base->extra;
1690 n_eq += base->n_eq;
1691 n_ineq += base->n_ineq;
1693 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1694 dim = NULL;
1695 if (!ext)
1696 goto error;
1698 if (dims_ok)
1699 ext->sample = isl_vec_copy(base->sample);
1700 flags = base->flags;
1701 ext = add_constraints(ext, base, 0, 0);
1702 if (ext) {
1703 ext->flags = flags;
1704 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1707 return ext;
1709 error:
1710 isl_space_free(dim);
1711 isl_basic_map_free(base);
1712 return NULL;
1715 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1716 __isl_take isl_space *dim, unsigned extra,
1717 unsigned n_eq, unsigned n_ineq)
1719 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1720 dim, extra, n_eq, n_ineq));
1723 struct isl_basic_map *isl_basic_map_extend_constraints(
1724 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1726 if (!base)
1727 return NULL;
1728 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1729 0, n_eq, n_ineq);
1732 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1733 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1734 unsigned n_eq, unsigned n_ineq)
1736 struct isl_basic_map *bmap;
1737 isl_space *dim;
1739 if (!base)
1740 return NULL;
1741 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1742 if (!dim)
1743 goto error;
1745 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1746 return bmap;
1747 error:
1748 isl_basic_map_free(base);
1749 return NULL;
1752 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1753 unsigned nparam, unsigned dim, unsigned extra,
1754 unsigned n_eq, unsigned n_ineq)
1756 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1757 nparam, 0, dim, extra, n_eq, n_ineq));
1760 struct isl_basic_set *isl_basic_set_extend_constraints(
1761 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1763 isl_basic_map *bmap = bset_to_bmap(base);
1764 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1765 return bset_from_bmap(bmap);
1768 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1770 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1773 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1775 if (!bmap)
1776 return NULL;
1778 if (bmap->ref > 1) {
1779 bmap->ref--;
1780 bmap = isl_basic_map_dup(bmap);
1782 if (bmap) {
1783 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1784 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1786 return bmap;
1789 /* Clear all cached information in "map", either because it is about
1790 * to be modified or because it is being freed.
1791 * Always return the same pointer that is passed in.
1792 * This is needed for the use in isl_map_free.
1794 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1796 isl_basic_map_free(map->cached_simple_hull[0]);
1797 isl_basic_map_free(map->cached_simple_hull[1]);
1798 map->cached_simple_hull[0] = NULL;
1799 map->cached_simple_hull[1] = NULL;
1800 return map;
1803 struct isl_set *isl_set_cow(struct isl_set *set)
1805 return isl_map_cow(set);
1808 /* Return an isl_map that is equal to "map" and that has only
1809 * a single reference.
1811 * If the original input already has only one reference, then
1812 * simply return it, but clear all cached information, since
1813 * it may be rendered invalid by the operations that will be
1814 * performed on the result.
1816 * Otherwise, create a duplicate (without any cached information).
1818 struct isl_map *isl_map_cow(struct isl_map *map)
1820 if (!map)
1821 return NULL;
1823 if (map->ref == 1)
1824 return clear_caches(map);
1825 map->ref--;
1826 return isl_map_dup(map);
1829 static void swap_vars(struct isl_blk blk, isl_int *a,
1830 unsigned a_len, unsigned b_len)
1832 isl_seq_cpy(blk.data, a+a_len, b_len);
1833 isl_seq_cpy(blk.data+b_len, a, a_len);
1834 isl_seq_cpy(a, blk.data, b_len+a_len);
1837 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1838 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1840 int i;
1841 struct isl_blk blk;
1843 if (!bmap)
1844 goto error;
1846 isl_assert(bmap->ctx,
1847 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1849 if (n1 == 0 || n2 == 0)
1850 return bmap;
1852 bmap = isl_basic_map_cow(bmap);
1853 if (!bmap)
1854 return NULL;
1856 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1857 if (isl_blk_is_error(blk))
1858 goto error;
1860 for (i = 0; i < bmap->n_eq; ++i)
1861 swap_vars(blk,
1862 bmap->eq[i] + pos, n1, n2);
1864 for (i = 0; i < bmap->n_ineq; ++i)
1865 swap_vars(blk,
1866 bmap->ineq[i] + pos, n1, n2);
1868 for (i = 0; i < bmap->n_div; ++i)
1869 swap_vars(blk,
1870 bmap->div[i]+1 + pos, n1, n2);
1872 isl_blk_free(bmap->ctx, blk);
1874 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1875 bmap = isl_basic_map_gauss(bmap, NULL);
1876 return isl_basic_map_finalize(bmap);
1877 error:
1878 isl_basic_map_free(bmap);
1879 return NULL;
1882 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1884 int i = 0;
1885 unsigned total;
1886 if (!bmap)
1887 goto error;
1888 total = isl_basic_map_total_dim(bmap);
1889 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
1890 return isl_basic_map_free(bmap);
1891 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1892 if (bmap->n_eq > 0)
1893 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1894 else {
1895 i = isl_basic_map_alloc_equality(bmap);
1896 if (i < 0)
1897 goto error;
1899 isl_int_set_si(bmap->eq[i][0], 1);
1900 isl_seq_clr(bmap->eq[i]+1, total);
1901 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1902 isl_vec_free(bmap->sample);
1903 bmap->sample = NULL;
1904 return isl_basic_map_finalize(bmap);
1905 error:
1906 isl_basic_map_free(bmap);
1907 return NULL;
1910 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1912 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
1915 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1916 * of "bmap").
1918 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1920 isl_int *t = bmap->div[a];
1921 bmap->div[a] = bmap->div[b];
1922 bmap->div[b] = t;
1925 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1926 * div definitions accordingly.
1928 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1930 int i;
1931 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1933 swap_div(bmap, a, b);
1935 for (i = 0; i < bmap->n_eq; ++i)
1936 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1938 for (i = 0; i < bmap->n_ineq; ++i)
1939 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1941 for (i = 0; i < bmap->n_div; ++i)
1942 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1943 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1946 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1947 * div definitions accordingly.
1949 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1951 isl_basic_map_swap_div(bset, a, b);
1954 /* Eliminate the specified n dimensions starting at first from the
1955 * constraints, without removing the dimensions from the space.
1956 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1958 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1959 enum isl_dim_type type, unsigned first, unsigned n)
1961 int i;
1963 if (!map)
1964 return NULL;
1965 if (n == 0)
1966 return map;
1968 if (first + n > isl_map_dim(map, type) || first + n < first)
1969 isl_die(map->ctx, isl_error_invalid,
1970 "index out of bounds", goto error);
1972 map = isl_map_cow(map);
1973 if (!map)
1974 return NULL;
1976 for (i = 0; i < map->n; ++i) {
1977 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1978 if (!map->p[i])
1979 goto error;
1981 return map;
1982 error:
1983 isl_map_free(map);
1984 return NULL;
1987 /* Eliminate the specified n dimensions starting at first from the
1988 * constraints, without removing the dimensions from the space.
1989 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1991 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1992 enum isl_dim_type type, unsigned first, unsigned n)
1994 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
1997 /* Eliminate the specified n dimensions starting at first from the
1998 * constraints, without removing the dimensions from the space.
1999 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2001 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2002 unsigned first, unsigned n)
2004 return isl_set_eliminate(set, isl_dim_set, first, n);
2007 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2008 __isl_take isl_basic_map *bmap)
2010 if (!bmap)
2011 return NULL;
2012 bmap = isl_basic_map_eliminate_vars(bmap,
2013 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2014 if (!bmap)
2015 return NULL;
2016 bmap->n_div = 0;
2017 return isl_basic_map_finalize(bmap);
2020 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2021 __isl_take isl_basic_set *bset)
2023 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2026 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2028 int i;
2030 if (!map)
2031 return NULL;
2032 if (map->n == 0)
2033 return map;
2035 map = isl_map_cow(map);
2036 if (!map)
2037 return NULL;
2039 for (i = 0; i < map->n; ++i) {
2040 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2041 if (!map->p[i])
2042 goto error;
2044 return map;
2045 error:
2046 isl_map_free(map);
2047 return NULL;
2050 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2052 return isl_map_remove_divs(set);
2055 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2056 enum isl_dim_type type, unsigned first, unsigned n)
2058 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2059 return isl_basic_map_free(bmap);
2060 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2061 return bmap;
2062 bmap = isl_basic_map_eliminate_vars(bmap,
2063 isl_basic_map_offset(bmap, type) - 1 + first, n);
2064 if (!bmap)
2065 return bmap;
2066 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2067 return bmap;
2068 bmap = isl_basic_map_drop(bmap, type, first, n);
2069 return bmap;
2072 /* Return true if the definition of the given div (recursively) involves
2073 * any of the given variables.
2075 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2076 unsigned first, unsigned n)
2078 int i;
2079 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2081 if (isl_int_is_zero(bmap->div[div][0]))
2082 return isl_bool_false;
2083 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2084 return isl_bool_true;
2086 for (i = bmap->n_div - 1; i >= 0; --i) {
2087 isl_bool involves;
2089 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2090 continue;
2091 involves = div_involves_vars(bmap, i, first, n);
2092 if (involves < 0 || involves)
2093 return involves;
2096 return isl_bool_false;
2099 /* Try and add a lower and/or upper bound on "div" to "bmap"
2100 * based on inequality "i".
2101 * "total" is the total number of variables (excluding the divs).
2102 * "v" is a temporary object that can be used during the calculations.
2103 * If "lb" is set, then a lower bound should be constructed.
2104 * If "ub" is set, then an upper bound should be constructed.
2106 * The calling function has already checked that the inequality does not
2107 * reference "div", but we still need to check that the inequality is
2108 * of the right form. We'll consider the case where we want to construct
2109 * a lower bound. The construction of upper bounds is similar.
2111 * Let "div" be of the form
2113 * q = floor((a + f(x))/d)
2115 * We essentially check if constraint "i" is of the form
2117 * b + f(x) >= 0
2119 * so that we can use it to derive a lower bound on "div".
2120 * However, we allow a slightly more general form
2122 * b + g(x) >= 0
2124 * with the condition that the coefficients of g(x) - f(x) are all
2125 * divisible by d.
2126 * Rewriting this constraint as
2128 * 0 >= -b - g(x)
2130 * adding a + f(x) to both sides and dividing by d, we obtain
2132 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2134 * Taking the floor on both sides, we obtain
2136 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2138 * or
2140 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2142 * In the case of an upper bound, we construct the constraint
2144 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2147 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2148 __isl_take isl_basic_map *bmap, int div, int i,
2149 unsigned total, isl_int v, int lb, int ub)
2151 int j;
2153 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2154 if (lb) {
2155 isl_int_sub(v, bmap->ineq[i][1 + j],
2156 bmap->div[div][1 + 1 + j]);
2157 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2159 if (ub) {
2160 isl_int_add(v, bmap->ineq[i][1 + j],
2161 bmap->div[div][1 + 1 + j]);
2162 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2165 if (!lb && !ub)
2166 return bmap;
2168 bmap = isl_basic_map_cow(bmap);
2169 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2170 if (lb) {
2171 int k = isl_basic_map_alloc_inequality(bmap);
2172 if (k < 0)
2173 goto error;
2174 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2175 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2176 bmap->div[div][1 + j]);
2177 isl_int_cdiv_q(bmap->ineq[k][j],
2178 bmap->ineq[k][j], bmap->div[div][0]);
2180 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2182 if (ub) {
2183 int k = isl_basic_map_alloc_inequality(bmap);
2184 if (k < 0)
2185 goto error;
2186 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2187 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2188 bmap->div[div][1 + j]);
2189 isl_int_fdiv_q(bmap->ineq[k][j],
2190 bmap->ineq[k][j], bmap->div[div][0]);
2192 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2195 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2196 return bmap;
2197 error:
2198 isl_basic_map_free(bmap);
2199 return NULL;
2202 /* This function is called right before "div" is eliminated from "bmap"
2203 * using Fourier-Motzkin.
2204 * Look through the constraints of "bmap" for constraints on the argument
2205 * of the integer division and use them to construct constraints on the
2206 * integer division itself. These constraints can then be combined
2207 * during the Fourier-Motzkin elimination.
2208 * Note that it is only useful to introduce lower bounds on "div"
2209 * if "bmap" already contains upper bounds on "div" as the newly
2210 * introduce lower bounds can then be combined with the pre-existing
2211 * upper bounds. Similarly for upper bounds.
2212 * We therefore first check if "bmap" contains any lower and/or upper bounds
2213 * on "div".
2215 * It is interesting to note that the introduction of these constraints
2216 * can indeed lead to more accurate results, even when compared to
2217 * deriving constraints on the argument of "div" from constraints on "div".
2218 * Consider, for example, the set
2220 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2222 * The second constraint can be rewritten as
2224 * 2 * [(-i-2j+3)/4] + k >= 0
2226 * from which we can derive
2228 * -i - 2j + 3 >= -2k
2230 * or
2232 * i + 2j <= 3 + 2k
2234 * Combined with the first constraint, we obtain
2236 * -3 <= 3 + 2k or k >= -3
2238 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2239 * the first constraint, we obtain
2241 * [(i + 2j)/4] >= [-3/4] = -1
2243 * Combining this constraint with the second constraint, we obtain
2245 * k >= -2
2247 static __isl_give isl_basic_map *insert_bounds_on_div(
2248 __isl_take isl_basic_map *bmap, int div)
2250 int i;
2251 int check_lb, check_ub;
2252 isl_int v;
2253 unsigned total;
2255 if (!bmap)
2256 return NULL;
2258 if (isl_int_is_zero(bmap->div[div][0]))
2259 return bmap;
2261 total = isl_space_dim(bmap->dim, isl_dim_all);
2263 check_lb = 0;
2264 check_ub = 0;
2265 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2266 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2267 if (s > 0)
2268 check_ub = 1;
2269 if (s < 0)
2270 check_lb = 1;
2273 if (!check_lb && !check_ub)
2274 return bmap;
2276 isl_int_init(v);
2278 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2279 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2280 continue;
2282 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2283 check_lb, check_ub);
2286 isl_int_clear(v);
2288 return bmap;
2291 /* Remove all divs (recursively) involving any of the given dimensions
2292 * in their definitions.
2294 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2295 __isl_take isl_basic_map *bmap,
2296 enum isl_dim_type type, unsigned first, unsigned n)
2298 int i;
2300 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2301 return isl_basic_map_free(bmap);
2302 first += isl_basic_map_offset(bmap, type);
2304 for (i = bmap->n_div - 1; i >= 0; --i) {
2305 isl_bool involves;
2307 involves = div_involves_vars(bmap, i, first, n);
2308 if (involves < 0)
2309 return isl_basic_map_free(bmap);
2310 if (!involves)
2311 continue;
2312 bmap = insert_bounds_on_div(bmap, i);
2313 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2314 if (!bmap)
2315 return NULL;
2316 i = bmap->n_div;
2319 return bmap;
2322 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2323 __isl_take isl_basic_set *bset,
2324 enum isl_dim_type type, unsigned first, unsigned n)
2326 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2329 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2330 enum isl_dim_type type, unsigned first, unsigned n)
2332 int i;
2334 if (!map)
2335 return NULL;
2336 if (map->n == 0)
2337 return map;
2339 map = isl_map_cow(map);
2340 if (!map)
2341 return NULL;
2343 for (i = 0; i < map->n; ++i) {
2344 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2345 type, first, n);
2346 if (!map->p[i])
2347 goto error;
2349 return map;
2350 error:
2351 isl_map_free(map);
2352 return NULL;
2355 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2356 enum isl_dim_type type, unsigned first, unsigned n)
2358 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2359 type, first, n));
2362 /* Does the description of "bmap" depend on the specified dimensions?
2363 * We also check whether the dimensions appear in any of the div definitions.
2364 * In principle there is no need for this check. If the dimensions appear
2365 * in a div definition, they also appear in the defining constraints of that
2366 * div.
2368 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2369 enum isl_dim_type type, unsigned first, unsigned n)
2371 int i;
2373 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2374 return isl_bool_error;
2376 first += isl_basic_map_offset(bmap, type);
2377 for (i = 0; i < bmap->n_eq; ++i)
2378 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2379 return isl_bool_true;
2380 for (i = 0; i < bmap->n_ineq; ++i)
2381 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2382 return isl_bool_true;
2383 for (i = 0; i < bmap->n_div; ++i) {
2384 if (isl_int_is_zero(bmap->div[i][0]))
2385 continue;
2386 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2387 return isl_bool_true;
2390 return isl_bool_false;
2393 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2394 enum isl_dim_type type, unsigned first, unsigned n)
2396 int i;
2398 if (!map)
2399 return isl_bool_error;
2401 if (first + n > isl_map_dim(map, type))
2402 isl_die(map->ctx, isl_error_invalid,
2403 "index out of bounds", return isl_bool_error);
2405 for (i = 0; i < map->n; ++i) {
2406 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2407 type, first, n);
2408 if (involves < 0 || involves)
2409 return involves;
2412 return isl_bool_false;
2415 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2416 enum isl_dim_type type, unsigned first, unsigned n)
2418 return isl_basic_map_involves_dims(bset, type, first, n);
2421 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2422 enum isl_dim_type type, unsigned first, unsigned n)
2424 return isl_map_involves_dims(set, type, first, n);
2427 /* Drop all constraints in bmap that involve any of the dimensions
2428 * first to first+n-1.
2430 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2431 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2433 int i;
2435 if (n == 0)
2436 return bmap;
2438 bmap = isl_basic_map_cow(bmap);
2440 if (!bmap)
2441 return NULL;
2443 for (i = bmap->n_eq - 1; i >= 0; --i) {
2444 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2445 continue;
2446 isl_basic_map_drop_equality(bmap, i);
2449 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2450 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2451 continue;
2452 isl_basic_map_drop_inequality(bmap, i);
2455 bmap = isl_basic_map_add_known_div_constraints(bmap);
2456 return bmap;
2459 /* Drop all constraints in bset that involve any of the dimensions
2460 * first to first+n-1.
2462 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2463 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2465 return isl_basic_map_drop_constraints_involving(bset, first, n);
2468 /* Drop all constraints in bmap that do not involve any of the dimensions
2469 * first to first + n - 1 of the given type.
2471 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2472 __isl_take isl_basic_map *bmap,
2473 enum isl_dim_type type, unsigned first, unsigned n)
2475 int i;
2477 if (n == 0) {
2478 isl_space *space = isl_basic_map_get_space(bmap);
2479 isl_basic_map_free(bmap);
2480 return isl_basic_map_universe(space);
2482 bmap = isl_basic_map_cow(bmap);
2483 if (!bmap)
2484 return NULL;
2486 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2487 return isl_basic_map_free(bmap);
2489 first += isl_basic_map_offset(bmap, type) - 1;
2491 for (i = bmap->n_eq - 1; i >= 0; --i) {
2492 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2493 continue;
2494 isl_basic_map_drop_equality(bmap, i);
2497 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2498 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2499 continue;
2500 isl_basic_map_drop_inequality(bmap, i);
2503 bmap = isl_basic_map_add_known_div_constraints(bmap);
2504 return bmap;
2507 /* Drop all constraints in bset that do not involve any of the dimensions
2508 * first to first + n - 1 of the given type.
2510 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2511 __isl_take isl_basic_set *bset,
2512 enum isl_dim_type type, unsigned first, unsigned n)
2514 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2515 type, first, n);
2518 /* Drop all constraints in bmap that involve any of the dimensions
2519 * first to first + n - 1 of the given type.
2521 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2522 __isl_take isl_basic_map *bmap,
2523 enum isl_dim_type type, unsigned first, unsigned n)
2525 if (!bmap)
2526 return NULL;
2527 if (n == 0)
2528 return bmap;
2530 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2531 return isl_basic_map_free(bmap);
2533 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2534 first += isl_basic_map_offset(bmap, type) - 1;
2535 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2538 /* Drop all constraints in bset that involve any of the dimensions
2539 * first to first + n - 1 of the given type.
2541 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2542 __isl_take isl_basic_set *bset,
2543 enum isl_dim_type type, unsigned first, unsigned n)
2545 return isl_basic_map_drop_constraints_involving_dims(bset,
2546 type, first, n);
2549 /* Drop constraints from "map" by applying "drop" to each basic map.
2551 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2552 enum isl_dim_type type, unsigned first, unsigned n,
2553 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2554 enum isl_dim_type type, unsigned first, unsigned n))
2556 int i;
2557 unsigned dim;
2559 if (!map)
2560 return NULL;
2562 dim = isl_map_dim(map, type);
2563 if (first + n > dim || first + n < first)
2564 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2565 "index out of bounds", return isl_map_free(map));
2567 map = isl_map_cow(map);
2568 if (!map)
2569 return NULL;
2571 for (i = 0; i < map->n; ++i) {
2572 map->p[i] = drop(map->p[i], type, first, n);
2573 if (!map->p[i])
2574 return isl_map_free(map);
2577 if (map->n > 1)
2578 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2580 return map;
2583 /* Drop all constraints in map that involve any of the dimensions
2584 * first to first + n - 1 of the given type.
2586 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2587 __isl_take isl_map *map,
2588 enum isl_dim_type type, unsigned first, unsigned n)
2590 if (n == 0)
2591 return map;
2592 return drop_constraints(map, type, first, n,
2593 &isl_basic_map_drop_constraints_involving_dims);
2596 /* Drop all constraints in "map" that do not involve any of the dimensions
2597 * first to first + n - 1 of the given type.
2599 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2600 __isl_take isl_map *map,
2601 enum isl_dim_type type, unsigned first, unsigned n)
2603 if (n == 0) {
2604 isl_space *space = isl_map_get_space(map);
2605 isl_map_free(map);
2606 return isl_map_universe(space);
2608 return drop_constraints(map, type, first, n,
2609 &isl_basic_map_drop_constraints_not_involving_dims);
2612 /* Drop all constraints in set that involve any of the dimensions
2613 * first to first + n - 1 of the given type.
2615 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
2616 __isl_take isl_set *set,
2617 enum isl_dim_type type, unsigned first, unsigned n)
2619 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2622 /* Drop all constraints in "set" that do not involve any of the dimensions
2623 * first to first + n - 1 of the given type.
2625 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
2626 __isl_take isl_set *set,
2627 enum isl_dim_type type, unsigned first, unsigned n)
2629 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
2632 /* Does local variable "div" of "bmap" have a complete explicit representation?
2633 * Having a complete explicit representation requires not only
2634 * an explicit representation, but also that all local variables
2635 * that appear in this explicit representation in turn have
2636 * a complete explicit representation.
2638 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2640 int i;
2641 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2642 isl_bool marked;
2644 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2645 if (marked < 0 || marked)
2646 return isl_bool_not(marked);
2648 for (i = bmap->n_div - 1; i >= 0; --i) {
2649 isl_bool known;
2651 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2652 continue;
2653 known = isl_basic_map_div_is_known(bmap, i);
2654 if (known < 0 || !known)
2655 return known;
2658 return isl_bool_true;
2661 /* Remove all divs that are unknown or defined in terms of unknown divs.
2663 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2664 __isl_take isl_basic_map *bmap)
2666 int i;
2668 if (!bmap)
2669 return NULL;
2671 for (i = bmap->n_div - 1; i >= 0; --i) {
2672 if (isl_basic_map_div_is_known(bmap, i))
2673 continue;
2674 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2675 if (!bmap)
2676 return NULL;
2677 i = bmap->n_div;
2680 return bmap;
2683 /* Remove all divs that are unknown or defined in terms of unknown divs.
2685 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2686 __isl_take isl_basic_set *bset)
2688 return isl_basic_map_remove_unknown_divs(bset);
2691 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2693 int i;
2695 if (!map)
2696 return NULL;
2697 if (map->n == 0)
2698 return map;
2700 map = isl_map_cow(map);
2701 if (!map)
2702 return NULL;
2704 for (i = 0; i < map->n; ++i) {
2705 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2706 if (!map->p[i])
2707 goto error;
2709 return map;
2710 error:
2711 isl_map_free(map);
2712 return NULL;
2715 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2717 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
2720 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2721 __isl_take isl_basic_set *bset,
2722 enum isl_dim_type type, unsigned first, unsigned n)
2724 isl_basic_map *bmap = bset_to_bmap(bset);
2725 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2726 return bset_from_bmap(bmap);
2729 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2730 enum isl_dim_type type, unsigned first, unsigned n)
2732 int i;
2734 if (n == 0)
2735 return map;
2737 map = isl_map_cow(map);
2738 if (!map)
2739 return NULL;
2740 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2742 for (i = 0; i < map->n; ++i) {
2743 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2744 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2745 if (!map->p[i])
2746 goto error;
2748 map = isl_map_drop(map, type, first, n);
2749 return map;
2750 error:
2751 isl_map_free(map);
2752 return NULL;
2755 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2756 enum isl_dim_type type, unsigned first, unsigned n)
2758 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2759 type, first, n));
2762 /* Project out n inputs starting at first using Fourier-Motzkin */
2763 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2764 unsigned first, unsigned n)
2766 return isl_map_remove_dims(map, isl_dim_in, first, n);
2769 static void dump_term(struct isl_basic_map *bmap,
2770 isl_int c, int pos, FILE *out)
2772 const char *name;
2773 unsigned in = isl_basic_map_n_in(bmap);
2774 unsigned dim = in + isl_basic_map_n_out(bmap);
2775 unsigned nparam = isl_basic_map_n_param(bmap);
2776 if (!pos)
2777 isl_int_print(out, c, 0);
2778 else {
2779 if (!isl_int_is_one(c))
2780 isl_int_print(out, c, 0);
2781 if (pos < 1 + nparam) {
2782 name = isl_space_get_dim_name(bmap->dim,
2783 isl_dim_param, pos - 1);
2784 if (name)
2785 fprintf(out, "%s", name);
2786 else
2787 fprintf(out, "p%d", pos - 1);
2788 } else if (pos < 1 + nparam + in)
2789 fprintf(out, "i%d", pos - 1 - nparam);
2790 else if (pos < 1 + nparam + dim)
2791 fprintf(out, "o%d", pos - 1 - nparam - in);
2792 else
2793 fprintf(out, "e%d", pos - 1 - nparam - dim);
2797 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2798 int sign, FILE *out)
2800 int i;
2801 int first;
2802 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2803 isl_int v;
2805 isl_int_init(v);
2806 for (i = 0, first = 1; i < len; ++i) {
2807 if (isl_int_sgn(c[i]) * sign <= 0)
2808 continue;
2809 if (!first)
2810 fprintf(out, " + ");
2811 first = 0;
2812 isl_int_abs(v, c[i]);
2813 dump_term(bmap, v, i, out);
2815 isl_int_clear(v);
2816 if (first)
2817 fprintf(out, "0");
2820 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2821 const char *op, FILE *out, int indent)
2823 int i;
2825 fprintf(out, "%*s", indent, "");
2827 dump_constraint_sign(bmap, c, 1, out);
2828 fprintf(out, " %s ", op);
2829 dump_constraint_sign(bmap, c, -1, out);
2831 fprintf(out, "\n");
2833 for (i = bmap->n_div; i < bmap->extra; ++i) {
2834 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2835 continue;
2836 fprintf(out, "%*s", indent, "");
2837 fprintf(out, "ERROR: unused div coefficient not zero\n");
2838 abort();
2842 static void dump_constraints(struct isl_basic_map *bmap,
2843 isl_int **c, unsigned n,
2844 const char *op, FILE *out, int indent)
2846 int i;
2848 for (i = 0; i < n; ++i)
2849 dump_constraint(bmap, c[i], op, out, indent);
2852 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2854 int j;
2855 int first = 1;
2856 unsigned total = isl_basic_map_total_dim(bmap);
2858 for (j = 0; j < 1 + total; ++j) {
2859 if (isl_int_is_zero(exp[j]))
2860 continue;
2861 if (!first && isl_int_is_pos(exp[j]))
2862 fprintf(out, "+");
2863 dump_term(bmap, exp[j], j, out);
2864 first = 0;
2868 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2870 int i;
2872 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2873 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2875 for (i = 0; i < bmap->n_div; ++i) {
2876 fprintf(out, "%*s", indent, "");
2877 fprintf(out, "e%d = [(", i);
2878 dump_affine(bmap, bmap->div[i]+1, out);
2879 fprintf(out, ")/");
2880 isl_int_print(out, bmap->div[i][0], 0);
2881 fprintf(out, "]\n");
2885 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2886 FILE *out, int indent)
2888 if (!bset) {
2889 fprintf(out, "null basic set\n");
2890 return;
2893 fprintf(out, "%*s", indent, "");
2894 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2895 bset->ref, bset->dim->nparam, bset->dim->n_out,
2896 bset->extra, bset->flags);
2897 dump(bset_to_bmap(bset), out, indent);
2900 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2901 FILE *out, int indent)
2903 if (!bmap) {
2904 fprintf(out, "null basic map\n");
2905 return;
2908 fprintf(out, "%*s", indent, "");
2909 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2910 "flags: %x, n_name: %d\n",
2911 bmap->ref,
2912 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2913 bmap->extra, bmap->flags, bmap->dim->n_id);
2914 dump(bmap, out, indent);
2917 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2919 unsigned total;
2920 if (!bmap)
2921 return -1;
2922 total = isl_basic_map_total_dim(bmap);
2923 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2924 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2925 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2926 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2927 return 0;
2930 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2931 unsigned flags)
2933 if (!space)
2934 return NULL;
2935 if (isl_space_dim(space, isl_dim_in) != 0)
2936 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2937 "set cannot have input dimensions", goto error);
2938 return isl_map_alloc_space(space, n, flags);
2939 error:
2940 isl_space_free(space);
2941 return NULL;
2944 /* Make sure "map" has room for at least "n" more basic maps.
2946 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2948 int i;
2949 struct isl_map *grown = NULL;
2951 if (!map)
2952 return NULL;
2953 isl_assert(map->ctx, n >= 0, goto error);
2954 if (map->n + n <= map->size)
2955 return map;
2956 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2957 if (!grown)
2958 goto error;
2959 for (i = 0; i < map->n; ++i) {
2960 grown->p[i] = isl_basic_map_copy(map->p[i]);
2961 if (!grown->p[i])
2962 goto error;
2963 grown->n++;
2965 isl_map_free(map);
2966 return grown;
2967 error:
2968 isl_map_free(grown);
2969 isl_map_free(map);
2970 return NULL;
2973 /* Make sure "set" has room for at least "n" more basic sets.
2975 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2977 return set_from_map(isl_map_grow(set_to_map(set), n));
2980 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2982 return isl_map_from_basic_map(bset);
2985 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2987 struct isl_map *map;
2989 if (!bmap)
2990 return NULL;
2992 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2993 return isl_map_add_basic_map(map, bmap);
2996 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2997 __isl_take isl_basic_set *bset)
2999 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3000 bset_to_bmap(bset)));
3003 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3005 return isl_map_free(set);
3008 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3010 int i;
3012 if (!set) {
3013 fprintf(out, "null set\n");
3014 return;
3017 fprintf(out, "%*s", indent, "");
3018 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3019 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3020 set->flags);
3021 for (i = 0; i < set->n; ++i) {
3022 fprintf(out, "%*s", indent, "");
3023 fprintf(out, "basic set %d:\n", i);
3024 isl_basic_set_print_internal(set->p[i], out, indent+4);
3028 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3030 int i;
3032 if (!map) {
3033 fprintf(out, "null map\n");
3034 return;
3037 fprintf(out, "%*s", indent, "");
3038 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3039 "flags: %x, n_name: %d\n",
3040 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3041 map->dim->n_out, map->flags, map->dim->n_id);
3042 for (i = 0; i < map->n; ++i) {
3043 fprintf(out, "%*s", indent, "");
3044 fprintf(out, "basic map %d:\n", i);
3045 isl_basic_map_print_internal(map->p[i], out, indent+4);
3049 struct isl_basic_map *isl_basic_map_intersect_domain(
3050 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3052 struct isl_basic_map *bmap_domain;
3054 if (!bmap || !bset)
3055 goto error;
3057 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3058 bset->dim, isl_dim_param), goto error);
3060 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3061 isl_assert(bset->ctx,
3062 isl_basic_map_compatible_domain(bmap, bset), goto error);
3064 bmap = isl_basic_map_cow(bmap);
3065 if (!bmap)
3066 goto error;
3067 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3068 bset->n_div, bset->n_eq, bset->n_ineq);
3069 bmap_domain = isl_basic_map_from_domain(bset);
3070 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3072 bmap = isl_basic_map_simplify(bmap);
3073 return isl_basic_map_finalize(bmap);
3074 error:
3075 isl_basic_map_free(bmap);
3076 isl_basic_set_free(bset);
3077 return NULL;
3080 /* Check that the space of "bset" is the same as that of the range of "bmap".
3082 static isl_stat isl_basic_map_check_compatible_range(
3083 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3085 isl_bool ok;
3087 ok = isl_basic_map_compatible_range(bmap, bset);
3088 if (ok < 0)
3089 return isl_stat_error;
3090 if (!ok)
3091 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3092 "incompatible spaces", return isl_stat_error);
3094 return isl_stat_ok;
3097 struct isl_basic_map *isl_basic_map_intersect_range(
3098 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3100 struct isl_basic_map *bmap_range;
3102 if (!bmap || !bset)
3103 goto error;
3105 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3106 bset->dim, isl_dim_param), goto error);
3108 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3109 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3110 goto error;
3112 if (isl_basic_set_plain_is_universe(bset)) {
3113 isl_basic_set_free(bset);
3114 return bmap;
3117 bmap = isl_basic_map_cow(bmap);
3118 if (!bmap)
3119 goto error;
3120 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3121 bset->n_div, bset->n_eq, bset->n_ineq);
3122 bmap_range = bset_to_bmap(bset);
3123 bmap = add_constraints(bmap, bmap_range, 0, 0);
3125 bmap = isl_basic_map_simplify(bmap);
3126 return isl_basic_map_finalize(bmap);
3127 error:
3128 isl_basic_map_free(bmap);
3129 isl_basic_set_free(bset);
3130 return NULL;
3133 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3134 __isl_keep isl_vec *vec)
3136 int i;
3137 unsigned total;
3138 isl_int s;
3140 if (!bmap || !vec)
3141 return isl_bool_error;
3143 total = 1 + isl_basic_map_total_dim(bmap);
3144 if (total != vec->size)
3145 return isl_bool_false;
3147 isl_int_init(s);
3149 for (i = 0; i < bmap->n_eq; ++i) {
3150 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3151 if (!isl_int_is_zero(s)) {
3152 isl_int_clear(s);
3153 return isl_bool_false;
3157 for (i = 0; i < bmap->n_ineq; ++i) {
3158 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3159 if (isl_int_is_neg(s)) {
3160 isl_int_clear(s);
3161 return isl_bool_false;
3165 isl_int_clear(s);
3167 return isl_bool_true;
3170 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3171 __isl_keep isl_vec *vec)
3173 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3176 struct isl_basic_map *isl_basic_map_intersect(
3177 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3179 struct isl_vec *sample = NULL;
3181 if (!bmap1 || !bmap2)
3182 goto error;
3184 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
3185 bmap2->dim, isl_dim_param), goto error);
3186 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3187 isl_space_dim(bmap1->dim, isl_dim_param) &&
3188 isl_space_dim(bmap2->dim, isl_dim_all) !=
3189 isl_space_dim(bmap2->dim, isl_dim_param))
3190 return isl_basic_map_intersect(bmap2, bmap1);
3192 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3193 isl_space_dim(bmap2->dim, isl_dim_param))
3194 isl_assert(bmap1->ctx,
3195 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3197 if (isl_basic_map_plain_is_empty(bmap1)) {
3198 isl_basic_map_free(bmap2);
3199 return bmap1;
3201 if (isl_basic_map_plain_is_empty(bmap2)) {
3202 isl_basic_map_free(bmap1);
3203 return bmap2;
3206 if (bmap1->sample &&
3207 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3208 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3209 sample = isl_vec_copy(bmap1->sample);
3210 else if (bmap2->sample &&
3211 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3212 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3213 sample = isl_vec_copy(bmap2->sample);
3215 bmap1 = isl_basic_map_cow(bmap1);
3216 if (!bmap1)
3217 goto error;
3218 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3219 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3220 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3222 if (!bmap1)
3223 isl_vec_free(sample);
3224 else if (sample) {
3225 isl_vec_free(bmap1->sample);
3226 bmap1->sample = sample;
3229 bmap1 = isl_basic_map_simplify(bmap1);
3230 return isl_basic_map_finalize(bmap1);
3231 error:
3232 if (sample)
3233 isl_vec_free(sample);
3234 isl_basic_map_free(bmap1);
3235 isl_basic_map_free(bmap2);
3236 return NULL;
3239 struct isl_basic_set *isl_basic_set_intersect(
3240 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3242 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3243 bset_to_bmap(bset2)));
3246 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3247 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3249 return isl_basic_set_intersect(bset1, bset2);
3252 /* Special case of isl_map_intersect, where both map1 and map2
3253 * are convex, without any divs and such that either map1 or map2
3254 * contains a single constraint. This constraint is then simply
3255 * added to the other map.
3257 static __isl_give isl_map *map_intersect_add_constraint(
3258 __isl_take isl_map *map1, __isl_take isl_map *map2)
3260 isl_assert(map1->ctx, map1->n == 1, goto error);
3261 isl_assert(map2->ctx, map1->n == 1, goto error);
3262 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3263 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3265 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3266 return isl_map_intersect(map2, map1);
3268 map1 = isl_map_cow(map1);
3269 if (!map1)
3270 goto error;
3271 if (isl_map_plain_is_empty(map1)) {
3272 isl_map_free(map2);
3273 return map1;
3275 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3276 if (map2->p[0]->n_eq == 1)
3277 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3278 else
3279 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3280 map2->p[0]->ineq[0]);
3282 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3283 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3284 if (!map1->p[0])
3285 goto error;
3287 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3288 isl_basic_map_free(map1->p[0]);
3289 map1->n = 0;
3292 isl_map_free(map2);
3294 return map1;
3295 error:
3296 isl_map_free(map1);
3297 isl_map_free(map2);
3298 return NULL;
3301 /* map2 may be either a parameter domain or a map living in the same
3302 * space as map1.
3304 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3305 __isl_take isl_map *map2)
3307 unsigned flags = 0;
3308 isl_map *result;
3309 int i, j;
3311 if (!map1 || !map2)
3312 goto error;
3314 if ((isl_map_plain_is_empty(map1) ||
3315 isl_map_plain_is_universe(map2)) &&
3316 isl_space_is_equal(map1->dim, map2->dim)) {
3317 isl_map_free(map2);
3318 return map1;
3320 if ((isl_map_plain_is_empty(map2) ||
3321 isl_map_plain_is_universe(map1)) &&
3322 isl_space_is_equal(map1->dim, map2->dim)) {
3323 isl_map_free(map1);
3324 return map2;
3327 if (map1->n == 1 && map2->n == 1 &&
3328 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3329 isl_space_is_equal(map1->dim, map2->dim) &&
3330 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3331 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3332 return map_intersect_add_constraint(map1, map2);
3334 if (isl_space_dim(map2->dim, isl_dim_all) !=
3335 isl_space_dim(map2->dim, isl_dim_param))
3336 isl_assert(map1->ctx,
3337 isl_space_is_equal(map1->dim, map2->dim), goto error);
3339 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3340 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3341 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3343 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3344 map1->n * map2->n, flags);
3345 if (!result)
3346 goto error;
3347 for (i = 0; i < map1->n; ++i)
3348 for (j = 0; j < map2->n; ++j) {
3349 struct isl_basic_map *part;
3350 part = isl_basic_map_intersect(
3351 isl_basic_map_copy(map1->p[i]),
3352 isl_basic_map_copy(map2->p[j]));
3353 if (isl_basic_map_is_empty(part) < 0)
3354 part = isl_basic_map_free(part);
3355 result = isl_map_add_basic_map(result, part);
3356 if (!result)
3357 goto error;
3359 isl_map_free(map1);
3360 isl_map_free(map2);
3361 return result;
3362 error:
3363 isl_map_free(map1);
3364 isl_map_free(map2);
3365 return NULL;
3368 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3369 __isl_take isl_map *map2)
3371 if (!map1 || !map2)
3372 goto error;
3373 if (!isl_space_is_equal(map1->dim, map2->dim))
3374 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3375 "spaces don't match", goto error);
3376 return map_intersect_internal(map1, map2);
3377 error:
3378 isl_map_free(map1);
3379 isl_map_free(map2);
3380 return NULL;
3383 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3384 __isl_take isl_map *map2)
3386 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3389 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3391 return set_from_map(isl_map_intersect(set_to_map(set1),
3392 set_to_map(set2)));
3395 /* map_intersect_internal accepts intersections
3396 * with parameter domains, so we can just call that function.
3398 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3399 __isl_take isl_set *params)
3401 return map_intersect_internal(map, params);
3404 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3405 __isl_take isl_map *map2)
3407 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3410 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3411 __isl_take isl_set *params)
3413 return isl_map_intersect_params(set, params);
3416 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3418 isl_space *space;
3419 unsigned pos, n1, n2;
3421 if (!bmap)
3422 return NULL;
3423 bmap = isl_basic_map_cow(bmap);
3424 if (!bmap)
3425 return NULL;
3426 space = isl_space_reverse(isl_space_copy(bmap->dim));
3427 pos = isl_basic_map_offset(bmap, isl_dim_in);
3428 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3429 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3430 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3431 return isl_basic_map_reset_space(bmap, space);
3434 static __isl_give isl_basic_map *basic_map_space_reset(
3435 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3437 isl_space *space;
3439 if (!bmap)
3440 return NULL;
3441 if (!isl_space_is_named_or_nested(bmap->dim, type))
3442 return bmap;
3444 space = isl_basic_map_get_space(bmap);
3445 space = isl_space_reset(space, type);
3446 bmap = isl_basic_map_reset_space(bmap, space);
3447 return bmap;
3450 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3451 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3452 unsigned pos, unsigned n)
3454 isl_bool rational;
3455 isl_space *res_dim;
3456 struct isl_basic_map *res;
3457 struct isl_dim_map *dim_map;
3458 unsigned total, off;
3459 enum isl_dim_type t;
3461 if (n == 0)
3462 return basic_map_space_reset(bmap, type);
3464 if (!bmap)
3465 return NULL;
3467 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3469 total = isl_basic_map_total_dim(bmap) + n;
3470 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3471 off = 0;
3472 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3473 if (t != type) {
3474 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3475 } else {
3476 unsigned size = isl_basic_map_dim(bmap, t);
3477 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3478 0, pos, off);
3479 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3480 pos, size - pos, off + pos + n);
3482 off += isl_space_dim(res_dim, t);
3484 isl_dim_map_div(dim_map, bmap, off);
3486 res = isl_basic_map_alloc_space(res_dim,
3487 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3488 rational = isl_basic_map_is_rational(bmap);
3489 if (rational < 0)
3490 res = isl_basic_map_free(res);
3491 if (rational)
3492 res = isl_basic_map_set_rational(res);
3493 if (isl_basic_map_plain_is_empty(bmap)) {
3494 isl_basic_map_free(bmap);
3495 free(dim_map);
3496 return isl_basic_map_set_to_empty(res);
3498 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3499 return isl_basic_map_finalize(res);
3502 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3503 __isl_take isl_basic_set *bset,
3504 enum isl_dim_type type, unsigned pos, unsigned n)
3506 return isl_basic_map_insert_dims(bset, type, pos, n);
3509 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3510 enum isl_dim_type type, unsigned n)
3512 if (!bmap)
3513 return NULL;
3514 return isl_basic_map_insert_dims(bmap, type,
3515 isl_basic_map_dim(bmap, type), n);
3518 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3519 enum isl_dim_type type, unsigned n)
3521 if (!bset)
3522 return NULL;
3523 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3524 return isl_basic_map_add_dims(bset, type, n);
3525 error:
3526 isl_basic_set_free(bset);
3527 return NULL;
3530 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3531 enum isl_dim_type type)
3533 isl_space *space;
3535 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3536 return map;
3538 space = isl_map_get_space(map);
3539 space = isl_space_reset(space, type);
3540 map = isl_map_reset_space(map, space);
3541 return map;
3544 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3545 enum isl_dim_type type, unsigned pos, unsigned n)
3547 int i;
3549 if (n == 0)
3550 return map_space_reset(map, type);
3552 map = isl_map_cow(map);
3553 if (!map)
3554 return NULL;
3556 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3557 if (!map->dim)
3558 goto error;
3560 for (i = 0; i < map->n; ++i) {
3561 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3562 if (!map->p[i])
3563 goto error;
3566 return map;
3567 error:
3568 isl_map_free(map);
3569 return NULL;
3572 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3573 enum isl_dim_type type, unsigned pos, unsigned n)
3575 return isl_map_insert_dims(set, type, pos, n);
3578 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3579 enum isl_dim_type type, unsigned n)
3581 if (!map)
3582 return NULL;
3583 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3586 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3587 enum isl_dim_type type, unsigned n)
3589 if (!set)
3590 return NULL;
3591 isl_assert(set->ctx, type != isl_dim_in, goto error);
3592 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3593 error:
3594 isl_set_free(set);
3595 return NULL;
3598 __isl_give isl_basic_map *isl_basic_map_move_dims(
3599 __isl_take isl_basic_map *bmap,
3600 enum isl_dim_type dst_type, unsigned dst_pos,
3601 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3603 struct isl_dim_map *dim_map;
3604 struct isl_basic_map *res;
3605 enum isl_dim_type t;
3606 unsigned total, off;
3608 if (!bmap)
3609 return NULL;
3610 if (n == 0)
3611 return bmap;
3613 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3614 return isl_basic_map_free(bmap);
3616 if (dst_type == src_type && dst_pos == src_pos)
3617 return bmap;
3619 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3621 if (pos(bmap->dim, dst_type) + dst_pos ==
3622 pos(bmap->dim, src_type) + src_pos +
3623 ((src_type < dst_type) ? n : 0)) {
3624 bmap = isl_basic_map_cow(bmap);
3625 if (!bmap)
3626 return NULL;
3628 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3629 src_type, src_pos, n);
3630 if (!bmap->dim)
3631 goto error;
3633 bmap = isl_basic_map_finalize(bmap);
3635 return bmap;
3638 total = isl_basic_map_total_dim(bmap);
3639 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3641 off = 0;
3642 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3643 unsigned size = isl_space_dim(bmap->dim, t);
3644 if (t == dst_type) {
3645 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3646 0, dst_pos, off);
3647 off += dst_pos;
3648 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3649 src_pos, n, off);
3650 off += n;
3651 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3652 dst_pos, size - dst_pos, off);
3653 off += size - dst_pos;
3654 } else if (t == src_type) {
3655 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3656 0, src_pos, off);
3657 off += src_pos;
3658 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3659 src_pos + n, size - src_pos - n, off);
3660 off += size - src_pos - n;
3661 } else {
3662 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3663 off += size;
3666 isl_dim_map_div(dim_map, bmap, off);
3668 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3669 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3670 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3671 if (!bmap)
3672 goto error;
3674 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3675 src_type, src_pos, n);
3676 if (!bmap->dim)
3677 goto error;
3679 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3680 bmap = isl_basic_map_gauss(bmap, NULL);
3681 bmap = isl_basic_map_finalize(bmap);
3683 return bmap;
3684 error:
3685 isl_basic_map_free(bmap);
3686 return NULL;
3689 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3690 enum isl_dim_type dst_type, unsigned dst_pos,
3691 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3693 isl_basic_map *bmap = bset_to_bmap(bset);
3694 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3695 src_type, src_pos, n);
3696 return bset_from_bmap(bmap);
3699 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3700 enum isl_dim_type dst_type, unsigned dst_pos,
3701 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3703 if (!set)
3704 return NULL;
3705 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3706 return set_from_map(isl_map_move_dims(set_to_map(set),
3707 dst_type, dst_pos, src_type, src_pos, n));
3708 error:
3709 isl_set_free(set);
3710 return NULL;
3713 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3714 enum isl_dim_type dst_type, unsigned dst_pos,
3715 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3717 int i;
3719 if (!map)
3720 return NULL;
3721 if (n == 0)
3722 return map;
3724 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3725 goto error);
3727 if (dst_type == src_type && dst_pos == src_pos)
3728 return map;
3730 isl_assert(map->ctx, dst_type != src_type, goto error);
3732 map = isl_map_cow(map);
3733 if (!map)
3734 return NULL;
3736 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3737 if (!map->dim)
3738 goto error;
3740 for (i = 0; i < map->n; ++i) {
3741 map->p[i] = isl_basic_map_move_dims(map->p[i],
3742 dst_type, dst_pos,
3743 src_type, src_pos, n);
3744 if (!map->p[i])
3745 goto error;
3748 return map;
3749 error:
3750 isl_map_free(map);
3751 return NULL;
3754 /* Move the specified dimensions to the last columns right before
3755 * the divs. Don't change the dimension specification of bmap.
3756 * That's the responsibility of the caller.
3758 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3759 enum isl_dim_type type, unsigned first, unsigned n)
3761 struct isl_dim_map *dim_map;
3762 struct isl_basic_map *res;
3763 enum isl_dim_type t;
3764 unsigned total, off;
3766 if (!bmap)
3767 return NULL;
3768 if (pos(bmap->dim, type) + first + n ==
3769 1 + isl_space_dim(bmap->dim, isl_dim_all))
3770 return bmap;
3772 total = isl_basic_map_total_dim(bmap);
3773 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3775 off = 0;
3776 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3777 unsigned size = isl_space_dim(bmap->dim, t);
3778 if (t == type) {
3779 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3780 0, first, off);
3781 off += first;
3782 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3783 first, n, total - bmap->n_div - n);
3784 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3785 first + n, size - (first + n), off);
3786 off += size - (first + n);
3787 } else {
3788 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3789 off += size;
3792 isl_dim_map_div(dim_map, bmap, off + n);
3794 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3795 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3796 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3797 return res;
3800 /* Insert "n" rows in the divs of "bmap".
3802 * The number of columns is not changed, which means that the last
3803 * dimensions of "bmap" are being reintepreted as the new divs.
3804 * The space of "bmap" is not adjusted, however, which means
3805 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3806 * from the space of "bmap" is the responsibility of the caller.
3808 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3809 int n)
3811 int i;
3812 size_t row_size;
3813 isl_int **new_div;
3814 isl_int *old;
3816 bmap = isl_basic_map_cow(bmap);
3817 if (!bmap)
3818 return NULL;
3820 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3821 old = bmap->block2.data;
3822 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3823 (bmap->extra + n) * (1 + row_size));
3824 if (!bmap->block2.data)
3825 return isl_basic_map_free(bmap);
3826 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3827 if (!new_div)
3828 return isl_basic_map_free(bmap);
3829 for (i = 0; i < n; ++i) {
3830 new_div[i] = bmap->block2.data +
3831 (bmap->extra + i) * (1 + row_size);
3832 isl_seq_clr(new_div[i], 1 + row_size);
3834 for (i = 0; i < bmap->extra; ++i)
3835 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3836 free(bmap->div);
3837 bmap->div = new_div;
3838 bmap->n_div += n;
3839 bmap->extra += n;
3841 return bmap;
3844 /* Drop constraints from "bmap" that only involve the variables
3845 * of "type" in the range [first, first + n] that are not related
3846 * to any of the variables outside that interval.
3847 * These constraints cannot influence the values for the variables
3848 * outside the interval, except in case they cause "bmap" to be empty.
3849 * Only drop the constraints if "bmap" is known to be non-empty.
3851 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3852 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3853 unsigned first, unsigned n)
3855 int i;
3856 int *groups;
3857 unsigned dim, n_div;
3858 isl_bool non_empty;
3860 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3861 if (non_empty < 0)
3862 return isl_basic_map_free(bmap);
3863 if (!non_empty)
3864 return bmap;
3866 dim = isl_basic_map_dim(bmap, isl_dim_all);
3867 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3868 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3869 if (!groups)
3870 return isl_basic_map_free(bmap);
3871 first += isl_basic_map_offset(bmap, type) - 1;
3872 for (i = 0; i < first; ++i)
3873 groups[i] = -1;
3874 for (i = first + n; i < dim - n_div; ++i)
3875 groups[i] = -1;
3877 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3879 return bmap;
3882 /* Turn the n dimensions of type type, starting at first
3883 * into existentially quantified variables.
3885 * If a subset of the projected out variables are unrelated
3886 * to any of the variables that remain, then the constraints
3887 * involving this subset are simply dropped first.
3889 __isl_give isl_basic_map *isl_basic_map_project_out(
3890 __isl_take isl_basic_map *bmap,
3891 enum isl_dim_type type, unsigned first, unsigned n)
3893 if (n == 0)
3894 return basic_map_space_reset(bmap, type);
3895 if (type == isl_dim_div)
3896 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3897 "cannot project out existentially quantified variables",
3898 return isl_basic_map_free(bmap));
3900 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3901 if (!bmap)
3902 return NULL;
3904 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3905 return isl_basic_map_remove_dims(bmap, type, first, n);
3907 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3908 return isl_basic_map_free(bmap);
3910 bmap = move_last(bmap, type, first, n);
3911 bmap = isl_basic_map_cow(bmap);
3912 bmap = insert_div_rows(bmap, n);
3913 if (!bmap)
3914 return NULL;
3916 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3917 if (!bmap->dim)
3918 goto error;
3919 bmap = isl_basic_map_simplify(bmap);
3920 bmap = isl_basic_map_drop_redundant_divs(bmap);
3921 return isl_basic_map_finalize(bmap);
3922 error:
3923 isl_basic_map_free(bmap);
3924 return NULL;
3927 /* Turn the n dimensions of type type, starting at first
3928 * into existentially quantified variables.
3930 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3931 enum isl_dim_type type, unsigned first, unsigned n)
3933 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3934 type, first, n));
3937 /* Turn the n dimensions of type type, starting at first
3938 * into existentially quantified variables.
3940 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3941 enum isl_dim_type type, unsigned first, unsigned n)
3943 int i;
3945 if (!map)
3946 return NULL;
3948 if (n == 0)
3949 return map_space_reset(map, type);
3951 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3953 map = isl_map_cow(map);
3954 if (!map)
3955 return NULL;
3957 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3958 if (!map->dim)
3959 goto error;
3961 for (i = 0; i < map->n; ++i) {
3962 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3963 if (!map->p[i])
3964 goto error;
3967 return map;
3968 error:
3969 isl_map_free(map);
3970 return NULL;
3973 /* Turn the n dimensions of type type, starting at first
3974 * into existentially quantified variables.
3976 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3977 enum isl_dim_type type, unsigned first, unsigned n)
3979 return set_from_map(isl_map_project_out(set_to_map(set),
3980 type, first, n));
3983 /* Return a map that projects the elements in "set" onto their
3984 * "n" set dimensions starting at "first".
3985 * "type" should be equal to isl_dim_set.
3987 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3988 enum isl_dim_type type, unsigned first, unsigned n)
3990 int i;
3991 int dim;
3992 isl_map *map;
3994 if (!set)
3995 return NULL;
3996 if (type != isl_dim_set)
3997 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3998 "only set dimensions can be projected out", goto error);
3999 dim = isl_set_dim(set, isl_dim_set);
4000 if (first + n > dim || first + n < first)
4001 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4002 "index out of bounds", goto error);
4004 map = isl_map_from_domain(set);
4005 map = isl_map_add_dims(map, isl_dim_out, n);
4006 for (i = 0; i < n; ++i)
4007 map = isl_map_equate(map, isl_dim_in, first + i,
4008 isl_dim_out, i);
4009 return map;
4010 error:
4011 isl_set_free(set);
4012 return NULL;
4015 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4017 int i, j;
4019 for (i = 0; i < n; ++i) {
4020 j = isl_basic_map_alloc_div(bmap);
4021 if (j < 0)
4022 goto error;
4023 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4025 return bmap;
4026 error:
4027 isl_basic_map_free(bmap);
4028 return NULL;
4031 struct isl_basic_map *isl_basic_map_apply_range(
4032 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4034 isl_space *dim_result = NULL;
4035 struct isl_basic_map *bmap;
4036 unsigned n_in, n_out, n, nparam, total, pos;
4037 struct isl_dim_map *dim_map1, *dim_map2;
4039 if (!bmap1 || !bmap2)
4040 goto error;
4041 if (!isl_space_match(bmap1->dim, isl_dim_param,
4042 bmap2->dim, isl_dim_param))
4043 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4044 "parameters don't match", goto error);
4045 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4046 bmap2->dim, isl_dim_in))
4047 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4048 "spaces don't match", goto error);
4050 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4051 isl_space_copy(bmap2->dim));
4053 n_in = isl_basic_map_n_in(bmap1);
4054 n_out = isl_basic_map_n_out(bmap2);
4055 n = isl_basic_map_n_out(bmap1);
4056 nparam = isl_basic_map_n_param(bmap1);
4058 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4059 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4060 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4061 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4062 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4063 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4064 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4065 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4066 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4067 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4068 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4070 bmap = isl_basic_map_alloc_space(dim_result,
4071 bmap1->n_div + bmap2->n_div + n,
4072 bmap1->n_eq + bmap2->n_eq,
4073 bmap1->n_ineq + bmap2->n_ineq);
4074 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4075 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4076 bmap = add_divs(bmap, n);
4077 bmap = isl_basic_map_simplify(bmap);
4078 bmap = isl_basic_map_drop_redundant_divs(bmap);
4079 return isl_basic_map_finalize(bmap);
4080 error:
4081 isl_basic_map_free(bmap1);
4082 isl_basic_map_free(bmap2);
4083 return NULL;
4086 struct isl_basic_set *isl_basic_set_apply(
4087 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4089 if (!bset || !bmap)
4090 goto error;
4092 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4093 goto error);
4095 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4096 bmap));
4097 error:
4098 isl_basic_set_free(bset);
4099 isl_basic_map_free(bmap);
4100 return NULL;
4103 struct isl_basic_map *isl_basic_map_apply_domain(
4104 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4106 if (!bmap1 || !bmap2)
4107 goto error;
4109 if (!isl_space_match(bmap1->dim, isl_dim_param,
4110 bmap2->dim, isl_dim_param))
4111 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4112 "parameters don't match", goto error);
4113 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4114 bmap2->dim, isl_dim_in))
4115 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4116 "spaces don't match", goto error);
4118 bmap1 = isl_basic_map_reverse(bmap1);
4119 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4120 return isl_basic_map_reverse(bmap1);
4121 error:
4122 isl_basic_map_free(bmap1);
4123 isl_basic_map_free(bmap2);
4124 return NULL;
4127 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4128 * A \cap B -> f(A) + f(B)
4130 struct isl_basic_map *isl_basic_map_sum(
4131 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4133 unsigned n_in, n_out, nparam, total, pos;
4134 struct isl_basic_map *bmap = NULL;
4135 struct isl_dim_map *dim_map1, *dim_map2;
4136 int i;
4138 if (!bmap1 || !bmap2)
4139 goto error;
4141 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4142 goto error);
4144 nparam = isl_basic_map_n_param(bmap1);
4145 n_in = isl_basic_map_n_in(bmap1);
4146 n_out = isl_basic_map_n_out(bmap1);
4148 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4149 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4150 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4151 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4152 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4153 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4154 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4155 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4156 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4157 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4158 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4160 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4161 bmap1->n_div + bmap2->n_div + 2 * n_out,
4162 bmap1->n_eq + bmap2->n_eq + n_out,
4163 bmap1->n_ineq + bmap2->n_ineq);
4164 for (i = 0; i < n_out; ++i) {
4165 int j = isl_basic_map_alloc_equality(bmap);
4166 if (j < 0)
4167 goto error;
4168 isl_seq_clr(bmap->eq[j], 1+total);
4169 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4170 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4171 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4173 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4174 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4175 bmap = add_divs(bmap, 2 * n_out);
4177 bmap = isl_basic_map_simplify(bmap);
4178 return isl_basic_map_finalize(bmap);
4179 error:
4180 isl_basic_map_free(bmap);
4181 isl_basic_map_free(bmap1);
4182 isl_basic_map_free(bmap2);
4183 return NULL;
4186 /* Given two maps A -> f(A) and B -> g(B), construct a map
4187 * A \cap B -> f(A) + f(B)
4189 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
4191 struct isl_map *result;
4192 int i, j;
4194 if (!map1 || !map2)
4195 goto error;
4197 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4199 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4200 map1->n * map2->n, 0);
4201 if (!result)
4202 goto error;
4203 for (i = 0; i < map1->n; ++i)
4204 for (j = 0; j < map2->n; ++j) {
4205 struct isl_basic_map *part;
4206 part = isl_basic_map_sum(
4207 isl_basic_map_copy(map1->p[i]),
4208 isl_basic_map_copy(map2->p[j]));
4209 if (isl_basic_map_is_empty(part))
4210 isl_basic_map_free(part);
4211 else
4212 result = isl_map_add_basic_map(result, part);
4213 if (!result)
4214 goto error;
4216 isl_map_free(map1);
4217 isl_map_free(map2);
4218 return result;
4219 error:
4220 isl_map_free(map1);
4221 isl_map_free(map2);
4222 return NULL;
4225 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4226 __isl_take isl_set *set2)
4228 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4231 /* Given a basic map A -> f(A), construct A -> -f(A).
4233 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4235 int i, j;
4236 unsigned off, n;
4238 bmap = isl_basic_map_cow(bmap);
4239 if (!bmap)
4240 return NULL;
4242 n = isl_basic_map_dim(bmap, isl_dim_out);
4243 off = isl_basic_map_offset(bmap, isl_dim_out);
4244 for (i = 0; i < bmap->n_eq; ++i)
4245 for (j = 0; j < n; ++j)
4246 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4247 for (i = 0; i < bmap->n_ineq; ++i)
4248 for (j = 0; j < n; ++j)
4249 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4250 for (i = 0; i < bmap->n_div; ++i)
4251 for (j = 0; j < n; ++j)
4252 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4253 bmap = isl_basic_map_gauss(bmap, NULL);
4254 return isl_basic_map_finalize(bmap);
4257 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4259 return isl_basic_map_neg(bset);
4262 /* Given a map A -> f(A), construct A -> -f(A).
4264 struct isl_map *isl_map_neg(struct isl_map *map)
4266 int i;
4268 map = isl_map_cow(map);
4269 if (!map)
4270 return NULL;
4272 for (i = 0; i < map->n; ++i) {
4273 map->p[i] = isl_basic_map_neg(map->p[i]);
4274 if (!map->p[i])
4275 goto error;
4278 return map;
4279 error:
4280 isl_map_free(map);
4281 return NULL;
4284 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4286 return set_from_map(isl_map_neg(set_to_map(set)));
4289 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4290 * A -> floor(f(A)/d).
4292 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4293 isl_int d)
4295 unsigned n_in, n_out, nparam, total, pos;
4296 struct isl_basic_map *result = NULL;
4297 struct isl_dim_map *dim_map;
4298 int i;
4300 if (!bmap)
4301 return NULL;
4303 nparam = isl_basic_map_n_param(bmap);
4304 n_in = isl_basic_map_n_in(bmap);
4305 n_out = isl_basic_map_n_out(bmap);
4307 total = nparam + n_in + n_out + bmap->n_div + n_out;
4308 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4309 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4310 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4311 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4312 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4314 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4315 bmap->n_div + n_out,
4316 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4317 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4318 result = add_divs(result, n_out);
4319 for (i = 0; i < n_out; ++i) {
4320 int j;
4321 j = isl_basic_map_alloc_inequality(result);
4322 if (j < 0)
4323 goto error;
4324 isl_seq_clr(result->ineq[j], 1+total);
4325 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4326 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4327 j = isl_basic_map_alloc_inequality(result);
4328 if (j < 0)
4329 goto error;
4330 isl_seq_clr(result->ineq[j], 1+total);
4331 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4332 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4333 isl_int_sub_ui(result->ineq[j][0], d, 1);
4336 result = isl_basic_map_simplify(result);
4337 return isl_basic_map_finalize(result);
4338 error:
4339 isl_basic_map_free(result);
4340 return NULL;
4343 /* Given a map A -> f(A) and an integer d, construct a map
4344 * A -> floor(f(A)/d).
4346 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4348 int i;
4350 map = isl_map_cow(map);
4351 if (!map)
4352 return NULL;
4354 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4355 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4356 for (i = 0; i < map->n; ++i) {
4357 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4358 if (!map->p[i])
4359 goto error;
4362 return map;
4363 error:
4364 isl_map_free(map);
4365 return NULL;
4368 /* Given a map A -> f(A) and an integer d, construct a map
4369 * A -> floor(f(A)/d).
4371 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4372 __isl_take isl_val *d)
4374 if (!map || !d)
4375 goto error;
4376 if (!isl_val_is_int(d))
4377 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4378 "expecting integer denominator", goto error);
4379 map = isl_map_floordiv(map, d->n);
4380 isl_val_free(d);
4381 return map;
4382 error:
4383 isl_map_free(map);
4384 isl_val_free(d);
4385 return NULL;
4388 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4390 int i;
4391 unsigned nparam;
4392 unsigned n_in;
4394 i = isl_basic_map_alloc_equality(bmap);
4395 if (i < 0)
4396 goto error;
4397 nparam = isl_basic_map_n_param(bmap);
4398 n_in = isl_basic_map_n_in(bmap);
4399 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4400 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4401 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4402 return isl_basic_map_finalize(bmap);
4403 error:
4404 isl_basic_map_free(bmap);
4405 return NULL;
4408 /* Add a constraint to "bmap" expressing i_pos < o_pos
4410 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4412 int i;
4413 unsigned nparam;
4414 unsigned n_in;
4416 i = isl_basic_map_alloc_inequality(bmap);
4417 if (i < 0)
4418 goto error;
4419 nparam = isl_basic_map_n_param(bmap);
4420 n_in = isl_basic_map_n_in(bmap);
4421 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4422 isl_int_set_si(bmap->ineq[i][0], -1);
4423 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4424 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4425 return isl_basic_map_finalize(bmap);
4426 error:
4427 isl_basic_map_free(bmap);
4428 return NULL;
4431 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4433 static __isl_give isl_basic_map *var_less_or_equal(
4434 __isl_take isl_basic_map *bmap, unsigned pos)
4436 int i;
4437 unsigned nparam;
4438 unsigned n_in;
4440 i = isl_basic_map_alloc_inequality(bmap);
4441 if (i < 0)
4442 goto error;
4443 nparam = isl_basic_map_n_param(bmap);
4444 n_in = isl_basic_map_n_in(bmap);
4445 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4446 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4447 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4448 return isl_basic_map_finalize(bmap);
4449 error:
4450 isl_basic_map_free(bmap);
4451 return NULL;
4454 /* Add a constraint to "bmap" expressing i_pos > o_pos
4456 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4458 int i;
4459 unsigned nparam;
4460 unsigned n_in;
4462 i = isl_basic_map_alloc_inequality(bmap);
4463 if (i < 0)
4464 goto error;
4465 nparam = isl_basic_map_n_param(bmap);
4466 n_in = isl_basic_map_n_in(bmap);
4467 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4468 isl_int_set_si(bmap->ineq[i][0], -1);
4469 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4470 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4471 return isl_basic_map_finalize(bmap);
4472 error:
4473 isl_basic_map_free(bmap);
4474 return NULL;
4477 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4479 static __isl_give isl_basic_map *var_more_or_equal(
4480 __isl_take isl_basic_map *bmap, unsigned pos)
4482 int i;
4483 unsigned nparam;
4484 unsigned n_in;
4486 i = isl_basic_map_alloc_inequality(bmap);
4487 if (i < 0)
4488 goto error;
4489 nparam = isl_basic_map_n_param(bmap);
4490 n_in = isl_basic_map_n_in(bmap);
4491 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4492 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4493 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4494 return isl_basic_map_finalize(bmap);
4495 error:
4496 isl_basic_map_free(bmap);
4497 return NULL;
4500 __isl_give isl_basic_map *isl_basic_map_equal(
4501 __isl_take isl_space *dim, unsigned n_equal)
4503 int i;
4504 struct isl_basic_map *bmap;
4505 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4506 if (!bmap)
4507 return NULL;
4508 for (i = 0; i < n_equal && bmap; ++i)
4509 bmap = var_equal(bmap, i);
4510 return isl_basic_map_finalize(bmap);
4513 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4515 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4516 unsigned pos)
4518 int i;
4519 struct isl_basic_map *bmap;
4520 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4521 if (!bmap)
4522 return NULL;
4523 for (i = 0; i < pos && bmap; ++i)
4524 bmap = var_equal(bmap, i);
4525 if (bmap)
4526 bmap = var_less(bmap, pos);
4527 return isl_basic_map_finalize(bmap);
4530 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4532 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4533 __isl_take isl_space *dim, unsigned pos)
4535 int i;
4536 isl_basic_map *bmap;
4538 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4539 for (i = 0; i < pos; ++i)
4540 bmap = var_equal(bmap, i);
4541 bmap = var_less_or_equal(bmap, pos);
4542 return isl_basic_map_finalize(bmap);
4545 /* Return a relation on "dim" expressing i_pos > o_pos
4547 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4548 unsigned pos)
4550 int i;
4551 struct isl_basic_map *bmap;
4552 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4553 if (!bmap)
4554 return NULL;
4555 for (i = 0; i < pos && bmap; ++i)
4556 bmap = var_equal(bmap, i);
4557 if (bmap)
4558 bmap = var_more(bmap, pos);
4559 return isl_basic_map_finalize(bmap);
4562 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4564 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4565 __isl_take isl_space *dim, unsigned pos)
4567 int i;
4568 isl_basic_map *bmap;
4570 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4571 for (i = 0; i < pos; ++i)
4572 bmap = var_equal(bmap, i);
4573 bmap = var_more_or_equal(bmap, pos);
4574 return isl_basic_map_finalize(bmap);
4577 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4578 unsigned n, int equal)
4580 struct isl_map *map;
4581 int i;
4583 if (n == 0 && equal)
4584 return isl_map_universe(dims);
4586 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4588 for (i = 0; i + 1 < n; ++i)
4589 map = isl_map_add_basic_map(map,
4590 isl_basic_map_less_at(isl_space_copy(dims), i));
4591 if (n > 0) {
4592 if (equal)
4593 map = isl_map_add_basic_map(map,
4594 isl_basic_map_less_or_equal_at(dims, n - 1));
4595 else
4596 map = isl_map_add_basic_map(map,
4597 isl_basic_map_less_at(dims, n - 1));
4598 } else
4599 isl_space_free(dims);
4601 return map;
4604 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4606 if (!dims)
4607 return NULL;
4608 return map_lex_lte_first(dims, dims->n_out, equal);
4611 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4613 return map_lex_lte_first(dim, n, 0);
4616 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4618 return map_lex_lte_first(dim, n, 1);
4621 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4623 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4626 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4628 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4631 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4632 unsigned n, int equal)
4634 struct isl_map *map;
4635 int i;
4637 if (n == 0 && equal)
4638 return isl_map_universe(dims);
4640 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4642 for (i = 0; i + 1 < n; ++i)
4643 map = isl_map_add_basic_map(map,
4644 isl_basic_map_more_at(isl_space_copy(dims), i));
4645 if (n > 0) {
4646 if (equal)
4647 map = isl_map_add_basic_map(map,
4648 isl_basic_map_more_or_equal_at(dims, n - 1));
4649 else
4650 map = isl_map_add_basic_map(map,
4651 isl_basic_map_more_at(dims, n - 1));
4652 } else
4653 isl_space_free(dims);
4655 return map;
4658 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4660 if (!dims)
4661 return NULL;
4662 return map_lex_gte_first(dims, dims->n_out, equal);
4665 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4667 return map_lex_gte_first(dim, n, 0);
4670 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4672 return map_lex_gte_first(dim, n, 1);
4675 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4677 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4680 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4682 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4685 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4686 __isl_take isl_set *set2)
4688 isl_map *map;
4689 map = isl_map_lex_le(isl_set_get_space(set1));
4690 map = isl_map_intersect_domain(map, set1);
4691 map = isl_map_intersect_range(map, set2);
4692 return map;
4695 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4696 __isl_take isl_set *set2)
4698 isl_map *map;
4699 map = isl_map_lex_lt(isl_set_get_space(set1));
4700 map = isl_map_intersect_domain(map, set1);
4701 map = isl_map_intersect_range(map, set2);
4702 return map;
4705 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4706 __isl_take isl_set *set2)
4708 isl_map *map;
4709 map = isl_map_lex_ge(isl_set_get_space(set1));
4710 map = isl_map_intersect_domain(map, set1);
4711 map = isl_map_intersect_range(map, set2);
4712 return map;
4715 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4716 __isl_take isl_set *set2)
4718 isl_map *map;
4719 map = isl_map_lex_gt(isl_set_get_space(set1));
4720 map = isl_map_intersect_domain(map, set1);
4721 map = isl_map_intersect_range(map, set2);
4722 return map;
4725 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4726 __isl_take isl_map *map2)
4728 isl_map *map;
4729 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4730 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4731 map = isl_map_apply_range(map, isl_map_reverse(map2));
4732 return map;
4735 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4736 __isl_take isl_map *map2)
4738 isl_map *map;
4739 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4740 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4741 map = isl_map_apply_range(map, isl_map_reverse(map2));
4742 return map;
4745 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4746 __isl_take isl_map *map2)
4748 isl_map *map;
4749 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4750 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4751 map = isl_map_apply_range(map, isl_map_reverse(map2));
4752 return map;
4755 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4756 __isl_take isl_map *map2)
4758 isl_map *map;
4759 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4760 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4761 map = isl_map_apply_range(map, isl_map_reverse(map2));
4762 return map;
4765 /* For a div d = floor(f/m), add the constraint
4767 * f - m d >= 0
4769 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4770 unsigned pos, isl_int *div)
4772 int i;
4773 unsigned total = isl_basic_map_total_dim(bmap);
4775 i = isl_basic_map_alloc_inequality(bmap);
4776 if (i < 0)
4777 return isl_stat_error;
4778 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4779 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4781 return isl_stat_ok;
4784 /* For a div d = floor(f/m), add the constraint
4786 * -(f-(m-1)) + m d >= 0
4788 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4789 unsigned pos, isl_int *div)
4791 int i;
4792 unsigned total = isl_basic_map_total_dim(bmap);
4794 i = isl_basic_map_alloc_inequality(bmap);
4795 if (i < 0)
4796 return isl_stat_error;
4797 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4798 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4799 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4800 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4802 return isl_stat_ok;
4805 /* For a div d = floor(f/m), add the constraints
4807 * f - m d >= 0
4808 * -(f-(m-1)) + m d >= 0
4810 * Note that the second constraint is the negation of
4812 * f - m d >= m
4814 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4815 unsigned pos, isl_int *div)
4817 if (add_upper_div_constraint(bmap, pos, div) < 0)
4818 return -1;
4819 if (add_lower_div_constraint(bmap, pos, div) < 0)
4820 return -1;
4821 return 0;
4824 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4825 unsigned pos, isl_int *div)
4827 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4828 pos, div);
4831 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4833 unsigned total = isl_basic_map_total_dim(bmap);
4834 unsigned div_pos = total - bmap->n_div + div;
4836 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4837 bmap->div[div]);
4840 /* For each known div d = floor(f/m), add the constraints
4842 * f - m d >= 0
4843 * -(f-(m-1)) + m d >= 0
4845 * Remove duplicate constraints in case of some these div constraints
4846 * already appear in "bmap".
4848 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4849 __isl_take isl_basic_map *bmap)
4851 unsigned n_div;
4853 if (!bmap)
4854 return NULL;
4855 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4856 if (n_div == 0)
4857 return bmap;
4859 bmap = add_known_div_constraints(bmap);
4860 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4861 bmap = isl_basic_map_finalize(bmap);
4862 return bmap;
4865 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4867 * In particular, if this div is of the form d = floor(f/m),
4868 * then add the constraint
4870 * f - m d >= 0
4872 * if sign < 0 or the constraint
4874 * -(f-(m-1)) + m d >= 0
4876 * if sign > 0.
4878 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4879 unsigned div, int sign)
4881 unsigned total;
4882 unsigned div_pos;
4884 if (!bmap)
4885 return -1;
4887 total = isl_basic_map_total_dim(bmap);
4888 div_pos = total - bmap->n_div + div;
4890 if (sign < 0)
4891 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4892 else
4893 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4896 struct isl_basic_set *isl_basic_map_underlying_set(
4897 struct isl_basic_map *bmap)
4899 if (!bmap)
4900 goto error;
4901 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4902 bmap->n_div == 0 &&
4903 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4904 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4905 return bset_from_bmap(bmap);
4906 bmap = isl_basic_map_cow(bmap);
4907 if (!bmap)
4908 goto error;
4909 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4910 if (!bmap->dim)
4911 goto error;
4912 bmap->extra -= bmap->n_div;
4913 bmap->n_div = 0;
4914 bmap = isl_basic_map_finalize(bmap);
4915 return bset_from_bmap(bmap);
4916 error:
4917 isl_basic_map_free(bmap);
4918 return NULL;
4921 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4922 __isl_take isl_basic_set *bset)
4924 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4927 /* Replace each element in "list" by the result of applying
4928 * isl_basic_map_underlying_set to the element.
4930 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4931 __isl_take isl_basic_map_list *list)
4933 int i, n;
4935 if (!list)
4936 return NULL;
4938 n = isl_basic_map_list_n_basic_map(list);
4939 for (i = 0; i < n; ++i) {
4940 isl_basic_map *bmap;
4941 isl_basic_set *bset;
4943 bmap = isl_basic_map_list_get_basic_map(list, i);
4944 bset = isl_basic_set_underlying_set(bmap);
4945 list = isl_basic_set_list_set_basic_set(list, i, bset);
4948 return list;
4951 struct isl_basic_map *isl_basic_map_overlying_set(
4952 struct isl_basic_set *bset, struct isl_basic_map *like)
4954 struct isl_basic_map *bmap;
4955 struct isl_ctx *ctx;
4956 unsigned total;
4957 int i;
4959 if (!bset || !like)
4960 goto error;
4961 ctx = bset->ctx;
4962 isl_assert(ctx, bset->n_div == 0, goto error);
4963 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4964 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4965 goto error);
4966 if (like->n_div == 0) {
4967 isl_space *space = isl_basic_map_get_space(like);
4968 isl_basic_map_free(like);
4969 return isl_basic_map_reset_space(bset, space);
4971 bset = isl_basic_set_cow(bset);
4972 if (!bset)
4973 goto error;
4974 total = bset->dim->n_out + bset->extra;
4975 bmap = bset_to_bmap(bset);
4976 isl_space_free(bmap->dim);
4977 bmap->dim = isl_space_copy(like->dim);
4978 if (!bmap->dim)
4979 goto error;
4980 bmap->n_div = like->n_div;
4981 bmap->extra += like->n_div;
4982 if (bmap->extra) {
4983 unsigned ltotal;
4984 isl_int **div;
4985 ltotal = total - bmap->extra + like->extra;
4986 if (ltotal > total)
4987 ltotal = total;
4988 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4989 bmap->extra * (1 + 1 + total));
4990 if (isl_blk_is_error(bmap->block2))
4991 goto error;
4992 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4993 if (!div)
4994 goto error;
4995 bmap->div = div;
4996 for (i = 0; i < bmap->extra; ++i)
4997 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4998 for (i = 0; i < like->n_div; ++i) {
4999 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5000 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5002 bmap = isl_basic_map_add_known_div_constraints(bmap);
5004 isl_basic_map_free(like);
5005 bmap = isl_basic_map_simplify(bmap);
5006 bmap = isl_basic_map_finalize(bmap);
5007 return bmap;
5008 error:
5009 isl_basic_map_free(like);
5010 isl_basic_set_free(bset);
5011 return NULL;
5014 struct isl_basic_set *isl_basic_set_from_underlying_set(
5015 struct isl_basic_set *bset, struct isl_basic_set *like)
5017 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5018 bset_to_bmap(like)));
5021 struct isl_set *isl_map_underlying_set(struct isl_map *map)
5023 int i;
5025 map = isl_map_cow(map);
5026 if (!map)
5027 return NULL;
5028 map->dim = isl_space_cow(map->dim);
5029 if (!map->dim)
5030 goto error;
5032 for (i = 1; i < map->n; ++i)
5033 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5034 goto error);
5035 for (i = 0; i < map->n; ++i) {
5036 map->p[i] = bset_to_bmap(
5037 isl_basic_map_underlying_set(map->p[i]));
5038 if (!map->p[i])
5039 goto error;
5041 if (map->n == 0)
5042 map->dim = isl_space_underlying(map->dim, 0);
5043 else {
5044 isl_space_free(map->dim);
5045 map->dim = isl_space_copy(map->p[0]->dim);
5047 if (!map->dim)
5048 goto error;
5049 return set_from_map(map);
5050 error:
5051 isl_map_free(map);
5052 return NULL;
5055 /* Replace the space of "bmap" by "space".
5057 * If the space of "bmap" is identical to "space" (including the identifiers
5058 * of the input and output dimensions), then simply return the original input.
5060 __isl_give isl_basic_map *isl_basic_map_reset_space(
5061 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5063 isl_bool equal;
5065 if (!bmap)
5066 goto error;
5067 equal = isl_space_is_equal(bmap->dim, space);
5068 if (equal >= 0 && equal)
5069 equal = isl_space_match(bmap->dim, isl_dim_in,
5070 space, isl_dim_in);
5071 if (equal >= 0 && equal)
5072 equal = isl_space_match(bmap->dim, isl_dim_out,
5073 space, isl_dim_out);
5074 if (equal < 0)
5075 goto error;
5076 if (equal) {
5077 isl_space_free(space);
5078 return bmap;
5080 bmap = isl_basic_map_cow(bmap);
5081 if (!bmap || !space)
5082 goto error;
5084 isl_space_free(bmap->dim);
5085 bmap->dim = space;
5087 bmap = isl_basic_map_finalize(bmap);
5089 return bmap;
5090 error:
5091 isl_basic_map_free(bmap);
5092 isl_space_free(space);
5093 return NULL;
5096 __isl_give isl_basic_set *isl_basic_set_reset_space(
5097 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5099 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5100 dim));
5103 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5104 __isl_take isl_space *dim)
5106 int i;
5108 map = isl_map_cow(map);
5109 if (!map || !dim)
5110 goto error;
5112 for (i = 0; i < map->n; ++i) {
5113 map->p[i] = isl_basic_map_reset_space(map->p[i],
5114 isl_space_copy(dim));
5115 if (!map->p[i])
5116 goto error;
5118 isl_space_free(map->dim);
5119 map->dim = dim;
5121 return map;
5122 error:
5123 isl_map_free(map);
5124 isl_space_free(dim);
5125 return NULL;
5128 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5129 __isl_take isl_space *dim)
5131 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5134 /* Compute the parameter domain of the given basic set.
5136 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5138 isl_bool is_params;
5139 isl_space *space;
5140 unsigned n;
5142 is_params = isl_basic_set_is_params(bset);
5143 if (is_params < 0)
5144 return isl_basic_set_free(bset);
5145 if (is_params)
5146 return bset;
5148 n = isl_basic_set_dim(bset, isl_dim_set);
5149 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5150 space = isl_basic_set_get_space(bset);
5151 space = isl_space_params(space);
5152 bset = isl_basic_set_reset_space(bset, space);
5153 return bset;
5156 /* Construct a zero-dimensional basic set with the given parameter domain.
5158 __isl_give isl_basic_set *isl_basic_set_from_params(
5159 __isl_take isl_basic_set *bset)
5161 isl_space *space;
5162 space = isl_basic_set_get_space(bset);
5163 space = isl_space_set_from_params(space);
5164 bset = isl_basic_set_reset_space(bset, space);
5165 return bset;
5168 /* Compute the parameter domain of the given set.
5170 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5172 isl_space *space;
5173 unsigned n;
5175 if (isl_set_is_params(set))
5176 return set;
5178 n = isl_set_dim(set, isl_dim_set);
5179 set = isl_set_project_out(set, isl_dim_set, 0, n);
5180 space = isl_set_get_space(set);
5181 space = isl_space_params(space);
5182 set = isl_set_reset_space(set, space);
5183 return set;
5186 /* Construct a zero-dimensional set with the given parameter domain.
5188 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5190 isl_space *space;
5191 space = isl_set_get_space(set);
5192 space = isl_space_set_from_params(space);
5193 set = isl_set_reset_space(set, space);
5194 return set;
5197 /* Compute the parameter domain of the given map.
5199 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5201 isl_space *space;
5202 unsigned n;
5204 n = isl_map_dim(map, isl_dim_in);
5205 map = isl_map_project_out(map, isl_dim_in, 0, n);
5206 n = isl_map_dim(map, isl_dim_out);
5207 map = isl_map_project_out(map, isl_dim_out, 0, n);
5208 space = isl_map_get_space(map);
5209 space = isl_space_params(space);
5210 map = isl_map_reset_space(map, space);
5211 return map;
5214 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5216 isl_space *space;
5217 unsigned n_out;
5219 if (!bmap)
5220 return NULL;
5221 space = isl_space_domain(isl_basic_map_get_space(bmap));
5223 n_out = isl_basic_map_n_out(bmap);
5224 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5226 return isl_basic_map_reset_space(bmap, space);
5229 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5231 if (!bmap)
5232 return isl_bool_error;
5233 return isl_space_may_be_set(bmap->dim);
5236 /* Is this basic map actually a set?
5237 * Users should never call this function. Outside of isl,
5238 * the type should indicate whether something is a set or a map.
5240 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5242 if (!bmap)
5243 return isl_bool_error;
5244 return isl_space_is_set(bmap->dim);
5247 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5249 isl_bool is_set;
5251 is_set = isl_basic_map_is_set(bmap);
5252 if (is_set < 0)
5253 goto error;
5254 if (is_set)
5255 return bmap;
5256 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5257 error:
5258 isl_basic_map_free(bmap);
5259 return NULL;
5262 __isl_give isl_basic_map *isl_basic_map_domain_map(
5263 __isl_take isl_basic_map *bmap)
5265 int i;
5266 isl_space *dim;
5267 isl_basic_map *domain;
5268 int nparam, n_in, n_out;
5270 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5271 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5272 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5274 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5275 domain = isl_basic_map_universe(dim);
5277 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5278 bmap = isl_basic_map_apply_range(bmap, domain);
5279 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5281 for (i = 0; i < n_in; ++i)
5282 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5283 isl_dim_out, i);
5285 bmap = isl_basic_map_gauss(bmap, NULL);
5286 return isl_basic_map_finalize(bmap);
5289 __isl_give isl_basic_map *isl_basic_map_range_map(
5290 __isl_take isl_basic_map *bmap)
5292 int i;
5293 isl_space *dim;
5294 isl_basic_map *range;
5295 int nparam, n_in, n_out;
5297 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5298 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5299 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5301 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5302 range = isl_basic_map_universe(dim);
5304 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5305 bmap = isl_basic_map_apply_range(bmap, range);
5306 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5308 for (i = 0; i < n_out; ++i)
5309 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5310 isl_dim_out, i);
5312 bmap = isl_basic_map_gauss(bmap, NULL);
5313 return isl_basic_map_finalize(bmap);
5316 int isl_map_may_be_set(__isl_keep isl_map *map)
5318 if (!map)
5319 return -1;
5320 return isl_space_may_be_set(map->dim);
5323 /* Is this map actually a set?
5324 * Users should never call this function. Outside of isl,
5325 * the type should indicate whether something is a set or a map.
5327 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5329 if (!map)
5330 return isl_bool_error;
5331 return isl_space_is_set(map->dim);
5334 struct isl_set *isl_map_range(struct isl_map *map)
5336 int i;
5337 isl_bool is_set;
5338 struct isl_set *set;
5340 is_set = isl_map_is_set(map);
5341 if (is_set < 0)
5342 goto error;
5343 if (is_set)
5344 return set_from_map(map);
5346 map = isl_map_cow(map);
5347 if (!map)
5348 goto error;
5350 set = set_from_map(map);
5351 set->dim = isl_space_range(set->dim);
5352 if (!set->dim)
5353 goto error;
5354 for (i = 0; i < map->n; ++i) {
5355 set->p[i] = isl_basic_map_range(map->p[i]);
5356 if (!set->p[i])
5357 goto error;
5359 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5360 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5361 return set;
5362 error:
5363 isl_map_free(map);
5364 return NULL;
5367 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5369 int i;
5371 map = isl_map_cow(map);
5372 if (!map)
5373 return NULL;
5375 map->dim = isl_space_domain_map(map->dim);
5376 if (!map->dim)
5377 goto error;
5378 for (i = 0; i < map->n; ++i) {
5379 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5380 if (!map->p[i])
5381 goto error;
5383 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5384 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5385 return map;
5386 error:
5387 isl_map_free(map);
5388 return NULL;
5391 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5393 int i;
5394 isl_space *range_dim;
5396 map = isl_map_cow(map);
5397 if (!map)
5398 return NULL;
5400 range_dim = isl_space_range(isl_map_get_space(map));
5401 range_dim = isl_space_from_range(range_dim);
5402 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5403 map->dim = isl_space_join(map->dim, range_dim);
5404 if (!map->dim)
5405 goto error;
5406 for (i = 0; i < map->n; ++i) {
5407 map->p[i] = isl_basic_map_range_map(map->p[i]);
5408 if (!map->p[i])
5409 goto error;
5411 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5412 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5413 return map;
5414 error:
5415 isl_map_free(map);
5416 return NULL;
5419 /* Given a wrapped map of the form A[B -> C],
5420 * return the map A[B -> C] -> B.
5422 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5424 isl_id *id;
5425 isl_map *map;
5427 if (!set)
5428 return NULL;
5429 if (!isl_set_has_tuple_id(set))
5430 return isl_map_domain_map(isl_set_unwrap(set));
5432 id = isl_set_get_tuple_id(set);
5433 map = isl_map_domain_map(isl_set_unwrap(set));
5434 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5436 return map;
5439 __isl_give isl_basic_map *isl_basic_map_from_domain(
5440 __isl_take isl_basic_set *bset)
5442 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5445 __isl_give isl_basic_map *isl_basic_map_from_range(
5446 __isl_take isl_basic_set *bset)
5448 isl_space *space;
5449 space = isl_basic_set_get_space(bset);
5450 space = isl_space_from_range(space);
5451 bset = isl_basic_set_reset_space(bset, space);
5452 return bset_to_bmap(bset);
5455 /* Create a relation with the given set as range.
5456 * The domain of the created relation is a zero-dimensional
5457 * flat anonymous space.
5459 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5461 isl_space *space;
5462 space = isl_set_get_space(set);
5463 space = isl_space_from_range(space);
5464 set = isl_set_reset_space(set, space);
5465 return set_to_map(set);
5468 /* Create a relation with the given set as domain.
5469 * The range of the created relation is a zero-dimensional
5470 * flat anonymous space.
5472 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5474 return isl_map_reverse(isl_map_from_range(set));
5477 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5478 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5480 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5483 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5484 __isl_take isl_set *range)
5486 return isl_map_apply_range(isl_map_reverse(domain), range);
5489 /* Return a newly allocated isl_map with given space and flags and
5490 * room for "n" basic maps.
5491 * Make sure that all cached information is cleared.
5493 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5494 unsigned flags)
5496 struct isl_map *map;
5498 if (!space)
5499 return NULL;
5500 if (n < 0)
5501 isl_die(space->ctx, isl_error_internal,
5502 "negative number of basic maps", goto error);
5503 map = isl_calloc(space->ctx, struct isl_map,
5504 sizeof(struct isl_map) +
5505 (n - 1) * sizeof(struct isl_basic_map *));
5506 if (!map)
5507 goto error;
5509 map->ctx = space->ctx;
5510 isl_ctx_ref(map->ctx);
5511 map->ref = 1;
5512 map->size = n;
5513 map->n = 0;
5514 map->dim = space;
5515 map->flags = flags;
5516 return map;
5517 error:
5518 isl_space_free(space);
5519 return NULL;
5522 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5524 struct isl_basic_map *bmap;
5525 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5526 bmap = isl_basic_map_set_to_empty(bmap);
5527 return bmap;
5530 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5532 struct isl_basic_set *bset;
5533 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5534 bset = isl_basic_set_set_to_empty(bset);
5535 return bset;
5538 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5540 struct isl_basic_map *bmap;
5541 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5542 bmap = isl_basic_map_finalize(bmap);
5543 return bmap;
5546 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5548 struct isl_basic_set *bset;
5549 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5550 bset = isl_basic_set_finalize(bset);
5551 return bset;
5554 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5556 int i;
5557 unsigned total = isl_space_dim(dim, isl_dim_all);
5558 isl_basic_map *bmap;
5560 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5561 for (i = 0; i < total; ++i) {
5562 int k = isl_basic_map_alloc_inequality(bmap);
5563 if (k < 0)
5564 goto error;
5565 isl_seq_clr(bmap->ineq[k], 1 + total);
5566 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5568 return bmap;
5569 error:
5570 isl_basic_map_free(bmap);
5571 return NULL;
5574 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5576 return isl_basic_map_nat_universe(dim);
5579 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5581 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5584 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5586 return isl_map_nat_universe(dim);
5589 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5591 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5594 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5596 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5599 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5601 struct isl_map *map;
5602 if (!dim)
5603 return NULL;
5604 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5605 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5606 return map;
5609 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5611 struct isl_set *set;
5612 if (!dim)
5613 return NULL;
5614 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5615 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5616 return set;
5619 struct isl_map *isl_map_dup(struct isl_map *map)
5621 int i;
5622 struct isl_map *dup;
5624 if (!map)
5625 return NULL;
5626 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5627 for (i = 0; i < map->n; ++i)
5628 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5629 return dup;
5632 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5633 __isl_take isl_basic_map *bmap)
5635 if (!bmap || !map)
5636 goto error;
5637 if (isl_basic_map_plain_is_empty(bmap)) {
5638 isl_basic_map_free(bmap);
5639 return map;
5641 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5642 isl_assert(map->ctx, map->n < map->size, goto error);
5643 map->p[map->n] = bmap;
5644 map->n++;
5645 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5646 return map;
5647 error:
5648 if (map)
5649 isl_map_free(map);
5650 if (bmap)
5651 isl_basic_map_free(bmap);
5652 return NULL;
5655 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5657 int i;
5659 if (!map)
5660 return NULL;
5662 if (--map->ref > 0)
5663 return NULL;
5665 clear_caches(map);
5666 isl_ctx_deref(map->ctx);
5667 for (i = 0; i < map->n; ++i)
5668 isl_basic_map_free(map->p[i]);
5669 isl_space_free(map->dim);
5670 free(map);
5672 return NULL;
5675 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5676 struct isl_basic_map *bmap, unsigned pos, int value)
5678 int j;
5680 bmap = isl_basic_map_cow(bmap);
5681 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5682 j = isl_basic_map_alloc_equality(bmap);
5683 if (j < 0)
5684 goto error;
5685 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5686 isl_int_set_si(bmap->eq[j][pos], -1);
5687 isl_int_set_si(bmap->eq[j][0], value);
5688 bmap = isl_basic_map_simplify(bmap);
5689 return isl_basic_map_finalize(bmap);
5690 error:
5691 isl_basic_map_free(bmap);
5692 return NULL;
5695 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5696 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5698 int j;
5700 bmap = isl_basic_map_cow(bmap);
5701 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5702 j = isl_basic_map_alloc_equality(bmap);
5703 if (j < 0)
5704 goto error;
5705 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5706 isl_int_set_si(bmap->eq[j][pos], -1);
5707 isl_int_set(bmap->eq[j][0], value);
5708 bmap = isl_basic_map_simplify(bmap);
5709 return isl_basic_map_finalize(bmap);
5710 error:
5711 isl_basic_map_free(bmap);
5712 return NULL;
5715 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5716 enum isl_dim_type type, unsigned pos, int value)
5718 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5719 return isl_basic_map_free(bmap);
5720 return isl_basic_map_fix_pos_si(bmap,
5721 isl_basic_map_offset(bmap, type) + pos, value);
5724 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5725 enum isl_dim_type type, unsigned pos, isl_int value)
5727 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5728 return isl_basic_map_free(bmap);
5729 return isl_basic_map_fix_pos(bmap,
5730 isl_basic_map_offset(bmap, type) + pos, value);
5733 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5734 * to be equal to "v".
5736 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5737 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5739 if (!bmap || !v)
5740 goto error;
5741 if (!isl_val_is_int(v))
5742 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5743 "expecting integer value", goto error);
5744 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5745 goto error;
5746 pos += isl_basic_map_offset(bmap, type);
5747 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5748 isl_val_free(v);
5749 return bmap;
5750 error:
5751 isl_basic_map_free(bmap);
5752 isl_val_free(v);
5753 return NULL;
5756 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5757 * to be equal to "v".
5759 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5760 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5762 return isl_basic_map_fix_val(bset, type, pos, v);
5765 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5766 enum isl_dim_type type, unsigned pos, int value)
5768 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5769 type, pos, value));
5772 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5773 enum isl_dim_type type, unsigned pos, isl_int value)
5775 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5776 type, pos, value));
5779 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5780 unsigned input, int value)
5782 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5785 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5786 unsigned dim, int value)
5788 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5789 isl_dim_set, dim, value));
5792 static int remove_if_empty(__isl_keep isl_map *map, int i)
5794 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5796 if (empty < 0)
5797 return -1;
5798 if (!empty)
5799 return 0;
5801 isl_basic_map_free(map->p[i]);
5802 if (i != map->n - 1) {
5803 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5804 map->p[i] = map->p[map->n - 1];
5806 map->n--;
5808 return 0;
5811 /* Perform "fn" on each basic map of "map", where we may not be holding
5812 * the only reference to "map".
5813 * In particular, "fn" should be a semantics preserving operation
5814 * that we want to apply to all copies of "map". We therefore need
5815 * to be careful not to modify "map" in a way that breaks "map"
5816 * in case anything goes wrong.
5818 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5819 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5821 struct isl_basic_map *bmap;
5822 int i;
5824 if (!map)
5825 return NULL;
5827 for (i = map->n - 1; i >= 0; --i) {
5828 bmap = isl_basic_map_copy(map->p[i]);
5829 bmap = fn(bmap);
5830 if (!bmap)
5831 goto error;
5832 isl_basic_map_free(map->p[i]);
5833 map->p[i] = bmap;
5834 if (remove_if_empty(map, i) < 0)
5835 goto error;
5838 return map;
5839 error:
5840 isl_map_free(map);
5841 return NULL;
5844 struct isl_map *isl_map_fix_si(struct isl_map *map,
5845 enum isl_dim_type type, unsigned pos, int value)
5847 int i;
5849 map = isl_map_cow(map);
5850 if (!map)
5851 return NULL;
5853 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5854 for (i = map->n - 1; i >= 0; --i) {
5855 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5856 if (remove_if_empty(map, i) < 0)
5857 goto error;
5859 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5860 return map;
5861 error:
5862 isl_map_free(map);
5863 return NULL;
5866 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5867 enum isl_dim_type type, unsigned pos, int value)
5869 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
5872 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5873 enum isl_dim_type type, unsigned pos, isl_int value)
5875 int i;
5877 map = isl_map_cow(map);
5878 if (!map)
5879 return NULL;
5881 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5882 for (i = 0; i < map->n; ++i) {
5883 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5884 if (!map->p[i])
5885 goto error;
5887 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5888 return map;
5889 error:
5890 isl_map_free(map);
5891 return NULL;
5894 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5895 enum isl_dim_type type, unsigned pos, isl_int value)
5897 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
5900 /* Fix the value of the variable at position "pos" of type "type" of "map"
5901 * to be equal to "v".
5903 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5904 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5906 int i;
5908 map = isl_map_cow(map);
5909 if (!map || !v)
5910 goto error;
5912 if (!isl_val_is_int(v))
5913 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5914 "expecting integer value", goto error);
5915 if (pos >= isl_map_dim(map, type))
5916 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5917 "index out of bounds", goto error);
5918 for (i = map->n - 1; i >= 0; --i) {
5919 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5920 isl_val_copy(v));
5921 if (remove_if_empty(map, i) < 0)
5922 goto error;
5924 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5925 isl_val_free(v);
5926 return map;
5927 error:
5928 isl_map_free(map);
5929 isl_val_free(v);
5930 return NULL;
5933 /* Fix the value of the variable at position "pos" of type "type" of "set"
5934 * to be equal to "v".
5936 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5937 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5939 return isl_map_fix_val(set, type, pos, v);
5942 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5943 unsigned input, int value)
5945 return isl_map_fix_si(map, isl_dim_in, input, value);
5948 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5950 return set_from_map(isl_map_fix_si(set_to_map(set),
5951 isl_dim_set, dim, value));
5954 static __isl_give isl_basic_map *basic_map_bound_si(
5955 __isl_take isl_basic_map *bmap,
5956 enum isl_dim_type type, unsigned pos, int value, int upper)
5958 int j;
5960 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5961 return isl_basic_map_free(bmap);
5962 pos += isl_basic_map_offset(bmap, type);
5963 bmap = isl_basic_map_cow(bmap);
5964 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5965 j = isl_basic_map_alloc_inequality(bmap);
5966 if (j < 0)
5967 goto error;
5968 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5969 if (upper) {
5970 isl_int_set_si(bmap->ineq[j][pos], -1);
5971 isl_int_set_si(bmap->ineq[j][0], value);
5972 } else {
5973 isl_int_set_si(bmap->ineq[j][pos], 1);
5974 isl_int_set_si(bmap->ineq[j][0], -value);
5976 bmap = isl_basic_map_simplify(bmap);
5977 return isl_basic_map_finalize(bmap);
5978 error:
5979 isl_basic_map_free(bmap);
5980 return NULL;
5983 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5984 __isl_take isl_basic_map *bmap,
5985 enum isl_dim_type type, unsigned pos, int value)
5987 return basic_map_bound_si(bmap, type, pos, value, 0);
5990 /* Constrain the values of the given dimension to be no greater than "value".
5992 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5993 __isl_take isl_basic_map *bmap,
5994 enum isl_dim_type type, unsigned pos, int value)
5996 return basic_map_bound_si(bmap, type, pos, value, 1);
5999 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6000 enum isl_dim_type type, unsigned pos, int value, int upper)
6002 int i;
6004 map = isl_map_cow(map);
6005 if (!map)
6006 return NULL;
6008 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6009 for (i = 0; i < map->n; ++i) {
6010 map->p[i] = basic_map_bound_si(map->p[i],
6011 type, pos, value, upper);
6012 if (!map->p[i])
6013 goto error;
6015 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6016 return map;
6017 error:
6018 isl_map_free(map);
6019 return NULL;
6022 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6023 enum isl_dim_type type, unsigned pos, int value)
6025 return map_bound_si(map, type, pos, value, 0);
6028 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6029 enum isl_dim_type type, unsigned pos, int value)
6031 return map_bound_si(map, type, pos, value, 1);
6034 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6035 enum isl_dim_type type, unsigned pos, int value)
6037 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6038 type, pos, value));
6041 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6042 enum isl_dim_type type, unsigned pos, int value)
6044 return isl_map_upper_bound_si(set, type, pos, value);
6047 /* Bound the given variable of "bmap" from below (or above is "upper"
6048 * is set) to "value".
6050 static __isl_give isl_basic_map *basic_map_bound(
6051 __isl_take isl_basic_map *bmap,
6052 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6054 int j;
6056 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6057 return isl_basic_map_free(bmap);
6058 pos += isl_basic_map_offset(bmap, type);
6059 bmap = isl_basic_map_cow(bmap);
6060 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6061 j = isl_basic_map_alloc_inequality(bmap);
6062 if (j < 0)
6063 goto error;
6064 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6065 if (upper) {
6066 isl_int_set_si(bmap->ineq[j][pos], -1);
6067 isl_int_set(bmap->ineq[j][0], value);
6068 } else {
6069 isl_int_set_si(bmap->ineq[j][pos], 1);
6070 isl_int_neg(bmap->ineq[j][0], value);
6072 bmap = isl_basic_map_simplify(bmap);
6073 return isl_basic_map_finalize(bmap);
6074 error:
6075 isl_basic_map_free(bmap);
6076 return NULL;
6079 /* Bound the given variable of "map" from below (or above is "upper"
6080 * is set) to "value".
6082 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6083 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6085 int i;
6087 map = isl_map_cow(map);
6088 if (!map)
6089 return NULL;
6091 if (pos >= isl_map_dim(map, type))
6092 isl_die(map->ctx, isl_error_invalid,
6093 "index out of bounds", goto error);
6094 for (i = map->n - 1; i >= 0; --i) {
6095 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6096 if (remove_if_empty(map, i) < 0)
6097 goto error;
6099 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6100 return map;
6101 error:
6102 isl_map_free(map);
6103 return NULL;
6106 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6107 enum isl_dim_type type, unsigned pos, isl_int value)
6109 return map_bound(map, type, pos, value, 0);
6112 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6113 enum isl_dim_type type, unsigned pos, isl_int value)
6115 return map_bound(map, type, pos, value, 1);
6118 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6119 enum isl_dim_type type, unsigned pos, isl_int value)
6121 return isl_map_lower_bound(set, type, pos, value);
6124 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6125 enum isl_dim_type type, unsigned pos, isl_int value)
6127 return isl_map_upper_bound(set, type, pos, value);
6130 /* Force the values of the variable at position "pos" of type "type" of "set"
6131 * to be no smaller than "value".
6133 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6134 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6136 if (!value)
6137 goto error;
6138 if (!isl_val_is_int(value))
6139 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6140 "expecting integer value", goto error);
6141 set = isl_set_lower_bound(set, type, pos, value->n);
6142 isl_val_free(value);
6143 return set;
6144 error:
6145 isl_val_free(value);
6146 isl_set_free(set);
6147 return NULL;
6150 /* Force the values of the variable at position "pos" of type "type" of "set"
6151 * to be no greater than "value".
6153 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6154 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6156 if (!value)
6157 goto error;
6158 if (!isl_val_is_int(value))
6159 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6160 "expecting integer value", goto error);
6161 set = isl_set_upper_bound(set, type, pos, value->n);
6162 isl_val_free(value);
6163 return set;
6164 error:
6165 isl_val_free(value);
6166 isl_set_free(set);
6167 return NULL;
6170 struct isl_map *isl_map_reverse(struct isl_map *map)
6172 int i;
6174 map = isl_map_cow(map);
6175 if (!map)
6176 return NULL;
6178 map->dim = isl_space_reverse(map->dim);
6179 if (!map->dim)
6180 goto error;
6181 for (i = 0; i < map->n; ++i) {
6182 map->p[i] = isl_basic_map_reverse(map->p[i]);
6183 if (!map->p[i])
6184 goto error;
6186 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6187 return map;
6188 error:
6189 isl_map_free(map);
6190 return NULL;
6193 #undef TYPE
6194 #define TYPE isl_pw_multi_aff
6195 #undef SUFFIX
6196 #define SUFFIX _pw_multi_aff
6197 #undef EMPTY
6198 #define EMPTY isl_pw_multi_aff_empty
6199 #undef ADD
6200 #define ADD isl_pw_multi_aff_union_add
6201 #include "isl_map_lexopt_templ.c"
6203 /* Given a map "map", compute the lexicographically minimal
6204 * (or maximal) image element for each domain element in dom,
6205 * in the form of an isl_pw_multi_aff.
6206 * If "empty" is not NULL, then set *empty to those elements in dom that
6207 * do not have an image element.
6208 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6209 * should be computed over the domain of "map". "empty" is also NULL
6210 * in this case.
6212 * We first compute the lexicographically minimal or maximal element
6213 * in the first basic map. This results in a partial solution "res"
6214 * and a subset "todo" of dom that still need to be handled.
6215 * We then consider each of the remaining maps in "map" and successively
6216 * update both "res" and "todo".
6217 * If "empty" is NULL, then the todo sets are not needed and therefore
6218 * also not computed.
6220 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6221 __isl_take isl_map *map, __isl_take isl_set *dom,
6222 __isl_give isl_set **empty, unsigned flags)
6224 int i;
6225 int full;
6226 isl_pw_multi_aff *res;
6227 isl_set *todo;
6229 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6230 if (!map || (!full && !dom))
6231 goto error;
6233 if (isl_map_plain_is_empty(map)) {
6234 if (empty)
6235 *empty = dom;
6236 else
6237 isl_set_free(dom);
6238 return isl_pw_multi_aff_from_map(map);
6241 res = basic_map_partial_lexopt_pw_multi_aff(
6242 isl_basic_map_copy(map->p[0]),
6243 isl_set_copy(dom), empty, flags);
6245 if (empty)
6246 todo = *empty;
6247 for (i = 1; i < map->n; ++i) {
6248 isl_pw_multi_aff *res_i;
6250 res_i = basic_map_partial_lexopt_pw_multi_aff(
6251 isl_basic_map_copy(map->p[i]),
6252 isl_set_copy(dom), empty, flags);
6254 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6255 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6256 else
6257 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6259 if (empty)
6260 todo = isl_set_intersect(todo, *empty);
6263 isl_set_free(dom);
6264 isl_map_free(map);
6266 if (empty)
6267 *empty = todo;
6269 return res;
6270 error:
6271 if (empty)
6272 *empty = NULL;
6273 isl_set_free(dom);
6274 isl_map_free(map);
6275 return NULL;
6278 #undef TYPE
6279 #define TYPE isl_map
6280 #undef SUFFIX
6281 #define SUFFIX
6282 #undef EMPTY
6283 #define EMPTY isl_map_empty
6284 #undef ADD
6285 #define ADD isl_map_union_disjoint
6286 #include "isl_map_lexopt_templ.c"
6288 /* Given a map "map", compute the lexicographically minimal
6289 * (or maximal) image element for each domain element in "dom",
6290 * in the form of an isl_map.
6291 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6292 * do not have an image element.
6293 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6294 * should be computed over the domain of "map". "empty" is also NULL
6295 * in this case.
6297 * If the input consists of more than one disjunct, then first
6298 * compute the desired result in the form of an isl_pw_multi_aff and
6299 * then convert that into an isl_map.
6301 * This function used to have an explicit implementation in terms
6302 * of isl_maps, but it would continually intersect the domains of
6303 * partial results with the complement of the domain of the next
6304 * partial solution, potentially leading to an explosion in the number
6305 * of disjuncts if there are several disjuncts in the input.
6306 * An even earlier implementation of this function would look for
6307 * better results in the domain of the partial result and for extra
6308 * results in the complement of this domain, which would lead to
6309 * even more splintering.
6311 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6312 __isl_take isl_map *map, __isl_take isl_set *dom,
6313 __isl_give isl_set **empty, unsigned flags)
6315 int full;
6316 struct isl_map *res;
6317 isl_pw_multi_aff *pma;
6319 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6320 if (!map || (!full && !dom))
6321 goto error;
6323 if (isl_map_plain_is_empty(map)) {
6324 if (empty)
6325 *empty = dom;
6326 else
6327 isl_set_free(dom);
6328 return map;
6331 if (map->n == 1) {
6332 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6333 dom, empty, flags);
6334 isl_map_free(map);
6335 return res;
6338 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6339 flags);
6340 return isl_map_from_pw_multi_aff(pma);
6341 error:
6342 if (empty)
6343 *empty = NULL;
6344 isl_set_free(dom);
6345 isl_map_free(map);
6346 return NULL;
6349 __isl_give isl_map *isl_map_partial_lexmax(
6350 __isl_take isl_map *map, __isl_take isl_set *dom,
6351 __isl_give isl_set **empty)
6353 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6356 __isl_give isl_map *isl_map_partial_lexmin(
6357 __isl_take isl_map *map, __isl_take isl_set *dom,
6358 __isl_give isl_set **empty)
6360 return isl_map_partial_lexopt(map, dom, empty, 0);
6363 __isl_give isl_set *isl_set_partial_lexmin(
6364 __isl_take isl_set *set, __isl_take isl_set *dom,
6365 __isl_give isl_set **empty)
6367 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6368 dom, empty));
6371 __isl_give isl_set *isl_set_partial_lexmax(
6372 __isl_take isl_set *set, __isl_take isl_set *dom,
6373 __isl_give isl_set **empty)
6375 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6376 dom, empty));
6379 /* Compute the lexicographic minimum (or maximum if "flags" includes
6380 * ISL_OPT_MAX) of "bset" over its parametric domain.
6382 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6383 unsigned flags)
6385 return isl_basic_map_lexopt(bset, flags);
6388 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6390 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6393 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6395 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6398 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6400 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6403 /* Compute the lexicographic minimum of "bset" over its parametric domain
6404 * for the purpose of quantifier elimination.
6405 * That is, find an explicit representation for all the existentially
6406 * quantified variables in "bset" by computing their lexicographic
6407 * minimum.
6409 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6410 __isl_take isl_basic_set *bset)
6412 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6415 /* Extract the first and only affine expression from list
6416 * and then add it to *pwaff with the given dom.
6417 * This domain is known to be disjoint from other domains
6418 * because of the way isl_basic_map_foreach_lexmax works.
6420 static isl_stat update_dim_opt(__isl_take isl_basic_set *dom,
6421 __isl_take isl_aff_list *list, void *user)
6423 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6424 isl_aff *aff;
6425 isl_pw_aff **pwaff = user;
6426 isl_pw_aff *pwaff_i;
6428 if (!list)
6429 goto error;
6430 if (isl_aff_list_n_aff(list) != 1)
6431 isl_die(ctx, isl_error_internal,
6432 "expecting single element list", goto error);
6434 aff = isl_aff_list_get_aff(list, 0);
6435 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6437 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6439 isl_aff_list_free(list);
6441 return isl_stat_ok;
6442 error:
6443 isl_basic_set_free(dom);
6444 isl_aff_list_free(list);
6445 return isl_stat_error;
6448 /* Given a basic map with one output dimension, compute the minimum or
6449 * maximum of that dimension as an isl_pw_aff.
6451 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6452 * call update_dim_opt on each leaf of the result.
6454 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6455 int max)
6457 isl_space *dim = isl_basic_map_get_space(bmap);
6458 isl_pw_aff *pwaff;
6459 isl_stat r;
6461 dim = isl_space_from_domain(isl_space_domain(dim));
6462 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6463 pwaff = isl_pw_aff_empty(dim);
6465 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6466 if (r < 0)
6467 return isl_pw_aff_free(pwaff);
6469 return pwaff;
6472 /* Compute the minimum or maximum of the given output dimension
6473 * as a function of the parameters and the input dimensions,
6474 * but independently of the other output dimensions.
6476 * We first project out the other output dimension and then compute
6477 * the "lexicographic" maximum in each basic map, combining the results
6478 * using isl_pw_aff_union_max.
6480 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6481 int max)
6483 int i;
6484 isl_pw_aff *pwaff;
6485 unsigned n_out;
6487 n_out = isl_map_dim(map, isl_dim_out);
6488 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6489 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6490 if (!map)
6491 return NULL;
6493 if (map->n == 0) {
6494 isl_space *dim = isl_map_get_space(map);
6495 isl_map_free(map);
6496 return isl_pw_aff_empty(dim);
6499 pwaff = basic_map_dim_opt(map->p[0], max);
6500 for (i = 1; i < map->n; ++i) {
6501 isl_pw_aff *pwaff_i;
6503 pwaff_i = basic_map_dim_opt(map->p[i], max);
6504 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6507 isl_map_free(map);
6509 return pwaff;
6512 /* Compute the minimum of the given output dimension as a function of the
6513 * parameters and input dimensions, but independently of
6514 * the other output dimensions.
6516 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6518 return map_dim_opt(map, pos, 0);
6521 /* Compute the maximum of the given output dimension as a function of the
6522 * parameters and input dimensions, but independently of
6523 * the other output dimensions.
6525 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6527 return map_dim_opt(map, pos, 1);
6530 /* Compute the minimum or maximum of the given set dimension
6531 * as a function of the parameters,
6532 * but independently of the other set dimensions.
6534 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6535 int max)
6537 return map_dim_opt(set, pos, max);
6540 /* Compute the maximum of the given set dimension as a function of the
6541 * parameters, but independently of the other set dimensions.
6543 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6545 return set_dim_opt(set, pos, 1);
6548 /* Compute the minimum of the given set dimension as a function of the
6549 * parameters, but independently of the other set dimensions.
6551 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6553 return set_dim_opt(set, pos, 0);
6556 /* Apply a preimage specified by "mat" on the parameters of "bset".
6557 * bset is assumed to have only parameters and divs.
6559 static struct isl_basic_set *basic_set_parameter_preimage(
6560 struct isl_basic_set *bset, struct isl_mat *mat)
6562 unsigned nparam;
6564 if (!bset || !mat)
6565 goto error;
6567 bset->dim = isl_space_cow(bset->dim);
6568 if (!bset->dim)
6569 goto error;
6571 nparam = isl_basic_set_dim(bset, isl_dim_param);
6573 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6575 bset->dim->nparam = 0;
6576 bset->dim->n_out = nparam;
6577 bset = isl_basic_set_preimage(bset, mat);
6578 if (bset) {
6579 bset->dim->nparam = bset->dim->n_out;
6580 bset->dim->n_out = 0;
6582 return bset;
6583 error:
6584 isl_mat_free(mat);
6585 isl_basic_set_free(bset);
6586 return NULL;
6589 /* Apply a preimage specified by "mat" on the parameters of "set".
6590 * set is assumed to have only parameters and divs.
6592 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6593 __isl_take isl_mat *mat)
6595 isl_space *space;
6596 unsigned nparam;
6598 if (!set || !mat)
6599 goto error;
6601 nparam = isl_set_dim(set, isl_dim_param);
6603 if (mat->n_row != 1 + nparam)
6604 isl_die(isl_set_get_ctx(set), isl_error_internal,
6605 "unexpected number of rows", goto error);
6607 space = isl_set_get_space(set);
6608 space = isl_space_move_dims(space, isl_dim_set, 0,
6609 isl_dim_param, 0, nparam);
6610 set = isl_set_reset_space(set, space);
6611 set = isl_set_preimage(set, mat);
6612 nparam = isl_set_dim(set, isl_dim_out);
6613 space = isl_set_get_space(set);
6614 space = isl_space_move_dims(space, isl_dim_param, 0,
6615 isl_dim_out, 0, nparam);
6616 set = isl_set_reset_space(set, space);
6617 return set;
6618 error:
6619 isl_mat_free(mat);
6620 isl_set_free(set);
6621 return NULL;
6624 /* Intersect the basic set "bset" with the affine space specified by the
6625 * equalities in "eq".
6627 static struct isl_basic_set *basic_set_append_equalities(
6628 struct isl_basic_set *bset, struct isl_mat *eq)
6630 int i, k;
6631 unsigned len;
6633 if (!bset || !eq)
6634 goto error;
6636 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6637 eq->n_row, 0);
6638 if (!bset)
6639 goto error;
6641 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6642 for (i = 0; i < eq->n_row; ++i) {
6643 k = isl_basic_set_alloc_equality(bset);
6644 if (k < 0)
6645 goto error;
6646 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6647 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6649 isl_mat_free(eq);
6651 bset = isl_basic_set_gauss(bset, NULL);
6652 bset = isl_basic_set_finalize(bset);
6654 return bset;
6655 error:
6656 isl_mat_free(eq);
6657 isl_basic_set_free(bset);
6658 return NULL;
6661 /* Intersect the set "set" with the affine space specified by the
6662 * equalities in "eq".
6664 static struct isl_set *set_append_equalities(struct isl_set *set,
6665 struct isl_mat *eq)
6667 int i;
6669 if (!set || !eq)
6670 goto error;
6672 for (i = 0; i < set->n; ++i) {
6673 set->p[i] = basic_set_append_equalities(set->p[i],
6674 isl_mat_copy(eq));
6675 if (!set->p[i])
6676 goto error;
6678 isl_mat_free(eq);
6679 return set;
6680 error:
6681 isl_mat_free(eq);
6682 isl_set_free(set);
6683 return NULL;
6686 /* Given a basic set "bset" that only involves parameters and existentially
6687 * quantified variables, return the index of the first equality
6688 * that only involves parameters. If there is no such equality then
6689 * return bset->n_eq.
6691 * This function assumes that isl_basic_set_gauss has been called on "bset".
6693 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6695 int i, j;
6696 unsigned nparam, n_div;
6698 if (!bset)
6699 return -1;
6701 nparam = isl_basic_set_dim(bset, isl_dim_param);
6702 n_div = isl_basic_set_dim(bset, isl_dim_div);
6704 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6705 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6706 ++i;
6709 return i;
6712 /* Compute an explicit representation for the existentially quantified
6713 * variables in "bset" by computing the "minimal value" of the set
6714 * variables. Since there are no set variables, the computation of
6715 * the minimal value essentially computes an explicit representation
6716 * of the non-empty part(s) of "bset".
6718 * The input only involves parameters and existentially quantified variables.
6719 * All equalities among parameters have been removed.
6721 * Since the existentially quantified variables in the result are in general
6722 * going to be different from those in the input, we first replace
6723 * them by the minimal number of variables based on their equalities.
6724 * This should simplify the parametric integer programming.
6726 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6728 isl_morph *morph1, *morph2;
6729 isl_set *set;
6730 unsigned n;
6732 if (!bset)
6733 return NULL;
6734 if (bset->n_eq == 0)
6735 return isl_basic_set_lexmin_compute_divs(bset);
6737 morph1 = isl_basic_set_parameter_compression(bset);
6738 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6739 bset = isl_basic_set_lift(bset);
6740 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6741 bset = isl_morph_basic_set(morph2, bset);
6742 n = isl_basic_set_dim(bset, isl_dim_set);
6743 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6745 set = isl_basic_set_lexmin_compute_divs(bset);
6747 set = isl_morph_set(isl_morph_inverse(morph1), set);
6749 return set;
6752 /* Project the given basic set onto its parameter domain, possibly introducing
6753 * new, explicit, existential variables in the constraints.
6754 * The input has parameters and (possibly implicit) existential variables.
6755 * The output has the same parameters, but only
6756 * explicit existentially quantified variables.
6758 * The actual projection is performed by pip, but pip doesn't seem
6759 * to like equalities very much, so we first remove the equalities
6760 * among the parameters by performing a variable compression on
6761 * the parameters. Afterward, an inverse transformation is performed
6762 * and the equalities among the parameters are inserted back in.
6764 * The variable compression on the parameters may uncover additional
6765 * equalities that were only implicit before. We therefore check
6766 * if there are any new parameter equalities in the result and
6767 * if so recurse. The removal of parameter equalities is required
6768 * for the parameter compression performed by base_compute_divs.
6770 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6772 int i;
6773 struct isl_mat *eq;
6774 struct isl_mat *T, *T2;
6775 struct isl_set *set;
6776 unsigned nparam;
6778 bset = isl_basic_set_cow(bset);
6779 if (!bset)
6780 return NULL;
6782 if (bset->n_eq == 0)
6783 return base_compute_divs(bset);
6785 bset = isl_basic_set_gauss(bset, NULL);
6786 if (!bset)
6787 return NULL;
6788 if (isl_basic_set_plain_is_empty(bset))
6789 return isl_set_from_basic_set(bset);
6791 i = first_parameter_equality(bset);
6792 if (i == bset->n_eq)
6793 return base_compute_divs(bset);
6795 nparam = isl_basic_set_dim(bset, isl_dim_param);
6796 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6797 0, 1 + nparam);
6798 eq = isl_mat_cow(eq);
6799 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6800 if (T && T->n_col == 0) {
6801 isl_mat_free(T);
6802 isl_mat_free(T2);
6803 isl_mat_free(eq);
6804 bset = isl_basic_set_set_to_empty(bset);
6805 return isl_set_from_basic_set(bset);
6807 bset = basic_set_parameter_preimage(bset, T);
6809 i = first_parameter_equality(bset);
6810 if (!bset)
6811 set = NULL;
6812 else if (i == bset->n_eq)
6813 set = base_compute_divs(bset);
6814 else
6815 set = parameter_compute_divs(bset);
6816 set = set_parameter_preimage(set, T2);
6817 set = set_append_equalities(set, eq);
6818 return set;
6821 /* Insert the divs from "ls" before those of "bmap".
6823 * The number of columns is not changed, which means that the last
6824 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6825 * The caller is responsible for removing the same number of dimensions
6826 * from the space of "bmap".
6828 static __isl_give isl_basic_map *insert_divs_from_local_space(
6829 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6831 int i;
6832 int n_div;
6833 int old_n_div;
6835 n_div = isl_local_space_dim(ls, isl_dim_div);
6836 if (n_div == 0)
6837 return bmap;
6839 old_n_div = bmap->n_div;
6840 bmap = insert_div_rows(bmap, n_div);
6841 if (!bmap)
6842 return NULL;
6844 for (i = 0; i < n_div; ++i) {
6845 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6846 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6849 return bmap;
6852 /* Replace the space of "bmap" by the space and divs of "ls".
6854 * If "ls" has any divs, then we simplify the result since we may
6855 * have discovered some additional equalities that could simplify
6856 * the div expressions.
6858 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6859 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6861 int n_div;
6863 bmap = isl_basic_map_cow(bmap);
6864 if (!bmap || !ls)
6865 goto error;
6867 n_div = isl_local_space_dim(ls, isl_dim_div);
6868 bmap = insert_divs_from_local_space(bmap, ls);
6869 if (!bmap)
6870 goto error;
6872 isl_space_free(bmap->dim);
6873 bmap->dim = isl_local_space_get_space(ls);
6874 if (!bmap->dim)
6875 goto error;
6877 isl_local_space_free(ls);
6878 if (n_div > 0)
6879 bmap = isl_basic_map_simplify(bmap);
6880 bmap = isl_basic_map_finalize(bmap);
6881 return bmap;
6882 error:
6883 isl_basic_map_free(bmap);
6884 isl_local_space_free(ls);
6885 return NULL;
6888 /* Replace the space of "map" by the space and divs of "ls".
6890 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6891 __isl_take isl_local_space *ls)
6893 int i;
6895 map = isl_map_cow(map);
6896 if (!map || !ls)
6897 goto error;
6899 for (i = 0; i < map->n; ++i) {
6900 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6901 isl_local_space_copy(ls));
6902 if (!map->p[i])
6903 goto error;
6905 isl_space_free(map->dim);
6906 map->dim = isl_local_space_get_space(ls);
6907 if (!map->dim)
6908 goto error;
6910 isl_local_space_free(ls);
6911 return map;
6912 error:
6913 isl_local_space_free(ls);
6914 isl_map_free(map);
6915 return NULL;
6918 /* Compute an explicit representation for the existentially
6919 * quantified variables for which do not know any explicit representation yet.
6921 * We first sort the existentially quantified variables so that the
6922 * existentially quantified variables for which we already have an explicit
6923 * representation are placed before those for which we do not.
6924 * The input dimensions, the output dimensions and the existentially
6925 * quantified variables for which we already have an explicit
6926 * representation are then turned into parameters.
6927 * compute_divs returns a map with the same parameters and
6928 * no input or output dimensions and the dimension specification
6929 * is reset to that of the input, including the existentially quantified
6930 * variables for which we already had an explicit representation.
6932 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6934 struct isl_basic_set *bset;
6935 struct isl_set *set;
6936 struct isl_map *map;
6937 isl_space *dim;
6938 isl_local_space *ls;
6939 unsigned nparam;
6940 unsigned n_in;
6941 unsigned n_out;
6942 int n_known;
6943 int i;
6945 bmap = isl_basic_map_sort_divs(bmap);
6946 bmap = isl_basic_map_cow(bmap);
6947 if (!bmap)
6948 return NULL;
6950 n_known = isl_basic_map_first_unknown_div(bmap);
6951 if (n_known < 0)
6952 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6954 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6955 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6956 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6957 dim = isl_space_set_alloc(bmap->ctx,
6958 nparam + n_in + n_out + n_known, 0);
6959 if (!dim)
6960 goto error;
6962 ls = isl_basic_map_get_local_space(bmap);
6963 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6964 n_known, bmap->n_div - n_known);
6965 if (n_known > 0) {
6966 for (i = n_known; i < bmap->n_div; ++i)
6967 swap_div(bmap, i - n_known, i);
6968 bmap->n_div -= n_known;
6969 bmap->extra -= n_known;
6971 bmap = isl_basic_map_reset_space(bmap, dim);
6972 bset = bset_from_bmap(bmap);
6974 set = parameter_compute_divs(bset);
6975 map = set_to_map(set);
6976 map = replace_space_by_local_space(map, ls);
6978 return map;
6979 error:
6980 isl_basic_map_free(bmap);
6981 return NULL;
6984 /* Remove the explicit representation of local variable "div",
6985 * if there is any.
6987 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6988 __isl_take isl_basic_map *bmap, int div)
6990 isl_bool unknown;
6992 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
6993 if (unknown < 0)
6994 return isl_basic_map_free(bmap);
6995 if (unknown)
6996 return bmap;
6998 bmap = isl_basic_map_cow(bmap);
6999 if (!bmap)
7000 return NULL;
7001 isl_int_set_si(bmap->div[div][0], 0);
7002 return bmap;
7005 /* Is local variable "div" of "bmap" marked as not having an explicit
7006 * representation?
7007 * Note that even if "div" is not marked in this way and therefore
7008 * has an explicit representation, this representation may still
7009 * depend (indirectly) on other local variables that do not
7010 * have an explicit representation.
7012 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7013 int div)
7015 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7016 return isl_bool_error;
7017 return isl_int_is_zero(bmap->div[div][0]);
7020 /* Return the position of the first local variable that does not
7021 * have an explicit representation.
7022 * Return the total number of local variables if they all have
7023 * an explicit representation.
7024 * Return -1 on error.
7026 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7028 int i;
7030 if (!bmap)
7031 return -1;
7033 for (i = 0; i < bmap->n_div; ++i) {
7034 if (!isl_basic_map_div_is_known(bmap, i))
7035 return i;
7037 return bmap->n_div;
7040 /* Return the position of the first local variable that does not
7041 * have an explicit representation.
7042 * Return the total number of local variables if they all have
7043 * an explicit representation.
7044 * Return -1 on error.
7046 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7048 return isl_basic_map_first_unknown_div(bset);
7051 /* Does "bmap" have an explicit representation for all local variables?
7053 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7055 int first, n;
7057 n = isl_basic_map_dim(bmap, isl_dim_div);
7058 first = isl_basic_map_first_unknown_div(bmap);
7059 if (first < 0)
7060 return isl_bool_error;
7061 return first == n;
7064 /* Do all basic maps in "map" have an explicit representation
7065 * for all local variables?
7067 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7069 int i;
7071 if (!map)
7072 return isl_bool_error;
7074 for (i = 0; i < map->n; ++i) {
7075 int known = isl_basic_map_divs_known(map->p[i]);
7076 if (known <= 0)
7077 return known;
7080 return isl_bool_true;
7083 /* If bmap contains any unknown divs, then compute explicit
7084 * expressions for them. However, this computation may be
7085 * quite expensive, so first try to remove divs that aren't
7086 * strictly needed.
7088 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7090 int known;
7091 struct isl_map *map;
7093 known = isl_basic_map_divs_known(bmap);
7094 if (known < 0)
7095 goto error;
7096 if (known)
7097 return isl_map_from_basic_map(bmap);
7099 bmap = isl_basic_map_drop_redundant_divs(bmap);
7101 known = isl_basic_map_divs_known(bmap);
7102 if (known < 0)
7103 goto error;
7104 if (known)
7105 return isl_map_from_basic_map(bmap);
7107 map = compute_divs(bmap);
7108 return map;
7109 error:
7110 isl_basic_map_free(bmap);
7111 return NULL;
7114 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7116 int i;
7117 int known;
7118 struct isl_map *res;
7120 if (!map)
7121 return NULL;
7122 if (map->n == 0)
7123 return map;
7125 known = isl_map_divs_known(map);
7126 if (known < 0) {
7127 isl_map_free(map);
7128 return NULL;
7130 if (known)
7131 return map;
7133 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7134 for (i = 1 ; i < map->n; ++i) {
7135 struct isl_map *r2;
7136 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7137 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7138 res = isl_map_union_disjoint(res, r2);
7139 else
7140 res = isl_map_union(res, r2);
7142 isl_map_free(map);
7144 return res;
7147 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7149 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7152 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7154 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7157 struct isl_set *isl_map_domain(struct isl_map *map)
7159 int i;
7160 struct isl_set *set;
7162 if (!map)
7163 goto error;
7165 map = isl_map_cow(map);
7166 if (!map)
7167 return NULL;
7169 set = set_from_map(map);
7170 set->dim = isl_space_domain(set->dim);
7171 if (!set->dim)
7172 goto error;
7173 for (i = 0; i < map->n; ++i) {
7174 set->p[i] = isl_basic_map_domain(map->p[i]);
7175 if (!set->p[i])
7176 goto error;
7178 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7179 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7180 return set;
7181 error:
7182 isl_map_free(map);
7183 return NULL;
7186 /* Return the union of "map1" and "map2", where we assume for now that
7187 * "map1" and "map2" are disjoint. Note that the basic maps inside
7188 * "map1" or "map2" may not be disjoint from each other.
7189 * Also note that this function is also called from isl_map_union,
7190 * which takes care of handling the situation where "map1" and "map2"
7191 * may not be disjoint.
7193 * If one of the inputs is empty, we can simply return the other input.
7194 * Similarly, if one of the inputs is universal, then it is equal to the union.
7196 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7197 __isl_take isl_map *map2)
7199 int i;
7200 unsigned flags = 0;
7201 struct isl_map *map = NULL;
7202 int is_universe;
7204 if (!map1 || !map2)
7205 goto error;
7207 if (!isl_space_is_equal(map1->dim, map2->dim))
7208 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7209 "spaces don't match", goto error);
7211 if (map1->n == 0) {
7212 isl_map_free(map1);
7213 return map2;
7215 if (map2->n == 0) {
7216 isl_map_free(map2);
7217 return map1;
7220 is_universe = isl_map_plain_is_universe(map1);
7221 if (is_universe < 0)
7222 goto error;
7223 if (is_universe) {
7224 isl_map_free(map2);
7225 return map1;
7228 is_universe = isl_map_plain_is_universe(map2);
7229 if (is_universe < 0)
7230 goto error;
7231 if (is_universe) {
7232 isl_map_free(map1);
7233 return map2;
7236 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7237 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7238 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7240 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7241 map1->n + map2->n, flags);
7242 if (!map)
7243 goto error;
7244 for (i = 0; i < map1->n; ++i) {
7245 map = isl_map_add_basic_map(map,
7246 isl_basic_map_copy(map1->p[i]));
7247 if (!map)
7248 goto error;
7250 for (i = 0; i < map2->n; ++i) {
7251 map = isl_map_add_basic_map(map,
7252 isl_basic_map_copy(map2->p[i]));
7253 if (!map)
7254 goto error;
7256 isl_map_free(map1);
7257 isl_map_free(map2);
7258 return map;
7259 error:
7260 isl_map_free(map);
7261 isl_map_free(map1);
7262 isl_map_free(map2);
7263 return NULL;
7266 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7267 * guaranteed to be disjoint by the caller.
7269 * Note that this functions is called from within isl_map_make_disjoint,
7270 * so we have to be careful not to touch the constraints of the inputs
7271 * in any way.
7273 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7274 __isl_take isl_map *map2)
7276 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7279 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7280 * not be disjoint. The parameters are assumed to have been aligned.
7282 * We currently simply call map_union_disjoint, the internal operation
7283 * of which does not really depend on the inputs being disjoint.
7284 * If the result contains more than one basic map, then we clear
7285 * the disjoint flag since the result may contain basic maps from
7286 * both inputs and these are not guaranteed to be disjoint.
7288 * As a special case, if "map1" and "map2" are obviously equal,
7289 * then we simply return "map1".
7291 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7292 __isl_take isl_map *map2)
7294 int equal;
7296 if (!map1 || !map2)
7297 goto error;
7299 equal = isl_map_plain_is_equal(map1, map2);
7300 if (equal < 0)
7301 goto error;
7302 if (equal) {
7303 isl_map_free(map2);
7304 return map1;
7307 map1 = map_union_disjoint(map1, map2);
7308 if (!map1)
7309 return NULL;
7310 if (map1->n > 1)
7311 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7312 return map1;
7313 error:
7314 isl_map_free(map1);
7315 isl_map_free(map2);
7316 return NULL;
7319 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7320 * not be disjoint.
7322 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7323 __isl_take isl_map *map2)
7325 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7328 struct isl_set *isl_set_union_disjoint(
7329 struct isl_set *set1, struct isl_set *set2)
7331 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7332 set_to_map(set2)));
7335 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7337 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7340 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7341 * the results.
7343 * "map" and "set" are assumed to be compatible and non-NULL.
7345 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7346 __isl_take isl_set *set,
7347 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7348 __isl_take isl_basic_set *bset))
7350 unsigned flags = 0;
7351 struct isl_map *result;
7352 int i, j;
7354 if (isl_set_plain_is_universe(set)) {
7355 isl_set_free(set);
7356 return map;
7359 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7360 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7361 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7363 result = isl_map_alloc_space(isl_space_copy(map->dim),
7364 map->n * set->n, flags);
7365 for (i = 0; result && i < map->n; ++i)
7366 for (j = 0; j < set->n; ++j) {
7367 result = isl_map_add_basic_map(result,
7368 fn(isl_basic_map_copy(map->p[i]),
7369 isl_basic_set_copy(set->p[j])));
7370 if (!result)
7371 break;
7374 isl_map_free(map);
7375 isl_set_free(set);
7376 return result;
7379 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7380 __isl_take isl_set *set)
7382 isl_bool ok;
7384 ok = isl_map_compatible_range(map, set);
7385 if (ok < 0)
7386 goto error;
7387 if (!ok)
7388 isl_die(set->ctx, isl_error_invalid,
7389 "incompatible spaces", goto error);
7391 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7392 error:
7393 isl_map_free(map);
7394 isl_set_free(set);
7395 return NULL;
7398 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7399 __isl_take isl_set *set)
7401 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7404 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7405 __isl_take isl_set *set)
7407 isl_bool ok;
7409 ok = isl_map_compatible_domain(map, set);
7410 if (ok < 0)
7411 goto error;
7412 if (!ok)
7413 isl_die(set->ctx, isl_error_invalid,
7414 "incompatible spaces", goto error);
7416 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7417 error:
7418 isl_map_free(map);
7419 isl_set_free(set);
7420 return NULL;
7423 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7424 __isl_take isl_set *set)
7426 return isl_map_align_params_map_map_and(map, set,
7427 &map_intersect_domain);
7430 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7431 __isl_take isl_map *map2)
7433 if (!map1 || !map2)
7434 goto error;
7435 map1 = isl_map_reverse(map1);
7436 map1 = isl_map_apply_range(map1, map2);
7437 return isl_map_reverse(map1);
7438 error:
7439 isl_map_free(map1);
7440 isl_map_free(map2);
7441 return NULL;
7444 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7445 __isl_take isl_map *map2)
7447 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7450 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7451 __isl_take isl_map *map2)
7453 isl_space *dim_result;
7454 struct isl_map *result;
7455 int i, j;
7457 if (!map1 || !map2)
7458 goto error;
7460 dim_result = isl_space_join(isl_space_copy(map1->dim),
7461 isl_space_copy(map2->dim));
7463 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7464 if (!result)
7465 goto error;
7466 for (i = 0; i < map1->n; ++i)
7467 for (j = 0; j < map2->n; ++j) {
7468 result = isl_map_add_basic_map(result,
7469 isl_basic_map_apply_range(
7470 isl_basic_map_copy(map1->p[i]),
7471 isl_basic_map_copy(map2->p[j])));
7472 if (!result)
7473 goto error;
7475 isl_map_free(map1);
7476 isl_map_free(map2);
7477 if (result && result->n <= 1)
7478 ISL_F_SET(result, ISL_MAP_DISJOINT);
7479 return result;
7480 error:
7481 isl_map_free(map1);
7482 isl_map_free(map2);
7483 return NULL;
7486 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7487 __isl_take isl_map *map2)
7489 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7493 * returns range - domain
7495 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7497 isl_space *target_space;
7498 struct isl_basic_set *bset;
7499 unsigned dim;
7500 unsigned nparam;
7501 int i;
7503 if (!bmap)
7504 goto error;
7505 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7506 bmap->dim, isl_dim_out),
7507 goto error);
7508 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7509 dim = isl_basic_map_n_in(bmap);
7510 nparam = isl_basic_map_n_param(bmap);
7511 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7512 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7513 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7514 for (i = 0; i < dim; ++i) {
7515 int j = isl_basic_map_alloc_equality(bmap);
7516 if (j < 0) {
7517 bmap = isl_basic_map_free(bmap);
7518 break;
7520 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7521 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7522 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7523 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7525 bset = isl_basic_map_domain(bmap);
7526 bset = isl_basic_set_reset_space(bset, target_space);
7527 return bset;
7528 error:
7529 isl_basic_map_free(bmap);
7530 return NULL;
7534 * returns range - domain
7536 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7538 int i;
7539 isl_space *dim;
7540 struct isl_set *result;
7542 if (!map)
7543 return NULL;
7545 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7546 map->dim, isl_dim_out),
7547 goto error);
7548 dim = isl_map_get_space(map);
7549 dim = isl_space_domain(dim);
7550 result = isl_set_alloc_space(dim, map->n, 0);
7551 if (!result)
7552 goto error;
7553 for (i = 0; i < map->n; ++i)
7554 result = isl_set_add_basic_set(result,
7555 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7556 isl_map_free(map);
7557 return result;
7558 error:
7559 isl_map_free(map);
7560 return NULL;
7564 * returns [domain -> range] -> range - domain
7566 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7567 __isl_take isl_basic_map *bmap)
7569 int i, k;
7570 isl_space *dim;
7571 isl_basic_map *domain;
7572 int nparam, n;
7573 unsigned total;
7575 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7576 bmap->dim, isl_dim_out))
7577 isl_die(bmap->ctx, isl_error_invalid,
7578 "domain and range don't match", goto error);
7580 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7581 n = isl_basic_map_dim(bmap, isl_dim_in);
7583 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7584 domain = isl_basic_map_universe(dim);
7586 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7587 bmap = isl_basic_map_apply_range(bmap, domain);
7588 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7590 total = isl_basic_map_total_dim(bmap);
7592 for (i = 0; i < n; ++i) {
7593 k = isl_basic_map_alloc_equality(bmap);
7594 if (k < 0)
7595 goto error;
7596 isl_seq_clr(bmap->eq[k], 1 + total);
7597 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7598 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7599 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7602 bmap = isl_basic_map_gauss(bmap, NULL);
7603 return isl_basic_map_finalize(bmap);
7604 error:
7605 isl_basic_map_free(bmap);
7606 return NULL;
7610 * returns [domain -> range] -> range - domain
7612 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7614 int i;
7615 isl_space *domain_dim;
7617 if (!map)
7618 return NULL;
7620 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7621 map->dim, isl_dim_out))
7622 isl_die(map->ctx, isl_error_invalid,
7623 "domain and range don't match", goto error);
7625 map = isl_map_cow(map);
7626 if (!map)
7627 return NULL;
7629 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7630 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7631 map->dim = isl_space_join(map->dim, domain_dim);
7632 if (!map->dim)
7633 goto error;
7634 for (i = 0; i < map->n; ++i) {
7635 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7636 if (!map->p[i])
7637 goto error;
7639 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7640 return map;
7641 error:
7642 isl_map_free(map);
7643 return NULL;
7646 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7648 struct isl_basic_map *bmap;
7649 unsigned nparam;
7650 unsigned dim;
7651 int i;
7653 if (!dims)
7654 return NULL;
7656 nparam = dims->nparam;
7657 dim = dims->n_out;
7658 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7659 if (!bmap)
7660 goto error;
7662 for (i = 0; i < dim; ++i) {
7663 int j = isl_basic_map_alloc_equality(bmap);
7664 if (j < 0)
7665 goto error;
7666 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7667 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7668 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7670 return isl_basic_map_finalize(bmap);
7671 error:
7672 isl_basic_map_free(bmap);
7673 return NULL;
7676 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7678 if (!dim)
7679 return NULL;
7680 if (dim->n_in != dim->n_out)
7681 isl_die(dim->ctx, isl_error_invalid,
7682 "number of input and output dimensions needs to be "
7683 "the same", goto error);
7684 return basic_map_identity(dim);
7685 error:
7686 isl_space_free(dim);
7687 return NULL;
7690 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7692 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7695 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7697 isl_space *dim = isl_set_get_space(set);
7698 isl_map *id;
7699 id = isl_map_identity(isl_space_map_from_set(dim));
7700 return isl_map_intersect_range(id, set);
7703 /* Construct a basic set with all set dimensions having only non-negative
7704 * values.
7706 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7707 __isl_take isl_space *space)
7709 int i;
7710 unsigned nparam;
7711 unsigned dim;
7712 struct isl_basic_set *bset;
7714 if (!space)
7715 return NULL;
7716 nparam = space->nparam;
7717 dim = space->n_out;
7718 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7719 if (!bset)
7720 return NULL;
7721 for (i = 0; i < dim; ++i) {
7722 int k = isl_basic_set_alloc_inequality(bset);
7723 if (k < 0)
7724 goto error;
7725 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7726 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7728 return bset;
7729 error:
7730 isl_basic_set_free(bset);
7731 return NULL;
7734 /* Construct the half-space x_pos >= 0.
7736 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7737 int pos)
7739 int k;
7740 isl_basic_set *nonneg;
7742 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7743 k = isl_basic_set_alloc_inequality(nonneg);
7744 if (k < 0)
7745 goto error;
7746 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7747 isl_int_set_si(nonneg->ineq[k][pos], 1);
7749 return isl_basic_set_finalize(nonneg);
7750 error:
7751 isl_basic_set_free(nonneg);
7752 return NULL;
7755 /* Construct the half-space x_pos <= -1.
7757 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7759 int k;
7760 isl_basic_set *neg;
7762 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7763 k = isl_basic_set_alloc_inequality(neg);
7764 if (k < 0)
7765 goto error;
7766 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7767 isl_int_set_si(neg->ineq[k][0], -1);
7768 isl_int_set_si(neg->ineq[k][pos], -1);
7770 return isl_basic_set_finalize(neg);
7771 error:
7772 isl_basic_set_free(neg);
7773 return NULL;
7776 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7777 enum isl_dim_type type, unsigned first, unsigned n)
7779 int i;
7780 unsigned offset;
7781 isl_basic_set *nonneg;
7782 isl_basic_set *neg;
7784 if (!set)
7785 return NULL;
7786 if (n == 0)
7787 return set;
7789 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7791 offset = pos(set->dim, type);
7792 for (i = 0; i < n; ++i) {
7793 nonneg = nonneg_halfspace(isl_set_get_space(set),
7794 offset + first + i);
7795 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7797 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7800 return set;
7801 error:
7802 isl_set_free(set);
7803 return NULL;
7806 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7807 int len,
7808 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7809 void *user)
7811 isl_set *half;
7813 if (!set)
7814 return isl_stat_error;
7815 if (isl_set_plain_is_empty(set)) {
7816 isl_set_free(set);
7817 return isl_stat_ok;
7819 if (first == len)
7820 return fn(set, signs, user);
7822 signs[first] = 1;
7823 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7824 1 + first));
7825 half = isl_set_intersect(half, isl_set_copy(set));
7826 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7827 goto error;
7829 signs[first] = -1;
7830 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7831 1 + first));
7832 half = isl_set_intersect(half, set);
7833 return foreach_orthant(half, signs, first + 1, len, fn, user);
7834 error:
7835 isl_set_free(set);
7836 return isl_stat_error;
7839 /* Call "fn" on the intersections of "set" with each of the orthants
7840 * (except for obviously empty intersections). The orthant is identified
7841 * by the signs array, with each entry having value 1 or -1 according
7842 * to the sign of the corresponding variable.
7844 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
7845 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7846 void *user)
7848 unsigned nparam;
7849 unsigned nvar;
7850 int *signs;
7851 isl_stat r;
7853 if (!set)
7854 return isl_stat_error;
7855 if (isl_set_plain_is_empty(set))
7856 return isl_stat_ok;
7858 nparam = isl_set_dim(set, isl_dim_param);
7859 nvar = isl_set_dim(set, isl_dim_set);
7861 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7863 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7864 fn, user);
7866 free(signs);
7868 return r;
7871 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7873 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
7876 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7877 __isl_keep isl_basic_map *bmap2)
7879 int is_subset;
7880 struct isl_map *map1;
7881 struct isl_map *map2;
7883 if (!bmap1 || !bmap2)
7884 return isl_bool_error;
7886 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7887 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7889 is_subset = isl_map_is_subset(map1, map2);
7891 isl_map_free(map1);
7892 isl_map_free(map2);
7894 return is_subset;
7897 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7898 __isl_keep isl_basic_set *bset2)
7900 return isl_basic_map_is_subset(bset1, bset2);
7903 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7904 __isl_keep isl_basic_map *bmap2)
7906 isl_bool is_subset;
7908 if (!bmap1 || !bmap2)
7909 return isl_bool_error;
7910 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7911 if (is_subset != isl_bool_true)
7912 return is_subset;
7913 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7914 return is_subset;
7917 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7918 __isl_keep isl_basic_set *bset2)
7920 return isl_basic_map_is_equal(
7921 bset_to_bmap(bset1), bset_to_bmap(bset2));
7924 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7926 int i;
7927 int is_empty;
7929 if (!map)
7930 return isl_bool_error;
7931 for (i = 0; i < map->n; ++i) {
7932 is_empty = isl_basic_map_is_empty(map->p[i]);
7933 if (is_empty < 0)
7934 return isl_bool_error;
7935 if (!is_empty)
7936 return isl_bool_false;
7938 return isl_bool_true;
7941 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7943 return map ? map->n == 0 : isl_bool_error;
7946 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7948 return set ? set->n == 0 : isl_bool_error;
7951 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7953 return isl_map_is_empty(set_to_map(set));
7956 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
7957 __isl_keep isl_map *map2)
7959 if (!map1 || !map2)
7960 return isl_bool_error;
7962 return isl_space_is_equal(map1->dim, map2->dim);
7965 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
7966 __isl_keep isl_set *set2)
7968 if (!set1 || !set2)
7969 return isl_bool_error;
7971 return isl_space_is_equal(set1->dim, set2->dim);
7974 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7976 isl_bool is_subset;
7978 if (!map1 || !map2)
7979 return isl_bool_error;
7980 is_subset = isl_map_is_subset(map1, map2);
7981 if (is_subset != isl_bool_true)
7982 return is_subset;
7983 is_subset = isl_map_is_subset(map2, map1);
7984 return is_subset;
7987 /* Is "map1" equal to "map2"?
7989 * First check if they are obviously equal.
7990 * If not, then perform a more detailed analysis.
7992 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7994 isl_bool equal;
7996 equal = isl_map_plain_is_equal(map1, map2);
7997 if (equal < 0 || equal)
7998 return equal;
7999 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8002 isl_bool isl_basic_map_is_strict_subset(
8003 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8005 isl_bool is_subset;
8007 if (!bmap1 || !bmap2)
8008 return isl_bool_error;
8009 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8010 if (is_subset != isl_bool_true)
8011 return is_subset;
8012 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8013 if (is_subset == isl_bool_error)
8014 return is_subset;
8015 return !is_subset;
8018 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8019 __isl_keep isl_map *map2)
8021 isl_bool is_subset;
8023 if (!map1 || !map2)
8024 return isl_bool_error;
8025 is_subset = isl_map_is_subset(map1, map2);
8026 if (is_subset != isl_bool_true)
8027 return is_subset;
8028 is_subset = isl_map_is_subset(map2, map1);
8029 if (is_subset == isl_bool_error)
8030 return is_subset;
8031 return !is_subset;
8034 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8035 __isl_keep isl_set *set2)
8037 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8040 /* Is "bmap" obviously equal to the universe with the same space?
8042 * That is, does it not have any constraints?
8044 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8046 if (!bmap)
8047 return isl_bool_error;
8048 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8051 /* Is "bset" obviously equal to the universe with the same space?
8053 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8055 return isl_basic_map_plain_is_universe(bset);
8058 /* If "c" does not involve any existentially quantified variables,
8059 * then set *univ to false and abort
8061 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8063 isl_bool *univ = user;
8064 unsigned n;
8066 n = isl_constraint_dim(c, isl_dim_div);
8067 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8068 isl_constraint_free(c);
8069 if (*univ < 0 || !*univ)
8070 return isl_stat_error;
8071 return isl_stat_ok;
8074 /* Is "bmap" equal to the universe with the same space?
8076 * First check if it is obviously equal to the universe.
8077 * If not and if there are any constraints not involving
8078 * existentially quantified variables, then it is certainly
8079 * not equal to the universe.
8080 * Otherwise, check if the universe is a subset of "bmap".
8082 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8084 isl_bool univ;
8085 isl_basic_map *test;
8087 univ = isl_basic_map_plain_is_universe(bmap);
8088 if (univ < 0 || univ)
8089 return univ;
8090 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8091 return isl_bool_false;
8092 univ = isl_bool_true;
8093 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8094 univ)
8095 return isl_bool_error;
8096 if (univ < 0 || !univ)
8097 return univ;
8098 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8099 univ = isl_basic_map_is_subset(test, bmap);
8100 isl_basic_map_free(test);
8101 return univ;
8104 /* Is "bset" equal to the universe with the same space?
8106 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8108 return isl_basic_map_is_universe(bset);
8111 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8113 int i;
8115 if (!map)
8116 return isl_bool_error;
8118 for (i = 0; i < map->n; ++i) {
8119 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8120 if (r < 0 || r)
8121 return r;
8124 return isl_bool_false;
8127 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8129 return isl_map_plain_is_universe(set_to_map(set));
8132 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8134 struct isl_basic_set *bset = NULL;
8135 struct isl_vec *sample = NULL;
8136 isl_bool empty, non_empty;
8138 if (!bmap)
8139 return isl_bool_error;
8141 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8142 return isl_bool_true;
8144 if (isl_basic_map_plain_is_universe(bmap))
8145 return isl_bool_false;
8147 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8148 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8149 copy = isl_basic_map_remove_redundancies(copy);
8150 empty = isl_basic_map_plain_is_empty(copy);
8151 isl_basic_map_free(copy);
8152 return empty;
8155 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8156 if (non_empty < 0)
8157 return isl_bool_error;
8158 if (non_empty)
8159 return isl_bool_false;
8160 isl_vec_free(bmap->sample);
8161 bmap->sample = NULL;
8162 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8163 if (!bset)
8164 return isl_bool_error;
8165 sample = isl_basic_set_sample_vec(bset);
8166 if (!sample)
8167 return isl_bool_error;
8168 empty = sample->size == 0;
8169 isl_vec_free(bmap->sample);
8170 bmap->sample = sample;
8171 if (empty)
8172 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8174 return empty;
8177 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8179 if (!bmap)
8180 return isl_bool_error;
8181 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8184 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8186 if (!bset)
8187 return isl_bool_error;
8188 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8191 /* Is "bmap" known to be non-empty?
8193 * That is, is the cached sample still valid?
8195 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8197 unsigned total;
8199 if (!bmap)
8200 return isl_bool_error;
8201 if (!bmap->sample)
8202 return isl_bool_false;
8203 total = 1 + isl_basic_map_total_dim(bmap);
8204 if (bmap->sample->size != total)
8205 return isl_bool_false;
8206 return isl_basic_map_contains(bmap, bmap->sample);
8209 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8211 return isl_basic_map_is_empty(bset_to_bmap(bset));
8214 struct isl_map *isl_basic_map_union(
8215 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8217 struct isl_map *map;
8218 if (!bmap1 || !bmap2)
8219 goto error;
8221 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8223 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8224 if (!map)
8225 goto error;
8226 map = isl_map_add_basic_map(map, bmap1);
8227 map = isl_map_add_basic_map(map, bmap2);
8228 return map;
8229 error:
8230 isl_basic_map_free(bmap1);
8231 isl_basic_map_free(bmap2);
8232 return NULL;
8235 struct isl_set *isl_basic_set_union(
8236 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8238 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8239 bset_to_bmap(bset2)));
8242 /* Order divs such that any div only depends on previous divs */
8243 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8245 int i;
8246 unsigned off;
8248 if (!bmap)
8249 return NULL;
8251 off = isl_space_dim(bmap->dim, isl_dim_all);
8253 for (i = 0; i < bmap->n_div; ++i) {
8254 int pos;
8255 if (isl_int_is_zero(bmap->div[i][0]))
8256 continue;
8257 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8258 bmap->n_div-i);
8259 if (pos == -1)
8260 continue;
8261 if (pos == 0)
8262 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8263 "integer division depends on itself",
8264 return isl_basic_map_free(bmap));
8265 isl_basic_map_swap_div(bmap, i, i + pos);
8266 --i;
8268 return bmap;
8271 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8273 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8276 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8278 int i;
8280 if (!map)
8281 return 0;
8283 for (i = 0; i < map->n; ++i) {
8284 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8285 if (!map->p[i])
8286 goto error;
8289 return map;
8290 error:
8291 isl_map_free(map);
8292 return NULL;
8295 /* Sort the local variables of "bset".
8297 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8298 __isl_take isl_basic_set *bset)
8300 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8303 /* Apply the expansion computed by isl_merge_divs.
8304 * The expansion itself is given by "exp" while the resulting
8305 * list of divs is given by "div".
8307 * Move the integer divisions of "bmap" into the right position
8308 * according to "exp" and then introduce the additional integer
8309 * divisions, adding div constraints.
8310 * The moving should be done first to avoid moving coefficients
8311 * in the definitions of the extra integer divisions.
8313 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8314 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8316 int i, j;
8317 int n_div;
8319 bmap = isl_basic_map_cow(bmap);
8320 if (!bmap || !div)
8321 goto error;
8323 if (div->n_row < bmap->n_div)
8324 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8325 "not an expansion", goto error);
8327 n_div = bmap->n_div;
8328 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8329 div->n_row - n_div, 0,
8330 2 * (div->n_row - n_div));
8332 for (i = n_div; i < div->n_row; ++i)
8333 if (isl_basic_map_alloc_div(bmap) < 0)
8334 goto error;
8336 for (j = n_div - 1; j >= 0; --j) {
8337 if (exp[j] == j)
8338 break;
8339 isl_basic_map_swap_div(bmap, j, exp[j]);
8341 j = 0;
8342 for (i = 0; i < div->n_row; ++i) {
8343 if (j < n_div && exp[j] == i) {
8344 j++;
8345 } else {
8346 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8347 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8348 continue;
8349 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8350 goto error;
8354 isl_mat_free(div);
8355 return bmap;
8356 error:
8357 isl_basic_map_free(bmap);
8358 isl_mat_free(div);
8359 return NULL;
8362 /* Apply the expansion computed by isl_merge_divs.
8363 * The expansion itself is given by "exp" while the resulting
8364 * list of divs is given by "div".
8366 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8367 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8369 return isl_basic_map_expand_divs(bset, div, exp);
8372 /* Look for a div in dst that corresponds to the div "div" in src.
8373 * The divs before "div" in src and dst are assumed to be the same.
8375 * Returns -1 if no corresponding div was found and the position
8376 * of the corresponding div in dst otherwise.
8378 static int find_div(struct isl_basic_map *dst,
8379 struct isl_basic_map *src, unsigned div)
8381 int i;
8383 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8385 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8386 for (i = div; i < dst->n_div; ++i)
8387 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8388 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8389 dst->n_div - div) == -1)
8390 return i;
8391 return -1;
8394 /* Align the divs of "dst" to those of "src", adding divs from "src"
8395 * if needed. That is, make sure that the first src->n_div divs
8396 * of the result are equal to those of src.
8398 * The result is not finalized as by design it will have redundant
8399 * divs if any divs from "src" were copied.
8401 __isl_give isl_basic_map *isl_basic_map_align_divs(
8402 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8404 int i;
8405 int known, extended;
8406 unsigned total;
8408 if (!dst || !src)
8409 return isl_basic_map_free(dst);
8411 if (src->n_div == 0)
8412 return dst;
8414 known = isl_basic_map_divs_known(src);
8415 if (known < 0)
8416 return isl_basic_map_free(dst);
8417 if (!known)
8418 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8419 "some src divs are unknown",
8420 return isl_basic_map_free(dst));
8422 src = isl_basic_map_order_divs(src);
8424 extended = 0;
8425 total = isl_space_dim(src->dim, isl_dim_all);
8426 for (i = 0; i < src->n_div; ++i) {
8427 int j = find_div(dst, src, i);
8428 if (j < 0) {
8429 if (!extended) {
8430 int extra = src->n_div - i;
8431 dst = isl_basic_map_cow(dst);
8432 if (!dst)
8433 return NULL;
8434 dst = isl_basic_map_extend_space(dst,
8435 isl_space_copy(dst->dim),
8436 extra, 0, 2 * extra);
8437 extended = 1;
8439 j = isl_basic_map_alloc_div(dst);
8440 if (j < 0)
8441 return isl_basic_map_free(dst);
8442 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8443 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8444 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8445 return isl_basic_map_free(dst);
8447 if (j != i)
8448 isl_basic_map_swap_div(dst, i, j);
8450 return dst;
8453 struct isl_map *isl_map_align_divs(struct isl_map *map)
8455 int i;
8457 if (!map)
8458 return NULL;
8459 if (map->n == 0)
8460 return map;
8461 map = isl_map_compute_divs(map);
8462 map = isl_map_cow(map);
8463 if (!map)
8464 return NULL;
8466 for (i = 1; i < map->n; ++i)
8467 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8468 for (i = 1; i < map->n; ++i) {
8469 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8470 if (!map->p[i])
8471 return isl_map_free(map);
8474 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8475 return map;
8478 struct isl_set *isl_set_align_divs(struct isl_set *set)
8480 return set_from_map(isl_map_align_divs(set_to_map(set)));
8483 /* Align the divs of the basic maps in "map" to those
8484 * of the basic maps in "list", as well as to the other basic maps in "map".
8485 * The elements in "list" are assumed to have known divs.
8487 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8488 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8490 int i, n;
8492 map = isl_map_compute_divs(map);
8493 map = isl_map_cow(map);
8494 if (!map || !list)
8495 return isl_map_free(map);
8496 if (map->n == 0)
8497 return map;
8499 n = isl_basic_map_list_n_basic_map(list);
8500 for (i = 0; i < n; ++i) {
8501 isl_basic_map *bmap;
8503 bmap = isl_basic_map_list_get_basic_map(list, i);
8504 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8505 isl_basic_map_free(bmap);
8507 if (!map->p[0])
8508 return isl_map_free(map);
8510 return isl_map_align_divs(map);
8513 /* Align the divs of each element of "list" to those of "bmap".
8514 * Both "bmap" and the elements of "list" are assumed to have known divs.
8516 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8517 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8519 int i, n;
8521 if (!list || !bmap)
8522 return isl_basic_map_list_free(list);
8524 n = isl_basic_map_list_n_basic_map(list);
8525 for (i = 0; i < n; ++i) {
8526 isl_basic_map *bmap_i;
8528 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8529 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8530 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8533 return list;
8536 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8537 __isl_take isl_map *map)
8539 isl_bool ok;
8541 ok = isl_map_compatible_domain(map, set);
8542 if (ok < 0)
8543 goto error;
8544 if (!ok)
8545 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8546 "incompatible spaces", goto error);
8547 map = isl_map_intersect_domain(map, set);
8548 set = isl_map_range(map);
8549 return set;
8550 error:
8551 isl_set_free(set);
8552 isl_map_free(map);
8553 return NULL;
8556 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8557 __isl_take isl_map *map)
8559 return isl_map_align_params_map_map_and(set, map, &set_apply);
8562 /* There is no need to cow as removing empty parts doesn't change
8563 * the meaning of the set.
8565 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8567 int i;
8569 if (!map)
8570 return NULL;
8572 for (i = map->n - 1; i >= 0; --i)
8573 remove_if_empty(map, i);
8575 return map;
8578 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8580 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8583 /* Given two basic sets bset1 and bset2, compute the maximal difference
8584 * between the values of dimension pos in bset1 and those in bset2
8585 * for any common value of the parameters and dimensions preceding pos.
8587 static enum isl_lp_result basic_set_maximal_difference_at(
8588 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8589 int pos, isl_int *opt)
8591 isl_basic_map *bmap1;
8592 isl_basic_map *bmap2;
8593 struct isl_ctx *ctx;
8594 struct isl_vec *obj;
8595 unsigned total;
8596 unsigned nparam;
8597 unsigned dim1;
8598 enum isl_lp_result res;
8600 if (!bset1 || !bset2)
8601 return isl_lp_error;
8603 nparam = isl_basic_set_n_param(bset1);
8604 dim1 = isl_basic_set_n_dim(bset1);
8606 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8607 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8608 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8609 isl_dim_out, 0, pos);
8610 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8611 isl_dim_out, 0, pos);
8612 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8613 if (!bmap1)
8614 return isl_lp_error;
8616 total = isl_basic_map_total_dim(bmap1);
8617 ctx = bmap1->ctx;
8618 obj = isl_vec_alloc(ctx, 1 + total);
8619 if (!obj)
8620 goto error;
8621 isl_seq_clr(obj->block.data, 1 + total);
8622 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8623 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8624 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8625 opt, NULL, NULL);
8626 isl_basic_map_free(bmap1);
8627 isl_vec_free(obj);
8628 return res;
8629 error:
8630 isl_basic_map_free(bmap1);
8631 return isl_lp_error;
8634 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8635 * for any common value of the parameters and dimensions preceding pos
8636 * in both basic sets, the values of dimension pos in bset1 are
8637 * smaller or larger than those in bset2.
8639 * Returns
8640 * 1 if bset1 follows bset2
8641 * -1 if bset1 precedes bset2
8642 * 0 if bset1 and bset2 are incomparable
8643 * -2 if some error occurred.
8645 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8646 struct isl_basic_set *bset2, int pos)
8648 isl_int opt;
8649 enum isl_lp_result res;
8650 int cmp;
8652 isl_int_init(opt);
8654 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8656 if (res == isl_lp_empty)
8657 cmp = 0;
8658 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8659 res == isl_lp_unbounded)
8660 cmp = 1;
8661 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8662 cmp = -1;
8663 else
8664 cmp = -2;
8666 isl_int_clear(opt);
8667 return cmp;
8670 /* Given two basic sets bset1 and bset2, check whether
8671 * for any common value of the parameters and dimensions preceding pos
8672 * there is a value of dimension pos in bset1 that is larger
8673 * than a value of the same dimension in bset2.
8675 * Return
8676 * 1 if there exists such a pair
8677 * 0 if there is no such pair, but there is a pair of equal values
8678 * -1 otherwise
8679 * -2 if some error occurred.
8681 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8682 __isl_keep isl_basic_set *bset2, int pos)
8684 isl_int opt;
8685 enum isl_lp_result res;
8686 int cmp;
8688 isl_int_init(opt);
8690 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8692 if (res == isl_lp_empty)
8693 cmp = -1;
8694 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8695 res == isl_lp_unbounded)
8696 cmp = 1;
8697 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8698 cmp = -1;
8699 else if (res == isl_lp_ok)
8700 cmp = 0;
8701 else
8702 cmp = -2;
8704 isl_int_clear(opt);
8705 return cmp;
8708 /* Given two sets set1 and set2, check whether
8709 * for any common value of the parameters and dimensions preceding pos
8710 * there is a value of dimension pos in set1 that is larger
8711 * than a value of the same dimension in set2.
8713 * Return
8714 * 1 if there exists such a pair
8715 * 0 if there is no such pair, but there is a pair of equal values
8716 * -1 otherwise
8717 * -2 if some error occurred.
8719 int isl_set_follows_at(__isl_keep isl_set *set1,
8720 __isl_keep isl_set *set2, int pos)
8722 int i, j;
8723 int follows = -1;
8725 if (!set1 || !set2)
8726 return -2;
8728 for (i = 0; i < set1->n; ++i)
8729 for (j = 0; j < set2->n; ++j) {
8730 int f;
8731 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8732 if (f == 1 || f == -2)
8733 return f;
8734 if (f > follows)
8735 follows = f;
8738 return follows;
8741 static isl_bool isl_basic_map_plain_has_fixed_var(
8742 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
8744 int i;
8745 int d;
8746 unsigned total;
8748 if (!bmap)
8749 return isl_bool_error;
8750 total = isl_basic_map_total_dim(bmap);
8751 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8752 for (; d+1 > pos; --d)
8753 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8754 break;
8755 if (d != pos)
8756 continue;
8757 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8758 return isl_bool_false;
8759 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8760 return isl_bool_false;
8761 if (!isl_int_is_one(bmap->eq[i][1+d]))
8762 return isl_bool_false;
8763 if (val)
8764 isl_int_neg(*val, bmap->eq[i][0]);
8765 return isl_bool_true;
8767 return isl_bool_false;
8770 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8771 unsigned pos, isl_int *val)
8773 int i;
8774 isl_int v;
8775 isl_int tmp;
8776 isl_bool fixed;
8778 if (!map)
8779 return isl_bool_error;
8780 if (map->n == 0)
8781 return isl_bool_false;
8782 if (map->n == 1)
8783 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8784 isl_int_init(v);
8785 isl_int_init(tmp);
8786 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8787 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
8788 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8789 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
8790 fixed = isl_bool_false;
8792 if (val)
8793 isl_int_set(*val, v);
8794 isl_int_clear(tmp);
8795 isl_int_clear(v);
8796 return fixed;
8799 static isl_bool isl_basic_set_plain_has_fixed_var(
8800 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
8802 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
8803 pos, val);
8806 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8807 enum isl_dim_type type, unsigned pos, isl_int *val)
8809 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
8810 return isl_bool_error;
8811 return isl_basic_map_plain_has_fixed_var(bmap,
8812 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8815 /* If "bmap" obviously lies on a hyperplane where the given dimension
8816 * has a fixed value, then return that value.
8817 * Otherwise return NaN.
8819 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8820 __isl_keep isl_basic_map *bmap,
8821 enum isl_dim_type type, unsigned pos)
8823 isl_ctx *ctx;
8824 isl_val *v;
8825 isl_bool fixed;
8827 if (!bmap)
8828 return NULL;
8829 ctx = isl_basic_map_get_ctx(bmap);
8830 v = isl_val_alloc(ctx);
8831 if (!v)
8832 return NULL;
8833 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8834 if (fixed < 0)
8835 return isl_val_free(v);
8836 if (fixed) {
8837 isl_int_set_si(v->d, 1);
8838 return v;
8840 isl_val_free(v);
8841 return isl_val_nan(ctx);
8844 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
8845 enum isl_dim_type type, unsigned pos, isl_int *val)
8847 if (pos >= isl_map_dim(map, type))
8848 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8849 "position out of bounds", return isl_bool_error);
8850 return isl_map_plain_has_fixed_var(map,
8851 map_offset(map, type) - 1 + pos, val);
8854 /* If "map" obviously lies on a hyperplane where the given dimension
8855 * has a fixed value, then return that value.
8856 * Otherwise return NaN.
8858 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8859 enum isl_dim_type type, unsigned pos)
8861 isl_ctx *ctx;
8862 isl_val *v;
8863 isl_bool fixed;
8865 if (!map)
8866 return NULL;
8867 ctx = isl_map_get_ctx(map);
8868 v = isl_val_alloc(ctx);
8869 if (!v)
8870 return NULL;
8871 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8872 if (fixed < 0)
8873 return isl_val_free(v);
8874 if (fixed) {
8875 isl_int_set_si(v->d, 1);
8876 return v;
8878 isl_val_free(v);
8879 return isl_val_nan(ctx);
8882 /* If "set" obviously lies on a hyperplane where the given dimension
8883 * has a fixed value, then return that value.
8884 * Otherwise return NaN.
8886 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8887 enum isl_dim_type type, unsigned pos)
8889 return isl_map_plain_get_val_if_fixed(set, type, pos);
8892 isl_bool isl_set_plain_is_fixed(__isl_keep isl_set *set,
8893 enum isl_dim_type type, unsigned pos, isl_int *val)
8895 return isl_map_plain_is_fixed(set, type, pos, val);
8898 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8899 * then return this fixed value in *val.
8901 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8902 unsigned dim, isl_int *val)
8904 return isl_basic_set_plain_has_fixed_var(bset,
8905 isl_basic_set_n_param(bset) + dim, val);
8908 /* Return -1 if the constraint "c1" should be sorted before "c2"
8909 * and 1 if it should be sorted after "c2".
8910 * Return 0 if the two constraints are the same (up to the constant term).
8912 * In particular, if a constraint involves later variables than another
8913 * then it is sorted after this other constraint.
8914 * uset_gist depends on constraints without existentially quantified
8915 * variables sorting first.
8917 * For constraints that have the same latest variable, those
8918 * with the same coefficient for this latest variable (first in absolute value
8919 * and then in actual value) are grouped together.
8920 * This is useful for detecting pairs of constraints that can
8921 * be chained in their printed representation.
8923 * Finally, within a group, constraints are sorted according to
8924 * their coefficients (excluding the constant term).
8926 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8928 isl_int **c1 = (isl_int **) p1;
8929 isl_int **c2 = (isl_int **) p2;
8930 int l1, l2;
8931 unsigned size = *(unsigned *) arg;
8932 int cmp;
8934 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8935 l2 = isl_seq_last_non_zero(*c2 + 1, size);
8937 if (l1 != l2)
8938 return l1 - l2;
8940 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
8941 if (cmp != 0)
8942 return cmp;
8943 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
8944 if (cmp != 0)
8945 return -cmp;
8947 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
8950 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
8951 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
8952 * and 0 if the two constraints are the same (up to the constant term).
8954 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
8955 isl_int *c1, isl_int *c2)
8957 unsigned total;
8959 if (!bmap)
8960 return -2;
8961 total = isl_basic_map_total_dim(bmap);
8962 return sort_constraint_cmp(&c1, &c2, &total);
8965 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
8966 __isl_take isl_basic_map *bmap)
8968 unsigned total;
8970 if (!bmap)
8971 return NULL;
8972 if (bmap->n_ineq == 0)
8973 return bmap;
8974 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8975 return bmap;
8976 total = isl_basic_map_total_dim(bmap);
8977 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
8978 &sort_constraint_cmp, &total) < 0)
8979 return isl_basic_map_free(bmap);
8980 return bmap;
8983 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8984 __isl_take isl_basic_set *bset)
8986 isl_basic_map *bmap = bset_to_bmap(bset);
8987 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
8990 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8992 if (!bmap)
8993 return NULL;
8994 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8995 return bmap;
8996 bmap = isl_basic_map_remove_redundancies(bmap);
8997 bmap = isl_basic_map_sort_constraints(bmap);
8998 if (bmap)
8999 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9000 return bmap;
9002 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9003 __isl_keep isl_basic_map *bmap2)
9005 int i, cmp;
9006 unsigned total;
9008 if (!bmap1 || !bmap2)
9009 return -1;
9011 if (bmap1 == bmap2)
9012 return 0;
9013 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9014 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9015 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9016 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9017 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9018 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9019 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9020 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9021 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9022 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9023 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9024 return 0;
9025 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9026 return 1;
9027 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9028 return -1;
9029 if (bmap1->n_eq != bmap2->n_eq)
9030 return bmap1->n_eq - bmap2->n_eq;
9031 if (bmap1->n_ineq != bmap2->n_ineq)
9032 return bmap1->n_ineq - bmap2->n_ineq;
9033 if (bmap1->n_div != bmap2->n_div)
9034 return bmap1->n_div - bmap2->n_div;
9035 total = isl_basic_map_total_dim(bmap1);
9036 for (i = 0; i < bmap1->n_eq; ++i) {
9037 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9038 if (cmp)
9039 return cmp;
9041 for (i = 0; i < bmap1->n_ineq; ++i) {
9042 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9043 if (cmp)
9044 return cmp;
9046 for (i = 0; i < bmap1->n_div; ++i) {
9047 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9048 if (cmp)
9049 return cmp;
9051 return 0;
9054 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9055 __isl_keep isl_basic_set *bset2)
9057 return isl_basic_map_plain_cmp(bset1, bset2);
9060 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9062 int i, cmp;
9064 if (set1 == set2)
9065 return 0;
9066 if (set1->n != set2->n)
9067 return set1->n - set2->n;
9069 for (i = 0; i < set1->n; ++i) {
9070 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9071 if (cmp)
9072 return cmp;
9075 return 0;
9078 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9079 __isl_keep isl_basic_map *bmap2)
9081 if (!bmap1 || !bmap2)
9082 return isl_bool_error;
9083 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9086 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9087 __isl_keep isl_basic_set *bset2)
9089 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9090 bset_to_bmap(bset2));
9093 static int qsort_bmap_cmp(const void *p1, const void *p2)
9095 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9096 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9098 return isl_basic_map_plain_cmp(bmap1, bmap2);
9101 /* Sort the basic maps of "map" and remove duplicate basic maps.
9103 * While removing basic maps, we make sure that the basic maps remain
9104 * sorted because isl_map_normalize expects the basic maps of the result
9105 * to be sorted.
9107 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9109 int i, j;
9111 map = isl_map_remove_empty_parts(map);
9112 if (!map)
9113 return NULL;
9114 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9115 for (i = map->n - 1; i >= 1; --i) {
9116 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9117 continue;
9118 isl_basic_map_free(map->p[i-1]);
9119 for (j = i; j < map->n; ++j)
9120 map->p[j - 1] = map->p[j];
9121 map->n--;
9124 return map;
9127 /* Remove obvious duplicates among the basic maps of "map".
9129 * Unlike isl_map_normalize, this function does not remove redundant
9130 * constraints and only removes duplicates that have exactly the same
9131 * constraints in the input. It does sort the constraints and
9132 * the basic maps to ease the detection of duplicates.
9134 * If "map" has already been normalized or if the basic maps are
9135 * disjoint, then there can be no duplicates.
9137 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9139 int i;
9140 isl_basic_map *bmap;
9142 if (!map)
9143 return NULL;
9144 if (map->n <= 1)
9145 return map;
9146 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9147 return map;
9148 for (i = 0; i < map->n; ++i) {
9149 bmap = isl_basic_map_copy(map->p[i]);
9150 bmap = isl_basic_map_sort_constraints(bmap);
9151 if (!bmap)
9152 return isl_map_free(map);
9153 isl_basic_map_free(map->p[i]);
9154 map->p[i] = bmap;
9157 map = sort_and_remove_duplicates(map);
9158 return map;
9161 /* We normalize in place, but if anything goes wrong we need
9162 * to return NULL, so we need to make sure we don't change the
9163 * meaning of any possible other copies of map.
9165 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9167 int i;
9168 struct isl_basic_map *bmap;
9170 if (!map)
9171 return NULL;
9172 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9173 return map;
9174 for (i = 0; i < map->n; ++i) {
9175 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9176 if (!bmap)
9177 goto error;
9178 isl_basic_map_free(map->p[i]);
9179 map->p[i] = bmap;
9182 map = sort_and_remove_duplicates(map);
9183 if (map)
9184 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9185 return map;
9186 error:
9187 isl_map_free(map);
9188 return NULL;
9191 struct isl_set *isl_set_normalize(struct isl_set *set)
9193 return set_from_map(isl_map_normalize(set_to_map(set)));
9196 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9197 __isl_keep isl_map *map2)
9199 int i;
9200 isl_bool equal;
9202 if (!map1 || !map2)
9203 return isl_bool_error;
9205 if (map1 == map2)
9206 return isl_bool_true;
9207 if (!isl_space_is_equal(map1->dim, map2->dim))
9208 return isl_bool_false;
9210 map1 = isl_map_copy(map1);
9211 map2 = isl_map_copy(map2);
9212 map1 = isl_map_normalize(map1);
9213 map2 = isl_map_normalize(map2);
9214 if (!map1 || !map2)
9215 goto error;
9216 equal = map1->n == map2->n;
9217 for (i = 0; equal && i < map1->n; ++i) {
9218 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9219 if (equal < 0)
9220 goto error;
9222 isl_map_free(map1);
9223 isl_map_free(map2);
9224 return equal;
9225 error:
9226 isl_map_free(map1);
9227 isl_map_free(map2);
9228 return isl_bool_error;
9231 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9232 __isl_keep isl_set *set2)
9234 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9237 /* Return the basic maps in "map" as a list.
9239 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9240 __isl_keep isl_map *map)
9242 int i;
9243 isl_ctx *ctx;
9244 isl_basic_map_list *list;
9246 if (!map)
9247 return NULL;
9248 ctx = isl_map_get_ctx(map);
9249 list = isl_basic_map_list_alloc(ctx, map->n);
9251 for (i = 0; i < map->n; ++i) {
9252 isl_basic_map *bmap;
9254 bmap = isl_basic_map_copy(map->p[i]);
9255 list = isl_basic_map_list_add(list, bmap);
9258 return list;
9261 /* Return the intersection of the elements in the non-empty list "list".
9262 * All elements are assumed to live in the same space.
9264 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9265 __isl_take isl_basic_map_list *list)
9267 int i, n;
9268 isl_basic_map *bmap;
9270 if (!list)
9271 return NULL;
9272 n = isl_basic_map_list_n_basic_map(list);
9273 if (n < 1)
9274 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9275 "expecting non-empty list", goto error);
9277 bmap = isl_basic_map_list_get_basic_map(list, 0);
9278 for (i = 1; i < n; ++i) {
9279 isl_basic_map *bmap_i;
9281 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9282 bmap = isl_basic_map_intersect(bmap, bmap_i);
9285 isl_basic_map_list_free(list);
9286 return bmap;
9287 error:
9288 isl_basic_map_list_free(list);
9289 return NULL;
9292 /* Return the intersection of the elements in the non-empty list "list".
9293 * All elements are assumed to live in the same space.
9295 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9296 __isl_take isl_basic_set_list *list)
9298 return isl_basic_map_list_intersect(list);
9301 /* Return the union of the elements of "list".
9302 * The list is required to have at least one element.
9304 __isl_give isl_set *isl_basic_set_list_union(
9305 __isl_take isl_basic_set_list *list)
9307 int i, n;
9308 isl_space *space;
9309 isl_basic_set *bset;
9310 isl_set *set;
9312 if (!list)
9313 return NULL;
9314 n = isl_basic_set_list_n_basic_set(list);
9315 if (n < 1)
9316 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9317 "expecting non-empty list", goto error);
9319 bset = isl_basic_set_list_get_basic_set(list, 0);
9320 space = isl_basic_set_get_space(bset);
9321 isl_basic_set_free(bset);
9323 set = isl_set_alloc_space(space, n, 0);
9324 for (i = 0; i < n; ++i) {
9325 bset = isl_basic_set_list_get_basic_set(list, i);
9326 set = isl_set_add_basic_set(set, bset);
9329 isl_basic_set_list_free(list);
9330 return set;
9331 error:
9332 isl_basic_set_list_free(list);
9333 return NULL;
9336 /* Return the union of the elements in the non-empty list "list".
9337 * All elements are assumed to live in the same space.
9339 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9341 int i, n;
9342 isl_set *set;
9344 if (!list)
9345 return NULL;
9346 n = isl_set_list_n_set(list);
9347 if (n < 1)
9348 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9349 "expecting non-empty list", goto error);
9351 set = isl_set_list_get_set(list, 0);
9352 for (i = 1; i < n; ++i) {
9353 isl_set *set_i;
9355 set_i = isl_set_list_get_set(list, i);
9356 set = isl_set_union(set, set_i);
9359 isl_set_list_free(list);
9360 return set;
9361 error:
9362 isl_set_list_free(list);
9363 return NULL;
9366 struct isl_basic_map *isl_basic_map_product(
9367 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9369 isl_space *dim_result = NULL;
9370 struct isl_basic_map *bmap;
9371 unsigned in1, in2, out1, out2, nparam, total, pos;
9372 struct isl_dim_map *dim_map1, *dim_map2;
9374 if (!bmap1 || !bmap2)
9375 goto error;
9377 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9378 bmap2->dim, isl_dim_param), goto error);
9379 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9380 isl_space_copy(bmap2->dim));
9382 in1 = isl_basic_map_n_in(bmap1);
9383 in2 = isl_basic_map_n_in(bmap2);
9384 out1 = isl_basic_map_n_out(bmap1);
9385 out2 = isl_basic_map_n_out(bmap2);
9386 nparam = isl_basic_map_n_param(bmap1);
9388 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9389 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9390 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9391 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9392 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9393 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9394 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9395 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9396 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9397 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9398 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9400 bmap = isl_basic_map_alloc_space(dim_result,
9401 bmap1->n_div + bmap2->n_div,
9402 bmap1->n_eq + bmap2->n_eq,
9403 bmap1->n_ineq + bmap2->n_ineq);
9404 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9405 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9406 bmap = isl_basic_map_simplify(bmap);
9407 return isl_basic_map_finalize(bmap);
9408 error:
9409 isl_basic_map_free(bmap1);
9410 isl_basic_map_free(bmap2);
9411 return NULL;
9414 __isl_give isl_basic_map *isl_basic_map_flat_product(
9415 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9417 isl_basic_map *prod;
9419 prod = isl_basic_map_product(bmap1, bmap2);
9420 prod = isl_basic_map_flatten(prod);
9421 return prod;
9424 __isl_give isl_basic_set *isl_basic_set_flat_product(
9425 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9427 return isl_basic_map_flat_range_product(bset1, bset2);
9430 __isl_give isl_basic_map *isl_basic_map_domain_product(
9431 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9433 isl_space *space_result = NULL;
9434 isl_basic_map *bmap;
9435 unsigned in1, in2, out, nparam, total, pos;
9436 struct isl_dim_map *dim_map1, *dim_map2;
9438 if (!bmap1 || !bmap2)
9439 goto error;
9441 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9442 isl_space_copy(bmap2->dim));
9444 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9445 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9446 out = isl_basic_map_dim(bmap1, isl_dim_out);
9447 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9449 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9450 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9451 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9452 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9453 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9454 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9455 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9456 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9457 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9458 isl_dim_map_div(dim_map1, bmap1, pos += out);
9459 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9461 bmap = isl_basic_map_alloc_space(space_result,
9462 bmap1->n_div + bmap2->n_div,
9463 bmap1->n_eq + bmap2->n_eq,
9464 bmap1->n_ineq + bmap2->n_ineq);
9465 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9466 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9467 bmap = isl_basic_map_simplify(bmap);
9468 return isl_basic_map_finalize(bmap);
9469 error:
9470 isl_basic_map_free(bmap1);
9471 isl_basic_map_free(bmap2);
9472 return NULL;
9475 __isl_give isl_basic_map *isl_basic_map_range_product(
9476 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9478 isl_bool rational;
9479 isl_space *dim_result = NULL;
9480 isl_basic_map *bmap;
9481 unsigned in, out1, out2, nparam, total, pos;
9482 struct isl_dim_map *dim_map1, *dim_map2;
9484 rational = isl_basic_map_is_rational(bmap1);
9485 if (rational >= 0 && rational)
9486 rational = isl_basic_map_is_rational(bmap2);
9487 if (!bmap1 || !bmap2 || rational < 0)
9488 goto error;
9490 if (!isl_space_match(bmap1->dim, isl_dim_param,
9491 bmap2->dim, isl_dim_param))
9492 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9493 "parameters don't match", goto error);
9495 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9496 isl_space_copy(bmap2->dim));
9498 in = isl_basic_map_dim(bmap1, isl_dim_in);
9499 out1 = isl_basic_map_n_out(bmap1);
9500 out2 = isl_basic_map_n_out(bmap2);
9501 nparam = isl_basic_map_n_param(bmap1);
9503 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9504 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9505 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9506 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9507 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9508 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9509 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9510 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9511 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9512 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9513 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9515 bmap = isl_basic_map_alloc_space(dim_result,
9516 bmap1->n_div + bmap2->n_div,
9517 bmap1->n_eq + bmap2->n_eq,
9518 bmap1->n_ineq + bmap2->n_ineq);
9519 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9520 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9521 if (rational)
9522 bmap = isl_basic_map_set_rational(bmap);
9523 bmap = isl_basic_map_simplify(bmap);
9524 return isl_basic_map_finalize(bmap);
9525 error:
9526 isl_basic_map_free(bmap1);
9527 isl_basic_map_free(bmap2);
9528 return NULL;
9531 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9532 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9534 isl_basic_map *prod;
9536 prod = isl_basic_map_range_product(bmap1, bmap2);
9537 prod = isl_basic_map_flatten_range(prod);
9538 return prod;
9541 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9542 * and collect the results.
9543 * The result live in the space obtained by calling "space_product"
9544 * on the spaces of "map1" and "map2".
9545 * If "remove_duplicates" is set then the result may contain duplicates
9546 * (even if the inputs do not) and so we try and remove the obvious
9547 * duplicates.
9549 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9550 __isl_take isl_map *map2,
9551 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9552 __isl_take isl_space *right),
9553 __isl_give isl_basic_map *(*basic_map_product)(
9554 __isl_take isl_basic_map *left,
9555 __isl_take isl_basic_map *right),
9556 int remove_duplicates)
9558 unsigned flags = 0;
9559 struct isl_map *result;
9560 int i, j;
9562 if (!map1 || !map2)
9563 goto error;
9565 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9566 map2->dim, isl_dim_param), goto error);
9568 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9569 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9570 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9572 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9573 isl_space_copy(map2->dim)),
9574 map1->n * map2->n, flags);
9575 if (!result)
9576 goto error;
9577 for (i = 0; i < map1->n; ++i)
9578 for (j = 0; j < map2->n; ++j) {
9579 struct isl_basic_map *part;
9580 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9581 isl_basic_map_copy(map2->p[j]));
9582 if (isl_basic_map_is_empty(part))
9583 isl_basic_map_free(part);
9584 else
9585 result = isl_map_add_basic_map(result, part);
9586 if (!result)
9587 goto error;
9589 if (remove_duplicates)
9590 result = isl_map_remove_obvious_duplicates(result);
9591 isl_map_free(map1);
9592 isl_map_free(map2);
9593 return result;
9594 error:
9595 isl_map_free(map1);
9596 isl_map_free(map2);
9597 return NULL;
9600 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9602 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9603 __isl_take isl_map *map2)
9605 return map_product(map1, map2, &isl_space_product,
9606 &isl_basic_map_product, 0);
9609 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9610 __isl_take isl_map *map2)
9612 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9615 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9617 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9618 __isl_take isl_map *map2)
9620 isl_map *prod;
9622 prod = isl_map_product(map1, map2);
9623 prod = isl_map_flatten(prod);
9624 return prod;
9627 /* Given two set A and B, construct its Cartesian product A x B.
9629 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9631 return isl_map_range_product(set1, set2);
9634 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9635 __isl_take isl_set *set2)
9637 return isl_map_flat_range_product(set1, set2);
9640 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9642 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9643 __isl_take isl_map *map2)
9645 return map_product(map1, map2, &isl_space_domain_product,
9646 &isl_basic_map_domain_product, 1);
9649 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9651 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9652 __isl_take isl_map *map2)
9654 return map_product(map1, map2, &isl_space_range_product,
9655 &isl_basic_map_range_product, 1);
9658 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9659 __isl_take isl_map *map2)
9661 return isl_map_align_params_map_map_and(map1, map2,
9662 &map_domain_product_aligned);
9665 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9666 __isl_take isl_map *map2)
9668 return isl_map_align_params_map_map_and(map1, map2,
9669 &map_range_product_aligned);
9672 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9674 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9676 isl_space *space;
9677 int total1, keep1, total2, keep2;
9679 if (!map)
9680 return NULL;
9681 if (!isl_space_domain_is_wrapping(map->dim) ||
9682 !isl_space_range_is_wrapping(map->dim))
9683 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9684 "not a product", return isl_map_free(map));
9686 space = isl_map_get_space(map);
9687 total1 = isl_space_dim(space, isl_dim_in);
9688 total2 = isl_space_dim(space, isl_dim_out);
9689 space = isl_space_factor_domain(space);
9690 keep1 = isl_space_dim(space, isl_dim_in);
9691 keep2 = isl_space_dim(space, isl_dim_out);
9692 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9693 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9694 map = isl_map_reset_space(map, space);
9696 return map;
9699 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9701 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9703 isl_space *space;
9704 int total1, keep1, total2, keep2;
9706 if (!map)
9707 return NULL;
9708 if (!isl_space_domain_is_wrapping(map->dim) ||
9709 !isl_space_range_is_wrapping(map->dim))
9710 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9711 "not a product", return isl_map_free(map));
9713 space = isl_map_get_space(map);
9714 total1 = isl_space_dim(space, isl_dim_in);
9715 total2 = isl_space_dim(space, isl_dim_out);
9716 space = isl_space_factor_range(space);
9717 keep1 = isl_space_dim(space, isl_dim_in);
9718 keep2 = isl_space_dim(space, isl_dim_out);
9719 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9720 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9721 map = isl_map_reset_space(map, space);
9723 return map;
9726 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9728 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9730 isl_space *space;
9731 int total, keep;
9733 if (!map)
9734 return NULL;
9735 if (!isl_space_domain_is_wrapping(map->dim))
9736 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9737 "domain is not a product", return isl_map_free(map));
9739 space = isl_map_get_space(map);
9740 total = isl_space_dim(space, isl_dim_in);
9741 space = isl_space_domain_factor_domain(space);
9742 keep = isl_space_dim(space, isl_dim_in);
9743 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9744 map = isl_map_reset_space(map, space);
9746 return map;
9749 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9751 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9753 isl_space *space;
9754 int total, keep;
9756 if (!map)
9757 return NULL;
9758 if (!isl_space_domain_is_wrapping(map->dim))
9759 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9760 "domain is not a product", return isl_map_free(map));
9762 space = isl_map_get_space(map);
9763 total = isl_space_dim(space, isl_dim_in);
9764 space = isl_space_domain_factor_range(space);
9765 keep = isl_space_dim(space, isl_dim_in);
9766 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9767 map = isl_map_reset_space(map, space);
9769 return map;
9772 /* Given a map A -> [B -> C], extract the map A -> B.
9774 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9776 isl_space *space;
9777 int total, keep;
9779 if (!map)
9780 return NULL;
9781 if (!isl_space_range_is_wrapping(map->dim))
9782 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9783 "range is not a product", return isl_map_free(map));
9785 space = isl_map_get_space(map);
9786 total = isl_space_dim(space, isl_dim_out);
9787 space = isl_space_range_factor_domain(space);
9788 keep = isl_space_dim(space, isl_dim_out);
9789 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9790 map = isl_map_reset_space(map, space);
9792 return map;
9795 /* Given a map A -> [B -> C], extract the map A -> C.
9797 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9799 isl_space *space;
9800 int total, keep;
9802 if (!map)
9803 return NULL;
9804 if (!isl_space_range_is_wrapping(map->dim))
9805 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9806 "range is not a product", return isl_map_free(map));
9808 space = isl_map_get_space(map);
9809 total = isl_space_dim(space, isl_dim_out);
9810 space = isl_space_range_factor_range(space);
9811 keep = isl_space_dim(space, isl_dim_out);
9812 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9813 map = isl_map_reset_space(map, space);
9815 return map;
9818 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9820 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9821 __isl_take isl_map *map2)
9823 isl_map *prod;
9825 prod = isl_map_domain_product(map1, map2);
9826 prod = isl_map_flatten_domain(prod);
9827 return prod;
9830 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9832 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9833 __isl_take isl_map *map2)
9835 isl_map *prod;
9837 prod = isl_map_range_product(map1, map2);
9838 prod = isl_map_flatten_range(prod);
9839 return prod;
9842 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9844 int i;
9845 uint32_t hash = isl_hash_init();
9846 unsigned total;
9848 if (!bmap)
9849 return 0;
9850 bmap = isl_basic_map_copy(bmap);
9851 bmap = isl_basic_map_normalize(bmap);
9852 if (!bmap)
9853 return 0;
9854 total = isl_basic_map_total_dim(bmap);
9855 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9856 for (i = 0; i < bmap->n_eq; ++i) {
9857 uint32_t c_hash;
9858 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9859 isl_hash_hash(hash, c_hash);
9861 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9862 for (i = 0; i < bmap->n_ineq; ++i) {
9863 uint32_t c_hash;
9864 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9865 isl_hash_hash(hash, c_hash);
9867 isl_hash_byte(hash, bmap->n_div & 0xFF);
9868 for (i = 0; i < bmap->n_div; ++i) {
9869 uint32_t c_hash;
9870 if (isl_int_is_zero(bmap->div[i][0]))
9871 continue;
9872 isl_hash_byte(hash, i & 0xFF);
9873 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9874 isl_hash_hash(hash, c_hash);
9876 isl_basic_map_free(bmap);
9877 return hash;
9880 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9882 return isl_basic_map_get_hash(bset_to_bmap(bset));
9885 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9887 int i;
9888 uint32_t hash;
9890 if (!map)
9891 return 0;
9892 map = isl_map_copy(map);
9893 map = isl_map_normalize(map);
9894 if (!map)
9895 return 0;
9897 hash = isl_hash_init();
9898 for (i = 0; i < map->n; ++i) {
9899 uint32_t bmap_hash;
9900 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9901 isl_hash_hash(hash, bmap_hash);
9904 isl_map_free(map);
9906 return hash;
9909 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9911 return isl_map_get_hash(set_to_map(set));
9914 /* Return the number of basic maps in the (current) representation of "map".
9916 int isl_map_n_basic_map(__isl_keep isl_map *map)
9918 return map ? map->n : 0;
9921 int isl_set_n_basic_set(__isl_keep isl_set *set)
9923 return set ? set->n : 0;
9926 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
9927 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9929 int i;
9931 if (!map)
9932 return isl_stat_error;
9934 for (i = 0; i < map->n; ++i)
9935 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9936 return isl_stat_error;
9938 return isl_stat_ok;
9941 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
9942 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9944 int i;
9946 if (!set)
9947 return isl_stat_error;
9949 for (i = 0; i < set->n; ++i)
9950 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9951 return isl_stat_error;
9953 return isl_stat_ok;
9956 /* Return a list of basic sets, the union of which is equal to "set".
9958 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
9959 __isl_keep isl_set *set)
9961 int i;
9962 isl_basic_set_list *list;
9964 if (!set)
9965 return NULL;
9967 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
9968 for (i = 0; i < set->n; ++i) {
9969 isl_basic_set *bset;
9971 bset = isl_basic_set_copy(set->p[i]);
9972 list = isl_basic_set_list_add(list, bset);
9975 return list;
9978 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9980 isl_space *dim;
9982 if (!bset)
9983 return NULL;
9985 bset = isl_basic_set_cow(bset);
9986 if (!bset)
9987 return NULL;
9989 dim = isl_basic_set_get_space(bset);
9990 dim = isl_space_lift(dim, bset->n_div);
9991 if (!dim)
9992 goto error;
9993 isl_space_free(bset->dim);
9994 bset->dim = dim;
9995 bset->extra -= bset->n_div;
9996 bset->n_div = 0;
9998 bset = isl_basic_set_finalize(bset);
10000 return bset;
10001 error:
10002 isl_basic_set_free(bset);
10003 return NULL;
10006 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10008 int i;
10009 isl_space *dim;
10010 unsigned n_div;
10012 set = isl_set_align_divs(set);
10014 if (!set)
10015 return NULL;
10017 set = isl_set_cow(set);
10018 if (!set)
10019 return NULL;
10021 n_div = set->p[0]->n_div;
10022 dim = isl_set_get_space(set);
10023 dim = isl_space_lift(dim, n_div);
10024 if (!dim)
10025 goto error;
10026 isl_space_free(set->dim);
10027 set->dim = dim;
10029 for (i = 0; i < set->n; ++i) {
10030 set->p[i] = isl_basic_set_lift(set->p[i]);
10031 if (!set->p[i])
10032 goto error;
10035 return set;
10036 error:
10037 isl_set_free(set);
10038 return NULL;
10041 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10043 unsigned dim;
10044 int size = 0;
10046 if (!bset)
10047 return -1;
10049 dim = isl_basic_set_total_dim(bset);
10050 size += bset->n_eq * (1 + dim);
10051 size += bset->n_ineq * (1 + dim);
10052 size += bset->n_div * (2 + dim);
10054 return size;
10057 int isl_set_size(__isl_keep isl_set *set)
10059 int i;
10060 int size = 0;
10062 if (!set)
10063 return -1;
10065 for (i = 0; i < set->n; ++i)
10066 size += isl_basic_set_size(set->p[i]);
10068 return size;
10071 /* Check if there is any lower bound (if lower == 0) and/or upper
10072 * bound (if upper == 0) on the specified dim.
10074 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10075 enum isl_dim_type type, unsigned pos, int lower, int upper)
10077 int i;
10079 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10080 return isl_bool_error;
10082 pos += isl_basic_map_offset(bmap, type);
10084 for (i = 0; i < bmap->n_div; ++i) {
10085 if (isl_int_is_zero(bmap->div[i][0]))
10086 continue;
10087 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10088 return isl_bool_true;
10091 for (i = 0; i < bmap->n_eq; ++i)
10092 if (!isl_int_is_zero(bmap->eq[i][pos]))
10093 return isl_bool_true;
10095 for (i = 0; i < bmap->n_ineq; ++i) {
10096 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10097 if (sgn > 0)
10098 lower = 1;
10099 if (sgn < 0)
10100 upper = 1;
10103 return lower && upper;
10106 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10107 enum isl_dim_type type, unsigned pos)
10109 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10112 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10113 enum isl_dim_type type, unsigned pos)
10115 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10118 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10119 enum isl_dim_type type, unsigned pos)
10121 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10124 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10125 enum isl_dim_type type, unsigned pos)
10127 int i;
10129 if (!map)
10130 return isl_bool_error;
10132 for (i = 0; i < map->n; ++i) {
10133 isl_bool bounded;
10134 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10135 if (bounded < 0 || !bounded)
10136 return bounded;
10139 return isl_bool_true;
10142 /* Return true if the specified dim is involved in both an upper bound
10143 * and a lower bound.
10145 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10146 enum isl_dim_type type, unsigned pos)
10148 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10151 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10153 static isl_bool has_any_bound(__isl_keep isl_map *map,
10154 enum isl_dim_type type, unsigned pos,
10155 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10156 enum isl_dim_type type, unsigned pos))
10158 int i;
10160 if (!map)
10161 return isl_bool_error;
10163 for (i = 0; i < map->n; ++i) {
10164 isl_bool bounded;
10165 bounded = fn(map->p[i], type, pos);
10166 if (bounded < 0 || bounded)
10167 return bounded;
10170 return isl_bool_false;
10173 /* Return 1 if the specified dim is involved in any lower bound.
10175 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10176 enum isl_dim_type type, unsigned pos)
10178 return has_any_bound(set, type, pos,
10179 &isl_basic_map_dim_has_lower_bound);
10182 /* Return 1 if the specified dim is involved in any upper bound.
10184 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10185 enum isl_dim_type type, unsigned pos)
10187 return has_any_bound(set, type, pos,
10188 &isl_basic_map_dim_has_upper_bound);
10191 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10193 static isl_bool has_bound(__isl_keep isl_map *map,
10194 enum isl_dim_type type, unsigned pos,
10195 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10196 enum isl_dim_type type, unsigned pos))
10198 int i;
10200 if (!map)
10201 return isl_bool_error;
10203 for (i = 0; i < map->n; ++i) {
10204 isl_bool bounded;
10205 bounded = fn(map->p[i], type, pos);
10206 if (bounded < 0 || !bounded)
10207 return bounded;
10210 return isl_bool_true;
10213 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10215 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10216 enum isl_dim_type type, unsigned pos)
10218 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10221 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10223 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10224 enum isl_dim_type type, unsigned pos)
10226 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10229 /* For each of the "n" variables starting at "first", determine
10230 * the sign of the variable and put the results in the first "n"
10231 * elements of the array "signs".
10232 * Sign
10233 * 1 means that the variable is non-negative
10234 * -1 means that the variable is non-positive
10235 * 0 means the variable attains both positive and negative values.
10237 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10238 unsigned first, unsigned n, int *signs)
10240 isl_vec *bound = NULL;
10241 struct isl_tab *tab = NULL;
10242 struct isl_tab_undo *snap;
10243 int i;
10245 if (!bset || !signs)
10246 return isl_stat_error;
10248 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10249 tab = isl_tab_from_basic_set(bset, 0);
10250 if (!bound || !tab)
10251 goto error;
10253 isl_seq_clr(bound->el, bound->size);
10254 isl_int_set_si(bound->el[0], -1);
10256 snap = isl_tab_snap(tab);
10257 for (i = 0; i < n; ++i) {
10258 int empty;
10260 isl_int_set_si(bound->el[1 + first + i], -1);
10261 if (isl_tab_add_ineq(tab, bound->el) < 0)
10262 goto error;
10263 empty = tab->empty;
10264 isl_int_set_si(bound->el[1 + first + i], 0);
10265 if (isl_tab_rollback(tab, snap) < 0)
10266 goto error;
10268 if (empty) {
10269 signs[i] = 1;
10270 continue;
10273 isl_int_set_si(bound->el[1 + first + i], 1);
10274 if (isl_tab_add_ineq(tab, bound->el) < 0)
10275 goto error;
10276 empty = tab->empty;
10277 isl_int_set_si(bound->el[1 + first + i], 0);
10278 if (isl_tab_rollback(tab, snap) < 0)
10279 goto error;
10281 signs[i] = empty ? -1 : 0;
10284 isl_tab_free(tab);
10285 isl_vec_free(bound);
10286 return isl_stat_ok;
10287 error:
10288 isl_tab_free(tab);
10289 isl_vec_free(bound);
10290 return isl_stat_error;
10293 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10294 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10296 if (!bset || !signs)
10297 return isl_stat_error;
10298 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10299 return isl_stat_error);
10301 first += pos(bset->dim, type) - 1;
10302 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10305 /* Is it possible for the integer division "div" to depend (possibly
10306 * indirectly) on any output dimensions?
10308 * If the div is undefined, then we conservatively assume that it
10309 * may depend on them.
10310 * Otherwise, we check if it actually depends on them or on any integer
10311 * divisions that may depend on them.
10313 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10315 int i;
10316 unsigned n_out, o_out;
10317 unsigned n_div, o_div;
10319 if (isl_int_is_zero(bmap->div[div][0]))
10320 return isl_bool_true;
10322 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10323 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10325 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10326 return isl_bool_true;
10328 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10329 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10331 for (i = 0; i < n_div; ++i) {
10332 isl_bool may_involve;
10334 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10335 continue;
10336 may_involve = div_may_involve_output(bmap, i);
10337 if (may_involve < 0 || may_involve)
10338 return may_involve;
10341 return isl_bool_false;
10344 /* Return the first integer division of "bmap" in the range
10345 * [first, first + n[ that may depend on any output dimensions and
10346 * that has a non-zero coefficient in "c" (where the first coefficient
10347 * in "c" corresponds to integer division "first").
10349 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10350 isl_int *c, int first, int n)
10352 int k;
10354 if (!bmap)
10355 return -1;
10357 for (k = first; k < first + n; ++k) {
10358 isl_bool may_involve;
10360 if (isl_int_is_zero(c[k]))
10361 continue;
10362 may_involve = div_may_involve_output(bmap, k);
10363 if (may_involve < 0)
10364 return -1;
10365 if (may_involve)
10366 return k;
10369 return first + n;
10372 /* Look for a pair of inequality constraints in "bmap" of the form
10374 * -l + i >= 0 or i >= l
10375 * and
10376 * n + l - i >= 0 or i <= l + n
10378 * with n < "m" and i the output dimension at position "pos".
10379 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10380 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10381 * and earlier output dimensions, as well as integer divisions that do
10382 * not involve any of the output dimensions.
10384 * Return the index of the first inequality constraint or bmap->n_ineq
10385 * if no such pair can be found.
10387 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10388 int pos, isl_int m)
10390 int i, j;
10391 isl_ctx *ctx;
10392 unsigned total;
10393 unsigned n_div, o_div;
10394 unsigned n_out, o_out;
10395 int less;
10397 if (!bmap)
10398 return -1;
10400 ctx = isl_basic_map_get_ctx(bmap);
10401 total = isl_basic_map_total_dim(bmap);
10402 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10403 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10404 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10405 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10406 for (i = 0; i < bmap->n_ineq; ++i) {
10407 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10408 continue;
10409 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10410 n_out - (pos + 1)) != -1)
10411 continue;
10412 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10413 0, n_div) < n_div)
10414 continue;
10415 for (j = i + 1; j < bmap->n_ineq; ++j) {
10416 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10417 ctx->one))
10418 continue;
10419 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10420 bmap->ineq[j] + 1, total))
10421 continue;
10422 break;
10424 if (j >= bmap->n_ineq)
10425 continue;
10426 isl_int_add(bmap->ineq[i][0],
10427 bmap->ineq[i][0], bmap->ineq[j][0]);
10428 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10429 isl_int_sub(bmap->ineq[i][0],
10430 bmap->ineq[i][0], bmap->ineq[j][0]);
10431 if (!less)
10432 continue;
10433 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10434 return i;
10435 else
10436 return j;
10439 return bmap->n_ineq;
10442 /* Return the index of the equality of "bmap" that defines
10443 * the output dimension "pos" in terms of earlier dimensions.
10444 * The equality may also involve integer divisions, as long
10445 * as those integer divisions are defined in terms of
10446 * parameters or input dimensions.
10447 * In this case, *div is set to the number of integer divisions and
10448 * *ineq is set to the number of inequality constraints (provided
10449 * div and ineq are not NULL).
10451 * The equality may also involve a single integer division involving
10452 * the output dimensions (typically only output dimension "pos") as
10453 * long as the coefficient of output dimension "pos" is 1 or -1 and
10454 * there is a pair of constraints i >= l and i <= l + n, with i referring
10455 * to output dimension "pos", l an expression involving only earlier
10456 * dimensions and n smaller than the coefficient of the integer division
10457 * in the equality. In this case, the output dimension can be defined
10458 * in terms of a modulo expression that does not involve the integer division.
10459 * *div is then set to this single integer division and
10460 * *ineq is set to the index of constraint i >= l.
10462 * Return bmap->n_eq if there is no such equality.
10463 * Return -1 on error.
10465 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10466 int pos, int *div, int *ineq)
10468 int j, k, l;
10469 unsigned n_out, o_out;
10470 unsigned n_div, o_div;
10472 if (!bmap)
10473 return -1;
10475 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10476 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10477 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10478 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10480 if (ineq)
10481 *ineq = bmap->n_ineq;
10482 if (div)
10483 *div = n_div;
10484 for (j = 0; j < bmap->n_eq; ++j) {
10485 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10486 continue;
10487 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10488 n_out - (pos + 1)) != -1)
10489 continue;
10490 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10491 0, n_div);
10492 if (k >= n_div)
10493 return j;
10494 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10495 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10496 continue;
10497 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10498 k + 1, n_div - (k+1)) < n_div)
10499 continue;
10500 l = find_modulo_constraint_pair(bmap, pos,
10501 bmap->eq[j][o_div + k]);
10502 if (l < 0)
10503 return -1;
10504 if (l >= bmap->n_ineq)
10505 continue;
10506 if (div)
10507 *div = k;
10508 if (ineq)
10509 *ineq = l;
10510 return j;
10513 return bmap->n_eq;
10516 /* Check if the given basic map is obviously single-valued.
10517 * In particular, for each output dimension, check that there is
10518 * an equality that defines the output dimension in terms of
10519 * earlier dimensions.
10521 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10523 int i;
10524 unsigned n_out;
10526 if (!bmap)
10527 return isl_bool_error;
10529 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10531 for (i = 0; i < n_out; ++i) {
10532 int eq;
10534 eq = isl_basic_map_output_defining_equality(bmap, i,
10535 NULL, NULL);
10536 if (eq < 0)
10537 return isl_bool_error;
10538 if (eq >= bmap->n_eq)
10539 return isl_bool_false;
10542 return isl_bool_true;
10545 /* Check if the given basic map is single-valued.
10546 * We simply compute
10548 * M \circ M^-1
10550 * and check if the result is a subset of the identity mapping.
10552 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10554 isl_space *space;
10555 isl_basic_map *test;
10556 isl_basic_map *id;
10557 isl_bool sv;
10559 sv = isl_basic_map_plain_is_single_valued(bmap);
10560 if (sv < 0 || sv)
10561 return sv;
10563 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10564 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10566 space = isl_basic_map_get_space(bmap);
10567 space = isl_space_map_from_set(isl_space_range(space));
10568 id = isl_basic_map_identity(space);
10570 sv = isl_basic_map_is_subset(test, id);
10572 isl_basic_map_free(test);
10573 isl_basic_map_free(id);
10575 return sv;
10578 /* Check if the given map is obviously single-valued.
10580 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10582 if (!map)
10583 return isl_bool_error;
10584 if (map->n == 0)
10585 return isl_bool_true;
10586 if (map->n >= 2)
10587 return isl_bool_false;
10589 return isl_basic_map_plain_is_single_valued(map->p[0]);
10592 /* Check if the given map is single-valued.
10593 * We simply compute
10595 * M \circ M^-1
10597 * and check if the result is a subset of the identity mapping.
10599 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10601 isl_space *dim;
10602 isl_map *test;
10603 isl_map *id;
10604 isl_bool sv;
10606 sv = isl_map_plain_is_single_valued(map);
10607 if (sv < 0 || sv)
10608 return sv;
10610 test = isl_map_reverse(isl_map_copy(map));
10611 test = isl_map_apply_range(test, isl_map_copy(map));
10613 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10614 id = isl_map_identity(dim);
10616 sv = isl_map_is_subset(test, id);
10618 isl_map_free(test);
10619 isl_map_free(id);
10621 return sv;
10624 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10626 isl_bool in;
10628 map = isl_map_copy(map);
10629 map = isl_map_reverse(map);
10630 in = isl_map_is_single_valued(map);
10631 isl_map_free(map);
10633 return in;
10636 /* Check if the given map is obviously injective.
10638 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10640 isl_bool in;
10642 map = isl_map_copy(map);
10643 map = isl_map_reverse(map);
10644 in = isl_map_plain_is_single_valued(map);
10645 isl_map_free(map);
10647 return in;
10650 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10652 isl_bool sv;
10654 sv = isl_map_is_single_valued(map);
10655 if (sv < 0 || !sv)
10656 return sv;
10658 return isl_map_is_injective(map);
10661 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10663 return isl_map_is_single_valued(set_to_map(set));
10666 /* Does "map" only map elements to themselves?
10668 * If the domain and range spaces are different, then "map"
10669 * is considered not to be an identity relation, even if it is empty.
10670 * Otherwise, construct the maximal identity relation and
10671 * check whether "map" is a subset of this relation.
10673 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10675 isl_space *space;
10676 isl_map *id;
10677 isl_bool equal, is_identity;
10679 space = isl_map_get_space(map);
10680 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10681 isl_space_free(space);
10682 if (equal < 0 || !equal)
10683 return equal;
10685 id = isl_map_identity(isl_map_get_space(map));
10686 is_identity = isl_map_is_subset(map, id);
10687 isl_map_free(id);
10689 return is_identity;
10692 int isl_map_is_translation(__isl_keep isl_map *map)
10694 int ok;
10695 isl_set *delta;
10697 delta = isl_map_deltas(isl_map_copy(map));
10698 ok = isl_set_is_singleton(delta);
10699 isl_set_free(delta);
10701 return ok;
10704 static int unique(isl_int *p, unsigned pos, unsigned len)
10706 if (isl_seq_first_non_zero(p, pos) != -1)
10707 return 0;
10708 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10709 return 0;
10710 return 1;
10713 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10715 int i, j;
10716 unsigned nvar;
10717 unsigned ovar;
10719 if (!bset)
10720 return isl_bool_error;
10722 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10723 return isl_bool_false;
10725 nvar = isl_basic_set_dim(bset, isl_dim_set);
10726 ovar = isl_space_offset(bset->dim, isl_dim_set);
10727 for (j = 0; j < nvar; ++j) {
10728 int lower = 0, upper = 0;
10729 for (i = 0; i < bset->n_eq; ++i) {
10730 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10731 continue;
10732 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10733 return isl_bool_false;
10734 break;
10736 if (i < bset->n_eq)
10737 continue;
10738 for (i = 0; i < bset->n_ineq; ++i) {
10739 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10740 continue;
10741 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10742 return isl_bool_false;
10743 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10744 lower = 1;
10745 else
10746 upper = 1;
10748 if (!lower || !upper)
10749 return isl_bool_false;
10752 return isl_bool_true;
10755 isl_bool isl_set_is_box(__isl_keep isl_set *set)
10757 if (!set)
10758 return isl_bool_error;
10759 if (set->n != 1)
10760 return isl_bool_false;
10762 return isl_basic_set_is_box(set->p[0]);
10765 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10767 if (!bset)
10768 return isl_bool_error;
10770 return isl_space_is_wrapping(bset->dim);
10773 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
10775 if (!set)
10776 return isl_bool_error;
10778 return isl_space_is_wrapping(set->dim);
10781 /* Modify the space of "map" through a call to "change".
10782 * If "can_change" is set (not NULL), then first call it to check
10783 * if the modification is allowed, printing the error message "cannot_change"
10784 * if it is not.
10786 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
10787 isl_bool (*can_change)(__isl_keep isl_map *map),
10788 const char *cannot_change,
10789 __isl_give isl_space *(*change)(__isl_take isl_space *space))
10791 isl_bool ok;
10792 isl_space *space;
10794 if (!map)
10795 return NULL;
10797 ok = can_change ? can_change(map) : isl_bool_true;
10798 if (ok < 0)
10799 return isl_map_free(map);
10800 if (!ok)
10801 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
10802 return isl_map_free(map));
10804 space = change(isl_map_get_space(map));
10805 map = isl_map_reset_space(map, space);
10807 return map;
10810 /* Is the domain of "map" a wrapped relation?
10812 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10814 if (!map)
10815 return isl_bool_error;
10817 return isl_space_domain_is_wrapping(map->dim);
10820 /* Is the range of "map" a wrapped relation?
10822 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
10824 if (!map)
10825 return isl_bool_error;
10827 return isl_space_range_is_wrapping(map->dim);
10830 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10832 bmap = isl_basic_map_cow(bmap);
10833 if (!bmap)
10834 return NULL;
10836 bmap->dim = isl_space_wrap(bmap->dim);
10837 if (!bmap->dim)
10838 goto error;
10840 bmap = isl_basic_map_finalize(bmap);
10842 return bset_from_bmap(bmap);
10843 error:
10844 isl_basic_map_free(bmap);
10845 return NULL;
10848 /* Given a map A -> B, return the set (A -> B).
10850 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10852 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
10855 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10857 bset = isl_basic_set_cow(bset);
10858 if (!bset)
10859 return NULL;
10861 bset->dim = isl_space_unwrap(bset->dim);
10862 if (!bset->dim)
10863 goto error;
10865 bset = isl_basic_set_finalize(bset);
10867 return bset_to_bmap(bset);
10868 error:
10869 isl_basic_set_free(bset);
10870 return NULL;
10873 /* Given a set (A -> B), return the map A -> B.
10874 * Error out if "set" is not of the form (A -> B).
10876 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10878 return isl_map_change_space(set, &isl_set_is_wrapping,
10879 "not a wrapping set", &isl_space_unwrap);
10882 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10883 enum isl_dim_type type)
10885 if (!bmap)
10886 return NULL;
10888 if (!isl_space_is_named_or_nested(bmap->dim, type))
10889 return bmap;
10891 bmap = isl_basic_map_cow(bmap);
10892 if (!bmap)
10893 return NULL;
10895 bmap->dim = isl_space_reset(bmap->dim, type);
10896 if (!bmap->dim)
10897 goto error;
10899 bmap = isl_basic_map_finalize(bmap);
10901 return bmap;
10902 error:
10903 isl_basic_map_free(bmap);
10904 return NULL;
10907 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10908 enum isl_dim_type type)
10910 int i;
10912 if (!map)
10913 return NULL;
10915 if (!isl_space_is_named_or_nested(map->dim, type))
10916 return map;
10918 map = isl_map_cow(map);
10919 if (!map)
10920 return NULL;
10922 for (i = 0; i < map->n; ++i) {
10923 map->p[i] = isl_basic_map_reset(map->p[i], type);
10924 if (!map->p[i])
10925 goto error;
10927 map->dim = isl_space_reset(map->dim, type);
10928 if (!map->dim)
10929 goto error;
10931 return map;
10932 error:
10933 isl_map_free(map);
10934 return NULL;
10937 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10939 if (!bmap)
10940 return NULL;
10942 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10943 return bmap;
10945 bmap = isl_basic_map_cow(bmap);
10946 if (!bmap)
10947 return NULL;
10949 bmap->dim = isl_space_flatten(bmap->dim);
10950 if (!bmap->dim)
10951 goto error;
10953 bmap = isl_basic_map_finalize(bmap);
10955 return bmap;
10956 error:
10957 isl_basic_map_free(bmap);
10958 return NULL;
10961 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10963 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
10966 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10967 __isl_take isl_basic_map *bmap)
10969 if (!bmap)
10970 return NULL;
10972 if (!bmap->dim->nested[0])
10973 return bmap;
10975 bmap = isl_basic_map_cow(bmap);
10976 if (!bmap)
10977 return NULL;
10979 bmap->dim = isl_space_flatten_domain(bmap->dim);
10980 if (!bmap->dim)
10981 goto error;
10983 bmap = isl_basic_map_finalize(bmap);
10985 return bmap;
10986 error:
10987 isl_basic_map_free(bmap);
10988 return NULL;
10991 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10992 __isl_take isl_basic_map *bmap)
10994 if (!bmap)
10995 return NULL;
10997 if (!bmap->dim->nested[1])
10998 return bmap;
11000 bmap = isl_basic_map_cow(bmap);
11001 if (!bmap)
11002 return NULL;
11004 bmap->dim = isl_space_flatten_range(bmap->dim);
11005 if (!bmap->dim)
11006 goto error;
11008 bmap = isl_basic_map_finalize(bmap);
11010 return bmap;
11011 error:
11012 isl_basic_map_free(bmap);
11013 return NULL;
11016 /* Remove any internal structure from the spaces of domain and range of "map".
11018 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11020 if (!map)
11021 return NULL;
11023 if (!map->dim->nested[0] && !map->dim->nested[1])
11024 return map;
11026 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11029 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11031 return set_from_map(isl_map_flatten(set_to_map(set)));
11034 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11036 isl_space *dim, *flat_dim;
11037 isl_map *map;
11039 dim = isl_set_get_space(set);
11040 flat_dim = isl_space_flatten(isl_space_copy(dim));
11041 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11042 map = isl_map_intersect_domain(map, set);
11044 return map;
11047 /* Remove any internal structure from the space of the domain of "map".
11049 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11051 if (!map)
11052 return NULL;
11054 if (!map->dim->nested[0])
11055 return map;
11057 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11060 /* Remove any internal structure from the space of the range of "map".
11062 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11064 if (!map)
11065 return NULL;
11067 if (!map->dim->nested[1])
11068 return map;
11070 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11073 /* Reorder the dimensions of "bmap" according to the given dim_map
11074 * and set the dimension specification to "dim" and
11075 * perform Gaussian elimination on the result.
11077 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11078 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11080 isl_basic_map *res;
11081 unsigned flags;
11083 bmap = isl_basic_map_cow(bmap);
11084 if (!bmap || !dim || !dim_map)
11085 goto error;
11087 flags = bmap->flags;
11088 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11089 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11090 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11091 res = isl_basic_map_alloc_space(dim,
11092 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11093 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11094 if (res)
11095 res->flags = flags;
11096 res = isl_basic_map_gauss(res, NULL);
11097 res = isl_basic_map_finalize(res);
11098 return res;
11099 error:
11100 free(dim_map);
11101 isl_basic_map_free(bmap);
11102 isl_space_free(dim);
11103 return NULL;
11106 /* Reorder the dimensions of "map" according to given reordering.
11108 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11109 __isl_take isl_reordering *r)
11111 int i;
11112 struct isl_dim_map *dim_map;
11114 map = isl_map_cow(map);
11115 dim_map = isl_dim_map_from_reordering(r);
11116 if (!map || !r || !dim_map)
11117 goto error;
11119 for (i = 0; i < map->n; ++i) {
11120 struct isl_dim_map *dim_map_i;
11122 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11124 map->p[i] = isl_basic_map_realign(map->p[i],
11125 isl_space_copy(r->dim), dim_map_i);
11127 if (!map->p[i])
11128 goto error;
11131 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11133 isl_reordering_free(r);
11134 free(dim_map);
11135 return map;
11136 error:
11137 free(dim_map);
11138 isl_map_free(map);
11139 isl_reordering_free(r);
11140 return NULL;
11143 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11144 __isl_take isl_reordering *r)
11146 return set_from_map(isl_map_realign(set_to_map(set), r));
11149 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11150 __isl_take isl_space *model)
11152 isl_ctx *ctx;
11154 if (!map || !model)
11155 goto error;
11157 ctx = isl_space_get_ctx(model);
11158 if (!isl_space_has_named_params(model))
11159 isl_die(ctx, isl_error_invalid,
11160 "model has unnamed parameters", goto error);
11161 if (!isl_space_has_named_params(map->dim))
11162 isl_die(ctx, isl_error_invalid,
11163 "relation has unnamed parameters", goto error);
11164 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11165 isl_reordering *exp;
11167 model = isl_space_drop_dims(model, isl_dim_in,
11168 0, isl_space_dim(model, isl_dim_in));
11169 model = isl_space_drop_dims(model, isl_dim_out,
11170 0, isl_space_dim(model, isl_dim_out));
11171 exp = isl_parameter_alignment_reordering(map->dim, model);
11172 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11173 map = isl_map_realign(map, exp);
11176 isl_space_free(model);
11177 return map;
11178 error:
11179 isl_space_free(model);
11180 isl_map_free(map);
11181 return NULL;
11184 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11185 __isl_take isl_space *model)
11187 return isl_map_align_params(set, model);
11190 /* Align the parameters of "bmap" to those of "model", introducing
11191 * additional parameters if needed.
11193 __isl_give isl_basic_map *isl_basic_map_align_params(
11194 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11196 isl_ctx *ctx;
11198 if (!bmap || !model)
11199 goto error;
11201 ctx = isl_space_get_ctx(model);
11202 if (!isl_space_has_named_params(model))
11203 isl_die(ctx, isl_error_invalid,
11204 "model has unnamed parameters", goto error);
11205 if (!isl_space_has_named_params(bmap->dim))
11206 isl_die(ctx, isl_error_invalid,
11207 "relation has unnamed parameters", goto error);
11208 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11209 isl_reordering *exp;
11210 struct isl_dim_map *dim_map;
11212 model = isl_space_drop_dims(model, isl_dim_in,
11213 0, isl_space_dim(model, isl_dim_in));
11214 model = isl_space_drop_dims(model, isl_dim_out,
11215 0, isl_space_dim(model, isl_dim_out));
11216 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11217 exp = isl_reordering_extend_space(exp,
11218 isl_basic_map_get_space(bmap));
11219 dim_map = isl_dim_map_from_reordering(exp);
11220 bmap = isl_basic_map_realign(bmap,
11221 exp ? isl_space_copy(exp->dim) : NULL,
11222 isl_dim_map_extend(dim_map, bmap));
11223 isl_reordering_free(exp);
11224 free(dim_map);
11227 isl_space_free(model);
11228 return bmap;
11229 error:
11230 isl_space_free(model);
11231 isl_basic_map_free(bmap);
11232 return NULL;
11235 /* Align the parameters of "bset" to those of "model", introducing
11236 * additional parameters if needed.
11238 __isl_give isl_basic_set *isl_basic_set_align_params(
11239 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11241 return isl_basic_map_align_params(bset, model);
11244 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11245 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11246 enum isl_dim_type c2, enum isl_dim_type c3,
11247 enum isl_dim_type c4, enum isl_dim_type c5)
11249 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11250 struct isl_mat *mat;
11251 int i, j, k;
11252 int pos;
11254 if (!bmap)
11255 return NULL;
11256 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11257 isl_basic_map_total_dim(bmap) + 1);
11258 if (!mat)
11259 return NULL;
11260 for (i = 0; i < bmap->n_eq; ++i)
11261 for (j = 0, pos = 0; j < 5; ++j) {
11262 int off = isl_basic_map_offset(bmap, c[j]);
11263 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11264 isl_int_set(mat->row[i][pos],
11265 bmap->eq[i][off + k]);
11266 ++pos;
11270 return mat;
11273 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11274 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11275 enum isl_dim_type c2, enum isl_dim_type c3,
11276 enum isl_dim_type c4, enum isl_dim_type c5)
11278 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11279 struct isl_mat *mat;
11280 int i, j, k;
11281 int pos;
11283 if (!bmap)
11284 return NULL;
11285 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11286 isl_basic_map_total_dim(bmap) + 1);
11287 if (!mat)
11288 return NULL;
11289 for (i = 0; i < bmap->n_ineq; ++i)
11290 for (j = 0, pos = 0; j < 5; ++j) {
11291 int off = isl_basic_map_offset(bmap, c[j]);
11292 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11293 isl_int_set(mat->row[i][pos],
11294 bmap->ineq[i][off + k]);
11295 ++pos;
11299 return mat;
11302 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11303 __isl_take isl_space *dim,
11304 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11305 enum isl_dim_type c2, enum isl_dim_type c3,
11306 enum isl_dim_type c4, enum isl_dim_type c5)
11308 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11309 isl_basic_map *bmap;
11310 unsigned total;
11311 unsigned extra;
11312 int i, j, k, l;
11313 int pos;
11315 if (!dim || !eq || !ineq)
11316 goto error;
11318 if (eq->n_col != ineq->n_col)
11319 isl_die(dim->ctx, isl_error_invalid,
11320 "equalities and inequalities matrices should have "
11321 "same number of columns", goto error);
11323 total = 1 + isl_space_dim(dim, isl_dim_all);
11325 if (eq->n_col < total)
11326 isl_die(dim->ctx, isl_error_invalid,
11327 "number of columns too small", goto error);
11329 extra = eq->n_col - total;
11331 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11332 eq->n_row, ineq->n_row);
11333 if (!bmap)
11334 goto error;
11335 for (i = 0; i < extra; ++i) {
11336 k = isl_basic_map_alloc_div(bmap);
11337 if (k < 0)
11338 goto error;
11339 isl_int_set_si(bmap->div[k][0], 0);
11341 for (i = 0; i < eq->n_row; ++i) {
11342 l = isl_basic_map_alloc_equality(bmap);
11343 if (l < 0)
11344 goto error;
11345 for (j = 0, pos = 0; j < 5; ++j) {
11346 int off = isl_basic_map_offset(bmap, c[j]);
11347 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11348 isl_int_set(bmap->eq[l][off + k],
11349 eq->row[i][pos]);
11350 ++pos;
11354 for (i = 0; i < ineq->n_row; ++i) {
11355 l = isl_basic_map_alloc_inequality(bmap);
11356 if (l < 0)
11357 goto error;
11358 for (j = 0, pos = 0; j < 5; ++j) {
11359 int off = isl_basic_map_offset(bmap, c[j]);
11360 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11361 isl_int_set(bmap->ineq[l][off + k],
11362 ineq->row[i][pos]);
11363 ++pos;
11368 isl_space_free(dim);
11369 isl_mat_free(eq);
11370 isl_mat_free(ineq);
11372 bmap = isl_basic_map_simplify(bmap);
11373 return isl_basic_map_finalize(bmap);
11374 error:
11375 isl_space_free(dim);
11376 isl_mat_free(eq);
11377 isl_mat_free(ineq);
11378 return NULL;
11381 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11382 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11383 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11385 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11386 c1, c2, c3, c4, isl_dim_in);
11389 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11390 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11391 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11393 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11394 c1, c2, c3, c4, isl_dim_in);
11397 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11398 __isl_take isl_space *dim,
11399 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11400 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11402 isl_basic_map *bmap;
11403 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11404 c1, c2, c3, c4, isl_dim_in);
11405 return bset_from_bmap(bmap);
11408 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11410 if (!bmap)
11411 return isl_bool_error;
11413 return isl_space_can_zip(bmap->dim);
11416 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11418 if (!map)
11419 return isl_bool_error;
11421 return isl_space_can_zip(map->dim);
11424 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11425 * (A -> C) -> (B -> D).
11427 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11429 unsigned pos;
11430 unsigned n1;
11431 unsigned n2;
11433 if (!bmap)
11434 return NULL;
11436 if (!isl_basic_map_can_zip(bmap))
11437 isl_die(bmap->ctx, isl_error_invalid,
11438 "basic map cannot be zipped", goto error);
11439 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11440 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11441 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11442 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11443 bmap = isl_basic_map_cow(bmap);
11444 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11445 if (!bmap)
11446 return NULL;
11447 bmap->dim = isl_space_zip(bmap->dim);
11448 if (!bmap->dim)
11449 goto error;
11450 bmap = isl_basic_map_mark_final(bmap);
11451 return bmap;
11452 error:
11453 isl_basic_map_free(bmap);
11454 return NULL;
11457 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11458 * (A -> C) -> (B -> D).
11460 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11462 int i;
11464 if (!map)
11465 return NULL;
11467 if (!isl_map_can_zip(map))
11468 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11469 goto error);
11471 map = isl_map_cow(map);
11472 if (!map)
11473 return NULL;
11475 for (i = 0; i < map->n; ++i) {
11476 map->p[i] = isl_basic_map_zip(map->p[i]);
11477 if (!map->p[i])
11478 goto error;
11481 map->dim = isl_space_zip(map->dim);
11482 if (!map->dim)
11483 goto error;
11485 return map;
11486 error:
11487 isl_map_free(map);
11488 return NULL;
11491 /* Can we apply isl_basic_map_curry to "bmap"?
11492 * That is, does it have a nested relation in its domain?
11494 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11496 if (!bmap)
11497 return isl_bool_error;
11499 return isl_space_can_curry(bmap->dim);
11502 /* Can we apply isl_map_curry to "map"?
11503 * That is, does it have a nested relation in its domain?
11505 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11507 if (!map)
11508 return isl_bool_error;
11510 return isl_space_can_curry(map->dim);
11513 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11514 * A -> (B -> C).
11516 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11519 if (!bmap)
11520 return NULL;
11522 if (!isl_basic_map_can_curry(bmap))
11523 isl_die(bmap->ctx, isl_error_invalid,
11524 "basic map cannot be curried", goto error);
11525 bmap = isl_basic_map_cow(bmap);
11526 if (!bmap)
11527 return NULL;
11528 bmap->dim = isl_space_curry(bmap->dim);
11529 if (!bmap->dim)
11530 goto error;
11531 bmap = isl_basic_map_mark_final(bmap);
11532 return bmap;
11533 error:
11534 isl_basic_map_free(bmap);
11535 return NULL;
11538 /* Given a map (A -> B) -> C, return the corresponding map
11539 * A -> (B -> C).
11541 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11543 return isl_map_change_space(map, &isl_map_can_curry,
11544 "map cannot be curried", &isl_space_curry);
11547 /* Can isl_map_range_curry be applied to "map"?
11548 * That is, does it have a nested relation in its range,
11549 * the domain of which is itself a nested relation?
11551 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11553 if (!map)
11554 return isl_bool_error;
11556 return isl_space_can_range_curry(map->dim);
11559 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11560 * A -> (B -> (C -> D)).
11562 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11564 return isl_map_change_space(map, &isl_map_can_range_curry,
11565 "map range cannot be curried",
11566 &isl_space_range_curry);
11569 /* Can we apply isl_basic_map_uncurry to "bmap"?
11570 * That is, does it have a nested relation in its domain?
11572 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11574 if (!bmap)
11575 return isl_bool_error;
11577 return isl_space_can_uncurry(bmap->dim);
11580 /* Can we apply isl_map_uncurry to "map"?
11581 * That is, does it have a nested relation in its domain?
11583 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11585 if (!map)
11586 return isl_bool_error;
11588 return isl_space_can_uncurry(map->dim);
11591 /* Given a basic map A -> (B -> C), return the corresponding basic map
11592 * (A -> B) -> C.
11594 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11597 if (!bmap)
11598 return NULL;
11600 if (!isl_basic_map_can_uncurry(bmap))
11601 isl_die(bmap->ctx, isl_error_invalid,
11602 "basic map cannot be uncurried",
11603 return isl_basic_map_free(bmap));
11604 bmap = isl_basic_map_cow(bmap);
11605 if (!bmap)
11606 return NULL;
11607 bmap->dim = isl_space_uncurry(bmap->dim);
11608 if (!bmap->dim)
11609 return isl_basic_map_free(bmap);
11610 bmap = isl_basic_map_mark_final(bmap);
11611 return bmap;
11614 /* Given a map A -> (B -> C), return the corresponding map
11615 * (A -> B) -> C.
11617 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11619 return isl_map_change_space(map, &isl_map_can_uncurry,
11620 "map cannot be uncurried", &isl_space_uncurry);
11623 /* Construct a basic map mapping the domain of the affine expression
11624 * to a one-dimensional range prescribed by the affine expression.
11625 * If "rational" is set, then construct a rational basic map.
11627 * A NaN affine expression cannot be converted to a basic map.
11629 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
11630 __isl_take isl_aff *aff, int rational)
11632 int k;
11633 int pos;
11634 isl_bool is_nan;
11635 isl_local_space *ls;
11636 isl_basic_map *bmap = NULL;
11638 if (!aff)
11639 return NULL;
11640 is_nan = isl_aff_is_nan(aff);
11641 if (is_nan < 0)
11642 goto error;
11643 if (is_nan)
11644 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11645 "cannot convert NaN", goto error);
11647 ls = isl_aff_get_local_space(aff);
11648 bmap = isl_basic_map_from_local_space(ls);
11649 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11650 k = isl_basic_map_alloc_equality(bmap);
11651 if (k < 0)
11652 goto error;
11654 pos = isl_basic_map_offset(bmap, isl_dim_out);
11655 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11656 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11657 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11658 aff->v->size - (pos + 1));
11660 isl_aff_free(aff);
11661 if (rational)
11662 bmap = isl_basic_map_set_rational(bmap);
11663 bmap = isl_basic_map_finalize(bmap);
11664 return bmap;
11665 error:
11666 isl_aff_free(aff);
11667 isl_basic_map_free(bmap);
11668 return NULL;
11671 /* Construct a basic map mapping the domain of the affine expression
11672 * to a one-dimensional range prescribed by the affine expression.
11674 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11676 return isl_basic_map_from_aff2(aff, 0);
11679 /* Construct a map mapping the domain of the affine expression
11680 * to a one-dimensional range prescribed by the affine expression.
11682 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11684 isl_basic_map *bmap;
11686 bmap = isl_basic_map_from_aff(aff);
11687 return isl_map_from_basic_map(bmap);
11690 /* Construct a basic map mapping the domain the multi-affine expression
11691 * to its range, with each dimension in the range equated to the
11692 * corresponding affine expression.
11693 * If "rational" is set, then construct a rational basic map.
11695 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
11696 __isl_take isl_multi_aff *maff, int rational)
11698 int i;
11699 isl_space *space;
11700 isl_basic_map *bmap;
11702 if (!maff)
11703 return NULL;
11705 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11706 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11707 "invalid space", goto error);
11709 space = isl_space_domain(isl_multi_aff_get_space(maff));
11710 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11711 if (rational)
11712 bmap = isl_basic_map_set_rational(bmap);
11714 for (i = 0; i < maff->n; ++i) {
11715 isl_aff *aff;
11716 isl_basic_map *bmap_i;
11718 aff = isl_aff_copy(maff->p[i]);
11719 bmap_i = isl_basic_map_from_aff2(aff, rational);
11721 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11724 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11726 isl_multi_aff_free(maff);
11727 return bmap;
11728 error:
11729 isl_multi_aff_free(maff);
11730 return NULL;
11733 /* Construct a basic map mapping the domain the multi-affine expression
11734 * to its range, with each dimension in the range equated to the
11735 * corresponding affine expression.
11737 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11738 __isl_take isl_multi_aff *ma)
11740 return isl_basic_map_from_multi_aff2(ma, 0);
11743 /* Construct a map mapping the domain the multi-affine expression
11744 * to its range, with each dimension in the range equated to the
11745 * corresponding affine expression.
11747 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11749 isl_basic_map *bmap;
11751 bmap = isl_basic_map_from_multi_aff(maff);
11752 return isl_map_from_basic_map(bmap);
11755 /* Construct a basic map mapping a domain in the given space to
11756 * to an n-dimensional range, with n the number of elements in the list,
11757 * where each coordinate in the range is prescribed by the
11758 * corresponding affine expression.
11759 * The domains of all affine expressions in the list are assumed to match
11760 * domain_dim.
11762 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11763 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11765 int i;
11766 isl_space *dim;
11767 isl_basic_map *bmap;
11769 if (!list)
11770 return NULL;
11772 dim = isl_space_from_domain(domain_dim);
11773 bmap = isl_basic_map_universe(dim);
11775 for (i = 0; i < list->n; ++i) {
11776 isl_aff *aff;
11777 isl_basic_map *bmap_i;
11779 aff = isl_aff_copy(list->p[i]);
11780 bmap_i = isl_basic_map_from_aff(aff);
11782 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11785 isl_aff_list_free(list);
11786 return bmap;
11789 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11790 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11792 return isl_map_equate(set, type1, pos1, type2, pos2);
11795 /* Construct a basic map where the given dimensions are equal to each other.
11797 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11798 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11800 isl_basic_map *bmap = NULL;
11801 int i;
11803 if (!space)
11804 return NULL;
11806 if (pos1 >= isl_space_dim(space, type1))
11807 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11808 "index out of bounds", goto error);
11809 if (pos2 >= isl_space_dim(space, type2))
11810 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11811 "index out of bounds", goto error);
11813 if (type1 == type2 && pos1 == pos2)
11814 return isl_basic_map_universe(space);
11816 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11817 i = isl_basic_map_alloc_equality(bmap);
11818 if (i < 0)
11819 goto error;
11820 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11821 pos1 += isl_basic_map_offset(bmap, type1);
11822 pos2 += isl_basic_map_offset(bmap, type2);
11823 isl_int_set_si(bmap->eq[i][pos1], -1);
11824 isl_int_set_si(bmap->eq[i][pos2], 1);
11825 bmap = isl_basic_map_finalize(bmap);
11826 isl_space_free(space);
11827 return bmap;
11828 error:
11829 isl_space_free(space);
11830 isl_basic_map_free(bmap);
11831 return NULL;
11834 /* Add a constraint imposing that the given two dimensions are equal.
11836 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11837 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11839 isl_basic_map *eq;
11841 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11843 bmap = isl_basic_map_intersect(bmap, eq);
11845 return bmap;
11848 /* Add a constraint imposing that the given two dimensions are equal.
11850 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11851 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11853 isl_basic_map *bmap;
11855 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11857 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11859 return map;
11862 /* Add a constraint imposing that the given two dimensions have opposite values.
11864 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11865 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11867 isl_basic_map *bmap = NULL;
11868 int i;
11870 if (!map)
11871 return NULL;
11873 if (pos1 >= isl_map_dim(map, type1))
11874 isl_die(map->ctx, isl_error_invalid,
11875 "index out of bounds", goto error);
11876 if (pos2 >= isl_map_dim(map, type2))
11877 isl_die(map->ctx, isl_error_invalid,
11878 "index out of bounds", goto error);
11880 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11881 i = isl_basic_map_alloc_equality(bmap);
11882 if (i < 0)
11883 goto error;
11884 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11885 pos1 += isl_basic_map_offset(bmap, type1);
11886 pos2 += isl_basic_map_offset(bmap, type2);
11887 isl_int_set_si(bmap->eq[i][pos1], 1);
11888 isl_int_set_si(bmap->eq[i][pos2], 1);
11889 bmap = isl_basic_map_finalize(bmap);
11891 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11893 return map;
11894 error:
11895 isl_basic_map_free(bmap);
11896 isl_map_free(map);
11897 return NULL;
11900 /* Construct a constraint imposing that the value of the first dimension is
11901 * greater than or equal to that of the second.
11903 static __isl_give isl_constraint *constraint_order_ge(
11904 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11905 enum isl_dim_type type2, int pos2)
11907 isl_constraint *c;
11909 if (!space)
11910 return NULL;
11912 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
11914 if (pos1 >= isl_constraint_dim(c, type1))
11915 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11916 "index out of bounds", return isl_constraint_free(c));
11917 if (pos2 >= isl_constraint_dim(c, type2))
11918 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11919 "index out of bounds", return isl_constraint_free(c));
11921 if (type1 == type2 && pos1 == pos2)
11922 return c;
11924 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11925 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11927 return c;
11930 /* Add a constraint imposing that the value of the first dimension is
11931 * greater than or equal to that of the second.
11933 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11934 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11936 isl_constraint *c;
11937 isl_space *space;
11939 if (type1 == type2 && pos1 == pos2)
11940 return bmap;
11941 space = isl_basic_map_get_space(bmap);
11942 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11943 bmap = isl_basic_map_add_constraint(bmap, c);
11945 return bmap;
11948 /* Add a constraint imposing that the value of the first dimension is
11949 * greater than or equal to that of the second.
11951 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11952 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11954 isl_constraint *c;
11955 isl_space *space;
11957 if (type1 == type2 && pos1 == pos2)
11958 return map;
11959 space = isl_map_get_space(map);
11960 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11961 map = isl_map_add_constraint(map, c);
11963 return map;
11966 /* Add a constraint imposing that the value of the first dimension is
11967 * less than or equal to that of the second.
11969 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11970 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11972 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11975 /* Construct a basic map where the value of the first dimension is
11976 * greater than that of the second.
11978 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11979 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11981 isl_basic_map *bmap = NULL;
11982 int i;
11984 if (!space)
11985 return NULL;
11987 if (pos1 >= isl_space_dim(space, type1))
11988 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11989 "index out of bounds", goto error);
11990 if (pos2 >= isl_space_dim(space, type2))
11991 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11992 "index out of bounds", goto error);
11994 if (type1 == type2 && pos1 == pos2)
11995 return isl_basic_map_empty(space);
11997 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11998 i = isl_basic_map_alloc_inequality(bmap);
11999 if (i < 0)
12000 return isl_basic_map_free(bmap);
12001 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12002 pos1 += isl_basic_map_offset(bmap, type1);
12003 pos2 += isl_basic_map_offset(bmap, type2);
12004 isl_int_set_si(bmap->ineq[i][pos1], 1);
12005 isl_int_set_si(bmap->ineq[i][pos2], -1);
12006 isl_int_set_si(bmap->ineq[i][0], -1);
12007 bmap = isl_basic_map_finalize(bmap);
12009 return bmap;
12010 error:
12011 isl_space_free(space);
12012 isl_basic_map_free(bmap);
12013 return NULL;
12016 /* Add a constraint imposing that the value of the first dimension is
12017 * greater than that of the second.
12019 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12020 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12022 isl_basic_map *gt;
12024 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12026 bmap = isl_basic_map_intersect(bmap, gt);
12028 return bmap;
12031 /* Add a constraint imposing that the value of the first dimension is
12032 * greater than that of the second.
12034 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12035 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12037 isl_basic_map *bmap;
12039 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12041 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12043 return map;
12046 /* Add a constraint imposing that the value of the first dimension is
12047 * smaller than that of the second.
12049 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12050 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12052 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12055 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12056 int pos)
12058 isl_aff *div;
12059 isl_local_space *ls;
12061 if (!bmap)
12062 return NULL;
12064 if (!isl_basic_map_divs_known(bmap))
12065 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12066 "some divs are unknown", return NULL);
12068 ls = isl_basic_map_get_local_space(bmap);
12069 div = isl_local_space_get_div(ls, pos);
12070 isl_local_space_free(ls);
12072 return div;
12075 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12076 int pos)
12078 return isl_basic_map_get_div(bset, pos);
12081 /* Plug in "subs" for dimension "type", "pos" of "bset".
12083 * Let i be the dimension to replace and let "subs" be of the form
12085 * f/d
12087 * Any integer division with a non-zero coefficient for i,
12089 * floor((a i + g)/m)
12091 * is replaced by
12093 * floor((a f + d g)/(m d))
12095 * Constraints of the form
12097 * a i + g
12099 * are replaced by
12101 * a f + d g
12103 * We currently require that "subs" is an integral expression.
12104 * Handling rational expressions may require us to add stride constraints
12105 * as we do in isl_basic_set_preimage_multi_aff.
12107 __isl_give isl_basic_set *isl_basic_set_substitute(
12108 __isl_take isl_basic_set *bset,
12109 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12111 int i;
12112 isl_int v;
12113 isl_ctx *ctx;
12115 if (bset && isl_basic_set_plain_is_empty(bset))
12116 return bset;
12118 bset = isl_basic_set_cow(bset);
12119 if (!bset || !subs)
12120 goto error;
12122 ctx = isl_basic_set_get_ctx(bset);
12123 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12124 isl_die(ctx, isl_error_invalid,
12125 "spaces don't match", goto error);
12126 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12127 isl_die(ctx, isl_error_unsupported,
12128 "cannot handle divs yet", goto error);
12129 if (!isl_int_is_one(subs->v->el[0]))
12130 isl_die(ctx, isl_error_invalid,
12131 "can only substitute integer expressions", goto error);
12133 pos += isl_basic_set_offset(bset, type);
12135 isl_int_init(v);
12137 for (i = 0; i < bset->n_eq; ++i) {
12138 if (isl_int_is_zero(bset->eq[i][pos]))
12139 continue;
12140 isl_int_set(v, bset->eq[i][pos]);
12141 isl_int_set_si(bset->eq[i][pos], 0);
12142 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12143 v, subs->v->el + 1, subs->v->size - 1);
12146 for (i = 0; i < bset->n_ineq; ++i) {
12147 if (isl_int_is_zero(bset->ineq[i][pos]))
12148 continue;
12149 isl_int_set(v, bset->ineq[i][pos]);
12150 isl_int_set_si(bset->ineq[i][pos], 0);
12151 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12152 v, subs->v->el + 1, subs->v->size - 1);
12155 for (i = 0; i < bset->n_div; ++i) {
12156 if (isl_int_is_zero(bset->div[i][1 + pos]))
12157 continue;
12158 isl_int_set(v, bset->div[i][1 + pos]);
12159 isl_int_set_si(bset->div[i][1 + pos], 0);
12160 isl_seq_combine(bset->div[i] + 1,
12161 subs->v->el[0], bset->div[i] + 1,
12162 v, subs->v->el + 1, subs->v->size - 1);
12163 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12166 isl_int_clear(v);
12168 bset = isl_basic_set_simplify(bset);
12169 return isl_basic_set_finalize(bset);
12170 error:
12171 isl_basic_set_free(bset);
12172 return NULL;
12175 /* Plug in "subs" for dimension "type", "pos" of "set".
12177 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12178 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12180 int i;
12182 if (set && isl_set_plain_is_empty(set))
12183 return set;
12185 set = isl_set_cow(set);
12186 if (!set || !subs)
12187 goto error;
12189 for (i = set->n - 1; i >= 0; --i) {
12190 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12191 if (remove_if_empty(set, i) < 0)
12192 goto error;
12195 return set;
12196 error:
12197 isl_set_free(set);
12198 return NULL;
12201 /* Check if the range of "ma" is compatible with the domain or range
12202 * (depending on "type") of "bmap".
12204 static isl_stat check_basic_map_compatible_range_multi_aff(
12205 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12206 __isl_keep isl_multi_aff *ma)
12208 isl_bool m;
12209 isl_space *ma_space;
12211 ma_space = isl_multi_aff_get_space(ma);
12213 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12214 if (m < 0)
12215 goto error;
12216 if (!m)
12217 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12218 "parameters don't match", goto error);
12219 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12220 if (m < 0)
12221 goto error;
12222 if (!m)
12223 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12224 "spaces don't match", goto error);
12226 isl_space_free(ma_space);
12227 return isl_stat_ok;
12228 error:
12229 isl_space_free(ma_space);
12230 return isl_stat_error;
12233 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12234 * coefficients before the transformed range of dimensions,
12235 * the "n_after" coefficients after the transformed range of dimensions
12236 * and the coefficients of the other divs in "bmap".
12238 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12239 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12241 int i;
12242 int n_param;
12243 int n_set;
12244 isl_local_space *ls;
12246 if (n_div == 0)
12247 return 0;
12249 ls = isl_aff_get_domain_local_space(ma->p[0]);
12250 if (!ls)
12251 return -1;
12253 n_param = isl_local_space_dim(ls, isl_dim_param);
12254 n_set = isl_local_space_dim(ls, isl_dim_set);
12255 for (i = 0; i < n_div; ++i) {
12256 int o_bmap = 0, o_ls = 0;
12258 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12259 o_bmap += 1 + 1 + n_param;
12260 o_ls += 1 + 1 + n_param;
12261 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12262 o_bmap += n_before;
12263 isl_seq_cpy(bmap->div[i] + o_bmap,
12264 ls->div->row[i] + o_ls, n_set);
12265 o_bmap += n_set;
12266 o_ls += n_set;
12267 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12268 o_bmap += n_after;
12269 isl_seq_cpy(bmap->div[i] + o_bmap,
12270 ls->div->row[i] + o_ls, n_div);
12271 o_bmap += n_div;
12272 o_ls += n_div;
12273 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12274 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12275 goto error;
12278 isl_local_space_free(ls);
12279 return 0;
12280 error:
12281 isl_local_space_free(ls);
12282 return -1;
12285 /* How many stride constraints does "ma" enforce?
12286 * That is, how many of the affine expressions have a denominator
12287 * different from one?
12289 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12291 int i;
12292 int strides = 0;
12294 for (i = 0; i < ma->n; ++i)
12295 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12296 strides++;
12298 return strides;
12301 /* For each affine expression in ma of the form
12303 * x_i = (f_i y + h_i)/m_i
12305 * with m_i different from one, add a constraint to "bmap"
12306 * of the form
12308 * f_i y + h_i = m_i alpha_i
12310 * with alpha_i an additional existentially quantified variable.
12312 * The input variables of "ma" correspond to a subset of the variables
12313 * of "bmap". There are "n_before" variables in "bmap" before this
12314 * subset and "n_after" variables after this subset.
12315 * The integer divisions of the affine expressions in "ma" are assumed
12316 * to have been aligned. There are "n_div_ma" of them and
12317 * they appear first in "bmap", straight after the "n_after" variables.
12319 static __isl_give isl_basic_map *add_ma_strides(
12320 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12321 int n_before, int n_after, int n_div_ma)
12323 int i, k;
12324 int div;
12325 int total;
12326 int n_param;
12327 int n_in;
12329 total = isl_basic_map_total_dim(bmap);
12330 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12331 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12332 for (i = 0; i < ma->n; ++i) {
12333 int o_bmap = 0, o_ma = 1;
12335 if (isl_int_is_one(ma->p[i]->v->el[0]))
12336 continue;
12337 div = isl_basic_map_alloc_div(bmap);
12338 k = isl_basic_map_alloc_equality(bmap);
12339 if (div < 0 || k < 0)
12340 goto error;
12341 isl_int_set_si(bmap->div[div][0], 0);
12342 isl_seq_cpy(bmap->eq[k] + o_bmap,
12343 ma->p[i]->v->el + o_ma, 1 + n_param);
12344 o_bmap += 1 + n_param;
12345 o_ma += 1 + n_param;
12346 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12347 o_bmap += n_before;
12348 isl_seq_cpy(bmap->eq[k] + o_bmap,
12349 ma->p[i]->v->el + o_ma, n_in);
12350 o_bmap += n_in;
12351 o_ma += n_in;
12352 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12353 o_bmap += n_after;
12354 isl_seq_cpy(bmap->eq[k] + o_bmap,
12355 ma->p[i]->v->el + o_ma, n_div_ma);
12356 o_bmap += n_div_ma;
12357 o_ma += n_div_ma;
12358 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12359 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12360 total++;
12363 return bmap;
12364 error:
12365 isl_basic_map_free(bmap);
12366 return NULL;
12369 /* Replace the domain or range space (depending on "type) of "space" by "set".
12371 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12372 enum isl_dim_type type, __isl_take isl_space *set)
12374 if (type == isl_dim_in) {
12375 space = isl_space_range(space);
12376 space = isl_space_map_from_domain_and_range(set, space);
12377 } else {
12378 space = isl_space_domain(space);
12379 space = isl_space_map_from_domain_and_range(space, set);
12382 return space;
12385 /* Compute the preimage of the domain or range (depending on "type")
12386 * of "bmap" under the function represented by "ma".
12387 * In other words, plug in "ma" in the domain or range of "bmap".
12388 * The result is a basic map that lives in the same space as "bmap"
12389 * except that the domain or range has been replaced by
12390 * the domain space of "ma".
12392 * If bmap is represented by
12394 * A(p) + S u + B x + T v + C(divs) >= 0,
12396 * where u and x are input and output dimensions if type == isl_dim_out
12397 * while x and v are input and output dimensions if type == isl_dim_in,
12398 * and ma is represented by
12400 * x = D(p) + F(y) + G(divs')
12402 * then the result is
12404 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12406 * The divs in the input set are similarly adjusted.
12407 * In particular
12409 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12411 * becomes
12413 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12414 * B_i G(divs') + c_i(divs))/n_i)
12416 * If bmap is not a rational map and if F(y) involves any denominators
12418 * x_i = (f_i y + h_i)/m_i
12420 * then additional constraints are added to ensure that we only
12421 * map back integer points. That is we enforce
12423 * f_i y + h_i = m_i alpha_i
12425 * with alpha_i an additional existentially quantified variable.
12427 * We first copy over the divs from "ma".
12428 * Then we add the modified constraints and divs from "bmap".
12429 * Finally, we add the stride constraints, if needed.
12431 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12432 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12433 __isl_take isl_multi_aff *ma)
12435 int i, k;
12436 isl_space *space;
12437 isl_basic_map *res = NULL;
12438 int n_before, n_after, n_div_bmap, n_div_ma;
12439 isl_int f, c1, c2, g;
12440 isl_bool rational;
12441 int strides;
12443 isl_int_init(f);
12444 isl_int_init(c1);
12445 isl_int_init(c2);
12446 isl_int_init(g);
12448 ma = isl_multi_aff_align_divs(ma);
12449 if (!bmap || !ma)
12450 goto error;
12451 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12452 goto error;
12454 if (type == isl_dim_in) {
12455 n_before = 0;
12456 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12457 } else {
12458 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12459 n_after = 0;
12461 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12462 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12464 space = isl_multi_aff_get_domain_space(ma);
12465 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12466 rational = isl_basic_map_is_rational(bmap);
12467 strides = rational ? 0 : multi_aff_strides(ma);
12468 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12469 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12470 if (rational)
12471 res = isl_basic_map_set_rational(res);
12473 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12474 if (isl_basic_map_alloc_div(res) < 0)
12475 goto error;
12477 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12478 goto error;
12480 for (i = 0; i < bmap->n_eq; ++i) {
12481 k = isl_basic_map_alloc_equality(res);
12482 if (k < 0)
12483 goto error;
12484 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12485 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12488 for (i = 0; i < bmap->n_ineq; ++i) {
12489 k = isl_basic_map_alloc_inequality(res);
12490 if (k < 0)
12491 goto error;
12492 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12493 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12496 for (i = 0; i < bmap->n_div; ++i) {
12497 if (isl_int_is_zero(bmap->div[i][0])) {
12498 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12499 continue;
12501 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12502 n_before, n_after, n_div_ma, n_div_bmap,
12503 f, c1, c2, g, 1);
12506 if (strides)
12507 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12509 isl_int_clear(f);
12510 isl_int_clear(c1);
12511 isl_int_clear(c2);
12512 isl_int_clear(g);
12513 isl_basic_map_free(bmap);
12514 isl_multi_aff_free(ma);
12515 res = isl_basic_map_simplify(res);
12516 return isl_basic_map_finalize(res);
12517 error:
12518 isl_int_clear(f);
12519 isl_int_clear(c1);
12520 isl_int_clear(c2);
12521 isl_int_clear(g);
12522 isl_basic_map_free(bmap);
12523 isl_multi_aff_free(ma);
12524 isl_basic_map_free(res);
12525 return NULL;
12528 /* Compute the preimage of "bset" under the function represented by "ma".
12529 * In other words, plug in "ma" in "bset". The result is a basic set
12530 * that lives in the domain space of "ma".
12532 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12533 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12535 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12538 /* Compute the preimage of the domain of "bmap" under the function
12539 * represented by "ma".
12540 * In other words, plug in "ma" in the domain of "bmap".
12541 * The result is a basic map that lives in the same space as "bmap"
12542 * except that the domain has been replaced by the domain space of "ma".
12544 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12545 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12547 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12550 /* Compute the preimage of the range of "bmap" under the function
12551 * represented by "ma".
12552 * In other words, plug in "ma" in the range of "bmap".
12553 * The result is a basic map that lives in the same space as "bmap"
12554 * except that the range has been replaced by the domain space of "ma".
12556 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12557 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12559 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12562 /* Check if the range of "ma" is compatible with the domain or range
12563 * (depending on "type") of "map".
12564 * Return isl_stat_error if anything is wrong.
12566 static isl_stat check_map_compatible_range_multi_aff(
12567 __isl_keep isl_map *map, enum isl_dim_type type,
12568 __isl_keep isl_multi_aff *ma)
12570 isl_bool m;
12571 isl_space *ma_space;
12573 ma_space = isl_multi_aff_get_space(ma);
12574 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12575 isl_space_free(ma_space);
12576 if (m < 0)
12577 return isl_stat_error;
12578 if (!m)
12579 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12580 "spaces don't match", return isl_stat_error);
12581 return isl_stat_ok;
12584 /* Compute the preimage of the domain or range (depending on "type")
12585 * of "map" under the function represented by "ma".
12586 * In other words, plug in "ma" in the domain or range of "map".
12587 * The result is a map that lives in the same space as "map"
12588 * except that the domain or range has been replaced by
12589 * the domain space of "ma".
12591 * The parameters are assumed to have been aligned.
12593 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12594 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12596 int i;
12597 isl_space *space;
12599 map = isl_map_cow(map);
12600 ma = isl_multi_aff_align_divs(ma);
12601 if (!map || !ma)
12602 goto error;
12603 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12604 goto error;
12606 for (i = 0; i < map->n; ++i) {
12607 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12608 isl_multi_aff_copy(ma));
12609 if (!map->p[i])
12610 goto error;
12613 space = isl_multi_aff_get_domain_space(ma);
12614 space = isl_space_set(isl_map_get_space(map), type, space);
12616 isl_space_free(map->dim);
12617 map->dim = space;
12618 if (!map->dim)
12619 goto error;
12621 isl_multi_aff_free(ma);
12622 if (map->n > 1)
12623 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12624 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12625 return map;
12626 error:
12627 isl_multi_aff_free(ma);
12628 isl_map_free(map);
12629 return NULL;
12632 /* Compute the preimage of the domain or range (depending on "type")
12633 * of "map" under the function represented by "ma".
12634 * In other words, plug in "ma" in the domain or range of "map".
12635 * The result is a map that lives in the same space as "map"
12636 * except that the domain or range has been replaced by
12637 * the domain space of "ma".
12639 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12640 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12642 if (!map || !ma)
12643 goto error;
12645 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12646 return map_preimage_multi_aff(map, type, ma);
12648 if (!isl_space_has_named_params(map->dim) ||
12649 !isl_space_has_named_params(ma->space))
12650 isl_die(map->ctx, isl_error_invalid,
12651 "unaligned unnamed parameters", goto error);
12652 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12653 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12655 return map_preimage_multi_aff(map, type, ma);
12656 error:
12657 isl_multi_aff_free(ma);
12658 return isl_map_free(map);
12661 /* Compute the preimage of "set" under the function represented by "ma".
12662 * In other words, plug in "ma" in "set". The result is a set
12663 * that lives in the domain space of "ma".
12665 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12666 __isl_take isl_multi_aff *ma)
12668 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12671 /* Compute the preimage of the domain of "map" under the function
12672 * represented by "ma".
12673 * In other words, plug in "ma" in the domain of "map".
12674 * The result is a map that lives in the same space as "map"
12675 * except that the domain has been replaced by the domain space of "ma".
12677 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12678 __isl_take isl_multi_aff *ma)
12680 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12683 /* Compute the preimage of the range of "map" under the function
12684 * represented by "ma".
12685 * In other words, plug in "ma" in the range of "map".
12686 * The result is a map that lives in the same space as "map"
12687 * except that the range has been replaced by the domain space of "ma".
12689 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12690 __isl_take isl_multi_aff *ma)
12692 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12695 /* Compute the preimage of "map" under the function represented by "pma".
12696 * In other words, plug in "pma" in the domain or range of "map".
12697 * The result is a map that lives in the same space as "map",
12698 * except that the space of type "type" has been replaced by
12699 * the domain space of "pma".
12701 * The parameters of "map" and "pma" are assumed to have been aligned.
12703 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12704 __isl_take isl_map *map, enum isl_dim_type type,
12705 __isl_take isl_pw_multi_aff *pma)
12707 int i;
12708 isl_map *res;
12710 if (!pma)
12711 goto error;
12713 if (pma->n == 0) {
12714 isl_pw_multi_aff_free(pma);
12715 res = isl_map_empty(isl_map_get_space(map));
12716 isl_map_free(map);
12717 return res;
12720 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12721 isl_multi_aff_copy(pma->p[0].maff));
12722 if (type == isl_dim_in)
12723 res = isl_map_intersect_domain(res,
12724 isl_map_copy(pma->p[0].set));
12725 else
12726 res = isl_map_intersect_range(res,
12727 isl_map_copy(pma->p[0].set));
12729 for (i = 1; i < pma->n; ++i) {
12730 isl_map *res_i;
12732 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12733 isl_multi_aff_copy(pma->p[i].maff));
12734 if (type == isl_dim_in)
12735 res_i = isl_map_intersect_domain(res_i,
12736 isl_map_copy(pma->p[i].set));
12737 else
12738 res_i = isl_map_intersect_range(res_i,
12739 isl_map_copy(pma->p[i].set));
12740 res = isl_map_union(res, res_i);
12743 isl_pw_multi_aff_free(pma);
12744 isl_map_free(map);
12745 return res;
12746 error:
12747 isl_pw_multi_aff_free(pma);
12748 isl_map_free(map);
12749 return NULL;
12752 /* Compute the preimage of "map" under the function represented by "pma".
12753 * In other words, plug in "pma" in the domain or range of "map".
12754 * The result is a map that lives in the same space as "map",
12755 * except that the space of type "type" has been replaced by
12756 * the domain space of "pma".
12758 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12759 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12761 if (!map || !pma)
12762 goto error;
12764 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12765 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12767 if (!isl_space_has_named_params(map->dim) ||
12768 !isl_space_has_named_params(pma->dim))
12769 isl_die(map->ctx, isl_error_invalid,
12770 "unaligned unnamed parameters", goto error);
12771 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12772 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12774 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12775 error:
12776 isl_pw_multi_aff_free(pma);
12777 return isl_map_free(map);
12780 /* Compute the preimage of "set" under the function represented by "pma".
12781 * In other words, plug in "pma" in "set". The result is a set
12782 * that lives in the domain space of "pma".
12784 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12785 __isl_take isl_pw_multi_aff *pma)
12787 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12790 /* Compute the preimage of the domain of "map" under the function
12791 * represented by "pma".
12792 * In other words, plug in "pma" in the domain of "map".
12793 * The result is a map that lives in the same space as "map",
12794 * except that domain space has been replaced by the domain space of "pma".
12796 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12797 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12799 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12802 /* Compute the preimage of the range of "map" under the function
12803 * represented by "pma".
12804 * In other words, plug in "pma" in the range of "map".
12805 * The result is a map that lives in the same space as "map",
12806 * except that range space has been replaced by the domain space of "pma".
12808 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12809 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12811 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12814 /* Compute the preimage of "map" under the function represented by "mpa".
12815 * In other words, plug in "mpa" in the domain or range of "map".
12816 * The result is a map that lives in the same space as "map",
12817 * except that the space of type "type" has been replaced by
12818 * the domain space of "mpa".
12820 * If the map does not involve any constraints that refer to the
12821 * dimensions of the substituted space, then the only possible
12822 * effect of "mpa" on the map is to map the space to a different space.
12823 * We create a separate isl_multi_aff to effectuate this change
12824 * in order to avoid spurious splitting of the map along the pieces
12825 * of "mpa".
12827 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12828 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12830 int n;
12831 isl_pw_multi_aff *pma;
12833 if (!map || !mpa)
12834 goto error;
12836 n = isl_map_dim(map, type);
12837 if (!isl_map_involves_dims(map, type, 0, n)) {
12838 isl_space *space;
12839 isl_multi_aff *ma;
12841 space = isl_multi_pw_aff_get_space(mpa);
12842 isl_multi_pw_aff_free(mpa);
12843 ma = isl_multi_aff_zero(space);
12844 return isl_map_preimage_multi_aff(map, type, ma);
12847 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12848 return isl_map_preimage_pw_multi_aff(map, type, pma);
12849 error:
12850 isl_map_free(map);
12851 isl_multi_pw_aff_free(mpa);
12852 return NULL;
12855 /* Compute the preimage of "map" under the function represented by "mpa".
12856 * In other words, plug in "mpa" in the domain "map".
12857 * The result is a map that lives in the same space as "map",
12858 * except that domain space has been replaced by the domain space of "mpa".
12860 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12861 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12863 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12866 /* Compute the preimage of "set" by the function represented by "mpa".
12867 * In other words, plug in "mpa" in "set".
12869 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12870 __isl_take isl_multi_pw_aff *mpa)
12872 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
12875 /* Are the "n" "coefficients" starting at "first" of the integer division
12876 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
12877 * to each other?
12878 * The "coefficient" at position 0 is the denominator.
12879 * The "coefficient" at position 1 is the constant term.
12881 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
12882 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
12883 unsigned first, unsigned n)
12885 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
12886 return isl_bool_error;
12887 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
12888 return isl_bool_error;
12889 return isl_seq_eq(bmap1->div[pos1] + first,
12890 bmap2->div[pos2] + first, n);
12893 /* Are the integer division expressions at position "pos1" in "bmap1" and
12894 * "pos2" in "bmap2" equal to each other, except that the constant terms
12895 * are different?
12897 isl_bool isl_basic_map_equal_div_expr_except_constant(
12898 __isl_keep isl_basic_map *bmap1, int pos1,
12899 __isl_keep isl_basic_map *bmap2, int pos2)
12901 isl_bool equal;
12902 unsigned total;
12904 if (!bmap1 || !bmap2)
12905 return isl_bool_error;
12906 total = isl_basic_map_total_dim(bmap1);
12907 if (total != isl_basic_map_total_dim(bmap2))
12908 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
12909 "incomparable div expressions", return isl_bool_error);
12910 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
12911 0, 1);
12912 if (equal < 0 || !equal)
12913 return equal;
12914 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
12915 1, 1);
12916 if (equal < 0 || equal)
12917 return isl_bool_not(equal);
12918 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
12919 2, total);
12922 /* Replace the numerator of the constant term of the integer division
12923 * expression at position "div" in "bmap" by "value".
12924 * The caller guarantees that this does not change the meaning
12925 * of the input.
12927 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
12928 __isl_take isl_basic_map *bmap, int div, int value)
12930 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
12931 return isl_basic_map_free(bmap);
12933 isl_int_set_si(bmap->div[div][1], value);
12935 return bmap;
12938 /* Is the point "inner" internal to inequality constraint "ineq"
12939 * of "bset"?
12940 * The point is considered to be internal to the inequality constraint,
12941 * if it strictly lies on the positive side of the inequality constraint,
12942 * or if it lies on the constraint and the constraint is lexico-positive.
12944 static isl_bool is_internal(__isl_keep isl_vec *inner,
12945 __isl_keep isl_basic_set *bset, int ineq)
12947 isl_ctx *ctx;
12948 int pos;
12949 unsigned total;
12951 if (!inner || !bset)
12952 return isl_bool_error;
12954 ctx = isl_basic_set_get_ctx(bset);
12955 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
12956 &ctx->normalize_gcd);
12957 if (!isl_int_is_zero(ctx->normalize_gcd))
12958 return isl_int_is_nonneg(ctx->normalize_gcd);
12960 total = isl_basic_set_dim(bset, isl_dim_all);
12961 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
12962 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
12965 /* Tighten the inequality constraints of "bset" that are outward with respect
12966 * to the point "vec".
12967 * That is, tighten the constraints that are not satisfied by "vec".
12969 * "vec" is a point internal to some superset S of "bset" that is used
12970 * to make the subsets of S disjoint, by tightening one half of the constraints
12971 * that separate two subsets. In particular, the constraints of S
12972 * are all satisfied by "vec" and should not be tightened.
12973 * Of the internal constraints, those that have "vec" on the outside
12974 * are tightened. The shared facet is included in the adjacent subset
12975 * with the opposite constraint.
12976 * For constraints that saturate "vec", this criterion cannot be used
12977 * to determine which of the two sides should be tightened.
12978 * Instead, the sign of the first non-zero coefficient is used
12979 * to make this choice. Note that this second criterion is never used
12980 * on the constraints of S since "vec" is interior to "S".
12982 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
12983 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
12985 int j;
12987 bset = isl_basic_set_cow(bset);
12988 if (!bset)
12989 return NULL;
12990 for (j = 0; j < bset->n_ineq; ++j) {
12991 isl_bool internal;
12993 internal = is_internal(vec, bset, j);
12994 if (internal < 0)
12995 return isl_basic_set_free(bset);
12996 if (internal)
12997 continue;
12998 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13001 return bset;