extract out shared isl_set_has_aligned_params
[isl.git] / isl_map.c
blob3c398f722bd0eaa92acb2e805e06d59077590c93
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 /* Do "bmap1" and "bmap2" have the same parameters?
214 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
215 __isl_keep isl_basic_map *bmap2)
217 isl_space *space1, *space2;
219 space1 = isl_basic_map_peek_space(bmap1);
220 space2 = isl_basic_map_peek_space(bmap2);
221 return isl_space_match(space1, isl_dim_param, space2, isl_dim_param);
224 /* Do "map1" and "map2" have the same parameters?
226 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
227 __isl_keep isl_map *map2)
229 isl_space *space1, *space2;
231 space1 = isl_map_peek_space(map1);
232 space2 = isl_map_peek_space(map2);
233 return isl_space_match(space1, isl_dim_param, space2, isl_dim_param);
236 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
237 __isl_keep isl_set *set)
239 isl_bool m;
240 if (!map || !set)
241 return isl_bool_error;
242 m = isl_map_has_equal_params(map, set_to_map(set));
243 if (m < 0 || !m)
244 return m;
245 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
246 set->dim, isl_dim_set);
249 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
250 __isl_keep isl_basic_set *bset)
252 isl_bool m;
253 if (!bmap || !bset)
254 return isl_bool_error;
255 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
256 if (m < 0 || !m)
257 return m;
258 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
259 bset->dim, isl_dim_set);
262 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
263 __isl_keep isl_set *set)
265 isl_bool m;
266 if (!map || !set)
267 return isl_bool_error;
268 m = isl_map_has_equal_params(map, set_to_map(set));
269 if (m < 0 || !m)
270 return m;
271 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
272 set->dim, isl_dim_set);
275 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
276 __isl_keep isl_basic_set *bset)
278 isl_bool m;
279 if (!bmap || !bset)
280 return isl_bool_error;
281 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
282 if (m < 0 || !m)
283 return m;
284 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
285 bset->dim, isl_dim_set);
288 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
290 return bmap ? bmap->ctx : NULL;
293 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
295 return bset ? bset->ctx : NULL;
298 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
300 return map ? map->ctx : NULL;
303 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
305 return set ? set->ctx : NULL;
308 /* Return the space of "bmap".
310 __isl_keep isl_space *isl_basic_map_peek_space(
311 __isl_keep const isl_basic_map *bmap)
313 return bmap ? bmap->dim : NULL;
316 /* Return the space of "bset".
318 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
320 return isl_basic_map_peek_space(bset_to_bmap(bset));
323 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
325 return isl_space_copy(isl_basic_map_peek_space(bmap));
328 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
330 return isl_basic_map_get_space(bset_to_bmap(bset));
333 /* Extract the divs in "bmap" as a matrix.
335 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
337 int i;
338 isl_ctx *ctx;
339 isl_mat *div;
340 unsigned total;
341 unsigned cols;
343 if (!bmap)
344 return NULL;
346 ctx = isl_basic_map_get_ctx(bmap);
347 total = isl_space_dim(bmap->dim, isl_dim_all);
348 cols = 1 + 1 + total + bmap->n_div;
349 div = isl_mat_alloc(ctx, bmap->n_div, cols);
350 if (!div)
351 return NULL;
353 for (i = 0; i < bmap->n_div; ++i)
354 isl_seq_cpy(div->row[i], bmap->div[i], cols);
356 return div;
359 /* Extract the divs in "bset" as a matrix.
361 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
363 return isl_basic_map_get_divs(bset);
366 __isl_give isl_local_space *isl_basic_map_get_local_space(
367 __isl_keep isl_basic_map *bmap)
369 isl_mat *div;
371 if (!bmap)
372 return NULL;
374 div = isl_basic_map_get_divs(bmap);
375 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
378 __isl_give isl_local_space *isl_basic_set_get_local_space(
379 __isl_keep isl_basic_set *bset)
381 return isl_basic_map_get_local_space(bset);
384 /* For each known div d = floor(f/m), add the constraints
386 * f - m d >= 0
387 * -(f-(m-1)) + m d >= 0
389 * Do not finalize the result.
391 static __isl_give isl_basic_map *add_known_div_constraints(
392 __isl_take isl_basic_map *bmap)
394 int i;
395 unsigned n_div;
397 if (!bmap)
398 return NULL;
399 n_div = isl_basic_map_dim(bmap, isl_dim_div);
400 if (n_div == 0)
401 return bmap;
402 bmap = isl_basic_map_cow(bmap);
403 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
404 if (!bmap)
405 return NULL;
406 for (i = 0; i < n_div; ++i) {
407 if (isl_int_is_zero(bmap->div[i][0]))
408 continue;
409 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
410 return isl_basic_map_free(bmap);
413 return bmap;
416 __isl_give isl_basic_map *isl_basic_map_from_local_space(
417 __isl_take isl_local_space *ls)
419 int i;
420 int n_div;
421 isl_basic_map *bmap;
423 if (!ls)
424 return NULL;
426 n_div = isl_local_space_dim(ls, isl_dim_div);
427 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
428 n_div, 0, 2 * n_div);
430 for (i = 0; i < n_div; ++i)
431 if (isl_basic_map_alloc_div(bmap) < 0)
432 goto error;
434 for (i = 0; i < n_div; ++i)
435 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
436 bmap = add_known_div_constraints(bmap);
438 isl_local_space_free(ls);
439 return bmap;
440 error:
441 isl_local_space_free(ls);
442 isl_basic_map_free(bmap);
443 return NULL;
446 __isl_give isl_basic_set *isl_basic_set_from_local_space(
447 __isl_take isl_local_space *ls)
449 return isl_basic_map_from_local_space(ls);
452 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
454 return isl_space_copy(isl_map_peek_space(map));
457 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
459 if (!set)
460 return NULL;
461 return isl_space_copy(set->dim);
464 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
465 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
467 bmap = isl_basic_map_cow(bmap);
468 if (!bmap)
469 return NULL;
470 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
471 if (!bmap->dim)
472 goto error;
473 bmap = isl_basic_map_finalize(bmap);
474 return bmap;
475 error:
476 isl_basic_map_free(bmap);
477 return NULL;
480 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
481 __isl_take isl_basic_set *bset, const char *s)
483 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
486 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
487 enum isl_dim_type type)
489 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
492 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
493 enum isl_dim_type type, const char *s)
495 int i;
497 map = isl_map_cow(map);
498 if (!map)
499 return NULL;
501 map->dim = isl_space_set_tuple_name(map->dim, type, s);
502 if (!map->dim)
503 goto error;
505 for (i = 0; i < map->n; ++i) {
506 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
507 if (!map->p[i])
508 goto error;
511 return map;
512 error:
513 isl_map_free(map);
514 return NULL;
517 /* Replace the identifier of the tuple of type "type" by "id".
519 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
520 __isl_take isl_basic_map *bmap,
521 enum isl_dim_type type, __isl_take isl_id *id)
523 bmap = isl_basic_map_cow(bmap);
524 if (!bmap)
525 goto error;
526 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
527 if (!bmap->dim)
528 return isl_basic_map_free(bmap);
529 bmap = isl_basic_map_finalize(bmap);
530 return bmap;
531 error:
532 isl_id_free(id);
533 return NULL;
536 /* Replace the identifier of the tuple by "id".
538 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
539 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
541 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
544 /* Does the input or output tuple have a name?
546 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
548 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
551 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
552 enum isl_dim_type type)
554 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
557 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
558 const char *s)
560 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
561 isl_dim_set, s));
564 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
565 enum isl_dim_type type, __isl_take isl_id *id)
567 map = isl_map_cow(map);
568 if (!map)
569 goto error;
571 map->dim = isl_space_set_tuple_id(map->dim, type, id);
573 return isl_map_reset_space(map, isl_space_copy(map->dim));
574 error:
575 isl_id_free(id);
576 return NULL;
579 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
580 __isl_take isl_id *id)
582 return isl_map_set_tuple_id(set, isl_dim_set, id);
585 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
586 enum isl_dim_type type)
588 map = isl_map_cow(map);
589 if (!map)
590 return NULL;
592 map->dim = isl_space_reset_tuple_id(map->dim, type);
594 return isl_map_reset_space(map, isl_space_copy(map->dim));
597 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
599 return isl_map_reset_tuple_id(set, isl_dim_set);
602 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
604 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
607 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
608 enum isl_dim_type type)
610 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
613 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
615 return isl_map_has_tuple_id(set, isl_dim_set);
618 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
620 return isl_map_get_tuple_id(set, isl_dim_set);
623 /* Does the set tuple have a name?
625 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
627 if (!set)
628 return isl_bool_error;
629 return isl_space_has_tuple_name(set->dim, isl_dim_set);
633 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
635 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
638 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
640 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
643 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
644 enum isl_dim_type type, unsigned pos)
646 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
649 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
650 enum isl_dim_type type, unsigned pos)
652 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
655 /* Does the given dimension have a name?
657 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
658 enum isl_dim_type type, unsigned pos)
660 if (!map)
661 return isl_bool_error;
662 return isl_space_has_dim_name(map->dim, type, pos);
665 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
666 enum isl_dim_type type, unsigned pos)
668 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
671 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
672 enum isl_dim_type type, unsigned pos)
674 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
677 /* Does the given dimension have a name?
679 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
680 enum isl_dim_type type, unsigned pos)
682 if (!set)
683 return isl_bool_error;
684 return isl_space_has_dim_name(set->dim, type, pos);
687 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
688 __isl_take isl_basic_map *bmap,
689 enum isl_dim_type type, unsigned pos, const char *s)
691 bmap = isl_basic_map_cow(bmap);
692 if (!bmap)
693 return NULL;
694 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
695 if (!bmap->dim)
696 goto error;
697 return isl_basic_map_finalize(bmap);
698 error:
699 isl_basic_map_free(bmap);
700 return NULL;
703 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
704 enum isl_dim_type type, unsigned pos, const char *s)
706 int i;
708 map = isl_map_cow(map);
709 if (!map)
710 return NULL;
712 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
713 if (!map->dim)
714 goto error;
716 for (i = 0; i < map->n; ++i) {
717 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
718 if (!map->p[i])
719 goto error;
722 return map;
723 error:
724 isl_map_free(map);
725 return NULL;
728 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
729 __isl_take isl_basic_set *bset,
730 enum isl_dim_type type, unsigned pos, const char *s)
732 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
733 type, pos, s));
736 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
737 enum isl_dim_type type, unsigned pos, const char *s)
739 return set_from_map(isl_map_set_dim_name(set_to_map(set),
740 type, pos, s));
743 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
744 enum isl_dim_type type, unsigned pos)
746 if (!bmap)
747 return isl_bool_error;
748 return isl_space_has_dim_id(bmap->dim, type, pos);
751 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
752 enum isl_dim_type type, unsigned pos)
754 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
757 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
758 enum isl_dim_type type, unsigned pos)
760 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
763 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
764 enum isl_dim_type type, unsigned pos)
766 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
769 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
770 enum isl_dim_type type, unsigned pos)
772 return isl_map_has_dim_id(set, type, pos);
775 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
776 enum isl_dim_type type, unsigned pos)
778 return isl_map_get_dim_id(set, type, pos);
781 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
782 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
784 map = isl_map_cow(map);
785 if (!map)
786 goto error;
788 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
790 return isl_map_reset_space(map, isl_space_copy(map->dim));
791 error:
792 isl_id_free(id);
793 return NULL;
796 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
797 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
799 return isl_map_set_dim_id(set, type, pos, id);
802 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
803 __isl_keep isl_id *id)
805 if (!map)
806 return -1;
807 return isl_space_find_dim_by_id(map->dim, type, id);
810 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
811 __isl_keep isl_id *id)
813 return isl_map_find_dim_by_id(set, type, id);
816 /* Return the position of the dimension of the given type and name
817 * in "bmap".
818 * Return -1 if no such dimension can be found.
820 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
821 enum isl_dim_type type, const char *name)
823 if (!bmap)
824 return -1;
825 return isl_space_find_dim_by_name(bmap->dim, type, name);
828 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
829 const char *name)
831 if (!map)
832 return -1;
833 return isl_space_find_dim_by_name(map->dim, type, name);
836 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
837 const char *name)
839 return isl_map_find_dim_by_name(set, type, name);
842 /* Reset the user pointer on all identifiers of parameters and tuples
843 * of the space of "map".
845 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
847 isl_space *space;
849 space = isl_map_get_space(map);
850 space = isl_space_reset_user(space);
851 map = isl_map_reset_space(map, space);
853 return map;
856 /* Reset the user pointer on all identifiers of parameters and tuples
857 * of the space of "set".
859 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
861 return isl_map_reset_user(set);
864 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
866 if (!bmap)
867 return isl_bool_error;
868 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
871 /* Has "map" been marked as a rational map?
872 * In particular, have all basic maps in "map" been marked this way?
873 * An empty map is not considered to be rational.
874 * Maps where only some of the basic maps are marked rational
875 * are not allowed.
877 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
879 int i;
880 isl_bool rational;
882 if (!map)
883 return isl_bool_error;
884 if (map->n == 0)
885 return isl_bool_false;
886 rational = isl_basic_map_is_rational(map->p[0]);
887 if (rational < 0)
888 return rational;
889 for (i = 1; i < map->n; ++i) {
890 isl_bool rational_i;
892 rational_i = isl_basic_map_is_rational(map->p[i]);
893 if (rational_i < 0)
894 return rational_i;
895 if (rational != rational_i)
896 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
897 "mixed rational and integer basic maps "
898 "not supported", return isl_bool_error);
901 return rational;
904 /* Has "set" been marked as a rational set?
905 * In particular, have all basic set in "set" been marked this way?
906 * An empty set is not considered to be rational.
907 * Sets where only some of the basic sets are marked rational
908 * are not allowed.
910 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
912 return isl_map_is_rational(set);
915 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
917 return isl_basic_map_is_rational(bset);
920 /* Does "bmap" contain any rational points?
922 * If "bmap" has an equality for each dimension, equating the dimension
923 * to an integer constant, then it has no rational points, even if it
924 * is marked as rational.
926 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
928 isl_bool has_rational = isl_bool_true;
929 unsigned total;
931 if (!bmap)
932 return isl_bool_error;
933 if (isl_basic_map_plain_is_empty(bmap))
934 return isl_bool_false;
935 if (!isl_basic_map_is_rational(bmap))
936 return isl_bool_false;
937 bmap = isl_basic_map_copy(bmap);
938 bmap = isl_basic_map_implicit_equalities(bmap);
939 if (!bmap)
940 return isl_bool_error;
941 total = isl_basic_map_total_dim(bmap);
942 if (bmap->n_eq == total) {
943 int i, j;
944 for (i = 0; i < bmap->n_eq; ++i) {
945 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
946 if (j < 0)
947 break;
948 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
949 !isl_int_is_negone(bmap->eq[i][1 + j]))
950 break;
951 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
952 total - j - 1);
953 if (j >= 0)
954 break;
956 if (i == bmap->n_eq)
957 has_rational = isl_bool_false;
959 isl_basic_map_free(bmap);
961 return has_rational;
964 /* Does "map" contain any rational points?
966 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
968 int i;
969 isl_bool has_rational;
971 if (!map)
972 return isl_bool_error;
973 for (i = 0; i < map->n; ++i) {
974 has_rational = isl_basic_map_has_rational(map->p[i]);
975 if (has_rational < 0 || has_rational)
976 return has_rational;
978 return isl_bool_false;
981 /* Does "set" contain any rational points?
983 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
985 return isl_map_has_rational(set);
988 /* Is this basic set a parameter domain?
990 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
992 if (!bset)
993 return isl_bool_error;
994 return isl_space_is_params(bset->dim);
997 /* Is this set a parameter domain?
999 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1001 if (!set)
1002 return isl_bool_error;
1003 return isl_space_is_params(set->dim);
1006 /* Is this map actually a parameter domain?
1007 * Users should never call this function. Outside of isl,
1008 * a map can never be a parameter domain.
1010 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1012 if (!map)
1013 return isl_bool_error;
1014 return isl_space_is_params(map->dim);
1017 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
1018 struct isl_basic_map *bmap, unsigned extra,
1019 unsigned n_eq, unsigned n_ineq)
1021 int i;
1022 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1024 bmap->ctx = ctx;
1025 isl_ctx_ref(ctx);
1027 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1028 if (isl_blk_is_error(bmap->block))
1029 goto error;
1031 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1032 if ((n_ineq + n_eq) && !bmap->ineq)
1033 goto error;
1035 if (extra == 0) {
1036 bmap->block2 = isl_blk_empty();
1037 bmap->div = NULL;
1038 } else {
1039 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1040 if (isl_blk_is_error(bmap->block2))
1041 goto error;
1043 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1044 if (!bmap->div)
1045 goto error;
1048 for (i = 0; i < n_ineq + n_eq; ++i)
1049 bmap->ineq[i] = bmap->block.data + i * row_size;
1051 for (i = 0; i < extra; ++i)
1052 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1054 bmap->ref = 1;
1055 bmap->flags = 0;
1056 bmap->c_size = n_eq + n_ineq;
1057 bmap->eq = bmap->ineq + n_ineq;
1058 bmap->extra = extra;
1059 bmap->n_eq = 0;
1060 bmap->n_ineq = 0;
1061 bmap->n_div = 0;
1062 bmap->sample = NULL;
1064 return bmap;
1065 error:
1066 isl_basic_map_free(bmap);
1067 return NULL;
1070 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1071 unsigned nparam, unsigned dim, unsigned extra,
1072 unsigned n_eq, unsigned n_ineq)
1074 struct isl_basic_map *bmap;
1075 isl_space *space;
1077 space = isl_space_set_alloc(ctx, nparam, dim);
1078 if (!space)
1079 return NULL;
1081 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1082 return bset_from_bmap(bmap);
1085 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1086 unsigned extra, unsigned n_eq, unsigned n_ineq)
1088 struct isl_basic_map *bmap;
1089 if (!dim)
1090 return NULL;
1091 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1092 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1093 return bset_from_bmap(bmap);
1094 error:
1095 isl_space_free(dim);
1096 return NULL;
1099 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1100 unsigned extra, unsigned n_eq, unsigned n_ineq)
1102 struct isl_basic_map *bmap;
1104 if (!dim)
1105 return NULL;
1106 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1107 if (!bmap)
1108 goto error;
1109 bmap->dim = dim;
1111 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1112 error:
1113 isl_space_free(dim);
1114 return NULL;
1117 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1118 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1119 unsigned n_eq, unsigned n_ineq)
1121 struct isl_basic_map *bmap;
1122 isl_space *dim;
1124 dim = isl_space_alloc(ctx, nparam, in, out);
1125 if (!dim)
1126 return NULL;
1128 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1129 return bmap;
1132 static void dup_constraints(
1133 struct isl_basic_map *dst, struct isl_basic_map *src)
1135 int i;
1136 unsigned total = isl_basic_map_total_dim(src);
1138 for (i = 0; i < src->n_eq; ++i) {
1139 int j = isl_basic_map_alloc_equality(dst);
1140 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1143 for (i = 0; i < src->n_ineq; ++i) {
1144 int j = isl_basic_map_alloc_inequality(dst);
1145 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1148 for (i = 0; i < src->n_div; ++i) {
1149 int j = isl_basic_map_alloc_div(dst);
1150 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1152 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1155 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1157 struct isl_basic_map *dup;
1159 if (!bmap)
1160 return NULL;
1161 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1162 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1163 if (!dup)
1164 return NULL;
1165 dup_constraints(dup, bmap);
1166 dup->flags = bmap->flags;
1167 dup->sample = isl_vec_copy(bmap->sample);
1168 return dup;
1171 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1173 struct isl_basic_map *dup;
1175 dup = isl_basic_map_dup(bset_to_bmap(bset));
1176 return bset_from_bmap(dup);
1179 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1181 if (!bset)
1182 return NULL;
1184 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1185 bset->ref++;
1186 return bset;
1188 return isl_basic_set_dup(bset);
1191 struct isl_set *isl_set_copy(struct isl_set *set)
1193 if (!set)
1194 return NULL;
1196 set->ref++;
1197 return set;
1200 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1202 if (!bmap)
1203 return NULL;
1205 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1206 bmap->ref++;
1207 return bmap;
1209 bmap = isl_basic_map_dup(bmap);
1210 if (bmap)
1211 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1212 return bmap;
1215 struct isl_map *isl_map_copy(struct isl_map *map)
1217 if (!map)
1218 return NULL;
1220 map->ref++;
1221 return map;
1224 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1226 if (!bmap)
1227 return NULL;
1229 if (--bmap->ref > 0)
1230 return NULL;
1232 isl_ctx_deref(bmap->ctx);
1233 free(bmap->div);
1234 isl_blk_free(bmap->ctx, bmap->block2);
1235 free(bmap->ineq);
1236 isl_blk_free(bmap->ctx, bmap->block);
1237 isl_vec_free(bmap->sample);
1238 isl_space_free(bmap->dim);
1239 free(bmap);
1241 return NULL;
1244 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1246 return isl_basic_map_free(bset_to_bmap(bset));
1249 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1251 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1254 /* Check that "map" has only named parameters, reporting an error
1255 * if it does not.
1257 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1259 return isl_space_check_named_params(isl_map_peek_space(map));
1262 /* Check that "bmap1" and "bmap2" have the same parameters,
1263 * reporting an error if they do not.
1265 static isl_stat isl_basic_map_check_equal_params(
1266 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1268 isl_bool match;
1270 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1271 if (match < 0)
1272 return isl_stat_error;
1273 if (!match)
1274 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1275 "parameters don't match", return isl_stat_error);
1276 return isl_stat_ok;
1279 __isl_give isl_map *isl_map_align_params_map_map_and(
1280 __isl_take isl_map *map1, __isl_take isl_map *map2,
1281 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1282 __isl_take isl_map *map2))
1284 if (!map1 || !map2)
1285 goto error;
1286 if (isl_map_has_equal_params(map1, map2))
1287 return fn(map1, map2);
1288 if (isl_map_check_named_params(map1) < 0)
1289 goto error;
1290 if (isl_map_check_named_params(map2) < 0)
1291 goto error;
1292 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1293 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1294 return fn(map1, map2);
1295 error:
1296 isl_map_free(map1);
1297 isl_map_free(map2);
1298 return NULL;
1301 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1302 __isl_keep isl_map *map2,
1303 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1305 isl_bool r;
1307 if (!map1 || !map2)
1308 return isl_bool_error;
1309 if (isl_map_has_equal_params(map1, map2))
1310 return fn(map1, map2);
1311 if (isl_map_check_named_params(map1) < 0)
1312 return isl_bool_error;
1313 if (isl_map_check_named_params(map2) < 0)
1314 return isl_bool_error;
1315 map1 = isl_map_copy(map1);
1316 map2 = isl_map_copy(map2);
1317 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1318 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1319 r = fn(map1, map2);
1320 isl_map_free(map1);
1321 isl_map_free(map2);
1322 return r;
1325 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1327 struct isl_ctx *ctx;
1328 if (!bmap)
1329 return -1;
1330 ctx = bmap->ctx;
1331 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1332 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1333 return -1);
1334 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1335 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1336 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1337 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1338 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1339 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1340 isl_int *t;
1341 int j = isl_basic_map_alloc_inequality(bmap);
1342 if (j < 0)
1343 return -1;
1344 t = bmap->ineq[j];
1345 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1346 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1347 bmap->eq[-1] = t;
1348 bmap->n_eq++;
1349 bmap->n_ineq--;
1350 bmap->eq--;
1351 return 0;
1353 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1354 bmap->extra - bmap->n_div);
1355 return bmap->n_eq++;
1358 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1360 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1363 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1365 if (!bmap)
1366 return -1;
1367 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1368 bmap->n_eq -= n;
1369 return 0;
1372 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1374 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1377 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1379 isl_int *t;
1380 if (!bmap)
1381 return -1;
1382 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1384 if (pos != bmap->n_eq - 1) {
1385 t = bmap->eq[pos];
1386 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1387 bmap->eq[bmap->n_eq - 1] = t;
1389 bmap->n_eq--;
1390 return 0;
1393 /* Turn inequality "pos" of "bmap" into an equality.
1395 * In particular, we move the inequality in front of the equalities
1396 * and move the last inequality in the position of the moved inequality.
1397 * Note that isl_tab_make_equalities_explicit depends on this particular
1398 * change in the ordering of the constraints.
1400 void isl_basic_map_inequality_to_equality(
1401 struct isl_basic_map *bmap, unsigned pos)
1403 isl_int *t;
1405 t = bmap->ineq[pos];
1406 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1407 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1408 bmap->eq[-1] = t;
1409 bmap->n_eq++;
1410 bmap->n_ineq--;
1411 bmap->eq--;
1412 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1413 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1414 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1415 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1418 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1420 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1423 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1425 struct isl_ctx *ctx;
1426 if (!bmap)
1427 return -1;
1428 ctx = bmap->ctx;
1429 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1430 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1431 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1432 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1433 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1434 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1435 1 + isl_basic_map_total_dim(bmap),
1436 bmap->extra - bmap->n_div);
1437 return bmap->n_ineq++;
1440 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1442 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1445 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1447 if (!bmap)
1448 return -1;
1449 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1450 bmap->n_ineq -= n;
1451 return 0;
1454 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1456 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1459 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1461 isl_int *t;
1462 if (!bmap)
1463 return -1;
1464 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1466 if (pos != bmap->n_ineq - 1) {
1467 t = bmap->ineq[pos];
1468 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1469 bmap->ineq[bmap->n_ineq - 1] = t;
1470 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1472 bmap->n_ineq--;
1473 return 0;
1476 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1478 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1481 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1482 isl_int *eq)
1484 int k;
1486 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1487 if (!bmap)
1488 return NULL;
1489 k = isl_basic_map_alloc_equality(bmap);
1490 if (k < 0)
1491 goto error;
1492 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1493 return bmap;
1494 error:
1495 isl_basic_map_free(bmap);
1496 return NULL;
1499 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1500 isl_int *eq)
1502 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1505 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1506 isl_int *ineq)
1508 int k;
1510 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1511 if (!bmap)
1512 return NULL;
1513 k = isl_basic_map_alloc_inequality(bmap);
1514 if (k < 0)
1515 goto error;
1516 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1517 return bmap;
1518 error:
1519 isl_basic_map_free(bmap);
1520 return NULL;
1523 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1524 isl_int *ineq)
1526 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1529 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1531 if (!bmap)
1532 return -1;
1533 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1534 isl_seq_clr(bmap->div[bmap->n_div] +
1535 1 + 1 + isl_basic_map_total_dim(bmap),
1536 bmap->extra - bmap->n_div);
1537 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1538 return bmap->n_div++;
1541 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1543 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1546 /* Check that there are "n" dimensions of type "type" starting at "first"
1547 * in "bmap".
1549 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1550 enum isl_dim_type type, unsigned first, unsigned n)
1552 unsigned dim;
1554 if (!bmap)
1555 return isl_stat_error;
1556 dim = isl_basic_map_dim(bmap, type);
1557 if (first + n > dim || first + n < first)
1558 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1559 "position or range out of bounds",
1560 return isl_stat_error);
1561 return isl_stat_ok;
1564 /* Insert an extra integer division, prescribed by "div", to "bmap"
1565 * at (integer division) position "pos".
1567 * The integer division is first added at the end and then moved
1568 * into the right position.
1570 __isl_give isl_basic_map *isl_basic_map_insert_div(
1571 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1573 int i, k;
1575 bmap = isl_basic_map_cow(bmap);
1576 if (!bmap || !div)
1577 return isl_basic_map_free(bmap);
1579 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1580 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1581 "unexpected size", return isl_basic_map_free(bmap));
1582 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1583 return isl_basic_map_free(bmap);
1585 bmap = isl_basic_map_extend_space(bmap,
1586 isl_basic_map_get_space(bmap), 1, 0, 2);
1587 k = isl_basic_map_alloc_div(bmap);
1588 if (k < 0)
1589 return isl_basic_map_free(bmap);
1590 isl_seq_cpy(bmap->div[k], div->el, div->size);
1591 isl_int_set_si(bmap->div[k][div->size], 0);
1593 for (i = k; i > pos; --i)
1594 isl_basic_map_swap_div(bmap, i, i - 1);
1596 return bmap;
1599 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1601 if (!bmap)
1602 return isl_stat_error;
1603 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1604 bmap->n_div -= n;
1605 return isl_stat_ok;
1608 /* Copy constraint from src to dst, putting the vars of src at offset
1609 * dim_off in dst and the divs of src at offset div_off in dst.
1610 * If both sets are actually map, then dim_off applies to the input
1611 * variables.
1613 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1614 struct isl_basic_map *src_map, isl_int *src,
1615 unsigned in_off, unsigned out_off, unsigned div_off)
1617 unsigned src_nparam = isl_basic_map_n_param(src_map);
1618 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1619 unsigned src_in = isl_basic_map_n_in(src_map);
1620 unsigned dst_in = isl_basic_map_n_in(dst_map);
1621 unsigned src_out = isl_basic_map_n_out(src_map);
1622 unsigned dst_out = isl_basic_map_n_out(dst_map);
1623 isl_int_set(dst[0], src[0]);
1624 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1625 if (dst_nparam > src_nparam)
1626 isl_seq_clr(dst+1+src_nparam,
1627 dst_nparam - src_nparam);
1628 isl_seq_clr(dst+1+dst_nparam, in_off);
1629 isl_seq_cpy(dst+1+dst_nparam+in_off,
1630 src+1+src_nparam,
1631 isl_min(dst_in-in_off, src_in));
1632 if (dst_in-in_off > src_in)
1633 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1634 dst_in - in_off - src_in);
1635 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1636 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1637 src+1+src_nparam+src_in,
1638 isl_min(dst_out-out_off, src_out));
1639 if (dst_out-out_off > src_out)
1640 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1641 dst_out - out_off - src_out);
1642 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1643 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1644 src+1+src_nparam+src_in+src_out,
1645 isl_min(dst_map->extra-div_off, src_map->n_div));
1646 if (dst_map->n_div-div_off > src_map->n_div)
1647 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1648 div_off+src_map->n_div,
1649 dst_map->n_div - div_off - src_map->n_div);
1652 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1653 struct isl_basic_map *src_map, isl_int *src,
1654 unsigned in_off, unsigned out_off, unsigned div_off)
1656 isl_int_set(dst[0], src[0]);
1657 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1660 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1661 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1663 int i;
1664 unsigned div_off;
1666 if (!bmap1 || !bmap2)
1667 goto error;
1669 div_off = bmap1->n_div;
1671 for (i = 0; i < bmap2->n_eq; ++i) {
1672 int i1 = isl_basic_map_alloc_equality(bmap1);
1673 if (i1 < 0)
1674 goto error;
1675 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1676 i_pos, o_pos, div_off);
1679 for (i = 0; i < bmap2->n_ineq; ++i) {
1680 int i1 = isl_basic_map_alloc_inequality(bmap1);
1681 if (i1 < 0)
1682 goto error;
1683 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1684 i_pos, o_pos, div_off);
1687 for (i = 0; i < bmap2->n_div; ++i) {
1688 int i1 = isl_basic_map_alloc_div(bmap1);
1689 if (i1 < 0)
1690 goto error;
1691 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1692 i_pos, o_pos, div_off);
1695 isl_basic_map_free(bmap2);
1697 return bmap1;
1699 error:
1700 isl_basic_map_free(bmap1);
1701 isl_basic_map_free(bmap2);
1702 return NULL;
1705 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1706 struct isl_basic_set *bset2, unsigned pos)
1708 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1709 bset_to_bmap(bset2), 0, pos));
1712 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1713 __isl_take isl_space *dim, unsigned extra,
1714 unsigned n_eq, unsigned n_ineq)
1716 struct isl_basic_map *ext;
1717 unsigned flags;
1718 int dims_ok;
1720 if (!dim)
1721 goto error;
1723 if (!base)
1724 goto error;
1726 dims_ok = isl_space_is_equal(base->dim, dim) &&
1727 base->extra >= base->n_div + extra;
1729 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1730 room_for_ineq(base, n_ineq)) {
1731 isl_space_free(dim);
1732 return base;
1735 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1736 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1737 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1738 extra += base->extra;
1739 n_eq += base->n_eq;
1740 n_ineq += base->n_ineq;
1742 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1743 dim = NULL;
1744 if (!ext)
1745 goto error;
1747 if (dims_ok)
1748 ext->sample = isl_vec_copy(base->sample);
1749 flags = base->flags;
1750 ext = add_constraints(ext, base, 0, 0);
1751 if (ext) {
1752 ext->flags = flags;
1753 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1756 return ext;
1758 error:
1759 isl_space_free(dim);
1760 isl_basic_map_free(base);
1761 return NULL;
1764 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1765 __isl_take isl_space *dim, unsigned extra,
1766 unsigned n_eq, unsigned n_ineq)
1768 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1769 dim, extra, n_eq, n_ineq));
1772 struct isl_basic_map *isl_basic_map_extend_constraints(
1773 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1775 if (!base)
1776 return NULL;
1777 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1778 0, n_eq, n_ineq);
1781 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1782 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1783 unsigned n_eq, unsigned n_ineq)
1785 struct isl_basic_map *bmap;
1786 isl_space *dim;
1788 if (!base)
1789 return NULL;
1790 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1791 if (!dim)
1792 goto error;
1794 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1795 return bmap;
1796 error:
1797 isl_basic_map_free(base);
1798 return NULL;
1801 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1802 unsigned nparam, unsigned dim, unsigned extra,
1803 unsigned n_eq, unsigned n_ineq)
1805 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1806 nparam, 0, dim, extra, n_eq, n_ineq));
1809 struct isl_basic_set *isl_basic_set_extend_constraints(
1810 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1812 isl_basic_map *bmap = bset_to_bmap(base);
1813 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1814 return bset_from_bmap(bmap);
1817 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1819 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1822 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1824 if (!bmap)
1825 return NULL;
1827 if (bmap->ref > 1) {
1828 bmap->ref--;
1829 bmap = isl_basic_map_dup(bmap);
1831 if (bmap) {
1832 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1833 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1835 return bmap;
1838 /* Clear all cached information in "map", either because it is about
1839 * to be modified or because it is being freed.
1840 * Always return the same pointer that is passed in.
1841 * This is needed for the use in isl_map_free.
1843 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1845 isl_basic_map_free(map->cached_simple_hull[0]);
1846 isl_basic_map_free(map->cached_simple_hull[1]);
1847 map->cached_simple_hull[0] = NULL;
1848 map->cached_simple_hull[1] = NULL;
1849 return map;
1852 struct isl_set *isl_set_cow(struct isl_set *set)
1854 return isl_map_cow(set);
1857 /* Return an isl_map that is equal to "map" and that has only
1858 * a single reference.
1860 * If the original input already has only one reference, then
1861 * simply return it, but clear all cached information, since
1862 * it may be rendered invalid by the operations that will be
1863 * performed on the result.
1865 * Otherwise, create a duplicate (without any cached information).
1867 struct isl_map *isl_map_cow(struct isl_map *map)
1869 if (!map)
1870 return NULL;
1872 if (map->ref == 1)
1873 return clear_caches(map);
1874 map->ref--;
1875 return isl_map_dup(map);
1878 static void swap_vars(struct isl_blk blk, isl_int *a,
1879 unsigned a_len, unsigned b_len)
1881 isl_seq_cpy(blk.data, a+a_len, b_len);
1882 isl_seq_cpy(blk.data+b_len, a, a_len);
1883 isl_seq_cpy(a, blk.data, b_len+a_len);
1886 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1887 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1889 int i;
1890 struct isl_blk blk;
1892 if (!bmap)
1893 goto error;
1895 isl_assert(bmap->ctx,
1896 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1898 if (n1 == 0 || n2 == 0)
1899 return bmap;
1901 bmap = isl_basic_map_cow(bmap);
1902 if (!bmap)
1903 return NULL;
1905 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1906 if (isl_blk_is_error(blk))
1907 goto error;
1909 for (i = 0; i < bmap->n_eq; ++i)
1910 swap_vars(blk,
1911 bmap->eq[i] + pos, n1, n2);
1913 for (i = 0; i < bmap->n_ineq; ++i)
1914 swap_vars(blk,
1915 bmap->ineq[i] + pos, n1, n2);
1917 for (i = 0; i < bmap->n_div; ++i)
1918 swap_vars(blk,
1919 bmap->div[i]+1 + pos, n1, n2);
1921 isl_blk_free(bmap->ctx, blk);
1923 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1924 bmap = isl_basic_map_gauss(bmap, NULL);
1925 return isl_basic_map_finalize(bmap);
1926 error:
1927 isl_basic_map_free(bmap);
1928 return NULL;
1931 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1933 int i = 0;
1934 unsigned total;
1935 if (!bmap)
1936 goto error;
1937 total = isl_basic_map_total_dim(bmap);
1938 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
1939 return isl_basic_map_free(bmap);
1940 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1941 if (bmap->n_eq > 0)
1942 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1943 else {
1944 i = isl_basic_map_alloc_equality(bmap);
1945 if (i < 0)
1946 goto error;
1948 isl_int_set_si(bmap->eq[i][0], 1);
1949 isl_seq_clr(bmap->eq[i]+1, total);
1950 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1951 isl_vec_free(bmap->sample);
1952 bmap->sample = NULL;
1953 return isl_basic_map_finalize(bmap);
1954 error:
1955 isl_basic_map_free(bmap);
1956 return NULL;
1959 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1961 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
1964 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1965 * of "bmap").
1967 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1969 isl_int *t = bmap->div[a];
1970 bmap->div[a] = bmap->div[b];
1971 bmap->div[b] = t;
1974 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1975 * div definitions accordingly.
1977 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1979 int i;
1980 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1982 swap_div(bmap, a, b);
1984 for (i = 0; i < bmap->n_eq; ++i)
1985 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1987 for (i = 0; i < bmap->n_ineq; ++i)
1988 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1990 for (i = 0; i < bmap->n_div; ++i)
1991 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1992 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1995 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1996 * div definitions accordingly.
1998 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
2000 isl_basic_map_swap_div(bset, a, b);
2003 /* Eliminate the specified n dimensions starting at first from the
2004 * constraints, without removing the dimensions from the space.
2005 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2007 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2008 enum isl_dim_type type, unsigned first, unsigned n)
2010 int i;
2012 if (!map)
2013 return NULL;
2014 if (n == 0)
2015 return map;
2017 if (first + n > isl_map_dim(map, type) || first + n < first)
2018 isl_die(map->ctx, isl_error_invalid,
2019 "index out of bounds", goto error);
2021 map = isl_map_cow(map);
2022 if (!map)
2023 return NULL;
2025 for (i = 0; i < map->n; ++i) {
2026 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2027 if (!map->p[i])
2028 goto error;
2030 return map;
2031 error:
2032 isl_map_free(map);
2033 return NULL;
2036 /* Eliminate the specified n dimensions starting at first from the
2037 * constraints, without removing the dimensions from the space.
2038 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2040 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2041 enum isl_dim_type type, unsigned first, unsigned n)
2043 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2046 /* Eliminate the specified n dimensions starting at first from the
2047 * constraints, without removing the dimensions from the space.
2048 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2050 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2051 unsigned first, unsigned n)
2053 return isl_set_eliminate(set, isl_dim_set, first, n);
2056 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2057 __isl_take isl_basic_map *bmap)
2059 if (!bmap)
2060 return NULL;
2061 bmap = isl_basic_map_eliminate_vars(bmap,
2062 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2063 if (!bmap)
2064 return NULL;
2065 bmap->n_div = 0;
2066 return isl_basic_map_finalize(bmap);
2069 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2070 __isl_take isl_basic_set *bset)
2072 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2075 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2077 int i;
2079 if (!map)
2080 return NULL;
2081 if (map->n == 0)
2082 return map;
2084 map = isl_map_cow(map);
2085 if (!map)
2086 return NULL;
2088 for (i = 0; i < map->n; ++i) {
2089 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2090 if (!map->p[i])
2091 goto error;
2093 return map;
2094 error:
2095 isl_map_free(map);
2096 return NULL;
2099 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2101 return isl_map_remove_divs(set);
2104 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2105 enum isl_dim_type type, unsigned first, unsigned n)
2107 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2108 return isl_basic_map_free(bmap);
2109 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2110 return bmap;
2111 bmap = isl_basic_map_eliminate_vars(bmap,
2112 isl_basic_map_offset(bmap, type) - 1 + first, n);
2113 if (!bmap)
2114 return bmap;
2115 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2116 return bmap;
2117 bmap = isl_basic_map_drop(bmap, type, first, n);
2118 return bmap;
2121 /* Return true if the definition of the given div (recursively) involves
2122 * any of the given variables.
2124 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2125 unsigned first, unsigned n)
2127 int i;
2128 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2130 if (isl_int_is_zero(bmap->div[div][0]))
2131 return isl_bool_false;
2132 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2133 return isl_bool_true;
2135 for (i = bmap->n_div - 1; i >= 0; --i) {
2136 isl_bool involves;
2138 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2139 continue;
2140 involves = div_involves_vars(bmap, i, first, n);
2141 if (involves < 0 || involves)
2142 return involves;
2145 return isl_bool_false;
2148 /* Try and add a lower and/or upper bound on "div" to "bmap"
2149 * based on inequality "i".
2150 * "total" is the total number of variables (excluding the divs).
2151 * "v" is a temporary object that can be used during the calculations.
2152 * If "lb" is set, then a lower bound should be constructed.
2153 * If "ub" is set, then an upper bound should be constructed.
2155 * The calling function has already checked that the inequality does not
2156 * reference "div", but we still need to check that the inequality is
2157 * of the right form. We'll consider the case where we want to construct
2158 * a lower bound. The construction of upper bounds is similar.
2160 * Let "div" be of the form
2162 * q = floor((a + f(x))/d)
2164 * We essentially check if constraint "i" is of the form
2166 * b + f(x) >= 0
2168 * so that we can use it to derive a lower bound on "div".
2169 * However, we allow a slightly more general form
2171 * b + g(x) >= 0
2173 * with the condition that the coefficients of g(x) - f(x) are all
2174 * divisible by d.
2175 * Rewriting this constraint as
2177 * 0 >= -b - g(x)
2179 * adding a + f(x) to both sides and dividing by d, we obtain
2181 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2183 * Taking the floor on both sides, we obtain
2185 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2187 * or
2189 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2191 * In the case of an upper bound, we construct the constraint
2193 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2196 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2197 __isl_take isl_basic_map *bmap, int div, int i,
2198 unsigned total, isl_int v, int lb, int ub)
2200 int j;
2202 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2203 if (lb) {
2204 isl_int_sub(v, bmap->ineq[i][1 + j],
2205 bmap->div[div][1 + 1 + j]);
2206 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2208 if (ub) {
2209 isl_int_add(v, bmap->ineq[i][1 + j],
2210 bmap->div[div][1 + 1 + j]);
2211 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2214 if (!lb && !ub)
2215 return bmap;
2217 bmap = isl_basic_map_cow(bmap);
2218 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2219 if (lb) {
2220 int k = isl_basic_map_alloc_inequality(bmap);
2221 if (k < 0)
2222 goto error;
2223 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2224 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2225 bmap->div[div][1 + j]);
2226 isl_int_cdiv_q(bmap->ineq[k][j],
2227 bmap->ineq[k][j], bmap->div[div][0]);
2229 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2231 if (ub) {
2232 int k = isl_basic_map_alloc_inequality(bmap);
2233 if (k < 0)
2234 goto error;
2235 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2236 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2237 bmap->div[div][1 + j]);
2238 isl_int_fdiv_q(bmap->ineq[k][j],
2239 bmap->ineq[k][j], bmap->div[div][0]);
2241 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2244 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2245 return bmap;
2246 error:
2247 isl_basic_map_free(bmap);
2248 return NULL;
2251 /* This function is called right before "div" is eliminated from "bmap"
2252 * using Fourier-Motzkin.
2253 * Look through the constraints of "bmap" for constraints on the argument
2254 * of the integer division and use them to construct constraints on the
2255 * integer division itself. These constraints can then be combined
2256 * during the Fourier-Motzkin elimination.
2257 * Note that it is only useful to introduce lower bounds on "div"
2258 * if "bmap" already contains upper bounds on "div" as the newly
2259 * introduce lower bounds can then be combined with the pre-existing
2260 * upper bounds. Similarly for upper bounds.
2261 * We therefore first check if "bmap" contains any lower and/or upper bounds
2262 * on "div".
2264 * It is interesting to note that the introduction of these constraints
2265 * can indeed lead to more accurate results, even when compared to
2266 * deriving constraints on the argument of "div" from constraints on "div".
2267 * Consider, for example, the set
2269 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2271 * The second constraint can be rewritten as
2273 * 2 * [(-i-2j+3)/4] + k >= 0
2275 * from which we can derive
2277 * -i - 2j + 3 >= -2k
2279 * or
2281 * i + 2j <= 3 + 2k
2283 * Combined with the first constraint, we obtain
2285 * -3 <= 3 + 2k or k >= -3
2287 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2288 * the first constraint, we obtain
2290 * [(i + 2j)/4] >= [-3/4] = -1
2292 * Combining this constraint with the second constraint, we obtain
2294 * k >= -2
2296 static __isl_give isl_basic_map *insert_bounds_on_div(
2297 __isl_take isl_basic_map *bmap, int div)
2299 int i;
2300 int check_lb, check_ub;
2301 isl_int v;
2302 unsigned total;
2304 if (!bmap)
2305 return NULL;
2307 if (isl_int_is_zero(bmap->div[div][0]))
2308 return bmap;
2310 total = isl_space_dim(bmap->dim, isl_dim_all);
2312 check_lb = 0;
2313 check_ub = 0;
2314 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2315 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2316 if (s > 0)
2317 check_ub = 1;
2318 if (s < 0)
2319 check_lb = 1;
2322 if (!check_lb && !check_ub)
2323 return bmap;
2325 isl_int_init(v);
2327 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2328 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2329 continue;
2331 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2332 check_lb, check_ub);
2335 isl_int_clear(v);
2337 return bmap;
2340 /* Remove all divs (recursively) involving any of the given dimensions
2341 * in their definitions.
2343 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2344 __isl_take isl_basic_map *bmap,
2345 enum isl_dim_type type, unsigned first, unsigned n)
2347 int i;
2349 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2350 return isl_basic_map_free(bmap);
2351 first += isl_basic_map_offset(bmap, type);
2353 for (i = bmap->n_div - 1; i >= 0; --i) {
2354 isl_bool involves;
2356 involves = div_involves_vars(bmap, i, first, n);
2357 if (involves < 0)
2358 return isl_basic_map_free(bmap);
2359 if (!involves)
2360 continue;
2361 bmap = insert_bounds_on_div(bmap, i);
2362 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2363 if (!bmap)
2364 return NULL;
2365 i = bmap->n_div;
2368 return bmap;
2371 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2372 __isl_take isl_basic_set *bset,
2373 enum isl_dim_type type, unsigned first, unsigned n)
2375 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2378 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2379 enum isl_dim_type type, unsigned first, unsigned n)
2381 int i;
2383 if (!map)
2384 return NULL;
2385 if (map->n == 0)
2386 return map;
2388 map = isl_map_cow(map);
2389 if (!map)
2390 return NULL;
2392 for (i = 0; i < map->n; ++i) {
2393 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2394 type, first, n);
2395 if (!map->p[i])
2396 goto error;
2398 return map;
2399 error:
2400 isl_map_free(map);
2401 return NULL;
2404 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2405 enum isl_dim_type type, unsigned first, unsigned n)
2407 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2408 type, first, n));
2411 /* Does the description of "bmap" depend on the specified dimensions?
2412 * We also check whether the dimensions appear in any of the div definitions.
2413 * In principle there is no need for this check. If the dimensions appear
2414 * in a div definition, they also appear in the defining constraints of that
2415 * div.
2417 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2418 enum isl_dim_type type, unsigned first, unsigned n)
2420 int i;
2422 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2423 return isl_bool_error;
2425 first += isl_basic_map_offset(bmap, type);
2426 for (i = 0; i < bmap->n_eq; ++i)
2427 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2428 return isl_bool_true;
2429 for (i = 0; i < bmap->n_ineq; ++i)
2430 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2431 return isl_bool_true;
2432 for (i = 0; i < bmap->n_div; ++i) {
2433 if (isl_int_is_zero(bmap->div[i][0]))
2434 continue;
2435 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2436 return isl_bool_true;
2439 return isl_bool_false;
2442 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2443 enum isl_dim_type type, unsigned first, unsigned n)
2445 int i;
2447 if (!map)
2448 return isl_bool_error;
2450 if (first + n > isl_map_dim(map, type))
2451 isl_die(map->ctx, isl_error_invalid,
2452 "index out of bounds", return isl_bool_error);
2454 for (i = 0; i < map->n; ++i) {
2455 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2456 type, first, n);
2457 if (involves < 0 || involves)
2458 return involves;
2461 return isl_bool_false;
2464 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2465 enum isl_dim_type type, unsigned first, unsigned n)
2467 return isl_basic_map_involves_dims(bset, type, first, n);
2470 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2471 enum isl_dim_type type, unsigned first, unsigned n)
2473 return isl_map_involves_dims(set, type, first, n);
2476 /* Drop all constraints in bmap that involve any of the dimensions
2477 * first to first+n-1.
2479 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2480 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2482 int i;
2484 if (n == 0)
2485 return bmap;
2487 bmap = isl_basic_map_cow(bmap);
2489 if (!bmap)
2490 return NULL;
2492 for (i = bmap->n_eq - 1; i >= 0; --i) {
2493 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2494 continue;
2495 isl_basic_map_drop_equality(bmap, i);
2498 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2499 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2500 continue;
2501 isl_basic_map_drop_inequality(bmap, i);
2504 bmap = isl_basic_map_add_known_div_constraints(bmap);
2505 return bmap;
2508 /* Drop all constraints in bset that involve any of the dimensions
2509 * first to first+n-1.
2511 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2512 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2514 return isl_basic_map_drop_constraints_involving(bset, first, n);
2517 /* Drop all constraints in bmap that do not involve any of the dimensions
2518 * first to first + n - 1 of the given type.
2520 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2521 __isl_take isl_basic_map *bmap,
2522 enum isl_dim_type type, unsigned first, unsigned n)
2524 int i;
2526 if (n == 0) {
2527 isl_space *space = isl_basic_map_get_space(bmap);
2528 isl_basic_map_free(bmap);
2529 return isl_basic_map_universe(space);
2531 bmap = isl_basic_map_cow(bmap);
2532 if (!bmap)
2533 return NULL;
2535 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2536 return isl_basic_map_free(bmap);
2538 first += isl_basic_map_offset(bmap, type) - 1;
2540 for (i = bmap->n_eq - 1; i >= 0; --i) {
2541 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2542 continue;
2543 isl_basic_map_drop_equality(bmap, i);
2546 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2547 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2548 continue;
2549 isl_basic_map_drop_inequality(bmap, i);
2552 bmap = isl_basic_map_add_known_div_constraints(bmap);
2553 return bmap;
2556 /* Drop all constraints in bset that do not involve any of the dimensions
2557 * first to first + n - 1 of the given type.
2559 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2560 __isl_take isl_basic_set *bset,
2561 enum isl_dim_type type, unsigned first, unsigned n)
2563 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2564 type, first, n);
2567 /* Drop all constraints in bmap that involve any of the dimensions
2568 * first to first + n - 1 of the given type.
2570 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2571 __isl_take isl_basic_map *bmap,
2572 enum isl_dim_type type, unsigned first, unsigned n)
2574 if (!bmap)
2575 return NULL;
2576 if (n == 0)
2577 return bmap;
2579 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2580 return isl_basic_map_free(bmap);
2582 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2583 first += isl_basic_map_offset(bmap, type) - 1;
2584 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2587 /* Drop all constraints in bset that involve any of the dimensions
2588 * first to first + n - 1 of the given type.
2590 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2591 __isl_take isl_basic_set *bset,
2592 enum isl_dim_type type, unsigned first, unsigned n)
2594 return isl_basic_map_drop_constraints_involving_dims(bset,
2595 type, first, n);
2598 /* Drop constraints from "map" by applying "drop" to each basic map.
2600 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2601 enum isl_dim_type type, unsigned first, unsigned n,
2602 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2603 enum isl_dim_type type, unsigned first, unsigned n))
2605 int i;
2606 unsigned dim;
2608 if (!map)
2609 return NULL;
2611 dim = isl_map_dim(map, type);
2612 if (first + n > dim || first + n < first)
2613 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2614 "index out of bounds", return isl_map_free(map));
2616 map = isl_map_cow(map);
2617 if (!map)
2618 return NULL;
2620 for (i = 0; i < map->n; ++i) {
2621 map->p[i] = drop(map->p[i], type, first, n);
2622 if (!map->p[i])
2623 return isl_map_free(map);
2626 if (map->n > 1)
2627 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2629 return map;
2632 /* Drop all constraints in map that involve any of the dimensions
2633 * first to first + n - 1 of the given type.
2635 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2636 __isl_take isl_map *map,
2637 enum isl_dim_type type, unsigned first, unsigned n)
2639 if (n == 0)
2640 return map;
2641 return drop_constraints(map, type, first, n,
2642 &isl_basic_map_drop_constraints_involving_dims);
2645 /* Drop all constraints in "map" that do not involve any of the dimensions
2646 * first to first + n - 1 of the given type.
2648 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2649 __isl_take isl_map *map,
2650 enum isl_dim_type type, unsigned first, unsigned n)
2652 if (n == 0) {
2653 isl_space *space = isl_map_get_space(map);
2654 isl_map_free(map);
2655 return isl_map_universe(space);
2657 return drop_constraints(map, type, first, n,
2658 &isl_basic_map_drop_constraints_not_involving_dims);
2661 /* Drop all constraints in set that involve any of the dimensions
2662 * first to first + n - 1 of the given type.
2664 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
2665 __isl_take isl_set *set,
2666 enum isl_dim_type type, unsigned first, unsigned n)
2668 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2671 /* Drop all constraints in "set" that do not involve any of the dimensions
2672 * first to first + n - 1 of the given type.
2674 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
2675 __isl_take isl_set *set,
2676 enum isl_dim_type type, unsigned first, unsigned n)
2678 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
2681 /* Does local variable "div" of "bmap" have a complete explicit representation?
2682 * Having a complete explicit representation requires not only
2683 * an explicit representation, but also that all local variables
2684 * that appear in this explicit representation in turn have
2685 * a complete explicit representation.
2687 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2689 int i;
2690 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2691 isl_bool marked;
2693 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2694 if (marked < 0 || marked)
2695 return isl_bool_not(marked);
2697 for (i = bmap->n_div - 1; i >= 0; --i) {
2698 isl_bool known;
2700 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2701 continue;
2702 known = isl_basic_map_div_is_known(bmap, i);
2703 if (known < 0 || !known)
2704 return known;
2707 return isl_bool_true;
2710 /* Remove all divs that are unknown or defined in terms of unknown divs.
2712 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2713 __isl_take isl_basic_map *bmap)
2715 int i;
2717 if (!bmap)
2718 return NULL;
2720 for (i = bmap->n_div - 1; i >= 0; --i) {
2721 if (isl_basic_map_div_is_known(bmap, i))
2722 continue;
2723 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2724 if (!bmap)
2725 return NULL;
2726 i = bmap->n_div;
2729 return bmap;
2732 /* Remove all divs that are unknown or defined in terms of unknown divs.
2734 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2735 __isl_take isl_basic_set *bset)
2737 return isl_basic_map_remove_unknown_divs(bset);
2740 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2742 int i;
2744 if (!map)
2745 return NULL;
2746 if (map->n == 0)
2747 return map;
2749 map = isl_map_cow(map);
2750 if (!map)
2751 return NULL;
2753 for (i = 0; i < map->n; ++i) {
2754 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2755 if (!map->p[i])
2756 goto error;
2758 return map;
2759 error:
2760 isl_map_free(map);
2761 return NULL;
2764 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2766 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
2769 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2770 __isl_take isl_basic_set *bset,
2771 enum isl_dim_type type, unsigned first, unsigned n)
2773 isl_basic_map *bmap = bset_to_bmap(bset);
2774 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2775 return bset_from_bmap(bmap);
2778 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2779 enum isl_dim_type type, unsigned first, unsigned n)
2781 int i;
2783 if (n == 0)
2784 return map;
2786 map = isl_map_cow(map);
2787 if (!map)
2788 return NULL;
2789 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2791 for (i = 0; i < map->n; ++i) {
2792 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2793 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2794 if (!map->p[i])
2795 goto error;
2797 map = isl_map_drop(map, type, first, n);
2798 return map;
2799 error:
2800 isl_map_free(map);
2801 return NULL;
2804 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2805 enum isl_dim_type type, unsigned first, unsigned n)
2807 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2808 type, first, n));
2811 /* Project out n inputs starting at first using Fourier-Motzkin */
2812 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2813 unsigned first, unsigned n)
2815 return isl_map_remove_dims(map, isl_dim_in, first, n);
2818 static void dump_term(struct isl_basic_map *bmap,
2819 isl_int c, int pos, FILE *out)
2821 const char *name;
2822 unsigned in = isl_basic_map_n_in(bmap);
2823 unsigned dim = in + isl_basic_map_n_out(bmap);
2824 unsigned nparam = isl_basic_map_n_param(bmap);
2825 if (!pos)
2826 isl_int_print(out, c, 0);
2827 else {
2828 if (!isl_int_is_one(c))
2829 isl_int_print(out, c, 0);
2830 if (pos < 1 + nparam) {
2831 name = isl_space_get_dim_name(bmap->dim,
2832 isl_dim_param, pos - 1);
2833 if (name)
2834 fprintf(out, "%s", name);
2835 else
2836 fprintf(out, "p%d", pos - 1);
2837 } else if (pos < 1 + nparam + in)
2838 fprintf(out, "i%d", pos - 1 - nparam);
2839 else if (pos < 1 + nparam + dim)
2840 fprintf(out, "o%d", pos - 1 - nparam - in);
2841 else
2842 fprintf(out, "e%d", pos - 1 - nparam - dim);
2846 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2847 int sign, FILE *out)
2849 int i;
2850 int first;
2851 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2852 isl_int v;
2854 isl_int_init(v);
2855 for (i = 0, first = 1; i < len; ++i) {
2856 if (isl_int_sgn(c[i]) * sign <= 0)
2857 continue;
2858 if (!first)
2859 fprintf(out, " + ");
2860 first = 0;
2861 isl_int_abs(v, c[i]);
2862 dump_term(bmap, v, i, out);
2864 isl_int_clear(v);
2865 if (first)
2866 fprintf(out, "0");
2869 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2870 const char *op, FILE *out, int indent)
2872 int i;
2874 fprintf(out, "%*s", indent, "");
2876 dump_constraint_sign(bmap, c, 1, out);
2877 fprintf(out, " %s ", op);
2878 dump_constraint_sign(bmap, c, -1, out);
2880 fprintf(out, "\n");
2882 for (i = bmap->n_div; i < bmap->extra; ++i) {
2883 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2884 continue;
2885 fprintf(out, "%*s", indent, "");
2886 fprintf(out, "ERROR: unused div coefficient not zero\n");
2887 abort();
2891 static void dump_constraints(struct isl_basic_map *bmap,
2892 isl_int **c, unsigned n,
2893 const char *op, FILE *out, int indent)
2895 int i;
2897 for (i = 0; i < n; ++i)
2898 dump_constraint(bmap, c[i], op, out, indent);
2901 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2903 int j;
2904 int first = 1;
2905 unsigned total = isl_basic_map_total_dim(bmap);
2907 for (j = 0; j < 1 + total; ++j) {
2908 if (isl_int_is_zero(exp[j]))
2909 continue;
2910 if (!first && isl_int_is_pos(exp[j]))
2911 fprintf(out, "+");
2912 dump_term(bmap, exp[j], j, out);
2913 first = 0;
2917 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2919 int i;
2921 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2922 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2924 for (i = 0; i < bmap->n_div; ++i) {
2925 fprintf(out, "%*s", indent, "");
2926 fprintf(out, "e%d = [(", i);
2927 dump_affine(bmap, bmap->div[i]+1, out);
2928 fprintf(out, ")/");
2929 isl_int_print(out, bmap->div[i][0], 0);
2930 fprintf(out, "]\n");
2934 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2935 FILE *out, int indent)
2937 if (!bset) {
2938 fprintf(out, "null basic set\n");
2939 return;
2942 fprintf(out, "%*s", indent, "");
2943 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2944 bset->ref, bset->dim->nparam, bset->dim->n_out,
2945 bset->extra, bset->flags);
2946 dump(bset_to_bmap(bset), out, indent);
2949 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2950 FILE *out, int indent)
2952 if (!bmap) {
2953 fprintf(out, "null basic map\n");
2954 return;
2957 fprintf(out, "%*s", indent, "");
2958 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2959 "flags: %x, n_name: %d\n",
2960 bmap->ref,
2961 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2962 bmap->extra, bmap->flags, bmap->dim->n_id);
2963 dump(bmap, out, indent);
2966 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2968 unsigned total;
2969 if (!bmap)
2970 return -1;
2971 total = isl_basic_map_total_dim(bmap);
2972 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2973 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2974 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2975 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2976 return 0;
2979 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2980 unsigned flags)
2982 if (!space)
2983 return NULL;
2984 if (isl_space_dim(space, isl_dim_in) != 0)
2985 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2986 "set cannot have input dimensions", goto error);
2987 return isl_map_alloc_space(space, n, flags);
2988 error:
2989 isl_space_free(space);
2990 return NULL;
2993 /* Make sure "map" has room for at least "n" more basic maps.
2995 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2997 int i;
2998 struct isl_map *grown = NULL;
3000 if (!map)
3001 return NULL;
3002 isl_assert(map->ctx, n >= 0, goto error);
3003 if (map->n + n <= map->size)
3004 return map;
3005 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3006 if (!grown)
3007 goto error;
3008 for (i = 0; i < map->n; ++i) {
3009 grown->p[i] = isl_basic_map_copy(map->p[i]);
3010 if (!grown->p[i])
3011 goto error;
3012 grown->n++;
3014 isl_map_free(map);
3015 return grown;
3016 error:
3017 isl_map_free(grown);
3018 isl_map_free(map);
3019 return NULL;
3022 /* Make sure "set" has room for at least "n" more basic sets.
3024 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3026 return set_from_map(isl_map_grow(set_to_map(set), n));
3029 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
3031 return isl_map_from_basic_map(bset);
3034 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
3036 struct isl_map *map;
3038 if (!bmap)
3039 return NULL;
3041 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3042 return isl_map_add_basic_map(map, bmap);
3045 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3046 __isl_take isl_basic_set *bset)
3048 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3049 bset_to_bmap(bset)));
3052 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3054 return isl_map_free(set);
3057 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3059 int i;
3061 if (!set) {
3062 fprintf(out, "null set\n");
3063 return;
3066 fprintf(out, "%*s", indent, "");
3067 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3068 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3069 set->flags);
3070 for (i = 0; i < set->n; ++i) {
3071 fprintf(out, "%*s", indent, "");
3072 fprintf(out, "basic set %d:\n", i);
3073 isl_basic_set_print_internal(set->p[i], out, indent+4);
3077 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3079 int i;
3081 if (!map) {
3082 fprintf(out, "null map\n");
3083 return;
3086 fprintf(out, "%*s", indent, "");
3087 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3088 "flags: %x, n_name: %d\n",
3089 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3090 map->dim->n_out, map->flags, map->dim->n_id);
3091 for (i = 0; i < map->n; ++i) {
3092 fprintf(out, "%*s", indent, "");
3093 fprintf(out, "basic map %d:\n", i);
3094 isl_basic_map_print_internal(map->p[i], out, indent+4);
3098 struct isl_basic_map *isl_basic_map_intersect_domain(
3099 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3101 struct isl_basic_map *bmap_domain;
3103 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3104 goto error;
3106 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3107 isl_assert(bset->ctx,
3108 isl_basic_map_compatible_domain(bmap, bset), goto error);
3110 bmap = isl_basic_map_cow(bmap);
3111 if (!bmap)
3112 goto error;
3113 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3114 bset->n_div, bset->n_eq, bset->n_ineq);
3115 bmap_domain = isl_basic_map_from_domain(bset);
3116 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3118 bmap = isl_basic_map_simplify(bmap);
3119 return isl_basic_map_finalize(bmap);
3120 error:
3121 isl_basic_map_free(bmap);
3122 isl_basic_set_free(bset);
3123 return NULL;
3126 /* Check that the space of "bset" is the same as that of the range of "bmap".
3128 static isl_stat isl_basic_map_check_compatible_range(
3129 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3131 isl_bool ok;
3133 ok = isl_basic_map_compatible_range(bmap, bset);
3134 if (ok < 0)
3135 return isl_stat_error;
3136 if (!ok)
3137 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3138 "incompatible spaces", return isl_stat_error);
3140 return isl_stat_ok;
3143 struct isl_basic_map *isl_basic_map_intersect_range(
3144 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3146 struct isl_basic_map *bmap_range;
3148 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3149 goto error;
3151 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3152 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3153 goto error;
3155 if (isl_basic_set_plain_is_universe(bset)) {
3156 isl_basic_set_free(bset);
3157 return bmap;
3160 bmap = isl_basic_map_cow(bmap);
3161 if (!bmap)
3162 goto error;
3163 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3164 bset->n_div, bset->n_eq, bset->n_ineq);
3165 bmap_range = bset_to_bmap(bset);
3166 bmap = add_constraints(bmap, bmap_range, 0, 0);
3168 bmap = isl_basic_map_simplify(bmap);
3169 return isl_basic_map_finalize(bmap);
3170 error:
3171 isl_basic_map_free(bmap);
3172 isl_basic_set_free(bset);
3173 return NULL;
3176 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3177 __isl_keep isl_vec *vec)
3179 int i;
3180 unsigned total;
3181 isl_int s;
3183 if (!bmap || !vec)
3184 return isl_bool_error;
3186 total = 1 + isl_basic_map_total_dim(bmap);
3187 if (total != vec->size)
3188 return isl_bool_false;
3190 isl_int_init(s);
3192 for (i = 0; i < bmap->n_eq; ++i) {
3193 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3194 if (!isl_int_is_zero(s)) {
3195 isl_int_clear(s);
3196 return isl_bool_false;
3200 for (i = 0; i < bmap->n_ineq; ++i) {
3201 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3202 if (isl_int_is_neg(s)) {
3203 isl_int_clear(s);
3204 return isl_bool_false;
3208 isl_int_clear(s);
3210 return isl_bool_true;
3213 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3214 __isl_keep isl_vec *vec)
3216 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3219 struct isl_basic_map *isl_basic_map_intersect(
3220 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3222 struct isl_vec *sample = NULL;
3224 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3225 goto error;
3226 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3227 isl_space_dim(bmap1->dim, isl_dim_param) &&
3228 isl_space_dim(bmap2->dim, isl_dim_all) !=
3229 isl_space_dim(bmap2->dim, isl_dim_param))
3230 return isl_basic_map_intersect(bmap2, bmap1);
3232 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3233 isl_space_dim(bmap2->dim, isl_dim_param))
3234 isl_assert(bmap1->ctx,
3235 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3237 if (isl_basic_map_plain_is_empty(bmap1)) {
3238 isl_basic_map_free(bmap2);
3239 return bmap1;
3241 if (isl_basic_map_plain_is_empty(bmap2)) {
3242 isl_basic_map_free(bmap1);
3243 return bmap2;
3246 if (bmap1->sample &&
3247 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3248 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3249 sample = isl_vec_copy(bmap1->sample);
3250 else if (bmap2->sample &&
3251 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3252 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3253 sample = isl_vec_copy(bmap2->sample);
3255 bmap1 = isl_basic_map_cow(bmap1);
3256 if (!bmap1)
3257 goto error;
3258 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3259 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3260 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3262 if (!bmap1)
3263 isl_vec_free(sample);
3264 else if (sample) {
3265 isl_vec_free(bmap1->sample);
3266 bmap1->sample = sample;
3269 bmap1 = isl_basic_map_simplify(bmap1);
3270 return isl_basic_map_finalize(bmap1);
3271 error:
3272 if (sample)
3273 isl_vec_free(sample);
3274 isl_basic_map_free(bmap1);
3275 isl_basic_map_free(bmap2);
3276 return NULL;
3279 struct isl_basic_set *isl_basic_set_intersect(
3280 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3282 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3283 bset_to_bmap(bset2)));
3286 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3287 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3289 return isl_basic_set_intersect(bset1, bset2);
3292 /* Special case of isl_map_intersect, where both map1 and map2
3293 * are convex, without any divs and such that either map1 or map2
3294 * contains a single constraint. This constraint is then simply
3295 * added to the other map.
3297 static __isl_give isl_map *map_intersect_add_constraint(
3298 __isl_take isl_map *map1, __isl_take isl_map *map2)
3300 isl_assert(map1->ctx, map1->n == 1, goto error);
3301 isl_assert(map2->ctx, map1->n == 1, goto error);
3302 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3303 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3305 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3306 return isl_map_intersect(map2, map1);
3308 map1 = isl_map_cow(map1);
3309 if (!map1)
3310 goto error;
3311 if (isl_map_plain_is_empty(map1)) {
3312 isl_map_free(map2);
3313 return map1;
3315 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3316 if (map2->p[0]->n_eq == 1)
3317 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3318 else
3319 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3320 map2->p[0]->ineq[0]);
3322 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3323 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3324 if (!map1->p[0])
3325 goto error;
3327 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3328 isl_basic_map_free(map1->p[0]);
3329 map1->n = 0;
3332 isl_map_free(map2);
3334 return map1;
3335 error:
3336 isl_map_free(map1);
3337 isl_map_free(map2);
3338 return NULL;
3341 /* map2 may be either a parameter domain or a map living in the same
3342 * space as map1.
3344 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3345 __isl_take isl_map *map2)
3347 unsigned flags = 0;
3348 isl_map *result;
3349 int i, j;
3351 if (!map1 || !map2)
3352 goto error;
3354 if ((isl_map_plain_is_empty(map1) ||
3355 isl_map_plain_is_universe(map2)) &&
3356 isl_space_is_equal(map1->dim, map2->dim)) {
3357 isl_map_free(map2);
3358 return map1;
3360 if ((isl_map_plain_is_empty(map2) ||
3361 isl_map_plain_is_universe(map1)) &&
3362 isl_space_is_equal(map1->dim, map2->dim)) {
3363 isl_map_free(map1);
3364 return map2;
3367 if (map1->n == 1 && map2->n == 1 &&
3368 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3369 isl_space_is_equal(map1->dim, map2->dim) &&
3370 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3371 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3372 return map_intersect_add_constraint(map1, map2);
3374 if (isl_space_dim(map2->dim, isl_dim_all) !=
3375 isl_space_dim(map2->dim, isl_dim_param))
3376 isl_assert(map1->ctx,
3377 isl_space_is_equal(map1->dim, map2->dim), goto error);
3379 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3380 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3381 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3383 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3384 map1->n * map2->n, flags);
3385 if (!result)
3386 goto error;
3387 for (i = 0; i < map1->n; ++i)
3388 for (j = 0; j < map2->n; ++j) {
3389 struct isl_basic_map *part;
3390 part = isl_basic_map_intersect(
3391 isl_basic_map_copy(map1->p[i]),
3392 isl_basic_map_copy(map2->p[j]));
3393 if (isl_basic_map_is_empty(part) < 0)
3394 part = isl_basic_map_free(part);
3395 result = isl_map_add_basic_map(result, part);
3396 if (!result)
3397 goto error;
3399 isl_map_free(map1);
3400 isl_map_free(map2);
3401 return result;
3402 error:
3403 isl_map_free(map1);
3404 isl_map_free(map2);
3405 return NULL;
3408 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3409 __isl_take isl_map *map2)
3411 if (!map1 || !map2)
3412 goto error;
3413 if (!isl_space_is_equal(map1->dim, map2->dim))
3414 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3415 "spaces don't match", goto error);
3416 return map_intersect_internal(map1, map2);
3417 error:
3418 isl_map_free(map1);
3419 isl_map_free(map2);
3420 return NULL;
3423 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3424 __isl_take isl_map *map2)
3426 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3429 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3431 return set_from_map(isl_map_intersect(set_to_map(set1),
3432 set_to_map(set2)));
3435 /* map_intersect_internal accepts intersections
3436 * with parameter domains, so we can just call that function.
3438 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3439 __isl_take isl_set *params)
3441 return map_intersect_internal(map, params);
3444 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3445 __isl_take isl_map *map2)
3447 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3450 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3451 __isl_take isl_set *params)
3453 return isl_map_intersect_params(set, params);
3456 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3458 isl_space *space;
3459 unsigned pos, n1, n2;
3461 if (!bmap)
3462 return NULL;
3463 bmap = isl_basic_map_cow(bmap);
3464 if (!bmap)
3465 return NULL;
3466 space = isl_space_reverse(isl_space_copy(bmap->dim));
3467 pos = isl_basic_map_offset(bmap, isl_dim_in);
3468 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3469 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3470 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3471 return isl_basic_map_reset_space(bmap, space);
3474 static __isl_give isl_basic_map *basic_map_space_reset(
3475 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3477 isl_space *space;
3479 if (!bmap)
3480 return NULL;
3481 if (!isl_space_is_named_or_nested(bmap->dim, type))
3482 return bmap;
3484 space = isl_basic_map_get_space(bmap);
3485 space = isl_space_reset(space, type);
3486 bmap = isl_basic_map_reset_space(bmap, space);
3487 return bmap;
3490 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3491 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3492 unsigned pos, unsigned n)
3494 isl_bool rational;
3495 isl_space *res_dim;
3496 struct isl_basic_map *res;
3497 struct isl_dim_map *dim_map;
3498 unsigned total, off;
3499 enum isl_dim_type t;
3501 if (n == 0)
3502 return basic_map_space_reset(bmap, type);
3504 if (!bmap)
3505 return NULL;
3507 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3509 total = isl_basic_map_total_dim(bmap) + n;
3510 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3511 off = 0;
3512 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3513 if (t != type) {
3514 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3515 } else {
3516 unsigned size = isl_basic_map_dim(bmap, t);
3517 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3518 0, pos, off);
3519 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3520 pos, size - pos, off + pos + n);
3522 off += isl_space_dim(res_dim, t);
3524 isl_dim_map_div(dim_map, bmap, off);
3526 res = isl_basic_map_alloc_space(res_dim,
3527 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3528 rational = isl_basic_map_is_rational(bmap);
3529 if (rational < 0)
3530 res = isl_basic_map_free(res);
3531 if (rational)
3532 res = isl_basic_map_set_rational(res);
3533 if (isl_basic_map_plain_is_empty(bmap)) {
3534 isl_basic_map_free(bmap);
3535 free(dim_map);
3536 return isl_basic_map_set_to_empty(res);
3538 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3539 return isl_basic_map_finalize(res);
3542 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3543 __isl_take isl_basic_set *bset,
3544 enum isl_dim_type type, unsigned pos, unsigned n)
3546 return isl_basic_map_insert_dims(bset, type, pos, n);
3549 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3550 enum isl_dim_type type, unsigned n)
3552 if (!bmap)
3553 return NULL;
3554 return isl_basic_map_insert_dims(bmap, type,
3555 isl_basic_map_dim(bmap, type), n);
3558 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3559 enum isl_dim_type type, unsigned n)
3561 if (!bset)
3562 return NULL;
3563 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3564 return isl_basic_map_add_dims(bset, type, n);
3565 error:
3566 isl_basic_set_free(bset);
3567 return NULL;
3570 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3571 enum isl_dim_type type)
3573 isl_space *space;
3575 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3576 return map;
3578 space = isl_map_get_space(map);
3579 space = isl_space_reset(space, type);
3580 map = isl_map_reset_space(map, space);
3581 return map;
3584 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3585 enum isl_dim_type type, unsigned pos, unsigned n)
3587 int i;
3589 if (n == 0)
3590 return map_space_reset(map, type);
3592 map = isl_map_cow(map);
3593 if (!map)
3594 return NULL;
3596 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3597 if (!map->dim)
3598 goto error;
3600 for (i = 0; i < map->n; ++i) {
3601 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3602 if (!map->p[i])
3603 goto error;
3606 return map;
3607 error:
3608 isl_map_free(map);
3609 return NULL;
3612 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3613 enum isl_dim_type type, unsigned pos, unsigned n)
3615 return isl_map_insert_dims(set, type, pos, n);
3618 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3619 enum isl_dim_type type, unsigned n)
3621 if (!map)
3622 return NULL;
3623 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3626 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3627 enum isl_dim_type type, unsigned n)
3629 if (!set)
3630 return NULL;
3631 isl_assert(set->ctx, type != isl_dim_in, goto error);
3632 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3633 error:
3634 isl_set_free(set);
3635 return NULL;
3638 __isl_give isl_basic_map *isl_basic_map_move_dims(
3639 __isl_take isl_basic_map *bmap,
3640 enum isl_dim_type dst_type, unsigned dst_pos,
3641 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3643 struct isl_dim_map *dim_map;
3644 struct isl_basic_map *res;
3645 enum isl_dim_type t;
3646 unsigned total, off;
3648 if (!bmap)
3649 return NULL;
3650 if (n == 0)
3651 return bmap;
3653 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3654 return isl_basic_map_free(bmap);
3656 if (dst_type == src_type && dst_pos == src_pos)
3657 return bmap;
3659 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3661 if (pos(bmap->dim, dst_type) + dst_pos ==
3662 pos(bmap->dim, src_type) + src_pos +
3663 ((src_type < dst_type) ? n : 0)) {
3664 bmap = isl_basic_map_cow(bmap);
3665 if (!bmap)
3666 return NULL;
3668 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3669 src_type, src_pos, n);
3670 if (!bmap->dim)
3671 goto error;
3673 bmap = isl_basic_map_finalize(bmap);
3675 return bmap;
3678 total = isl_basic_map_total_dim(bmap);
3679 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3681 off = 0;
3682 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3683 unsigned size = isl_space_dim(bmap->dim, t);
3684 if (t == dst_type) {
3685 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3686 0, dst_pos, off);
3687 off += dst_pos;
3688 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3689 src_pos, n, off);
3690 off += n;
3691 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3692 dst_pos, size - dst_pos, off);
3693 off += size - dst_pos;
3694 } else if (t == src_type) {
3695 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3696 0, src_pos, off);
3697 off += src_pos;
3698 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3699 src_pos + n, size - src_pos - n, off);
3700 off += size - src_pos - n;
3701 } else {
3702 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3703 off += size;
3706 isl_dim_map_div(dim_map, bmap, off);
3708 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3709 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3710 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3711 if (!bmap)
3712 goto error;
3714 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3715 src_type, src_pos, n);
3716 if (!bmap->dim)
3717 goto error;
3719 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3720 bmap = isl_basic_map_gauss(bmap, NULL);
3721 bmap = isl_basic_map_finalize(bmap);
3723 return bmap;
3724 error:
3725 isl_basic_map_free(bmap);
3726 return NULL;
3729 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3730 enum isl_dim_type dst_type, unsigned dst_pos,
3731 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3733 isl_basic_map *bmap = bset_to_bmap(bset);
3734 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3735 src_type, src_pos, n);
3736 return bset_from_bmap(bmap);
3739 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3740 enum isl_dim_type dst_type, unsigned dst_pos,
3741 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3743 if (!set)
3744 return NULL;
3745 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3746 return set_from_map(isl_map_move_dims(set_to_map(set),
3747 dst_type, dst_pos, src_type, src_pos, n));
3748 error:
3749 isl_set_free(set);
3750 return NULL;
3753 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3754 enum isl_dim_type dst_type, unsigned dst_pos,
3755 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3757 int i;
3759 if (!map)
3760 return NULL;
3761 if (n == 0)
3762 return map;
3764 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3765 goto error);
3767 if (dst_type == src_type && dst_pos == src_pos)
3768 return map;
3770 isl_assert(map->ctx, dst_type != src_type, goto error);
3772 map = isl_map_cow(map);
3773 if (!map)
3774 return NULL;
3776 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3777 if (!map->dim)
3778 goto error;
3780 for (i = 0; i < map->n; ++i) {
3781 map->p[i] = isl_basic_map_move_dims(map->p[i],
3782 dst_type, dst_pos,
3783 src_type, src_pos, n);
3784 if (!map->p[i])
3785 goto error;
3788 return map;
3789 error:
3790 isl_map_free(map);
3791 return NULL;
3794 /* Move the specified dimensions to the last columns right before
3795 * the divs. Don't change the dimension specification of bmap.
3796 * That's the responsibility of the caller.
3798 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3799 enum isl_dim_type type, unsigned first, unsigned n)
3801 struct isl_dim_map *dim_map;
3802 struct isl_basic_map *res;
3803 enum isl_dim_type t;
3804 unsigned total, off;
3806 if (!bmap)
3807 return NULL;
3808 if (pos(bmap->dim, type) + first + n ==
3809 1 + isl_space_dim(bmap->dim, isl_dim_all))
3810 return bmap;
3812 total = isl_basic_map_total_dim(bmap);
3813 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3815 off = 0;
3816 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3817 unsigned size = isl_space_dim(bmap->dim, t);
3818 if (t == type) {
3819 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3820 0, first, off);
3821 off += first;
3822 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3823 first, n, total - bmap->n_div - n);
3824 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3825 first + n, size - (first + n), off);
3826 off += size - (first + n);
3827 } else {
3828 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3829 off += size;
3832 isl_dim_map_div(dim_map, bmap, off + n);
3834 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3835 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3836 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3837 return res;
3840 /* Insert "n" rows in the divs of "bmap".
3842 * The number of columns is not changed, which means that the last
3843 * dimensions of "bmap" are being reintepreted as the new divs.
3844 * The space of "bmap" is not adjusted, however, which means
3845 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3846 * from the space of "bmap" is the responsibility of the caller.
3848 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3849 int n)
3851 int i;
3852 size_t row_size;
3853 isl_int **new_div;
3854 isl_int *old;
3856 bmap = isl_basic_map_cow(bmap);
3857 if (!bmap)
3858 return NULL;
3860 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3861 old = bmap->block2.data;
3862 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3863 (bmap->extra + n) * (1 + row_size));
3864 if (!bmap->block2.data)
3865 return isl_basic_map_free(bmap);
3866 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3867 if (!new_div)
3868 return isl_basic_map_free(bmap);
3869 for (i = 0; i < n; ++i) {
3870 new_div[i] = bmap->block2.data +
3871 (bmap->extra + i) * (1 + row_size);
3872 isl_seq_clr(new_div[i], 1 + row_size);
3874 for (i = 0; i < bmap->extra; ++i)
3875 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3876 free(bmap->div);
3877 bmap->div = new_div;
3878 bmap->n_div += n;
3879 bmap->extra += n;
3881 return bmap;
3884 /* Drop constraints from "bmap" that only involve the variables
3885 * of "type" in the range [first, first + n] that are not related
3886 * to any of the variables outside that interval.
3887 * These constraints cannot influence the values for the variables
3888 * outside the interval, except in case they cause "bmap" to be empty.
3889 * Only drop the constraints if "bmap" is known to be non-empty.
3891 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3892 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3893 unsigned first, unsigned n)
3895 int i;
3896 int *groups;
3897 unsigned dim, n_div;
3898 isl_bool non_empty;
3900 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3901 if (non_empty < 0)
3902 return isl_basic_map_free(bmap);
3903 if (!non_empty)
3904 return bmap;
3906 dim = isl_basic_map_dim(bmap, isl_dim_all);
3907 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3908 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3909 if (!groups)
3910 return isl_basic_map_free(bmap);
3911 first += isl_basic_map_offset(bmap, type) - 1;
3912 for (i = 0; i < first; ++i)
3913 groups[i] = -1;
3914 for (i = first + n; i < dim - n_div; ++i)
3915 groups[i] = -1;
3917 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3919 return bmap;
3922 /* Turn the n dimensions of type type, starting at first
3923 * into existentially quantified variables.
3925 * If a subset of the projected out variables are unrelated
3926 * to any of the variables that remain, then the constraints
3927 * involving this subset are simply dropped first.
3929 __isl_give isl_basic_map *isl_basic_map_project_out(
3930 __isl_take isl_basic_map *bmap,
3931 enum isl_dim_type type, unsigned first, unsigned n)
3933 if (n == 0)
3934 return basic_map_space_reset(bmap, type);
3935 if (type == isl_dim_div)
3936 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3937 "cannot project out existentially quantified variables",
3938 return isl_basic_map_free(bmap));
3940 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3941 if (!bmap)
3942 return NULL;
3944 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3945 return isl_basic_map_remove_dims(bmap, type, first, n);
3947 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3948 return isl_basic_map_free(bmap);
3950 bmap = move_last(bmap, type, first, n);
3951 bmap = isl_basic_map_cow(bmap);
3952 bmap = insert_div_rows(bmap, n);
3953 if (!bmap)
3954 return NULL;
3956 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3957 if (!bmap->dim)
3958 goto error;
3959 bmap = isl_basic_map_simplify(bmap);
3960 bmap = isl_basic_map_drop_redundant_divs(bmap);
3961 return isl_basic_map_finalize(bmap);
3962 error:
3963 isl_basic_map_free(bmap);
3964 return NULL;
3967 /* Turn the n dimensions of type type, starting at first
3968 * into existentially quantified variables.
3970 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3971 enum isl_dim_type type, unsigned first, unsigned n)
3973 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3974 type, first, n));
3977 /* Turn the n dimensions of type type, starting at first
3978 * into existentially quantified variables.
3980 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3981 enum isl_dim_type type, unsigned first, unsigned n)
3983 int i;
3985 if (!map)
3986 return NULL;
3988 if (n == 0)
3989 return map_space_reset(map, type);
3991 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3993 map = isl_map_cow(map);
3994 if (!map)
3995 return NULL;
3997 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3998 if (!map->dim)
3999 goto error;
4001 for (i = 0; i < map->n; ++i) {
4002 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4003 if (!map->p[i])
4004 goto error;
4007 return map;
4008 error:
4009 isl_map_free(map);
4010 return NULL;
4013 /* Turn the n dimensions of type type, starting at first
4014 * into existentially quantified variables.
4016 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4017 enum isl_dim_type type, unsigned first, unsigned n)
4019 return set_from_map(isl_map_project_out(set_to_map(set),
4020 type, first, n));
4023 /* Return a map that projects the elements in "set" onto their
4024 * "n" set dimensions starting at "first".
4025 * "type" should be equal to isl_dim_set.
4027 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4028 enum isl_dim_type type, unsigned first, unsigned n)
4030 int i;
4031 int dim;
4032 isl_map *map;
4034 if (!set)
4035 return NULL;
4036 if (type != isl_dim_set)
4037 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4038 "only set dimensions can be projected out", goto error);
4039 dim = isl_set_dim(set, isl_dim_set);
4040 if (first + n > dim || first + n < first)
4041 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4042 "index out of bounds", goto error);
4044 map = isl_map_from_domain(set);
4045 map = isl_map_add_dims(map, isl_dim_out, n);
4046 for (i = 0; i < n; ++i)
4047 map = isl_map_equate(map, isl_dim_in, first + i,
4048 isl_dim_out, i);
4049 return map;
4050 error:
4051 isl_set_free(set);
4052 return NULL;
4055 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4057 int i, j;
4059 for (i = 0; i < n; ++i) {
4060 j = isl_basic_map_alloc_div(bmap);
4061 if (j < 0)
4062 goto error;
4063 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4065 return bmap;
4066 error:
4067 isl_basic_map_free(bmap);
4068 return NULL;
4071 struct isl_basic_map *isl_basic_map_apply_range(
4072 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4074 isl_space *dim_result = NULL;
4075 struct isl_basic_map *bmap;
4076 unsigned n_in, n_out, n, nparam, total, pos;
4077 struct isl_dim_map *dim_map1, *dim_map2;
4079 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4080 goto error;
4081 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4082 bmap2->dim, isl_dim_in))
4083 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4084 "spaces don't match", goto error);
4086 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4087 isl_space_copy(bmap2->dim));
4089 n_in = isl_basic_map_n_in(bmap1);
4090 n_out = isl_basic_map_n_out(bmap2);
4091 n = isl_basic_map_n_out(bmap1);
4092 nparam = isl_basic_map_n_param(bmap1);
4094 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4095 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4096 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4097 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4098 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4099 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4100 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4101 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4102 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4103 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4104 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4106 bmap = isl_basic_map_alloc_space(dim_result,
4107 bmap1->n_div + bmap2->n_div + n,
4108 bmap1->n_eq + bmap2->n_eq,
4109 bmap1->n_ineq + bmap2->n_ineq);
4110 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4111 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4112 bmap = add_divs(bmap, n);
4113 bmap = isl_basic_map_simplify(bmap);
4114 bmap = isl_basic_map_drop_redundant_divs(bmap);
4115 return isl_basic_map_finalize(bmap);
4116 error:
4117 isl_basic_map_free(bmap1);
4118 isl_basic_map_free(bmap2);
4119 return NULL;
4122 struct isl_basic_set *isl_basic_set_apply(
4123 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4125 if (!bset || !bmap)
4126 goto error;
4128 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4129 goto error);
4131 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4132 bmap));
4133 error:
4134 isl_basic_set_free(bset);
4135 isl_basic_map_free(bmap);
4136 return NULL;
4139 struct isl_basic_map *isl_basic_map_apply_domain(
4140 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4142 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4143 goto error;
4144 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4145 bmap2->dim, isl_dim_in))
4146 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4147 "spaces don't match", goto error);
4149 bmap1 = isl_basic_map_reverse(bmap1);
4150 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4151 return isl_basic_map_reverse(bmap1);
4152 error:
4153 isl_basic_map_free(bmap1);
4154 isl_basic_map_free(bmap2);
4155 return NULL;
4158 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4159 * A \cap B -> f(A) + f(B)
4161 struct isl_basic_map *isl_basic_map_sum(
4162 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4164 unsigned n_in, n_out, nparam, total, pos;
4165 struct isl_basic_map *bmap = NULL;
4166 struct isl_dim_map *dim_map1, *dim_map2;
4167 int i;
4169 if (!bmap1 || !bmap2)
4170 goto error;
4172 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4173 goto error);
4175 nparam = isl_basic_map_n_param(bmap1);
4176 n_in = isl_basic_map_n_in(bmap1);
4177 n_out = isl_basic_map_n_out(bmap1);
4179 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4180 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4181 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4182 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4183 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4184 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4185 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4186 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4187 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4188 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4189 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4191 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4192 bmap1->n_div + bmap2->n_div + 2 * n_out,
4193 bmap1->n_eq + bmap2->n_eq + n_out,
4194 bmap1->n_ineq + bmap2->n_ineq);
4195 for (i = 0; i < n_out; ++i) {
4196 int j = isl_basic_map_alloc_equality(bmap);
4197 if (j < 0)
4198 goto error;
4199 isl_seq_clr(bmap->eq[j], 1+total);
4200 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4201 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4202 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4204 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4205 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4206 bmap = add_divs(bmap, 2 * n_out);
4208 bmap = isl_basic_map_simplify(bmap);
4209 return isl_basic_map_finalize(bmap);
4210 error:
4211 isl_basic_map_free(bmap);
4212 isl_basic_map_free(bmap1);
4213 isl_basic_map_free(bmap2);
4214 return NULL;
4217 /* Given two maps A -> f(A) and B -> g(B), construct a map
4218 * A \cap B -> f(A) + f(B)
4220 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
4222 struct isl_map *result;
4223 int i, j;
4225 if (!map1 || !map2)
4226 goto error;
4228 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4230 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4231 map1->n * map2->n, 0);
4232 if (!result)
4233 goto error;
4234 for (i = 0; i < map1->n; ++i)
4235 for (j = 0; j < map2->n; ++j) {
4236 struct isl_basic_map *part;
4237 part = isl_basic_map_sum(
4238 isl_basic_map_copy(map1->p[i]),
4239 isl_basic_map_copy(map2->p[j]));
4240 if (isl_basic_map_is_empty(part))
4241 isl_basic_map_free(part);
4242 else
4243 result = isl_map_add_basic_map(result, part);
4244 if (!result)
4245 goto error;
4247 isl_map_free(map1);
4248 isl_map_free(map2);
4249 return result;
4250 error:
4251 isl_map_free(map1);
4252 isl_map_free(map2);
4253 return NULL;
4256 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4257 __isl_take isl_set *set2)
4259 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4262 /* Given a basic map A -> f(A), construct A -> -f(A).
4264 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4266 int i, j;
4267 unsigned off, n;
4269 bmap = isl_basic_map_cow(bmap);
4270 if (!bmap)
4271 return NULL;
4273 n = isl_basic_map_dim(bmap, isl_dim_out);
4274 off = isl_basic_map_offset(bmap, isl_dim_out);
4275 for (i = 0; i < bmap->n_eq; ++i)
4276 for (j = 0; j < n; ++j)
4277 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4278 for (i = 0; i < bmap->n_ineq; ++i)
4279 for (j = 0; j < n; ++j)
4280 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4281 for (i = 0; i < bmap->n_div; ++i)
4282 for (j = 0; j < n; ++j)
4283 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4284 bmap = isl_basic_map_gauss(bmap, NULL);
4285 return isl_basic_map_finalize(bmap);
4288 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4290 return isl_basic_map_neg(bset);
4293 /* Given a map A -> f(A), construct A -> -f(A).
4295 struct isl_map *isl_map_neg(struct isl_map *map)
4297 int i;
4299 map = isl_map_cow(map);
4300 if (!map)
4301 return NULL;
4303 for (i = 0; i < map->n; ++i) {
4304 map->p[i] = isl_basic_map_neg(map->p[i]);
4305 if (!map->p[i])
4306 goto error;
4309 return map;
4310 error:
4311 isl_map_free(map);
4312 return NULL;
4315 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4317 return set_from_map(isl_map_neg(set_to_map(set)));
4320 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4321 * A -> floor(f(A)/d).
4323 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4324 isl_int d)
4326 unsigned n_in, n_out, nparam, total, pos;
4327 struct isl_basic_map *result = NULL;
4328 struct isl_dim_map *dim_map;
4329 int i;
4331 if (!bmap)
4332 return NULL;
4334 nparam = isl_basic_map_n_param(bmap);
4335 n_in = isl_basic_map_n_in(bmap);
4336 n_out = isl_basic_map_n_out(bmap);
4338 total = nparam + n_in + n_out + bmap->n_div + n_out;
4339 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4340 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4341 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4342 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4343 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4345 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4346 bmap->n_div + n_out,
4347 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4348 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4349 result = add_divs(result, n_out);
4350 for (i = 0; i < n_out; ++i) {
4351 int j;
4352 j = isl_basic_map_alloc_inequality(result);
4353 if (j < 0)
4354 goto error;
4355 isl_seq_clr(result->ineq[j], 1+total);
4356 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4357 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4358 j = isl_basic_map_alloc_inequality(result);
4359 if (j < 0)
4360 goto error;
4361 isl_seq_clr(result->ineq[j], 1+total);
4362 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4363 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4364 isl_int_sub_ui(result->ineq[j][0], d, 1);
4367 result = isl_basic_map_simplify(result);
4368 return isl_basic_map_finalize(result);
4369 error:
4370 isl_basic_map_free(result);
4371 return NULL;
4374 /* Given a map A -> f(A) and an integer d, construct a map
4375 * A -> floor(f(A)/d).
4377 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4379 int i;
4381 map = isl_map_cow(map);
4382 if (!map)
4383 return NULL;
4385 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4386 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4387 for (i = 0; i < map->n; ++i) {
4388 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4389 if (!map->p[i])
4390 goto error;
4393 return map;
4394 error:
4395 isl_map_free(map);
4396 return NULL;
4399 /* Given a map A -> f(A) and an integer d, construct a map
4400 * A -> floor(f(A)/d).
4402 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4403 __isl_take isl_val *d)
4405 if (!map || !d)
4406 goto error;
4407 if (!isl_val_is_int(d))
4408 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4409 "expecting integer denominator", goto error);
4410 map = isl_map_floordiv(map, d->n);
4411 isl_val_free(d);
4412 return map;
4413 error:
4414 isl_map_free(map);
4415 isl_val_free(d);
4416 return NULL;
4419 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4421 int i;
4422 unsigned nparam;
4423 unsigned n_in;
4425 i = isl_basic_map_alloc_equality(bmap);
4426 if (i < 0)
4427 goto error;
4428 nparam = isl_basic_map_n_param(bmap);
4429 n_in = isl_basic_map_n_in(bmap);
4430 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4431 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4432 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4433 return isl_basic_map_finalize(bmap);
4434 error:
4435 isl_basic_map_free(bmap);
4436 return NULL;
4439 /* Add a constraint to "bmap" expressing i_pos < o_pos
4441 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4443 int i;
4444 unsigned nparam;
4445 unsigned n_in;
4447 i = isl_basic_map_alloc_inequality(bmap);
4448 if (i < 0)
4449 goto error;
4450 nparam = isl_basic_map_n_param(bmap);
4451 n_in = isl_basic_map_n_in(bmap);
4452 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4453 isl_int_set_si(bmap->ineq[i][0], -1);
4454 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4455 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4456 return isl_basic_map_finalize(bmap);
4457 error:
4458 isl_basic_map_free(bmap);
4459 return NULL;
4462 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4464 static __isl_give isl_basic_map *var_less_or_equal(
4465 __isl_take isl_basic_map *bmap, unsigned pos)
4467 int i;
4468 unsigned nparam;
4469 unsigned n_in;
4471 i = isl_basic_map_alloc_inequality(bmap);
4472 if (i < 0)
4473 goto error;
4474 nparam = isl_basic_map_n_param(bmap);
4475 n_in = isl_basic_map_n_in(bmap);
4476 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4477 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4478 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4479 return isl_basic_map_finalize(bmap);
4480 error:
4481 isl_basic_map_free(bmap);
4482 return NULL;
4485 /* Add a constraint to "bmap" expressing i_pos > o_pos
4487 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4489 int i;
4490 unsigned nparam;
4491 unsigned n_in;
4493 i = isl_basic_map_alloc_inequality(bmap);
4494 if (i < 0)
4495 goto error;
4496 nparam = isl_basic_map_n_param(bmap);
4497 n_in = isl_basic_map_n_in(bmap);
4498 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4499 isl_int_set_si(bmap->ineq[i][0], -1);
4500 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4501 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4502 return isl_basic_map_finalize(bmap);
4503 error:
4504 isl_basic_map_free(bmap);
4505 return NULL;
4508 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4510 static __isl_give isl_basic_map *var_more_or_equal(
4511 __isl_take isl_basic_map *bmap, unsigned pos)
4513 int i;
4514 unsigned nparam;
4515 unsigned n_in;
4517 i = isl_basic_map_alloc_inequality(bmap);
4518 if (i < 0)
4519 goto error;
4520 nparam = isl_basic_map_n_param(bmap);
4521 n_in = isl_basic_map_n_in(bmap);
4522 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4523 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4524 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4525 return isl_basic_map_finalize(bmap);
4526 error:
4527 isl_basic_map_free(bmap);
4528 return NULL;
4531 __isl_give isl_basic_map *isl_basic_map_equal(
4532 __isl_take isl_space *dim, unsigned n_equal)
4534 int i;
4535 struct isl_basic_map *bmap;
4536 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4537 if (!bmap)
4538 return NULL;
4539 for (i = 0; i < n_equal && bmap; ++i)
4540 bmap = var_equal(bmap, i);
4541 return isl_basic_map_finalize(bmap);
4544 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4546 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4547 unsigned pos)
4549 int i;
4550 struct isl_basic_map *bmap;
4551 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4552 if (!bmap)
4553 return NULL;
4554 for (i = 0; i < pos && bmap; ++i)
4555 bmap = var_equal(bmap, i);
4556 if (bmap)
4557 bmap = var_less(bmap, pos);
4558 return isl_basic_map_finalize(bmap);
4561 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4563 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4564 __isl_take isl_space *dim, unsigned pos)
4566 int i;
4567 isl_basic_map *bmap;
4569 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4570 for (i = 0; i < pos; ++i)
4571 bmap = var_equal(bmap, i);
4572 bmap = var_less_or_equal(bmap, pos);
4573 return isl_basic_map_finalize(bmap);
4576 /* Return a relation on "dim" expressing i_pos > o_pos
4578 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4579 unsigned pos)
4581 int i;
4582 struct isl_basic_map *bmap;
4583 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4584 if (!bmap)
4585 return NULL;
4586 for (i = 0; i < pos && bmap; ++i)
4587 bmap = var_equal(bmap, i);
4588 if (bmap)
4589 bmap = var_more(bmap, pos);
4590 return isl_basic_map_finalize(bmap);
4593 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4595 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4596 __isl_take isl_space *dim, unsigned pos)
4598 int i;
4599 isl_basic_map *bmap;
4601 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4602 for (i = 0; i < pos; ++i)
4603 bmap = var_equal(bmap, i);
4604 bmap = var_more_or_equal(bmap, pos);
4605 return isl_basic_map_finalize(bmap);
4608 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4609 unsigned n, int equal)
4611 struct isl_map *map;
4612 int i;
4614 if (n == 0 && equal)
4615 return isl_map_universe(dims);
4617 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4619 for (i = 0; i + 1 < n; ++i)
4620 map = isl_map_add_basic_map(map,
4621 isl_basic_map_less_at(isl_space_copy(dims), i));
4622 if (n > 0) {
4623 if (equal)
4624 map = isl_map_add_basic_map(map,
4625 isl_basic_map_less_or_equal_at(dims, n - 1));
4626 else
4627 map = isl_map_add_basic_map(map,
4628 isl_basic_map_less_at(dims, n - 1));
4629 } else
4630 isl_space_free(dims);
4632 return map;
4635 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4637 if (!dims)
4638 return NULL;
4639 return map_lex_lte_first(dims, dims->n_out, equal);
4642 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4644 return map_lex_lte_first(dim, n, 0);
4647 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4649 return map_lex_lte_first(dim, n, 1);
4652 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4654 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4657 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4659 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4662 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4663 unsigned n, int equal)
4665 struct isl_map *map;
4666 int i;
4668 if (n == 0 && equal)
4669 return isl_map_universe(dims);
4671 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4673 for (i = 0; i + 1 < n; ++i)
4674 map = isl_map_add_basic_map(map,
4675 isl_basic_map_more_at(isl_space_copy(dims), i));
4676 if (n > 0) {
4677 if (equal)
4678 map = isl_map_add_basic_map(map,
4679 isl_basic_map_more_or_equal_at(dims, n - 1));
4680 else
4681 map = isl_map_add_basic_map(map,
4682 isl_basic_map_more_at(dims, n - 1));
4683 } else
4684 isl_space_free(dims);
4686 return map;
4689 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4691 if (!dims)
4692 return NULL;
4693 return map_lex_gte_first(dims, dims->n_out, equal);
4696 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4698 return map_lex_gte_first(dim, n, 0);
4701 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4703 return map_lex_gte_first(dim, n, 1);
4706 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4708 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4711 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4713 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4716 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4717 __isl_take isl_set *set2)
4719 isl_map *map;
4720 map = isl_map_lex_le(isl_set_get_space(set1));
4721 map = isl_map_intersect_domain(map, set1);
4722 map = isl_map_intersect_range(map, set2);
4723 return map;
4726 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4727 __isl_take isl_set *set2)
4729 isl_map *map;
4730 map = isl_map_lex_lt(isl_set_get_space(set1));
4731 map = isl_map_intersect_domain(map, set1);
4732 map = isl_map_intersect_range(map, set2);
4733 return map;
4736 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4737 __isl_take isl_set *set2)
4739 isl_map *map;
4740 map = isl_map_lex_ge(isl_set_get_space(set1));
4741 map = isl_map_intersect_domain(map, set1);
4742 map = isl_map_intersect_range(map, set2);
4743 return map;
4746 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4747 __isl_take isl_set *set2)
4749 isl_map *map;
4750 map = isl_map_lex_gt(isl_set_get_space(set1));
4751 map = isl_map_intersect_domain(map, set1);
4752 map = isl_map_intersect_range(map, set2);
4753 return map;
4756 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4757 __isl_take isl_map *map2)
4759 isl_map *map;
4760 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4761 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4762 map = isl_map_apply_range(map, isl_map_reverse(map2));
4763 return map;
4766 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4767 __isl_take isl_map *map2)
4769 isl_map *map;
4770 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4771 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4772 map = isl_map_apply_range(map, isl_map_reverse(map2));
4773 return map;
4776 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4777 __isl_take isl_map *map2)
4779 isl_map *map;
4780 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4781 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4782 map = isl_map_apply_range(map, isl_map_reverse(map2));
4783 return map;
4786 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4787 __isl_take isl_map *map2)
4789 isl_map *map;
4790 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4791 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4792 map = isl_map_apply_range(map, isl_map_reverse(map2));
4793 return map;
4796 /* For a div d = floor(f/m), add the constraint
4798 * f - m d >= 0
4800 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4801 unsigned pos, isl_int *div)
4803 int i;
4804 unsigned total = isl_basic_map_total_dim(bmap);
4806 i = isl_basic_map_alloc_inequality(bmap);
4807 if (i < 0)
4808 return isl_stat_error;
4809 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4810 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4812 return isl_stat_ok;
4815 /* For a div d = floor(f/m), add the constraint
4817 * -(f-(m-1)) + m d >= 0
4819 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4820 unsigned pos, isl_int *div)
4822 int i;
4823 unsigned total = isl_basic_map_total_dim(bmap);
4825 i = isl_basic_map_alloc_inequality(bmap);
4826 if (i < 0)
4827 return isl_stat_error;
4828 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4829 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4830 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4831 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4833 return isl_stat_ok;
4836 /* For a div d = floor(f/m), add the constraints
4838 * f - m d >= 0
4839 * -(f-(m-1)) + m d >= 0
4841 * Note that the second constraint is the negation of
4843 * f - m d >= m
4845 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4846 unsigned pos, isl_int *div)
4848 if (add_upper_div_constraint(bmap, pos, div) < 0)
4849 return -1;
4850 if (add_lower_div_constraint(bmap, pos, div) < 0)
4851 return -1;
4852 return 0;
4855 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4856 unsigned pos, isl_int *div)
4858 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4859 pos, div);
4862 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4864 unsigned total = isl_basic_map_total_dim(bmap);
4865 unsigned div_pos = total - bmap->n_div + div;
4867 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4868 bmap->div[div]);
4871 /* For each known div d = floor(f/m), add the constraints
4873 * f - m d >= 0
4874 * -(f-(m-1)) + m d >= 0
4876 * Remove duplicate constraints in case of some these div constraints
4877 * already appear in "bmap".
4879 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4880 __isl_take isl_basic_map *bmap)
4882 unsigned n_div;
4884 if (!bmap)
4885 return NULL;
4886 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4887 if (n_div == 0)
4888 return bmap;
4890 bmap = add_known_div_constraints(bmap);
4891 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4892 bmap = isl_basic_map_finalize(bmap);
4893 return bmap;
4896 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4898 * In particular, if this div is of the form d = floor(f/m),
4899 * then add the constraint
4901 * f - m d >= 0
4903 * if sign < 0 or the constraint
4905 * -(f-(m-1)) + m d >= 0
4907 * if sign > 0.
4909 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4910 unsigned div, int sign)
4912 unsigned total;
4913 unsigned div_pos;
4915 if (!bmap)
4916 return -1;
4918 total = isl_basic_map_total_dim(bmap);
4919 div_pos = total - bmap->n_div + div;
4921 if (sign < 0)
4922 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4923 else
4924 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4927 struct isl_basic_set *isl_basic_map_underlying_set(
4928 struct isl_basic_map *bmap)
4930 if (!bmap)
4931 goto error;
4932 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4933 bmap->n_div == 0 &&
4934 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4935 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4936 return bset_from_bmap(bmap);
4937 bmap = isl_basic_map_cow(bmap);
4938 if (!bmap)
4939 goto error;
4940 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4941 if (!bmap->dim)
4942 goto error;
4943 bmap->extra -= bmap->n_div;
4944 bmap->n_div = 0;
4945 bmap = isl_basic_map_finalize(bmap);
4946 return bset_from_bmap(bmap);
4947 error:
4948 isl_basic_map_free(bmap);
4949 return NULL;
4952 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4953 __isl_take isl_basic_set *bset)
4955 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4958 /* Replace each element in "list" by the result of applying
4959 * isl_basic_map_underlying_set to the element.
4961 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4962 __isl_take isl_basic_map_list *list)
4964 int i, n;
4966 if (!list)
4967 return NULL;
4969 n = isl_basic_map_list_n_basic_map(list);
4970 for (i = 0; i < n; ++i) {
4971 isl_basic_map *bmap;
4972 isl_basic_set *bset;
4974 bmap = isl_basic_map_list_get_basic_map(list, i);
4975 bset = isl_basic_set_underlying_set(bmap);
4976 list = isl_basic_set_list_set_basic_set(list, i, bset);
4979 return list;
4982 struct isl_basic_map *isl_basic_map_overlying_set(
4983 struct isl_basic_set *bset, struct isl_basic_map *like)
4985 struct isl_basic_map *bmap;
4986 struct isl_ctx *ctx;
4987 unsigned total;
4988 int i;
4990 if (!bset || !like)
4991 goto error;
4992 ctx = bset->ctx;
4993 isl_assert(ctx, bset->n_div == 0, goto error);
4994 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4995 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4996 goto error);
4997 if (like->n_div == 0) {
4998 isl_space *space = isl_basic_map_get_space(like);
4999 isl_basic_map_free(like);
5000 return isl_basic_map_reset_space(bset, space);
5002 bset = isl_basic_set_cow(bset);
5003 if (!bset)
5004 goto error;
5005 total = bset->dim->n_out + bset->extra;
5006 bmap = bset_to_bmap(bset);
5007 isl_space_free(bmap->dim);
5008 bmap->dim = isl_space_copy(like->dim);
5009 if (!bmap->dim)
5010 goto error;
5011 bmap->n_div = like->n_div;
5012 bmap->extra += like->n_div;
5013 if (bmap->extra) {
5014 unsigned ltotal;
5015 isl_int **div;
5016 ltotal = total - bmap->extra + like->extra;
5017 if (ltotal > total)
5018 ltotal = total;
5019 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5020 bmap->extra * (1 + 1 + total));
5021 if (isl_blk_is_error(bmap->block2))
5022 goto error;
5023 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5024 if (!div)
5025 goto error;
5026 bmap->div = div;
5027 for (i = 0; i < bmap->extra; ++i)
5028 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5029 for (i = 0; i < like->n_div; ++i) {
5030 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5031 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5033 bmap = isl_basic_map_add_known_div_constraints(bmap);
5035 isl_basic_map_free(like);
5036 bmap = isl_basic_map_simplify(bmap);
5037 bmap = isl_basic_map_finalize(bmap);
5038 return bmap;
5039 error:
5040 isl_basic_map_free(like);
5041 isl_basic_set_free(bset);
5042 return NULL;
5045 struct isl_basic_set *isl_basic_set_from_underlying_set(
5046 struct isl_basic_set *bset, struct isl_basic_set *like)
5048 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5049 bset_to_bmap(like)));
5052 struct isl_set *isl_map_underlying_set(struct isl_map *map)
5054 int i;
5056 map = isl_map_cow(map);
5057 if (!map)
5058 return NULL;
5059 map->dim = isl_space_cow(map->dim);
5060 if (!map->dim)
5061 goto error;
5063 for (i = 1; i < map->n; ++i)
5064 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5065 goto error);
5066 for (i = 0; i < map->n; ++i) {
5067 map->p[i] = bset_to_bmap(
5068 isl_basic_map_underlying_set(map->p[i]));
5069 if (!map->p[i])
5070 goto error;
5072 if (map->n == 0)
5073 map->dim = isl_space_underlying(map->dim, 0);
5074 else {
5075 isl_space_free(map->dim);
5076 map->dim = isl_space_copy(map->p[0]->dim);
5078 if (!map->dim)
5079 goto error;
5080 return set_from_map(map);
5081 error:
5082 isl_map_free(map);
5083 return NULL;
5086 /* Replace the space of "bmap" by "space".
5088 * If the space of "bmap" is identical to "space" (including the identifiers
5089 * of the input and output dimensions), then simply return the original input.
5091 __isl_give isl_basic_map *isl_basic_map_reset_space(
5092 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5094 isl_bool equal;
5095 isl_space *bmap_space;
5097 bmap_space = isl_basic_map_peek_space(bmap);
5098 equal = isl_space_is_equal(bmap_space, space);
5099 if (equal >= 0 && equal)
5100 equal = isl_space_match(bmap_space, isl_dim_in,
5101 space, isl_dim_in);
5102 if (equal >= 0 && equal)
5103 equal = isl_space_match(bmap_space, isl_dim_out,
5104 space, isl_dim_out);
5105 if (equal < 0)
5106 goto error;
5107 if (equal) {
5108 isl_space_free(space);
5109 return bmap;
5111 bmap = isl_basic_map_cow(bmap);
5112 if (!bmap || !space)
5113 goto error;
5115 isl_space_free(bmap->dim);
5116 bmap->dim = space;
5118 bmap = isl_basic_map_finalize(bmap);
5120 return bmap;
5121 error:
5122 isl_basic_map_free(bmap);
5123 isl_space_free(space);
5124 return NULL;
5127 __isl_give isl_basic_set *isl_basic_set_reset_space(
5128 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5130 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5131 dim));
5134 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5135 __isl_take isl_space *dim)
5137 int i;
5139 map = isl_map_cow(map);
5140 if (!map || !dim)
5141 goto error;
5143 for (i = 0; i < map->n; ++i) {
5144 map->p[i] = isl_basic_map_reset_space(map->p[i],
5145 isl_space_copy(dim));
5146 if (!map->p[i])
5147 goto error;
5149 isl_space_free(map->dim);
5150 map->dim = dim;
5152 return map;
5153 error:
5154 isl_map_free(map);
5155 isl_space_free(dim);
5156 return NULL;
5159 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5160 __isl_take isl_space *dim)
5162 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5165 /* Compute the parameter domain of the given basic set.
5167 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5169 isl_bool is_params;
5170 isl_space *space;
5171 unsigned n;
5173 is_params = isl_basic_set_is_params(bset);
5174 if (is_params < 0)
5175 return isl_basic_set_free(bset);
5176 if (is_params)
5177 return bset;
5179 n = isl_basic_set_dim(bset, isl_dim_set);
5180 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5181 space = isl_basic_set_get_space(bset);
5182 space = isl_space_params(space);
5183 bset = isl_basic_set_reset_space(bset, space);
5184 return bset;
5187 /* Construct a zero-dimensional basic set with the given parameter domain.
5189 __isl_give isl_basic_set *isl_basic_set_from_params(
5190 __isl_take isl_basic_set *bset)
5192 isl_space *space;
5193 space = isl_basic_set_get_space(bset);
5194 space = isl_space_set_from_params(space);
5195 bset = isl_basic_set_reset_space(bset, space);
5196 return bset;
5199 /* Compute the parameter domain of the given set.
5201 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5203 isl_space *space;
5204 unsigned n;
5206 if (isl_set_is_params(set))
5207 return set;
5209 n = isl_set_dim(set, isl_dim_set);
5210 set = isl_set_project_out(set, isl_dim_set, 0, n);
5211 space = isl_set_get_space(set);
5212 space = isl_space_params(space);
5213 set = isl_set_reset_space(set, space);
5214 return set;
5217 /* Construct a zero-dimensional set with the given parameter domain.
5219 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5221 isl_space *space;
5222 space = isl_set_get_space(set);
5223 space = isl_space_set_from_params(space);
5224 set = isl_set_reset_space(set, space);
5225 return set;
5228 /* Compute the parameter domain of the given map.
5230 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5232 isl_space *space;
5233 unsigned n;
5235 n = isl_map_dim(map, isl_dim_in);
5236 map = isl_map_project_out(map, isl_dim_in, 0, n);
5237 n = isl_map_dim(map, isl_dim_out);
5238 map = isl_map_project_out(map, isl_dim_out, 0, n);
5239 space = isl_map_get_space(map);
5240 space = isl_space_params(space);
5241 map = isl_map_reset_space(map, space);
5242 return map;
5245 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5247 isl_space *space;
5248 unsigned n_out;
5250 if (!bmap)
5251 return NULL;
5252 space = isl_space_domain(isl_basic_map_get_space(bmap));
5254 n_out = isl_basic_map_n_out(bmap);
5255 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5257 return isl_basic_map_reset_space(bmap, space);
5260 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5262 if (!bmap)
5263 return isl_bool_error;
5264 return isl_space_may_be_set(bmap->dim);
5267 /* Is this basic map actually a set?
5268 * Users should never call this function. Outside of isl,
5269 * the type should indicate whether something is a set or a map.
5271 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5273 if (!bmap)
5274 return isl_bool_error;
5275 return isl_space_is_set(bmap->dim);
5278 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5280 isl_bool is_set;
5282 is_set = isl_basic_map_is_set(bmap);
5283 if (is_set < 0)
5284 goto error;
5285 if (is_set)
5286 return bmap;
5287 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5288 error:
5289 isl_basic_map_free(bmap);
5290 return NULL;
5293 __isl_give isl_basic_map *isl_basic_map_domain_map(
5294 __isl_take isl_basic_map *bmap)
5296 int i;
5297 isl_space *dim;
5298 isl_basic_map *domain;
5299 int nparam, n_in, n_out;
5301 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5302 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5303 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5305 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5306 domain = isl_basic_map_universe(dim);
5308 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5309 bmap = isl_basic_map_apply_range(bmap, domain);
5310 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5312 for (i = 0; i < n_in; ++i)
5313 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5314 isl_dim_out, i);
5316 bmap = isl_basic_map_gauss(bmap, NULL);
5317 return isl_basic_map_finalize(bmap);
5320 __isl_give isl_basic_map *isl_basic_map_range_map(
5321 __isl_take isl_basic_map *bmap)
5323 int i;
5324 isl_space *dim;
5325 isl_basic_map *range;
5326 int nparam, n_in, n_out;
5328 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5329 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5330 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5332 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5333 range = isl_basic_map_universe(dim);
5335 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5336 bmap = isl_basic_map_apply_range(bmap, range);
5337 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5339 for (i = 0; i < n_out; ++i)
5340 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5341 isl_dim_out, i);
5343 bmap = isl_basic_map_gauss(bmap, NULL);
5344 return isl_basic_map_finalize(bmap);
5347 int isl_map_may_be_set(__isl_keep isl_map *map)
5349 if (!map)
5350 return -1;
5351 return isl_space_may_be_set(map->dim);
5354 /* Is this map actually a set?
5355 * Users should never call this function. Outside of isl,
5356 * the type should indicate whether something is a set or a map.
5358 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5360 if (!map)
5361 return isl_bool_error;
5362 return isl_space_is_set(map->dim);
5365 struct isl_set *isl_map_range(struct isl_map *map)
5367 int i;
5368 isl_bool is_set;
5369 struct isl_set *set;
5371 is_set = isl_map_is_set(map);
5372 if (is_set < 0)
5373 goto error;
5374 if (is_set)
5375 return set_from_map(map);
5377 map = isl_map_cow(map);
5378 if (!map)
5379 goto error;
5381 set = set_from_map(map);
5382 set->dim = isl_space_range(set->dim);
5383 if (!set->dim)
5384 goto error;
5385 for (i = 0; i < map->n; ++i) {
5386 set->p[i] = isl_basic_map_range(map->p[i]);
5387 if (!set->p[i])
5388 goto error;
5390 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5391 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5392 return set;
5393 error:
5394 isl_map_free(map);
5395 return NULL;
5398 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5400 int i;
5402 map = isl_map_cow(map);
5403 if (!map)
5404 return NULL;
5406 map->dim = isl_space_domain_map(map->dim);
5407 if (!map->dim)
5408 goto error;
5409 for (i = 0; i < map->n; ++i) {
5410 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5411 if (!map->p[i])
5412 goto error;
5414 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5415 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5416 return map;
5417 error:
5418 isl_map_free(map);
5419 return NULL;
5422 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5424 int i;
5425 isl_space *range_dim;
5427 map = isl_map_cow(map);
5428 if (!map)
5429 return NULL;
5431 range_dim = isl_space_range(isl_map_get_space(map));
5432 range_dim = isl_space_from_range(range_dim);
5433 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5434 map->dim = isl_space_join(map->dim, range_dim);
5435 if (!map->dim)
5436 goto error;
5437 for (i = 0; i < map->n; ++i) {
5438 map->p[i] = isl_basic_map_range_map(map->p[i]);
5439 if (!map->p[i])
5440 goto error;
5442 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5443 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5444 return map;
5445 error:
5446 isl_map_free(map);
5447 return NULL;
5450 /* Given a wrapped map of the form A[B -> C],
5451 * return the map A[B -> C] -> B.
5453 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5455 isl_id *id;
5456 isl_map *map;
5458 if (!set)
5459 return NULL;
5460 if (!isl_set_has_tuple_id(set))
5461 return isl_map_domain_map(isl_set_unwrap(set));
5463 id = isl_set_get_tuple_id(set);
5464 map = isl_map_domain_map(isl_set_unwrap(set));
5465 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5467 return map;
5470 __isl_give isl_basic_map *isl_basic_map_from_domain(
5471 __isl_take isl_basic_set *bset)
5473 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5476 __isl_give isl_basic_map *isl_basic_map_from_range(
5477 __isl_take isl_basic_set *bset)
5479 isl_space *space;
5480 space = isl_basic_set_get_space(bset);
5481 space = isl_space_from_range(space);
5482 bset = isl_basic_set_reset_space(bset, space);
5483 return bset_to_bmap(bset);
5486 /* Create a relation with the given set as range.
5487 * The domain of the created relation is a zero-dimensional
5488 * flat anonymous space.
5490 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5492 isl_space *space;
5493 space = isl_set_get_space(set);
5494 space = isl_space_from_range(space);
5495 set = isl_set_reset_space(set, space);
5496 return set_to_map(set);
5499 /* Create a relation with the given set as domain.
5500 * The range of the created relation is a zero-dimensional
5501 * flat anonymous space.
5503 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5505 return isl_map_reverse(isl_map_from_range(set));
5508 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5509 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5511 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5514 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5515 __isl_take isl_set *range)
5517 return isl_map_apply_range(isl_map_reverse(domain), range);
5520 /* Return a newly allocated isl_map with given space and flags and
5521 * room for "n" basic maps.
5522 * Make sure that all cached information is cleared.
5524 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5525 unsigned flags)
5527 struct isl_map *map;
5529 if (!space)
5530 return NULL;
5531 if (n < 0)
5532 isl_die(space->ctx, isl_error_internal,
5533 "negative number of basic maps", goto error);
5534 map = isl_calloc(space->ctx, struct isl_map,
5535 sizeof(struct isl_map) +
5536 (n - 1) * sizeof(struct isl_basic_map *));
5537 if (!map)
5538 goto error;
5540 map->ctx = space->ctx;
5541 isl_ctx_ref(map->ctx);
5542 map->ref = 1;
5543 map->size = n;
5544 map->n = 0;
5545 map->dim = space;
5546 map->flags = flags;
5547 return map;
5548 error:
5549 isl_space_free(space);
5550 return NULL;
5553 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5555 struct isl_basic_map *bmap;
5556 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5557 bmap = isl_basic_map_set_to_empty(bmap);
5558 return bmap;
5561 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5563 struct isl_basic_set *bset;
5564 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5565 bset = isl_basic_set_set_to_empty(bset);
5566 return bset;
5569 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5571 struct isl_basic_map *bmap;
5572 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5573 bmap = isl_basic_map_finalize(bmap);
5574 return bmap;
5577 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5579 struct isl_basic_set *bset;
5580 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5581 bset = isl_basic_set_finalize(bset);
5582 return bset;
5585 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5587 int i;
5588 unsigned total = isl_space_dim(dim, isl_dim_all);
5589 isl_basic_map *bmap;
5591 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5592 for (i = 0; i < total; ++i) {
5593 int k = isl_basic_map_alloc_inequality(bmap);
5594 if (k < 0)
5595 goto error;
5596 isl_seq_clr(bmap->ineq[k], 1 + total);
5597 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5599 return bmap;
5600 error:
5601 isl_basic_map_free(bmap);
5602 return NULL;
5605 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5607 return isl_basic_map_nat_universe(dim);
5610 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5612 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5615 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5617 return isl_map_nat_universe(dim);
5620 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5622 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5625 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5627 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5630 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5632 struct isl_map *map;
5633 if (!dim)
5634 return NULL;
5635 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5636 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5637 return map;
5640 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5642 struct isl_set *set;
5643 if (!dim)
5644 return NULL;
5645 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5646 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5647 return set;
5650 struct isl_map *isl_map_dup(struct isl_map *map)
5652 int i;
5653 struct isl_map *dup;
5655 if (!map)
5656 return NULL;
5657 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5658 for (i = 0; i < map->n; ++i)
5659 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5660 return dup;
5663 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5664 __isl_take isl_basic_map *bmap)
5666 if (!bmap || !map)
5667 goto error;
5668 if (isl_basic_map_plain_is_empty(bmap)) {
5669 isl_basic_map_free(bmap);
5670 return map;
5672 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5673 isl_assert(map->ctx, map->n < map->size, goto error);
5674 map->p[map->n] = bmap;
5675 map->n++;
5676 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5677 return map;
5678 error:
5679 if (map)
5680 isl_map_free(map);
5681 if (bmap)
5682 isl_basic_map_free(bmap);
5683 return NULL;
5686 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5688 int i;
5690 if (!map)
5691 return NULL;
5693 if (--map->ref > 0)
5694 return NULL;
5696 clear_caches(map);
5697 isl_ctx_deref(map->ctx);
5698 for (i = 0; i < map->n; ++i)
5699 isl_basic_map_free(map->p[i]);
5700 isl_space_free(map->dim);
5701 free(map);
5703 return NULL;
5706 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5707 struct isl_basic_map *bmap, unsigned pos, int value)
5709 int j;
5711 bmap = isl_basic_map_cow(bmap);
5712 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5713 j = isl_basic_map_alloc_equality(bmap);
5714 if (j < 0)
5715 goto error;
5716 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5717 isl_int_set_si(bmap->eq[j][pos], -1);
5718 isl_int_set_si(bmap->eq[j][0], value);
5719 bmap = isl_basic_map_simplify(bmap);
5720 return isl_basic_map_finalize(bmap);
5721 error:
5722 isl_basic_map_free(bmap);
5723 return NULL;
5726 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5727 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5729 int j;
5731 bmap = isl_basic_map_cow(bmap);
5732 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5733 j = isl_basic_map_alloc_equality(bmap);
5734 if (j < 0)
5735 goto error;
5736 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5737 isl_int_set_si(bmap->eq[j][pos], -1);
5738 isl_int_set(bmap->eq[j][0], value);
5739 bmap = isl_basic_map_simplify(bmap);
5740 return isl_basic_map_finalize(bmap);
5741 error:
5742 isl_basic_map_free(bmap);
5743 return NULL;
5746 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5747 enum isl_dim_type type, unsigned pos, int value)
5749 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5750 return isl_basic_map_free(bmap);
5751 return isl_basic_map_fix_pos_si(bmap,
5752 isl_basic_map_offset(bmap, type) + pos, value);
5755 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5756 enum isl_dim_type type, unsigned pos, isl_int value)
5758 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5759 return isl_basic_map_free(bmap);
5760 return isl_basic_map_fix_pos(bmap,
5761 isl_basic_map_offset(bmap, type) + pos, value);
5764 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5765 * to be equal to "v".
5767 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5768 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5770 if (!bmap || !v)
5771 goto error;
5772 if (!isl_val_is_int(v))
5773 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5774 "expecting integer value", goto error);
5775 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5776 goto error;
5777 pos += isl_basic_map_offset(bmap, type);
5778 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5779 isl_val_free(v);
5780 return bmap;
5781 error:
5782 isl_basic_map_free(bmap);
5783 isl_val_free(v);
5784 return NULL;
5787 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5788 * to be equal to "v".
5790 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5791 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5793 return isl_basic_map_fix_val(bset, type, pos, v);
5796 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5797 enum isl_dim_type type, unsigned pos, int value)
5799 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5800 type, pos, value));
5803 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5804 enum isl_dim_type type, unsigned pos, isl_int value)
5806 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5807 type, pos, value));
5810 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5811 unsigned input, int value)
5813 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5816 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5817 unsigned dim, int value)
5819 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5820 isl_dim_set, dim, value));
5823 static int remove_if_empty(__isl_keep isl_map *map, int i)
5825 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5827 if (empty < 0)
5828 return -1;
5829 if (!empty)
5830 return 0;
5832 isl_basic_map_free(map->p[i]);
5833 if (i != map->n - 1) {
5834 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5835 map->p[i] = map->p[map->n - 1];
5837 map->n--;
5839 return 0;
5842 /* Perform "fn" on each basic map of "map", where we may not be holding
5843 * the only reference to "map".
5844 * In particular, "fn" should be a semantics preserving operation
5845 * that we want to apply to all copies of "map". We therefore need
5846 * to be careful not to modify "map" in a way that breaks "map"
5847 * in case anything goes wrong.
5849 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5850 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5852 struct isl_basic_map *bmap;
5853 int i;
5855 if (!map)
5856 return NULL;
5858 for (i = map->n - 1; i >= 0; --i) {
5859 bmap = isl_basic_map_copy(map->p[i]);
5860 bmap = fn(bmap);
5861 if (!bmap)
5862 goto error;
5863 isl_basic_map_free(map->p[i]);
5864 map->p[i] = bmap;
5865 if (remove_if_empty(map, i) < 0)
5866 goto error;
5869 return map;
5870 error:
5871 isl_map_free(map);
5872 return NULL;
5875 struct isl_map *isl_map_fix_si(struct isl_map *map,
5876 enum isl_dim_type type, unsigned pos, int value)
5878 int i;
5880 map = isl_map_cow(map);
5881 if (!map)
5882 return NULL;
5884 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5885 for (i = map->n - 1; i >= 0; --i) {
5886 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5887 if (remove_if_empty(map, i) < 0)
5888 goto error;
5890 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5891 return map;
5892 error:
5893 isl_map_free(map);
5894 return NULL;
5897 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5898 enum isl_dim_type type, unsigned pos, int value)
5900 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
5903 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5904 enum isl_dim_type type, unsigned pos, isl_int value)
5906 int i;
5908 map = isl_map_cow(map);
5909 if (!map)
5910 return NULL;
5912 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5913 for (i = 0; i < map->n; ++i) {
5914 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5915 if (!map->p[i])
5916 goto error;
5918 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5919 return map;
5920 error:
5921 isl_map_free(map);
5922 return NULL;
5925 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5926 enum isl_dim_type type, unsigned pos, isl_int value)
5928 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
5931 /* Fix the value of the variable at position "pos" of type "type" of "map"
5932 * to be equal to "v".
5934 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5935 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5937 int i;
5939 map = isl_map_cow(map);
5940 if (!map || !v)
5941 goto error;
5943 if (!isl_val_is_int(v))
5944 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5945 "expecting integer value", goto error);
5946 if (pos >= isl_map_dim(map, type))
5947 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5948 "index out of bounds", goto error);
5949 for (i = map->n - 1; i >= 0; --i) {
5950 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5951 isl_val_copy(v));
5952 if (remove_if_empty(map, i) < 0)
5953 goto error;
5955 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5956 isl_val_free(v);
5957 return map;
5958 error:
5959 isl_map_free(map);
5960 isl_val_free(v);
5961 return NULL;
5964 /* Fix the value of the variable at position "pos" of type "type" of "set"
5965 * to be equal to "v".
5967 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5968 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5970 return isl_map_fix_val(set, type, pos, v);
5973 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5974 unsigned input, int value)
5976 return isl_map_fix_si(map, isl_dim_in, input, value);
5979 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5981 return set_from_map(isl_map_fix_si(set_to_map(set),
5982 isl_dim_set, dim, value));
5985 static __isl_give isl_basic_map *basic_map_bound_si(
5986 __isl_take isl_basic_map *bmap,
5987 enum isl_dim_type type, unsigned pos, int value, int upper)
5989 int j;
5991 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5992 return isl_basic_map_free(bmap);
5993 pos += isl_basic_map_offset(bmap, type);
5994 bmap = isl_basic_map_cow(bmap);
5995 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5996 j = isl_basic_map_alloc_inequality(bmap);
5997 if (j < 0)
5998 goto error;
5999 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6000 if (upper) {
6001 isl_int_set_si(bmap->ineq[j][pos], -1);
6002 isl_int_set_si(bmap->ineq[j][0], value);
6003 } else {
6004 isl_int_set_si(bmap->ineq[j][pos], 1);
6005 isl_int_set_si(bmap->ineq[j][0], -value);
6007 bmap = isl_basic_map_simplify(bmap);
6008 return isl_basic_map_finalize(bmap);
6009 error:
6010 isl_basic_map_free(bmap);
6011 return NULL;
6014 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6015 __isl_take isl_basic_map *bmap,
6016 enum isl_dim_type type, unsigned pos, int value)
6018 return basic_map_bound_si(bmap, type, pos, value, 0);
6021 /* Constrain the values of the given dimension to be no greater than "value".
6023 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6024 __isl_take isl_basic_map *bmap,
6025 enum isl_dim_type type, unsigned pos, int value)
6027 return basic_map_bound_si(bmap, type, pos, value, 1);
6030 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6031 enum isl_dim_type type, unsigned pos, int value, int upper)
6033 int i;
6035 map = isl_map_cow(map);
6036 if (!map)
6037 return NULL;
6039 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6040 for (i = 0; i < map->n; ++i) {
6041 map->p[i] = basic_map_bound_si(map->p[i],
6042 type, pos, value, upper);
6043 if (!map->p[i])
6044 goto error;
6046 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6047 return map;
6048 error:
6049 isl_map_free(map);
6050 return NULL;
6053 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6054 enum isl_dim_type type, unsigned pos, int value)
6056 return map_bound_si(map, type, pos, value, 0);
6059 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6060 enum isl_dim_type type, unsigned pos, int value)
6062 return map_bound_si(map, type, pos, value, 1);
6065 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6066 enum isl_dim_type type, unsigned pos, int value)
6068 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6069 type, pos, value));
6072 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6073 enum isl_dim_type type, unsigned pos, int value)
6075 return isl_map_upper_bound_si(set, type, pos, value);
6078 /* Bound the given variable of "bmap" from below (or above is "upper"
6079 * is set) to "value".
6081 static __isl_give isl_basic_map *basic_map_bound(
6082 __isl_take isl_basic_map *bmap,
6083 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6085 int j;
6087 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6088 return isl_basic_map_free(bmap);
6089 pos += isl_basic_map_offset(bmap, type);
6090 bmap = isl_basic_map_cow(bmap);
6091 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6092 j = isl_basic_map_alloc_inequality(bmap);
6093 if (j < 0)
6094 goto error;
6095 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6096 if (upper) {
6097 isl_int_set_si(bmap->ineq[j][pos], -1);
6098 isl_int_set(bmap->ineq[j][0], value);
6099 } else {
6100 isl_int_set_si(bmap->ineq[j][pos], 1);
6101 isl_int_neg(bmap->ineq[j][0], value);
6103 bmap = isl_basic_map_simplify(bmap);
6104 return isl_basic_map_finalize(bmap);
6105 error:
6106 isl_basic_map_free(bmap);
6107 return NULL;
6110 /* Bound the given variable of "map" from below (or above is "upper"
6111 * is set) to "value".
6113 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6114 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6116 int i;
6118 map = isl_map_cow(map);
6119 if (!map)
6120 return NULL;
6122 if (pos >= isl_map_dim(map, type))
6123 isl_die(map->ctx, isl_error_invalid,
6124 "index out of bounds", goto error);
6125 for (i = map->n - 1; i >= 0; --i) {
6126 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6127 if (remove_if_empty(map, i) < 0)
6128 goto error;
6130 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6131 return map;
6132 error:
6133 isl_map_free(map);
6134 return NULL;
6137 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6138 enum isl_dim_type type, unsigned pos, isl_int value)
6140 return map_bound(map, type, pos, value, 0);
6143 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6144 enum isl_dim_type type, unsigned pos, isl_int value)
6146 return map_bound(map, type, pos, value, 1);
6149 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6150 enum isl_dim_type type, unsigned pos, isl_int value)
6152 return isl_map_lower_bound(set, type, pos, value);
6155 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6156 enum isl_dim_type type, unsigned pos, isl_int value)
6158 return isl_map_upper_bound(set, type, pos, value);
6161 /* Force the values of the variable at position "pos" of type "type" of "set"
6162 * to be no smaller than "value".
6164 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6165 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6167 if (!value)
6168 goto error;
6169 if (!isl_val_is_int(value))
6170 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6171 "expecting integer value", goto error);
6172 set = isl_set_lower_bound(set, type, pos, value->n);
6173 isl_val_free(value);
6174 return set;
6175 error:
6176 isl_val_free(value);
6177 isl_set_free(set);
6178 return NULL;
6181 /* Force the values of the variable at position "pos" of type "type" of "set"
6182 * to be no greater than "value".
6184 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6185 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6187 if (!value)
6188 goto error;
6189 if (!isl_val_is_int(value))
6190 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6191 "expecting integer value", goto error);
6192 set = isl_set_upper_bound(set, type, pos, value->n);
6193 isl_val_free(value);
6194 return set;
6195 error:
6196 isl_val_free(value);
6197 isl_set_free(set);
6198 return NULL;
6201 struct isl_map *isl_map_reverse(struct isl_map *map)
6203 int i;
6205 map = isl_map_cow(map);
6206 if (!map)
6207 return NULL;
6209 map->dim = isl_space_reverse(map->dim);
6210 if (!map->dim)
6211 goto error;
6212 for (i = 0; i < map->n; ++i) {
6213 map->p[i] = isl_basic_map_reverse(map->p[i]);
6214 if (!map->p[i])
6215 goto error;
6217 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6218 return map;
6219 error:
6220 isl_map_free(map);
6221 return NULL;
6224 #undef TYPE
6225 #define TYPE isl_pw_multi_aff
6226 #undef SUFFIX
6227 #define SUFFIX _pw_multi_aff
6228 #undef EMPTY
6229 #define EMPTY isl_pw_multi_aff_empty
6230 #undef ADD
6231 #define ADD isl_pw_multi_aff_union_add
6232 #include "isl_map_lexopt_templ.c"
6234 /* Given a map "map", compute the lexicographically minimal
6235 * (or maximal) image element for each domain element in dom,
6236 * in the form of an isl_pw_multi_aff.
6237 * If "empty" is not NULL, then set *empty to those elements in dom that
6238 * do not have an image element.
6239 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6240 * should be computed over the domain of "map". "empty" is also NULL
6241 * in this case.
6243 * We first compute the lexicographically minimal or maximal element
6244 * in the first basic map. This results in a partial solution "res"
6245 * and a subset "todo" of dom that still need to be handled.
6246 * We then consider each of the remaining maps in "map" and successively
6247 * update both "res" and "todo".
6248 * If "empty" is NULL, then the todo sets are not needed and therefore
6249 * also not computed.
6251 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6252 __isl_take isl_map *map, __isl_take isl_set *dom,
6253 __isl_give isl_set **empty, unsigned flags)
6255 int i;
6256 int full;
6257 isl_pw_multi_aff *res;
6258 isl_set *todo;
6260 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6261 if (!map || (!full && !dom))
6262 goto error;
6264 if (isl_map_plain_is_empty(map)) {
6265 if (empty)
6266 *empty = dom;
6267 else
6268 isl_set_free(dom);
6269 return isl_pw_multi_aff_from_map(map);
6272 res = basic_map_partial_lexopt_pw_multi_aff(
6273 isl_basic_map_copy(map->p[0]),
6274 isl_set_copy(dom), empty, flags);
6276 if (empty)
6277 todo = *empty;
6278 for (i = 1; i < map->n; ++i) {
6279 isl_pw_multi_aff *res_i;
6281 res_i = basic_map_partial_lexopt_pw_multi_aff(
6282 isl_basic_map_copy(map->p[i]),
6283 isl_set_copy(dom), empty, flags);
6285 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6286 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6287 else
6288 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6290 if (empty)
6291 todo = isl_set_intersect(todo, *empty);
6294 isl_set_free(dom);
6295 isl_map_free(map);
6297 if (empty)
6298 *empty = todo;
6300 return res;
6301 error:
6302 if (empty)
6303 *empty = NULL;
6304 isl_set_free(dom);
6305 isl_map_free(map);
6306 return NULL;
6309 #undef TYPE
6310 #define TYPE isl_map
6311 #undef SUFFIX
6312 #define SUFFIX
6313 #undef EMPTY
6314 #define EMPTY isl_map_empty
6315 #undef ADD
6316 #define ADD isl_map_union_disjoint
6317 #include "isl_map_lexopt_templ.c"
6319 /* Given a map "map", compute the lexicographically minimal
6320 * (or maximal) image element for each domain element in "dom",
6321 * in the form of an isl_map.
6322 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6323 * do not have an image element.
6324 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6325 * should be computed over the domain of "map". "empty" is also NULL
6326 * in this case.
6328 * If the input consists of more than one disjunct, then first
6329 * compute the desired result in the form of an isl_pw_multi_aff and
6330 * then convert that into an isl_map.
6332 * This function used to have an explicit implementation in terms
6333 * of isl_maps, but it would continually intersect the domains of
6334 * partial results with the complement of the domain of the next
6335 * partial solution, potentially leading to an explosion in the number
6336 * of disjuncts if there are several disjuncts in the input.
6337 * An even earlier implementation of this function would look for
6338 * better results in the domain of the partial result and for extra
6339 * results in the complement of this domain, which would lead to
6340 * even more splintering.
6342 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6343 __isl_take isl_map *map, __isl_take isl_set *dom,
6344 __isl_give isl_set **empty, unsigned flags)
6346 int full;
6347 struct isl_map *res;
6348 isl_pw_multi_aff *pma;
6350 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6351 if (!map || (!full && !dom))
6352 goto error;
6354 if (isl_map_plain_is_empty(map)) {
6355 if (empty)
6356 *empty = dom;
6357 else
6358 isl_set_free(dom);
6359 return map;
6362 if (map->n == 1) {
6363 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6364 dom, empty, flags);
6365 isl_map_free(map);
6366 return res;
6369 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6370 flags);
6371 return isl_map_from_pw_multi_aff(pma);
6372 error:
6373 if (empty)
6374 *empty = NULL;
6375 isl_set_free(dom);
6376 isl_map_free(map);
6377 return NULL;
6380 __isl_give isl_map *isl_map_partial_lexmax(
6381 __isl_take isl_map *map, __isl_take isl_set *dom,
6382 __isl_give isl_set **empty)
6384 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6387 __isl_give isl_map *isl_map_partial_lexmin(
6388 __isl_take isl_map *map, __isl_take isl_set *dom,
6389 __isl_give isl_set **empty)
6391 return isl_map_partial_lexopt(map, dom, empty, 0);
6394 __isl_give isl_set *isl_set_partial_lexmin(
6395 __isl_take isl_set *set, __isl_take isl_set *dom,
6396 __isl_give isl_set **empty)
6398 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6399 dom, empty));
6402 __isl_give isl_set *isl_set_partial_lexmax(
6403 __isl_take isl_set *set, __isl_take isl_set *dom,
6404 __isl_give isl_set **empty)
6406 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6407 dom, empty));
6410 /* Compute the lexicographic minimum (or maximum if "flags" includes
6411 * ISL_OPT_MAX) of "bset" over its parametric domain.
6413 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6414 unsigned flags)
6416 return isl_basic_map_lexopt(bset, flags);
6419 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6421 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6424 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6426 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6429 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6431 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6434 /* Compute the lexicographic minimum of "bset" over its parametric domain
6435 * for the purpose of quantifier elimination.
6436 * That is, find an explicit representation for all the existentially
6437 * quantified variables in "bset" by computing their lexicographic
6438 * minimum.
6440 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6441 __isl_take isl_basic_set *bset)
6443 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6446 /* Extract the first and only affine expression from list
6447 * and then add it to *pwaff with the given dom.
6448 * This domain is known to be disjoint from other domains
6449 * because of the way isl_basic_map_foreach_lexmax works.
6451 static isl_stat update_dim_opt(__isl_take isl_basic_set *dom,
6452 __isl_take isl_aff_list *list, void *user)
6454 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6455 isl_aff *aff;
6456 isl_pw_aff **pwaff = user;
6457 isl_pw_aff *pwaff_i;
6459 if (!list)
6460 goto error;
6461 if (isl_aff_list_n_aff(list) != 1)
6462 isl_die(ctx, isl_error_internal,
6463 "expecting single element list", goto error);
6465 aff = isl_aff_list_get_aff(list, 0);
6466 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6468 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6470 isl_aff_list_free(list);
6472 return isl_stat_ok;
6473 error:
6474 isl_basic_set_free(dom);
6475 isl_aff_list_free(list);
6476 return isl_stat_error;
6479 /* Given a basic map with one output dimension, compute the minimum or
6480 * maximum of that dimension as an isl_pw_aff.
6482 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6483 * call update_dim_opt on each leaf of the result.
6485 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6486 int max)
6488 isl_space *dim = isl_basic_map_get_space(bmap);
6489 isl_pw_aff *pwaff;
6490 isl_stat r;
6492 dim = isl_space_from_domain(isl_space_domain(dim));
6493 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6494 pwaff = isl_pw_aff_empty(dim);
6496 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6497 if (r < 0)
6498 return isl_pw_aff_free(pwaff);
6500 return pwaff;
6503 /* Compute the minimum or maximum of the given output dimension
6504 * as a function of the parameters and the input dimensions,
6505 * but independently of the other output dimensions.
6507 * We first project out the other output dimension and then compute
6508 * the "lexicographic" maximum in each basic map, combining the results
6509 * using isl_pw_aff_union_max.
6511 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6512 int max)
6514 int i;
6515 isl_pw_aff *pwaff;
6516 unsigned n_out;
6518 n_out = isl_map_dim(map, isl_dim_out);
6519 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6520 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6521 if (!map)
6522 return NULL;
6524 if (map->n == 0) {
6525 isl_space *dim = isl_map_get_space(map);
6526 isl_map_free(map);
6527 return isl_pw_aff_empty(dim);
6530 pwaff = basic_map_dim_opt(map->p[0], max);
6531 for (i = 1; i < map->n; ++i) {
6532 isl_pw_aff *pwaff_i;
6534 pwaff_i = basic_map_dim_opt(map->p[i], max);
6535 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6538 isl_map_free(map);
6540 return pwaff;
6543 /* Compute the minimum of the given output dimension as a function of the
6544 * parameters and input dimensions, but independently of
6545 * the other output dimensions.
6547 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6549 return map_dim_opt(map, pos, 0);
6552 /* Compute the maximum of the given output dimension as a function of the
6553 * parameters and input dimensions, but independently of
6554 * the other output dimensions.
6556 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6558 return map_dim_opt(map, pos, 1);
6561 /* Compute the minimum or maximum of the given set dimension
6562 * as a function of the parameters,
6563 * but independently of the other set dimensions.
6565 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6566 int max)
6568 return map_dim_opt(set, pos, max);
6571 /* Compute the maximum of the given set dimension as a function of the
6572 * parameters, but independently of the other set dimensions.
6574 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6576 return set_dim_opt(set, pos, 1);
6579 /* Compute the minimum of the given set dimension as a function of the
6580 * parameters, but independently of the other set dimensions.
6582 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6584 return set_dim_opt(set, pos, 0);
6587 /* Apply a preimage specified by "mat" on the parameters of "bset".
6588 * bset is assumed to have only parameters and divs.
6590 static struct isl_basic_set *basic_set_parameter_preimage(
6591 struct isl_basic_set *bset, struct isl_mat *mat)
6593 unsigned nparam;
6595 if (!bset || !mat)
6596 goto error;
6598 bset->dim = isl_space_cow(bset->dim);
6599 if (!bset->dim)
6600 goto error;
6602 nparam = isl_basic_set_dim(bset, isl_dim_param);
6604 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6606 bset->dim->nparam = 0;
6607 bset->dim->n_out = nparam;
6608 bset = isl_basic_set_preimage(bset, mat);
6609 if (bset) {
6610 bset->dim->nparam = bset->dim->n_out;
6611 bset->dim->n_out = 0;
6613 return bset;
6614 error:
6615 isl_mat_free(mat);
6616 isl_basic_set_free(bset);
6617 return NULL;
6620 /* Apply a preimage specified by "mat" on the parameters of "set".
6621 * set is assumed to have only parameters and divs.
6623 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6624 __isl_take isl_mat *mat)
6626 isl_space *space;
6627 unsigned nparam;
6629 if (!set || !mat)
6630 goto error;
6632 nparam = isl_set_dim(set, isl_dim_param);
6634 if (mat->n_row != 1 + nparam)
6635 isl_die(isl_set_get_ctx(set), isl_error_internal,
6636 "unexpected number of rows", goto error);
6638 space = isl_set_get_space(set);
6639 space = isl_space_move_dims(space, isl_dim_set, 0,
6640 isl_dim_param, 0, nparam);
6641 set = isl_set_reset_space(set, space);
6642 set = isl_set_preimage(set, mat);
6643 nparam = isl_set_dim(set, isl_dim_out);
6644 space = isl_set_get_space(set);
6645 space = isl_space_move_dims(space, isl_dim_param, 0,
6646 isl_dim_out, 0, nparam);
6647 set = isl_set_reset_space(set, space);
6648 return set;
6649 error:
6650 isl_mat_free(mat);
6651 isl_set_free(set);
6652 return NULL;
6655 /* Intersect the basic set "bset" with the affine space specified by the
6656 * equalities in "eq".
6658 static struct isl_basic_set *basic_set_append_equalities(
6659 struct isl_basic_set *bset, struct isl_mat *eq)
6661 int i, k;
6662 unsigned len;
6664 if (!bset || !eq)
6665 goto error;
6667 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6668 eq->n_row, 0);
6669 if (!bset)
6670 goto error;
6672 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6673 for (i = 0; i < eq->n_row; ++i) {
6674 k = isl_basic_set_alloc_equality(bset);
6675 if (k < 0)
6676 goto error;
6677 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6678 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6680 isl_mat_free(eq);
6682 bset = isl_basic_set_gauss(bset, NULL);
6683 bset = isl_basic_set_finalize(bset);
6685 return bset;
6686 error:
6687 isl_mat_free(eq);
6688 isl_basic_set_free(bset);
6689 return NULL;
6692 /* Intersect the set "set" with the affine space specified by the
6693 * equalities in "eq".
6695 static struct isl_set *set_append_equalities(struct isl_set *set,
6696 struct isl_mat *eq)
6698 int i;
6700 if (!set || !eq)
6701 goto error;
6703 for (i = 0; i < set->n; ++i) {
6704 set->p[i] = basic_set_append_equalities(set->p[i],
6705 isl_mat_copy(eq));
6706 if (!set->p[i])
6707 goto error;
6709 isl_mat_free(eq);
6710 return set;
6711 error:
6712 isl_mat_free(eq);
6713 isl_set_free(set);
6714 return NULL;
6717 /* Given a basic set "bset" that only involves parameters and existentially
6718 * quantified variables, return the index of the first equality
6719 * that only involves parameters. If there is no such equality then
6720 * return bset->n_eq.
6722 * This function assumes that isl_basic_set_gauss has been called on "bset".
6724 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6726 int i, j;
6727 unsigned nparam, n_div;
6729 if (!bset)
6730 return -1;
6732 nparam = isl_basic_set_dim(bset, isl_dim_param);
6733 n_div = isl_basic_set_dim(bset, isl_dim_div);
6735 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6736 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6737 ++i;
6740 return i;
6743 /* Compute an explicit representation for the existentially quantified
6744 * variables in "bset" by computing the "minimal value" of the set
6745 * variables. Since there are no set variables, the computation of
6746 * the minimal value essentially computes an explicit representation
6747 * of the non-empty part(s) of "bset".
6749 * The input only involves parameters and existentially quantified variables.
6750 * All equalities among parameters have been removed.
6752 * Since the existentially quantified variables in the result are in general
6753 * going to be different from those in the input, we first replace
6754 * them by the minimal number of variables based on their equalities.
6755 * This should simplify the parametric integer programming.
6757 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6759 isl_morph *morph1, *morph2;
6760 isl_set *set;
6761 unsigned n;
6763 if (!bset)
6764 return NULL;
6765 if (bset->n_eq == 0)
6766 return isl_basic_set_lexmin_compute_divs(bset);
6768 morph1 = isl_basic_set_parameter_compression(bset);
6769 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6770 bset = isl_basic_set_lift(bset);
6771 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6772 bset = isl_morph_basic_set(morph2, bset);
6773 n = isl_basic_set_dim(bset, isl_dim_set);
6774 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6776 set = isl_basic_set_lexmin_compute_divs(bset);
6778 set = isl_morph_set(isl_morph_inverse(morph1), set);
6780 return set;
6783 /* Project the given basic set onto its parameter domain, possibly introducing
6784 * new, explicit, existential variables in the constraints.
6785 * The input has parameters and (possibly implicit) existential variables.
6786 * The output has the same parameters, but only
6787 * explicit existentially quantified variables.
6789 * The actual projection is performed by pip, but pip doesn't seem
6790 * to like equalities very much, so we first remove the equalities
6791 * among the parameters by performing a variable compression on
6792 * the parameters. Afterward, an inverse transformation is performed
6793 * and the equalities among the parameters are inserted back in.
6795 * The variable compression on the parameters may uncover additional
6796 * equalities that were only implicit before. We therefore check
6797 * if there are any new parameter equalities in the result and
6798 * if so recurse. The removal of parameter equalities is required
6799 * for the parameter compression performed by base_compute_divs.
6801 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6803 int i;
6804 struct isl_mat *eq;
6805 struct isl_mat *T, *T2;
6806 struct isl_set *set;
6807 unsigned nparam;
6809 bset = isl_basic_set_cow(bset);
6810 if (!bset)
6811 return NULL;
6813 if (bset->n_eq == 0)
6814 return base_compute_divs(bset);
6816 bset = isl_basic_set_gauss(bset, NULL);
6817 if (!bset)
6818 return NULL;
6819 if (isl_basic_set_plain_is_empty(bset))
6820 return isl_set_from_basic_set(bset);
6822 i = first_parameter_equality(bset);
6823 if (i == bset->n_eq)
6824 return base_compute_divs(bset);
6826 nparam = isl_basic_set_dim(bset, isl_dim_param);
6827 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6828 0, 1 + nparam);
6829 eq = isl_mat_cow(eq);
6830 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6831 if (T && T->n_col == 0) {
6832 isl_mat_free(T);
6833 isl_mat_free(T2);
6834 isl_mat_free(eq);
6835 bset = isl_basic_set_set_to_empty(bset);
6836 return isl_set_from_basic_set(bset);
6838 bset = basic_set_parameter_preimage(bset, T);
6840 i = first_parameter_equality(bset);
6841 if (!bset)
6842 set = NULL;
6843 else if (i == bset->n_eq)
6844 set = base_compute_divs(bset);
6845 else
6846 set = parameter_compute_divs(bset);
6847 set = set_parameter_preimage(set, T2);
6848 set = set_append_equalities(set, eq);
6849 return set;
6852 /* Insert the divs from "ls" before those of "bmap".
6854 * The number of columns is not changed, which means that the last
6855 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6856 * The caller is responsible for removing the same number of dimensions
6857 * from the space of "bmap".
6859 static __isl_give isl_basic_map *insert_divs_from_local_space(
6860 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6862 int i;
6863 int n_div;
6864 int old_n_div;
6866 n_div = isl_local_space_dim(ls, isl_dim_div);
6867 if (n_div == 0)
6868 return bmap;
6870 old_n_div = bmap->n_div;
6871 bmap = insert_div_rows(bmap, n_div);
6872 if (!bmap)
6873 return NULL;
6875 for (i = 0; i < n_div; ++i) {
6876 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6877 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6880 return bmap;
6883 /* Replace the space of "bmap" by the space and divs of "ls".
6885 * If "ls" has any divs, then we simplify the result since we may
6886 * have discovered some additional equalities that could simplify
6887 * the div expressions.
6889 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6890 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6892 int n_div;
6894 bmap = isl_basic_map_cow(bmap);
6895 if (!bmap || !ls)
6896 goto error;
6898 n_div = isl_local_space_dim(ls, isl_dim_div);
6899 bmap = insert_divs_from_local_space(bmap, ls);
6900 if (!bmap)
6901 goto error;
6903 isl_space_free(bmap->dim);
6904 bmap->dim = isl_local_space_get_space(ls);
6905 if (!bmap->dim)
6906 goto error;
6908 isl_local_space_free(ls);
6909 if (n_div > 0)
6910 bmap = isl_basic_map_simplify(bmap);
6911 bmap = isl_basic_map_finalize(bmap);
6912 return bmap;
6913 error:
6914 isl_basic_map_free(bmap);
6915 isl_local_space_free(ls);
6916 return NULL;
6919 /* Replace the space of "map" by the space and divs of "ls".
6921 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6922 __isl_take isl_local_space *ls)
6924 int i;
6926 map = isl_map_cow(map);
6927 if (!map || !ls)
6928 goto error;
6930 for (i = 0; i < map->n; ++i) {
6931 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6932 isl_local_space_copy(ls));
6933 if (!map->p[i])
6934 goto error;
6936 isl_space_free(map->dim);
6937 map->dim = isl_local_space_get_space(ls);
6938 if (!map->dim)
6939 goto error;
6941 isl_local_space_free(ls);
6942 return map;
6943 error:
6944 isl_local_space_free(ls);
6945 isl_map_free(map);
6946 return NULL;
6949 /* Compute an explicit representation for the existentially
6950 * quantified variables for which do not know any explicit representation yet.
6952 * We first sort the existentially quantified variables so that the
6953 * existentially quantified variables for which we already have an explicit
6954 * representation are placed before those for which we do not.
6955 * The input dimensions, the output dimensions and the existentially
6956 * quantified variables for which we already have an explicit
6957 * representation are then turned into parameters.
6958 * compute_divs returns a map with the same parameters and
6959 * no input or output dimensions and the dimension specification
6960 * is reset to that of the input, including the existentially quantified
6961 * variables for which we already had an explicit representation.
6963 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6965 struct isl_basic_set *bset;
6966 struct isl_set *set;
6967 struct isl_map *map;
6968 isl_space *dim;
6969 isl_local_space *ls;
6970 unsigned nparam;
6971 unsigned n_in;
6972 unsigned n_out;
6973 int n_known;
6974 int i;
6976 bmap = isl_basic_map_sort_divs(bmap);
6977 bmap = isl_basic_map_cow(bmap);
6978 if (!bmap)
6979 return NULL;
6981 n_known = isl_basic_map_first_unknown_div(bmap);
6982 if (n_known < 0)
6983 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6985 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6986 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6987 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6988 dim = isl_space_set_alloc(bmap->ctx,
6989 nparam + n_in + n_out + n_known, 0);
6990 if (!dim)
6991 goto error;
6993 ls = isl_basic_map_get_local_space(bmap);
6994 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6995 n_known, bmap->n_div - n_known);
6996 if (n_known > 0) {
6997 for (i = n_known; i < bmap->n_div; ++i)
6998 swap_div(bmap, i - n_known, i);
6999 bmap->n_div -= n_known;
7000 bmap->extra -= n_known;
7002 bmap = isl_basic_map_reset_space(bmap, dim);
7003 bset = bset_from_bmap(bmap);
7005 set = parameter_compute_divs(bset);
7006 map = set_to_map(set);
7007 map = replace_space_by_local_space(map, ls);
7009 return map;
7010 error:
7011 isl_basic_map_free(bmap);
7012 return NULL;
7015 /* Remove the explicit representation of local variable "div",
7016 * if there is any.
7018 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7019 __isl_take isl_basic_map *bmap, int div)
7021 isl_bool unknown;
7023 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7024 if (unknown < 0)
7025 return isl_basic_map_free(bmap);
7026 if (unknown)
7027 return bmap;
7029 bmap = isl_basic_map_cow(bmap);
7030 if (!bmap)
7031 return NULL;
7032 isl_int_set_si(bmap->div[div][0], 0);
7033 return bmap;
7036 /* Is local variable "div" of "bmap" marked as not having an explicit
7037 * representation?
7038 * Note that even if "div" is not marked in this way and therefore
7039 * has an explicit representation, this representation may still
7040 * depend (indirectly) on other local variables that do not
7041 * have an explicit representation.
7043 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7044 int div)
7046 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7047 return isl_bool_error;
7048 return isl_int_is_zero(bmap->div[div][0]);
7051 /* Return the position of the first local variable that does not
7052 * have an explicit representation.
7053 * Return the total number of local variables if they all have
7054 * an explicit representation.
7055 * Return -1 on error.
7057 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7059 int i;
7061 if (!bmap)
7062 return -1;
7064 for (i = 0; i < bmap->n_div; ++i) {
7065 if (!isl_basic_map_div_is_known(bmap, i))
7066 return i;
7068 return bmap->n_div;
7071 /* Return the position of the first local variable that does not
7072 * have an explicit representation.
7073 * Return the total number of local variables if they all have
7074 * an explicit representation.
7075 * Return -1 on error.
7077 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7079 return isl_basic_map_first_unknown_div(bset);
7082 /* Does "bmap" have an explicit representation for all local variables?
7084 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7086 int first, n;
7088 n = isl_basic_map_dim(bmap, isl_dim_div);
7089 first = isl_basic_map_first_unknown_div(bmap);
7090 if (first < 0)
7091 return isl_bool_error;
7092 return first == n;
7095 /* Do all basic maps in "map" have an explicit representation
7096 * for all local variables?
7098 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7100 int i;
7102 if (!map)
7103 return isl_bool_error;
7105 for (i = 0; i < map->n; ++i) {
7106 int known = isl_basic_map_divs_known(map->p[i]);
7107 if (known <= 0)
7108 return known;
7111 return isl_bool_true;
7114 /* If bmap contains any unknown divs, then compute explicit
7115 * expressions for them. However, this computation may be
7116 * quite expensive, so first try to remove divs that aren't
7117 * strictly needed.
7119 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7121 int known;
7122 struct isl_map *map;
7124 known = isl_basic_map_divs_known(bmap);
7125 if (known < 0)
7126 goto error;
7127 if (known)
7128 return isl_map_from_basic_map(bmap);
7130 bmap = isl_basic_map_drop_redundant_divs(bmap);
7132 known = isl_basic_map_divs_known(bmap);
7133 if (known < 0)
7134 goto error;
7135 if (known)
7136 return isl_map_from_basic_map(bmap);
7138 map = compute_divs(bmap);
7139 return map;
7140 error:
7141 isl_basic_map_free(bmap);
7142 return NULL;
7145 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7147 int i;
7148 int known;
7149 struct isl_map *res;
7151 if (!map)
7152 return NULL;
7153 if (map->n == 0)
7154 return map;
7156 known = isl_map_divs_known(map);
7157 if (known < 0) {
7158 isl_map_free(map);
7159 return NULL;
7161 if (known)
7162 return map;
7164 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7165 for (i = 1 ; i < map->n; ++i) {
7166 struct isl_map *r2;
7167 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7168 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7169 res = isl_map_union_disjoint(res, r2);
7170 else
7171 res = isl_map_union(res, r2);
7173 isl_map_free(map);
7175 return res;
7178 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7180 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7183 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7185 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7188 struct isl_set *isl_map_domain(struct isl_map *map)
7190 int i;
7191 struct isl_set *set;
7193 if (!map)
7194 goto error;
7196 map = isl_map_cow(map);
7197 if (!map)
7198 return NULL;
7200 set = set_from_map(map);
7201 set->dim = isl_space_domain(set->dim);
7202 if (!set->dim)
7203 goto error;
7204 for (i = 0; i < map->n; ++i) {
7205 set->p[i] = isl_basic_map_domain(map->p[i]);
7206 if (!set->p[i])
7207 goto error;
7209 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7210 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7211 return set;
7212 error:
7213 isl_map_free(map);
7214 return NULL;
7217 /* Return the union of "map1" and "map2", where we assume for now that
7218 * "map1" and "map2" are disjoint. Note that the basic maps inside
7219 * "map1" or "map2" may not be disjoint from each other.
7220 * Also note that this function is also called from isl_map_union,
7221 * which takes care of handling the situation where "map1" and "map2"
7222 * may not be disjoint.
7224 * If one of the inputs is empty, we can simply return the other input.
7225 * Similarly, if one of the inputs is universal, then it is equal to the union.
7227 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7228 __isl_take isl_map *map2)
7230 int i;
7231 unsigned flags = 0;
7232 struct isl_map *map = NULL;
7233 int is_universe;
7235 if (!map1 || !map2)
7236 goto error;
7238 if (!isl_space_is_equal(map1->dim, map2->dim))
7239 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7240 "spaces don't match", goto error);
7242 if (map1->n == 0) {
7243 isl_map_free(map1);
7244 return map2;
7246 if (map2->n == 0) {
7247 isl_map_free(map2);
7248 return map1;
7251 is_universe = isl_map_plain_is_universe(map1);
7252 if (is_universe < 0)
7253 goto error;
7254 if (is_universe) {
7255 isl_map_free(map2);
7256 return map1;
7259 is_universe = isl_map_plain_is_universe(map2);
7260 if (is_universe < 0)
7261 goto error;
7262 if (is_universe) {
7263 isl_map_free(map1);
7264 return map2;
7267 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7268 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7269 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7271 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7272 map1->n + map2->n, flags);
7273 if (!map)
7274 goto error;
7275 for (i = 0; i < map1->n; ++i) {
7276 map = isl_map_add_basic_map(map,
7277 isl_basic_map_copy(map1->p[i]));
7278 if (!map)
7279 goto error;
7281 for (i = 0; i < map2->n; ++i) {
7282 map = isl_map_add_basic_map(map,
7283 isl_basic_map_copy(map2->p[i]));
7284 if (!map)
7285 goto error;
7287 isl_map_free(map1);
7288 isl_map_free(map2);
7289 return map;
7290 error:
7291 isl_map_free(map);
7292 isl_map_free(map1);
7293 isl_map_free(map2);
7294 return NULL;
7297 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7298 * guaranteed to be disjoint by the caller.
7300 * Note that this functions is called from within isl_map_make_disjoint,
7301 * so we have to be careful not to touch the constraints of the inputs
7302 * in any way.
7304 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7305 __isl_take isl_map *map2)
7307 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7310 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7311 * not be disjoint. The parameters are assumed to have been aligned.
7313 * We currently simply call map_union_disjoint, the internal operation
7314 * of which does not really depend on the inputs being disjoint.
7315 * If the result contains more than one basic map, then we clear
7316 * the disjoint flag since the result may contain basic maps from
7317 * both inputs and these are not guaranteed to be disjoint.
7319 * As a special case, if "map1" and "map2" are obviously equal,
7320 * then we simply return "map1".
7322 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7323 __isl_take isl_map *map2)
7325 int equal;
7327 if (!map1 || !map2)
7328 goto error;
7330 equal = isl_map_plain_is_equal(map1, map2);
7331 if (equal < 0)
7332 goto error;
7333 if (equal) {
7334 isl_map_free(map2);
7335 return map1;
7338 map1 = map_union_disjoint(map1, map2);
7339 if (!map1)
7340 return NULL;
7341 if (map1->n > 1)
7342 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7343 return map1;
7344 error:
7345 isl_map_free(map1);
7346 isl_map_free(map2);
7347 return NULL;
7350 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7351 * not be disjoint.
7353 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7354 __isl_take isl_map *map2)
7356 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7359 struct isl_set *isl_set_union_disjoint(
7360 struct isl_set *set1, struct isl_set *set2)
7362 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7363 set_to_map(set2)));
7366 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7368 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7371 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7372 * the results.
7374 * "map" and "set" are assumed to be compatible and non-NULL.
7376 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7377 __isl_take isl_set *set,
7378 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7379 __isl_take isl_basic_set *bset))
7381 unsigned flags = 0;
7382 struct isl_map *result;
7383 int i, j;
7385 if (isl_set_plain_is_universe(set)) {
7386 isl_set_free(set);
7387 return map;
7390 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7391 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7392 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7394 result = isl_map_alloc_space(isl_space_copy(map->dim),
7395 map->n * set->n, flags);
7396 for (i = 0; result && i < map->n; ++i)
7397 for (j = 0; j < set->n; ++j) {
7398 result = isl_map_add_basic_map(result,
7399 fn(isl_basic_map_copy(map->p[i]),
7400 isl_basic_set_copy(set->p[j])));
7401 if (!result)
7402 break;
7405 isl_map_free(map);
7406 isl_set_free(set);
7407 return result;
7410 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7411 __isl_take isl_set *set)
7413 isl_bool ok;
7415 ok = isl_map_compatible_range(map, set);
7416 if (ok < 0)
7417 goto error;
7418 if (!ok)
7419 isl_die(set->ctx, isl_error_invalid,
7420 "incompatible spaces", goto error);
7422 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7423 error:
7424 isl_map_free(map);
7425 isl_set_free(set);
7426 return NULL;
7429 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7430 __isl_take isl_set *set)
7432 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7435 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7436 __isl_take isl_set *set)
7438 isl_bool ok;
7440 ok = isl_map_compatible_domain(map, set);
7441 if (ok < 0)
7442 goto error;
7443 if (!ok)
7444 isl_die(set->ctx, isl_error_invalid,
7445 "incompatible spaces", goto error);
7447 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7448 error:
7449 isl_map_free(map);
7450 isl_set_free(set);
7451 return NULL;
7454 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7455 __isl_take isl_set *set)
7457 return isl_map_align_params_map_map_and(map, set,
7458 &map_intersect_domain);
7461 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7462 __isl_take isl_map *map2)
7464 if (!map1 || !map2)
7465 goto error;
7466 map1 = isl_map_reverse(map1);
7467 map1 = isl_map_apply_range(map1, map2);
7468 return isl_map_reverse(map1);
7469 error:
7470 isl_map_free(map1);
7471 isl_map_free(map2);
7472 return NULL;
7475 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7476 __isl_take isl_map *map2)
7478 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7481 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7482 __isl_take isl_map *map2)
7484 isl_space *dim_result;
7485 struct isl_map *result;
7486 int i, j;
7488 if (!map1 || !map2)
7489 goto error;
7491 dim_result = isl_space_join(isl_space_copy(map1->dim),
7492 isl_space_copy(map2->dim));
7494 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7495 if (!result)
7496 goto error;
7497 for (i = 0; i < map1->n; ++i)
7498 for (j = 0; j < map2->n; ++j) {
7499 result = isl_map_add_basic_map(result,
7500 isl_basic_map_apply_range(
7501 isl_basic_map_copy(map1->p[i]),
7502 isl_basic_map_copy(map2->p[j])));
7503 if (!result)
7504 goto error;
7506 isl_map_free(map1);
7507 isl_map_free(map2);
7508 if (result && result->n <= 1)
7509 ISL_F_SET(result, ISL_MAP_DISJOINT);
7510 return result;
7511 error:
7512 isl_map_free(map1);
7513 isl_map_free(map2);
7514 return NULL;
7517 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7518 __isl_take isl_map *map2)
7520 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7524 * returns range - domain
7526 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7528 isl_space *target_space;
7529 struct isl_basic_set *bset;
7530 unsigned dim;
7531 unsigned nparam;
7532 int i;
7534 if (!bmap)
7535 goto error;
7536 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7537 bmap->dim, isl_dim_out),
7538 goto error);
7539 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7540 dim = isl_basic_map_n_in(bmap);
7541 nparam = isl_basic_map_n_param(bmap);
7542 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7543 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7544 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7545 for (i = 0; i < dim; ++i) {
7546 int j = isl_basic_map_alloc_equality(bmap);
7547 if (j < 0) {
7548 bmap = isl_basic_map_free(bmap);
7549 break;
7551 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7552 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7553 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7554 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7556 bset = isl_basic_map_domain(bmap);
7557 bset = isl_basic_set_reset_space(bset, target_space);
7558 return bset;
7559 error:
7560 isl_basic_map_free(bmap);
7561 return NULL;
7565 * returns range - domain
7567 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7569 int i;
7570 isl_space *dim;
7571 struct isl_set *result;
7573 if (!map)
7574 return NULL;
7576 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7577 map->dim, isl_dim_out),
7578 goto error);
7579 dim = isl_map_get_space(map);
7580 dim = isl_space_domain(dim);
7581 result = isl_set_alloc_space(dim, map->n, 0);
7582 if (!result)
7583 goto error;
7584 for (i = 0; i < map->n; ++i)
7585 result = isl_set_add_basic_set(result,
7586 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7587 isl_map_free(map);
7588 return result;
7589 error:
7590 isl_map_free(map);
7591 return NULL;
7595 * returns [domain -> range] -> range - domain
7597 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7598 __isl_take isl_basic_map *bmap)
7600 int i, k;
7601 isl_space *dim;
7602 isl_basic_map *domain;
7603 int nparam, n;
7604 unsigned total;
7606 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7607 bmap->dim, isl_dim_out))
7608 isl_die(bmap->ctx, isl_error_invalid,
7609 "domain and range don't match", goto error);
7611 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7612 n = isl_basic_map_dim(bmap, isl_dim_in);
7614 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7615 domain = isl_basic_map_universe(dim);
7617 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7618 bmap = isl_basic_map_apply_range(bmap, domain);
7619 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7621 total = isl_basic_map_total_dim(bmap);
7623 for (i = 0; i < n; ++i) {
7624 k = isl_basic_map_alloc_equality(bmap);
7625 if (k < 0)
7626 goto error;
7627 isl_seq_clr(bmap->eq[k], 1 + total);
7628 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7629 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7630 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7633 bmap = isl_basic_map_gauss(bmap, NULL);
7634 return isl_basic_map_finalize(bmap);
7635 error:
7636 isl_basic_map_free(bmap);
7637 return NULL;
7641 * returns [domain -> range] -> range - domain
7643 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7645 int i;
7646 isl_space *domain_dim;
7648 if (!map)
7649 return NULL;
7651 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7652 map->dim, isl_dim_out))
7653 isl_die(map->ctx, isl_error_invalid,
7654 "domain and range don't match", goto error);
7656 map = isl_map_cow(map);
7657 if (!map)
7658 return NULL;
7660 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7661 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7662 map->dim = isl_space_join(map->dim, domain_dim);
7663 if (!map->dim)
7664 goto error;
7665 for (i = 0; i < map->n; ++i) {
7666 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7667 if (!map->p[i])
7668 goto error;
7670 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7671 return map;
7672 error:
7673 isl_map_free(map);
7674 return NULL;
7677 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7679 struct isl_basic_map *bmap;
7680 unsigned nparam;
7681 unsigned dim;
7682 int i;
7684 if (!dims)
7685 return NULL;
7687 nparam = dims->nparam;
7688 dim = dims->n_out;
7689 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7690 if (!bmap)
7691 goto error;
7693 for (i = 0; i < dim; ++i) {
7694 int j = isl_basic_map_alloc_equality(bmap);
7695 if (j < 0)
7696 goto error;
7697 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7698 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7699 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7701 return isl_basic_map_finalize(bmap);
7702 error:
7703 isl_basic_map_free(bmap);
7704 return NULL;
7707 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7709 if (!dim)
7710 return NULL;
7711 if (dim->n_in != dim->n_out)
7712 isl_die(dim->ctx, isl_error_invalid,
7713 "number of input and output dimensions needs to be "
7714 "the same", goto error);
7715 return basic_map_identity(dim);
7716 error:
7717 isl_space_free(dim);
7718 return NULL;
7721 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7723 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7726 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7728 isl_space *dim = isl_set_get_space(set);
7729 isl_map *id;
7730 id = isl_map_identity(isl_space_map_from_set(dim));
7731 return isl_map_intersect_range(id, set);
7734 /* Construct a basic set with all set dimensions having only non-negative
7735 * values.
7737 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7738 __isl_take isl_space *space)
7740 int i;
7741 unsigned nparam;
7742 unsigned dim;
7743 struct isl_basic_set *bset;
7745 if (!space)
7746 return NULL;
7747 nparam = space->nparam;
7748 dim = space->n_out;
7749 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7750 if (!bset)
7751 return NULL;
7752 for (i = 0; i < dim; ++i) {
7753 int k = isl_basic_set_alloc_inequality(bset);
7754 if (k < 0)
7755 goto error;
7756 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7757 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7759 return bset;
7760 error:
7761 isl_basic_set_free(bset);
7762 return NULL;
7765 /* Construct the half-space x_pos >= 0.
7767 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7768 int pos)
7770 int k;
7771 isl_basic_set *nonneg;
7773 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7774 k = isl_basic_set_alloc_inequality(nonneg);
7775 if (k < 0)
7776 goto error;
7777 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7778 isl_int_set_si(nonneg->ineq[k][pos], 1);
7780 return isl_basic_set_finalize(nonneg);
7781 error:
7782 isl_basic_set_free(nonneg);
7783 return NULL;
7786 /* Construct the half-space x_pos <= -1.
7788 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7790 int k;
7791 isl_basic_set *neg;
7793 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7794 k = isl_basic_set_alloc_inequality(neg);
7795 if (k < 0)
7796 goto error;
7797 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7798 isl_int_set_si(neg->ineq[k][0], -1);
7799 isl_int_set_si(neg->ineq[k][pos], -1);
7801 return isl_basic_set_finalize(neg);
7802 error:
7803 isl_basic_set_free(neg);
7804 return NULL;
7807 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7808 enum isl_dim_type type, unsigned first, unsigned n)
7810 int i;
7811 unsigned offset;
7812 isl_basic_set *nonneg;
7813 isl_basic_set *neg;
7815 if (!set)
7816 return NULL;
7817 if (n == 0)
7818 return set;
7820 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7822 offset = pos(set->dim, type);
7823 for (i = 0; i < n; ++i) {
7824 nonneg = nonneg_halfspace(isl_set_get_space(set),
7825 offset + first + i);
7826 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7828 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7831 return set;
7832 error:
7833 isl_set_free(set);
7834 return NULL;
7837 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7838 int len,
7839 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7840 void *user)
7842 isl_set *half;
7844 if (!set)
7845 return isl_stat_error;
7846 if (isl_set_plain_is_empty(set)) {
7847 isl_set_free(set);
7848 return isl_stat_ok;
7850 if (first == len)
7851 return fn(set, signs, user);
7853 signs[first] = 1;
7854 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7855 1 + first));
7856 half = isl_set_intersect(half, isl_set_copy(set));
7857 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7858 goto error;
7860 signs[first] = -1;
7861 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7862 1 + first));
7863 half = isl_set_intersect(half, set);
7864 return foreach_orthant(half, signs, first + 1, len, fn, user);
7865 error:
7866 isl_set_free(set);
7867 return isl_stat_error;
7870 /* Call "fn" on the intersections of "set" with each of the orthants
7871 * (except for obviously empty intersections). The orthant is identified
7872 * by the signs array, with each entry having value 1 or -1 according
7873 * to the sign of the corresponding variable.
7875 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
7876 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7877 void *user)
7879 unsigned nparam;
7880 unsigned nvar;
7881 int *signs;
7882 isl_stat r;
7884 if (!set)
7885 return isl_stat_error;
7886 if (isl_set_plain_is_empty(set))
7887 return isl_stat_ok;
7889 nparam = isl_set_dim(set, isl_dim_param);
7890 nvar = isl_set_dim(set, isl_dim_set);
7892 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7894 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7895 fn, user);
7897 free(signs);
7899 return r;
7902 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7904 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
7907 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7908 __isl_keep isl_basic_map *bmap2)
7910 int is_subset;
7911 struct isl_map *map1;
7912 struct isl_map *map2;
7914 if (!bmap1 || !bmap2)
7915 return isl_bool_error;
7917 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7918 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7920 is_subset = isl_map_is_subset(map1, map2);
7922 isl_map_free(map1);
7923 isl_map_free(map2);
7925 return is_subset;
7928 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7929 __isl_keep isl_basic_set *bset2)
7931 return isl_basic_map_is_subset(bset1, bset2);
7934 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7935 __isl_keep isl_basic_map *bmap2)
7937 isl_bool is_subset;
7939 if (!bmap1 || !bmap2)
7940 return isl_bool_error;
7941 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7942 if (is_subset != isl_bool_true)
7943 return is_subset;
7944 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7945 return is_subset;
7948 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7949 __isl_keep isl_basic_set *bset2)
7951 return isl_basic_map_is_equal(
7952 bset_to_bmap(bset1), bset_to_bmap(bset2));
7955 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7957 int i;
7958 int is_empty;
7960 if (!map)
7961 return isl_bool_error;
7962 for (i = 0; i < map->n; ++i) {
7963 is_empty = isl_basic_map_is_empty(map->p[i]);
7964 if (is_empty < 0)
7965 return isl_bool_error;
7966 if (!is_empty)
7967 return isl_bool_false;
7969 return isl_bool_true;
7972 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7974 return map ? map->n == 0 : isl_bool_error;
7977 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7979 return set ? set->n == 0 : isl_bool_error;
7982 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7984 return isl_map_is_empty(set_to_map(set));
7987 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
7988 __isl_keep isl_map *map2)
7990 if (!map1 || !map2)
7991 return isl_bool_error;
7993 return isl_space_is_equal(map1->dim, map2->dim);
7996 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
7997 __isl_keep isl_set *set2)
7999 if (!set1 || !set2)
8000 return isl_bool_error;
8002 return isl_space_is_equal(set1->dim, set2->dim);
8005 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8007 isl_bool is_subset;
8009 if (!map1 || !map2)
8010 return isl_bool_error;
8011 is_subset = isl_map_is_subset(map1, map2);
8012 if (is_subset != isl_bool_true)
8013 return is_subset;
8014 is_subset = isl_map_is_subset(map2, map1);
8015 return is_subset;
8018 /* Is "map1" equal to "map2"?
8020 * First check if they are obviously equal.
8021 * If not, then perform a more detailed analysis.
8023 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8025 isl_bool equal;
8027 equal = isl_map_plain_is_equal(map1, map2);
8028 if (equal < 0 || equal)
8029 return equal;
8030 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8033 isl_bool isl_basic_map_is_strict_subset(
8034 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8036 isl_bool is_subset;
8038 if (!bmap1 || !bmap2)
8039 return isl_bool_error;
8040 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8041 if (is_subset != isl_bool_true)
8042 return is_subset;
8043 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8044 if (is_subset == isl_bool_error)
8045 return is_subset;
8046 return !is_subset;
8049 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8050 __isl_keep isl_map *map2)
8052 isl_bool is_subset;
8054 if (!map1 || !map2)
8055 return isl_bool_error;
8056 is_subset = isl_map_is_subset(map1, map2);
8057 if (is_subset != isl_bool_true)
8058 return is_subset;
8059 is_subset = isl_map_is_subset(map2, map1);
8060 if (is_subset == isl_bool_error)
8061 return is_subset;
8062 return !is_subset;
8065 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8066 __isl_keep isl_set *set2)
8068 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8071 /* Is "bmap" obviously equal to the universe with the same space?
8073 * That is, does it not have any constraints?
8075 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8077 if (!bmap)
8078 return isl_bool_error;
8079 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8082 /* Is "bset" obviously equal to the universe with the same space?
8084 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8086 return isl_basic_map_plain_is_universe(bset);
8089 /* If "c" does not involve any existentially quantified variables,
8090 * then set *univ to false and abort
8092 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8094 isl_bool *univ = user;
8095 unsigned n;
8097 n = isl_constraint_dim(c, isl_dim_div);
8098 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8099 isl_constraint_free(c);
8100 if (*univ < 0 || !*univ)
8101 return isl_stat_error;
8102 return isl_stat_ok;
8105 /* Is "bmap" equal to the universe with the same space?
8107 * First check if it is obviously equal to the universe.
8108 * If not and if there are any constraints not involving
8109 * existentially quantified variables, then it is certainly
8110 * not equal to the universe.
8111 * Otherwise, check if the universe is a subset of "bmap".
8113 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8115 isl_bool univ;
8116 isl_basic_map *test;
8118 univ = isl_basic_map_plain_is_universe(bmap);
8119 if (univ < 0 || univ)
8120 return univ;
8121 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8122 return isl_bool_false;
8123 univ = isl_bool_true;
8124 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8125 univ)
8126 return isl_bool_error;
8127 if (univ < 0 || !univ)
8128 return univ;
8129 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8130 univ = isl_basic_map_is_subset(test, bmap);
8131 isl_basic_map_free(test);
8132 return univ;
8135 /* Is "bset" equal to the universe with the same space?
8137 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8139 return isl_basic_map_is_universe(bset);
8142 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8144 int i;
8146 if (!map)
8147 return isl_bool_error;
8149 for (i = 0; i < map->n; ++i) {
8150 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8151 if (r < 0 || r)
8152 return r;
8155 return isl_bool_false;
8158 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8160 return isl_map_plain_is_universe(set_to_map(set));
8163 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8165 struct isl_basic_set *bset = NULL;
8166 struct isl_vec *sample = NULL;
8167 isl_bool empty, non_empty;
8169 if (!bmap)
8170 return isl_bool_error;
8172 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8173 return isl_bool_true;
8175 if (isl_basic_map_plain_is_universe(bmap))
8176 return isl_bool_false;
8178 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8179 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8180 copy = isl_basic_map_remove_redundancies(copy);
8181 empty = isl_basic_map_plain_is_empty(copy);
8182 isl_basic_map_free(copy);
8183 return empty;
8186 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8187 if (non_empty < 0)
8188 return isl_bool_error;
8189 if (non_empty)
8190 return isl_bool_false;
8191 isl_vec_free(bmap->sample);
8192 bmap->sample = NULL;
8193 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8194 if (!bset)
8195 return isl_bool_error;
8196 sample = isl_basic_set_sample_vec(bset);
8197 if (!sample)
8198 return isl_bool_error;
8199 empty = sample->size == 0;
8200 isl_vec_free(bmap->sample);
8201 bmap->sample = sample;
8202 if (empty)
8203 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8205 return empty;
8208 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8210 if (!bmap)
8211 return isl_bool_error;
8212 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8215 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8217 if (!bset)
8218 return isl_bool_error;
8219 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8222 /* Is "bmap" known to be non-empty?
8224 * That is, is the cached sample still valid?
8226 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8228 unsigned total;
8230 if (!bmap)
8231 return isl_bool_error;
8232 if (!bmap->sample)
8233 return isl_bool_false;
8234 total = 1 + isl_basic_map_total_dim(bmap);
8235 if (bmap->sample->size != total)
8236 return isl_bool_false;
8237 return isl_basic_map_contains(bmap, bmap->sample);
8240 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8242 return isl_basic_map_is_empty(bset_to_bmap(bset));
8245 struct isl_map *isl_basic_map_union(
8246 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8248 struct isl_map *map;
8249 if (!bmap1 || !bmap2)
8250 goto error;
8252 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8254 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8255 if (!map)
8256 goto error;
8257 map = isl_map_add_basic_map(map, bmap1);
8258 map = isl_map_add_basic_map(map, bmap2);
8259 return map;
8260 error:
8261 isl_basic_map_free(bmap1);
8262 isl_basic_map_free(bmap2);
8263 return NULL;
8266 struct isl_set *isl_basic_set_union(
8267 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8269 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8270 bset_to_bmap(bset2)));
8273 /* Order divs such that any div only depends on previous divs */
8274 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8276 int i;
8277 unsigned off;
8279 if (!bmap)
8280 return NULL;
8282 off = isl_space_dim(bmap->dim, isl_dim_all);
8284 for (i = 0; i < bmap->n_div; ++i) {
8285 int pos;
8286 if (isl_int_is_zero(bmap->div[i][0]))
8287 continue;
8288 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8289 bmap->n_div-i);
8290 if (pos == -1)
8291 continue;
8292 if (pos == 0)
8293 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8294 "integer division depends on itself",
8295 return isl_basic_map_free(bmap));
8296 isl_basic_map_swap_div(bmap, i, i + pos);
8297 --i;
8299 return bmap;
8302 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8304 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8307 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8309 int i;
8311 if (!map)
8312 return 0;
8314 for (i = 0; i < map->n; ++i) {
8315 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8316 if (!map->p[i])
8317 goto error;
8320 return map;
8321 error:
8322 isl_map_free(map);
8323 return NULL;
8326 /* Sort the local variables of "bset".
8328 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8329 __isl_take isl_basic_set *bset)
8331 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8334 /* Apply the expansion computed by isl_merge_divs.
8335 * The expansion itself is given by "exp" while the resulting
8336 * list of divs is given by "div".
8338 * Move the integer divisions of "bmap" into the right position
8339 * according to "exp" and then introduce the additional integer
8340 * divisions, adding div constraints.
8341 * The moving should be done first to avoid moving coefficients
8342 * in the definitions of the extra integer divisions.
8344 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8345 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8347 int i, j;
8348 int n_div;
8350 bmap = isl_basic_map_cow(bmap);
8351 if (!bmap || !div)
8352 goto error;
8354 if (div->n_row < bmap->n_div)
8355 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8356 "not an expansion", goto error);
8358 n_div = bmap->n_div;
8359 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8360 div->n_row - n_div, 0,
8361 2 * (div->n_row - n_div));
8363 for (i = n_div; i < div->n_row; ++i)
8364 if (isl_basic_map_alloc_div(bmap) < 0)
8365 goto error;
8367 for (j = n_div - 1; j >= 0; --j) {
8368 if (exp[j] == j)
8369 break;
8370 isl_basic_map_swap_div(bmap, j, exp[j]);
8372 j = 0;
8373 for (i = 0; i < div->n_row; ++i) {
8374 if (j < n_div && exp[j] == i) {
8375 j++;
8376 } else {
8377 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8378 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8379 continue;
8380 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8381 goto error;
8385 isl_mat_free(div);
8386 return bmap;
8387 error:
8388 isl_basic_map_free(bmap);
8389 isl_mat_free(div);
8390 return NULL;
8393 /* Apply the expansion computed by isl_merge_divs.
8394 * The expansion itself is given by "exp" while the resulting
8395 * list of divs is given by "div".
8397 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8398 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8400 return isl_basic_map_expand_divs(bset, div, exp);
8403 /* Look for a div in dst that corresponds to the div "div" in src.
8404 * The divs before "div" in src and dst are assumed to be the same.
8406 * Returns -1 if no corresponding div was found and the position
8407 * of the corresponding div in dst otherwise.
8409 static int find_div(struct isl_basic_map *dst,
8410 struct isl_basic_map *src, unsigned div)
8412 int i;
8414 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8416 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8417 for (i = div; i < dst->n_div; ++i)
8418 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8419 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8420 dst->n_div - div) == -1)
8421 return i;
8422 return -1;
8425 /* Align the divs of "dst" to those of "src", adding divs from "src"
8426 * if needed. That is, make sure that the first src->n_div divs
8427 * of the result are equal to those of src.
8429 * The result is not finalized as by design it will have redundant
8430 * divs if any divs from "src" were copied.
8432 __isl_give isl_basic_map *isl_basic_map_align_divs(
8433 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8435 int i;
8436 int known, extended;
8437 unsigned total;
8439 if (!dst || !src)
8440 return isl_basic_map_free(dst);
8442 if (src->n_div == 0)
8443 return dst;
8445 known = isl_basic_map_divs_known(src);
8446 if (known < 0)
8447 return isl_basic_map_free(dst);
8448 if (!known)
8449 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8450 "some src divs are unknown",
8451 return isl_basic_map_free(dst));
8453 src = isl_basic_map_order_divs(src);
8455 extended = 0;
8456 total = isl_space_dim(src->dim, isl_dim_all);
8457 for (i = 0; i < src->n_div; ++i) {
8458 int j = find_div(dst, src, i);
8459 if (j < 0) {
8460 if (!extended) {
8461 int extra = src->n_div - i;
8462 dst = isl_basic_map_cow(dst);
8463 if (!dst)
8464 return NULL;
8465 dst = isl_basic_map_extend_space(dst,
8466 isl_space_copy(dst->dim),
8467 extra, 0, 2 * extra);
8468 extended = 1;
8470 j = isl_basic_map_alloc_div(dst);
8471 if (j < 0)
8472 return isl_basic_map_free(dst);
8473 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8474 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8475 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8476 return isl_basic_map_free(dst);
8478 if (j != i)
8479 isl_basic_map_swap_div(dst, i, j);
8481 return dst;
8484 struct isl_map *isl_map_align_divs(struct isl_map *map)
8486 int i;
8488 if (!map)
8489 return NULL;
8490 if (map->n == 0)
8491 return map;
8492 map = isl_map_compute_divs(map);
8493 map = isl_map_cow(map);
8494 if (!map)
8495 return NULL;
8497 for (i = 1; i < map->n; ++i)
8498 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8499 for (i = 1; i < map->n; ++i) {
8500 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8501 if (!map->p[i])
8502 return isl_map_free(map);
8505 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8506 return map;
8509 struct isl_set *isl_set_align_divs(struct isl_set *set)
8511 return set_from_map(isl_map_align_divs(set_to_map(set)));
8514 /* Align the divs of the basic maps in "map" to those
8515 * of the basic maps in "list", as well as to the other basic maps in "map".
8516 * The elements in "list" are assumed to have known divs.
8518 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8519 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8521 int i, n;
8523 map = isl_map_compute_divs(map);
8524 map = isl_map_cow(map);
8525 if (!map || !list)
8526 return isl_map_free(map);
8527 if (map->n == 0)
8528 return map;
8530 n = isl_basic_map_list_n_basic_map(list);
8531 for (i = 0; i < n; ++i) {
8532 isl_basic_map *bmap;
8534 bmap = isl_basic_map_list_get_basic_map(list, i);
8535 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8536 isl_basic_map_free(bmap);
8538 if (!map->p[0])
8539 return isl_map_free(map);
8541 return isl_map_align_divs(map);
8544 /* Align the divs of each element of "list" to those of "bmap".
8545 * Both "bmap" and the elements of "list" are assumed to have known divs.
8547 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8548 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8550 int i, n;
8552 if (!list || !bmap)
8553 return isl_basic_map_list_free(list);
8555 n = isl_basic_map_list_n_basic_map(list);
8556 for (i = 0; i < n; ++i) {
8557 isl_basic_map *bmap_i;
8559 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8560 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8561 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8564 return list;
8567 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8568 __isl_take isl_map *map)
8570 isl_bool ok;
8572 ok = isl_map_compatible_domain(map, set);
8573 if (ok < 0)
8574 goto error;
8575 if (!ok)
8576 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8577 "incompatible spaces", goto error);
8578 map = isl_map_intersect_domain(map, set);
8579 set = isl_map_range(map);
8580 return set;
8581 error:
8582 isl_set_free(set);
8583 isl_map_free(map);
8584 return NULL;
8587 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8588 __isl_take isl_map *map)
8590 return isl_map_align_params_map_map_and(set, map, &set_apply);
8593 /* There is no need to cow as removing empty parts doesn't change
8594 * the meaning of the set.
8596 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8598 int i;
8600 if (!map)
8601 return NULL;
8603 for (i = map->n - 1; i >= 0; --i)
8604 remove_if_empty(map, i);
8606 return map;
8609 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8611 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8614 /* Given two basic sets bset1 and bset2, compute the maximal difference
8615 * between the values of dimension pos in bset1 and those in bset2
8616 * for any common value of the parameters and dimensions preceding pos.
8618 static enum isl_lp_result basic_set_maximal_difference_at(
8619 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8620 int pos, isl_int *opt)
8622 isl_basic_map *bmap1;
8623 isl_basic_map *bmap2;
8624 struct isl_ctx *ctx;
8625 struct isl_vec *obj;
8626 unsigned total;
8627 unsigned nparam;
8628 unsigned dim1;
8629 enum isl_lp_result res;
8631 if (!bset1 || !bset2)
8632 return isl_lp_error;
8634 nparam = isl_basic_set_n_param(bset1);
8635 dim1 = isl_basic_set_n_dim(bset1);
8637 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8638 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8639 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8640 isl_dim_out, 0, pos);
8641 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8642 isl_dim_out, 0, pos);
8643 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8644 if (!bmap1)
8645 return isl_lp_error;
8647 total = isl_basic_map_total_dim(bmap1);
8648 ctx = bmap1->ctx;
8649 obj = isl_vec_alloc(ctx, 1 + total);
8650 if (!obj)
8651 goto error;
8652 isl_seq_clr(obj->block.data, 1 + total);
8653 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8654 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8655 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8656 opt, NULL, NULL);
8657 isl_basic_map_free(bmap1);
8658 isl_vec_free(obj);
8659 return res;
8660 error:
8661 isl_basic_map_free(bmap1);
8662 return isl_lp_error;
8665 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8666 * for any common value of the parameters and dimensions preceding pos
8667 * in both basic sets, the values of dimension pos in bset1 are
8668 * smaller or larger than those in bset2.
8670 * Returns
8671 * 1 if bset1 follows bset2
8672 * -1 if bset1 precedes bset2
8673 * 0 if bset1 and bset2 are incomparable
8674 * -2 if some error occurred.
8676 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8677 struct isl_basic_set *bset2, int pos)
8679 isl_int opt;
8680 enum isl_lp_result res;
8681 int cmp;
8683 isl_int_init(opt);
8685 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8687 if (res == isl_lp_empty)
8688 cmp = 0;
8689 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8690 res == isl_lp_unbounded)
8691 cmp = 1;
8692 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8693 cmp = -1;
8694 else
8695 cmp = -2;
8697 isl_int_clear(opt);
8698 return cmp;
8701 /* Given two basic sets bset1 and bset2, check whether
8702 * for any common value of the parameters and dimensions preceding pos
8703 * there is a value of dimension pos in bset1 that is larger
8704 * than a value of the same dimension in bset2.
8706 * Return
8707 * 1 if there exists such a pair
8708 * 0 if there is no such pair, but there is a pair of equal values
8709 * -1 otherwise
8710 * -2 if some error occurred.
8712 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8713 __isl_keep isl_basic_set *bset2, int pos)
8715 isl_int opt;
8716 enum isl_lp_result res;
8717 int cmp;
8719 isl_int_init(opt);
8721 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8723 if (res == isl_lp_empty)
8724 cmp = -1;
8725 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8726 res == isl_lp_unbounded)
8727 cmp = 1;
8728 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8729 cmp = -1;
8730 else if (res == isl_lp_ok)
8731 cmp = 0;
8732 else
8733 cmp = -2;
8735 isl_int_clear(opt);
8736 return cmp;
8739 /* Given two sets set1 and set2, check whether
8740 * for any common value of the parameters and dimensions preceding pos
8741 * there is a value of dimension pos in set1 that is larger
8742 * than a value of the same dimension in set2.
8744 * Return
8745 * 1 if there exists such a pair
8746 * 0 if there is no such pair, but there is a pair of equal values
8747 * -1 otherwise
8748 * -2 if some error occurred.
8750 int isl_set_follows_at(__isl_keep isl_set *set1,
8751 __isl_keep isl_set *set2, int pos)
8753 int i, j;
8754 int follows = -1;
8756 if (!set1 || !set2)
8757 return -2;
8759 for (i = 0; i < set1->n; ++i)
8760 for (j = 0; j < set2->n; ++j) {
8761 int f;
8762 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8763 if (f == 1 || f == -2)
8764 return f;
8765 if (f > follows)
8766 follows = f;
8769 return follows;
8772 static isl_bool isl_basic_map_plain_has_fixed_var(
8773 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
8775 int i;
8776 int d;
8777 unsigned total;
8779 if (!bmap)
8780 return isl_bool_error;
8781 total = isl_basic_map_total_dim(bmap);
8782 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8783 for (; d+1 > pos; --d)
8784 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8785 break;
8786 if (d != pos)
8787 continue;
8788 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8789 return isl_bool_false;
8790 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8791 return isl_bool_false;
8792 if (!isl_int_is_one(bmap->eq[i][1+d]))
8793 return isl_bool_false;
8794 if (val)
8795 isl_int_neg(*val, bmap->eq[i][0]);
8796 return isl_bool_true;
8798 return isl_bool_false;
8801 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8802 unsigned pos, isl_int *val)
8804 int i;
8805 isl_int v;
8806 isl_int tmp;
8807 isl_bool fixed;
8809 if (!map)
8810 return isl_bool_error;
8811 if (map->n == 0)
8812 return isl_bool_false;
8813 if (map->n == 1)
8814 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8815 isl_int_init(v);
8816 isl_int_init(tmp);
8817 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8818 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
8819 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8820 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
8821 fixed = isl_bool_false;
8823 if (val)
8824 isl_int_set(*val, v);
8825 isl_int_clear(tmp);
8826 isl_int_clear(v);
8827 return fixed;
8830 static isl_bool isl_basic_set_plain_has_fixed_var(
8831 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
8833 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
8834 pos, val);
8837 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8838 enum isl_dim_type type, unsigned pos, isl_int *val)
8840 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
8841 return isl_bool_error;
8842 return isl_basic_map_plain_has_fixed_var(bmap,
8843 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8846 /* If "bmap" obviously lies on a hyperplane where the given dimension
8847 * has a fixed value, then return that value.
8848 * Otherwise return NaN.
8850 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8851 __isl_keep isl_basic_map *bmap,
8852 enum isl_dim_type type, unsigned pos)
8854 isl_ctx *ctx;
8855 isl_val *v;
8856 isl_bool fixed;
8858 if (!bmap)
8859 return NULL;
8860 ctx = isl_basic_map_get_ctx(bmap);
8861 v = isl_val_alloc(ctx);
8862 if (!v)
8863 return NULL;
8864 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8865 if (fixed < 0)
8866 return isl_val_free(v);
8867 if (fixed) {
8868 isl_int_set_si(v->d, 1);
8869 return v;
8871 isl_val_free(v);
8872 return isl_val_nan(ctx);
8875 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
8876 enum isl_dim_type type, unsigned pos, isl_int *val)
8878 if (pos >= isl_map_dim(map, type))
8879 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8880 "position out of bounds", return isl_bool_error);
8881 return isl_map_plain_has_fixed_var(map,
8882 map_offset(map, type) - 1 + pos, val);
8885 /* If "map" obviously lies on a hyperplane where the given dimension
8886 * has a fixed value, then return that value.
8887 * Otherwise return NaN.
8889 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8890 enum isl_dim_type type, unsigned pos)
8892 isl_ctx *ctx;
8893 isl_val *v;
8894 isl_bool fixed;
8896 if (!map)
8897 return NULL;
8898 ctx = isl_map_get_ctx(map);
8899 v = isl_val_alloc(ctx);
8900 if (!v)
8901 return NULL;
8902 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8903 if (fixed < 0)
8904 return isl_val_free(v);
8905 if (fixed) {
8906 isl_int_set_si(v->d, 1);
8907 return v;
8909 isl_val_free(v);
8910 return isl_val_nan(ctx);
8913 /* If "set" obviously lies on a hyperplane where the given dimension
8914 * has a fixed value, then return that value.
8915 * Otherwise return NaN.
8917 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8918 enum isl_dim_type type, unsigned pos)
8920 return isl_map_plain_get_val_if_fixed(set, type, pos);
8923 isl_bool isl_set_plain_is_fixed(__isl_keep isl_set *set,
8924 enum isl_dim_type type, unsigned pos, isl_int *val)
8926 return isl_map_plain_is_fixed(set, type, pos, val);
8929 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8930 * then return this fixed value in *val.
8932 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8933 unsigned dim, isl_int *val)
8935 return isl_basic_set_plain_has_fixed_var(bset,
8936 isl_basic_set_n_param(bset) + dim, val);
8939 /* Return -1 if the constraint "c1" should be sorted before "c2"
8940 * and 1 if it should be sorted after "c2".
8941 * Return 0 if the two constraints are the same (up to the constant term).
8943 * In particular, if a constraint involves later variables than another
8944 * then it is sorted after this other constraint.
8945 * uset_gist depends on constraints without existentially quantified
8946 * variables sorting first.
8948 * For constraints that have the same latest variable, those
8949 * with the same coefficient for this latest variable (first in absolute value
8950 * and then in actual value) are grouped together.
8951 * This is useful for detecting pairs of constraints that can
8952 * be chained in their printed representation.
8954 * Finally, within a group, constraints are sorted according to
8955 * their coefficients (excluding the constant term).
8957 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8959 isl_int **c1 = (isl_int **) p1;
8960 isl_int **c2 = (isl_int **) p2;
8961 int l1, l2;
8962 unsigned size = *(unsigned *) arg;
8963 int cmp;
8965 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8966 l2 = isl_seq_last_non_zero(*c2 + 1, size);
8968 if (l1 != l2)
8969 return l1 - l2;
8971 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
8972 if (cmp != 0)
8973 return cmp;
8974 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
8975 if (cmp != 0)
8976 return -cmp;
8978 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
8981 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
8982 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
8983 * and 0 if the two constraints are the same (up to the constant term).
8985 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
8986 isl_int *c1, isl_int *c2)
8988 unsigned total;
8990 if (!bmap)
8991 return -2;
8992 total = isl_basic_map_total_dim(bmap);
8993 return sort_constraint_cmp(&c1, &c2, &total);
8996 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
8997 __isl_take isl_basic_map *bmap)
8999 unsigned total;
9001 if (!bmap)
9002 return NULL;
9003 if (bmap->n_ineq == 0)
9004 return bmap;
9005 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9006 return bmap;
9007 total = isl_basic_map_total_dim(bmap);
9008 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9009 &sort_constraint_cmp, &total) < 0)
9010 return isl_basic_map_free(bmap);
9011 return bmap;
9014 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9015 __isl_take isl_basic_set *bset)
9017 isl_basic_map *bmap = bset_to_bmap(bset);
9018 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9021 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9023 if (!bmap)
9024 return NULL;
9025 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9026 return bmap;
9027 bmap = isl_basic_map_remove_redundancies(bmap);
9028 bmap = isl_basic_map_sort_constraints(bmap);
9029 if (bmap)
9030 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9031 return bmap;
9033 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9034 __isl_keep isl_basic_map *bmap2)
9036 int i, cmp;
9037 unsigned total;
9039 if (!bmap1 || !bmap2)
9040 return -1;
9042 if (bmap1 == bmap2)
9043 return 0;
9044 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9045 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9046 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9047 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9048 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9049 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9050 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9051 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9052 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9053 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9054 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9055 return 0;
9056 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9057 return 1;
9058 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9059 return -1;
9060 if (bmap1->n_eq != bmap2->n_eq)
9061 return bmap1->n_eq - bmap2->n_eq;
9062 if (bmap1->n_ineq != bmap2->n_ineq)
9063 return bmap1->n_ineq - bmap2->n_ineq;
9064 if (bmap1->n_div != bmap2->n_div)
9065 return bmap1->n_div - bmap2->n_div;
9066 total = isl_basic_map_total_dim(bmap1);
9067 for (i = 0; i < bmap1->n_eq; ++i) {
9068 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9069 if (cmp)
9070 return cmp;
9072 for (i = 0; i < bmap1->n_ineq; ++i) {
9073 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9074 if (cmp)
9075 return cmp;
9077 for (i = 0; i < bmap1->n_div; ++i) {
9078 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9079 if (cmp)
9080 return cmp;
9082 return 0;
9085 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9086 __isl_keep isl_basic_set *bset2)
9088 return isl_basic_map_plain_cmp(bset1, bset2);
9091 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9093 int i, cmp;
9095 if (set1 == set2)
9096 return 0;
9097 if (set1->n != set2->n)
9098 return set1->n - set2->n;
9100 for (i = 0; i < set1->n; ++i) {
9101 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9102 if (cmp)
9103 return cmp;
9106 return 0;
9109 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9110 __isl_keep isl_basic_map *bmap2)
9112 if (!bmap1 || !bmap2)
9113 return isl_bool_error;
9114 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9117 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9118 __isl_keep isl_basic_set *bset2)
9120 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9121 bset_to_bmap(bset2));
9124 static int qsort_bmap_cmp(const void *p1, const void *p2)
9126 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9127 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9129 return isl_basic_map_plain_cmp(bmap1, bmap2);
9132 /* Sort the basic maps of "map" and remove duplicate basic maps.
9134 * While removing basic maps, we make sure that the basic maps remain
9135 * sorted because isl_map_normalize expects the basic maps of the result
9136 * to be sorted.
9138 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9140 int i, j;
9142 map = isl_map_remove_empty_parts(map);
9143 if (!map)
9144 return NULL;
9145 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9146 for (i = map->n - 1; i >= 1; --i) {
9147 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9148 continue;
9149 isl_basic_map_free(map->p[i-1]);
9150 for (j = i; j < map->n; ++j)
9151 map->p[j - 1] = map->p[j];
9152 map->n--;
9155 return map;
9158 /* Remove obvious duplicates among the basic maps of "map".
9160 * Unlike isl_map_normalize, this function does not remove redundant
9161 * constraints and only removes duplicates that have exactly the same
9162 * constraints in the input. It does sort the constraints and
9163 * the basic maps to ease the detection of duplicates.
9165 * If "map" has already been normalized or if the basic maps are
9166 * disjoint, then there can be no duplicates.
9168 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9170 int i;
9171 isl_basic_map *bmap;
9173 if (!map)
9174 return NULL;
9175 if (map->n <= 1)
9176 return map;
9177 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9178 return map;
9179 for (i = 0; i < map->n; ++i) {
9180 bmap = isl_basic_map_copy(map->p[i]);
9181 bmap = isl_basic_map_sort_constraints(bmap);
9182 if (!bmap)
9183 return isl_map_free(map);
9184 isl_basic_map_free(map->p[i]);
9185 map->p[i] = bmap;
9188 map = sort_and_remove_duplicates(map);
9189 return map;
9192 /* We normalize in place, but if anything goes wrong we need
9193 * to return NULL, so we need to make sure we don't change the
9194 * meaning of any possible other copies of map.
9196 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9198 int i;
9199 struct isl_basic_map *bmap;
9201 if (!map)
9202 return NULL;
9203 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9204 return map;
9205 for (i = 0; i < map->n; ++i) {
9206 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9207 if (!bmap)
9208 goto error;
9209 isl_basic_map_free(map->p[i]);
9210 map->p[i] = bmap;
9213 map = sort_and_remove_duplicates(map);
9214 if (map)
9215 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9216 return map;
9217 error:
9218 isl_map_free(map);
9219 return NULL;
9222 struct isl_set *isl_set_normalize(struct isl_set *set)
9224 return set_from_map(isl_map_normalize(set_to_map(set)));
9227 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9228 __isl_keep isl_map *map2)
9230 int i;
9231 isl_bool equal;
9233 if (!map1 || !map2)
9234 return isl_bool_error;
9236 if (map1 == map2)
9237 return isl_bool_true;
9238 if (!isl_space_is_equal(map1->dim, map2->dim))
9239 return isl_bool_false;
9241 map1 = isl_map_copy(map1);
9242 map2 = isl_map_copy(map2);
9243 map1 = isl_map_normalize(map1);
9244 map2 = isl_map_normalize(map2);
9245 if (!map1 || !map2)
9246 goto error;
9247 equal = map1->n == map2->n;
9248 for (i = 0; equal && i < map1->n; ++i) {
9249 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9250 if (equal < 0)
9251 goto error;
9253 isl_map_free(map1);
9254 isl_map_free(map2);
9255 return equal;
9256 error:
9257 isl_map_free(map1);
9258 isl_map_free(map2);
9259 return isl_bool_error;
9262 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9263 __isl_keep isl_set *set2)
9265 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9268 /* Return the basic maps in "map" as a list.
9270 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9271 __isl_keep isl_map *map)
9273 int i;
9274 isl_ctx *ctx;
9275 isl_basic_map_list *list;
9277 if (!map)
9278 return NULL;
9279 ctx = isl_map_get_ctx(map);
9280 list = isl_basic_map_list_alloc(ctx, map->n);
9282 for (i = 0; i < map->n; ++i) {
9283 isl_basic_map *bmap;
9285 bmap = isl_basic_map_copy(map->p[i]);
9286 list = isl_basic_map_list_add(list, bmap);
9289 return list;
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_map *isl_basic_map_list_intersect(
9296 __isl_take isl_basic_map_list *list)
9298 int i, n;
9299 isl_basic_map *bmap;
9301 if (!list)
9302 return NULL;
9303 n = isl_basic_map_list_n_basic_map(list);
9304 if (n < 1)
9305 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9306 "expecting non-empty list", goto error);
9308 bmap = isl_basic_map_list_get_basic_map(list, 0);
9309 for (i = 1; i < n; ++i) {
9310 isl_basic_map *bmap_i;
9312 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9313 bmap = isl_basic_map_intersect(bmap, bmap_i);
9316 isl_basic_map_list_free(list);
9317 return bmap;
9318 error:
9319 isl_basic_map_list_free(list);
9320 return NULL;
9323 /* Return the intersection of the elements in the non-empty list "list".
9324 * All elements are assumed to live in the same space.
9326 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9327 __isl_take isl_basic_set_list *list)
9329 return isl_basic_map_list_intersect(list);
9332 /* Return the union of the elements of "list".
9333 * The list is required to have at least one element.
9335 __isl_give isl_set *isl_basic_set_list_union(
9336 __isl_take isl_basic_set_list *list)
9338 int i, n;
9339 isl_space *space;
9340 isl_basic_set *bset;
9341 isl_set *set;
9343 if (!list)
9344 return NULL;
9345 n = isl_basic_set_list_n_basic_set(list);
9346 if (n < 1)
9347 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9348 "expecting non-empty list", goto error);
9350 bset = isl_basic_set_list_get_basic_set(list, 0);
9351 space = isl_basic_set_get_space(bset);
9352 isl_basic_set_free(bset);
9354 set = isl_set_alloc_space(space, n, 0);
9355 for (i = 0; i < n; ++i) {
9356 bset = isl_basic_set_list_get_basic_set(list, i);
9357 set = isl_set_add_basic_set(set, bset);
9360 isl_basic_set_list_free(list);
9361 return set;
9362 error:
9363 isl_basic_set_list_free(list);
9364 return NULL;
9367 /* Return the union of the elements in the non-empty list "list".
9368 * All elements are assumed to live in the same space.
9370 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9372 int i, n;
9373 isl_set *set;
9375 if (!list)
9376 return NULL;
9377 n = isl_set_list_n_set(list);
9378 if (n < 1)
9379 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9380 "expecting non-empty list", goto error);
9382 set = isl_set_list_get_set(list, 0);
9383 for (i = 1; i < n; ++i) {
9384 isl_set *set_i;
9386 set_i = isl_set_list_get_set(list, i);
9387 set = isl_set_union(set, set_i);
9390 isl_set_list_free(list);
9391 return set;
9392 error:
9393 isl_set_list_free(list);
9394 return NULL;
9397 struct isl_basic_map *isl_basic_map_product(
9398 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9400 isl_space *dim_result = NULL;
9401 struct isl_basic_map *bmap;
9402 unsigned in1, in2, out1, out2, nparam, total, pos;
9403 struct isl_dim_map *dim_map1, *dim_map2;
9405 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9406 goto error;
9407 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9408 isl_space_copy(bmap2->dim));
9410 in1 = isl_basic_map_n_in(bmap1);
9411 in2 = isl_basic_map_n_in(bmap2);
9412 out1 = isl_basic_map_n_out(bmap1);
9413 out2 = isl_basic_map_n_out(bmap2);
9414 nparam = isl_basic_map_n_param(bmap1);
9416 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9417 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9418 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9419 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9420 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9421 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9422 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9423 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9424 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9425 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9426 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9428 bmap = isl_basic_map_alloc_space(dim_result,
9429 bmap1->n_div + bmap2->n_div,
9430 bmap1->n_eq + bmap2->n_eq,
9431 bmap1->n_ineq + bmap2->n_ineq);
9432 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9433 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9434 bmap = isl_basic_map_simplify(bmap);
9435 return isl_basic_map_finalize(bmap);
9436 error:
9437 isl_basic_map_free(bmap1);
9438 isl_basic_map_free(bmap2);
9439 return NULL;
9442 __isl_give isl_basic_map *isl_basic_map_flat_product(
9443 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9445 isl_basic_map *prod;
9447 prod = isl_basic_map_product(bmap1, bmap2);
9448 prod = isl_basic_map_flatten(prod);
9449 return prod;
9452 __isl_give isl_basic_set *isl_basic_set_flat_product(
9453 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9455 return isl_basic_map_flat_range_product(bset1, bset2);
9458 __isl_give isl_basic_map *isl_basic_map_domain_product(
9459 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9461 isl_space *space_result = NULL;
9462 isl_basic_map *bmap;
9463 unsigned in1, in2, out, nparam, total, pos;
9464 struct isl_dim_map *dim_map1, *dim_map2;
9466 if (!bmap1 || !bmap2)
9467 goto error;
9469 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9470 isl_space_copy(bmap2->dim));
9472 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9473 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9474 out = isl_basic_map_dim(bmap1, isl_dim_out);
9475 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9477 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9478 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9479 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9480 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9481 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9482 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9483 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9484 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9485 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9486 isl_dim_map_div(dim_map1, bmap1, pos += out);
9487 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9489 bmap = isl_basic_map_alloc_space(space_result,
9490 bmap1->n_div + bmap2->n_div,
9491 bmap1->n_eq + bmap2->n_eq,
9492 bmap1->n_ineq + bmap2->n_ineq);
9493 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9494 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9495 bmap = isl_basic_map_simplify(bmap);
9496 return isl_basic_map_finalize(bmap);
9497 error:
9498 isl_basic_map_free(bmap1);
9499 isl_basic_map_free(bmap2);
9500 return NULL;
9503 __isl_give isl_basic_map *isl_basic_map_range_product(
9504 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9506 isl_bool rational;
9507 isl_space *dim_result = NULL;
9508 isl_basic_map *bmap;
9509 unsigned in, out1, out2, nparam, total, pos;
9510 struct isl_dim_map *dim_map1, *dim_map2;
9512 rational = isl_basic_map_is_rational(bmap1);
9513 if (rational >= 0 && rational)
9514 rational = isl_basic_map_is_rational(bmap2);
9515 if (!bmap1 || !bmap2 || rational < 0)
9516 goto error;
9518 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9519 goto error;
9521 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9522 isl_space_copy(bmap2->dim));
9524 in = isl_basic_map_dim(bmap1, isl_dim_in);
9525 out1 = isl_basic_map_n_out(bmap1);
9526 out2 = isl_basic_map_n_out(bmap2);
9527 nparam = isl_basic_map_n_param(bmap1);
9529 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9530 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9531 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9532 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9533 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9534 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9535 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9536 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9537 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9538 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9539 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9541 bmap = isl_basic_map_alloc_space(dim_result,
9542 bmap1->n_div + bmap2->n_div,
9543 bmap1->n_eq + bmap2->n_eq,
9544 bmap1->n_ineq + bmap2->n_ineq);
9545 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9546 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9547 if (rational)
9548 bmap = isl_basic_map_set_rational(bmap);
9549 bmap = isl_basic_map_simplify(bmap);
9550 return isl_basic_map_finalize(bmap);
9551 error:
9552 isl_basic_map_free(bmap1);
9553 isl_basic_map_free(bmap2);
9554 return NULL;
9557 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9558 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9560 isl_basic_map *prod;
9562 prod = isl_basic_map_range_product(bmap1, bmap2);
9563 prod = isl_basic_map_flatten_range(prod);
9564 return prod;
9567 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9568 * and collect the results.
9569 * The result live in the space obtained by calling "space_product"
9570 * on the spaces of "map1" and "map2".
9571 * If "remove_duplicates" is set then the result may contain duplicates
9572 * (even if the inputs do not) and so we try and remove the obvious
9573 * duplicates.
9575 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9576 __isl_take isl_map *map2,
9577 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9578 __isl_take isl_space *right),
9579 __isl_give isl_basic_map *(*basic_map_product)(
9580 __isl_take isl_basic_map *left,
9581 __isl_take isl_basic_map *right),
9582 int remove_duplicates)
9584 unsigned flags = 0;
9585 struct isl_map *result;
9586 int i, j;
9587 isl_bool m;
9589 m = isl_map_has_equal_params(map1, map2);
9590 if (m < 0)
9591 goto error;
9592 if (!m)
9593 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
9594 "parameters don't match", goto error);
9596 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9597 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9598 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9600 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9601 isl_space_copy(map2->dim)),
9602 map1->n * map2->n, flags);
9603 if (!result)
9604 goto error;
9605 for (i = 0; i < map1->n; ++i)
9606 for (j = 0; j < map2->n; ++j) {
9607 struct isl_basic_map *part;
9608 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9609 isl_basic_map_copy(map2->p[j]));
9610 if (isl_basic_map_is_empty(part))
9611 isl_basic_map_free(part);
9612 else
9613 result = isl_map_add_basic_map(result, part);
9614 if (!result)
9615 goto error;
9617 if (remove_duplicates)
9618 result = isl_map_remove_obvious_duplicates(result);
9619 isl_map_free(map1);
9620 isl_map_free(map2);
9621 return result;
9622 error:
9623 isl_map_free(map1);
9624 isl_map_free(map2);
9625 return NULL;
9628 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9630 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9631 __isl_take isl_map *map2)
9633 return map_product(map1, map2, &isl_space_product,
9634 &isl_basic_map_product, 0);
9637 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9638 __isl_take isl_map *map2)
9640 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9643 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9645 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9646 __isl_take isl_map *map2)
9648 isl_map *prod;
9650 prod = isl_map_product(map1, map2);
9651 prod = isl_map_flatten(prod);
9652 return prod;
9655 /* Given two set A and B, construct its Cartesian product A x B.
9657 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9659 return isl_map_range_product(set1, set2);
9662 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9663 __isl_take isl_set *set2)
9665 return isl_map_flat_range_product(set1, set2);
9668 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9670 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9671 __isl_take isl_map *map2)
9673 return map_product(map1, map2, &isl_space_domain_product,
9674 &isl_basic_map_domain_product, 1);
9677 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9679 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9680 __isl_take isl_map *map2)
9682 return map_product(map1, map2, &isl_space_range_product,
9683 &isl_basic_map_range_product, 1);
9686 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9687 __isl_take isl_map *map2)
9689 return isl_map_align_params_map_map_and(map1, map2,
9690 &map_domain_product_aligned);
9693 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9694 __isl_take isl_map *map2)
9696 return isl_map_align_params_map_map_and(map1, map2,
9697 &map_range_product_aligned);
9700 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9702 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9704 isl_space *space;
9705 int total1, keep1, total2, keep2;
9707 if (!map)
9708 return NULL;
9709 if (!isl_space_domain_is_wrapping(map->dim) ||
9710 !isl_space_range_is_wrapping(map->dim))
9711 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9712 "not a product", return isl_map_free(map));
9714 space = isl_map_get_space(map);
9715 total1 = isl_space_dim(space, isl_dim_in);
9716 total2 = isl_space_dim(space, isl_dim_out);
9717 space = isl_space_factor_domain(space);
9718 keep1 = isl_space_dim(space, isl_dim_in);
9719 keep2 = isl_space_dim(space, isl_dim_out);
9720 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9721 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9722 map = isl_map_reset_space(map, space);
9724 return map;
9727 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9729 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9731 isl_space *space;
9732 int total1, keep1, total2, keep2;
9734 if (!map)
9735 return NULL;
9736 if (!isl_space_domain_is_wrapping(map->dim) ||
9737 !isl_space_range_is_wrapping(map->dim))
9738 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9739 "not a product", return isl_map_free(map));
9741 space = isl_map_get_space(map);
9742 total1 = isl_space_dim(space, isl_dim_in);
9743 total2 = isl_space_dim(space, isl_dim_out);
9744 space = isl_space_factor_range(space);
9745 keep1 = isl_space_dim(space, isl_dim_in);
9746 keep2 = isl_space_dim(space, isl_dim_out);
9747 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9748 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9749 map = isl_map_reset_space(map, space);
9751 return map;
9754 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9756 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9758 isl_space *space;
9759 int total, keep;
9761 if (!map)
9762 return NULL;
9763 if (!isl_space_domain_is_wrapping(map->dim))
9764 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9765 "domain is not a product", return isl_map_free(map));
9767 space = isl_map_get_space(map);
9768 total = isl_space_dim(space, isl_dim_in);
9769 space = isl_space_domain_factor_domain(space);
9770 keep = isl_space_dim(space, isl_dim_in);
9771 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9772 map = isl_map_reset_space(map, space);
9774 return map;
9777 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9779 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9781 isl_space *space;
9782 int total, keep;
9784 if (!map)
9785 return NULL;
9786 if (!isl_space_domain_is_wrapping(map->dim))
9787 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9788 "domain is not a product", return isl_map_free(map));
9790 space = isl_map_get_space(map);
9791 total = isl_space_dim(space, isl_dim_in);
9792 space = isl_space_domain_factor_range(space);
9793 keep = isl_space_dim(space, isl_dim_in);
9794 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9795 map = isl_map_reset_space(map, space);
9797 return map;
9800 /* Given a map A -> [B -> C], extract the map A -> B.
9802 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9804 isl_space *space;
9805 int total, keep;
9807 if (!map)
9808 return NULL;
9809 if (!isl_space_range_is_wrapping(map->dim))
9810 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9811 "range is not a product", return isl_map_free(map));
9813 space = isl_map_get_space(map);
9814 total = isl_space_dim(space, isl_dim_out);
9815 space = isl_space_range_factor_domain(space);
9816 keep = isl_space_dim(space, isl_dim_out);
9817 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9818 map = isl_map_reset_space(map, space);
9820 return map;
9823 /* Given a map A -> [B -> C], extract the map A -> C.
9825 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9827 isl_space *space;
9828 int total, keep;
9830 if (!map)
9831 return NULL;
9832 if (!isl_space_range_is_wrapping(map->dim))
9833 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9834 "range is not a product", return isl_map_free(map));
9836 space = isl_map_get_space(map);
9837 total = isl_space_dim(space, isl_dim_out);
9838 space = isl_space_range_factor_range(space);
9839 keep = isl_space_dim(space, isl_dim_out);
9840 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9841 map = isl_map_reset_space(map, space);
9843 return map;
9846 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9848 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9849 __isl_take isl_map *map2)
9851 isl_map *prod;
9853 prod = isl_map_domain_product(map1, map2);
9854 prod = isl_map_flatten_domain(prod);
9855 return prod;
9858 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9860 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9861 __isl_take isl_map *map2)
9863 isl_map *prod;
9865 prod = isl_map_range_product(map1, map2);
9866 prod = isl_map_flatten_range(prod);
9867 return prod;
9870 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9872 int i;
9873 uint32_t hash = isl_hash_init();
9874 unsigned total;
9876 if (!bmap)
9877 return 0;
9878 bmap = isl_basic_map_copy(bmap);
9879 bmap = isl_basic_map_normalize(bmap);
9880 if (!bmap)
9881 return 0;
9882 total = isl_basic_map_total_dim(bmap);
9883 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9884 for (i = 0; i < bmap->n_eq; ++i) {
9885 uint32_t c_hash;
9886 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9887 isl_hash_hash(hash, c_hash);
9889 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9890 for (i = 0; i < bmap->n_ineq; ++i) {
9891 uint32_t c_hash;
9892 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9893 isl_hash_hash(hash, c_hash);
9895 isl_hash_byte(hash, bmap->n_div & 0xFF);
9896 for (i = 0; i < bmap->n_div; ++i) {
9897 uint32_t c_hash;
9898 if (isl_int_is_zero(bmap->div[i][0]))
9899 continue;
9900 isl_hash_byte(hash, i & 0xFF);
9901 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9902 isl_hash_hash(hash, c_hash);
9904 isl_basic_map_free(bmap);
9905 return hash;
9908 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9910 return isl_basic_map_get_hash(bset_to_bmap(bset));
9913 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9915 int i;
9916 uint32_t hash;
9918 if (!map)
9919 return 0;
9920 map = isl_map_copy(map);
9921 map = isl_map_normalize(map);
9922 if (!map)
9923 return 0;
9925 hash = isl_hash_init();
9926 for (i = 0; i < map->n; ++i) {
9927 uint32_t bmap_hash;
9928 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9929 isl_hash_hash(hash, bmap_hash);
9932 isl_map_free(map);
9934 return hash;
9937 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9939 return isl_map_get_hash(set_to_map(set));
9942 /* Return the number of basic maps in the (current) representation of "map".
9944 int isl_map_n_basic_map(__isl_keep isl_map *map)
9946 return map ? map->n : 0;
9949 int isl_set_n_basic_set(__isl_keep isl_set *set)
9951 return set ? set->n : 0;
9954 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
9955 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9957 int i;
9959 if (!map)
9960 return isl_stat_error;
9962 for (i = 0; i < map->n; ++i)
9963 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9964 return isl_stat_error;
9966 return isl_stat_ok;
9969 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
9970 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9972 int i;
9974 if (!set)
9975 return isl_stat_error;
9977 for (i = 0; i < set->n; ++i)
9978 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9979 return isl_stat_error;
9981 return isl_stat_ok;
9984 /* Return a list of basic sets, the union of which is equal to "set".
9986 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
9987 __isl_keep isl_set *set)
9989 int i;
9990 isl_basic_set_list *list;
9992 if (!set)
9993 return NULL;
9995 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
9996 for (i = 0; i < set->n; ++i) {
9997 isl_basic_set *bset;
9999 bset = isl_basic_set_copy(set->p[i]);
10000 list = isl_basic_set_list_add(list, bset);
10003 return list;
10006 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10008 isl_space *dim;
10010 if (!bset)
10011 return NULL;
10013 bset = isl_basic_set_cow(bset);
10014 if (!bset)
10015 return NULL;
10017 dim = isl_basic_set_get_space(bset);
10018 dim = isl_space_lift(dim, bset->n_div);
10019 if (!dim)
10020 goto error;
10021 isl_space_free(bset->dim);
10022 bset->dim = dim;
10023 bset->extra -= bset->n_div;
10024 bset->n_div = 0;
10026 bset = isl_basic_set_finalize(bset);
10028 return bset;
10029 error:
10030 isl_basic_set_free(bset);
10031 return NULL;
10034 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10036 int i;
10037 isl_space *dim;
10038 unsigned n_div;
10040 set = isl_set_align_divs(set);
10042 if (!set)
10043 return NULL;
10045 set = isl_set_cow(set);
10046 if (!set)
10047 return NULL;
10049 n_div = set->p[0]->n_div;
10050 dim = isl_set_get_space(set);
10051 dim = isl_space_lift(dim, n_div);
10052 if (!dim)
10053 goto error;
10054 isl_space_free(set->dim);
10055 set->dim = dim;
10057 for (i = 0; i < set->n; ++i) {
10058 set->p[i] = isl_basic_set_lift(set->p[i]);
10059 if (!set->p[i])
10060 goto error;
10063 return set;
10064 error:
10065 isl_set_free(set);
10066 return NULL;
10069 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10071 unsigned dim;
10072 int size = 0;
10074 if (!bset)
10075 return -1;
10077 dim = isl_basic_set_total_dim(bset);
10078 size += bset->n_eq * (1 + dim);
10079 size += bset->n_ineq * (1 + dim);
10080 size += bset->n_div * (2 + dim);
10082 return size;
10085 int isl_set_size(__isl_keep isl_set *set)
10087 int i;
10088 int size = 0;
10090 if (!set)
10091 return -1;
10093 for (i = 0; i < set->n; ++i)
10094 size += isl_basic_set_size(set->p[i]);
10096 return size;
10099 /* Check if there is any lower bound (if lower == 0) and/or upper
10100 * bound (if upper == 0) on the specified dim.
10102 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10103 enum isl_dim_type type, unsigned pos, int lower, int upper)
10105 int i;
10107 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10108 return isl_bool_error;
10110 pos += isl_basic_map_offset(bmap, type);
10112 for (i = 0; i < bmap->n_div; ++i) {
10113 if (isl_int_is_zero(bmap->div[i][0]))
10114 continue;
10115 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10116 return isl_bool_true;
10119 for (i = 0; i < bmap->n_eq; ++i)
10120 if (!isl_int_is_zero(bmap->eq[i][pos]))
10121 return isl_bool_true;
10123 for (i = 0; i < bmap->n_ineq; ++i) {
10124 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10125 if (sgn > 0)
10126 lower = 1;
10127 if (sgn < 0)
10128 upper = 1;
10131 return lower && upper;
10134 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10135 enum isl_dim_type type, unsigned pos)
10137 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10140 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10141 enum isl_dim_type type, unsigned pos)
10143 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10146 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10147 enum isl_dim_type type, unsigned pos)
10149 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10152 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10153 enum isl_dim_type type, unsigned pos)
10155 int i;
10157 if (!map)
10158 return isl_bool_error;
10160 for (i = 0; i < map->n; ++i) {
10161 isl_bool bounded;
10162 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10163 if (bounded < 0 || !bounded)
10164 return bounded;
10167 return isl_bool_true;
10170 /* Return true if the specified dim is involved in both an upper bound
10171 * and a lower bound.
10173 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10174 enum isl_dim_type type, unsigned pos)
10176 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10179 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10181 static isl_bool has_any_bound(__isl_keep isl_map *map,
10182 enum isl_dim_type type, unsigned pos,
10183 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10184 enum isl_dim_type type, unsigned pos))
10186 int i;
10188 if (!map)
10189 return isl_bool_error;
10191 for (i = 0; i < map->n; ++i) {
10192 isl_bool bounded;
10193 bounded = fn(map->p[i], type, pos);
10194 if (bounded < 0 || bounded)
10195 return bounded;
10198 return isl_bool_false;
10201 /* Return 1 if the specified dim is involved in any lower bound.
10203 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10204 enum isl_dim_type type, unsigned pos)
10206 return has_any_bound(set, type, pos,
10207 &isl_basic_map_dim_has_lower_bound);
10210 /* Return 1 if the specified dim is involved in any upper bound.
10212 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10213 enum isl_dim_type type, unsigned pos)
10215 return has_any_bound(set, type, pos,
10216 &isl_basic_map_dim_has_upper_bound);
10219 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10221 static isl_bool has_bound(__isl_keep isl_map *map,
10222 enum isl_dim_type type, unsigned pos,
10223 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10224 enum isl_dim_type type, unsigned pos))
10226 int i;
10228 if (!map)
10229 return isl_bool_error;
10231 for (i = 0; i < map->n; ++i) {
10232 isl_bool bounded;
10233 bounded = fn(map->p[i], type, pos);
10234 if (bounded < 0 || !bounded)
10235 return bounded;
10238 return isl_bool_true;
10241 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10243 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10244 enum isl_dim_type type, unsigned pos)
10246 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10249 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10251 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10252 enum isl_dim_type type, unsigned pos)
10254 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10257 /* For each of the "n" variables starting at "first", determine
10258 * the sign of the variable and put the results in the first "n"
10259 * elements of the array "signs".
10260 * Sign
10261 * 1 means that the variable is non-negative
10262 * -1 means that the variable is non-positive
10263 * 0 means the variable attains both positive and negative values.
10265 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10266 unsigned first, unsigned n, int *signs)
10268 isl_vec *bound = NULL;
10269 struct isl_tab *tab = NULL;
10270 struct isl_tab_undo *snap;
10271 int i;
10273 if (!bset || !signs)
10274 return isl_stat_error;
10276 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10277 tab = isl_tab_from_basic_set(bset, 0);
10278 if (!bound || !tab)
10279 goto error;
10281 isl_seq_clr(bound->el, bound->size);
10282 isl_int_set_si(bound->el[0], -1);
10284 snap = isl_tab_snap(tab);
10285 for (i = 0; i < n; ++i) {
10286 int empty;
10288 isl_int_set_si(bound->el[1 + first + i], -1);
10289 if (isl_tab_add_ineq(tab, bound->el) < 0)
10290 goto error;
10291 empty = tab->empty;
10292 isl_int_set_si(bound->el[1 + first + i], 0);
10293 if (isl_tab_rollback(tab, snap) < 0)
10294 goto error;
10296 if (empty) {
10297 signs[i] = 1;
10298 continue;
10301 isl_int_set_si(bound->el[1 + first + i], 1);
10302 if (isl_tab_add_ineq(tab, bound->el) < 0)
10303 goto error;
10304 empty = tab->empty;
10305 isl_int_set_si(bound->el[1 + first + i], 0);
10306 if (isl_tab_rollback(tab, snap) < 0)
10307 goto error;
10309 signs[i] = empty ? -1 : 0;
10312 isl_tab_free(tab);
10313 isl_vec_free(bound);
10314 return isl_stat_ok;
10315 error:
10316 isl_tab_free(tab);
10317 isl_vec_free(bound);
10318 return isl_stat_error;
10321 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10322 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10324 if (!bset || !signs)
10325 return isl_stat_error;
10326 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10327 return isl_stat_error);
10329 first += pos(bset->dim, type) - 1;
10330 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10333 /* Is it possible for the integer division "div" to depend (possibly
10334 * indirectly) on any output dimensions?
10336 * If the div is undefined, then we conservatively assume that it
10337 * may depend on them.
10338 * Otherwise, we check if it actually depends on them or on any integer
10339 * divisions that may depend on them.
10341 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10343 int i;
10344 unsigned n_out, o_out;
10345 unsigned n_div, o_div;
10347 if (isl_int_is_zero(bmap->div[div][0]))
10348 return isl_bool_true;
10350 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10351 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10353 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10354 return isl_bool_true;
10356 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10357 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10359 for (i = 0; i < n_div; ++i) {
10360 isl_bool may_involve;
10362 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10363 continue;
10364 may_involve = div_may_involve_output(bmap, i);
10365 if (may_involve < 0 || may_involve)
10366 return may_involve;
10369 return isl_bool_false;
10372 /* Return the first integer division of "bmap" in the range
10373 * [first, first + n[ that may depend on any output dimensions and
10374 * that has a non-zero coefficient in "c" (where the first coefficient
10375 * in "c" corresponds to integer division "first").
10377 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10378 isl_int *c, int first, int n)
10380 int k;
10382 if (!bmap)
10383 return -1;
10385 for (k = first; k < first + n; ++k) {
10386 isl_bool may_involve;
10388 if (isl_int_is_zero(c[k]))
10389 continue;
10390 may_involve = div_may_involve_output(bmap, k);
10391 if (may_involve < 0)
10392 return -1;
10393 if (may_involve)
10394 return k;
10397 return first + n;
10400 /* Look for a pair of inequality constraints in "bmap" of the form
10402 * -l + i >= 0 or i >= l
10403 * and
10404 * n + l - i >= 0 or i <= l + n
10406 * with n < "m" and i the output dimension at position "pos".
10407 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10408 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10409 * and earlier output dimensions, as well as integer divisions that do
10410 * not involve any of the output dimensions.
10412 * Return the index of the first inequality constraint or bmap->n_ineq
10413 * if no such pair can be found.
10415 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10416 int pos, isl_int m)
10418 int i, j;
10419 isl_ctx *ctx;
10420 unsigned total;
10421 unsigned n_div, o_div;
10422 unsigned n_out, o_out;
10423 int less;
10425 if (!bmap)
10426 return -1;
10428 ctx = isl_basic_map_get_ctx(bmap);
10429 total = isl_basic_map_total_dim(bmap);
10430 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10431 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10432 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10433 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10434 for (i = 0; i < bmap->n_ineq; ++i) {
10435 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10436 continue;
10437 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10438 n_out - (pos + 1)) != -1)
10439 continue;
10440 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10441 0, n_div) < n_div)
10442 continue;
10443 for (j = i + 1; j < bmap->n_ineq; ++j) {
10444 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10445 ctx->one))
10446 continue;
10447 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10448 bmap->ineq[j] + 1, total))
10449 continue;
10450 break;
10452 if (j >= bmap->n_ineq)
10453 continue;
10454 isl_int_add(bmap->ineq[i][0],
10455 bmap->ineq[i][0], bmap->ineq[j][0]);
10456 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10457 isl_int_sub(bmap->ineq[i][0],
10458 bmap->ineq[i][0], bmap->ineq[j][0]);
10459 if (!less)
10460 continue;
10461 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10462 return i;
10463 else
10464 return j;
10467 return bmap->n_ineq;
10470 /* Return the index of the equality of "bmap" that defines
10471 * the output dimension "pos" in terms of earlier dimensions.
10472 * The equality may also involve integer divisions, as long
10473 * as those integer divisions are defined in terms of
10474 * parameters or input dimensions.
10475 * In this case, *div is set to the number of integer divisions and
10476 * *ineq is set to the number of inequality constraints (provided
10477 * div and ineq are not NULL).
10479 * The equality may also involve a single integer division involving
10480 * the output dimensions (typically only output dimension "pos") as
10481 * long as the coefficient of output dimension "pos" is 1 or -1 and
10482 * there is a pair of constraints i >= l and i <= l + n, with i referring
10483 * to output dimension "pos", l an expression involving only earlier
10484 * dimensions and n smaller than the coefficient of the integer division
10485 * in the equality. In this case, the output dimension can be defined
10486 * in terms of a modulo expression that does not involve the integer division.
10487 * *div is then set to this single integer division and
10488 * *ineq is set to the index of constraint i >= l.
10490 * Return bmap->n_eq if there is no such equality.
10491 * Return -1 on error.
10493 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10494 int pos, int *div, int *ineq)
10496 int j, k, l;
10497 unsigned n_out, o_out;
10498 unsigned n_div, o_div;
10500 if (!bmap)
10501 return -1;
10503 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10504 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10505 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10506 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10508 if (ineq)
10509 *ineq = bmap->n_ineq;
10510 if (div)
10511 *div = n_div;
10512 for (j = 0; j < bmap->n_eq; ++j) {
10513 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10514 continue;
10515 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10516 n_out - (pos + 1)) != -1)
10517 continue;
10518 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10519 0, n_div);
10520 if (k >= n_div)
10521 return j;
10522 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10523 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10524 continue;
10525 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10526 k + 1, n_div - (k+1)) < n_div)
10527 continue;
10528 l = find_modulo_constraint_pair(bmap, pos,
10529 bmap->eq[j][o_div + k]);
10530 if (l < 0)
10531 return -1;
10532 if (l >= bmap->n_ineq)
10533 continue;
10534 if (div)
10535 *div = k;
10536 if (ineq)
10537 *ineq = l;
10538 return j;
10541 return bmap->n_eq;
10544 /* Check if the given basic map is obviously single-valued.
10545 * In particular, for each output dimension, check that there is
10546 * an equality that defines the output dimension in terms of
10547 * earlier dimensions.
10549 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10551 int i;
10552 unsigned n_out;
10554 if (!bmap)
10555 return isl_bool_error;
10557 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10559 for (i = 0; i < n_out; ++i) {
10560 int eq;
10562 eq = isl_basic_map_output_defining_equality(bmap, i,
10563 NULL, NULL);
10564 if (eq < 0)
10565 return isl_bool_error;
10566 if (eq >= bmap->n_eq)
10567 return isl_bool_false;
10570 return isl_bool_true;
10573 /* Check if the given basic map is single-valued.
10574 * We simply compute
10576 * M \circ M^-1
10578 * and check if the result is a subset of the identity mapping.
10580 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10582 isl_space *space;
10583 isl_basic_map *test;
10584 isl_basic_map *id;
10585 isl_bool sv;
10587 sv = isl_basic_map_plain_is_single_valued(bmap);
10588 if (sv < 0 || sv)
10589 return sv;
10591 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10592 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10594 space = isl_basic_map_get_space(bmap);
10595 space = isl_space_map_from_set(isl_space_range(space));
10596 id = isl_basic_map_identity(space);
10598 sv = isl_basic_map_is_subset(test, id);
10600 isl_basic_map_free(test);
10601 isl_basic_map_free(id);
10603 return sv;
10606 /* Check if the given map is obviously single-valued.
10608 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10610 if (!map)
10611 return isl_bool_error;
10612 if (map->n == 0)
10613 return isl_bool_true;
10614 if (map->n >= 2)
10615 return isl_bool_false;
10617 return isl_basic_map_plain_is_single_valued(map->p[0]);
10620 /* Check if the given map is single-valued.
10621 * We simply compute
10623 * M \circ M^-1
10625 * and check if the result is a subset of the identity mapping.
10627 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10629 isl_space *dim;
10630 isl_map *test;
10631 isl_map *id;
10632 isl_bool sv;
10634 sv = isl_map_plain_is_single_valued(map);
10635 if (sv < 0 || sv)
10636 return sv;
10638 test = isl_map_reverse(isl_map_copy(map));
10639 test = isl_map_apply_range(test, isl_map_copy(map));
10641 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10642 id = isl_map_identity(dim);
10644 sv = isl_map_is_subset(test, id);
10646 isl_map_free(test);
10647 isl_map_free(id);
10649 return sv;
10652 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10654 isl_bool in;
10656 map = isl_map_copy(map);
10657 map = isl_map_reverse(map);
10658 in = isl_map_is_single_valued(map);
10659 isl_map_free(map);
10661 return in;
10664 /* Check if the given map is obviously injective.
10666 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10668 isl_bool in;
10670 map = isl_map_copy(map);
10671 map = isl_map_reverse(map);
10672 in = isl_map_plain_is_single_valued(map);
10673 isl_map_free(map);
10675 return in;
10678 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10680 isl_bool sv;
10682 sv = isl_map_is_single_valued(map);
10683 if (sv < 0 || !sv)
10684 return sv;
10686 return isl_map_is_injective(map);
10689 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10691 return isl_map_is_single_valued(set_to_map(set));
10694 /* Does "map" only map elements to themselves?
10696 * If the domain and range spaces are different, then "map"
10697 * is considered not to be an identity relation, even if it is empty.
10698 * Otherwise, construct the maximal identity relation and
10699 * check whether "map" is a subset of this relation.
10701 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10703 isl_space *space;
10704 isl_map *id;
10705 isl_bool equal, is_identity;
10707 space = isl_map_get_space(map);
10708 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10709 isl_space_free(space);
10710 if (equal < 0 || !equal)
10711 return equal;
10713 id = isl_map_identity(isl_map_get_space(map));
10714 is_identity = isl_map_is_subset(map, id);
10715 isl_map_free(id);
10717 return is_identity;
10720 int isl_map_is_translation(__isl_keep isl_map *map)
10722 int ok;
10723 isl_set *delta;
10725 delta = isl_map_deltas(isl_map_copy(map));
10726 ok = isl_set_is_singleton(delta);
10727 isl_set_free(delta);
10729 return ok;
10732 static int unique(isl_int *p, unsigned pos, unsigned len)
10734 if (isl_seq_first_non_zero(p, pos) != -1)
10735 return 0;
10736 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10737 return 0;
10738 return 1;
10741 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10743 int i, j;
10744 unsigned nvar;
10745 unsigned ovar;
10747 if (!bset)
10748 return isl_bool_error;
10750 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10751 return isl_bool_false;
10753 nvar = isl_basic_set_dim(bset, isl_dim_set);
10754 ovar = isl_space_offset(bset->dim, isl_dim_set);
10755 for (j = 0; j < nvar; ++j) {
10756 int lower = 0, upper = 0;
10757 for (i = 0; i < bset->n_eq; ++i) {
10758 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10759 continue;
10760 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10761 return isl_bool_false;
10762 break;
10764 if (i < bset->n_eq)
10765 continue;
10766 for (i = 0; i < bset->n_ineq; ++i) {
10767 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10768 continue;
10769 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10770 return isl_bool_false;
10771 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10772 lower = 1;
10773 else
10774 upper = 1;
10776 if (!lower || !upper)
10777 return isl_bool_false;
10780 return isl_bool_true;
10783 isl_bool isl_set_is_box(__isl_keep isl_set *set)
10785 if (!set)
10786 return isl_bool_error;
10787 if (set->n != 1)
10788 return isl_bool_false;
10790 return isl_basic_set_is_box(set->p[0]);
10793 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10795 if (!bset)
10796 return isl_bool_error;
10798 return isl_space_is_wrapping(bset->dim);
10801 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
10803 if (!set)
10804 return isl_bool_error;
10806 return isl_space_is_wrapping(set->dim);
10809 /* Modify the space of "map" through a call to "change".
10810 * If "can_change" is set (not NULL), then first call it to check
10811 * if the modification is allowed, printing the error message "cannot_change"
10812 * if it is not.
10814 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
10815 isl_bool (*can_change)(__isl_keep isl_map *map),
10816 const char *cannot_change,
10817 __isl_give isl_space *(*change)(__isl_take isl_space *space))
10819 isl_bool ok;
10820 isl_space *space;
10822 if (!map)
10823 return NULL;
10825 ok = can_change ? can_change(map) : isl_bool_true;
10826 if (ok < 0)
10827 return isl_map_free(map);
10828 if (!ok)
10829 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
10830 return isl_map_free(map));
10832 space = change(isl_map_get_space(map));
10833 map = isl_map_reset_space(map, space);
10835 return map;
10838 /* Is the domain of "map" a wrapped relation?
10840 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10842 if (!map)
10843 return isl_bool_error;
10845 return isl_space_domain_is_wrapping(map->dim);
10848 /* Is the range of "map" a wrapped relation?
10850 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
10852 if (!map)
10853 return isl_bool_error;
10855 return isl_space_range_is_wrapping(map->dim);
10858 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10860 bmap = isl_basic_map_cow(bmap);
10861 if (!bmap)
10862 return NULL;
10864 bmap->dim = isl_space_wrap(bmap->dim);
10865 if (!bmap->dim)
10866 goto error;
10868 bmap = isl_basic_map_finalize(bmap);
10870 return bset_from_bmap(bmap);
10871 error:
10872 isl_basic_map_free(bmap);
10873 return NULL;
10876 /* Given a map A -> B, return the set (A -> B).
10878 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10880 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
10883 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10885 bset = isl_basic_set_cow(bset);
10886 if (!bset)
10887 return NULL;
10889 bset->dim = isl_space_unwrap(bset->dim);
10890 if (!bset->dim)
10891 goto error;
10893 bset = isl_basic_set_finalize(bset);
10895 return bset_to_bmap(bset);
10896 error:
10897 isl_basic_set_free(bset);
10898 return NULL;
10901 /* Given a set (A -> B), return the map A -> B.
10902 * Error out if "set" is not of the form (A -> B).
10904 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10906 return isl_map_change_space(set, &isl_set_is_wrapping,
10907 "not a wrapping set", &isl_space_unwrap);
10910 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10911 enum isl_dim_type type)
10913 if (!bmap)
10914 return NULL;
10916 if (!isl_space_is_named_or_nested(bmap->dim, type))
10917 return bmap;
10919 bmap = isl_basic_map_cow(bmap);
10920 if (!bmap)
10921 return NULL;
10923 bmap->dim = isl_space_reset(bmap->dim, type);
10924 if (!bmap->dim)
10925 goto error;
10927 bmap = isl_basic_map_finalize(bmap);
10929 return bmap;
10930 error:
10931 isl_basic_map_free(bmap);
10932 return NULL;
10935 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10936 enum isl_dim_type type)
10938 int i;
10940 if (!map)
10941 return NULL;
10943 if (!isl_space_is_named_or_nested(map->dim, type))
10944 return map;
10946 map = isl_map_cow(map);
10947 if (!map)
10948 return NULL;
10950 for (i = 0; i < map->n; ++i) {
10951 map->p[i] = isl_basic_map_reset(map->p[i], type);
10952 if (!map->p[i])
10953 goto error;
10955 map->dim = isl_space_reset(map->dim, type);
10956 if (!map->dim)
10957 goto error;
10959 return map;
10960 error:
10961 isl_map_free(map);
10962 return NULL;
10965 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10967 if (!bmap)
10968 return NULL;
10970 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10971 return bmap;
10973 bmap = isl_basic_map_cow(bmap);
10974 if (!bmap)
10975 return NULL;
10977 bmap->dim = isl_space_flatten(bmap->dim);
10978 if (!bmap->dim)
10979 goto error;
10981 bmap = isl_basic_map_finalize(bmap);
10983 return bmap;
10984 error:
10985 isl_basic_map_free(bmap);
10986 return NULL;
10989 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10991 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
10994 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10995 __isl_take isl_basic_map *bmap)
10997 if (!bmap)
10998 return NULL;
11000 if (!bmap->dim->nested[0])
11001 return bmap;
11003 bmap = isl_basic_map_cow(bmap);
11004 if (!bmap)
11005 return NULL;
11007 bmap->dim = isl_space_flatten_domain(bmap->dim);
11008 if (!bmap->dim)
11009 goto error;
11011 bmap = isl_basic_map_finalize(bmap);
11013 return bmap;
11014 error:
11015 isl_basic_map_free(bmap);
11016 return NULL;
11019 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11020 __isl_take isl_basic_map *bmap)
11022 if (!bmap)
11023 return NULL;
11025 if (!bmap->dim->nested[1])
11026 return bmap;
11028 bmap = isl_basic_map_cow(bmap);
11029 if (!bmap)
11030 return NULL;
11032 bmap->dim = isl_space_flatten_range(bmap->dim);
11033 if (!bmap->dim)
11034 goto error;
11036 bmap = isl_basic_map_finalize(bmap);
11038 return bmap;
11039 error:
11040 isl_basic_map_free(bmap);
11041 return NULL;
11044 /* Remove any internal structure from the spaces of domain and range of "map".
11046 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11048 if (!map)
11049 return NULL;
11051 if (!map->dim->nested[0] && !map->dim->nested[1])
11052 return map;
11054 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11057 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11059 return set_from_map(isl_map_flatten(set_to_map(set)));
11062 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11064 isl_space *dim, *flat_dim;
11065 isl_map *map;
11067 dim = isl_set_get_space(set);
11068 flat_dim = isl_space_flatten(isl_space_copy(dim));
11069 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11070 map = isl_map_intersect_domain(map, set);
11072 return map;
11075 /* Remove any internal structure from the space of the domain of "map".
11077 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11079 if (!map)
11080 return NULL;
11082 if (!map->dim->nested[0])
11083 return map;
11085 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11088 /* Remove any internal structure from the space of the range of "map".
11090 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11092 if (!map)
11093 return NULL;
11095 if (!map->dim->nested[1])
11096 return map;
11098 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11101 /* Reorder the dimensions of "bmap" according to the given dim_map
11102 * and set the dimension specification to "dim" and
11103 * perform Gaussian elimination on the result.
11105 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11106 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11108 isl_basic_map *res;
11109 unsigned flags;
11111 bmap = isl_basic_map_cow(bmap);
11112 if (!bmap || !dim || !dim_map)
11113 goto error;
11115 flags = bmap->flags;
11116 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11117 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11118 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11119 res = isl_basic_map_alloc_space(dim,
11120 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11121 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11122 if (res)
11123 res->flags = flags;
11124 res = isl_basic_map_gauss(res, NULL);
11125 res = isl_basic_map_finalize(res);
11126 return res;
11127 error:
11128 free(dim_map);
11129 isl_basic_map_free(bmap);
11130 isl_space_free(dim);
11131 return NULL;
11134 /* Reorder the dimensions of "map" according to given reordering.
11136 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11137 __isl_take isl_reordering *r)
11139 int i;
11140 struct isl_dim_map *dim_map;
11142 map = isl_map_cow(map);
11143 dim_map = isl_dim_map_from_reordering(r);
11144 if (!map || !r || !dim_map)
11145 goto error;
11147 for (i = 0; i < map->n; ++i) {
11148 struct isl_dim_map *dim_map_i;
11150 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11152 map->p[i] = isl_basic_map_realign(map->p[i],
11153 isl_space_copy(r->dim), dim_map_i);
11155 if (!map->p[i])
11156 goto error;
11159 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11161 isl_reordering_free(r);
11162 free(dim_map);
11163 return map;
11164 error:
11165 free(dim_map);
11166 isl_map_free(map);
11167 isl_reordering_free(r);
11168 return NULL;
11171 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11172 __isl_take isl_reordering *r)
11174 return set_from_map(isl_map_realign(set_to_map(set), r));
11177 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11178 __isl_take isl_space *model)
11180 isl_ctx *ctx;
11181 isl_bool aligned;
11183 if (!map || !model)
11184 goto error;
11186 ctx = isl_space_get_ctx(model);
11187 if (!isl_space_has_named_params(model))
11188 isl_die(ctx, isl_error_invalid,
11189 "model has unnamed parameters", goto error);
11190 if (isl_map_check_named_params(map) < 0)
11191 goto error;
11192 aligned = isl_map_space_has_equal_params(map, model);
11193 if (aligned < 0)
11194 goto error;
11195 if (!aligned) {
11196 isl_reordering *exp;
11198 model = isl_space_drop_dims(model, isl_dim_in,
11199 0, isl_space_dim(model, isl_dim_in));
11200 model = isl_space_drop_dims(model, isl_dim_out,
11201 0, isl_space_dim(model, isl_dim_out));
11202 exp = isl_parameter_alignment_reordering(map->dim, model);
11203 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11204 map = isl_map_realign(map, exp);
11207 isl_space_free(model);
11208 return map;
11209 error:
11210 isl_space_free(model);
11211 isl_map_free(map);
11212 return NULL;
11215 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11216 __isl_take isl_space *model)
11218 return isl_map_align_params(set, model);
11221 /* Align the parameters of "bmap" to those of "model", introducing
11222 * additional parameters if needed.
11224 __isl_give isl_basic_map *isl_basic_map_align_params(
11225 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11227 isl_ctx *ctx;
11229 if (!bmap || !model)
11230 goto error;
11232 ctx = isl_space_get_ctx(model);
11233 if (!isl_space_has_named_params(model))
11234 isl_die(ctx, isl_error_invalid,
11235 "model has unnamed parameters", goto error);
11236 if (!isl_space_has_named_params(bmap->dim))
11237 isl_die(ctx, isl_error_invalid,
11238 "relation has unnamed parameters", goto error);
11239 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11240 isl_reordering *exp;
11241 struct isl_dim_map *dim_map;
11243 model = isl_space_drop_dims(model, isl_dim_in,
11244 0, isl_space_dim(model, isl_dim_in));
11245 model = isl_space_drop_dims(model, isl_dim_out,
11246 0, isl_space_dim(model, isl_dim_out));
11247 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11248 exp = isl_reordering_extend_space(exp,
11249 isl_basic_map_get_space(bmap));
11250 dim_map = isl_dim_map_from_reordering(exp);
11251 bmap = isl_basic_map_realign(bmap,
11252 exp ? isl_space_copy(exp->dim) : NULL,
11253 isl_dim_map_extend(dim_map, bmap));
11254 isl_reordering_free(exp);
11255 free(dim_map);
11258 isl_space_free(model);
11259 return bmap;
11260 error:
11261 isl_space_free(model);
11262 isl_basic_map_free(bmap);
11263 return NULL;
11266 /* Do "bset" and "space" have the same parameters?
11268 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11269 __isl_keep isl_space *space)
11271 isl_space *bset_space;
11273 bset_space = isl_basic_set_peek_space(bset);
11274 return isl_space_match(bset_space, isl_dim_param, space, isl_dim_param);
11277 /* Do "map" and "space" have the same parameters?
11279 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11280 __isl_keep isl_space *space)
11282 isl_space *map_space;
11284 map_space = isl_map_peek_space(map);
11285 return isl_space_match(map_space, isl_dim_param, space, isl_dim_param);
11288 /* Do "set" and "space" have the same parameters?
11290 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11291 __isl_keep isl_space *space)
11293 return isl_map_space_has_equal_params(set_to_map(set), space);
11296 /* Align the parameters of "bset" to those of "model", introducing
11297 * additional parameters if needed.
11299 __isl_give isl_basic_set *isl_basic_set_align_params(
11300 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11302 return isl_basic_map_align_params(bset, model);
11305 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11306 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11307 enum isl_dim_type c2, enum isl_dim_type c3,
11308 enum isl_dim_type c4, enum isl_dim_type c5)
11310 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11311 struct isl_mat *mat;
11312 int i, j, k;
11313 int pos;
11315 if (!bmap)
11316 return NULL;
11317 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11318 isl_basic_map_total_dim(bmap) + 1);
11319 if (!mat)
11320 return NULL;
11321 for (i = 0; i < bmap->n_eq; ++i)
11322 for (j = 0, pos = 0; j < 5; ++j) {
11323 int off = isl_basic_map_offset(bmap, c[j]);
11324 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11325 isl_int_set(mat->row[i][pos],
11326 bmap->eq[i][off + k]);
11327 ++pos;
11331 return mat;
11334 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11335 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11336 enum isl_dim_type c2, enum isl_dim_type c3,
11337 enum isl_dim_type c4, enum isl_dim_type c5)
11339 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11340 struct isl_mat *mat;
11341 int i, j, k;
11342 int pos;
11344 if (!bmap)
11345 return NULL;
11346 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11347 isl_basic_map_total_dim(bmap) + 1);
11348 if (!mat)
11349 return NULL;
11350 for (i = 0; i < bmap->n_ineq; ++i)
11351 for (j = 0, pos = 0; j < 5; ++j) {
11352 int off = isl_basic_map_offset(bmap, c[j]);
11353 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11354 isl_int_set(mat->row[i][pos],
11355 bmap->ineq[i][off + k]);
11356 ++pos;
11360 return mat;
11363 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11364 __isl_take isl_space *dim,
11365 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11366 enum isl_dim_type c2, enum isl_dim_type c3,
11367 enum isl_dim_type c4, enum isl_dim_type c5)
11369 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11370 isl_basic_map *bmap;
11371 unsigned total;
11372 unsigned extra;
11373 int i, j, k, l;
11374 int pos;
11376 if (!dim || !eq || !ineq)
11377 goto error;
11379 if (eq->n_col != ineq->n_col)
11380 isl_die(dim->ctx, isl_error_invalid,
11381 "equalities and inequalities matrices should have "
11382 "same number of columns", goto error);
11384 total = 1 + isl_space_dim(dim, isl_dim_all);
11386 if (eq->n_col < total)
11387 isl_die(dim->ctx, isl_error_invalid,
11388 "number of columns too small", goto error);
11390 extra = eq->n_col - total;
11392 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11393 eq->n_row, ineq->n_row);
11394 if (!bmap)
11395 goto error;
11396 for (i = 0; i < extra; ++i) {
11397 k = isl_basic_map_alloc_div(bmap);
11398 if (k < 0)
11399 goto error;
11400 isl_int_set_si(bmap->div[k][0], 0);
11402 for (i = 0; i < eq->n_row; ++i) {
11403 l = isl_basic_map_alloc_equality(bmap);
11404 if (l < 0)
11405 goto error;
11406 for (j = 0, pos = 0; j < 5; ++j) {
11407 int off = isl_basic_map_offset(bmap, c[j]);
11408 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11409 isl_int_set(bmap->eq[l][off + k],
11410 eq->row[i][pos]);
11411 ++pos;
11415 for (i = 0; i < ineq->n_row; ++i) {
11416 l = isl_basic_map_alloc_inequality(bmap);
11417 if (l < 0)
11418 goto error;
11419 for (j = 0, pos = 0; j < 5; ++j) {
11420 int off = isl_basic_map_offset(bmap, c[j]);
11421 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11422 isl_int_set(bmap->ineq[l][off + k],
11423 ineq->row[i][pos]);
11424 ++pos;
11429 isl_space_free(dim);
11430 isl_mat_free(eq);
11431 isl_mat_free(ineq);
11433 bmap = isl_basic_map_simplify(bmap);
11434 return isl_basic_map_finalize(bmap);
11435 error:
11436 isl_space_free(dim);
11437 isl_mat_free(eq);
11438 isl_mat_free(ineq);
11439 return NULL;
11442 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11443 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11444 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11446 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11447 c1, c2, c3, c4, isl_dim_in);
11450 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11451 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11452 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11454 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11455 c1, c2, c3, c4, isl_dim_in);
11458 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11459 __isl_take isl_space *dim,
11460 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11461 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11463 isl_basic_map *bmap;
11464 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11465 c1, c2, c3, c4, isl_dim_in);
11466 return bset_from_bmap(bmap);
11469 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11471 if (!bmap)
11472 return isl_bool_error;
11474 return isl_space_can_zip(bmap->dim);
11477 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11479 if (!map)
11480 return isl_bool_error;
11482 return isl_space_can_zip(map->dim);
11485 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11486 * (A -> C) -> (B -> D).
11488 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11490 unsigned pos;
11491 unsigned n1;
11492 unsigned n2;
11494 if (!bmap)
11495 return NULL;
11497 if (!isl_basic_map_can_zip(bmap))
11498 isl_die(bmap->ctx, isl_error_invalid,
11499 "basic map cannot be zipped", goto error);
11500 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11501 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11502 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11503 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11504 bmap = isl_basic_map_cow(bmap);
11505 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11506 if (!bmap)
11507 return NULL;
11508 bmap->dim = isl_space_zip(bmap->dim);
11509 if (!bmap->dim)
11510 goto error;
11511 bmap = isl_basic_map_mark_final(bmap);
11512 return bmap;
11513 error:
11514 isl_basic_map_free(bmap);
11515 return NULL;
11518 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11519 * (A -> C) -> (B -> D).
11521 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11523 int i;
11525 if (!map)
11526 return NULL;
11528 if (!isl_map_can_zip(map))
11529 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11530 goto error);
11532 map = isl_map_cow(map);
11533 if (!map)
11534 return NULL;
11536 for (i = 0; i < map->n; ++i) {
11537 map->p[i] = isl_basic_map_zip(map->p[i]);
11538 if (!map->p[i])
11539 goto error;
11542 map->dim = isl_space_zip(map->dim);
11543 if (!map->dim)
11544 goto error;
11546 return map;
11547 error:
11548 isl_map_free(map);
11549 return NULL;
11552 /* Can we apply isl_basic_map_curry to "bmap"?
11553 * That is, does it have a nested relation in its domain?
11555 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11557 if (!bmap)
11558 return isl_bool_error;
11560 return isl_space_can_curry(bmap->dim);
11563 /* Can we apply isl_map_curry to "map"?
11564 * That is, does it have a nested relation in its domain?
11566 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11568 if (!map)
11569 return isl_bool_error;
11571 return isl_space_can_curry(map->dim);
11574 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11575 * A -> (B -> C).
11577 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11580 if (!bmap)
11581 return NULL;
11583 if (!isl_basic_map_can_curry(bmap))
11584 isl_die(bmap->ctx, isl_error_invalid,
11585 "basic map cannot be curried", goto error);
11586 bmap = isl_basic_map_cow(bmap);
11587 if (!bmap)
11588 return NULL;
11589 bmap->dim = isl_space_curry(bmap->dim);
11590 if (!bmap->dim)
11591 goto error;
11592 bmap = isl_basic_map_mark_final(bmap);
11593 return bmap;
11594 error:
11595 isl_basic_map_free(bmap);
11596 return NULL;
11599 /* Given a map (A -> B) -> C, return the corresponding map
11600 * A -> (B -> C).
11602 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11604 return isl_map_change_space(map, &isl_map_can_curry,
11605 "map cannot be curried", &isl_space_curry);
11608 /* Can isl_map_range_curry be applied to "map"?
11609 * That is, does it have a nested relation in its range,
11610 * the domain of which is itself a nested relation?
11612 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11614 if (!map)
11615 return isl_bool_error;
11617 return isl_space_can_range_curry(map->dim);
11620 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11621 * A -> (B -> (C -> D)).
11623 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11625 return isl_map_change_space(map, &isl_map_can_range_curry,
11626 "map range cannot be curried",
11627 &isl_space_range_curry);
11630 /* Can we apply isl_basic_map_uncurry to "bmap"?
11631 * That is, does it have a nested relation in its domain?
11633 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11635 if (!bmap)
11636 return isl_bool_error;
11638 return isl_space_can_uncurry(bmap->dim);
11641 /* Can we apply isl_map_uncurry to "map"?
11642 * That is, does it have a nested relation in its domain?
11644 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11646 if (!map)
11647 return isl_bool_error;
11649 return isl_space_can_uncurry(map->dim);
11652 /* Given a basic map A -> (B -> C), return the corresponding basic map
11653 * (A -> B) -> C.
11655 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11658 if (!bmap)
11659 return NULL;
11661 if (!isl_basic_map_can_uncurry(bmap))
11662 isl_die(bmap->ctx, isl_error_invalid,
11663 "basic map cannot be uncurried",
11664 return isl_basic_map_free(bmap));
11665 bmap = isl_basic_map_cow(bmap);
11666 if (!bmap)
11667 return NULL;
11668 bmap->dim = isl_space_uncurry(bmap->dim);
11669 if (!bmap->dim)
11670 return isl_basic_map_free(bmap);
11671 bmap = isl_basic_map_mark_final(bmap);
11672 return bmap;
11675 /* Given a map A -> (B -> C), return the corresponding map
11676 * (A -> B) -> C.
11678 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11680 return isl_map_change_space(map, &isl_map_can_uncurry,
11681 "map cannot be uncurried", &isl_space_uncurry);
11684 /* Construct a basic map mapping the domain of the affine expression
11685 * to a one-dimensional range prescribed by the affine expression.
11686 * If "rational" is set, then construct a rational basic map.
11688 * A NaN affine expression cannot be converted to a basic map.
11690 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
11691 __isl_take isl_aff *aff, int rational)
11693 int k;
11694 int pos;
11695 isl_bool is_nan;
11696 isl_local_space *ls;
11697 isl_basic_map *bmap = NULL;
11699 if (!aff)
11700 return NULL;
11701 is_nan = isl_aff_is_nan(aff);
11702 if (is_nan < 0)
11703 goto error;
11704 if (is_nan)
11705 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11706 "cannot convert NaN", goto error);
11708 ls = isl_aff_get_local_space(aff);
11709 bmap = isl_basic_map_from_local_space(ls);
11710 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11711 k = isl_basic_map_alloc_equality(bmap);
11712 if (k < 0)
11713 goto error;
11715 pos = isl_basic_map_offset(bmap, isl_dim_out);
11716 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11717 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11718 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11719 aff->v->size - (pos + 1));
11721 isl_aff_free(aff);
11722 if (rational)
11723 bmap = isl_basic_map_set_rational(bmap);
11724 bmap = isl_basic_map_finalize(bmap);
11725 return bmap;
11726 error:
11727 isl_aff_free(aff);
11728 isl_basic_map_free(bmap);
11729 return NULL;
11732 /* Construct a basic map mapping the domain of the affine expression
11733 * to a one-dimensional range prescribed by the affine expression.
11735 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11737 return isl_basic_map_from_aff2(aff, 0);
11740 /* Construct a map mapping the domain of the affine expression
11741 * to a one-dimensional range prescribed by the affine expression.
11743 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11745 isl_basic_map *bmap;
11747 bmap = isl_basic_map_from_aff(aff);
11748 return isl_map_from_basic_map(bmap);
11751 /* Construct a basic map mapping the domain the multi-affine expression
11752 * to its range, with each dimension in the range equated to the
11753 * corresponding affine expression.
11754 * If "rational" is set, then construct a rational basic map.
11756 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
11757 __isl_take isl_multi_aff *maff, int rational)
11759 int i;
11760 isl_space *space;
11761 isl_basic_map *bmap;
11763 if (!maff)
11764 return NULL;
11766 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11767 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11768 "invalid space", goto error);
11770 space = isl_space_domain(isl_multi_aff_get_space(maff));
11771 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11772 if (rational)
11773 bmap = isl_basic_map_set_rational(bmap);
11775 for (i = 0; i < maff->n; ++i) {
11776 isl_aff *aff;
11777 isl_basic_map *bmap_i;
11779 aff = isl_aff_copy(maff->p[i]);
11780 bmap_i = isl_basic_map_from_aff2(aff, rational);
11782 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11785 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11787 isl_multi_aff_free(maff);
11788 return bmap;
11789 error:
11790 isl_multi_aff_free(maff);
11791 return NULL;
11794 /* Construct a basic map mapping the domain the multi-affine expression
11795 * to its range, with each dimension in the range equated to the
11796 * corresponding affine expression.
11798 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11799 __isl_take isl_multi_aff *ma)
11801 return isl_basic_map_from_multi_aff2(ma, 0);
11804 /* Construct a map mapping the domain the multi-affine expression
11805 * to its range, with each dimension in the range equated to the
11806 * corresponding affine expression.
11808 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11810 isl_basic_map *bmap;
11812 bmap = isl_basic_map_from_multi_aff(maff);
11813 return isl_map_from_basic_map(bmap);
11816 /* Construct a basic map mapping a domain in the given space to
11817 * to an n-dimensional range, with n the number of elements in the list,
11818 * where each coordinate in the range is prescribed by the
11819 * corresponding affine expression.
11820 * The domains of all affine expressions in the list are assumed to match
11821 * domain_dim.
11823 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11824 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11826 int i;
11827 isl_space *dim;
11828 isl_basic_map *bmap;
11830 if (!list)
11831 return NULL;
11833 dim = isl_space_from_domain(domain_dim);
11834 bmap = isl_basic_map_universe(dim);
11836 for (i = 0; i < list->n; ++i) {
11837 isl_aff *aff;
11838 isl_basic_map *bmap_i;
11840 aff = isl_aff_copy(list->p[i]);
11841 bmap_i = isl_basic_map_from_aff(aff);
11843 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11846 isl_aff_list_free(list);
11847 return bmap;
11850 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11851 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11853 return isl_map_equate(set, type1, pos1, type2, pos2);
11856 /* Construct a basic map where the given dimensions are equal to each other.
11858 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11859 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11861 isl_basic_map *bmap = NULL;
11862 int i;
11864 if (!space)
11865 return NULL;
11867 if (pos1 >= isl_space_dim(space, type1))
11868 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11869 "index out of bounds", goto error);
11870 if (pos2 >= isl_space_dim(space, type2))
11871 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11872 "index out of bounds", goto error);
11874 if (type1 == type2 && pos1 == pos2)
11875 return isl_basic_map_universe(space);
11877 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11878 i = isl_basic_map_alloc_equality(bmap);
11879 if (i < 0)
11880 goto error;
11881 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11882 pos1 += isl_basic_map_offset(bmap, type1);
11883 pos2 += isl_basic_map_offset(bmap, type2);
11884 isl_int_set_si(bmap->eq[i][pos1], -1);
11885 isl_int_set_si(bmap->eq[i][pos2], 1);
11886 bmap = isl_basic_map_finalize(bmap);
11887 isl_space_free(space);
11888 return bmap;
11889 error:
11890 isl_space_free(space);
11891 isl_basic_map_free(bmap);
11892 return NULL;
11895 /* Add a constraint imposing that the given two dimensions are equal.
11897 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11898 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11900 isl_basic_map *eq;
11902 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11904 bmap = isl_basic_map_intersect(bmap, eq);
11906 return bmap;
11909 /* Add a constraint imposing that the given two dimensions are equal.
11911 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11912 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11914 isl_basic_map *bmap;
11916 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11918 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11920 return map;
11923 /* Add a constraint imposing that the given two dimensions have opposite values.
11925 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11926 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11928 isl_basic_map *bmap = NULL;
11929 int i;
11931 if (!map)
11932 return NULL;
11934 if (pos1 >= isl_map_dim(map, type1))
11935 isl_die(map->ctx, isl_error_invalid,
11936 "index out of bounds", goto error);
11937 if (pos2 >= isl_map_dim(map, type2))
11938 isl_die(map->ctx, isl_error_invalid,
11939 "index out of bounds", goto error);
11941 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11942 i = isl_basic_map_alloc_equality(bmap);
11943 if (i < 0)
11944 goto error;
11945 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11946 pos1 += isl_basic_map_offset(bmap, type1);
11947 pos2 += isl_basic_map_offset(bmap, type2);
11948 isl_int_set_si(bmap->eq[i][pos1], 1);
11949 isl_int_set_si(bmap->eq[i][pos2], 1);
11950 bmap = isl_basic_map_finalize(bmap);
11952 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11954 return map;
11955 error:
11956 isl_basic_map_free(bmap);
11957 isl_map_free(map);
11958 return NULL;
11961 /* Construct a constraint imposing that the value of the first dimension is
11962 * greater than or equal to that of the second.
11964 static __isl_give isl_constraint *constraint_order_ge(
11965 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11966 enum isl_dim_type type2, int pos2)
11968 isl_constraint *c;
11970 if (!space)
11971 return NULL;
11973 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
11975 if (pos1 >= isl_constraint_dim(c, type1))
11976 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11977 "index out of bounds", return isl_constraint_free(c));
11978 if (pos2 >= isl_constraint_dim(c, type2))
11979 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11980 "index out of bounds", return isl_constraint_free(c));
11982 if (type1 == type2 && pos1 == pos2)
11983 return c;
11985 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11986 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11988 return c;
11991 /* Add a constraint imposing that the value of the first dimension is
11992 * greater than or equal to that of the second.
11994 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11995 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11997 isl_constraint *c;
11998 isl_space *space;
12000 if (type1 == type2 && pos1 == pos2)
12001 return bmap;
12002 space = isl_basic_map_get_space(bmap);
12003 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12004 bmap = isl_basic_map_add_constraint(bmap, c);
12006 return bmap;
12009 /* Add a constraint imposing that the value of the first dimension is
12010 * greater than or equal to that of the second.
12012 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12013 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12015 isl_constraint *c;
12016 isl_space *space;
12018 if (type1 == type2 && pos1 == pos2)
12019 return map;
12020 space = isl_map_get_space(map);
12021 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12022 map = isl_map_add_constraint(map, c);
12024 return map;
12027 /* Add a constraint imposing that the value of the first dimension is
12028 * less than or equal to that of the second.
12030 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12031 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12033 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12036 /* Construct a basic map where the value of the first dimension is
12037 * greater than that of the second.
12039 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12040 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12042 isl_basic_map *bmap = NULL;
12043 int i;
12045 if (!space)
12046 return NULL;
12048 if (pos1 >= isl_space_dim(space, type1))
12049 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12050 "index out of bounds", goto error);
12051 if (pos2 >= isl_space_dim(space, type2))
12052 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12053 "index out of bounds", goto error);
12055 if (type1 == type2 && pos1 == pos2)
12056 return isl_basic_map_empty(space);
12058 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12059 i = isl_basic_map_alloc_inequality(bmap);
12060 if (i < 0)
12061 return isl_basic_map_free(bmap);
12062 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12063 pos1 += isl_basic_map_offset(bmap, type1);
12064 pos2 += isl_basic_map_offset(bmap, type2);
12065 isl_int_set_si(bmap->ineq[i][pos1], 1);
12066 isl_int_set_si(bmap->ineq[i][pos2], -1);
12067 isl_int_set_si(bmap->ineq[i][0], -1);
12068 bmap = isl_basic_map_finalize(bmap);
12070 return bmap;
12071 error:
12072 isl_space_free(space);
12073 isl_basic_map_free(bmap);
12074 return NULL;
12077 /* Add a constraint imposing that the value of the first dimension is
12078 * greater than that of the second.
12080 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12081 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12083 isl_basic_map *gt;
12085 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12087 bmap = isl_basic_map_intersect(bmap, gt);
12089 return bmap;
12092 /* Add a constraint imposing that the value of the first dimension is
12093 * greater than that of the second.
12095 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12096 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12098 isl_basic_map *bmap;
12100 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12102 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12104 return map;
12107 /* Add a constraint imposing that the value of the first dimension is
12108 * smaller than that of the second.
12110 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12111 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12113 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12116 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12117 int pos)
12119 isl_aff *div;
12120 isl_local_space *ls;
12122 if (!bmap)
12123 return NULL;
12125 if (!isl_basic_map_divs_known(bmap))
12126 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12127 "some divs are unknown", return NULL);
12129 ls = isl_basic_map_get_local_space(bmap);
12130 div = isl_local_space_get_div(ls, pos);
12131 isl_local_space_free(ls);
12133 return div;
12136 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12137 int pos)
12139 return isl_basic_map_get_div(bset, pos);
12142 /* Plug in "subs" for dimension "type", "pos" of "bset".
12144 * Let i be the dimension to replace and let "subs" be of the form
12146 * f/d
12148 * Any integer division with a non-zero coefficient for i,
12150 * floor((a i + g)/m)
12152 * is replaced by
12154 * floor((a f + d g)/(m d))
12156 * Constraints of the form
12158 * a i + g
12160 * are replaced by
12162 * a f + d g
12164 * We currently require that "subs" is an integral expression.
12165 * Handling rational expressions may require us to add stride constraints
12166 * as we do in isl_basic_set_preimage_multi_aff.
12168 __isl_give isl_basic_set *isl_basic_set_substitute(
12169 __isl_take isl_basic_set *bset,
12170 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12172 int i;
12173 isl_int v;
12174 isl_ctx *ctx;
12176 if (bset && isl_basic_set_plain_is_empty(bset))
12177 return bset;
12179 bset = isl_basic_set_cow(bset);
12180 if (!bset || !subs)
12181 goto error;
12183 ctx = isl_basic_set_get_ctx(bset);
12184 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12185 isl_die(ctx, isl_error_invalid,
12186 "spaces don't match", goto error);
12187 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12188 isl_die(ctx, isl_error_unsupported,
12189 "cannot handle divs yet", goto error);
12190 if (!isl_int_is_one(subs->v->el[0]))
12191 isl_die(ctx, isl_error_invalid,
12192 "can only substitute integer expressions", goto error);
12194 pos += isl_basic_set_offset(bset, type);
12196 isl_int_init(v);
12198 for (i = 0; i < bset->n_eq; ++i) {
12199 if (isl_int_is_zero(bset->eq[i][pos]))
12200 continue;
12201 isl_int_set(v, bset->eq[i][pos]);
12202 isl_int_set_si(bset->eq[i][pos], 0);
12203 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12204 v, subs->v->el + 1, subs->v->size - 1);
12207 for (i = 0; i < bset->n_ineq; ++i) {
12208 if (isl_int_is_zero(bset->ineq[i][pos]))
12209 continue;
12210 isl_int_set(v, bset->ineq[i][pos]);
12211 isl_int_set_si(bset->ineq[i][pos], 0);
12212 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12213 v, subs->v->el + 1, subs->v->size - 1);
12216 for (i = 0; i < bset->n_div; ++i) {
12217 if (isl_int_is_zero(bset->div[i][1 + pos]))
12218 continue;
12219 isl_int_set(v, bset->div[i][1 + pos]);
12220 isl_int_set_si(bset->div[i][1 + pos], 0);
12221 isl_seq_combine(bset->div[i] + 1,
12222 subs->v->el[0], bset->div[i] + 1,
12223 v, subs->v->el + 1, subs->v->size - 1);
12224 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12227 isl_int_clear(v);
12229 bset = isl_basic_set_simplify(bset);
12230 return isl_basic_set_finalize(bset);
12231 error:
12232 isl_basic_set_free(bset);
12233 return NULL;
12236 /* Plug in "subs" for dimension "type", "pos" of "set".
12238 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12239 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12241 int i;
12243 if (set && isl_set_plain_is_empty(set))
12244 return set;
12246 set = isl_set_cow(set);
12247 if (!set || !subs)
12248 goto error;
12250 for (i = set->n - 1; i >= 0; --i) {
12251 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12252 if (remove_if_empty(set, i) < 0)
12253 goto error;
12256 return set;
12257 error:
12258 isl_set_free(set);
12259 return NULL;
12262 /* Check if the range of "ma" is compatible with the domain or range
12263 * (depending on "type") of "bmap".
12265 static isl_stat check_basic_map_compatible_range_multi_aff(
12266 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12267 __isl_keep isl_multi_aff *ma)
12269 isl_bool m;
12270 isl_space *ma_space;
12272 ma_space = isl_multi_aff_get_space(ma);
12274 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12275 if (m < 0)
12276 goto error;
12277 if (!m)
12278 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12279 "parameters don't match", goto error);
12280 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12281 if (m < 0)
12282 goto error;
12283 if (!m)
12284 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12285 "spaces don't match", goto error);
12287 isl_space_free(ma_space);
12288 return isl_stat_ok;
12289 error:
12290 isl_space_free(ma_space);
12291 return isl_stat_error;
12294 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12295 * coefficients before the transformed range of dimensions,
12296 * the "n_after" coefficients after the transformed range of dimensions
12297 * and the coefficients of the other divs in "bmap".
12299 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12300 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12302 int i;
12303 int n_param;
12304 int n_set;
12305 isl_local_space *ls;
12307 if (n_div == 0)
12308 return 0;
12310 ls = isl_aff_get_domain_local_space(ma->p[0]);
12311 if (!ls)
12312 return -1;
12314 n_param = isl_local_space_dim(ls, isl_dim_param);
12315 n_set = isl_local_space_dim(ls, isl_dim_set);
12316 for (i = 0; i < n_div; ++i) {
12317 int o_bmap = 0, o_ls = 0;
12319 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12320 o_bmap += 1 + 1 + n_param;
12321 o_ls += 1 + 1 + n_param;
12322 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12323 o_bmap += n_before;
12324 isl_seq_cpy(bmap->div[i] + o_bmap,
12325 ls->div->row[i] + o_ls, n_set);
12326 o_bmap += n_set;
12327 o_ls += n_set;
12328 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12329 o_bmap += n_after;
12330 isl_seq_cpy(bmap->div[i] + o_bmap,
12331 ls->div->row[i] + o_ls, n_div);
12332 o_bmap += n_div;
12333 o_ls += n_div;
12334 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12335 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12336 goto error;
12339 isl_local_space_free(ls);
12340 return 0;
12341 error:
12342 isl_local_space_free(ls);
12343 return -1;
12346 /* How many stride constraints does "ma" enforce?
12347 * That is, how many of the affine expressions have a denominator
12348 * different from one?
12350 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12352 int i;
12353 int strides = 0;
12355 for (i = 0; i < ma->n; ++i)
12356 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12357 strides++;
12359 return strides;
12362 /* For each affine expression in ma of the form
12364 * x_i = (f_i y + h_i)/m_i
12366 * with m_i different from one, add a constraint to "bmap"
12367 * of the form
12369 * f_i y + h_i = m_i alpha_i
12371 * with alpha_i an additional existentially quantified variable.
12373 * The input variables of "ma" correspond to a subset of the variables
12374 * of "bmap". There are "n_before" variables in "bmap" before this
12375 * subset and "n_after" variables after this subset.
12376 * The integer divisions of the affine expressions in "ma" are assumed
12377 * to have been aligned. There are "n_div_ma" of them and
12378 * they appear first in "bmap", straight after the "n_after" variables.
12380 static __isl_give isl_basic_map *add_ma_strides(
12381 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12382 int n_before, int n_after, int n_div_ma)
12384 int i, k;
12385 int div;
12386 int total;
12387 int n_param;
12388 int n_in;
12390 total = isl_basic_map_total_dim(bmap);
12391 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12392 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12393 for (i = 0; i < ma->n; ++i) {
12394 int o_bmap = 0, o_ma = 1;
12396 if (isl_int_is_one(ma->p[i]->v->el[0]))
12397 continue;
12398 div = isl_basic_map_alloc_div(bmap);
12399 k = isl_basic_map_alloc_equality(bmap);
12400 if (div < 0 || k < 0)
12401 goto error;
12402 isl_int_set_si(bmap->div[div][0], 0);
12403 isl_seq_cpy(bmap->eq[k] + o_bmap,
12404 ma->p[i]->v->el + o_ma, 1 + n_param);
12405 o_bmap += 1 + n_param;
12406 o_ma += 1 + n_param;
12407 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12408 o_bmap += n_before;
12409 isl_seq_cpy(bmap->eq[k] + o_bmap,
12410 ma->p[i]->v->el + o_ma, n_in);
12411 o_bmap += n_in;
12412 o_ma += n_in;
12413 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12414 o_bmap += n_after;
12415 isl_seq_cpy(bmap->eq[k] + o_bmap,
12416 ma->p[i]->v->el + o_ma, n_div_ma);
12417 o_bmap += n_div_ma;
12418 o_ma += n_div_ma;
12419 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12420 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12421 total++;
12424 return bmap;
12425 error:
12426 isl_basic_map_free(bmap);
12427 return NULL;
12430 /* Replace the domain or range space (depending on "type) of "space" by "set".
12432 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12433 enum isl_dim_type type, __isl_take isl_space *set)
12435 if (type == isl_dim_in) {
12436 space = isl_space_range(space);
12437 space = isl_space_map_from_domain_and_range(set, space);
12438 } else {
12439 space = isl_space_domain(space);
12440 space = isl_space_map_from_domain_and_range(space, set);
12443 return space;
12446 /* Compute the preimage of the domain or range (depending on "type")
12447 * of "bmap" under the function represented by "ma".
12448 * In other words, plug in "ma" in the domain or range of "bmap".
12449 * The result is a basic map that lives in the same space as "bmap"
12450 * except that the domain or range has been replaced by
12451 * the domain space of "ma".
12453 * If bmap is represented by
12455 * A(p) + S u + B x + T v + C(divs) >= 0,
12457 * where u and x are input and output dimensions if type == isl_dim_out
12458 * while x and v are input and output dimensions if type == isl_dim_in,
12459 * and ma is represented by
12461 * x = D(p) + F(y) + G(divs')
12463 * then the result is
12465 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12467 * The divs in the input set are similarly adjusted.
12468 * In particular
12470 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12472 * becomes
12474 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12475 * B_i G(divs') + c_i(divs))/n_i)
12477 * If bmap is not a rational map and if F(y) involves any denominators
12479 * x_i = (f_i y + h_i)/m_i
12481 * then additional constraints are added to ensure that we only
12482 * map back integer points. That is we enforce
12484 * f_i y + h_i = m_i alpha_i
12486 * with alpha_i an additional existentially quantified variable.
12488 * We first copy over the divs from "ma".
12489 * Then we add the modified constraints and divs from "bmap".
12490 * Finally, we add the stride constraints, if needed.
12492 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12493 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12494 __isl_take isl_multi_aff *ma)
12496 int i, k;
12497 isl_space *space;
12498 isl_basic_map *res = NULL;
12499 int n_before, n_after, n_div_bmap, n_div_ma;
12500 isl_int f, c1, c2, g;
12501 isl_bool rational;
12502 int strides;
12504 isl_int_init(f);
12505 isl_int_init(c1);
12506 isl_int_init(c2);
12507 isl_int_init(g);
12509 ma = isl_multi_aff_align_divs(ma);
12510 if (!bmap || !ma)
12511 goto error;
12512 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12513 goto error;
12515 if (type == isl_dim_in) {
12516 n_before = 0;
12517 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12518 } else {
12519 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12520 n_after = 0;
12522 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12523 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12525 space = isl_multi_aff_get_domain_space(ma);
12526 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12527 rational = isl_basic_map_is_rational(bmap);
12528 strides = rational ? 0 : multi_aff_strides(ma);
12529 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12530 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12531 if (rational)
12532 res = isl_basic_map_set_rational(res);
12534 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12535 if (isl_basic_map_alloc_div(res) < 0)
12536 goto error;
12538 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12539 goto error;
12541 for (i = 0; i < bmap->n_eq; ++i) {
12542 k = isl_basic_map_alloc_equality(res);
12543 if (k < 0)
12544 goto error;
12545 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12546 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12549 for (i = 0; i < bmap->n_ineq; ++i) {
12550 k = isl_basic_map_alloc_inequality(res);
12551 if (k < 0)
12552 goto error;
12553 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12554 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12557 for (i = 0; i < bmap->n_div; ++i) {
12558 if (isl_int_is_zero(bmap->div[i][0])) {
12559 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12560 continue;
12562 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12563 n_before, n_after, n_div_ma, n_div_bmap,
12564 f, c1, c2, g, 1);
12567 if (strides)
12568 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12570 isl_int_clear(f);
12571 isl_int_clear(c1);
12572 isl_int_clear(c2);
12573 isl_int_clear(g);
12574 isl_basic_map_free(bmap);
12575 isl_multi_aff_free(ma);
12576 res = isl_basic_map_simplify(res);
12577 return isl_basic_map_finalize(res);
12578 error:
12579 isl_int_clear(f);
12580 isl_int_clear(c1);
12581 isl_int_clear(c2);
12582 isl_int_clear(g);
12583 isl_basic_map_free(bmap);
12584 isl_multi_aff_free(ma);
12585 isl_basic_map_free(res);
12586 return NULL;
12589 /* Compute the preimage of "bset" under the function represented by "ma".
12590 * In other words, plug in "ma" in "bset". The result is a basic set
12591 * that lives in the domain space of "ma".
12593 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12594 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12596 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12599 /* Compute the preimage of the domain of "bmap" under the function
12600 * represented by "ma".
12601 * In other words, plug in "ma" in the domain of "bmap".
12602 * The result is a basic map that lives in the same space as "bmap"
12603 * except that the domain has been replaced by the domain space of "ma".
12605 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12606 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12608 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12611 /* Compute the preimage of the range of "bmap" under the function
12612 * represented by "ma".
12613 * In other words, plug in "ma" in the range of "bmap".
12614 * The result is a basic map that lives in the same space as "bmap"
12615 * except that the range has been replaced by the domain space of "ma".
12617 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12618 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12620 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12623 /* Check if the range of "ma" is compatible with the domain or range
12624 * (depending on "type") of "map".
12625 * Return isl_stat_error if anything is wrong.
12627 static isl_stat check_map_compatible_range_multi_aff(
12628 __isl_keep isl_map *map, enum isl_dim_type type,
12629 __isl_keep isl_multi_aff *ma)
12631 isl_bool m;
12632 isl_space *ma_space;
12634 ma_space = isl_multi_aff_get_space(ma);
12635 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12636 isl_space_free(ma_space);
12637 if (m < 0)
12638 return isl_stat_error;
12639 if (!m)
12640 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12641 "spaces don't match", return isl_stat_error);
12642 return isl_stat_ok;
12645 /* Compute the preimage of the domain or range (depending on "type")
12646 * of "map" under the function represented by "ma".
12647 * In other words, plug in "ma" in the domain or range of "map".
12648 * The result is a map that lives in the same space as "map"
12649 * except that the domain or range has been replaced by
12650 * the domain space of "ma".
12652 * The parameters are assumed to have been aligned.
12654 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12655 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12657 int i;
12658 isl_space *space;
12660 map = isl_map_cow(map);
12661 ma = isl_multi_aff_align_divs(ma);
12662 if (!map || !ma)
12663 goto error;
12664 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12665 goto error;
12667 for (i = 0; i < map->n; ++i) {
12668 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12669 isl_multi_aff_copy(ma));
12670 if (!map->p[i])
12671 goto error;
12674 space = isl_multi_aff_get_domain_space(ma);
12675 space = isl_space_set(isl_map_get_space(map), type, space);
12677 isl_space_free(map->dim);
12678 map->dim = space;
12679 if (!map->dim)
12680 goto error;
12682 isl_multi_aff_free(ma);
12683 if (map->n > 1)
12684 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12685 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12686 return map;
12687 error:
12688 isl_multi_aff_free(ma);
12689 isl_map_free(map);
12690 return NULL;
12693 /* Compute the preimage of the domain or range (depending on "type")
12694 * of "map" under the function represented by "ma".
12695 * In other words, plug in "ma" in the domain or range of "map".
12696 * The result is a map that lives in the same space as "map"
12697 * except that the domain or range has been replaced by
12698 * the domain space of "ma".
12700 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12701 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12703 isl_bool aligned;
12705 if (!map || !ma)
12706 goto error;
12708 aligned = isl_map_space_has_equal_params(map, ma->space);
12709 if (aligned < 0)
12710 goto error;
12711 if (aligned)
12712 return map_preimage_multi_aff(map, type, ma);
12714 if (isl_map_check_named_params(map) < 0)
12715 goto error;
12716 if (!isl_space_has_named_params(ma->space))
12717 isl_die(map->ctx, isl_error_invalid,
12718 "unaligned unnamed parameters", goto error);
12719 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12720 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12722 return map_preimage_multi_aff(map, type, ma);
12723 error:
12724 isl_multi_aff_free(ma);
12725 return isl_map_free(map);
12728 /* Compute the preimage of "set" under the function represented by "ma".
12729 * In other words, plug in "ma" in "set". The result is a set
12730 * that lives in the domain space of "ma".
12732 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12733 __isl_take isl_multi_aff *ma)
12735 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12738 /* Compute the preimage of the domain of "map" under the function
12739 * represented by "ma".
12740 * In other words, plug in "ma" in the domain of "map".
12741 * The result is a map that lives in the same space as "map"
12742 * except that the domain has been replaced by the domain space of "ma".
12744 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12745 __isl_take isl_multi_aff *ma)
12747 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12750 /* Compute the preimage of the range of "map" under the function
12751 * represented by "ma".
12752 * In other words, plug in "ma" in the range of "map".
12753 * The result is a map that lives in the same space as "map"
12754 * except that the range has been replaced by the domain space of "ma".
12756 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12757 __isl_take isl_multi_aff *ma)
12759 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12762 /* Compute the preimage of "map" under the function represented by "pma".
12763 * In other words, plug in "pma" in the domain or range of "map".
12764 * The result is a map that lives in the same space as "map",
12765 * except that the space of type "type" has been replaced by
12766 * the domain space of "pma".
12768 * The parameters of "map" and "pma" are assumed to have been aligned.
12770 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12771 __isl_take isl_map *map, enum isl_dim_type type,
12772 __isl_take isl_pw_multi_aff *pma)
12774 int i;
12775 isl_map *res;
12777 if (!pma)
12778 goto error;
12780 if (pma->n == 0) {
12781 isl_pw_multi_aff_free(pma);
12782 res = isl_map_empty(isl_map_get_space(map));
12783 isl_map_free(map);
12784 return res;
12787 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12788 isl_multi_aff_copy(pma->p[0].maff));
12789 if (type == isl_dim_in)
12790 res = isl_map_intersect_domain(res,
12791 isl_map_copy(pma->p[0].set));
12792 else
12793 res = isl_map_intersect_range(res,
12794 isl_map_copy(pma->p[0].set));
12796 for (i = 1; i < pma->n; ++i) {
12797 isl_map *res_i;
12799 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12800 isl_multi_aff_copy(pma->p[i].maff));
12801 if (type == isl_dim_in)
12802 res_i = isl_map_intersect_domain(res_i,
12803 isl_map_copy(pma->p[i].set));
12804 else
12805 res_i = isl_map_intersect_range(res_i,
12806 isl_map_copy(pma->p[i].set));
12807 res = isl_map_union(res, res_i);
12810 isl_pw_multi_aff_free(pma);
12811 isl_map_free(map);
12812 return res;
12813 error:
12814 isl_pw_multi_aff_free(pma);
12815 isl_map_free(map);
12816 return NULL;
12819 /* Compute the preimage of "map" under the function represented by "pma".
12820 * In other words, plug in "pma" in the domain or range of "map".
12821 * The result is a map that lives in the same space as "map",
12822 * except that the space of type "type" has been replaced by
12823 * the domain space of "pma".
12825 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12826 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12828 isl_bool aligned;
12830 if (!map || !pma)
12831 goto error;
12833 aligned = isl_map_space_has_equal_params(map, pma->dim);
12834 if (aligned < 0)
12835 goto error;
12836 if (aligned)
12837 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12839 if (isl_map_check_named_params(map) < 0)
12840 goto error;
12841 if (!isl_space_has_named_params(pma->dim))
12842 isl_die(map->ctx, isl_error_invalid,
12843 "unaligned unnamed parameters", goto error);
12844 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12845 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12847 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12848 error:
12849 isl_pw_multi_aff_free(pma);
12850 return isl_map_free(map);
12853 /* Compute the preimage of "set" under the function represented by "pma".
12854 * In other words, plug in "pma" in "set". The result is a set
12855 * that lives in the domain space of "pma".
12857 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12858 __isl_take isl_pw_multi_aff *pma)
12860 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12863 /* Compute the preimage of the domain of "map" under the function
12864 * represented by "pma".
12865 * In other words, plug in "pma" in the domain of "map".
12866 * The result is a map that lives in the same space as "map",
12867 * except that domain space has been replaced by the domain space of "pma".
12869 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12870 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12872 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12875 /* Compute the preimage of the range of "map" under the function
12876 * represented by "pma".
12877 * In other words, plug in "pma" in the range of "map".
12878 * The result is a map that lives in the same space as "map",
12879 * except that range space has been replaced by the domain space of "pma".
12881 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12882 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12884 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12887 /* Compute the preimage of "map" under the function represented by "mpa".
12888 * In other words, plug in "mpa" in the domain or range of "map".
12889 * The result is a map that lives in the same space as "map",
12890 * except that the space of type "type" has been replaced by
12891 * the domain space of "mpa".
12893 * If the map does not involve any constraints that refer to the
12894 * dimensions of the substituted space, then the only possible
12895 * effect of "mpa" on the map is to map the space to a different space.
12896 * We create a separate isl_multi_aff to effectuate this change
12897 * in order to avoid spurious splitting of the map along the pieces
12898 * of "mpa".
12900 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12901 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12903 int n;
12904 isl_pw_multi_aff *pma;
12906 if (!map || !mpa)
12907 goto error;
12909 n = isl_map_dim(map, type);
12910 if (!isl_map_involves_dims(map, type, 0, n)) {
12911 isl_space *space;
12912 isl_multi_aff *ma;
12914 space = isl_multi_pw_aff_get_space(mpa);
12915 isl_multi_pw_aff_free(mpa);
12916 ma = isl_multi_aff_zero(space);
12917 return isl_map_preimage_multi_aff(map, type, ma);
12920 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12921 return isl_map_preimage_pw_multi_aff(map, type, pma);
12922 error:
12923 isl_map_free(map);
12924 isl_multi_pw_aff_free(mpa);
12925 return NULL;
12928 /* Compute the preimage of "map" under the function represented by "mpa".
12929 * In other words, plug in "mpa" in the domain "map".
12930 * The result is a map that lives in the same space as "map",
12931 * except that domain space has been replaced by the domain space of "mpa".
12933 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12934 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12936 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12939 /* Compute the preimage of "set" by the function represented by "mpa".
12940 * In other words, plug in "mpa" in "set".
12942 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12943 __isl_take isl_multi_pw_aff *mpa)
12945 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
12948 /* Are the "n" "coefficients" starting at "first" of the integer division
12949 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
12950 * to each other?
12951 * The "coefficient" at position 0 is the denominator.
12952 * The "coefficient" at position 1 is the constant term.
12954 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
12955 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
12956 unsigned first, unsigned n)
12958 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
12959 return isl_bool_error;
12960 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
12961 return isl_bool_error;
12962 return isl_seq_eq(bmap1->div[pos1] + first,
12963 bmap2->div[pos2] + first, n);
12966 /* Are the integer division expressions at position "pos1" in "bmap1" and
12967 * "pos2" in "bmap2" equal to each other, except that the constant terms
12968 * are different?
12970 isl_bool isl_basic_map_equal_div_expr_except_constant(
12971 __isl_keep isl_basic_map *bmap1, int pos1,
12972 __isl_keep isl_basic_map *bmap2, int pos2)
12974 isl_bool equal;
12975 unsigned total;
12977 if (!bmap1 || !bmap2)
12978 return isl_bool_error;
12979 total = isl_basic_map_total_dim(bmap1);
12980 if (total != isl_basic_map_total_dim(bmap2))
12981 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
12982 "incomparable div expressions", return isl_bool_error);
12983 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
12984 0, 1);
12985 if (equal < 0 || !equal)
12986 return equal;
12987 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
12988 1, 1);
12989 if (equal < 0 || equal)
12990 return isl_bool_not(equal);
12991 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
12992 2, total);
12995 /* Replace the numerator of the constant term of the integer division
12996 * expression at position "div" in "bmap" by "value".
12997 * The caller guarantees that this does not change the meaning
12998 * of the input.
13000 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13001 __isl_take isl_basic_map *bmap, int div, int value)
13003 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13004 return isl_basic_map_free(bmap);
13006 isl_int_set_si(bmap->div[div][1], value);
13008 return bmap;
13011 /* Is the point "inner" internal to inequality constraint "ineq"
13012 * of "bset"?
13013 * The point is considered to be internal to the inequality constraint,
13014 * if it strictly lies on the positive side of the inequality constraint,
13015 * or if it lies on the constraint and the constraint is lexico-positive.
13017 static isl_bool is_internal(__isl_keep isl_vec *inner,
13018 __isl_keep isl_basic_set *bset, int ineq)
13020 isl_ctx *ctx;
13021 int pos;
13022 unsigned total;
13024 if (!inner || !bset)
13025 return isl_bool_error;
13027 ctx = isl_basic_set_get_ctx(bset);
13028 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13029 &ctx->normalize_gcd);
13030 if (!isl_int_is_zero(ctx->normalize_gcd))
13031 return isl_int_is_nonneg(ctx->normalize_gcd);
13033 total = isl_basic_set_dim(bset, isl_dim_all);
13034 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13035 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13038 /* Tighten the inequality constraints of "bset" that are outward with respect
13039 * to the point "vec".
13040 * That is, tighten the constraints that are not satisfied by "vec".
13042 * "vec" is a point internal to some superset S of "bset" that is used
13043 * to make the subsets of S disjoint, by tightening one half of the constraints
13044 * that separate two subsets. In particular, the constraints of S
13045 * are all satisfied by "vec" and should not be tightened.
13046 * Of the internal constraints, those that have "vec" on the outside
13047 * are tightened. The shared facet is included in the adjacent subset
13048 * with the opposite constraint.
13049 * For constraints that saturate "vec", this criterion cannot be used
13050 * to determine which of the two sides should be tightened.
13051 * Instead, the sign of the first non-zero coefficient is used
13052 * to make this choice. Note that this second criterion is never used
13053 * on the constraints of S since "vec" is interior to "S".
13055 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13056 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13058 int j;
13060 bset = isl_basic_set_cow(bset);
13061 if (!bset)
13062 return NULL;
13063 for (j = 0; j < bset->n_ineq; ++j) {
13064 isl_bool internal;
13066 internal = is_internal(vec, bset, j);
13067 if (internal < 0)
13068 return isl_basic_set_free(bset);
13069 if (internal)
13070 continue;
13071 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13074 return bset;