isl_basic_map_align_params: extract out isl_basic_map_check_space
[isl.git] / isl_map.c
blob912a246f0ecfaba37774b5d91411a006227a5f8b
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/id.h>
27 #include <isl/constraint.h>
28 #include "isl_space_private.h"
29 #include "isl_equalities.h"
30 #include <isl_lp_private.h>
31 #include <isl_seq.h>
32 #include <isl/set.h>
33 #include <isl/map.h>
34 #include <isl_reordering.h>
35 #include "isl_sample.h"
36 #include <isl_sort.h>
37 #include "isl_tab.h"
38 #include <isl/vec.h>
39 #include <isl_mat_private.h>
40 #include <isl_vec_private.h>
41 #include <isl_dim_map.h>
42 #include <isl_local_space_private.h>
43 #include <isl_aff_private.h>
44 #include <isl_options_private.h>
45 #include <isl_morph.h>
46 #include <isl_val_private.h>
48 #include <bset_to_bmap.c>
49 #include <bset_from_bmap.c>
50 #include <set_to_map.c>
51 #include <set_from_map.c>
53 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
55 switch (type) {
56 case isl_dim_param: return dim->nparam;
57 case isl_dim_in: return dim->n_in;
58 case isl_dim_out: return dim->n_out;
59 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
60 default: return 0;
64 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
66 switch (type) {
67 case isl_dim_param: return 1;
68 case isl_dim_in: return 1 + dim->nparam;
69 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
70 default: return 0;
74 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
75 enum isl_dim_type type)
77 if (!bmap)
78 return 0;
79 switch (type) {
80 case isl_dim_cst: return 1;
81 case isl_dim_param:
82 case isl_dim_in:
83 case isl_dim_out: return isl_space_dim(bmap->dim, type);
84 case isl_dim_div: return bmap->n_div;
85 case isl_dim_all: return isl_basic_map_total_dim(bmap);
86 default: return 0;
90 /* Return the space of "map".
92 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
94 return map ? map->dim : NULL;
97 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
99 return map ? n(map->dim, type) : 0;
102 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
104 return set ? n(set->dim, type) : 0;
107 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
108 enum isl_dim_type type)
110 isl_space *space;
112 if (!bmap)
113 return 0;
115 space = bmap->dim;
116 switch (type) {
117 case isl_dim_cst: return 0;
118 case isl_dim_param: return 1;
119 case isl_dim_in: return 1 + space->nparam;
120 case isl_dim_out: return 1 + space->nparam + space->n_in;
121 case isl_dim_div: return 1 + space->nparam + space->n_in +
122 space->n_out;
123 default: return 0;
127 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
128 enum isl_dim_type type)
130 return isl_basic_map_offset(bset, type);
133 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
135 return pos(map->dim, type);
138 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
139 enum isl_dim_type type)
141 return isl_basic_map_dim(bset, type);
144 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
146 return isl_basic_set_dim(bset, isl_dim_set);
149 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
151 return isl_basic_set_dim(bset, isl_dim_param);
154 unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
156 if (!bset)
157 return 0;
158 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
161 unsigned isl_set_n_dim(__isl_keep isl_set *set)
163 return isl_set_dim(set, isl_dim_set);
166 unsigned isl_set_n_param(__isl_keep isl_set *set)
168 return isl_set_dim(set, isl_dim_param);
171 unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
173 return bmap ? bmap->dim->n_in : 0;
176 unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
178 return bmap ? bmap->dim->n_out : 0;
181 unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
183 return bmap ? bmap->dim->nparam : 0;
186 unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
188 return bmap ? bmap->n_div : 0;
191 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
193 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
196 unsigned isl_map_n_in(__isl_keep const isl_map *map)
198 return map ? map->dim->n_in : 0;
201 unsigned isl_map_n_out(__isl_keep const isl_map *map)
203 return map ? map->dim->n_out : 0;
206 unsigned isl_map_n_param(__isl_keep const isl_map *map)
208 return map ? map->dim->nparam : 0;
211 /* Return the number of equality constraints in the description of "bmap".
212 * Return -1 on error.
214 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
216 if (!bmap)
217 return -1;
218 return bmap->n_eq;
221 /* Return the number of equality constraints in the description of "bset".
222 * Return -1 on error.
224 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
226 return isl_basic_map_n_equality(bset_to_bmap(bset));
229 /* Return the number of inequality constraints in the description of "bmap".
230 * Return -1 on error.
232 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
234 if (!bmap)
235 return -1;
236 return bmap->n_ineq;
239 /* Return the number of inequality constraints in the description of "bset".
240 * Return -1 on error.
242 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
244 return isl_basic_map_n_inequality(bset_to_bmap(bset));
247 /* Do "bmap1" and "bmap2" have the same parameters?
249 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
250 __isl_keep isl_basic_map *bmap2)
252 isl_space *space1, *space2;
254 space1 = isl_basic_map_peek_space(bmap1);
255 space2 = isl_basic_map_peek_space(bmap2);
256 return isl_space_has_equal_params(space1, space2);
259 /* Do "map1" and "map2" have the same parameters?
261 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
262 __isl_keep isl_map *map2)
264 isl_space *space1, *space2;
266 space1 = isl_map_peek_space(map1);
267 space2 = isl_map_peek_space(map2);
268 return isl_space_has_equal_params(space1, space2);
271 /* Do "map" and "set" have the same parameters?
273 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
274 __isl_keep isl_set *set)
276 return isl_map_has_equal_params(map, set_to_map(set));
279 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
280 __isl_keep isl_set *set)
282 isl_bool m;
283 if (!map || !set)
284 return isl_bool_error;
285 m = isl_map_has_equal_params(map, set_to_map(set));
286 if (m < 0 || !m)
287 return m;
288 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
289 set->dim, isl_dim_set);
292 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
293 __isl_keep isl_basic_set *bset)
295 isl_bool m;
296 if (!bmap || !bset)
297 return isl_bool_error;
298 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
299 if (m < 0 || !m)
300 return m;
301 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
302 bset->dim, isl_dim_set);
305 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
306 __isl_keep isl_set *set)
308 isl_bool m;
309 if (!map || !set)
310 return isl_bool_error;
311 m = isl_map_has_equal_params(map, set_to_map(set));
312 if (m < 0 || !m)
313 return m;
314 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
315 set->dim, isl_dim_set);
318 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
319 __isl_keep isl_basic_set *bset)
321 isl_bool m;
322 if (!bmap || !bset)
323 return isl_bool_error;
324 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
325 if (m < 0 || !m)
326 return m;
327 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
328 bset->dim, isl_dim_set);
331 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
333 return bmap ? bmap->ctx : NULL;
336 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
338 return bset ? bset->ctx : NULL;
341 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
343 return map ? map->ctx : NULL;
346 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
348 return set ? set->ctx : NULL;
351 /* Return the space of "bmap".
353 __isl_keep isl_space *isl_basic_map_peek_space(
354 __isl_keep const isl_basic_map *bmap)
356 return bmap ? bmap->dim : NULL;
359 /* Return the space of "bset".
361 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
363 return isl_basic_map_peek_space(bset_to_bmap(bset));
366 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
368 return isl_space_copy(isl_basic_map_peek_space(bmap));
371 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
373 return isl_basic_map_get_space(bset_to_bmap(bset));
376 /* Extract the divs in "bmap" as a matrix.
378 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
380 int i;
381 isl_ctx *ctx;
382 isl_mat *div;
383 unsigned total;
384 unsigned cols;
386 if (!bmap)
387 return NULL;
389 ctx = isl_basic_map_get_ctx(bmap);
390 total = isl_space_dim(bmap->dim, isl_dim_all);
391 cols = 1 + 1 + total + bmap->n_div;
392 div = isl_mat_alloc(ctx, bmap->n_div, cols);
393 if (!div)
394 return NULL;
396 for (i = 0; i < bmap->n_div; ++i)
397 isl_seq_cpy(div->row[i], bmap->div[i], cols);
399 return div;
402 /* Extract the divs in "bset" as a matrix.
404 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
406 return isl_basic_map_get_divs(bset);
409 __isl_give isl_local_space *isl_basic_map_get_local_space(
410 __isl_keep isl_basic_map *bmap)
412 isl_mat *div;
414 if (!bmap)
415 return NULL;
417 div = isl_basic_map_get_divs(bmap);
418 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
421 __isl_give isl_local_space *isl_basic_set_get_local_space(
422 __isl_keep isl_basic_set *bset)
424 return isl_basic_map_get_local_space(bset);
427 /* For each known div d = floor(f/m), add the constraints
429 * f - m d >= 0
430 * -(f-(m-1)) + m d >= 0
432 * Do not finalize the result.
434 static __isl_give isl_basic_map *add_known_div_constraints(
435 __isl_take isl_basic_map *bmap)
437 int i;
438 unsigned n_div;
440 if (!bmap)
441 return NULL;
442 n_div = isl_basic_map_dim(bmap, isl_dim_div);
443 if (n_div == 0)
444 return bmap;
445 bmap = isl_basic_map_cow(bmap);
446 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
447 if (!bmap)
448 return NULL;
449 for (i = 0; i < n_div; ++i) {
450 if (isl_int_is_zero(bmap->div[i][0]))
451 continue;
452 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
453 return isl_basic_map_free(bmap);
456 return bmap;
459 __isl_give isl_basic_map *isl_basic_map_from_local_space(
460 __isl_take isl_local_space *ls)
462 int i;
463 int n_div;
464 isl_basic_map *bmap;
466 if (!ls)
467 return NULL;
469 n_div = isl_local_space_dim(ls, isl_dim_div);
470 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
471 n_div, 0, 2 * n_div);
473 for (i = 0; i < n_div; ++i)
474 if (isl_basic_map_alloc_div(bmap) < 0)
475 goto error;
477 for (i = 0; i < n_div; ++i)
478 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
479 bmap = add_known_div_constraints(bmap);
481 isl_local_space_free(ls);
482 return bmap;
483 error:
484 isl_local_space_free(ls);
485 isl_basic_map_free(bmap);
486 return NULL;
489 __isl_give isl_basic_set *isl_basic_set_from_local_space(
490 __isl_take isl_local_space *ls)
492 return isl_basic_map_from_local_space(ls);
495 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
497 return isl_space_copy(isl_map_peek_space(map));
500 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
502 if (!set)
503 return NULL;
504 return isl_space_copy(set->dim);
507 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
508 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
510 bmap = isl_basic_map_cow(bmap);
511 if (!bmap)
512 return NULL;
513 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
514 if (!bmap->dim)
515 goto error;
516 bmap = isl_basic_map_finalize(bmap);
517 return bmap;
518 error:
519 isl_basic_map_free(bmap);
520 return NULL;
523 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
524 __isl_take isl_basic_set *bset, const char *s)
526 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
529 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
530 enum isl_dim_type type)
532 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
535 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
536 enum isl_dim_type type, const char *s)
538 int i;
540 map = isl_map_cow(map);
541 if (!map)
542 return NULL;
544 map->dim = isl_space_set_tuple_name(map->dim, type, s);
545 if (!map->dim)
546 goto error;
548 for (i = 0; i < map->n; ++i) {
549 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
550 if (!map->p[i])
551 goto error;
554 return map;
555 error:
556 isl_map_free(map);
557 return NULL;
560 /* Replace the identifier of the tuple of type "type" by "id".
562 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
563 __isl_take isl_basic_map *bmap,
564 enum isl_dim_type type, __isl_take isl_id *id)
566 bmap = isl_basic_map_cow(bmap);
567 if (!bmap)
568 goto error;
569 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
570 if (!bmap->dim)
571 return isl_basic_map_free(bmap);
572 bmap = isl_basic_map_finalize(bmap);
573 return bmap;
574 error:
575 isl_id_free(id);
576 return NULL;
579 /* Replace the identifier of the tuple by "id".
581 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
582 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
584 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
587 /* Does the input or output tuple have a name?
589 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
591 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
594 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
595 enum isl_dim_type type)
597 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
600 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
601 const char *s)
603 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
604 isl_dim_set, s));
607 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
608 enum isl_dim_type type, __isl_take isl_id *id)
610 map = isl_map_cow(map);
611 if (!map)
612 goto error;
614 map->dim = isl_space_set_tuple_id(map->dim, type, id);
616 return isl_map_reset_space(map, isl_space_copy(map->dim));
617 error:
618 isl_id_free(id);
619 return NULL;
622 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
623 __isl_take isl_id *id)
625 return isl_map_set_tuple_id(set, isl_dim_set, id);
628 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
629 enum isl_dim_type type)
631 map = isl_map_cow(map);
632 if (!map)
633 return NULL;
635 map->dim = isl_space_reset_tuple_id(map->dim, type);
637 return isl_map_reset_space(map, isl_space_copy(map->dim));
640 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
642 return isl_map_reset_tuple_id(set, isl_dim_set);
645 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
647 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
650 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
651 enum isl_dim_type type)
653 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
656 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
658 return isl_map_has_tuple_id(set, isl_dim_set);
661 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
663 return isl_map_get_tuple_id(set, isl_dim_set);
666 /* Does the set tuple have a name?
668 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
670 if (!set)
671 return isl_bool_error;
672 return isl_space_has_tuple_name(set->dim, isl_dim_set);
676 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
678 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
681 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
683 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
686 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
687 enum isl_dim_type type, unsigned pos)
689 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
692 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
693 enum isl_dim_type type, unsigned pos)
695 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
698 /* Does the given dimension have a name?
700 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
701 enum isl_dim_type type, unsigned pos)
703 if (!map)
704 return isl_bool_error;
705 return isl_space_has_dim_name(map->dim, type, pos);
708 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
709 enum isl_dim_type type, unsigned pos)
711 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
714 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
715 enum isl_dim_type type, unsigned pos)
717 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
720 /* Does the given dimension have a name?
722 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
723 enum isl_dim_type type, unsigned pos)
725 if (!set)
726 return isl_bool_error;
727 return isl_space_has_dim_name(set->dim, type, pos);
730 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
731 __isl_take isl_basic_map *bmap,
732 enum isl_dim_type type, unsigned pos, const char *s)
734 bmap = isl_basic_map_cow(bmap);
735 if (!bmap)
736 return NULL;
737 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
738 if (!bmap->dim)
739 goto error;
740 return isl_basic_map_finalize(bmap);
741 error:
742 isl_basic_map_free(bmap);
743 return NULL;
746 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
747 enum isl_dim_type type, unsigned pos, const char *s)
749 int i;
751 map = isl_map_cow(map);
752 if (!map)
753 return NULL;
755 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
756 if (!map->dim)
757 goto error;
759 for (i = 0; i < map->n; ++i) {
760 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
761 if (!map->p[i])
762 goto error;
765 return map;
766 error:
767 isl_map_free(map);
768 return NULL;
771 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
772 __isl_take isl_basic_set *bset,
773 enum isl_dim_type type, unsigned pos, const char *s)
775 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
776 type, pos, s));
779 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
780 enum isl_dim_type type, unsigned pos, const char *s)
782 return set_from_map(isl_map_set_dim_name(set_to_map(set),
783 type, pos, s));
786 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
787 enum isl_dim_type type, unsigned pos)
789 if (!bmap)
790 return isl_bool_error;
791 return isl_space_has_dim_id(bmap->dim, type, pos);
794 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
795 enum isl_dim_type type, unsigned pos)
797 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
800 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
801 enum isl_dim_type type, unsigned pos)
803 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
806 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
807 enum isl_dim_type type, unsigned pos)
809 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
812 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
813 enum isl_dim_type type, unsigned pos)
815 return isl_map_has_dim_id(set, type, pos);
818 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
819 enum isl_dim_type type, unsigned pos)
821 return isl_map_get_dim_id(set, type, pos);
824 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
825 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
827 map = isl_map_cow(map);
828 if (!map)
829 goto error;
831 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
833 return isl_map_reset_space(map, isl_space_copy(map->dim));
834 error:
835 isl_id_free(id);
836 return NULL;
839 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
840 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
842 return isl_map_set_dim_id(set, type, pos, id);
845 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
846 __isl_keep isl_id *id)
848 if (!map)
849 return -1;
850 return isl_space_find_dim_by_id(map->dim, type, id);
853 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
854 __isl_keep isl_id *id)
856 return isl_map_find_dim_by_id(set, type, id);
859 /* Return the position of the dimension of the given type and name
860 * in "bmap".
861 * Return -1 if no such dimension can be found.
863 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
864 enum isl_dim_type type, const char *name)
866 if (!bmap)
867 return -1;
868 return isl_space_find_dim_by_name(bmap->dim, type, name);
871 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
872 const char *name)
874 if (!map)
875 return -1;
876 return isl_space_find_dim_by_name(map->dim, type, name);
879 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
880 const char *name)
882 return isl_map_find_dim_by_name(set, type, name);
885 /* Check whether equality i of bset is a pure stride constraint
886 * on a single dimension, i.e., of the form
888 * v = k e
890 * with k a constant and e an existentially quantified variable.
892 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
894 unsigned nparam;
895 unsigned d;
896 unsigned n_div;
897 int pos1;
898 int pos2;
900 if (!bset)
901 return isl_bool_error;
903 if (!isl_int_is_zero(bset->eq[i][0]))
904 return isl_bool_false;
906 nparam = isl_basic_set_dim(bset, isl_dim_param);
907 d = isl_basic_set_dim(bset, isl_dim_set);
908 n_div = isl_basic_set_dim(bset, isl_dim_div);
910 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
911 return isl_bool_false;
912 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
913 if (pos1 == -1)
914 return isl_bool_false;
915 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
916 d - pos1 - 1) != -1)
917 return isl_bool_false;
919 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
920 if (pos2 == -1)
921 return isl_bool_false;
922 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
923 n_div - pos2 - 1) != -1)
924 return isl_bool_false;
925 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
926 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
927 return isl_bool_false;
929 return isl_bool_true;
932 /* Reset the user pointer on all identifiers of parameters and tuples
933 * of the space of "map".
935 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
937 isl_space *space;
939 space = isl_map_get_space(map);
940 space = isl_space_reset_user(space);
941 map = isl_map_reset_space(map, space);
943 return map;
946 /* Reset the user pointer on all identifiers of parameters and tuples
947 * of the space of "set".
949 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
951 return isl_map_reset_user(set);
954 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
956 if (!bmap)
957 return isl_bool_error;
958 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
961 /* Has "map" been marked as a rational map?
962 * In particular, have all basic maps in "map" been marked this way?
963 * An empty map is not considered to be rational.
964 * Maps where only some of the basic maps are marked rational
965 * are not allowed.
967 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
969 int i;
970 isl_bool rational;
972 if (!map)
973 return isl_bool_error;
974 if (map->n == 0)
975 return isl_bool_false;
976 rational = isl_basic_map_is_rational(map->p[0]);
977 if (rational < 0)
978 return rational;
979 for (i = 1; i < map->n; ++i) {
980 isl_bool rational_i;
982 rational_i = isl_basic_map_is_rational(map->p[i]);
983 if (rational_i < 0)
984 return rational_i;
985 if (rational != rational_i)
986 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
987 "mixed rational and integer basic maps "
988 "not supported", return isl_bool_error);
991 return rational;
994 /* Has "set" been marked as a rational set?
995 * In particular, have all basic set in "set" been marked this way?
996 * An empty set is not considered to be rational.
997 * Sets where only some of the basic sets are marked rational
998 * are not allowed.
1000 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1002 return isl_map_is_rational(set);
1005 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1007 return isl_basic_map_is_rational(bset);
1010 /* Does "bmap" contain any rational points?
1012 * If "bmap" has an equality for each dimension, equating the dimension
1013 * to an integer constant, then it has no rational points, even if it
1014 * is marked as rational.
1016 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1018 isl_bool has_rational = isl_bool_true;
1019 unsigned total;
1021 if (!bmap)
1022 return isl_bool_error;
1023 if (isl_basic_map_plain_is_empty(bmap))
1024 return isl_bool_false;
1025 if (!isl_basic_map_is_rational(bmap))
1026 return isl_bool_false;
1027 bmap = isl_basic_map_copy(bmap);
1028 bmap = isl_basic_map_implicit_equalities(bmap);
1029 if (!bmap)
1030 return isl_bool_error;
1031 total = isl_basic_map_total_dim(bmap);
1032 if (bmap->n_eq == total) {
1033 int i, j;
1034 for (i = 0; i < bmap->n_eq; ++i) {
1035 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1036 if (j < 0)
1037 break;
1038 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1039 !isl_int_is_negone(bmap->eq[i][1 + j]))
1040 break;
1041 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1042 total - j - 1);
1043 if (j >= 0)
1044 break;
1046 if (i == bmap->n_eq)
1047 has_rational = isl_bool_false;
1049 isl_basic_map_free(bmap);
1051 return has_rational;
1054 /* Does "map" contain any rational points?
1056 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1058 int i;
1059 isl_bool has_rational;
1061 if (!map)
1062 return isl_bool_error;
1063 for (i = 0; i < map->n; ++i) {
1064 has_rational = isl_basic_map_has_rational(map->p[i]);
1065 if (has_rational < 0 || has_rational)
1066 return has_rational;
1068 return isl_bool_false;
1071 /* Does "set" contain any rational points?
1073 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1075 return isl_map_has_rational(set);
1078 /* Is this basic set a parameter domain?
1080 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1082 if (!bset)
1083 return isl_bool_error;
1084 return isl_space_is_params(bset->dim);
1087 /* Is this set a parameter domain?
1089 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1091 if (!set)
1092 return isl_bool_error;
1093 return isl_space_is_params(set->dim);
1096 /* Is this map actually a parameter domain?
1097 * Users should never call this function. Outside of isl,
1098 * a map can never be a parameter domain.
1100 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1102 if (!map)
1103 return isl_bool_error;
1104 return isl_space_is_params(map->dim);
1107 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
1108 struct isl_basic_map *bmap, unsigned extra,
1109 unsigned n_eq, unsigned n_ineq)
1111 int i;
1112 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1114 bmap->ctx = ctx;
1115 isl_ctx_ref(ctx);
1117 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1118 if (isl_blk_is_error(bmap->block))
1119 goto error;
1121 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1122 if ((n_ineq + n_eq) && !bmap->ineq)
1123 goto error;
1125 if (extra == 0) {
1126 bmap->block2 = isl_blk_empty();
1127 bmap->div = NULL;
1128 } else {
1129 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1130 if (isl_blk_is_error(bmap->block2))
1131 goto error;
1133 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1134 if (!bmap->div)
1135 goto error;
1138 for (i = 0; i < n_ineq + n_eq; ++i)
1139 bmap->ineq[i] = bmap->block.data + i * row_size;
1141 for (i = 0; i < extra; ++i)
1142 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1144 bmap->ref = 1;
1145 bmap->flags = 0;
1146 bmap->c_size = n_eq + n_ineq;
1147 bmap->eq = bmap->ineq + n_ineq;
1148 bmap->extra = extra;
1149 bmap->n_eq = 0;
1150 bmap->n_ineq = 0;
1151 bmap->n_div = 0;
1152 bmap->sample = NULL;
1154 return bmap;
1155 error:
1156 isl_basic_map_free(bmap);
1157 return NULL;
1160 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1161 unsigned nparam, unsigned dim, unsigned extra,
1162 unsigned n_eq, unsigned n_ineq)
1164 struct isl_basic_map *bmap;
1165 isl_space *space;
1167 space = isl_space_set_alloc(ctx, nparam, dim);
1168 if (!space)
1169 return NULL;
1171 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1172 return bset_from_bmap(bmap);
1175 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1176 unsigned extra, unsigned n_eq, unsigned n_ineq)
1178 struct isl_basic_map *bmap;
1179 if (!dim)
1180 return NULL;
1181 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1182 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1183 return bset_from_bmap(bmap);
1184 error:
1185 isl_space_free(dim);
1186 return NULL;
1189 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1190 unsigned extra, unsigned n_eq, unsigned n_ineq)
1192 struct isl_basic_map *bmap;
1194 if (!dim)
1195 return NULL;
1196 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1197 if (!bmap)
1198 goto error;
1199 bmap->dim = dim;
1201 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1202 error:
1203 isl_space_free(dim);
1204 return NULL;
1207 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1208 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1209 unsigned n_eq, unsigned n_ineq)
1211 struct isl_basic_map *bmap;
1212 isl_space *dim;
1214 dim = isl_space_alloc(ctx, nparam, in, out);
1215 if (!dim)
1216 return NULL;
1218 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1219 return bmap;
1222 static void dup_constraints(
1223 struct isl_basic_map *dst, struct isl_basic_map *src)
1225 int i;
1226 unsigned total = isl_basic_map_total_dim(src);
1228 for (i = 0; i < src->n_eq; ++i) {
1229 int j = isl_basic_map_alloc_equality(dst);
1230 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1233 for (i = 0; i < src->n_ineq; ++i) {
1234 int j = isl_basic_map_alloc_inequality(dst);
1235 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1238 for (i = 0; i < src->n_div; ++i) {
1239 int j = isl_basic_map_alloc_div(dst);
1240 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1242 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1245 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1247 struct isl_basic_map *dup;
1249 if (!bmap)
1250 return NULL;
1251 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1252 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1253 if (!dup)
1254 return NULL;
1255 dup_constraints(dup, bmap);
1256 dup->flags = bmap->flags;
1257 dup->sample = isl_vec_copy(bmap->sample);
1258 return dup;
1261 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1263 struct isl_basic_map *dup;
1265 dup = isl_basic_map_dup(bset_to_bmap(bset));
1266 return bset_from_bmap(dup);
1269 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1271 if (!bset)
1272 return NULL;
1274 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1275 bset->ref++;
1276 return bset;
1278 return isl_basic_set_dup(bset);
1281 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1283 if (!set)
1284 return NULL;
1286 set->ref++;
1287 return set;
1290 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1292 if (!bmap)
1293 return NULL;
1295 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1296 bmap->ref++;
1297 return bmap;
1299 bmap = isl_basic_map_dup(bmap);
1300 if (bmap)
1301 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1302 return bmap;
1305 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1307 if (!map)
1308 return NULL;
1310 map->ref++;
1311 return map;
1314 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1316 if (!bmap)
1317 return NULL;
1319 if (--bmap->ref > 0)
1320 return NULL;
1322 isl_ctx_deref(bmap->ctx);
1323 free(bmap->div);
1324 isl_blk_free(bmap->ctx, bmap->block2);
1325 free(bmap->ineq);
1326 isl_blk_free(bmap->ctx, bmap->block);
1327 isl_vec_free(bmap->sample);
1328 isl_space_free(bmap->dim);
1329 free(bmap);
1331 return NULL;
1334 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1336 return isl_basic_map_free(bset_to_bmap(bset));
1339 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1341 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1344 /* Check that "map" has only named parameters, reporting an error
1345 * if it does not.
1347 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1349 return isl_space_check_named_params(isl_map_peek_space(map));
1352 /* Check that "bmap" has only named parameters, reporting an error
1353 * if it does not.
1355 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1357 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1360 /* Check that "bmap1" and "bmap2" have the same parameters,
1361 * reporting an error if they do not.
1363 static isl_stat isl_basic_map_check_equal_params(
1364 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1366 isl_bool match;
1368 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1369 if (match < 0)
1370 return isl_stat_error;
1371 if (!match)
1372 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1373 "parameters don't match", return isl_stat_error);
1374 return isl_stat_ok;
1377 __isl_give isl_map *isl_map_align_params_map_map_and(
1378 __isl_take isl_map *map1, __isl_take isl_map *map2,
1379 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1380 __isl_take isl_map *map2))
1382 if (!map1 || !map2)
1383 goto error;
1384 if (isl_map_has_equal_params(map1, map2))
1385 return fn(map1, map2);
1386 if (isl_map_check_named_params(map1) < 0)
1387 goto error;
1388 if (isl_map_check_named_params(map2) < 0)
1389 goto error;
1390 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1391 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1392 return fn(map1, map2);
1393 error:
1394 isl_map_free(map1);
1395 isl_map_free(map2);
1396 return NULL;
1399 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1400 __isl_keep isl_map *map2,
1401 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1403 isl_bool r;
1405 if (!map1 || !map2)
1406 return isl_bool_error;
1407 if (isl_map_has_equal_params(map1, map2))
1408 return fn(map1, map2);
1409 if (isl_map_check_named_params(map1) < 0)
1410 return isl_bool_error;
1411 if (isl_map_check_named_params(map2) < 0)
1412 return isl_bool_error;
1413 map1 = isl_map_copy(map1);
1414 map2 = isl_map_copy(map2);
1415 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1416 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1417 r = fn(map1, map2);
1418 isl_map_free(map1);
1419 isl_map_free(map2);
1420 return r;
1423 int isl_basic_map_alloc_equality(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_con(bmap, 1), return -1);
1430 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1431 return -1);
1432 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1433 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1434 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1435 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1436 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1437 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1438 isl_int *t;
1439 int j = isl_basic_map_alloc_inequality(bmap);
1440 if (j < 0)
1441 return -1;
1442 t = bmap->ineq[j];
1443 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1444 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1445 bmap->eq[-1] = t;
1446 bmap->n_eq++;
1447 bmap->n_ineq--;
1448 bmap->eq--;
1449 return 0;
1451 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1452 bmap->extra - bmap->n_div);
1453 return bmap->n_eq++;
1456 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1458 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1461 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1463 if (!bmap)
1464 return -1;
1465 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1466 bmap->n_eq -= n;
1467 return 0;
1470 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1472 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1475 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1477 isl_int *t;
1478 if (!bmap)
1479 return -1;
1480 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1482 if (pos != bmap->n_eq - 1) {
1483 t = bmap->eq[pos];
1484 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1485 bmap->eq[bmap->n_eq - 1] = t;
1487 bmap->n_eq--;
1488 return 0;
1491 /* Turn inequality "pos" of "bmap" into an equality.
1493 * In particular, we move the inequality in front of the equalities
1494 * and move the last inequality in the position of the moved inequality.
1495 * Note that isl_tab_make_equalities_explicit depends on this particular
1496 * change in the ordering of the constraints.
1498 void isl_basic_map_inequality_to_equality(
1499 struct isl_basic_map *bmap, unsigned pos)
1501 isl_int *t;
1503 t = bmap->ineq[pos];
1504 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1505 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1506 bmap->eq[-1] = t;
1507 bmap->n_eq++;
1508 bmap->n_ineq--;
1509 bmap->eq--;
1510 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1511 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1512 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1513 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1516 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1518 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1521 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1523 struct isl_ctx *ctx;
1524 if (!bmap)
1525 return -1;
1526 ctx = bmap->ctx;
1527 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1528 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1529 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1530 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1531 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1532 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1533 1 + isl_basic_map_total_dim(bmap),
1534 bmap->extra - bmap->n_div);
1535 return bmap->n_ineq++;
1538 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1540 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1543 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1545 if (!bmap)
1546 return -1;
1547 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1548 bmap->n_ineq -= n;
1549 return 0;
1552 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1554 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1557 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1559 isl_int *t;
1560 if (!bmap)
1561 return -1;
1562 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1564 if (pos != bmap->n_ineq - 1) {
1565 t = bmap->ineq[pos];
1566 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1567 bmap->ineq[bmap->n_ineq - 1] = t;
1568 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1570 bmap->n_ineq--;
1571 return 0;
1574 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1576 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1579 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1580 isl_int *eq)
1582 int k;
1584 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1585 if (!bmap)
1586 return NULL;
1587 k = isl_basic_map_alloc_equality(bmap);
1588 if (k < 0)
1589 goto error;
1590 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1591 return bmap;
1592 error:
1593 isl_basic_map_free(bmap);
1594 return NULL;
1597 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1598 isl_int *eq)
1600 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1603 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1604 isl_int *ineq)
1606 int k;
1608 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1609 if (!bmap)
1610 return NULL;
1611 k = isl_basic_map_alloc_inequality(bmap);
1612 if (k < 0)
1613 goto error;
1614 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1615 return bmap;
1616 error:
1617 isl_basic_map_free(bmap);
1618 return NULL;
1621 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1622 isl_int *ineq)
1624 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1627 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1629 if (!bmap)
1630 return -1;
1631 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1632 isl_seq_clr(bmap->div[bmap->n_div] +
1633 1 + 1 + isl_basic_map_total_dim(bmap),
1634 bmap->extra - bmap->n_div);
1635 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1636 return bmap->n_div++;
1639 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1641 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1644 /* Check that there are "n" dimensions of type "type" starting at "first"
1645 * in "bmap".
1647 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1648 enum isl_dim_type type, unsigned first, unsigned n)
1650 unsigned dim;
1652 if (!bmap)
1653 return isl_stat_error;
1654 dim = isl_basic_map_dim(bmap, type);
1655 if (first + n > dim || first + n < first)
1656 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1657 "position or range out of bounds",
1658 return isl_stat_error);
1659 return isl_stat_ok;
1662 /* Insert an extra integer division, prescribed by "div", to "bmap"
1663 * at (integer division) position "pos".
1665 * The integer division is first added at the end and then moved
1666 * into the right position.
1668 __isl_give isl_basic_map *isl_basic_map_insert_div(
1669 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1671 int i, k;
1673 bmap = isl_basic_map_cow(bmap);
1674 if (!bmap || !div)
1675 return isl_basic_map_free(bmap);
1677 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1678 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1679 "unexpected size", return isl_basic_map_free(bmap));
1680 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1681 return isl_basic_map_free(bmap);
1683 bmap = isl_basic_map_extend_space(bmap,
1684 isl_basic_map_get_space(bmap), 1, 0, 2);
1685 k = isl_basic_map_alloc_div(bmap);
1686 if (k < 0)
1687 return isl_basic_map_free(bmap);
1688 isl_seq_cpy(bmap->div[k], div->el, div->size);
1689 isl_int_set_si(bmap->div[k][div->size], 0);
1691 for (i = k; i > pos; --i)
1692 isl_basic_map_swap_div(bmap, i, i - 1);
1694 return bmap;
1697 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1699 if (!bmap)
1700 return isl_stat_error;
1701 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1702 bmap->n_div -= n;
1703 return isl_stat_ok;
1706 /* Copy constraint from src to dst, putting the vars of src at offset
1707 * dim_off in dst and the divs of src at offset div_off in dst.
1708 * If both sets are actually map, then dim_off applies to the input
1709 * variables.
1711 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1712 struct isl_basic_map *src_map, isl_int *src,
1713 unsigned in_off, unsigned out_off, unsigned div_off)
1715 unsigned src_nparam = isl_basic_map_dim(src_map, isl_dim_param);
1716 unsigned dst_nparam = isl_basic_map_dim(dst_map, isl_dim_param);
1717 unsigned src_in = isl_basic_map_dim(src_map, isl_dim_in);
1718 unsigned dst_in = isl_basic_map_dim(dst_map, isl_dim_in);
1719 unsigned src_out = isl_basic_map_dim(src_map, isl_dim_out);
1720 unsigned dst_out = isl_basic_map_dim(dst_map, isl_dim_out);
1721 isl_int_set(dst[0], src[0]);
1722 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1723 if (dst_nparam > src_nparam)
1724 isl_seq_clr(dst+1+src_nparam,
1725 dst_nparam - src_nparam);
1726 isl_seq_clr(dst+1+dst_nparam, in_off);
1727 isl_seq_cpy(dst+1+dst_nparam+in_off,
1728 src+1+src_nparam,
1729 isl_min(dst_in-in_off, src_in));
1730 if (dst_in-in_off > src_in)
1731 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1732 dst_in - in_off - src_in);
1733 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1734 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1735 src+1+src_nparam+src_in,
1736 isl_min(dst_out-out_off, src_out));
1737 if (dst_out-out_off > src_out)
1738 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1739 dst_out - out_off - src_out);
1740 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1741 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1742 src+1+src_nparam+src_in+src_out,
1743 isl_min(dst_map->extra-div_off, src_map->n_div));
1744 if (dst_map->n_div-div_off > src_map->n_div)
1745 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1746 div_off+src_map->n_div,
1747 dst_map->n_div - div_off - src_map->n_div);
1750 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1751 struct isl_basic_map *src_map, isl_int *src,
1752 unsigned in_off, unsigned out_off, unsigned div_off)
1754 isl_int_set(dst[0], src[0]);
1755 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1758 static __isl_give isl_basic_map *add_constraints(
1759 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1760 unsigned i_pos, unsigned o_pos)
1762 int i;
1763 unsigned div_off;
1765 if (!bmap1 || !bmap2)
1766 goto error;
1768 div_off = bmap1->n_div;
1770 for (i = 0; i < bmap2->n_eq; ++i) {
1771 int i1 = isl_basic_map_alloc_equality(bmap1);
1772 if (i1 < 0)
1773 goto error;
1774 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1775 i_pos, o_pos, div_off);
1778 for (i = 0; i < bmap2->n_ineq; ++i) {
1779 int i1 = isl_basic_map_alloc_inequality(bmap1);
1780 if (i1 < 0)
1781 goto error;
1782 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1783 i_pos, o_pos, div_off);
1786 for (i = 0; i < bmap2->n_div; ++i) {
1787 int i1 = isl_basic_map_alloc_div(bmap1);
1788 if (i1 < 0)
1789 goto error;
1790 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1791 i_pos, o_pos, div_off);
1794 isl_basic_map_free(bmap2);
1796 return bmap1;
1798 error:
1799 isl_basic_map_free(bmap1);
1800 isl_basic_map_free(bmap2);
1801 return NULL;
1804 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1805 struct isl_basic_set *bset2, unsigned pos)
1807 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1808 bset_to_bmap(bset2), 0, pos));
1811 __isl_give isl_basic_map *isl_basic_map_extend_space(
1812 __isl_take isl_basic_map *base, __isl_take isl_space *dim,
1813 unsigned extra, unsigned n_eq, unsigned n_ineq)
1815 struct isl_basic_map *ext;
1816 unsigned flags;
1817 int dims_ok;
1819 if (!dim)
1820 goto error;
1822 if (!base)
1823 goto error;
1825 dims_ok = isl_space_is_equal(base->dim, dim) &&
1826 base->extra >= base->n_div + extra;
1828 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1829 room_for_ineq(base, n_ineq)) {
1830 isl_space_free(dim);
1831 return base;
1834 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1835 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1836 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1837 extra += base->extra;
1838 n_eq += base->n_eq;
1839 n_ineq += base->n_ineq;
1841 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1842 dim = NULL;
1843 if (!ext)
1844 goto error;
1846 if (dims_ok)
1847 ext->sample = isl_vec_copy(base->sample);
1848 flags = base->flags;
1849 ext = add_constraints(ext, base, 0, 0);
1850 if (ext) {
1851 ext->flags = flags;
1852 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1855 return ext;
1857 error:
1858 isl_space_free(dim);
1859 isl_basic_map_free(base);
1860 return NULL;
1863 __isl_give isl_basic_set *isl_basic_set_extend_space(
1864 __isl_take isl_basic_set *base,
1865 __isl_take isl_space *dim, unsigned extra,
1866 unsigned n_eq, unsigned n_ineq)
1868 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1869 dim, extra, n_eq, n_ineq));
1872 struct isl_basic_map *isl_basic_map_extend_constraints(
1873 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1875 if (!base)
1876 return NULL;
1877 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1878 0, n_eq, n_ineq);
1881 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1882 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1883 unsigned n_eq, unsigned n_ineq)
1885 struct isl_basic_map *bmap;
1886 isl_space *dim;
1888 if (!base)
1889 return NULL;
1890 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1891 if (!dim)
1892 goto error;
1894 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1895 return bmap;
1896 error:
1897 isl_basic_map_free(base);
1898 return NULL;
1901 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1902 unsigned nparam, unsigned dim, unsigned extra,
1903 unsigned n_eq, unsigned n_ineq)
1905 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1906 nparam, 0, dim, extra, n_eq, n_ineq));
1909 struct isl_basic_set *isl_basic_set_extend_constraints(
1910 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1912 isl_basic_map *bmap = bset_to_bmap(base);
1913 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1914 return bset_from_bmap(bmap);
1917 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1919 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1922 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1924 if (!bmap)
1925 return NULL;
1927 if (bmap->ref > 1) {
1928 bmap->ref--;
1929 bmap = isl_basic_map_dup(bmap);
1931 if (bmap) {
1932 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1933 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1935 return bmap;
1938 /* Clear all cached information in "map", either because it is about
1939 * to be modified or because it is being freed.
1940 * Always return the same pointer that is passed in.
1941 * This is needed for the use in isl_map_free.
1943 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1945 isl_basic_map_free(map->cached_simple_hull[0]);
1946 isl_basic_map_free(map->cached_simple_hull[1]);
1947 map->cached_simple_hull[0] = NULL;
1948 map->cached_simple_hull[1] = NULL;
1949 return map;
1952 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1954 return isl_map_cow(set);
1957 /* Return an isl_map that is equal to "map" and that has only
1958 * a single reference.
1960 * If the original input already has only one reference, then
1961 * simply return it, but clear all cached information, since
1962 * it may be rendered invalid by the operations that will be
1963 * performed on the result.
1965 * Otherwise, create a duplicate (without any cached information).
1967 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1969 if (!map)
1970 return NULL;
1972 if (map->ref == 1)
1973 return clear_caches(map);
1974 map->ref--;
1975 return isl_map_dup(map);
1978 static void swap_vars(struct isl_blk blk, isl_int *a,
1979 unsigned a_len, unsigned b_len)
1981 isl_seq_cpy(blk.data, a+a_len, b_len);
1982 isl_seq_cpy(blk.data+b_len, a, a_len);
1983 isl_seq_cpy(a, blk.data, b_len+a_len);
1986 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1987 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1989 int i;
1990 struct isl_blk blk;
1992 if (!bmap)
1993 goto error;
1995 isl_assert(bmap->ctx,
1996 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1998 if (n1 == 0 || n2 == 0)
1999 return bmap;
2001 bmap = isl_basic_map_cow(bmap);
2002 if (!bmap)
2003 return NULL;
2005 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2006 if (isl_blk_is_error(blk))
2007 goto error;
2009 for (i = 0; i < bmap->n_eq; ++i)
2010 swap_vars(blk,
2011 bmap->eq[i] + pos, n1, n2);
2013 for (i = 0; i < bmap->n_ineq; ++i)
2014 swap_vars(blk,
2015 bmap->ineq[i] + pos, n1, n2);
2017 for (i = 0; i < bmap->n_div; ++i)
2018 swap_vars(blk,
2019 bmap->div[i]+1 + pos, n1, n2);
2021 isl_blk_free(bmap->ctx, blk);
2023 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
2024 bmap = isl_basic_map_gauss(bmap, NULL);
2025 return isl_basic_map_finalize(bmap);
2026 error:
2027 isl_basic_map_free(bmap);
2028 return NULL;
2031 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2032 __isl_take isl_basic_map *bmap)
2034 int i = 0;
2035 unsigned total;
2036 if (!bmap)
2037 goto error;
2038 total = isl_basic_map_total_dim(bmap);
2039 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2040 return isl_basic_map_free(bmap);
2041 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2042 if (bmap->n_eq > 0)
2043 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2044 else {
2045 i = isl_basic_map_alloc_equality(bmap);
2046 if (i < 0)
2047 goto error;
2049 isl_int_set_si(bmap->eq[i][0], 1);
2050 isl_seq_clr(bmap->eq[i]+1, total);
2051 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2052 isl_vec_free(bmap->sample);
2053 bmap->sample = NULL;
2054 return isl_basic_map_finalize(bmap);
2055 error:
2056 isl_basic_map_free(bmap);
2057 return NULL;
2060 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2061 __isl_take isl_basic_set *bset)
2063 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2066 __isl_give isl_basic_map *isl_basic_map_set_rational(
2067 __isl_take isl_basic_map *bmap)
2069 if (!bmap)
2070 return NULL;
2072 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2073 return bmap;
2075 bmap = isl_basic_map_cow(bmap);
2076 if (!bmap)
2077 return NULL;
2079 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2081 return isl_basic_map_finalize(bmap);
2084 __isl_give isl_basic_set *isl_basic_set_set_rational(
2085 __isl_take isl_basic_set *bset)
2087 return isl_basic_map_set_rational(bset);
2090 __isl_give isl_basic_set *isl_basic_set_set_integral(
2091 __isl_take isl_basic_set *bset)
2093 if (!bset)
2094 return NULL;
2096 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2097 return bset;
2099 bset = isl_basic_set_cow(bset);
2100 if (!bset)
2101 return NULL;
2103 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2105 return isl_basic_set_finalize(bset);
2108 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2110 int i;
2112 map = isl_map_cow(map);
2113 if (!map)
2114 return NULL;
2115 for (i = 0; i < map->n; ++i) {
2116 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2117 if (!map->p[i])
2118 goto error;
2120 return map;
2121 error:
2122 isl_map_free(map);
2123 return NULL;
2126 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2128 return isl_map_set_rational(set);
2131 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2132 * of "bmap").
2134 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2136 isl_int *t = bmap->div[a];
2137 bmap->div[a] = bmap->div[b];
2138 bmap->div[b] = t;
2141 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2142 * div definitions accordingly.
2144 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2146 int i;
2147 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2149 swap_div(bmap, a, b);
2151 for (i = 0; i < bmap->n_eq; ++i)
2152 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2154 for (i = 0; i < bmap->n_ineq; ++i)
2155 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2157 for (i = 0; i < bmap->n_div; ++i)
2158 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2159 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2162 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
2163 * div definitions accordingly.
2165 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
2167 isl_basic_map_swap_div(bset, a, b);
2170 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2172 isl_seq_cpy(c, c + n, rem);
2173 isl_seq_clr(c + rem, n);
2176 /* Drop n dimensions starting at first.
2178 * In principle, this frees up some extra variables as the number
2179 * of columns remains constant, but we would have to extend
2180 * the div array too as the number of rows in this array is assumed
2181 * to be equal to extra.
2183 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2184 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2186 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2189 /* Move "n" divs starting at "first" to the end of the list of divs.
2191 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2192 unsigned first, unsigned n)
2194 isl_int **div;
2195 int i;
2197 if (first + n == bmap->n_div)
2198 return bmap;
2200 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2201 if (!div)
2202 goto error;
2203 for (i = 0; i < n; ++i)
2204 div[i] = bmap->div[first + i];
2205 for (i = 0; i < bmap->n_div - first - n; ++i)
2206 bmap->div[first + i] = bmap->div[first + n + i];
2207 for (i = 0; i < n; ++i)
2208 bmap->div[bmap->n_div - n + i] = div[i];
2209 free(div);
2210 return bmap;
2211 error:
2212 isl_basic_map_free(bmap);
2213 return NULL;
2216 /* Check that there are "n" dimensions of type "type" starting at "first"
2217 * in "map".
2219 static isl_stat isl_map_check_range(__isl_keep isl_map *map,
2220 enum isl_dim_type type, unsigned first, unsigned n)
2222 if (!map)
2223 return isl_stat_error;
2224 if (first + n > isl_map_dim(map, type) || first + n < first)
2225 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2226 "position or range out of bounds",
2227 return isl_stat_error);
2228 return isl_stat_ok;
2231 /* Drop "n" dimensions of type "type" starting at "first".
2233 * In principle, this frees up some extra variables as the number
2234 * of columns remains constant, but we would have to extend
2235 * the div array too as the number of rows in this array is assumed
2236 * to be equal to extra.
2238 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2239 enum isl_dim_type type, unsigned first, unsigned n)
2241 int i;
2242 unsigned dim;
2243 unsigned offset;
2244 unsigned left;
2246 if (!bmap)
2247 goto error;
2249 dim = isl_basic_map_dim(bmap, type);
2250 isl_assert(bmap->ctx, first + n <= dim, goto error);
2252 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2253 return bmap;
2255 bmap = isl_basic_map_cow(bmap);
2256 if (!bmap)
2257 return NULL;
2259 offset = isl_basic_map_offset(bmap, type) + first;
2260 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2261 for (i = 0; i < bmap->n_eq; ++i)
2262 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2264 for (i = 0; i < bmap->n_ineq; ++i)
2265 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2267 for (i = 0; i < bmap->n_div; ++i)
2268 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2270 if (type == isl_dim_div) {
2271 bmap = move_divs_last(bmap, first, n);
2272 if (!bmap)
2273 goto error;
2274 if (isl_basic_map_free_div(bmap, n) < 0)
2275 return isl_basic_map_free(bmap);
2276 } else
2277 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2278 if (!bmap->dim)
2279 goto error;
2281 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2282 bmap = isl_basic_map_simplify(bmap);
2283 return isl_basic_map_finalize(bmap);
2284 error:
2285 isl_basic_map_free(bmap);
2286 return NULL;
2289 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2290 enum isl_dim_type type, unsigned first, unsigned n)
2292 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2293 type, first, n));
2296 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2297 enum isl_dim_type type, unsigned first, unsigned n)
2299 int i;
2301 if (isl_map_check_range(map, type, first, n) < 0)
2302 return isl_map_free(map);
2304 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2305 return map;
2306 map = isl_map_cow(map);
2307 if (!map)
2308 goto error;
2309 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2310 if (!map->dim)
2311 goto error;
2313 for (i = 0; i < map->n; ++i) {
2314 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2315 if (!map->p[i])
2316 goto error;
2318 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2320 return map;
2321 error:
2322 isl_map_free(map);
2323 return NULL;
2326 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2327 enum isl_dim_type type, unsigned first, unsigned n)
2329 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2333 * We don't cow, as the div is assumed to be redundant.
2335 __isl_give isl_basic_map *isl_basic_map_drop_div(
2336 __isl_take isl_basic_map *bmap, unsigned div)
2338 int i;
2339 unsigned pos;
2341 if (!bmap)
2342 goto error;
2344 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
2346 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
2348 for (i = 0; i < bmap->n_eq; ++i)
2349 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
2351 for (i = 0; i < bmap->n_ineq; ++i) {
2352 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
2353 isl_basic_map_drop_inequality(bmap, i);
2354 --i;
2355 continue;
2357 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
2360 for (i = 0; i < bmap->n_div; ++i)
2361 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
2363 if (div != bmap->n_div - 1) {
2364 int j;
2365 isl_int *t = bmap->div[div];
2367 for (j = div; j < bmap->n_div - 1; ++j)
2368 bmap->div[j] = bmap->div[j+1];
2370 bmap->div[bmap->n_div - 1] = t;
2372 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2373 if (isl_basic_map_free_div(bmap, 1) < 0)
2374 return isl_basic_map_free(bmap);
2376 return bmap;
2377 error:
2378 isl_basic_map_free(bmap);
2379 return NULL;
2382 /* Eliminate the specified n dimensions starting at first from the
2383 * constraints, without removing the dimensions from the space.
2384 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2386 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2387 enum isl_dim_type type, unsigned first, unsigned n)
2389 int i;
2391 if (n == 0)
2392 return map;
2394 if (isl_map_check_range(map, type, first, n) < 0)
2395 return isl_map_free(map);
2397 map = isl_map_cow(map);
2398 if (!map)
2399 return NULL;
2401 for (i = 0; i < map->n; ++i) {
2402 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2403 if (!map->p[i])
2404 goto error;
2406 return map;
2407 error:
2408 isl_map_free(map);
2409 return NULL;
2412 /* Eliminate the specified n dimensions starting at first from the
2413 * constraints, without removing the dimensions from the space.
2414 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2416 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2417 enum isl_dim_type type, unsigned first, unsigned n)
2419 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2422 /* Eliminate the specified n dimensions starting at first from the
2423 * constraints, without removing the dimensions from the space.
2424 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2426 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2427 unsigned first, unsigned n)
2429 return isl_set_eliminate(set, isl_dim_set, first, n);
2432 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2433 __isl_take isl_basic_map *bmap)
2435 if (!bmap)
2436 return NULL;
2437 bmap = isl_basic_map_eliminate_vars(bmap,
2438 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2439 if (!bmap)
2440 return NULL;
2441 bmap->n_div = 0;
2442 return isl_basic_map_finalize(bmap);
2445 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2446 __isl_take isl_basic_set *bset)
2448 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2451 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2453 int i;
2455 if (!map)
2456 return NULL;
2457 if (map->n == 0)
2458 return map;
2460 map = isl_map_cow(map);
2461 if (!map)
2462 return NULL;
2464 for (i = 0; i < map->n; ++i) {
2465 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2466 if (!map->p[i])
2467 goto error;
2469 return map;
2470 error:
2471 isl_map_free(map);
2472 return NULL;
2475 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2477 return isl_map_remove_divs(set);
2480 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2481 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2482 unsigned first, unsigned n)
2484 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2485 return isl_basic_map_free(bmap);
2486 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2487 return bmap;
2488 bmap = isl_basic_map_eliminate_vars(bmap,
2489 isl_basic_map_offset(bmap, type) - 1 + first, n);
2490 if (!bmap)
2491 return bmap;
2492 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2493 return bmap;
2494 bmap = isl_basic_map_drop(bmap, type, first, n);
2495 return bmap;
2498 /* Return true if the definition of the given div (recursively) involves
2499 * any of the given variables.
2501 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2502 unsigned first, unsigned n)
2504 int i;
2505 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2507 if (isl_int_is_zero(bmap->div[div][0]))
2508 return isl_bool_false;
2509 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2510 return isl_bool_true;
2512 for (i = bmap->n_div - 1; i >= 0; --i) {
2513 isl_bool involves;
2515 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2516 continue;
2517 involves = div_involves_vars(bmap, i, first, n);
2518 if (involves < 0 || involves)
2519 return involves;
2522 return isl_bool_false;
2525 /* Try and add a lower and/or upper bound on "div" to "bmap"
2526 * based on inequality "i".
2527 * "total" is the total number of variables (excluding the divs).
2528 * "v" is a temporary object that can be used during the calculations.
2529 * If "lb" is set, then a lower bound should be constructed.
2530 * If "ub" is set, then an upper bound should be constructed.
2532 * The calling function has already checked that the inequality does not
2533 * reference "div", but we still need to check that the inequality is
2534 * of the right form. We'll consider the case where we want to construct
2535 * a lower bound. The construction of upper bounds is similar.
2537 * Let "div" be of the form
2539 * q = floor((a + f(x))/d)
2541 * We essentially check if constraint "i" is of the form
2543 * b + f(x) >= 0
2545 * so that we can use it to derive a lower bound on "div".
2546 * However, we allow a slightly more general form
2548 * b + g(x) >= 0
2550 * with the condition that the coefficients of g(x) - f(x) are all
2551 * divisible by d.
2552 * Rewriting this constraint as
2554 * 0 >= -b - g(x)
2556 * adding a + f(x) to both sides and dividing by d, we obtain
2558 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2560 * Taking the floor on both sides, we obtain
2562 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2564 * or
2566 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2568 * In the case of an upper bound, we construct the constraint
2570 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2573 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2574 __isl_take isl_basic_map *bmap, int div, int i,
2575 unsigned total, isl_int v, int lb, int ub)
2577 int j;
2579 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2580 if (lb) {
2581 isl_int_sub(v, bmap->ineq[i][1 + j],
2582 bmap->div[div][1 + 1 + j]);
2583 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2585 if (ub) {
2586 isl_int_add(v, bmap->ineq[i][1 + j],
2587 bmap->div[div][1 + 1 + j]);
2588 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2591 if (!lb && !ub)
2592 return bmap;
2594 bmap = isl_basic_map_cow(bmap);
2595 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2596 if (lb) {
2597 int k = isl_basic_map_alloc_inequality(bmap);
2598 if (k < 0)
2599 goto error;
2600 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2601 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2602 bmap->div[div][1 + j]);
2603 isl_int_cdiv_q(bmap->ineq[k][j],
2604 bmap->ineq[k][j], bmap->div[div][0]);
2606 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2608 if (ub) {
2609 int k = isl_basic_map_alloc_inequality(bmap);
2610 if (k < 0)
2611 goto error;
2612 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2613 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2614 bmap->div[div][1 + j]);
2615 isl_int_fdiv_q(bmap->ineq[k][j],
2616 bmap->ineq[k][j], bmap->div[div][0]);
2618 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2621 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2622 return bmap;
2623 error:
2624 isl_basic_map_free(bmap);
2625 return NULL;
2628 /* This function is called right before "div" is eliminated from "bmap"
2629 * using Fourier-Motzkin.
2630 * Look through the constraints of "bmap" for constraints on the argument
2631 * of the integer division and use them to construct constraints on the
2632 * integer division itself. These constraints can then be combined
2633 * during the Fourier-Motzkin elimination.
2634 * Note that it is only useful to introduce lower bounds on "div"
2635 * if "bmap" already contains upper bounds on "div" as the newly
2636 * introduce lower bounds can then be combined with the pre-existing
2637 * upper bounds. Similarly for upper bounds.
2638 * We therefore first check if "bmap" contains any lower and/or upper bounds
2639 * on "div".
2641 * It is interesting to note that the introduction of these constraints
2642 * can indeed lead to more accurate results, even when compared to
2643 * deriving constraints on the argument of "div" from constraints on "div".
2644 * Consider, for example, the set
2646 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2648 * The second constraint can be rewritten as
2650 * 2 * [(-i-2j+3)/4] + k >= 0
2652 * from which we can derive
2654 * -i - 2j + 3 >= -2k
2656 * or
2658 * i + 2j <= 3 + 2k
2660 * Combined with the first constraint, we obtain
2662 * -3 <= 3 + 2k or k >= -3
2664 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2665 * the first constraint, we obtain
2667 * [(i + 2j)/4] >= [-3/4] = -1
2669 * Combining this constraint with the second constraint, we obtain
2671 * k >= -2
2673 static __isl_give isl_basic_map *insert_bounds_on_div(
2674 __isl_take isl_basic_map *bmap, int div)
2676 int i;
2677 int check_lb, check_ub;
2678 isl_int v;
2679 unsigned total;
2681 if (!bmap)
2682 return NULL;
2684 if (isl_int_is_zero(bmap->div[div][0]))
2685 return bmap;
2687 total = isl_space_dim(bmap->dim, isl_dim_all);
2689 check_lb = 0;
2690 check_ub = 0;
2691 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2692 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2693 if (s > 0)
2694 check_ub = 1;
2695 if (s < 0)
2696 check_lb = 1;
2699 if (!check_lb && !check_ub)
2700 return bmap;
2702 isl_int_init(v);
2704 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2705 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2706 continue;
2708 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2709 check_lb, check_ub);
2712 isl_int_clear(v);
2714 return bmap;
2717 /* Remove all divs (recursively) involving any of the given dimensions
2718 * in their definitions.
2720 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2721 __isl_take isl_basic_map *bmap,
2722 enum isl_dim_type type, unsigned first, unsigned n)
2724 int i;
2726 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2727 return isl_basic_map_free(bmap);
2728 first += isl_basic_map_offset(bmap, type);
2730 for (i = bmap->n_div - 1; i >= 0; --i) {
2731 isl_bool involves;
2733 involves = div_involves_vars(bmap, i, first, n);
2734 if (involves < 0)
2735 return isl_basic_map_free(bmap);
2736 if (!involves)
2737 continue;
2738 bmap = insert_bounds_on_div(bmap, i);
2739 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2740 if (!bmap)
2741 return NULL;
2742 i = bmap->n_div;
2745 return bmap;
2748 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2749 __isl_take isl_basic_set *bset,
2750 enum isl_dim_type type, unsigned first, unsigned n)
2752 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2755 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2756 enum isl_dim_type type, unsigned first, unsigned n)
2758 int i;
2760 if (!map)
2761 return NULL;
2762 if (map->n == 0)
2763 return map;
2765 map = isl_map_cow(map);
2766 if (!map)
2767 return NULL;
2769 for (i = 0; i < map->n; ++i) {
2770 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2771 type, first, n);
2772 if (!map->p[i])
2773 goto error;
2775 return map;
2776 error:
2777 isl_map_free(map);
2778 return NULL;
2781 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2782 enum isl_dim_type type, unsigned first, unsigned n)
2784 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2785 type, first, n));
2788 /* Does the description of "bmap" depend on the specified dimensions?
2789 * We also check whether the dimensions appear in any of the div definitions.
2790 * In principle there is no need for this check. If the dimensions appear
2791 * in a div definition, they also appear in the defining constraints of that
2792 * div.
2794 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2795 enum isl_dim_type type, unsigned first, unsigned n)
2797 int i;
2799 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2800 return isl_bool_error;
2802 first += isl_basic_map_offset(bmap, type);
2803 for (i = 0; i < bmap->n_eq; ++i)
2804 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2805 return isl_bool_true;
2806 for (i = 0; i < bmap->n_ineq; ++i)
2807 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2808 return isl_bool_true;
2809 for (i = 0; i < bmap->n_div; ++i) {
2810 if (isl_int_is_zero(bmap->div[i][0]))
2811 continue;
2812 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2813 return isl_bool_true;
2816 return isl_bool_false;
2819 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2820 enum isl_dim_type type, unsigned first, unsigned n)
2822 int i;
2824 if (isl_map_check_range(map, type, first, n) < 0)
2825 return isl_bool_error;
2827 for (i = 0; i < map->n; ++i) {
2828 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2829 type, first, n);
2830 if (involves < 0 || involves)
2831 return involves;
2834 return isl_bool_false;
2837 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2838 enum isl_dim_type type, unsigned first, unsigned n)
2840 return isl_basic_map_involves_dims(bset, type, first, n);
2843 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2844 enum isl_dim_type type, unsigned first, unsigned n)
2846 return isl_map_involves_dims(set, type, first, n);
2849 /* Drop all constraints in bmap that involve any of the dimensions
2850 * first to first+n-1.
2852 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2853 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2855 int i;
2857 if (n == 0)
2858 return bmap;
2860 bmap = isl_basic_map_cow(bmap);
2862 if (!bmap)
2863 return NULL;
2865 for (i = bmap->n_eq - 1; i >= 0; --i) {
2866 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2867 continue;
2868 isl_basic_map_drop_equality(bmap, i);
2871 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2872 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2873 continue;
2874 isl_basic_map_drop_inequality(bmap, i);
2877 bmap = isl_basic_map_add_known_div_constraints(bmap);
2878 return bmap;
2881 /* Drop all constraints in bset that involve any of the dimensions
2882 * first to first+n-1.
2884 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2885 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2887 return isl_basic_map_drop_constraints_involving(bset, first, n);
2890 /* Drop all constraints in bmap that do not involve any of the dimensions
2891 * first to first + n - 1 of the given type.
2893 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2894 __isl_take isl_basic_map *bmap,
2895 enum isl_dim_type type, unsigned first, unsigned n)
2897 int i;
2899 if (n == 0) {
2900 isl_space *space = isl_basic_map_get_space(bmap);
2901 isl_basic_map_free(bmap);
2902 return isl_basic_map_universe(space);
2904 bmap = isl_basic_map_cow(bmap);
2905 if (!bmap)
2906 return NULL;
2908 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2909 return isl_basic_map_free(bmap);
2911 first += isl_basic_map_offset(bmap, type) - 1;
2913 for (i = bmap->n_eq - 1; i >= 0; --i) {
2914 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2915 continue;
2916 isl_basic_map_drop_equality(bmap, i);
2919 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2920 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2921 continue;
2922 isl_basic_map_drop_inequality(bmap, i);
2925 bmap = isl_basic_map_add_known_div_constraints(bmap);
2926 return bmap;
2929 /* Drop all constraints in bset that do not involve any of the dimensions
2930 * first to first + n - 1 of the given type.
2932 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2933 __isl_take isl_basic_set *bset,
2934 enum isl_dim_type type, unsigned first, unsigned n)
2936 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2937 type, first, n);
2940 /* Drop all constraints in bmap that involve any of the dimensions
2941 * first to first + n - 1 of the given type.
2943 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2944 __isl_take isl_basic_map *bmap,
2945 enum isl_dim_type type, unsigned first, unsigned n)
2947 if (!bmap)
2948 return NULL;
2949 if (n == 0)
2950 return bmap;
2952 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2953 return isl_basic_map_free(bmap);
2955 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2956 first += isl_basic_map_offset(bmap, type) - 1;
2957 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2960 /* Drop all constraints in bset that involve any of the dimensions
2961 * first to first + n - 1 of the given type.
2963 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2964 __isl_take isl_basic_set *bset,
2965 enum isl_dim_type type, unsigned first, unsigned n)
2967 return isl_basic_map_drop_constraints_involving_dims(bset,
2968 type, first, n);
2971 /* Drop constraints from "map" by applying "drop" to each basic map.
2973 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2974 enum isl_dim_type type, unsigned first, unsigned n,
2975 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2976 enum isl_dim_type type, unsigned first, unsigned n))
2978 int i;
2980 if (isl_map_check_range(map, type, first, n) < 0)
2981 return isl_map_free(map);
2983 map = isl_map_cow(map);
2984 if (!map)
2985 return NULL;
2987 for (i = 0; i < map->n; ++i) {
2988 map->p[i] = drop(map->p[i], type, first, n);
2989 if (!map->p[i])
2990 return isl_map_free(map);
2993 if (map->n > 1)
2994 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2996 return map;
2999 /* Drop all constraints in map that involve any of the dimensions
3000 * first to first + n - 1 of the given type.
3002 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3003 __isl_take isl_map *map,
3004 enum isl_dim_type type, unsigned first, unsigned n)
3006 if (n == 0)
3007 return map;
3008 return drop_constraints(map, type, first, n,
3009 &isl_basic_map_drop_constraints_involving_dims);
3012 /* Drop all constraints in "map" that do not involve any of the dimensions
3013 * first to first + n - 1 of the given type.
3015 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3016 __isl_take isl_map *map,
3017 enum isl_dim_type type, unsigned first, unsigned n)
3019 if (n == 0) {
3020 isl_space *space = isl_map_get_space(map);
3021 isl_map_free(map);
3022 return isl_map_universe(space);
3024 return drop_constraints(map, type, first, n,
3025 &isl_basic_map_drop_constraints_not_involving_dims);
3028 /* Drop all constraints in set that involve any of the dimensions
3029 * first to first + n - 1 of the given type.
3031 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3032 __isl_take isl_set *set,
3033 enum isl_dim_type type, unsigned first, unsigned n)
3035 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3038 /* Drop all constraints in "set" that do not involve any of the dimensions
3039 * first to first + n - 1 of the given type.
3041 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3042 __isl_take isl_set *set,
3043 enum isl_dim_type type, unsigned first, unsigned n)
3045 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3048 /* Does local variable "div" of "bmap" have a complete explicit representation?
3049 * Having a complete explicit representation requires not only
3050 * an explicit representation, but also that all local variables
3051 * that appear in this explicit representation in turn have
3052 * a complete explicit representation.
3054 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3056 int i;
3057 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3058 isl_bool marked;
3060 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3061 if (marked < 0 || marked)
3062 return isl_bool_not(marked);
3064 for (i = bmap->n_div - 1; i >= 0; --i) {
3065 isl_bool known;
3067 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3068 continue;
3069 known = isl_basic_map_div_is_known(bmap, i);
3070 if (known < 0 || !known)
3071 return known;
3074 return isl_bool_true;
3077 /* Remove all divs that are unknown or defined in terms of unknown divs.
3079 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3080 __isl_take isl_basic_map *bmap)
3082 int i;
3084 if (!bmap)
3085 return NULL;
3087 for (i = bmap->n_div - 1; i >= 0; --i) {
3088 if (isl_basic_map_div_is_known(bmap, i))
3089 continue;
3090 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3091 if (!bmap)
3092 return NULL;
3093 i = bmap->n_div;
3096 return bmap;
3099 /* Remove all divs that are unknown or defined in terms of unknown divs.
3101 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3102 __isl_take isl_basic_set *bset)
3104 return isl_basic_map_remove_unknown_divs(bset);
3107 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3109 int i;
3111 if (!map)
3112 return NULL;
3113 if (map->n == 0)
3114 return map;
3116 map = isl_map_cow(map);
3117 if (!map)
3118 return NULL;
3120 for (i = 0; i < map->n; ++i) {
3121 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3122 if (!map->p[i])
3123 goto error;
3125 return map;
3126 error:
3127 isl_map_free(map);
3128 return NULL;
3131 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3133 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3136 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3137 __isl_take isl_basic_set *bset,
3138 enum isl_dim_type type, unsigned first, unsigned n)
3140 isl_basic_map *bmap = bset_to_bmap(bset);
3141 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3142 return bset_from_bmap(bmap);
3145 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3146 enum isl_dim_type type, unsigned first, unsigned n)
3148 int i;
3150 if (n == 0)
3151 return map;
3153 map = isl_map_cow(map);
3154 if (isl_map_check_range(map, type, first, n) < 0)
3155 return isl_map_free(map);
3157 for (i = 0; i < map->n; ++i) {
3158 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3159 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3160 if (!map->p[i])
3161 goto error;
3163 map = isl_map_drop(map, type, first, n);
3164 return map;
3165 error:
3166 isl_map_free(map);
3167 return NULL;
3170 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3171 enum isl_dim_type type, unsigned first, unsigned n)
3173 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3174 type, first, n));
3177 /* Project out n inputs starting at first using Fourier-Motzkin */
3178 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3179 unsigned first, unsigned n)
3181 return isl_map_remove_dims(map, isl_dim_in, first, n);
3184 static void dump_term(struct isl_basic_map *bmap,
3185 isl_int c, int pos, FILE *out)
3187 const char *name;
3188 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3189 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3190 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3191 if (!pos)
3192 isl_int_print(out, c, 0);
3193 else {
3194 if (!isl_int_is_one(c))
3195 isl_int_print(out, c, 0);
3196 if (pos < 1 + nparam) {
3197 name = isl_space_get_dim_name(bmap->dim,
3198 isl_dim_param, pos - 1);
3199 if (name)
3200 fprintf(out, "%s", name);
3201 else
3202 fprintf(out, "p%d", pos - 1);
3203 } else if (pos < 1 + nparam + in)
3204 fprintf(out, "i%d", pos - 1 - nparam);
3205 else if (pos < 1 + nparam + dim)
3206 fprintf(out, "o%d", pos - 1 - nparam - in);
3207 else
3208 fprintf(out, "e%d", pos - 1 - nparam - dim);
3212 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3213 int sign, FILE *out)
3215 int i;
3216 int first;
3217 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3218 isl_int v;
3220 isl_int_init(v);
3221 for (i = 0, first = 1; i < len; ++i) {
3222 if (isl_int_sgn(c[i]) * sign <= 0)
3223 continue;
3224 if (!first)
3225 fprintf(out, " + ");
3226 first = 0;
3227 isl_int_abs(v, c[i]);
3228 dump_term(bmap, v, i, out);
3230 isl_int_clear(v);
3231 if (first)
3232 fprintf(out, "0");
3235 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3236 const char *op, FILE *out, int indent)
3238 int i;
3240 fprintf(out, "%*s", indent, "");
3242 dump_constraint_sign(bmap, c, 1, out);
3243 fprintf(out, " %s ", op);
3244 dump_constraint_sign(bmap, c, -1, out);
3246 fprintf(out, "\n");
3248 for (i = bmap->n_div; i < bmap->extra; ++i) {
3249 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3250 continue;
3251 fprintf(out, "%*s", indent, "");
3252 fprintf(out, "ERROR: unused div coefficient not zero\n");
3253 abort();
3257 static void dump_constraints(struct isl_basic_map *bmap,
3258 isl_int **c, unsigned n,
3259 const char *op, FILE *out, int indent)
3261 int i;
3263 for (i = 0; i < n; ++i)
3264 dump_constraint(bmap, c[i], op, out, indent);
3267 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3269 int j;
3270 int first = 1;
3271 unsigned total = isl_basic_map_total_dim(bmap);
3273 for (j = 0; j < 1 + total; ++j) {
3274 if (isl_int_is_zero(exp[j]))
3275 continue;
3276 if (!first && isl_int_is_pos(exp[j]))
3277 fprintf(out, "+");
3278 dump_term(bmap, exp[j], j, out);
3279 first = 0;
3283 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3285 int i;
3287 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3288 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3290 for (i = 0; i < bmap->n_div; ++i) {
3291 fprintf(out, "%*s", indent, "");
3292 fprintf(out, "e%d = [(", i);
3293 dump_affine(bmap, bmap->div[i]+1, out);
3294 fprintf(out, ")/");
3295 isl_int_print(out, bmap->div[i][0], 0);
3296 fprintf(out, "]\n");
3300 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3301 FILE *out, int indent)
3303 if (!bset) {
3304 fprintf(out, "null basic set\n");
3305 return;
3308 fprintf(out, "%*s", indent, "");
3309 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3310 bset->ref, bset->dim->nparam, bset->dim->n_out,
3311 bset->extra, bset->flags);
3312 dump(bset_to_bmap(bset), out, indent);
3315 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3316 FILE *out, int indent)
3318 if (!bmap) {
3319 fprintf(out, "null basic map\n");
3320 return;
3323 fprintf(out, "%*s", indent, "");
3324 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3325 "flags: %x, n_name: %d\n",
3326 bmap->ref,
3327 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3328 bmap->extra, bmap->flags, bmap->dim->n_id);
3329 dump(bmap, out, indent);
3332 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3334 unsigned total;
3335 if (!bmap)
3336 return -1;
3337 total = isl_basic_map_total_dim(bmap);
3338 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
3339 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3340 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3341 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3342 return 0;
3345 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3346 unsigned flags)
3348 if (!space)
3349 return NULL;
3350 if (isl_space_dim(space, isl_dim_in) != 0)
3351 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3352 "set cannot have input dimensions", goto error);
3353 return isl_map_alloc_space(space, n, flags);
3354 error:
3355 isl_space_free(space);
3356 return NULL;
3359 /* Make sure "map" has room for at least "n" more basic maps.
3361 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3363 int i;
3364 struct isl_map *grown = NULL;
3366 if (!map)
3367 return NULL;
3368 isl_assert(map->ctx, n >= 0, goto error);
3369 if (map->n + n <= map->size)
3370 return map;
3371 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3372 if (!grown)
3373 goto error;
3374 for (i = 0; i < map->n; ++i) {
3375 grown->p[i] = isl_basic_map_copy(map->p[i]);
3376 if (!grown->p[i])
3377 goto error;
3378 grown->n++;
3380 isl_map_free(map);
3381 return grown;
3382 error:
3383 isl_map_free(grown);
3384 isl_map_free(map);
3385 return NULL;
3388 /* Make sure "set" has room for at least "n" more basic sets.
3390 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3392 return set_from_map(isl_map_grow(set_to_map(set), n));
3395 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3397 return isl_map_from_basic_map(bset);
3400 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3402 struct isl_map *map;
3404 if (!bmap)
3405 return NULL;
3407 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3408 return isl_map_add_basic_map(map, bmap);
3411 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3412 __isl_take isl_basic_set *bset)
3414 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3415 bset_to_bmap(bset)));
3418 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3420 return isl_map_free(set);
3423 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3425 int i;
3427 if (!set) {
3428 fprintf(out, "null set\n");
3429 return;
3432 fprintf(out, "%*s", indent, "");
3433 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3434 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3435 set->flags);
3436 for (i = 0; i < set->n; ++i) {
3437 fprintf(out, "%*s", indent, "");
3438 fprintf(out, "basic set %d:\n", i);
3439 isl_basic_set_print_internal(set->p[i], out, indent+4);
3443 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3445 int i;
3447 if (!map) {
3448 fprintf(out, "null map\n");
3449 return;
3452 fprintf(out, "%*s", indent, "");
3453 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3454 "flags: %x, n_name: %d\n",
3455 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3456 map->dim->n_out, map->flags, map->dim->n_id);
3457 for (i = 0; i < map->n; ++i) {
3458 fprintf(out, "%*s", indent, "");
3459 fprintf(out, "basic map %d:\n", i);
3460 isl_basic_map_print_internal(map->p[i], out, indent+4);
3464 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3465 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3467 struct isl_basic_map *bmap_domain;
3469 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3470 goto error;
3472 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3473 isl_assert(bset->ctx,
3474 isl_basic_map_compatible_domain(bmap, bset), goto error);
3476 bmap = isl_basic_map_cow(bmap);
3477 if (!bmap)
3478 goto error;
3479 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3480 bset->n_div, bset->n_eq, bset->n_ineq);
3481 bmap_domain = isl_basic_map_from_domain(bset);
3482 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3484 bmap = isl_basic_map_simplify(bmap);
3485 return isl_basic_map_finalize(bmap);
3486 error:
3487 isl_basic_map_free(bmap);
3488 isl_basic_set_free(bset);
3489 return NULL;
3492 /* Check that the space of "bset" is the same as that of the range of "bmap".
3494 static isl_stat isl_basic_map_check_compatible_range(
3495 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3497 isl_bool ok;
3499 ok = isl_basic_map_compatible_range(bmap, bset);
3500 if (ok < 0)
3501 return isl_stat_error;
3502 if (!ok)
3503 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3504 "incompatible spaces", return isl_stat_error);
3506 return isl_stat_ok;
3509 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3510 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3512 struct isl_basic_map *bmap_range;
3514 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3515 goto error;
3517 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3518 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3519 goto error;
3521 if (isl_basic_set_plain_is_universe(bset)) {
3522 isl_basic_set_free(bset);
3523 return bmap;
3526 bmap = isl_basic_map_cow(bmap);
3527 if (!bmap)
3528 goto error;
3529 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3530 bset->n_div, bset->n_eq, bset->n_ineq);
3531 bmap_range = bset_to_bmap(bset);
3532 bmap = add_constraints(bmap, bmap_range, 0, 0);
3534 bmap = isl_basic_map_simplify(bmap);
3535 return isl_basic_map_finalize(bmap);
3536 error:
3537 isl_basic_map_free(bmap);
3538 isl_basic_set_free(bset);
3539 return NULL;
3542 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3543 __isl_keep isl_vec *vec)
3545 int i;
3546 unsigned total;
3547 isl_int s;
3549 if (!bmap || !vec)
3550 return isl_bool_error;
3552 total = 1 + isl_basic_map_total_dim(bmap);
3553 if (total != vec->size)
3554 return isl_bool_false;
3556 isl_int_init(s);
3558 for (i = 0; i < bmap->n_eq; ++i) {
3559 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3560 if (!isl_int_is_zero(s)) {
3561 isl_int_clear(s);
3562 return isl_bool_false;
3566 for (i = 0; i < bmap->n_ineq; ++i) {
3567 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3568 if (isl_int_is_neg(s)) {
3569 isl_int_clear(s);
3570 return isl_bool_false;
3574 isl_int_clear(s);
3576 return isl_bool_true;
3579 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3580 __isl_keep isl_vec *vec)
3582 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3585 __isl_give isl_basic_map *isl_basic_map_intersect(
3586 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3588 struct isl_vec *sample = NULL;
3590 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3591 goto error;
3592 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3593 isl_space_dim(bmap1->dim, isl_dim_param) &&
3594 isl_space_dim(bmap2->dim, isl_dim_all) !=
3595 isl_space_dim(bmap2->dim, isl_dim_param))
3596 return isl_basic_map_intersect(bmap2, bmap1);
3598 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3599 isl_space_dim(bmap2->dim, isl_dim_param))
3600 isl_assert(bmap1->ctx,
3601 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3603 if (isl_basic_map_plain_is_empty(bmap1)) {
3604 isl_basic_map_free(bmap2);
3605 return bmap1;
3607 if (isl_basic_map_plain_is_empty(bmap2)) {
3608 isl_basic_map_free(bmap1);
3609 return bmap2;
3612 if (bmap1->sample &&
3613 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3614 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3615 sample = isl_vec_copy(bmap1->sample);
3616 else if (bmap2->sample &&
3617 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3618 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3619 sample = isl_vec_copy(bmap2->sample);
3621 bmap1 = isl_basic_map_cow(bmap1);
3622 if (!bmap1)
3623 goto error;
3624 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3625 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3626 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3628 if (!bmap1)
3629 isl_vec_free(sample);
3630 else if (sample) {
3631 isl_vec_free(bmap1->sample);
3632 bmap1->sample = sample;
3635 bmap1 = isl_basic_map_simplify(bmap1);
3636 return isl_basic_map_finalize(bmap1);
3637 error:
3638 if (sample)
3639 isl_vec_free(sample);
3640 isl_basic_map_free(bmap1);
3641 isl_basic_map_free(bmap2);
3642 return NULL;
3645 struct isl_basic_set *isl_basic_set_intersect(
3646 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3648 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3649 bset_to_bmap(bset2)));
3652 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3653 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3655 return isl_basic_set_intersect(bset1, bset2);
3658 /* Special case of isl_map_intersect, where both map1 and map2
3659 * are convex, without any divs and such that either map1 or map2
3660 * contains a single constraint. This constraint is then simply
3661 * added to the other map.
3663 static __isl_give isl_map *map_intersect_add_constraint(
3664 __isl_take isl_map *map1, __isl_take isl_map *map2)
3666 isl_assert(map1->ctx, map1->n == 1, goto error);
3667 isl_assert(map2->ctx, map1->n == 1, goto error);
3668 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3669 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3671 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3672 return isl_map_intersect(map2, map1);
3674 map1 = isl_map_cow(map1);
3675 if (!map1)
3676 goto error;
3677 if (isl_map_plain_is_empty(map1)) {
3678 isl_map_free(map2);
3679 return map1;
3681 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3682 if (map2->p[0]->n_eq == 1)
3683 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3684 else
3685 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3686 map2->p[0]->ineq[0]);
3688 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3689 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3690 if (!map1->p[0])
3691 goto error;
3693 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3694 isl_basic_map_free(map1->p[0]);
3695 map1->n = 0;
3698 isl_map_free(map2);
3700 return map1;
3701 error:
3702 isl_map_free(map1);
3703 isl_map_free(map2);
3704 return NULL;
3707 /* map2 may be either a parameter domain or a map living in the same
3708 * space as map1.
3710 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3711 __isl_take isl_map *map2)
3713 unsigned flags = 0;
3714 isl_map *result;
3715 int i, j;
3717 if (!map1 || !map2)
3718 goto error;
3720 if ((isl_map_plain_is_empty(map1) ||
3721 isl_map_plain_is_universe(map2)) &&
3722 isl_space_is_equal(map1->dim, map2->dim)) {
3723 isl_map_free(map2);
3724 return map1;
3726 if ((isl_map_plain_is_empty(map2) ||
3727 isl_map_plain_is_universe(map1)) &&
3728 isl_space_is_equal(map1->dim, map2->dim)) {
3729 isl_map_free(map1);
3730 return map2;
3733 if (map1->n == 1 && map2->n == 1 &&
3734 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3735 isl_space_is_equal(map1->dim, map2->dim) &&
3736 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3737 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3738 return map_intersect_add_constraint(map1, map2);
3740 if (isl_space_dim(map2->dim, isl_dim_all) !=
3741 isl_space_dim(map2->dim, isl_dim_param))
3742 isl_assert(map1->ctx,
3743 isl_space_is_equal(map1->dim, map2->dim), goto error);
3745 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3746 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3747 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3749 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3750 map1->n * map2->n, flags);
3751 if (!result)
3752 goto error;
3753 for (i = 0; i < map1->n; ++i)
3754 for (j = 0; j < map2->n; ++j) {
3755 struct isl_basic_map *part;
3756 part = isl_basic_map_intersect(
3757 isl_basic_map_copy(map1->p[i]),
3758 isl_basic_map_copy(map2->p[j]));
3759 if (isl_basic_map_is_empty(part) < 0)
3760 part = isl_basic_map_free(part);
3761 result = isl_map_add_basic_map(result, part);
3762 if (!result)
3763 goto error;
3765 isl_map_free(map1);
3766 isl_map_free(map2);
3767 return result;
3768 error:
3769 isl_map_free(map1);
3770 isl_map_free(map2);
3771 return NULL;
3774 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3775 __isl_take isl_map *map2)
3777 if (!map1 || !map2)
3778 goto error;
3779 if (!isl_space_is_equal(map1->dim, map2->dim))
3780 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3781 "spaces don't match", goto error);
3782 return map_intersect_internal(map1, map2);
3783 error:
3784 isl_map_free(map1);
3785 isl_map_free(map2);
3786 return NULL;
3789 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3790 __isl_take isl_map *map2)
3792 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3795 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3797 return set_from_map(isl_map_intersect(set_to_map(set1),
3798 set_to_map(set2)));
3801 /* map_intersect_internal accepts intersections
3802 * with parameter domains, so we can just call that function.
3804 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3805 __isl_take isl_set *params)
3807 return map_intersect_internal(map, params);
3810 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3811 __isl_take isl_map *map2)
3813 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3816 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3817 __isl_take isl_set *params)
3819 return isl_map_intersect_params(set, params);
3822 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3824 isl_space *space;
3825 unsigned pos, n1, n2;
3827 if (!bmap)
3828 return NULL;
3829 bmap = isl_basic_map_cow(bmap);
3830 if (!bmap)
3831 return NULL;
3832 space = isl_space_reverse(isl_space_copy(bmap->dim));
3833 pos = isl_basic_map_offset(bmap, isl_dim_in);
3834 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3835 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3836 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3837 return isl_basic_map_reset_space(bmap, space);
3840 static __isl_give isl_basic_map *basic_map_space_reset(
3841 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3843 isl_space *space;
3845 if (!bmap)
3846 return NULL;
3847 if (!isl_space_is_named_or_nested(bmap->dim, type))
3848 return bmap;
3850 space = isl_basic_map_get_space(bmap);
3851 space = isl_space_reset(space, type);
3852 bmap = isl_basic_map_reset_space(bmap, space);
3853 return bmap;
3856 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3857 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3858 unsigned pos, unsigned n)
3860 isl_bool rational;
3861 isl_space *res_dim;
3862 struct isl_basic_map *res;
3863 struct isl_dim_map *dim_map;
3864 unsigned total, off;
3865 enum isl_dim_type t;
3867 if (n == 0)
3868 return basic_map_space_reset(bmap, type);
3870 if (!bmap)
3871 return NULL;
3873 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3875 total = isl_basic_map_total_dim(bmap) + n;
3876 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3877 off = 0;
3878 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3879 if (t != type) {
3880 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3881 } else {
3882 unsigned size = isl_basic_map_dim(bmap, t);
3883 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3884 0, pos, off);
3885 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3886 pos, size - pos, off + pos + n);
3888 off += isl_space_dim(res_dim, t);
3890 isl_dim_map_div(dim_map, bmap, off);
3892 res = isl_basic_map_alloc_space(res_dim,
3893 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3894 rational = isl_basic_map_is_rational(bmap);
3895 if (rational < 0)
3896 res = isl_basic_map_free(res);
3897 if (rational)
3898 res = isl_basic_map_set_rational(res);
3899 if (isl_basic_map_plain_is_empty(bmap)) {
3900 isl_basic_map_free(bmap);
3901 free(dim_map);
3902 return isl_basic_map_set_to_empty(res);
3904 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3905 return isl_basic_map_finalize(res);
3908 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3909 __isl_take isl_basic_set *bset,
3910 enum isl_dim_type type, unsigned pos, unsigned n)
3912 return isl_basic_map_insert_dims(bset, type, pos, n);
3915 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3916 enum isl_dim_type type, unsigned n)
3918 if (!bmap)
3919 return NULL;
3920 return isl_basic_map_insert_dims(bmap, type,
3921 isl_basic_map_dim(bmap, type), n);
3924 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3925 enum isl_dim_type type, unsigned n)
3927 if (!bset)
3928 return NULL;
3929 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3930 return isl_basic_map_add_dims(bset, type, n);
3931 error:
3932 isl_basic_set_free(bset);
3933 return NULL;
3936 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3937 enum isl_dim_type type)
3939 isl_space *space;
3941 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3942 return map;
3944 space = isl_map_get_space(map);
3945 space = isl_space_reset(space, type);
3946 map = isl_map_reset_space(map, space);
3947 return map;
3950 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3951 enum isl_dim_type type, unsigned pos, unsigned n)
3953 int i;
3955 if (n == 0)
3956 return map_space_reset(map, type);
3958 map = isl_map_cow(map);
3959 if (!map)
3960 return NULL;
3962 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3963 if (!map->dim)
3964 goto error;
3966 for (i = 0; i < map->n; ++i) {
3967 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3968 if (!map->p[i])
3969 goto error;
3972 return map;
3973 error:
3974 isl_map_free(map);
3975 return NULL;
3978 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3979 enum isl_dim_type type, unsigned pos, unsigned n)
3981 return isl_map_insert_dims(set, type, pos, n);
3984 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3985 enum isl_dim_type type, unsigned n)
3987 if (!map)
3988 return NULL;
3989 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3992 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3993 enum isl_dim_type type, unsigned n)
3995 if (!set)
3996 return NULL;
3997 isl_assert(set->ctx, type != isl_dim_in, goto error);
3998 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3999 error:
4000 isl_set_free(set);
4001 return NULL;
4004 __isl_give isl_basic_map *isl_basic_map_move_dims(
4005 __isl_take isl_basic_map *bmap,
4006 enum isl_dim_type dst_type, unsigned dst_pos,
4007 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4009 struct isl_dim_map *dim_map;
4010 struct isl_basic_map *res;
4011 enum isl_dim_type t;
4012 unsigned total, off;
4014 if (!bmap)
4015 return NULL;
4016 if (n == 0) {
4017 bmap = isl_basic_map_reset(bmap, src_type);
4018 bmap = isl_basic_map_reset(bmap, dst_type);
4019 return bmap;
4022 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4023 return isl_basic_map_free(bmap);
4025 if (dst_type == src_type && dst_pos == src_pos)
4026 return bmap;
4028 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4030 if (pos(bmap->dim, dst_type) + dst_pos ==
4031 pos(bmap->dim, src_type) + src_pos +
4032 ((src_type < dst_type) ? n : 0)) {
4033 bmap = isl_basic_map_cow(bmap);
4034 if (!bmap)
4035 return NULL;
4037 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4038 src_type, src_pos, n);
4039 if (!bmap->dim)
4040 goto error;
4042 bmap = isl_basic_map_finalize(bmap);
4044 return bmap;
4047 total = isl_basic_map_total_dim(bmap);
4048 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4050 off = 0;
4051 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4052 unsigned size = isl_space_dim(bmap->dim, t);
4053 if (t == dst_type) {
4054 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4055 0, dst_pos, off);
4056 off += dst_pos;
4057 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4058 src_pos, n, off);
4059 off += n;
4060 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4061 dst_pos, size - dst_pos, off);
4062 off += size - dst_pos;
4063 } else if (t == src_type) {
4064 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4065 0, src_pos, off);
4066 off += src_pos;
4067 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4068 src_pos + n, size - src_pos - n, off);
4069 off += size - src_pos - n;
4070 } else {
4071 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4072 off += size;
4075 isl_dim_map_div(dim_map, bmap, off);
4077 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4078 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4079 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4080 if (!bmap)
4081 goto error;
4083 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4084 src_type, src_pos, n);
4085 if (!bmap->dim)
4086 goto error;
4088 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
4089 bmap = isl_basic_map_gauss(bmap, NULL);
4090 bmap = isl_basic_map_finalize(bmap);
4092 return bmap;
4093 error:
4094 isl_basic_map_free(bmap);
4095 return NULL;
4098 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4099 enum isl_dim_type dst_type, unsigned dst_pos,
4100 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4102 isl_basic_map *bmap = bset_to_bmap(bset);
4103 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4104 src_type, src_pos, n);
4105 return bset_from_bmap(bmap);
4108 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4109 enum isl_dim_type dst_type, unsigned dst_pos,
4110 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4112 if (!set)
4113 return NULL;
4114 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4115 return set_from_map(isl_map_move_dims(set_to_map(set),
4116 dst_type, dst_pos, src_type, src_pos, n));
4117 error:
4118 isl_set_free(set);
4119 return NULL;
4122 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4123 enum isl_dim_type dst_type, unsigned dst_pos,
4124 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4126 int i;
4128 if (n == 0) {
4129 map = isl_map_reset(map, src_type);
4130 map = isl_map_reset(map, dst_type);
4131 return map;
4134 if (isl_map_check_range(map, src_type, src_pos, n))
4135 return isl_map_free(map);
4137 if (dst_type == src_type && dst_pos == src_pos)
4138 return map;
4140 isl_assert(map->ctx, dst_type != src_type, goto error);
4142 map = isl_map_cow(map);
4143 if (!map)
4144 return NULL;
4146 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4147 if (!map->dim)
4148 goto error;
4150 for (i = 0; i < map->n; ++i) {
4151 map->p[i] = isl_basic_map_move_dims(map->p[i],
4152 dst_type, dst_pos,
4153 src_type, src_pos, n);
4154 if (!map->p[i])
4155 goto error;
4158 return map;
4159 error:
4160 isl_map_free(map);
4161 return NULL;
4164 /* Move the specified dimensions to the last columns right before
4165 * the divs. Don't change the dimension specification of bmap.
4166 * That's the responsibility of the caller.
4168 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4169 enum isl_dim_type type, unsigned first, unsigned n)
4171 struct isl_dim_map *dim_map;
4172 struct isl_basic_map *res;
4173 enum isl_dim_type t;
4174 unsigned total, off;
4176 if (!bmap)
4177 return NULL;
4178 if (pos(bmap->dim, type) + first + n ==
4179 1 + isl_space_dim(bmap->dim, isl_dim_all))
4180 return bmap;
4182 total = isl_basic_map_total_dim(bmap);
4183 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4185 off = 0;
4186 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4187 unsigned size = isl_space_dim(bmap->dim, t);
4188 if (t == type) {
4189 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4190 0, first, off);
4191 off += first;
4192 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4193 first, n, total - bmap->n_div - n);
4194 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4195 first + n, size - (first + n), off);
4196 off += size - (first + n);
4197 } else {
4198 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4199 off += size;
4202 isl_dim_map_div(dim_map, bmap, off + n);
4204 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4205 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4206 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4207 return res;
4210 /* Insert "n" rows in the divs of "bmap".
4212 * The number of columns is not changed, which means that the last
4213 * dimensions of "bmap" are being reintepreted as the new divs.
4214 * The space of "bmap" is not adjusted, however, which means
4215 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4216 * from the space of "bmap" is the responsibility of the caller.
4218 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4219 int n)
4221 int i;
4222 size_t row_size;
4223 isl_int **new_div;
4224 isl_int *old;
4226 bmap = isl_basic_map_cow(bmap);
4227 if (!bmap)
4228 return NULL;
4230 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4231 old = bmap->block2.data;
4232 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4233 (bmap->extra + n) * (1 + row_size));
4234 if (!bmap->block2.data)
4235 return isl_basic_map_free(bmap);
4236 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4237 if (!new_div)
4238 return isl_basic_map_free(bmap);
4239 for (i = 0; i < n; ++i) {
4240 new_div[i] = bmap->block2.data +
4241 (bmap->extra + i) * (1 + row_size);
4242 isl_seq_clr(new_div[i], 1 + row_size);
4244 for (i = 0; i < bmap->extra; ++i)
4245 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4246 free(bmap->div);
4247 bmap->div = new_div;
4248 bmap->n_div += n;
4249 bmap->extra += n;
4251 return bmap;
4254 /* Drop constraints from "bmap" that only involve the variables
4255 * of "type" in the range [first, first + n] that are not related
4256 * to any of the variables outside that interval.
4257 * These constraints cannot influence the values for the variables
4258 * outside the interval, except in case they cause "bmap" to be empty.
4259 * Only drop the constraints if "bmap" is known to be non-empty.
4261 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4262 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4263 unsigned first, unsigned n)
4265 int i;
4266 int *groups;
4267 unsigned dim, n_div;
4268 isl_bool non_empty;
4270 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4271 if (non_empty < 0)
4272 return isl_basic_map_free(bmap);
4273 if (!non_empty)
4274 return bmap;
4276 dim = isl_basic_map_dim(bmap, isl_dim_all);
4277 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4278 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4279 if (!groups)
4280 return isl_basic_map_free(bmap);
4281 first += isl_basic_map_offset(bmap, type) - 1;
4282 for (i = 0; i < first; ++i)
4283 groups[i] = -1;
4284 for (i = first + n; i < dim - n_div; ++i)
4285 groups[i] = -1;
4287 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4289 return bmap;
4292 /* Turn the n dimensions of type type, starting at first
4293 * into existentially quantified variables.
4295 * If a subset of the projected out variables are unrelated
4296 * to any of the variables that remain, then the constraints
4297 * involving this subset are simply dropped first.
4299 __isl_give isl_basic_map *isl_basic_map_project_out(
4300 __isl_take isl_basic_map *bmap,
4301 enum isl_dim_type type, unsigned first, unsigned n)
4303 isl_bool empty;
4305 if (n == 0)
4306 return basic_map_space_reset(bmap, type);
4307 if (type == isl_dim_div)
4308 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4309 "cannot project out existentially quantified variables",
4310 return isl_basic_map_free(bmap));
4312 empty = isl_basic_map_plain_is_empty(bmap);
4313 if (empty < 0)
4314 return isl_basic_map_free(bmap);
4315 if (empty)
4316 bmap = isl_basic_map_set_to_empty(bmap);
4318 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4319 if (!bmap)
4320 return NULL;
4322 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4323 return isl_basic_map_remove_dims(bmap, type, first, n);
4325 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4326 return isl_basic_map_free(bmap);
4328 bmap = move_last(bmap, type, first, n);
4329 bmap = isl_basic_map_cow(bmap);
4330 bmap = insert_div_rows(bmap, n);
4331 if (!bmap)
4332 return NULL;
4334 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4335 if (!bmap->dim)
4336 goto error;
4337 bmap = isl_basic_map_simplify(bmap);
4338 bmap = isl_basic_map_drop_redundant_divs(bmap);
4339 return isl_basic_map_finalize(bmap);
4340 error:
4341 isl_basic_map_free(bmap);
4342 return NULL;
4345 /* Turn the n dimensions of type type, starting at first
4346 * into existentially quantified variables.
4348 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4349 enum isl_dim_type type, unsigned first, unsigned n)
4351 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4352 type, first, n));
4355 /* Turn the n dimensions of type type, starting at first
4356 * into existentially quantified variables.
4358 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4359 enum isl_dim_type type, unsigned first, unsigned n)
4361 int i;
4363 if (n == 0)
4364 return map_space_reset(map, type);
4366 if (isl_map_check_range(map, type, first, n) < 0)
4367 return isl_map_free(map);
4369 map = isl_map_cow(map);
4370 if (!map)
4371 return NULL;
4373 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4374 if (!map->dim)
4375 goto error;
4377 for (i = 0; i < map->n; ++i) {
4378 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4379 if (!map->p[i])
4380 goto error;
4383 return map;
4384 error:
4385 isl_map_free(map);
4386 return NULL;
4389 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4390 * into existentially quantified variables.
4392 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4393 enum isl_dim_type type, unsigned first, unsigned n)
4395 unsigned dim;
4397 if (isl_map_check_range(map, type, first, n) < 0)
4398 return isl_map_free(map);
4399 dim = isl_map_dim(map, type);
4400 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4401 map = isl_map_project_out(map, type, 0, first);
4402 return map;
4405 /* Turn the n dimensions of type type, starting at first
4406 * into existentially quantified variables.
4408 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4409 enum isl_dim_type type, unsigned first, unsigned n)
4411 return set_from_map(isl_map_project_out(set_to_map(set),
4412 type, first, n));
4415 /* Return a map that projects the elements in "set" onto their
4416 * "n" set dimensions starting at "first".
4417 * "type" should be equal to isl_dim_set.
4419 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4420 enum isl_dim_type type, unsigned first, unsigned n)
4422 int i;
4423 int dim;
4424 isl_map *map;
4426 if (!set)
4427 return NULL;
4428 if (type != isl_dim_set)
4429 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4430 "only set dimensions can be projected out", goto error);
4431 dim = isl_set_dim(set, isl_dim_set);
4432 if (first + n > dim || first + n < first)
4433 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4434 "index out of bounds", goto error);
4436 map = isl_map_from_domain(set);
4437 map = isl_map_add_dims(map, isl_dim_out, n);
4438 for (i = 0; i < n; ++i)
4439 map = isl_map_equate(map, isl_dim_in, first + i,
4440 isl_dim_out, i);
4441 return map;
4442 error:
4443 isl_set_free(set);
4444 return NULL;
4447 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4449 int i, j;
4451 for (i = 0; i < n; ++i) {
4452 j = isl_basic_map_alloc_div(bmap);
4453 if (j < 0)
4454 goto error;
4455 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4457 return bmap;
4458 error:
4459 isl_basic_map_free(bmap);
4460 return NULL;
4463 struct isl_basic_map *isl_basic_map_apply_range(
4464 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4466 isl_space *dim_result = NULL;
4467 struct isl_basic_map *bmap;
4468 unsigned n_in, n_out, n, nparam, total, pos;
4469 struct isl_dim_map *dim_map1, *dim_map2;
4471 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4472 goto error;
4473 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4474 bmap2->dim, isl_dim_in))
4475 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4476 "spaces don't match", goto error);
4478 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4479 isl_space_copy(bmap2->dim));
4481 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4482 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4483 n = isl_basic_map_dim(bmap1, isl_dim_out);
4484 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4486 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4487 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4488 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4489 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4490 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4491 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4492 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4493 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4494 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4495 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4496 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4498 bmap = isl_basic_map_alloc_space(dim_result,
4499 bmap1->n_div + bmap2->n_div + n,
4500 bmap1->n_eq + bmap2->n_eq,
4501 bmap1->n_ineq + bmap2->n_ineq);
4502 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4503 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4504 bmap = add_divs(bmap, n);
4505 bmap = isl_basic_map_simplify(bmap);
4506 bmap = isl_basic_map_drop_redundant_divs(bmap);
4507 return isl_basic_map_finalize(bmap);
4508 error:
4509 isl_basic_map_free(bmap1);
4510 isl_basic_map_free(bmap2);
4511 return NULL;
4514 struct isl_basic_set *isl_basic_set_apply(
4515 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4517 if (!bset || !bmap)
4518 goto error;
4520 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4521 goto error);
4523 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4524 bmap));
4525 error:
4526 isl_basic_set_free(bset);
4527 isl_basic_map_free(bmap);
4528 return NULL;
4531 struct isl_basic_map *isl_basic_map_apply_domain(
4532 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4534 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4535 goto error;
4536 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4537 bmap2->dim, isl_dim_in))
4538 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4539 "spaces don't match", goto error);
4541 bmap1 = isl_basic_map_reverse(bmap1);
4542 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4543 return isl_basic_map_reverse(bmap1);
4544 error:
4545 isl_basic_map_free(bmap1);
4546 isl_basic_map_free(bmap2);
4547 return NULL;
4550 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4551 * A \cap B -> f(A) + f(B)
4553 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4554 __isl_take isl_basic_map *bmap2)
4556 unsigned n_in, n_out, nparam, total, pos;
4557 struct isl_basic_map *bmap = NULL;
4558 struct isl_dim_map *dim_map1, *dim_map2;
4559 int i;
4561 if (!bmap1 || !bmap2)
4562 goto error;
4564 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4565 goto error);
4567 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4568 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4569 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4571 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4572 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4573 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4574 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4575 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4576 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4577 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4578 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4579 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4580 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4581 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4583 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4584 bmap1->n_div + bmap2->n_div + 2 * n_out,
4585 bmap1->n_eq + bmap2->n_eq + n_out,
4586 bmap1->n_ineq + bmap2->n_ineq);
4587 for (i = 0; i < n_out; ++i) {
4588 int j = isl_basic_map_alloc_equality(bmap);
4589 if (j < 0)
4590 goto error;
4591 isl_seq_clr(bmap->eq[j], 1+total);
4592 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4593 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4594 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4596 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4597 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4598 bmap = add_divs(bmap, 2 * n_out);
4600 bmap = isl_basic_map_simplify(bmap);
4601 return isl_basic_map_finalize(bmap);
4602 error:
4603 isl_basic_map_free(bmap);
4604 isl_basic_map_free(bmap1);
4605 isl_basic_map_free(bmap2);
4606 return NULL;
4609 /* Given two maps A -> f(A) and B -> g(B), construct a map
4610 * A \cap B -> f(A) + f(B)
4612 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4613 __isl_take isl_map *map2)
4615 struct isl_map *result;
4616 int i, j;
4618 if (!map1 || !map2)
4619 goto error;
4621 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4623 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4624 map1->n * map2->n, 0);
4625 if (!result)
4626 goto error;
4627 for (i = 0; i < map1->n; ++i)
4628 for (j = 0; j < map2->n; ++j) {
4629 struct isl_basic_map *part;
4630 part = isl_basic_map_sum(
4631 isl_basic_map_copy(map1->p[i]),
4632 isl_basic_map_copy(map2->p[j]));
4633 if (isl_basic_map_is_empty(part))
4634 isl_basic_map_free(part);
4635 else
4636 result = isl_map_add_basic_map(result, part);
4637 if (!result)
4638 goto error;
4640 isl_map_free(map1);
4641 isl_map_free(map2);
4642 return result;
4643 error:
4644 isl_map_free(map1);
4645 isl_map_free(map2);
4646 return NULL;
4649 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4650 __isl_take isl_set *set2)
4652 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4655 /* Given a basic map A -> f(A), construct A -> -f(A).
4657 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4659 int i, j;
4660 unsigned off, n;
4662 bmap = isl_basic_map_cow(bmap);
4663 if (!bmap)
4664 return NULL;
4666 n = isl_basic_map_dim(bmap, isl_dim_out);
4667 off = isl_basic_map_offset(bmap, isl_dim_out);
4668 for (i = 0; i < bmap->n_eq; ++i)
4669 for (j = 0; j < n; ++j)
4670 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4671 for (i = 0; i < bmap->n_ineq; ++i)
4672 for (j = 0; j < n; ++j)
4673 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4674 for (i = 0; i < bmap->n_div; ++i)
4675 for (j = 0; j < n; ++j)
4676 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4677 bmap = isl_basic_map_gauss(bmap, NULL);
4678 return isl_basic_map_finalize(bmap);
4681 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4683 return isl_basic_map_neg(bset);
4686 /* Given a map A -> f(A), construct A -> -f(A).
4688 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4690 int i;
4692 map = isl_map_cow(map);
4693 if (!map)
4694 return NULL;
4696 for (i = 0; i < map->n; ++i) {
4697 map->p[i] = isl_basic_map_neg(map->p[i]);
4698 if (!map->p[i])
4699 goto error;
4702 return map;
4703 error:
4704 isl_map_free(map);
4705 return NULL;
4708 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4710 return set_from_map(isl_map_neg(set_to_map(set)));
4713 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4714 * A -> floor(f(A)/d).
4716 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4717 isl_int d)
4719 unsigned n_in, n_out, nparam, total, pos;
4720 struct isl_basic_map *result = NULL;
4721 struct isl_dim_map *dim_map;
4722 int i;
4724 if (!bmap)
4725 return NULL;
4727 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4728 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4729 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4731 total = nparam + n_in + n_out + bmap->n_div + n_out;
4732 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4733 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4734 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4735 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4736 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4738 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4739 bmap->n_div + n_out,
4740 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4741 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4742 result = add_divs(result, n_out);
4743 for (i = 0; i < n_out; ++i) {
4744 int j;
4745 j = isl_basic_map_alloc_inequality(result);
4746 if (j < 0)
4747 goto error;
4748 isl_seq_clr(result->ineq[j], 1+total);
4749 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4750 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4751 j = isl_basic_map_alloc_inequality(result);
4752 if (j < 0)
4753 goto error;
4754 isl_seq_clr(result->ineq[j], 1+total);
4755 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4756 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4757 isl_int_sub_ui(result->ineq[j][0], d, 1);
4760 result = isl_basic_map_simplify(result);
4761 return isl_basic_map_finalize(result);
4762 error:
4763 isl_basic_map_free(result);
4764 return NULL;
4767 /* Given a map A -> f(A) and an integer d, construct a map
4768 * A -> floor(f(A)/d).
4770 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4772 int i;
4774 map = isl_map_cow(map);
4775 if (!map)
4776 return NULL;
4778 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4779 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4780 for (i = 0; i < map->n; ++i) {
4781 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4782 if (!map->p[i])
4783 goto error;
4786 return map;
4787 error:
4788 isl_map_free(map);
4789 return NULL;
4792 /* Given a map A -> f(A) and an integer d, construct a map
4793 * A -> floor(f(A)/d).
4795 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4796 __isl_take isl_val *d)
4798 if (!map || !d)
4799 goto error;
4800 if (!isl_val_is_int(d))
4801 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4802 "expecting integer denominator", goto error);
4803 map = isl_map_floordiv(map, d->n);
4804 isl_val_free(d);
4805 return map;
4806 error:
4807 isl_map_free(map);
4808 isl_val_free(d);
4809 return NULL;
4812 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4813 unsigned pos)
4815 int i;
4816 unsigned nparam;
4817 unsigned n_in;
4819 i = isl_basic_map_alloc_equality(bmap);
4820 if (i < 0)
4821 goto error;
4822 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4823 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4824 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4825 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4826 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4827 return isl_basic_map_finalize(bmap);
4828 error:
4829 isl_basic_map_free(bmap);
4830 return NULL;
4833 /* Add a constraint to "bmap" expressing i_pos < o_pos
4835 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4836 unsigned pos)
4838 int i;
4839 unsigned nparam;
4840 unsigned n_in;
4842 i = isl_basic_map_alloc_inequality(bmap);
4843 if (i < 0)
4844 goto error;
4845 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4846 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4847 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4848 isl_int_set_si(bmap->ineq[i][0], -1);
4849 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4850 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4851 return isl_basic_map_finalize(bmap);
4852 error:
4853 isl_basic_map_free(bmap);
4854 return NULL;
4857 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4859 static __isl_give isl_basic_map *var_less_or_equal(
4860 __isl_take isl_basic_map *bmap, unsigned pos)
4862 int i;
4863 unsigned nparam;
4864 unsigned n_in;
4866 i = isl_basic_map_alloc_inequality(bmap);
4867 if (i < 0)
4868 goto error;
4869 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4870 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4871 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4872 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4873 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4874 return isl_basic_map_finalize(bmap);
4875 error:
4876 isl_basic_map_free(bmap);
4877 return NULL;
4880 /* Add a constraint to "bmap" expressing i_pos > o_pos
4882 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4883 unsigned pos)
4885 int i;
4886 unsigned nparam;
4887 unsigned n_in;
4889 i = isl_basic_map_alloc_inequality(bmap);
4890 if (i < 0)
4891 goto error;
4892 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4893 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4894 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4895 isl_int_set_si(bmap->ineq[i][0], -1);
4896 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4897 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4898 return isl_basic_map_finalize(bmap);
4899 error:
4900 isl_basic_map_free(bmap);
4901 return NULL;
4904 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4906 static __isl_give isl_basic_map *var_more_or_equal(
4907 __isl_take isl_basic_map *bmap, unsigned pos)
4909 int i;
4910 unsigned nparam;
4911 unsigned n_in;
4913 i = isl_basic_map_alloc_inequality(bmap);
4914 if (i < 0)
4915 goto error;
4916 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4917 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4918 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4919 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4920 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4921 return isl_basic_map_finalize(bmap);
4922 error:
4923 isl_basic_map_free(bmap);
4924 return NULL;
4927 __isl_give isl_basic_map *isl_basic_map_equal(
4928 __isl_take isl_space *dim, unsigned n_equal)
4930 int i;
4931 struct isl_basic_map *bmap;
4932 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4933 if (!bmap)
4934 return NULL;
4935 for (i = 0; i < n_equal && bmap; ++i)
4936 bmap = var_equal(bmap, i);
4937 return isl_basic_map_finalize(bmap);
4940 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4942 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4943 unsigned pos)
4945 int i;
4946 struct isl_basic_map *bmap;
4947 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4948 if (!bmap)
4949 return NULL;
4950 for (i = 0; i < pos && bmap; ++i)
4951 bmap = var_equal(bmap, i);
4952 if (bmap)
4953 bmap = var_less(bmap, pos);
4954 return isl_basic_map_finalize(bmap);
4957 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4959 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4960 __isl_take isl_space *dim, unsigned pos)
4962 int i;
4963 isl_basic_map *bmap;
4965 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4966 for (i = 0; i < pos; ++i)
4967 bmap = var_equal(bmap, i);
4968 bmap = var_less_or_equal(bmap, pos);
4969 return isl_basic_map_finalize(bmap);
4972 /* Return a relation on "dim" expressing i_pos > o_pos
4974 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4975 unsigned pos)
4977 int i;
4978 struct isl_basic_map *bmap;
4979 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4980 if (!bmap)
4981 return NULL;
4982 for (i = 0; i < pos && bmap; ++i)
4983 bmap = var_equal(bmap, i);
4984 if (bmap)
4985 bmap = var_more(bmap, pos);
4986 return isl_basic_map_finalize(bmap);
4989 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4991 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4992 __isl_take isl_space *dim, unsigned pos)
4994 int i;
4995 isl_basic_map *bmap;
4997 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4998 for (i = 0; i < pos; ++i)
4999 bmap = var_equal(bmap, i);
5000 bmap = var_more_or_equal(bmap, pos);
5001 return isl_basic_map_finalize(bmap);
5004 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
5005 unsigned n, int equal)
5007 struct isl_map *map;
5008 int i;
5010 if (n == 0 && equal)
5011 return isl_map_universe(dims);
5013 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
5015 for (i = 0; i + 1 < n; ++i)
5016 map = isl_map_add_basic_map(map,
5017 isl_basic_map_less_at(isl_space_copy(dims), i));
5018 if (n > 0) {
5019 if (equal)
5020 map = isl_map_add_basic_map(map,
5021 isl_basic_map_less_or_equal_at(dims, n - 1));
5022 else
5023 map = isl_map_add_basic_map(map,
5024 isl_basic_map_less_at(dims, n - 1));
5025 } else
5026 isl_space_free(dims);
5028 return map;
5031 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
5033 if (!dims)
5034 return NULL;
5035 return map_lex_lte_first(dims, dims->n_out, equal);
5038 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5040 return map_lex_lte_first(dim, n, 0);
5043 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5045 return map_lex_lte_first(dim, n, 1);
5048 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5050 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5053 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5055 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5058 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
5059 unsigned n, int equal)
5061 struct isl_map *map;
5062 int i;
5064 if (n == 0 && equal)
5065 return isl_map_universe(dims);
5067 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
5069 for (i = 0; i + 1 < n; ++i)
5070 map = isl_map_add_basic_map(map,
5071 isl_basic_map_more_at(isl_space_copy(dims), i));
5072 if (n > 0) {
5073 if (equal)
5074 map = isl_map_add_basic_map(map,
5075 isl_basic_map_more_or_equal_at(dims, n - 1));
5076 else
5077 map = isl_map_add_basic_map(map,
5078 isl_basic_map_more_at(dims, n - 1));
5079 } else
5080 isl_space_free(dims);
5082 return map;
5085 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
5087 if (!dims)
5088 return NULL;
5089 return map_lex_gte_first(dims, dims->n_out, equal);
5092 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5094 return map_lex_gte_first(dim, n, 0);
5097 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5099 return map_lex_gte_first(dim, n, 1);
5102 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5104 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5107 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5109 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5112 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5113 __isl_take isl_set *set2)
5115 isl_map *map;
5116 map = isl_map_lex_le(isl_set_get_space(set1));
5117 map = isl_map_intersect_domain(map, set1);
5118 map = isl_map_intersect_range(map, set2);
5119 return map;
5122 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5123 __isl_take isl_set *set2)
5125 isl_map *map;
5126 map = isl_map_lex_lt(isl_set_get_space(set1));
5127 map = isl_map_intersect_domain(map, set1);
5128 map = isl_map_intersect_range(map, set2);
5129 return map;
5132 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5133 __isl_take isl_set *set2)
5135 isl_map *map;
5136 map = isl_map_lex_ge(isl_set_get_space(set1));
5137 map = isl_map_intersect_domain(map, set1);
5138 map = isl_map_intersect_range(map, set2);
5139 return map;
5142 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5143 __isl_take isl_set *set2)
5145 isl_map *map;
5146 map = isl_map_lex_gt(isl_set_get_space(set1));
5147 map = isl_map_intersect_domain(map, set1);
5148 map = isl_map_intersect_range(map, set2);
5149 return map;
5152 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5153 __isl_take isl_map *map2)
5155 isl_map *map;
5156 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5157 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5158 map = isl_map_apply_range(map, isl_map_reverse(map2));
5159 return map;
5162 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5163 __isl_take isl_map *map2)
5165 isl_map *map;
5166 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5167 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5168 map = isl_map_apply_range(map, isl_map_reverse(map2));
5169 return map;
5172 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5173 __isl_take isl_map *map2)
5175 isl_map *map;
5176 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5177 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5178 map = isl_map_apply_range(map, isl_map_reverse(map2));
5179 return map;
5182 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5183 __isl_take isl_map *map2)
5185 isl_map *map;
5186 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5187 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5188 map = isl_map_apply_range(map, isl_map_reverse(map2));
5189 return map;
5192 /* For a div d = floor(f/m), add the constraint
5194 * f - m d >= 0
5196 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5197 unsigned pos, isl_int *div)
5199 int i;
5200 unsigned total = isl_basic_map_total_dim(bmap);
5202 i = isl_basic_map_alloc_inequality(bmap);
5203 if (i < 0)
5204 return isl_stat_error;
5205 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5206 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
5208 return isl_stat_ok;
5211 /* For a div d = floor(f/m), add the constraint
5213 * -(f-(m-1)) + m d >= 0
5215 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5216 unsigned pos, isl_int *div)
5218 int i;
5219 unsigned total = isl_basic_map_total_dim(bmap);
5221 i = isl_basic_map_alloc_inequality(bmap);
5222 if (i < 0)
5223 return isl_stat_error;
5224 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5225 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
5226 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5227 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5229 return isl_stat_ok;
5232 /* For a div d = floor(f/m), add the constraints
5234 * f - m d >= 0
5235 * -(f-(m-1)) + m d >= 0
5237 * Note that the second constraint is the negation of
5239 * f - m d >= m
5241 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5242 unsigned pos, isl_int *div)
5244 if (add_upper_div_constraint(bmap, pos, div) < 0)
5245 return -1;
5246 if (add_lower_div_constraint(bmap, pos, div) < 0)
5247 return -1;
5248 return 0;
5251 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
5252 unsigned pos, isl_int *div)
5254 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5255 pos, div);
5258 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
5260 unsigned total = isl_basic_map_total_dim(bmap);
5261 unsigned div_pos = total - bmap->n_div + div;
5263 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5264 bmap->div[div]);
5267 /* For each known div d = floor(f/m), add the constraints
5269 * f - m d >= 0
5270 * -(f-(m-1)) + m d >= 0
5272 * Remove duplicate constraints in case of some these div constraints
5273 * already appear in "bmap".
5275 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5276 __isl_take isl_basic_map *bmap)
5278 unsigned n_div;
5280 if (!bmap)
5281 return NULL;
5282 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5283 if (n_div == 0)
5284 return bmap;
5286 bmap = add_known_div_constraints(bmap);
5287 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5288 bmap = isl_basic_map_finalize(bmap);
5289 return bmap;
5292 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5294 * In particular, if this div is of the form d = floor(f/m),
5295 * then add the constraint
5297 * f - m d >= 0
5299 * if sign < 0 or the constraint
5301 * -(f-(m-1)) + m d >= 0
5303 * if sign > 0.
5305 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5306 unsigned div, int sign)
5308 unsigned total;
5309 unsigned div_pos;
5311 if (!bmap)
5312 return -1;
5314 total = isl_basic_map_total_dim(bmap);
5315 div_pos = total - bmap->n_div + div;
5317 if (sign < 0)
5318 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5319 else
5320 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5323 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5324 __isl_take isl_basic_map *bmap)
5326 if (!bmap)
5327 goto error;
5328 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5329 bmap->n_div == 0 &&
5330 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5331 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5332 return bset_from_bmap(bmap);
5333 bmap = isl_basic_map_cow(bmap);
5334 if (!bmap)
5335 goto error;
5336 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5337 if (!bmap->dim)
5338 goto error;
5339 bmap->extra -= bmap->n_div;
5340 bmap->n_div = 0;
5341 bmap = isl_basic_map_finalize(bmap);
5342 return bset_from_bmap(bmap);
5343 error:
5344 isl_basic_map_free(bmap);
5345 return NULL;
5348 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5349 __isl_take isl_basic_set *bset)
5351 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5354 /* Replace each element in "list" by the result of applying
5355 * isl_basic_map_underlying_set to the element.
5357 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5358 __isl_take isl_basic_map_list *list)
5360 int i, n;
5362 if (!list)
5363 return NULL;
5365 n = isl_basic_map_list_n_basic_map(list);
5366 for (i = 0; i < n; ++i) {
5367 isl_basic_map *bmap;
5368 isl_basic_set *bset;
5370 bmap = isl_basic_map_list_get_basic_map(list, i);
5371 bset = isl_basic_set_underlying_set(bmap);
5372 list = isl_basic_set_list_set_basic_set(list, i, bset);
5375 return list;
5378 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5379 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5381 struct isl_basic_map *bmap;
5382 struct isl_ctx *ctx;
5383 unsigned total;
5384 int i;
5386 if (!bset || !like)
5387 goto error;
5388 ctx = bset->ctx;
5389 isl_assert(ctx, bset->n_div == 0, goto error);
5390 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5391 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5392 goto error);
5393 if (like->n_div == 0) {
5394 isl_space *space = isl_basic_map_get_space(like);
5395 isl_basic_map_free(like);
5396 return isl_basic_map_reset_space(bset, space);
5398 bset = isl_basic_set_cow(bset);
5399 if (!bset)
5400 goto error;
5401 total = bset->dim->n_out + bset->extra;
5402 bmap = bset_to_bmap(bset);
5403 isl_space_free(bmap->dim);
5404 bmap->dim = isl_space_copy(like->dim);
5405 if (!bmap->dim)
5406 goto error;
5407 bmap->n_div = like->n_div;
5408 bmap->extra += like->n_div;
5409 if (bmap->extra) {
5410 unsigned ltotal;
5411 isl_int **div;
5412 ltotal = total - bmap->extra + like->extra;
5413 if (ltotal > total)
5414 ltotal = total;
5415 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5416 bmap->extra * (1 + 1 + total));
5417 if (isl_blk_is_error(bmap->block2))
5418 goto error;
5419 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5420 if (!div)
5421 goto error;
5422 bmap->div = div;
5423 for (i = 0; i < bmap->extra; ++i)
5424 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5425 for (i = 0; i < like->n_div; ++i) {
5426 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5427 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5429 bmap = isl_basic_map_add_known_div_constraints(bmap);
5431 isl_basic_map_free(like);
5432 bmap = isl_basic_map_simplify(bmap);
5433 bmap = isl_basic_map_finalize(bmap);
5434 return bmap;
5435 error:
5436 isl_basic_map_free(like);
5437 isl_basic_set_free(bset);
5438 return NULL;
5441 struct isl_basic_set *isl_basic_set_from_underlying_set(
5442 struct isl_basic_set *bset, struct isl_basic_set *like)
5444 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5445 bset_to_bmap(like)));
5448 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5450 int i;
5452 map = isl_map_cow(map);
5453 if (!map)
5454 return NULL;
5455 map->dim = isl_space_cow(map->dim);
5456 if (!map->dim)
5457 goto error;
5459 for (i = 1; i < map->n; ++i)
5460 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5461 goto error);
5462 for (i = 0; i < map->n; ++i) {
5463 map->p[i] = bset_to_bmap(
5464 isl_basic_map_underlying_set(map->p[i]));
5465 if (!map->p[i])
5466 goto error;
5468 if (map->n == 0)
5469 map->dim = isl_space_underlying(map->dim, 0);
5470 else {
5471 isl_space_free(map->dim);
5472 map->dim = isl_space_copy(map->p[0]->dim);
5474 if (!map->dim)
5475 goto error;
5476 return set_from_map(map);
5477 error:
5478 isl_map_free(map);
5479 return NULL;
5482 /* Replace the space of "bmap" by "space".
5484 * If the space of "bmap" is identical to "space" (including the identifiers
5485 * of the input and output dimensions), then simply return the original input.
5487 __isl_give isl_basic_map *isl_basic_map_reset_space(
5488 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5490 isl_bool equal;
5491 isl_space *bmap_space;
5493 bmap_space = isl_basic_map_peek_space(bmap);
5494 equal = isl_space_is_equal(bmap_space, space);
5495 if (equal >= 0 && equal)
5496 equal = isl_space_has_equal_ids(bmap_space, space);
5497 if (equal < 0)
5498 goto error;
5499 if (equal) {
5500 isl_space_free(space);
5501 return bmap;
5503 bmap = isl_basic_map_cow(bmap);
5504 if (!bmap || !space)
5505 goto error;
5507 isl_space_free(bmap->dim);
5508 bmap->dim = space;
5510 bmap = isl_basic_map_finalize(bmap);
5512 return bmap;
5513 error:
5514 isl_basic_map_free(bmap);
5515 isl_space_free(space);
5516 return NULL;
5519 __isl_give isl_basic_set *isl_basic_set_reset_space(
5520 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5522 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5523 dim));
5526 /* Check that the total dimensions of "map" and "space" are the same.
5528 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5529 __isl_keep isl_space *space)
5531 unsigned dim1, dim2;
5533 if (!map || !space)
5534 return isl_stat_error;
5535 dim1 = isl_map_dim(map, isl_dim_all);
5536 dim2 = isl_space_dim(space, isl_dim_all);
5537 if (dim1 == dim2)
5538 return isl_stat_ok;
5539 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5540 "total dimensions do not match", return isl_stat_error);
5543 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5544 __isl_take isl_space *dim)
5546 int i;
5548 map = isl_map_cow(map);
5549 if (!map || !dim)
5550 goto error;
5552 for (i = 0; i < map->n; ++i) {
5553 map->p[i] = isl_basic_map_reset_space(map->p[i],
5554 isl_space_copy(dim));
5555 if (!map->p[i])
5556 goto error;
5558 isl_space_free(map->dim);
5559 map->dim = dim;
5561 return map;
5562 error:
5563 isl_map_free(map);
5564 isl_space_free(dim);
5565 return NULL;
5568 /* Replace the space of "map" by "space", without modifying
5569 * the dimension of "map".
5571 * If the space of "map" is identical to "space" (including the identifiers
5572 * of the input and output dimensions), then simply return the original input.
5574 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5575 __isl_take isl_space *space)
5577 isl_bool equal;
5578 isl_space *map_space;
5580 map_space = isl_map_peek_space(map);
5581 equal = isl_space_is_equal(map_space, space);
5582 if (equal >= 0 && equal)
5583 equal = isl_space_has_equal_ids(map_space, space);
5584 if (equal < 0)
5585 goto error;
5586 if (equal) {
5587 isl_space_free(space);
5588 return map;
5590 if (check_map_space_equal_total_dim(map, space) < 0)
5591 goto error;
5592 return isl_map_reset_space(map, space);
5593 error:
5594 isl_map_free(map);
5595 isl_space_free(space);
5596 return NULL;
5599 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5600 __isl_take isl_space *dim)
5602 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5605 /* Compute the parameter domain of the given basic set.
5607 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5609 isl_bool is_params;
5610 isl_space *space;
5611 unsigned n;
5613 is_params = isl_basic_set_is_params(bset);
5614 if (is_params < 0)
5615 return isl_basic_set_free(bset);
5616 if (is_params)
5617 return bset;
5619 n = isl_basic_set_dim(bset, isl_dim_set);
5620 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5621 space = isl_basic_set_get_space(bset);
5622 space = isl_space_params(space);
5623 bset = isl_basic_set_reset_space(bset, space);
5624 return bset;
5627 /* Construct a zero-dimensional basic set with the given parameter domain.
5629 __isl_give isl_basic_set *isl_basic_set_from_params(
5630 __isl_take isl_basic_set *bset)
5632 isl_space *space;
5633 space = isl_basic_set_get_space(bset);
5634 space = isl_space_set_from_params(space);
5635 bset = isl_basic_set_reset_space(bset, space);
5636 return bset;
5639 /* Compute the parameter domain of the given set.
5641 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5643 isl_space *space;
5644 unsigned n;
5646 if (isl_set_is_params(set))
5647 return set;
5649 n = isl_set_dim(set, isl_dim_set);
5650 set = isl_set_project_out(set, isl_dim_set, 0, n);
5651 space = isl_set_get_space(set);
5652 space = isl_space_params(space);
5653 set = isl_set_reset_space(set, space);
5654 return set;
5657 /* Construct a zero-dimensional set with the given parameter domain.
5659 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5661 isl_space *space;
5662 space = isl_set_get_space(set);
5663 space = isl_space_set_from_params(space);
5664 set = isl_set_reset_space(set, space);
5665 return set;
5668 /* Compute the parameter domain of the given map.
5670 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5672 isl_space *space;
5673 unsigned n;
5675 n = isl_map_dim(map, isl_dim_in);
5676 map = isl_map_project_out(map, isl_dim_in, 0, n);
5677 n = isl_map_dim(map, isl_dim_out);
5678 map = isl_map_project_out(map, isl_dim_out, 0, n);
5679 space = isl_map_get_space(map);
5680 space = isl_space_params(space);
5681 map = isl_map_reset_space(map, space);
5682 return map;
5685 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5687 isl_space *space;
5688 unsigned n_out;
5690 if (!bmap)
5691 return NULL;
5692 space = isl_space_domain(isl_basic_map_get_space(bmap));
5694 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5695 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5697 return isl_basic_map_reset_space(bmap, space);
5700 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5702 if (!bmap)
5703 return isl_bool_error;
5704 return isl_space_may_be_set(bmap->dim);
5707 /* Is this basic map actually a set?
5708 * Users should never call this function. Outside of isl,
5709 * the type should indicate whether something is a set or a map.
5711 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5713 if (!bmap)
5714 return isl_bool_error;
5715 return isl_space_is_set(bmap->dim);
5718 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5720 isl_bool is_set;
5722 is_set = isl_basic_map_is_set(bmap);
5723 if (is_set < 0)
5724 goto error;
5725 if (is_set)
5726 return bmap;
5727 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5728 error:
5729 isl_basic_map_free(bmap);
5730 return NULL;
5733 __isl_give isl_basic_map *isl_basic_map_domain_map(
5734 __isl_take isl_basic_map *bmap)
5736 int i;
5737 isl_space *dim;
5738 isl_basic_map *domain;
5739 int nparam, n_in, n_out;
5741 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5742 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5743 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5745 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5746 domain = isl_basic_map_universe(dim);
5748 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5749 bmap = isl_basic_map_apply_range(bmap, domain);
5750 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5752 for (i = 0; i < n_in; ++i)
5753 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5754 isl_dim_out, i);
5756 bmap = isl_basic_map_gauss(bmap, NULL);
5757 return isl_basic_map_finalize(bmap);
5760 __isl_give isl_basic_map *isl_basic_map_range_map(
5761 __isl_take isl_basic_map *bmap)
5763 int i;
5764 isl_space *dim;
5765 isl_basic_map *range;
5766 int nparam, n_in, n_out;
5768 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5769 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5770 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5772 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5773 range = isl_basic_map_universe(dim);
5775 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5776 bmap = isl_basic_map_apply_range(bmap, range);
5777 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5779 for (i = 0; i < n_out; ++i)
5780 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5781 isl_dim_out, i);
5783 bmap = isl_basic_map_gauss(bmap, NULL);
5784 return isl_basic_map_finalize(bmap);
5787 int isl_map_may_be_set(__isl_keep isl_map *map)
5789 if (!map)
5790 return -1;
5791 return isl_space_may_be_set(map->dim);
5794 /* Is this map actually a set?
5795 * Users should never call this function. Outside of isl,
5796 * the type should indicate whether something is a set or a map.
5798 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5800 if (!map)
5801 return isl_bool_error;
5802 return isl_space_is_set(map->dim);
5805 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5807 int i;
5808 isl_bool is_set;
5809 struct isl_set *set;
5811 is_set = isl_map_is_set(map);
5812 if (is_set < 0)
5813 goto error;
5814 if (is_set)
5815 return set_from_map(map);
5817 map = isl_map_cow(map);
5818 if (!map)
5819 goto error;
5821 set = set_from_map(map);
5822 set->dim = isl_space_range(set->dim);
5823 if (!set->dim)
5824 goto error;
5825 for (i = 0; i < map->n; ++i) {
5826 set->p[i] = isl_basic_map_range(map->p[i]);
5827 if (!set->p[i])
5828 goto error;
5830 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5831 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5832 return set;
5833 error:
5834 isl_map_free(map);
5835 return NULL;
5838 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5840 int i;
5842 map = isl_map_cow(map);
5843 if (!map)
5844 return NULL;
5846 map->dim = isl_space_domain_map(map->dim);
5847 if (!map->dim)
5848 goto error;
5849 for (i = 0; i < map->n; ++i) {
5850 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5851 if (!map->p[i])
5852 goto error;
5854 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5855 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5856 return map;
5857 error:
5858 isl_map_free(map);
5859 return NULL;
5862 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5864 int i;
5865 isl_space *range_dim;
5867 map = isl_map_cow(map);
5868 if (!map)
5869 return NULL;
5871 range_dim = isl_space_range(isl_map_get_space(map));
5872 range_dim = isl_space_from_range(range_dim);
5873 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5874 map->dim = isl_space_join(map->dim, range_dim);
5875 if (!map->dim)
5876 goto error;
5877 for (i = 0; i < map->n; ++i) {
5878 map->p[i] = isl_basic_map_range_map(map->p[i]);
5879 if (!map->p[i])
5880 goto error;
5882 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5883 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5884 return map;
5885 error:
5886 isl_map_free(map);
5887 return NULL;
5890 /* Given a wrapped map of the form A[B -> C],
5891 * return the map A[B -> C] -> B.
5893 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5895 isl_id *id;
5896 isl_map *map;
5898 if (!set)
5899 return NULL;
5900 if (!isl_set_has_tuple_id(set))
5901 return isl_map_domain_map(isl_set_unwrap(set));
5903 id = isl_set_get_tuple_id(set);
5904 map = isl_map_domain_map(isl_set_unwrap(set));
5905 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5907 return map;
5910 __isl_give isl_basic_map *isl_basic_map_from_domain(
5911 __isl_take isl_basic_set *bset)
5913 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5916 __isl_give isl_basic_map *isl_basic_map_from_range(
5917 __isl_take isl_basic_set *bset)
5919 isl_space *space;
5920 space = isl_basic_set_get_space(bset);
5921 space = isl_space_from_range(space);
5922 bset = isl_basic_set_reset_space(bset, space);
5923 return bset_to_bmap(bset);
5926 /* Create a relation with the given set as range.
5927 * The domain of the created relation is a zero-dimensional
5928 * flat anonymous space.
5930 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5932 isl_space *space;
5933 space = isl_set_get_space(set);
5934 space = isl_space_from_range(space);
5935 set = isl_set_reset_space(set, space);
5936 return set_to_map(set);
5939 /* Create a relation with the given set as domain.
5940 * The range of the created relation is a zero-dimensional
5941 * flat anonymous space.
5943 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5945 return isl_map_reverse(isl_map_from_range(set));
5948 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5949 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5951 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5954 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5955 __isl_take isl_set *range)
5957 return isl_map_apply_range(isl_map_reverse(domain), range);
5960 /* Return a newly allocated isl_map with given space and flags and
5961 * room for "n" basic maps.
5962 * Make sure that all cached information is cleared.
5964 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5965 unsigned flags)
5967 struct isl_map *map;
5969 if (!space)
5970 return NULL;
5971 if (n < 0)
5972 isl_die(space->ctx, isl_error_internal,
5973 "negative number of basic maps", goto error);
5974 map = isl_calloc(space->ctx, struct isl_map,
5975 sizeof(struct isl_map) +
5976 (n - 1) * sizeof(struct isl_basic_map *));
5977 if (!map)
5978 goto error;
5980 map->ctx = space->ctx;
5981 isl_ctx_ref(map->ctx);
5982 map->ref = 1;
5983 map->size = n;
5984 map->n = 0;
5985 map->dim = space;
5986 map->flags = flags;
5987 return map;
5988 error:
5989 isl_space_free(space);
5990 return NULL;
5993 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5995 struct isl_basic_map *bmap;
5996 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5997 bmap = isl_basic_map_set_to_empty(bmap);
5998 return bmap;
6001 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6003 struct isl_basic_set *bset;
6004 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6005 bset = isl_basic_set_set_to_empty(bset);
6006 return bset;
6009 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6011 struct isl_basic_map *bmap;
6012 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6013 bmap = isl_basic_map_finalize(bmap);
6014 return bmap;
6017 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6019 struct isl_basic_set *bset;
6020 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6021 bset = isl_basic_set_finalize(bset);
6022 return bset;
6025 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
6027 int i;
6028 unsigned total = isl_space_dim(dim, isl_dim_all);
6029 isl_basic_map *bmap;
6031 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
6032 for (i = 0; i < total; ++i) {
6033 int k = isl_basic_map_alloc_inequality(bmap);
6034 if (k < 0)
6035 goto error;
6036 isl_seq_clr(bmap->ineq[k], 1 + total);
6037 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6039 return bmap;
6040 error:
6041 isl_basic_map_free(bmap);
6042 return NULL;
6045 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
6047 return isl_basic_map_nat_universe(dim);
6050 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
6052 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
6055 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
6057 return isl_map_nat_universe(dim);
6060 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6062 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6065 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6067 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6070 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6072 struct isl_map *map;
6073 if (!space)
6074 return NULL;
6075 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6076 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6077 return map;
6080 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6082 struct isl_set *set;
6083 if (!space)
6084 return NULL;
6085 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6086 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6087 return set;
6090 struct isl_map *isl_map_dup(struct isl_map *map)
6092 int i;
6093 struct isl_map *dup;
6095 if (!map)
6096 return NULL;
6097 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6098 for (i = 0; i < map->n; ++i)
6099 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6100 return dup;
6103 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6104 __isl_take isl_basic_map *bmap)
6106 if (!bmap || !map)
6107 goto error;
6108 if (isl_basic_map_plain_is_empty(bmap)) {
6109 isl_basic_map_free(bmap);
6110 return map;
6112 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6113 isl_assert(map->ctx, map->n < map->size, goto error);
6114 map->p[map->n] = bmap;
6115 map->n++;
6116 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6117 return map;
6118 error:
6119 if (map)
6120 isl_map_free(map);
6121 if (bmap)
6122 isl_basic_map_free(bmap);
6123 return NULL;
6126 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6128 int i;
6130 if (!map)
6131 return NULL;
6133 if (--map->ref > 0)
6134 return NULL;
6136 clear_caches(map);
6137 isl_ctx_deref(map->ctx);
6138 for (i = 0; i < map->n; ++i)
6139 isl_basic_map_free(map->p[i]);
6140 isl_space_free(map->dim);
6141 free(map);
6143 return NULL;
6146 static struct isl_basic_map *isl_basic_map_fix_pos_si(
6147 struct isl_basic_map *bmap, unsigned pos, int value)
6149 int j;
6151 bmap = isl_basic_map_cow(bmap);
6152 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6153 j = isl_basic_map_alloc_equality(bmap);
6154 if (j < 0)
6155 goto error;
6156 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6157 isl_int_set_si(bmap->eq[j][pos], -1);
6158 isl_int_set_si(bmap->eq[j][0], value);
6159 bmap = isl_basic_map_simplify(bmap);
6160 return isl_basic_map_finalize(bmap);
6161 error:
6162 isl_basic_map_free(bmap);
6163 return NULL;
6166 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6167 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6169 int j;
6171 bmap = isl_basic_map_cow(bmap);
6172 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6173 j = isl_basic_map_alloc_equality(bmap);
6174 if (j < 0)
6175 goto error;
6176 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6177 isl_int_set_si(bmap->eq[j][pos], -1);
6178 isl_int_set(bmap->eq[j][0], value);
6179 bmap = isl_basic_map_simplify(bmap);
6180 return isl_basic_map_finalize(bmap);
6181 error:
6182 isl_basic_map_free(bmap);
6183 return NULL;
6186 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6187 enum isl_dim_type type, unsigned pos, int value)
6189 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6190 return isl_basic_map_free(bmap);
6191 return isl_basic_map_fix_pos_si(bmap,
6192 isl_basic_map_offset(bmap, type) + pos, value);
6195 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6196 enum isl_dim_type type, unsigned pos, isl_int value)
6198 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6199 return isl_basic_map_free(bmap);
6200 return isl_basic_map_fix_pos(bmap,
6201 isl_basic_map_offset(bmap, type) + pos, value);
6204 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6205 * to be equal to "v".
6207 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6208 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6210 if (!bmap || !v)
6211 goto error;
6212 if (!isl_val_is_int(v))
6213 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6214 "expecting integer value", goto error);
6215 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6216 goto error;
6217 pos += isl_basic_map_offset(bmap, type);
6218 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6219 isl_val_free(v);
6220 return bmap;
6221 error:
6222 isl_basic_map_free(bmap);
6223 isl_val_free(v);
6224 return NULL;
6227 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6228 * to be equal to "v".
6230 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6231 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6233 return isl_basic_map_fix_val(bset, type, pos, v);
6236 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6237 enum isl_dim_type type, unsigned pos, int value)
6239 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6240 type, pos, value));
6243 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6244 enum isl_dim_type type, unsigned pos, isl_int value)
6246 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6247 type, pos, value));
6250 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6251 unsigned input, int value)
6253 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6256 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6257 unsigned dim, int value)
6259 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6260 isl_dim_set, dim, value));
6263 static int remove_if_empty(__isl_keep isl_map *map, int i)
6265 int empty = isl_basic_map_plain_is_empty(map->p[i]);
6267 if (empty < 0)
6268 return -1;
6269 if (!empty)
6270 return 0;
6272 isl_basic_map_free(map->p[i]);
6273 if (i != map->n - 1) {
6274 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6275 map->p[i] = map->p[map->n - 1];
6277 map->n--;
6279 return 0;
6282 /* Perform "fn" on each basic map of "map", where we may not be holding
6283 * the only reference to "map".
6284 * In particular, "fn" should be a semantics preserving operation
6285 * that we want to apply to all copies of "map". We therefore need
6286 * to be careful not to modify "map" in a way that breaks "map"
6287 * in case anything goes wrong.
6289 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6290 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6292 struct isl_basic_map *bmap;
6293 int i;
6295 if (!map)
6296 return NULL;
6298 for (i = map->n - 1; i >= 0; --i) {
6299 bmap = isl_basic_map_copy(map->p[i]);
6300 bmap = fn(bmap);
6301 if (!bmap)
6302 goto error;
6303 isl_basic_map_free(map->p[i]);
6304 map->p[i] = bmap;
6305 if (remove_if_empty(map, i) < 0)
6306 goto error;
6309 return map;
6310 error:
6311 isl_map_free(map);
6312 return NULL;
6315 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6316 enum isl_dim_type type, unsigned pos, int value)
6318 int i;
6320 map = isl_map_cow(map);
6321 if (!map)
6322 return NULL;
6324 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6325 for (i = map->n - 1; i >= 0; --i) {
6326 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6327 if (remove_if_empty(map, i) < 0)
6328 goto error;
6330 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6331 return map;
6332 error:
6333 isl_map_free(map);
6334 return NULL;
6337 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6338 enum isl_dim_type type, unsigned pos, int value)
6340 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6343 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6344 enum isl_dim_type type, unsigned pos, isl_int value)
6346 int i;
6348 map = isl_map_cow(map);
6349 if (!map)
6350 return NULL;
6352 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6353 for (i = 0; i < map->n; ++i) {
6354 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6355 if (!map->p[i])
6356 goto error;
6358 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6359 return map;
6360 error:
6361 isl_map_free(map);
6362 return NULL;
6365 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6366 enum isl_dim_type type, unsigned pos, isl_int value)
6368 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6371 /* Fix the value of the variable at position "pos" of type "type" of "map"
6372 * to be equal to "v".
6374 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6375 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6377 int i;
6379 map = isl_map_cow(map);
6380 if (!map || !v)
6381 goto error;
6383 if (!isl_val_is_int(v))
6384 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6385 "expecting integer value", goto error);
6386 if (pos >= isl_map_dim(map, type))
6387 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6388 "index out of bounds", goto error);
6389 for (i = map->n - 1; i >= 0; --i) {
6390 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6391 isl_val_copy(v));
6392 if (remove_if_empty(map, i) < 0)
6393 goto error;
6395 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6396 isl_val_free(v);
6397 return map;
6398 error:
6399 isl_map_free(map);
6400 isl_val_free(v);
6401 return NULL;
6404 /* Fix the value of the variable at position "pos" of type "type" of "set"
6405 * to be equal to "v".
6407 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6408 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6410 return isl_map_fix_val(set, type, pos, v);
6413 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6414 unsigned input, int value)
6416 return isl_map_fix_si(map, isl_dim_in, input, value);
6419 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6421 return set_from_map(isl_map_fix_si(set_to_map(set),
6422 isl_dim_set, dim, value));
6425 static __isl_give isl_basic_map *basic_map_bound_si(
6426 __isl_take isl_basic_map *bmap,
6427 enum isl_dim_type type, unsigned pos, int value, int upper)
6429 int j;
6431 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6432 return isl_basic_map_free(bmap);
6433 pos += isl_basic_map_offset(bmap, type);
6434 bmap = isl_basic_map_cow(bmap);
6435 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6436 j = isl_basic_map_alloc_inequality(bmap);
6437 if (j < 0)
6438 goto error;
6439 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6440 if (upper) {
6441 isl_int_set_si(bmap->ineq[j][pos], -1);
6442 isl_int_set_si(bmap->ineq[j][0], value);
6443 } else {
6444 isl_int_set_si(bmap->ineq[j][pos], 1);
6445 isl_int_set_si(bmap->ineq[j][0], -value);
6447 bmap = isl_basic_map_simplify(bmap);
6448 return isl_basic_map_finalize(bmap);
6449 error:
6450 isl_basic_map_free(bmap);
6451 return NULL;
6454 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6455 __isl_take isl_basic_map *bmap,
6456 enum isl_dim_type type, unsigned pos, int value)
6458 return basic_map_bound_si(bmap, type, pos, value, 0);
6461 /* Constrain the values of the given dimension to be no greater than "value".
6463 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6464 __isl_take isl_basic_map *bmap,
6465 enum isl_dim_type type, unsigned pos, int value)
6467 return basic_map_bound_si(bmap, type, pos, value, 1);
6470 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6471 enum isl_dim_type type, unsigned pos, int value, int upper)
6473 int i;
6475 map = isl_map_cow(map);
6476 if (!map)
6477 return NULL;
6479 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6480 for (i = 0; i < map->n; ++i) {
6481 map->p[i] = basic_map_bound_si(map->p[i],
6482 type, pos, value, upper);
6483 if (!map->p[i])
6484 goto error;
6486 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6487 return map;
6488 error:
6489 isl_map_free(map);
6490 return NULL;
6493 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6494 enum isl_dim_type type, unsigned pos, int value)
6496 return map_bound_si(map, type, pos, value, 0);
6499 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6500 enum isl_dim_type type, unsigned pos, int value)
6502 return map_bound_si(map, type, pos, value, 1);
6505 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6506 enum isl_dim_type type, unsigned pos, int value)
6508 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6509 type, pos, value));
6512 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6513 enum isl_dim_type type, unsigned pos, int value)
6515 return isl_map_upper_bound_si(set, type, pos, value);
6518 /* Bound the given variable of "bmap" from below (or above is "upper"
6519 * is set) to "value".
6521 static __isl_give isl_basic_map *basic_map_bound(
6522 __isl_take isl_basic_map *bmap,
6523 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6525 int j;
6527 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6528 return isl_basic_map_free(bmap);
6529 pos += isl_basic_map_offset(bmap, type);
6530 bmap = isl_basic_map_cow(bmap);
6531 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6532 j = isl_basic_map_alloc_inequality(bmap);
6533 if (j < 0)
6534 goto error;
6535 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6536 if (upper) {
6537 isl_int_set_si(bmap->ineq[j][pos], -1);
6538 isl_int_set(bmap->ineq[j][0], value);
6539 } else {
6540 isl_int_set_si(bmap->ineq[j][pos], 1);
6541 isl_int_neg(bmap->ineq[j][0], value);
6543 bmap = isl_basic_map_simplify(bmap);
6544 return isl_basic_map_finalize(bmap);
6545 error:
6546 isl_basic_map_free(bmap);
6547 return NULL;
6550 /* Bound the given variable of "map" from below (or above is "upper"
6551 * is set) to "value".
6553 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6554 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6556 int i;
6558 map = isl_map_cow(map);
6559 if (!map)
6560 return NULL;
6562 if (pos >= isl_map_dim(map, type))
6563 isl_die(map->ctx, isl_error_invalid,
6564 "index out of bounds", goto error);
6565 for (i = map->n - 1; i >= 0; --i) {
6566 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6567 if (remove_if_empty(map, i) < 0)
6568 goto error;
6570 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6571 return map;
6572 error:
6573 isl_map_free(map);
6574 return NULL;
6577 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6578 enum isl_dim_type type, unsigned pos, isl_int value)
6580 return map_bound(map, type, pos, value, 0);
6583 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6584 enum isl_dim_type type, unsigned pos, isl_int value)
6586 return map_bound(map, type, pos, value, 1);
6589 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6590 enum isl_dim_type type, unsigned pos, isl_int value)
6592 return isl_map_lower_bound(set, type, pos, value);
6595 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6596 enum isl_dim_type type, unsigned pos, isl_int value)
6598 return isl_map_upper_bound(set, type, pos, value);
6601 /* Force the values of the variable at position "pos" of type "type" of "set"
6602 * to be no smaller than "value".
6604 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6605 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6607 if (!value)
6608 goto error;
6609 if (!isl_val_is_int(value))
6610 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6611 "expecting integer value", goto error);
6612 set = isl_set_lower_bound(set, type, pos, value->n);
6613 isl_val_free(value);
6614 return set;
6615 error:
6616 isl_val_free(value);
6617 isl_set_free(set);
6618 return NULL;
6621 /* Force the values of the variable at position "pos" of type "type" of "set"
6622 * to be no greater than "value".
6624 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6625 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6627 if (!value)
6628 goto error;
6629 if (!isl_val_is_int(value))
6630 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6631 "expecting integer value", goto error);
6632 set = isl_set_upper_bound(set, type, pos, value->n);
6633 isl_val_free(value);
6634 return set;
6635 error:
6636 isl_val_free(value);
6637 isl_set_free(set);
6638 return NULL;
6641 /* Bound the given variable of "bset" from below (or above is "upper"
6642 * is set) to "value".
6644 static __isl_give isl_basic_set *isl_basic_set_bound(
6645 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6646 isl_int value, int upper)
6648 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6649 type, pos, value, upper));
6652 /* Bound the given variable of "bset" from below (or above is "upper"
6653 * is set) to "value".
6655 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6656 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6657 __isl_take isl_val *value, int upper)
6659 if (!value)
6660 goto error;
6661 if (!isl_val_is_int(value))
6662 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6663 "expecting integer value", goto error);
6664 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6665 isl_val_free(value);
6666 return bset;
6667 error:
6668 isl_val_free(value);
6669 isl_basic_set_free(bset);
6670 return NULL;
6673 /* Bound the given variable of "bset" from below to "value".
6675 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6676 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6677 __isl_take isl_val *value)
6679 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6682 /* Bound the given variable of "bset" from above to "value".
6684 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6685 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6686 __isl_take isl_val *value)
6688 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6691 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6693 int i;
6695 map = isl_map_cow(map);
6696 if (!map)
6697 return NULL;
6699 map->dim = isl_space_reverse(map->dim);
6700 if (!map->dim)
6701 goto error;
6702 for (i = 0; i < map->n; ++i) {
6703 map->p[i] = isl_basic_map_reverse(map->p[i]);
6704 if (!map->p[i])
6705 goto error;
6707 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6708 return map;
6709 error:
6710 isl_map_free(map);
6711 return NULL;
6714 #undef TYPE
6715 #define TYPE isl_pw_multi_aff
6716 #undef SUFFIX
6717 #define SUFFIX _pw_multi_aff
6718 #undef EMPTY
6719 #define EMPTY isl_pw_multi_aff_empty
6720 #undef ADD
6721 #define ADD isl_pw_multi_aff_union_add
6722 #include "isl_map_lexopt_templ.c"
6724 /* Given a map "map", compute the lexicographically minimal
6725 * (or maximal) image element for each domain element in dom,
6726 * in the form of an isl_pw_multi_aff.
6727 * If "empty" is not NULL, then set *empty to those elements in dom that
6728 * do not have an image element.
6729 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6730 * should be computed over the domain of "map". "empty" is also NULL
6731 * in this case.
6733 * We first compute the lexicographically minimal or maximal element
6734 * in the first basic map. This results in a partial solution "res"
6735 * and a subset "todo" of dom that still need to be handled.
6736 * We then consider each of the remaining maps in "map" and successively
6737 * update both "res" and "todo".
6738 * If "empty" is NULL, then the todo sets are not needed and therefore
6739 * also not computed.
6741 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6742 __isl_take isl_map *map, __isl_take isl_set *dom,
6743 __isl_give isl_set **empty, unsigned flags)
6745 int i;
6746 int full;
6747 isl_pw_multi_aff *res;
6748 isl_set *todo;
6750 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6751 if (!map || (!full && !dom))
6752 goto error;
6754 if (isl_map_plain_is_empty(map)) {
6755 if (empty)
6756 *empty = dom;
6757 else
6758 isl_set_free(dom);
6759 return isl_pw_multi_aff_from_map(map);
6762 res = basic_map_partial_lexopt_pw_multi_aff(
6763 isl_basic_map_copy(map->p[0]),
6764 isl_set_copy(dom), empty, flags);
6766 if (empty)
6767 todo = *empty;
6768 for (i = 1; i < map->n; ++i) {
6769 isl_pw_multi_aff *res_i;
6771 res_i = basic_map_partial_lexopt_pw_multi_aff(
6772 isl_basic_map_copy(map->p[i]),
6773 isl_set_copy(dom), empty, flags);
6775 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6776 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6777 else
6778 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6780 if (empty)
6781 todo = isl_set_intersect(todo, *empty);
6784 isl_set_free(dom);
6785 isl_map_free(map);
6787 if (empty)
6788 *empty = todo;
6790 return res;
6791 error:
6792 if (empty)
6793 *empty = NULL;
6794 isl_set_free(dom);
6795 isl_map_free(map);
6796 return NULL;
6799 #undef TYPE
6800 #define TYPE isl_map
6801 #undef SUFFIX
6802 #define SUFFIX
6803 #undef EMPTY
6804 #define EMPTY isl_map_empty
6805 #undef ADD
6806 #define ADD isl_map_union_disjoint
6807 #include "isl_map_lexopt_templ.c"
6809 /* Given a map "map", compute the lexicographically minimal
6810 * (or maximal) image element for each domain element in "dom",
6811 * in the form of an isl_map.
6812 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6813 * do not have an image element.
6814 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6815 * should be computed over the domain of "map". "empty" is also NULL
6816 * in this case.
6818 * If the input consists of more than one disjunct, then first
6819 * compute the desired result in the form of an isl_pw_multi_aff and
6820 * then convert that into an isl_map.
6822 * This function used to have an explicit implementation in terms
6823 * of isl_maps, but it would continually intersect the domains of
6824 * partial results with the complement of the domain of the next
6825 * partial solution, potentially leading to an explosion in the number
6826 * of disjuncts if there are several disjuncts in the input.
6827 * An even earlier implementation of this function would look for
6828 * better results in the domain of the partial result and for extra
6829 * results in the complement of this domain, which would lead to
6830 * even more splintering.
6832 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6833 __isl_take isl_map *map, __isl_take isl_set *dom,
6834 __isl_give isl_set **empty, unsigned flags)
6836 int full;
6837 struct isl_map *res;
6838 isl_pw_multi_aff *pma;
6840 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6841 if (!map || (!full && !dom))
6842 goto error;
6844 if (isl_map_plain_is_empty(map)) {
6845 if (empty)
6846 *empty = dom;
6847 else
6848 isl_set_free(dom);
6849 return map;
6852 if (map->n == 1) {
6853 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6854 dom, empty, flags);
6855 isl_map_free(map);
6856 return res;
6859 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6860 flags);
6861 return isl_map_from_pw_multi_aff(pma);
6862 error:
6863 if (empty)
6864 *empty = NULL;
6865 isl_set_free(dom);
6866 isl_map_free(map);
6867 return NULL;
6870 __isl_give isl_map *isl_map_partial_lexmax(
6871 __isl_take isl_map *map, __isl_take isl_set *dom,
6872 __isl_give isl_set **empty)
6874 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6877 __isl_give isl_map *isl_map_partial_lexmin(
6878 __isl_take isl_map *map, __isl_take isl_set *dom,
6879 __isl_give isl_set **empty)
6881 return isl_map_partial_lexopt(map, dom, empty, 0);
6884 __isl_give isl_set *isl_set_partial_lexmin(
6885 __isl_take isl_set *set, __isl_take isl_set *dom,
6886 __isl_give isl_set **empty)
6888 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6889 dom, empty));
6892 __isl_give isl_set *isl_set_partial_lexmax(
6893 __isl_take isl_set *set, __isl_take isl_set *dom,
6894 __isl_give isl_set **empty)
6896 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6897 dom, empty));
6900 /* Compute the lexicographic minimum (or maximum if "flags" includes
6901 * ISL_OPT_MAX) of "bset" over its parametric domain.
6903 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6904 unsigned flags)
6906 return isl_basic_map_lexopt(bset, flags);
6909 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6911 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6914 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6916 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6919 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6921 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6924 /* Compute the lexicographic minimum of "bset" over its parametric domain
6925 * for the purpose of quantifier elimination.
6926 * That is, find an explicit representation for all the existentially
6927 * quantified variables in "bset" by computing their lexicographic
6928 * minimum.
6930 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6931 __isl_take isl_basic_set *bset)
6933 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6936 /* Given a basic map with one output dimension, compute the minimum or
6937 * maximum of that dimension as an isl_pw_aff.
6939 * Compute the optimum as a lexicographic optimum over the single
6940 * output dimension and extract the single isl_pw_aff from the result.
6942 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6943 int max)
6945 isl_pw_multi_aff *pma;
6946 isl_pw_aff *pwaff;
6948 bmap = isl_basic_map_copy(bmap);
6949 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6950 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6951 isl_pw_multi_aff_free(pma);
6953 return pwaff;
6956 /* Compute the minimum or maximum of the given output dimension
6957 * as a function of the parameters and the input dimensions,
6958 * but independently of the other output dimensions.
6960 * We first project out the other output dimension and then compute
6961 * the "lexicographic" maximum in each basic map, combining the results
6962 * using isl_pw_aff_union_max.
6964 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6965 int max)
6967 int i;
6968 isl_pw_aff *pwaff;
6969 unsigned n_out;
6971 n_out = isl_map_dim(map, isl_dim_out);
6972 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6973 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6974 if (!map)
6975 return NULL;
6977 if (map->n == 0) {
6978 isl_space *dim = isl_map_get_space(map);
6979 isl_map_free(map);
6980 return isl_pw_aff_empty(dim);
6983 pwaff = basic_map_dim_opt(map->p[0], max);
6984 for (i = 1; i < map->n; ++i) {
6985 isl_pw_aff *pwaff_i;
6987 pwaff_i = basic_map_dim_opt(map->p[i], max);
6988 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6991 isl_map_free(map);
6993 return pwaff;
6996 /* Compute the minimum of the given output dimension as a function of the
6997 * parameters and input dimensions, but independently of
6998 * the other output dimensions.
7000 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7002 return map_dim_opt(map, pos, 0);
7005 /* Compute the maximum of the given output dimension as a function of the
7006 * parameters and input dimensions, but independently of
7007 * the other output dimensions.
7009 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7011 return map_dim_opt(map, pos, 1);
7014 /* Compute the minimum or maximum of the given set dimension
7015 * as a function of the parameters,
7016 * but independently of the other set dimensions.
7018 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7019 int max)
7021 return map_dim_opt(set, pos, max);
7024 /* Compute the maximum of the given set dimension as a function of the
7025 * parameters, but independently of the other set dimensions.
7027 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7029 return set_dim_opt(set, pos, 1);
7032 /* Compute the minimum of the given set dimension as a function of the
7033 * parameters, but independently of the other set dimensions.
7035 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7037 return set_dim_opt(set, pos, 0);
7040 /* Apply a preimage specified by "mat" on the parameters of "bset".
7041 * bset is assumed to have only parameters and divs.
7043 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7044 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7046 unsigned nparam;
7048 if (!bset || !mat)
7049 goto error;
7051 bset->dim = isl_space_cow(bset->dim);
7052 if (!bset->dim)
7053 goto error;
7055 nparam = isl_basic_set_dim(bset, isl_dim_param);
7057 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7059 bset->dim->nparam = 0;
7060 bset->dim->n_out = nparam;
7061 bset = isl_basic_set_preimage(bset, mat);
7062 if (bset) {
7063 bset->dim->nparam = bset->dim->n_out;
7064 bset->dim->n_out = 0;
7066 return bset;
7067 error:
7068 isl_mat_free(mat);
7069 isl_basic_set_free(bset);
7070 return NULL;
7073 /* Apply a preimage specified by "mat" on the parameters of "set".
7074 * set is assumed to have only parameters and divs.
7076 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7077 __isl_take isl_mat *mat)
7079 isl_space *space;
7080 unsigned nparam;
7082 if (!set || !mat)
7083 goto error;
7085 nparam = isl_set_dim(set, isl_dim_param);
7087 if (mat->n_row != 1 + nparam)
7088 isl_die(isl_set_get_ctx(set), isl_error_internal,
7089 "unexpected number of rows", goto error);
7091 space = isl_set_get_space(set);
7092 space = isl_space_move_dims(space, isl_dim_set, 0,
7093 isl_dim_param, 0, nparam);
7094 set = isl_set_reset_space(set, space);
7095 set = isl_set_preimage(set, mat);
7096 nparam = isl_set_dim(set, isl_dim_out);
7097 space = isl_set_get_space(set);
7098 space = isl_space_move_dims(space, isl_dim_param, 0,
7099 isl_dim_out, 0, nparam);
7100 set = isl_set_reset_space(set, space);
7101 return set;
7102 error:
7103 isl_mat_free(mat);
7104 isl_set_free(set);
7105 return NULL;
7108 /* Intersect the basic set "bset" with the affine space specified by the
7109 * equalities in "eq".
7111 static __isl_give isl_basic_set *basic_set_append_equalities(
7112 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7114 int i, k;
7115 unsigned len;
7117 if (!bset || !eq)
7118 goto error;
7120 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7121 eq->n_row, 0);
7122 if (!bset)
7123 goto error;
7125 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7126 for (i = 0; i < eq->n_row; ++i) {
7127 k = isl_basic_set_alloc_equality(bset);
7128 if (k < 0)
7129 goto error;
7130 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7131 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7133 isl_mat_free(eq);
7135 bset = isl_basic_set_gauss(bset, NULL);
7136 bset = isl_basic_set_finalize(bset);
7138 return bset;
7139 error:
7140 isl_mat_free(eq);
7141 isl_basic_set_free(bset);
7142 return NULL;
7145 /* Intersect the set "set" with the affine space specified by the
7146 * equalities in "eq".
7148 static struct isl_set *set_append_equalities(struct isl_set *set,
7149 struct isl_mat *eq)
7151 int i;
7153 if (!set || !eq)
7154 goto error;
7156 for (i = 0; i < set->n; ++i) {
7157 set->p[i] = basic_set_append_equalities(set->p[i],
7158 isl_mat_copy(eq));
7159 if (!set->p[i])
7160 goto error;
7162 isl_mat_free(eq);
7163 return set;
7164 error:
7165 isl_mat_free(eq);
7166 isl_set_free(set);
7167 return NULL;
7170 /* Given a basic set "bset" that only involves parameters and existentially
7171 * quantified variables, return the index of the first equality
7172 * that only involves parameters. If there is no such equality then
7173 * return bset->n_eq.
7175 * This function assumes that isl_basic_set_gauss has been called on "bset".
7177 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7179 int i, j;
7180 unsigned nparam, n_div;
7182 if (!bset)
7183 return -1;
7185 nparam = isl_basic_set_dim(bset, isl_dim_param);
7186 n_div = isl_basic_set_dim(bset, isl_dim_div);
7188 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7189 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7190 ++i;
7193 return i;
7196 /* Compute an explicit representation for the existentially quantified
7197 * variables in "bset" by computing the "minimal value" of the set
7198 * variables. Since there are no set variables, the computation of
7199 * the minimal value essentially computes an explicit representation
7200 * of the non-empty part(s) of "bset".
7202 * The input only involves parameters and existentially quantified variables.
7203 * All equalities among parameters have been removed.
7205 * Since the existentially quantified variables in the result are in general
7206 * going to be different from those in the input, we first replace
7207 * them by the minimal number of variables based on their equalities.
7208 * This should simplify the parametric integer programming.
7210 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7212 isl_morph *morph1, *morph2;
7213 isl_set *set;
7214 unsigned n;
7216 if (!bset)
7217 return NULL;
7218 if (bset->n_eq == 0)
7219 return isl_basic_set_lexmin_compute_divs(bset);
7221 morph1 = isl_basic_set_parameter_compression(bset);
7222 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7223 bset = isl_basic_set_lift(bset);
7224 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7225 bset = isl_morph_basic_set(morph2, bset);
7226 n = isl_basic_set_dim(bset, isl_dim_set);
7227 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7229 set = isl_basic_set_lexmin_compute_divs(bset);
7231 set = isl_morph_set(isl_morph_inverse(morph1), set);
7233 return set;
7236 /* Project the given basic set onto its parameter domain, possibly introducing
7237 * new, explicit, existential variables in the constraints.
7238 * The input has parameters and (possibly implicit) existential variables.
7239 * The output has the same parameters, but only
7240 * explicit existentially quantified variables.
7242 * The actual projection is performed by pip, but pip doesn't seem
7243 * to like equalities very much, so we first remove the equalities
7244 * among the parameters by performing a variable compression on
7245 * the parameters. Afterward, an inverse transformation is performed
7246 * and the equalities among the parameters are inserted back in.
7248 * The variable compression on the parameters may uncover additional
7249 * equalities that were only implicit before. We therefore check
7250 * if there are any new parameter equalities in the result and
7251 * if so recurse. The removal of parameter equalities is required
7252 * for the parameter compression performed by base_compute_divs.
7254 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7256 int i;
7257 struct isl_mat *eq;
7258 struct isl_mat *T, *T2;
7259 struct isl_set *set;
7260 unsigned nparam;
7262 bset = isl_basic_set_cow(bset);
7263 if (!bset)
7264 return NULL;
7266 if (bset->n_eq == 0)
7267 return base_compute_divs(bset);
7269 bset = isl_basic_set_gauss(bset, NULL);
7270 if (!bset)
7271 return NULL;
7272 if (isl_basic_set_plain_is_empty(bset))
7273 return isl_set_from_basic_set(bset);
7275 i = first_parameter_equality(bset);
7276 if (i == bset->n_eq)
7277 return base_compute_divs(bset);
7279 nparam = isl_basic_set_dim(bset, isl_dim_param);
7280 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7281 0, 1 + nparam);
7282 eq = isl_mat_cow(eq);
7283 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7284 if (T && T->n_col == 0) {
7285 isl_mat_free(T);
7286 isl_mat_free(T2);
7287 isl_mat_free(eq);
7288 bset = isl_basic_set_set_to_empty(bset);
7289 return isl_set_from_basic_set(bset);
7291 bset = basic_set_parameter_preimage(bset, T);
7293 i = first_parameter_equality(bset);
7294 if (!bset)
7295 set = NULL;
7296 else if (i == bset->n_eq)
7297 set = base_compute_divs(bset);
7298 else
7299 set = parameter_compute_divs(bset);
7300 set = set_parameter_preimage(set, T2);
7301 set = set_append_equalities(set, eq);
7302 return set;
7305 /* Insert the divs from "ls" before those of "bmap".
7307 * The number of columns is not changed, which means that the last
7308 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7309 * The caller is responsible for removing the same number of dimensions
7310 * from the space of "bmap".
7312 static __isl_give isl_basic_map *insert_divs_from_local_space(
7313 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7315 int i;
7316 int n_div;
7317 int old_n_div;
7319 n_div = isl_local_space_dim(ls, isl_dim_div);
7320 if (n_div == 0)
7321 return bmap;
7323 old_n_div = bmap->n_div;
7324 bmap = insert_div_rows(bmap, n_div);
7325 if (!bmap)
7326 return NULL;
7328 for (i = 0; i < n_div; ++i) {
7329 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7330 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7333 return bmap;
7336 /* Replace the space of "bmap" by the space and divs of "ls".
7338 * If "ls" has any divs, then we simplify the result since we may
7339 * have discovered some additional equalities that could simplify
7340 * the div expressions.
7342 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7343 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7345 int n_div;
7347 bmap = isl_basic_map_cow(bmap);
7348 if (!bmap || !ls)
7349 goto error;
7351 n_div = isl_local_space_dim(ls, isl_dim_div);
7352 bmap = insert_divs_from_local_space(bmap, ls);
7353 if (!bmap)
7354 goto error;
7356 isl_space_free(bmap->dim);
7357 bmap->dim = isl_local_space_get_space(ls);
7358 if (!bmap->dim)
7359 goto error;
7361 isl_local_space_free(ls);
7362 if (n_div > 0)
7363 bmap = isl_basic_map_simplify(bmap);
7364 bmap = isl_basic_map_finalize(bmap);
7365 return bmap;
7366 error:
7367 isl_basic_map_free(bmap);
7368 isl_local_space_free(ls);
7369 return NULL;
7372 /* Replace the space of "map" by the space and divs of "ls".
7374 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7375 __isl_take isl_local_space *ls)
7377 int i;
7379 map = isl_map_cow(map);
7380 if (!map || !ls)
7381 goto error;
7383 for (i = 0; i < map->n; ++i) {
7384 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7385 isl_local_space_copy(ls));
7386 if (!map->p[i])
7387 goto error;
7389 isl_space_free(map->dim);
7390 map->dim = isl_local_space_get_space(ls);
7391 if (!map->dim)
7392 goto error;
7394 isl_local_space_free(ls);
7395 return map;
7396 error:
7397 isl_local_space_free(ls);
7398 isl_map_free(map);
7399 return NULL;
7402 /* Compute an explicit representation for the existentially
7403 * quantified variables for which do not know any explicit representation yet.
7405 * We first sort the existentially quantified variables so that the
7406 * existentially quantified variables for which we already have an explicit
7407 * representation are placed before those for which we do not.
7408 * The input dimensions, the output dimensions and the existentially
7409 * quantified variables for which we already have an explicit
7410 * representation are then turned into parameters.
7411 * compute_divs returns a map with the same parameters and
7412 * no input or output dimensions and the dimension specification
7413 * is reset to that of the input, including the existentially quantified
7414 * variables for which we already had an explicit representation.
7416 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7418 struct isl_basic_set *bset;
7419 struct isl_set *set;
7420 struct isl_map *map;
7421 isl_space *dim;
7422 isl_local_space *ls;
7423 unsigned nparam;
7424 unsigned n_in;
7425 unsigned n_out;
7426 int n_known;
7427 int i;
7429 bmap = isl_basic_map_sort_divs(bmap);
7430 bmap = isl_basic_map_cow(bmap);
7431 if (!bmap)
7432 return NULL;
7434 n_known = isl_basic_map_first_unknown_div(bmap);
7435 if (n_known < 0)
7436 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7438 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7439 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7440 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7441 dim = isl_space_set_alloc(bmap->ctx,
7442 nparam + n_in + n_out + n_known, 0);
7443 if (!dim)
7444 goto error;
7446 ls = isl_basic_map_get_local_space(bmap);
7447 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7448 n_known, bmap->n_div - n_known);
7449 if (n_known > 0) {
7450 for (i = n_known; i < bmap->n_div; ++i)
7451 swap_div(bmap, i - n_known, i);
7452 bmap->n_div -= n_known;
7453 bmap->extra -= n_known;
7455 bmap = isl_basic_map_reset_space(bmap, dim);
7456 bset = bset_from_bmap(bmap);
7458 set = parameter_compute_divs(bset);
7459 map = set_to_map(set);
7460 map = replace_space_by_local_space(map, ls);
7462 return map;
7463 error:
7464 isl_basic_map_free(bmap);
7465 return NULL;
7468 /* Remove the explicit representation of local variable "div",
7469 * if there is any.
7471 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7472 __isl_take isl_basic_map *bmap, int div)
7474 isl_bool unknown;
7476 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7477 if (unknown < 0)
7478 return isl_basic_map_free(bmap);
7479 if (unknown)
7480 return bmap;
7482 bmap = isl_basic_map_cow(bmap);
7483 if (!bmap)
7484 return NULL;
7485 isl_int_set_si(bmap->div[div][0], 0);
7486 return bmap;
7489 /* Is local variable "div" of "bmap" marked as not having an explicit
7490 * representation?
7491 * Note that even if "div" is not marked in this way and therefore
7492 * has an explicit representation, this representation may still
7493 * depend (indirectly) on other local variables that do not
7494 * have an explicit representation.
7496 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7497 int div)
7499 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7500 return isl_bool_error;
7501 return isl_int_is_zero(bmap->div[div][0]);
7504 /* Return the position of the first local variable that does not
7505 * have an explicit representation.
7506 * Return the total number of local variables if they all have
7507 * an explicit representation.
7508 * Return -1 on error.
7510 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7512 int i;
7514 if (!bmap)
7515 return -1;
7517 for (i = 0; i < bmap->n_div; ++i) {
7518 if (!isl_basic_map_div_is_known(bmap, i))
7519 return i;
7521 return bmap->n_div;
7524 /* Return the position of the first local variable that does not
7525 * have an explicit representation.
7526 * Return the total number of local variables if they all have
7527 * an explicit representation.
7528 * Return -1 on error.
7530 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7532 return isl_basic_map_first_unknown_div(bset);
7535 /* Does "bmap" have an explicit representation for all local variables?
7537 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7539 int first, n;
7541 n = isl_basic_map_dim(bmap, isl_dim_div);
7542 first = isl_basic_map_first_unknown_div(bmap);
7543 if (first < 0)
7544 return isl_bool_error;
7545 return first == n;
7548 /* Do all basic maps in "map" have an explicit representation
7549 * for all local variables?
7551 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7553 int i;
7555 if (!map)
7556 return isl_bool_error;
7558 for (i = 0; i < map->n; ++i) {
7559 int known = isl_basic_map_divs_known(map->p[i]);
7560 if (known <= 0)
7561 return known;
7564 return isl_bool_true;
7567 /* If bmap contains any unknown divs, then compute explicit
7568 * expressions for them. However, this computation may be
7569 * quite expensive, so first try to remove divs that aren't
7570 * strictly needed.
7572 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7574 int known;
7575 struct isl_map *map;
7577 known = isl_basic_map_divs_known(bmap);
7578 if (known < 0)
7579 goto error;
7580 if (known)
7581 return isl_map_from_basic_map(bmap);
7583 bmap = isl_basic_map_drop_redundant_divs(bmap);
7585 known = isl_basic_map_divs_known(bmap);
7586 if (known < 0)
7587 goto error;
7588 if (known)
7589 return isl_map_from_basic_map(bmap);
7591 map = compute_divs(bmap);
7592 return map;
7593 error:
7594 isl_basic_map_free(bmap);
7595 return NULL;
7598 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7600 int i;
7601 int known;
7602 struct isl_map *res;
7604 if (!map)
7605 return NULL;
7606 if (map->n == 0)
7607 return map;
7609 known = isl_map_divs_known(map);
7610 if (known < 0) {
7611 isl_map_free(map);
7612 return NULL;
7614 if (known)
7615 return map;
7617 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7618 for (i = 1 ; i < map->n; ++i) {
7619 struct isl_map *r2;
7620 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7621 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7622 res = isl_map_union_disjoint(res, r2);
7623 else
7624 res = isl_map_union(res, r2);
7626 isl_map_free(map);
7628 return res;
7631 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7633 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7636 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7638 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7641 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7643 int i;
7644 struct isl_set *set;
7646 if (!map)
7647 goto error;
7649 map = isl_map_cow(map);
7650 if (!map)
7651 return NULL;
7653 set = set_from_map(map);
7654 set->dim = isl_space_domain(set->dim);
7655 if (!set->dim)
7656 goto error;
7657 for (i = 0; i < map->n; ++i) {
7658 set->p[i] = isl_basic_map_domain(map->p[i]);
7659 if (!set->p[i])
7660 goto error;
7662 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7663 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7664 return set;
7665 error:
7666 isl_map_free(map);
7667 return NULL;
7670 /* Return the union of "map1" and "map2", where we assume for now that
7671 * "map1" and "map2" are disjoint. Note that the basic maps inside
7672 * "map1" or "map2" may not be disjoint from each other.
7673 * Also note that this function is also called from isl_map_union,
7674 * which takes care of handling the situation where "map1" and "map2"
7675 * may not be disjoint.
7677 * If one of the inputs is empty, we can simply return the other input.
7678 * Similarly, if one of the inputs is universal, then it is equal to the union.
7680 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7681 __isl_take isl_map *map2)
7683 int i;
7684 unsigned flags = 0;
7685 struct isl_map *map = NULL;
7686 int is_universe;
7688 if (!map1 || !map2)
7689 goto error;
7691 if (!isl_space_is_equal(map1->dim, map2->dim))
7692 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7693 "spaces don't match", goto error);
7695 if (map1->n == 0) {
7696 isl_map_free(map1);
7697 return map2;
7699 if (map2->n == 0) {
7700 isl_map_free(map2);
7701 return map1;
7704 is_universe = isl_map_plain_is_universe(map1);
7705 if (is_universe < 0)
7706 goto error;
7707 if (is_universe) {
7708 isl_map_free(map2);
7709 return map1;
7712 is_universe = isl_map_plain_is_universe(map2);
7713 if (is_universe < 0)
7714 goto error;
7715 if (is_universe) {
7716 isl_map_free(map1);
7717 return map2;
7720 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7721 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7722 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7724 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7725 map1->n + map2->n, flags);
7726 if (!map)
7727 goto error;
7728 for (i = 0; i < map1->n; ++i) {
7729 map = isl_map_add_basic_map(map,
7730 isl_basic_map_copy(map1->p[i]));
7731 if (!map)
7732 goto error;
7734 for (i = 0; i < map2->n; ++i) {
7735 map = isl_map_add_basic_map(map,
7736 isl_basic_map_copy(map2->p[i]));
7737 if (!map)
7738 goto error;
7740 isl_map_free(map1);
7741 isl_map_free(map2);
7742 return map;
7743 error:
7744 isl_map_free(map);
7745 isl_map_free(map1);
7746 isl_map_free(map2);
7747 return NULL;
7750 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7751 * guaranteed to be disjoint by the caller.
7753 * Note that this functions is called from within isl_map_make_disjoint,
7754 * so we have to be careful not to touch the constraints of the inputs
7755 * in any way.
7757 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7758 __isl_take isl_map *map2)
7760 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7763 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7764 * not be disjoint. The parameters are assumed to have been aligned.
7766 * We currently simply call map_union_disjoint, the internal operation
7767 * of which does not really depend on the inputs being disjoint.
7768 * If the result contains more than one basic map, then we clear
7769 * the disjoint flag since the result may contain basic maps from
7770 * both inputs and these are not guaranteed to be disjoint.
7772 * As a special case, if "map1" and "map2" are obviously equal,
7773 * then we simply return "map1".
7775 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7776 __isl_take isl_map *map2)
7778 int equal;
7780 if (!map1 || !map2)
7781 goto error;
7783 equal = isl_map_plain_is_equal(map1, map2);
7784 if (equal < 0)
7785 goto error;
7786 if (equal) {
7787 isl_map_free(map2);
7788 return map1;
7791 map1 = map_union_disjoint(map1, map2);
7792 if (!map1)
7793 return NULL;
7794 if (map1->n > 1)
7795 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7796 return map1;
7797 error:
7798 isl_map_free(map1);
7799 isl_map_free(map2);
7800 return NULL;
7803 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7804 * not be disjoint.
7806 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7807 __isl_take isl_map *map2)
7809 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7812 __isl_give isl_set *isl_set_union_disjoint(
7813 __isl_take isl_set *set1, __isl_take isl_set *set2)
7815 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7816 set_to_map(set2)));
7819 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7821 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7824 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7825 * the results.
7827 * "map" and "set" are assumed to be compatible and non-NULL.
7829 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7830 __isl_take isl_set *set,
7831 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7832 __isl_take isl_basic_set *bset))
7834 unsigned flags = 0;
7835 struct isl_map *result;
7836 int i, j;
7838 if (isl_set_plain_is_universe(set)) {
7839 isl_set_free(set);
7840 return map;
7843 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7844 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7845 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7847 result = isl_map_alloc_space(isl_space_copy(map->dim),
7848 map->n * set->n, flags);
7849 for (i = 0; result && i < map->n; ++i)
7850 for (j = 0; j < set->n; ++j) {
7851 result = isl_map_add_basic_map(result,
7852 fn(isl_basic_map_copy(map->p[i]),
7853 isl_basic_set_copy(set->p[j])));
7854 if (!result)
7855 break;
7858 isl_map_free(map);
7859 isl_set_free(set);
7860 return result;
7863 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7864 __isl_take isl_set *set)
7866 isl_bool ok;
7868 ok = isl_map_compatible_range(map, set);
7869 if (ok < 0)
7870 goto error;
7871 if (!ok)
7872 isl_die(set->ctx, isl_error_invalid,
7873 "incompatible spaces", goto error);
7875 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7876 error:
7877 isl_map_free(map);
7878 isl_set_free(set);
7879 return NULL;
7882 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7883 __isl_take isl_set *set)
7885 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7888 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7889 __isl_take isl_set *set)
7891 isl_bool ok;
7893 ok = isl_map_compatible_domain(map, set);
7894 if (ok < 0)
7895 goto error;
7896 if (!ok)
7897 isl_die(set->ctx, isl_error_invalid,
7898 "incompatible spaces", goto error);
7900 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7901 error:
7902 isl_map_free(map);
7903 isl_set_free(set);
7904 return NULL;
7907 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7908 __isl_take isl_set *set)
7910 return isl_map_align_params_map_map_and(map, set,
7911 &map_intersect_domain);
7914 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7915 * in the space B -> C, return the intersection.
7916 * The parameters are assumed to have been aligned.
7918 * The map "factor" is first extended to a map living in the space
7919 * [A -> B] -> C and then a regular intersection is computed.
7921 static __isl_give isl_map *map_intersect_domain_factor_range(
7922 __isl_take isl_map *map, __isl_take isl_map *factor)
7924 isl_space *space;
7925 isl_map *ext_factor;
7927 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7928 ext_factor = isl_map_universe(space);
7929 ext_factor = isl_map_domain_product(ext_factor, factor);
7930 return map_intersect(map, ext_factor);
7933 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7934 * in the space B -> C, return the intersection.
7936 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7937 __isl_take isl_map *map, __isl_take isl_map *factor)
7939 return isl_map_align_params_map_map_and(map, factor,
7940 &map_intersect_domain_factor_range);
7943 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7944 * in the space A -> C, return the intersection.
7946 * The map "factor" is first extended to a map living in the space
7947 * A -> [B -> C] and then a regular intersection is computed.
7949 static __isl_give isl_map *map_intersect_range_factor_range(
7950 __isl_take isl_map *map, __isl_take isl_map *factor)
7952 isl_space *space;
7953 isl_map *ext_factor;
7955 space = isl_space_range_factor_domain(isl_map_get_space(map));
7956 ext_factor = isl_map_universe(space);
7957 ext_factor = isl_map_range_product(ext_factor, factor);
7958 return isl_map_intersect(map, ext_factor);
7961 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7962 * in the space A -> C, return the intersection.
7964 __isl_give isl_map *isl_map_intersect_range_factor_range(
7965 __isl_take isl_map *map, __isl_take isl_map *factor)
7967 return isl_map_align_params_map_map_and(map, factor,
7968 &map_intersect_range_factor_range);
7971 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7972 __isl_take isl_map *map2)
7974 if (!map1 || !map2)
7975 goto error;
7976 map1 = isl_map_reverse(map1);
7977 map1 = isl_map_apply_range(map1, map2);
7978 return isl_map_reverse(map1);
7979 error:
7980 isl_map_free(map1);
7981 isl_map_free(map2);
7982 return NULL;
7985 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7986 __isl_take isl_map *map2)
7988 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7991 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7992 __isl_take isl_map *map2)
7994 isl_space *dim_result;
7995 struct isl_map *result;
7996 int i, j;
7998 if (!map1 || !map2)
7999 goto error;
8001 dim_result = isl_space_join(isl_space_copy(map1->dim),
8002 isl_space_copy(map2->dim));
8004 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
8005 if (!result)
8006 goto error;
8007 for (i = 0; i < map1->n; ++i)
8008 for (j = 0; j < map2->n; ++j) {
8009 result = isl_map_add_basic_map(result,
8010 isl_basic_map_apply_range(
8011 isl_basic_map_copy(map1->p[i]),
8012 isl_basic_map_copy(map2->p[j])));
8013 if (!result)
8014 goto error;
8016 isl_map_free(map1);
8017 isl_map_free(map2);
8018 if (result && result->n <= 1)
8019 ISL_F_SET(result, ISL_MAP_DISJOINT);
8020 return result;
8021 error:
8022 isl_map_free(map1);
8023 isl_map_free(map2);
8024 return NULL;
8027 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8028 __isl_take isl_map *map2)
8030 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
8034 * returns range - domain
8036 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8038 isl_space *target_space;
8039 struct isl_basic_set *bset;
8040 unsigned dim;
8041 unsigned nparam;
8042 int i;
8044 if (!bmap)
8045 goto error;
8046 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8047 bmap->dim, isl_dim_out),
8048 goto error);
8049 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8050 dim = isl_basic_map_dim(bmap, isl_dim_in);
8051 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8052 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8053 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8054 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8055 for (i = 0; i < dim; ++i) {
8056 int j = isl_basic_map_alloc_equality(bmap);
8057 if (j < 0) {
8058 bmap = isl_basic_map_free(bmap);
8059 break;
8061 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8062 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8063 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8064 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8066 bset = isl_basic_map_domain(bmap);
8067 bset = isl_basic_set_reset_space(bset, target_space);
8068 return bset;
8069 error:
8070 isl_basic_map_free(bmap);
8071 return NULL;
8075 * returns range - domain
8077 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8079 int i;
8080 isl_space *dim;
8081 struct isl_set *result;
8083 if (!map)
8084 return NULL;
8086 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8087 map->dim, isl_dim_out),
8088 goto error);
8089 dim = isl_map_get_space(map);
8090 dim = isl_space_domain(dim);
8091 result = isl_set_alloc_space(dim, map->n, 0);
8092 if (!result)
8093 goto error;
8094 for (i = 0; i < map->n; ++i)
8095 result = isl_set_add_basic_set(result,
8096 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8097 isl_map_free(map);
8098 return result;
8099 error:
8100 isl_map_free(map);
8101 return NULL;
8105 * returns [domain -> range] -> range - domain
8107 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8108 __isl_take isl_basic_map *bmap)
8110 int i, k;
8111 isl_space *dim;
8112 isl_basic_map *domain;
8113 int nparam, n;
8114 unsigned total;
8116 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8117 bmap->dim, isl_dim_out))
8118 isl_die(bmap->ctx, isl_error_invalid,
8119 "domain and range don't match", goto error);
8121 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8122 n = isl_basic_map_dim(bmap, isl_dim_in);
8124 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
8125 domain = isl_basic_map_universe(dim);
8127 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8128 bmap = isl_basic_map_apply_range(bmap, domain);
8129 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8131 total = isl_basic_map_total_dim(bmap);
8133 for (i = 0; i < n; ++i) {
8134 k = isl_basic_map_alloc_equality(bmap);
8135 if (k < 0)
8136 goto error;
8137 isl_seq_clr(bmap->eq[k], 1 + total);
8138 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8139 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8140 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8143 bmap = isl_basic_map_gauss(bmap, NULL);
8144 return isl_basic_map_finalize(bmap);
8145 error:
8146 isl_basic_map_free(bmap);
8147 return NULL;
8151 * returns [domain -> range] -> range - domain
8153 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8155 int i;
8156 isl_space *domain_dim;
8158 if (!map)
8159 return NULL;
8161 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
8162 map->dim, isl_dim_out))
8163 isl_die(map->ctx, isl_error_invalid,
8164 "domain and range don't match", goto error);
8166 map = isl_map_cow(map);
8167 if (!map)
8168 return NULL;
8170 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
8171 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8172 map->dim = isl_space_join(map->dim, domain_dim);
8173 if (!map->dim)
8174 goto error;
8175 for (i = 0; i < map->n; ++i) {
8176 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8177 if (!map->p[i])
8178 goto error;
8180 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8181 return map;
8182 error:
8183 isl_map_free(map);
8184 return NULL;
8187 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
8189 struct isl_basic_map *bmap;
8190 unsigned nparam;
8191 unsigned dim;
8192 int i;
8194 if (!dims)
8195 return NULL;
8197 nparam = dims->nparam;
8198 dim = dims->n_out;
8199 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
8200 if (!bmap)
8201 goto error;
8203 for (i = 0; i < dim; ++i) {
8204 int j = isl_basic_map_alloc_equality(bmap);
8205 if (j < 0)
8206 goto error;
8207 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8208 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8209 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
8211 return isl_basic_map_finalize(bmap);
8212 error:
8213 isl_basic_map_free(bmap);
8214 return NULL;
8217 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
8219 if (!dim)
8220 return NULL;
8221 if (dim->n_in != dim->n_out)
8222 isl_die(dim->ctx, isl_error_invalid,
8223 "number of input and output dimensions needs to be "
8224 "the same", goto error);
8225 return basic_map_identity(dim);
8226 error:
8227 isl_space_free(dim);
8228 return NULL;
8231 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8233 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8236 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8238 isl_space *dim = isl_set_get_space(set);
8239 isl_map *id;
8240 id = isl_map_identity(isl_space_map_from_set(dim));
8241 return isl_map_intersect_range(id, set);
8244 /* Construct a basic set with all set dimensions having only non-negative
8245 * values.
8247 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8248 __isl_take isl_space *space)
8250 int i;
8251 unsigned nparam;
8252 unsigned dim;
8253 struct isl_basic_set *bset;
8255 if (!space)
8256 return NULL;
8257 nparam = space->nparam;
8258 dim = space->n_out;
8259 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8260 if (!bset)
8261 return NULL;
8262 for (i = 0; i < dim; ++i) {
8263 int k = isl_basic_set_alloc_inequality(bset);
8264 if (k < 0)
8265 goto error;
8266 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8267 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8269 return bset;
8270 error:
8271 isl_basic_set_free(bset);
8272 return NULL;
8275 /* Construct the half-space x_pos >= 0.
8277 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
8278 int pos)
8280 int k;
8281 isl_basic_set *nonneg;
8283 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8284 k = isl_basic_set_alloc_inequality(nonneg);
8285 if (k < 0)
8286 goto error;
8287 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8288 isl_int_set_si(nonneg->ineq[k][pos], 1);
8290 return isl_basic_set_finalize(nonneg);
8291 error:
8292 isl_basic_set_free(nonneg);
8293 return NULL;
8296 /* Construct the half-space x_pos <= -1.
8298 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
8300 int k;
8301 isl_basic_set *neg;
8303 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8304 k = isl_basic_set_alloc_inequality(neg);
8305 if (k < 0)
8306 goto error;
8307 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8308 isl_int_set_si(neg->ineq[k][0], -1);
8309 isl_int_set_si(neg->ineq[k][pos], -1);
8311 return isl_basic_set_finalize(neg);
8312 error:
8313 isl_basic_set_free(neg);
8314 return NULL;
8317 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8318 enum isl_dim_type type, unsigned first, unsigned n)
8320 int i;
8321 unsigned offset;
8322 isl_basic_set *nonneg;
8323 isl_basic_set *neg;
8325 if (!set)
8326 return NULL;
8327 if (n == 0)
8328 return set;
8330 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
8332 offset = pos(set->dim, type);
8333 for (i = 0; i < n; ++i) {
8334 nonneg = nonneg_halfspace(isl_set_get_space(set),
8335 offset + first + i);
8336 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8338 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8341 return set;
8342 error:
8343 isl_set_free(set);
8344 return NULL;
8347 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8348 int len,
8349 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8350 void *user)
8352 isl_set *half;
8354 if (!set)
8355 return isl_stat_error;
8356 if (isl_set_plain_is_empty(set)) {
8357 isl_set_free(set);
8358 return isl_stat_ok;
8360 if (first == len)
8361 return fn(set, signs, user);
8363 signs[first] = 1;
8364 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8365 1 + first));
8366 half = isl_set_intersect(half, isl_set_copy(set));
8367 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8368 goto error;
8370 signs[first] = -1;
8371 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8372 1 + first));
8373 half = isl_set_intersect(half, set);
8374 return foreach_orthant(half, signs, first + 1, len, fn, user);
8375 error:
8376 isl_set_free(set);
8377 return isl_stat_error;
8380 /* Call "fn" on the intersections of "set" with each of the orthants
8381 * (except for obviously empty intersections). The orthant is identified
8382 * by the signs array, with each entry having value 1 or -1 according
8383 * to the sign of the corresponding variable.
8385 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8386 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8387 void *user)
8389 unsigned nparam;
8390 unsigned nvar;
8391 int *signs;
8392 isl_stat r;
8394 if (!set)
8395 return isl_stat_error;
8396 if (isl_set_plain_is_empty(set))
8397 return isl_stat_ok;
8399 nparam = isl_set_dim(set, isl_dim_param);
8400 nvar = isl_set_dim(set, isl_dim_set);
8402 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8404 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8405 fn, user);
8407 free(signs);
8409 return r;
8412 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8414 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8417 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8418 __isl_keep isl_basic_map *bmap2)
8420 int is_subset;
8421 struct isl_map *map1;
8422 struct isl_map *map2;
8424 if (!bmap1 || !bmap2)
8425 return isl_bool_error;
8427 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8428 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8430 is_subset = isl_map_is_subset(map1, map2);
8432 isl_map_free(map1);
8433 isl_map_free(map2);
8435 return is_subset;
8438 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8439 __isl_keep isl_basic_set *bset2)
8441 return isl_basic_map_is_subset(bset1, bset2);
8444 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8445 __isl_keep isl_basic_map *bmap2)
8447 isl_bool is_subset;
8449 if (!bmap1 || !bmap2)
8450 return isl_bool_error;
8451 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8452 if (is_subset != isl_bool_true)
8453 return is_subset;
8454 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8455 return is_subset;
8458 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8459 __isl_keep isl_basic_set *bset2)
8461 return isl_basic_map_is_equal(
8462 bset_to_bmap(bset1), bset_to_bmap(bset2));
8465 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8467 int i;
8468 int is_empty;
8470 if (!map)
8471 return isl_bool_error;
8472 for (i = 0; i < map->n; ++i) {
8473 is_empty = isl_basic_map_is_empty(map->p[i]);
8474 if (is_empty < 0)
8475 return isl_bool_error;
8476 if (!is_empty)
8477 return isl_bool_false;
8479 return isl_bool_true;
8482 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8484 return map ? map->n == 0 : isl_bool_error;
8487 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8489 return set ? set->n == 0 : isl_bool_error;
8492 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8494 return isl_map_is_empty(set_to_map(set));
8497 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8498 __isl_keep isl_map *map2)
8500 if (!map1 || !map2)
8501 return isl_bool_error;
8503 return isl_space_is_equal(map1->dim, map2->dim);
8506 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8507 __isl_keep isl_set *set2)
8509 if (!set1 || !set2)
8510 return isl_bool_error;
8512 return isl_space_is_equal(set1->dim, set2->dim);
8515 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8517 isl_bool is_subset;
8519 if (!map1 || !map2)
8520 return isl_bool_error;
8521 is_subset = isl_map_is_subset(map1, map2);
8522 if (is_subset != isl_bool_true)
8523 return is_subset;
8524 is_subset = isl_map_is_subset(map2, map1);
8525 return is_subset;
8528 /* Is "map1" equal to "map2"?
8530 * First check if they are obviously equal.
8531 * If not, then perform a more detailed analysis.
8533 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8535 isl_bool equal;
8537 equal = isl_map_plain_is_equal(map1, map2);
8538 if (equal < 0 || equal)
8539 return equal;
8540 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8543 isl_bool isl_basic_map_is_strict_subset(
8544 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8546 isl_bool is_subset;
8548 if (!bmap1 || !bmap2)
8549 return isl_bool_error;
8550 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8551 if (is_subset != isl_bool_true)
8552 return is_subset;
8553 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8554 if (is_subset == isl_bool_error)
8555 return is_subset;
8556 return !is_subset;
8559 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8560 __isl_keep isl_map *map2)
8562 isl_bool is_subset;
8564 if (!map1 || !map2)
8565 return isl_bool_error;
8566 is_subset = isl_map_is_subset(map1, map2);
8567 if (is_subset != isl_bool_true)
8568 return is_subset;
8569 is_subset = isl_map_is_subset(map2, map1);
8570 if (is_subset == isl_bool_error)
8571 return is_subset;
8572 return !is_subset;
8575 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8576 __isl_keep isl_set *set2)
8578 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8581 /* Is "bmap" obviously equal to the universe with the same space?
8583 * That is, does it not have any constraints?
8585 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8587 if (!bmap)
8588 return isl_bool_error;
8589 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8592 /* Is "bset" obviously equal to the universe with the same space?
8594 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8596 return isl_basic_map_plain_is_universe(bset);
8599 /* If "c" does not involve any existentially quantified variables,
8600 * then set *univ to false and abort
8602 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8604 isl_bool *univ = user;
8605 unsigned n;
8607 n = isl_constraint_dim(c, isl_dim_div);
8608 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8609 isl_constraint_free(c);
8610 if (*univ < 0 || !*univ)
8611 return isl_stat_error;
8612 return isl_stat_ok;
8615 /* Is "bmap" equal to the universe with the same space?
8617 * First check if it is obviously equal to the universe.
8618 * If not and if there are any constraints not involving
8619 * existentially quantified variables, then it is certainly
8620 * not equal to the universe.
8621 * Otherwise, check if the universe is a subset of "bmap".
8623 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8625 isl_bool univ;
8626 isl_basic_map *test;
8628 univ = isl_basic_map_plain_is_universe(bmap);
8629 if (univ < 0 || univ)
8630 return univ;
8631 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8632 return isl_bool_false;
8633 univ = isl_bool_true;
8634 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8635 univ)
8636 return isl_bool_error;
8637 if (univ < 0 || !univ)
8638 return univ;
8639 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8640 univ = isl_basic_map_is_subset(test, bmap);
8641 isl_basic_map_free(test);
8642 return univ;
8645 /* Is "bset" equal to the universe with the same space?
8647 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8649 return isl_basic_map_is_universe(bset);
8652 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8654 int i;
8656 if (!map)
8657 return isl_bool_error;
8659 for (i = 0; i < map->n; ++i) {
8660 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8661 if (r < 0 || r)
8662 return r;
8665 return isl_bool_false;
8668 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8670 return isl_map_plain_is_universe(set_to_map(set));
8673 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8675 struct isl_basic_set *bset = NULL;
8676 struct isl_vec *sample = NULL;
8677 isl_bool empty, non_empty;
8679 if (!bmap)
8680 return isl_bool_error;
8682 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8683 return isl_bool_true;
8685 if (isl_basic_map_plain_is_universe(bmap))
8686 return isl_bool_false;
8688 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8689 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8690 copy = isl_basic_map_remove_redundancies(copy);
8691 empty = isl_basic_map_plain_is_empty(copy);
8692 isl_basic_map_free(copy);
8693 return empty;
8696 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8697 if (non_empty < 0)
8698 return isl_bool_error;
8699 if (non_empty)
8700 return isl_bool_false;
8701 isl_vec_free(bmap->sample);
8702 bmap->sample = NULL;
8703 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8704 if (!bset)
8705 return isl_bool_error;
8706 sample = isl_basic_set_sample_vec(bset);
8707 if (!sample)
8708 return isl_bool_error;
8709 empty = sample->size == 0;
8710 isl_vec_free(bmap->sample);
8711 bmap->sample = sample;
8712 if (empty)
8713 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8715 return empty;
8718 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8720 if (!bmap)
8721 return isl_bool_error;
8722 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8725 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8727 if (!bset)
8728 return isl_bool_error;
8729 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8732 /* Is "bmap" known to be non-empty?
8734 * That is, is the cached sample still valid?
8736 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8738 unsigned total;
8740 if (!bmap)
8741 return isl_bool_error;
8742 if (!bmap->sample)
8743 return isl_bool_false;
8744 total = 1 + isl_basic_map_total_dim(bmap);
8745 if (bmap->sample->size != total)
8746 return isl_bool_false;
8747 return isl_basic_map_contains(bmap, bmap->sample);
8750 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8752 return isl_basic_map_is_empty(bset_to_bmap(bset));
8755 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8756 __isl_take isl_basic_map *bmap2)
8758 struct isl_map *map;
8759 if (!bmap1 || !bmap2)
8760 goto error;
8762 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8764 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8765 if (!map)
8766 goto error;
8767 map = isl_map_add_basic_map(map, bmap1);
8768 map = isl_map_add_basic_map(map, bmap2);
8769 return map;
8770 error:
8771 isl_basic_map_free(bmap1);
8772 isl_basic_map_free(bmap2);
8773 return NULL;
8776 struct isl_set *isl_basic_set_union(
8777 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8779 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8780 bset_to_bmap(bset2)));
8783 /* Order divs such that any div only depends on previous divs */
8784 __isl_give isl_basic_map *isl_basic_map_order_divs(
8785 __isl_take isl_basic_map *bmap)
8787 int i;
8788 unsigned off;
8790 if (!bmap)
8791 return NULL;
8793 off = isl_space_dim(bmap->dim, isl_dim_all);
8795 for (i = 0; i < bmap->n_div; ++i) {
8796 int pos;
8797 if (isl_int_is_zero(bmap->div[i][0]))
8798 continue;
8799 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8800 bmap->n_div-i);
8801 if (pos == -1)
8802 continue;
8803 if (pos == 0)
8804 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8805 "integer division depends on itself",
8806 return isl_basic_map_free(bmap));
8807 isl_basic_map_swap_div(bmap, i, i + pos);
8808 --i;
8810 return bmap;
8813 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8815 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8818 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8820 int i;
8822 if (!map)
8823 return 0;
8825 for (i = 0; i < map->n; ++i) {
8826 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8827 if (!map->p[i])
8828 goto error;
8831 return map;
8832 error:
8833 isl_map_free(map);
8834 return NULL;
8837 /* Sort the local variables of "bset".
8839 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8840 __isl_take isl_basic_set *bset)
8842 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8845 /* Apply the expansion computed by isl_merge_divs.
8846 * The expansion itself is given by "exp" while the resulting
8847 * list of divs is given by "div".
8849 * Move the integer divisions of "bmap" into the right position
8850 * according to "exp" and then introduce the additional integer
8851 * divisions, adding div constraints.
8852 * The moving should be done first to avoid moving coefficients
8853 * in the definitions of the extra integer divisions.
8855 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8856 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8858 int i, j;
8859 int n_div;
8861 bmap = isl_basic_map_cow(bmap);
8862 if (!bmap || !div)
8863 goto error;
8865 if (div->n_row < bmap->n_div)
8866 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8867 "not an expansion", goto error);
8869 n_div = bmap->n_div;
8870 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8871 div->n_row - n_div, 0,
8872 2 * (div->n_row - n_div));
8874 for (i = n_div; i < div->n_row; ++i)
8875 if (isl_basic_map_alloc_div(bmap) < 0)
8876 goto error;
8878 for (j = n_div - 1; j >= 0; --j) {
8879 if (exp[j] == j)
8880 break;
8881 isl_basic_map_swap_div(bmap, j, exp[j]);
8883 j = 0;
8884 for (i = 0; i < div->n_row; ++i) {
8885 if (j < n_div && exp[j] == i) {
8886 j++;
8887 } else {
8888 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8889 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8890 continue;
8891 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8892 goto error;
8896 isl_mat_free(div);
8897 return bmap;
8898 error:
8899 isl_basic_map_free(bmap);
8900 isl_mat_free(div);
8901 return NULL;
8904 /* Apply the expansion computed by isl_merge_divs.
8905 * The expansion itself is given by "exp" while the resulting
8906 * list of divs is given by "div".
8908 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8909 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8911 return isl_basic_map_expand_divs(bset, div, exp);
8914 /* Look for a div in dst that corresponds to the div "div" in src.
8915 * The divs before "div" in src and dst are assumed to be the same.
8917 * Returns -1 if no corresponding div was found and the position
8918 * of the corresponding div in dst otherwise.
8920 static int find_div(__isl_keep isl_basic_map *dst,
8921 __isl_keep isl_basic_map *src, unsigned div)
8923 int i;
8925 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8927 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8928 for (i = div; i < dst->n_div; ++i)
8929 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8930 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8931 dst->n_div - div) == -1)
8932 return i;
8933 return -1;
8936 /* Align the divs of "dst" to those of "src", adding divs from "src"
8937 * if needed. That is, make sure that the first src->n_div divs
8938 * of the result are equal to those of src.
8940 * The result is not finalized as by design it will have redundant
8941 * divs if any divs from "src" were copied.
8943 __isl_give isl_basic_map *isl_basic_map_align_divs(
8944 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8946 int i;
8947 int known, extended;
8948 unsigned total;
8950 if (!dst || !src)
8951 return isl_basic_map_free(dst);
8953 if (src->n_div == 0)
8954 return dst;
8956 known = isl_basic_map_divs_known(src);
8957 if (known < 0)
8958 return isl_basic_map_free(dst);
8959 if (!known)
8960 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8961 "some src divs are unknown",
8962 return isl_basic_map_free(dst));
8964 src = isl_basic_map_order_divs(src);
8966 extended = 0;
8967 total = isl_space_dim(src->dim, isl_dim_all);
8968 for (i = 0; i < src->n_div; ++i) {
8969 int j = find_div(dst, src, i);
8970 if (j < 0) {
8971 if (!extended) {
8972 int extra = src->n_div - i;
8973 dst = isl_basic_map_cow(dst);
8974 if (!dst)
8975 return NULL;
8976 dst = isl_basic_map_extend_space(dst,
8977 isl_space_copy(dst->dim),
8978 extra, 0, 2 * extra);
8979 extended = 1;
8981 j = isl_basic_map_alloc_div(dst);
8982 if (j < 0)
8983 return isl_basic_map_free(dst);
8984 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8985 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8986 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8987 return isl_basic_map_free(dst);
8989 if (j != i)
8990 isl_basic_map_swap_div(dst, i, j);
8992 return dst;
8995 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8997 int i;
8999 if (!map)
9000 return NULL;
9001 if (map->n == 0)
9002 return map;
9003 map = isl_map_compute_divs(map);
9004 map = isl_map_cow(map);
9005 if (!map)
9006 return NULL;
9008 for (i = 1; i < map->n; ++i)
9009 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9010 for (i = 1; i < map->n; ++i) {
9011 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9012 if (!map->p[i])
9013 return isl_map_free(map);
9016 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
9017 return map;
9020 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9022 return isl_map_align_divs_internal(map);
9025 struct isl_set *isl_set_align_divs(struct isl_set *set)
9027 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9030 /* Align the divs of the basic maps in "map" to those
9031 * of the basic maps in "list", as well as to the other basic maps in "map".
9032 * The elements in "list" are assumed to have known divs.
9034 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9035 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9037 int i, n;
9039 map = isl_map_compute_divs(map);
9040 map = isl_map_cow(map);
9041 if (!map || !list)
9042 return isl_map_free(map);
9043 if (map->n == 0)
9044 return map;
9046 n = isl_basic_map_list_n_basic_map(list);
9047 for (i = 0; i < n; ++i) {
9048 isl_basic_map *bmap;
9050 bmap = isl_basic_map_list_get_basic_map(list, i);
9051 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9052 isl_basic_map_free(bmap);
9054 if (!map->p[0])
9055 return isl_map_free(map);
9057 return isl_map_align_divs_internal(map);
9060 /* Align the divs of each element of "list" to those of "bmap".
9061 * Both "bmap" and the elements of "list" are assumed to have known divs.
9063 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9064 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9066 int i, n;
9068 if (!list || !bmap)
9069 return isl_basic_map_list_free(list);
9071 n = isl_basic_map_list_n_basic_map(list);
9072 for (i = 0; i < n; ++i) {
9073 isl_basic_map *bmap_i;
9075 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9076 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9077 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9080 return list;
9083 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9084 __isl_take isl_map *map)
9086 isl_bool ok;
9088 ok = isl_map_compatible_domain(map, set);
9089 if (ok < 0)
9090 goto error;
9091 if (!ok)
9092 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9093 "incompatible spaces", goto error);
9094 map = isl_map_intersect_domain(map, set);
9095 set = isl_map_range(map);
9096 return set;
9097 error:
9098 isl_set_free(set);
9099 isl_map_free(map);
9100 return NULL;
9103 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9104 __isl_take isl_map *map)
9106 return isl_map_align_params_map_map_and(set, map, &set_apply);
9109 /* There is no need to cow as removing empty parts doesn't change
9110 * the meaning of the set.
9112 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9114 int i;
9116 if (!map)
9117 return NULL;
9119 for (i = map->n - 1; i >= 0; --i)
9120 remove_if_empty(map, i);
9122 return map;
9125 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9127 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9130 /* Create a binary relation that maps the shared initial "pos" dimensions
9131 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9133 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9134 __isl_keep isl_basic_set *bset2, int pos)
9136 isl_basic_map *bmap1;
9137 isl_basic_map *bmap2;
9139 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9140 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9141 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9142 isl_dim_out, 0, pos);
9143 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9144 isl_dim_out, 0, pos);
9145 return isl_basic_map_range_product(bmap1, bmap2);
9148 /* Given two basic sets bset1 and bset2, compute the maximal difference
9149 * between the values of dimension pos in bset1 and those in bset2
9150 * for any common value of the parameters and dimensions preceding pos.
9152 static enum isl_lp_result basic_set_maximal_difference_at(
9153 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9154 int pos, isl_int *opt)
9156 isl_basic_map *bmap1;
9157 struct isl_ctx *ctx;
9158 struct isl_vec *obj;
9159 unsigned total;
9160 unsigned nparam;
9161 unsigned dim1;
9162 enum isl_lp_result res;
9164 if (!bset1 || !bset2)
9165 return isl_lp_error;
9167 nparam = isl_basic_set_n_param(bset1);
9168 dim1 = isl_basic_set_n_dim(bset1);
9170 bmap1 = join_initial(bset1, bset2, pos);
9171 if (!bmap1)
9172 return isl_lp_error;
9174 total = isl_basic_map_total_dim(bmap1);
9175 ctx = bmap1->ctx;
9176 obj = isl_vec_alloc(ctx, 1 + total);
9177 if (!obj)
9178 goto error;
9179 isl_seq_clr(obj->block.data, 1 + total);
9180 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9181 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9182 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9183 opt, NULL, NULL);
9184 isl_basic_map_free(bmap1);
9185 isl_vec_free(obj);
9186 return res;
9187 error:
9188 isl_basic_map_free(bmap1);
9189 return isl_lp_error;
9192 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9193 * for any common value of the parameters and dimensions preceding pos
9194 * in both basic sets, the values of dimension pos in bset1 are
9195 * smaller or larger than those in bset2.
9197 * Returns
9198 * 1 if bset1 follows bset2
9199 * -1 if bset1 precedes bset2
9200 * 0 if bset1 and bset2 are incomparable
9201 * -2 if some error occurred.
9203 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
9204 struct isl_basic_set *bset2, int pos)
9206 isl_int opt;
9207 enum isl_lp_result res;
9208 int cmp;
9210 isl_int_init(opt);
9212 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9214 if (res == isl_lp_empty)
9215 cmp = 0;
9216 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9217 res == isl_lp_unbounded)
9218 cmp = 1;
9219 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9220 cmp = -1;
9221 else
9222 cmp = -2;
9224 isl_int_clear(opt);
9225 return cmp;
9228 /* Given two basic sets bset1 and bset2, check whether
9229 * for any common value of the parameters and dimensions preceding pos
9230 * there is a value of dimension pos in bset1 that is larger
9231 * than a value of the same dimension in bset2.
9233 * Return
9234 * 1 if there exists such a pair
9235 * 0 if there is no such pair, but there is a pair of equal values
9236 * -1 otherwise
9237 * -2 if some error occurred.
9239 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9240 __isl_keep isl_basic_set *bset2, int pos)
9242 isl_bool empty;
9243 isl_basic_map *bmap;
9244 unsigned dim1;
9246 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9247 bmap = join_initial(bset1, bset2, pos);
9248 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9249 isl_dim_out, dim1 - pos);
9250 empty = isl_basic_map_is_empty(bmap);
9251 if (empty < 0)
9252 goto error;
9253 if (empty) {
9254 isl_basic_map_free(bmap);
9255 return -1;
9257 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9258 isl_dim_out, dim1 - pos);
9259 empty = isl_basic_map_is_empty(bmap);
9260 if (empty < 0)
9261 goto error;
9262 isl_basic_map_free(bmap);
9263 if (empty)
9264 return 0;
9265 return 1;
9266 error:
9267 isl_basic_map_free(bmap);
9268 return -2;
9271 /* Given two sets set1 and set2, check whether
9272 * for any common value of the parameters and dimensions preceding pos
9273 * there is a value of dimension pos in set1 that is larger
9274 * than a value of the same dimension in set2.
9276 * Return
9277 * 1 if there exists such a pair
9278 * 0 if there is no such pair, but there is a pair of equal values
9279 * -1 otherwise
9280 * -2 if some error occurred.
9282 int isl_set_follows_at(__isl_keep isl_set *set1,
9283 __isl_keep isl_set *set2, int pos)
9285 int i, j;
9286 int follows = -1;
9288 if (!set1 || !set2)
9289 return -2;
9291 for (i = 0; i < set1->n; ++i)
9292 for (j = 0; j < set2->n; ++j) {
9293 int f;
9294 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9295 if (f == 1 || f == -2)
9296 return f;
9297 if (f > follows)
9298 follows = f;
9301 return follows;
9304 static isl_bool isl_basic_map_plain_has_fixed_var(
9305 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9307 int i;
9308 int d;
9309 unsigned total;
9311 if (!bmap)
9312 return isl_bool_error;
9313 total = isl_basic_map_total_dim(bmap);
9314 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9315 for (; d+1 > pos; --d)
9316 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9317 break;
9318 if (d != pos)
9319 continue;
9320 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9321 return isl_bool_false;
9322 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9323 return isl_bool_false;
9324 if (!isl_int_is_one(bmap->eq[i][1+d]))
9325 return isl_bool_false;
9326 if (val)
9327 isl_int_neg(*val, bmap->eq[i][0]);
9328 return isl_bool_true;
9330 return isl_bool_false;
9333 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9334 unsigned pos, isl_int *val)
9336 int i;
9337 isl_int v;
9338 isl_int tmp;
9339 isl_bool fixed;
9341 if (!map)
9342 return isl_bool_error;
9343 if (map->n == 0)
9344 return isl_bool_false;
9345 if (map->n == 1)
9346 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9347 isl_int_init(v);
9348 isl_int_init(tmp);
9349 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9350 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9351 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9352 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9353 fixed = isl_bool_false;
9355 if (val)
9356 isl_int_set(*val, v);
9357 isl_int_clear(tmp);
9358 isl_int_clear(v);
9359 return fixed;
9362 static isl_bool isl_basic_set_plain_has_fixed_var(
9363 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9365 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9366 pos, val);
9369 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9370 enum isl_dim_type type, unsigned pos, isl_int *val)
9372 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9373 return isl_bool_error;
9374 return isl_basic_map_plain_has_fixed_var(bmap,
9375 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9378 /* If "bmap" obviously lies on a hyperplane where the given dimension
9379 * has a fixed value, then return that value.
9380 * Otherwise return NaN.
9382 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9383 __isl_keep isl_basic_map *bmap,
9384 enum isl_dim_type type, unsigned pos)
9386 isl_ctx *ctx;
9387 isl_val *v;
9388 isl_bool fixed;
9390 if (!bmap)
9391 return NULL;
9392 ctx = isl_basic_map_get_ctx(bmap);
9393 v = isl_val_alloc(ctx);
9394 if (!v)
9395 return NULL;
9396 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9397 if (fixed < 0)
9398 return isl_val_free(v);
9399 if (fixed) {
9400 isl_int_set_si(v->d, 1);
9401 return v;
9403 isl_val_free(v);
9404 return isl_val_nan(ctx);
9407 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9408 enum isl_dim_type type, unsigned pos, isl_int *val)
9410 if (pos >= isl_map_dim(map, type))
9411 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9412 "position out of bounds", return isl_bool_error);
9413 return isl_map_plain_has_fixed_var(map,
9414 map_offset(map, type) - 1 + pos, val);
9417 /* If "map" obviously lies on a hyperplane where the given dimension
9418 * has a fixed value, then return that value.
9419 * Otherwise return NaN.
9421 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9422 enum isl_dim_type type, unsigned pos)
9424 isl_ctx *ctx;
9425 isl_val *v;
9426 isl_bool fixed;
9428 if (!map)
9429 return NULL;
9430 ctx = isl_map_get_ctx(map);
9431 v = isl_val_alloc(ctx);
9432 if (!v)
9433 return NULL;
9434 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9435 if (fixed < 0)
9436 return isl_val_free(v);
9437 if (fixed) {
9438 isl_int_set_si(v->d, 1);
9439 return v;
9441 isl_val_free(v);
9442 return isl_val_nan(ctx);
9445 /* If "set" obviously lies on a hyperplane where the given dimension
9446 * has a fixed value, then return that value.
9447 * Otherwise return NaN.
9449 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9450 enum isl_dim_type type, unsigned pos)
9452 return isl_map_plain_get_val_if_fixed(set, type, pos);
9455 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9456 * then return this fixed value in *val.
9458 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9459 unsigned dim, isl_int *val)
9461 return isl_basic_set_plain_has_fixed_var(bset,
9462 isl_basic_set_n_param(bset) + dim, val);
9465 /* Return -1 if the constraint "c1" should be sorted before "c2"
9466 * and 1 if it should be sorted after "c2".
9467 * Return 0 if the two constraints are the same (up to the constant term).
9469 * In particular, if a constraint involves later variables than another
9470 * then it is sorted after this other constraint.
9471 * uset_gist depends on constraints without existentially quantified
9472 * variables sorting first.
9474 * For constraints that have the same latest variable, those
9475 * with the same coefficient for this latest variable (first in absolute value
9476 * and then in actual value) are grouped together.
9477 * This is useful for detecting pairs of constraints that can
9478 * be chained in their printed representation.
9480 * Finally, within a group, constraints are sorted according to
9481 * their coefficients (excluding the constant term).
9483 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9485 isl_int **c1 = (isl_int **) p1;
9486 isl_int **c2 = (isl_int **) p2;
9487 int l1, l2;
9488 unsigned size = *(unsigned *) arg;
9489 int cmp;
9491 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9492 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9494 if (l1 != l2)
9495 return l1 - l2;
9497 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9498 if (cmp != 0)
9499 return cmp;
9500 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9501 if (cmp != 0)
9502 return -cmp;
9504 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9507 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9508 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9509 * and 0 if the two constraints are the same (up to the constant term).
9511 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9512 isl_int *c1, isl_int *c2)
9514 unsigned total;
9516 if (!bmap)
9517 return -2;
9518 total = isl_basic_map_total_dim(bmap);
9519 return sort_constraint_cmp(&c1, &c2, &total);
9522 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9523 __isl_take isl_basic_map *bmap)
9525 unsigned total;
9527 if (!bmap)
9528 return NULL;
9529 if (bmap->n_ineq == 0)
9530 return bmap;
9531 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9532 return bmap;
9533 total = isl_basic_map_total_dim(bmap);
9534 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9535 &sort_constraint_cmp, &total) < 0)
9536 return isl_basic_map_free(bmap);
9537 return bmap;
9540 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9541 __isl_take isl_basic_set *bset)
9543 isl_basic_map *bmap = bset_to_bmap(bset);
9544 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9547 __isl_give isl_basic_map *isl_basic_map_normalize(
9548 __isl_take isl_basic_map *bmap)
9550 if (!bmap)
9551 return NULL;
9552 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9553 return bmap;
9554 bmap = isl_basic_map_remove_redundancies(bmap);
9555 bmap = isl_basic_map_sort_constraints(bmap);
9556 if (bmap)
9557 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9558 return bmap;
9560 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9561 __isl_keep isl_basic_map *bmap2)
9563 int i, cmp;
9564 unsigned total;
9565 isl_space *space1, *space2;
9567 if (!bmap1 || !bmap2)
9568 return -1;
9570 if (bmap1 == bmap2)
9571 return 0;
9572 space1 = isl_basic_map_peek_space(bmap1);
9573 space2 = isl_basic_map_peek_space(bmap2);
9574 cmp = isl_space_cmp(space1, space2);
9575 if (cmp)
9576 return cmp;
9577 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9578 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9579 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9580 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9581 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9582 return 0;
9583 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9584 return 1;
9585 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9586 return -1;
9587 if (bmap1->n_eq != bmap2->n_eq)
9588 return bmap1->n_eq - bmap2->n_eq;
9589 if (bmap1->n_ineq != bmap2->n_ineq)
9590 return bmap1->n_ineq - bmap2->n_ineq;
9591 if (bmap1->n_div != bmap2->n_div)
9592 return bmap1->n_div - bmap2->n_div;
9593 total = isl_basic_map_total_dim(bmap1);
9594 for (i = 0; i < bmap1->n_eq; ++i) {
9595 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9596 if (cmp)
9597 return cmp;
9599 for (i = 0; i < bmap1->n_ineq; ++i) {
9600 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9601 if (cmp)
9602 return cmp;
9604 for (i = 0; i < bmap1->n_div; ++i) {
9605 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9606 if (cmp)
9607 return cmp;
9609 return 0;
9612 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9613 __isl_keep isl_basic_set *bset2)
9615 return isl_basic_map_plain_cmp(bset1, bset2);
9618 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9620 int i, cmp;
9622 if (set1 == set2)
9623 return 0;
9624 if (set1->n != set2->n)
9625 return set1->n - set2->n;
9627 for (i = 0; i < set1->n; ++i) {
9628 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9629 if (cmp)
9630 return cmp;
9633 return 0;
9636 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9637 __isl_keep isl_basic_map *bmap2)
9639 if (!bmap1 || !bmap2)
9640 return isl_bool_error;
9641 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9644 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9645 __isl_keep isl_basic_set *bset2)
9647 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9648 bset_to_bmap(bset2));
9651 static int qsort_bmap_cmp(const void *p1, const void *p2)
9653 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9654 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9656 return isl_basic_map_plain_cmp(bmap1, bmap2);
9659 /* Sort the basic maps of "map" and remove duplicate basic maps.
9661 * While removing basic maps, we make sure that the basic maps remain
9662 * sorted because isl_map_normalize expects the basic maps of the result
9663 * to be sorted.
9665 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9667 int i, j;
9669 map = isl_map_remove_empty_parts(map);
9670 if (!map)
9671 return NULL;
9672 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9673 for (i = map->n - 1; i >= 1; --i) {
9674 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9675 continue;
9676 isl_basic_map_free(map->p[i-1]);
9677 for (j = i; j < map->n; ++j)
9678 map->p[j - 1] = map->p[j];
9679 map->n--;
9682 return map;
9685 /* Remove obvious duplicates among the basic maps of "map".
9687 * Unlike isl_map_normalize, this function does not remove redundant
9688 * constraints and only removes duplicates that have exactly the same
9689 * constraints in the input. It does sort the constraints and
9690 * the basic maps to ease the detection of duplicates.
9692 * If "map" has already been normalized or if the basic maps are
9693 * disjoint, then there can be no duplicates.
9695 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9697 int i;
9698 isl_basic_map *bmap;
9700 if (!map)
9701 return NULL;
9702 if (map->n <= 1)
9703 return map;
9704 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9705 return map;
9706 for (i = 0; i < map->n; ++i) {
9707 bmap = isl_basic_map_copy(map->p[i]);
9708 bmap = isl_basic_map_sort_constraints(bmap);
9709 if (!bmap)
9710 return isl_map_free(map);
9711 isl_basic_map_free(map->p[i]);
9712 map->p[i] = bmap;
9715 map = sort_and_remove_duplicates(map);
9716 return map;
9719 /* We normalize in place, but if anything goes wrong we need
9720 * to return NULL, so we need to make sure we don't change the
9721 * meaning of any possible other copies of map.
9723 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9725 int i;
9726 struct isl_basic_map *bmap;
9728 if (!map)
9729 return NULL;
9730 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9731 return map;
9732 for (i = 0; i < map->n; ++i) {
9733 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9734 if (!bmap)
9735 goto error;
9736 isl_basic_map_free(map->p[i]);
9737 map->p[i] = bmap;
9740 map = sort_and_remove_duplicates(map);
9741 if (map)
9742 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9743 return map;
9744 error:
9745 isl_map_free(map);
9746 return NULL;
9749 struct isl_set *isl_set_normalize(struct isl_set *set)
9751 return set_from_map(isl_map_normalize(set_to_map(set)));
9754 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9755 __isl_keep isl_map *map2)
9757 int i;
9758 isl_bool equal;
9760 if (!map1 || !map2)
9761 return isl_bool_error;
9763 if (map1 == map2)
9764 return isl_bool_true;
9765 if (!isl_space_is_equal(map1->dim, map2->dim))
9766 return isl_bool_false;
9768 map1 = isl_map_copy(map1);
9769 map2 = isl_map_copy(map2);
9770 map1 = isl_map_normalize(map1);
9771 map2 = isl_map_normalize(map2);
9772 if (!map1 || !map2)
9773 goto error;
9774 equal = map1->n == map2->n;
9775 for (i = 0; equal && i < map1->n; ++i) {
9776 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9777 if (equal < 0)
9778 goto error;
9780 isl_map_free(map1);
9781 isl_map_free(map2);
9782 return equal;
9783 error:
9784 isl_map_free(map1);
9785 isl_map_free(map2);
9786 return isl_bool_error;
9789 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9790 __isl_keep isl_set *set2)
9792 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9795 /* Return the basic maps in "map" as a list.
9797 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9798 __isl_keep isl_map *map)
9800 int i;
9801 isl_ctx *ctx;
9802 isl_basic_map_list *list;
9804 if (!map)
9805 return NULL;
9806 ctx = isl_map_get_ctx(map);
9807 list = isl_basic_map_list_alloc(ctx, map->n);
9809 for (i = 0; i < map->n; ++i) {
9810 isl_basic_map *bmap;
9812 bmap = isl_basic_map_copy(map->p[i]);
9813 list = isl_basic_map_list_add(list, bmap);
9816 return list;
9819 /* Return the intersection of the elements in the non-empty list "list".
9820 * All elements are assumed to live in the same space.
9822 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9823 __isl_take isl_basic_map_list *list)
9825 int i, n;
9826 isl_basic_map *bmap;
9828 if (!list)
9829 return NULL;
9830 n = isl_basic_map_list_n_basic_map(list);
9831 if (n < 1)
9832 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9833 "expecting non-empty list", goto error);
9835 bmap = isl_basic_map_list_get_basic_map(list, 0);
9836 for (i = 1; i < n; ++i) {
9837 isl_basic_map *bmap_i;
9839 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9840 bmap = isl_basic_map_intersect(bmap, bmap_i);
9843 isl_basic_map_list_free(list);
9844 return bmap;
9845 error:
9846 isl_basic_map_list_free(list);
9847 return NULL;
9850 /* Return the intersection of the elements in the non-empty list "list".
9851 * All elements are assumed to live in the same space.
9853 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9854 __isl_take isl_basic_set_list *list)
9856 return isl_basic_map_list_intersect(list);
9859 /* Return the union of the elements of "list".
9860 * The list is required to have at least one element.
9862 __isl_give isl_set *isl_basic_set_list_union(
9863 __isl_take isl_basic_set_list *list)
9865 int i, n;
9866 isl_space *space;
9867 isl_basic_set *bset;
9868 isl_set *set;
9870 if (!list)
9871 return NULL;
9872 n = isl_basic_set_list_n_basic_set(list);
9873 if (n < 1)
9874 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9875 "expecting non-empty list", goto error);
9877 bset = isl_basic_set_list_get_basic_set(list, 0);
9878 space = isl_basic_set_get_space(bset);
9879 isl_basic_set_free(bset);
9881 set = isl_set_alloc_space(space, n, 0);
9882 for (i = 0; i < n; ++i) {
9883 bset = isl_basic_set_list_get_basic_set(list, i);
9884 set = isl_set_add_basic_set(set, bset);
9887 isl_basic_set_list_free(list);
9888 return set;
9889 error:
9890 isl_basic_set_list_free(list);
9891 return NULL;
9894 /* Return the union of the elements in the non-empty list "list".
9895 * All elements are assumed to live in the same space.
9897 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9899 int i, n;
9900 isl_set *set;
9902 if (!list)
9903 return NULL;
9904 n = isl_set_list_n_set(list);
9905 if (n < 1)
9906 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9907 "expecting non-empty list", goto error);
9909 set = isl_set_list_get_set(list, 0);
9910 for (i = 1; i < n; ++i) {
9911 isl_set *set_i;
9913 set_i = isl_set_list_get_set(list, i);
9914 set = isl_set_union(set, set_i);
9917 isl_set_list_free(list);
9918 return set;
9919 error:
9920 isl_set_list_free(list);
9921 return NULL;
9924 __isl_give isl_basic_map *isl_basic_map_product(
9925 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9927 isl_space *dim_result = NULL;
9928 struct isl_basic_map *bmap;
9929 unsigned in1, in2, out1, out2, nparam, total, pos;
9930 struct isl_dim_map *dim_map1, *dim_map2;
9932 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9933 goto error;
9934 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9935 isl_space_copy(bmap2->dim));
9937 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9938 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9939 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9940 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9941 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9943 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9944 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9945 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9946 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9947 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9948 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9949 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9950 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9951 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9952 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9953 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9955 bmap = isl_basic_map_alloc_space(dim_result,
9956 bmap1->n_div + bmap2->n_div,
9957 bmap1->n_eq + bmap2->n_eq,
9958 bmap1->n_ineq + bmap2->n_ineq);
9959 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9960 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9961 bmap = isl_basic_map_simplify(bmap);
9962 return isl_basic_map_finalize(bmap);
9963 error:
9964 isl_basic_map_free(bmap1);
9965 isl_basic_map_free(bmap2);
9966 return NULL;
9969 __isl_give isl_basic_map *isl_basic_map_flat_product(
9970 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9972 isl_basic_map *prod;
9974 prod = isl_basic_map_product(bmap1, bmap2);
9975 prod = isl_basic_map_flatten(prod);
9976 return prod;
9979 __isl_give isl_basic_set *isl_basic_set_flat_product(
9980 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9982 return isl_basic_map_flat_range_product(bset1, bset2);
9985 __isl_give isl_basic_map *isl_basic_map_domain_product(
9986 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9988 isl_space *space_result = NULL;
9989 isl_basic_map *bmap;
9990 unsigned in1, in2, out, nparam, total, pos;
9991 struct isl_dim_map *dim_map1, *dim_map2;
9993 if (!bmap1 || !bmap2)
9994 goto error;
9996 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9997 isl_space_copy(bmap2->dim));
9999 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10000 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10001 out = isl_basic_map_dim(bmap1, isl_dim_out);
10002 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10004 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10005 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10006 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10007 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10008 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10009 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10010 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10011 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10012 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10013 isl_dim_map_div(dim_map1, bmap1, pos += out);
10014 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10016 bmap = isl_basic_map_alloc_space(space_result,
10017 bmap1->n_div + bmap2->n_div,
10018 bmap1->n_eq + bmap2->n_eq,
10019 bmap1->n_ineq + bmap2->n_ineq);
10020 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10021 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10022 bmap = isl_basic_map_simplify(bmap);
10023 return isl_basic_map_finalize(bmap);
10024 error:
10025 isl_basic_map_free(bmap1);
10026 isl_basic_map_free(bmap2);
10027 return NULL;
10030 __isl_give isl_basic_map *isl_basic_map_range_product(
10031 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10033 isl_bool rational;
10034 isl_space *dim_result = NULL;
10035 isl_basic_map *bmap;
10036 unsigned in, out1, out2, nparam, total, pos;
10037 struct isl_dim_map *dim_map1, *dim_map2;
10039 rational = isl_basic_map_is_rational(bmap1);
10040 if (rational >= 0 && rational)
10041 rational = isl_basic_map_is_rational(bmap2);
10042 if (!bmap1 || !bmap2 || rational < 0)
10043 goto error;
10045 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10046 goto error;
10048 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10049 isl_space_copy(bmap2->dim));
10051 in = isl_basic_map_dim(bmap1, isl_dim_in);
10052 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10053 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10054 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10056 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10057 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10058 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10059 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10060 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10061 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10062 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10063 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10064 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10065 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10066 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10068 bmap = isl_basic_map_alloc_space(dim_result,
10069 bmap1->n_div + bmap2->n_div,
10070 bmap1->n_eq + bmap2->n_eq,
10071 bmap1->n_ineq + bmap2->n_ineq);
10072 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10073 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10074 if (rational)
10075 bmap = isl_basic_map_set_rational(bmap);
10076 bmap = isl_basic_map_simplify(bmap);
10077 return isl_basic_map_finalize(bmap);
10078 error:
10079 isl_basic_map_free(bmap1);
10080 isl_basic_map_free(bmap2);
10081 return NULL;
10084 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10085 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10087 isl_basic_map *prod;
10089 prod = isl_basic_map_range_product(bmap1, bmap2);
10090 prod = isl_basic_map_flatten_range(prod);
10091 return prod;
10094 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10095 * and collect the results.
10096 * The result live in the space obtained by calling "space_product"
10097 * on the spaces of "map1" and "map2".
10098 * If "remove_duplicates" is set then the result may contain duplicates
10099 * (even if the inputs do not) and so we try and remove the obvious
10100 * duplicates.
10102 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10103 __isl_take isl_map *map2,
10104 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10105 __isl_take isl_space *right),
10106 __isl_give isl_basic_map *(*basic_map_product)(
10107 __isl_take isl_basic_map *left,
10108 __isl_take isl_basic_map *right),
10109 int remove_duplicates)
10111 unsigned flags = 0;
10112 struct isl_map *result;
10113 int i, j;
10114 isl_bool m;
10116 m = isl_map_has_equal_params(map1, map2);
10117 if (m < 0)
10118 goto error;
10119 if (!m)
10120 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10121 "parameters don't match", goto error);
10123 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10124 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10125 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10127 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10128 isl_space_copy(map2->dim)),
10129 map1->n * map2->n, flags);
10130 if (!result)
10131 goto error;
10132 for (i = 0; i < map1->n; ++i)
10133 for (j = 0; j < map2->n; ++j) {
10134 struct isl_basic_map *part;
10135 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10136 isl_basic_map_copy(map2->p[j]));
10137 if (isl_basic_map_is_empty(part))
10138 isl_basic_map_free(part);
10139 else
10140 result = isl_map_add_basic_map(result, part);
10141 if (!result)
10142 goto error;
10144 if (remove_duplicates)
10145 result = isl_map_remove_obvious_duplicates(result);
10146 isl_map_free(map1);
10147 isl_map_free(map2);
10148 return result;
10149 error:
10150 isl_map_free(map1);
10151 isl_map_free(map2);
10152 return NULL;
10155 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10157 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10158 __isl_take isl_map *map2)
10160 return map_product(map1, map2, &isl_space_product,
10161 &isl_basic_map_product, 0);
10164 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10165 __isl_take isl_map *map2)
10167 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10170 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10172 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10173 __isl_take isl_map *map2)
10175 isl_map *prod;
10177 prod = isl_map_product(map1, map2);
10178 prod = isl_map_flatten(prod);
10179 return prod;
10182 /* Given two set A and B, construct its Cartesian product A x B.
10184 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10186 return isl_map_range_product(set1, set2);
10189 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10190 __isl_take isl_set *set2)
10192 return isl_map_flat_range_product(set1, set2);
10195 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10197 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10198 __isl_take isl_map *map2)
10200 return map_product(map1, map2, &isl_space_domain_product,
10201 &isl_basic_map_domain_product, 1);
10204 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10206 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10207 __isl_take isl_map *map2)
10209 return map_product(map1, map2, &isl_space_range_product,
10210 &isl_basic_map_range_product, 1);
10213 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10214 __isl_take isl_map *map2)
10216 return isl_map_align_params_map_map_and(map1, map2,
10217 &map_domain_product_aligned);
10220 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10221 __isl_take isl_map *map2)
10223 return isl_map_align_params_map_map_and(map1, map2,
10224 &map_range_product_aligned);
10227 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10229 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10231 isl_space *space;
10232 int total1, keep1, total2, keep2;
10234 if (!map)
10235 return NULL;
10236 if (!isl_space_domain_is_wrapping(map->dim) ||
10237 !isl_space_range_is_wrapping(map->dim))
10238 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10239 "not a product", return isl_map_free(map));
10241 space = isl_map_get_space(map);
10242 total1 = isl_space_dim(space, isl_dim_in);
10243 total2 = isl_space_dim(space, isl_dim_out);
10244 space = isl_space_factor_domain(space);
10245 keep1 = isl_space_dim(space, isl_dim_in);
10246 keep2 = isl_space_dim(space, isl_dim_out);
10247 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10248 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10249 map = isl_map_reset_space(map, space);
10251 return map;
10254 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10256 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10258 isl_space *space;
10259 int total1, keep1, total2, keep2;
10261 if (!map)
10262 return NULL;
10263 if (!isl_space_domain_is_wrapping(map->dim) ||
10264 !isl_space_range_is_wrapping(map->dim))
10265 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10266 "not a product", return isl_map_free(map));
10268 space = isl_map_get_space(map);
10269 total1 = isl_space_dim(space, isl_dim_in);
10270 total2 = isl_space_dim(space, isl_dim_out);
10271 space = isl_space_factor_range(space);
10272 keep1 = isl_space_dim(space, isl_dim_in);
10273 keep2 = isl_space_dim(space, isl_dim_out);
10274 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10275 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10276 map = isl_map_reset_space(map, space);
10278 return map;
10281 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10283 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10285 isl_space *space;
10286 int total, keep;
10288 if (!map)
10289 return NULL;
10290 if (!isl_space_domain_is_wrapping(map->dim))
10291 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10292 "domain is not a product", return isl_map_free(map));
10294 space = isl_map_get_space(map);
10295 total = isl_space_dim(space, isl_dim_in);
10296 space = isl_space_domain_factor_domain(space);
10297 keep = isl_space_dim(space, isl_dim_in);
10298 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10299 map = isl_map_reset_space(map, space);
10301 return map;
10304 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10306 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10308 isl_space *space;
10309 int total, keep;
10311 if (!map)
10312 return NULL;
10313 if (!isl_space_domain_is_wrapping(map->dim))
10314 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10315 "domain is not a product", return isl_map_free(map));
10317 space = isl_map_get_space(map);
10318 total = isl_space_dim(space, isl_dim_in);
10319 space = isl_space_domain_factor_range(space);
10320 keep = isl_space_dim(space, isl_dim_in);
10321 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10322 map = isl_map_reset_space(map, space);
10324 return map;
10327 /* Given a map A -> [B -> C], extract the map A -> B.
10329 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10331 isl_space *space;
10332 int total, keep;
10334 if (!map)
10335 return NULL;
10336 if (!isl_space_range_is_wrapping(map->dim))
10337 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10338 "range is not a product", return isl_map_free(map));
10340 space = isl_map_get_space(map);
10341 total = isl_space_dim(space, isl_dim_out);
10342 space = isl_space_range_factor_domain(space);
10343 keep = isl_space_dim(space, isl_dim_out);
10344 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10345 map = isl_map_reset_space(map, space);
10347 return map;
10350 /* Given a map A -> [B -> C], extract the map A -> C.
10352 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10354 isl_space *space;
10355 int total, keep;
10357 if (!map)
10358 return NULL;
10359 if (!isl_space_range_is_wrapping(map->dim))
10360 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10361 "range is not a product", return isl_map_free(map));
10363 space = isl_map_get_space(map);
10364 total = isl_space_dim(space, isl_dim_out);
10365 space = isl_space_range_factor_range(space);
10366 keep = isl_space_dim(space, isl_dim_out);
10367 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10368 map = isl_map_reset_space(map, space);
10370 return map;
10373 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10375 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10376 __isl_take isl_map *map2)
10378 isl_map *prod;
10380 prod = isl_map_domain_product(map1, map2);
10381 prod = isl_map_flatten_domain(prod);
10382 return prod;
10385 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10387 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10388 __isl_take isl_map *map2)
10390 isl_map *prod;
10392 prod = isl_map_range_product(map1, map2);
10393 prod = isl_map_flatten_range(prod);
10394 return prod;
10397 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10399 int i;
10400 uint32_t hash = isl_hash_init();
10401 unsigned total;
10403 if (!bmap)
10404 return 0;
10405 bmap = isl_basic_map_copy(bmap);
10406 bmap = isl_basic_map_normalize(bmap);
10407 if (!bmap)
10408 return 0;
10409 total = isl_basic_map_total_dim(bmap);
10410 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10411 for (i = 0; i < bmap->n_eq; ++i) {
10412 uint32_t c_hash;
10413 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10414 isl_hash_hash(hash, c_hash);
10416 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10417 for (i = 0; i < bmap->n_ineq; ++i) {
10418 uint32_t c_hash;
10419 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10420 isl_hash_hash(hash, c_hash);
10422 isl_hash_byte(hash, bmap->n_div & 0xFF);
10423 for (i = 0; i < bmap->n_div; ++i) {
10424 uint32_t c_hash;
10425 if (isl_int_is_zero(bmap->div[i][0]))
10426 continue;
10427 isl_hash_byte(hash, i & 0xFF);
10428 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10429 isl_hash_hash(hash, c_hash);
10431 isl_basic_map_free(bmap);
10432 return hash;
10435 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10437 return isl_basic_map_get_hash(bset_to_bmap(bset));
10440 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10442 int i;
10443 uint32_t hash;
10445 if (!map)
10446 return 0;
10447 map = isl_map_copy(map);
10448 map = isl_map_normalize(map);
10449 if (!map)
10450 return 0;
10452 hash = isl_hash_init();
10453 for (i = 0; i < map->n; ++i) {
10454 uint32_t bmap_hash;
10455 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10456 isl_hash_hash(hash, bmap_hash);
10459 isl_map_free(map);
10461 return hash;
10464 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10466 return isl_map_get_hash(set_to_map(set));
10469 /* Return the number of basic maps in the (current) representation of "map".
10471 int isl_map_n_basic_map(__isl_keep isl_map *map)
10473 return map ? map->n : 0;
10476 int isl_set_n_basic_set(__isl_keep isl_set *set)
10478 return set ? set->n : 0;
10481 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10482 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10484 int i;
10486 if (!map)
10487 return isl_stat_error;
10489 for (i = 0; i < map->n; ++i)
10490 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10491 return isl_stat_error;
10493 return isl_stat_ok;
10496 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10497 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10499 int i;
10501 if (!set)
10502 return isl_stat_error;
10504 for (i = 0; i < set->n; ++i)
10505 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10506 return isl_stat_error;
10508 return isl_stat_ok;
10511 /* Return a list of basic sets, the union of which is equal to "set".
10513 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10514 __isl_keep isl_set *set)
10516 int i;
10517 isl_basic_set_list *list;
10519 if (!set)
10520 return NULL;
10522 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10523 for (i = 0; i < set->n; ++i) {
10524 isl_basic_set *bset;
10526 bset = isl_basic_set_copy(set->p[i]);
10527 list = isl_basic_set_list_add(list, bset);
10530 return list;
10533 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10535 isl_space *dim;
10537 if (!bset)
10538 return NULL;
10540 bset = isl_basic_set_cow(bset);
10541 if (!bset)
10542 return NULL;
10544 dim = isl_basic_set_get_space(bset);
10545 dim = isl_space_lift(dim, bset->n_div);
10546 if (!dim)
10547 goto error;
10548 isl_space_free(bset->dim);
10549 bset->dim = dim;
10550 bset->extra -= bset->n_div;
10551 bset->n_div = 0;
10553 bset = isl_basic_set_finalize(bset);
10555 return bset;
10556 error:
10557 isl_basic_set_free(bset);
10558 return NULL;
10561 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10563 int i;
10564 isl_space *dim;
10565 unsigned n_div;
10567 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10569 if (!set)
10570 return NULL;
10572 set = isl_set_cow(set);
10573 if (!set)
10574 return NULL;
10576 n_div = set->p[0]->n_div;
10577 dim = isl_set_get_space(set);
10578 dim = isl_space_lift(dim, n_div);
10579 if (!dim)
10580 goto error;
10581 isl_space_free(set->dim);
10582 set->dim = dim;
10584 for (i = 0; i < set->n; ++i) {
10585 set->p[i] = isl_basic_set_lift(set->p[i]);
10586 if (!set->p[i])
10587 goto error;
10590 return set;
10591 error:
10592 isl_set_free(set);
10593 return NULL;
10596 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10598 unsigned dim;
10599 int size = 0;
10601 if (!bset)
10602 return -1;
10604 dim = isl_basic_set_total_dim(bset);
10605 size += bset->n_eq * (1 + dim);
10606 size += bset->n_ineq * (1 + dim);
10607 size += bset->n_div * (2 + dim);
10609 return size;
10612 int isl_set_size(__isl_keep isl_set *set)
10614 int i;
10615 int size = 0;
10617 if (!set)
10618 return -1;
10620 for (i = 0; i < set->n; ++i)
10621 size += isl_basic_set_size(set->p[i]);
10623 return size;
10626 /* Check if there is any lower bound (if lower == 0) and/or upper
10627 * bound (if upper == 0) on the specified dim.
10629 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10630 enum isl_dim_type type, unsigned pos, int lower, int upper)
10632 int i;
10634 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10635 return isl_bool_error;
10637 pos += isl_basic_map_offset(bmap, type);
10639 for (i = 0; i < bmap->n_div; ++i) {
10640 if (isl_int_is_zero(bmap->div[i][0]))
10641 continue;
10642 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10643 return isl_bool_true;
10646 for (i = 0; i < bmap->n_eq; ++i)
10647 if (!isl_int_is_zero(bmap->eq[i][pos]))
10648 return isl_bool_true;
10650 for (i = 0; i < bmap->n_ineq; ++i) {
10651 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10652 if (sgn > 0)
10653 lower = 1;
10654 if (sgn < 0)
10655 upper = 1;
10658 return lower && upper;
10661 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10662 enum isl_dim_type type, unsigned pos)
10664 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10667 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10668 enum isl_dim_type type, unsigned pos)
10670 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10673 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10674 enum isl_dim_type type, unsigned pos)
10676 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10679 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10680 enum isl_dim_type type, unsigned pos)
10682 int i;
10684 if (!map)
10685 return isl_bool_error;
10687 for (i = 0; i < map->n; ++i) {
10688 isl_bool bounded;
10689 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10690 if (bounded < 0 || !bounded)
10691 return bounded;
10694 return isl_bool_true;
10697 /* Return true if the specified dim is involved in both an upper bound
10698 * and a lower bound.
10700 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10701 enum isl_dim_type type, unsigned pos)
10703 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10706 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10708 static isl_bool has_any_bound(__isl_keep isl_map *map,
10709 enum isl_dim_type type, unsigned pos,
10710 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10711 enum isl_dim_type type, unsigned pos))
10713 int i;
10715 if (!map)
10716 return isl_bool_error;
10718 for (i = 0; i < map->n; ++i) {
10719 isl_bool bounded;
10720 bounded = fn(map->p[i], type, pos);
10721 if (bounded < 0 || bounded)
10722 return bounded;
10725 return isl_bool_false;
10728 /* Return 1 if the specified dim is involved in any lower bound.
10730 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10731 enum isl_dim_type type, unsigned pos)
10733 return has_any_bound(set, type, pos,
10734 &isl_basic_map_dim_has_lower_bound);
10737 /* Return 1 if the specified dim is involved in any upper bound.
10739 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10740 enum isl_dim_type type, unsigned pos)
10742 return has_any_bound(set, type, pos,
10743 &isl_basic_map_dim_has_upper_bound);
10746 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10748 static isl_bool has_bound(__isl_keep isl_map *map,
10749 enum isl_dim_type type, unsigned pos,
10750 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10751 enum isl_dim_type type, unsigned pos))
10753 int i;
10755 if (!map)
10756 return isl_bool_error;
10758 for (i = 0; i < map->n; ++i) {
10759 isl_bool bounded;
10760 bounded = fn(map->p[i], type, pos);
10761 if (bounded < 0 || !bounded)
10762 return bounded;
10765 return isl_bool_true;
10768 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10770 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10771 enum isl_dim_type type, unsigned pos)
10773 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10776 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10778 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10779 enum isl_dim_type type, unsigned pos)
10781 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10784 /* For each of the "n" variables starting at "first", determine
10785 * the sign of the variable and put the results in the first "n"
10786 * elements of the array "signs".
10787 * Sign
10788 * 1 means that the variable is non-negative
10789 * -1 means that the variable is non-positive
10790 * 0 means the variable attains both positive and negative values.
10792 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10793 unsigned first, unsigned n, int *signs)
10795 isl_vec *bound = NULL;
10796 struct isl_tab *tab = NULL;
10797 struct isl_tab_undo *snap;
10798 int i;
10800 if (!bset || !signs)
10801 return isl_stat_error;
10803 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10804 tab = isl_tab_from_basic_set(bset, 0);
10805 if (!bound || !tab)
10806 goto error;
10808 isl_seq_clr(bound->el, bound->size);
10809 isl_int_set_si(bound->el[0], -1);
10811 snap = isl_tab_snap(tab);
10812 for (i = 0; i < n; ++i) {
10813 int empty;
10815 isl_int_set_si(bound->el[1 + first + i], -1);
10816 if (isl_tab_add_ineq(tab, bound->el) < 0)
10817 goto error;
10818 empty = tab->empty;
10819 isl_int_set_si(bound->el[1 + first + i], 0);
10820 if (isl_tab_rollback(tab, snap) < 0)
10821 goto error;
10823 if (empty) {
10824 signs[i] = 1;
10825 continue;
10828 isl_int_set_si(bound->el[1 + first + i], 1);
10829 if (isl_tab_add_ineq(tab, bound->el) < 0)
10830 goto error;
10831 empty = tab->empty;
10832 isl_int_set_si(bound->el[1 + first + i], 0);
10833 if (isl_tab_rollback(tab, snap) < 0)
10834 goto error;
10836 signs[i] = empty ? -1 : 0;
10839 isl_tab_free(tab);
10840 isl_vec_free(bound);
10841 return isl_stat_ok;
10842 error:
10843 isl_tab_free(tab);
10844 isl_vec_free(bound);
10845 return isl_stat_error;
10848 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10849 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10851 if (!bset || !signs)
10852 return isl_stat_error;
10853 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10854 return isl_stat_error);
10856 first += pos(bset->dim, type) - 1;
10857 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10860 /* Is it possible for the integer division "div" to depend (possibly
10861 * indirectly) on any output dimensions?
10863 * If the div is undefined, then we conservatively assume that it
10864 * may depend on them.
10865 * Otherwise, we check if it actually depends on them or on any integer
10866 * divisions that may depend on them.
10868 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10870 int i;
10871 unsigned n_out, o_out;
10872 unsigned n_div, o_div;
10874 if (isl_int_is_zero(bmap->div[div][0]))
10875 return isl_bool_true;
10877 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10878 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10880 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10881 return isl_bool_true;
10883 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10884 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10886 for (i = 0; i < n_div; ++i) {
10887 isl_bool may_involve;
10889 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10890 continue;
10891 may_involve = div_may_involve_output(bmap, i);
10892 if (may_involve < 0 || may_involve)
10893 return may_involve;
10896 return isl_bool_false;
10899 /* Return the first integer division of "bmap" in the range
10900 * [first, first + n[ that may depend on any output dimensions and
10901 * that has a non-zero coefficient in "c" (where the first coefficient
10902 * in "c" corresponds to integer division "first").
10904 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10905 isl_int *c, int first, int n)
10907 int k;
10909 if (!bmap)
10910 return -1;
10912 for (k = first; k < first + n; ++k) {
10913 isl_bool may_involve;
10915 if (isl_int_is_zero(c[k]))
10916 continue;
10917 may_involve = div_may_involve_output(bmap, k);
10918 if (may_involve < 0)
10919 return -1;
10920 if (may_involve)
10921 return k;
10924 return first + n;
10927 /* Look for a pair of inequality constraints in "bmap" of the form
10929 * -l + i >= 0 or i >= l
10930 * and
10931 * n + l - i >= 0 or i <= l + n
10933 * with n < "m" and i the output dimension at position "pos".
10934 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10935 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10936 * and earlier output dimensions, as well as integer divisions that do
10937 * not involve any of the output dimensions.
10939 * Return the index of the first inequality constraint or bmap->n_ineq
10940 * if no such pair can be found.
10942 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10943 int pos, isl_int m)
10945 int i, j;
10946 isl_ctx *ctx;
10947 unsigned total;
10948 unsigned n_div, o_div;
10949 unsigned n_out, o_out;
10950 int less;
10952 if (!bmap)
10953 return -1;
10955 ctx = isl_basic_map_get_ctx(bmap);
10956 total = isl_basic_map_total_dim(bmap);
10957 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10958 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10959 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10960 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10961 for (i = 0; i < bmap->n_ineq; ++i) {
10962 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10963 continue;
10964 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10965 n_out - (pos + 1)) != -1)
10966 continue;
10967 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10968 0, n_div) < n_div)
10969 continue;
10970 for (j = i + 1; j < bmap->n_ineq; ++j) {
10971 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10972 ctx->one))
10973 continue;
10974 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10975 bmap->ineq[j] + 1, total))
10976 continue;
10977 break;
10979 if (j >= bmap->n_ineq)
10980 continue;
10981 isl_int_add(bmap->ineq[i][0],
10982 bmap->ineq[i][0], bmap->ineq[j][0]);
10983 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10984 isl_int_sub(bmap->ineq[i][0],
10985 bmap->ineq[i][0], bmap->ineq[j][0]);
10986 if (!less)
10987 continue;
10988 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10989 return i;
10990 else
10991 return j;
10994 return bmap->n_ineq;
10997 /* Return the index of the equality of "bmap" that defines
10998 * the output dimension "pos" in terms of earlier dimensions.
10999 * The equality may also involve integer divisions, as long
11000 * as those integer divisions are defined in terms of
11001 * parameters or input dimensions.
11002 * In this case, *div is set to the number of integer divisions and
11003 * *ineq is set to the number of inequality constraints (provided
11004 * div and ineq are not NULL).
11006 * The equality may also involve a single integer division involving
11007 * the output dimensions (typically only output dimension "pos") as
11008 * long as the coefficient of output dimension "pos" is 1 or -1 and
11009 * there is a pair of constraints i >= l and i <= l + n, with i referring
11010 * to output dimension "pos", l an expression involving only earlier
11011 * dimensions and n smaller than the coefficient of the integer division
11012 * in the equality. In this case, the output dimension can be defined
11013 * in terms of a modulo expression that does not involve the integer division.
11014 * *div is then set to this single integer division and
11015 * *ineq is set to the index of constraint i >= l.
11017 * Return bmap->n_eq if there is no such equality.
11018 * Return -1 on error.
11020 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11021 int pos, int *div, int *ineq)
11023 int j, k, l;
11024 unsigned n_out, o_out;
11025 unsigned n_div, o_div;
11027 if (!bmap)
11028 return -1;
11030 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11031 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11032 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11033 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11035 if (ineq)
11036 *ineq = bmap->n_ineq;
11037 if (div)
11038 *div = n_div;
11039 for (j = 0; j < bmap->n_eq; ++j) {
11040 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11041 continue;
11042 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11043 n_out - (pos + 1)) != -1)
11044 continue;
11045 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11046 0, n_div);
11047 if (k >= n_div)
11048 return j;
11049 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11050 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11051 continue;
11052 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11053 k + 1, n_div - (k+1)) < n_div)
11054 continue;
11055 l = find_modulo_constraint_pair(bmap, pos,
11056 bmap->eq[j][o_div + k]);
11057 if (l < 0)
11058 return -1;
11059 if (l >= bmap->n_ineq)
11060 continue;
11061 if (div)
11062 *div = k;
11063 if (ineq)
11064 *ineq = l;
11065 return j;
11068 return bmap->n_eq;
11071 /* Check if the given basic map is obviously single-valued.
11072 * In particular, for each output dimension, check that there is
11073 * an equality that defines the output dimension in terms of
11074 * earlier dimensions.
11076 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11078 int i;
11079 unsigned n_out;
11081 if (!bmap)
11082 return isl_bool_error;
11084 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11086 for (i = 0; i < n_out; ++i) {
11087 int eq;
11089 eq = isl_basic_map_output_defining_equality(bmap, i,
11090 NULL, NULL);
11091 if (eq < 0)
11092 return isl_bool_error;
11093 if (eq >= bmap->n_eq)
11094 return isl_bool_false;
11097 return isl_bool_true;
11100 /* Check if the given basic map is single-valued.
11101 * We simply compute
11103 * M \circ M^-1
11105 * and check if the result is a subset of the identity mapping.
11107 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11109 isl_space *space;
11110 isl_basic_map *test;
11111 isl_basic_map *id;
11112 isl_bool sv;
11114 sv = isl_basic_map_plain_is_single_valued(bmap);
11115 if (sv < 0 || sv)
11116 return sv;
11118 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11119 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11121 space = isl_basic_map_get_space(bmap);
11122 space = isl_space_map_from_set(isl_space_range(space));
11123 id = isl_basic_map_identity(space);
11125 sv = isl_basic_map_is_subset(test, id);
11127 isl_basic_map_free(test);
11128 isl_basic_map_free(id);
11130 return sv;
11133 /* Check if the given map is obviously single-valued.
11135 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11137 if (!map)
11138 return isl_bool_error;
11139 if (map->n == 0)
11140 return isl_bool_true;
11141 if (map->n >= 2)
11142 return isl_bool_false;
11144 return isl_basic_map_plain_is_single_valued(map->p[0]);
11147 /* Check if the given map is single-valued.
11148 * We simply compute
11150 * M \circ M^-1
11152 * and check if the result is a subset of the identity mapping.
11154 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11156 isl_space *dim;
11157 isl_map *test;
11158 isl_map *id;
11159 isl_bool sv;
11161 sv = isl_map_plain_is_single_valued(map);
11162 if (sv < 0 || sv)
11163 return sv;
11165 test = isl_map_reverse(isl_map_copy(map));
11166 test = isl_map_apply_range(test, isl_map_copy(map));
11168 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11169 id = isl_map_identity(dim);
11171 sv = isl_map_is_subset(test, id);
11173 isl_map_free(test);
11174 isl_map_free(id);
11176 return sv;
11179 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11181 isl_bool in;
11183 map = isl_map_copy(map);
11184 map = isl_map_reverse(map);
11185 in = isl_map_is_single_valued(map);
11186 isl_map_free(map);
11188 return in;
11191 /* Check if the given map is obviously injective.
11193 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11195 isl_bool in;
11197 map = isl_map_copy(map);
11198 map = isl_map_reverse(map);
11199 in = isl_map_plain_is_single_valued(map);
11200 isl_map_free(map);
11202 return in;
11205 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11207 isl_bool sv;
11209 sv = isl_map_is_single_valued(map);
11210 if (sv < 0 || !sv)
11211 return sv;
11213 return isl_map_is_injective(map);
11216 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11218 return isl_map_is_single_valued(set_to_map(set));
11221 /* Does "map" only map elements to themselves?
11223 * If the domain and range spaces are different, then "map"
11224 * is considered not to be an identity relation, even if it is empty.
11225 * Otherwise, construct the maximal identity relation and
11226 * check whether "map" is a subset of this relation.
11228 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11230 isl_space *space;
11231 isl_map *id;
11232 isl_bool equal, is_identity;
11234 space = isl_map_get_space(map);
11235 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11236 isl_space_free(space);
11237 if (equal < 0 || !equal)
11238 return equal;
11240 id = isl_map_identity(isl_map_get_space(map));
11241 is_identity = isl_map_is_subset(map, id);
11242 isl_map_free(id);
11244 return is_identity;
11247 int isl_map_is_translation(__isl_keep isl_map *map)
11249 int ok;
11250 isl_set *delta;
11252 delta = isl_map_deltas(isl_map_copy(map));
11253 ok = isl_set_is_singleton(delta);
11254 isl_set_free(delta);
11256 return ok;
11259 static int unique(isl_int *p, unsigned pos, unsigned len)
11261 if (isl_seq_first_non_zero(p, pos) != -1)
11262 return 0;
11263 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11264 return 0;
11265 return 1;
11268 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11270 int i, j;
11271 unsigned nvar;
11272 unsigned ovar;
11274 if (!bset)
11275 return isl_bool_error;
11277 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11278 return isl_bool_false;
11280 nvar = isl_basic_set_dim(bset, isl_dim_set);
11281 ovar = isl_space_offset(bset->dim, isl_dim_set);
11282 for (j = 0; j < nvar; ++j) {
11283 int lower = 0, upper = 0;
11284 for (i = 0; i < bset->n_eq; ++i) {
11285 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11286 continue;
11287 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11288 return isl_bool_false;
11289 break;
11291 if (i < bset->n_eq)
11292 continue;
11293 for (i = 0; i < bset->n_ineq; ++i) {
11294 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11295 continue;
11296 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11297 return isl_bool_false;
11298 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11299 lower = 1;
11300 else
11301 upper = 1;
11303 if (!lower || !upper)
11304 return isl_bool_false;
11307 return isl_bool_true;
11310 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11312 if (!set)
11313 return isl_bool_error;
11314 if (set->n != 1)
11315 return isl_bool_false;
11317 return isl_basic_set_is_box(set->p[0]);
11320 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11322 if (!bset)
11323 return isl_bool_error;
11325 return isl_space_is_wrapping(bset->dim);
11328 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11330 if (!set)
11331 return isl_bool_error;
11333 return isl_space_is_wrapping(set->dim);
11336 /* Modify the space of "map" through a call to "change".
11337 * If "can_change" is set (not NULL), then first call it to check
11338 * if the modification is allowed, printing the error message "cannot_change"
11339 * if it is not.
11341 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11342 isl_bool (*can_change)(__isl_keep isl_map *map),
11343 const char *cannot_change,
11344 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11346 isl_bool ok;
11347 isl_space *space;
11349 if (!map)
11350 return NULL;
11352 ok = can_change ? can_change(map) : isl_bool_true;
11353 if (ok < 0)
11354 return isl_map_free(map);
11355 if (!ok)
11356 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11357 return isl_map_free(map));
11359 space = change(isl_map_get_space(map));
11360 map = isl_map_reset_space(map, space);
11362 return map;
11365 /* Is the domain of "map" a wrapped relation?
11367 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11369 if (!map)
11370 return isl_bool_error;
11372 return isl_space_domain_is_wrapping(map->dim);
11375 /* Does "map" have a wrapped relation in both domain and range?
11377 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11379 return isl_space_is_product(isl_map_peek_space(map));
11382 /* Is the range of "map" a wrapped relation?
11384 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11386 if (!map)
11387 return isl_bool_error;
11389 return isl_space_range_is_wrapping(map->dim);
11392 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11394 bmap = isl_basic_map_cow(bmap);
11395 if (!bmap)
11396 return NULL;
11398 bmap->dim = isl_space_wrap(bmap->dim);
11399 if (!bmap->dim)
11400 goto error;
11402 bmap = isl_basic_map_finalize(bmap);
11404 return bset_from_bmap(bmap);
11405 error:
11406 isl_basic_map_free(bmap);
11407 return NULL;
11410 /* Given a map A -> B, return the set (A -> B).
11412 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11414 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11417 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11419 bset = isl_basic_set_cow(bset);
11420 if (!bset)
11421 return NULL;
11423 bset->dim = isl_space_unwrap(bset->dim);
11424 if (!bset->dim)
11425 goto error;
11427 bset = isl_basic_set_finalize(bset);
11429 return bset_to_bmap(bset);
11430 error:
11431 isl_basic_set_free(bset);
11432 return NULL;
11435 /* Given a set (A -> B), return the map A -> B.
11436 * Error out if "set" is not of the form (A -> B).
11438 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11440 return isl_map_change_space(set, &isl_set_is_wrapping,
11441 "not a wrapping set", &isl_space_unwrap);
11444 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11445 enum isl_dim_type type)
11447 if (!bmap)
11448 return NULL;
11450 if (!isl_space_is_named_or_nested(bmap->dim, type))
11451 return bmap;
11453 bmap = isl_basic_map_cow(bmap);
11454 if (!bmap)
11455 return NULL;
11457 bmap->dim = isl_space_reset(bmap->dim, type);
11458 if (!bmap->dim)
11459 goto error;
11461 bmap = isl_basic_map_finalize(bmap);
11463 return bmap;
11464 error:
11465 isl_basic_map_free(bmap);
11466 return NULL;
11469 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11470 enum isl_dim_type type)
11472 int i;
11474 if (!map)
11475 return NULL;
11477 if (!isl_space_is_named_or_nested(map->dim, type))
11478 return map;
11480 map = isl_map_cow(map);
11481 if (!map)
11482 return NULL;
11484 for (i = 0; i < map->n; ++i) {
11485 map->p[i] = isl_basic_map_reset(map->p[i], type);
11486 if (!map->p[i])
11487 goto error;
11489 map->dim = isl_space_reset(map->dim, type);
11490 if (!map->dim)
11491 goto error;
11493 return map;
11494 error:
11495 isl_map_free(map);
11496 return NULL;
11499 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11501 if (!bmap)
11502 return NULL;
11504 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11505 return bmap;
11507 bmap = isl_basic_map_cow(bmap);
11508 if (!bmap)
11509 return NULL;
11511 bmap->dim = isl_space_flatten(bmap->dim);
11512 if (!bmap->dim)
11513 goto error;
11515 bmap = isl_basic_map_finalize(bmap);
11517 return bmap;
11518 error:
11519 isl_basic_map_free(bmap);
11520 return NULL;
11523 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11525 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11528 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11529 __isl_take isl_basic_map *bmap)
11531 if (!bmap)
11532 return NULL;
11534 if (!bmap->dim->nested[0])
11535 return bmap;
11537 bmap = isl_basic_map_cow(bmap);
11538 if (!bmap)
11539 return NULL;
11541 bmap->dim = isl_space_flatten_domain(bmap->dim);
11542 if (!bmap->dim)
11543 goto error;
11545 bmap = isl_basic_map_finalize(bmap);
11547 return bmap;
11548 error:
11549 isl_basic_map_free(bmap);
11550 return NULL;
11553 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11554 __isl_take isl_basic_map *bmap)
11556 if (!bmap)
11557 return NULL;
11559 if (!bmap->dim->nested[1])
11560 return bmap;
11562 bmap = isl_basic_map_cow(bmap);
11563 if (!bmap)
11564 return NULL;
11566 bmap->dim = isl_space_flatten_range(bmap->dim);
11567 if (!bmap->dim)
11568 goto error;
11570 bmap = isl_basic_map_finalize(bmap);
11572 return bmap;
11573 error:
11574 isl_basic_map_free(bmap);
11575 return NULL;
11578 /* Remove any internal structure from the spaces of domain and range of "map".
11580 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11582 if (!map)
11583 return NULL;
11585 if (!map->dim->nested[0] && !map->dim->nested[1])
11586 return map;
11588 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11591 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11593 return set_from_map(isl_map_flatten(set_to_map(set)));
11596 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11598 isl_space *dim, *flat_dim;
11599 isl_map *map;
11601 dim = isl_set_get_space(set);
11602 flat_dim = isl_space_flatten(isl_space_copy(dim));
11603 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11604 map = isl_map_intersect_domain(map, set);
11606 return map;
11609 /* Remove any internal structure from the space of the domain of "map".
11611 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11613 if (!map)
11614 return NULL;
11616 if (!map->dim->nested[0])
11617 return map;
11619 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11622 /* Remove any internal structure from the space of the range of "map".
11624 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11626 if (!map)
11627 return NULL;
11629 if (!map->dim->nested[1])
11630 return map;
11632 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11635 /* Reorder the dimensions of "bmap" according to the given dim_map
11636 * and set the dimension specification to "dim" and
11637 * perform Gaussian elimination on the result.
11639 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11640 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11642 isl_basic_map *res;
11643 unsigned flags;
11645 bmap = isl_basic_map_cow(bmap);
11646 if (!bmap || !dim || !dim_map)
11647 goto error;
11649 flags = bmap->flags;
11650 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11651 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11652 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11653 res = isl_basic_map_alloc_space(dim,
11654 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11655 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11656 if (res)
11657 res->flags = flags;
11658 res = isl_basic_map_gauss(res, NULL);
11659 res = isl_basic_map_finalize(res);
11660 return res;
11661 error:
11662 free(dim_map);
11663 isl_basic_map_free(bmap);
11664 isl_space_free(dim);
11665 return NULL;
11668 /* Reorder the dimensions of "map" according to given reordering.
11670 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11671 __isl_take isl_reordering *r)
11673 int i;
11674 struct isl_dim_map *dim_map;
11676 map = isl_map_cow(map);
11677 dim_map = isl_dim_map_from_reordering(r);
11678 if (!map || !r || !dim_map)
11679 goto error;
11681 for (i = 0; i < map->n; ++i) {
11682 struct isl_dim_map *dim_map_i;
11684 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11686 map->p[i] = isl_basic_map_realign(map->p[i],
11687 isl_space_copy(r->dim), dim_map_i);
11689 if (!map->p[i])
11690 goto error;
11693 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11695 isl_reordering_free(r);
11696 free(dim_map);
11697 return map;
11698 error:
11699 free(dim_map);
11700 isl_map_free(map);
11701 isl_reordering_free(r);
11702 return NULL;
11705 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11706 __isl_take isl_reordering *r)
11708 return set_from_map(isl_map_realign(set_to_map(set), r));
11711 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11712 __isl_take isl_space *model)
11714 isl_ctx *ctx;
11715 isl_bool aligned;
11717 if (!map || !model)
11718 goto error;
11720 ctx = isl_space_get_ctx(model);
11721 if (!isl_space_has_named_params(model))
11722 isl_die(ctx, isl_error_invalid,
11723 "model has unnamed parameters", goto error);
11724 if (isl_map_check_named_params(map) < 0)
11725 goto error;
11726 aligned = isl_map_space_has_equal_params(map, model);
11727 if (aligned < 0)
11728 goto error;
11729 if (!aligned) {
11730 isl_reordering *exp;
11732 model = isl_space_drop_dims(model, isl_dim_in,
11733 0, isl_space_dim(model, isl_dim_in));
11734 model = isl_space_drop_dims(model, isl_dim_out,
11735 0, isl_space_dim(model, isl_dim_out));
11736 exp = isl_parameter_alignment_reordering(map->dim, model);
11737 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11738 map = isl_map_realign(map, exp);
11741 isl_space_free(model);
11742 return map;
11743 error:
11744 isl_space_free(model);
11745 isl_map_free(map);
11746 return NULL;
11749 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11750 __isl_take isl_space *model)
11752 return isl_map_align_params(set, model);
11755 /* Align the parameters of "bmap" to those of "model", introducing
11756 * additional parameters if needed.
11758 __isl_give isl_basic_map *isl_basic_map_align_params(
11759 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11761 isl_ctx *ctx;
11762 isl_bool equal_params;
11764 if (!bmap || !model)
11765 goto error;
11767 ctx = isl_space_get_ctx(model);
11768 if (!isl_space_has_named_params(model))
11769 isl_die(ctx, isl_error_invalid,
11770 "model has unnamed parameters", goto error);
11771 if (isl_basic_map_check_named_params(bmap) < 0)
11772 goto error;
11773 equal_params = isl_space_has_equal_params(bmap->dim, model);
11774 if (equal_params < 0)
11775 goto error;
11776 if (!equal_params) {
11777 isl_reordering *exp;
11778 struct isl_dim_map *dim_map;
11780 model = isl_space_drop_dims(model, isl_dim_in,
11781 0, isl_space_dim(model, isl_dim_in));
11782 model = isl_space_drop_dims(model, isl_dim_out,
11783 0, isl_space_dim(model, isl_dim_out));
11784 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11785 exp = isl_reordering_extend_space(exp,
11786 isl_basic_map_get_space(bmap));
11787 dim_map = isl_dim_map_from_reordering(exp);
11788 bmap = isl_basic_map_realign(bmap,
11789 exp ? isl_space_copy(exp->dim) : NULL,
11790 isl_dim_map_extend(dim_map, bmap));
11791 isl_reordering_free(exp);
11792 free(dim_map);
11795 isl_space_free(model);
11796 return bmap;
11797 error:
11798 isl_space_free(model);
11799 isl_basic_map_free(bmap);
11800 return NULL;
11803 /* Do "bset" and "space" have the same parameters?
11805 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11806 __isl_keep isl_space *space)
11808 isl_space *bset_space;
11810 bset_space = isl_basic_set_peek_space(bset);
11811 return isl_space_has_equal_params(bset_space, space);
11814 /* Do "map" and "space" have the same parameters?
11816 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11817 __isl_keep isl_space *space)
11819 isl_space *map_space;
11821 map_space = isl_map_peek_space(map);
11822 return isl_space_has_equal_params(map_space, space);
11825 /* Do "set" and "space" have the same parameters?
11827 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11828 __isl_keep isl_space *space)
11830 return isl_map_space_has_equal_params(set_to_map(set), space);
11833 /* Align the parameters of "bset" to those of "model", introducing
11834 * additional parameters if needed.
11836 __isl_give isl_basic_set *isl_basic_set_align_params(
11837 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11839 return isl_basic_map_align_params(bset, model);
11842 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11843 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11844 enum isl_dim_type c2, enum isl_dim_type c3,
11845 enum isl_dim_type c4, enum isl_dim_type c5)
11847 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11848 struct isl_mat *mat;
11849 int i, j, k;
11850 int pos;
11852 if (!bmap)
11853 return NULL;
11854 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11855 isl_basic_map_total_dim(bmap) + 1);
11856 if (!mat)
11857 return NULL;
11858 for (i = 0; i < bmap->n_eq; ++i)
11859 for (j = 0, pos = 0; j < 5; ++j) {
11860 int off = isl_basic_map_offset(bmap, c[j]);
11861 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11862 isl_int_set(mat->row[i][pos],
11863 bmap->eq[i][off + k]);
11864 ++pos;
11868 return mat;
11871 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11872 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11873 enum isl_dim_type c2, enum isl_dim_type c3,
11874 enum isl_dim_type c4, enum isl_dim_type c5)
11876 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11877 struct isl_mat *mat;
11878 int i, j, k;
11879 int pos;
11881 if (!bmap)
11882 return NULL;
11883 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11884 isl_basic_map_total_dim(bmap) + 1);
11885 if (!mat)
11886 return NULL;
11887 for (i = 0; i < bmap->n_ineq; ++i)
11888 for (j = 0, pos = 0; j < 5; ++j) {
11889 int off = isl_basic_map_offset(bmap, c[j]);
11890 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11891 isl_int_set(mat->row[i][pos],
11892 bmap->ineq[i][off + k]);
11893 ++pos;
11897 return mat;
11900 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11901 __isl_take isl_space *dim,
11902 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11903 enum isl_dim_type c2, enum isl_dim_type c3,
11904 enum isl_dim_type c4, enum isl_dim_type c5)
11906 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11907 isl_basic_map *bmap;
11908 unsigned total;
11909 unsigned extra;
11910 int i, j, k, l;
11911 int pos;
11913 if (!dim || !eq || !ineq)
11914 goto error;
11916 if (eq->n_col != ineq->n_col)
11917 isl_die(dim->ctx, isl_error_invalid,
11918 "equalities and inequalities matrices should have "
11919 "same number of columns", goto error);
11921 total = 1 + isl_space_dim(dim, isl_dim_all);
11923 if (eq->n_col < total)
11924 isl_die(dim->ctx, isl_error_invalid,
11925 "number of columns too small", goto error);
11927 extra = eq->n_col - total;
11929 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11930 eq->n_row, ineq->n_row);
11931 if (!bmap)
11932 goto error;
11933 for (i = 0; i < extra; ++i) {
11934 k = isl_basic_map_alloc_div(bmap);
11935 if (k < 0)
11936 goto error;
11937 isl_int_set_si(bmap->div[k][0], 0);
11939 for (i = 0; i < eq->n_row; ++i) {
11940 l = isl_basic_map_alloc_equality(bmap);
11941 if (l < 0)
11942 goto error;
11943 for (j = 0, pos = 0; j < 5; ++j) {
11944 int off = isl_basic_map_offset(bmap, c[j]);
11945 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11946 isl_int_set(bmap->eq[l][off + k],
11947 eq->row[i][pos]);
11948 ++pos;
11952 for (i = 0; i < ineq->n_row; ++i) {
11953 l = isl_basic_map_alloc_inequality(bmap);
11954 if (l < 0)
11955 goto error;
11956 for (j = 0, pos = 0; j < 5; ++j) {
11957 int off = isl_basic_map_offset(bmap, c[j]);
11958 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11959 isl_int_set(bmap->ineq[l][off + k],
11960 ineq->row[i][pos]);
11961 ++pos;
11966 isl_space_free(dim);
11967 isl_mat_free(eq);
11968 isl_mat_free(ineq);
11970 bmap = isl_basic_map_simplify(bmap);
11971 return isl_basic_map_finalize(bmap);
11972 error:
11973 isl_space_free(dim);
11974 isl_mat_free(eq);
11975 isl_mat_free(ineq);
11976 return NULL;
11979 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11980 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11981 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11983 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11984 c1, c2, c3, c4, isl_dim_in);
11987 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11988 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11989 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11991 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11992 c1, c2, c3, c4, isl_dim_in);
11995 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11996 __isl_take isl_space *dim,
11997 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11998 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12000 isl_basic_map *bmap;
12001 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
12002 c1, c2, c3, c4, isl_dim_in);
12003 return bset_from_bmap(bmap);
12006 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12008 if (!bmap)
12009 return isl_bool_error;
12011 return isl_space_can_zip(bmap->dim);
12014 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12016 if (!map)
12017 return isl_bool_error;
12019 return isl_space_can_zip(map->dim);
12022 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12023 * (A -> C) -> (B -> D).
12025 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12027 unsigned pos;
12028 unsigned n1;
12029 unsigned n2;
12031 if (!bmap)
12032 return NULL;
12034 if (!isl_basic_map_can_zip(bmap))
12035 isl_die(bmap->ctx, isl_error_invalid,
12036 "basic map cannot be zipped", goto error);
12037 pos = isl_basic_map_offset(bmap, isl_dim_in) +
12038 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12039 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12040 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12041 bmap = isl_basic_map_cow(bmap);
12042 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12043 if (!bmap)
12044 return NULL;
12045 bmap->dim = isl_space_zip(bmap->dim);
12046 if (!bmap->dim)
12047 goto error;
12048 bmap = isl_basic_map_mark_final(bmap);
12049 return bmap;
12050 error:
12051 isl_basic_map_free(bmap);
12052 return NULL;
12055 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12056 * (A -> C) -> (B -> D).
12058 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12060 int i;
12062 if (!map)
12063 return NULL;
12065 if (!isl_map_can_zip(map))
12066 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12067 goto error);
12069 map = isl_map_cow(map);
12070 if (!map)
12071 return NULL;
12073 for (i = 0; i < map->n; ++i) {
12074 map->p[i] = isl_basic_map_zip(map->p[i]);
12075 if (!map->p[i])
12076 goto error;
12079 map->dim = isl_space_zip(map->dim);
12080 if (!map->dim)
12081 goto error;
12083 return map;
12084 error:
12085 isl_map_free(map);
12086 return NULL;
12089 /* Can we apply isl_basic_map_curry to "bmap"?
12090 * That is, does it have a nested relation in its domain?
12092 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12094 if (!bmap)
12095 return isl_bool_error;
12097 return isl_space_can_curry(bmap->dim);
12100 /* Can we apply isl_map_curry to "map"?
12101 * That is, does it have a nested relation in its domain?
12103 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12105 if (!map)
12106 return isl_bool_error;
12108 return isl_space_can_curry(map->dim);
12111 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12112 * A -> (B -> C).
12114 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12117 if (!bmap)
12118 return NULL;
12120 if (!isl_basic_map_can_curry(bmap))
12121 isl_die(bmap->ctx, isl_error_invalid,
12122 "basic map cannot be curried", goto error);
12123 bmap = isl_basic_map_cow(bmap);
12124 if (!bmap)
12125 return NULL;
12126 bmap->dim = isl_space_curry(bmap->dim);
12127 if (!bmap->dim)
12128 goto error;
12129 bmap = isl_basic_map_mark_final(bmap);
12130 return bmap;
12131 error:
12132 isl_basic_map_free(bmap);
12133 return NULL;
12136 /* Given a map (A -> B) -> C, return the corresponding map
12137 * A -> (B -> C).
12139 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12141 return isl_map_change_space(map, &isl_map_can_curry,
12142 "map cannot be curried", &isl_space_curry);
12145 /* Can isl_map_range_curry be applied to "map"?
12146 * That is, does it have a nested relation in its range,
12147 * the domain of which is itself a nested relation?
12149 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12151 if (!map)
12152 return isl_bool_error;
12154 return isl_space_can_range_curry(map->dim);
12157 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12158 * A -> (B -> (C -> D)).
12160 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12162 return isl_map_change_space(map, &isl_map_can_range_curry,
12163 "map range cannot be curried",
12164 &isl_space_range_curry);
12167 /* Can we apply isl_basic_map_uncurry to "bmap"?
12168 * That is, does it have a nested relation in its domain?
12170 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12172 if (!bmap)
12173 return isl_bool_error;
12175 return isl_space_can_uncurry(bmap->dim);
12178 /* Can we apply isl_map_uncurry to "map"?
12179 * That is, does it have a nested relation in its domain?
12181 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12183 if (!map)
12184 return isl_bool_error;
12186 return isl_space_can_uncurry(map->dim);
12189 /* Given a basic map A -> (B -> C), return the corresponding basic map
12190 * (A -> B) -> C.
12192 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12195 if (!bmap)
12196 return NULL;
12198 if (!isl_basic_map_can_uncurry(bmap))
12199 isl_die(bmap->ctx, isl_error_invalid,
12200 "basic map cannot be uncurried",
12201 return isl_basic_map_free(bmap));
12202 bmap = isl_basic_map_cow(bmap);
12203 if (!bmap)
12204 return NULL;
12205 bmap->dim = isl_space_uncurry(bmap->dim);
12206 if (!bmap->dim)
12207 return isl_basic_map_free(bmap);
12208 bmap = isl_basic_map_mark_final(bmap);
12209 return bmap;
12212 /* Given a map A -> (B -> C), return the corresponding map
12213 * (A -> B) -> C.
12215 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12217 return isl_map_change_space(map, &isl_map_can_uncurry,
12218 "map cannot be uncurried", &isl_space_uncurry);
12221 /* Construct a basic map mapping the domain of the affine expression
12222 * to a one-dimensional range prescribed by the affine expression.
12223 * If "rational" is set, then construct a rational basic map.
12225 * A NaN affine expression cannot be converted to a basic map.
12227 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
12228 __isl_take isl_aff *aff, int rational)
12230 int k;
12231 int pos;
12232 isl_bool is_nan;
12233 isl_local_space *ls;
12234 isl_basic_map *bmap = NULL;
12236 if (!aff)
12237 return NULL;
12238 is_nan = isl_aff_is_nan(aff);
12239 if (is_nan < 0)
12240 goto error;
12241 if (is_nan)
12242 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
12243 "cannot convert NaN", goto error);
12245 ls = isl_aff_get_local_space(aff);
12246 bmap = isl_basic_map_from_local_space(ls);
12247 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
12248 k = isl_basic_map_alloc_equality(bmap);
12249 if (k < 0)
12250 goto error;
12252 pos = isl_basic_map_offset(bmap, isl_dim_out);
12253 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
12254 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
12255 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
12256 aff->v->size - (pos + 1));
12258 isl_aff_free(aff);
12259 if (rational)
12260 bmap = isl_basic_map_set_rational(bmap);
12261 bmap = isl_basic_map_gauss(bmap, NULL);
12262 bmap = isl_basic_map_finalize(bmap);
12263 return bmap;
12264 error:
12265 isl_aff_free(aff);
12266 isl_basic_map_free(bmap);
12267 return NULL;
12270 /* Construct a basic map mapping the domain of the affine expression
12271 * to a one-dimensional range prescribed by the affine expression.
12273 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
12275 return isl_basic_map_from_aff2(aff, 0);
12278 /* Construct a map mapping the domain of the affine expression
12279 * to a one-dimensional range prescribed by the affine expression.
12281 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
12283 isl_basic_map *bmap;
12285 bmap = isl_basic_map_from_aff(aff);
12286 return isl_map_from_basic_map(bmap);
12289 /* Construct a basic map mapping the domain the multi-affine expression
12290 * to its range, with each dimension in the range equated to the
12291 * corresponding affine expression.
12292 * If "rational" is set, then construct a rational basic map.
12294 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12295 __isl_take isl_multi_aff *maff, int rational)
12297 int i;
12298 isl_space *space;
12299 isl_basic_map *bmap;
12301 if (!maff)
12302 return NULL;
12304 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12305 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12306 "invalid space", goto error);
12308 space = isl_space_domain(isl_multi_aff_get_space(maff));
12309 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12310 if (rational)
12311 bmap = isl_basic_map_set_rational(bmap);
12313 for (i = 0; i < maff->n; ++i) {
12314 isl_aff *aff;
12315 isl_basic_map *bmap_i;
12317 aff = isl_aff_copy(maff->u.p[i]);
12318 bmap_i = isl_basic_map_from_aff2(aff, rational);
12320 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12323 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12325 isl_multi_aff_free(maff);
12326 return bmap;
12327 error:
12328 isl_multi_aff_free(maff);
12329 return NULL;
12332 /* Construct a basic map mapping the domain the multi-affine expression
12333 * to its range, with each dimension in the range equated to the
12334 * corresponding affine expression.
12336 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12337 __isl_take isl_multi_aff *ma)
12339 return isl_basic_map_from_multi_aff2(ma, 0);
12342 /* Construct a map mapping the domain the multi-affine expression
12343 * to its range, with each dimension in the range equated to the
12344 * corresponding affine expression.
12346 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12348 isl_basic_map *bmap;
12350 bmap = isl_basic_map_from_multi_aff(maff);
12351 return isl_map_from_basic_map(bmap);
12354 /* Construct a basic map mapping a domain in the given space to
12355 * to an n-dimensional range, with n the number of elements in the list,
12356 * where each coordinate in the range is prescribed by the
12357 * corresponding affine expression.
12358 * The domains of all affine expressions in the list are assumed to match
12359 * domain_dim.
12361 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12362 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12364 int i;
12365 isl_space *dim;
12366 isl_basic_map *bmap;
12368 if (!list)
12369 return NULL;
12371 dim = isl_space_from_domain(domain_dim);
12372 bmap = isl_basic_map_universe(dim);
12374 for (i = 0; i < list->n; ++i) {
12375 isl_aff *aff;
12376 isl_basic_map *bmap_i;
12378 aff = isl_aff_copy(list->p[i]);
12379 bmap_i = isl_basic_map_from_aff(aff);
12381 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12384 isl_aff_list_free(list);
12385 return bmap;
12388 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12389 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12391 return isl_map_equate(set, type1, pos1, type2, pos2);
12394 /* Construct a basic map where the given dimensions are equal to each other.
12396 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12397 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12399 isl_basic_map *bmap = NULL;
12400 int i;
12402 if (!space)
12403 return NULL;
12405 if (pos1 >= isl_space_dim(space, type1))
12406 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12407 "index out of bounds", goto error);
12408 if (pos2 >= isl_space_dim(space, type2))
12409 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12410 "index out of bounds", goto error);
12412 if (type1 == type2 && pos1 == pos2)
12413 return isl_basic_map_universe(space);
12415 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12416 i = isl_basic_map_alloc_equality(bmap);
12417 if (i < 0)
12418 goto error;
12419 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12420 pos1 += isl_basic_map_offset(bmap, type1);
12421 pos2 += isl_basic_map_offset(bmap, type2);
12422 isl_int_set_si(bmap->eq[i][pos1], -1);
12423 isl_int_set_si(bmap->eq[i][pos2], 1);
12424 bmap = isl_basic_map_finalize(bmap);
12425 isl_space_free(space);
12426 return bmap;
12427 error:
12428 isl_space_free(space);
12429 isl_basic_map_free(bmap);
12430 return NULL;
12433 /* Add a constraint imposing that the given two dimensions are equal.
12435 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12436 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12438 isl_basic_map *eq;
12440 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12442 bmap = isl_basic_map_intersect(bmap, eq);
12444 return bmap;
12447 /* Add a constraint imposing that the given two dimensions are equal.
12449 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12450 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12452 isl_basic_map *bmap;
12454 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12456 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12458 return map;
12461 /* Add a constraint imposing that the given two dimensions have opposite values.
12463 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12464 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12466 isl_basic_map *bmap = NULL;
12467 int i;
12469 if (!map)
12470 return NULL;
12472 if (pos1 >= isl_map_dim(map, type1))
12473 isl_die(map->ctx, isl_error_invalid,
12474 "index out of bounds", goto error);
12475 if (pos2 >= isl_map_dim(map, type2))
12476 isl_die(map->ctx, isl_error_invalid,
12477 "index out of bounds", goto error);
12479 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12480 i = isl_basic_map_alloc_equality(bmap);
12481 if (i < 0)
12482 goto error;
12483 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12484 pos1 += isl_basic_map_offset(bmap, type1);
12485 pos2 += isl_basic_map_offset(bmap, type2);
12486 isl_int_set_si(bmap->eq[i][pos1], 1);
12487 isl_int_set_si(bmap->eq[i][pos2], 1);
12488 bmap = isl_basic_map_finalize(bmap);
12490 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12492 return map;
12493 error:
12494 isl_basic_map_free(bmap);
12495 isl_map_free(map);
12496 return NULL;
12499 /* Construct a constraint imposing that the value of the first dimension is
12500 * greater than or equal to that of the second.
12502 static __isl_give isl_constraint *constraint_order_ge(
12503 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12504 enum isl_dim_type type2, int pos2)
12506 isl_constraint *c;
12508 if (!space)
12509 return NULL;
12511 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12513 if (pos1 >= isl_constraint_dim(c, type1))
12514 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12515 "index out of bounds", return isl_constraint_free(c));
12516 if (pos2 >= isl_constraint_dim(c, type2))
12517 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12518 "index out of bounds", return isl_constraint_free(c));
12520 if (type1 == type2 && pos1 == pos2)
12521 return c;
12523 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12524 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12526 return c;
12529 /* Add a constraint imposing that the value of the first dimension is
12530 * greater than or equal to that of the second.
12532 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12533 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12535 isl_constraint *c;
12536 isl_space *space;
12538 if (type1 == type2 && pos1 == pos2)
12539 return bmap;
12540 space = isl_basic_map_get_space(bmap);
12541 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12542 bmap = isl_basic_map_add_constraint(bmap, c);
12544 return bmap;
12547 /* Add a constraint imposing that the value of the first dimension is
12548 * greater than or equal to that of the second.
12550 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12551 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12553 isl_constraint *c;
12554 isl_space *space;
12556 if (type1 == type2 && pos1 == pos2)
12557 return map;
12558 space = isl_map_get_space(map);
12559 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12560 map = isl_map_add_constraint(map, c);
12562 return map;
12565 /* Add a constraint imposing that the value of the first dimension is
12566 * less than or equal to that of the second.
12568 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12569 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12571 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12574 /* Construct a basic map where the value of the first dimension is
12575 * greater than that of the second.
12577 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12578 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12580 isl_basic_map *bmap = NULL;
12581 int i;
12583 if (!space)
12584 return NULL;
12586 if (pos1 >= isl_space_dim(space, type1))
12587 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12588 "index out of bounds", goto error);
12589 if (pos2 >= isl_space_dim(space, type2))
12590 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12591 "index out of bounds", goto error);
12593 if (type1 == type2 && pos1 == pos2)
12594 return isl_basic_map_empty(space);
12596 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12597 i = isl_basic_map_alloc_inequality(bmap);
12598 if (i < 0)
12599 return isl_basic_map_free(bmap);
12600 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12601 pos1 += isl_basic_map_offset(bmap, type1);
12602 pos2 += isl_basic_map_offset(bmap, type2);
12603 isl_int_set_si(bmap->ineq[i][pos1], 1);
12604 isl_int_set_si(bmap->ineq[i][pos2], -1);
12605 isl_int_set_si(bmap->ineq[i][0], -1);
12606 bmap = isl_basic_map_finalize(bmap);
12608 return bmap;
12609 error:
12610 isl_space_free(space);
12611 isl_basic_map_free(bmap);
12612 return NULL;
12615 /* Add a constraint imposing that the value of the first dimension is
12616 * greater than that of the second.
12618 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12619 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12621 isl_basic_map *gt;
12623 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12625 bmap = isl_basic_map_intersect(bmap, gt);
12627 return bmap;
12630 /* Add a constraint imposing that the value of the first dimension is
12631 * greater than that of the second.
12633 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12634 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12636 isl_basic_map *bmap;
12638 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12640 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12642 return map;
12645 /* Add a constraint imposing that the value of the first dimension is
12646 * smaller than that of the second.
12648 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12649 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12651 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12654 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12655 int pos)
12657 isl_aff *div;
12658 isl_local_space *ls;
12660 if (!bmap)
12661 return NULL;
12663 if (!isl_basic_map_divs_known(bmap))
12664 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12665 "some divs are unknown", return NULL);
12667 ls = isl_basic_map_get_local_space(bmap);
12668 div = isl_local_space_get_div(ls, pos);
12669 isl_local_space_free(ls);
12671 return div;
12674 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12675 int pos)
12677 return isl_basic_map_get_div(bset, pos);
12680 /* Plug in "subs" for dimension "type", "pos" of "bset".
12682 * Let i be the dimension to replace and let "subs" be of the form
12684 * f/d
12686 * Any integer division with a non-zero coefficient for i,
12688 * floor((a i + g)/m)
12690 * is replaced by
12692 * floor((a f + d g)/(m d))
12694 * Constraints of the form
12696 * a i + g
12698 * are replaced by
12700 * a f + d g
12702 * We currently require that "subs" is an integral expression.
12703 * Handling rational expressions may require us to add stride constraints
12704 * as we do in isl_basic_set_preimage_multi_aff.
12706 __isl_give isl_basic_set *isl_basic_set_substitute(
12707 __isl_take isl_basic_set *bset,
12708 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12710 int i;
12711 isl_int v;
12712 isl_ctx *ctx;
12714 if (bset && isl_basic_set_plain_is_empty(bset))
12715 return bset;
12717 bset = isl_basic_set_cow(bset);
12718 if (!bset || !subs)
12719 goto error;
12721 ctx = isl_basic_set_get_ctx(bset);
12722 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12723 isl_die(ctx, isl_error_invalid,
12724 "spaces don't match", goto error);
12725 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12726 isl_die(ctx, isl_error_unsupported,
12727 "cannot handle divs yet", goto error);
12728 if (!isl_int_is_one(subs->v->el[0]))
12729 isl_die(ctx, isl_error_invalid,
12730 "can only substitute integer expressions", goto error);
12732 pos += isl_basic_set_offset(bset, type);
12734 isl_int_init(v);
12736 for (i = 0; i < bset->n_eq; ++i) {
12737 if (isl_int_is_zero(bset->eq[i][pos]))
12738 continue;
12739 isl_int_set(v, bset->eq[i][pos]);
12740 isl_int_set_si(bset->eq[i][pos], 0);
12741 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12742 v, subs->v->el + 1, subs->v->size - 1);
12745 for (i = 0; i < bset->n_ineq; ++i) {
12746 if (isl_int_is_zero(bset->ineq[i][pos]))
12747 continue;
12748 isl_int_set(v, bset->ineq[i][pos]);
12749 isl_int_set_si(bset->ineq[i][pos], 0);
12750 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12751 v, subs->v->el + 1, subs->v->size - 1);
12754 for (i = 0; i < bset->n_div; ++i) {
12755 if (isl_int_is_zero(bset->div[i][1 + pos]))
12756 continue;
12757 isl_int_set(v, bset->div[i][1 + pos]);
12758 isl_int_set_si(bset->div[i][1 + pos], 0);
12759 isl_seq_combine(bset->div[i] + 1,
12760 subs->v->el[0], bset->div[i] + 1,
12761 v, subs->v->el + 1, subs->v->size - 1);
12762 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12765 isl_int_clear(v);
12767 bset = isl_basic_set_simplify(bset);
12768 return isl_basic_set_finalize(bset);
12769 error:
12770 isl_basic_set_free(bset);
12771 return NULL;
12774 /* Plug in "subs" for dimension "type", "pos" of "set".
12776 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12777 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12779 int i;
12781 if (set && isl_set_plain_is_empty(set))
12782 return set;
12784 set = isl_set_cow(set);
12785 if (!set || !subs)
12786 goto error;
12788 for (i = set->n - 1; i >= 0; --i) {
12789 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12790 if (remove_if_empty(set, i) < 0)
12791 goto error;
12794 return set;
12795 error:
12796 isl_set_free(set);
12797 return NULL;
12800 /* Check if the range of "ma" is compatible with the domain or range
12801 * (depending on "type") of "bmap".
12803 static isl_stat check_basic_map_compatible_range_multi_aff(
12804 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12805 __isl_keep isl_multi_aff *ma)
12807 isl_bool m;
12808 isl_space *ma_space;
12810 ma_space = isl_multi_aff_get_space(ma);
12812 m = isl_space_has_equal_params(bmap->dim, ma_space);
12813 if (m < 0)
12814 goto error;
12815 if (!m)
12816 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12817 "parameters don't match", goto error);
12818 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12819 if (m < 0)
12820 goto error;
12821 if (!m)
12822 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12823 "spaces don't match", goto error);
12825 isl_space_free(ma_space);
12826 return isl_stat_ok;
12827 error:
12828 isl_space_free(ma_space);
12829 return isl_stat_error;
12832 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12833 * coefficients before the transformed range of dimensions,
12834 * the "n_after" coefficients after the transformed range of dimensions
12835 * and the coefficients of the other divs in "bmap".
12837 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12838 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12840 int i;
12841 int n_param;
12842 int n_set;
12843 isl_local_space *ls;
12845 if (n_div == 0)
12846 return 0;
12848 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12849 if (!ls)
12850 return -1;
12852 n_param = isl_local_space_dim(ls, isl_dim_param);
12853 n_set = isl_local_space_dim(ls, isl_dim_set);
12854 for (i = 0; i < n_div; ++i) {
12855 int o_bmap = 0, o_ls = 0;
12857 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12858 o_bmap += 1 + 1 + n_param;
12859 o_ls += 1 + 1 + n_param;
12860 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12861 o_bmap += n_before;
12862 isl_seq_cpy(bmap->div[i] + o_bmap,
12863 ls->div->row[i] + o_ls, n_set);
12864 o_bmap += n_set;
12865 o_ls += n_set;
12866 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12867 o_bmap += n_after;
12868 isl_seq_cpy(bmap->div[i] + o_bmap,
12869 ls->div->row[i] + o_ls, n_div);
12870 o_bmap += n_div;
12871 o_ls += n_div;
12872 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12873 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12874 goto error;
12877 isl_local_space_free(ls);
12878 return 0;
12879 error:
12880 isl_local_space_free(ls);
12881 return -1;
12884 /* How many stride constraints does "ma" enforce?
12885 * That is, how many of the affine expressions have a denominator
12886 * different from one?
12888 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12890 int i;
12891 int strides = 0;
12893 for (i = 0; i < ma->n; ++i)
12894 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12895 strides++;
12897 return strides;
12900 /* For each affine expression in ma of the form
12902 * x_i = (f_i y + h_i)/m_i
12904 * with m_i different from one, add a constraint to "bmap"
12905 * of the form
12907 * f_i y + h_i = m_i alpha_i
12909 * with alpha_i an additional existentially quantified variable.
12911 * The input variables of "ma" correspond to a subset of the variables
12912 * of "bmap". There are "n_before" variables in "bmap" before this
12913 * subset and "n_after" variables after this subset.
12914 * The integer divisions of the affine expressions in "ma" are assumed
12915 * to have been aligned. There are "n_div_ma" of them and
12916 * they appear first in "bmap", straight after the "n_after" variables.
12918 static __isl_give isl_basic_map *add_ma_strides(
12919 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12920 int n_before, int n_after, int n_div_ma)
12922 int i, k;
12923 int div;
12924 int total;
12925 int n_param;
12926 int n_in;
12928 total = isl_basic_map_total_dim(bmap);
12929 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12930 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12931 for (i = 0; i < ma->n; ++i) {
12932 int o_bmap = 0, o_ma = 1;
12934 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12935 continue;
12936 div = isl_basic_map_alloc_div(bmap);
12937 k = isl_basic_map_alloc_equality(bmap);
12938 if (div < 0 || k < 0)
12939 goto error;
12940 isl_int_set_si(bmap->div[div][0], 0);
12941 isl_seq_cpy(bmap->eq[k] + o_bmap,
12942 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12943 o_bmap += 1 + n_param;
12944 o_ma += 1 + n_param;
12945 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12946 o_bmap += n_before;
12947 isl_seq_cpy(bmap->eq[k] + o_bmap,
12948 ma->u.p[i]->v->el + o_ma, n_in);
12949 o_bmap += n_in;
12950 o_ma += n_in;
12951 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12952 o_bmap += n_after;
12953 isl_seq_cpy(bmap->eq[k] + o_bmap,
12954 ma->u.p[i]->v->el + o_ma, n_div_ma);
12955 o_bmap += n_div_ma;
12956 o_ma += n_div_ma;
12957 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12958 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12959 total++;
12962 return bmap;
12963 error:
12964 isl_basic_map_free(bmap);
12965 return NULL;
12968 /* Replace the domain or range space (depending on "type) of "space" by "set".
12970 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12971 enum isl_dim_type type, __isl_take isl_space *set)
12973 if (type == isl_dim_in) {
12974 space = isl_space_range(space);
12975 space = isl_space_map_from_domain_and_range(set, space);
12976 } else {
12977 space = isl_space_domain(space);
12978 space = isl_space_map_from_domain_and_range(space, set);
12981 return space;
12984 /* Compute the preimage of the domain or range (depending on "type")
12985 * of "bmap" under the function represented by "ma".
12986 * In other words, plug in "ma" in the domain or range of "bmap".
12987 * The result is a basic map that lives in the same space as "bmap"
12988 * except that the domain or range has been replaced by
12989 * the domain space of "ma".
12991 * If bmap is represented by
12993 * A(p) + S u + B x + T v + C(divs) >= 0,
12995 * where u and x are input and output dimensions if type == isl_dim_out
12996 * while x and v are input and output dimensions if type == isl_dim_in,
12997 * and ma is represented by
12999 * x = D(p) + F(y) + G(divs')
13001 * then the result is
13003 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13005 * The divs in the input set are similarly adjusted.
13006 * In particular
13008 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13010 * becomes
13012 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13013 * B_i G(divs') + c_i(divs))/n_i)
13015 * If bmap is not a rational map and if F(y) involves any denominators
13017 * x_i = (f_i y + h_i)/m_i
13019 * then additional constraints are added to ensure that we only
13020 * map back integer points. That is we enforce
13022 * f_i y + h_i = m_i alpha_i
13024 * with alpha_i an additional existentially quantified variable.
13026 * We first copy over the divs from "ma".
13027 * Then we add the modified constraints and divs from "bmap".
13028 * Finally, we add the stride constraints, if needed.
13030 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13031 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13032 __isl_take isl_multi_aff *ma)
13034 int i, k;
13035 isl_space *space;
13036 isl_basic_map *res = NULL;
13037 int n_before, n_after, n_div_bmap, n_div_ma;
13038 isl_int f, c1, c2, g;
13039 isl_bool rational;
13040 int strides;
13042 isl_int_init(f);
13043 isl_int_init(c1);
13044 isl_int_init(c2);
13045 isl_int_init(g);
13047 ma = isl_multi_aff_align_divs(ma);
13048 if (!bmap || !ma)
13049 goto error;
13050 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13051 goto error;
13053 if (type == isl_dim_in) {
13054 n_before = 0;
13055 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13056 } else {
13057 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13058 n_after = 0;
13060 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13061 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13063 space = isl_multi_aff_get_domain_space(ma);
13064 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13065 rational = isl_basic_map_is_rational(bmap);
13066 strides = rational ? 0 : multi_aff_strides(ma);
13067 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13068 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13069 if (rational)
13070 res = isl_basic_map_set_rational(res);
13072 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13073 if (isl_basic_map_alloc_div(res) < 0)
13074 goto error;
13076 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
13077 goto error;
13079 for (i = 0; i < bmap->n_eq; ++i) {
13080 k = isl_basic_map_alloc_equality(res);
13081 if (k < 0)
13082 goto error;
13083 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13084 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13087 for (i = 0; i < bmap->n_ineq; ++i) {
13088 k = isl_basic_map_alloc_inequality(res);
13089 if (k < 0)
13090 goto error;
13091 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13092 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13095 for (i = 0; i < bmap->n_div; ++i) {
13096 if (isl_int_is_zero(bmap->div[i][0])) {
13097 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13098 continue;
13100 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13101 n_before, n_after, n_div_ma, n_div_bmap,
13102 f, c1, c2, g, 1);
13105 if (strides)
13106 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13108 isl_int_clear(f);
13109 isl_int_clear(c1);
13110 isl_int_clear(c2);
13111 isl_int_clear(g);
13112 isl_basic_map_free(bmap);
13113 isl_multi_aff_free(ma);
13114 res = isl_basic_map_simplify(res);
13115 return isl_basic_map_finalize(res);
13116 error:
13117 isl_int_clear(f);
13118 isl_int_clear(c1);
13119 isl_int_clear(c2);
13120 isl_int_clear(g);
13121 isl_basic_map_free(bmap);
13122 isl_multi_aff_free(ma);
13123 isl_basic_map_free(res);
13124 return NULL;
13127 /* Compute the preimage of "bset" under the function represented by "ma".
13128 * In other words, plug in "ma" in "bset". The result is a basic set
13129 * that lives in the domain space of "ma".
13131 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13132 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13134 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13137 /* Compute the preimage of the domain of "bmap" under the function
13138 * represented by "ma".
13139 * In other words, plug in "ma" in the domain of "bmap".
13140 * The result is a basic map that lives in the same space as "bmap"
13141 * except that the domain has been replaced by the domain space of "ma".
13143 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13144 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13146 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13149 /* Compute the preimage of the range of "bmap" under the function
13150 * represented by "ma".
13151 * In other words, plug in "ma" in the range of "bmap".
13152 * The result is a basic map that lives in the same space as "bmap"
13153 * except that the range has been replaced by the domain space of "ma".
13155 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13156 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13158 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13161 /* Check if the range of "ma" is compatible with the domain or range
13162 * (depending on "type") of "map".
13163 * Return isl_stat_error if anything is wrong.
13165 static isl_stat check_map_compatible_range_multi_aff(
13166 __isl_keep isl_map *map, enum isl_dim_type type,
13167 __isl_keep isl_multi_aff *ma)
13169 isl_bool m;
13170 isl_space *ma_space;
13172 ma_space = isl_multi_aff_get_space(ma);
13173 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13174 isl_space_free(ma_space);
13175 if (m < 0)
13176 return isl_stat_error;
13177 if (!m)
13178 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13179 "spaces don't match", return isl_stat_error);
13180 return isl_stat_ok;
13183 /* Compute the preimage of the domain or range (depending on "type")
13184 * of "map" under the function represented by "ma".
13185 * In other words, plug in "ma" in the domain or range of "map".
13186 * The result is a map that lives in the same space as "map"
13187 * except that the domain or range has been replaced by
13188 * the domain space of "ma".
13190 * The parameters are assumed to have been aligned.
13192 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13193 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13195 int i;
13196 isl_space *space;
13198 map = isl_map_cow(map);
13199 ma = isl_multi_aff_align_divs(ma);
13200 if (!map || !ma)
13201 goto error;
13202 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13203 goto error;
13205 for (i = 0; i < map->n; ++i) {
13206 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13207 isl_multi_aff_copy(ma));
13208 if (!map->p[i])
13209 goto error;
13212 space = isl_multi_aff_get_domain_space(ma);
13213 space = isl_space_set(isl_map_get_space(map), type, space);
13215 isl_space_free(map->dim);
13216 map->dim = space;
13217 if (!map->dim)
13218 goto error;
13220 isl_multi_aff_free(ma);
13221 if (map->n > 1)
13222 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13223 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13224 return map;
13225 error:
13226 isl_multi_aff_free(ma);
13227 isl_map_free(map);
13228 return NULL;
13231 /* Compute the preimage of the domain or range (depending on "type")
13232 * of "map" under the function represented by "ma".
13233 * In other words, plug in "ma" in the domain or range of "map".
13234 * The result is a map that lives in the same space as "map"
13235 * except that the domain or range has been replaced by
13236 * the domain space of "ma".
13238 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13239 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13241 isl_bool aligned;
13243 if (!map || !ma)
13244 goto error;
13246 aligned = isl_map_space_has_equal_params(map, ma->space);
13247 if (aligned < 0)
13248 goto error;
13249 if (aligned)
13250 return map_preimage_multi_aff(map, type, ma);
13252 if (isl_map_check_named_params(map) < 0)
13253 goto error;
13254 if (!isl_space_has_named_params(ma->space))
13255 isl_die(map->ctx, isl_error_invalid,
13256 "unaligned unnamed parameters", goto error);
13257 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13258 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13260 return map_preimage_multi_aff(map, type, ma);
13261 error:
13262 isl_multi_aff_free(ma);
13263 return isl_map_free(map);
13266 /* Compute the preimage of "set" under the function represented by "ma".
13267 * In other words, plug in "ma" in "set". The result is a set
13268 * that lives in the domain space of "ma".
13270 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13271 __isl_take isl_multi_aff *ma)
13273 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13276 /* Compute the preimage of the domain of "map" under the function
13277 * represented by "ma".
13278 * In other words, plug in "ma" in the domain of "map".
13279 * The result is a map that lives in the same space as "map"
13280 * except that the domain has been replaced by the domain space of "ma".
13282 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13283 __isl_take isl_multi_aff *ma)
13285 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13288 /* Compute the preimage of the range of "map" under the function
13289 * represented by "ma".
13290 * In other words, plug in "ma" in the range of "map".
13291 * The result is a map that lives in the same space as "map"
13292 * except that the range has been replaced by the domain space of "ma".
13294 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13295 __isl_take isl_multi_aff *ma)
13297 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13300 /* Compute the preimage of "map" under the function represented by "pma".
13301 * In other words, plug in "pma" in the domain or range of "map".
13302 * The result is a map that lives in the same space as "map",
13303 * except that the space of type "type" has been replaced by
13304 * the domain space of "pma".
13306 * The parameters of "map" and "pma" are assumed to have been aligned.
13308 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13309 __isl_take isl_map *map, enum isl_dim_type type,
13310 __isl_take isl_pw_multi_aff *pma)
13312 int i;
13313 isl_map *res;
13315 if (!pma)
13316 goto error;
13318 if (pma->n == 0) {
13319 isl_pw_multi_aff_free(pma);
13320 res = isl_map_empty(isl_map_get_space(map));
13321 isl_map_free(map);
13322 return res;
13325 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13326 isl_multi_aff_copy(pma->p[0].maff));
13327 if (type == isl_dim_in)
13328 res = isl_map_intersect_domain(res,
13329 isl_map_copy(pma->p[0].set));
13330 else
13331 res = isl_map_intersect_range(res,
13332 isl_map_copy(pma->p[0].set));
13334 for (i = 1; i < pma->n; ++i) {
13335 isl_map *res_i;
13337 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13338 isl_multi_aff_copy(pma->p[i].maff));
13339 if (type == isl_dim_in)
13340 res_i = isl_map_intersect_domain(res_i,
13341 isl_map_copy(pma->p[i].set));
13342 else
13343 res_i = isl_map_intersect_range(res_i,
13344 isl_map_copy(pma->p[i].set));
13345 res = isl_map_union(res, res_i);
13348 isl_pw_multi_aff_free(pma);
13349 isl_map_free(map);
13350 return res;
13351 error:
13352 isl_pw_multi_aff_free(pma);
13353 isl_map_free(map);
13354 return NULL;
13357 /* Compute the preimage of "map" under the function represented by "pma".
13358 * In other words, plug in "pma" in the domain or range of "map".
13359 * The result is a map that lives in the same space as "map",
13360 * except that the space of type "type" has been replaced by
13361 * the domain space of "pma".
13363 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13364 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13366 isl_bool aligned;
13368 if (!map || !pma)
13369 goto error;
13371 aligned = isl_map_space_has_equal_params(map, pma->dim);
13372 if (aligned < 0)
13373 goto error;
13374 if (aligned)
13375 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13377 if (isl_map_check_named_params(map) < 0)
13378 goto error;
13379 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13380 goto error;
13381 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13382 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13384 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13385 error:
13386 isl_pw_multi_aff_free(pma);
13387 return isl_map_free(map);
13390 /* Compute the preimage of "set" under the function represented by "pma".
13391 * In other words, plug in "pma" in "set". The result is a set
13392 * that lives in the domain space of "pma".
13394 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13395 __isl_take isl_pw_multi_aff *pma)
13397 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13400 /* Compute the preimage of the domain of "map" under the function
13401 * represented by "pma".
13402 * In other words, plug in "pma" in the domain of "map".
13403 * The result is a map that lives in the same space as "map",
13404 * except that domain space has been replaced by the domain space of "pma".
13406 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13407 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13409 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13412 /* Compute the preimage of the range of "map" under the function
13413 * represented by "pma".
13414 * In other words, plug in "pma" in the range of "map".
13415 * The result is a map that lives in the same space as "map",
13416 * except that range space has been replaced by the domain space of "pma".
13418 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13419 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13421 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13424 /* Compute the preimage of "map" under the function represented by "mpa".
13425 * In other words, plug in "mpa" in the domain or range of "map".
13426 * The result is a map that lives in the same space as "map",
13427 * except that the space of type "type" has been replaced by
13428 * the domain space of "mpa".
13430 * If the map does not involve any constraints that refer to the
13431 * dimensions of the substituted space, then the only possible
13432 * effect of "mpa" on the map is to map the space to a different space.
13433 * We create a separate isl_multi_aff to effectuate this change
13434 * in order to avoid spurious splitting of the map along the pieces
13435 * of "mpa".
13436 * If "mpa" has a non-trivial explicit domain, however,
13437 * then the full substitution should be performed.
13439 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13440 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13442 int n;
13443 isl_bool full;
13444 isl_pw_multi_aff *pma;
13446 if (!map || !mpa)
13447 goto error;
13449 n = isl_map_dim(map, type);
13450 full = isl_map_involves_dims(map, type, 0, n);
13451 if (full >= 0 && !full)
13452 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13453 if (full < 0)
13454 goto error;
13455 if (!full) {
13456 isl_space *space;
13457 isl_multi_aff *ma;
13459 space = isl_multi_pw_aff_get_space(mpa);
13460 isl_multi_pw_aff_free(mpa);
13461 ma = isl_multi_aff_zero(space);
13462 return isl_map_preimage_multi_aff(map, type, ma);
13465 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13466 return isl_map_preimage_pw_multi_aff(map, type, pma);
13467 error:
13468 isl_map_free(map);
13469 isl_multi_pw_aff_free(mpa);
13470 return NULL;
13473 /* Compute the preimage of "map" under the function represented by "mpa".
13474 * In other words, plug in "mpa" in the domain "map".
13475 * The result is a map that lives in the same space as "map",
13476 * except that domain space has been replaced by the domain space of "mpa".
13478 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13479 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13481 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13484 /* Compute the preimage of "set" by the function represented by "mpa".
13485 * In other words, plug in "mpa" in "set".
13487 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13488 __isl_take isl_multi_pw_aff *mpa)
13490 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13493 /* Return a copy of the equality constraints of "bset" as a matrix.
13495 __isl_give isl_mat *isl_basic_set_extract_equalities(
13496 __isl_keep isl_basic_set *bset)
13498 isl_ctx *ctx;
13499 unsigned total;
13501 if (!bset)
13502 return NULL;
13504 ctx = isl_basic_set_get_ctx(bset);
13505 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13506 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13509 /* Are the "n" "coefficients" starting at "first" of the integer division
13510 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13511 * to each other?
13512 * The "coefficient" at position 0 is the denominator.
13513 * The "coefficient" at position 1 is the constant term.
13515 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13516 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13517 unsigned first, unsigned n)
13519 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13520 return isl_bool_error;
13521 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13522 return isl_bool_error;
13523 return isl_seq_eq(bmap1->div[pos1] + first,
13524 bmap2->div[pos2] + first, n);
13527 /* Are the integer division expressions at position "pos1" in "bmap1" and
13528 * "pos2" in "bmap2" equal to each other, except that the constant terms
13529 * are different?
13531 isl_bool isl_basic_map_equal_div_expr_except_constant(
13532 __isl_keep isl_basic_map *bmap1, int pos1,
13533 __isl_keep isl_basic_map *bmap2, int pos2)
13535 isl_bool equal;
13536 unsigned total;
13538 if (!bmap1 || !bmap2)
13539 return isl_bool_error;
13540 total = isl_basic_map_total_dim(bmap1);
13541 if (total != isl_basic_map_total_dim(bmap2))
13542 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13543 "incomparable div expressions", return isl_bool_error);
13544 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13545 0, 1);
13546 if (equal < 0 || !equal)
13547 return equal;
13548 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13549 1, 1);
13550 if (equal < 0 || equal)
13551 return isl_bool_not(equal);
13552 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13553 2, total);
13556 /* Replace the numerator of the constant term of the integer division
13557 * expression at position "div" in "bmap" by "value".
13558 * The caller guarantees that this does not change the meaning
13559 * of the input.
13561 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13562 __isl_take isl_basic_map *bmap, int div, int value)
13564 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13565 return isl_basic_map_free(bmap);
13567 isl_int_set_si(bmap->div[div][1], value);
13569 return bmap;
13572 /* Is the point "inner" internal to inequality constraint "ineq"
13573 * of "bset"?
13574 * The point is considered to be internal to the inequality constraint,
13575 * if it strictly lies on the positive side of the inequality constraint,
13576 * or if it lies on the constraint and the constraint is lexico-positive.
13578 static isl_bool is_internal(__isl_keep isl_vec *inner,
13579 __isl_keep isl_basic_set *bset, int ineq)
13581 isl_ctx *ctx;
13582 int pos;
13583 unsigned total;
13585 if (!inner || !bset)
13586 return isl_bool_error;
13588 ctx = isl_basic_set_get_ctx(bset);
13589 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13590 &ctx->normalize_gcd);
13591 if (!isl_int_is_zero(ctx->normalize_gcd))
13592 return isl_int_is_nonneg(ctx->normalize_gcd);
13594 total = isl_basic_set_dim(bset, isl_dim_all);
13595 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13596 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13599 /* Tighten the inequality constraints of "bset" that are outward with respect
13600 * to the point "vec".
13601 * That is, tighten the constraints that are not satisfied by "vec".
13603 * "vec" is a point internal to some superset S of "bset" that is used
13604 * to make the subsets of S disjoint, by tightening one half of the constraints
13605 * that separate two subsets. In particular, the constraints of S
13606 * are all satisfied by "vec" and should not be tightened.
13607 * Of the internal constraints, those that have "vec" on the outside
13608 * are tightened. The shared facet is included in the adjacent subset
13609 * with the opposite constraint.
13610 * For constraints that saturate "vec", this criterion cannot be used
13611 * to determine which of the two sides should be tightened.
13612 * Instead, the sign of the first non-zero coefficient is used
13613 * to make this choice. Note that this second criterion is never used
13614 * on the constraints of S since "vec" is interior to "S".
13616 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13617 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13619 int j;
13621 bset = isl_basic_set_cow(bset);
13622 if (!bset)
13623 return NULL;
13624 for (j = 0; j < bset->n_ineq; ++j) {
13625 isl_bool internal;
13627 internal = is_internal(vec, bset, j);
13628 if (internal < 0)
13629 return isl_basic_set_free(bset);
13630 if (internal)
13631 continue;
13632 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13635 return bset;
13638 /* Replace the variables x of type "type" starting at "first" in "bmap"
13639 * by x' with x = M x' with M the matrix trans.
13640 * That is, replace the corresponding coefficients c by c M.
13642 * The transformation matrix should be a square matrix.
13644 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13645 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13646 __isl_take isl_mat *trans)
13648 unsigned pos;
13650 bmap = isl_basic_map_cow(bmap);
13651 if (!bmap || !trans)
13652 goto error;
13654 if (trans->n_row != trans->n_col)
13655 isl_die(trans->ctx, isl_error_invalid,
13656 "expecting square transformation matrix", goto error);
13657 if (first + trans->n_row > isl_basic_map_dim(bmap, type))
13658 isl_die(trans->ctx, isl_error_invalid,
13659 "oversized transformation matrix", goto error);
13661 pos = isl_basic_map_offset(bmap, type) + first;
13663 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13664 isl_mat_copy(trans)) < 0)
13665 goto error;
13666 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13667 isl_mat_copy(trans)) < 0)
13668 goto error;
13669 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13670 isl_mat_copy(trans)) < 0)
13671 goto error;
13673 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
13674 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13676 isl_mat_free(trans);
13677 return bmap;
13678 error:
13679 isl_mat_free(trans);
13680 isl_basic_map_free(bmap);
13681 return NULL;
13684 /* Replace the variables x of type "type" starting at "first" in "bset"
13685 * by x' with x = M x' with M the matrix trans.
13686 * That is, replace the corresponding coefficients c by c M.
13688 * The transformation matrix should be a square matrix.
13690 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13691 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13692 __isl_take isl_mat *trans)
13694 return isl_basic_map_transform_dims(bset, type, first, trans);