isl_basic_map_product: add memory management annotations
[isl.git] / isl_map.c
blob24ae1871cf7dfae5fe7a81f201d44df445f7e1f2
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
22 #include <string.h>
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_blk.h>
26 #include <isl/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
30 #include <isl_seq.h>
31 #include <isl/set.h>
32 #include <isl/map.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
35 #include <isl_sort.h>
36 #include "isl_tab.h"
37 #include <isl/vec.h>
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
56 switch (type) {
57 case isl_dim_param: return dim->nparam;
58 case isl_dim_in: return dim->n_in;
59 case isl_dim_out: return dim->n_out;
60 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
61 default: return 0;
65 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
67 switch (type) {
68 case isl_dim_param: return 1;
69 case isl_dim_in: return 1 + dim->nparam;
70 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
71 default: return 0;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
76 enum isl_dim_type type)
78 if (!bmap)
79 return 0;
80 switch (type) {
81 case isl_dim_cst: return 1;
82 case isl_dim_param:
83 case isl_dim_in:
84 case isl_dim_out: return isl_space_dim(bmap->dim, type);
85 case isl_dim_div: return bmap->n_div;
86 case isl_dim_all: return isl_basic_map_total_dim(bmap);
87 default: return 0;
91 /* Return the space of "map".
93 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
95 return map ? map->dim : NULL;
98 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
100 return map ? n(map->dim, type) : 0;
103 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
105 return set ? n(set->dim, type) : 0;
108 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
109 enum isl_dim_type type)
111 isl_space *space;
113 if (!bmap)
114 return 0;
116 space = bmap->dim;
117 switch (type) {
118 case isl_dim_cst: return 0;
119 case isl_dim_param: return 1;
120 case isl_dim_in: return 1 + space->nparam;
121 case isl_dim_out: return 1 + space->nparam + space->n_in;
122 case isl_dim_div: return 1 + space->nparam + space->n_in +
123 space->n_out;
124 default: return 0;
128 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
129 enum isl_dim_type type)
131 return isl_basic_map_offset(bset, type);
134 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
136 return pos(map->dim, type);
139 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
140 enum isl_dim_type type)
142 return isl_basic_map_dim(bset, type);
145 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
147 return isl_basic_set_dim(bset, isl_dim_set);
150 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
152 return isl_basic_set_dim(bset, isl_dim_param);
155 unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
157 if (!bset)
158 return 0;
159 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
162 unsigned isl_set_n_dim(__isl_keep isl_set *set)
164 return isl_set_dim(set, isl_dim_set);
167 unsigned isl_set_n_param(__isl_keep isl_set *set)
169 return isl_set_dim(set, isl_dim_param);
172 unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
174 return bmap ? bmap->dim->n_in : 0;
177 unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
179 return bmap ? bmap->dim->n_out : 0;
182 unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
184 return bmap ? bmap->dim->nparam : 0;
187 unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
189 return bmap ? bmap->n_div : 0;
192 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
194 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
197 unsigned isl_map_n_in(const struct isl_map *map)
199 return map ? map->dim->n_in : 0;
202 unsigned isl_map_n_out(const struct isl_map *map)
204 return map ? map->dim->n_out : 0;
207 unsigned isl_map_n_param(const struct isl_map *map)
209 return map ? map->dim->nparam : 0;
212 /* Do "bmap1" and "bmap2" have the same parameters?
214 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
215 __isl_keep isl_basic_map *bmap2)
217 isl_space *space1, *space2;
219 space1 = isl_basic_map_peek_space(bmap1);
220 space2 = isl_basic_map_peek_space(bmap2);
221 return isl_space_has_equal_params(space1, space2);
224 /* Do "map1" and "map2" have the same parameters?
226 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
227 __isl_keep isl_map *map2)
229 isl_space *space1, *space2;
231 space1 = isl_map_peek_space(map1);
232 space2 = isl_map_peek_space(map2);
233 return isl_space_has_equal_params(space1, space2);
236 /* Do "map" and "set" have the same parameters?
238 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
239 __isl_keep isl_set *set)
241 return isl_map_has_equal_params(map, set_to_map(set));
244 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
245 __isl_keep isl_set *set)
247 isl_bool m;
248 if (!map || !set)
249 return isl_bool_error;
250 m = isl_map_has_equal_params(map, set_to_map(set));
251 if (m < 0 || !m)
252 return m;
253 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
254 set->dim, isl_dim_set);
257 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
258 __isl_keep isl_basic_set *bset)
260 isl_bool m;
261 if (!bmap || !bset)
262 return isl_bool_error;
263 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
264 if (m < 0 || !m)
265 return m;
266 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
267 bset->dim, isl_dim_set);
270 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
271 __isl_keep isl_set *set)
273 isl_bool m;
274 if (!map || !set)
275 return isl_bool_error;
276 m = isl_map_has_equal_params(map, set_to_map(set));
277 if (m < 0 || !m)
278 return m;
279 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
280 set->dim, isl_dim_set);
283 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
284 __isl_keep isl_basic_set *bset)
286 isl_bool m;
287 if (!bmap || !bset)
288 return isl_bool_error;
289 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
290 if (m < 0 || !m)
291 return m;
292 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
293 bset->dim, isl_dim_set);
296 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
298 return bmap ? bmap->ctx : NULL;
301 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
303 return bset ? bset->ctx : NULL;
306 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
308 return map ? map->ctx : NULL;
311 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
313 return set ? set->ctx : NULL;
316 /* Return the space of "bmap".
318 __isl_keep isl_space *isl_basic_map_peek_space(
319 __isl_keep const isl_basic_map *bmap)
321 return bmap ? bmap->dim : NULL;
324 /* Return the space of "bset".
326 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
328 return isl_basic_map_peek_space(bset_to_bmap(bset));
331 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
333 return isl_space_copy(isl_basic_map_peek_space(bmap));
336 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
338 return isl_basic_map_get_space(bset_to_bmap(bset));
341 /* Extract the divs in "bmap" as a matrix.
343 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
345 int i;
346 isl_ctx *ctx;
347 isl_mat *div;
348 unsigned total;
349 unsigned cols;
351 if (!bmap)
352 return NULL;
354 ctx = isl_basic_map_get_ctx(bmap);
355 total = isl_space_dim(bmap->dim, isl_dim_all);
356 cols = 1 + 1 + total + bmap->n_div;
357 div = isl_mat_alloc(ctx, bmap->n_div, cols);
358 if (!div)
359 return NULL;
361 for (i = 0; i < bmap->n_div; ++i)
362 isl_seq_cpy(div->row[i], bmap->div[i], cols);
364 return div;
367 /* Extract the divs in "bset" as a matrix.
369 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
371 return isl_basic_map_get_divs(bset);
374 __isl_give isl_local_space *isl_basic_map_get_local_space(
375 __isl_keep isl_basic_map *bmap)
377 isl_mat *div;
379 if (!bmap)
380 return NULL;
382 div = isl_basic_map_get_divs(bmap);
383 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
386 __isl_give isl_local_space *isl_basic_set_get_local_space(
387 __isl_keep isl_basic_set *bset)
389 return isl_basic_map_get_local_space(bset);
392 /* For each known div d = floor(f/m), add the constraints
394 * f - m d >= 0
395 * -(f-(m-1)) + m d >= 0
397 * Do not finalize the result.
399 static __isl_give isl_basic_map *add_known_div_constraints(
400 __isl_take isl_basic_map *bmap)
402 int i;
403 unsigned n_div;
405 if (!bmap)
406 return NULL;
407 n_div = isl_basic_map_dim(bmap, isl_dim_div);
408 if (n_div == 0)
409 return bmap;
410 bmap = isl_basic_map_cow(bmap);
411 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
412 if (!bmap)
413 return NULL;
414 for (i = 0; i < n_div; ++i) {
415 if (isl_int_is_zero(bmap->div[i][0]))
416 continue;
417 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
418 return isl_basic_map_free(bmap);
421 return bmap;
424 __isl_give isl_basic_map *isl_basic_map_from_local_space(
425 __isl_take isl_local_space *ls)
427 int i;
428 int n_div;
429 isl_basic_map *bmap;
431 if (!ls)
432 return NULL;
434 n_div = isl_local_space_dim(ls, isl_dim_div);
435 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
436 n_div, 0, 2 * n_div);
438 for (i = 0; i < n_div; ++i)
439 if (isl_basic_map_alloc_div(bmap) < 0)
440 goto error;
442 for (i = 0; i < n_div; ++i)
443 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
444 bmap = add_known_div_constraints(bmap);
446 isl_local_space_free(ls);
447 return bmap;
448 error:
449 isl_local_space_free(ls);
450 isl_basic_map_free(bmap);
451 return NULL;
454 __isl_give isl_basic_set *isl_basic_set_from_local_space(
455 __isl_take isl_local_space *ls)
457 return isl_basic_map_from_local_space(ls);
460 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
462 return isl_space_copy(isl_map_peek_space(map));
465 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
467 if (!set)
468 return NULL;
469 return isl_space_copy(set->dim);
472 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
473 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
475 bmap = isl_basic_map_cow(bmap);
476 if (!bmap)
477 return NULL;
478 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
479 if (!bmap->dim)
480 goto error;
481 bmap = isl_basic_map_finalize(bmap);
482 return bmap;
483 error:
484 isl_basic_map_free(bmap);
485 return NULL;
488 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
489 __isl_take isl_basic_set *bset, const char *s)
491 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
494 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
495 enum isl_dim_type type)
497 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
500 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
501 enum isl_dim_type type, const char *s)
503 int i;
505 map = isl_map_cow(map);
506 if (!map)
507 return NULL;
509 map->dim = isl_space_set_tuple_name(map->dim, type, s);
510 if (!map->dim)
511 goto error;
513 for (i = 0; i < map->n; ++i) {
514 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
515 if (!map->p[i])
516 goto error;
519 return map;
520 error:
521 isl_map_free(map);
522 return NULL;
525 /* Replace the identifier of the tuple of type "type" by "id".
527 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
528 __isl_take isl_basic_map *bmap,
529 enum isl_dim_type type, __isl_take isl_id *id)
531 bmap = isl_basic_map_cow(bmap);
532 if (!bmap)
533 goto error;
534 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
535 if (!bmap->dim)
536 return isl_basic_map_free(bmap);
537 bmap = isl_basic_map_finalize(bmap);
538 return bmap;
539 error:
540 isl_id_free(id);
541 return NULL;
544 /* Replace the identifier of the tuple by "id".
546 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
547 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
549 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
552 /* Does the input or output tuple have a name?
554 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
556 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
559 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
560 enum isl_dim_type type)
562 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
565 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
566 const char *s)
568 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
569 isl_dim_set, s));
572 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
573 enum isl_dim_type type, __isl_take isl_id *id)
575 map = isl_map_cow(map);
576 if (!map)
577 goto error;
579 map->dim = isl_space_set_tuple_id(map->dim, type, id);
581 return isl_map_reset_space(map, isl_space_copy(map->dim));
582 error:
583 isl_id_free(id);
584 return NULL;
587 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
588 __isl_take isl_id *id)
590 return isl_map_set_tuple_id(set, isl_dim_set, id);
593 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
594 enum isl_dim_type type)
596 map = isl_map_cow(map);
597 if (!map)
598 return NULL;
600 map->dim = isl_space_reset_tuple_id(map->dim, type);
602 return isl_map_reset_space(map, isl_space_copy(map->dim));
605 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
607 return isl_map_reset_tuple_id(set, isl_dim_set);
610 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
612 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
615 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
616 enum isl_dim_type type)
618 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
621 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
623 return isl_map_has_tuple_id(set, isl_dim_set);
626 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
628 return isl_map_get_tuple_id(set, isl_dim_set);
631 /* Does the set tuple have a name?
633 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
635 if (!set)
636 return isl_bool_error;
637 return isl_space_has_tuple_name(set->dim, isl_dim_set);
641 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
643 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
646 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
648 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
651 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
652 enum isl_dim_type type, unsigned pos)
654 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
657 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
658 enum isl_dim_type type, unsigned pos)
660 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
663 /* Does the given dimension have a name?
665 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
666 enum isl_dim_type type, unsigned pos)
668 if (!map)
669 return isl_bool_error;
670 return isl_space_has_dim_name(map->dim, type, pos);
673 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
674 enum isl_dim_type type, unsigned pos)
676 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
679 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
680 enum isl_dim_type type, unsigned pos)
682 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
685 /* Does the given dimension have a name?
687 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
688 enum isl_dim_type type, unsigned pos)
690 if (!set)
691 return isl_bool_error;
692 return isl_space_has_dim_name(set->dim, type, pos);
695 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
696 __isl_take isl_basic_map *bmap,
697 enum isl_dim_type type, unsigned pos, const char *s)
699 bmap = isl_basic_map_cow(bmap);
700 if (!bmap)
701 return NULL;
702 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
703 if (!bmap->dim)
704 goto error;
705 return isl_basic_map_finalize(bmap);
706 error:
707 isl_basic_map_free(bmap);
708 return NULL;
711 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
712 enum isl_dim_type type, unsigned pos, const char *s)
714 int i;
716 map = isl_map_cow(map);
717 if (!map)
718 return NULL;
720 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
721 if (!map->dim)
722 goto error;
724 for (i = 0; i < map->n; ++i) {
725 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
726 if (!map->p[i])
727 goto error;
730 return map;
731 error:
732 isl_map_free(map);
733 return NULL;
736 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
737 __isl_take isl_basic_set *bset,
738 enum isl_dim_type type, unsigned pos, const char *s)
740 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
741 type, pos, s));
744 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
745 enum isl_dim_type type, unsigned pos, const char *s)
747 return set_from_map(isl_map_set_dim_name(set_to_map(set),
748 type, pos, s));
751 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
752 enum isl_dim_type type, unsigned pos)
754 if (!bmap)
755 return isl_bool_error;
756 return isl_space_has_dim_id(bmap->dim, type, pos);
759 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
760 enum isl_dim_type type, unsigned pos)
762 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
765 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
766 enum isl_dim_type type, unsigned pos)
768 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
771 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
772 enum isl_dim_type type, unsigned pos)
774 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
777 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
778 enum isl_dim_type type, unsigned pos)
780 return isl_map_has_dim_id(set, type, pos);
783 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
784 enum isl_dim_type type, unsigned pos)
786 return isl_map_get_dim_id(set, type, pos);
789 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
790 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
792 map = isl_map_cow(map);
793 if (!map)
794 goto error;
796 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
798 return isl_map_reset_space(map, isl_space_copy(map->dim));
799 error:
800 isl_id_free(id);
801 return NULL;
804 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
805 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
807 return isl_map_set_dim_id(set, type, pos, id);
810 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
811 __isl_keep isl_id *id)
813 if (!map)
814 return -1;
815 return isl_space_find_dim_by_id(map->dim, type, id);
818 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
819 __isl_keep isl_id *id)
821 return isl_map_find_dim_by_id(set, type, id);
824 /* Return the position of the dimension of the given type and name
825 * in "bmap".
826 * Return -1 if no such dimension can be found.
828 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
829 enum isl_dim_type type, const char *name)
831 if (!bmap)
832 return -1;
833 return isl_space_find_dim_by_name(bmap->dim, type, name);
836 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
837 const char *name)
839 if (!map)
840 return -1;
841 return isl_space_find_dim_by_name(map->dim, type, name);
844 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
845 const char *name)
847 return isl_map_find_dim_by_name(set, type, name);
850 /* Check whether equality i of bset is a pure stride constraint
851 * on a single dimension, i.e., of the form
853 * v = k e
855 * with k a constant and e an existentially quantified variable.
857 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
859 unsigned nparam;
860 unsigned d;
861 unsigned n_div;
862 int pos1;
863 int pos2;
865 if (!bset)
866 return isl_bool_error;
868 if (!isl_int_is_zero(bset->eq[i][0]))
869 return isl_bool_false;
871 nparam = isl_basic_set_dim(bset, isl_dim_param);
872 d = isl_basic_set_dim(bset, isl_dim_set);
873 n_div = isl_basic_set_dim(bset, isl_dim_div);
875 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
876 return isl_bool_false;
877 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
878 if (pos1 == -1)
879 return isl_bool_false;
880 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
881 d - pos1 - 1) != -1)
882 return isl_bool_false;
884 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
885 if (pos2 == -1)
886 return isl_bool_false;
887 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
888 n_div - pos2 - 1) != -1)
889 return isl_bool_false;
890 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
891 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
892 return isl_bool_false;
894 return isl_bool_true;
897 /* Reset the user pointer on all identifiers of parameters and tuples
898 * of the space of "map".
900 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
902 isl_space *space;
904 space = isl_map_get_space(map);
905 space = isl_space_reset_user(space);
906 map = isl_map_reset_space(map, space);
908 return map;
911 /* Reset the user pointer on all identifiers of parameters and tuples
912 * of the space of "set".
914 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
916 return isl_map_reset_user(set);
919 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
921 if (!bmap)
922 return isl_bool_error;
923 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
926 /* Has "map" been marked as a rational map?
927 * In particular, have all basic maps in "map" been marked this way?
928 * An empty map is not considered to be rational.
929 * Maps where only some of the basic maps are marked rational
930 * are not allowed.
932 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
934 int i;
935 isl_bool rational;
937 if (!map)
938 return isl_bool_error;
939 if (map->n == 0)
940 return isl_bool_false;
941 rational = isl_basic_map_is_rational(map->p[0]);
942 if (rational < 0)
943 return rational;
944 for (i = 1; i < map->n; ++i) {
945 isl_bool rational_i;
947 rational_i = isl_basic_map_is_rational(map->p[i]);
948 if (rational_i < 0)
949 return rational_i;
950 if (rational != rational_i)
951 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
952 "mixed rational and integer basic maps "
953 "not supported", return isl_bool_error);
956 return rational;
959 /* Has "set" been marked as a rational set?
960 * In particular, have all basic set in "set" been marked this way?
961 * An empty set is not considered to be rational.
962 * Sets where only some of the basic sets are marked rational
963 * are not allowed.
965 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
967 return isl_map_is_rational(set);
970 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
972 return isl_basic_map_is_rational(bset);
975 /* Does "bmap" contain any rational points?
977 * If "bmap" has an equality for each dimension, equating the dimension
978 * to an integer constant, then it has no rational points, even if it
979 * is marked as rational.
981 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
983 isl_bool has_rational = isl_bool_true;
984 unsigned total;
986 if (!bmap)
987 return isl_bool_error;
988 if (isl_basic_map_plain_is_empty(bmap))
989 return isl_bool_false;
990 if (!isl_basic_map_is_rational(bmap))
991 return isl_bool_false;
992 bmap = isl_basic_map_copy(bmap);
993 bmap = isl_basic_map_implicit_equalities(bmap);
994 if (!bmap)
995 return isl_bool_error;
996 total = isl_basic_map_total_dim(bmap);
997 if (bmap->n_eq == total) {
998 int i, j;
999 for (i = 0; i < bmap->n_eq; ++i) {
1000 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1001 if (j < 0)
1002 break;
1003 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1004 !isl_int_is_negone(bmap->eq[i][1 + j]))
1005 break;
1006 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1007 total - j - 1);
1008 if (j >= 0)
1009 break;
1011 if (i == bmap->n_eq)
1012 has_rational = isl_bool_false;
1014 isl_basic_map_free(bmap);
1016 return has_rational;
1019 /* Does "map" contain any rational points?
1021 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1023 int i;
1024 isl_bool has_rational;
1026 if (!map)
1027 return isl_bool_error;
1028 for (i = 0; i < map->n; ++i) {
1029 has_rational = isl_basic_map_has_rational(map->p[i]);
1030 if (has_rational < 0 || has_rational)
1031 return has_rational;
1033 return isl_bool_false;
1036 /* Does "set" contain any rational points?
1038 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1040 return isl_map_has_rational(set);
1043 /* Is this basic set a parameter domain?
1045 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1047 if (!bset)
1048 return isl_bool_error;
1049 return isl_space_is_params(bset->dim);
1052 /* Is this set a parameter domain?
1054 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1056 if (!set)
1057 return isl_bool_error;
1058 return isl_space_is_params(set->dim);
1061 /* Is this map actually a parameter domain?
1062 * Users should never call this function. Outside of isl,
1063 * a map can never be a parameter domain.
1065 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1067 if (!map)
1068 return isl_bool_error;
1069 return isl_space_is_params(map->dim);
1072 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
1073 struct isl_basic_map *bmap, unsigned extra,
1074 unsigned n_eq, unsigned n_ineq)
1076 int i;
1077 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1079 bmap->ctx = ctx;
1080 isl_ctx_ref(ctx);
1082 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1083 if (isl_blk_is_error(bmap->block))
1084 goto error;
1086 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1087 if ((n_ineq + n_eq) && !bmap->ineq)
1088 goto error;
1090 if (extra == 0) {
1091 bmap->block2 = isl_blk_empty();
1092 bmap->div = NULL;
1093 } else {
1094 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1095 if (isl_blk_is_error(bmap->block2))
1096 goto error;
1098 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1099 if (!bmap->div)
1100 goto error;
1103 for (i = 0; i < n_ineq + n_eq; ++i)
1104 bmap->ineq[i] = bmap->block.data + i * row_size;
1106 for (i = 0; i < extra; ++i)
1107 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1109 bmap->ref = 1;
1110 bmap->flags = 0;
1111 bmap->c_size = n_eq + n_ineq;
1112 bmap->eq = bmap->ineq + n_ineq;
1113 bmap->extra = extra;
1114 bmap->n_eq = 0;
1115 bmap->n_ineq = 0;
1116 bmap->n_div = 0;
1117 bmap->sample = NULL;
1119 return bmap;
1120 error:
1121 isl_basic_map_free(bmap);
1122 return NULL;
1125 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1126 unsigned nparam, unsigned dim, unsigned extra,
1127 unsigned n_eq, unsigned n_ineq)
1129 struct isl_basic_map *bmap;
1130 isl_space *space;
1132 space = isl_space_set_alloc(ctx, nparam, dim);
1133 if (!space)
1134 return NULL;
1136 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1137 return bset_from_bmap(bmap);
1140 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1141 unsigned extra, unsigned n_eq, unsigned n_ineq)
1143 struct isl_basic_map *bmap;
1144 if (!dim)
1145 return NULL;
1146 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1147 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1148 return bset_from_bmap(bmap);
1149 error:
1150 isl_space_free(dim);
1151 return NULL;
1154 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1155 unsigned extra, unsigned n_eq, unsigned n_ineq)
1157 struct isl_basic_map *bmap;
1159 if (!dim)
1160 return NULL;
1161 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1162 if (!bmap)
1163 goto error;
1164 bmap->dim = dim;
1166 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1167 error:
1168 isl_space_free(dim);
1169 return NULL;
1172 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1173 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1174 unsigned n_eq, unsigned n_ineq)
1176 struct isl_basic_map *bmap;
1177 isl_space *dim;
1179 dim = isl_space_alloc(ctx, nparam, in, out);
1180 if (!dim)
1181 return NULL;
1183 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1184 return bmap;
1187 static void dup_constraints(
1188 struct isl_basic_map *dst, struct isl_basic_map *src)
1190 int i;
1191 unsigned total = isl_basic_map_total_dim(src);
1193 for (i = 0; i < src->n_eq; ++i) {
1194 int j = isl_basic_map_alloc_equality(dst);
1195 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1198 for (i = 0; i < src->n_ineq; ++i) {
1199 int j = isl_basic_map_alloc_inequality(dst);
1200 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1203 for (i = 0; i < src->n_div; ++i) {
1204 int j = isl_basic_map_alloc_div(dst);
1205 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1207 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1210 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1212 struct isl_basic_map *dup;
1214 if (!bmap)
1215 return NULL;
1216 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1217 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1218 if (!dup)
1219 return NULL;
1220 dup_constraints(dup, bmap);
1221 dup->flags = bmap->flags;
1222 dup->sample = isl_vec_copy(bmap->sample);
1223 return dup;
1226 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1228 struct isl_basic_map *dup;
1230 dup = isl_basic_map_dup(bset_to_bmap(bset));
1231 return bset_from_bmap(dup);
1234 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1236 if (!bset)
1237 return NULL;
1239 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1240 bset->ref++;
1241 return bset;
1243 return isl_basic_set_dup(bset);
1246 struct isl_set *isl_set_copy(struct isl_set *set)
1248 if (!set)
1249 return NULL;
1251 set->ref++;
1252 return set;
1255 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1257 if (!bmap)
1258 return NULL;
1260 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1261 bmap->ref++;
1262 return bmap;
1264 bmap = isl_basic_map_dup(bmap);
1265 if (bmap)
1266 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1267 return bmap;
1270 struct isl_map *isl_map_copy(struct isl_map *map)
1272 if (!map)
1273 return NULL;
1275 map->ref++;
1276 return map;
1279 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1281 if (!bmap)
1282 return NULL;
1284 if (--bmap->ref > 0)
1285 return NULL;
1287 isl_ctx_deref(bmap->ctx);
1288 free(bmap->div);
1289 isl_blk_free(bmap->ctx, bmap->block2);
1290 free(bmap->ineq);
1291 isl_blk_free(bmap->ctx, bmap->block);
1292 isl_vec_free(bmap->sample);
1293 isl_space_free(bmap->dim);
1294 free(bmap);
1296 return NULL;
1299 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1301 return isl_basic_map_free(bset_to_bmap(bset));
1304 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1306 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1309 /* Check that "map" has only named parameters, reporting an error
1310 * if it does not.
1312 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1314 return isl_space_check_named_params(isl_map_peek_space(map));
1317 /* Check that "bmap1" and "bmap2" have the same parameters,
1318 * reporting an error if they do not.
1320 static isl_stat isl_basic_map_check_equal_params(
1321 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1323 isl_bool match;
1325 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1326 if (match < 0)
1327 return isl_stat_error;
1328 if (!match)
1329 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1330 "parameters don't match", return isl_stat_error);
1331 return isl_stat_ok;
1334 __isl_give isl_map *isl_map_align_params_map_map_and(
1335 __isl_take isl_map *map1, __isl_take isl_map *map2,
1336 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1337 __isl_take isl_map *map2))
1339 if (!map1 || !map2)
1340 goto error;
1341 if (isl_map_has_equal_params(map1, map2))
1342 return fn(map1, map2);
1343 if (isl_map_check_named_params(map1) < 0)
1344 goto error;
1345 if (isl_map_check_named_params(map2) < 0)
1346 goto error;
1347 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1348 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1349 return fn(map1, map2);
1350 error:
1351 isl_map_free(map1);
1352 isl_map_free(map2);
1353 return NULL;
1356 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1357 __isl_keep isl_map *map2,
1358 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1360 isl_bool r;
1362 if (!map1 || !map2)
1363 return isl_bool_error;
1364 if (isl_map_has_equal_params(map1, map2))
1365 return fn(map1, map2);
1366 if (isl_map_check_named_params(map1) < 0)
1367 return isl_bool_error;
1368 if (isl_map_check_named_params(map2) < 0)
1369 return isl_bool_error;
1370 map1 = isl_map_copy(map1);
1371 map2 = isl_map_copy(map2);
1372 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1373 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1374 r = fn(map1, map2);
1375 isl_map_free(map1);
1376 isl_map_free(map2);
1377 return r;
1380 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1382 struct isl_ctx *ctx;
1383 if (!bmap)
1384 return -1;
1385 ctx = bmap->ctx;
1386 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1387 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1388 return -1);
1389 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1390 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1391 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1392 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1393 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1394 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1395 isl_int *t;
1396 int j = isl_basic_map_alloc_inequality(bmap);
1397 if (j < 0)
1398 return -1;
1399 t = bmap->ineq[j];
1400 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1401 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1402 bmap->eq[-1] = t;
1403 bmap->n_eq++;
1404 bmap->n_ineq--;
1405 bmap->eq--;
1406 return 0;
1408 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1409 bmap->extra - bmap->n_div);
1410 return bmap->n_eq++;
1413 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1415 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1418 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1420 if (!bmap)
1421 return -1;
1422 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1423 bmap->n_eq -= n;
1424 return 0;
1427 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1429 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1432 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1434 isl_int *t;
1435 if (!bmap)
1436 return -1;
1437 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1439 if (pos != bmap->n_eq - 1) {
1440 t = bmap->eq[pos];
1441 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1442 bmap->eq[bmap->n_eq - 1] = t;
1444 bmap->n_eq--;
1445 return 0;
1448 /* Turn inequality "pos" of "bmap" into an equality.
1450 * In particular, we move the inequality in front of the equalities
1451 * and move the last inequality in the position of the moved inequality.
1452 * Note that isl_tab_make_equalities_explicit depends on this particular
1453 * change in the ordering of the constraints.
1455 void isl_basic_map_inequality_to_equality(
1456 struct isl_basic_map *bmap, unsigned pos)
1458 isl_int *t;
1460 t = bmap->ineq[pos];
1461 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1462 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1463 bmap->eq[-1] = t;
1464 bmap->n_eq++;
1465 bmap->n_ineq--;
1466 bmap->eq--;
1467 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1468 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1469 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1470 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1473 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1475 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1478 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1480 struct isl_ctx *ctx;
1481 if (!bmap)
1482 return -1;
1483 ctx = bmap->ctx;
1484 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1485 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1486 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1487 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1488 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1489 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1490 1 + isl_basic_map_total_dim(bmap),
1491 bmap->extra - bmap->n_div);
1492 return bmap->n_ineq++;
1495 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1497 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1500 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1502 if (!bmap)
1503 return -1;
1504 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1505 bmap->n_ineq -= n;
1506 return 0;
1509 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1511 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1514 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1516 isl_int *t;
1517 if (!bmap)
1518 return -1;
1519 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1521 if (pos != bmap->n_ineq - 1) {
1522 t = bmap->ineq[pos];
1523 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1524 bmap->ineq[bmap->n_ineq - 1] = t;
1525 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1527 bmap->n_ineq--;
1528 return 0;
1531 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1533 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1536 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1537 isl_int *eq)
1539 int k;
1541 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1542 if (!bmap)
1543 return NULL;
1544 k = isl_basic_map_alloc_equality(bmap);
1545 if (k < 0)
1546 goto error;
1547 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1548 return bmap;
1549 error:
1550 isl_basic_map_free(bmap);
1551 return NULL;
1554 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1555 isl_int *eq)
1557 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1560 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1561 isl_int *ineq)
1563 int k;
1565 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1566 if (!bmap)
1567 return NULL;
1568 k = isl_basic_map_alloc_inequality(bmap);
1569 if (k < 0)
1570 goto error;
1571 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1572 return bmap;
1573 error:
1574 isl_basic_map_free(bmap);
1575 return NULL;
1578 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1579 isl_int *ineq)
1581 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1584 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1586 if (!bmap)
1587 return -1;
1588 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1589 isl_seq_clr(bmap->div[bmap->n_div] +
1590 1 + 1 + isl_basic_map_total_dim(bmap),
1591 bmap->extra - bmap->n_div);
1592 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1593 return bmap->n_div++;
1596 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1598 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1601 /* Check that there are "n" dimensions of type "type" starting at "first"
1602 * in "bmap".
1604 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1605 enum isl_dim_type type, unsigned first, unsigned n)
1607 unsigned dim;
1609 if (!bmap)
1610 return isl_stat_error;
1611 dim = isl_basic_map_dim(bmap, type);
1612 if (first + n > dim || first + n < first)
1613 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1614 "position or range out of bounds",
1615 return isl_stat_error);
1616 return isl_stat_ok;
1619 /* Insert an extra integer division, prescribed by "div", to "bmap"
1620 * at (integer division) position "pos".
1622 * The integer division is first added at the end and then moved
1623 * into the right position.
1625 __isl_give isl_basic_map *isl_basic_map_insert_div(
1626 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1628 int i, k;
1630 bmap = isl_basic_map_cow(bmap);
1631 if (!bmap || !div)
1632 return isl_basic_map_free(bmap);
1634 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1635 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1636 "unexpected size", return isl_basic_map_free(bmap));
1637 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1638 return isl_basic_map_free(bmap);
1640 bmap = isl_basic_map_extend_space(bmap,
1641 isl_basic_map_get_space(bmap), 1, 0, 2);
1642 k = isl_basic_map_alloc_div(bmap);
1643 if (k < 0)
1644 return isl_basic_map_free(bmap);
1645 isl_seq_cpy(bmap->div[k], div->el, div->size);
1646 isl_int_set_si(bmap->div[k][div->size], 0);
1648 for (i = k; i > pos; --i)
1649 isl_basic_map_swap_div(bmap, i, i - 1);
1651 return bmap;
1654 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1656 if (!bmap)
1657 return isl_stat_error;
1658 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1659 bmap->n_div -= n;
1660 return isl_stat_ok;
1663 /* Copy constraint from src to dst, putting the vars of src at offset
1664 * dim_off in dst and the divs of src at offset div_off in dst.
1665 * If both sets are actually map, then dim_off applies to the input
1666 * variables.
1668 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1669 struct isl_basic_map *src_map, isl_int *src,
1670 unsigned in_off, unsigned out_off, unsigned div_off)
1672 unsigned src_nparam = isl_basic_map_dim(src_map, isl_dim_param);
1673 unsigned dst_nparam = isl_basic_map_dim(dst_map, isl_dim_param);
1674 unsigned src_in = isl_basic_map_dim(src_map, isl_dim_in);
1675 unsigned dst_in = isl_basic_map_dim(dst_map, isl_dim_in);
1676 unsigned src_out = isl_basic_map_dim(src_map, isl_dim_out);
1677 unsigned dst_out = isl_basic_map_dim(dst_map, isl_dim_out);
1678 isl_int_set(dst[0], src[0]);
1679 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1680 if (dst_nparam > src_nparam)
1681 isl_seq_clr(dst+1+src_nparam,
1682 dst_nparam - src_nparam);
1683 isl_seq_clr(dst+1+dst_nparam, in_off);
1684 isl_seq_cpy(dst+1+dst_nparam+in_off,
1685 src+1+src_nparam,
1686 isl_min(dst_in-in_off, src_in));
1687 if (dst_in-in_off > src_in)
1688 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1689 dst_in - in_off - src_in);
1690 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1691 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1692 src+1+src_nparam+src_in,
1693 isl_min(dst_out-out_off, src_out));
1694 if (dst_out-out_off > src_out)
1695 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1696 dst_out - out_off - src_out);
1697 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1698 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1699 src+1+src_nparam+src_in+src_out,
1700 isl_min(dst_map->extra-div_off, src_map->n_div));
1701 if (dst_map->n_div-div_off > src_map->n_div)
1702 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1703 div_off+src_map->n_div,
1704 dst_map->n_div - div_off - src_map->n_div);
1707 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1708 struct isl_basic_map *src_map, isl_int *src,
1709 unsigned in_off, unsigned out_off, unsigned div_off)
1711 isl_int_set(dst[0], src[0]);
1712 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1715 static __isl_give isl_basic_map *add_constraints(
1716 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1717 unsigned i_pos, unsigned o_pos)
1719 int i;
1720 unsigned div_off;
1722 if (!bmap1 || !bmap2)
1723 goto error;
1725 div_off = bmap1->n_div;
1727 for (i = 0; i < bmap2->n_eq; ++i) {
1728 int i1 = isl_basic_map_alloc_equality(bmap1);
1729 if (i1 < 0)
1730 goto error;
1731 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1732 i_pos, o_pos, div_off);
1735 for (i = 0; i < bmap2->n_ineq; ++i) {
1736 int i1 = isl_basic_map_alloc_inequality(bmap1);
1737 if (i1 < 0)
1738 goto error;
1739 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1740 i_pos, o_pos, div_off);
1743 for (i = 0; i < bmap2->n_div; ++i) {
1744 int i1 = isl_basic_map_alloc_div(bmap1);
1745 if (i1 < 0)
1746 goto error;
1747 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1748 i_pos, o_pos, div_off);
1751 isl_basic_map_free(bmap2);
1753 return bmap1;
1755 error:
1756 isl_basic_map_free(bmap1);
1757 isl_basic_map_free(bmap2);
1758 return NULL;
1761 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1762 struct isl_basic_set *bset2, unsigned pos)
1764 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1765 bset_to_bmap(bset2), 0, pos));
1768 __isl_give isl_basic_map *isl_basic_map_extend_space(
1769 __isl_take isl_basic_map *base, __isl_take isl_space *dim,
1770 unsigned extra, unsigned n_eq, unsigned n_ineq)
1772 struct isl_basic_map *ext;
1773 unsigned flags;
1774 int dims_ok;
1776 if (!dim)
1777 goto error;
1779 if (!base)
1780 goto error;
1782 dims_ok = isl_space_is_equal(base->dim, dim) &&
1783 base->extra >= base->n_div + extra;
1785 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1786 room_for_ineq(base, n_ineq)) {
1787 isl_space_free(dim);
1788 return base;
1791 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1792 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1793 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1794 extra += base->extra;
1795 n_eq += base->n_eq;
1796 n_ineq += base->n_ineq;
1798 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1799 dim = NULL;
1800 if (!ext)
1801 goto error;
1803 if (dims_ok)
1804 ext->sample = isl_vec_copy(base->sample);
1805 flags = base->flags;
1806 ext = add_constraints(ext, base, 0, 0);
1807 if (ext) {
1808 ext->flags = flags;
1809 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1812 return ext;
1814 error:
1815 isl_space_free(dim);
1816 isl_basic_map_free(base);
1817 return NULL;
1820 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1821 __isl_take isl_space *dim, unsigned extra,
1822 unsigned n_eq, unsigned n_ineq)
1824 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1825 dim, extra, n_eq, n_ineq));
1828 struct isl_basic_map *isl_basic_map_extend_constraints(
1829 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1831 if (!base)
1832 return NULL;
1833 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1834 0, n_eq, n_ineq);
1837 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1838 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1839 unsigned n_eq, unsigned n_ineq)
1841 struct isl_basic_map *bmap;
1842 isl_space *dim;
1844 if (!base)
1845 return NULL;
1846 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1847 if (!dim)
1848 goto error;
1850 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1851 return bmap;
1852 error:
1853 isl_basic_map_free(base);
1854 return NULL;
1857 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1858 unsigned nparam, unsigned dim, unsigned extra,
1859 unsigned n_eq, unsigned n_ineq)
1861 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1862 nparam, 0, dim, extra, n_eq, n_ineq));
1865 struct isl_basic_set *isl_basic_set_extend_constraints(
1866 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1868 isl_basic_map *bmap = bset_to_bmap(base);
1869 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1870 return bset_from_bmap(bmap);
1873 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1875 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1878 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1880 if (!bmap)
1881 return NULL;
1883 if (bmap->ref > 1) {
1884 bmap->ref--;
1885 bmap = isl_basic_map_dup(bmap);
1887 if (bmap) {
1888 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1889 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1891 return bmap;
1894 /* Clear all cached information in "map", either because it is about
1895 * to be modified or because it is being freed.
1896 * Always return the same pointer that is passed in.
1897 * This is needed for the use in isl_map_free.
1899 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1901 isl_basic_map_free(map->cached_simple_hull[0]);
1902 isl_basic_map_free(map->cached_simple_hull[1]);
1903 map->cached_simple_hull[0] = NULL;
1904 map->cached_simple_hull[1] = NULL;
1905 return map;
1908 struct isl_set *isl_set_cow(struct isl_set *set)
1910 return isl_map_cow(set);
1913 /* Return an isl_map that is equal to "map" and that has only
1914 * a single reference.
1916 * If the original input already has only one reference, then
1917 * simply return it, but clear all cached information, since
1918 * it may be rendered invalid by the operations that will be
1919 * performed on the result.
1921 * Otherwise, create a duplicate (without any cached information).
1923 struct isl_map *isl_map_cow(struct isl_map *map)
1925 if (!map)
1926 return NULL;
1928 if (map->ref == 1)
1929 return clear_caches(map);
1930 map->ref--;
1931 return isl_map_dup(map);
1934 static void swap_vars(struct isl_blk blk, isl_int *a,
1935 unsigned a_len, unsigned b_len)
1937 isl_seq_cpy(blk.data, a+a_len, b_len);
1938 isl_seq_cpy(blk.data+b_len, a, a_len);
1939 isl_seq_cpy(a, blk.data, b_len+a_len);
1942 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1943 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1945 int i;
1946 struct isl_blk blk;
1948 if (!bmap)
1949 goto error;
1951 isl_assert(bmap->ctx,
1952 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1954 if (n1 == 0 || n2 == 0)
1955 return bmap;
1957 bmap = isl_basic_map_cow(bmap);
1958 if (!bmap)
1959 return NULL;
1961 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1962 if (isl_blk_is_error(blk))
1963 goto error;
1965 for (i = 0; i < bmap->n_eq; ++i)
1966 swap_vars(blk,
1967 bmap->eq[i] + pos, n1, n2);
1969 for (i = 0; i < bmap->n_ineq; ++i)
1970 swap_vars(blk,
1971 bmap->ineq[i] + pos, n1, n2);
1973 for (i = 0; i < bmap->n_div; ++i)
1974 swap_vars(blk,
1975 bmap->div[i]+1 + pos, n1, n2);
1977 isl_blk_free(bmap->ctx, blk);
1979 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1980 bmap = isl_basic_map_gauss(bmap, NULL);
1981 return isl_basic_map_finalize(bmap);
1982 error:
1983 isl_basic_map_free(bmap);
1984 return NULL;
1987 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1989 int i = 0;
1990 unsigned total;
1991 if (!bmap)
1992 goto error;
1993 total = isl_basic_map_total_dim(bmap);
1994 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
1995 return isl_basic_map_free(bmap);
1996 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1997 if (bmap->n_eq > 0)
1998 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1999 else {
2000 i = isl_basic_map_alloc_equality(bmap);
2001 if (i < 0)
2002 goto error;
2004 isl_int_set_si(bmap->eq[i][0], 1);
2005 isl_seq_clr(bmap->eq[i]+1, total);
2006 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2007 isl_vec_free(bmap->sample);
2008 bmap->sample = NULL;
2009 return isl_basic_map_finalize(bmap);
2010 error:
2011 isl_basic_map_free(bmap);
2012 return NULL;
2015 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
2017 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2020 __isl_give isl_basic_map *isl_basic_map_set_rational(
2021 __isl_take isl_basic_map *bmap)
2023 if (!bmap)
2024 return NULL;
2026 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2027 return bmap;
2029 bmap = isl_basic_map_cow(bmap);
2030 if (!bmap)
2031 return NULL;
2033 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2035 return isl_basic_map_finalize(bmap);
2038 __isl_give isl_basic_set *isl_basic_set_set_rational(
2039 __isl_take isl_basic_set *bset)
2041 return isl_basic_map_set_rational(bset);
2044 __isl_give isl_basic_set *isl_basic_set_set_integral(
2045 __isl_take isl_basic_set *bset)
2047 if (!bset)
2048 return NULL;
2050 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2051 return bset;
2053 bset = isl_basic_set_cow(bset);
2054 if (!bset)
2055 return NULL;
2057 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2059 return isl_basic_set_finalize(bset);
2062 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2064 int i;
2066 map = isl_map_cow(map);
2067 if (!map)
2068 return NULL;
2069 for (i = 0; i < map->n; ++i) {
2070 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2071 if (!map->p[i])
2072 goto error;
2074 return map;
2075 error:
2076 isl_map_free(map);
2077 return NULL;
2080 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2082 return isl_map_set_rational(set);
2085 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2086 * of "bmap").
2088 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2090 isl_int *t = bmap->div[a];
2091 bmap->div[a] = bmap->div[b];
2092 bmap->div[b] = t;
2095 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2096 * div definitions accordingly.
2098 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2100 int i;
2101 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2103 swap_div(bmap, a, b);
2105 for (i = 0; i < bmap->n_eq; ++i)
2106 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2108 for (i = 0; i < bmap->n_ineq; ++i)
2109 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2111 for (i = 0; i < bmap->n_div; ++i)
2112 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2113 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2116 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
2117 * div definitions accordingly.
2119 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
2121 isl_basic_map_swap_div(bset, a, b);
2124 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2126 isl_seq_cpy(c, c + n, rem);
2127 isl_seq_clr(c + rem, n);
2130 /* Drop n dimensions starting at first.
2132 * In principle, this frees up some extra variables as the number
2133 * of columns remains constant, but we would have to extend
2134 * the div array too as the number of rows in this array is assumed
2135 * to be equal to extra.
2137 struct isl_basic_set *isl_basic_set_drop_dims(
2138 struct isl_basic_set *bset, unsigned first, unsigned n)
2140 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2143 /* Move "n" divs starting at "first" to the end of the list of divs.
2145 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2146 unsigned first, unsigned n)
2148 isl_int **div;
2149 int i;
2151 if (first + n == bmap->n_div)
2152 return bmap;
2154 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2155 if (!div)
2156 goto error;
2157 for (i = 0; i < n; ++i)
2158 div[i] = bmap->div[first + i];
2159 for (i = 0; i < bmap->n_div - first - n; ++i)
2160 bmap->div[first + i] = bmap->div[first + n + i];
2161 for (i = 0; i < n; ++i)
2162 bmap->div[bmap->n_div - n + i] = div[i];
2163 free(div);
2164 return bmap;
2165 error:
2166 isl_basic_map_free(bmap);
2167 return NULL;
2170 /* Drop "n" dimensions of type "type" starting at "first".
2172 * In principle, this frees up some extra variables as the number
2173 * of columns remains constant, but we would have to extend
2174 * the div array too as the number of rows in this array is assumed
2175 * to be equal to extra.
2177 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
2178 enum isl_dim_type type, unsigned first, unsigned n)
2180 int i;
2181 unsigned dim;
2182 unsigned offset;
2183 unsigned left;
2185 if (!bmap)
2186 goto error;
2188 dim = isl_basic_map_dim(bmap, type);
2189 isl_assert(bmap->ctx, first + n <= dim, goto error);
2191 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2192 return bmap;
2194 bmap = isl_basic_map_cow(bmap);
2195 if (!bmap)
2196 return NULL;
2198 offset = isl_basic_map_offset(bmap, type) + first;
2199 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2200 for (i = 0; i < bmap->n_eq; ++i)
2201 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2203 for (i = 0; i < bmap->n_ineq; ++i)
2204 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2206 for (i = 0; i < bmap->n_div; ++i)
2207 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2209 if (type == isl_dim_div) {
2210 bmap = move_divs_last(bmap, first, n);
2211 if (!bmap)
2212 goto error;
2213 if (isl_basic_map_free_div(bmap, n) < 0)
2214 return isl_basic_map_free(bmap);
2215 } else
2216 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2217 if (!bmap->dim)
2218 goto error;
2220 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2221 bmap = isl_basic_map_simplify(bmap);
2222 return isl_basic_map_finalize(bmap);
2223 error:
2224 isl_basic_map_free(bmap);
2225 return NULL;
2228 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2229 enum isl_dim_type type, unsigned first, unsigned n)
2231 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2232 type, first, n));
2235 struct isl_map *isl_map_drop(struct isl_map *map,
2236 enum isl_dim_type type, unsigned first, unsigned n)
2238 int i;
2240 if (!map)
2241 goto error;
2243 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2245 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2246 return map;
2247 map = isl_map_cow(map);
2248 if (!map)
2249 goto error;
2250 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2251 if (!map->dim)
2252 goto error;
2254 for (i = 0; i < map->n; ++i) {
2255 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2256 if (!map->p[i])
2257 goto error;
2259 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2261 return map;
2262 error:
2263 isl_map_free(map);
2264 return NULL;
2267 struct isl_set *isl_set_drop(struct isl_set *set,
2268 enum isl_dim_type type, unsigned first, unsigned n)
2270 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2274 * We don't cow, as the div is assumed to be redundant.
2276 __isl_give isl_basic_map *isl_basic_map_drop_div(
2277 __isl_take isl_basic_map *bmap, unsigned div)
2279 int i;
2280 unsigned pos;
2282 if (!bmap)
2283 goto error;
2285 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
2287 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
2289 for (i = 0; i < bmap->n_eq; ++i)
2290 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
2292 for (i = 0; i < bmap->n_ineq; ++i) {
2293 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
2294 isl_basic_map_drop_inequality(bmap, i);
2295 --i;
2296 continue;
2298 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
2301 for (i = 0; i < bmap->n_div; ++i)
2302 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
2304 if (div != bmap->n_div - 1) {
2305 int j;
2306 isl_int *t = bmap->div[div];
2308 for (j = div; j < bmap->n_div - 1; ++j)
2309 bmap->div[j] = bmap->div[j+1];
2311 bmap->div[bmap->n_div - 1] = t;
2313 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2314 if (isl_basic_map_free_div(bmap, 1) < 0)
2315 return isl_basic_map_free(bmap);
2317 return bmap;
2318 error:
2319 isl_basic_map_free(bmap);
2320 return NULL;
2323 /* Eliminate the specified n dimensions starting at first from the
2324 * constraints, without removing the dimensions from the space.
2325 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2327 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2328 enum isl_dim_type type, unsigned first, unsigned n)
2330 int i;
2332 if (!map)
2333 return NULL;
2334 if (n == 0)
2335 return map;
2337 if (first + n > isl_map_dim(map, type) || first + n < first)
2338 isl_die(map->ctx, isl_error_invalid,
2339 "index out of bounds", goto error);
2341 map = isl_map_cow(map);
2342 if (!map)
2343 return NULL;
2345 for (i = 0; i < map->n; ++i) {
2346 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2347 if (!map->p[i])
2348 goto error;
2350 return map;
2351 error:
2352 isl_map_free(map);
2353 return NULL;
2356 /* Eliminate the specified n dimensions starting at first from the
2357 * constraints, without removing the dimensions from the space.
2358 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2360 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2361 enum isl_dim_type type, unsigned first, unsigned n)
2363 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2366 /* Eliminate the specified n dimensions starting at first from the
2367 * constraints, without removing the dimensions from the space.
2368 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2370 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2371 unsigned first, unsigned n)
2373 return isl_set_eliminate(set, isl_dim_set, first, n);
2376 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2377 __isl_take isl_basic_map *bmap)
2379 if (!bmap)
2380 return NULL;
2381 bmap = isl_basic_map_eliminate_vars(bmap,
2382 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2383 if (!bmap)
2384 return NULL;
2385 bmap->n_div = 0;
2386 return isl_basic_map_finalize(bmap);
2389 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2390 __isl_take isl_basic_set *bset)
2392 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2395 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2397 int i;
2399 if (!map)
2400 return NULL;
2401 if (map->n == 0)
2402 return map;
2404 map = isl_map_cow(map);
2405 if (!map)
2406 return NULL;
2408 for (i = 0; i < map->n; ++i) {
2409 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2410 if (!map->p[i])
2411 goto error;
2413 return map;
2414 error:
2415 isl_map_free(map);
2416 return NULL;
2419 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2421 return isl_map_remove_divs(set);
2424 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2425 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2426 unsigned first, unsigned n)
2428 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2429 return isl_basic_map_free(bmap);
2430 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2431 return bmap;
2432 bmap = isl_basic_map_eliminate_vars(bmap,
2433 isl_basic_map_offset(bmap, type) - 1 + first, n);
2434 if (!bmap)
2435 return bmap;
2436 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2437 return bmap;
2438 bmap = isl_basic_map_drop(bmap, type, first, n);
2439 return bmap;
2442 /* Return true if the definition of the given div (recursively) involves
2443 * any of the given variables.
2445 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2446 unsigned first, unsigned n)
2448 int i;
2449 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2451 if (isl_int_is_zero(bmap->div[div][0]))
2452 return isl_bool_false;
2453 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2454 return isl_bool_true;
2456 for (i = bmap->n_div - 1; i >= 0; --i) {
2457 isl_bool involves;
2459 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2460 continue;
2461 involves = div_involves_vars(bmap, i, first, n);
2462 if (involves < 0 || involves)
2463 return involves;
2466 return isl_bool_false;
2469 /* Try and add a lower and/or upper bound on "div" to "bmap"
2470 * based on inequality "i".
2471 * "total" is the total number of variables (excluding the divs).
2472 * "v" is a temporary object that can be used during the calculations.
2473 * If "lb" is set, then a lower bound should be constructed.
2474 * If "ub" is set, then an upper bound should be constructed.
2476 * The calling function has already checked that the inequality does not
2477 * reference "div", but we still need to check that the inequality is
2478 * of the right form. We'll consider the case where we want to construct
2479 * a lower bound. The construction of upper bounds is similar.
2481 * Let "div" be of the form
2483 * q = floor((a + f(x))/d)
2485 * We essentially check if constraint "i" is of the form
2487 * b + f(x) >= 0
2489 * so that we can use it to derive a lower bound on "div".
2490 * However, we allow a slightly more general form
2492 * b + g(x) >= 0
2494 * with the condition that the coefficients of g(x) - f(x) are all
2495 * divisible by d.
2496 * Rewriting this constraint as
2498 * 0 >= -b - g(x)
2500 * adding a + f(x) to both sides and dividing by d, we obtain
2502 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2504 * Taking the floor on both sides, we obtain
2506 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2508 * or
2510 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2512 * In the case of an upper bound, we construct the constraint
2514 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2517 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2518 __isl_take isl_basic_map *bmap, int div, int i,
2519 unsigned total, isl_int v, int lb, int ub)
2521 int j;
2523 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2524 if (lb) {
2525 isl_int_sub(v, bmap->ineq[i][1 + j],
2526 bmap->div[div][1 + 1 + j]);
2527 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2529 if (ub) {
2530 isl_int_add(v, bmap->ineq[i][1 + j],
2531 bmap->div[div][1 + 1 + j]);
2532 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2535 if (!lb && !ub)
2536 return bmap;
2538 bmap = isl_basic_map_cow(bmap);
2539 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2540 if (lb) {
2541 int k = isl_basic_map_alloc_inequality(bmap);
2542 if (k < 0)
2543 goto error;
2544 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2545 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2546 bmap->div[div][1 + j]);
2547 isl_int_cdiv_q(bmap->ineq[k][j],
2548 bmap->ineq[k][j], bmap->div[div][0]);
2550 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2552 if (ub) {
2553 int k = isl_basic_map_alloc_inequality(bmap);
2554 if (k < 0)
2555 goto error;
2556 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2557 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2558 bmap->div[div][1 + j]);
2559 isl_int_fdiv_q(bmap->ineq[k][j],
2560 bmap->ineq[k][j], bmap->div[div][0]);
2562 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2565 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2566 return bmap;
2567 error:
2568 isl_basic_map_free(bmap);
2569 return NULL;
2572 /* This function is called right before "div" is eliminated from "bmap"
2573 * using Fourier-Motzkin.
2574 * Look through the constraints of "bmap" for constraints on the argument
2575 * of the integer division and use them to construct constraints on the
2576 * integer division itself. These constraints can then be combined
2577 * during the Fourier-Motzkin elimination.
2578 * Note that it is only useful to introduce lower bounds on "div"
2579 * if "bmap" already contains upper bounds on "div" as the newly
2580 * introduce lower bounds can then be combined with the pre-existing
2581 * upper bounds. Similarly for upper bounds.
2582 * We therefore first check if "bmap" contains any lower and/or upper bounds
2583 * on "div".
2585 * It is interesting to note that the introduction of these constraints
2586 * can indeed lead to more accurate results, even when compared to
2587 * deriving constraints on the argument of "div" from constraints on "div".
2588 * Consider, for example, the set
2590 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2592 * The second constraint can be rewritten as
2594 * 2 * [(-i-2j+3)/4] + k >= 0
2596 * from which we can derive
2598 * -i - 2j + 3 >= -2k
2600 * or
2602 * i + 2j <= 3 + 2k
2604 * Combined with the first constraint, we obtain
2606 * -3 <= 3 + 2k or k >= -3
2608 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2609 * the first constraint, we obtain
2611 * [(i + 2j)/4] >= [-3/4] = -1
2613 * Combining this constraint with the second constraint, we obtain
2615 * k >= -2
2617 static __isl_give isl_basic_map *insert_bounds_on_div(
2618 __isl_take isl_basic_map *bmap, int div)
2620 int i;
2621 int check_lb, check_ub;
2622 isl_int v;
2623 unsigned total;
2625 if (!bmap)
2626 return NULL;
2628 if (isl_int_is_zero(bmap->div[div][0]))
2629 return bmap;
2631 total = isl_space_dim(bmap->dim, isl_dim_all);
2633 check_lb = 0;
2634 check_ub = 0;
2635 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2636 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2637 if (s > 0)
2638 check_ub = 1;
2639 if (s < 0)
2640 check_lb = 1;
2643 if (!check_lb && !check_ub)
2644 return bmap;
2646 isl_int_init(v);
2648 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2649 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2650 continue;
2652 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2653 check_lb, check_ub);
2656 isl_int_clear(v);
2658 return bmap;
2661 /* Remove all divs (recursively) involving any of the given dimensions
2662 * in their definitions.
2664 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2665 __isl_take isl_basic_map *bmap,
2666 enum isl_dim_type type, unsigned first, unsigned n)
2668 int i;
2670 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2671 return isl_basic_map_free(bmap);
2672 first += isl_basic_map_offset(bmap, type);
2674 for (i = bmap->n_div - 1; i >= 0; --i) {
2675 isl_bool involves;
2677 involves = div_involves_vars(bmap, i, first, n);
2678 if (involves < 0)
2679 return isl_basic_map_free(bmap);
2680 if (!involves)
2681 continue;
2682 bmap = insert_bounds_on_div(bmap, i);
2683 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2684 if (!bmap)
2685 return NULL;
2686 i = bmap->n_div;
2689 return bmap;
2692 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2693 __isl_take isl_basic_set *bset,
2694 enum isl_dim_type type, unsigned first, unsigned n)
2696 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2699 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2700 enum isl_dim_type type, unsigned first, unsigned n)
2702 int i;
2704 if (!map)
2705 return NULL;
2706 if (map->n == 0)
2707 return map;
2709 map = isl_map_cow(map);
2710 if (!map)
2711 return NULL;
2713 for (i = 0; i < map->n; ++i) {
2714 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2715 type, first, n);
2716 if (!map->p[i])
2717 goto error;
2719 return map;
2720 error:
2721 isl_map_free(map);
2722 return NULL;
2725 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2726 enum isl_dim_type type, unsigned first, unsigned n)
2728 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2729 type, first, n));
2732 /* Does the description of "bmap" depend on the specified dimensions?
2733 * We also check whether the dimensions appear in any of the div definitions.
2734 * In principle there is no need for this check. If the dimensions appear
2735 * in a div definition, they also appear in the defining constraints of that
2736 * div.
2738 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2739 enum isl_dim_type type, unsigned first, unsigned n)
2741 int i;
2743 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2744 return isl_bool_error;
2746 first += isl_basic_map_offset(bmap, type);
2747 for (i = 0; i < bmap->n_eq; ++i)
2748 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2749 return isl_bool_true;
2750 for (i = 0; i < bmap->n_ineq; ++i)
2751 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2752 return isl_bool_true;
2753 for (i = 0; i < bmap->n_div; ++i) {
2754 if (isl_int_is_zero(bmap->div[i][0]))
2755 continue;
2756 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2757 return isl_bool_true;
2760 return isl_bool_false;
2763 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2764 enum isl_dim_type type, unsigned first, unsigned n)
2766 int i;
2768 if (!map)
2769 return isl_bool_error;
2771 if (first + n > isl_map_dim(map, type))
2772 isl_die(map->ctx, isl_error_invalid,
2773 "index out of bounds", return isl_bool_error);
2775 for (i = 0; i < map->n; ++i) {
2776 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2777 type, first, n);
2778 if (involves < 0 || involves)
2779 return involves;
2782 return isl_bool_false;
2785 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2786 enum isl_dim_type type, unsigned first, unsigned n)
2788 return isl_basic_map_involves_dims(bset, type, first, n);
2791 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2792 enum isl_dim_type type, unsigned first, unsigned n)
2794 return isl_map_involves_dims(set, type, first, n);
2797 /* Drop all constraints in bmap that involve any of the dimensions
2798 * first to first+n-1.
2800 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2801 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2803 int i;
2805 if (n == 0)
2806 return bmap;
2808 bmap = isl_basic_map_cow(bmap);
2810 if (!bmap)
2811 return NULL;
2813 for (i = bmap->n_eq - 1; i >= 0; --i) {
2814 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2815 continue;
2816 isl_basic_map_drop_equality(bmap, i);
2819 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2820 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2821 continue;
2822 isl_basic_map_drop_inequality(bmap, i);
2825 bmap = isl_basic_map_add_known_div_constraints(bmap);
2826 return bmap;
2829 /* Drop all constraints in bset that involve any of the dimensions
2830 * first to first+n-1.
2832 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2833 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2835 return isl_basic_map_drop_constraints_involving(bset, first, n);
2838 /* Drop all constraints in bmap that do not involve any of the dimensions
2839 * first to first + n - 1 of the given type.
2841 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2842 __isl_take isl_basic_map *bmap,
2843 enum isl_dim_type type, unsigned first, unsigned n)
2845 int i;
2847 if (n == 0) {
2848 isl_space *space = isl_basic_map_get_space(bmap);
2849 isl_basic_map_free(bmap);
2850 return isl_basic_map_universe(space);
2852 bmap = isl_basic_map_cow(bmap);
2853 if (!bmap)
2854 return NULL;
2856 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2857 return isl_basic_map_free(bmap);
2859 first += isl_basic_map_offset(bmap, type) - 1;
2861 for (i = bmap->n_eq - 1; i >= 0; --i) {
2862 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2863 continue;
2864 isl_basic_map_drop_equality(bmap, i);
2867 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2868 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2869 continue;
2870 isl_basic_map_drop_inequality(bmap, i);
2873 bmap = isl_basic_map_add_known_div_constraints(bmap);
2874 return bmap;
2877 /* Drop all constraints in bset that do not involve any of the dimensions
2878 * first to first + n - 1 of the given type.
2880 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2881 __isl_take isl_basic_set *bset,
2882 enum isl_dim_type type, unsigned first, unsigned n)
2884 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2885 type, first, n);
2888 /* Drop all constraints in bmap that involve any of the dimensions
2889 * first to first + n - 1 of the given type.
2891 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2892 __isl_take isl_basic_map *bmap,
2893 enum isl_dim_type type, unsigned first, unsigned n)
2895 if (!bmap)
2896 return NULL;
2897 if (n == 0)
2898 return bmap;
2900 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2901 return isl_basic_map_free(bmap);
2903 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2904 first += isl_basic_map_offset(bmap, type) - 1;
2905 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2908 /* Drop all constraints in bset that involve any of the dimensions
2909 * first to first + n - 1 of the given type.
2911 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2912 __isl_take isl_basic_set *bset,
2913 enum isl_dim_type type, unsigned first, unsigned n)
2915 return isl_basic_map_drop_constraints_involving_dims(bset,
2916 type, first, n);
2919 /* Drop constraints from "map" by applying "drop" to each basic map.
2921 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2922 enum isl_dim_type type, unsigned first, unsigned n,
2923 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2924 enum isl_dim_type type, unsigned first, unsigned n))
2926 int i;
2927 unsigned dim;
2929 if (!map)
2930 return NULL;
2932 dim = isl_map_dim(map, type);
2933 if (first + n > dim || first + n < first)
2934 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2935 "index out of bounds", return isl_map_free(map));
2937 map = isl_map_cow(map);
2938 if (!map)
2939 return NULL;
2941 for (i = 0; i < map->n; ++i) {
2942 map->p[i] = drop(map->p[i], type, first, n);
2943 if (!map->p[i])
2944 return isl_map_free(map);
2947 if (map->n > 1)
2948 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2950 return map;
2953 /* Drop all constraints in map that involve any of the dimensions
2954 * first to first + n - 1 of the given type.
2956 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2957 __isl_take isl_map *map,
2958 enum isl_dim_type type, unsigned first, unsigned n)
2960 if (n == 0)
2961 return map;
2962 return drop_constraints(map, type, first, n,
2963 &isl_basic_map_drop_constraints_involving_dims);
2966 /* Drop all constraints in "map" that do not involve any of the dimensions
2967 * first to first + n - 1 of the given type.
2969 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2970 __isl_take isl_map *map,
2971 enum isl_dim_type type, unsigned first, unsigned n)
2973 if (n == 0) {
2974 isl_space *space = isl_map_get_space(map);
2975 isl_map_free(map);
2976 return isl_map_universe(space);
2978 return drop_constraints(map, type, first, n,
2979 &isl_basic_map_drop_constraints_not_involving_dims);
2982 /* Drop all constraints in set that involve any of the dimensions
2983 * first to first + n - 1 of the given type.
2985 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
2986 __isl_take isl_set *set,
2987 enum isl_dim_type type, unsigned first, unsigned n)
2989 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2992 /* Drop all constraints in "set" that do not involve any of the dimensions
2993 * first to first + n - 1 of the given type.
2995 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
2996 __isl_take isl_set *set,
2997 enum isl_dim_type type, unsigned first, unsigned n)
2999 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3002 /* Does local variable "div" of "bmap" have a complete explicit representation?
3003 * Having a complete explicit representation requires not only
3004 * an explicit representation, but also that all local variables
3005 * that appear in this explicit representation in turn have
3006 * a complete explicit representation.
3008 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3010 int i;
3011 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3012 isl_bool marked;
3014 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3015 if (marked < 0 || marked)
3016 return isl_bool_not(marked);
3018 for (i = bmap->n_div - 1; i >= 0; --i) {
3019 isl_bool known;
3021 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3022 continue;
3023 known = isl_basic_map_div_is_known(bmap, i);
3024 if (known < 0 || !known)
3025 return known;
3028 return isl_bool_true;
3031 /* Remove all divs that are unknown or defined in terms of unknown divs.
3033 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3034 __isl_take isl_basic_map *bmap)
3036 int i;
3038 if (!bmap)
3039 return NULL;
3041 for (i = bmap->n_div - 1; i >= 0; --i) {
3042 if (isl_basic_map_div_is_known(bmap, i))
3043 continue;
3044 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3045 if (!bmap)
3046 return NULL;
3047 i = bmap->n_div;
3050 return bmap;
3053 /* Remove all divs that are unknown or defined in terms of unknown divs.
3055 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3056 __isl_take isl_basic_set *bset)
3058 return isl_basic_map_remove_unknown_divs(bset);
3061 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3063 int i;
3065 if (!map)
3066 return NULL;
3067 if (map->n == 0)
3068 return map;
3070 map = isl_map_cow(map);
3071 if (!map)
3072 return NULL;
3074 for (i = 0; i < map->n; ++i) {
3075 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3076 if (!map->p[i])
3077 goto error;
3079 return map;
3080 error:
3081 isl_map_free(map);
3082 return NULL;
3085 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3087 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3090 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3091 __isl_take isl_basic_set *bset,
3092 enum isl_dim_type type, unsigned first, unsigned n)
3094 isl_basic_map *bmap = bset_to_bmap(bset);
3095 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3096 return bset_from_bmap(bmap);
3099 struct isl_map *isl_map_remove_dims(struct isl_map *map,
3100 enum isl_dim_type type, unsigned first, unsigned n)
3102 int i;
3104 if (n == 0)
3105 return map;
3107 map = isl_map_cow(map);
3108 if (!map)
3109 return NULL;
3110 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3112 for (i = 0; i < map->n; ++i) {
3113 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3114 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3115 if (!map->p[i])
3116 goto error;
3118 map = isl_map_drop(map, type, first, n);
3119 return map;
3120 error:
3121 isl_map_free(map);
3122 return NULL;
3125 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3126 enum isl_dim_type type, unsigned first, unsigned n)
3128 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3129 type, first, n));
3132 /* Project out n inputs starting at first using Fourier-Motzkin */
3133 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3134 unsigned first, unsigned n)
3136 return isl_map_remove_dims(map, isl_dim_in, first, n);
3139 static void dump_term(struct isl_basic_map *bmap,
3140 isl_int c, int pos, FILE *out)
3142 const char *name;
3143 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3144 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3145 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3146 if (!pos)
3147 isl_int_print(out, c, 0);
3148 else {
3149 if (!isl_int_is_one(c))
3150 isl_int_print(out, c, 0);
3151 if (pos < 1 + nparam) {
3152 name = isl_space_get_dim_name(bmap->dim,
3153 isl_dim_param, pos - 1);
3154 if (name)
3155 fprintf(out, "%s", name);
3156 else
3157 fprintf(out, "p%d", pos - 1);
3158 } else if (pos < 1 + nparam + in)
3159 fprintf(out, "i%d", pos - 1 - nparam);
3160 else if (pos < 1 + nparam + dim)
3161 fprintf(out, "o%d", pos - 1 - nparam - in);
3162 else
3163 fprintf(out, "e%d", pos - 1 - nparam - dim);
3167 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3168 int sign, FILE *out)
3170 int i;
3171 int first;
3172 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3173 isl_int v;
3175 isl_int_init(v);
3176 for (i = 0, first = 1; i < len; ++i) {
3177 if (isl_int_sgn(c[i]) * sign <= 0)
3178 continue;
3179 if (!first)
3180 fprintf(out, " + ");
3181 first = 0;
3182 isl_int_abs(v, c[i]);
3183 dump_term(bmap, v, i, out);
3185 isl_int_clear(v);
3186 if (first)
3187 fprintf(out, "0");
3190 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3191 const char *op, FILE *out, int indent)
3193 int i;
3195 fprintf(out, "%*s", indent, "");
3197 dump_constraint_sign(bmap, c, 1, out);
3198 fprintf(out, " %s ", op);
3199 dump_constraint_sign(bmap, c, -1, out);
3201 fprintf(out, "\n");
3203 for (i = bmap->n_div; i < bmap->extra; ++i) {
3204 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3205 continue;
3206 fprintf(out, "%*s", indent, "");
3207 fprintf(out, "ERROR: unused div coefficient not zero\n");
3208 abort();
3212 static void dump_constraints(struct isl_basic_map *bmap,
3213 isl_int **c, unsigned n,
3214 const char *op, FILE *out, int indent)
3216 int i;
3218 for (i = 0; i < n; ++i)
3219 dump_constraint(bmap, c[i], op, out, indent);
3222 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3224 int j;
3225 int first = 1;
3226 unsigned total = isl_basic_map_total_dim(bmap);
3228 for (j = 0; j < 1 + total; ++j) {
3229 if (isl_int_is_zero(exp[j]))
3230 continue;
3231 if (!first && isl_int_is_pos(exp[j]))
3232 fprintf(out, "+");
3233 dump_term(bmap, exp[j], j, out);
3234 first = 0;
3238 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3240 int i;
3242 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3243 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3245 for (i = 0; i < bmap->n_div; ++i) {
3246 fprintf(out, "%*s", indent, "");
3247 fprintf(out, "e%d = [(", i);
3248 dump_affine(bmap, bmap->div[i]+1, out);
3249 fprintf(out, ")/");
3250 isl_int_print(out, bmap->div[i][0], 0);
3251 fprintf(out, "]\n");
3255 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3256 FILE *out, int indent)
3258 if (!bset) {
3259 fprintf(out, "null basic set\n");
3260 return;
3263 fprintf(out, "%*s", indent, "");
3264 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3265 bset->ref, bset->dim->nparam, bset->dim->n_out,
3266 bset->extra, bset->flags);
3267 dump(bset_to_bmap(bset), out, indent);
3270 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3271 FILE *out, int indent)
3273 if (!bmap) {
3274 fprintf(out, "null basic map\n");
3275 return;
3278 fprintf(out, "%*s", indent, "");
3279 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3280 "flags: %x, n_name: %d\n",
3281 bmap->ref,
3282 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3283 bmap->extra, bmap->flags, bmap->dim->n_id);
3284 dump(bmap, out, indent);
3287 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3289 unsigned total;
3290 if (!bmap)
3291 return -1;
3292 total = isl_basic_map_total_dim(bmap);
3293 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
3294 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3295 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3296 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3297 return 0;
3300 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3301 unsigned flags)
3303 if (!space)
3304 return NULL;
3305 if (isl_space_dim(space, isl_dim_in) != 0)
3306 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3307 "set cannot have input dimensions", goto error);
3308 return isl_map_alloc_space(space, n, flags);
3309 error:
3310 isl_space_free(space);
3311 return NULL;
3314 /* Make sure "map" has room for at least "n" more basic maps.
3316 struct isl_map *isl_map_grow(struct isl_map *map, int n)
3318 int i;
3319 struct isl_map *grown = NULL;
3321 if (!map)
3322 return NULL;
3323 isl_assert(map->ctx, n >= 0, goto error);
3324 if (map->n + n <= map->size)
3325 return map;
3326 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3327 if (!grown)
3328 goto error;
3329 for (i = 0; i < map->n; ++i) {
3330 grown->p[i] = isl_basic_map_copy(map->p[i]);
3331 if (!grown->p[i])
3332 goto error;
3333 grown->n++;
3335 isl_map_free(map);
3336 return grown;
3337 error:
3338 isl_map_free(grown);
3339 isl_map_free(map);
3340 return NULL;
3343 /* Make sure "set" has room for at least "n" more basic sets.
3345 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3347 return set_from_map(isl_map_grow(set_to_map(set), n));
3350 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
3352 return isl_map_from_basic_map(bset);
3355 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
3357 struct isl_map *map;
3359 if (!bmap)
3360 return NULL;
3362 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3363 return isl_map_add_basic_map(map, bmap);
3366 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3367 __isl_take isl_basic_set *bset)
3369 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3370 bset_to_bmap(bset)));
3373 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3375 return isl_map_free(set);
3378 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3380 int i;
3382 if (!set) {
3383 fprintf(out, "null set\n");
3384 return;
3387 fprintf(out, "%*s", indent, "");
3388 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3389 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3390 set->flags);
3391 for (i = 0; i < set->n; ++i) {
3392 fprintf(out, "%*s", indent, "");
3393 fprintf(out, "basic set %d:\n", i);
3394 isl_basic_set_print_internal(set->p[i], out, indent+4);
3398 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3400 int i;
3402 if (!map) {
3403 fprintf(out, "null map\n");
3404 return;
3407 fprintf(out, "%*s", indent, "");
3408 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3409 "flags: %x, n_name: %d\n",
3410 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3411 map->dim->n_out, map->flags, map->dim->n_id);
3412 for (i = 0; i < map->n; ++i) {
3413 fprintf(out, "%*s", indent, "");
3414 fprintf(out, "basic map %d:\n", i);
3415 isl_basic_map_print_internal(map->p[i], out, indent+4);
3419 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3420 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3422 struct isl_basic_map *bmap_domain;
3424 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3425 goto error;
3427 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3428 isl_assert(bset->ctx,
3429 isl_basic_map_compatible_domain(bmap, bset), goto error);
3431 bmap = isl_basic_map_cow(bmap);
3432 if (!bmap)
3433 goto error;
3434 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3435 bset->n_div, bset->n_eq, bset->n_ineq);
3436 bmap_domain = isl_basic_map_from_domain(bset);
3437 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3439 bmap = isl_basic_map_simplify(bmap);
3440 return isl_basic_map_finalize(bmap);
3441 error:
3442 isl_basic_map_free(bmap);
3443 isl_basic_set_free(bset);
3444 return NULL;
3447 /* Check that the space of "bset" is the same as that of the range of "bmap".
3449 static isl_stat isl_basic_map_check_compatible_range(
3450 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3452 isl_bool ok;
3454 ok = isl_basic_map_compatible_range(bmap, bset);
3455 if (ok < 0)
3456 return isl_stat_error;
3457 if (!ok)
3458 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3459 "incompatible spaces", return isl_stat_error);
3461 return isl_stat_ok;
3464 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3465 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3467 struct isl_basic_map *bmap_range;
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_basic_map_check_compatible_range(bmap, bset) < 0)
3474 goto error;
3476 if (isl_basic_set_plain_is_universe(bset)) {
3477 isl_basic_set_free(bset);
3478 return bmap;
3481 bmap = isl_basic_map_cow(bmap);
3482 if (!bmap)
3483 goto error;
3484 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3485 bset->n_div, bset->n_eq, bset->n_ineq);
3486 bmap_range = bset_to_bmap(bset);
3487 bmap = add_constraints(bmap, bmap_range, 0, 0);
3489 bmap = isl_basic_map_simplify(bmap);
3490 return isl_basic_map_finalize(bmap);
3491 error:
3492 isl_basic_map_free(bmap);
3493 isl_basic_set_free(bset);
3494 return NULL;
3497 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3498 __isl_keep isl_vec *vec)
3500 int i;
3501 unsigned total;
3502 isl_int s;
3504 if (!bmap || !vec)
3505 return isl_bool_error;
3507 total = 1 + isl_basic_map_total_dim(bmap);
3508 if (total != vec->size)
3509 return isl_bool_false;
3511 isl_int_init(s);
3513 for (i = 0; i < bmap->n_eq; ++i) {
3514 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3515 if (!isl_int_is_zero(s)) {
3516 isl_int_clear(s);
3517 return isl_bool_false;
3521 for (i = 0; i < bmap->n_ineq; ++i) {
3522 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3523 if (isl_int_is_neg(s)) {
3524 isl_int_clear(s);
3525 return isl_bool_false;
3529 isl_int_clear(s);
3531 return isl_bool_true;
3534 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3535 __isl_keep isl_vec *vec)
3537 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3540 struct isl_basic_map *isl_basic_map_intersect(
3541 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3543 struct isl_vec *sample = NULL;
3545 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3546 goto error;
3547 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3548 isl_space_dim(bmap1->dim, isl_dim_param) &&
3549 isl_space_dim(bmap2->dim, isl_dim_all) !=
3550 isl_space_dim(bmap2->dim, isl_dim_param))
3551 return isl_basic_map_intersect(bmap2, bmap1);
3553 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3554 isl_space_dim(bmap2->dim, isl_dim_param))
3555 isl_assert(bmap1->ctx,
3556 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3558 if (isl_basic_map_plain_is_empty(bmap1)) {
3559 isl_basic_map_free(bmap2);
3560 return bmap1;
3562 if (isl_basic_map_plain_is_empty(bmap2)) {
3563 isl_basic_map_free(bmap1);
3564 return bmap2;
3567 if (bmap1->sample &&
3568 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3569 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3570 sample = isl_vec_copy(bmap1->sample);
3571 else if (bmap2->sample &&
3572 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3573 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3574 sample = isl_vec_copy(bmap2->sample);
3576 bmap1 = isl_basic_map_cow(bmap1);
3577 if (!bmap1)
3578 goto error;
3579 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3580 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3581 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3583 if (!bmap1)
3584 isl_vec_free(sample);
3585 else if (sample) {
3586 isl_vec_free(bmap1->sample);
3587 bmap1->sample = sample;
3590 bmap1 = isl_basic_map_simplify(bmap1);
3591 return isl_basic_map_finalize(bmap1);
3592 error:
3593 if (sample)
3594 isl_vec_free(sample);
3595 isl_basic_map_free(bmap1);
3596 isl_basic_map_free(bmap2);
3597 return NULL;
3600 struct isl_basic_set *isl_basic_set_intersect(
3601 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3603 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3604 bset_to_bmap(bset2)));
3607 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3608 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3610 return isl_basic_set_intersect(bset1, bset2);
3613 /* Special case of isl_map_intersect, where both map1 and map2
3614 * are convex, without any divs and such that either map1 or map2
3615 * contains a single constraint. This constraint is then simply
3616 * added to the other map.
3618 static __isl_give isl_map *map_intersect_add_constraint(
3619 __isl_take isl_map *map1, __isl_take isl_map *map2)
3621 isl_assert(map1->ctx, map1->n == 1, goto error);
3622 isl_assert(map2->ctx, map1->n == 1, goto error);
3623 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3624 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3626 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3627 return isl_map_intersect(map2, map1);
3629 map1 = isl_map_cow(map1);
3630 if (!map1)
3631 goto error;
3632 if (isl_map_plain_is_empty(map1)) {
3633 isl_map_free(map2);
3634 return map1;
3636 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3637 if (map2->p[0]->n_eq == 1)
3638 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3639 else
3640 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3641 map2->p[0]->ineq[0]);
3643 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3644 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3645 if (!map1->p[0])
3646 goto error;
3648 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3649 isl_basic_map_free(map1->p[0]);
3650 map1->n = 0;
3653 isl_map_free(map2);
3655 return map1;
3656 error:
3657 isl_map_free(map1);
3658 isl_map_free(map2);
3659 return NULL;
3662 /* map2 may be either a parameter domain or a map living in the same
3663 * space as map1.
3665 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3666 __isl_take isl_map *map2)
3668 unsigned flags = 0;
3669 isl_map *result;
3670 int i, j;
3672 if (!map1 || !map2)
3673 goto error;
3675 if ((isl_map_plain_is_empty(map1) ||
3676 isl_map_plain_is_universe(map2)) &&
3677 isl_space_is_equal(map1->dim, map2->dim)) {
3678 isl_map_free(map2);
3679 return map1;
3681 if ((isl_map_plain_is_empty(map2) ||
3682 isl_map_plain_is_universe(map1)) &&
3683 isl_space_is_equal(map1->dim, map2->dim)) {
3684 isl_map_free(map1);
3685 return map2;
3688 if (map1->n == 1 && map2->n == 1 &&
3689 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3690 isl_space_is_equal(map1->dim, map2->dim) &&
3691 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3692 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3693 return map_intersect_add_constraint(map1, map2);
3695 if (isl_space_dim(map2->dim, isl_dim_all) !=
3696 isl_space_dim(map2->dim, isl_dim_param))
3697 isl_assert(map1->ctx,
3698 isl_space_is_equal(map1->dim, map2->dim), goto error);
3700 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3701 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3702 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3704 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3705 map1->n * map2->n, flags);
3706 if (!result)
3707 goto error;
3708 for (i = 0; i < map1->n; ++i)
3709 for (j = 0; j < map2->n; ++j) {
3710 struct isl_basic_map *part;
3711 part = isl_basic_map_intersect(
3712 isl_basic_map_copy(map1->p[i]),
3713 isl_basic_map_copy(map2->p[j]));
3714 if (isl_basic_map_is_empty(part) < 0)
3715 part = isl_basic_map_free(part);
3716 result = isl_map_add_basic_map(result, part);
3717 if (!result)
3718 goto error;
3720 isl_map_free(map1);
3721 isl_map_free(map2);
3722 return result;
3723 error:
3724 isl_map_free(map1);
3725 isl_map_free(map2);
3726 return NULL;
3729 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3730 __isl_take isl_map *map2)
3732 if (!map1 || !map2)
3733 goto error;
3734 if (!isl_space_is_equal(map1->dim, map2->dim))
3735 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3736 "spaces don't match", goto error);
3737 return map_intersect_internal(map1, map2);
3738 error:
3739 isl_map_free(map1);
3740 isl_map_free(map2);
3741 return NULL;
3744 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3745 __isl_take isl_map *map2)
3747 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3750 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3752 return set_from_map(isl_map_intersect(set_to_map(set1),
3753 set_to_map(set2)));
3756 /* map_intersect_internal accepts intersections
3757 * with parameter domains, so we can just call that function.
3759 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3760 __isl_take isl_set *params)
3762 return map_intersect_internal(map, params);
3765 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3766 __isl_take isl_map *map2)
3768 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3771 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3772 __isl_take isl_set *params)
3774 return isl_map_intersect_params(set, params);
3777 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3779 isl_space *space;
3780 unsigned pos, n1, n2;
3782 if (!bmap)
3783 return NULL;
3784 bmap = isl_basic_map_cow(bmap);
3785 if (!bmap)
3786 return NULL;
3787 space = isl_space_reverse(isl_space_copy(bmap->dim));
3788 pos = isl_basic_map_offset(bmap, isl_dim_in);
3789 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3790 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3791 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3792 return isl_basic_map_reset_space(bmap, space);
3795 static __isl_give isl_basic_map *basic_map_space_reset(
3796 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3798 isl_space *space;
3800 if (!bmap)
3801 return NULL;
3802 if (!isl_space_is_named_or_nested(bmap->dim, type))
3803 return bmap;
3805 space = isl_basic_map_get_space(bmap);
3806 space = isl_space_reset(space, type);
3807 bmap = isl_basic_map_reset_space(bmap, space);
3808 return bmap;
3811 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3812 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3813 unsigned pos, unsigned n)
3815 isl_bool rational;
3816 isl_space *res_dim;
3817 struct isl_basic_map *res;
3818 struct isl_dim_map *dim_map;
3819 unsigned total, off;
3820 enum isl_dim_type t;
3822 if (n == 0)
3823 return basic_map_space_reset(bmap, type);
3825 if (!bmap)
3826 return NULL;
3828 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3830 total = isl_basic_map_total_dim(bmap) + n;
3831 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3832 off = 0;
3833 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3834 if (t != type) {
3835 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3836 } else {
3837 unsigned size = isl_basic_map_dim(bmap, t);
3838 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3839 0, pos, off);
3840 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3841 pos, size - pos, off + pos + n);
3843 off += isl_space_dim(res_dim, t);
3845 isl_dim_map_div(dim_map, bmap, off);
3847 res = isl_basic_map_alloc_space(res_dim,
3848 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3849 rational = isl_basic_map_is_rational(bmap);
3850 if (rational < 0)
3851 res = isl_basic_map_free(res);
3852 if (rational)
3853 res = isl_basic_map_set_rational(res);
3854 if (isl_basic_map_plain_is_empty(bmap)) {
3855 isl_basic_map_free(bmap);
3856 free(dim_map);
3857 return isl_basic_map_set_to_empty(res);
3859 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3860 return isl_basic_map_finalize(res);
3863 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3864 __isl_take isl_basic_set *bset,
3865 enum isl_dim_type type, unsigned pos, unsigned n)
3867 return isl_basic_map_insert_dims(bset, type, pos, n);
3870 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3871 enum isl_dim_type type, unsigned n)
3873 if (!bmap)
3874 return NULL;
3875 return isl_basic_map_insert_dims(bmap, type,
3876 isl_basic_map_dim(bmap, type), n);
3879 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3880 enum isl_dim_type type, unsigned n)
3882 if (!bset)
3883 return NULL;
3884 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3885 return isl_basic_map_add_dims(bset, type, n);
3886 error:
3887 isl_basic_set_free(bset);
3888 return NULL;
3891 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3892 enum isl_dim_type type)
3894 isl_space *space;
3896 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3897 return map;
3899 space = isl_map_get_space(map);
3900 space = isl_space_reset(space, type);
3901 map = isl_map_reset_space(map, space);
3902 return map;
3905 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3906 enum isl_dim_type type, unsigned pos, unsigned n)
3908 int i;
3910 if (n == 0)
3911 return map_space_reset(map, type);
3913 map = isl_map_cow(map);
3914 if (!map)
3915 return NULL;
3917 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3918 if (!map->dim)
3919 goto error;
3921 for (i = 0; i < map->n; ++i) {
3922 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3923 if (!map->p[i])
3924 goto error;
3927 return map;
3928 error:
3929 isl_map_free(map);
3930 return NULL;
3933 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3934 enum isl_dim_type type, unsigned pos, unsigned n)
3936 return isl_map_insert_dims(set, type, pos, n);
3939 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3940 enum isl_dim_type type, unsigned n)
3942 if (!map)
3943 return NULL;
3944 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3947 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3948 enum isl_dim_type type, unsigned n)
3950 if (!set)
3951 return NULL;
3952 isl_assert(set->ctx, type != isl_dim_in, goto error);
3953 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3954 error:
3955 isl_set_free(set);
3956 return NULL;
3959 __isl_give isl_basic_map *isl_basic_map_move_dims(
3960 __isl_take isl_basic_map *bmap,
3961 enum isl_dim_type dst_type, unsigned dst_pos,
3962 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3964 struct isl_dim_map *dim_map;
3965 struct isl_basic_map *res;
3966 enum isl_dim_type t;
3967 unsigned total, off;
3969 if (!bmap)
3970 return NULL;
3971 if (n == 0) {
3972 bmap = isl_basic_map_reset(bmap, src_type);
3973 bmap = isl_basic_map_reset(bmap, dst_type);
3974 return bmap;
3977 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3978 return isl_basic_map_free(bmap);
3980 if (dst_type == src_type && dst_pos == src_pos)
3981 return bmap;
3983 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3985 if (pos(bmap->dim, dst_type) + dst_pos ==
3986 pos(bmap->dim, src_type) + src_pos +
3987 ((src_type < dst_type) ? n : 0)) {
3988 bmap = isl_basic_map_cow(bmap);
3989 if (!bmap)
3990 return NULL;
3992 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3993 src_type, src_pos, n);
3994 if (!bmap->dim)
3995 goto error;
3997 bmap = isl_basic_map_finalize(bmap);
3999 return bmap;
4002 total = isl_basic_map_total_dim(bmap);
4003 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4005 off = 0;
4006 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4007 unsigned size = isl_space_dim(bmap->dim, t);
4008 if (t == dst_type) {
4009 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4010 0, dst_pos, off);
4011 off += dst_pos;
4012 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4013 src_pos, n, off);
4014 off += n;
4015 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4016 dst_pos, size - dst_pos, off);
4017 off += size - dst_pos;
4018 } else if (t == src_type) {
4019 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4020 0, src_pos, off);
4021 off += src_pos;
4022 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4023 src_pos + n, size - src_pos - n, off);
4024 off += size - src_pos - n;
4025 } else {
4026 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4027 off += size;
4030 isl_dim_map_div(dim_map, bmap, off);
4032 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4033 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4034 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4035 if (!bmap)
4036 goto error;
4038 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4039 src_type, src_pos, n);
4040 if (!bmap->dim)
4041 goto error;
4043 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
4044 bmap = isl_basic_map_gauss(bmap, NULL);
4045 bmap = isl_basic_map_finalize(bmap);
4047 return bmap;
4048 error:
4049 isl_basic_map_free(bmap);
4050 return NULL;
4053 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4054 enum isl_dim_type dst_type, unsigned dst_pos,
4055 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4057 isl_basic_map *bmap = bset_to_bmap(bset);
4058 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4059 src_type, src_pos, n);
4060 return bset_from_bmap(bmap);
4063 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4064 enum isl_dim_type dst_type, unsigned dst_pos,
4065 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4067 if (!set)
4068 return NULL;
4069 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4070 return set_from_map(isl_map_move_dims(set_to_map(set),
4071 dst_type, dst_pos, src_type, src_pos, n));
4072 error:
4073 isl_set_free(set);
4074 return NULL;
4077 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4078 enum isl_dim_type dst_type, unsigned dst_pos,
4079 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4081 int i;
4083 if (!map)
4084 return NULL;
4085 if (n == 0) {
4086 map = isl_map_reset(map, src_type);
4087 map = isl_map_reset(map, dst_type);
4088 return map;
4091 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
4092 goto error);
4094 if (dst_type == src_type && dst_pos == src_pos)
4095 return map;
4097 isl_assert(map->ctx, dst_type != src_type, goto error);
4099 map = isl_map_cow(map);
4100 if (!map)
4101 return NULL;
4103 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4104 if (!map->dim)
4105 goto error;
4107 for (i = 0; i < map->n; ++i) {
4108 map->p[i] = isl_basic_map_move_dims(map->p[i],
4109 dst_type, dst_pos,
4110 src_type, src_pos, n);
4111 if (!map->p[i])
4112 goto error;
4115 return map;
4116 error:
4117 isl_map_free(map);
4118 return NULL;
4121 /* Move the specified dimensions to the last columns right before
4122 * the divs. Don't change the dimension specification of bmap.
4123 * That's the responsibility of the caller.
4125 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4126 enum isl_dim_type type, unsigned first, unsigned n)
4128 struct isl_dim_map *dim_map;
4129 struct isl_basic_map *res;
4130 enum isl_dim_type t;
4131 unsigned total, off;
4133 if (!bmap)
4134 return NULL;
4135 if (pos(bmap->dim, type) + first + n ==
4136 1 + isl_space_dim(bmap->dim, isl_dim_all))
4137 return bmap;
4139 total = isl_basic_map_total_dim(bmap);
4140 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4142 off = 0;
4143 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4144 unsigned size = isl_space_dim(bmap->dim, t);
4145 if (t == type) {
4146 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4147 0, first, off);
4148 off += first;
4149 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4150 first, n, total - bmap->n_div - n);
4151 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4152 first + n, size - (first + n), off);
4153 off += size - (first + n);
4154 } else {
4155 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4156 off += size;
4159 isl_dim_map_div(dim_map, bmap, off + n);
4161 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4162 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4163 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4164 return res;
4167 /* Insert "n" rows in the divs of "bmap".
4169 * The number of columns is not changed, which means that the last
4170 * dimensions of "bmap" are being reintepreted as the new divs.
4171 * The space of "bmap" is not adjusted, however, which means
4172 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4173 * from the space of "bmap" is the responsibility of the caller.
4175 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4176 int n)
4178 int i;
4179 size_t row_size;
4180 isl_int **new_div;
4181 isl_int *old;
4183 bmap = isl_basic_map_cow(bmap);
4184 if (!bmap)
4185 return NULL;
4187 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4188 old = bmap->block2.data;
4189 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4190 (bmap->extra + n) * (1 + row_size));
4191 if (!bmap->block2.data)
4192 return isl_basic_map_free(bmap);
4193 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4194 if (!new_div)
4195 return isl_basic_map_free(bmap);
4196 for (i = 0; i < n; ++i) {
4197 new_div[i] = bmap->block2.data +
4198 (bmap->extra + i) * (1 + row_size);
4199 isl_seq_clr(new_div[i], 1 + row_size);
4201 for (i = 0; i < bmap->extra; ++i)
4202 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4203 free(bmap->div);
4204 bmap->div = new_div;
4205 bmap->n_div += n;
4206 bmap->extra += n;
4208 return bmap;
4211 /* Drop constraints from "bmap" that only involve the variables
4212 * of "type" in the range [first, first + n] that are not related
4213 * to any of the variables outside that interval.
4214 * These constraints cannot influence the values for the variables
4215 * outside the interval, except in case they cause "bmap" to be empty.
4216 * Only drop the constraints if "bmap" is known to be non-empty.
4218 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4219 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4220 unsigned first, unsigned n)
4222 int i;
4223 int *groups;
4224 unsigned dim, n_div;
4225 isl_bool non_empty;
4227 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4228 if (non_empty < 0)
4229 return isl_basic_map_free(bmap);
4230 if (!non_empty)
4231 return bmap;
4233 dim = isl_basic_map_dim(bmap, isl_dim_all);
4234 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4235 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4236 if (!groups)
4237 return isl_basic_map_free(bmap);
4238 first += isl_basic_map_offset(bmap, type) - 1;
4239 for (i = 0; i < first; ++i)
4240 groups[i] = -1;
4241 for (i = first + n; i < dim - n_div; ++i)
4242 groups[i] = -1;
4244 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4246 return bmap;
4249 /* Turn the n dimensions of type type, starting at first
4250 * into existentially quantified variables.
4252 * If a subset of the projected out variables are unrelated
4253 * to any of the variables that remain, then the constraints
4254 * involving this subset are simply dropped first.
4256 __isl_give isl_basic_map *isl_basic_map_project_out(
4257 __isl_take isl_basic_map *bmap,
4258 enum isl_dim_type type, unsigned first, unsigned n)
4260 isl_bool empty;
4262 if (n == 0)
4263 return basic_map_space_reset(bmap, type);
4264 if (type == isl_dim_div)
4265 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4266 "cannot project out existentially quantified variables",
4267 return isl_basic_map_free(bmap));
4269 empty = isl_basic_map_plain_is_empty(bmap);
4270 if (empty < 0)
4271 return isl_basic_map_free(bmap);
4272 if (empty)
4273 bmap = isl_basic_map_set_to_empty(bmap);
4275 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4276 if (!bmap)
4277 return NULL;
4279 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4280 return isl_basic_map_remove_dims(bmap, type, first, n);
4282 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4283 return isl_basic_map_free(bmap);
4285 bmap = move_last(bmap, type, first, n);
4286 bmap = isl_basic_map_cow(bmap);
4287 bmap = insert_div_rows(bmap, n);
4288 if (!bmap)
4289 return NULL;
4291 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4292 if (!bmap->dim)
4293 goto error;
4294 bmap = isl_basic_map_simplify(bmap);
4295 bmap = isl_basic_map_drop_redundant_divs(bmap);
4296 return isl_basic_map_finalize(bmap);
4297 error:
4298 isl_basic_map_free(bmap);
4299 return NULL;
4302 /* Turn the n dimensions of type type, starting at first
4303 * into existentially quantified variables.
4305 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4306 enum isl_dim_type type, unsigned first, unsigned n)
4308 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4309 type, first, n));
4312 /* Turn the n dimensions of type type, starting at first
4313 * into existentially quantified variables.
4315 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4316 enum isl_dim_type type, unsigned first, unsigned n)
4318 int i;
4320 if (!map)
4321 return NULL;
4323 if (n == 0)
4324 return map_space_reset(map, type);
4326 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
4328 map = isl_map_cow(map);
4329 if (!map)
4330 return NULL;
4332 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4333 if (!map->dim)
4334 goto error;
4336 for (i = 0; i < map->n; ++i) {
4337 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4338 if (!map->p[i])
4339 goto error;
4342 return map;
4343 error:
4344 isl_map_free(map);
4345 return NULL;
4348 /* Turn the n dimensions of type type, starting at first
4349 * into existentially quantified variables.
4351 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4352 enum isl_dim_type type, unsigned first, unsigned n)
4354 return set_from_map(isl_map_project_out(set_to_map(set),
4355 type, first, n));
4358 /* Return a map that projects the elements in "set" onto their
4359 * "n" set dimensions starting at "first".
4360 * "type" should be equal to isl_dim_set.
4362 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4363 enum isl_dim_type type, unsigned first, unsigned n)
4365 int i;
4366 int dim;
4367 isl_map *map;
4369 if (!set)
4370 return NULL;
4371 if (type != isl_dim_set)
4372 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4373 "only set dimensions can be projected out", goto error);
4374 dim = isl_set_dim(set, isl_dim_set);
4375 if (first + n > dim || first + n < first)
4376 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4377 "index out of bounds", goto error);
4379 map = isl_map_from_domain(set);
4380 map = isl_map_add_dims(map, isl_dim_out, n);
4381 for (i = 0; i < n; ++i)
4382 map = isl_map_equate(map, isl_dim_in, first + i,
4383 isl_dim_out, i);
4384 return map;
4385 error:
4386 isl_set_free(set);
4387 return NULL;
4390 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4392 int i, j;
4394 for (i = 0; i < n; ++i) {
4395 j = isl_basic_map_alloc_div(bmap);
4396 if (j < 0)
4397 goto error;
4398 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4400 return bmap;
4401 error:
4402 isl_basic_map_free(bmap);
4403 return NULL;
4406 struct isl_basic_map *isl_basic_map_apply_range(
4407 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4409 isl_space *dim_result = NULL;
4410 struct isl_basic_map *bmap;
4411 unsigned n_in, n_out, n, nparam, total, pos;
4412 struct isl_dim_map *dim_map1, *dim_map2;
4414 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4415 goto error;
4416 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4417 bmap2->dim, isl_dim_in))
4418 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4419 "spaces don't match", goto error);
4421 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4422 isl_space_copy(bmap2->dim));
4424 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4425 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4426 n = isl_basic_map_dim(bmap1, isl_dim_out);
4427 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4429 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4430 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4431 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4432 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4433 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4434 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4435 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4436 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4437 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4438 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4439 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4441 bmap = isl_basic_map_alloc_space(dim_result,
4442 bmap1->n_div + bmap2->n_div + n,
4443 bmap1->n_eq + bmap2->n_eq,
4444 bmap1->n_ineq + bmap2->n_ineq);
4445 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4446 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4447 bmap = add_divs(bmap, n);
4448 bmap = isl_basic_map_simplify(bmap);
4449 bmap = isl_basic_map_drop_redundant_divs(bmap);
4450 return isl_basic_map_finalize(bmap);
4451 error:
4452 isl_basic_map_free(bmap1);
4453 isl_basic_map_free(bmap2);
4454 return NULL;
4457 struct isl_basic_set *isl_basic_set_apply(
4458 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4460 if (!bset || !bmap)
4461 goto error;
4463 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4464 goto error);
4466 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4467 bmap));
4468 error:
4469 isl_basic_set_free(bset);
4470 isl_basic_map_free(bmap);
4471 return NULL;
4474 struct isl_basic_map *isl_basic_map_apply_domain(
4475 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4477 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4478 goto error;
4479 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4480 bmap2->dim, isl_dim_in))
4481 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4482 "spaces don't match", goto error);
4484 bmap1 = isl_basic_map_reverse(bmap1);
4485 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4486 return isl_basic_map_reverse(bmap1);
4487 error:
4488 isl_basic_map_free(bmap1);
4489 isl_basic_map_free(bmap2);
4490 return NULL;
4493 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4494 * A \cap B -> f(A) + f(B)
4496 struct isl_basic_map *isl_basic_map_sum(
4497 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4499 unsigned n_in, n_out, nparam, total, pos;
4500 struct isl_basic_map *bmap = NULL;
4501 struct isl_dim_map *dim_map1, *dim_map2;
4502 int i;
4504 if (!bmap1 || !bmap2)
4505 goto error;
4507 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4508 goto error);
4510 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4511 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4512 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4514 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4515 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4516 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4517 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4518 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4519 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4520 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4521 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4522 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4523 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4524 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4526 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4527 bmap1->n_div + bmap2->n_div + 2 * n_out,
4528 bmap1->n_eq + bmap2->n_eq + n_out,
4529 bmap1->n_ineq + bmap2->n_ineq);
4530 for (i = 0; i < n_out; ++i) {
4531 int j = isl_basic_map_alloc_equality(bmap);
4532 if (j < 0)
4533 goto error;
4534 isl_seq_clr(bmap->eq[j], 1+total);
4535 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4536 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4537 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4539 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4540 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4541 bmap = add_divs(bmap, 2 * n_out);
4543 bmap = isl_basic_map_simplify(bmap);
4544 return isl_basic_map_finalize(bmap);
4545 error:
4546 isl_basic_map_free(bmap);
4547 isl_basic_map_free(bmap1);
4548 isl_basic_map_free(bmap2);
4549 return NULL;
4552 /* Given two maps A -> f(A) and B -> g(B), construct a map
4553 * A \cap B -> f(A) + f(B)
4555 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
4557 struct isl_map *result;
4558 int i, j;
4560 if (!map1 || !map2)
4561 goto error;
4563 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4565 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4566 map1->n * map2->n, 0);
4567 if (!result)
4568 goto error;
4569 for (i = 0; i < map1->n; ++i)
4570 for (j = 0; j < map2->n; ++j) {
4571 struct isl_basic_map *part;
4572 part = isl_basic_map_sum(
4573 isl_basic_map_copy(map1->p[i]),
4574 isl_basic_map_copy(map2->p[j]));
4575 if (isl_basic_map_is_empty(part))
4576 isl_basic_map_free(part);
4577 else
4578 result = isl_map_add_basic_map(result, part);
4579 if (!result)
4580 goto error;
4582 isl_map_free(map1);
4583 isl_map_free(map2);
4584 return result;
4585 error:
4586 isl_map_free(map1);
4587 isl_map_free(map2);
4588 return NULL;
4591 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4592 __isl_take isl_set *set2)
4594 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4597 /* Given a basic map A -> f(A), construct A -> -f(A).
4599 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4601 int i, j;
4602 unsigned off, n;
4604 bmap = isl_basic_map_cow(bmap);
4605 if (!bmap)
4606 return NULL;
4608 n = isl_basic_map_dim(bmap, isl_dim_out);
4609 off = isl_basic_map_offset(bmap, isl_dim_out);
4610 for (i = 0; i < bmap->n_eq; ++i)
4611 for (j = 0; j < n; ++j)
4612 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4613 for (i = 0; i < bmap->n_ineq; ++i)
4614 for (j = 0; j < n; ++j)
4615 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4616 for (i = 0; i < bmap->n_div; ++i)
4617 for (j = 0; j < n; ++j)
4618 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4619 bmap = isl_basic_map_gauss(bmap, NULL);
4620 return isl_basic_map_finalize(bmap);
4623 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4625 return isl_basic_map_neg(bset);
4628 /* Given a map A -> f(A), construct A -> -f(A).
4630 struct isl_map *isl_map_neg(struct isl_map *map)
4632 int i;
4634 map = isl_map_cow(map);
4635 if (!map)
4636 return NULL;
4638 for (i = 0; i < map->n; ++i) {
4639 map->p[i] = isl_basic_map_neg(map->p[i]);
4640 if (!map->p[i])
4641 goto error;
4644 return map;
4645 error:
4646 isl_map_free(map);
4647 return NULL;
4650 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4652 return set_from_map(isl_map_neg(set_to_map(set)));
4655 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4656 * A -> floor(f(A)/d).
4658 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4659 isl_int d)
4661 unsigned n_in, n_out, nparam, total, pos;
4662 struct isl_basic_map *result = NULL;
4663 struct isl_dim_map *dim_map;
4664 int i;
4666 if (!bmap)
4667 return NULL;
4669 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4670 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4671 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4673 total = nparam + n_in + n_out + bmap->n_div + n_out;
4674 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4675 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4676 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4677 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4678 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4680 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4681 bmap->n_div + n_out,
4682 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4683 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4684 result = add_divs(result, n_out);
4685 for (i = 0; i < n_out; ++i) {
4686 int j;
4687 j = isl_basic_map_alloc_inequality(result);
4688 if (j < 0)
4689 goto error;
4690 isl_seq_clr(result->ineq[j], 1+total);
4691 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4692 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4693 j = isl_basic_map_alloc_inequality(result);
4694 if (j < 0)
4695 goto error;
4696 isl_seq_clr(result->ineq[j], 1+total);
4697 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4698 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4699 isl_int_sub_ui(result->ineq[j][0], d, 1);
4702 result = isl_basic_map_simplify(result);
4703 return isl_basic_map_finalize(result);
4704 error:
4705 isl_basic_map_free(result);
4706 return NULL;
4709 /* Given a map A -> f(A) and an integer d, construct a map
4710 * A -> floor(f(A)/d).
4712 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4714 int i;
4716 map = isl_map_cow(map);
4717 if (!map)
4718 return NULL;
4720 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4721 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4722 for (i = 0; i < map->n; ++i) {
4723 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4724 if (!map->p[i])
4725 goto error;
4728 return map;
4729 error:
4730 isl_map_free(map);
4731 return NULL;
4734 /* Given a map A -> f(A) and an integer d, construct a map
4735 * A -> floor(f(A)/d).
4737 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4738 __isl_take isl_val *d)
4740 if (!map || !d)
4741 goto error;
4742 if (!isl_val_is_int(d))
4743 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4744 "expecting integer denominator", goto error);
4745 map = isl_map_floordiv(map, d->n);
4746 isl_val_free(d);
4747 return map;
4748 error:
4749 isl_map_free(map);
4750 isl_val_free(d);
4751 return NULL;
4754 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4755 unsigned pos)
4757 int i;
4758 unsigned nparam;
4759 unsigned n_in;
4761 i = isl_basic_map_alloc_equality(bmap);
4762 if (i < 0)
4763 goto error;
4764 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4765 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4766 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4767 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4768 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4769 return isl_basic_map_finalize(bmap);
4770 error:
4771 isl_basic_map_free(bmap);
4772 return NULL;
4775 /* Add a constraint to "bmap" expressing i_pos < o_pos
4777 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4778 unsigned pos)
4780 int i;
4781 unsigned nparam;
4782 unsigned n_in;
4784 i = isl_basic_map_alloc_inequality(bmap);
4785 if (i < 0)
4786 goto error;
4787 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4788 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4789 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4790 isl_int_set_si(bmap->ineq[i][0], -1);
4791 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4792 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4793 return isl_basic_map_finalize(bmap);
4794 error:
4795 isl_basic_map_free(bmap);
4796 return NULL;
4799 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4801 static __isl_give isl_basic_map *var_less_or_equal(
4802 __isl_take isl_basic_map *bmap, unsigned pos)
4804 int i;
4805 unsigned nparam;
4806 unsigned n_in;
4808 i = isl_basic_map_alloc_inequality(bmap);
4809 if (i < 0)
4810 goto error;
4811 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4812 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4813 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4814 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4815 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4816 return isl_basic_map_finalize(bmap);
4817 error:
4818 isl_basic_map_free(bmap);
4819 return NULL;
4822 /* Add a constraint to "bmap" expressing i_pos > o_pos
4824 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4825 unsigned pos)
4827 int i;
4828 unsigned nparam;
4829 unsigned n_in;
4831 i = isl_basic_map_alloc_inequality(bmap);
4832 if (i < 0)
4833 goto error;
4834 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4835 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4836 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4837 isl_int_set_si(bmap->ineq[i][0], -1);
4838 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4839 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4840 return isl_basic_map_finalize(bmap);
4841 error:
4842 isl_basic_map_free(bmap);
4843 return NULL;
4846 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4848 static __isl_give isl_basic_map *var_more_or_equal(
4849 __isl_take isl_basic_map *bmap, unsigned pos)
4851 int i;
4852 unsigned nparam;
4853 unsigned n_in;
4855 i = isl_basic_map_alloc_inequality(bmap);
4856 if (i < 0)
4857 goto error;
4858 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4859 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4860 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4861 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4862 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4863 return isl_basic_map_finalize(bmap);
4864 error:
4865 isl_basic_map_free(bmap);
4866 return NULL;
4869 __isl_give isl_basic_map *isl_basic_map_equal(
4870 __isl_take isl_space *dim, unsigned n_equal)
4872 int i;
4873 struct isl_basic_map *bmap;
4874 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4875 if (!bmap)
4876 return NULL;
4877 for (i = 0; i < n_equal && bmap; ++i)
4878 bmap = var_equal(bmap, i);
4879 return isl_basic_map_finalize(bmap);
4882 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4884 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4885 unsigned pos)
4887 int i;
4888 struct isl_basic_map *bmap;
4889 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4890 if (!bmap)
4891 return NULL;
4892 for (i = 0; i < pos && bmap; ++i)
4893 bmap = var_equal(bmap, i);
4894 if (bmap)
4895 bmap = var_less(bmap, pos);
4896 return isl_basic_map_finalize(bmap);
4899 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4901 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4902 __isl_take isl_space *dim, unsigned pos)
4904 int i;
4905 isl_basic_map *bmap;
4907 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4908 for (i = 0; i < pos; ++i)
4909 bmap = var_equal(bmap, i);
4910 bmap = var_less_or_equal(bmap, pos);
4911 return isl_basic_map_finalize(bmap);
4914 /* Return a relation on "dim" expressing i_pos > o_pos
4916 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4917 unsigned pos)
4919 int i;
4920 struct isl_basic_map *bmap;
4921 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4922 if (!bmap)
4923 return NULL;
4924 for (i = 0; i < pos && bmap; ++i)
4925 bmap = var_equal(bmap, i);
4926 if (bmap)
4927 bmap = var_more(bmap, pos);
4928 return isl_basic_map_finalize(bmap);
4931 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4933 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4934 __isl_take isl_space *dim, unsigned pos)
4936 int i;
4937 isl_basic_map *bmap;
4939 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4940 for (i = 0; i < pos; ++i)
4941 bmap = var_equal(bmap, i);
4942 bmap = var_more_or_equal(bmap, pos);
4943 return isl_basic_map_finalize(bmap);
4946 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4947 unsigned n, int equal)
4949 struct isl_map *map;
4950 int i;
4952 if (n == 0 && equal)
4953 return isl_map_universe(dims);
4955 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4957 for (i = 0; i + 1 < n; ++i)
4958 map = isl_map_add_basic_map(map,
4959 isl_basic_map_less_at(isl_space_copy(dims), i));
4960 if (n > 0) {
4961 if (equal)
4962 map = isl_map_add_basic_map(map,
4963 isl_basic_map_less_or_equal_at(dims, n - 1));
4964 else
4965 map = isl_map_add_basic_map(map,
4966 isl_basic_map_less_at(dims, n - 1));
4967 } else
4968 isl_space_free(dims);
4970 return map;
4973 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4975 if (!dims)
4976 return NULL;
4977 return map_lex_lte_first(dims, dims->n_out, equal);
4980 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4982 return map_lex_lte_first(dim, n, 0);
4985 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4987 return map_lex_lte_first(dim, n, 1);
4990 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4992 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4995 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4997 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5000 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
5001 unsigned n, int equal)
5003 struct isl_map *map;
5004 int i;
5006 if (n == 0 && equal)
5007 return isl_map_universe(dims);
5009 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
5011 for (i = 0; i + 1 < n; ++i)
5012 map = isl_map_add_basic_map(map,
5013 isl_basic_map_more_at(isl_space_copy(dims), i));
5014 if (n > 0) {
5015 if (equal)
5016 map = isl_map_add_basic_map(map,
5017 isl_basic_map_more_or_equal_at(dims, n - 1));
5018 else
5019 map = isl_map_add_basic_map(map,
5020 isl_basic_map_more_at(dims, n - 1));
5021 } else
5022 isl_space_free(dims);
5024 return map;
5027 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
5029 if (!dims)
5030 return NULL;
5031 return map_lex_gte_first(dims, dims->n_out, equal);
5034 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5036 return map_lex_gte_first(dim, n, 0);
5039 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5041 return map_lex_gte_first(dim, n, 1);
5044 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5046 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5049 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5051 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5054 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5055 __isl_take isl_set *set2)
5057 isl_map *map;
5058 map = isl_map_lex_le(isl_set_get_space(set1));
5059 map = isl_map_intersect_domain(map, set1);
5060 map = isl_map_intersect_range(map, set2);
5061 return map;
5064 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5065 __isl_take isl_set *set2)
5067 isl_map *map;
5068 map = isl_map_lex_lt(isl_set_get_space(set1));
5069 map = isl_map_intersect_domain(map, set1);
5070 map = isl_map_intersect_range(map, set2);
5071 return map;
5074 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5075 __isl_take isl_set *set2)
5077 isl_map *map;
5078 map = isl_map_lex_ge(isl_set_get_space(set1));
5079 map = isl_map_intersect_domain(map, set1);
5080 map = isl_map_intersect_range(map, set2);
5081 return map;
5084 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5085 __isl_take isl_set *set2)
5087 isl_map *map;
5088 map = isl_map_lex_gt(isl_set_get_space(set1));
5089 map = isl_map_intersect_domain(map, set1);
5090 map = isl_map_intersect_range(map, set2);
5091 return map;
5094 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5095 __isl_take isl_map *map2)
5097 isl_map *map;
5098 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5099 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5100 map = isl_map_apply_range(map, isl_map_reverse(map2));
5101 return map;
5104 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5105 __isl_take isl_map *map2)
5107 isl_map *map;
5108 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5109 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5110 map = isl_map_apply_range(map, isl_map_reverse(map2));
5111 return map;
5114 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5115 __isl_take isl_map *map2)
5117 isl_map *map;
5118 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5119 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5120 map = isl_map_apply_range(map, isl_map_reverse(map2));
5121 return map;
5124 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5125 __isl_take isl_map *map2)
5127 isl_map *map;
5128 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5129 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5130 map = isl_map_apply_range(map, isl_map_reverse(map2));
5131 return map;
5134 /* For a div d = floor(f/m), add the constraint
5136 * f - m d >= 0
5138 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5139 unsigned pos, isl_int *div)
5141 int i;
5142 unsigned total = isl_basic_map_total_dim(bmap);
5144 i = isl_basic_map_alloc_inequality(bmap);
5145 if (i < 0)
5146 return isl_stat_error;
5147 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5148 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
5150 return isl_stat_ok;
5153 /* For a div d = floor(f/m), add the constraint
5155 * -(f-(m-1)) + m d >= 0
5157 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5158 unsigned pos, isl_int *div)
5160 int i;
5161 unsigned total = isl_basic_map_total_dim(bmap);
5163 i = isl_basic_map_alloc_inequality(bmap);
5164 if (i < 0)
5165 return isl_stat_error;
5166 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5167 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
5168 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5169 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5171 return isl_stat_ok;
5174 /* For a div d = floor(f/m), add the constraints
5176 * f - m d >= 0
5177 * -(f-(m-1)) + m d >= 0
5179 * Note that the second constraint is the negation of
5181 * f - m d >= m
5183 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5184 unsigned pos, isl_int *div)
5186 if (add_upper_div_constraint(bmap, pos, div) < 0)
5187 return -1;
5188 if (add_lower_div_constraint(bmap, pos, div) < 0)
5189 return -1;
5190 return 0;
5193 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
5194 unsigned pos, isl_int *div)
5196 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5197 pos, div);
5200 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
5202 unsigned total = isl_basic_map_total_dim(bmap);
5203 unsigned div_pos = total - bmap->n_div + div;
5205 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5206 bmap->div[div]);
5209 /* For each known div d = floor(f/m), add the constraints
5211 * f - m d >= 0
5212 * -(f-(m-1)) + m d >= 0
5214 * Remove duplicate constraints in case of some these div constraints
5215 * already appear in "bmap".
5217 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5218 __isl_take isl_basic_map *bmap)
5220 unsigned n_div;
5222 if (!bmap)
5223 return NULL;
5224 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5225 if (n_div == 0)
5226 return bmap;
5228 bmap = add_known_div_constraints(bmap);
5229 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5230 bmap = isl_basic_map_finalize(bmap);
5231 return bmap;
5234 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5236 * In particular, if this div is of the form d = floor(f/m),
5237 * then add the constraint
5239 * f - m d >= 0
5241 * if sign < 0 or the constraint
5243 * -(f-(m-1)) + m d >= 0
5245 * if sign > 0.
5247 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5248 unsigned div, int sign)
5250 unsigned total;
5251 unsigned div_pos;
5253 if (!bmap)
5254 return -1;
5256 total = isl_basic_map_total_dim(bmap);
5257 div_pos = total - bmap->n_div + div;
5259 if (sign < 0)
5260 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5261 else
5262 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5265 struct isl_basic_set *isl_basic_map_underlying_set(
5266 struct isl_basic_map *bmap)
5268 if (!bmap)
5269 goto error;
5270 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5271 bmap->n_div == 0 &&
5272 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5273 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5274 return bset_from_bmap(bmap);
5275 bmap = isl_basic_map_cow(bmap);
5276 if (!bmap)
5277 goto error;
5278 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5279 if (!bmap->dim)
5280 goto error;
5281 bmap->extra -= bmap->n_div;
5282 bmap->n_div = 0;
5283 bmap = isl_basic_map_finalize(bmap);
5284 return bset_from_bmap(bmap);
5285 error:
5286 isl_basic_map_free(bmap);
5287 return NULL;
5290 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5291 __isl_take isl_basic_set *bset)
5293 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5296 /* Replace each element in "list" by the result of applying
5297 * isl_basic_map_underlying_set to the element.
5299 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5300 __isl_take isl_basic_map_list *list)
5302 int i, n;
5304 if (!list)
5305 return NULL;
5307 n = isl_basic_map_list_n_basic_map(list);
5308 for (i = 0; i < n; ++i) {
5309 isl_basic_map *bmap;
5310 isl_basic_set *bset;
5312 bmap = isl_basic_map_list_get_basic_map(list, i);
5313 bset = isl_basic_set_underlying_set(bmap);
5314 list = isl_basic_set_list_set_basic_set(list, i, bset);
5317 return list;
5320 struct isl_basic_map *isl_basic_map_overlying_set(
5321 struct isl_basic_set *bset, struct isl_basic_map *like)
5323 struct isl_basic_map *bmap;
5324 struct isl_ctx *ctx;
5325 unsigned total;
5326 int i;
5328 if (!bset || !like)
5329 goto error;
5330 ctx = bset->ctx;
5331 isl_assert(ctx, bset->n_div == 0, goto error);
5332 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5333 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5334 goto error);
5335 if (like->n_div == 0) {
5336 isl_space *space = isl_basic_map_get_space(like);
5337 isl_basic_map_free(like);
5338 return isl_basic_map_reset_space(bset, space);
5340 bset = isl_basic_set_cow(bset);
5341 if (!bset)
5342 goto error;
5343 total = bset->dim->n_out + bset->extra;
5344 bmap = bset_to_bmap(bset);
5345 isl_space_free(bmap->dim);
5346 bmap->dim = isl_space_copy(like->dim);
5347 if (!bmap->dim)
5348 goto error;
5349 bmap->n_div = like->n_div;
5350 bmap->extra += like->n_div;
5351 if (bmap->extra) {
5352 unsigned ltotal;
5353 isl_int **div;
5354 ltotal = total - bmap->extra + like->extra;
5355 if (ltotal > total)
5356 ltotal = total;
5357 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5358 bmap->extra * (1 + 1 + total));
5359 if (isl_blk_is_error(bmap->block2))
5360 goto error;
5361 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5362 if (!div)
5363 goto error;
5364 bmap->div = div;
5365 for (i = 0; i < bmap->extra; ++i)
5366 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5367 for (i = 0; i < like->n_div; ++i) {
5368 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5369 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5371 bmap = isl_basic_map_add_known_div_constraints(bmap);
5373 isl_basic_map_free(like);
5374 bmap = isl_basic_map_simplify(bmap);
5375 bmap = isl_basic_map_finalize(bmap);
5376 return bmap;
5377 error:
5378 isl_basic_map_free(like);
5379 isl_basic_set_free(bset);
5380 return NULL;
5383 struct isl_basic_set *isl_basic_set_from_underlying_set(
5384 struct isl_basic_set *bset, struct isl_basic_set *like)
5386 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5387 bset_to_bmap(like)));
5390 struct isl_set *isl_map_underlying_set(struct isl_map *map)
5392 int i;
5394 map = isl_map_cow(map);
5395 if (!map)
5396 return NULL;
5397 map->dim = isl_space_cow(map->dim);
5398 if (!map->dim)
5399 goto error;
5401 for (i = 1; i < map->n; ++i)
5402 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5403 goto error);
5404 for (i = 0; i < map->n; ++i) {
5405 map->p[i] = bset_to_bmap(
5406 isl_basic_map_underlying_set(map->p[i]));
5407 if (!map->p[i])
5408 goto error;
5410 if (map->n == 0)
5411 map->dim = isl_space_underlying(map->dim, 0);
5412 else {
5413 isl_space_free(map->dim);
5414 map->dim = isl_space_copy(map->p[0]->dim);
5416 if (!map->dim)
5417 goto error;
5418 return set_from_map(map);
5419 error:
5420 isl_map_free(map);
5421 return NULL;
5424 /* Replace the space of "bmap" by "space".
5426 * If the space of "bmap" is identical to "space" (including the identifiers
5427 * of the input and output dimensions), then simply return the original input.
5429 __isl_give isl_basic_map *isl_basic_map_reset_space(
5430 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5432 isl_bool equal;
5433 isl_space *bmap_space;
5435 bmap_space = isl_basic_map_peek_space(bmap);
5436 equal = isl_space_is_equal(bmap_space, space);
5437 if (equal >= 0 && equal)
5438 equal = isl_space_has_equal_ids(bmap_space, space);
5439 if (equal < 0)
5440 goto error;
5441 if (equal) {
5442 isl_space_free(space);
5443 return bmap;
5445 bmap = isl_basic_map_cow(bmap);
5446 if (!bmap || !space)
5447 goto error;
5449 isl_space_free(bmap->dim);
5450 bmap->dim = space;
5452 bmap = isl_basic_map_finalize(bmap);
5454 return bmap;
5455 error:
5456 isl_basic_map_free(bmap);
5457 isl_space_free(space);
5458 return NULL;
5461 __isl_give isl_basic_set *isl_basic_set_reset_space(
5462 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5464 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5465 dim));
5468 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5469 __isl_take isl_space *dim)
5471 int i;
5473 map = isl_map_cow(map);
5474 if (!map || !dim)
5475 goto error;
5477 for (i = 0; i < map->n; ++i) {
5478 map->p[i] = isl_basic_map_reset_space(map->p[i],
5479 isl_space_copy(dim));
5480 if (!map->p[i])
5481 goto error;
5483 isl_space_free(map->dim);
5484 map->dim = dim;
5486 return map;
5487 error:
5488 isl_map_free(map);
5489 isl_space_free(dim);
5490 return NULL;
5493 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5494 __isl_take isl_space *dim)
5496 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5499 /* Compute the parameter domain of the given basic set.
5501 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5503 isl_bool is_params;
5504 isl_space *space;
5505 unsigned n;
5507 is_params = isl_basic_set_is_params(bset);
5508 if (is_params < 0)
5509 return isl_basic_set_free(bset);
5510 if (is_params)
5511 return bset;
5513 n = isl_basic_set_dim(bset, isl_dim_set);
5514 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5515 space = isl_basic_set_get_space(bset);
5516 space = isl_space_params(space);
5517 bset = isl_basic_set_reset_space(bset, space);
5518 return bset;
5521 /* Construct a zero-dimensional basic set with the given parameter domain.
5523 __isl_give isl_basic_set *isl_basic_set_from_params(
5524 __isl_take isl_basic_set *bset)
5526 isl_space *space;
5527 space = isl_basic_set_get_space(bset);
5528 space = isl_space_set_from_params(space);
5529 bset = isl_basic_set_reset_space(bset, space);
5530 return bset;
5533 /* Compute the parameter domain of the given set.
5535 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5537 isl_space *space;
5538 unsigned n;
5540 if (isl_set_is_params(set))
5541 return set;
5543 n = isl_set_dim(set, isl_dim_set);
5544 set = isl_set_project_out(set, isl_dim_set, 0, n);
5545 space = isl_set_get_space(set);
5546 space = isl_space_params(space);
5547 set = isl_set_reset_space(set, space);
5548 return set;
5551 /* Construct a zero-dimensional set with the given parameter domain.
5553 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5555 isl_space *space;
5556 space = isl_set_get_space(set);
5557 space = isl_space_set_from_params(space);
5558 set = isl_set_reset_space(set, space);
5559 return set;
5562 /* Compute the parameter domain of the given map.
5564 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5566 isl_space *space;
5567 unsigned n;
5569 n = isl_map_dim(map, isl_dim_in);
5570 map = isl_map_project_out(map, isl_dim_in, 0, n);
5571 n = isl_map_dim(map, isl_dim_out);
5572 map = isl_map_project_out(map, isl_dim_out, 0, n);
5573 space = isl_map_get_space(map);
5574 space = isl_space_params(space);
5575 map = isl_map_reset_space(map, space);
5576 return map;
5579 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5581 isl_space *space;
5582 unsigned n_out;
5584 if (!bmap)
5585 return NULL;
5586 space = isl_space_domain(isl_basic_map_get_space(bmap));
5588 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5589 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5591 return isl_basic_map_reset_space(bmap, space);
5594 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5596 if (!bmap)
5597 return isl_bool_error;
5598 return isl_space_may_be_set(bmap->dim);
5601 /* Is this basic map actually a set?
5602 * Users should never call this function. Outside of isl,
5603 * the type should indicate whether something is a set or a map.
5605 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5607 if (!bmap)
5608 return isl_bool_error;
5609 return isl_space_is_set(bmap->dim);
5612 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5614 isl_bool is_set;
5616 is_set = isl_basic_map_is_set(bmap);
5617 if (is_set < 0)
5618 goto error;
5619 if (is_set)
5620 return bmap;
5621 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5622 error:
5623 isl_basic_map_free(bmap);
5624 return NULL;
5627 __isl_give isl_basic_map *isl_basic_map_domain_map(
5628 __isl_take isl_basic_map *bmap)
5630 int i;
5631 isl_space *dim;
5632 isl_basic_map *domain;
5633 int nparam, n_in, n_out;
5635 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5636 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5637 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5639 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5640 domain = isl_basic_map_universe(dim);
5642 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5643 bmap = isl_basic_map_apply_range(bmap, domain);
5644 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5646 for (i = 0; i < n_in; ++i)
5647 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5648 isl_dim_out, i);
5650 bmap = isl_basic_map_gauss(bmap, NULL);
5651 return isl_basic_map_finalize(bmap);
5654 __isl_give isl_basic_map *isl_basic_map_range_map(
5655 __isl_take isl_basic_map *bmap)
5657 int i;
5658 isl_space *dim;
5659 isl_basic_map *range;
5660 int nparam, n_in, n_out;
5662 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5663 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5664 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5666 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5667 range = isl_basic_map_universe(dim);
5669 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5670 bmap = isl_basic_map_apply_range(bmap, range);
5671 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5673 for (i = 0; i < n_out; ++i)
5674 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5675 isl_dim_out, i);
5677 bmap = isl_basic_map_gauss(bmap, NULL);
5678 return isl_basic_map_finalize(bmap);
5681 int isl_map_may_be_set(__isl_keep isl_map *map)
5683 if (!map)
5684 return -1;
5685 return isl_space_may_be_set(map->dim);
5688 /* Is this map actually a set?
5689 * Users should never call this function. Outside of isl,
5690 * the type should indicate whether something is a set or a map.
5692 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5694 if (!map)
5695 return isl_bool_error;
5696 return isl_space_is_set(map->dim);
5699 struct isl_set *isl_map_range(struct isl_map *map)
5701 int i;
5702 isl_bool is_set;
5703 struct isl_set *set;
5705 is_set = isl_map_is_set(map);
5706 if (is_set < 0)
5707 goto error;
5708 if (is_set)
5709 return set_from_map(map);
5711 map = isl_map_cow(map);
5712 if (!map)
5713 goto error;
5715 set = set_from_map(map);
5716 set->dim = isl_space_range(set->dim);
5717 if (!set->dim)
5718 goto error;
5719 for (i = 0; i < map->n; ++i) {
5720 set->p[i] = isl_basic_map_range(map->p[i]);
5721 if (!set->p[i])
5722 goto error;
5724 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5725 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5726 return set;
5727 error:
5728 isl_map_free(map);
5729 return NULL;
5732 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5734 int i;
5736 map = isl_map_cow(map);
5737 if (!map)
5738 return NULL;
5740 map->dim = isl_space_domain_map(map->dim);
5741 if (!map->dim)
5742 goto error;
5743 for (i = 0; i < map->n; ++i) {
5744 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5745 if (!map->p[i])
5746 goto error;
5748 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5749 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5750 return map;
5751 error:
5752 isl_map_free(map);
5753 return NULL;
5756 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5758 int i;
5759 isl_space *range_dim;
5761 map = isl_map_cow(map);
5762 if (!map)
5763 return NULL;
5765 range_dim = isl_space_range(isl_map_get_space(map));
5766 range_dim = isl_space_from_range(range_dim);
5767 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5768 map->dim = isl_space_join(map->dim, range_dim);
5769 if (!map->dim)
5770 goto error;
5771 for (i = 0; i < map->n; ++i) {
5772 map->p[i] = isl_basic_map_range_map(map->p[i]);
5773 if (!map->p[i])
5774 goto error;
5776 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5777 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5778 return map;
5779 error:
5780 isl_map_free(map);
5781 return NULL;
5784 /* Given a wrapped map of the form A[B -> C],
5785 * return the map A[B -> C] -> B.
5787 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5789 isl_id *id;
5790 isl_map *map;
5792 if (!set)
5793 return NULL;
5794 if (!isl_set_has_tuple_id(set))
5795 return isl_map_domain_map(isl_set_unwrap(set));
5797 id = isl_set_get_tuple_id(set);
5798 map = isl_map_domain_map(isl_set_unwrap(set));
5799 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5801 return map;
5804 __isl_give isl_basic_map *isl_basic_map_from_domain(
5805 __isl_take isl_basic_set *bset)
5807 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5810 __isl_give isl_basic_map *isl_basic_map_from_range(
5811 __isl_take isl_basic_set *bset)
5813 isl_space *space;
5814 space = isl_basic_set_get_space(bset);
5815 space = isl_space_from_range(space);
5816 bset = isl_basic_set_reset_space(bset, space);
5817 return bset_to_bmap(bset);
5820 /* Create a relation with the given set as range.
5821 * The domain of the created relation is a zero-dimensional
5822 * flat anonymous space.
5824 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5826 isl_space *space;
5827 space = isl_set_get_space(set);
5828 space = isl_space_from_range(space);
5829 set = isl_set_reset_space(set, space);
5830 return set_to_map(set);
5833 /* Create a relation with the given set as domain.
5834 * The range of the created relation is a zero-dimensional
5835 * flat anonymous space.
5837 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5839 return isl_map_reverse(isl_map_from_range(set));
5842 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5843 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5845 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5848 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5849 __isl_take isl_set *range)
5851 return isl_map_apply_range(isl_map_reverse(domain), range);
5854 /* Return a newly allocated isl_map with given space and flags and
5855 * room for "n" basic maps.
5856 * Make sure that all cached information is cleared.
5858 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5859 unsigned flags)
5861 struct isl_map *map;
5863 if (!space)
5864 return NULL;
5865 if (n < 0)
5866 isl_die(space->ctx, isl_error_internal,
5867 "negative number of basic maps", goto error);
5868 map = isl_calloc(space->ctx, struct isl_map,
5869 sizeof(struct isl_map) +
5870 (n - 1) * sizeof(struct isl_basic_map *));
5871 if (!map)
5872 goto error;
5874 map->ctx = space->ctx;
5875 isl_ctx_ref(map->ctx);
5876 map->ref = 1;
5877 map->size = n;
5878 map->n = 0;
5879 map->dim = space;
5880 map->flags = flags;
5881 return map;
5882 error:
5883 isl_space_free(space);
5884 return NULL;
5887 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5889 struct isl_basic_map *bmap;
5890 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5891 bmap = isl_basic_map_set_to_empty(bmap);
5892 return bmap;
5895 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5897 struct isl_basic_set *bset;
5898 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5899 bset = isl_basic_set_set_to_empty(bset);
5900 return bset;
5903 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5905 struct isl_basic_map *bmap;
5906 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5907 bmap = isl_basic_map_finalize(bmap);
5908 return bmap;
5911 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5913 struct isl_basic_set *bset;
5914 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5915 bset = isl_basic_set_finalize(bset);
5916 return bset;
5919 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5921 int i;
5922 unsigned total = isl_space_dim(dim, isl_dim_all);
5923 isl_basic_map *bmap;
5925 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5926 for (i = 0; i < total; ++i) {
5927 int k = isl_basic_map_alloc_inequality(bmap);
5928 if (k < 0)
5929 goto error;
5930 isl_seq_clr(bmap->ineq[k], 1 + total);
5931 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5933 return bmap;
5934 error:
5935 isl_basic_map_free(bmap);
5936 return NULL;
5939 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5941 return isl_basic_map_nat_universe(dim);
5944 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5946 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5949 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5951 return isl_map_nat_universe(dim);
5954 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5956 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5959 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5961 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5964 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5966 struct isl_map *map;
5967 if (!dim)
5968 return NULL;
5969 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5970 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5971 return map;
5974 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5976 struct isl_set *set;
5977 if (!dim)
5978 return NULL;
5979 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5980 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5981 return set;
5984 struct isl_map *isl_map_dup(struct isl_map *map)
5986 int i;
5987 struct isl_map *dup;
5989 if (!map)
5990 return NULL;
5991 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5992 for (i = 0; i < map->n; ++i)
5993 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5994 return dup;
5997 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5998 __isl_take isl_basic_map *bmap)
6000 if (!bmap || !map)
6001 goto error;
6002 if (isl_basic_map_plain_is_empty(bmap)) {
6003 isl_basic_map_free(bmap);
6004 return map;
6006 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6007 isl_assert(map->ctx, map->n < map->size, goto error);
6008 map->p[map->n] = bmap;
6009 map->n++;
6010 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6011 return map;
6012 error:
6013 if (map)
6014 isl_map_free(map);
6015 if (bmap)
6016 isl_basic_map_free(bmap);
6017 return NULL;
6020 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6022 int i;
6024 if (!map)
6025 return NULL;
6027 if (--map->ref > 0)
6028 return NULL;
6030 clear_caches(map);
6031 isl_ctx_deref(map->ctx);
6032 for (i = 0; i < map->n; ++i)
6033 isl_basic_map_free(map->p[i]);
6034 isl_space_free(map->dim);
6035 free(map);
6037 return NULL;
6040 static struct isl_basic_map *isl_basic_map_fix_pos_si(
6041 struct isl_basic_map *bmap, unsigned pos, int value)
6043 int j;
6045 bmap = isl_basic_map_cow(bmap);
6046 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6047 j = isl_basic_map_alloc_equality(bmap);
6048 if (j < 0)
6049 goto error;
6050 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6051 isl_int_set_si(bmap->eq[j][pos], -1);
6052 isl_int_set_si(bmap->eq[j][0], value);
6053 bmap = isl_basic_map_simplify(bmap);
6054 return isl_basic_map_finalize(bmap);
6055 error:
6056 isl_basic_map_free(bmap);
6057 return NULL;
6060 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6061 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6063 int j;
6065 bmap = isl_basic_map_cow(bmap);
6066 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6067 j = isl_basic_map_alloc_equality(bmap);
6068 if (j < 0)
6069 goto error;
6070 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6071 isl_int_set_si(bmap->eq[j][pos], -1);
6072 isl_int_set(bmap->eq[j][0], value);
6073 bmap = isl_basic_map_simplify(bmap);
6074 return isl_basic_map_finalize(bmap);
6075 error:
6076 isl_basic_map_free(bmap);
6077 return NULL;
6080 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
6081 enum isl_dim_type type, unsigned pos, int value)
6083 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6084 return isl_basic_map_free(bmap);
6085 return isl_basic_map_fix_pos_si(bmap,
6086 isl_basic_map_offset(bmap, type) + pos, value);
6089 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6090 enum isl_dim_type type, unsigned pos, isl_int value)
6092 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6093 return isl_basic_map_free(bmap);
6094 return isl_basic_map_fix_pos(bmap,
6095 isl_basic_map_offset(bmap, type) + pos, value);
6098 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6099 * to be equal to "v".
6101 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6102 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6104 if (!bmap || !v)
6105 goto error;
6106 if (!isl_val_is_int(v))
6107 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6108 "expecting integer value", goto error);
6109 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6110 goto error;
6111 pos += isl_basic_map_offset(bmap, type);
6112 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6113 isl_val_free(v);
6114 return bmap;
6115 error:
6116 isl_basic_map_free(bmap);
6117 isl_val_free(v);
6118 return NULL;
6121 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6122 * to be equal to "v".
6124 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6125 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6127 return isl_basic_map_fix_val(bset, type, pos, v);
6130 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6131 enum isl_dim_type type, unsigned pos, int value)
6133 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6134 type, pos, value));
6137 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6138 enum isl_dim_type type, unsigned pos, isl_int value)
6140 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6141 type, pos, value));
6144 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6145 unsigned input, int value)
6147 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6150 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6151 unsigned dim, int value)
6153 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6154 isl_dim_set, dim, value));
6157 static int remove_if_empty(__isl_keep isl_map *map, int i)
6159 int empty = isl_basic_map_plain_is_empty(map->p[i]);
6161 if (empty < 0)
6162 return -1;
6163 if (!empty)
6164 return 0;
6166 isl_basic_map_free(map->p[i]);
6167 if (i != map->n - 1) {
6168 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6169 map->p[i] = map->p[map->n - 1];
6171 map->n--;
6173 return 0;
6176 /* Perform "fn" on each basic map of "map", where we may not be holding
6177 * the only reference to "map".
6178 * In particular, "fn" should be a semantics preserving operation
6179 * that we want to apply to all copies of "map". We therefore need
6180 * to be careful not to modify "map" in a way that breaks "map"
6181 * in case anything goes wrong.
6183 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6184 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6186 struct isl_basic_map *bmap;
6187 int i;
6189 if (!map)
6190 return NULL;
6192 for (i = map->n - 1; i >= 0; --i) {
6193 bmap = isl_basic_map_copy(map->p[i]);
6194 bmap = fn(bmap);
6195 if (!bmap)
6196 goto error;
6197 isl_basic_map_free(map->p[i]);
6198 map->p[i] = bmap;
6199 if (remove_if_empty(map, i) < 0)
6200 goto error;
6203 return map;
6204 error:
6205 isl_map_free(map);
6206 return NULL;
6209 struct isl_map *isl_map_fix_si(struct isl_map *map,
6210 enum isl_dim_type type, unsigned pos, int value)
6212 int i;
6214 map = isl_map_cow(map);
6215 if (!map)
6216 return NULL;
6218 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6219 for (i = map->n - 1; i >= 0; --i) {
6220 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6221 if (remove_if_empty(map, i) < 0)
6222 goto error;
6224 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6225 return map;
6226 error:
6227 isl_map_free(map);
6228 return NULL;
6231 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6232 enum isl_dim_type type, unsigned pos, int value)
6234 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6237 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6238 enum isl_dim_type type, unsigned pos, isl_int value)
6240 int i;
6242 map = isl_map_cow(map);
6243 if (!map)
6244 return NULL;
6246 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6247 for (i = 0; i < map->n; ++i) {
6248 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6249 if (!map->p[i])
6250 goto error;
6252 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6253 return map;
6254 error:
6255 isl_map_free(map);
6256 return NULL;
6259 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6260 enum isl_dim_type type, unsigned pos, isl_int value)
6262 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6265 /* Fix the value of the variable at position "pos" of type "type" of "map"
6266 * to be equal to "v".
6268 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6269 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6271 int i;
6273 map = isl_map_cow(map);
6274 if (!map || !v)
6275 goto error;
6277 if (!isl_val_is_int(v))
6278 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6279 "expecting integer value", goto error);
6280 if (pos >= isl_map_dim(map, type))
6281 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6282 "index out of bounds", goto error);
6283 for (i = map->n - 1; i >= 0; --i) {
6284 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6285 isl_val_copy(v));
6286 if (remove_if_empty(map, i) < 0)
6287 goto error;
6289 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6290 isl_val_free(v);
6291 return map;
6292 error:
6293 isl_map_free(map);
6294 isl_val_free(v);
6295 return NULL;
6298 /* Fix the value of the variable at position "pos" of type "type" of "set"
6299 * to be equal to "v".
6301 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6302 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6304 return isl_map_fix_val(set, type, pos, v);
6307 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6308 unsigned input, int value)
6310 return isl_map_fix_si(map, isl_dim_in, input, value);
6313 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6315 return set_from_map(isl_map_fix_si(set_to_map(set),
6316 isl_dim_set, dim, value));
6319 static __isl_give isl_basic_map *basic_map_bound_si(
6320 __isl_take isl_basic_map *bmap,
6321 enum isl_dim_type type, unsigned pos, int value, int upper)
6323 int j;
6325 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6326 return isl_basic_map_free(bmap);
6327 pos += isl_basic_map_offset(bmap, type);
6328 bmap = isl_basic_map_cow(bmap);
6329 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6330 j = isl_basic_map_alloc_inequality(bmap);
6331 if (j < 0)
6332 goto error;
6333 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6334 if (upper) {
6335 isl_int_set_si(bmap->ineq[j][pos], -1);
6336 isl_int_set_si(bmap->ineq[j][0], value);
6337 } else {
6338 isl_int_set_si(bmap->ineq[j][pos], 1);
6339 isl_int_set_si(bmap->ineq[j][0], -value);
6341 bmap = isl_basic_map_simplify(bmap);
6342 return isl_basic_map_finalize(bmap);
6343 error:
6344 isl_basic_map_free(bmap);
6345 return NULL;
6348 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6349 __isl_take isl_basic_map *bmap,
6350 enum isl_dim_type type, unsigned pos, int value)
6352 return basic_map_bound_si(bmap, type, pos, value, 0);
6355 /* Constrain the values of the given dimension to be no greater than "value".
6357 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6358 __isl_take isl_basic_map *bmap,
6359 enum isl_dim_type type, unsigned pos, int value)
6361 return basic_map_bound_si(bmap, type, pos, value, 1);
6364 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6365 enum isl_dim_type type, unsigned pos, int value, int upper)
6367 int i;
6369 map = isl_map_cow(map);
6370 if (!map)
6371 return NULL;
6373 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6374 for (i = 0; i < map->n; ++i) {
6375 map->p[i] = basic_map_bound_si(map->p[i],
6376 type, pos, value, upper);
6377 if (!map->p[i])
6378 goto error;
6380 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6381 return map;
6382 error:
6383 isl_map_free(map);
6384 return NULL;
6387 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6388 enum isl_dim_type type, unsigned pos, int value)
6390 return map_bound_si(map, type, pos, value, 0);
6393 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6394 enum isl_dim_type type, unsigned pos, int value)
6396 return map_bound_si(map, type, pos, value, 1);
6399 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6400 enum isl_dim_type type, unsigned pos, int value)
6402 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6403 type, pos, value));
6406 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6407 enum isl_dim_type type, unsigned pos, int value)
6409 return isl_map_upper_bound_si(set, type, pos, value);
6412 /* Bound the given variable of "bmap" from below (or above is "upper"
6413 * is set) to "value".
6415 static __isl_give isl_basic_map *basic_map_bound(
6416 __isl_take isl_basic_map *bmap,
6417 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6419 int j;
6421 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6422 return isl_basic_map_free(bmap);
6423 pos += isl_basic_map_offset(bmap, type);
6424 bmap = isl_basic_map_cow(bmap);
6425 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6426 j = isl_basic_map_alloc_inequality(bmap);
6427 if (j < 0)
6428 goto error;
6429 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6430 if (upper) {
6431 isl_int_set_si(bmap->ineq[j][pos], -1);
6432 isl_int_set(bmap->ineq[j][0], value);
6433 } else {
6434 isl_int_set_si(bmap->ineq[j][pos], 1);
6435 isl_int_neg(bmap->ineq[j][0], value);
6437 bmap = isl_basic_map_simplify(bmap);
6438 return isl_basic_map_finalize(bmap);
6439 error:
6440 isl_basic_map_free(bmap);
6441 return NULL;
6444 /* Bound the given variable of "map" from below (or above is "upper"
6445 * is set) to "value".
6447 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6448 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6450 int i;
6452 map = isl_map_cow(map);
6453 if (!map)
6454 return NULL;
6456 if (pos >= isl_map_dim(map, type))
6457 isl_die(map->ctx, isl_error_invalid,
6458 "index out of bounds", goto error);
6459 for (i = map->n - 1; i >= 0; --i) {
6460 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6461 if (remove_if_empty(map, i) < 0)
6462 goto error;
6464 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6465 return map;
6466 error:
6467 isl_map_free(map);
6468 return NULL;
6471 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6472 enum isl_dim_type type, unsigned pos, isl_int value)
6474 return map_bound(map, type, pos, value, 0);
6477 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6478 enum isl_dim_type type, unsigned pos, isl_int value)
6480 return map_bound(map, type, pos, value, 1);
6483 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6484 enum isl_dim_type type, unsigned pos, isl_int value)
6486 return isl_map_lower_bound(set, type, pos, value);
6489 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6490 enum isl_dim_type type, unsigned pos, isl_int value)
6492 return isl_map_upper_bound(set, type, pos, value);
6495 /* Force the values of the variable at position "pos" of type "type" of "set"
6496 * to be no smaller than "value".
6498 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6499 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6501 if (!value)
6502 goto error;
6503 if (!isl_val_is_int(value))
6504 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6505 "expecting integer value", goto error);
6506 set = isl_set_lower_bound(set, type, pos, value->n);
6507 isl_val_free(value);
6508 return set;
6509 error:
6510 isl_val_free(value);
6511 isl_set_free(set);
6512 return NULL;
6515 /* Force the values of the variable at position "pos" of type "type" of "set"
6516 * to be no greater than "value".
6518 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6519 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6521 if (!value)
6522 goto error;
6523 if (!isl_val_is_int(value))
6524 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6525 "expecting integer value", goto error);
6526 set = isl_set_upper_bound(set, type, pos, value->n);
6527 isl_val_free(value);
6528 return set;
6529 error:
6530 isl_val_free(value);
6531 isl_set_free(set);
6532 return NULL;
6535 struct isl_map *isl_map_reverse(struct isl_map *map)
6537 int i;
6539 map = isl_map_cow(map);
6540 if (!map)
6541 return NULL;
6543 map->dim = isl_space_reverse(map->dim);
6544 if (!map->dim)
6545 goto error;
6546 for (i = 0; i < map->n; ++i) {
6547 map->p[i] = isl_basic_map_reverse(map->p[i]);
6548 if (!map->p[i])
6549 goto error;
6551 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6552 return map;
6553 error:
6554 isl_map_free(map);
6555 return NULL;
6558 #undef TYPE
6559 #define TYPE isl_pw_multi_aff
6560 #undef SUFFIX
6561 #define SUFFIX _pw_multi_aff
6562 #undef EMPTY
6563 #define EMPTY isl_pw_multi_aff_empty
6564 #undef ADD
6565 #define ADD isl_pw_multi_aff_union_add
6566 #include "isl_map_lexopt_templ.c"
6568 /* Given a map "map", compute the lexicographically minimal
6569 * (or maximal) image element for each domain element in dom,
6570 * in the form of an isl_pw_multi_aff.
6571 * If "empty" is not NULL, then set *empty to those elements in dom that
6572 * do not have an image element.
6573 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6574 * should be computed over the domain of "map". "empty" is also NULL
6575 * in this case.
6577 * We first compute the lexicographically minimal or maximal element
6578 * in the first basic map. This results in a partial solution "res"
6579 * and a subset "todo" of dom that still need to be handled.
6580 * We then consider each of the remaining maps in "map" and successively
6581 * update both "res" and "todo".
6582 * If "empty" is NULL, then the todo sets are not needed and therefore
6583 * also not computed.
6585 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6586 __isl_take isl_map *map, __isl_take isl_set *dom,
6587 __isl_give isl_set **empty, unsigned flags)
6589 int i;
6590 int full;
6591 isl_pw_multi_aff *res;
6592 isl_set *todo;
6594 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6595 if (!map || (!full && !dom))
6596 goto error;
6598 if (isl_map_plain_is_empty(map)) {
6599 if (empty)
6600 *empty = dom;
6601 else
6602 isl_set_free(dom);
6603 return isl_pw_multi_aff_from_map(map);
6606 res = basic_map_partial_lexopt_pw_multi_aff(
6607 isl_basic_map_copy(map->p[0]),
6608 isl_set_copy(dom), empty, flags);
6610 if (empty)
6611 todo = *empty;
6612 for (i = 1; i < map->n; ++i) {
6613 isl_pw_multi_aff *res_i;
6615 res_i = basic_map_partial_lexopt_pw_multi_aff(
6616 isl_basic_map_copy(map->p[i]),
6617 isl_set_copy(dom), empty, flags);
6619 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6620 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6621 else
6622 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6624 if (empty)
6625 todo = isl_set_intersect(todo, *empty);
6628 isl_set_free(dom);
6629 isl_map_free(map);
6631 if (empty)
6632 *empty = todo;
6634 return res;
6635 error:
6636 if (empty)
6637 *empty = NULL;
6638 isl_set_free(dom);
6639 isl_map_free(map);
6640 return NULL;
6643 #undef TYPE
6644 #define TYPE isl_map
6645 #undef SUFFIX
6646 #define SUFFIX
6647 #undef EMPTY
6648 #define EMPTY isl_map_empty
6649 #undef ADD
6650 #define ADD isl_map_union_disjoint
6651 #include "isl_map_lexopt_templ.c"
6653 /* Given a map "map", compute the lexicographically minimal
6654 * (or maximal) image element for each domain element in "dom",
6655 * in the form of an isl_map.
6656 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6657 * do not have an image element.
6658 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6659 * should be computed over the domain of "map". "empty" is also NULL
6660 * in this case.
6662 * If the input consists of more than one disjunct, then first
6663 * compute the desired result in the form of an isl_pw_multi_aff and
6664 * then convert that into an isl_map.
6666 * This function used to have an explicit implementation in terms
6667 * of isl_maps, but it would continually intersect the domains of
6668 * partial results with the complement of the domain of the next
6669 * partial solution, potentially leading to an explosion in the number
6670 * of disjuncts if there are several disjuncts in the input.
6671 * An even earlier implementation of this function would look for
6672 * better results in the domain of the partial result and for extra
6673 * results in the complement of this domain, which would lead to
6674 * even more splintering.
6676 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6677 __isl_take isl_map *map, __isl_take isl_set *dom,
6678 __isl_give isl_set **empty, unsigned flags)
6680 int full;
6681 struct isl_map *res;
6682 isl_pw_multi_aff *pma;
6684 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6685 if (!map || (!full && !dom))
6686 goto error;
6688 if (isl_map_plain_is_empty(map)) {
6689 if (empty)
6690 *empty = dom;
6691 else
6692 isl_set_free(dom);
6693 return map;
6696 if (map->n == 1) {
6697 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6698 dom, empty, flags);
6699 isl_map_free(map);
6700 return res;
6703 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6704 flags);
6705 return isl_map_from_pw_multi_aff(pma);
6706 error:
6707 if (empty)
6708 *empty = NULL;
6709 isl_set_free(dom);
6710 isl_map_free(map);
6711 return NULL;
6714 __isl_give isl_map *isl_map_partial_lexmax(
6715 __isl_take isl_map *map, __isl_take isl_set *dom,
6716 __isl_give isl_set **empty)
6718 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6721 __isl_give isl_map *isl_map_partial_lexmin(
6722 __isl_take isl_map *map, __isl_take isl_set *dom,
6723 __isl_give isl_set **empty)
6725 return isl_map_partial_lexopt(map, dom, empty, 0);
6728 __isl_give isl_set *isl_set_partial_lexmin(
6729 __isl_take isl_set *set, __isl_take isl_set *dom,
6730 __isl_give isl_set **empty)
6732 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6733 dom, empty));
6736 __isl_give isl_set *isl_set_partial_lexmax(
6737 __isl_take isl_set *set, __isl_take isl_set *dom,
6738 __isl_give isl_set **empty)
6740 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6741 dom, empty));
6744 /* Compute the lexicographic minimum (or maximum if "flags" includes
6745 * ISL_OPT_MAX) of "bset" over its parametric domain.
6747 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6748 unsigned flags)
6750 return isl_basic_map_lexopt(bset, flags);
6753 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6755 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6758 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6760 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6763 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6765 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6768 /* Compute the lexicographic minimum of "bset" over its parametric domain
6769 * for the purpose of quantifier elimination.
6770 * That is, find an explicit representation for all the existentially
6771 * quantified variables in "bset" by computing their lexicographic
6772 * minimum.
6774 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6775 __isl_take isl_basic_set *bset)
6777 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6780 /* Extract the first and only affine expression from list
6781 * and then add it to *pwaff with the given dom.
6782 * This domain is known to be disjoint from other domains
6783 * because of the way isl_basic_map_foreach_lexmax works.
6785 static isl_stat update_dim_opt(__isl_take isl_basic_set *dom,
6786 __isl_take isl_aff_list *list, void *user)
6788 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6789 isl_aff *aff;
6790 isl_pw_aff **pwaff = user;
6791 isl_pw_aff *pwaff_i;
6793 if (!list)
6794 goto error;
6795 if (isl_aff_list_n_aff(list) != 1)
6796 isl_die(ctx, isl_error_internal,
6797 "expecting single element list", goto error);
6799 aff = isl_aff_list_get_aff(list, 0);
6800 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6802 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6804 isl_aff_list_free(list);
6806 return isl_stat_ok;
6807 error:
6808 isl_basic_set_free(dom);
6809 isl_aff_list_free(list);
6810 return isl_stat_error;
6813 /* Given a basic map with one output dimension, compute the minimum or
6814 * maximum of that dimension as an isl_pw_aff.
6816 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6817 * call update_dim_opt on each leaf of the result.
6819 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6820 int max)
6822 isl_space *dim = isl_basic_map_get_space(bmap);
6823 isl_pw_aff *pwaff;
6824 isl_stat r;
6826 dim = isl_space_from_domain(isl_space_domain(dim));
6827 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6828 pwaff = isl_pw_aff_empty(dim);
6830 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6831 if (r < 0)
6832 return isl_pw_aff_free(pwaff);
6834 return pwaff;
6837 /* Compute the minimum or maximum of the given output dimension
6838 * as a function of the parameters and the input dimensions,
6839 * but independently of the other output dimensions.
6841 * We first project out the other output dimension and then compute
6842 * the "lexicographic" maximum in each basic map, combining the results
6843 * using isl_pw_aff_union_max.
6845 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6846 int max)
6848 int i;
6849 isl_pw_aff *pwaff;
6850 unsigned n_out;
6852 n_out = isl_map_dim(map, isl_dim_out);
6853 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6854 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6855 if (!map)
6856 return NULL;
6858 if (map->n == 0) {
6859 isl_space *dim = isl_map_get_space(map);
6860 isl_map_free(map);
6861 return isl_pw_aff_empty(dim);
6864 pwaff = basic_map_dim_opt(map->p[0], max);
6865 for (i = 1; i < map->n; ++i) {
6866 isl_pw_aff *pwaff_i;
6868 pwaff_i = basic_map_dim_opt(map->p[i], max);
6869 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6872 isl_map_free(map);
6874 return pwaff;
6877 /* Compute the minimum of the given output dimension as a function of the
6878 * parameters and input dimensions, but independently of
6879 * the other output dimensions.
6881 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6883 return map_dim_opt(map, pos, 0);
6886 /* Compute the maximum of the given output dimension as a function of the
6887 * parameters and input dimensions, but independently of
6888 * the other output dimensions.
6890 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6892 return map_dim_opt(map, pos, 1);
6895 /* Compute the minimum or maximum of the given set dimension
6896 * as a function of the parameters,
6897 * but independently of the other set dimensions.
6899 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6900 int max)
6902 return map_dim_opt(set, pos, max);
6905 /* Compute the maximum of the given set dimension as a function of the
6906 * parameters, but independently of the other set dimensions.
6908 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6910 return set_dim_opt(set, pos, 1);
6913 /* Compute the minimum of the given set dimension as a function of the
6914 * parameters, but independently of the other set dimensions.
6916 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6918 return set_dim_opt(set, pos, 0);
6921 /* Apply a preimage specified by "mat" on the parameters of "bset".
6922 * bset is assumed to have only parameters and divs.
6924 static __isl_give isl_basic_set *basic_set_parameter_preimage(
6925 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
6927 unsigned nparam;
6929 if (!bset || !mat)
6930 goto error;
6932 bset->dim = isl_space_cow(bset->dim);
6933 if (!bset->dim)
6934 goto error;
6936 nparam = isl_basic_set_dim(bset, isl_dim_param);
6938 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6940 bset->dim->nparam = 0;
6941 bset->dim->n_out = nparam;
6942 bset = isl_basic_set_preimage(bset, mat);
6943 if (bset) {
6944 bset->dim->nparam = bset->dim->n_out;
6945 bset->dim->n_out = 0;
6947 return bset;
6948 error:
6949 isl_mat_free(mat);
6950 isl_basic_set_free(bset);
6951 return NULL;
6954 /* Apply a preimage specified by "mat" on the parameters of "set".
6955 * set is assumed to have only parameters and divs.
6957 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6958 __isl_take isl_mat *mat)
6960 isl_space *space;
6961 unsigned nparam;
6963 if (!set || !mat)
6964 goto error;
6966 nparam = isl_set_dim(set, isl_dim_param);
6968 if (mat->n_row != 1 + nparam)
6969 isl_die(isl_set_get_ctx(set), isl_error_internal,
6970 "unexpected number of rows", goto error);
6972 space = isl_set_get_space(set);
6973 space = isl_space_move_dims(space, isl_dim_set, 0,
6974 isl_dim_param, 0, nparam);
6975 set = isl_set_reset_space(set, space);
6976 set = isl_set_preimage(set, mat);
6977 nparam = isl_set_dim(set, isl_dim_out);
6978 space = isl_set_get_space(set);
6979 space = isl_space_move_dims(space, isl_dim_param, 0,
6980 isl_dim_out, 0, nparam);
6981 set = isl_set_reset_space(set, space);
6982 return set;
6983 error:
6984 isl_mat_free(mat);
6985 isl_set_free(set);
6986 return NULL;
6989 /* Intersect the basic set "bset" with the affine space specified by the
6990 * equalities in "eq".
6992 static __isl_give isl_basic_set *basic_set_append_equalities(
6993 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
6995 int i, k;
6996 unsigned len;
6998 if (!bset || !eq)
6999 goto error;
7001 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7002 eq->n_row, 0);
7003 if (!bset)
7004 goto error;
7006 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7007 for (i = 0; i < eq->n_row; ++i) {
7008 k = isl_basic_set_alloc_equality(bset);
7009 if (k < 0)
7010 goto error;
7011 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7012 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7014 isl_mat_free(eq);
7016 bset = isl_basic_set_gauss(bset, NULL);
7017 bset = isl_basic_set_finalize(bset);
7019 return bset;
7020 error:
7021 isl_mat_free(eq);
7022 isl_basic_set_free(bset);
7023 return NULL;
7026 /* Intersect the set "set" with the affine space specified by the
7027 * equalities in "eq".
7029 static struct isl_set *set_append_equalities(struct isl_set *set,
7030 struct isl_mat *eq)
7032 int i;
7034 if (!set || !eq)
7035 goto error;
7037 for (i = 0; i < set->n; ++i) {
7038 set->p[i] = basic_set_append_equalities(set->p[i],
7039 isl_mat_copy(eq));
7040 if (!set->p[i])
7041 goto error;
7043 isl_mat_free(eq);
7044 return set;
7045 error:
7046 isl_mat_free(eq);
7047 isl_set_free(set);
7048 return NULL;
7051 /* Given a basic set "bset" that only involves parameters and existentially
7052 * quantified variables, return the index of the first equality
7053 * that only involves parameters. If there is no such equality then
7054 * return bset->n_eq.
7056 * This function assumes that isl_basic_set_gauss has been called on "bset".
7058 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7060 int i, j;
7061 unsigned nparam, n_div;
7063 if (!bset)
7064 return -1;
7066 nparam = isl_basic_set_dim(bset, isl_dim_param);
7067 n_div = isl_basic_set_dim(bset, isl_dim_div);
7069 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7070 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7071 ++i;
7074 return i;
7077 /* Compute an explicit representation for the existentially quantified
7078 * variables in "bset" by computing the "minimal value" of the set
7079 * variables. Since there are no set variables, the computation of
7080 * the minimal value essentially computes an explicit representation
7081 * of the non-empty part(s) of "bset".
7083 * The input only involves parameters and existentially quantified variables.
7084 * All equalities among parameters have been removed.
7086 * Since the existentially quantified variables in the result are in general
7087 * going to be different from those in the input, we first replace
7088 * them by the minimal number of variables based on their equalities.
7089 * This should simplify the parametric integer programming.
7091 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7093 isl_morph *morph1, *morph2;
7094 isl_set *set;
7095 unsigned n;
7097 if (!bset)
7098 return NULL;
7099 if (bset->n_eq == 0)
7100 return isl_basic_set_lexmin_compute_divs(bset);
7102 morph1 = isl_basic_set_parameter_compression(bset);
7103 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7104 bset = isl_basic_set_lift(bset);
7105 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7106 bset = isl_morph_basic_set(morph2, bset);
7107 n = isl_basic_set_dim(bset, isl_dim_set);
7108 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7110 set = isl_basic_set_lexmin_compute_divs(bset);
7112 set = isl_morph_set(isl_morph_inverse(morph1), set);
7114 return set;
7117 /* Project the given basic set onto its parameter domain, possibly introducing
7118 * new, explicit, existential variables in the constraints.
7119 * The input has parameters and (possibly implicit) existential variables.
7120 * The output has the same parameters, but only
7121 * explicit existentially quantified variables.
7123 * The actual projection is performed by pip, but pip doesn't seem
7124 * to like equalities very much, so we first remove the equalities
7125 * among the parameters by performing a variable compression on
7126 * the parameters. Afterward, an inverse transformation is performed
7127 * and the equalities among the parameters are inserted back in.
7129 * The variable compression on the parameters may uncover additional
7130 * equalities that were only implicit before. We therefore check
7131 * if there are any new parameter equalities in the result and
7132 * if so recurse. The removal of parameter equalities is required
7133 * for the parameter compression performed by base_compute_divs.
7135 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7137 int i;
7138 struct isl_mat *eq;
7139 struct isl_mat *T, *T2;
7140 struct isl_set *set;
7141 unsigned nparam;
7143 bset = isl_basic_set_cow(bset);
7144 if (!bset)
7145 return NULL;
7147 if (bset->n_eq == 0)
7148 return base_compute_divs(bset);
7150 bset = isl_basic_set_gauss(bset, NULL);
7151 if (!bset)
7152 return NULL;
7153 if (isl_basic_set_plain_is_empty(bset))
7154 return isl_set_from_basic_set(bset);
7156 i = first_parameter_equality(bset);
7157 if (i == bset->n_eq)
7158 return base_compute_divs(bset);
7160 nparam = isl_basic_set_dim(bset, isl_dim_param);
7161 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7162 0, 1 + nparam);
7163 eq = isl_mat_cow(eq);
7164 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7165 if (T && T->n_col == 0) {
7166 isl_mat_free(T);
7167 isl_mat_free(T2);
7168 isl_mat_free(eq);
7169 bset = isl_basic_set_set_to_empty(bset);
7170 return isl_set_from_basic_set(bset);
7172 bset = basic_set_parameter_preimage(bset, T);
7174 i = first_parameter_equality(bset);
7175 if (!bset)
7176 set = NULL;
7177 else if (i == bset->n_eq)
7178 set = base_compute_divs(bset);
7179 else
7180 set = parameter_compute_divs(bset);
7181 set = set_parameter_preimage(set, T2);
7182 set = set_append_equalities(set, eq);
7183 return set;
7186 /* Insert the divs from "ls" before those of "bmap".
7188 * The number of columns is not changed, which means that the last
7189 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7190 * The caller is responsible for removing the same number of dimensions
7191 * from the space of "bmap".
7193 static __isl_give isl_basic_map *insert_divs_from_local_space(
7194 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7196 int i;
7197 int n_div;
7198 int old_n_div;
7200 n_div = isl_local_space_dim(ls, isl_dim_div);
7201 if (n_div == 0)
7202 return bmap;
7204 old_n_div = bmap->n_div;
7205 bmap = insert_div_rows(bmap, n_div);
7206 if (!bmap)
7207 return NULL;
7209 for (i = 0; i < n_div; ++i) {
7210 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7211 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7214 return bmap;
7217 /* Replace the space of "bmap" by the space and divs of "ls".
7219 * If "ls" has any divs, then we simplify the result since we may
7220 * have discovered some additional equalities that could simplify
7221 * the div expressions.
7223 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7224 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7226 int n_div;
7228 bmap = isl_basic_map_cow(bmap);
7229 if (!bmap || !ls)
7230 goto error;
7232 n_div = isl_local_space_dim(ls, isl_dim_div);
7233 bmap = insert_divs_from_local_space(bmap, ls);
7234 if (!bmap)
7235 goto error;
7237 isl_space_free(bmap->dim);
7238 bmap->dim = isl_local_space_get_space(ls);
7239 if (!bmap->dim)
7240 goto error;
7242 isl_local_space_free(ls);
7243 if (n_div > 0)
7244 bmap = isl_basic_map_simplify(bmap);
7245 bmap = isl_basic_map_finalize(bmap);
7246 return bmap;
7247 error:
7248 isl_basic_map_free(bmap);
7249 isl_local_space_free(ls);
7250 return NULL;
7253 /* Replace the space of "map" by the space and divs of "ls".
7255 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7256 __isl_take isl_local_space *ls)
7258 int i;
7260 map = isl_map_cow(map);
7261 if (!map || !ls)
7262 goto error;
7264 for (i = 0; i < map->n; ++i) {
7265 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7266 isl_local_space_copy(ls));
7267 if (!map->p[i])
7268 goto error;
7270 isl_space_free(map->dim);
7271 map->dim = isl_local_space_get_space(ls);
7272 if (!map->dim)
7273 goto error;
7275 isl_local_space_free(ls);
7276 return map;
7277 error:
7278 isl_local_space_free(ls);
7279 isl_map_free(map);
7280 return NULL;
7283 /* Compute an explicit representation for the existentially
7284 * quantified variables for which do not know any explicit representation yet.
7286 * We first sort the existentially quantified variables so that the
7287 * existentially quantified variables for which we already have an explicit
7288 * representation are placed before those for which we do not.
7289 * The input dimensions, the output dimensions and the existentially
7290 * quantified variables for which we already have an explicit
7291 * representation are then turned into parameters.
7292 * compute_divs returns a map with the same parameters and
7293 * no input or output dimensions and the dimension specification
7294 * is reset to that of the input, including the existentially quantified
7295 * variables for which we already had an explicit representation.
7297 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7299 struct isl_basic_set *bset;
7300 struct isl_set *set;
7301 struct isl_map *map;
7302 isl_space *dim;
7303 isl_local_space *ls;
7304 unsigned nparam;
7305 unsigned n_in;
7306 unsigned n_out;
7307 int n_known;
7308 int i;
7310 bmap = isl_basic_map_sort_divs(bmap);
7311 bmap = isl_basic_map_cow(bmap);
7312 if (!bmap)
7313 return NULL;
7315 n_known = isl_basic_map_first_unknown_div(bmap);
7316 if (n_known < 0)
7317 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7319 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7320 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7321 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7322 dim = isl_space_set_alloc(bmap->ctx,
7323 nparam + n_in + n_out + n_known, 0);
7324 if (!dim)
7325 goto error;
7327 ls = isl_basic_map_get_local_space(bmap);
7328 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7329 n_known, bmap->n_div - n_known);
7330 if (n_known > 0) {
7331 for (i = n_known; i < bmap->n_div; ++i)
7332 swap_div(bmap, i - n_known, i);
7333 bmap->n_div -= n_known;
7334 bmap->extra -= n_known;
7336 bmap = isl_basic_map_reset_space(bmap, dim);
7337 bset = bset_from_bmap(bmap);
7339 set = parameter_compute_divs(bset);
7340 map = set_to_map(set);
7341 map = replace_space_by_local_space(map, ls);
7343 return map;
7344 error:
7345 isl_basic_map_free(bmap);
7346 return NULL;
7349 /* Remove the explicit representation of local variable "div",
7350 * if there is any.
7352 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7353 __isl_take isl_basic_map *bmap, int div)
7355 isl_bool unknown;
7357 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7358 if (unknown < 0)
7359 return isl_basic_map_free(bmap);
7360 if (unknown)
7361 return bmap;
7363 bmap = isl_basic_map_cow(bmap);
7364 if (!bmap)
7365 return NULL;
7366 isl_int_set_si(bmap->div[div][0], 0);
7367 return bmap;
7370 /* Is local variable "div" of "bmap" marked as not having an explicit
7371 * representation?
7372 * Note that even if "div" is not marked in this way and therefore
7373 * has an explicit representation, this representation may still
7374 * depend (indirectly) on other local variables that do not
7375 * have an explicit representation.
7377 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7378 int div)
7380 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7381 return isl_bool_error;
7382 return isl_int_is_zero(bmap->div[div][0]);
7385 /* Return the position of the first local variable that does not
7386 * have an explicit representation.
7387 * Return the total number of local variables if they all have
7388 * an explicit representation.
7389 * Return -1 on error.
7391 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7393 int i;
7395 if (!bmap)
7396 return -1;
7398 for (i = 0; i < bmap->n_div; ++i) {
7399 if (!isl_basic_map_div_is_known(bmap, i))
7400 return i;
7402 return bmap->n_div;
7405 /* Return the position of the first local variable that does not
7406 * have an explicit representation.
7407 * Return the total number of local variables if they all have
7408 * an explicit representation.
7409 * Return -1 on error.
7411 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7413 return isl_basic_map_first_unknown_div(bset);
7416 /* Does "bmap" have an explicit representation for all local variables?
7418 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7420 int first, n;
7422 n = isl_basic_map_dim(bmap, isl_dim_div);
7423 first = isl_basic_map_first_unknown_div(bmap);
7424 if (first < 0)
7425 return isl_bool_error;
7426 return first == n;
7429 /* Do all basic maps in "map" have an explicit representation
7430 * for all local variables?
7432 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7434 int i;
7436 if (!map)
7437 return isl_bool_error;
7439 for (i = 0; i < map->n; ++i) {
7440 int known = isl_basic_map_divs_known(map->p[i]);
7441 if (known <= 0)
7442 return known;
7445 return isl_bool_true;
7448 /* If bmap contains any unknown divs, then compute explicit
7449 * expressions for them. However, this computation may be
7450 * quite expensive, so first try to remove divs that aren't
7451 * strictly needed.
7453 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7455 int known;
7456 struct isl_map *map;
7458 known = isl_basic_map_divs_known(bmap);
7459 if (known < 0)
7460 goto error;
7461 if (known)
7462 return isl_map_from_basic_map(bmap);
7464 bmap = isl_basic_map_drop_redundant_divs(bmap);
7466 known = isl_basic_map_divs_known(bmap);
7467 if (known < 0)
7468 goto error;
7469 if (known)
7470 return isl_map_from_basic_map(bmap);
7472 map = compute_divs(bmap);
7473 return map;
7474 error:
7475 isl_basic_map_free(bmap);
7476 return NULL;
7479 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7481 int i;
7482 int known;
7483 struct isl_map *res;
7485 if (!map)
7486 return NULL;
7487 if (map->n == 0)
7488 return map;
7490 known = isl_map_divs_known(map);
7491 if (known < 0) {
7492 isl_map_free(map);
7493 return NULL;
7495 if (known)
7496 return map;
7498 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7499 for (i = 1 ; i < map->n; ++i) {
7500 struct isl_map *r2;
7501 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7502 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7503 res = isl_map_union_disjoint(res, r2);
7504 else
7505 res = isl_map_union(res, r2);
7507 isl_map_free(map);
7509 return res;
7512 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7514 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7517 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7519 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7522 struct isl_set *isl_map_domain(struct isl_map *map)
7524 int i;
7525 struct isl_set *set;
7527 if (!map)
7528 goto error;
7530 map = isl_map_cow(map);
7531 if (!map)
7532 return NULL;
7534 set = set_from_map(map);
7535 set->dim = isl_space_domain(set->dim);
7536 if (!set->dim)
7537 goto error;
7538 for (i = 0; i < map->n; ++i) {
7539 set->p[i] = isl_basic_map_domain(map->p[i]);
7540 if (!set->p[i])
7541 goto error;
7543 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7544 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7545 return set;
7546 error:
7547 isl_map_free(map);
7548 return NULL;
7551 /* Return the union of "map1" and "map2", where we assume for now that
7552 * "map1" and "map2" are disjoint. Note that the basic maps inside
7553 * "map1" or "map2" may not be disjoint from each other.
7554 * Also note that this function is also called from isl_map_union,
7555 * which takes care of handling the situation where "map1" and "map2"
7556 * may not be disjoint.
7558 * If one of the inputs is empty, we can simply return the other input.
7559 * Similarly, if one of the inputs is universal, then it is equal to the union.
7561 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7562 __isl_take isl_map *map2)
7564 int i;
7565 unsigned flags = 0;
7566 struct isl_map *map = NULL;
7567 int is_universe;
7569 if (!map1 || !map2)
7570 goto error;
7572 if (!isl_space_is_equal(map1->dim, map2->dim))
7573 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7574 "spaces don't match", goto error);
7576 if (map1->n == 0) {
7577 isl_map_free(map1);
7578 return map2;
7580 if (map2->n == 0) {
7581 isl_map_free(map2);
7582 return map1;
7585 is_universe = isl_map_plain_is_universe(map1);
7586 if (is_universe < 0)
7587 goto error;
7588 if (is_universe) {
7589 isl_map_free(map2);
7590 return map1;
7593 is_universe = isl_map_plain_is_universe(map2);
7594 if (is_universe < 0)
7595 goto error;
7596 if (is_universe) {
7597 isl_map_free(map1);
7598 return map2;
7601 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7602 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7603 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7605 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7606 map1->n + map2->n, flags);
7607 if (!map)
7608 goto error;
7609 for (i = 0; i < map1->n; ++i) {
7610 map = isl_map_add_basic_map(map,
7611 isl_basic_map_copy(map1->p[i]));
7612 if (!map)
7613 goto error;
7615 for (i = 0; i < map2->n; ++i) {
7616 map = isl_map_add_basic_map(map,
7617 isl_basic_map_copy(map2->p[i]));
7618 if (!map)
7619 goto error;
7621 isl_map_free(map1);
7622 isl_map_free(map2);
7623 return map;
7624 error:
7625 isl_map_free(map);
7626 isl_map_free(map1);
7627 isl_map_free(map2);
7628 return NULL;
7631 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7632 * guaranteed to be disjoint by the caller.
7634 * Note that this functions is called from within isl_map_make_disjoint,
7635 * so we have to be careful not to touch the constraints of the inputs
7636 * in any way.
7638 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7639 __isl_take isl_map *map2)
7641 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7644 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7645 * not be disjoint. The parameters are assumed to have been aligned.
7647 * We currently simply call map_union_disjoint, the internal operation
7648 * of which does not really depend on the inputs being disjoint.
7649 * If the result contains more than one basic map, then we clear
7650 * the disjoint flag since the result may contain basic maps from
7651 * both inputs and these are not guaranteed to be disjoint.
7653 * As a special case, if "map1" and "map2" are obviously equal,
7654 * then we simply return "map1".
7656 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7657 __isl_take isl_map *map2)
7659 int equal;
7661 if (!map1 || !map2)
7662 goto error;
7664 equal = isl_map_plain_is_equal(map1, map2);
7665 if (equal < 0)
7666 goto error;
7667 if (equal) {
7668 isl_map_free(map2);
7669 return map1;
7672 map1 = map_union_disjoint(map1, map2);
7673 if (!map1)
7674 return NULL;
7675 if (map1->n > 1)
7676 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7677 return map1;
7678 error:
7679 isl_map_free(map1);
7680 isl_map_free(map2);
7681 return NULL;
7684 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7685 * not be disjoint.
7687 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7688 __isl_take isl_map *map2)
7690 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7693 struct isl_set *isl_set_union_disjoint(
7694 struct isl_set *set1, struct isl_set *set2)
7696 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7697 set_to_map(set2)));
7700 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7702 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7705 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7706 * the results.
7708 * "map" and "set" are assumed to be compatible and non-NULL.
7710 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7711 __isl_take isl_set *set,
7712 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7713 __isl_take isl_basic_set *bset))
7715 unsigned flags = 0;
7716 struct isl_map *result;
7717 int i, j;
7719 if (isl_set_plain_is_universe(set)) {
7720 isl_set_free(set);
7721 return map;
7724 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7725 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7726 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7728 result = isl_map_alloc_space(isl_space_copy(map->dim),
7729 map->n * set->n, flags);
7730 for (i = 0; result && i < map->n; ++i)
7731 for (j = 0; j < set->n; ++j) {
7732 result = isl_map_add_basic_map(result,
7733 fn(isl_basic_map_copy(map->p[i]),
7734 isl_basic_set_copy(set->p[j])));
7735 if (!result)
7736 break;
7739 isl_map_free(map);
7740 isl_set_free(set);
7741 return result;
7744 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7745 __isl_take isl_set *set)
7747 isl_bool ok;
7749 ok = isl_map_compatible_range(map, set);
7750 if (ok < 0)
7751 goto error;
7752 if (!ok)
7753 isl_die(set->ctx, isl_error_invalid,
7754 "incompatible spaces", goto error);
7756 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7757 error:
7758 isl_map_free(map);
7759 isl_set_free(set);
7760 return NULL;
7763 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7764 __isl_take isl_set *set)
7766 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7769 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7770 __isl_take isl_set *set)
7772 isl_bool ok;
7774 ok = isl_map_compatible_domain(map, set);
7775 if (ok < 0)
7776 goto error;
7777 if (!ok)
7778 isl_die(set->ctx, isl_error_invalid,
7779 "incompatible spaces", goto error);
7781 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7782 error:
7783 isl_map_free(map);
7784 isl_set_free(set);
7785 return NULL;
7788 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7789 __isl_take isl_set *set)
7791 return isl_map_align_params_map_map_and(map, set,
7792 &map_intersect_domain);
7795 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7796 __isl_take isl_map *map2)
7798 if (!map1 || !map2)
7799 goto error;
7800 map1 = isl_map_reverse(map1);
7801 map1 = isl_map_apply_range(map1, map2);
7802 return isl_map_reverse(map1);
7803 error:
7804 isl_map_free(map1);
7805 isl_map_free(map2);
7806 return NULL;
7809 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7810 __isl_take isl_map *map2)
7812 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7815 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7816 __isl_take isl_map *map2)
7818 isl_space *dim_result;
7819 struct isl_map *result;
7820 int i, j;
7822 if (!map1 || !map2)
7823 goto error;
7825 dim_result = isl_space_join(isl_space_copy(map1->dim),
7826 isl_space_copy(map2->dim));
7828 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7829 if (!result)
7830 goto error;
7831 for (i = 0; i < map1->n; ++i)
7832 for (j = 0; j < map2->n; ++j) {
7833 result = isl_map_add_basic_map(result,
7834 isl_basic_map_apply_range(
7835 isl_basic_map_copy(map1->p[i]),
7836 isl_basic_map_copy(map2->p[j])));
7837 if (!result)
7838 goto error;
7840 isl_map_free(map1);
7841 isl_map_free(map2);
7842 if (result && result->n <= 1)
7843 ISL_F_SET(result, ISL_MAP_DISJOINT);
7844 return result;
7845 error:
7846 isl_map_free(map1);
7847 isl_map_free(map2);
7848 return NULL;
7851 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7852 __isl_take isl_map *map2)
7854 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7858 * returns range - domain
7860 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7862 isl_space *target_space;
7863 struct isl_basic_set *bset;
7864 unsigned dim;
7865 unsigned nparam;
7866 int i;
7868 if (!bmap)
7869 goto error;
7870 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7871 bmap->dim, isl_dim_out),
7872 goto error);
7873 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7874 dim = isl_basic_map_dim(bmap, isl_dim_in);
7875 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7876 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7877 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7878 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7879 for (i = 0; i < dim; ++i) {
7880 int j = isl_basic_map_alloc_equality(bmap);
7881 if (j < 0) {
7882 bmap = isl_basic_map_free(bmap);
7883 break;
7885 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7886 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7887 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7888 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7890 bset = isl_basic_map_domain(bmap);
7891 bset = isl_basic_set_reset_space(bset, target_space);
7892 return bset;
7893 error:
7894 isl_basic_map_free(bmap);
7895 return NULL;
7899 * returns range - domain
7901 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7903 int i;
7904 isl_space *dim;
7905 struct isl_set *result;
7907 if (!map)
7908 return NULL;
7910 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7911 map->dim, isl_dim_out),
7912 goto error);
7913 dim = isl_map_get_space(map);
7914 dim = isl_space_domain(dim);
7915 result = isl_set_alloc_space(dim, map->n, 0);
7916 if (!result)
7917 goto error;
7918 for (i = 0; i < map->n; ++i)
7919 result = isl_set_add_basic_set(result,
7920 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7921 isl_map_free(map);
7922 return result;
7923 error:
7924 isl_map_free(map);
7925 return NULL;
7929 * returns [domain -> range] -> range - domain
7931 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7932 __isl_take isl_basic_map *bmap)
7934 int i, k;
7935 isl_space *dim;
7936 isl_basic_map *domain;
7937 int nparam, n;
7938 unsigned total;
7940 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7941 bmap->dim, isl_dim_out))
7942 isl_die(bmap->ctx, isl_error_invalid,
7943 "domain and range don't match", goto error);
7945 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7946 n = isl_basic_map_dim(bmap, isl_dim_in);
7948 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7949 domain = isl_basic_map_universe(dim);
7951 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7952 bmap = isl_basic_map_apply_range(bmap, domain);
7953 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7955 total = isl_basic_map_total_dim(bmap);
7957 for (i = 0; i < n; ++i) {
7958 k = isl_basic_map_alloc_equality(bmap);
7959 if (k < 0)
7960 goto error;
7961 isl_seq_clr(bmap->eq[k], 1 + total);
7962 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7963 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7964 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7967 bmap = isl_basic_map_gauss(bmap, NULL);
7968 return isl_basic_map_finalize(bmap);
7969 error:
7970 isl_basic_map_free(bmap);
7971 return NULL;
7975 * returns [domain -> range] -> range - domain
7977 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7979 int i;
7980 isl_space *domain_dim;
7982 if (!map)
7983 return NULL;
7985 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7986 map->dim, isl_dim_out))
7987 isl_die(map->ctx, isl_error_invalid,
7988 "domain and range don't match", goto error);
7990 map = isl_map_cow(map);
7991 if (!map)
7992 return NULL;
7994 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7995 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7996 map->dim = isl_space_join(map->dim, domain_dim);
7997 if (!map->dim)
7998 goto error;
7999 for (i = 0; i < map->n; ++i) {
8000 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8001 if (!map->p[i])
8002 goto error;
8004 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8005 return map;
8006 error:
8007 isl_map_free(map);
8008 return NULL;
8011 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
8013 struct isl_basic_map *bmap;
8014 unsigned nparam;
8015 unsigned dim;
8016 int i;
8018 if (!dims)
8019 return NULL;
8021 nparam = dims->nparam;
8022 dim = dims->n_out;
8023 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
8024 if (!bmap)
8025 goto error;
8027 for (i = 0; i < dim; ++i) {
8028 int j = isl_basic_map_alloc_equality(bmap);
8029 if (j < 0)
8030 goto error;
8031 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8032 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8033 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
8035 return isl_basic_map_finalize(bmap);
8036 error:
8037 isl_basic_map_free(bmap);
8038 return NULL;
8041 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
8043 if (!dim)
8044 return NULL;
8045 if (dim->n_in != dim->n_out)
8046 isl_die(dim->ctx, isl_error_invalid,
8047 "number of input and output dimensions needs to be "
8048 "the same", goto error);
8049 return basic_map_identity(dim);
8050 error:
8051 isl_space_free(dim);
8052 return NULL;
8055 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8057 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8060 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8062 isl_space *dim = isl_set_get_space(set);
8063 isl_map *id;
8064 id = isl_map_identity(isl_space_map_from_set(dim));
8065 return isl_map_intersect_range(id, set);
8068 /* Construct a basic set with all set dimensions having only non-negative
8069 * values.
8071 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8072 __isl_take isl_space *space)
8074 int i;
8075 unsigned nparam;
8076 unsigned dim;
8077 struct isl_basic_set *bset;
8079 if (!space)
8080 return NULL;
8081 nparam = space->nparam;
8082 dim = space->n_out;
8083 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8084 if (!bset)
8085 return NULL;
8086 for (i = 0; i < dim; ++i) {
8087 int k = isl_basic_set_alloc_inequality(bset);
8088 if (k < 0)
8089 goto error;
8090 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8091 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8093 return bset;
8094 error:
8095 isl_basic_set_free(bset);
8096 return NULL;
8099 /* Construct the half-space x_pos >= 0.
8101 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
8102 int pos)
8104 int k;
8105 isl_basic_set *nonneg;
8107 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8108 k = isl_basic_set_alloc_inequality(nonneg);
8109 if (k < 0)
8110 goto error;
8111 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8112 isl_int_set_si(nonneg->ineq[k][pos], 1);
8114 return isl_basic_set_finalize(nonneg);
8115 error:
8116 isl_basic_set_free(nonneg);
8117 return NULL;
8120 /* Construct the half-space x_pos <= -1.
8122 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
8124 int k;
8125 isl_basic_set *neg;
8127 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8128 k = isl_basic_set_alloc_inequality(neg);
8129 if (k < 0)
8130 goto error;
8131 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8132 isl_int_set_si(neg->ineq[k][0], -1);
8133 isl_int_set_si(neg->ineq[k][pos], -1);
8135 return isl_basic_set_finalize(neg);
8136 error:
8137 isl_basic_set_free(neg);
8138 return NULL;
8141 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8142 enum isl_dim_type type, unsigned first, unsigned n)
8144 int i;
8145 unsigned offset;
8146 isl_basic_set *nonneg;
8147 isl_basic_set *neg;
8149 if (!set)
8150 return NULL;
8151 if (n == 0)
8152 return set;
8154 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
8156 offset = pos(set->dim, type);
8157 for (i = 0; i < n; ++i) {
8158 nonneg = nonneg_halfspace(isl_set_get_space(set),
8159 offset + first + i);
8160 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8162 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8165 return set;
8166 error:
8167 isl_set_free(set);
8168 return NULL;
8171 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8172 int len,
8173 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8174 void *user)
8176 isl_set *half;
8178 if (!set)
8179 return isl_stat_error;
8180 if (isl_set_plain_is_empty(set)) {
8181 isl_set_free(set);
8182 return isl_stat_ok;
8184 if (first == len)
8185 return fn(set, signs, user);
8187 signs[first] = 1;
8188 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8189 1 + first));
8190 half = isl_set_intersect(half, isl_set_copy(set));
8191 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8192 goto error;
8194 signs[first] = -1;
8195 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8196 1 + first));
8197 half = isl_set_intersect(half, set);
8198 return foreach_orthant(half, signs, first + 1, len, fn, user);
8199 error:
8200 isl_set_free(set);
8201 return isl_stat_error;
8204 /* Call "fn" on the intersections of "set" with each of the orthants
8205 * (except for obviously empty intersections). The orthant is identified
8206 * by the signs array, with each entry having value 1 or -1 according
8207 * to the sign of the corresponding variable.
8209 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8210 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8211 void *user)
8213 unsigned nparam;
8214 unsigned nvar;
8215 int *signs;
8216 isl_stat r;
8218 if (!set)
8219 return isl_stat_error;
8220 if (isl_set_plain_is_empty(set))
8221 return isl_stat_ok;
8223 nparam = isl_set_dim(set, isl_dim_param);
8224 nvar = isl_set_dim(set, isl_dim_set);
8226 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8228 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8229 fn, user);
8231 free(signs);
8233 return r;
8236 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8238 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8241 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8242 __isl_keep isl_basic_map *bmap2)
8244 int is_subset;
8245 struct isl_map *map1;
8246 struct isl_map *map2;
8248 if (!bmap1 || !bmap2)
8249 return isl_bool_error;
8251 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8252 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8254 is_subset = isl_map_is_subset(map1, map2);
8256 isl_map_free(map1);
8257 isl_map_free(map2);
8259 return is_subset;
8262 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8263 __isl_keep isl_basic_set *bset2)
8265 return isl_basic_map_is_subset(bset1, bset2);
8268 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8269 __isl_keep isl_basic_map *bmap2)
8271 isl_bool is_subset;
8273 if (!bmap1 || !bmap2)
8274 return isl_bool_error;
8275 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8276 if (is_subset != isl_bool_true)
8277 return is_subset;
8278 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8279 return is_subset;
8282 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8283 __isl_keep isl_basic_set *bset2)
8285 return isl_basic_map_is_equal(
8286 bset_to_bmap(bset1), bset_to_bmap(bset2));
8289 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8291 int i;
8292 int is_empty;
8294 if (!map)
8295 return isl_bool_error;
8296 for (i = 0; i < map->n; ++i) {
8297 is_empty = isl_basic_map_is_empty(map->p[i]);
8298 if (is_empty < 0)
8299 return isl_bool_error;
8300 if (!is_empty)
8301 return isl_bool_false;
8303 return isl_bool_true;
8306 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8308 return map ? map->n == 0 : isl_bool_error;
8311 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8313 return set ? set->n == 0 : isl_bool_error;
8316 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8318 return isl_map_is_empty(set_to_map(set));
8321 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8322 __isl_keep isl_map *map2)
8324 if (!map1 || !map2)
8325 return isl_bool_error;
8327 return isl_space_is_equal(map1->dim, map2->dim);
8330 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8331 __isl_keep isl_set *set2)
8333 if (!set1 || !set2)
8334 return isl_bool_error;
8336 return isl_space_is_equal(set1->dim, set2->dim);
8339 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8341 isl_bool is_subset;
8343 if (!map1 || !map2)
8344 return isl_bool_error;
8345 is_subset = isl_map_is_subset(map1, map2);
8346 if (is_subset != isl_bool_true)
8347 return is_subset;
8348 is_subset = isl_map_is_subset(map2, map1);
8349 return is_subset;
8352 /* Is "map1" equal to "map2"?
8354 * First check if they are obviously equal.
8355 * If not, then perform a more detailed analysis.
8357 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8359 isl_bool equal;
8361 equal = isl_map_plain_is_equal(map1, map2);
8362 if (equal < 0 || equal)
8363 return equal;
8364 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8367 isl_bool isl_basic_map_is_strict_subset(
8368 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8370 isl_bool is_subset;
8372 if (!bmap1 || !bmap2)
8373 return isl_bool_error;
8374 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8375 if (is_subset != isl_bool_true)
8376 return is_subset;
8377 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8378 if (is_subset == isl_bool_error)
8379 return is_subset;
8380 return !is_subset;
8383 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8384 __isl_keep isl_map *map2)
8386 isl_bool is_subset;
8388 if (!map1 || !map2)
8389 return isl_bool_error;
8390 is_subset = isl_map_is_subset(map1, map2);
8391 if (is_subset != isl_bool_true)
8392 return is_subset;
8393 is_subset = isl_map_is_subset(map2, map1);
8394 if (is_subset == isl_bool_error)
8395 return is_subset;
8396 return !is_subset;
8399 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8400 __isl_keep isl_set *set2)
8402 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8405 /* Is "bmap" obviously equal to the universe with the same space?
8407 * That is, does it not have any constraints?
8409 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8411 if (!bmap)
8412 return isl_bool_error;
8413 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8416 /* Is "bset" obviously equal to the universe with the same space?
8418 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8420 return isl_basic_map_plain_is_universe(bset);
8423 /* If "c" does not involve any existentially quantified variables,
8424 * then set *univ to false and abort
8426 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8428 isl_bool *univ = user;
8429 unsigned n;
8431 n = isl_constraint_dim(c, isl_dim_div);
8432 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8433 isl_constraint_free(c);
8434 if (*univ < 0 || !*univ)
8435 return isl_stat_error;
8436 return isl_stat_ok;
8439 /* Is "bmap" equal to the universe with the same space?
8441 * First check if it is obviously equal to the universe.
8442 * If not and if there are any constraints not involving
8443 * existentially quantified variables, then it is certainly
8444 * not equal to the universe.
8445 * Otherwise, check if the universe is a subset of "bmap".
8447 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8449 isl_bool univ;
8450 isl_basic_map *test;
8452 univ = isl_basic_map_plain_is_universe(bmap);
8453 if (univ < 0 || univ)
8454 return univ;
8455 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8456 return isl_bool_false;
8457 univ = isl_bool_true;
8458 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8459 univ)
8460 return isl_bool_error;
8461 if (univ < 0 || !univ)
8462 return univ;
8463 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8464 univ = isl_basic_map_is_subset(test, bmap);
8465 isl_basic_map_free(test);
8466 return univ;
8469 /* Is "bset" equal to the universe with the same space?
8471 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8473 return isl_basic_map_is_universe(bset);
8476 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8478 int i;
8480 if (!map)
8481 return isl_bool_error;
8483 for (i = 0; i < map->n; ++i) {
8484 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8485 if (r < 0 || r)
8486 return r;
8489 return isl_bool_false;
8492 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8494 return isl_map_plain_is_universe(set_to_map(set));
8497 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8499 struct isl_basic_set *bset = NULL;
8500 struct isl_vec *sample = NULL;
8501 isl_bool empty, non_empty;
8503 if (!bmap)
8504 return isl_bool_error;
8506 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8507 return isl_bool_true;
8509 if (isl_basic_map_plain_is_universe(bmap))
8510 return isl_bool_false;
8512 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8513 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8514 copy = isl_basic_map_remove_redundancies(copy);
8515 empty = isl_basic_map_plain_is_empty(copy);
8516 isl_basic_map_free(copy);
8517 return empty;
8520 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8521 if (non_empty < 0)
8522 return isl_bool_error;
8523 if (non_empty)
8524 return isl_bool_false;
8525 isl_vec_free(bmap->sample);
8526 bmap->sample = NULL;
8527 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8528 if (!bset)
8529 return isl_bool_error;
8530 sample = isl_basic_set_sample_vec(bset);
8531 if (!sample)
8532 return isl_bool_error;
8533 empty = sample->size == 0;
8534 isl_vec_free(bmap->sample);
8535 bmap->sample = sample;
8536 if (empty)
8537 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8539 return empty;
8542 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8544 if (!bmap)
8545 return isl_bool_error;
8546 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8549 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8551 if (!bset)
8552 return isl_bool_error;
8553 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8556 /* Is "bmap" known to be non-empty?
8558 * That is, is the cached sample still valid?
8560 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8562 unsigned total;
8564 if (!bmap)
8565 return isl_bool_error;
8566 if (!bmap->sample)
8567 return isl_bool_false;
8568 total = 1 + isl_basic_map_total_dim(bmap);
8569 if (bmap->sample->size != total)
8570 return isl_bool_false;
8571 return isl_basic_map_contains(bmap, bmap->sample);
8574 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8576 return isl_basic_map_is_empty(bset_to_bmap(bset));
8579 struct isl_map *isl_basic_map_union(
8580 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8582 struct isl_map *map;
8583 if (!bmap1 || !bmap2)
8584 goto error;
8586 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8588 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8589 if (!map)
8590 goto error;
8591 map = isl_map_add_basic_map(map, bmap1);
8592 map = isl_map_add_basic_map(map, bmap2);
8593 return map;
8594 error:
8595 isl_basic_map_free(bmap1);
8596 isl_basic_map_free(bmap2);
8597 return NULL;
8600 struct isl_set *isl_basic_set_union(
8601 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8603 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8604 bset_to_bmap(bset2)));
8607 /* Order divs such that any div only depends on previous divs */
8608 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8610 int i;
8611 unsigned off;
8613 if (!bmap)
8614 return NULL;
8616 off = isl_space_dim(bmap->dim, isl_dim_all);
8618 for (i = 0; i < bmap->n_div; ++i) {
8619 int pos;
8620 if (isl_int_is_zero(bmap->div[i][0]))
8621 continue;
8622 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8623 bmap->n_div-i);
8624 if (pos == -1)
8625 continue;
8626 if (pos == 0)
8627 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8628 "integer division depends on itself",
8629 return isl_basic_map_free(bmap));
8630 isl_basic_map_swap_div(bmap, i, i + pos);
8631 --i;
8633 return bmap;
8636 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8638 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8641 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8643 int i;
8645 if (!map)
8646 return 0;
8648 for (i = 0; i < map->n; ++i) {
8649 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8650 if (!map->p[i])
8651 goto error;
8654 return map;
8655 error:
8656 isl_map_free(map);
8657 return NULL;
8660 /* Sort the local variables of "bset".
8662 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8663 __isl_take isl_basic_set *bset)
8665 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8668 /* Apply the expansion computed by isl_merge_divs.
8669 * The expansion itself is given by "exp" while the resulting
8670 * list of divs is given by "div".
8672 * Move the integer divisions of "bmap" into the right position
8673 * according to "exp" and then introduce the additional integer
8674 * divisions, adding div constraints.
8675 * The moving should be done first to avoid moving coefficients
8676 * in the definitions of the extra integer divisions.
8678 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8679 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8681 int i, j;
8682 int n_div;
8684 bmap = isl_basic_map_cow(bmap);
8685 if (!bmap || !div)
8686 goto error;
8688 if (div->n_row < bmap->n_div)
8689 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8690 "not an expansion", goto error);
8692 n_div = bmap->n_div;
8693 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8694 div->n_row - n_div, 0,
8695 2 * (div->n_row - n_div));
8697 for (i = n_div; i < div->n_row; ++i)
8698 if (isl_basic_map_alloc_div(bmap) < 0)
8699 goto error;
8701 for (j = n_div - 1; j >= 0; --j) {
8702 if (exp[j] == j)
8703 break;
8704 isl_basic_map_swap_div(bmap, j, exp[j]);
8706 j = 0;
8707 for (i = 0; i < div->n_row; ++i) {
8708 if (j < n_div && exp[j] == i) {
8709 j++;
8710 } else {
8711 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8712 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8713 continue;
8714 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8715 goto error;
8719 isl_mat_free(div);
8720 return bmap;
8721 error:
8722 isl_basic_map_free(bmap);
8723 isl_mat_free(div);
8724 return NULL;
8727 /* Apply the expansion computed by isl_merge_divs.
8728 * The expansion itself is given by "exp" while the resulting
8729 * list of divs is given by "div".
8731 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8732 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8734 return isl_basic_map_expand_divs(bset, div, exp);
8737 /* Look for a div in dst that corresponds to the div "div" in src.
8738 * The divs before "div" in src and dst are assumed to be the same.
8740 * Returns -1 if no corresponding div was found and the position
8741 * of the corresponding div in dst otherwise.
8743 static int find_div(__isl_keep isl_basic_map *dst,
8744 __isl_keep isl_basic_map *src, unsigned div)
8746 int i;
8748 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8750 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8751 for (i = div; i < dst->n_div; ++i)
8752 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8753 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8754 dst->n_div - div) == -1)
8755 return i;
8756 return -1;
8759 /* Align the divs of "dst" to those of "src", adding divs from "src"
8760 * if needed. That is, make sure that the first src->n_div divs
8761 * of the result are equal to those of src.
8763 * The result is not finalized as by design it will have redundant
8764 * divs if any divs from "src" were copied.
8766 __isl_give isl_basic_map *isl_basic_map_align_divs(
8767 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8769 int i;
8770 int known, extended;
8771 unsigned total;
8773 if (!dst || !src)
8774 return isl_basic_map_free(dst);
8776 if (src->n_div == 0)
8777 return dst;
8779 known = isl_basic_map_divs_known(src);
8780 if (known < 0)
8781 return isl_basic_map_free(dst);
8782 if (!known)
8783 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8784 "some src divs are unknown",
8785 return isl_basic_map_free(dst));
8787 src = isl_basic_map_order_divs(src);
8789 extended = 0;
8790 total = isl_space_dim(src->dim, isl_dim_all);
8791 for (i = 0; i < src->n_div; ++i) {
8792 int j = find_div(dst, src, i);
8793 if (j < 0) {
8794 if (!extended) {
8795 int extra = src->n_div - i;
8796 dst = isl_basic_map_cow(dst);
8797 if (!dst)
8798 return NULL;
8799 dst = isl_basic_map_extend_space(dst,
8800 isl_space_copy(dst->dim),
8801 extra, 0, 2 * extra);
8802 extended = 1;
8804 j = isl_basic_map_alloc_div(dst);
8805 if (j < 0)
8806 return isl_basic_map_free(dst);
8807 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8808 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8809 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8810 return isl_basic_map_free(dst);
8812 if (j != i)
8813 isl_basic_map_swap_div(dst, i, j);
8815 return dst;
8818 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8820 int i;
8822 if (!map)
8823 return NULL;
8824 if (map->n == 0)
8825 return map;
8826 map = isl_map_compute_divs(map);
8827 map = isl_map_cow(map);
8828 if (!map)
8829 return NULL;
8831 for (i = 1; i < map->n; ++i)
8832 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8833 for (i = 1; i < map->n; ++i) {
8834 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8835 if (!map->p[i])
8836 return isl_map_free(map);
8839 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8840 return map;
8843 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8845 return isl_map_align_divs_internal(map);
8848 struct isl_set *isl_set_align_divs(struct isl_set *set)
8850 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8853 /* Align the divs of the basic maps in "map" to those
8854 * of the basic maps in "list", as well as to the other basic maps in "map".
8855 * The elements in "list" are assumed to have known divs.
8857 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8858 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8860 int i, n;
8862 map = isl_map_compute_divs(map);
8863 map = isl_map_cow(map);
8864 if (!map || !list)
8865 return isl_map_free(map);
8866 if (map->n == 0)
8867 return map;
8869 n = isl_basic_map_list_n_basic_map(list);
8870 for (i = 0; i < n; ++i) {
8871 isl_basic_map *bmap;
8873 bmap = isl_basic_map_list_get_basic_map(list, i);
8874 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8875 isl_basic_map_free(bmap);
8877 if (!map->p[0])
8878 return isl_map_free(map);
8880 return isl_map_align_divs_internal(map);
8883 /* Align the divs of each element of "list" to those of "bmap".
8884 * Both "bmap" and the elements of "list" are assumed to have known divs.
8886 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8887 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8889 int i, n;
8891 if (!list || !bmap)
8892 return isl_basic_map_list_free(list);
8894 n = isl_basic_map_list_n_basic_map(list);
8895 for (i = 0; i < n; ++i) {
8896 isl_basic_map *bmap_i;
8898 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8899 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8900 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8903 return list;
8906 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8907 __isl_take isl_map *map)
8909 isl_bool ok;
8911 ok = isl_map_compatible_domain(map, set);
8912 if (ok < 0)
8913 goto error;
8914 if (!ok)
8915 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8916 "incompatible spaces", goto error);
8917 map = isl_map_intersect_domain(map, set);
8918 set = isl_map_range(map);
8919 return set;
8920 error:
8921 isl_set_free(set);
8922 isl_map_free(map);
8923 return NULL;
8926 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8927 __isl_take isl_map *map)
8929 return isl_map_align_params_map_map_and(set, map, &set_apply);
8932 /* There is no need to cow as removing empty parts doesn't change
8933 * the meaning of the set.
8935 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8937 int i;
8939 if (!map)
8940 return NULL;
8942 for (i = map->n - 1; i >= 0; --i)
8943 remove_if_empty(map, i);
8945 return map;
8948 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8950 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8953 /* Given two basic sets bset1 and bset2, compute the maximal difference
8954 * between the values of dimension pos in bset1 and those in bset2
8955 * for any common value of the parameters and dimensions preceding pos.
8957 static enum isl_lp_result basic_set_maximal_difference_at(
8958 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8959 int pos, isl_int *opt)
8961 isl_basic_map *bmap1;
8962 isl_basic_map *bmap2;
8963 struct isl_ctx *ctx;
8964 struct isl_vec *obj;
8965 unsigned total;
8966 unsigned nparam;
8967 unsigned dim1;
8968 enum isl_lp_result res;
8970 if (!bset1 || !bset2)
8971 return isl_lp_error;
8973 nparam = isl_basic_set_n_param(bset1);
8974 dim1 = isl_basic_set_n_dim(bset1);
8976 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8977 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8978 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8979 isl_dim_out, 0, pos);
8980 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8981 isl_dim_out, 0, pos);
8982 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8983 if (!bmap1)
8984 return isl_lp_error;
8986 total = isl_basic_map_total_dim(bmap1);
8987 ctx = bmap1->ctx;
8988 obj = isl_vec_alloc(ctx, 1 + total);
8989 if (!obj)
8990 goto error;
8991 isl_seq_clr(obj->block.data, 1 + total);
8992 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8993 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8994 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8995 opt, NULL, NULL);
8996 isl_basic_map_free(bmap1);
8997 isl_vec_free(obj);
8998 return res;
8999 error:
9000 isl_basic_map_free(bmap1);
9001 return isl_lp_error;
9004 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9005 * for any common value of the parameters and dimensions preceding pos
9006 * in both basic sets, the values of dimension pos in bset1 are
9007 * smaller or larger than those in bset2.
9009 * Returns
9010 * 1 if bset1 follows bset2
9011 * -1 if bset1 precedes bset2
9012 * 0 if bset1 and bset2 are incomparable
9013 * -2 if some error occurred.
9015 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
9016 struct isl_basic_set *bset2, int pos)
9018 isl_int opt;
9019 enum isl_lp_result res;
9020 int cmp;
9022 isl_int_init(opt);
9024 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9026 if (res == isl_lp_empty)
9027 cmp = 0;
9028 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9029 res == isl_lp_unbounded)
9030 cmp = 1;
9031 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9032 cmp = -1;
9033 else
9034 cmp = -2;
9036 isl_int_clear(opt);
9037 return cmp;
9040 /* Given two basic sets bset1 and bset2, check whether
9041 * for any common value of the parameters and dimensions preceding pos
9042 * there is a value of dimension pos in bset1 that is larger
9043 * than a value of the same dimension in bset2.
9045 * Return
9046 * 1 if there exists such a pair
9047 * 0 if there is no such pair, but there is a pair of equal values
9048 * -1 otherwise
9049 * -2 if some error occurred.
9051 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9052 __isl_keep isl_basic_set *bset2, int pos)
9054 isl_int opt;
9055 enum isl_lp_result res;
9056 int cmp;
9058 isl_int_init(opt);
9060 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9062 if (res == isl_lp_empty)
9063 cmp = -1;
9064 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9065 res == isl_lp_unbounded)
9066 cmp = 1;
9067 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9068 cmp = -1;
9069 else if (res == isl_lp_ok)
9070 cmp = 0;
9071 else
9072 cmp = -2;
9074 isl_int_clear(opt);
9075 return cmp;
9078 /* Given two sets set1 and set2, check whether
9079 * for any common value of the parameters and dimensions preceding pos
9080 * there is a value of dimension pos in set1 that is larger
9081 * than a value of the same dimension in set2.
9083 * Return
9084 * 1 if there exists such a pair
9085 * 0 if there is no such pair, but there is a pair of equal values
9086 * -1 otherwise
9087 * -2 if some error occurred.
9089 int isl_set_follows_at(__isl_keep isl_set *set1,
9090 __isl_keep isl_set *set2, int pos)
9092 int i, j;
9093 int follows = -1;
9095 if (!set1 || !set2)
9096 return -2;
9098 for (i = 0; i < set1->n; ++i)
9099 for (j = 0; j < set2->n; ++j) {
9100 int f;
9101 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9102 if (f == 1 || f == -2)
9103 return f;
9104 if (f > follows)
9105 follows = f;
9108 return follows;
9111 static isl_bool isl_basic_map_plain_has_fixed_var(
9112 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9114 int i;
9115 int d;
9116 unsigned total;
9118 if (!bmap)
9119 return isl_bool_error;
9120 total = isl_basic_map_total_dim(bmap);
9121 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9122 for (; d+1 > pos; --d)
9123 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9124 break;
9125 if (d != pos)
9126 continue;
9127 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9128 return isl_bool_false;
9129 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9130 return isl_bool_false;
9131 if (!isl_int_is_one(bmap->eq[i][1+d]))
9132 return isl_bool_false;
9133 if (val)
9134 isl_int_neg(*val, bmap->eq[i][0]);
9135 return isl_bool_true;
9137 return isl_bool_false;
9140 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9141 unsigned pos, isl_int *val)
9143 int i;
9144 isl_int v;
9145 isl_int tmp;
9146 isl_bool fixed;
9148 if (!map)
9149 return isl_bool_error;
9150 if (map->n == 0)
9151 return isl_bool_false;
9152 if (map->n == 1)
9153 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9154 isl_int_init(v);
9155 isl_int_init(tmp);
9156 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9157 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9158 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9159 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9160 fixed = isl_bool_false;
9162 if (val)
9163 isl_int_set(*val, v);
9164 isl_int_clear(tmp);
9165 isl_int_clear(v);
9166 return fixed;
9169 static isl_bool isl_basic_set_plain_has_fixed_var(
9170 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9172 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9173 pos, val);
9176 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9177 enum isl_dim_type type, unsigned pos, isl_int *val)
9179 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9180 return isl_bool_error;
9181 return isl_basic_map_plain_has_fixed_var(bmap,
9182 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9185 /* If "bmap" obviously lies on a hyperplane where the given dimension
9186 * has a fixed value, then return that value.
9187 * Otherwise return NaN.
9189 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9190 __isl_keep isl_basic_map *bmap,
9191 enum isl_dim_type type, unsigned pos)
9193 isl_ctx *ctx;
9194 isl_val *v;
9195 isl_bool fixed;
9197 if (!bmap)
9198 return NULL;
9199 ctx = isl_basic_map_get_ctx(bmap);
9200 v = isl_val_alloc(ctx);
9201 if (!v)
9202 return NULL;
9203 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9204 if (fixed < 0)
9205 return isl_val_free(v);
9206 if (fixed) {
9207 isl_int_set_si(v->d, 1);
9208 return v;
9210 isl_val_free(v);
9211 return isl_val_nan(ctx);
9214 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9215 enum isl_dim_type type, unsigned pos, isl_int *val)
9217 if (pos >= isl_map_dim(map, type))
9218 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9219 "position out of bounds", return isl_bool_error);
9220 return isl_map_plain_has_fixed_var(map,
9221 map_offset(map, type) - 1 + pos, val);
9224 /* If "map" obviously lies on a hyperplane where the given dimension
9225 * has a fixed value, then return that value.
9226 * Otherwise return NaN.
9228 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9229 enum isl_dim_type type, unsigned pos)
9231 isl_ctx *ctx;
9232 isl_val *v;
9233 isl_bool fixed;
9235 if (!map)
9236 return NULL;
9237 ctx = isl_map_get_ctx(map);
9238 v = isl_val_alloc(ctx);
9239 if (!v)
9240 return NULL;
9241 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9242 if (fixed < 0)
9243 return isl_val_free(v);
9244 if (fixed) {
9245 isl_int_set_si(v->d, 1);
9246 return v;
9248 isl_val_free(v);
9249 return isl_val_nan(ctx);
9252 /* If "set" obviously lies on a hyperplane where the given dimension
9253 * has a fixed value, then return that value.
9254 * Otherwise return NaN.
9256 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9257 enum isl_dim_type type, unsigned pos)
9259 return isl_map_plain_get_val_if_fixed(set, type, pos);
9262 isl_bool isl_set_plain_is_fixed(__isl_keep isl_set *set,
9263 enum isl_dim_type type, unsigned pos, isl_int *val)
9265 return isl_map_plain_is_fixed(set, type, pos, val);
9268 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9269 * then return this fixed value in *val.
9271 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9272 unsigned dim, isl_int *val)
9274 return isl_basic_set_plain_has_fixed_var(bset,
9275 isl_basic_set_n_param(bset) + dim, val);
9278 /* Return -1 if the constraint "c1" should be sorted before "c2"
9279 * and 1 if it should be sorted after "c2".
9280 * Return 0 if the two constraints are the same (up to the constant term).
9282 * In particular, if a constraint involves later variables than another
9283 * then it is sorted after this other constraint.
9284 * uset_gist depends on constraints without existentially quantified
9285 * variables sorting first.
9287 * For constraints that have the same latest variable, those
9288 * with the same coefficient for this latest variable (first in absolute value
9289 * and then in actual value) are grouped together.
9290 * This is useful for detecting pairs of constraints that can
9291 * be chained in their printed representation.
9293 * Finally, within a group, constraints are sorted according to
9294 * their coefficients (excluding the constant term).
9296 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9298 isl_int **c1 = (isl_int **) p1;
9299 isl_int **c2 = (isl_int **) p2;
9300 int l1, l2;
9301 unsigned size = *(unsigned *) arg;
9302 int cmp;
9304 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9305 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9307 if (l1 != l2)
9308 return l1 - l2;
9310 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9311 if (cmp != 0)
9312 return cmp;
9313 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9314 if (cmp != 0)
9315 return -cmp;
9317 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9320 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9321 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9322 * and 0 if the two constraints are the same (up to the constant term).
9324 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9325 isl_int *c1, isl_int *c2)
9327 unsigned total;
9329 if (!bmap)
9330 return -2;
9331 total = isl_basic_map_total_dim(bmap);
9332 return sort_constraint_cmp(&c1, &c2, &total);
9335 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9336 __isl_take isl_basic_map *bmap)
9338 unsigned total;
9340 if (!bmap)
9341 return NULL;
9342 if (bmap->n_ineq == 0)
9343 return bmap;
9344 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9345 return bmap;
9346 total = isl_basic_map_total_dim(bmap);
9347 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9348 &sort_constraint_cmp, &total) < 0)
9349 return isl_basic_map_free(bmap);
9350 return bmap;
9353 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9354 __isl_take isl_basic_set *bset)
9356 isl_basic_map *bmap = bset_to_bmap(bset);
9357 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9360 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9362 if (!bmap)
9363 return NULL;
9364 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9365 return bmap;
9366 bmap = isl_basic_map_remove_redundancies(bmap);
9367 bmap = isl_basic_map_sort_constraints(bmap);
9368 if (bmap)
9369 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9370 return bmap;
9372 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9373 __isl_keep isl_basic_map *bmap2)
9375 int i, cmp;
9376 unsigned total;
9377 isl_space *space1, *space2;
9379 if (!bmap1 || !bmap2)
9380 return -1;
9382 if (bmap1 == bmap2)
9383 return 0;
9384 space1 = isl_basic_map_peek_space(bmap1);
9385 space2 = isl_basic_map_peek_space(bmap2);
9386 cmp = isl_space_cmp(space1, space2);
9387 if (cmp)
9388 return cmp;
9389 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9390 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9391 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9392 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9393 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9394 return 0;
9395 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9396 return 1;
9397 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9398 return -1;
9399 if (bmap1->n_eq != bmap2->n_eq)
9400 return bmap1->n_eq - bmap2->n_eq;
9401 if (bmap1->n_ineq != bmap2->n_ineq)
9402 return bmap1->n_ineq - bmap2->n_ineq;
9403 if (bmap1->n_div != bmap2->n_div)
9404 return bmap1->n_div - bmap2->n_div;
9405 total = isl_basic_map_total_dim(bmap1);
9406 for (i = 0; i < bmap1->n_eq; ++i) {
9407 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9408 if (cmp)
9409 return cmp;
9411 for (i = 0; i < bmap1->n_ineq; ++i) {
9412 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9413 if (cmp)
9414 return cmp;
9416 for (i = 0; i < bmap1->n_div; ++i) {
9417 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9418 if (cmp)
9419 return cmp;
9421 return 0;
9424 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9425 __isl_keep isl_basic_set *bset2)
9427 return isl_basic_map_plain_cmp(bset1, bset2);
9430 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9432 int i, cmp;
9434 if (set1 == set2)
9435 return 0;
9436 if (set1->n != set2->n)
9437 return set1->n - set2->n;
9439 for (i = 0; i < set1->n; ++i) {
9440 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9441 if (cmp)
9442 return cmp;
9445 return 0;
9448 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9449 __isl_keep isl_basic_map *bmap2)
9451 if (!bmap1 || !bmap2)
9452 return isl_bool_error;
9453 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9456 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9457 __isl_keep isl_basic_set *bset2)
9459 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9460 bset_to_bmap(bset2));
9463 static int qsort_bmap_cmp(const void *p1, const void *p2)
9465 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9466 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9468 return isl_basic_map_plain_cmp(bmap1, bmap2);
9471 /* Sort the basic maps of "map" and remove duplicate basic maps.
9473 * While removing basic maps, we make sure that the basic maps remain
9474 * sorted because isl_map_normalize expects the basic maps of the result
9475 * to be sorted.
9477 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9479 int i, j;
9481 map = isl_map_remove_empty_parts(map);
9482 if (!map)
9483 return NULL;
9484 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9485 for (i = map->n - 1; i >= 1; --i) {
9486 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9487 continue;
9488 isl_basic_map_free(map->p[i-1]);
9489 for (j = i; j < map->n; ++j)
9490 map->p[j - 1] = map->p[j];
9491 map->n--;
9494 return map;
9497 /* Remove obvious duplicates among the basic maps of "map".
9499 * Unlike isl_map_normalize, this function does not remove redundant
9500 * constraints and only removes duplicates that have exactly the same
9501 * constraints in the input. It does sort the constraints and
9502 * the basic maps to ease the detection of duplicates.
9504 * If "map" has already been normalized or if the basic maps are
9505 * disjoint, then there can be no duplicates.
9507 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9509 int i;
9510 isl_basic_map *bmap;
9512 if (!map)
9513 return NULL;
9514 if (map->n <= 1)
9515 return map;
9516 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9517 return map;
9518 for (i = 0; i < map->n; ++i) {
9519 bmap = isl_basic_map_copy(map->p[i]);
9520 bmap = isl_basic_map_sort_constraints(bmap);
9521 if (!bmap)
9522 return isl_map_free(map);
9523 isl_basic_map_free(map->p[i]);
9524 map->p[i] = bmap;
9527 map = sort_and_remove_duplicates(map);
9528 return map;
9531 /* We normalize in place, but if anything goes wrong we need
9532 * to return NULL, so we need to make sure we don't change the
9533 * meaning of any possible other copies of map.
9535 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9537 int i;
9538 struct isl_basic_map *bmap;
9540 if (!map)
9541 return NULL;
9542 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9543 return map;
9544 for (i = 0; i < map->n; ++i) {
9545 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9546 if (!bmap)
9547 goto error;
9548 isl_basic_map_free(map->p[i]);
9549 map->p[i] = bmap;
9552 map = sort_and_remove_duplicates(map);
9553 if (map)
9554 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9555 return map;
9556 error:
9557 isl_map_free(map);
9558 return NULL;
9561 struct isl_set *isl_set_normalize(struct isl_set *set)
9563 return set_from_map(isl_map_normalize(set_to_map(set)));
9566 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9567 __isl_keep isl_map *map2)
9569 int i;
9570 isl_bool equal;
9572 if (!map1 || !map2)
9573 return isl_bool_error;
9575 if (map1 == map2)
9576 return isl_bool_true;
9577 if (!isl_space_is_equal(map1->dim, map2->dim))
9578 return isl_bool_false;
9580 map1 = isl_map_copy(map1);
9581 map2 = isl_map_copy(map2);
9582 map1 = isl_map_normalize(map1);
9583 map2 = isl_map_normalize(map2);
9584 if (!map1 || !map2)
9585 goto error;
9586 equal = map1->n == map2->n;
9587 for (i = 0; equal && i < map1->n; ++i) {
9588 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9589 if (equal < 0)
9590 goto error;
9592 isl_map_free(map1);
9593 isl_map_free(map2);
9594 return equal;
9595 error:
9596 isl_map_free(map1);
9597 isl_map_free(map2);
9598 return isl_bool_error;
9601 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9602 __isl_keep isl_set *set2)
9604 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9607 /* Return the basic maps in "map" as a list.
9609 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9610 __isl_keep isl_map *map)
9612 int i;
9613 isl_ctx *ctx;
9614 isl_basic_map_list *list;
9616 if (!map)
9617 return NULL;
9618 ctx = isl_map_get_ctx(map);
9619 list = isl_basic_map_list_alloc(ctx, map->n);
9621 for (i = 0; i < map->n; ++i) {
9622 isl_basic_map *bmap;
9624 bmap = isl_basic_map_copy(map->p[i]);
9625 list = isl_basic_map_list_add(list, bmap);
9628 return list;
9631 /* Return the intersection of the elements in the non-empty list "list".
9632 * All elements are assumed to live in the same space.
9634 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9635 __isl_take isl_basic_map_list *list)
9637 int i, n;
9638 isl_basic_map *bmap;
9640 if (!list)
9641 return NULL;
9642 n = isl_basic_map_list_n_basic_map(list);
9643 if (n < 1)
9644 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9645 "expecting non-empty list", goto error);
9647 bmap = isl_basic_map_list_get_basic_map(list, 0);
9648 for (i = 1; i < n; ++i) {
9649 isl_basic_map *bmap_i;
9651 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9652 bmap = isl_basic_map_intersect(bmap, bmap_i);
9655 isl_basic_map_list_free(list);
9656 return bmap;
9657 error:
9658 isl_basic_map_list_free(list);
9659 return NULL;
9662 /* Return the intersection of the elements in the non-empty list "list".
9663 * All elements are assumed to live in the same space.
9665 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9666 __isl_take isl_basic_set_list *list)
9668 return isl_basic_map_list_intersect(list);
9671 /* Return the union of the elements of "list".
9672 * The list is required to have at least one element.
9674 __isl_give isl_set *isl_basic_set_list_union(
9675 __isl_take isl_basic_set_list *list)
9677 int i, n;
9678 isl_space *space;
9679 isl_basic_set *bset;
9680 isl_set *set;
9682 if (!list)
9683 return NULL;
9684 n = isl_basic_set_list_n_basic_set(list);
9685 if (n < 1)
9686 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9687 "expecting non-empty list", goto error);
9689 bset = isl_basic_set_list_get_basic_set(list, 0);
9690 space = isl_basic_set_get_space(bset);
9691 isl_basic_set_free(bset);
9693 set = isl_set_alloc_space(space, n, 0);
9694 for (i = 0; i < n; ++i) {
9695 bset = isl_basic_set_list_get_basic_set(list, i);
9696 set = isl_set_add_basic_set(set, bset);
9699 isl_basic_set_list_free(list);
9700 return set;
9701 error:
9702 isl_basic_set_list_free(list);
9703 return NULL;
9706 /* Return the union of the elements in the non-empty list "list".
9707 * All elements are assumed to live in the same space.
9709 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9711 int i, n;
9712 isl_set *set;
9714 if (!list)
9715 return NULL;
9716 n = isl_set_list_n_set(list);
9717 if (n < 1)
9718 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9719 "expecting non-empty list", goto error);
9721 set = isl_set_list_get_set(list, 0);
9722 for (i = 1; i < n; ++i) {
9723 isl_set *set_i;
9725 set_i = isl_set_list_get_set(list, i);
9726 set = isl_set_union(set, set_i);
9729 isl_set_list_free(list);
9730 return set;
9731 error:
9732 isl_set_list_free(list);
9733 return NULL;
9736 __isl_give isl_basic_map *isl_basic_map_product(
9737 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9739 isl_space *dim_result = NULL;
9740 struct isl_basic_map *bmap;
9741 unsigned in1, in2, out1, out2, nparam, total, pos;
9742 struct isl_dim_map *dim_map1, *dim_map2;
9744 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9745 goto error;
9746 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9747 isl_space_copy(bmap2->dim));
9749 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9750 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9751 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9752 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9753 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9755 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9756 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9757 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9758 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9759 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9760 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9761 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9762 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9763 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9764 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9765 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9767 bmap = isl_basic_map_alloc_space(dim_result,
9768 bmap1->n_div + bmap2->n_div,
9769 bmap1->n_eq + bmap2->n_eq,
9770 bmap1->n_ineq + bmap2->n_ineq);
9771 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9772 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9773 bmap = isl_basic_map_simplify(bmap);
9774 return isl_basic_map_finalize(bmap);
9775 error:
9776 isl_basic_map_free(bmap1);
9777 isl_basic_map_free(bmap2);
9778 return NULL;
9781 __isl_give isl_basic_map *isl_basic_map_flat_product(
9782 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9784 isl_basic_map *prod;
9786 prod = isl_basic_map_product(bmap1, bmap2);
9787 prod = isl_basic_map_flatten(prod);
9788 return prod;
9791 __isl_give isl_basic_set *isl_basic_set_flat_product(
9792 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9794 return isl_basic_map_flat_range_product(bset1, bset2);
9797 __isl_give isl_basic_map *isl_basic_map_domain_product(
9798 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9800 isl_space *space_result = NULL;
9801 isl_basic_map *bmap;
9802 unsigned in1, in2, out, nparam, total, pos;
9803 struct isl_dim_map *dim_map1, *dim_map2;
9805 if (!bmap1 || !bmap2)
9806 goto error;
9808 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9809 isl_space_copy(bmap2->dim));
9811 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9812 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9813 out = isl_basic_map_dim(bmap1, isl_dim_out);
9814 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9816 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9817 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9818 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9819 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9820 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9821 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9822 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9823 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9824 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9825 isl_dim_map_div(dim_map1, bmap1, pos += out);
9826 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9828 bmap = isl_basic_map_alloc_space(space_result,
9829 bmap1->n_div + bmap2->n_div,
9830 bmap1->n_eq + bmap2->n_eq,
9831 bmap1->n_ineq + bmap2->n_ineq);
9832 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9833 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9834 bmap = isl_basic_map_simplify(bmap);
9835 return isl_basic_map_finalize(bmap);
9836 error:
9837 isl_basic_map_free(bmap1);
9838 isl_basic_map_free(bmap2);
9839 return NULL;
9842 __isl_give isl_basic_map *isl_basic_map_range_product(
9843 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9845 isl_bool rational;
9846 isl_space *dim_result = NULL;
9847 isl_basic_map *bmap;
9848 unsigned in, out1, out2, nparam, total, pos;
9849 struct isl_dim_map *dim_map1, *dim_map2;
9851 rational = isl_basic_map_is_rational(bmap1);
9852 if (rational >= 0 && rational)
9853 rational = isl_basic_map_is_rational(bmap2);
9854 if (!bmap1 || !bmap2 || rational < 0)
9855 goto error;
9857 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9858 goto error;
9860 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9861 isl_space_copy(bmap2->dim));
9863 in = isl_basic_map_dim(bmap1, isl_dim_in);
9864 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9865 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9866 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9868 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9869 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9870 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9871 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9872 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9873 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9874 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9875 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9876 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9877 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9878 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9880 bmap = isl_basic_map_alloc_space(dim_result,
9881 bmap1->n_div + bmap2->n_div,
9882 bmap1->n_eq + bmap2->n_eq,
9883 bmap1->n_ineq + bmap2->n_ineq);
9884 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9885 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9886 if (rational)
9887 bmap = isl_basic_map_set_rational(bmap);
9888 bmap = isl_basic_map_simplify(bmap);
9889 return isl_basic_map_finalize(bmap);
9890 error:
9891 isl_basic_map_free(bmap1);
9892 isl_basic_map_free(bmap2);
9893 return NULL;
9896 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9897 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9899 isl_basic_map *prod;
9901 prod = isl_basic_map_range_product(bmap1, bmap2);
9902 prod = isl_basic_map_flatten_range(prod);
9903 return prod;
9906 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9907 * and collect the results.
9908 * The result live in the space obtained by calling "space_product"
9909 * on the spaces of "map1" and "map2".
9910 * If "remove_duplicates" is set then the result may contain duplicates
9911 * (even if the inputs do not) and so we try and remove the obvious
9912 * duplicates.
9914 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9915 __isl_take isl_map *map2,
9916 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9917 __isl_take isl_space *right),
9918 __isl_give isl_basic_map *(*basic_map_product)(
9919 __isl_take isl_basic_map *left,
9920 __isl_take isl_basic_map *right),
9921 int remove_duplicates)
9923 unsigned flags = 0;
9924 struct isl_map *result;
9925 int i, j;
9926 isl_bool m;
9928 m = isl_map_has_equal_params(map1, map2);
9929 if (m < 0)
9930 goto error;
9931 if (!m)
9932 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
9933 "parameters don't match", goto error);
9935 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9936 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9937 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9939 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9940 isl_space_copy(map2->dim)),
9941 map1->n * map2->n, flags);
9942 if (!result)
9943 goto error;
9944 for (i = 0; i < map1->n; ++i)
9945 for (j = 0; j < map2->n; ++j) {
9946 struct isl_basic_map *part;
9947 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9948 isl_basic_map_copy(map2->p[j]));
9949 if (isl_basic_map_is_empty(part))
9950 isl_basic_map_free(part);
9951 else
9952 result = isl_map_add_basic_map(result, part);
9953 if (!result)
9954 goto error;
9956 if (remove_duplicates)
9957 result = isl_map_remove_obvious_duplicates(result);
9958 isl_map_free(map1);
9959 isl_map_free(map2);
9960 return result;
9961 error:
9962 isl_map_free(map1);
9963 isl_map_free(map2);
9964 return NULL;
9967 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9969 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9970 __isl_take isl_map *map2)
9972 return map_product(map1, map2, &isl_space_product,
9973 &isl_basic_map_product, 0);
9976 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9977 __isl_take isl_map *map2)
9979 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9982 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9984 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9985 __isl_take isl_map *map2)
9987 isl_map *prod;
9989 prod = isl_map_product(map1, map2);
9990 prod = isl_map_flatten(prod);
9991 return prod;
9994 /* Given two set A and B, construct its Cartesian product A x B.
9996 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9998 return isl_map_range_product(set1, set2);
10001 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10002 __isl_take isl_set *set2)
10004 return isl_map_flat_range_product(set1, set2);
10007 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10009 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10010 __isl_take isl_map *map2)
10012 return map_product(map1, map2, &isl_space_domain_product,
10013 &isl_basic_map_domain_product, 1);
10016 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10018 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10019 __isl_take isl_map *map2)
10021 return map_product(map1, map2, &isl_space_range_product,
10022 &isl_basic_map_range_product, 1);
10025 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10026 __isl_take isl_map *map2)
10028 return isl_map_align_params_map_map_and(map1, map2,
10029 &map_domain_product_aligned);
10032 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10033 __isl_take isl_map *map2)
10035 return isl_map_align_params_map_map_and(map1, map2,
10036 &map_range_product_aligned);
10039 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10041 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10043 isl_space *space;
10044 int total1, keep1, total2, keep2;
10046 if (!map)
10047 return NULL;
10048 if (!isl_space_domain_is_wrapping(map->dim) ||
10049 !isl_space_range_is_wrapping(map->dim))
10050 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10051 "not a product", return isl_map_free(map));
10053 space = isl_map_get_space(map);
10054 total1 = isl_space_dim(space, isl_dim_in);
10055 total2 = isl_space_dim(space, isl_dim_out);
10056 space = isl_space_factor_domain(space);
10057 keep1 = isl_space_dim(space, isl_dim_in);
10058 keep2 = isl_space_dim(space, isl_dim_out);
10059 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10060 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10061 map = isl_map_reset_space(map, space);
10063 return map;
10066 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10068 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10070 isl_space *space;
10071 int total1, keep1, total2, keep2;
10073 if (!map)
10074 return NULL;
10075 if (!isl_space_domain_is_wrapping(map->dim) ||
10076 !isl_space_range_is_wrapping(map->dim))
10077 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10078 "not a product", return isl_map_free(map));
10080 space = isl_map_get_space(map);
10081 total1 = isl_space_dim(space, isl_dim_in);
10082 total2 = isl_space_dim(space, isl_dim_out);
10083 space = isl_space_factor_range(space);
10084 keep1 = isl_space_dim(space, isl_dim_in);
10085 keep2 = isl_space_dim(space, isl_dim_out);
10086 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10087 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10088 map = isl_map_reset_space(map, space);
10090 return map;
10093 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10095 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10097 isl_space *space;
10098 int total, keep;
10100 if (!map)
10101 return NULL;
10102 if (!isl_space_domain_is_wrapping(map->dim))
10103 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10104 "domain is not a product", return isl_map_free(map));
10106 space = isl_map_get_space(map);
10107 total = isl_space_dim(space, isl_dim_in);
10108 space = isl_space_domain_factor_domain(space);
10109 keep = isl_space_dim(space, isl_dim_in);
10110 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10111 map = isl_map_reset_space(map, space);
10113 return map;
10116 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10118 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10120 isl_space *space;
10121 int total, keep;
10123 if (!map)
10124 return NULL;
10125 if (!isl_space_domain_is_wrapping(map->dim))
10126 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10127 "domain is not a product", return isl_map_free(map));
10129 space = isl_map_get_space(map);
10130 total = isl_space_dim(space, isl_dim_in);
10131 space = isl_space_domain_factor_range(space);
10132 keep = isl_space_dim(space, isl_dim_in);
10133 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10134 map = isl_map_reset_space(map, space);
10136 return map;
10139 /* Given a map A -> [B -> C], extract the map A -> B.
10141 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10143 isl_space *space;
10144 int total, keep;
10146 if (!map)
10147 return NULL;
10148 if (!isl_space_range_is_wrapping(map->dim))
10149 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10150 "range is not a product", return isl_map_free(map));
10152 space = isl_map_get_space(map);
10153 total = isl_space_dim(space, isl_dim_out);
10154 space = isl_space_range_factor_domain(space);
10155 keep = isl_space_dim(space, isl_dim_out);
10156 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10157 map = isl_map_reset_space(map, space);
10159 return map;
10162 /* Given a map A -> [B -> C], extract the map A -> C.
10164 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10166 isl_space *space;
10167 int total, keep;
10169 if (!map)
10170 return NULL;
10171 if (!isl_space_range_is_wrapping(map->dim))
10172 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10173 "range is not a product", return isl_map_free(map));
10175 space = isl_map_get_space(map);
10176 total = isl_space_dim(space, isl_dim_out);
10177 space = isl_space_range_factor_range(space);
10178 keep = isl_space_dim(space, isl_dim_out);
10179 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10180 map = isl_map_reset_space(map, space);
10182 return map;
10185 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10187 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10188 __isl_take isl_map *map2)
10190 isl_map *prod;
10192 prod = isl_map_domain_product(map1, map2);
10193 prod = isl_map_flatten_domain(prod);
10194 return prod;
10197 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10199 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10200 __isl_take isl_map *map2)
10202 isl_map *prod;
10204 prod = isl_map_range_product(map1, map2);
10205 prod = isl_map_flatten_range(prod);
10206 return prod;
10209 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10211 int i;
10212 uint32_t hash = isl_hash_init();
10213 unsigned total;
10215 if (!bmap)
10216 return 0;
10217 bmap = isl_basic_map_copy(bmap);
10218 bmap = isl_basic_map_normalize(bmap);
10219 if (!bmap)
10220 return 0;
10221 total = isl_basic_map_total_dim(bmap);
10222 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10223 for (i = 0; i < bmap->n_eq; ++i) {
10224 uint32_t c_hash;
10225 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10226 isl_hash_hash(hash, c_hash);
10228 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10229 for (i = 0; i < bmap->n_ineq; ++i) {
10230 uint32_t c_hash;
10231 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10232 isl_hash_hash(hash, c_hash);
10234 isl_hash_byte(hash, bmap->n_div & 0xFF);
10235 for (i = 0; i < bmap->n_div; ++i) {
10236 uint32_t c_hash;
10237 if (isl_int_is_zero(bmap->div[i][0]))
10238 continue;
10239 isl_hash_byte(hash, i & 0xFF);
10240 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10241 isl_hash_hash(hash, c_hash);
10243 isl_basic_map_free(bmap);
10244 return hash;
10247 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10249 return isl_basic_map_get_hash(bset_to_bmap(bset));
10252 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10254 int i;
10255 uint32_t hash;
10257 if (!map)
10258 return 0;
10259 map = isl_map_copy(map);
10260 map = isl_map_normalize(map);
10261 if (!map)
10262 return 0;
10264 hash = isl_hash_init();
10265 for (i = 0; i < map->n; ++i) {
10266 uint32_t bmap_hash;
10267 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10268 isl_hash_hash(hash, bmap_hash);
10271 isl_map_free(map);
10273 return hash;
10276 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10278 return isl_map_get_hash(set_to_map(set));
10281 /* Return the number of basic maps in the (current) representation of "map".
10283 int isl_map_n_basic_map(__isl_keep isl_map *map)
10285 return map ? map->n : 0;
10288 int isl_set_n_basic_set(__isl_keep isl_set *set)
10290 return set ? set->n : 0;
10293 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10294 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10296 int i;
10298 if (!map)
10299 return isl_stat_error;
10301 for (i = 0; i < map->n; ++i)
10302 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10303 return isl_stat_error;
10305 return isl_stat_ok;
10308 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10309 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10311 int i;
10313 if (!set)
10314 return isl_stat_error;
10316 for (i = 0; i < set->n; ++i)
10317 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10318 return isl_stat_error;
10320 return isl_stat_ok;
10323 /* Return a list of basic sets, the union of which is equal to "set".
10325 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10326 __isl_keep isl_set *set)
10328 int i;
10329 isl_basic_set_list *list;
10331 if (!set)
10332 return NULL;
10334 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10335 for (i = 0; i < set->n; ++i) {
10336 isl_basic_set *bset;
10338 bset = isl_basic_set_copy(set->p[i]);
10339 list = isl_basic_set_list_add(list, bset);
10342 return list;
10345 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10347 isl_space *dim;
10349 if (!bset)
10350 return NULL;
10352 bset = isl_basic_set_cow(bset);
10353 if (!bset)
10354 return NULL;
10356 dim = isl_basic_set_get_space(bset);
10357 dim = isl_space_lift(dim, bset->n_div);
10358 if (!dim)
10359 goto error;
10360 isl_space_free(bset->dim);
10361 bset->dim = dim;
10362 bset->extra -= bset->n_div;
10363 bset->n_div = 0;
10365 bset = isl_basic_set_finalize(bset);
10367 return bset;
10368 error:
10369 isl_basic_set_free(bset);
10370 return NULL;
10373 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10375 int i;
10376 isl_space *dim;
10377 unsigned n_div;
10379 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10381 if (!set)
10382 return NULL;
10384 set = isl_set_cow(set);
10385 if (!set)
10386 return NULL;
10388 n_div = set->p[0]->n_div;
10389 dim = isl_set_get_space(set);
10390 dim = isl_space_lift(dim, n_div);
10391 if (!dim)
10392 goto error;
10393 isl_space_free(set->dim);
10394 set->dim = dim;
10396 for (i = 0; i < set->n; ++i) {
10397 set->p[i] = isl_basic_set_lift(set->p[i]);
10398 if (!set->p[i])
10399 goto error;
10402 return set;
10403 error:
10404 isl_set_free(set);
10405 return NULL;
10408 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10410 unsigned dim;
10411 int size = 0;
10413 if (!bset)
10414 return -1;
10416 dim = isl_basic_set_total_dim(bset);
10417 size += bset->n_eq * (1 + dim);
10418 size += bset->n_ineq * (1 + dim);
10419 size += bset->n_div * (2 + dim);
10421 return size;
10424 int isl_set_size(__isl_keep isl_set *set)
10426 int i;
10427 int size = 0;
10429 if (!set)
10430 return -1;
10432 for (i = 0; i < set->n; ++i)
10433 size += isl_basic_set_size(set->p[i]);
10435 return size;
10438 /* Check if there is any lower bound (if lower == 0) and/or upper
10439 * bound (if upper == 0) on the specified dim.
10441 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10442 enum isl_dim_type type, unsigned pos, int lower, int upper)
10444 int i;
10446 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10447 return isl_bool_error;
10449 pos += isl_basic_map_offset(bmap, type);
10451 for (i = 0; i < bmap->n_div; ++i) {
10452 if (isl_int_is_zero(bmap->div[i][0]))
10453 continue;
10454 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10455 return isl_bool_true;
10458 for (i = 0; i < bmap->n_eq; ++i)
10459 if (!isl_int_is_zero(bmap->eq[i][pos]))
10460 return isl_bool_true;
10462 for (i = 0; i < bmap->n_ineq; ++i) {
10463 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10464 if (sgn > 0)
10465 lower = 1;
10466 if (sgn < 0)
10467 upper = 1;
10470 return lower && upper;
10473 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10474 enum isl_dim_type type, unsigned pos)
10476 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10479 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10480 enum isl_dim_type type, unsigned pos)
10482 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10485 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10486 enum isl_dim_type type, unsigned pos)
10488 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10491 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10492 enum isl_dim_type type, unsigned pos)
10494 int i;
10496 if (!map)
10497 return isl_bool_error;
10499 for (i = 0; i < map->n; ++i) {
10500 isl_bool bounded;
10501 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10502 if (bounded < 0 || !bounded)
10503 return bounded;
10506 return isl_bool_true;
10509 /* Return true if the specified dim is involved in both an upper bound
10510 * and a lower bound.
10512 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10513 enum isl_dim_type type, unsigned pos)
10515 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10518 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10520 static isl_bool has_any_bound(__isl_keep isl_map *map,
10521 enum isl_dim_type type, unsigned pos,
10522 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10523 enum isl_dim_type type, unsigned pos))
10525 int i;
10527 if (!map)
10528 return isl_bool_error;
10530 for (i = 0; i < map->n; ++i) {
10531 isl_bool bounded;
10532 bounded = fn(map->p[i], type, pos);
10533 if (bounded < 0 || bounded)
10534 return bounded;
10537 return isl_bool_false;
10540 /* Return 1 if the specified dim is involved in any lower bound.
10542 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10543 enum isl_dim_type type, unsigned pos)
10545 return has_any_bound(set, type, pos,
10546 &isl_basic_map_dim_has_lower_bound);
10549 /* Return 1 if the specified dim is involved in any upper bound.
10551 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10552 enum isl_dim_type type, unsigned pos)
10554 return has_any_bound(set, type, pos,
10555 &isl_basic_map_dim_has_upper_bound);
10558 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10560 static isl_bool has_bound(__isl_keep isl_map *map,
10561 enum isl_dim_type type, unsigned pos,
10562 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10563 enum isl_dim_type type, unsigned pos))
10565 int i;
10567 if (!map)
10568 return isl_bool_error;
10570 for (i = 0; i < map->n; ++i) {
10571 isl_bool bounded;
10572 bounded = fn(map->p[i], type, pos);
10573 if (bounded < 0 || !bounded)
10574 return bounded;
10577 return isl_bool_true;
10580 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10582 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10583 enum isl_dim_type type, unsigned pos)
10585 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10588 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10590 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10591 enum isl_dim_type type, unsigned pos)
10593 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10596 /* For each of the "n" variables starting at "first", determine
10597 * the sign of the variable and put the results in the first "n"
10598 * elements of the array "signs".
10599 * Sign
10600 * 1 means that the variable is non-negative
10601 * -1 means that the variable is non-positive
10602 * 0 means the variable attains both positive and negative values.
10604 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10605 unsigned first, unsigned n, int *signs)
10607 isl_vec *bound = NULL;
10608 struct isl_tab *tab = NULL;
10609 struct isl_tab_undo *snap;
10610 int i;
10612 if (!bset || !signs)
10613 return isl_stat_error;
10615 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10616 tab = isl_tab_from_basic_set(bset, 0);
10617 if (!bound || !tab)
10618 goto error;
10620 isl_seq_clr(bound->el, bound->size);
10621 isl_int_set_si(bound->el[0], -1);
10623 snap = isl_tab_snap(tab);
10624 for (i = 0; i < n; ++i) {
10625 int empty;
10627 isl_int_set_si(bound->el[1 + first + i], -1);
10628 if (isl_tab_add_ineq(tab, bound->el) < 0)
10629 goto error;
10630 empty = tab->empty;
10631 isl_int_set_si(bound->el[1 + first + i], 0);
10632 if (isl_tab_rollback(tab, snap) < 0)
10633 goto error;
10635 if (empty) {
10636 signs[i] = 1;
10637 continue;
10640 isl_int_set_si(bound->el[1 + first + i], 1);
10641 if (isl_tab_add_ineq(tab, bound->el) < 0)
10642 goto error;
10643 empty = tab->empty;
10644 isl_int_set_si(bound->el[1 + first + i], 0);
10645 if (isl_tab_rollback(tab, snap) < 0)
10646 goto error;
10648 signs[i] = empty ? -1 : 0;
10651 isl_tab_free(tab);
10652 isl_vec_free(bound);
10653 return isl_stat_ok;
10654 error:
10655 isl_tab_free(tab);
10656 isl_vec_free(bound);
10657 return isl_stat_error;
10660 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10661 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10663 if (!bset || !signs)
10664 return isl_stat_error;
10665 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10666 return isl_stat_error);
10668 first += pos(bset->dim, type) - 1;
10669 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10672 /* Is it possible for the integer division "div" to depend (possibly
10673 * indirectly) on any output dimensions?
10675 * If the div is undefined, then we conservatively assume that it
10676 * may depend on them.
10677 * Otherwise, we check if it actually depends on them or on any integer
10678 * divisions that may depend on them.
10680 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10682 int i;
10683 unsigned n_out, o_out;
10684 unsigned n_div, o_div;
10686 if (isl_int_is_zero(bmap->div[div][0]))
10687 return isl_bool_true;
10689 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10690 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10692 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10693 return isl_bool_true;
10695 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10696 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10698 for (i = 0; i < n_div; ++i) {
10699 isl_bool may_involve;
10701 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10702 continue;
10703 may_involve = div_may_involve_output(bmap, i);
10704 if (may_involve < 0 || may_involve)
10705 return may_involve;
10708 return isl_bool_false;
10711 /* Return the first integer division of "bmap" in the range
10712 * [first, first + n[ that may depend on any output dimensions and
10713 * that has a non-zero coefficient in "c" (where the first coefficient
10714 * in "c" corresponds to integer division "first").
10716 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10717 isl_int *c, int first, int n)
10719 int k;
10721 if (!bmap)
10722 return -1;
10724 for (k = first; k < first + n; ++k) {
10725 isl_bool may_involve;
10727 if (isl_int_is_zero(c[k]))
10728 continue;
10729 may_involve = div_may_involve_output(bmap, k);
10730 if (may_involve < 0)
10731 return -1;
10732 if (may_involve)
10733 return k;
10736 return first + n;
10739 /* Look for a pair of inequality constraints in "bmap" of the form
10741 * -l + i >= 0 or i >= l
10742 * and
10743 * n + l - i >= 0 or i <= l + n
10745 * with n < "m" and i the output dimension at position "pos".
10746 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10747 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10748 * and earlier output dimensions, as well as integer divisions that do
10749 * not involve any of the output dimensions.
10751 * Return the index of the first inequality constraint or bmap->n_ineq
10752 * if no such pair can be found.
10754 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10755 int pos, isl_int m)
10757 int i, j;
10758 isl_ctx *ctx;
10759 unsigned total;
10760 unsigned n_div, o_div;
10761 unsigned n_out, o_out;
10762 int less;
10764 if (!bmap)
10765 return -1;
10767 ctx = isl_basic_map_get_ctx(bmap);
10768 total = isl_basic_map_total_dim(bmap);
10769 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10770 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10771 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10772 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10773 for (i = 0; i < bmap->n_ineq; ++i) {
10774 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10775 continue;
10776 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10777 n_out - (pos + 1)) != -1)
10778 continue;
10779 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10780 0, n_div) < n_div)
10781 continue;
10782 for (j = i + 1; j < bmap->n_ineq; ++j) {
10783 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10784 ctx->one))
10785 continue;
10786 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10787 bmap->ineq[j] + 1, total))
10788 continue;
10789 break;
10791 if (j >= bmap->n_ineq)
10792 continue;
10793 isl_int_add(bmap->ineq[i][0],
10794 bmap->ineq[i][0], bmap->ineq[j][0]);
10795 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10796 isl_int_sub(bmap->ineq[i][0],
10797 bmap->ineq[i][0], bmap->ineq[j][0]);
10798 if (!less)
10799 continue;
10800 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10801 return i;
10802 else
10803 return j;
10806 return bmap->n_ineq;
10809 /* Return the index of the equality of "bmap" that defines
10810 * the output dimension "pos" in terms of earlier dimensions.
10811 * The equality may also involve integer divisions, as long
10812 * as those integer divisions are defined in terms of
10813 * parameters or input dimensions.
10814 * In this case, *div is set to the number of integer divisions and
10815 * *ineq is set to the number of inequality constraints (provided
10816 * div and ineq are not NULL).
10818 * The equality may also involve a single integer division involving
10819 * the output dimensions (typically only output dimension "pos") as
10820 * long as the coefficient of output dimension "pos" is 1 or -1 and
10821 * there is a pair of constraints i >= l and i <= l + n, with i referring
10822 * to output dimension "pos", l an expression involving only earlier
10823 * dimensions and n smaller than the coefficient of the integer division
10824 * in the equality. In this case, the output dimension can be defined
10825 * in terms of a modulo expression that does not involve the integer division.
10826 * *div is then set to this single integer division and
10827 * *ineq is set to the index of constraint i >= l.
10829 * Return bmap->n_eq if there is no such equality.
10830 * Return -1 on error.
10832 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10833 int pos, int *div, int *ineq)
10835 int j, k, l;
10836 unsigned n_out, o_out;
10837 unsigned n_div, o_div;
10839 if (!bmap)
10840 return -1;
10842 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10843 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10844 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10845 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10847 if (ineq)
10848 *ineq = bmap->n_ineq;
10849 if (div)
10850 *div = n_div;
10851 for (j = 0; j < bmap->n_eq; ++j) {
10852 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10853 continue;
10854 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10855 n_out - (pos + 1)) != -1)
10856 continue;
10857 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10858 0, n_div);
10859 if (k >= n_div)
10860 return j;
10861 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10862 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10863 continue;
10864 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10865 k + 1, n_div - (k+1)) < n_div)
10866 continue;
10867 l = find_modulo_constraint_pair(bmap, pos,
10868 bmap->eq[j][o_div + k]);
10869 if (l < 0)
10870 return -1;
10871 if (l >= bmap->n_ineq)
10872 continue;
10873 if (div)
10874 *div = k;
10875 if (ineq)
10876 *ineq = l;
10877 return j;
10880 return bmap->n_eq;
10883 /* Check if the given basic map is obviously single-valued.
10884 * In particular, for each output dimension, check that there is
10885 * an equality that defines the output dimension in terms of
10886 * earlier dimensions.
10888 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10890 int i;
10891 unsigned n_out;
10893 if (!bmap)
10894 return isl_bool_error;
10896 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10898 for (i = 0; i < n_out; ++i) {
10899 int eq;
10901 eq = isl_basic_map_output_defining_equality(bmap, i,
10902 NULL, NULL);
10903 if (eq < 0)
10904 return isl_bool_error;
10905 if (eq >= bmap->n_eq)
10906 return isl_bool_false;
10909 return isl_bool_true;
10912 /* Check if the given basic map is single-valued.
10913 * We simply compute
10915 * M \circ M^-1
10917 * and check if the result is a subset of the identity mapping.
10919 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10921 isl_space *space;
10922 isl_basic_map *test;
10923 isl_basic_map *id;
10924 isl_bool sv;
10926 sv = isl_basic_map_plain_is_single_valued(bmap);
10927 if (sv < 0 || sv)
10928 return sv;
10930 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10931 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10933 space = isl_basic_map_get_space(bmap);
10934 space = isl_space_map_from_set(isl_space_range(space));
10935 id = isl_basic_map_identity(space);
10937 sv = isl_basic_map_is_subset(test, id);
10939 isl_basic_map_free(test);
10940 isl_basic_map_free(id);
10942 return sv;
10945 /* Check if the given map is obviously single-valued.
10947 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10949 if (!map)
10950 return isl_bool_error;
10951 if (map->n == 0)
10952 return isl_bool_true;
10953 if (map->n >= 2)
10954 return isl_bool_false;
10956 return isl_basic_map_plain_is_single_valued(map->p[0]);
10959 /* Check if the given map is single-valued.
10960 * We simply compute
10962 * M \circ M^-1
10964 * and check if the result is a subset of the identity mapping.
10966 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10968 isl_space *dim;
10969 isl_map *test;
10970 isl_map *id;
10971 isl_bool sv;
10973 sv = isl_map_plain_is_single_valued(map);
10974 if (sv < 0 || sv)
10975 return sv;
10977 test = isl_map_reverse(isl_map_copy(map));
10978 test = isl_map_apply_range(test, isl_map_copy(map));
10980 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10981 id = isl_map_identity(dim);
10983 sv = isl_map_is_subset(test, id);
10985 isl_map_free(test);
10986 isl_map_free(id);
10988 return sv;
10991 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10993 isl_bool in;
10995 map = isl_map_copy(map);
10996 map = isl_map_reverse(map);
10997 in = isl_map_is_single_valued(map);
10998 isl_map_free(map);
11000 return in;
11003 /* Check if the given map is obviously injective.
11005 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11007 isl_bool in;
11009 map = isl_map_copy(map);
11010 map = isl_map_reverse(map);
11011 in = isl_map_plain_is_single_valued(map);
11012 isl_map_free(map);
11014 return in;
11017 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11019 isl_bool sv;
11021 sv = isl_map_is_single_valued(map);
11022 if (sv < 0 || !sv)
11023 return sv;
11025 return isl_map_is_injective(map);
11028 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11030 return isl_map_is_single_valued(set_to_map(set));
11033 /* Does "map" only map elements to themselves?
11035 * If the domain and range spaces are different, then "map"
11036 * is considered not to be an identity relation, even if it is empty.
11037 * Otherwise, construct the maximal identity relation and
11038 * check whether "map" is a subset of this relation.
11040 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11042 isl_space *space;
11043 isl_map *id;
11044 isl_bool equal, is_identity;
11046 space = isl_map_get_space(map);
11047 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11048 isl_space_free(space);
11049 if (equal < 0 || !equal)
11050 return equal;
11052 id = isl_map_identity(isl_map_get_space(map));
11053 is_identity = isl_map_is_subset(map, id);
11054 isl_map_free(id);
11056 return is_identity;
11059 int isl_map_is_translation(__isl_keep isl_map *map)
11061 int ok;
11062 isl_set *delta;
11064 delta = isl_map_deltas(isl_map_copy(map));
11065 ok = isl_set_is_singleton(delta);
11066 isl_set_free(delta);
11068 return ok;
11071 static int unique(isl_int *p, unsigned pos, unsigned len)
11073 if (isl_seq_first_non_zero(p, pos) != -1)
11074 return 0;
11075 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11076 return 0;
11077 return 1;
11080 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11082 int i, j;
11083 unsigned nvar;
11084 unsigned ovar;
11086 if (!bset)
11087 return isl_bool_error;
11089 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11090 return isl_bool_false;
11092 nvar = isl_basic_set_dim(bset, isl_dim_set);
11093 ovar = isl_space_offset(bset->dim, isl_dim_set);
11094 for (j = 0; j < nvar; ++j) {
11095 int lower = 0, upper = 0;
11096 for (i = 0; i < bset->n_eq; ++i) {
11097 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11098 continue;
11099 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11100 return isl_bool_false;
11101 break;
11103 if (i < bset->n_eq)
11104 continue;
11105 for (i = 0; i < bset->n_ineq; ++i) {
11106 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11107 continue;
11108 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11109 return isl_bool_false;
11110 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11111 lower = 1;
11112 else
11113 upper = 1;
11115 if (!lower || !upper)
11116 return isl_bool_false;
11119 return isl_bool_true;
11122 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11124 if (!set)
11125 return isl_bool_error;
11126 if (set->n != 1)
11127 return isl_bool_false;
11129 return isl_basic_set_is_box(set->p[0]);
11132 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11134 if (!bset)
11135 return isl_bool_error;
11137 return isl_space_is_wrapping(bset->dim);
11140 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11142 if (!set)
11143 return isl_bool_error;
11145 return isl_space_is_wrapping(set->dim);
11148 /* Modify the space of "map" through a call to "change".
11149 * If "can_change" is set (not NULL), then first call it to check
11150 * if the modification is allowed, printing the error message "cannot_change"
11151 * if it is not.
11153 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11154 isl_bool (*can_change)(__isl_keep isl_map *map),
11155 const char *cannot_change,
11156 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11158 isl_bool ok;
11159 isl_space *space;
11161 if (!map)
11162 return NULL;
11164 ok = can_change ? can_change(map) : isl_bool_true;
11165 if (ok < 0)
11166 return isl_map_free(map);
11167 if (!ok)
11168 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11169 return isl_map_free(map));
11171 space = change(isl_map_get_space(map));
11172 map = isl_map_reset_space(map, space);
11174 return map;
11177 /* Is the domain of "map" a wrapped relation?
11179 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11181 if (!map)
11182 return isl_bool_error;
11184 return isl_space_domain_is_wrapping(map->dim);
11187 /* Is the range of "map" a wrapped relation?
11189 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11191 if (!map)
11192 return isl_bool_error;
11194 return isl_space_range_is_wrapping(map->dim);
11197 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11199 bmap = isl_basic_map_cow(bmap);
11200 if (!bmap)
11201 return NULL;
11203 bmap->dim = isl_space_wrap(bmap->dim);
11204 if (!bmap->dim)
11205 goto error;
11207 bmap = isl_basic_map_finalize(bmap);
11209 return bset_from_bmap(bmap);
11210 error:
11211 isl_basic_map_free(bmap);
11212 return NULL;
11215 /* Given a map A -> B, return the set (A -> B).
11217 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11219 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11222 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11224 bset = isl_basic_set_cow(bset);
11225 if (!bset)
11226 return NULL;
11228 bset->dim = isl_space_unwrap(bset->dim);
11229 if (!bset->dim)
11230 goto error;
11232 bset = isl_basic_set_finalize(bset);
11234 return bset_to_bmap(bset);
11235 error:
11236 isl_basic_set_free(bset);
11237 return NULL;
11240 /* Given a set (A -> B), return the map A -> B.
11241 * Error out if "set" is not of the form (A -> B).
11243 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11245 return isl_map_change_space(set, &isl_set_is_wrapping,
11246 "not a wrapping set", &isl_space_unwrap);
11249 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11250 enum isl_dim_type type)
11252 if (!bmap)
11253 return NULL;
11255 if (!isl_space_is_named_or_nested(bmap->dim, type))
11256 return bmap;
11258 bmap = isl_basic_map_cow(bmap);
11259 if (!bmap)
11260 return NULL;
11262 bmap->dim = isl_space_reset(bmap->dim, type);
11263 if (!bmap->dim)
11264 goto error;
11266 bmap = isl_basic_map_finalize(bmap);
11268 return bmap;
11269 error:
11270 isl_basic_map_free(bmap);
11271 return NULL;
11274 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11275 enum isl_dim_type type)
11277 int i;
11279 if (!map)
11280 return NULL;
11282 if (!isl_space_is_named_or_nested(map->dim, type))
11283 return map;
11285 map = isl_map_cow(map);
11286 if (!map)
11287 return NULL;
11289 for (i = 0; i < map->n; ++i) {
11290 map->p[i] = isl_basic_map_reset(map->p[i], type);
11291 if (!map->p[i])
11292 goto error;
11294 map->dim = isl_space_reset(map->dim, type);
11295 if (!map->dim)
11296 goto error;
11298 return map;
11299 error:
11300 isl_map_free(map);
11301 return NULL;
11304 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11306 if (!bmap)
11307 return NULL;
11309 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11310 return bmap;
11312 bmap = isl_basic_map_cow(bmap);
11313 if (!bmap)
11314 return NULL;
11316 bmap->dim = isl_space_flatten(bmap->dim);
11317 if (!bmap->dim)
11318 goto error;
11320 bmap = isl_basic_map_finalize(bmap);
11322 return bmap;
11323 error:
11324 isl_basic_map_free(bmap);
11325 return NULL;
11328 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11330 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11333 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11334 __isl_take isl_basic_map *bmap)
11336 if (!bmap)
11337 return NULL;
11339 if (!bmap->dim->nested[0])
11340 return bmap;
11342 bmap = isl_basic_map_cow(bmap);
11343 if (!bmap)
11344 return NULL;
11346 bmap->dim = isl_space_flatten_domain(bmap->dim);
11347 if (!bmap->dim)
11348 goto error;
11350 bmap = isl_basic_map_finalize(bmap);
11352 return bmap;
11353 error:
11354 isl_basic_map_free(bmap);
11355 return NULL;
11358 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11359 __isl_take isl_basic_map *bmap)
11361 if (!bmap)
11362 return NULL;
11364 if (!bmap->dim->nested[1])
11365 return bmap;
11367 bmap = isl_basic_map_cow(bmap);
11368 if (!bmap)
11369 return NULL;
11371 bmap->dim = isl_space_flatten_range(bmap->dim);
11372 if (!bmap->dim)
11373 goto error;
11375 bmap = isl_basic_map_finalize(bmap);
11377 return bmap;
11378 error:
11379 isl_basic_map_free(bmap);
11380 return NULL;
11383 /* Remove any internal structure from the spaces of domain and range of "map".
11385 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11387 if (!map)
11388 return NULL;
11390 if (!map->dim->nested[0] && !map->dim->nested[1])
11391 return map;
11393 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11396 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11398 return set_from_map(isl_map_flatten(set_to_map(set)));
11401 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11403 isl_space *dim, *flat_dim;
11404 isl_map *map;
11406 dim = isl_set_get_space(set);
11407 flat_dim = isl_space_flatten(isl_space_copy(dim));
11408 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11409 map = isl_map_intersect_domain(map, set);
11411 return map;
11414 /* Remove any internal structure from the space of the domain of "map".
11416 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11418 if (!map)
11419 return NULL;
11421 if (!map->dim->nested[0])
11422 return map;
11424 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11427 /* Remove any internal structure from the space of the range of "map".
11429 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11431 if (!map)
11432 return NULL;
11434 if (!map->dim->nested[1])
11435 return map;
11437 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11440 /* Reorder the dimensions of "bmap" according to the given dim_map
11441 * and set the dimension specification to "dim" and
11442 * perform Gaussian elimination on the result.
11444 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11445 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11447 isl_basic_map *res;
11448 unsigned flags;
11450 bmap = isl_basic_map_cow(bmap);
11451 if (!bmap || !dim || !dim_map)
11452 goto error;
11454 flags = bmap->flags;
11455 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11456 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11457 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11458 res = isl_basic_map_alloc_space(dim,
11459 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11460 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11461 if (res)
11462 res->flags = flags;
11463 res = isl_basic_map_gauss(res, NULL);
11464 res = isl_basic_map_finalize(res);
11465 return res;
11466 error:
11467 free(dim_map);
11468 isl_basic_map_free(bmap);
11469 isl_space_free(dim);
11470 return NULL;
11473 /* Reorder the dimensions of "map" according to given reordering.
11475 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11476 __isl_take isl_reordering *r)
11478 int i;
11479 struct isl_dim_map *dim_map;
11481 map = isl_map_cow(map);
11482 dim_map = isl_dim_map_from_reordering(r);
11483 if (!map || !r || !dim_map)
11484 goto error;
11486 for (i = 0; i < map->n; ++i) {
11487 struct isl_dim_map *dim_map_i;
11489 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11491 map->p[i] = isl_basic_map_realign(map->p[i],
11492 isl_space_copy(r->dim), dim_map_i);
11494 if (!map->p[i])
11495 goto error;
11498 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11500 isl_reordering_free(r);
11501 free(dim_map);
11502 return map;
11503 error:
11504 free(dim_map);
11505 isl_map_free(map);
11506 isl_reordering_free(r);
11507 return NULL;
11510 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11511 __isl_take isl_reordering *r)
11513 return set_from_map(isl_map_realign(set_to_map(set), r));
11516 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11517 __isl_take isl_space *model)
11519 isl_ctx *ctx;
11520 isl_bool aligned;
11522 if (!map || !model)
11523 goto error;
11525 ctx = isl_space_get_ctx(model);
11526 if (!isl_space_has_named_params(model))
11527 isl_die(ctx, isl_error_invalid,
11528 "model has unnamed parameters", goto error);
11529 if (isl_map_check_named_params(map) < 0)
11530 goto error;
11531 aligned = isl_map_space_has_equal_params(map, model);
11532 if (aligned < 0)
11533 goto error;
11534 if (!aligned) {
11535 isl_reordering *exp;
11537 model = isl_space_drop_dims(model, isl_dim_in,
11538 0, isl_space_dim(model, isl_dim_in));
11539 model = isl_space_drop_dims(model, isl_dim_out,
11540 0, isl_space_dim(model, isl_dim_out));
11541 exp = isl_parameter_alignment_reordering(map->dim, model);
11542 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11543 map = isl_map_realign(map, exp);
11546 isl_space_free(model);
11547 return map;
11548 error:
11549 isl_space_free(model);
11550 isl_map_free(map);
11551 return NULL;
11554 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11555 __isl_take isl_space *model)
11557 return isl_map_align_params(set, model);
11560 /* Align the parameters of "bmap" to those of "model", introducing
11561 * additional parameters if needed.
11563 __isl_give isl_basic_map *isl_basic_map_align_params(
11564 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11566 isl_ctx *ctx;
11567 isl_bool equal_params;
11569 if (!bmap || !model)
11570 goto error;
11572 ctx = isl_space_get_ctx(model);
11573 if (!isl_space_has_named_params(model))
11574 isl_die(ctx, isl_error_invalid,
11575 "model has unnamed parameters", goto error);
11576 if (!isl_space_has_named_params(bmap->dim))
11577 isl_die(ctx, isl_error_invalid,
11578 "relation has unnamed parameters", goto error);
11579 equal_params = isl_space_has_equal_params(bmap->dim, model);
11580 if (equal_params < 0)
11581 goto error;
11582 if (!equal_params) {
11583 isl_reordering *exp;
11584 struct isl_dim_map *dim_map;
11586 model = isl_space_drop_dims(model, isl_dim_in,
11587 0, isl_space_dim(model, isl_dim_in));
11588 model = isl_space_drop_dims(model, isl_dim_out,
11589 0, isl_space_dim(model, isl_dim_out));
11590 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11591 exp = isl_reordering_extend_space(exp,
11592 isl_basic_map_get_space(bmap));
11593 dim_map = isl_dim_map_from_reordering(exp);
11594 bmap = isl_basic_map_realign(bmap,
11595 exp ? isl_space_copy(exp->dim) : NULL,
11596 isl_dim_map_extend(dim_map, bmap));
11597 isl_reordering_free(exp);
11598 free(dim_map);
11601 isl_space_free(model);
11602 return bmap;
11603 error:
11604 isl_space_free(model);
11605 isl_basic_map_free(bmap);
11606 return NULL;
11609 /* Do "bset" and "space" have the same parameters?
11611 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11612 __isl_keep isl_space *space)
11614 isl_space *bset_space;
11616 bset_space = isl_basic_set_peek_space(bset);
11617 return isl_space_has_equal_params(bset_space, space);
11620 /* Do "map" and "space" have the same parameters?
11622 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11623 __isl_keep isl_space *space)
11625 isl_space *map_space;
11627 map_space = isl_map_peek_space(map);
11628 return isl_space_has_equal_params(map_space, space);
11631 /* Do "set" and "space" have the same parameters?
11633 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11634 __isl_keep isl_space *space)
11636 return isl_map_space_has_equal_params(set_to_map(set), space);
11639 /* Align the parameters of "bset" to those of "model", introducing
11640 * additional parameters if needed.
11642 __isl_give isl_basic_set *isl_basic_set_align_params(
11643 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11645 return isl_basic_map_align_params(bset, model);
11648 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11649 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11650 enum isl_dim_type c2, enum isl_dim_type c3,
11651 enum isl_dim_type c4, enum isl_dim_type c5)
11653 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11654 struct isl_mat *mat;
11655 int i, j, k;
11656 int pos;
11658 if (!bmap)
11659 return NULL;
11660 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11661 isl_basic_map_total_dim(bmap) + 1);
11662 if (!mat)
11663 return NULL;
11664 for (i = 0; i < bmap->n_eq; ++i)
11665 for (j = 0, pos = 0; j < 5; ++j) {
11666 int off = isl_basic_map_offset(bmap, c[j]);
11667 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11668 isl_int_set(mat->row[i][pos],
11669 bmap->eq[i][off + k]);
11670 ++pos;
11674 return mat;
11677 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11678 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11679 enum isl_dim_type c2, enum isl_dim_type c3,
11680 enum isl_dim_type c4, enum isl_dim_type c5)
11682 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11683 struct isl_mat *mat;
11684 int i, j, k;
11685 int pos;
11687 if (!bmap)
11688 return NULL;
11689 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11690 isl_basic_map_total_dim(bmap) + 1);
11691 if (!mat)
11692 return NULL;
11693 for (i = 0; i < bmap->n_ineq; ++i)
11694 for (j = 0, pos = 0; j < 5; ++j) {
11695 int off = isl_basic_map_offset(bmap, c[j]);
11696 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11697 isl_int_set(mat->row[i][pos],
11698 bmap->ineq[i][off + k]);
11699 ++pos;
11703 return mat;
11706 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11707 __isl_take isl_space *dim,
11708 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11709 enum isl_dim_type c2, enum isl_dim_type c3,
11710 enum isl_dim_type c4, enum isl_dim_type c5)
11712 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11713 isl_basic_map *bmap;
11714 unsigned total;
11715 unsigned extra;
11716 int i, j, k, l;
11717 int pos;
11719 if (!dim || !eq || !ineq)
11720 goto error;
11722 if (eq->n_col != ineq->n_col)
11723 isl_die(dim->ctx, isl_error_invalid,
11724 "equalities and inequalities matrices should have "
11725 "same number of columns", goto error);
11727 total = 1 + isl_space_dim(dim, isl_dim_all);
11729 if (eq->n_col < total)
11730 isl_die(dim->ctx, isl_error_invalid,
11731 "number of columns too small", goto error);
11733 extra = eq->n_col - total;
11735 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11736 eq->n_row, ineq->n_row);
11737 if (!bmap)
11738 goto error;
11739 for (i = 0; i < extra; ++i) {
11740 k = isl_basic_map_alloc_div(bmap);
11741 if (k < 0)
11742 goto error;
11743 isl_int_set_si(bmap->div[k][0], 0);
11745 for (i = 0; i < eq->n_row; ++i) {
11746 l = isl_basic_map_alloc_equality(bmap);
11747 if (l < 0)
11748 goto error;
11749 for (j = 0, pos = 0; j < 5; ++j) {
11750 int off = isl_basic_map_offset(bmap, c[j]);
11751 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11752 isl_int_set(bmap->eq[l][off + k],
11753 eq->row[i][pos]);
11754 ++pos;
11758 for (i = 0; i < ineq->n_row; ++i) {
11759 l = isl_basic_map_alloc_inequality(bmap);
11760 if (l < 0)
11761 goto error;
11762 for (j = 0, pos = 0; j < 5; ++j) {
11763 int off = isl_basic_map_offset(bmap, c[j]);
11764 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11765 isl_int_set(bmap->ineq[l][off + k],
11766 ineq->row[i][pos]);
11767 ++pos;
11772 isl_space_free(dim);
11773 isl_mat_free(eq);
11774 isl_mat_free(ineq);
11776 bmap = isl_basic_map_simplify(bmap);
11777 return isl_basic_map_finalize(bmap);
11778 error:
11779 isl_space_free(dim);
11780 isl_mat_free(eq);
11781 isl_mat_free(ineq);
11782 return NULL;
11785 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11786 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11787 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11789 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11790 c1, c2, c3, c4, isl_dim_in);
11793 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11794 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11795 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11797 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11798 c1, c2, c3, c4, isl_dim_in);
11801 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11802 __isl_take isl_space *dim,
11803 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11804 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11806 isl_basic_map *bmap;
11807 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11808 c1, c2, c3, c4, isl_dim_in);
11809 return bset_from_bmap(bmap);
11812 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11814 if (!bmap)
11815 return isl_bool_error;
11817 return isl_space_can_zip(bmap->dim);
11820 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11822 if (!map)
11823 return isl_bool_error;
11825 return isl_space_can_zip(map->dim);
11828 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11829 * (A -> C) -> (B -> D).
11831 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11833 unsigned pos;
11834 unsigned n1;
11835 unsigned n2;
11837 if (!bmap)
11838 return NULL;
11840 if (!isl_basic_map_can_zip(bmap))
11841 isl_die(bmap->ctx, isl_error_invalid,
11842 "basic map cannot be zipped", goto error);
11843 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11844 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11845 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11846 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11847 bmap = isl_basic_map_cow(bmap);
11848 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11849 if (!bmap)
11850 return NULL;
11851 bmap->dim = isl_space_zip(bmap->dim);
11852 if (!bmap->dim)
11853 goto error;
11854 bmap = isl_basic_map_mark_final(bmap);
11855 return bmap;
11856 error:
11857 isl_basic_map_free(bmap);
11858 return NULL;
11861 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11862 * (A -> C) -> (B -> D).
11864 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11866 int i;
11868 if (!map)
11869 return NULL;
11871 if (!isl_map_can_zip(map))
11872 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11873 goto error);
11875 map = isl_map_cow(map);
11876 if (!map)
11877 return NULL;
11879 for (i = 0; i < map->n; ++i) {
11880 map->p[i] = isl_basic_map_zip(map->p[i]);
11881 if (!map->p[i])
11882 goto error;
11885 map->dim = isl_space_zip(map->dim);
11886 if (!map->dim)
11887 goto error;
11889 return map;
11890 error:
11891 isl_map_free(map);
11892 return NULL;
11895 /* Can we apply isl_basic_map_curry to "bmap"?
11896 * That is, does it have a nested relation in its domain?
11898 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11900 if (!bmap)
11901 return isl_bool_error;
11903 return isl_space_can_curry(bmap->dim);
11906 /* Can we apply isl_map_curry to "map"?
11907 * That is, does it have a nested relation in its domain?
11909 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11911 if (!map)
11912 return isl_bool_error;
11914 return isl_space_can_curry(map->dim);
11917 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11918 * A -> (B -> C).
11920 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11923 if (!bmap)
11924 return NULL;
11926 if (!isl_basic_map_can_curry(bmap))
11927 isl_die(bmap->ctx, isl_error_invalid,
11928 "basic map cannot be curried", goto error);
11929 bmap = isl_basic_map_cow(bmap);
11930 if (!bmap)
11931 return NULL;
11932 bmap->dim = isl_space_curry(bmap->dim);
11933 if (!bmap->dim)
11934 goto error;
11935 bmap = isl_basic_map_mark_final(bmap);
11936 return bmap;
11937 error:
11938 isl_basic_map_free(bmap);
11939 return NULL;
11942 /* Given a map (A -> B) -> C, return the corresponding map
11943 * A -> (B -> C).
11945 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11947 return isl_map_change_space(map, &isl_map_can_curry,
11948 "map cannot be curried", &isl_space_curry);
11951 /* Can isl_map_range_curry be applied to "map"?
11952 * That is, does it have a nested relation in its range,
11953 * the domain of which is itself a nested relation?
11955 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11957 if (!map)
11958 return isl_bool_error;
11960 return isl_space_can_range_curry(map->dim);
11963 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11964 * A -> (B -> (C -> D)).
11966 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11968 return isl_map_change_space(map, &isl_map_can_range_curry,
11969 "map range cannot be curried",
11970 &isl_space_range_curry);
11973 /* Can we apply isl_basic_map_uncurry to "bmap"?
11974 * That is, does it have a nested relation in its domain?
11976 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11978 if (!bmap)
11979 return isl_bool_error;
11981 return isl_space_can_uncurry(bmap->dim);
11984 /* Can we apply isl_map_uncurry to "map"?
11985 * That is, does it have a nested relation in its domain?
11987 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11989 if (!map)
11990 return isl_bool_error;
11992 return isl_space_can_uncurry(map->dim);
11995 /* Given a basic map A -> (B -> C), return the corresponding basic map
11996 * (A -> B) -> C.
11998 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12001 if (!bmap)
12002 return NULL;
12004 if (!isl_basic_map_can_uncurry(bmap))
12005 isl_die(bmap->ctx, isl_error_invalid,
12006 "basic map cannot be uncurried",
12007 return isl_basic_map_free(bmap));
12008 bmap = isl_basic_map_cow(bmap);
12009 if (!bmap)
12010 return NULL;
12011 bmap->dim = isl_space_uncurry(bmap->dim);
12012 if (!bmap->dim)
12013 return isl_basic_map_free(bmap);
12014 bmap = isl_basic_map_mark_final(bmap);
12015 return bmap;
12018 /* Given a map A -> (B -> C), return the corresponding map
12019 * (A -> B) -> C.
12021 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12023 return isl_map_change_space(map, &isl_map_can_uncurry,
12024 "map cannot be uncurried", &isl_space_uncurry);
12027 /* Construct a basic map mapping the domain of the affine expression
12028 * to a one-dimensional range prescribed by the affine expression.
12029 * If "rational" is set, then construct a rational basic map.
12031 * A NaN affine expression cannot be converted to a basic map.
12033 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
12034 __isl_take isl_aff *aff, int rational)
12036 int k;
12037 int pos;
12038 isl_bool is_nan;
12039 isl_local_space *ls;
12040 isl_basic_map *bmap = NULL;
12042 if (!aff)
12043 return NULL;
12044 is_nan = isl_aff_is_nan(aff);
12045 if (is_nan < 0)
12046 goto error;
12047 if (is_nan)
12048 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
12049 "cannot convert NaN", goto error);
12051 ls = isl_aff_get_local_space(aff);
12052 bmap = isl_basic_map_from_local_space(ls);
12053 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
12054 k = isl_basic_map_alloc_equality(bmap);
12055 if (k < 0)
12056 goto error;
12058 pos = isl_basic_map_offset(bmap, isl_dim_out);
12059 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
12060 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
12061 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
12062 aff->v->size - (pos + 1));
12064 isl_aff_free(aff);
12065 if (rational)
12066 bmap = isl_basic_map_set_rational(bmap);
12067 bmap = isl_basic_map_gauss(bmap, NULL);
12068 bmap = isl_basic_map_finalize(bmap);
12069 return bmap;
12070 error:
12071 isl_aff_free(aff);
12072 isl_basic_map_free(bmap);
12073 return NULL;
12076 /* Construct a basic map mapping the domain of the affine expression
12077 * to a one-dimensional range prescribed by the affine expression.
12079 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
12081 return isl_basic_map_from_aff2(aff, 0);
12084 /* Construct a map mapping the domain of the affine expression
12085 * to a one-dimensional range prescribed by the affine expression.
12087 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
12089 isl_basic_map *bmap;
12091 bmap = isl_basic_map_from_aff(aff);
12092 return isl_map_from_basic_map(bmap);
12095 /* Construct a basic map mapping the domain the multi-affine expression
12096 * to its range, with each dimension in the range equated to the
12097 * corresponding affine expression.
12098 * If "rational" is set, then construct a rational basic map.
12100 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12101 __isl_take isl_multi_aff *maff, int rational)
12103 int i;
12104 isl_space *space;
12105 isl_basic_map *bmap;
12107 if (!maff)
12108 return NULL;
12110 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12111 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12112 "invalid space", goto error);
12114 space = isl_space_domain(isl_multi_aff_get_space(maff));
12115 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12116 if (rational)
12117 bmap = isl_basic_map_set_rational(bmap);
12119 for (i = 0; i < maff->n; ++i) {
12120 isl_aff *aff;
12121 isl_basic_map *bmap_i;
12123 aff = isl_aff_copy(maff->p[i]);
12124 bmap_i = isl_basic_map_from_aff2(aff, rational);
12126 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12129 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12131 isl_multi_aff_free(maff);
12132 return bmap;
12133 error:
12134 isl_multi_aff_free(maff);
12135 return NULL;
12138 /* Construct a basic map mapping the domain the multi-affine expression
12139 * to its range, with each dimension in the range equated to the
12140 * corresponding affine expression.
12142 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12143 __isl_take isl_multi_aff *ma)
12145 return isl_basic_map_from_multi_aff2(ma, 0);
12148 /* Construct a map mapping the domain the multi-affine expression
12149 * to its range, with each dimension in the range equated to the
12150 * corresponding affine expression.
12152 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12154 isl_basic_map *bmap;
12156 bmap = isl_basic_map_from_multi_aff(maff);
12157 return isl_map_from_basic_map(bmap);
12160 /* Construct a basic map mapping a domain in the given space to
12161 * to an n-dimensional range, with n the number of elements in the list,
12162 * where each coordinate in the range is prescribed by the
12163 * corresponding affine expression.
12164 * The domains of all affine expressions in the list are assumed to match
12165 * domain_dim.
12167 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12168 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12170 int i;
12171 isl_space *dim;
12172 isl_basic_map *bmap;
12174 if (!list)
12175 return NULL;
12177 dim = isl_space_from_domain(domain_dim);
12178 bmap = isl_basic_map_universe(dim);
12180 for (i = 0; i < list->n; ++i) {
12181 isl_aff *aff;
12182 isl_basic_map *bmap_i;
12184 aff = isl_aff_copy(list->p[i]);
12185 bmap_i = isl_basic_map_from_aff(aff);
12187 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12190 isl_aff_list_free(list);
12191 return bmap;
12194 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12195 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12197 return isl_map_equate(set, type1, pos1, type2, pos2);
12200 /* Construct a basic map where the given dimensions are equal to each other.
12202 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12203 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12205 isl_basic_map *bmap = NULL;
12206 int i;
12208 if (!space)
12209 return NULL;
12211 if (pos1 >= isl_space_dim(space, type1))
12212 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12213 "index out of bounds", goto error);
12214 if (pos2 >= isl_space_dim(space, type2))
12215 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12216 "index out of bounds", goto error);
12218 if (type1 == type2 && pos1 == pos2)
12219 return isl_basic_map_universe(space);
12221 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12222 i = isl_basic_map_alloc_equality(bmap);
12223 if (i < 0)
12224 goto error;
12225 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12226 pos1 += isl_basic_map_offset(bmap, type1);
12227 pos2 += isl_basic_map_offset(bmap, type2);
12228 isl_int_set_si(bmap->eq[i][pos1], -1);
12229 isl_int_set_si(bmap->eq[i][pos2], 1);
12230 bmap = isl_basic_map_finalize(bmap);
12231 isl_space_free(space);
12232 return bmap;
12233 error:
12234 isl_space_free(space);
12235 isl_basic_map_free(bmap);
12236 return NULL;
12239 /* Add a constraint imposing that the given two dimensions are equal.
12241 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12242 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12244 isl_basic_map *eq;
12246 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12248 bmap = isl_basic_map_intersect(bmap, eq);
12250 return bmap;
12253 /* Add a constraint imposing that the given two dimensions are equal.
12255 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12256 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12258 isl_basic_map *bmap;
12260 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12262 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12264 return map;
12267 /* Add a constraint imposing that the given two dimensions have opposite values.
12269 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12270 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12272 isl_basic_map *bmap = NULL;
12273 int i;
12275 if (!map)
12276 return NULL;
12278 if (pos1 >= isl_map_dim(map, type1))
12279 isl_die(map->ctx, isl_error_invalid,
12280 "index out of bounds", goto error);
12281 if (pos2 >= isl_map_dim(map, type2))
12282 isl_die(map->ctx, isl_error_invalid,
12283 "index out of bounds", goto error);
12285 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12286 i = isl_basic_map_alloc_equality(bmap);
12287 if (i < 0)
12288 goto error;
12289 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12290 pos1 += isl_basic_map_offset(bmap, type1);
12291 pos2 += isl_basic_map_offset(bmap, type2);
12292 isl_int_set_si(bmap->eq[i][pos1], 1);
12293 isl_int_set_si(bmap->eq[i][pos2], 1);
12294 bmap = isl_basic_map_finalize(bmap);
12296 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12298 return map;
12299 error:
12300 isl_basic_map_free(bmap);
12301 isl_map_free(map);
12302 return NULL;
12305 /* Construct a constraint imposing that the value of the first dimension is
12306 * greater than or equal to that of the second.
12308 static __isl_give isl_constraint *constraint_order_ge(
12309 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12310 enum isl_dim_type type2, int pos2)
12312 isl_constraint *c;
12314 if (!space)
12315 return NULL;
12317 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12319 if (pos1 >= isl_constraint_dim(c, type1))
12320 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12321 "index out of bounds", return isl_constraint_free(c));
12322 if (pos2 >= isl_constraint_dim(c, type2))
12323 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12324 "index out of bounds", return isl_constraint_free(c));
12326 if (type1 == type2 && pos1 == pos2)
12327 return c;
12329 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12330 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12332 return c;
12335 /* Add a constraint imposing that the value of the first dimension is
12336 * greater than or equal to that of the second.
12338 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12339 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12341 isl_constraint *c;
12342 isl_space *space;
12344 if (type1 == type2 && pos1 == pos2)
12345 return bmap;
12346 space = isl_basic_map_get_space(bmap);
12347 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12348 bmap = isl_basic_map_add_constraint(bmap, c);
12350 return bmap;
12353 /* Add a constraint imposing that the value of the first dimension is
12354 * greater than or equal to that of the second.
12356 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12357 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12359 isl_constraint *c;
12360 isl_space *space;
12362 if (type1 == type2 && pos1 == pos2)
12363 return map;
12364 space = isl_map_get_space(map);
12365 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12366 map = isl_map_add_constraint(map, c);
12368 return map;
12371 /* Add a constraint imposing that the value of the first dimension is
12372 * less than or equal to that of the second.
12374 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12375 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12377 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12380 /* Construct a basic map where the value of the first dimension is
12381 * greater than that of the second.
12383 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12384 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12386 isl_basic_map *bmap = NULL;
12387 int i;
12389 if (!space)
12390 return NULL;
12392 if (pos1 >= isl_space_dim(space, type1))
12393 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12394 "index out of bounds", goto error);
12395 if (pos2 >= isl_space_dim(space, type2))
12396 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12397 "index out of bounds", goto error);
12399 if (type1 == type2 && pos1 == pos2)
12400 return isl_basic_map_empty(space);
12402 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12403 i = isl_basic_map_alloc_inequality(bmap);
12404 if (i < 0)
12405 return isl_basic_map_free(bmap);
12406 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12407 pos1 += isl_basic_map_offset(bmap, type1);
12408 pos2 += isl_basic_map_offset(bmap, type2);
12409 isl_int_set_si(bmap->ineq[i][pos1], 1);
12410 isl_int_set_si(bmap->ineq[i][pos2], -1);
12411 isl_int_set_si(bmap->ineq[i][0], -1);
12412 bmap = isl_basic_map_finalize(bmap);
12414 return bmap;
12415 error:
12416 isl_space_free(space);
12417 isl_basic_map_free(bmap);
12418 return NULL;
12421 /* Add a constraint imposing that the value of the first dimension is
12422 * greater than that of the second.
12424 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12425 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12427 isl_basic_map *gt;
12429 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12431 bmap = isl_basic_map_intersect(bmap, gt);
12433 return bmap;
12436 /* Add a constraint imposing that the value of the first dimension is
12437 * greater than that of the second.
12439 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12440 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12442 isl_basic_map *bmap;
12444 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12446 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12448 return map;
12451 /* Add a constraint imposing that the value of the first dimension is
12452 * smaller than that of the second.
12454 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12455 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12457 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12460 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12461 int pos)
12463 isl_aff *div;
12464 isl_local_space *ls;
12466 if (!bmap)
12467 return NULL;
12469 if (!isl_basic_map_divs_known(bmap))
12470 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12471 "some divs are unknown", return NULL);
12473 ls = isl_basic_map_get_local_space(bmap);
12474 div = isl_local_space_get_div(ls, pos);
12475 isl_local_space_free(ls);
12477 return div;
12480 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12481 int pos)
12483 return isl_basic_map_get_div(bset, pos);
12486 /* Plug in "subs" for dimension "type", "pos" of "bset".
12488 * Let i be the dimension to replace and let "subs" be of the form
12490 * f/d
12492 * Any integer division with a non-zero coefficient for i,
12494 * floor((a i + g)/m)
12496 * is replaced by
12498 * floor((a f + d g)/(m d))
12500 * Constraints of the form
12502 * a i + g
12504 * are replaced by
12506 * a f + d g
12508 * We currently require that "subs" is an integral expression.
12509 * Handling rational expressions may require us to add stride constraints
12510 * as we do in isl_basic_set_preimage_multi_aff.
12512 __isl_give isl_basic_set *isl_basic_set_substitute(
12513 __isl_take isl_basic_set *bset,
12514 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12516 int i;
12517 isl_int v;
12518 isl_ctx *ctx;
12520 if (bset && isl_basic_set_plain_is_empty(bset))
12521 return bset;
12523 bset = isl_basic_set_cow(bset);
12524 if (!bset || !subs)
12525 goto error;
12527 ctx = isl_basic_set_get_ctx(bset);
12528 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12529 isl_die(ctx, isl_error_invalid,
12530 "spaces don't match", goto error);
12531 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12532 isl_die(ctx, isl_error_unsupported,
12533 "cannot handle divs yet", goto error);
12534 if (!isl_int_is_one(subs->v->el[0]))
12535 isl_die(ctx, isl_error_invalid,
12536 "can only substitute integer expressions", goto error);
12538 pos += isl_basic_set_offset(bset, type);
12540 isl_int_init(v);
12542 for (i = 0; i < bset->n_eq; ++i) {
12543 if (isl_int_is_zero(bset->eq[i][pos]))
12544 continue;
12545 isl_int_set(v, bset->eq[i][pos]);
12546 isl_int_set_si(bset->eq[i][pos], 0);
12547 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12548 v, subs->v->el + 1, subs->v->size - 1);
12551 for (i = 0; i < bset->n_ineq; ++i) {
12552 if (isl_int_is_zero(bset->ineq[i][pos]))
12553 continue;
12554 isl_int_set(v, bset->ineq[i][pos]);
12555 isl_int_set_si(bset->ineq[i][pos], 0);
12556 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12557 v, subs->v->el + 1, subs->v->size - 1);
12560 for (i = 0; i < bset->n_div; ++i) {
12561 if (isl_int_is_zero(bset->div[i][1 + pos]))
12562 continue;
12563 isl_int_set(v, bset->div[i][1 + pos]);
12564 isl_int_set_si(bset->div[i][1 + pos], 0);
12565 isl_seq_combine(bset->div[i] + 1,
12566 subs->v->el[0], bset->div[i] + 1,
12567 v, subs->v->el + 1, subs->v->size - 1);
12568 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12571 isl_int_clear(v);
12573 bset = isl_basic_set_simplify(bset);
12574 return isl_basic_set_finalize(bset);
12575 error:
12576 isl_basic_set_free(bset);
12577 return NULL;
12580 /* Plug in "subs" for dimension "type", "pos" of "set".
12582 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12583 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12585 int i;
12587 if (set && isl_set_plain_is_empty(set))
12588 return set;
12590 set = isl_set_cow(set);
12591 if (!set || !subs)
12592 goto error;
12594 for (i = set->n - 1; i >= 0; --i) {
12595 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12596 if (remove_if_empty(set, i) < 0)
12597 goto error;
12600 return set;
12601 error:
12602 isl_set_free(set);
12603 return NULL;
12606 /* Check if the range of "ma" is compatible with the domain or range
12607 * (depending on "type") of "bmap".
12609 static isl_stat check_basic_map_compatible_range_multi_aff(
12610 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12611 __isl_keep isl_multi_aff *ma)
12613 isl_bool m;
12614 isl_space *ma_space;
12616 ma_space = isl_multi_aff_get_space(ma);
12618 m = isl_space_has_equal_params(bmap->dim, ma_space);
12619 if (m < 0)
12620 goto error;
12621 if (!m)
12622 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12623 "parameters don't match", goto error);
12624 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12625 if (m < 0)
12626 goto error;
12627 if (!m)
12628 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12629 "spaces don't match", goto error);
12631 isl_space_free(ma_space);
12632 return isl_stat_ok;
12633 error:
12634 isl_space_free(ma_space);
12635 return isl_stat_error;
12638 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12639 * coefficients before the transformed range of dimensions,
12640 * the "n_after" coefficients after the transformed range of dimensions
12641 * and the coefficients of the other divs in "bmap".
12643 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12644 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12646 int i;
12647 int n_param;
12648 int n_set;
12649 isl_local_space *ls;
12651 if (n_div == 0)
12652 return 0;
12654 ls = isl_aff_get_domain_local_space(ma->p[0]);
12655 if (!ls)
12656 return -1;
12658 n_param = isl_local_space_dim(ls, isl_dim_param);
12659 n_set = isl_local_space_dim(ls, isl_dim_set);
12660 for (i = 0; i < n_div; ++i) {
12661 int o_bmap = 0, o_ls = 0;
12663 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12664 o_bmap += 1 + 1 + n_param;
12665 o_ls += 1 + 1 + n_param;
12666 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12667 o_bmap += n_before;
12668 isl_seq_cpy(bmap->div[i] + o_bmap,
12669 ls->div->row[i] + o_ls, n_set);
12670 o_bmap += n_set;
12671 o_ls += n_set;
12672 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12673 o_bmap += n_after;
12674 isl_seq_cpy(bmap->div[i] + o_bmap,
12675 ls->div->row[i] + o_ls, n_div);
12676 o_bmap += n_div;
12677 o_ls += n_div;
12678 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12679 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12680 goto error;
12683 isl_local_space_free(ls);
12684 return 0;
12685 error:
12686 isl_local_space_free(ls);
12687 return -1;
12690 /* How many stride constraints does "ma" enforce?
12691 * That is, how many of the affine expressions have a denominator
12692 * different from one?
12694 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12696 int i;
12697 int strides = 0;
12699 for (i = 0; i < ma->n; ++i)
12700 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12701 strides++;
12703 return strides;
12706 /* For each affine expression in ma of the form
12708 * x_i = (f_i y + h_i)/m_i
12710 * with m_i different from one, add a constraint to "bmap"
12711 * of the form
12713 * f_i y + h_i = m_i alpha_i
12715 * with alpha_i an additional existentially quantified variable.
12717 * The input variables of "ma" correspond to a subset of the variables
12718 * of "bmap". There are "n_before" variables in "bmap" before this
12719 * subset and "n_after" variables after this subset.
12720 * The integer divisions of the affine expressions in "ma" are assumed
12721 * to have been aligned. There are "n_div_ma" of them and
12722 * they appear first in "bmap", straight after the "n_after" variables.
12724 static __isl_give isl_basic_map *add_ma_strides(
12725 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12726 int n_before, int n_after, int n_div_ma)
12728 int i, k;
12729 int div;
12730 int total;
12731 int n_param;
12732 int n_in;
12734 total = isl_basic_map_total_dim(bmap);
12735 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12736 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12737 for (i = 0; i < ma->n; ++i) {
12738 int o_bmap = 0, o_ma = 1;
12740 if (isl_int_is_one(ma->p[i]->v->el[0]))
12741 continue;
12742 div = isl_basic_map_alloc_div(bmap);
12743 k = isl_basic_map_alloc_equality(bmap);
12744 if (div < 0 || k < 0)
12745 goto error;
12746 isl_int_set_si(bmap->div[div][0], 0);
12747 isl_seq_cpy(bmap->eq[k] + o_bmap,
12748 ma->p[i]->v->el + o_ma, 1 + n_param);
12749 o_bmap += 1 + n_param;
12750 o_ma += 1 + n_param;
12751 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12752 o_bmap += n_before;
12753 isl_seq_cpy(bmap->eq[k] + o_bmap,
12754 ma->p[i]->v->el + o_ma, n_in);
12755 o_bmap += n_in;
12756 o_ma += n_in;
12757 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12758 o_bmap += n_after;
12759 isl_seq_cpy(bmap->eq[k] + o_bmap,
12760 ma->p[i]->v->el + o_ma, n_div_ma);
12761 o_bmap += n_div_ma;
12762 o_ma += n_div_ma;
12763 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12764 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12765 total++;
12768 return bmap;
12769 error:
12770 isl_basic_map_free(bmap);
12771 return NULL;
12774 /* Replace the domain or range space (depending on "type) of "space" by "set".
12776 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12777 enum isl_dim_type type, __isl_take isl_space *set)
12779 if (type == isl_dim_in) {
12780 space = isl_space_range(space);
12781 space = isl_space_map_from_domain_and_range(set, space);
12782 } else {
12783 space = isl_space_domain(space);
12784 space = isl_space_map_from_domain_and_range(space, set);
12787 return space;
12790 /* Compute the preimage of the domain or range (depending on "type")
12791 * of "bmap" under the function represented by "ma".
12792 * In other words, plug in "ma" in the domain or range of "bmap".
12793 * The result is a basic map that lives in the same space as "bmap"
12794 * except that the domain or range has been replaced by
12795 * the domain space of "ma".
12797 * If bmap is represented by
12799 * A(p) + S u + B x + T v + C(divs) >= 0,
12801 * where u and x are input and output dimensions if type == isl_dim_out
12802 * while x and v are input and output dimensions if type == isl_dim_in,
12803 * and ma is represented by
12805 * x = D(p) + F(y) + G(divs')
12807 * then the result is
12809 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12811 * The divs in the input set are similarly adjusted.
12812 * In particular
12814 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12816 * becomes
12818 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12819 * B_i G(divs') + c_i(divs))/n_i)
12821 * If bmap is not a rational map and if F(y) involves any denominators
12823 * x_i = (f_i y + h_i)/m_i
12825 * then additional constraints are added to ensure that we only
12826 * map back integer points. That is we enforce
12828 * f_i y + h_i = m_i alpha_i
12830 * with alpha_i an additional existentially quantified variable.
12832 * We first copy over the divs from "ma".
12833 * Then we add the modified constraints and divs from "bmap".
12834 * Finally, we add the stride constraints, if needed.
12836 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12837 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12838 __isl_take isl_multi_aff *ma)
12840 int i, k;
12841 isl_space *space;
12842 isl_basic_map *res = NULL;
12843 int n_before, n_after, n_div_bmap, n_div_ma;
12844 isl_int f, c1, c2, g;
12845 isl_bool rational;
12846 int strides;
12848 isl_int_init(f);
12849 isl_int_init(c1);
12850 isl_int_init(c2);
12851 isl_int_init(g);
12853 ma = isl_multi_aff_align_divs(ma);
12854 if (!bmap || !ma)
12855 goto error;
12856 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12857 goto error;
12859 if (type == isl_dim_in) {
12860 n_before = 0;
12861 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12862 } else {
12863 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12864 n_after = 0;
12866 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12867 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12869 space = isl_multi_aff_get_domain_space(ma);
12870 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12871 rational = isl_basic_map_is_rational(bmap);
12872 strides = rational ? 0 : multi_aff_strides(ma);
12873 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12874 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12875 if (rational)
12876 res = isl_basic_map_set_rational(res);
12878 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12879 if (isl_basic_map_alloc_div(res) < 0)
12880 goto error;
12882 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12883 goto error;
12885 for (i = 0; i < bmap->n_eq; ++i) {
12886 k = isl_basic_map_alloc_equality(res);
12887 if (k < 0)
12888 goto error;
12889 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12890 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12893 for (i = 0; i < bmap->n_ineq; ++i) {
12894 k = isl_basic_map_alloc_inequality(res);
12895 if (k < 0)
12896 goto error;
12897 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12898 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12901 for (i = 0; i < bmap->n_div; ++i) {
12902 if (isl_int_is_zero(bmap->div[i][0])) {
12903 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12904 continue;
12906 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12907 n_before, n_after, n_div_ma, n_div_bmap,
12908 f, c1, c2, g, 1);
12911 if (strides)
12912 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12914 isl_int_clear(f);
12915 isl_int_clear(c1);
12916 isl_int_clear(c2);
12917 isl_int_clear(g);
12918 isl_basic_map_free(bmap);
12919 isl_multi_aff_free(ma);
12920 res = isl_basic_map_simplify(res);
12921 return isl_basic_map_finalize(res);
12922 error:
12923 isl_int_clear(f);
12924 isl_int_clear(c1);
12925 isl_int_clear(c2);
12926 isl_int_clear(g);
12927 isl_basic_map_free(bmap);
12928 isl_multi_aff_free(ma);
12929 isl_basic_map_free(res);
12930 return NULL;
12933 /* Compute the preimage of "bset" under the function represented by "ma".
12934 * In other words, plug in "ma" in "bset". The result is a basic set
12935 * that lives in the domain space of "ma".
12937 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12938 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12940 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12943 /* Compute the preimage of the domain of "bmap" under the function
12944 * represented by "ma".
12945 * In other words, plug in "ma" in the domain of "bmap".
12946 * The result is a basic map that lives in the same space as "bmap"
12947 * except that the domain has been replaced by the domain space of "ma".
12949 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12950 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12952 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12955 /* Compute the preimage of the range of "bmap" under the function
12956 * represented by "ma".
12957 * In other words, plug in "ma" in the range of "bmap".
12958 * The result is a basic map that lives in the same space as "bmap"
12959 * except that the range has been replaced by the domain space of "ma".
12961 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12962 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12964 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12967 /* Check if the range of "ma" is compatible with the domain or range
12968 * (depending on "type") of "map".
12969 * Return isl_stat_error if anything is wrong.
12971 static isl_stat check_map_compatible_range_multi_aff(
12972 __isl_keep isl_map *map, enum isl_dim_type type,
12973 __isl_keep isl_multi_aff *ma)
12975 isl_bool m;
12976 isl_space *ma_space;
12978 ma_space = isl_multi_aff_get_space(ma);
12979 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12980 isl_space_free(ma_space);
12981 if (m < 0)
12982 return isl_stat_error;
12983 if (!m)
12984 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12985 "spaces don't match", return isl_stat_error);
12986 return isl_stat_ok;
12989 /* Compute the preimage of the domain or range (depending on "type")
12990 * of "map" under the function represented by "ma".
12991 * In other words, plug in "ma" in the domain or range of "map".
12992 * The result is a map that lives in the same space as "map"
12993 * except that the domain or range has been replaced by
12994 * the domain space of "ma".
12996 * The parameters are assumed to have been aligned.
12998 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12999 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13001 int i;
13002 isl_space *space;
13004 map = isl_map_cow(map);
13005 ma = isl_multi_aff_align_divs(ma);
13006 if (!map || !ma)
13007 goto error;
13008 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13009 goto error;
13011 for (i = 0; i < map->n; ++i) {
13012 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13013 isl_multi_aff_copy(ma));
13014 if (!map->p[i])
13015 goto error;
13018 space = isl_multi_aff_get_domain_space(ma);
13019 space = isl_space_set(isl_map_get_space(map), type, space);
13021 isl_space_free(map->dim);
13022 map->dim = space;
13023 if (!map->dim)
13024 goto error;
13026 isl_multi_aff_free(ma);
13027 if (map->n > 1)
13028 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13029 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13030 return map;
13031 error:
13032 isl_multi_aff_free(ma);
13033 isl_map_free(map);
13034 return NULL;
13037 /* Compute the preimage of the domain or range (depending on "type")
13038 * of "map" under the function represented by "ma".
13039 * In other words, plug in "ma" in the domain or range of "map".
13040 * The result is a map that lives in the same space as "map"
13041 * except that the domain or range has been replaced by
13042 * the domain space of "ma".
13044 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13045 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13047 isl_bool aligned;
13049 if (!map || !ma)
13050 goto error;
13052 aligned = isl_map_space_has_equal_params(map, ma->space);
13053 if (aligned < 0)
13054 goto error;
13055 if (aligned)
13056 return map_preimage_multi_aff(map, type, ma);
13058 if (isl_map_check_named_params(map) < 0)
13059 goto error;
13060 if (!isl_space_has_named_params(ma->space))
13061 isl_die(map->ctx, isl_error_invalid,
13062 "unaligned unnamed parameters", goto error);
13063 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13064 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13066 return map_preimage_multi_aff(map, type, ma);
13067 error:
13068 isl_multi_aff_free(ma);
13069 return isl_map_free(map);
13072 /* Compute the preimage of "set" under the function represented by "ma".
13073 * In other words, plug in "ma" in "set". The result is a set
13074 * that lives in the domain space of "ma".
13076 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13077 __isl_take isl_multi_aff *ma)
13079 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13082 /* Compute the preimage of the domain of "map" under the function
13083 * represented by "ma".
13084 * In other words, plug in "ma" in the domain of "map".
13085 * The result is a map that lives in the same space as "map"
13086 * except that the domain has been replaced by the domain space of "ma".
13088 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13089 __isl_take isl_multi_aff *ma)
13091 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13094 /* Compute the preimage of the range of "map" under the function
13095 * represented by "ma".
13096 * In other words, plug in "ma" in the range of "map".
13097 * The result is a map that lives in the same space as "map"
13098 * except that the range has been replaced by the domain space of "ma".
13100 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13101 __isl_take isl_multi_aff *ma)
13103 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13106 /* Compute the preimage of "map" under the function represented by "pma".
13107 * In other words, plug in "pma" in the domain or range of "map".
13108 * The result is a map that lives in the same space as "map",
13109 * except that the space of type "type" has been replaced by
13110 * the domain space of "pma".
13112 * The parameters of "map" and "pma" are assumed to have been aligned.
13114 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13115 __isl_take isl_map *map, enum isl_dim_type type,
13116 __isl_take isl_pw_multi_aff *pma)
13118 int i;
13119 isl_map *res;
13121 if (!pma)
13122 goto error;
13124 if (pma->n == 0) {
13125 isl_pw_multi_aff_free(pma);
13126 res = isl_map_empty(isl_map_get_space(map));
13127 isl_map_free(map);
13128 return res;
13131 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13132 isl_multi_aff_copy(pma->p[0].maff));
13133 if (type == isl_dim_in)
13134 res = isl_map_intersect_domain(res,
13135 isl_map_copy(pma->p[0].set));
13136 else
13137 res = isl_map_intersect_range(res,
13138 isl_map_copy(pma->p[0].set));
13140 for (i = 1; i < pma->n; ++i) {
13141 isl_map *res_i;
13143 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13144 isl_multi_aff_copy(pma->p[i].maff));
13145 if (type == isl_dim_in)
13146 res_i = isl_map_intersect_domain(res_i,
13147 isl_map_copy(pma->p[i].set));
13148 else
13149 res_i = isl_map_intersect_range(res_i,
13150 isl_map_copy(pma->p[i].set));
13151 res = isl_map_union(res, res_i);
13154 isl_pw_multi_aff_free(pma);
13155 isl_map_free(map);
13156 return res;
13157 error:
13158 isl_pw_multi_aff_free(pma);
13159 isl_map_free(map);
13160 return NULL;
13163 /* Compute the preimage of "map" under the function represented by "pma".
13164 * In other words, plug in "pma" in the domain or range of "map".
13165 * The result is a map that lives in the same space as "map",
13166 * except that the space of type "type" has been replaced by
13167 * the domain space of "pma".
13169 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13170 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13172 isl_bool aligned;
13174 if (!map || !pma)
13175 goto error;
13177 aligned = isl_map_space_has_equal_params(map, pma->dim);
13178 if (aligned < 0)
13179 goto error;
13180 if (aligned)
13181 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13183 if (isl_map_check_named_params(map) < 0)
13184 goto error;
13185 if (!isl_space_has_named_params(pma->dim))
13186 isl_die(map->ctx, isl_error_invalid,
13187 "unaligned unnamed parameters", goto error);
13188 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13189 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13191 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13192 error:
13193 isl_pw_multi_aff_free(pma);
13194 return isl_map_free(map);
13197 /* Compute the preimage of "set" under the function represented by "pma".
13198 * In other words, plug in "pma" in "set". The result is a set
13199 * that lives in the domain space of "pma".
13201 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13202 __isl_take isl_pw_multi_aff *pma)
13204 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13207 /* Compute the preimage of the domain of "map" under the function
13208 * represented by "pma".
13209 * In other words, plug in "pma" in the domain of "map".
13210 * The result is a map that lives in the same space as "map",
13211 * except that domain space has been replaced by the domain space of "pma".
13213 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13214 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13216 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13219 /* Compute the preimage of the range of "map" under the function
13220 * represented by "pma".
13221 * In other words, plug in "pma" in the range of "map".
13222 * The result is a map that lives in the same space as "map",
13223 * except that range space has been replaced by the domain space of "pma".
13225 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13226 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13228 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13231 /* Compute the preimage of "map" under the function represented by "mpa".
13232 * In other words, plug in "mpa" in the domain or range of "map".
13233 * The result is a map that lives in the same space as "map",
13234 * except that the space of type "type" has been replaced by
13235 * the domain space of "mpa".
13237 * If the map does not involve any constraints that refer to the
13238 * dimensions of the substituted space, then the only possible
13239 * effect of "mpa" on the map is to map the space to a different space.
13240 * We create a separate isl_multi_aff to effectuate this change
13241 * in order to avoid spurious splitting of the map along the pieces
13242 * of "mpa".
13244 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13245 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13247 int n;
13248 isl_pw_multi_aff *pma;
13250 if (!map || !mpa)
13251 goto error;
13253 n = isl_map_dim(map, type);
13254 if (!isl_map_involves_dims(map, type, 0, n)) {
13255 isl_space *space;
13256 isl_multi_aff *ma;
13258 space = isl_multi_pw_aff_get_space(mpa);
13259 isl_multi_pw_aff_free(mpa);
13260 ma = isl_multi_aff_zero(space);
13261 return isl_map_preimage_multi_aff(map, type, ma);
13264 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13265 return isl_map_preimage_pw_multi_aff(map, type, pma);
13266 error:
13267 isl_map_free(map);
13268 isl_multi_pw_aff_free(mpa);
13269 return NULL;
13272 /* Compute the preimage of "map" under the function represented by "mpa".
13273 * In other words, plug in "mpa" in the domain "map".
13274 * The result is a map that lives in the same space as "map",
13275 * except that domain space has been replaced by the domain space of "mpa".
13277 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13278 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13280 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13283 /* Compute the preimage of "set" by the function represented by "mpa".
13284 * In other words, plug in "mpa" in "set".
13286 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13287 __isl_take isl_multi_pw_aff *mpa)
13289 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13292 /* Are the "n" "coefficients" starting at "first" of the integer division
13293 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13294 * to each other?
13295 * The "coefficient" at position 0 is the denominator.
13296 * The "coefficient" at position 1 is the constant term.
13298 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13299 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13300 unsigned first, unsigned n)
13302 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13303 return isl_bool_error;
13304 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13305 return isl_bool_error;
13306 return isl_seq_eq(bmap1->div[pos1] + first,
13307 bmap2->div[pos2] + first, n);
13310 /* Are the integer division expressions at position "pos1" in "bmap1" and
13311 * "pos2" in "bmap2" equal to each other, except that the constant terms
13312 * are different?
13314 isl_bool isl_basic_map_equal_div_expr_except_constant(
13315 __isl_keep isl_basic_map *bmap1, int pos1,
13316 __isl_keep isl_basic_map *bmap2, int pos2)
13318 isl_bool equal;
13319 unsigned total;
13321 if (!bmap1 || !bmap2)
13322 return isl_bool_error;
13323 total = isl_basic_map_total_dim(bmap1);
13324 if (total != isl_basic_map_total_dim(bmap2))
13325 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13326 "incomparable div expressions", return isl_bool_error);
13327 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13328 0, 1);
13329 if (equal < 0 || !equal)
13330 return equal;
13331 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13332 1, 1);
13333 if (equal < 0 || equal)
13334 return isl_bool_not(equal);
13335 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13336 2, total);
13339 /* Replace the numerator of the constant term of the integer division
13340 * expression at position "div" in "bmap" by "value".
13341 * The caller guarantees that this does not change the meaning
13342 * of the input.
13344 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13345 __isl_take isl_basic_map *bmap, int div, int value)
13347 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13348 return isl_basic_map_free(bmap);
13350 isl_int_set_si(bmap->div[div][1], value);
13352 return bmap;
13355 /* Is the point "inner" internal to inequality constraint "ineq"
13356 * of "bset"?
13357 * The point is considered to be internal to the inequality constraint,
13358 * if it strictly lies on the positive side of the inequality constraint,
13359 * or if it lies on the constraint and the constraint is lexico-positive.
13361 static isl_bool is_internal(__isl_keep isl_vec *inner,
13362 __isl_keep isl_basic_set *bset, int ineq)
13364 isl_ctx *ctx;
13365 int pos;
13366 unsigned total;
13368 if (!inner || !bset)
13369 return isl_bool_error;
13371 ctx = isl_basic_set_get_ctx(bset);
13372 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13373 &ctx->normalize_gcd);
13374 if (!isl_int_is_zero(ctx->normalize_gcd))
13375 return isl_int_is_nonneg(ctx->normalize_gcd);
13377 total = isl_basic_set_dim(bset, isl_dim_all);
13378 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13379 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13382 /* Tighten the inequality constraints of "bset" that are outward with respect
13383 * to the point "vec".
13384 * That is, tighten the constraints that are not satisfied by "vec".
13386 * "vec" is a point internal to some superset S of "bset" that is used
13387 * to make the subsets of S disjoint, by tightening one half of the constraints
13388 * that separate two subsets. In particular, the constraints of S
13389 * are all satisfied by "vec" and should not be tightened.
13390 * Of the internal constraints, those that have "vec" on the outside
13391 * are tightened. The shared facet is included in the adjacent subset
13392 * with the opposite constraint.
13393 * For constraints that saturate "vec", this criterion cannot be used
13394 * to determine which of the two sides should be tightened.
13395 * Instead, the sign of the first non-zero coefficient is used
13396 * to make this choice. Note that this second criterion is never used
13397 * on the constraints of S since "vec" is interior to "S".
13399 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13400 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13402 int j;
13404 bset = isl_basic_set_cow(bset);
13405 if (!bset)
13406 return NULL;
13407 for (j = 0; j < bset->n_ineq; ++j) {
13408 isl_bool internal;
13410 internal = is_internal(vec, bset, j);
13411 if (internal < 0)
13412 return isl_basic_set_free(bset);
13413 if (internal)
13414 continue;
13415 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13418 return bset;
13421 /* Replace the variables x of type "type" starting at "first" in "bmap"
13422 * by x' with x = M x' with M the matrix trans.
13423 * That is, replace the corresponding coefficients c by c M.
13425 * The transformation matrix should be a square matrix.
13427 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13428 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13429 __isl_take isl_mat *trans)
13431 unsigned pos;
13433 bmap = isl_basic_map_cow(bmap);
13434 if (!bmap || !trans)
13435 goto error;
13437 if (trans->n_row != trans->n_col)
13438 isl_die(trans->ctx, isl_error_invalid,
13439 "expecting square transformation matrix", goto error);
13440 if (first + trans->n_row > isl_basic_map_dim(bmap, type))
13441 isl_die(trans->ctx, isl_error_invalid,
13442 "oversized transformation matrix", goto error);
13444 pos = isl_basic_map_offset(bmap, type) + first;
13446 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13447 isl_mat_copy(trans)) < 0)
13448 goto error;
13449 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13450 isl_mat_copy(trans)) < 0)
13451 goto error;
13452 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13453 isl_mat_copy(trans)) < 0)
13454 goto error;
13456 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
13457 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13459 isl_mat_free(trans);
13460 return bmap;
13461 error:
13462 isl_mat_free(trans);
13463 isl_basic_map_free(bmap);
13464 return NULL;
13467 /* Replace the variables x of type "type" starting at "first" in "bset"
13468 * by x' with x = M x' with M the matrix trans.
13469 * That is, replace the corresponding coefficients c by c M.
13471 * The transformation matrix should be a square matrix.
13473 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13474 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13475 __isl_take isl_mat *trans)
13477 return isl_basic_map_transform_dims(bset, type, first, trans);