isl_map_list.c: directly include required header
[isl.git] / isl_map.c
blob6cfc1da8f6ba0dd9bd2612c01fb6914d15e1df36
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
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <string.h>
19 #include <isl_ctx_private.h>
20 #include <isl_map_private.h>
21 #include <isl_blk.h>
22 #include <isl/constraint.h>
23 #include "isl_space_private.h"
24 #include "isl_equalities.h"
25 #include <isl_lp_private.h>
26 #include <isl_seq.h>
27 #include <isl/set.h>
28 #include <isl/map.h>
29 #include <isl_reordering.h>
30 #include "isl_sample.h"
31 #include <isl_sort.h>
32 #include "isl_tab.h"
33 #include <isl/vec.h>
34 #include <isl_mat_private.h>
35 #include <isl_vec_private.h>
36 #include <isl_dim_map.h>
37 #include <isl_local_space_private.h>
38 #include <isl_aff_private.h>
39 #include <isl_options_private.h>
40 #include <isl_morph.h>
41 #include <isl_val_private.h>
42 #include <isl/deprecated/map_int.h>
43 #include <isl/deprecated/set_int.h>
45 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
47 switch (type) {
48 case isl_dim_param: return dim->nparam;
49 case isl_dim_in: return dim->n_in;
50 case isl_dim_out: return dim->n_out;
51 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
52 default: return 0;
56 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
58 switch (type) {
59 case isl_dim_param: return 1;
60 case isl_dim_in: return 1 + dim->nparam;
61 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
62 default: return 0;
66 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
67 enum isl_dim_type type)
69 if (!bmap)
70 return 0;
71 switch (type) {
72 case isl_dim_cst: return 1;
73 case isl_dim_param:
74 case isl_dim_in:
75 case isl_dim_out: return isl_space_dim(bmap->dim, type);
76 case isl_dim_div: return bmap->n_div;
77 case isl_dim_all: return isl_basic_map_total_dim(bmap);
78 default: return 0;
82 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
84 return map ? n(map->dim, type) : 0;
87 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
89 return set ? n(set->dim, type) : 0;
92 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
93 enum isl_dim_type type)
95 isl_space *dim = bmap->dim;
96 switch (type) {
97 case isl_dim_cst: return 0;
98 case isl_dim_param: return 1;
99 case isl_dim_in: return 1 + dim->nparam;
100 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
101 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
102 default: return 0;
106 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
107 enum isl_dim_type type)
109 return isl_basic_map_offset(bset, type);
112 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
114 return pos(map->dim, type);
117 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
118 enum isl_dim_type type)
120 return isl_basic_map_dim(bset, type);
123 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
125 return isl_basic_set_dim(bset, isl_dim_set);
128 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
130 return isl_basic_set_dim(bset, isl_dim_param);
133 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
135 if (!bset)
136 return 0;
137 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
140 unsigned isl_set_n_dim(__isl_keep isl_set *set)
142 return isl_set_dim(set, isl_dim_set);
145 unsigned isl_set_n_param(__isl_keep isl_set *set)
147 return isl_set_dim(set, isl_dim_param);
150 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
152 return bmap ? bmap->dim->n_in : 0;
155 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
157 return bmap ? bmap->dim->n_out : 0;
160 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
162 return bmap ? bmap->dim->nparam : 0;
165 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
167 return bmap ? bmap->n_div : 0;
170 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
172 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
175 unsigned isl_map_n_in(const struct isl_map *map)
177 return map ? map->dim->n_in : 0;
180 unsigned isl_map_n_out(const struct isl_map *map)
182 return map ? map->dim->n_out : 0;
185 unsigned isl_map_n_param(const struct isl_map *map)
187 return map ? map->dim->nparam : 0;
190 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
192 int m;
193 if (!map || !set)
194 return -1;
195 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
196 if (m < 0 || !m)
197 return m;
198 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
199 set->dim, isl_dim_set);
202 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
203 struct isl_basic_set *bset)
205 int m;
206 if (!bmap || !bset)
207 return -1;
208 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
209 if (m < 0 || !m)
210 return m;
211 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
212 bset->dim, isl_dim_set);
215 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
217 int m;
218 if (!map || !set)
219 return -1;
220 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
221 if (m < 0 || !m)
222 return m;
223 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
224 set->dim, isl_dim_set);
227 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
228 struct isl_basic_set *bset)
230 int m;
231 if (!bmap || !bset)
232 return -1;
233 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
234 if (m < 0 || !m)
235 return m;
236 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
237 bset->dim, isl_dim_set);
240 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
242 return bmap ? bmap->ctx : NULL;
245 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
247 return bset ? bset->ctx : NULL;
250 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
252 return map ? map->ctx : NULL;
255 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
257 return set ? set->ctx : NULL;
260 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
262 if (!bmap)
263 return NULL;
264 return isl_space_copy(bmap->dim);
267 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
269 if (!bset)
270 return NULL;
271 return isl_space_copy(bset->dim);
274 /* Extract the divs in "bmap" as a matrix.
276 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
278 int i;
279 isl_ctx *ctx;
280 isl_mat *div;
281 unsigned total;
282 unsigned cols;
284 if (!bmap)
285 return NULL;
287 ctx = isl_basic_map_get_ctx(bmap);
288 total = isl_space_dim(bmap->dim, isl_dim_all);
289 cols = 1 + 1 + total + bmap->n_div;
290 div = isl_mat_alloc(ctx, bmap->n_div, cols);
291 if (!div)
292 return NULL;
294 for (i = 0; i < bmap->n_div; ++i)
295 isl_seq_cpy(div->row[i], bmap->div[i], cols);
297 return div;
300 /* Extract the divs in "bset" as a matrix.
302 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
304 return isl_basic_map_get_divs(bset);
307 __isl_give isl_local_space *isl_basic_map_get_local_space(
308 __isl_keep isl_basic_map *bmap)
310 isl_mat *div;
312 if (!bmap)
313 return NULL;
315 div = isl_basic_map_get_divs(bmap);
316 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
319 __isl_give isl_local_space *isl_basic_set_get_local_space(
320 __isl_keep isl_basic_set *bset)
322 return isl_basic_map_get_local_space(bset);
325 __isl_give isl_basic_map *isl_basic_map_from_local_space(
326 __isl_take isl_local_space *ls)
328 int i;
329 int n_div;
330 isl_basic_map *bmap;
332 if (!ls)
333 return NULL;
335 n_div = isl_local_space_dim(ls, isl_dim_div);
336 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
337 n_div, 0, 2 * n_div);
339 for (i = 0; i < n_div; ++i)
340 if (isl_basic_map_alloc_div(bmap) < 0)
341 goto error;
343 for (i = 0; i < n_div; ++i) {
344 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
345 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
346 goto error;
349 isl_local_space_free(ls);
350 return bmap;
351 error:
352 isl_local_space_free(ls);
353 isl_basic_map_free(bmap);
354 return NULL;
357 __isl_give isl_basic_set *isl_basic_set_from_local_space(
358 __isl_take isl_local_space *ls)
360 return isl_basic_map_from_local_space(ls);
363 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
365 if (!map)
366 return NULL;
367 return isl_space_copy(map->dim);
370 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
372 if (!set)
373 return NULL;
374 return isl_space_copy(set->dim);
377 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
378 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
380 bmap = isl_basic_map_cow(bmap);
381 if (!bmap)
382 return NULL;
383 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
384 if (!bmap->dim)
385 goto error;
386 bmap = isl_basic_map_finalize(bmap);
387 return bmap;
388 error:
389 isl_basic_map_free(bmap);
390 return NULL;
393 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
394 __isl_take isl_basic_set *bset, const char *s)
396 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
399 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
400 enum isl_dim_type type)
402 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
405 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
406 enum isl_dim_type type, const char *s)
408 int i;
410 map = isl_map_cow(map);
411 if (!map)
412 return NULL;
414 map->dim = isl_space_set_tuple_name(map->dim, type, s);
415 if (!map->dim)
416 goto error;
418 for (i = 0; i < map->n; ++i) {
419 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
420 if (!map->p[i])
421 goto error;
424 return map;
425 error:
426 isl_map_free(map);
427 return NULL;
430 /* Replace the identifier of the tuple of type "type" by "id".
432 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
433 __isl_take isl_basic_map *bmap,
434 enum isl_dim_type type, __isl_take isl_id *id)
436 bmap = isl_basic_map_cow(bmap);
437 if (!bmap)
438 goto error;
439 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
440 if (!bmap->dim)
441 return isl_basic_map_free(bmap);
442 bmap = isl_basic_map_finalize(bmap);
443 return bmap;
444 error:
445 isl_id_free(id);
446 return NULL;
449 /* Replace the identifier of the tuple by "id".
451 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
452 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
454 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
457 /* Does the input or output tuple have a name?
459 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
461 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
464 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
465 enum isl_dim_type type)
467 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
470 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
471 const char *s)
473 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
476 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
477 enum isl_dim_type type, __isl_take isl_id *id)
479 map = isl_map_cow(map);
480 if (!map)
481 goto error;
483 map->dim = isl_space_set_tuple_id(map->dim, type, id);
485 return isl_map_reset_space(map, isl_space_copy(map->dim));
486 error:
487 isl_id_free(id);
488 return NULL;
491 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
492 __isl_take isl_id *id)
494 return isl_map_set_tuple_id(set, isl_dim_set, id);
497 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
498 enum isl_dim_type type)
500 map = isl_map_cow(map);
501 if (!map)
502 return NULL;
504 map->dim = isl_space_reset_tuple_id(map->dim, type);
506 return isl_map_reset_space(map, isl_space_copy(map->dim));
509 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
511 return isl_map_reset_tuple_id(set, isl_dim_set);
514 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
516 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
519 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
520 enum isl_dim_type type)
522 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
525 int isl_set_has_tuple_id(__isl_keep isl_set *set)
527 return isl_map_has_tuple_id(set, isl_dim_set);
530 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
532 return isl_map_get_tuple_id(set, isl_dim_set);
535 /* Does the set tuple have a name?
537 int isl_set_has_tuple_name(__isl_keep isl_set *set)
539 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
543 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
545 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
548 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
550 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
553 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
554 enum isl_dim_type type, unsigned pos)
556 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
559 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
560 enum isl_dim_type type, unsigned pos)
562 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
565 /* Does the given dimension have a name?
567 int isl_map_has_dim_name(__isl_keep isl_map *map,
568 enum isl_dim_type type, unsigned pos)
570 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
573 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
574 enum isl_dim_type type, unsigned pos)
576 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
579 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
580 enum isl_dim_type type, unsigned pos)
582 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
585 /* Does the given dimension have a name?
587 int isl_set_has_dim_name(__isl_keep isl_set *set,
588 enum isl_dim_type type, unsigned pos)
590 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
593 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
594 __isl_take isl_basic_map *bmap,
595 enum isl_dim_type type, unsigned pos, const char *s)
597 bmap = isl_basic_map_cow(bmap);
598 if (!bmap)
599 return NULL;
600 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
601 if (!bmap->dim)
602 goto error;
603 return isl_basic_map_finalize(bmap);
604 error:
605 isl_basic_map_free(bmap);
606 return NULL;
609 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
610 enum isl_dim_type type, unsigned pos, const char *s)
612 int i;
614 map = isl_map_cow(map);
615 if (!map)
616 return NULL;
618 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
619 if (!map->dim)
620 goto error;
622 for (i = 0; i < map->n; ++i) {
623 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
624 if (!map->p[i])
625 goto error;
628 return map;
629 error:
630 isl_map_free(map);
631 return NULL;
634 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
635 __isl_take isl_basic_set *bset,
636 enum isl_dim_type type, unsigned pos, const char *s)
638 return (isl_basic_set *)isl_basic_map_set_dim_name(
639 (isl_basic_map *)bset, type, pos, s);
642 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
643 enum isl_dim_type type, unsigned pos, const char *s)
645 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
648 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
649 enum isl_dim_type type, unsigned pos)
651 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
654 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
655 enum isl_dim_type type, unsigned pos)
657 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
660 int isl_map_has_dim_id(__isl_keep isl_map *map,
661 enum isl_dim_type type, unsigned pos)
663 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
666 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
667 enum isl_dim_type type, unsigned pos)
669 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
672 int isl_set_has_dim_id(__isl_keep isl_set *set,
673 enum isl_dim_type type, unsigned pos)
675 return isl_map_has_dim_id(set, type, pos);
678 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
679 enum isl_dim_type type, unsigned pos)
681 return isl_map_get_dim_id(set, type, pos);
684 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
685 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
687 map = isl_map_cow(map);
688 if (!map)
689 goto error;
691 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
693 return isl_map_reset_space(map, isl_space_copy(map->dim));
694 error:
695 isl_id_free(id);
696 return NULL;
699 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
700 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
702 return isl_map_set_dim_id(set, type, pos, id);
705 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
706 __isl_keep isl_id *id)
708 if (!map)
709 return -1;
710 return isl_space_find_dim_by_id(map->dim, type, id);
713 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
714 __isl_keep isl_id *id)
716 return isl_map_find_dim_by_id(set, type, id);
719 /* Return the position of the dimension of the given type and name
720 * in "bmap".
721 * Return -1 if no such dimension can be found.
723 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
724 enum isl_dim_type type, const char *name)
726 if (!bmap)
727 return -1;
728 return isl_space_find_dim_by_name(bmap->dim, type, name);
731 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
732 const char *name)
734 if (!map)
735 return -1;
736 return isl_space_find_dim_by_name(map->dim, type, name);
739 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
740 const char *name)
742 return isl_map_find_dim_by_name(set, type, name);
745 /* Reset the user pointer on all identifiers of parameters and tuples
746 * of the space of "map".
748 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
750 isl_space *space;
752 space = isl_map_get_space(map);
753 space = isl_space_reset_user(space);
754 map = isl_map_reset_space(map, space);
756 return map;
759 /* Reset the user pointer on all identifiers of parameters and tuples
760 * of the space of "set".
762 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
764 return isl_map_reset_user(set);
767 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
769 if (!bmap)
770 return -1;
771 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
774 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
776 return isl_basic_map_is_rational(bset);
779 /* Does "bmap" contain any rational points?
781 * If "bmap" has an equality for each dimension, equating the dimension
782 * to an integer constant, then it has no rational points, even if it
783 * is marked as rational.
785 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
787 int has_rational = 1;
788 unsigned total;
790 if (!bmap)
791 return -1;
792 if (isl_basic_map_plain_is_empty(bmap))
793 return 0;
794 if (!isl_basic_map_is_rational(bmap))
795 return 0;
796 bmap = isl_basic_map_copy(bmap);
797 bmap = isl_basic_map_implicit_equalities(bmap);
798 if (!bmap)
799 return -1;
800 total = isl_basic_map_total_dim(bmap);
801 if (bmap->n_eq == total) {
802 int i, j;
803 for (i = 0; i < bmap->n_eq; ++i) {
804 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
805 if (j < 0)
806 break;
807 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
808 !isl_int_is_negone(bmap->eq[i][1 + j]))
809 break;
810 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
811 total - j - 1);
812 if (j >= 0)
813 break;
815 if (i == bmap->n_eq)
816 has_rational = 0;
818 isl_basic_map_free(bmap);
820 return has_rational;
823 /* Does "map" contain any rational points?
825 int isl_map_has_rational(__isl_keep isl_map *map)
827 int i;
828 int has_rational;
830 if (!map)
831 return -1;
832 for (i = 0; i < map->n; ++i) {
833 has_rational = isl_basic_map_has_rational(map->p[i]);
834 if (has_rational < 0)
835 return -1;
836 if (has_rational)
837 return 1;
839 return 0;
842 /* Does "set" contain any rational points?
844 int isl_set_has_rational(__isl_keep isl_set *set)
846 return isl_map_has_rational(set);
849 /* Is this basic set a parameter domain?
851 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
853 if (!bset)
854 return -1;
855 return isl_space_is_params(bset->dim);
858 /* Is this set a parameter domain?
860 int isl_set_is_params(__isl_keep isl_set *set)
862 if (!set)
863 return -1;
864 return isl_space_is_params(set->dim);
867 /* Is this map actually a parameter domain?
868 * Users should never call this function. Outside of isl,
869 * a map can never be a parameter domain.
871 int isl_map_is_params(__isl_keep isl_map *map)
873 if (!map)
874 return -1;
875 return isl_space_is_params(map->dim);
878 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
879 struct isl_basic_map *bmap, unsigned extra,
880 unsigned n_eq, unsigned n_ineq)
882 int i;
883 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
885 bmap->ctx = ctx;
886 isl_ctx_ref(ctx);
888 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
889 if (isl_blk_is_error(bmap->block))
890 goto error;
892 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
893 if ((n_ineq + n_eq) && !bmap->ineq)
894 goto error;
896 if (extra == 0) {
897 bmap->block2 = isl_blk_empty();
898 bmap->div = NULL;
899 } else {
900 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
901 if (isl_blk_is_error(bmap->block2))
902 goto error;
904 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
905 if (!bmap->div)
906 goto error;
909 for (i = 0; i < n_ineq + n_eq; ++i)
910 bmap->ineq[i] = bmap->block.data + i * row_size;
912 for (i = 0; i < extra; ++i)
913 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
915 bmap->ref = 1;
916 bmap->flags = 0;
917 bmap->c_size = n_eq + n_ineq;
918 bmap->eq = bmap->ineq + n_ineq;
919 bmap->extra = extra;
920 bmap->n_eq = 0;
921 bmap->n_ineq = 0;
922 bmap->n_div = 0;
923 bmap->sample = NULL;
925 return bmap;
926 error:
927 isl_basic_map_free(bmap);
928 return NULL;
931 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
932 unsigned nparam, unsigned dim, unsigned extra,
933 unsigned n_eq, unsigned n_ineq)
935 struct isl_basic_map *bmap;
936 isl_space *space;
938 space = isl_space_set_alloc(ctx, nparam, dim);
939 if (!space)
940 return NULL;
942 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
943 return (struct isl_basic_set *)bmap;
946 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
947 unsigned extra, unsigned n_eq, unsigned n_ineq)
949 struct isl_basic_map *bmap;
950 if (!dim)
951 return NULL;
952 isl_assert(dim->ctx, dim->n_in == 0, goto error);
953 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
954 return (struct isl_basic_set *)bmap;
955 error:
956 isl_space_free(dim);
957 return NULL;
960 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
961 unsigned extra, unsigned n_eq, unsigned n_ineq)
963 struct isl_basic_map *bmap;
965 if (!dim)
966 return NULL;
967 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
968 if (!bmap)
969 goto error;
970 bmap->dim = dim;
972 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
973 error:
974 isl_space_free(dim);
975 return NULL;
978 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
979 unsigned nparam, unsigned in, unsigned out, unsigned extra,
980 unsigned n_eq, unsigned n_ineq)
982 struct isl_basic_map *bmap;
983 isl_space *dim;
985 dim = isl_space_alloc(ctx, nparam, in, out);
986 if (!dim)
987 return NULL;
989 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
990 return bmap;
993 static void dup_constraints(
994 struct isl_basic_map *dst, struct isl_basic_map *src)
996 int i;
997 unsigned total = isl_basic_map_total_dim(src);
999 for (i = 0; i < src->n_eq; ++i) {
1000 int j = isl_basic_map_alloc_equality(dst);
1001 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1004 for (i = 0; i < src->n_ineq; ++i) {
1005 int j = isl_basic_map_alloc_inequality(dst);
1006 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1009 for (i = 0; i < src->n_div; ++i) {
1010 int j = isl_basic_map_alloc_div(dst);
1011 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1013 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1016 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1018 struct isl_basic_map *dup;
1020 if (!bmap)
1021 return NULL;
1022 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1023 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1024 if (!dup)
1025 return NULL;
1026 dup_constraints(dup, bmap);
1027 dup->flags = bmap->flags;
1028 dup->sample = isl_vec_copy(bmap->sample);
1029 return dup;
1032 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1034 struct isl_basic_map *dup;
1036 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1037 return (struct isl_basic_set *)dup;
1040 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1042 if (!bset)
1043 return NULL;
1045 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1046 bset->ref++;
1047 return bset;
1049 return isl_basic_set_dup(bset);
1052 struct isl_set *isl_set_copy(struct isl_set *set)
1054 if (!set)
1055 return NULL;
1057 set->ref++;
1058 return set;
1061 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1063 if (!bmap)
1064 return NULL;
1066 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1067 bmap->ref++;
1068 return bmap;
1070 bmap = isl_basic_map_dup(bmap);
1071 if (bmap)
1072 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1073 return bmap;
1076 struct isl_map *isl_map_copy(struct isl_map *map)
1078 if (!map)
1079 return NULL;
1081 map->ref++;
1082 return map;
1085 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1087 if (!bmap)
1088 return NULL;
1090 if (--bmap->ref > 0)
1091 return NULL;
1093 isl_ctx_deref(bmap->ctx);
1094 free(bmap->div);
1095 isl_blk_free(bmap->ctx, bmap->block2);
1096 free(bmap->ineq);
1097 isl_blk_free(bmap->ctx, bmap->block);
1098 isl_vec_free(bmap->sample);
1099 isl_space_free(bmap->dim);
1100 free(bmap);
1102 return NULL;
1105 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1107 return isl_basic_map_free((struct isl_basic_map *)bset);
1110 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1112 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1115 __isl_give isl_map *isl_map_align_params_map_map_and(
1116 __isl_take isl_map *map1, __isl_take isl_map *map2,
1117 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1118 __isl_take isl_map *map2))
1120 if (!map1 || !map2)
1121 goto error;
1122 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1123 return fn(map1, map2);
1124 if (!isl_space_has_named_params(map1->dim) ||
1125 !isl_space_has_named_params(map2->dim))
1126 isl_die(map1->ctx, isl_error_invalid,
1127 "unaligned unnamed parameters", goto error);
1128 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1129 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1130 return fn(map1, map2);
1131 error:
1132 isl_map_free(map1);
1133 isl_map_free(map2);
1134 return NULL;
1137 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1138 __isl_keep isl_map *map2,
1139 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1141 int r;
1143 if (!map1 || !map2)
1144 return -1;
1145 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1146 return fn(map1, map2);
1147 if (!isl_space_has_named_params(map1->dim) ||
1148 !isl_space_has_named_params(map2->dim))
1149 isl_die(map1->ctx, isl_error_invalid,
1150 "unaligned unnamed parameters", return -1);
1151 map1 = isl_map_copy(map1);
1152 map2 = isl_map_copy(map2);
1153 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1154 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1155 r = fn(map1, map2);
1156 isl_map_free(map1);
1157 isl_map_free(map2);
1158 return r;
1161 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1163 struct isl_ctx *ctx;
1164 if (!bmap)
1165 return -1;
1166 ctx = bmap->ctx;
1167 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1168 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1169 return -1);
1170 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1171 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1172 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1173 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1174 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1175 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1176 isl_int *t;
1177 int j = isl_basic_map_alloc_inequality(bmap);
1178 if (j < 0)
1179 return -1;
1180 t = bmap->ineq[j];
1181 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1182 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1183 bmap->eq[-1] = t;
1184 bmap->n_eq++;
1185 bmap->n_ineq--;
1186 bmap->eq--;
1187 return 0;
1189 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1190 bmap->extra - bmap->n_div);
1191 return bmap->n_eq++;
1194 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1196 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1199 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1201 if (!bmap)
1202 return -1;
1203 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1204 bmap->n_eq -= n;
1205 return 0;
1208 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1210 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1213 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1215 isl_int *t;
1216 if (!bmap)
1217 return -1;
1218 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1220 if (pos != bmap->n_eq - 1) {
1221 t = bmap->eq[pos];
1222 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1223 bmap->eq[bmap->n_eq - 1] = t;
1225 bmap->n_eq--;
1226 return 0;
1229 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1231 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1234 /* Turn inequality "pos" of "bmap" into an equality.
1236 * In particular, we move the inequality in front of the equalities
1237 * and move the last inequality in the position of the moved inequality.
1238 * Note that isl_tab_make_equalities_explicit depends on this particular
1239 * change in the ordering of the constraints.
1241 void isl_basic_map_inequality_to_equality(
1242 struct isl_basic_map *bmap, unsigned pos)
1244 isl_int *t;
1246 t = bmap->ineq[pos];
1247 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1248 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1249 bmap->eq[-1] = t;
1250 bmap->n_eq++;
1251 bmap->n_ineq--;
1252 bmap->eq--;
1253 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1254 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1255 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1256 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1259 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1261 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1264 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1266 struct isl_ctx *ctx;
1267 if (!bmap)
1268 return -1;
1269 ctx = bmap->ctx;
1270 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1272 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1273 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1274 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1275 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1276 1 + isl_basic_map_total_dim(bmap),
1277 bmap->extra - bmap->n_div);
1278 return bmap->n_ineq++;
1281 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1283 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1286 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1288 if (!bmap)
1289 return -1;
1290 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1291 bmap->n_ineq -= n;
1292 return 0;
1295 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1297 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1300 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1302 isl_int *t;
1303 if (!bmap)
1304 return -1;
1305 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1307 if (pos != bmap->n_ineq - 1) {
1308 t = bmap->ineq[pos];
1309 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1310 bmap->ineq[bmap->n_ineq - 1] = t;
1311 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1313 bmap->n_ineq--;
1314 return 0;
1317 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1319 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1322 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1323 isl_int *eq)
1325 int k;
1327 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1328 if (!bmap)
1329 return NULL;
1330 k = isl_basic_map_alloc_equality(bmap);
1331 if (k < 0)
1332 goto error;
1333 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1334 return bmap;
1335 error:
1336 isl_basic_map_free(bmap);
1337 return NULL;
1340 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1341 isl_int *eq)
1343 return (isl_basic_set *)
1344 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1347 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1348 isl_int *ineq)
1350 int k;
1352 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1353 if (!bmap)
1354 return NULL;
1355 k = isl_basic_map_alloc_inequality(bmap);
1356 if (k < 0)
1357 goto error;
1358 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1359 return bmap;
1360 error:
1361 isl_basic_map_free(bmap);
1362 return NULL;
1365 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1366 isl_int *ineq)
1368 return (isl_basic_set *)
1369 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1372 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1374 if (!bmap)
1375 return -1;
1376 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1377 isl_seq_clr(bmap->div[bmap->n_div] +
1378 1 + 1 + isl_basic_map_total_dim(bmap),
1379 bmap->extra - bmap->n_div);
1380 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1381 return bmap->n_div++;
1384 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1386 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1389 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1391 if (!bmap)
1392 return -1;
1393 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1394 bmap->n_div -= n;
1395 return 0;
1398 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1400 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1403 /* Copy constraint from src to dst, putting the vars of src at offset
1404 * dim_off in dst and the divs of src at offset div_off in dst.
1405 * If both sets are actually map, then dim_off applies to the input
1406 * variables.
1408 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1409 struct isl_basic_map *src_map, isl_int *src,
1410 unsigned in_off, unsigned out_off, unsigned div_off)
1412 unsigned src_nparam = isl_basic_map_n_param(src_map);
1413 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1414 unsigned src_in = isl_basic_map_n_in(src_map);
1415 unsigned dst_in = isl_basic_map_n_in(dst_map);
1416 unsigned src_out = isl_basic_map_n_out(src_map);
1417 unsigned dst_out = isl_basic_map_n_out(dst_map);
1418 isl_int_set(dst[0], src[0]);
1419 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1420 if (dst_nparam > src_nparam)
1421 isl_seq_clr(dst+1+src_nparam,
1422 dst_nparam - src_nparam);
1423 isl_seq_clr(dst+1+dst_nparam, in_off);
1424 isl_seq_cpy(dst+1+dst_nparam+in_off,
1425 src+1+src_nparam,
1426 isl_min(dst_in-in_off, src_in));
1427 if (dst_in-in_off > src_in)
1428 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1429 dst_in - in_off - src_in);
1430 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1431 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1432 src+1+src_nparam+src_in,
1433 isl_min(dst_out-out_off, src_out));
1434 if (dst_out-out_off > src_out)
1435 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1436 dst_out - out_off - src_out);
1437 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1438 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1439 src+1+src_nparam+src_in+src_out,
1440 isl_min(dst_map->extra-div_off, src_map->n_div));
1441 if (dst_map->n_div-div_off > src_map->n_div)
1442 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1443 div_off+src_map->n_div,
1444 dst_map->n_div - div_off - src_map->n_div);
1447 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1448 struct isl_basic_map *src_map, isl_int *src,
1449 unsigned in_off, unsigned out_off, unsigned div_off)
1451 isl_int_set(dst[0], src[0]);
1452 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1455 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1456 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1458 int i;
1459 unsigned div_off;
1461 if (!bmap1 || !bmap2)
1462 goto error;
1464 div_off = bmap1->n_div;
1466 for (i = 0; i < bmap2->n_eq; ++i) {
1467 int i1 = isl_basic_map_alloc_equality(bmap1);
1468 if (i1 < 0)
1469 goto error;
1470 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1471 i_pos, o_pos, div_off);
1474 for (i = 0; i < bmap2->n_ineq; ++i) {
1475 int i1 = isl_basic_map_alloc_inequality(bmap1);
1476 if (i1 < 0)
1477 goto error;
1478 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1479 i_pos, o_pos, div_off);
1482 for (i = 0; i < bmap2->n_div; ++i) {
1483 int i1 = isl_basic_map_alloc_div(bmap1);
1484 if (i1 < 0)
1485 goto error;
1486 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1487 i_pos, o_pos, div_off);
1490 isl_basic_map_free(bmap2);
1492 return bmap1;
1494 error:
1495 isl_basic_map_free(bmap1);
1496 isl_basic_map_free(bmap2);
1497 return NULL;
1500 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1501 struct isl_basic_set *bset2, unsigned pos)
1503 return (struct isl_basic_set *)
1504 add_constraints((struct isl_basic_map *)bset1,
1505 (struct isl_basic_map *)bset2, 0, pos);
1508 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1509 __isl_take isl_space *dim, unsigned extra,
1510 unsigned n_eq, unsigned n_ineq)
1512 struct isl_basic_map *ext;
1513 unsigned flags;
1514 int dims_ok;
1516 if (!dim)
1517 goto error;
1519 if (!base)
1520 goto error;
1522 dims_ok = isl_space_is_equal(base->dim, dim) &&
1523 base->extra >= base->n_div + extra;
1525 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1526 room_for_ineq(base, n_ineq)) {
1527 isl_space_free(dim);
1528 return base;
1531 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1532 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1533 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1534 extra += base->extra;
1535 n_eq += base->n_eq;
1536 n_ineq += base->n_ineq;
1538 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1539 dim = NULL;
1540 if (!ext)
1541 goto error;
1543 if (dims_ok)
1544 ext->sample = isl_vec_copy(base->sample);
1545 flags = base->flags;
1546 ext = add_constraints(ext, base, 0, 0);
1547 if (ext) {
1548 ext->flags = flags;
1549 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1552 return ext;
1554 error:
1555 isl_space_free(dim);
1556 isl_basic_map_free(base);
1557 return NULL;
1560 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1561 __isl_take isl_space *dim, unsigned extra,
1562 unsigned n_eq, unsigned n_ineq)
1564 return (struct isl_basic_set *)
1565 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1566 extra, n_eq, n_ineq);
1569 struct isl_basic_map *isl_basic_map_extend_constraints(
1570 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1572 if (!base)
1573 return NULL;
1574 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1575 0, n_eq, n_ineq);
1578 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1579 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1580 unsigned n_eq, unsigned n_ineq)
1582 struct isl_basic_map *bmap;
1583 isl_space *dim;
1585 if (!base)
1586 return NULL;
1587 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1588 if (!dim)
1589 goto error;
1591 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1592 return bmap;
1593 error:
1594 isl_basic_map_free(base);
1595 return NULL;
1598 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1599 unsigned nparam, unsigned dim, unsigned extra,
1600 unsigned n_eq, unsigned n_ineq)
1602 return (struct isl_basic_set *)
1603 isl_basic_map_extend((struct isl_basic_map *)base,
1604 nparam, 0, dim, extra, n_eq, n_ineq);
1607 struct isl_basic_set *isl_basic_set_extend_constraints(
1608 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1610 return (struct isl_basic_set *)
1611 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1612 n_eq, n_ineq);
1615 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1617 return (struct isl_basic_set *)
1618 isl_basic_map_cow((struct isl_basic_map *)bset);
1621 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1623 if (!bmap)
1624 return NULL;
1626 if (bmap->ref > 1) {
1627 bmap->ref--;
1628 bmap = isl_basic_map_dup(bmap);
1630 if (bmap) {
1631 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1632 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1634 return bmap;
1637 struct isl_set *isl_set_cow(struct isl_set *set)
1639 if (!set)
1640 return NULL;
1642 if (set->ref == 1)
1643 return set;
1644 set->ref--;
1645 return isl_set_dup(set);
1648 struct isl_map *isl_map_cow(struct isl_map *map)
1650 if (!map)
1651 return NULL;
1653 if (map->ref == 1)
1654 return map;
1655 map->ref--;
1656 return isl_map_dup(map);
1659 static void swap_vars(struct isl_blk blk, isl_int *a,
1660 unsigned a_len, unsigned b_len)
1662 isl_seq_cpy(blk.data, a+a_len, b_len);
1663 isl_seq_cpy(blk.data+b_len, a, a_len);
1664 isl_seq_cpy(a, blk.data, b_len+a_len);
1667 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1668 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1670 int i;
1671 struct isl_blk blk;
1673 if (!bmap)
1674 goto error;
1676 isl_assert(bmap->ctx,
1677 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1679 if (n1 == 0 || n2 == 0)
1680 return bmap;
1682 bmap = isl_basic_map_cow(bmap);
1683 if (!bmap)
1684 return NULL;
1686 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1687 if (isl_blk_is_error(blk))
1688 goto error;
1690 for (i = 0; i < bmap->n_eq; ++i)
1691 swap_vars(blk,
1692 bmap->eq[i] + pos, n1, n2);
1694 for (i = 0; i < bmap->n_ineq; ++i)
1695 swap_vars(blk,
1696 bmap->ineq[i] + pos, n1, n2);
1698 for (i = 0; i < bmap->n_div; ++i)
1699 swap_vars(blk,
1700 bmap->div[i]+1 + pos, n1, n2);
1702 isl_blk_free(bmap->ctx, blk);
1704 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1705 bmap = isl_basic_map_gauss(bmap, NULL);
1706 return isl_basic_map_finalize(bmap);
1707 error:
1708 isl_basic_map_free(bmap);
1709 return NULL;
1712 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1713 __isl_take isl_basic_set *bset, unsigned n)
1715 unsigned dim;
1716 unsigned nparam;
1718 if (!bset)
1719 return NULL;
1721 nparam = isl_basic_set_n_param(bset);
1722 dim = isl_basic_set_n_dim(bset);
1723 isl_assert(bset->ctx, n <= dim, goto error);
1725 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1726 error:
1727 isl_basic_set_free(bset);
1728 return NULL;
1731 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1733 int i = 0;
1734 unsigned total;
1735 if (!bmap)
1736 goto error;
1737 total = isl_basic_map_total_dim(bmap);
1738 isl_basic_map_free_div(bmap, bmap->n_div);
1739 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1740 if (bmap->n_eq > 0)
1741 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1742 else {
1743 i = isl_basic_map_alloc_equality(bmap);
1744 if (i < 0)
1745 goto error;
1747 isl_int_set_si(bmap->eq[i][0], 1);
1748 isl_seq_clr(bmap->eq[i]+1, total);
1749 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1750 isl_vec_free(bmap->sample);
1751 bmap->sample = NULL;
1752 return isl_basic_map_finalize(bmap);
1753 error:
1754 isl_basic_map_free(bmap);
1755 return NULL;
1758 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1760 return (struct isl_basic_set *)
1761 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1764 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1765 * of "bmap").
1767 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1769 isl_int *t = bmap->div[a];
1770 bmap->div[a] = bmap->div[b];
1771 bmap->div[b] = t;
1774 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1775 * div definitions accordingly.
1777 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1779 int i;
1780 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1782 swap_div(bmap, a, b);
1784 for (i = 0; i < bmap->n_eq; ++i)
1785 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1787 for (i = 0; i < bmap->n_ineq; ++i)
1788 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1790 for (i = 0; i < bmap->n_div; ++i)
1791 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1792 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1795 /* Eliminate the specified n dimensions starting at first from the
1796 * constraints, without removing the dimensions from the space.
1797 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1799 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1800 enum isl_dim_type type, unsigned first, unsigned n)
1802 int i;
1804 if (!map)
1805 return NULL;
1806 if (n == 0)
1807 return map;
1809 if (first + n > isl_map_dim(map, type) || first + n < first)
1810 isl_die(map->ctx, isl_error_invalid,
1811 "index out of bounds", goto error);
1813 map = isl_map_cow(map);
1814 if (!map)
1815 return NULL;
1817 for (i = 0; i < map->n; ++i) {
1818 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1819 if (!map->p[i])
1820 goto error;
1822 return map;
1823 error:
1824 isl_map_free(map);
1825 return NULL;
1828 /* Eliminate the specified n dimensions starting at first from the
1829 * constraints, without removing the dimensions from the space.
1830 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1832 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1833 enum isl_dim_type type, unsigned first, unsigned n)
1835 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1838 /* Eliminate the specified n dimensions starting at first from the
1839 * constraints, without removing the dimensions from the space.
1840 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1842 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1843 unsigned first, unsigned n)
1845 return isl_set_eliminate(set, isl_dim_set, first, n);
1848 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1849 __isl_take isl_basic_map *bmap)
1851 if (!bmap)
1852 return NULL;
1853 bmap = isl_basic_map_eliminate_vars(bmap,
1854 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1855 if (!bmap)
1856 return NULL;
1857 bmap->n_div = 0;
1858 return isl_basic_map_finalize(bmap);
1861 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1862 __isl_take isl_basic_set *bset)
1864 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1865 (struct isl_basic_map *)bset);
1868 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1870 int i;
1872 if (!map)
1873 return NULL;
1874 if (map->n == 0)
1875 return map;
1877 map = isl_map_cow(map);
1878 if (!map)
1879 return NULL;
1881 for (i = 0; i < map->n; ++i) {
1882 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1883 if (!map->p[i])
1884 goto error;
1886 return map;
1887 error:
1888 isl_map_free(map);
1889 return NULL;
1892 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1894 return isl_map_remove_divs(set);
1897 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1898 enum isl_dim_type type, unsigned first, unsigned n)
1900 if (!bmap)
1901 return NULL;
1902 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1903 goto error);
1904 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1905 return bmap;
1906 bmap = isl_basic_map_eliminate_vars(bmap,
1907 isl_basic_map_offset(bmap, type) - 1 + first, n);
1908 if (!bmap)
1909 return bmap;
1910 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1911 return bmap;
1912 bmap = isl_basic_map_drop(bmap, type, first, n);
1913 return bmap;
1914 error:
1915 isl_basic_map_free(bmap);
1916 return NULL;
1919 /* Return true if the definition of the given div (recursively) involves
1920 * any of the given variables.
1922 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1923 unsigned first, unsigned n)
1925 int i;
1926 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1928 if (isl_int_is_zero(bmap->div[div][0]))
1929 return 0;
1930 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1931 return 1;
1933 for (i = bmap->n_div - 1; i >= 0; --i) {
1934 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1935 continue;
1936 if (div_involves_vars(bmap, i, first, n))
1937 return 1;
1940 return 0;
1943 /* Try and add a lower and/or upper bound on "div" to "bmap"
1944 * based on inequality "i".
1945 * "total" is the total number of variables (excluding the divs).
1946 * "v" is a temporary object that can be used during the calculations.
1947 * If "lb" is set, then a lower bound should be constructed.
1948 * If "ub" is set, then an upper bound should be constructed.
1950 * The calling function has already checked that the inequality does not
1951 * reference "div", but we still need to check that the inequality is
1952 * of the right form. We'll consider the case where we want to construct
1953 * a lower bound. The construction of upper bounds is similar.
1955 * Let "div" be of the form
1957 * q = floor((a + f(x))/d)
1959 * We essentially check if constraint "i" is of the form
1961 * b + f(x) >= 0
1963 * so that we can use it to derive a lower bound on "div".
1964 * However, we allow a slightly more general form
1966 * b + g(x) >= 0
1968 * with the condition that the coefficients of g(x) - f(x) are all
1969 * divisible by d.
1970 * Rewriting this constraint as
1972 * 0 >= -b - g(x)
1974 * adding a + f(x) to both sides and dividing by d, we obtain
1976 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1978 * Taking the floor on both sides, we obtain
1980 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1982 * or
1984 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1986 * In the case of an upper bound, we construct the constraint
1988 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1991 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1992 __isl_take isl_basic_map *bmap, int div, int i,
1993 unsigned total, isl_int v, int lb, int ub)
1995 int j;
1997 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1998 if (lb) {
1999 isl_int_sub(v, bmap->ineq[i][1 + j],
2000 bmap->div[div][1 + 1 + j]);
2001 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2003 if (ub) {
2004 isl_int_add(v, bmap->ineq[i][1 + j],
2005 bmap->div[div][1 + 1 + j]);
2006 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2009 if (!lb && !ub)
2010 return bmap;
2012 bmap = isl_basic_map_cow(bmap);
2013 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2014 if (lb) {
2015 int k = isl_basic_map_alloc_inequality(bmap);
2016 if (k < 0)
2017 goto error;
2018 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2019 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2020 bmap->div[div][1 + j]);
2021 isl_int_cdiv_q(bmap->ineq[k][j],
2022 bmap->ineq[k][j], bmap->div[div][0]);
2024 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2026 if (ub) {
2027 int k = isl_basic_map_alloc_inequality(bmap);
2028 if (k < 0)
2029 goto error;
2030 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2031 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2032 bmap->div[div][1 + j]);
2033 isl_int_fdiv_q(bmap->ineq[k][j],
2034 bmap->ineq[k][j], bmap->div[div][0]);
2036 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2039 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2040 return bmap;
2041 error:
2042 isl_basic_map_free(bmap);
2043 return NULL;
2046 /* This function is called right before "div" is eliminated from "bmap"
2047 * using Fourier-Motzkin.
2048 * Look through the constraints of "bmap" for constraints on the argument
2049 * of the integer division and use them to construct constraints on the
2050 * integer division itself. These constraints can then be combined
2051 * during the Fourier-Motzkin elimination.
2052 * Note that it is only useful to introduce lower bounds on "div"
2053 * if "bmap" already contains upper bounds on "div" as the newly
2054 * introduce lower bounds can then be combined with the pre-existing
2055 * upper bounds. Similarly for upper bounds.
2056 * We therefore first check if "bmap" contains any lower and/or upper bounds
2057 * on "div".
2059 * It is interesting to note that the introduction of these constraints
2060 * can indeed lead to more accurate results, even when compared to
2061 * deriving constraints on the argument of "div" from constraints on "div".
2062 * Consider, for example, the set
2064 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2066 * The second constraint can be rewritten as
2068 * 2 * [(-i-2j+3)/4] + k >= 0
2070 * from which we can derive
2072 * -i - 2j + 3 >= -2k
2074 * or
2076 * i + 2j <= 3 + 2k
2078 * Combined with the first constraint, we obtain
2080 * -3 <= 3 + 2k or k >= -3
2082 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2083 * the first constraint, we obtain
2085 * [(i + 2j)/4] >= [-3/4] = -1
2087 * Combining this constraint with the second constraint, we obtain
2089 * k >= -2
2091 static __isl_give isl_basic_map *insert_bounds_on_div(
2092 __isl_take isl_basic_map *bmap, int div)
2094 int i;
2095 int check_lb, check_ub;
2096 isl_int v;
2097 unsigned total;
2099 if (!bmap)
2100 return NULL;
2102 if (isl_int_is_zero(bmap->div[div][0]))
2103 return bmap;
2105 total = isl_space_dim(bmap->dim, isl_dim_all);
2107 check_lb = 0;
2108 check_ub = 0;
2109 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2110 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2111 if (s > 0)
2112 check_ub = 1;
2113 if (s < 0)
2114 check_lb = 1;
2117 if (!check_lb && !check_ub)
2118 return bmap;
2120 isl_int_init(v);
2122 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2123 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2124 continue;
2126 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2127 check_lb, check_ub);
2130 isl_int_clear(v);
2132 return bmap;
2135 /* Remove all divs (recursively) involving any of the given dimensions
2136 * in their definitions.
2138 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2139 __isl_take isl_basic_map *bmap,
2140 enum isl_dim_type type, unsigned first, unsigned n)
2142 int i;
2144 if (!bmap)
2145 return NULL;
2146 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2147 goto error);
2148 first += isl_basic_map_offset(bmap, type);
2150 for (i = bmap->n_div - 1; i >= 0; --i) {
2151 if (!div_involves_vars(bmap, i, first, n))
2152 continue;
2153 bmap = insert_bounds_on_div(bmap, i);
2154 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2155 if (!bmap)
2156 return NULL;
2157 i = bmap->n_div;
2160 return bmap;
2161 error:
2162 isl_basic_map_free(bmap);
2163 return NULL;
2166 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2167 __isl_take isl_basic_set *bset,
2168 enum isl_dim_type type, unsigned first, unsigned n)
2170 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2173 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2174 enum isl_dim_type type, unsigned first, unsigned n)
2176 int i;
2178 if (!map)
2179 return NULL;
2180 if (map->n == 0)
2181 return map;
2183 map = isl_map_cow(map);
2184 if (!map)
2185 return NULL;
2187 for (i = 0; i < map->n; ++i) {
2188 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2189 type, first, n);
2190 if (!map->p[i])
2191 goto error;
2193 return map;
2194 error:
2195 isl_map_free(map);
2196 return NULL;
2199 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2200 enum isl_dim_type type, unsigned first, unsigned n)
2202 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2203 type, first, n);
2206 /* Does the desciption of "bmap" depend on the specified dimensions?
2207 * We also check whether the dimensions appear in any of the div definitions.
2208 * In principle there is no need for this check. If the dimensions appear
2209 * in a div definition, they also appear in the defining constraints of that
2210 * div.
2212 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2213 enum isl_dim_type type, unsigned first, unsigned n)
2215 int i;
2217 if (!bmap)
2218 return -1;
2220 if (first + n > isl_basic_map_dim(bmap, type))
2221 isl_die(bmap->ctx, isl_error_invalid,
2222 "index out of bounds", return -1);
2224 first += isl_basic_map_offset(bmap, type);
2225 for (i = 0; i < bmap->n_eq; ++i)
2226 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2227 return 1;
2228 for (i = 0; i < bmap->n_ineq; ++i)
2229 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2230 return 1;
2231 for (i = 0; i < bmap->n_div; ++i) {
2232 if (isl_int_is_zero(bmap->div[i][0]))
2233 continue;
2234 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2235 return 1;
2238 return 0;
2241 int isl_map_involves_dims(__isl_keep isl_map *map,
2242 enum isl_dim_type type, unsigned first, unsigned n)
2244 int i;
2246 if (!map)
2247 return -1;
2249 if (first + n > isl_map_dim(map, type))
2250 isl_die(map->ctx, isl_error_invalid,
2251 "index out of bounds", return -1);
2253 for (i = 0; i < map->n; ++i) {
2254 int involves = isl_basic_map_involves_dims(map->p[i],
2255 type, first, n);
2256 if (involves < 0 || involves)
2257 return involves;
2260 return 0;
2263 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2264 enum isl_dim_type type, unsigned first, unsigned n)
2266 return isl_basic_map_involves_dims(bset, type, first, n);
2269 int isl_set_involves_dims(__isl_keep isl_set *set,
2270 enum isl_dim_type type, unsigned first, unsigned n)
2272 return isl_map_involves_dims(set, type, first, n);
2275 /* Return true if the definition of the given div is unknown or depends
2276 * on unknown divs.
2278 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2280 int i;
2281 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2283 if (isl_int_is_zero(bmap->div[div][0]))
2284 return 1;
2286 for (i = bmap->n_div - 1; i >= 0; --i) {
2287 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2288 continue;
2289 if (div_is_unknown(bmap, i))
2290 return 1;
2293 return 0;
2296 /* Remove all divs that are unknown or defined in terms of unknown divs.
2298 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2299 __isl_take isl_basic_map *bmap)
2301 int i;
2303 if (!bmap)
2304 return NULL;
2306 for (i = bmap->n_div - 1; i >= 0; --i) {
2307 if (!div_is_unknown(bmap, i))
2308 continue;
2309 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2310 if (!bmap)
2311 return NULL;
2312 i = bmap->n_div;
2315 return bmap;
2318 /* Remove all divs that are unknown or defined in terms of unknown divs.
2320 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2321 __isl_take isl_basic_set *bset)
2323 return isl_basic_map_remove_unknown_divs(bset);
2326 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2328 int i;
2330 if (!map)
2331 return NULL;
2332 if (map->n == 0)
2333 return map;
2335 map = isl_map_cow(map);
2336 if (!map)
2337 return NULL;
2339 for (i = 0; i < map->n; ++i) {
2340 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2341 if (!map->p[i])
2342 goto error;
2344 return map;
2345 error:
2346 isl_map_free(map);
2347 return NULL;
2350 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2352 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2355 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2356 __isl_take isl_basic_set *bset,
2357 enum isl_dim_type type, unsigned first, unsigned n)
2359 return (isl_basic_set *)
2360 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2363 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2364 enum isl_dim_type type, unsigned first, unsigned n)
2366 int i;
2368 if (n == 0)
2369 return map;
2371 map = isl_map_cow(map);
2372 if (!map)
2373 return NULL;
2374 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2376 for (i = 0; i < map->n; ++i) {
2377 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2378 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2379 if (!map->p[i])
2380 goto error;
2382 map = isl_map_drop(map, type, first, n);
2383 return map;
2384 error:
2385 isl_map_free(map);
2386 return NULL;
2389 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2390 enum isl_dim_type type, unsigned first, unsigned n)
2392 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2395 /* Project out n inputs starting at first using Fourier-Motzkin */
2396 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2397 unsigned first, unsigned n)
2399 return isl_map_remove_dims(map, isl_dim_in, first, n);
2402 static void dump_term(struct isl_basic_map *bmap,
2403 isl_int c, int pos, FILE *out)
2405 const char *name;
2406 unsigned in = isl_basic_map_n_in(bmap);
2407 unsigned dim = in + isl_basic_map_n_out(bmap);
2408 unsigned nparam = isl_basic_map_n_param(bmap);
2409 if (!pos)
2410 isl_int_print(out, c, 0);
2411 else {
2412 if (!isl_int_is_one(c))
2413 isl_int_print(out, c, 0);
2414 if (pos < 1 + nparam) {
2415 name = isl_space_get_dim_name(bmap->dim,
2416 isl_dim_param, pos - 1);
2417 if (name)
2418 fprintf(out, "%s", name);
2419 else
2420 fprintf(out, "p%d", pos - 1);
2421 } else if (pos < 1 + nparam + in)
2422 fprintf(out, "i%d", pos - 1 - nparam);
2423 else if (pos < 1 + nparam + dim)
2424 fprintf(out, "o%d", pos - 1 - nparam - in);
2425 else
2426 fprintf(out, "e%d", pos - 1 - nparam - dim);
2430 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2431 int sign, FILE *out)
2433 int i;
2434 int first;
2435 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2436 isl_int v;
2438 isl_int_init(v);
2439 for (i = 0, first = 1; i < len; ++i) {
2440 if (isl_int_sgn(c[i]) * sign <= 0)
2441 continue;
2442 if (!first)
2443 fprintf(out, " + ");
2444 first = 0;
2445 isl_int_abs(v, c[i]);
2446 dump_term(bmap, v, i, out);
2448 isl_int_clear(v);
2449 if (first)
2450 fprintf(out, "0");
2453 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2454 const char *op, FILE *out, int indent)
2456 int i;
2458 fprintf(out, "%*s", indent, "");
2460 dump_constraint_sign(bmap, c, 1, out);
2461 fprintf(out, " %s ", op);
2462 dump_constraint_sign(bmap, c, -1, out);
2464 fprintf(out, "\n");
2466 for (i = bmap->n_div; i < bmap->extra; ++i) {
2467 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2468 continue;
2469 fprintf(out, "%*s", indent, "");
2470 fprintf(out, "ERROR: unused div coefficient not zero\n");
2471 abort();
2475 static void dump_constraints(struct isl_basic_map *bmap,
2476 isl_int **c, unsigned n,
2477 const char *op, FILE *out, int indent)
2479 int i;
2481 for (i = 0; i < n; ++i)
2482 dump_constraint(bmap, c[i], op, out, indent);
2485 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2487 int j;
2488 int first = 1;
2489 unsigned total = isl_basic_map_total_dim(bmap);
2491 for (j = 0; j < 1 + total; ++j) {
2492 if (isl_int_is_zero(exp[j]))
2493 continue;
2494 if (!first && isl_int_is_pos(exp[j]))
2495 fprintf(out, "+");
2496 dump_term(bmap, exp[j], j, out);
2497 first = 0;
2501 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2503 int i;
2505 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2506 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2508 for (i = 0; i < bmap->n_div; ++i) {
2509 fprintf(out, "%*s", indent, "");
2510 fprintf(out, "e%d = [(", i);
2511 dump_affine(bmap, bmap->div[i]+1, out);
2512 fprintf(out, ")/");
2513 isl_int_print(out, bmap->div[i][0], 0);
2514 fprintf(out, "]\n");
2518 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2519 FILE *out, int indent)
2521 if (!bset) {
2522 fprintf(out, "null basic set\n");
2523 return;
2526 fprintf(out, "%*s", indent, "");
2527 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2528 bset->ref, bset->dim->nparam, bset->dim->n_out,
2529 bset->extra, bset->flags);
2530 dump((struct isl_basic_map *)bset, out, indent);
2533 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2534 FILE *out, int indent)
2536 if (!bmap) {
2537 fprintf(out, "null basic map\n");
2538 return;
2541 fprintf(out, "%*s", indent, "");
2542 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2543 "flags: %x, n_name: %d\n",
2544 bmap->ref,
2545 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2546 bmap->extra, bmap->flags, bmap->dim->n_id);
2547 dump(bmap, out, indent);
2550 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2552 unsigned total;
2553 if (!bmap)
2554 return -1;
2555 total = isl_basic_map_total_dim(bmap);
2556 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2557 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2558 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2559 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2560 return 0;
2563 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2564 unsigned flags)
2566 struct isl_set *set;
2568 if (!dim)
2569 return NULL;
2570 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2571 isl_assert(dim->ctx, n >= 0, goto error);
2572 set = isl_alloc(dim->ctx, struct isl_set,
2573 sizeof(struct isl_set) +
2574 (n - 1) * sizeof(struct isl_basic_set *));
2575 if (!set)
2576 goto error;
2578 set->ctx = dim->ctx;
2579 isl_ctx_ref(set->ctx);
2580 set->ref = 1;
2581 set->size = n;
2582 set->n = 0;
2583 set->dim = dim;
2584 set->flags = flags;
2585 return set;
2586 error:
2587 isl_space_free(dim);
2588 return NULL;
2591 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2592 unsigned nparam, unsigned dim, int n, unsigned flags)
2594 struct isl_set *set;
2595 isl_space *dims;
2597 dims = isl_space_alloc(ctx, nparam, 0, dim);
2598 if (!dims)
2599 return NULL;
2601 set = isl_set_alloc_space(dims, n, flags);
2602 return set;
2605 /* Make sure "map" has room for at least "n" more basic maps.
2607 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2609 int i;
2610 struct isl_map *grown = NULL;
2612 if (!map)
2613 return NULL;
2614 isl_assert(map->ctx, n >= 0, goto error);
2615 if (map->n + n <= map->size)
2616 return map;
2617 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2618 if (!grown)
2619 goto error;
2620 for (i = 0; i < map->n; ++i) {
2621 grown->p[i] = isl_basic_map_copy(map->p[i]);
2622 if (!grown->p[i])
2623 goto error;
2624 grown->n++;
2626 isl_map_free(map);
2627 return grown;
2628 error:
2629 isl_map_free(grown);
2630 isl_map_free(map);
2631 return NULL;
2634 /* Make sure "set" has room for at least "n" more basic sets.
2636 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2638 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2641 struct isl_set *isl_set_dup(struct isl_set *set)
2643 int i;
2644 struct isl_set *dup;
2646 if (!set)
2647 return NULL;
2649 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2650 if (!dup)
2651 return NULL;
2652 for (i = 0; i < set->n; ++i)
2653 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2654 return dup;
2657 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2659 return isl_map_from_basic_map(bset);
2662 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2664 struct isl_map *map;
2666 if (!bmap)
2667 return NULL;
2669 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2670 return isl_map_add_basic_map(map, bmap);
2673 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2674 __isl_take isl_basic_set *bset)
2676 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2677 (struct isl_basic_map *)bset);
2680 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2682 int i;
2684 if (!set)
2685 return NULL;
2687 if (--set->ref > 0)
2688 return NULL;
2690 isl_ctx_deref(set->ctx);
2691 for (i = 0; i < set->n; ++i)
2692 isl_basic_set_free(set->p[i]);
2693 isl_space_free(set->dim);
2694 free(set);
2696 return NULL;
2699 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2701 int i;
2703 if (!set) {
2704 fprintf(out, "null set\n");
2705 return;
2708 fprintf(out, "%*s", indent, "");
2709 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2710 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2711 set->flags);
2712 for (i = 0; i < set->n; ++i) {
2713 fprintf(out, "%*s", indent, "");
2714 fprintf(out, "basic set %d:\n", i);
2715 isl_basic_set_print_internal(set->p[i], out, indent+4);
2719 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2721 int i;
2723 if (!map) {
2724 fprintf(out, "null map\n");
2725 return;
2728 fprintf(out, "%*s", indent, "");
2729 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2730 "flags: %x, n_name: %d\n",
2731 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2732 map->dim->n_out, map->flags, map->dim->n_id);
2733 for (i = 0; i < map->n; ++i) {
2734 fprintf(out, "%*s", indent, "");
2735 fprintf(out, "basic map %d:\n", i);
2736 isl_basic_map_print_internal(map->p[i], out, indent+4);
2740 struct isl_basic_map *isl_basic_map_intersect_domain(
2741 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2743 struct isl_basic_map *bmap_domain;
2745 if (!bmap || !bset)
2746 goto error;
2748 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2749 bset->dim, isl_dim_param), goto error);
2751 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2752 isl_assert(bset->ctx,
2753 isl_basic_map_compatible_domain(bmap, bset), goto error);
2755 bmap = isl_basic_map_cow(bmap);
2756 if (!bmap)
2757 goto error;
2758 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2759 bset->n_div, bset->n_eq, bset->n_ineq);
2760 bmap_domain = isl_basic_map_from_domain(bset);
2761 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2763 bmap = isl_basic_map_simplify(bmap);
2764 return isl_basic_map_finalize(bmap);
2765 error:
2766 isl_basic_map_free(bmap);
2767 isl_basic_set_free(bset);
2768 return NULL;
2771 struct isl_basic_map *isl_basic_map_intersect_range(
2772 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2774 struct isl_basic_map *bmap_range;
2776 if (!bmap || !bset)
2777 goto error;
2779 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2780 bset->dim, isl_dim_param), goto error);
2782 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2783 isl_assert(bset->ctx,
2784 isl_basic_map_compatible_range(bmap, bset), goto error);
2786 if (isl_basic_set_is_universe(bset)) {
2787 isl_basic_set_free(bset);
2788 return bmap;
2791 bmap = isl_basic_map_cow(bmap);
2792 if (!bmap)
2793 goto error;
2794 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2795 bset->n_div, bset->n_eq, bset->n_ineq);
2796 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2797 bmap = add_constraints(bmap, bmap_range, 0, 0);
2799 bmap = isl_basic_map_simplify(bmap);
2800 return isl_basic_map_finalize(bmap);
2801 error:
2802 isl_basic_map_free(bmap);
2803 isl_basic_set_free(bset);
2804 return NULL;
2807 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2809 int i;
2810 unsigned total;
2811 isl_int s;
2813 if (!bmap || !vec)
2814 return -1;
2816 total = 1 + isl_basic_map_total_dim(bmap);
2817 if (total != vec->size)
2818 return -1;
2820 isl_int_init(s);
2822 for (i = 0; i < bmap->n_eq; ++i) {
2823 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2824 if (!isl_int_is_zero(s)) {
2825 isl_int_clear(s);
2826 return 0;
2830 for (i = 0; i < bmap->n_ineq; ++i) {
2831 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2832 if (isl_int_is_neg(s)) {
2833 isl_int_clear(s);
2834 return 0;
2838 isl_int_clear(s);
2840 return 1;
2843 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2845 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2848 struct isl_basic_map *isl_basic_map_intersect(
2849 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2851 struct isl_vec *sample = NULL;
2853 if (!bmap1 || !bmap2)
2854 goto error;
2856 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2857 bmap2->dim, isl_dim_param), goto error);
2858 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2859 isl_space_dim(bmap1->dim, isl_dim_param) &&
2860 isl_space_dim(bmap2->dim, isl_dim_all) !=
2861 isl_space_dim(bmap2->dim, isl_dim_param))
2862 return isl_basic_map_intersect(bmap2, bmap1);
2864 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2865 isl_space_dim(bmap2->dim, isl_dim_param))
2866 isl_assert(bmap1->ctx,
2867 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2869 if (bmap1->sample &&
2870 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2871 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2872 sample = isl_vec_copy(bmap1->sample);
2873 else if (bmap2->sample &&
2874 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2875 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2876 sample = isl_vec_copy(bmap2->sample);
2878 bmap1 = isl_basic_map_cow(bmap1);
2879 if (!bmap1)
2880 goto error;
2881 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2882 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2883 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2885 if (!bmap1)
2886 isl_vec_free(sample);
2887 else if (sample) {
2888 isl_vec_free(bmap1->sample);
2889 bmap1->sample = sample;
2892 bmap1 = isl_basic_map_simplify(bmap1);
2893 return isl_basic_map_finalize(bmap1);
2894 error:
2895 if (sample)
2896 isl_vec_free(sample);
2897 isl_basic_map_free(bmap1);
2898 isl_basic_map_free(bmap2);
2899 return NULL;
2902 struct isl_basic_set *isl_basic_set_intersect(
2903 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2905 return (struct isl_basic_set *)
2906 isl_basic_map_intersect(
2907 (struct isl_basic_map *)bset1,
2908 (struct isl_basic_map *)bset2);
2911 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2912 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2914 return isl_basic_set_intersect(bset1, bset2);
2917 /* Special case of isl_map_intersect, where both map1 and map2
2918 * are convex, without any divs and such that either map1 or map2
2919 * contains a single constraint. This constraint is then simply
2920 * added to the other map.
2922 static __isl_give isl_map *map_intersect_add_constraint(
2923 __isl_take isl_map *map1, __isl_take isl_map *map2)
2925 isl_assert(map1->ctx, map1->n == 1, goto error);
2926 isl_assert(map2->ctx, map1->n == 1, goto error);
2927 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2928 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2930 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2931 return isl_map_intersect(map2, map1);
2933 isl_assert(map2->ctx,
2934 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2936 map1 = isl_map_cow(map1);
2937 if (!map1)
2938 goto error;
2939 if (isl_map_plain_is_empty(map1)) {
2940 isl_map_free(map2);
2941 return map1;
2943 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2944 if (map2->p[0]->n_eq == 1)
2945 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2946 else
2947 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2948 map2->p[0]->ineq[0]);
2950 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2951 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2952 if (!map1->p[0])
2953 goto error;
2955 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2956 isl_basic_map_free(map1->p[0]);
2957 map1->n = 0;
2960 isl_map_free(map2);
2962 return map1;
2963 error:
2964 isl_map_free(map1);
2965 isl_map_free(map2);
2966 return NULL;
2969 /* map2 may be either a parameter domain or a map living in the same
2970 * space as map1.
2972 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2973 __isl_take isl_map *map2)
2975 unsigned flags = 0;
2976 isl_map *result;
2977 int i, j;
2979 if (!map1 || !map2)
2980 goto error;
2982 if ((isl_map_plain_is_empty(map1) ||
2983 isl_map_plain_is_universe(map2)) &&
2984 isl_space_is_equal(map1->dim, map2->dim)) {
2985 isl_map_free(map2);
2986 return map1;
2988 if ((isl_map_plain_is_empty(map2) ||
2989 isl_map_plain_is_universe(map1)) &&
2990 isl_space_is_equal(map1->dim, map2->dim)) {
2991 isl_map_free(map1);
2992 return map2;
2995 if (map1->n == 1 && map2->n == 1 &&
2996 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2997 isl_space_is_equal(map1->dim, map2->dim) &&
2998 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2999 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3000 return map_intersect_add_constraint(map1, map2);
3002 if (isl_space_dim(map2->dim, isl_dim_all) !=
3003 isl_space_dim(map2->dim, isl_dim_param))
3004 isl_assert(map1->ctx,
3005 isl_space_is_equal(map1->dim, map2->dim), goto error);
3007 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3008 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3009 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3011 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3012 map1->n * map2->n, flags);
3013 if (!result)
3014 goto error;
3015 for (i = 0; i < map1->n; ++i)
3016 for (j = 0; j < map2->n; ++j) {
3017 struct isl_basic_map *part;
3018 part = isl_basic_map_intersect(
3019 isl_basic_map_copy(map1->p[i]),
3020 isl_basic_map_copy(map2->p[j]));
3021 if (isl_basic_map_is_empty(part) < 0)
3022 part = isl_basic_map_free(part);
3023 result = isl_map_add_basic_map(result, part);
3024 if (!result)
3025 goto error;
3027 isl_map_free(map1);
3028 isl_map_free(map2);
3029 return result;
3030 error:
3031 isl_map_free(map1);
3032 isl_map_free(map2);
3033 return NULL;
3036 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3037 __isl_take isl_map *map2)
3039 if (!map1 || !map2)
3040 goto error;
3041 if (!isl_space_is_equal(map1->dim, map2->dim))
3042 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3043 "spaces don't match", goto error);
3044 return map_intersect_internal(map1, map2);
3045 error:
3046 isl_map_free(map1);
3047 isl_map_free(map2);
3048 return NULL;
3051 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3052 __isl_take isl_map *map2)
3054 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3057 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3059 return (struct isl_set *)
3060 isl_map_intersect((struct isl_map *)set1,
3061 (struct isl_map *)set2);
3064 /* map_intersect_internal accepts intersections
3065 * with parameter domains, so we can just call that function.
3067 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3068 __isl_take isl_set *params)
3070 return map_intersect_internal(map, params);
3073 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3074 __isl_take isl_map *map2)
3076 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3079 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3080 __isl_take isl_set *params)
3082 return isl_map_intersect_params(set, params);
3085 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3087 isl_space *dim;
3088 struct isl_basic_set *bset;
3089 unsigned in;
3091 if (!bmap)
3092 return NULL;
3093 bmap = isl_basic_map_cow(bmap);
3094 if (!bmap)
3095 return NULL;
3096 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3097 in = isl_basic_map_n_in(bmap);
3098 bset = isl_basic_set_from_basic_map(bmap);
3099 bset = isl_basic_set_swap_vars(bset, in);
3100 return isl_basic_map_from_basic_set(bset, dim);
3103 static __isl_give isl_basic_map *basic_map_space_reset(
3104 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3106 isl_space *space;
3108 if (!bmap)
3109 return NULL;
3110 if (!isl_space_is_named_or_nested(bmap->dim, type))
3111 return bmap;
3113 space = isl_basic_map_get_space(bmap);
3114 space = isl_space_reset(space, type);
3115 bmap = isl_basic_map_reset_space(bmap, space);
3116 return bmap;
3119 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3120 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3121 unsigned pos, unsigned n)
3123 isl_space *res_dim;
3124 struct isl_basic_map *res;
3125 struct isl_dim_map *dim_map;
3126 unsigned total, off;
3127 enum isl_dim_type t;
3129 if (n == 0)
3130 return basic_map_space_reset(bmap, type);
3132 if (!bmap)
3133 return NULL;
3135 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3137 total = isl_basic_map_total_dim(bmap) + n;
3138 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3139 off = 0;
3140 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3141 if (t != type) {
3142 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3143 } else {
3144 unsigned size = isl_basic_map_dim(bmap, t);
3145 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3146 0, pos, off);
3147 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3148 pos, size - pos, off + pos + n);
3150 off += isl_space_dim(res_dim, t);
3152 isl_dim_map_div(dim_map, bmap, off);
3154 res = isl_basic_map_alloc_space(res_dim,
3155 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3156 if (isl_basic_map_is_rational(bmap))
3157 res = isl_basic_map_set_rational(res);
3158 if (isl_basic_map_plain_is_empty(bmap)) {
3159 isl_basic_map_free(bmap);
3160 free(dim_map);
3161 return isl_basic_map_set_to_empty(res);
3163 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3164 return isl_basic_map_finalize(res);
3167 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3168 __isl_take isl_basic_set *bset,
3169 enum isl_dim_type type, unsigned pos, unsigned n)
3171 return isl_basic_map_insert_dims(bset, type, pos, n);
3174 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3175 enum isl_dim_type type, unsigned n)
3177 if (!bmap)
3178 return NULL;
3179 return isl_basic_map_insert_dims(bmap, type,
3180 isl_basic_map_dim(bmap, type), n);
3183 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3184 enum isl_dim_type type, unsigned n)
3186 if (!bset)
3187 return NULL;
3188 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3189 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3190 error:
3191 isl_basic_set_free(bset);
3192 return NULL;
3195 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3196 enum isl_dim_type type)
3198 isl_space *space;
3200 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3201 return map;
3203 space = isl_map_get_space(map);
3204 space = isl_space_reset(space, type);
3205 map = isl_map_reset_space(map, space);
3206 return map;
3209 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3210 enum isl_dim_type type, unsigned pos, unsigned n)
3212 int i;
3214 if (n == 0)
3215 return map_space_reset(map, type);
3217 map = isl_map_cow(map);
3218 if (!map)
3219 return NULL;
3221 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3222 if (!map->dim)
3223 goto error;
3225 for (i = 0; i < map->n; ++i) {
3226 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3227 if (!map->p[i])
3228 goto error;
3231 return map;
3232 error:
3233 isl_map_free(map);
3234 return NULL;
3237 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3238 enum isl_dim_type type, unsigned pos, unsigned n)
3240 return isl_map_insert_dims(set, type, pos, n);
3243 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3244 enum isl_dim_type type, unsigned n)
3246 if (!map)
3247 return NULL;
3248 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3251 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3252 enum isl_dim_type type, unsigned n)
3254 if (!set)
3255 return NULL;
3256 isl_assert(set->ctx, type != isl_dim_in, goto error);
3257 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3258 error:
3259 isl_set_free(set);
3260 return NULL;
3263 __isl_give isl_basic_map *isl_basic_map_move_dims(
3264 __isl_take isl_basic_map *bmap,
3265 enum isl_dim_type dst_type, unsigned dst_pos,
3266 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3268 struct isl_dim_map *dim_map;
3269 struct isl_basic_map *res;
3270 enum isl_dim_type t;
3271 unsigned total, off;
3273 if (!bmap)
3274 return NULL;
3275 if (n == 0)
3276 return bmap;
3278 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3279 goto error);
3281 if (dst_type == src_type && dst_pos == src_pos)
3282 return bmap;
3284 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3286 if (pos(bmap->dim, dst_type) + dst_pos ==
3287 pos(bmap->dim, src_type) + src_pos +
3288 ((src_type < dst_type) ? n : 0)) {
3289 bmap = isl_basic_map_cow(bmap);
3290 if (!bmap)
3291 return NULL;
3293 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3294 src_type, src_pos, n);
3295 if (!bmap->dim)
3296 goto error;
3298 bmap = isl_basic_map_finalize(bmap);
3300 return bmap;
3303 total = isl_basic_map_total_dim(bmap);
3304 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3306 off = 0;
3307 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3308 unsigned size = isl_space_dim(bmap->dim, t);
3309 if (t == dst_type) {
3310 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3311 0, dst_pos, off);
3312 off += dst_pos;
3313 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3314 src_pos, n, off);
3315 off += n;
3316 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3317 dst_pos, size - dst_pos, off);
3318 off += size - dst_pos;
3319 } else if (t == src_type) {
3320 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3321 0, src_pos, off);
3322 off += src_pos;
3323 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3324 src_pos + n, size - src_pos - n, off);
3325 off += size - src_pos - n;
3326 } else {
3327 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3328 off += size;
3331 isl_dim_map_div(dim_map, bmap, off);
3333 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3334 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3335 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3336 if (!bmap)
3337 goto error;
3339 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3340 src_type, src_pos, n);
3341 if (!bmap->dim)
3342 goto error;
3344 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3345 bmap = isl_basic_map_gauss(bmap, NULL);
3346 bmap = isl_basic_map_finalize(bmap);
3348 return bmap;
3349 error:
3350 isl_basic_map_free(bmap);
3351 return NULL;
3354 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3355 enum isl_dim_type dst_type, unsigned dst_pos,
3356 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3358 return (isl_basic_set *)isl_basic_map_move_dims(
3359 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3362 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3363 enum isl_dim_type dst_type, unsigned dst_pos,
3364 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3366 if (!set)
3367 return NULL;
3368 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3369 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3370 src_type, src_pos, n);
3371 error:
3372 isl_set_free(set);
3373 return NULL;
3376 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3377 enum isl_dim_type dst_type, unsigned dst_pos,
3378 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3380 int i;
3382 if (!map)
3383 return NULL;
3384 if (n == 0)
3385 return map;
3387 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3388 goto error);
3390 if (dst_type == src_type && dst_pos == src_pos)
3391 return map;
3393 isl_assert(map->ctx, dst_type != src_type, goto error);
3395 map = isl_map_cow(map);
3396 if (!map)
3397 return NULL;
3399 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3400 if (!map->dim)
3401 goto error;
3403 for (i = 0; i < map->n; ++i) {
3404 map->p[i] = isl_basic_map_move_dims(map->p[i],
3405 dst_type, dst_pos,
3406 src_type, src_pos, n);
3407 if (!map->p[i])
3408 goto error;
3411 return map;
3412 error:
3413 isl_map_free(map);
3414 return NULL;
3417 /* Move the specified dimensions to the last columns right before
3418 * the divs. Don't change the dimension specification of bmap.
3419 * That's the responsibility of the caller.
3421 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3422 enum isl_dim_type type, unsigned first, unsigned n)
3424 struct isl_dim_map *dim_map;
3425 struct isl_basic_map *res;
3426 enum isl_dim_type t;
3427 unsigned total, off;
3429 if (!bmap)
3430 return NULL;
3431 if (pos(bmap->dim, type) + first + n ==
3432 1 + isl_space_dim(bmap->dim, isl_dim_all))
3433 return bmap;
3435 total = isl_basic_map_total_dim(bmap);
3436 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3438 off = 0;
3439 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3440 unsigned size = isl_space_dim(bmap->dim, t);
3441 if (t == type) {
3442 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3443 0, first, off);
3444 off += first;
3445 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3446 first, n, total - bmap->n_div - n);
3447 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3448 first + n, size - (first + n), off);
3449 off += size - (first + n);
3450 } else {
3451 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3452 off += size;
3455 isl_dim_map_div(dim_map, bmap, off + n);
3457 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3458 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3459 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3460 return res;
3463 /* Insert "n" rows in the divs of "bmap".
3465 * The number of columns is not changed, which means that the last
3466 * dimensions of "bmap" are being reintepreted as the new divs.
3467 * The space of "bmap" is not adjusted, however, which means
3468 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3469 * from the space of "bmap" is the responsibility of the caller.
3471 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3472 int n)
3474 int i;
3475 size_t row_size;
3476 isl_int **new_div;
3477 isl_int *old;
3479 bmap = isl_basic_map_cow(bmap);
3480 if (!bmap)
3481 return NULL;
3483 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3484 old = bmap->block2.data;
3485 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3486 (bmap->extra + n) * (1 + row_size));
3487 if (!bmap->block2.data)
3488 return isl_basic_map_free(bmap);
3489 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3490 if (!new_div)
3491 return isl_basic_map_free(bmap);
3492 for (i = 0; i < n; ++i) {
3493 new_div[i] = bmap->block2.data +
3494 (bmap->extra + i) * (1 + row_size);
3495 isl_seq_clr(new_div[i], 1 + row_size);
3497 for (i = 0; i < bmap->extra; ++i)
3498 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3499 free(bmap->div);
3500 bmap->div = new_div;
3501 bmap->n_div += n;
3502 bmap->extra += n;
3504 return bmap;
3507 /* Turn the n dimensions of type type, starting at first
3508 * into existentially quantified variables.
3510 __isl_give isl_basic_map *isl_basic_map_project_out(
3511 __isl_take isl_basic_map *bmap,
3512 enum isl_dim_type type, unsigned first, unsigned n)
3514 if (n == 0)
3515 return basic_map_space_reset(bmap, type);
3517 if (!bmap)
3518 return NULL;
3520 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3521 return isl_basic_map_remove_dims(bmap, type, first, n);
3523 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3524 goto error);
3526 bmap = move_last(bmap, type, first, n);
3527 bmap = isl_basic_map_cow(bmap);
3528 bmap = insert_div_rows(bmap, n);
3529 if (!bmap)
3530 return NULL;
3532 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3533 if (!bmap->dim)
3534 goto error;
3535 bmap = isl_basic_map_simplify(bmap);
3536 bmap = isl_basic_map_drop_redundant_divs(bmap);
3537 return isl_basic_map_finalize(bmap);
3538 error:
3539 isl_basic_map_free(bmap);
3540 return NULL;
3543 /* Turn the n dimensions of type type, starting at first
3544 * into existentially quantified variables.
3546 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3547 enum isl_dim_type type, unsigned first, unsigned n)
3549 return (isl_basic_set *)isl_basic_map_project_out(
3550 (isl_basic_map *)bset, type, first, n);
3553 /* Turn the n dimensions of type type, starting at first
3554 * into existentially quantified variables.
3556 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3557 enum isl_dim_type type, unsigned first, unsigned n)
3559 int i;
3561 if (!map)
3562 return NULL;
3564 if (n == 0)
3565 return map_space_reset(map, type);
3567 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3569 map = isl_map_cow(map);
3570 if (!map)
3571 return NULL;
3573 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3574 if (!map->dim)
3575 goto error;
3577 for (i = 0; i < map->n; ++i) {
3578 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3579 if (!map->p[i])
3580 goto error;
3583 return map;
3584 error:
3585 isl_map_free(map);
3586 return NULL;
3589 /* Turn the n dimensions of type type, starting at first
3590 * into existentially quantified variables.
3592 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3593 enum isl_dim_type type, unsigned first, unsigned n)
3595 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3598 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3600 int i, j;
3602 for (i = 0; i < n; ++i) {
3603 j = isl_basic_map_alloc_div(bmap);
3604 if (j < 0)
3605 goto error;
3606 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3608 return bmap;
3609 error:
3610 isl_basic_map_free(bmap);
3611 return NULL;
3614 struct isl_basic_map *isl_basic_map_apply_range(
3615 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3617 isl_space *dim_result = NULL;
3618 struct isl_basic_map *bmap;
3619 unsigned n_in, n_out, n, nparam, total, pos;
3620 struct isl_dim_map *dim_map1, *dim_map2;
3622 if (!bmap1 || !bmap2)
3623 goto error;
3624 if (!isl_space_match(bmap1->dim, isl_dim_param,
3625 bmap2->dim, isl_dim_param))
3626 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3627 "parameters don't match", goto error);
3628 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3629 bmap2->dim, isl_dim_in))
3630 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3631 "spaces don't match", goto error);
3633 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3634 isl_space_copy(bmap2->dim));
3636 n_in = isl_basic_map_n_in(bmap1);
3637 n_out = isl_basic_map_n_out(bmap2);
3638 n = isl_basic_map_n_out(bmap1);
3639 nparam = isl_basic_map_n_param(bmap1);
3641 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3642 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3643 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3644 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3645 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3646 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3647 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3648 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3649 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3650 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3651 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3653 bmap = isl_basic_map_alloc_space(dim_result,
3654 bmap1->n_div + bmap2->n_div + n,
3655 bmap1->n_eq + bmap2->n_eq,
3656 bmap1->n_ineq + bmap2->n_ineq);
3657 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3658 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3659 bmap = add_divs(bmap, n);
3660 bmap = isl_basic_map_simplify(bmap);
3661 bmap = isl_basic_map_drop_redundant_divs(bmap);
3662 return isl_basic_map_finalize(bmap);
3663 error:
3664 isl_basic_map_free(bmap1);
3665 isl_basic_map_free(bmap2);
3666 return NULL;
3669 struct isl_basic_set *isl_basic_set_apply(
3670 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3672 if (!bset || !bmap)
3673 goto error;
3675 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3676 goto error);
3678 return (struct isl_basic_set *)
3679 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3680 error:
3681 isl_basic_set_free(bset);
3682 isl_basic_map_free(bmap);
3683 return NULL;
3686 struct isl_basic_map *isl_basic_map_apply_domain(
3687 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3689 if (!bmap1 || !bmap2)
3690 goto error;
3692 isl_assert(bmap1->ctx,
3693 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3694 isl_assert(bmap1->ctx,
3695 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3696 goto error);
3698 bmap1 = isl_basic_map_reverse(bmap1);
3699 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3700 return isl_basic_map_reverse(bmap1);
3701 error:
3702 isl_basic_map_free(bmap1);
3703 isl_basic_map_free(bmap2);
3704 return NULL;
3707 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3708 * A \cap B -> f(A) + f(B)
3710 struct isl_basic_map *isl_basic_map_sum(
3711 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3713 unsigned n_in, n_out, nparam, total, pos;
3714 struct isl_basic_map *bmap = NULL;
3715 struct isl_dim_map *dim_map1, *dim_map2;
3716 int i;
3718 if (!bmap1 || !bmap2)
3719 goto error;
3721 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3722 goto error);
3724 nparam = isl_basic_map_n_param(bmap1);
3725 n_in = isl_basic_map_n_in(bmap1);
3726 n_out = isl_basic_map_n_out(bmap1);
3728 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3729 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3730 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3731 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3732 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3733 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3734 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3735 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3736 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3737 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3738 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3740 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3741 bmap1->n_div + bmap2->n_div + 2 * n_out,
3742 bmap1->n_eq + bmap2->n_eq + n_out,
3743 bmap1->n_ineq + bmap2->n_ineq);
3744 for (i = 0; i < n_out; ++i) {
3745 int j = isl_basic_map_alloc_equality(bmap);
3746 if (j < 0)
3747 goto error;
3748 isl_seq_clr(bmap->eq[j], 1+total);
3749 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3750 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3751 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3753 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3754 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3755 bmap = add_divs(bmap, 2 * n_out);
3757 bmap = isl_basic_map_simplify(bmap);
3758 return isl_basic_map_finalize(bmap);
3759 error:
3760 isl_basic_map_free(bmap);
3761 isl_basic_map_free(bmap1);
3762 isl_basic_map_free(bmap2);
3763 return NULL;
3766 /* Given two maps A -> f(A) and B -> g(B), construct a map
3767 * A \cap B -> f(A) + f(B)
3769 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3771 struct isl_map *result;
3772 int i, j;
3774 if (!map1 || !map2)
3775 goto error;
3777 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3779 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3780 map1->n * map2->n, 0);
3781 if (!result)
3782 goto error;
3783 for (i = 0; i < map1->n; ++i)
3784 for (j = 0; j < map2->n; ++j) {
3785 struct isl_basic_map *part;
3786 part = isl_basic_map_sum(
3787 isl_basic_map_copy(map1->p[i]),
3788 isl_basic_map_copy(map2->p[j]));
3789 if (isl_basic_map_is_empty(part))
3790 isl_basic_map_free(part);
3791 else
3792 result = isl_map_add_basic_map(result, part);
3793 if (!result)
3794 goto error;
3796 isl_map_free(map1);
3797 isl_map_free(map2);
3798 return result;
3799 error:
3800 isl_map_free(map1);
3801 isl_map_free(map2);
3802 return NULL;
3805 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3806 __isl_take isl_set *set2)
3808 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3811 /* Given a basic map A -> f(A), construct A -> -f(A).
3813 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3815 int i, j;
3816 unsigned off, n;
3818 bmap = isl_basic_map_cow(bmap);
3819 if (!bmap)
3820 return NULL;
3822 n = isl_basic_map_dim(bmap, isl_dim_out);
3823 off = isl_basic_map_offset(bmap, isl_dim_out);
3824 for (i = 0; i < bmap->n_eq; ++i)
3825 for (j = 0; j < n; ++j)
3826 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3827 for (i = 0; i < bmap->n_ineq; ++i)
3828 for (j = 0; j < n; ++j)
3829 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3830 for (i = 0; i < bmap->n_div; ++i)
3831 for (j = 0; j < n; ++j)
3832 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3833 bmap = isl_basic_map_gauss(bmap, NULL);
3834 return isl_basic_map_finalize(bmap);
3837 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3839 return isl_basic_map_neg(bset);
3842 /* Given a map A -> f(A), construct A -> -f(A).
3844 struct isl_map *isl_map_neg(struct isl_map *map)
3846 int i;
3848 map = isl_map_cow(map);
3849 if (!map)
3850 return NULL;
3852 for (i = 0; i < map->n; ++i) {
3853 map->p[i] = isl_basic_map_neg(map->p[i]);
3854 if (!map->p[i])
3855 goto error;
3858 return map;
3859 error:
3860 isl_map_free(map);
3861 return NULL;
3864 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3866 return (isl_set *)isl_map_neg((isl_map *)set);
3869 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3870 * A -> floor(f(A)/d).
3872 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3873 isl_int d)
3875 unsigned n_in, n_out, nparam, total, pos;
3876 struct isl_basic_map *result = NULL;
3877 struct isl_dim_map *dim_map;
3878 int i;
3880 if (!bmap)
3881 return NULL;
3883 nparam = isl_basic_map_n_param(bmap);
3884 n_in = isl_basic_map_n_in(bmap);
3885 n_out = isl_basic_map_n_out(bmap);
3887 total = nparam + n_in + n_out + bmap->n_div + n_out;
3888 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3889 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3890 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3891 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3892 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3894 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3895 bmap->n_div + n_out,
3896 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3897 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3898 result = add_divs(result, n_out);
3899 for (i = 0; i < n_out; ++i) {
3900 int j;
3901 j = isl_basic_map_alloc_inequality(result);
3902 if (j < 0)
3903 goto error;
3904 isl_seq_clr(result->ineq[j], 1+total);
3905 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3906 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3907 j = isl_basic_map_alloc_inequality(result);
3908 if (j < 0)
3909 goto error;
3910 isl_seq_clr(result->ineq[j], 1+total);
3911 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3912 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3913 isl_int_sub_ui(result->ineq[j][0], d, 1);
3916 result = isl_basic_map_simplify(result);
3917 return isl_basic_map_finalize(result);
3918 error:
3919 isl_basic_map_free(result);
3920 return NULL;
3923 /* Given a map A -> f(A) and an integer d, construct a map
3924 * A -> floor(f(A)/d).
3926 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3928 int i;
3930 map = isl_map_cow(map);
3931 if (!map)
3932 return NULL;
3934 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3935 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3936 for (i = 0; i < map->n; ++i) {
3937 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3938 if (!map->p[i])
3939 goto error;
3942 return map;
3943 error:
3944 isl_map_free(map);
3945 return NULL;
3948 /* Given a map A -> f(A) and an integer d, construct a map
3949 * A -> floor(f(A)/d).
3951 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3952 __isl_take isl_val *d)
3954 if (!map || !d)
3955 goto error;
3956 if (!isl_val_is_int(d))
3957 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3958 "expecting integer denominator", goto error);
3959 map = isl_map_floordiv(map, d->n);
3960 isl_val_free(d);
3961 return map;
3962 error:
3963 isl_map_free(map);
3964 isl_val_free(d);
3965 return NULL;
3968 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3970 int i;
3971 unsigned nparam;
3972 unsigned n_in;
3974 i = isl_basic_map_alloc_equality(bmap);
3975 if (i < 0)
3976 goto error;
3977 nparam = isl_basic_map_n_param(bmap);
3978 n_in = isl_basic_map_n_in(bmap);
3979 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3980 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3981 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3982 return isl_basic_map_finalize(bmap);
3983 error:
3984 isl_basic_map_free(bmap);
3985 return NULL;
3988 /* Add a constraints to "bmap" expressing i_pos < o_pos
3990 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3992 int i;
3993 unsigned nparam;
3994 unsigned n_in;
3996 i = isl_basic_map_alloc_inequality(bmap);
3997 if (i < 0)
3998 goto error;
3999 nparam = isl_basic_map_n_param(bmap);
4000 n_in = isl_basic_map_n_in(bmap);
4001 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4002 isl_int_set_si(bmap->ineq[i][0], -1);
4003 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4004 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4005 return isl_basic_map_finalize(bmap);
4006 error:
4007 isl_basic_map_free(bmap);
4008 return NULL;
4011 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4013 static __isl_give isl_basic_map *var_less_or_equal(
4014 __isl_take isl_basic_map *bmap, unsigned pos)
4016 int i;
4017 unsigned nparam;
4018 unsigned n_in;
4020 i = isl_basic_map_alloc_inequality(bmap);
4021 if (i < 0)
4022 goto error;
4023 nparam = isl_basic_map_n_param(bmap);
4024 n_in = isl_basic_map_n_in(bmap);
4025 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4026 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4027 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4028 return isl_basic_map_finalize(bmap);
4029 error:
4030 isl_basic_map_free(bmap);
4031 return NULL;
4034 /* Add a constraints to "bmap" expressing i_pos > o_pos
4036 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4038 int i;
4039 unsigned nparam;
4040 unsigned n_in;
4042 i = isl_basic_map_alloc_inequality(bmap);
4043 if (i < 0)
4044 goto error;
4045 nparam = isl_basic_map_n_param(bmap);
4046 n_in = isl_basic_map_n_in(bmap);
4047 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4048 isl_int_set_si(bmap->ineq[i][0], -1);
4049 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4050 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4051 return isl_basic_map_finalize(bmap);
4052 error:
4053 isl_basic_map_free(bmap);
4054 return NULL;
4057 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4059 static __isl_give isl_basic_map *var_more_or_equal(
4060 __isl_take isl_basic_map *bmap, unsigned pos)
4062 int i;
4063 unsigned nparam;
4064 unsigned n_in;
4066 i = isl_basic_map_alloc_inequality(bmap);
4067 if (i < 0)
4068 goto error;
4069 nparam = isl_basic_map_n_param(bmap);
4070 n_in = isl_basic_map_n_in(bmap);
4071 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4072 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4073 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4074 return isl_basic_map_finalize(bmap);
4075 error:
4076 isl_basic_map_free(bmap);
4077 return NULL;
4080 __isl_give isl_basic_map *isl_basic_map_equal(
4081 __isl_take isl_space *dim, unsigned n_equal)
4083 int i;
4084 struct isl_basic_map *bmap;
4085 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4086 if (!bmap)
4087 return NULL;
4088 for (i = 0; i < n_equal && bmap; ++i)
4089 bmap = var_equal(bmap, i);
4090 return isl_basic_map_finalize(bmap);
4093 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4095 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4096 unsigned pos)
4098 int i;
4099 struct isl_basic_map *bmap;
4100 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4101 if (!bmap)
4102 return NULL;
4103 for (i = 0; i < pos && bmap; ++i)
4104 bmap = var_equal(bmap, i);
4105 if (bmap)
4106 bmap = var_less(bmap, pos);
4107 return isl_basic_map_finalize(bmap);
4110 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4112 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4113 __isl_take isl_space *dim, unsigned pos)
4115 int i;
4116 isl_basic_map *bmap;
4118 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4119 for (i = 0; i < pos; ++i)
4120 bmap = var_equal(bmap, i);
4121 bmap = var_less_or_equal(bmap, pos);
4122 return isl_basic_map_finalize(bmap);
4125 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4127 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4128 unsigned pos)
4130 int i;
4131 struct isl_basic_map *bmap;
4132 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4133 if (!bmap)
4134 return NULL;
4135 for (i = 0; i < pos && bmap; ++i)
4136 bmap = var_equal(bmap, i);
4137 if (bmap)
4138 bmap = var_more(bmap, pos);
4139 return isl_basic_map_finalize(bmap);
4142 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4144 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4145 __isl_take isl_space *dim, unsigned pos)
4147 int i;
4148 isl_basic_map *bmap;
4150 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4151 for (i = 0; i < pos; ++i)
4152 bmap = var_equal(bmap, i);
4153 bmap = var_more_or_equal(bmap, pos);
4154 return isl_basic_map_finalize(bmap);
4157 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4158 unsigned n, int equal)
4160 struct isl_map *map;
4161 int i;
4163 if (n == 0 && equal)
4164 return isl_map_universe(dims);
4166 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4168 for (i = 0; i + 1 < n; ++i)
4169 map = isl_map_add_basic_map(map,
4170 isl_basic_map_less_at(isl_space_copy(dims), i));
4171 if (n > 0) {
4172 if (equal)
4173 map = isl_map_add_basic_map(map,
4174 isl_basic_map_less_or_equal_at(dims, n - 1));
4175 else
4176 map = isl_map_add_basic_map(map,
4177 isl_basic_map_less_at(dims, n - 1));
4178 } else
4179 isl_space_free(dims);
4181 return map;
4184 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4186 if (!dims)
4187 return NULL;
4188 return map_lex_lte_first(dims, dims->n_out, equal);
4191 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4193 return map_lex_lte_first(dim, n, 0);
4196 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4198 return map_lex_lte_first(dim, n, 1);
4201 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4203 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4206 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4208 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4211 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4212 unsigned n, int equal)
4214 struct isl_map *map;
4215 int i;
4217 if (n == 0 && equal)
4218 return isl_map_universe(dims);
4220 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4222 for (i = 0; i + 1 < n; ++i)
4223 map = isl_map_add_basic_map(map,
4224 isl_basic_map_more_at(isl_space_copy(dims), i));
4225 if (n > 0) {
4226 if (equal)
4227 map = isl_map_add_basic_map(map,
4228 isl_basic_map_more_or_equal_at(dims, n - 1));
4229 else
4230 map = isl_map_add_basic_map(map,
4231 isl_basic_map_more_at(dims, n - 1));
4232 } else
4233 isl_space_free(dims);
4235 return map;
4238 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4240 if (!dims)
4241 return NULL;
4242 return map_lex_gte_first(dims, dims->n_out, equal);
4245 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4247 return map_lex_gte_first(dim, n, 0);
4250 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4252 return map_lex_gte_first(dim, n, 1);
4255 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4257 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4260 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4262 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4265 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4266 __isl_take isl_set *set2)
4268 isl_map *map;
4269 map = isl_map_lex_le(isl_set_get_space(set1));
4270 map = isl_map_intersect_domain(map, set1);
4271 map = isl_map_intersect_range(map, set2);
4272 return map;
4275 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4276 __isl_take isl_set *set2)
4278 isl_map *map;
4279 map = isl_map_lex_lt(isl_set_get_space(set1));
4280 map = isl_map_intersect_domain(map, set1);
4281 map = isl_map_intersect_range(map, set2);
4282 return map;
4285 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4286 __isl_take isl_set *set2)
4288 isl_map *map;
4289 map = isl_map_lex_ge(isl_set_get_space(set1));
4290 map = isl_map_intersect_domain(map, set1);
4291 map = isl_map_intersect_range(map, set2);
4292 return map;
4295 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4296 __isl_take isl_set *set2)
4298 isl_map *map;
4299 map = isl_map_lex_gt(isl_set_get_space(set1));
4300 map = isl_map_intersect_domain(map, set1);
4301 map = isl_map_intersect_range(map, set2);
4302 return map;
4305 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4306 __isl_take isl_map *map2)
4308 isl_map *map;
4309 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4310 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4311 map = isl_map_apply_range(map, isl_map_reverse(map2));
4312 return map;
4315 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4316 __isl_take isl_map *map2)
4318 isl_map *map;
4319 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4320 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4321 map = isl_map_apply_range(map, isl_map_reverse(map2));
4322 return map;
4325 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4326 __isl_take isl_map *map2)
4328 isl_map *map;
4329 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4330 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4331 map = isl_map_apply_range(map, isl_map_reverse(map2));
4332 return map;
4335 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4336 __isl_take isl_map *map2)
4338 isl_map *map;
4339 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4340 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4341 map = isl_map_apply_range(map, isl_map_reverse(map2));
4342 return map;
4345 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4346 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4348 struct isl_basic_map *bmap;
4350 bset = isl_basic_set_cow(bset);
4351 if (!bset || !dim)
4352 goto error;
4354 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4355 isl_space_free(bset->dim);
4356 bmap = (struct isl_basic_map *) bset;
4357 bmap->dim = dim;
4358 return isl_basic_map_finalize(bmap);
4359 error:
4360 isl_basic_set_free(bset);
4361 isl_space_free(dim);
4362 return NULL;
4365 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4367 if (!bmap)
4368 goto error;
4369 if (bmap->dim->n_in == 0)
4370 return (struct isl_basic_set *)bmap;
4371 bmap = isl_basic_map_cow(bmap);
4372 if (!bmap)
4373 goto error;
4374 bmap->dim = isl_space_as_set_space(bmap->dim);
4375 if (!bmap->dim)
4376 goto error;
4377 bmap = isl_basic_map_finalize(bmap);
4378 return (struct isl_basic_set *)bmap;
4379 error:
4380 isl_basic_map_free(bmap);
4381 return NULL;
4384 /* For a div d = floor(f/m), add the constraint
4386 * f - m d >= 0
4388 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4389 unsigned pos, isl_int *div)
4391 int i;
4392 unsigned total = isl_basic_map_total_dim(bmap);
4394 i = isl_basic_map_alloc_inequality(bmap);
4395 if (i < 0)
4396 return -1;
4397 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4398 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4400 return 0;
4403 /* For a div d = floor(f/m), add the constraint
4405 * -(f-(n-1)) + m d >= 0
4407 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4408 unsigned pos, isl_int *div)
4410 int i;
4411 unsigned total = isl_basic_map_total_dim(bmap);
4413 i = isl_basic_map_alloc_inequality(bmap);
4414 if (i < 0)
4415 return -1;
4416 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4417 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4418 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4419 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4421 return 0;
4424 /* For a div d = floor(f/m), add the constraints
4426 * f - m d >= 0
4427 * -(f-(n-1)) + m d >= 0
4429 * Note that the second constraint is the negation of
4431 * f - m d >= n
4433 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4434 unsigned pos, isl_int *div)
4436 if (add_upper_div_constraint(bmap, pos, div) < 0)
4437 return -1;
4438 if (add_lower_div_constraint(bmap, pos, div) < 0)
4439 return -1;
4440 return 0;
4443 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4444 unsigned pos, isl_int *div)
4446 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4447 pos, div);
4450 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4452 unsigned total = isl_basic_map_total_dim(bmap);
4453 unsigned div_pos = total - bmap->n_div + div;
4455 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4456 bmap->div[div]);
4459 /* For each known div d = floor(f/m), add the constraints
4461 * f - m d >= 0
4462 * -(f-(n-1)) + m d >= 0
4464 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4465 __isl_take isl_basic_map *bmap)
4467 int i;
4468 unsigned n_div;
4470 if (!bmap)
4471 return NULL;
4472 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4473 if (n_div == 0)
4474 return bmap;
4475 bmap = isl_basic_map_cow(bmap);
4476 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
4477 if (!bmap)
4478 return NULL;
4479 for (i = 0; i < n_div; ++i) {
4480 if (isl_int_is_zero(bmap->div[i][0]))
4481 continue;
4482 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4483 return isl_basic_map_free(bmap);
4486 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4487 bmap = isl_basic_map_finalize(bmap);
4488 return bmap;
4491 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4493 * In particular, if this div is of the form d = floor(f/m),
4494 * then add the constraint
4496 * f - m d >= 0
4498 * if sign < 0 or the constraint
4500 * -(f-(n-1)) + m d >= 0
4502 * if sign > 0.
4504 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4505 unsigned div, int sign)
4507 unsigned total;
4508 unsigned div_pos;
4510 if (!bmap)
4511 return -1;
4513 total = isl_basic_map_total_dim(bmap);
4514 div_pos = total - bmap->n_div + div;
4516 if (sign < 0)
4517 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4518 else
4519 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4522 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4524 return isl_basic_map_add_div_constraints(bset, div);
4527 struct isl_basic_set *isl_basic_map_underlying_set(
4528 struct isl_basic_map *bmap)
4530 if (!bmap)
4531 goto error;
4532 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4533 bmap->n_div == 0 &&
4534 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4535 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4536 return (struct isl_basic_set *)bmap;
4537 bmap = isl_basic_map_cow(bmap);
4538 if (!bmap)
4539 goto error;
4540 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4541 if (!bmap->dim)
4542 goto error;
4543 bmap->extra -= bmap->n_div;
4544 bmap->n_div = 0;
4545 bmap = isl_basic_map_finalize(bmap);
4546 return (struct isl_basic_set *)bmap;
4547 error:
4548 isl_basic_map_free(bmap);
4549 return NULL;
4552 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4553 __isl_take isl_basic_set *bset)
4555 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4558 /* Replace each element in "list" by the result of applying
4559 * isl_basic_map_underlying_set to the element.
4561 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4562 __isl_take isl_basic_map_list *list)
4564 int i, n;
4566 if (!list)
4567 return NULL;
4569 n = isl_basic_map_list_n_basic_map(list);
4570 for (i = 0; i < n; ++i) {
4571 isl_basic_map *bmap;
4572 isl_basic_set *bset;
4574 bmap = isl_basic_map_list_get_basic_map(list, i);
4575 bset = isl_basic_set_underlying_set(bmap);
4576 list = isl_basic_set_list_set_basic_set(list, i, bset);
4579 return list;
4582 struct isl_basic_map *isl_basic_map_overlying_set(
4583 struct isl_basic_set *bset, struct isl_basic_map *like)
4585 struct isl_basic_map *bmap;
4586 struct isl_ctx *ctx;
4587 unsigned total;
4588 int i;
4590 if (!bset || !like)
4591 goto error;
4592 ctx = bset->ctx;
4593 isl_assert(ctx, bset->n_div == 0, goto error);
4594 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4595 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4596 goto error);
4597 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4598 isl_basic_map_free(like);
4599 return (struct isl_basic_map *)bset;
4601 bset = isl_basic_set_cow(bset);
4602 if (!bset)
4603 goto error;
4604 total = bset->dim->n_out + bset->extra;
4605 bmap = (struct isl_basic_map *)bset;
4606 isl_space_free(bmap->dim);
4607 bmap->dim = isl_space_copy(like->dim);
4608 if (!bmap->dim)
4609 goto error;
4610 bmap->n_div = like->n_div;
4611 bmap->extra += like->n_div;
4612 if (bmap->extra) {
4613 unsigned ltotal;
4614 isl_int **div;
4615 ltotal = total - bmap->extra + like->extra;
4616 if (ltotal > total)
4617 ltotal = total;
4618 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4619 bmap->extra * (1 + 1 + total));
4620 if (isl_blk_is_error(bmap->block2))
4621 goto error;
4622 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4623 if (!div)
4624 goto error;
4625 bmap->div = div;
4626 for (i = 0; i < bmap->extra; ++i)
4627 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4628 for (i = 0; i < like->n_div; ++i) {
4629 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4630 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4632 bmap = isl_basic_map_add_known_div_constraints(bmap);
4634 isl_basic_map_free(like);
4635 bmap = isl_basic_map_simplify(bmap);
4636 bmap = isl_basic_map_finalize(bmap);
4637 return bmap;
4638 error:
4639 isl_basic_map_free(like);
4640 isl_basic_set_free(bset);
4641 return NULL;
4644 struct isl_basic_set *isl_basic_set_from_underlying_set(
4645 struct isl_basic_set *bset, struct isl_basic_set *like)
4647 return (struct isl_basic_set *)
4648 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4651 struct isl_set *isl_set_from_underlying_set(
4652 struct isl_set *set, struct isl_basic_set *like)
4654 int i;
4656 if (!set || !like)
4657 goto error;
4658 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4659 goto error);
4660 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4661 isl_basic_set_free(like);
4662 return set;
4664 set = isl_set_cow(set);
4665 if (!set)
4666 goto error;
4667 for (i = 0; i < set->n; ++i) {
4668 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4669 isl_basic_set_copy(like));
4670 if (!set->p[i])
4671 goto error;
4673 isl_space_free(set->dim);
4674 set->dim = isl_space_copy(like->dim);
4675 if (!set->dim)
4676 goto error;
4677 isl_basic_set_free(like);
4678 return set;
4679 error:
4680 isl_basic_set_free(like);
4681 isl_set_free(set);
4682 return NULL;
4685 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4687 int i;
4689 map = isl_map_cow(map);
4690 if (!map)
4691 return NULL;
4692 map->dim = isl_space_cow(map->dim);
4693 if (!map->dim)
4694 goto error;
4696 for (i = 1; i < map->n; ++i)
4697 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4698 goto error);
4699 for (i = 0; i < map->n; ++i) {
4700 map->p[i] = (struct isl_basic_map *)
4701 isl_basic_map_underlying_set(map->p[i]);
4702 if (!map->p[i])
4703 goto error;
4705 if (map->n == 0)
4706 map->dim = isl_space_underlying(map->dim, 0);
4707 else {
4708 isl_space_free(map->dim);
4709 map->dim = isl_space_copy(map->p[0]->dim);
4711 if (!map->dim)
4712 goto error;
4713 return (struct isl_set *)map;
4714 error:
4715 isl_map_free(map);
4716 return NULL;
4719 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4721 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4724 __isl_give isl_basic_map *isl_basic_map_reset_space(
4725 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4727 bmap = isl_basic_map_cow(bmap);
4728 if (!bmap || !dim)
4729 goto error;
4731 isl_space_free(bmap->dim);
4732 bmap->dim = dim;
4734 bmap = isl_basic_map_finalize(bmap);
4736 return bmap;
4737 error:
4738 isl_basic_map_free(bmap);
4739 isl_space_free(dim);
4740 return NULL;
4743 __isl_give isl_basic_set *isl_basic_set_reset_space(
4744 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4746 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4747 dim);
4750 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4751 __isl_take isl_space *dim)
4753 int i;
4755 map = isl_map_cow(map);
4756 if (!map || !dim)
4757 goto error;
4759 for (i = 0; i < map->n; ++i) {
4760 map->p[i] = isl_basic_map_reset_space(map->p[i],
4761 isl_space_copy(dim));
4762 if (!map->p[i])
4763 goto error;
4765 isl_space_free(map->dim);
4766 map->dim = dim;
4768 return map;
4769 error:
4770 isl_map_free(map);
4771 isl_space_free(dim);
4772 return NULL;
4775 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4776 __isl_take isl_space *dim)
4778 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4781 /* Compute the parameter domain of the given basic set.
4783 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4785 isl_space *space;
4786 unsigned n;
4788 if (isl_basic_set_is_params(bset))
4789 return bset;
4791 n = isl_basic_set_dim(bset, isl_dim_set);
4792 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4793 space = isl_basic_set_get_space(bset);
4794 space = isl_space_params(space);
4795 bset = isl_basic_set_reset_space(bset, space);
4796 return bset;
4799 /* Construct a zero-dimensional basic set with the given parameter domain.
4801 __isl_give isl_basic_set *isl_basic_set_from_params(
4802 __isl_take isl_basic_set *bset)
4804 isl_space *space;
4805 space = isl_basic_set_get_space(bset);
4806 space = isl_space_set_from_params(space);
4807 bset = isl_basic_set_reset_space(bset, space);
4808 return bset;
4811 /* Compute the parameter domain of the given set.
4813 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4815 isl_space *space;
4816 unsigned n;
4818 if (isl_set_is_params(set))
4819 return set;
4821 n = isl_set_dim(set, isl_dim_set);
4822 set = isl_set_project_out(set, isl_dim_set, 0, n);
4823 space = isl_set_get_space(set);
4824 space = isl_space_params(space);
4825 set = isl_set_reset_space(set, space);
4826 return set;
4829 /* Construct a zero-dimensional set with the given parameter domain.
4831 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4833 isl_space *space;
4834 space = isl_set_get_space(set);
4835 space = isl_space_set_from_params(space);
4836 set = isl_set_reset_space(set, space);
4837 return set;
4840 /* Compute the parameter domain of the given map.
4842 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4844 isl_space *space;
4845 unsigned n;
4847 n = isl_map_dim(map, isl_dim_in);
4848 map = isl_map_project_out(map, isl_dim_in, 0, n);
4849 n = isl_map_dim(map, isl_dim_out);
4850 map = isl_map_project_out(map, isl_dim_out, 0, n);
4851 space = isl_map_get_space(map);
4852 space = isl_space_params(space);
4853 map = isl_map_reset_space(map, space);
4854 return map;
4857 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4859 isl_space *dim;
4860 struct isl_basic_set *domain;
4861 unsigned n_in;
4862 unsigned n_out;
4864 if (!bmap)
4865 return NULL;
4866 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4868 n_in = isl_basic_map_n_in(bmap);
4869 n_out = isl_basic_map_n_out(bmap);
4870 domain = isl_basic_set_from_basic_map(bmap);
4871 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4873 domain = isl_basic_set_reset_space(domain, dim);
4875 return domain;
4878 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4880 if (!bmap)
4881 return -1;
4882 return isl_space_may_be_set(bmap->dim);
4885 /* Is this basic map actually a set?
4886 * Users should never call this function. Outside of isl,
4887 * the type should indicate whether something is a set or a map.
4889 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4891 if (!bmap)
4892 return -1;
4893 return isl_space_is_set(bmap->dim);
4896 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4898 if (!bmap)
4899 return NULL;
4900 if (isl_basic_map_is_set(bmap))
4901 return bmap;
4902 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4905 __isl_give isl_basic_map *isl_basic_map_domain_map(
4906 __isl_take isl_basic_map *bmap)
4908 int i, k;
4909 isl_space *dim;
4910 isl_basic_map *domain;
4911 int nparam, n_in, n_out;
4912 unsigned total;
4914 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4915 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4916 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4918 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4919 domain = isl_basic_map_universe(dim);
4921 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4922 bmap = isl_basic_map_apply_range(bmap, domain);
4923 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4925 total = isl_basic_map_total_dim(bmap);
4927 for (i = 0; i < n_in; ++i) {
4928 k = isl_basic_map_alloc_equality(bmap);
4929 if (k < 0)
4930 goto error;
4931 isl_seq_clr(bmap->eq[k], 1 + total);
4932 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4933 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4936 bmap = isl_basic_map_gauss(bmap, NULL);
4937 return isl_basic_map_finalize(bmap);
4938 error:
4939 isl_basic_map_free(bmap);
4940 return NULL;
4943 __isl_give isl_basic_map *isl_basic_map_range_map(
4944 __isl_take isl_basic_map *bmap)
4946 int i, k;
4947 isl_space *dim;
4948 isl_basic_map *range;
4949 int nparam, n_in, n_out;
4950 unsigned total;
4952 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4953 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4954 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4956 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4957 range = isl_basic_map_universe(dim);
4959 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4960 bmap = isl_basic_map_apply_range(bmap, range);
4961 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4963 total = isl_basic_map_total_dim(bmap);
4965 for (i = 0; i < n_out; ++i) {
4966 k = isl_basic_map_alloc_equality(bmap);
4967 if (k < 0)
4968 goto error;
4969 isl_seq_clr(bmap->eq[k], 1 + total);
4970 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4971 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4974 bmap = isl_basic_map_gauss(bmap, NULL);
4975 return isl_basic_map_finalize(bmap);
4976 error:
4977 isl_basic_map_free(bmap);
4978 return NULL;
4981 int isl_map_may_be_set(__isl_keep isl_map *map)
4983 if (!map)
4984 return -1;
4985 return isl_space_may_be_set(map->dim);
4988 /* Is this map actually a set?
4989 * Users should never call this function. Outside of isl,
4990 * the type should indicate whether something is a set or a map.
4992 int isl_map_is_set(__isl_keep isl_map *map)
4994 if (!map)
4995 return -1;
4996 return isl_space_is_set(map->dim);
4999 struct isl_set *isl_map_range(struct isl_map *map)
5001 int i;
5002 struct isl_set *set;
5004 if (!map)
5005 goto error;
5006 if (isl_map_is_set(map))
5007 return (isl_set *)map;
5009 map = isl_map_cow(map);
5010 if (!map)
5011 goto error;
5013 set = (struct isl_set *) map;
5014 set->dim = isl_space_range(set->dim);
5015 if (!set->dim)
5016 goto error;
5017 for (i = 0; i < map->n; ++i) {
5018 set->p[i] = isl_basic_map_range(map->p[i]);
5019 if (!set->p[i])
5020 goto error;
5022 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5023 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5024 return set;
5025 error:
5026 isl_map_free(map);
5027 return NULL;
5030 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5032 int i;
5034 map = isl_map_cow(map);
5035 if (!map)
5036 return NULL;
5038 map->dim = isl_space_domain_map(map->dim);
5039 if (!map->dim)
5040 goto error;
5041 for (i = 0; i < map->n; ++i) {
5042 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5043 if (!map->p[i])
5044 goto error;
5046 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5047 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5048 return map;
5049 error:
5050 isl_map_free(map);
5051 return NULL;
5054 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5056 int i;
5057 isl_space *range_dim;
5059 map = isl_map_cow(map);
5060 if (!map)
5061 return NULL;
5063 range_dim = isl_space_range(isl_map_get_space(map));
5064 range_dim = isl_space_from_range(range_dim);
5065 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5066 map->dim = isl_space_join(map->dim, range_dim);
5067 if (!map->dim)
5068 goto error;
5069 for (i = 0; i < map->n; ++i) {
5070 map->p[i] = isl_basic_map_range_map(map->p[i]);
5071 if (!map->p[i])
5072 goto error;
5074 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5075 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5076 return map;
5077 error:
5078 isl_map_free(map);
5079 return NULL;
5082 /* Given a wrapped map of the form A[B -> C],
5083 * return the map A[B -> C] -> B.
5085 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5087 isl_id *id;
5088 isl_map *map;
5090 if (!set)
5091 return NULL;
5092 if (!isl_set_has_tuple_id(set))
5093 return isl_map_domain_map(isl_set_unwrap(set));
5095 id = isl_set_get_tuple_id(set);
5096 map = isl_map_domain_map(isl_set_unwrap(set));
5097 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5099 return map;
5102 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5103 __isl_take isl_space *dim)
5105 int i;
5106 struct isl_map *map = NULL;
5108 set = isl_set_cow(set);
5109 if (!set || !dim)
5110 goto error;
5111 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5112 map = (struct isl_map *)set;
5113 for (i = 0; i < set->n; ++i) {
5114 map->p[i] = isl_basic_map_from_basic_set(
5115 set->p[i], isl_space_copy(dim));
5116 if (!map->p[i])
5117 goto error;
5119 isl_space_free(map->dim);
5120 map->dim = dim;
5121 return map;
5122 error:
5123 isl_space_free(dim);
5124 isl_set_free(set);
5125 return NULL;
5128 __isl_give isl_basic_map *isl_basic_map_from_domain(
5129 __isl_take isl_basic_set *bset)
5131 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5134 __isl_give isl_basic_map *isl_basic_map_from_range(
5135 __isl_take isl_basic_set *bset)
5137 isl_space *space;
5138 space = isl_basic_set_get_space(bset);
5139 space = isl_space_from_range(space);
5140 bset = isl_basic_set_reset_space(bset, space);
5141 return (isl_basic_map *)bset;
5144 /* Create a relation with the given set as range.
5145 * The domain of the created relation is a zero-dimensional
5146 * flat anonymous space.
5148 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5150 isl_space *space;
5151 space = isl_set_get_space(set);
5152 space = isl_space_from_range(space);
5153 set = isl_set_reset_space(set, space);
5154 return (struct isl_map *)set;
5157 /* Create a relation with the given set as domain.
5158 * The range of the created relation is a zero-dimensional
5159 * flat anonymous space.
5161 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5163 return isl_map_reverse(isl_map_from_range(set));
5166 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5167 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5169 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5172 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5173 __isl_take isl_set *range)
5175 return isl_map_apply_range(isl_map_reverse(domain), range);
5178 struct isl_set *isl_set_from_map(struct isl_map *map)
5180 int i;
5181 struct isl_set *set = NULL;
5183 if (!map)
5184 return NULL;
5185 map = isl_map_cow(map);
5186 if (!map)
5187 return NULL;
5188 map->dim = isl_space_as_set_space(map->dim);
5189 if (!map->dim)
5190 goto error;
5191 set = (struct isl_set *)map;
5192 for (i = 0; i < map->n; ++i) {
5193 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
5194 if (!set->p[i])
5195 goto error;
5197 return set;
5198 error:
5199 isl_map_free(map);
5200 return NULL;
5203 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5204 unsigned flags)
5206 struct isl_map *map;
5208 if (!dim)
5209 return NULL;
5210 if (n < 0)
5211 isl_die(dim->ctx, isl_error_internal,
5212 "negative number of basic maps", goto error);
5213 map = isl_alloc(dim->ctx, struct isl_map,
5214 sizeof(struct isl_map) +
5215 (n - 1) * sizeof(struct isl_basic_map *));
5216 if (!map)
5217 goto error;
5219 map->ctx = dim->ctx;
5220 isl_ctx_ref(map->ctx);
5221 map->ref = 1;
5222 map->size = n;
5223 map->n = 0;
5224 map->dim = dim;
5225 map->flags = flags;
5226 return map;
5227 error:
5228 isl_space_free(dim);
5229 return NULL;
5232 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5233 unsigned nparam, unsigned in, unsigned out, int n,
5234 unsigned flags)
5236 struct isl_map *map;
5237 isl_space *dims;
5239 dims = isl_space_alloc(ctx, nparam, in, out);
5240 if (!dims)
5241 return NULL;
5243 map = isl_map_alloc_space(dims, n, flags);
5244 return map;
5247 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5249 struct isl_basic_map *bmap;
5250 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5251 bmap = isl_basic_map_set_to_empty(bmap);
5252 return bmap;
5255 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5257 struct isl_basic_set *bset;
5258 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5259 bset = isl_basic_set_set_to_empty(bset);
5260 return bset;
5263 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5265 struct isl_basic_map *bmap;
5266 if (!model)
5267 return NULL;
5268 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5269 bmap = isl_basic_map_set_to_empty(bmap);
5270 return bmap;
5273 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5275 struct isl_basic_map *bmap;
5276 if (!model)
5277 return NULL;
5278 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5279 bmap = isl_basic_map_set_to_empty(bmap);
5280 return bmap;
5283 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5285 struct isl_basic_set *bset;
5286 if (!model)
5287 return NULL;
5288 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5289 bset = isl_basic_set_set_to_empty(bset);
5290 return bset;
5293 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5295 struct isl_basic_map *bmap;
5296 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5297 bmap = isl_basic_map_finalize(bmap);
5298 return bmap;
5301 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5303 struct isl_basic_set *bset;
5304 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5305 bset = isl_basic_set_finalize(bset);
5306 return bset;
5309 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5311 int i;
5312 unsigned total = isl_space_dim(dim, isl_dim_all);
5313 isl_basic_map *bmap;
5315 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5316 for (i = 0; i < total; ++i) {
5317 int k = isl_basic_map_alloc_inequality(bmap);
5318 if (k < 0)
5319 goto error;
5320 isl_seq_clr(bmap->ineq[k], 1 + total);
5321 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5323 return bmap;
5324 error:
5325 isl_basic_map_free(bmap);
5326 return NULL;
5329 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5331 return isl_basic_map_nat_universe(dim);
5334 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5336 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5339 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5341 return isl_map_nat_universe(dim);
5344 __isl_give isl_basic_map *isl_basic_map_universe_like(
5345 __isl_keep isl_basic_map *model)
5347 if (!model)
5348 return NULL;
5349 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5352 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5354 if (!model)
5355 return NULL;
5356 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5359 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5360 __isl_keep isl_set *model)
5362 if (!model)
5363 return NULL;
5364 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5367 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5369 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5372 struct isl_map *isl_map_empty_like(struct isl_map *model)
5374 if (!model)
5375 return NULL;
5376 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5379 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5381 if (!model)
5382 return NULL;
5383 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5386 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5388 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5391 struct isl_set *isl_set_empty_like(struct isl_set *model)
5393 if (!model)
5394 return NULL;
5395 return isl_set_empty(isl_space_copy(model->dim));
5398 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5400 struct isl_map *map;
5401 if (!dim)
5402 return NULL;
5403 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5404 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5405 return map;
5408 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5410 struct isl_set *set;
5411 if (!dim)
5412 return NULL;
5413 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5414 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5415 return set;
5418 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5420 if (!model)
5421 return NULL;
5422 return isl_set_universe(isl_space_copy(model->dim));
5425 struct isl_map *isl_map_dup(struct isl_map *map)
5427 int i;
5428 struct isl_map *dup;
5430 if (!map)
5431 return NULL;
5432 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5433 for (i = 0; i < map->n; ++i)
5434 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5435 return dup;
5438 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5439 __isl_take isl_basic_map *bmap)
5441 if (!bmap || !map)
5442 goto error;
5443 if (isl_basic_map_plain_is_empty(bmap)) {
5444 isl_basic_map_free(bmap);
5445 return map;
5447 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5448 isl_assert(map->ctx, map->n < map->size, goto error);
5449 map->p[map->n] = bmap;
5450 map->n++;
5451 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5452 return map;
5453 error:
5454 if (map)
5455 isl_map_free(map);
5456 if (bmap)
5457 isl_basic_map_free(bmap);
5458 return NULL;
5461 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5463 int i;
5465 if (!map)
5466 return NULL;
5468 if (--map->ref > 0)
5469 return NULL;
5471 isl_ctx_deref(map->ctx);
5472 for (i = 0; i < map->n; ++i)
5473 isl_basic_map_free(map->p[i]);
5474 isl_space_free(map->dim);
5475 free(map);
5477 return NULL;
5480 struct isl_map *isl_map_extend(struct isl_map *base,
5481 unsigned nparam, unsigned n_in, unsigned n_out)
5483 int i;
5485 base = isl_map_cow(base);
5486 if (!base)
5487 return NULL;
5489 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5490 if (!base->dim)
5491 goto error;
5492 for (i = 0; i < base->n; ++i) {
5493 base->p[i] = isl_basic_map_extend_space(base->p[i],
5494 isl_space_copy(base->dim), 0, 0, 0);
5495 if (!base->p[i])
5496 goto error;
5498 return base;
5499 error:
5500 isl_map_free(base);
5501 return NULL;
5504 struct isl_set *isl_set_extend(struct isl_set *base,
5505 unsigned nparam, unsigned dim)
5507 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5508 nparam, 0, dim);
5511 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5512 struct isl_basic_map *bmap, unsigned pos, int value)
5514 int j;
5516 bmap = isl_basic_map_cow(bmap);
5517 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5518 j = isl_basic_map_alloc_equality(bmap);
5519 if (j < 0)
5520 goto error;
5521 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5522 isl_int_set_si(bmap->eq[j][pos], -1);
5523 isl_int_set_si(bmap->eq[j][0], value);
5524 bmap = isl_basic_map_simplify(bmap);
5525 return isl_basic_map_finalize(bmap);
5526 error:
5527 isl_basic_map_free(bmap);
5528 return NULL;
5531 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5532 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5534 int j;
5536 bmap = isl_basic_map_cow(bmap);
5537 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5538 j = isl_basic_map_alloc_equality(bmap);
5539 if (j < 0)
5540 goto error;
5541 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5542 isl_int_set_si(bmap->eq[j][pos], -1);
5543 isl_int_set(bmap->eq[j][0], value);
5544 bmap = isl_basic_map_simplify(bmap);
5545 return isl_basic_map_finalize(bmap);
5546 error:
5547 isl_basic_map_free(bmap);
5548 return NULL;
5551 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5552 enum isl_dim_type type, unsigned pos, int value)
5554 if (!bmap)
5555 return NULL;
5556 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5557 return isl_basic_map_fix_pos_si(bmap,
5558 isl_basic_map_offset(bmap, type) + pos, value);
5559 error:
5560 isl_basic_map_free(bmap);
5561 return NULL;
5564 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5565 enum isl_dim_type type, unsigned pos, isl_int value)
5567 if (!bmap)
5568 return NULL;
5569 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5570 return isl_basic_map_fix_pos(bmap,
5571 isl_basic_map_offset(bmap, type) + pos, value);
5572 error:
5573 isl_basic_map_free(bmap);
5574 return NULL;
5577 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5578 * to be equal to "v".
5580 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5581 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5583 if (!bmap || !v)
5584 goto error;
5585 if (!isl_val_is_int(v))
5586 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5587 "expecting integer value", goto error);
5588 if (pos >= isl_basic_map_dim(bmap, type))
5589 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5590 "index out of bounds", goto error);
5591 pos += isl_basic_map_offset(bmap, type);
5592 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5593 isl_val_free(v);
5594 return bmap;
5595 error:
5596 isl_basic_map_free(bmap);
5597 isl_val_free(v);
5598 return NULL;
5601 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5602 * to be equal to "v".
5604 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5605 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5607 return isl_basic_map_fix_val(bset, type, pos, v);
5610 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5611 enum isl_dim_type type, unsigned pos, int value)
5613 return (struct isl_basic_set *)
5614 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5615 type, pos, value);
5618 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5619 enum isl_dim_type type, unsigned pos, isl_int value)
5621 return (struct isl_basic_set *)
5622 isl_basic_map_fix((struct isl_basic_map *)bset,
5623 type, pos, value);
5626 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5627 unsigned input, int value)
5629 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5632 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5633 unsigned dim, int value)
5635 return (struct isl_basic_set *)
5636 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5637 isl_dim_set, dim, value);
5640 static int remove_if_empty(__isl_keep isl_map *map, int i)
5642 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5644 if (empty < 0)
5645 return -1;
5646 if (!empty)
5647 return 0;
5649 isl_basic_map_free(map->p[i]);
5650 if (i != map->n - 1) {
5651 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5652 map->p[i] = map->p[map->n - 1];
5654 map->n--;
5656 return 0;
5659 /* Perform "fn" on each basic map of "map", where we may not be holding
5660 * the only reference to "map".
5661 * In particular, "fn" should be a semantics preserving operation
5662 * that we want to apply to all copies of "map". We therefore need
5663 * to be careful not to modify "map" in a way that breaks "map"
5664 * in case anything goes wrong.
5666 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5667 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5669 struct isl_basic_map *bmap;
5670 int i;
5672 if (!map)
5673 return NULL;
5675 for (i = map->n - 1; i >= 0; --i) {
5676 bmap = isl_basic_map_copy(map->p[i]);
5677 bmap = fn(bmap);
5678 if (!bmap)
5679 goto error;
5680 isl_basic_map_free(map->p[i]);
5681 map->p[i] = bmap;
5682 if (remove_if_empty(map, i) < 0)
5683 goto error;
5686 return map;
5687 error:
5688 isl_map_free(map);
5689 return NULL;
5692 struct isl_map *isl_map_fix_si(struct isl_map *map,
5693 enum isl_dim_type type, unsigned pos, int value)
5695 int i;
5697 map = isl_map_cow(map);
5698 if (!map)
5699 return NULL;
5701 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5702 for (i = map->n - 1; i >= 0; --i) {
5703 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5704 if (remove_if_empty(map, i) < 0)
5705 goto error;
5707 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5708 return map;
5709 error:
5710 isl_map_free(map);
5711 return NULL;
5714 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5715 enum isl_dim_type type, unsigned pos, int value)
5717 return (struct isl_set *)
5718 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5721 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5722 enum isl_dim_type type, unsigned pos, isl_int value)
5724 int i;
5726 map = isl_map_cow(map);
5727 if (!map)
5728 return NULL;
5730 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5731 for (i = 0; i < map->n; ++i) {
5732 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5733 if (!map->p[i])
5734 goto error;
5736 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5737 return map;
5738 error:
5739 isl_map_free(map);
5740 return NULL;
5743 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5744 enum isl_dim_type type, unsigned pos, isl_int value)
5746 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5749 /* Fix the value of the variable at position "pos" of type "type" of "map"
5750 * to be equal to "v".
5752 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5753 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5755 int i;
5757 map = isl_map_cow(map);
5758 if (!map || !v)
5759 goto error;
5761 if (!isl_val_is_int(v))
5762 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5763 "expecting integer value", goto error);
5764 if (pos >= isl_map_dim(map, type))
5765 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5766 "index out of bounds", goto error);
5767 for (i = map->n - 1; i >= 0; --i) {
5768 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5769 isl_val_copy(v));
5770 if (remove_if_empty(map, i) < 0)
5771 goto error;
5773 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5774 isl_val_free(v);
5775 return map;
5776 error:
5777 isl_map_free(map);
5778 isl_val_free(v);
5779 return NULL;
5782 /* Fix the value of the variable at position "pos" of type "type" of "set"
5783 * to be equal to "v".
5785 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5786 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5788 return isl_map_fix_val(set, type, pos, v);
5791 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5792 unsigned input, int value)
5794 return isl_map_fix_si(map, isl_dim_in, input, value);
5797 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5799 return (struct isl_set *)
5800 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5803 static __isl_give isl_basic_map *basic_map_bound_si(
5804 __isl_take isl_basic_map *bmap,
5805 enum isl_dim_type type, unsigned pos, int value, int upper)
5807 int j;
5809 if (!bmap)
5810 return NULL;
5811 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5812 pos += isl_basic_map_offset(bmap, type);
5813 bmap = isl_basic_map_cow(bmap);
5814 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5815 j = isl_basic_map_alloc_inequality(bmap);
5816 if (j < 0)
5817 goto error;
5818 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5819 if (upper) {
5820 isl_int_set_si(bmap->ineq[j][pos], -1);
5821 isl_int_set_si(bmap->ineq[j][0], value);
5822 } else {
5823 isl_int_set_si(bmap->ineq[j][pos], 1);
5824 isl_int_set_si(bmap->ineq[j][0], -value);
5826 bmap = isl_basic_map_simplify(bmap);
5827 return isl_basic_map_finalize(bmap);
5828 error:
5829 isl_basic_map_free(bmap);
5830 return NULL;
5833 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5834 __isl_take isl_basic_map *bmap,
5835 enum isl_dim_type type, unsigned pos, int value)
5837 return basic_map_bound_si(bmap, type, pos, value, 0);
5840 /* Constrain the values of the given dimension to be no greater than "value".
5842 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5843 __isl_take isl_basic_map *bmap,
5844 enum isl_dim_type type, unsigned pos, int value)
5846 return basic_map_bound_si(bmap, type, pos, value, 1);
5849 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5850 unsigned dim, isl_int value)
5852 int j;
5854 bset = isl_basic_set_cow(bset);
5855 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5856 j = isl_basic_set_alloc_inequality(bset);
5857 if (j < 0)
5858 goto error;
5859 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5860 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5861 isl_int_neg(bset->ineq[j][0], value);
5862 bset = isl_basic_set_simplify(bset);
5863 return isl_basic_set_finalize(bset);
5864 error:
5865 isl_basic_set_free(bset);
5866 return NULL;
5869 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5870 enum isl_dim_type type, unsigned pos, int value, int upper)
5872 int i;
5874 map = isl_map_cow(map);
5875 if (!map)
5876 return NULL;
5878 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5879 for (i = 0; i < map->n; ++i) {
5880 map->p[i] = basic_map_bound_si(map->p[i],
5881 type, pos, value, upper);
5882 if (!map->p[i])
5883 goto error;
5885 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5886 return map;
5887 error:
5888 isl_map_free(map);
5889 return NULL;
5892 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5893 enum isl_dim_type type, unsigned pos, int value)
5895 return map_bound_si(map, type, pos, value, 0);
5898 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5899 enum isl_dim_type type, unsigned pos, int value)
5901 return map_bound_si(map, type, pos, value, 1);
5904 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5905 enum isl_dim_type type, unsigned pos, int value)
5907 return (struct isl_set *)
5908 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5911 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5912 enum isl_dim_type type, unsigned pos, int value)
5914 return isl_map_upper_bound_si(set, type, pos, value);
5917 /* Bound the given variable of "bmap" from below (or above is "upper"
5918 * is set) to "value".
5920 static __isl_give isl_basic_map *basic_map_bound(
5921 __isl_take isl_basic_map *bmap,
5922 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5924 int j;
5926 if (!bmap)
5927 return NULL;
5928 if (pos >= isl_basic_map_dim(bmap, type))
5929 isl_die(bmap->ctx, isl_error_invalid,
5930 "index out of bounds", goto error);
5931 pos += isl_basic_map_offset(bmap, type);
5932 bmap = isl_basic_map_cow(bmap);
5933 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5934 j = isl_basic_map_alloc_inequality(bmap);
5935 if (j < 0)
5936 goto error;
5937 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5938 if (upper) {
5939 isl_int_set_si(bmap->ineq[j][pos], -1);
5940 isl_int_set(bmap->ineq[j][0], value);
5941 } else {
5942 isl_int_set_si(bmap->ineq[j][pos], 1);
5943 isl_int_neg(bmap->ineq[j][0], value);
5945 bmap = isl_basic_map_simplify(bmap);
5946 return isl_basic_map_finalize(bmap);
5947 error:
5948 isl_basic_map_free(bmap);
5949 return NULL;
5952 /* Bound the given variable of "map" from below (or above is "upper"
5953 * is set) to "value".
5955 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5956 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5958 int i;
5960 map = isl_map_cow(map);
5961 if (!map)
5962 return NULL;
5964 if (pos >= isl_map_dim(map, type))
5965 isl_die(map->ctx, isl_error_invalid,
5966 "index out of bounds", goto error);
5967 for (i = map->n - 1; i >= 0; --i) {
5968 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5969 if (remove_if_empty(map, i) < 0)
5970 goto error;
5972 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5973 return map;
5974 error:
5975 isl_map_free(map);
5976 return NULL;
5979 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5980 enum isl_dim_type type, unsigned pos, isl_int value)
5982 return map_bound(map, type, pos, value, 0);
5985 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5986 enum isl_dim_type type, unsigned pos, isl_int value)
5988 return map_bound(map, type, pos, value, 1);
5991 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5992 enum isl_dim_type type, unsigned pos, isl_int value)
5994 return isl_map_lower_bound(set, type, pos, value);
5997 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5998 enum isl_dim_type type, unsigned pos, isl_int value)
6000 return isl_map_upper_bound(set, type, pos, value);
6003 /* Force the values of the variable at position "pos" of type "type" of "set"
6004 * to be no smaller than "value".
6006 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6007 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6009 if (!value)
6010 goto error;
6011 if (!isl_val_is_int(value))
6012 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6013 "expecting integer value", goto error);
6014 set = isl_set_lower_bound(set, type, pos, value->n);
6015 isl_val_free(value);
6016 return set;
6017 error:
6018 isl_val_free(value);
6019 isl_set_free(set);
6020 return NULL;
6023 /* Force the values of the variable at position "pos" of type "type" of "set"
6024 * to be no greater than "value".
6026 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6027 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6029 if (!value)
6030 goto error;
6031 if (!isl_val_is_int(value))
6032 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6033 "expecting integer value", goto error);
6034 set = isl_set_upper_bound(set, type, pos, value->n);
6035 isl_val_free(value);
6036 return set;
6037 error:
6038 isl_val_free(value);
6039 isl_set_free(set);
6040 return NULL;
6043 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6044 isl_int value)
6046 int i;
6048 set = isl_set_cow(set);
6049 if (!set)
6050 return NULL;
6052 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6053 for (i = 0; i < set->n; ++i) {
6054 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6055 if (!set->p[i])
6056 goto error;
6058 return set;
6059 error:
6060 isl_set_free(set);
6061 return NULL;
6064 struct isl_map *isl_map_reverse(struct isl_map *map)
6066 int i;
6068 map = isl_map_cow(map);
6069 if (!map)
6070 return NULL;
6072 map->dim = isl_space_reverse(map->dim);
6073 if (!map->dim)
6074 goto error;
6075 for (i = 0; i < map->n; ++i) {
6076 map->p[i] = isl_basic_map_reverse(map->p[i]);
6077 if (!map->p[i])
6078 goto error;
6080 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6081 return map;
6082 error:
6083 isl_map_free(map);
6084 return NULL;
6087 static struct isl_map *isl_basic_map_partial_lexopt(
6088 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6089 struct isl_set **empty, int max)
6091 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6094 struct isl_map *isl_basic_map_partial_lexmax(
6095 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6096 struct isl_set **empty)
6098 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6101 struct isl_map *isl_basic_map_partial_lexmin(
6102 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6103 struct isl_set **empty)
6105 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6108 struct isl_set *isl_basic_set_partial_lexmin(
6109 struct isl_basic_set *bset, struct isl_basic_set *dom,
6110 struct isl_set **empty)
6112 return (struct isl_set *)
6113 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6114 dom, empty);
6117 struct isl_set *isl_basic_set_partial_lexmax(
6118 struct isl_basic_set *bset, struct isl_basic_set *dom,
6119 struct isl_set **empty)
6121 return (struct isl_set *)
6122 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6123 dom, empty);
6126 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6127 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6128 __isl_give isl_set **empty)
6130 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6133 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6134 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6135 __isl_give isl_set **empty)
6137 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6140 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6141 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6142 __isl_give isl_set **empty)
6144 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6147 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6148 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6149 __isl_give isl_set **empty)
6151 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6154 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6155 __isl_take isl_basic_map *bmap, int max)
6157 isl_basic_set *dom = NULL;
6158 isl_space *dom_space;
6160 if (!bmap)
6161 goto error;
6162 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6163 dom = isl_basic_set_universe(dom_space);
6164 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6165 error:
6166 isl_basic_map_free(bmap);
6167 return NULL;
6170 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6171 __isl_take isl_basic_map *bmap)
6173 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6176 #undef TYPE
6177 #define TYPE isl_pw_multi_aff
6178 #undef SUFFIX
6179 #define SUFFIX _pw_multi_aff
6180 #undef EMPTY
6181 #define EMPTY isl_pw_multi_aff_empty
6182 #undef ADD
6183 #define ADD isl_pw_multi_aff_union_add
6184 #include "isl_map_lexopt_templ.c"
6186 /* Given a map "map", compute the lexicographically minimal
6187 * (or maximal) image element for each domain element in dom,
6188 * in the form of an isl_pw_multi_aff.
6189 * Set *empty to those elements in dom that do not have an image element.
6191 * We first compute the lexicographically minimal or maximal element
6192 * in the first basic map. This results in a partial solution "res"
6193 * and a subset "todo" of dom that still need to be handled.
6194 * We then consider each of the remaining maps in "map" and successively
6195 * update both "res" and "todo".
6197 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6198 __isl_take isl_map *map, __isl_take isl_set *dom,
6199 __isl_give isl_set **empty, int max)
6201 int i;
6202 isl_pw_multi_aff *res;
6203 isl_set *todo;
6205 if (!map || !dom)
6206 goto error;
6208 if (isl_map_plain_is_empty(map)) {
6209 if (empty)
6210 *empty = dom;
6211 else
6212 isl_set_free(dom);
6213 return isl_pw_multi_aff_from_map(map);
6216 res = basic_map_partial_lexopt_pw_multi_aff(
6217 isl_basic_map_copy(map->p[0]),
6218 isl_set_copy(dom), &todo, max);
6220 for (i = 1; i < map->n; ++i) {
6221 isl_pw_multi_aff *res_i;
6222 isl_set *todo_i;
6224 res_i = basic_map_partial_lexopt_pw_multi_aff(
6225 isl_basic_map_copy(map->p[i]),
6226 isl_set_copy(dom), &todo_i, max);
6228 if (max)
6229 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6230 else
6231 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6233 todo = isl_set_intersect(todo, todo_i);
6236 isl_set_free(dom);
6237 isl_map_free(map);
6239 if (empty)
6240 *empty = todo;
6241 else
6242 isl_set_free(todo);
6244 return res;
6245 error:
6246 if (empty)
6247 *empty = NULL;
6248 isl_set_free(dom);
6249 isl_map_free(map);
6250 return NULL;
6253 #undef TYPE
6254 #define TYPE isl_map
6255 #undef SUFFIX
6256 #define SUFFIX
6257 #undef EMPTY
6258 #define EMPTY isl_map_empty
6259 #undef ADD
6260 #define ADD isl_map_union_disjoint
6261 #include "isl_map_lexopt_templ.c"
6263 /* Given a map "map", compute the lexicographically minimal
6264 * (or maximal) image element for each domain element in dom.
6265 * Set *empty to those elements in dom that do not have an image element.
6267 * We first compute the lexicographically minimal or maximal element
6268 * in the first basic map. This results in a partial solution "res"
6269 * and a subset "todo" of dom that still need to be handled.
6270 * We then consider each of the remaining maps in "map" and successively
6271 * update both "res" and "todo".
6273 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6274 * Assume we are computing the lexicographical maximum.
6275 * We first compute the lexicographically maximal element in basic map i.
6276 * This results in a partial solution res_i and a subset todo_i.
6277 * Then we combine these results with those obtain for the first k basic maps
6278 * to obtain a result that is valid for the first k+1 basic maps.
6279 * In particular, the set where there is no solution is the set where
6280 * there is no solution for the first k basic maps and also no solution
6281 * for the ith basic map, i.e.,
6283 * todo^i = todo^k * todo_i
6285 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6286 * solutions, arbitrarily breaking ties in favor of res^k.
6287 * That is, when res^k(a) >= res_i(a), we pick res^k and
6288 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6289 * the lexicographic order.)
6290 * In practice, we compute
6292 * res^k * (res_i . "<=")
6294 * and
6296 * res_i * (res^k . "<")
6298 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6299 * where only one of res^k and res_i provides a solution and we simply pick
6300 * that one, i.e.,
6302 * res^k * todo_i
6303 * and
6304 * res_i * todo^k
6306 * Note that we only compute these intersections when dom(res^k) intersects
6307 * dom(res_i). Otherwise, the only effect of these intersections is to
6308 * potentially break up res^k and res_i into smaller pieces.
6309 * We want to avoid such splintering as much as possible.
6310 * In fact, an earlier implementation of this function would look for
6311 * better results in the domain of res^k and for extra results in todo^k,
6312 * but this would always result in a splintering according to todo^k,
6313 * even when the domain of basic map i is disjoint from the domains of
6314 * the previous basic maps.
6316 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6317 __isl_take isl_map *map, __isl_take isl_set *dom,
6318 __isl_give isl_set **empty, int max)
6320 int i;
6321 struct isl_map *res;
6322 struct isl_set *todo;
6324 if (!map || !dom)
6325 goto error;
6327 if (isl_map_plain_is_empty(map)) {
6328 if (empty)
6329 *empty = dom;
6330 else
6331 isl_set_free(dom);
6332 return map;
6335 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6336 isl_set_copy(dom), &todo, max);
6338 for (i = 1; i < map->n; ++i) {
6339 isl_map *lt, *le;
6340 isl_map *res_i;
6341 isl_set *todo_i;
6342 isl_space *dim = isl_space_range(isl_map_get_space(res));
6344 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6345 isl_set_copy(dom), &todo_i, max);
6347 if (max) {
6348 lt = isl_map_lex_lt(isl_space_copy(dim));
6349 le = isl_map_lex_le(dim);
6350 } else {
6351 lt = isl_map_lex_gt(isl_space_copy(dim));
6352 le = isl_map_lex_ge(dim);
6354 lt = isl_map_apply_range(isl_map_copy(res), lt);
6355 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6356 le = isl_map_apply_range(isl_map_copy(res_i), le);
6357 le = isl_map_intersect(le, isl_map_copy(res));
6359 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6360 res = isl_map_intersect_domain(res,
6361 isl_set_copy(todo_i));
6362 res_i = isl_map_intersect_domain(res_i,
6363 isl_set_copy(todo));
6366 res = isl_map_union_disjoint(res, res_i);
6367 res = isl_map_union_disjoint(res, lt);
6368 res = isl_map_union_disjoint(res, le);
6370 todo = isl_set_intersect(todo, todo_i);
6373 isl_set_free(dom);
6374 isl_map_free(map);
6376 if (empty)
6377 *empty = todo;
6378 else
6379 isl_set_free(todo);
6381 return res;
6382 error:
6383 if (empty)
6384 *empty = NULL;
6385 isl_set_free(dom);
6386 isl_map_free(map);
6387 return NULL;
6390 __isl_give isl_map *isl_map_partial_lexmax(
6391 __isl_take isl_map *map, __isl_take isl_set *dom,
6392 __isl_give isl_set **empty)
6394 return isl_map_partial_lexopt(map, dom, empty, 1);
6397 __isl_give isl_map *isl_map_partial_lexmin(
6398 __isl_take isl_map *map, __isl_take isl_set *dom,
6399 __isl_give isl_set **empty)
6401 return isl_map_partial_lexopt(map, dom, empty, 0);
6404 __isl_give isl_set *isl_set_partial_lexmin(
6405 __isl_take isl_set *set, __isl_take isl_set *dom,
6406 __isl_give isl_set **empty)
6408 return (struct isl_set *)
6409 isl_map_partial_lexmin((struct isl_map *)set,
6410 dom, empty);
6413 __isl_give isl_set *isl_set_partial_lexmax(
6414 __isl_take isl_set *set, __isl_take isl_set *dom,
6415 __isl_give isl_set **empty)
6417 return (struct isl_set *)
6418 isl_map_partial_lexmax((struct isl_map *)set,
6419 dom, empty);
6422 /* Compute the lexicographic minimum (or maximum if "max" is set)
6423 * of "bmap" over its domain.
6425 * Since we are not interested in the part of the domain space where
6426 * there is no solution, we initialize the domain to those constraints
6427 * of "bmap" that only involve the parameters and the input dimensions.
6428 * This relieves the parametric programming engine from detecting those
6429 * inequalities and transferring them to the context. More importantly,
6430 * it ensures that those inequalities are transferred first and not
6431 * intermixed with inequalities that actually split the domain.
6433 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6435 int n_div;
6436 int n_out;
6437 isl_basic_map *copy;
6438 isl_basic_set *dom;
6440 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6441 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6442 copy = isl_basic_map_copy(bmap);
6443 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6444 isl_dim_div, 0, n_div);
6445 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6446 isl_dim_out, 0, n_out);
6447 dom = isl_basic_map_domain(copy);
6448 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6451 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6453 return isl_basic_map_lexopt(bmap, 0);
6456 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6458 return isl_basic_map_lexopt(bmap, 1);
6461 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6463 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6466 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6468 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6471 /* Extract the first and only affine expression from list
6472 * and then add it to *pwaff with the given dom.
6473 * This domain is known to be disjoint from other domains
6474 * because of the way isl_basic_map_foreach_lexmax works.
6476 static int update_dim_opt(__isl_take isl_basic_set *dom,
6477 __isl_take isl_aff_list *list, void *user)
6479 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6480 isl_aff *aff;
6481 isl_pw_aff **pwaff = user;
6482 isl_pw_aff *pwaff_i;
6484 if (!list)
6485 goto error;
6486 if (isl_aff_list_n_aff(list) != 1)
6487 isl_die(ctx, isl_error_internal,
6488 "expecting single element list", goto error);
6490 aff = isl_aff_list_get_aff(list, 0);
6491 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6493 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6495 isl_aff_list_free(list);
6497 return 0;
6498 error:
6499 isl_basic_set_free(dom);
6500 isl_aff_list_free(list);
6501 return -1;
6504 /* Given a basic map with one output dimension, compute the minimum or
6505 * maximum of that dimension as an isl_pw_aff.
6507 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6508 * call update_dim_opt on each leaf of the result.
6510 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6511 int max)
6513 isl_space *dim = isl_basic_map_get_space(bmap);
6514 isl_pw_aff *pwaff;
6515 int r;
6517 dim = isl_space_from_domain(isl_space_domain(dim));
6518 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6519 pwaff = isl_pw_aff_empty(dim);
6521 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6522 if (r < 0)
6523 return isl_pw_aff_free(pwaff);
6525 return pwaff;
6528 /* Compute the minimum or maximum of the given output dimension
6529 * as a function of the parameters and the input dimensions,
6530 * but independently of the other output dimensions.
6532 * We first project out the other output dimension and then compute
6533 * the "lexicographic" maximum in each basic map, combining the results
6534 * using isl_pw_aff_union_max.
6536 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6537 int max)
6539 int i;
6540 isl_pw_aff *pwaff;
6541 unsigned n_out;
6543 n_out = isl_map_dim(map, isl_dim_out);
6544 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6545 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6546 if (!map)
6547 return NULL;
6549 if (map->n == 0) {
6550 isl_space *dim = isl_map_get_space(map);
6551 isl_map_free(map);
6552 return isl_pw_aff_empty(dim);
6555 pwaff = basic_map_dim_opt(map->p[0], max);
6556 for (i = 1; i < map->n; ++i) {
6557 isl_pw_aff *pwaff_i;
6559 pwaff_i = basic_map_dim_opt(map->p[i], max);
6560 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6563 isl_map_free(map);
6565 return pwaff;
6568 /* Compute the maximum of the given output dimension as a function of the
6569 * parameters and input dimensions, but independently of
6570 * the other output dimensions.
6572 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6574 return map_dim_opt(map, pos, 1);
6577 /* Compute the minimum or maximum of the given set dimension
6578 * as a function of the parameters,
6579 * but independently of the other set dimensions.
6581 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6582 int max)
6584 return map_dim_opt(set, pos, max);
6587 /* Compute the maximum of the given set dimension as a function of the
6588 * parameters, but independently of the other set dimensions.
6590 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6592 return set_dim_opt(set, pos, 1);
6595 /* Compute the minimum of the given set dimension as a function of the
6596 * parameters, but independently of the other set dimensions.
6598 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6600 return set_dim_opt(set, pos, 0);
6603 /* Apply a preimage specified by "mat" on the parameters of "bset".
6604 * bset is assumed to have only parameters and divs.
6606 static struct isl_basic_set *basic_set_parameter_preimage(
6607 struct isl_basic_set *bset, struct isl_mat *mat)
6609 unsigned nparam;
6611 if (!bset || !mat)
6612 goto error;
6614 bset->dim = isl_space_cow(bset->dim);
6615 if (!bset->dim)
6616 goto error;
6618 nparam = isl_basic_set_dim(bset, isl_dim_param);
6620 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6622 bset->dim->nparam = 0;
6623 bset->dim->n_out = nparam;
6624 bset = isl_basic_set_preimage(bset, mat);
6625 if (bset) {
6626 bset->dim->nparam = bset->dim->n_out;
6627 bset->dim->n_out = 0;
6629 return bset;
6630 error:
6631 isl_mat_free(mat);
6632 isl_basic_set_free(bset);
6633 return NULL;
6636 /* Apply a preimage specified by "mat" on the parameters of "set".
6637 * set is assumed to have only parameters and divs.
6639 static struct isl_set *set_parameter_preimage(
6640 struct isl_set *set, struct isl_mat *mat)
6642 isl_space *dim = NULL;
6643 unsigned nparam;
6645 if (!set || !mat)
6646 goto error;
6648 dim = isl_space_copy(set->dim);
6649 dim = isl_space_cow(dim);
6650 if (!dim)
6651 goto error;
6653 nparam = isl_set_dim(set, isl_dim_param);
6655 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6657 dim->nparam = 0;
6658 dim->n_out = nparam;
6659 isl_set_reset_space(set, dim);
6660 set = isl_set_preimage(set, mat);
6661 if (!set)
6662 goto error2;
6663 dim = isl_space_copy(set->dim);
6664 dim = isl_space_cow(dim);
6665 if (!dim)
6666 goto error2;
6667 dim->nparam = dim->n_out;
6668 dim->n_out = 0;
6669 isl_set_reset_space(set, dim);
6670 return set;
6671 error:
6672 isl_space_free(dim);
6673 isl_mat_free(mat);
6674 error2:
6675 isl_set_free(set);
6676 return NULL;
6679 /* Intersect the basic set "bset" with the affine space specified by the
6680 * equalities in "eq".
6682 static struct isl_basic_set *basic_set_append_equalities(
6683 struct isl_basic_set *bset, struct isl_mat *eq)
6685 int i, k;
6686 unsigned len;
6688 if (!bset || !eq)
6689 goto error;
6691 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6692 eq->n_row, 0);
6693 if (!bset)
6694 goto error;
6696 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6697 for (i = 0; i < eq->n_row; ++i) {
6698 k = isl_basic_set_alloc_equality(bset);
6699 if (k < 0)
6700 goto error;
6701 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6702 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6704 isl_mat_free(eq);
6706 bset = isl_basic_set_gauss(bset, NULL);
6707 bset = isl_basic_set_finalize(bset);
6709 return bset;
6710 error:
6711 isl_mat_free(eq);
6712 isl_basic_set_free(bset);
6713 return NULL;
6716 /* Intersect the set "set" with the affine space specified by the
6717 * equalities in "eq".
6719 static struct isl_set *set_append_equalities(struct isl_set *set,
6720 struct isl_mat *eq)
6722 int i;
6724 if (!set || !eq)
6725 goto error;
6727 for (i = 0; i < set->n; ++i) {
6728 set->p[i] = basic_set_append_equalities(set->p[i],
6729 isl_mat_copy(eq));
6730 if (!set->p[i])
6731 goto error;
6733 isl_mat_free(eq);
6734 return set;
6735 error:
6736 isl_mat_free(eq);
6737 isl_set_free(set);
6738 return NULL;
6741 /* Given a basic set "bset" that only involves parameters and existentially
6742 * quantified variables, return the index of the first equality
6743 * that only involves parameters. If there is no such equality then
6744 * return bset->n_eq.
6746 * This function assumes that isl_basic_set_gauss has been called on "bset".
6748 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6750 int i, j;
6751 unsigned nparam, n_div;
6753 if (!bset)
6754 return -1;
6756 nparam = isl_basic_set_dim(bset, isl_dim_param);
6757 n_div = isl_basic_set_dim(bset, isl_dim_div);
6759 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6760 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6761 ++i;
6764 return i;
6767 /* Compute an explicit representation for the existentially quantified
6768 * variables in "bset" by computing the "minimal value" of the set
6769 * variables. Since there are no set variables, the computation of
6770 * the minimal value essentially computes an explicit representation
6771 * of the non-empty part(s) of "bset".
6773 * The input only involves parameters and existentially quantified variables.
6774 * All equalities among parameters have been removed.
6776 * Since the existentially quantified variables in the result are in general
6777 * going to be different from those in the input, we first replace
6778 * them by the minimal number of variables based on their equalities.
6779 * This should simplify the parametric integer programming.
6781 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6783 isl_morph *morph1, *morph2;
6784 isl_set *set;
6785 unsigned n;
6787 if (!bset)
6788 return NULL;
6789 if (bset->n_eq == 0)
6790 return isl_basic_set_lexmin(bset);
6792 morph1 = isl_basic_set_parameter_compression(bset);
6793 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6794 bset = isl_basic_set_lift(bset);
6795 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6796 bset = isl_morph_basic_set(morph2, bset);
6797 n = isl_basic_set_dim(bset, isl_dim_set);
6798 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6800 set = isl_basic_set_lexmin(bset);
6802 set = isl_morph_set(isl_morph_inverse(morph1), set);
6804 return set;
6807 /* Project the given basic set onto its parameter domain, possibly introducing
6808 * new, explicit, existential variables in the constraints.
6809 * The input has parameters and (possibly implicit) existential variables.
6810 * The output has the same parameters, but only
6811 * explicit existentially quantified variables.
6813 * The actual projection is performed by pip, but pip doesn't seem
6814 * to like equalities very much, so we first remove the equalities
6815 * among the parameters by performing a variable compression on
6816 * the parameters. Afterward, an inverse transformation is performed
6817 * and the equalities among the parameters are inserted back in.
6819 * The variable compression on the parameters may uncover additional
6820 * equalities that were only implicit before. We therefore check
6821 * if there are any new parameter equalities in the result and
6822 * if so recurse. The removal of parameter equalities is required
6823 * for the parameter compression performed by base_compute_divs.
6825 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6827 int i;
6828 struct isl_mat *eq;
6829 struct isl_mat *T, *T2;
6830 struct isl_set *set;
6831 unsigned nparam;
6833 bset = isl_basic_set_cow(bset);
6834 if (!bset)
6835 return NULL;
6837 if (bset->n_eq == 0)
6838 return base_compute_divs(bset);
6840 bset = isl_basic_set_gauss(bset, NULL);
6841 if (!bset)
6842 return NULL;
6843 if (isl_basic_set_plain_is_empty(bset))
6844 return isl_set_from_basic_set(bset);
6846 i = first_parameter_equality(bset);
6847 if (i == bset->n_eq)
6848 return base_compute_divs(bset);
6850 nparam = isl_basic_set_dim(bset, isl_dim_param);
6851 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6852 0, 1 + nparam);
6853 eq = isl_mat_cow(eq);
6854 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6855 if (T && T->n_col == 0) {
6856 isl_mat_free(T);
6857 isl_mat_free(T2);
6858 isl_mat_free(eq);
6859 bset = isl_basic_set_set_to_empty(bset);
6860 return isl_set_from_basic_set(bset);
6862 bset = basic_set_parameter_preimage(bset, T);
6864 i = first_parameter_equality(bset);
6865 if (!bset)
6866 set = NULL;
6867 else if (i == bset->n_eq)
6868 set = base_compute_divs(bset);
6869 else
6870 set = parameter_compute_divs(bset);
6871 set = set_parameter_preimage(set, T2);
6872 set = set_append_equalities(set, eq);
6873 return set;
6876 /* Insert the divs from "ls" before those of "bmap".
6878 * The number of columns is not changed, which means that the last
6879 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6880 * The caller is responsible for removing the same number of dimensions
6881 * from the space of "bmap".
6883 static __isl_give isl_basic_map *insert_divs_from_local_space(
6884 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6886 int i;
6887 int n_div;
6888 int old_n_div;
6890 n_div = isl_local_space_dim(ls, isl_dim_div);
6891 if (n_div == 0)
6892 return bmap;
6894 old_n_div = bmap->n_div;
6895 bmap = insert_div_rows(bmap, n_div);
6896 if (!bmap)
6897 return NULL;
6899 for (i = 0; i < n_div; ++i) {
6900 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6901 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6904 return bmap;
6907 /* Replace the space of "bmap" by the space and divs of "ls".
6909 * If "ls" has any divs, then we simplify the result since we may
6910 * have discovered some additional equalities that could simplify
6911 * the div expressions.
6913 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6914 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6916 int n_div;
6918 bmap = isl_basic_map_cow(bmap);
6919 if (!bmap || !ls)
6920 goto error;
6922 n_div = isl_local_space_dim(ls, isl_dim_div);
6923 bmap = insert_divs_from_local_space(bmap, ls);
6924 if (!bmap)
6925 goto error;
6927 isl_space_free(bmap->dim);
6928 bmap->dim = isl_local_space_get_space(ls);
6929 if (!bmap->dim)
6930 goto error;
6932 isl_local_space_free(ls);
6933 if (n_div > 0)
6934 bmap = isl_basic_map_simplify(bmap);
6935 bmap = isl_basic_map_finalize(bmap);
6936 return bmap;
6937 error:
6938 isl_basic_map_free(bmap);
6939 isl_local_space_free(ls);
6940 return NULL;
6943 /* Replace the space of "map" by the space and divs of "ls".
6945 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6946 __isl_take isl_local_space *ls)
6948 int i;
6950 map = isl_map_cow(map);
6951 if (!map || !ls)
6952 goto error;
6954 for (i = 0; i < map->n; ++i) {
6955 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6956 isl_local_space_copy(ls));
6957 if (!map->p[i])
6958 goto error;
6960 isl_space_free(map->dim);
6961 map->dim = isl_local_space_get_space(ls);
6962 if (!map->dim)
6963 goto error;
6965 isl_local_space_free(ls);
6966 return map;
6967 error:
6968 isl_local_space_free(ls);
6969 isl_map_free(map);
6970 return NULL;
6973 /* Compute an explicit representation for the existentially
6974 * quantified variables for which do not know any explicit representation yet.
6976 * We first sort the existentially quantified variables so that the
6977 * existentially quantified variables for which we already have an explicit
6978 * representation are placed before those for which we do not.
6979 * The input dimensions, the output dimensions and the existentially
6980 * quantified variables for which we already have an explicit
6981 * representation are then turned into parameters.
6982 * compute_divs returns a map with the same parameters and
6983 * no input or output dimensions and the dimension specification
6984 * is reset to that of the input, including the existentially quantified
6985 * variables for which we already had an explicit representation.
6987 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6989 struct isl_basic_set *bset;
6990 struct isl_set *set;
6991 struct isl_map *map;
6992 isl_space *dim;
6993 isl_local_space *ls;
6994 unsigned nparam;
6995 unsigned n_in;
6996 unsigned n_out;
6997 unsigned n_known;
6998 int i;
7000 bmap = isl_basic_map_sort_divs(bmap);
7001 bmap = isl_basic_map_cow(bmap);
7002 if (!bmap)
7003 return NULL;
7005 for (n_known = 0; n_known < bmap->n_div; ++n_known)
7006 if (isl_int_is_zero(bmap->div[n_known][0]))
7007 break;
7009 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7010 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7011 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7012 dim = isl_space_set_alloc(bmap->ctx,
7013 nparam + n_in + n_out + n_known, 0);
7014 if (!dim)
7015 goto error;
7017 ls = isl_basic_map_get_local_space(bmap);
7018 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7019 n_known, bmap->n_div - n_known);
7020 if (n_known > 0) {
7021 for (i = n_known; i < bmap->n_div; ++i)
7022 swap_div(bmap, i - n_known, i);
7023 bmap->n_div -= n_known;
7024 bmap->extra -= n_known;
7026 bmap = isl_basic_map_reset_space(bmap, dim);
7027 bset = (struct isl_basic_set *)bmap;
7029 set = parameter_compute_divs(bset);
7030 map = (struct isl_map *)set;
7031 map = replace_space_by_local_space(map, ls);
7033 return map;
7034 error:
7035 isl_basic_map_free(bmap);
7036 return NULL;
7039 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7041 int i;
7042 unsigned off;
7044 if (!bmap)
7045 return -1;
7047 off = isl_space_dim(bmap->dim, isl_dim_all);
7048 for (i = 0; i < bmap->n_div; ++i) {
7049 if (isl_int_is_zero(bmap->div[i][0]))
7050 return 0;
7051 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
7052 return -1);
7054 return 1;
7057 static int map_divs_known(__isl_keep isl_map *map)
7059 int i;
7061 if (!map)
7062 return -1;
7064 for (i = 0; i < map->n; ++i) {
7065 int known = isl_basic_map_divs_known(map->p[i]);
7066 if (known <= 0)
7067 return known;
7070 return 1;
7073 /* If bmap contains any unknown divs, then compute explicit
7074 * expressions for them. However, this computation may be
7075 * quite expensive, so first try to remove divs that aren't
7076 * strictly needed.
7078 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7080 int known;
7081 struct isl_map *map;
7083 known = isl_basic_map_divs_known(bmap);
7084 if (known < 0)
7085 goto error;
7086 if (known)
7087 return isl_map_from_basic_map(bmap);
7089 bmap = isl_basic_map_drop_redundant_divs(bmap);
7091 known = isl_basic_map_divs_known(bmap);
7092 if (known < 0)
7093 goto error;
7094 if (known)
7095 return isl_map_from_basic_map(bmap);
7097 map = compute_divs(bmap);
7098 return map;
7099 error:
7100 isl_basic_map_free(bmap);
7101 return NULL;
7104 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7106 int i;
7107 int known;
7108 struct isl_map *res;
7110 if (!map)
7111 return NULL;
7112 if (map->n == 0)
7113 return map;
7115 known = map_divs_known(map);
7116 if (known < 0) {
7117 isl_map_free(map);
7118 return NULL;
7120 if (known)
7121 return map;
7123 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7124 for (i = 1 ; i < map->n; ++i) {
7125 struct isl_map *r2;
7126 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7127 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7128 res = isl_map_union_disjoint(res, r2);
7129 else
7130 res = isl_map_union(res, r2);
7132 isl_map_free(map);
7134 return res;
7137 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7139 return (struct isl_set *)
7140 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7143 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7145 return (struct isl_set *)
7146 isl_map_compute_divs((struct isl_map *)set);
7149 struct isl_set *isl_map_domain(struct isl_map *map)
7151 int i;
7152 struct isl_set *set;
7154 if (!map)
7155 goto error;
7157 map = isl_map_cow(map);
7158 if (!map)
7159 return NULL;
7161 set = (struct isl_set *)map;
7162 set->dim = isl_space_domain(set->dim);
7163 if (!set->dim)
7164 goto error;
7165 for (i = 0; i < map->n; ++i) {
7166 set->p[i] = isl_basic_map_domain(map->p[i]);
7167 if (!set->p[i])
7168 goto error;
7170 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7171 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7172 return set;
7173 error:
7174 isl_map_free(map);
7175 return NULL;
7178 /* Return the union of "map1" and "map2", where we assume for now that
7179 * "map1" and "map2" are disjoint. Note that the basic maps inside
7180 * "map1" or "map2" may not be disjoint from each other.
7181 * Also note that this function is also called from isl_map_union,
7182 * which takes care of handling the situation where "map1" and "map2"
7183 * may not be disjoint.
7185 * If one of the inputs is empty, we can simply return the other input.
7186 * Similarly, if one of the inputs is universal, then it is equal to the union.
7188 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7189 __isl_take isl_map *map2)
7191 int i;
7192 unsigned flags = 0;
7193 struct isl_map *map = NULL;
7194 int is_universe;
7196 if (!map1 || !map2)
7197 goto error;
7199 if (!isl_space_is_equal(map1->dim, map2->dim))
7200 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7201 "spaces don't match", goto error);
7203 if (map1->n == 0) {
7204 isl_map_free(map1);
7205 return map2;
7207 if (map2->n == 0) {
7208 isl_map_free(map2);
7209 return map1;
7212 is_universe = isl_map_plain_is_universe(map1);
7213 if (is_universe < 0)
7214 goto error;
7215 if (is_universe) {
7216 isl_map_free(map2);
7217 return map1;
7220 is_universe = isl_map_plain_is_universe(map2);
7221 if (is_universe < 0)
7222 goto error;
7223 if (is_universe) {
7224 isl_map_free(map1);
7225 return map2;
7228 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7229 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7230 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7232 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7233 map1->n + map2->n, flags);
7234 if (!map)
7235 goto error;
7236 for (i = 0; i < map1->n; ++i) {
7237 map = isl_map_add_basic_map(map,
7238 isl_basic_map_copy(map1->p[i]));
7239 if (!map)
7240 goto error;
7242 for (i = 0; i < map2->n; ++i) {
7243 map = isl_map_add_basic_map(map,
7244 isl_basic_map_copy(map2->p[i]));
7245 if (!map)
7246 goto error;
7248 isl_map_free(map1);
7249 isl_map_free(map2);
7250 return map;
7251 error:
7252 isl_map_free(map);
7253 isl_map_free(map1);
7254 isl_map_free(map2);
7255 return NULL;
7258 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7259 * guaranteed to be disjoint by the caller.
7261 * Note that this functions is called from within isl_map_make_disjoint,
7262 * so we have to be careful not to touch the constraints of the inputs
7263 * in any way.
7265 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7266 __isl_take isl_map *map2)
7268 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7271 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7272 * not be disjoint. The parameters are assumed to have been aligned.
7274 * We currently simply call map_union_disjoint, the internal operation
7275 * of which does not really depend on the inputs being disjoint.
7276 * If the result contains more than one basic map, then we clear
7277 * the disjoint flag since the result may contain basic maps from
7278 * both inputs and these are not guaranteed to be disjoint.
7280 * As a special case, if "map1" and "map2" are obviously equal,
7281 * then we simply return "map1".
7283 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7284 __isl_take isl_map *map2)
7286 int equal;
7288 if (!map1 || !map2)
7289 goto error;
7291 equal = isl_map_plain_is_equal(map1, map2);
7292 if (equal < 0)
7293 goto error;
7294 if (equal) {
7295 isl_map_free(map2);
7296 return map1;
7299 map1 = map_union_disjoint(map1, map2);
7300 if (!map1)
7301 return NULL;
7302 if (map1->n > 1)
7303 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7304 return map1;
7305 error:
7306 isl_map_free(map1);
7307 isl_map_free(map2);
7308 return NULL;
7311 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7312 * not be disjoint.
7314 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7315 __isl_take isl_map *map2)
7317 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7320 struct isl_set *isl_set_union_disjoint(
7321 struct isl_set *set1, struct isl_set *set2)
7323 return (struct isl_set *)
7324 isl_map_union_disjoint(
7325 (struct isl_map *)set1, (struct isl_map *)set2);
7328 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7330 return (struct isl_set *)
7331 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7334 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7335 * the results.
7337 * "map" and "set" are assumed to be compatible and non-NULL.
7339 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7340 __isl_take isl_set *set,
7341 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7342 __isl_take isl_basic_set *bset))
7344 unsigned flags = 0;
7345 struct isl_map *result;
7346 int i, j;
7348 if (isl_set_plain_is_universe(set)) {
7349 isl_set_free(set);
7350 return map;
7353 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7354 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7355 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7357 result = isl_map_alloc_space(isl_space_copy(map->dim),
7358 map->n * set->n, flags);
7359 for (i = 0; result && i < map->n; ++i)
7360 for (j = 0; j < set->n; ++j) {
7361 result = isl_map_add_basic_map(result,
7362 fn(isl_basic_map_copy(map->p[i]),
7363 isl_basic_set_copy(set->p[j])));
7364 if (!result)
7365 break;
7368 isl_map_free(map);
7369 isl_set_free(set);
7370 return result;
7373 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7374 __isl_take isl_set *set)
7376 if (!map || !set)
7377 goto error;
7379 if (!isl_map_compatible_range(map, set))
7380 isl_die(set->ctx, isl_error_invalid,
7381 "incompatible spaces", goto error);
7383 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7384 error:
7385 isl_map_free(map);
7386 isl_set_free(set);
7387 return NULL;
7390 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7391 __isl_take isl_set *set)
7393 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7396 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7397 __isl_take isl_set *set)
7399 if (!map || !set)
7400 goto error;
7402 if (!isl_map_compatible_domain(map, set))
7403 isl_die(set->ctx, isl_error_invalid,
7404 "incompatible spaces", goto error);
7406 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7407 error:
7408 isl_map_free(map);
7409 isl_set_free(set);
7410 return NULL;
7413 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7414 __isl_take isl_set *set)
7416 return isl_map_align_params_map_map_and(map, set,
7417 &map_intersect_domain);
7420 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7421 __isl_take isl_map *map2)
7423 if (!map1 || !map2)
7424 goto error;
7425 map1 = isl_map_reverse(map1);
7426 map1 = isl_map_apply_range(map1, map2);
7427 return isl_map_reverse(map1);
7428 error:
7429 isl_map_free(map1);
7430 isl_map_free(map2);
7431 return NULL;
7434 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7435 __isl_take isl_map *map2)
7437 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7440 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7441 __isl_take isl_map *map2)
7443 isl_space *dim_result;
7444 struct isl_map *result;
7445 int i, j;
7447 if (!map1 || !map2)
7448 goto error;
7450 dim_result = isl_space_join(isl_space_copy(map1->dim),
7451 isl_space_copy(map2->dim));
7453 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7454 if (!result)
7455 goto error;
7456 for (i = 0; i < map1->n; ++i)
7457 for (j = 0; j < map2->n; ++j) {
7458 result = isl_map_add_basic_map(result,
7459 isl_basic_map_apply_range(
7460 isl_basic_map_copy(map1->p[i]),
7461 isl_basic_map_copy(map2->p[j])));
7462 if (!result)
7463 goto error;
7465 isl_map_free(map1);
7466 isl_map_free(map2);
7467 if (result && result->n <= 1)
7468 ISL_F_SET(result, ISL_MAP_DISJOINT);
7469 return result;
7470 error:
7471 isl_map_free(map1);
7472 isl_map_free(map2);
7473 return NULL;
7476 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7477 __isl_take isl_map *map2)
7479 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7483 * returns range - domain
7485 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7487 isl_space *dims, *target_dim;
7488 struct isl_basic_set *bset;
7489 unsigned dim;
7490 unsigned nparam;
7491 int i;
7493 if (!bmap)
7494 goto error;
7495 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7496 bmap->dim, isl_dim_out),
7497 goto error);
7498 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7499 dim = isl_basic_map_n_in(bmap);
7500 nparam = isl_basic_map_n_param(bmap);
7501 bset = isl_basic_set_from_basic_map(bmap);
7502 bset = isl_basic_set_cow(bset);
7503 dims = isl_basic_set_get_space(bset);
7504 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7505 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7506 bset = isl_basic_set_swap_vars(bset, 2*dim);
7507 for (i = 0; i < dim; ++i) {
7508 int j = isl_basic_map_alloc_equality(
7509 (struct isl_basic_map *)bset);
7510 if (j < 0) {
7511 bset = isl_basic_set_free(bset);
7512 break;
7514 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7515 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7516 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7517 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7519 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7520 bset = isl_basic_set_reset_space(bset, target_dim);
7521 return bset;
7522 error:
7523 isl_basic_map_free(bmap);
7524 return NULL;
7528 * returns range - domain
7530 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7532 int i;
7533 isl_space *dim;
7534 struct isl_set *result;
7536 if (!map)
7537 return NULL;
7539 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7540 map->dim, isl_dim_out),
7541 goto error);
7542 dim = isl_map_get_space(map);
7543 dim = isl_space_domain(dim);
7544 result = isl_set_alloc_space(dim, map->n, 0);
7545 if (!result)
7546 goto error;
7547 for (i = 0; i < map->n; ++i)
7548 result = isl_set_add_basic_set(result,
7549 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7550 isl_map_free(map);
7551 return result;
7552 error:
7553 isl_map_free(map);
7554 return NULL;
7558 * returns [domain -> range] -> range - domain
7560 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7561 __isl_take isl_basic_map *bmap)
7563 int i, k;
7564 isl_space *dim;
7565 isl_basic_map *domain;
7566 int nparam, n;
7567 unsigned total;
7569 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7570 bmap->dim, isl_dim_out))
7571 isl_die(bmap->ctx, isl_error_invalid,
7572 "domain and range don't match", goto error);
7574 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7575 n = isl_basic_map_dim(bmap, isl_dim_in);
7577 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7578 domain = isl_basic_map_universe(dim);
7580 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7581 bmap = isl_basic_map_apply_range(bmap, domain);
7582 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7584 total = isl_basic_map_total_dim(bmap);
7586 for (i = 0; i < n; ++i) {
7587 k = isl_basic_map_alloc_equality(bmap);
7588 if (k < 0)
7589 goto error;
7590 isl_seq_clr(bmap->eq[k], 1 + total);
7591 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7592 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7593 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7596 bmap = isl_basic_map_gauss(bmap, NULL);
7597 return isl_basic_map_finalize(bmap);
7598 error:
7599 isl_basic_map_free(bmap);
7600 return NULL;
7604 * returns [domain -> range] -> range - domain
7606 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7608 int i;
7609 isl_space *domain_dim;
7611 if (!map)
7612 return NULL;
7614 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7615 map->dim, isl_dim_out))
7616 isl_die(map->ctx, isl_error_invalid,
7617 "domain and range don't match", goto error);
7619 map = isl_map_cow(map);
7620 if (!map)
7621 return NULL;
7623 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7624 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7625 map->dim = isl_space_join(map->dim, domain_dim);
7626 if (!map->dim)
7627 goto error;
7628 for (i = 0; i < map->n; ++i) {
7629 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7630 if (!map->p[i])
7631 goto error;
7633 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7634 return map;
7635 error:
7636 isl_map_free(map);
7637 return NULL;
7640 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7642 struct isl_basic_map *bmap;
7643 unsigned nparam;
7644 unsigned dim;
7645 int i;
7647 if (!dims)
7648 return NULL;
7650 nparam = dims->nparam;
7651 dim = dims->n_out;
7652 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7653 if (!bmap)
7654 goto error;
7656 for (i = 0; i < dim; ++i) {
7657 int j = isl_basic_map_alloc_equality(bmap);
7658 if (j < 0)
7659 goto error;
7660 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7661 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7662 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7664 return isl_basic_map_finalize(bmap);
7665 error:
7666 isl_basic_map_free(bmap);
7667 return NULL;
7670 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7672 if (!dim)
7673 return NULL;
7674 if (dim->n_in != dim->n_out)
7675 isl_die(dim->ctx, isl_error_invalid,
7676 "number of input and output dimensions needs to be "
7677 "the same", goto error);
7678 return basic_map_identity(dim);
7679 error:
7680 isl_space_free(dim);
7681 return NULL;
7684 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7686 if (!model || !model->dim)
7687 return NULL;
7688 return isl_basic_map_identity(isl_space_copy(model->dim));
7691 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7693 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7696 struct isl_map *isl_map_identity_like(struct isl_map *model)
7698 if (!model || !model->dim)
7699 return NULL;
7700 return isl_map_identity(isl_space_copy(model->dim));
7703 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7705 if (!model || !model->dim)
7706 return NULL;
7707 return isl_map_identity(isl_space_copy(model->dim));
7710 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7712 isl_space *dim = isl_set_get_space(set);
7713 isl_map *id;
7714 id = isl_map_identity(isl_space_map_from_set(dim));
7715 return isl_map_intersect_range(id, set);
7718 /* Construct a basic set with all set dimensions having only non-negative
7719 * values.
7721 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7722 __isl_take isl_space *space)
7724 int i;
7725 unsigned nparam;
7726 unsigned dim;
7727 struct isl_basic_set *bset;
7729 if (!space)
7730 return NULL;
7731 nparam = space->nparam;
7732 dim = space->n_out;
7733 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7734 if (!bset)
7735 return NULL;
7736 for (i = 0; i < dim; ++i) {
7737 int k = isl_basic_set_alloc_inequality(bset);
7738 if (k < 0)
7739 goto error;
7740 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7741 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7743 return bset;
7744 error:
7745 isl_basic_set_free(bset);
7746 return NULL;
7749 /* Construct the half-space x_pos >= 0.
7751 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7752 int pos)
7754 int k;
7755 isl_basic_set *nonneg;
7757 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7758 k = isl_basic_set_alloc_inequality(nonneg);
7759 if (k < 0)
7760 goto error;
7761 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7762 isl_int_set_si(nonneg->ineq[k][pos], 1);
7764 return isl_basic_set_finalize(nonneg);
7765 error:
7766 isl_basic_set_free(nonneg);
7767 return NULL;
7770 /* Construct the half-space x_pos <= -1.
7772 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7774 int k;
7775 isl_basic_set *neg;
7777 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7778 k = isl_basic_set_alloc_inequality(neg);
7779 if (k < 0)
7780 goto error;
7781 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7782 isl_int_set_si(neg->ineq[k][0], -1);
7783 isl_int_set_si(neg->ineq[k][pos], -1);
7785 return isl_basic_set_finalize(neg);
7786 error:
7787 isl_basic_set_free(neg);
7788 return NULL;
7791 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7792 enum isl_dim_type type, unsigned first, unsigned n)
7794 int i;
7795 isl_basic_set *nonneg;
7796 isl_basic_set *neg;
7798 if (!set)
7799 return NULL;
7800 if (n == 0)
7801 return set;
7803 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7805 for (i = 0; i < n; ++i) {
7806 nonneg = nonneg_halfspace(isl_set_get_space(set),
7807 pos(set->dim, type) + first + i);
7808 neg = neg_halfspace(isl_set_get_space(set),
7809 pos(set->dim, type) + first + i);
7811 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7814 return set;
7815 error:
7816 isl_set_free(set);
7817 return NULL;
7820 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7821 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7822 void *user)
7824 isl_set *half;
7826 if (!set)
7827 return -1;
7828 if (isl_set_plain_is_empty(set)) {
7829 isl_set_free(set);
7830 return 0;
7832 if (first == len)
7833 return fn(set, signs, user);
7835 signs[first] = 1;
7836 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7837 1 + first));
7838 half = isl_set_intersect(half, isl_set_copy(set));
7839 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7840 goto error;
7842 signs[first] = -1;
7843 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7844 1 + first));
7845 half = isl_set_intersect(half, set);
7846 return foreach_orthant(half, signs, first + 1, len, fn, user);
7847 error:
7848 isl_set_free(set);
7849 return -1;
7852 /* Call "fn" on the intersections of "set" with each of the orthants
7853 * (except for obviously empty intersections). The orthant is identified
7854 * by the signs array, with each entry having value 1 or -1 according
7855 * to the sign of the corresponding variable.
7857 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7858 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7859 void *user)
7861 unsigned nparam;
7862 unsigned nvar;
7863 int *signs;
7864 int r;
7866 if (!set)
7867 return -1;
7868 if (isl_set_plain_is_empty(set))
7869 return 0;
7871 nparam = isl_set_dim(set, isl_dim_param);
7872 nvar = isl_set_dim(set, isl_dim_set);
7874 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7876 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7877 fn, user);
7879 free(signs);
7881 return r;
7884 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7886 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7889 int isl_basic_map_is_subset(
7890 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7892 int is_subset;
7893 struct isl_map *map1;
7894 struct isl_map *map2;
7896 if (!bmap1 || !bmap2)
7897 return -1;
7899 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7900 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7902 is_subset = isl_map_is_subset(map1, map2);
7904 isl_map_free(map1);
7905 isl_map_free(map2);
7907 return is_subset;
7910 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7911 __isl_keep isl_basic_set *bset2)
7913 return isl_basic_map_is_subset(bset1, bset2);
7916 int isl_basic_map_is_equal(
7917 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7919 int is_subset;
7921 if (!bmap1 || !bmap2)
7922 return -1;
7923 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7924 if (is_subset != 1)
7925 return is_subset;
7926 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7927 return is_subset;
7930 int isl_basic_set_is_equal(
7931 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7933 return isl_basic_map_is_equal(
7934 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7937 int isl_map_is_empty(struct isl_map *map)
7939 int i;
7940 int is_empty;
7942 if (!map)
7943 return -1;
7944 for (i = 0; i < map->n; ++i) {
7945 is_empty = isl_basic_map_is_empty(map->p[i]);
7946 if (is_empty < 0)
7947 return -1;
7948 if (!is_empty)
7949 return 0;
7951 return 1;
7954 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7956 return map ? map->n == 0 : -1;
7959 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7961 return isl_map_plain_is_empty(map);
7964 int isl_set_plain_is_empty(struct isl_set *set)
7966 return set ? set->n == 0 : -1;
7969 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7971 return isl_set_plain_is_empty(set);
7974 int isl_set_is_empty(struct isl_set *set)
7976 return isl_map_is_empty((struct isl_map *)set);
7979 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7981 if (!map1 || !map2)
7982 return -1;
7984 return isl_space_is_equal(map1->dim, map2->dim);
7987 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7989 if (!set1 || !set2)
7990 return -1;
7992 return isl_space_is_equal(set1->dim, set2->dim);
7995 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7997 int is_subset;
7999 if (!map1 || !map2)
8000 return -1;
8001 is_subset = isl_map_is_subset(map1, map2);
8002 if (is_subset != 1)
8003 return is_subset;
8004 is_subset = isl_map_is_subset(map2, map1);
8005 return is_subset;
8008 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8010 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8013 int isl_basic_map_is_strict_subset(
8014 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8016 int is_subset;
8018 if (!bmap1 || !bmap2)
8019 return -1;
8020 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8021 if (is_subset != 1)
8022 return is_subset;
8023 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8024 if (is_subset == -1)
8025 return is_subset;
8026 return !is_subset;
8029 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
8031 int is_subset;
8033 if (!map1 || !map2)
8034 return -1;
8035 is_subset = isl_map_is_subset(map1, map2);
8036 if (is_subset != 1)
8037 return is_subset;
8038 is_subset = isl_map_is_subset(map2, map1);
8039 if (is_subset == -1)
8040 return is_subset;
8041 return !is_subset;
8044 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8046 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
8049 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
8051 if (!bmap)
8052 return -1;
8053 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8056 int isl_basic_set_is_universe(struct isl_basic_set *bset)
8058 if (!bset)
8059 return -1;
8060 return bset->n_eq == 0 && bset->n_ineq == 0;
8063 int isl_map_plain_is_universe(__isl_keep isl_map *map)
8065 int i;
8067 if (!map)
8068 return -1;
8070 for (i = 0; i < map->n; ++i) {
8071 int r = isl_basic_map_is_universe(map->p[i]);
8072 if (r < 0 || r)
8073 return r;
8076 return 0;
8079 int isl_set_plain_is_universe(__isl_keep isl_set *set)
8081 return isl_map_plain_is_universe((isl_map *) set);
8084 int isl_set_fast_is_universe(__isl_keep isl_set *set)
8086 return isl_set_plain_is_universe(set);
8089 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
8091 struct isl_basic_set *bset = NULL;
8092 struct isl_vec *sample = NULL;
8093 int empty;
8094 unsigned total;
8096 if (!bmap)
8097 return -1;
8099 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8100 return 1;
8102 if (isl_basic_map_is_universe(bmap))
8103 return 0;
8105 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8106 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8107 copy = isl_basic_map_remove_redundancies(copy);
8108 empty = isl_basic_map_plain_is_empty(copy);
8109 isl_basic_map_free(copy);
8110 return empty;
8113 total = 1 + isl_basic_map_total_dim(bmap);
8114 if (bmap->sample && bmap->sample->size == total) {
8115 int contains = isl_basic_map_contains(bmap, bmap->sample);
8116 if (contains < 0)
8117 return -1;
8118 if (contains)
8119 return 0;
8121 isl_vec_free(bmap->sample);
8122 bmap->sample = NULL;
8123 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8124 if (!bset)
8125 return -1;
8126 sample = isl_basic_set_sample_vec(bset);
8127 if (!sample)
8128 return -1;
8129 empty = sample->size == 0;
8130 isl_vec_free(bmap->sample);
8131 bmap->sample = sample;
8132 if (empty)
8133 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8135 return empty;
8138 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8140 if (!bmap)
8141 return -1;
8142 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8145 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
8147 return isl_basic_map_plain_is_empty(bmap);
8150 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8152 if (!bset)
8153 return -1;
8154 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8157 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
8159 return isl_basic_set_plain_is_empty(bset);
8162 int isl_basic_set_is_empty(struct isl_basic_set *bset)
8164 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8167 struct isl_map *isl_basic_map_union(
8168 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8170 struct isl_map *map;
8171 if (!bmap1 || !bmap2)
8172 goto error;
8174 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8176 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8177 if (!map)
8178 goto error;
8179 map = isl_map_add_basic_map(map, bmap1);
8180 map = isl_map_add_basic_map(map, bmap2);
8181 return map;
8182 error:
8183 isl_basic_map_free(bmap1);
8184 isl_basic_map_free(bmap2);
8185 return NULL;
8188 struct isl_set *isl_basic_set_union(
8189 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8191 return (struct isl_set *)isl_basic_map_union(
8192 (struct isl_basic_map *)bset1,
8193 (struct isl_basic_map *)bset2);
8196 /* Order divs such that any div only depends on previous divs */
8197 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8199 int i;
8200 unsigned off;
8202 if (!bmap)
8203 return NULL;
8205 off = isl_space_dim(bmap->dim, isl_dim_all);
8207 for (i = 0; i < bmap->n_div; ++i) {
8208 int pos;
8209 if (isl_int_is_zero(bmap->div[i][0]))
8210 continue;
8211 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8212 bmap->n_div-i);
8213 if (pos == -1)
8214 continue;
8215 isl_basic_map_swap_div(bmap, i, i + pos);
8216 --i;
8218 return bmap;
8221 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8223 return (struct isl_basic_set *)
8224 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8227 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8229 int i;
8231 if (!map)
8232 return 0;
8234 for (i = 0; i < map->n; ++i) {
8235 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8236 if (!map->p[i])
8237 goto error;
8240 return map;
8241 error:
8242 isl_map_free(map);
8243 return NULL;
8246 /* Apply the expansion computed by isl_merge_divs.
8247 * The expansion itself is given by "exp" while the resulting
8248 * list of divs is given by "div".
8250 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8251 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8253 int i, j;
8254 int n_div;
8256 bset = isl_basic_set_cow(bset);
8257 if (!bset || !div)
8258 goto error;
8260 if (div->n_row < bset->n_div)
8261 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8262 "not an expansion", goto error);
8264 n_div = bset->n_div;
8265 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8266 div->n_row - n_div, 0,
8267 2 * (div->n_row - n_div));
8269 for (i = n_div; i < div->n_row; ++i)
8270 if (isl_basic_set_alloc_div(bset) < 0)
8271 goto error;
8273 j = n_div - 1;
8274 for (i = div->n_row - 1; i >= 0; --i) {
8275 if (j >= 0 && exp[j] == i) {
8276 if (i != j)
8277 isl_basic_map_swap_div(bset, i, j);
8278 j--;
8279 } else {
8280 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8281 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8282 goto error;
8286 isl_mat_free(div);
8287 return bset;
8288 error:
8289 isl_basic_set_free(bset);
8290 isl_mat_free(div);
8291 return NULL;
8294 /* Look for a div in dst that corresponds to the div "div" in src.
8295 * The divs before "div" in src and dst are assumed to be the same.
8297 * Returns -1 if no corresponding div was found and the position
8298 * of the corresponding div in dst otherwise.
8300 static int find_div(struct isl_basic_map *dst,
8301 struct isl_basic_map *src, unsigned div)
8303 int i;
8305 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8307 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8308 for (i = div; i < dst->n_div; ++i)
8309 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8310 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8311 dst->n_div - div) == -1)
8312 return i;
8313 return -1;
8316 /* Align the divs of "dst" to those of "src", adding divs from "src"
8317 * if needed. That is, make sure that the first src->n_div divs
8318 * of the result are equal to those of src.
8320 * The result is not finalized as by design it will have redundant
8321 * divs if any divs from "src" were copied.
8323 __isl_give isl_basic_map *isl_basic_map_align_divs(
8324 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8326 int i;
8327 int known, extended;
8328 unsigned total;
8330 if (!dst || !src)
8331 return isl_basic_map_free(dst);
8333 if (src->n_div == 0)
8334 return dst;
8336 known = isl_basic_map_divs_known(src);
8337 if (known < 0)
8338 return isl_basic_map_free(dst);
8339 if (!known)
8340 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8341 "some src divs are unknown",
8342 return isl_basic_map_free(dst));
8344 src = isl_basic_map_order_divs(src);
8346 extended = 0;
8347 total = isl_space_dim(src->dim, isl_dim_all);
8348 for (i = 0; i < src->n_div; ++i) {
8349 int j = find_div(dst, src, i);
8350 if (j < 0) {
8351 if (!extended) {
8352 int extra = src->n_div - i;
8353 dst = isl_basic_map_cow(dst);
8354 if (!dst)
8355 return NULL;
8356 dst = isl_basic_map_extend_space(dst,
8357 isl_space_copy(dst->dim),
8358 extra, 0, 2 * extra);
8359 extended = 1;
8361 j = isl_basic_map_alloc_div(dst);
8362 if (j < 0)
8363 return isl_basic_map_free(dst);
8364 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8365 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8366 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8367 return isl_basic_map_free(dst);
8369 if (j != i)
8370 isl_basic_map_swap_div(dst, i, j);
8372 return dst;
8375 struct isl_basic_set *isl_basic_set_align_divs(
8376 struct isl_basic_set *dst, struct isl_basic_set *src)
8378 return (struct isl_basic_set *)isl_basic_map_align_divs(
8379 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8382 struct isl_map *isl_map_align_divs(struct isl_map *map)
8384 int i;
8386 if (!map)
8387 return NULL;
8388 if (map->n == 0)
8389 return map;
8390 map = isl_map_compute_divs(map);
8391 map = isl_map_cow(map);
8392 if (!map)
8393 return NULL;
8395 for (i = 1; i < map->n; ++i)
8396 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8397 for (i = 1; i < map->n; ++i) {
8398 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8399 if (!map->p[i])
8400 return isl_map_free(map);
8403 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8404 return map;
8407 struct isl_set *isl_set_align_divs(struct isl_set *set)
8409 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8412 /* Align the divs of the basic maps in "map" to those
8413 * of the basic maps in "list", as well as to the other basic maps in "map".
8414 * The elements in "list" are assumed to have known divs.
8416 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8417 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8419 int i, n;
8421 map = isl_map_compute_divs(map);
8422 map = isl_map_cow(map);
8423 if (!map || !list)
8424 return isl_map_free(map);
8425 if (map->n == 0)
8426 return map;
8428 n = isl_basic_map_list_n_basic_map(list);
8429 for (i = 0; i < n; ++i) {
8430 isl_basic_map *bmap;
8432 bmap = isl_basic_map_list_get_basic_map(list, i);
8433 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8434 isl_basic_map_free(bmap);
8436 if (!map->p[0])
8437 return isl_map_free(map);
8439 return isl_map_align_divs(map);
8442 /* Align the divs of each element of "list" to those of "bmap".
8443 * Both "bmap" and the elements of "list" are assumed to have known divs.
8445 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8446 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8448 int i, n;
8450 if (!list || !bmap)
8451 return isl_basic_map_list_free(list);
8453 n = isl_basic_map_list_n_basic_map(list);
8454 for (i = 0; i < n; ++i) {
8455 isl_basic_map *bmap_i;
8457 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8458 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8459 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8462 return list;
8465 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8466 __isl_take isl_map *map)
8468 if (!set || !map)
8469 goto error;
8470 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8471 map = isl_map_intersect_domain(map, set);
8472 set = isl_map_range(map);
8473 return set;
8474 error:
8475 isl_set_free(set);
8476 isl_map_free(map);
8477 return NULL;
8480 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8481 __isl_take isl_map *map)
8483 return isl_map_align_params_map_map_and(set, map, &set_apply);
8486 /* There is no need to cow as removing empty parts doesn't change
8487 * the meaning of the set.
8489 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8491 int i;
8493 if (!map)
8494 return NULL;
8496 for (i = map->n - 1; i >= 0; --i)
8497 remove_if_empty(map, i);
8499 return map;
8502 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8504 return (struct isl_set *)
8505 isl_map_remove_empty_parts((struct isl_map *)set);
8508 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8510 struct isl_basic_map *bmap;
8511 if (!map || map->n == 0)
8512 return NULL;
8513 bmap = map->p[map->n-1];
8514 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8515 return isl_basic_map_copy(bmap);
8518 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8520 return (struct isl_basic_set *)
8521 isl_map_copy_basic_map((struct isl_map *)set);
8524 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8525 __isl_keep isl_basic_map *bmap)
8527 int i;
8529 if (!map || !bmap)
8530 goto error;
8531 for (i = map->n-1; i >= 0; --i) {
8532 if (map->p[i] != bmap)
8533 continue;
8534 map = isl_map_cow(map);
8535 if (!map)
8536 goto error;
8537 isl_basic_map_free(map->p[i]);
8538 if (i != map->n-1) {
8539 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8540 map->p[i] = map->p[map->n-1];
8542 map->n--;
8543 return map;
8545 return map;
8546 error:
8547 isl_map_free(map);
8548 return NULL;
8551 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8552 struct isl_basic_set *bset)
8554 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8555 (struct isl_basic_map *)bset);
8558 /* Given two basic sets bset1 and bset2, compute the maximal difference
8559 * between the values of dimension pos in bset1 and those in bset2
8560 * for any common value of the parameters and dimensions preceding pos.
8562 static enum isl_lp_result basic_set_maximal_difference_at(
8563 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8564 int pos, isl_int *opt)
8566 isl_space *dims;
8567 struct isl_basic_map *bmap1 = NULL;
8568 struct isl_basic_map *bmap2 = NULL;
8569 struct isl_ctx *ctx;
8570 struct isl_vec *obj;
8571 unsigned total;
8572 unsigned nparam;
8573 unsigned dim1, dim2;
8574 enum isl_lp_result res;
8576 if (!bset1 || !bset2)
8577 return isl_lp_error;
8579 nparam = isl_basic_set_n_param(bset1);
8580 dim1 = isl_basic_set_n_dim(bset1);
8581 dim2 = isl_basic_set_n_dim(bset2);
8582 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8583 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8584 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8585 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8586 if (!bmap1 || !bmap2)
8587 goto error;
8588 bmap1 = isl_basic_map_cow(bmap1);
8589 bmap1 = isl_basic_map_extend(bmap1, nparam,
8590 pos, (dim1 - pos) + (dim2 - pos),
8591 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8592 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8593 if (!bmap1)
8594 goto error2;
8595 total = isl_basic_map_total_dim(bmap1);
8596 ctx = bmap1->ctx;
8597 obj = isl_vec_alloc(ctx, 1 + total);
8598 if (!obj)
8599 goto error2;
8600 isl_seq_clr(obj->block.data, 1 + total);
8601 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8602 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8603 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8604 opt, NULL, NULL);
8605 isl_basic_map_free(bmap1);
8606 isl_vec_free(obj);
8607 return res;
8608 error:
8609 isl_basic_map_free(bmap2);
8610 error2:
8611 isl_basic_map_free(bmap1);
8612 return isl_lp_error;
8615 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8616 * for any common value of the parameters and dimensions preceding pos
8617 * in both basic sets, the values of dimension pos in bset1 are
8618 * smaller or larger than those in bset2.
8620 * Returns
8621 * 1 if bset1 follows bset2
8622 * -1 if bset1 precedes bset2
8623 * 0 if bset1 and bset2 are incomparable
8624 * -2 if some error occurred.
8626 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8627 struct isl_basic_set *bset2, int pos)
8629 isl_int opt;
8630 enum isl_lp_result res;
8631 int cmp;
8633 isl_int_init(opt);
8635 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8637 if (res == isl_lp_empty)
8638 cmp = 0;
8639 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8640 res == isl_lp_unbounded)
8641 cmp = 1;
8642 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8643 cmp = -1;
8644 else
8645 cmp = -2;
8647 isl_int_clear(opt);
8648 return cmp;
8651 /* Given two basic sets bset1 and bset2, check whether
8652 * for any common value of the parameters and dimensions preceding pos
8653 * there is a value of dimension pos in bset1 that is larger
8654 * than a value of the same dimension in bset2.
8656 * Return
8657 * 1 if there exists such a pair
8658 * 0 if there is no such pair, but there is a pair of equal values
8659 * -1 otherwise
8660 * -2 if some error occurred.
8662 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8663 __isl_keep isl_basic_set *bset2, int pos)
8665 isl_int opt;
8666 enum isl_lp_result res;
8667 int cmp;
8669 isl_int_init(opt);
8671 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8673 if (res == isl_lp_empty)
8674 cmp = -1;
8675 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8676 res == isl_lp_unbounded)
8677 cmp = 1;
8678 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8679 cmp = -1;
8680 else if (res == isl_lp_ok)
8681 cmp = 0;
8682 else
8683 cmp = -2;
8685 isl_int_clear(opt);
8686 return cmp;
8689 /* Given two sets set1 and set2, check whether
8690 * for any common value of the parameters and dimensions preceding pos
8691 * there is a value of dimension pos in set1 that is larger
8692 * than a value of the same dimension in set2.
8694 * Return
8695 * 1 if there exists such a pair
8696 * 0 if there is no such pair, but there is a pair of equal values
8697 * -1 otherwise
8698 * -2 if some error occurred.
8700 int isl_set_follows_at(__isl_keep isl_set *set1,
8701 __isl_keep isl_set *set2, int pos)
8703 int i, j;
8704 int follows = -1;
8706 if (!set1 || !set2)
8707 return -2;
8709 for (i = 0; i < set1->n; ++i)
8710 for (j = 0; j < set2->n; ++j) {
8711 int f;
8712 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8713 if (f == 1 || f == -2)
8714 return f;
8715 if (f > follows)
8716 follows = f;
8719 return follows;
8722 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8723 unsigned pos, isl_int *val)
8725 int i;
8726 int d;
8727 unsigned total;
8729 if (!bmap)
8730 return -1;
8731 total = isl_basic_map_total_dim(bmap);
8732 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8733 for (; d+1 > pos; --d)
8734 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8735 break;
8736 if (d != pos)
8737 continue;
8738 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8739 return 0;
8740 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8741 return 0;
8742 if (!isl_int_is_one(bmap->eq[i][1+d]))
8743 return 0;
8744 if (val)
8745 isl_int_neg(*val, bmap->eq[i][0]);
8746 return 1;
8748 return 0;
8751 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8752 unsigned pos, isl_int *val)
8754 int i;
8755 isl_int v;
8756 isl_int tmp;
8757 int fixed;
8759 if (!map)
8760 return -1;
8761 if (map->n == 0)
8762 return 0;
8763 if (map->n == 1)
8764 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8765 isl_int_init(v);
8766 isl_int_init(tmp);
8767 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8768 for (i = 1; fixed == 1 && i < map->n; ++i) {
8769 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8770 if (fixed == 1 && isl_int_ne(tmp, v))
8771 fixed = 0;
8773 if (val)
8774 isl_int_set(*val, v);
8775 isl_int_clear(tmp);
8776 isl_int_clear(v);
8777 return fixed;
8780 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8781 unsigned pos, isl_int *val)
8783 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8784 pos, val);
8787 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8788 isl_int *val)
8790 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8793 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8794 enum isl_dim_type type, unsigned pos, isl_int *val)
8796 if (pos >= isl_basic_map_dim(bmap, type))
8797 return -1;
8798 return isl_basic_map_plain_has_fixed_var(bmap,
8799 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8802 /* If "bmap" obviously lies on a hyperplane where the given dimension
8803 * has a fixed value, then return that value.
8804 * Otherwise return NaN.
8806 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8807 __isl_keep isl_basic_map *bmap,
8808 enum isl_dim_type type, unsigned pos)
8810 isl_ctx *ctx;
8811 isl_val *v;
8812 int fixed;
8814 if (!bmap)
8815 return NULL;
8816 ctx = isl_basic_map_get_ctx(bmap);
8817 v = isl_val_alloc(ctx);
8818 if (!v)
8819 return NULL;
8820 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8821 if (fixed < 0)
8822 return isl_val_free(v);
8823 if (fixed) {
8824 isl_int_set_si(v->d, 1);
8825 return v;
8827 isl_val_free(v);
8828 return isl_val_nan(ctx);
8831 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8832 enum isl_dim_type type, unsigned pos, isl_int *val)
8834 if (pos >= isl_map_dim(map, type))
8835 return -1;
8836 return isl_map_plain_has_fixed_var(map,
8837 map_offset(map, type) - 1 + pos, val);
8840 /* If "map" obviously lies on a hyperplane where the given dimension
8841 * has a fixed value, then return that value.
8842 * Otherwise return NaN.
8844 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8845 enum isl_dim_type type, unsigned pos)
8847 isl_ctx *ctx;
8848 isl_val *v;
8849 int fixed;
8851 if (!map)
8852 return NULL;
8853 ctx = isl_map_get_ctx(map);
8854 v = isl_val_alloc(ctx);
8855 if (!v)
8856 return NULL;
8857 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8858 if (fixed < 0)
8859 return isl_val_free(v);
8860 if (fixed) {
8861 isl_int_set_si(v->d, 1);
8862 return v;
8864 isl_val_free(v);
8865 return isl_val_nan(ctx);
8868 /* If "set" obviously lies on a hyperplane where the given dimension
8869 * has a fixed value, then return that value.
8870 * Otherwise return NaN.
8872 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8873 enum isl_dim_type type, unsigned pos)
8875 return isl_map_plain_get_val_if_fixed(set, type, pos);
8878 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8879 enum isl_dim_type type, unsigned pos, isl_int *val)
8881 return isl_map_plain_is_fixed(set, type, pos, val);
8884 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8885 enum isl_dim_type type, unsigned pos, isl_int *val)
8887 return isl_map_plain_is_fixed(map, type, pos, val);
8890 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8891 * then return this fixed value in *val.
8893 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8894 unsigned dim, isl_int *val)
8896 return isl_basic_set_plain_has_fixed_var(bset,
8897 isl_basic_set_n_param(bset) + dim, val);
8900 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8901 * then return this fixed value in *val.
8903 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8904 unsigned dim, isl_int *val)
8906 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8909 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8910 unsigned dim, isl_int *val)
8912 return isl_set_plain_dim_is_fixed(set, dim, val);
8915 /* Check if input variable in has fixed value and if so and if val is not NULL,
8916 * then return this fixed value in *val.
8918 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8919 unsigned in, isl_int *val)
8921 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8924 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8925 * and if val is not NULL, then return this lower bound in *val.
8927 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8928 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8930 int i, i_eq = -1, i_ineq = -1;
8931 isl_int *c;
8932 unsigned total;
8933 unsigned nparam;
8935 if (!bset)
8936 return -1;
8937 total = isl_basic_set_total_dim(bset);
8938 nparam = isl_basic_set_n_param(bset);
8939 for (i = 0; i < bset->n_eq; ++i) {
8940 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8941 continue;
8942 if (i_eq != -1)
8943 return 0;
8944 i_eq = i;
8946 for (i = 0; i < bset->n_ineq; ++i) {
8947 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8948 continue;
8949 if (i_eq != -1 || i_ineq != -1)
8950 return 0;
8951 i_ineq = i;
8953 if (i_eq == -1 && i_ineq == -1)
8954 return 0;
8955 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8956 /* The coefficient should always be one due to normalization. */
8957 if (!isl_int_is_one(c[1+nparam+dim]))
8958 return 0;
8959 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8960 return 0;
8961 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8962 total - nparam - dim - 1) != -1)
8963 return 0;
8964 if (val)
8965 isl_int_neg(*val, c[0]);
8966 return 1;
8969 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8970 unsigned dim, isl_int *val)
8972 int i;
8973 isl_int v;
8974 isl_int tmp;
8975 int fixed;
8977 if (!set)
8978 return -1;
8979 if (set->n == 0)
8980 return 0;
8981 if (set->n == 1)
8982 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8983 dim, val);
8984 isl_int_init(v);
8985 isl_int_init(tmp);
8986 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8987 dim, &v);
8988 for (i = 1; fixed == 1 && i < set->n; ++i) {
8989 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8990 dim, &tmp);
8991 if (fixed == 1 && isl_int_ne(tmp, v))
8992 fixed = 0;
8994 if (val)
8995 isl_int_set(*val, v);
8996 isl_int_clear(tmp);
8997 isl_int_clear(v);
8998 return fixed;
9001 /* uset_gist depends on constraints without existentially quantified
9002 * variables sorting first.
9004 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9006 isl_int **c1 = (isl_int **) p1;
9007 isl_int **c2 = (isl_int **) p2;
9008 int l1, l2;
9009 unsigned size = *(unsigned *) arg;
9011 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9012 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9014 if (l1 != l2)
9015 return l1 - l2;
9017 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9020 static struct isl_basic_map *isl_basic_map_sort_constraints(
9021 struct isl_basic_map *bmap)
9023 unsigned total;
9025 if (!bmap)
9026 return NULL;
9027 if (bmap->n_ineq == 0)
9028 return bmap;
9029 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9030 return bmap;
9031 total = isl_basic_map_total_dim(bmap);
9032 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9033 &sort_constraint_cmp, &total) < 0)
9034 return isl_basic_map_free(bmap);
9035 return bmap;
9038 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9039 __isl_take isl_basic_set *bset)
9041 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9042 (struct isl_basic_map *)bset);
9045 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9047 if (!bmap)
9048 return NULL;
9049 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9050 return bmap;
9051 bmap = isl_basic_map_remove_redundancies(bmap);
9052 bmap = isl_basic_map_sort_constraints(bmap);
9053 if (bmap)
9054 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9055 return bmap;
9058 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9060 return (struct isl_basic_set *)isl_basic_map_normalize(
9061 (struct isl_basic_map *)bset);
9064 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9065 const __isl_keep isl_basic_map *bmap2)
9067 int i, cmp;
9068 unsigned total;
9070 if (!bmap1 || !bmap2)
9071 return -1;
9073 if (bmap1 == bmap2)
9074 return 0;
9075 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9076 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9077 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9078 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9079 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9080 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9081 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9082 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9083 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9084 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9085 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9086 return 0;
9087 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9088 return 1;
9089 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9090 return -1;
9091 if (bmap1->n_eq != bmap2->n_eq)
9092 return bmap1->n_eq - bmap2->n_eq;
9093 if (bmap1->n_ineq != bmap2->n_ineq)
9094 return bmap1->n_ineq - bmap2->n_ineq;
9095 if (bmap1->n_div != bmap2->n_div)
9096 return bmap1->n_div - bmap2->n_div;
9097 total = isl_basic_map_total_dim(bmap1);
9098 for (i = 0; i < bmap1->n_eq; ++i) {
9099 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9100 if (cmp)
9101 return cmp;
9103 for (i = 0; i < bmap1->n_ineq; ++i) {
9104 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9105 if (cmp)
9106 return cmp;
9108 for (i = 0; i < bmap1->n_div; ++i) {
9109 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9110 if (cmp)
9111 return cmp;
9113 return 0;
9116 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9117 const __isl_keep isl_basic_set *bset2)
9119 return isl_basic_map_plain_cmp(bset1, bset2);
9122 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9124 int i, cmp;
9126 if (set1 == set2)
9127 return 0;
9128 if (set1->n != set2->n)
9129 return set1->n - set2->n;
9131 for (i = 0; i < set1->n; ++i) {
9132 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9133 if (cmp)
9134 return cmp;
9137 return 0;
9140 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9141 __isl_keep isl_basic_map *bmap2)
9143 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9146 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9147 __isl_keep isl_basic_set *bset2)
9149 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9150 (isl_basic_map *)bset2);
9153 static int qsort_bmap_cmp(const void *p1, const void *p2)
9155 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9156 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9158 return isl_basic_map_plain_cmp(bmap1, bmap2);
9161 /* Sort the basic maps of "map" and remove duplicate basic maps.
9163 * While removing basic maps, we make sure that the basic maps remain
9164 * sorted because isl_map_normalize expects the basic maps of the result
9165 * to be sorted.
9167 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9169 int i, j;
9171 map = isl_map_remove_empty_parts(map);
9172 if (!map)
9173 return NULL;
9174 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9175 for (i = map->n - 1; i >= 1; --i) {
9176 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9177 continue;
9178 isl_basic_map_free(map->p[i-1]);
9179 for (j = i; j < map->n; ++j)
9180 map->p[j - 1] = map->p[j];
9181 map->n--;
9184 return map;
9187 /* Remove obvious duplicates among the basic maps of "map".
9189 * Unlike isl_map_normalize, this function does not remove redundant
9190 * constraints and only removes duplicates that have exactly the same
9191 * constraints in the input. It does sort the constraints and
9192 * the basic maps to ease the detection of duplicates.
9194 * If "map" has already been normalized or if the basic maps are
9195 * disjoint, then there can be no duplicates.
9197 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9199 int i;
9200 isl_basic_map *bmap;
9202 if (!map)
9203 return NULL;
9204 if (map->n <= 1)
9205 return map;
9206 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9207 return map;
9208 for (i = 0; i < map->n; ++i) {
9209 bmap = isl_basic_map_copy(map->p[i]);
9210 bmap = isl_basic_map_sort_constraints(bmap);
9211 if (!bmap)
9212 return isl_map_free(map);
9213 isl_basic_map_free(map->p[i]);
9214 map->p[i] = bmap;
9217 map = sort_and_remove_duplicates(map);
9218 return map;
9221 /* We normalize in place, but if anything goes wrong we need
9222 * to return NULL, so we need to make sure we don't change the
9223 * meaning of any possible other copies of map.
9225 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9227 int i;
9228 struct isl_basic_map *bmap;
9230 if (!map)
9231 return NULL;
9232 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9233 return map;
9234 for (i = 0; i < map->n; ++i) {
9235 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9236 if (!bmap)
9237 goto error;
9238 isl_basic_map_free(map->p[i]);
9239 map->p[i] = bmap;
9242 map = sort_and_remove_duplicates(map);
9243 if (map)
9244 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9245 return map;
9246 error:
9247 isl_map_free(map);
9248 return NULL;
9251 struct isl_set *isl_set_normalize(struct isl_set *set)
9253 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9256 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9258 int i;
9259 int equal;
9261 if (!map1 || !map2)
9262 return -1;
9264 if (map1 == map2)
9265 return 1;
9266 if (!isl_space_is_equal(map1->dim, map2->dim))
9267 return 0;
9269 map1 = isl_map_copy(map1);
9270 map2 = isl_map_copy(map2);
9271 map1 = isl_map_normalize(map1);
9272 map2 = isl_map_normalize(map2);
9273 if (!map1 || !map2)
9274 goto error;
9275 equal = map1->n == map2->n;
9276 for (i = 0; equal && i < map1->n; ++i) {
9277 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9278 if (equal < 0)
9279 goto error;
9281 isl_map_free(map1);
9282 isl_map_free(map2);
9283 return equal;
9284 error:
9285 isl_map_free(map1);
9286 isl_map_free(map2);
9287 return -1;
9290 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9292 return isl_map_plain_is_equal(map1, map2);
9295 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9297 return isl_map_plain_is_equal((struct isl_map *)set1,
9298 (struct isl_map *)set2);
9301 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9303 return isl_set_plain_is_equal(set1, set2);
9306 /* Return an interval that ranges from min to max (inclusive)
9308 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9309 isl_int min, isl_int max)
9311 int k;
9312 struct isl_basic_set *bset = NULL;
9314 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9315 if (!bset)
9316 goto error;
9318 k = isl_basic_set_alloc_inequality(bset);
9319 if (k < 0)
9320 goto error;
9321 isl_int_set_si(bset->ineq[k][1], 1);
9322 isl_int_neg(bset->ineq[k][0], min);
9324 k = isl_basic_set_alloc_inequality(bset);
9325 if (k < 0)
9326 goto error;
9327 isl_int_set_si(bset->ineq[k][1], -1);
9328 isl_int_set(bset->ineq[k][0], max);
9330 return bset;
9331 error:
9332 isl_basic_set_free(bset);
9333 return NULL;
9336 /* Return the basic maps in "map" as a list.
9338 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9339 __isl_keep isl_map *map)
9341 int i;
9342 isl_ctx *ctx;
9343 isl_basic_map_list *list;
9345 if (!map)
9346 return NULL;
9347 ctx = isl_map_get_ctx(map);
9348 list = isl_basic_map_list_alloc(ctx, map->n);
9350 for (i = 0; i < map->n; ++i) {
9351 isl_basic_map *bmap;
9353 bmap = isl_basic_map_copy(map->p[i]);
9354 list = isl_basic_map_list_add(list, bmap);
9357 return list;
9360 /* Return the intersection of the elements in the non-empty list "list".
9361 * All elements are assumed to live in the same space.
9363 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9364 __isl_take isl_basic_map_list *list)
9366 int i, n;
9367 isl_basic_map *bmap;
9369 if (!list)
9370 return NULL;
9371 n = isl_basic_map_list_n_basic_map(list);
9372 if (n < 1)
9373 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9374 "expecting non-empty list", goto error);
9376 bmap = isl_basic_map_list_get_basic_map(list, 0);
9377 for (i = 1; i < n; ++i) {
9378 isl_basic_map *bmap_i;
9380 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9381 bmap = isl_basic_map_intersect(bmap, bmap_i);
9384 isl_basic_map_list_free(list);
9385 return bmap;
9386 error:
9387 isl_basic_map_list_free(list);
9388 return NULL;
9391 /* Return the intersection of the elements in the non-empty list "list".
9392 * All elements are assumed to live in the same space.
9394 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9395 __isl_take isl_basic_set_list *list)
9397 return isl_basic_map_list_intersect(list);
9400 /* Return the Cartesian product of the basic sets in list (in the given order).
9402 __isl_give isl_basic_set *isl_basic_set_list_product(
9403 __isl_take struct isl_basic_set_list *list)
9405 int i;
9406 unsigned dim;
9407 unsigned nparam;
9408 unsigned extra;
9409 unsigned n_eq;
9410 unsigned n_ineq;
9411 struct isl_basic_set *product = NULL;
9413 if (!list)
9414 goto error;
9415 isl_assert(list->ctx, list->n > 0, goto error);
9416 isl_assert(list->ctx, list->p[0], goto error);
9417 nparam = isl_basic_set_n_param(list->p[0]);
9418 dim = isl_basic_set_n_dim(list->p[0]);
9419 extra = list->p[0]->n_div;
9420 n_eq = list->p[0]->n_eq;
9421 n_ineq = list->p[0]->n_ineq;
9422 for (i = 1; i < list->n; ++i) {
9423 isl_assert(list->ctx, list->p[i], goto error);
9424 isl_assert(list->ctx,
9425 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9426 dim += isl_basic_set_n_dim(list->p[i]);
9427 extra += list->p[i]->n_div;
9428 n_eq += list->p[i]->n_eq;
9429 n_ineq += list->p[i]->n_ineq;
9431 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9432 n_eq, n_ineq);
9433 if (!product)
9434 goto error;
9435 dim = 0;
9436 for (i = 0; i < list->n; ++i) {
9437 isl_basic_set_add_constraints(product,
9438 isl_basic_set_copy(list->p[i]), dim);
9439 dim += isl_basic_set_n_dim(list->p[i]);
9441 isl_basic_set_list_free(list);
9442 return product;
9443 error:
9444 isl_basic_set_free(product);
9445 isl_basic_set_list_free(list);
9446 return NULL;
9449 struct isl_basic_map *isl_basic_map_product(
9450 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9452 isl_space *dim_result = NULL;
9453 struct isl_basic_map *bmap;
9454 unsigned in1, in2, out1, out2, nparam, total, pos;
9455 struct isl_dim_map *dim_map1, *dim_map2;
9457 if (!bmap1 || !bmap2)
9458 goto error;
9460 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9461 bmap2->dim, isl_dim_param), goto error);
9462 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9463 isl_space_copy(bmap2->dim));
9465 in1 = isl_basic_map_n_in(bmap1);
9466 in2 = isl_basic_map_n_in(bmap2);
9467 out1 = isl_basic_map_n_out(bmap1);
9468 out2 = isl_basic_map_n_out(bmap2);
9469 nparam = isl_basic_map_n_param(bmap1);
9471 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9472 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9473 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9474 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9475 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9476 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9477 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9478 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9479 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9480 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9481 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9483 bmap = isl_basic_map_alloc_space(dim_result,
9484 bmap1->n_div + bmap2->n_div,
9485 bmap1->n_eq + bmap2->n_eq,
9486 bmap1->n_ineq + bmap2->n_ineq);
9487 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9488 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9489 bmap = isl_basic_map_simplify(bmap);
9490 return isl_basic_map_finalize(bmap);
9491 error:
9492 isl_basic_map_free(bmap1);
9493 isl_basic_map_free(bmap2);
9494 return NULL;
9497 __isl_give isl_basic_map *isl_basic_map_flat_product(
9498 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9500 isl_basic_map *prod;
9502 prod = isl_basic_map_product(bmap1, bmap2);
9503 prod = isl_basic_map_flatten(prod);
9504 return prod;
9507 __isl_give isl_basic_set *isl_basic_set_flat_product(
9508 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9510 return isl_basic_map_flat_range_product(bset1, bset2);
9513 __isl_give isl_basic_map *isl_basic_map_domain_product(
9514 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9516 isl_space *space_result = NULL;
9517 isl_basic_map *bmap;
9518 unsigned in1, in2, out, nparam, total, pos;
9519 struct isl_dim_map *dim_map1, *dim_map2;
9521 if (!bmap1 || !bmap2)
9522 goto error;
9524 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9525 isl_space_copy(bmap2->dim));
9527 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9528 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9529 out = isl_basic_map_dim(bmap1, isl_dim_out);
9530 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9532 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9533 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9534 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9535 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9536 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9537 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9538 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9539 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9540 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9541 isl_dim_map_div(dim_map1, bmap1, pos += out);
9542 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9544 bmap = isl_basic_map_alloc_space(space_result,
9545 bmap1->n_div + bmap2->n_div,
9546 bmap1->n_eq + bmap2->n_eq,
9547 bmap1->n_ineq + bmap2->n_ineq);
9548 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9549 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9550 bmap = isl_basic_map_simplify(bmap);
9551 return isl_basic_map_finalize(bmap);
9552 error:
9553 isl_basic_map_free(bmap1);
9554 isl_basic_map_free(bmap2);
9555 return NULL;
9558 __isl_give isl_basic_map *isl_basic_map_range_product(
9559 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9561 isl_space *dim_result = NULL;
9562 isl_basic_map *bmap;
9563 unsigned in, out1, out2, nparam, total, pos;
9564 struct isl_dim_map *dim_map1, *dim_map2;
9566 if (!bmap1 || !bmap2)
9567 goto error;
9569 if (!isl_space_match(bmap1->dim, isl_dim_param,
9570 bmap2->dim, isl_dim_param))
9571 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9572 "parameters don't match", goto error);
9574 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9575 isl_space_copy(bmap2->dim));
9577 in = isl_basic_map_dim(bmap1, isl_dim_in);
9578 out1 = isl_basic_map_n_out(bmap1);
9579 out2 = isl_basic_map_n_out(bmap2);
9580 nparam = isl_basic_map_n_param(bmap1);
9582 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9583 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9584 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9585 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9586 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9587 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9588 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9589 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9590 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9591 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9592 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9594 bmap = isl_basic_map_alloc_space(dim_result,
9595 bmap1->n_div + bmap2->n_div,
9596 bmap1->n_eq + bmap2->n_eq,
9597 bmap1->n_ineq + bmap2->n_ineq);
9598 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9599 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9600 bmap = isl_basic_map_simplify(bmap);
9601 return isl_basic_map_finalize(bmap);
9602 error:
9603 isl_basic_map_free(bmap1);
9604 isl_basic_map_free(bmap2);
9605 return NULL;
9608 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9609 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9611 isl_basic_map *prod;
9613 prod = isl_basic_map_range_product(bmap1, bmap2);
9614 prod = isl_basic_map_flatten_range(prod);
9615 return prod;
9618 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9619 * and collect the results.
9620 * The result live in the space obtained by calling "space_product"
9621 * on the spaces of "map1" and "map2".
9622 * If "remove_duplicates" is set then the result may contain duplicates
9623 * (even if the inputs do not) and so we try and remove the obvious
9624 * duplicates.
9626 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9627 __isl_take isl_map *map2,
9628 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9629 __isl_take isl_space *right),
9630 __isl_give isl_basic_map *(*basic_map_product)(
9631 __isl_take isl_basic_map *left,
9632 __isl_take isl_basic_map *right),
9633 int remove_duplicates)
9635 unsigned flags = 0;
9636 struct isl_map *result;
9637 int i, j;
9639 if (!map1 || !map2)
9640 goto error;
9642 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9643 map2->dim, isl_dim_param), goto error);
9645 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9646 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9647 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9649 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9650 isl_space_copy(map2->dim)),
9651 map1->n * map2->n, flags);
9652 if (!result)
9653 goto error;
9654 for (i = 0; i < map1->n; ++i)
9655 for (j = 0; j < map2->n; ++j) {
9656 struct isl_basic_map *part;
9657 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9658 isl_basic_map_copy(map2->p[j]));
9659 if (isl_basic_map_is_empty(part))
9660 isl_basic_map_free(part);
9661 else
9662 result = isl_map_add_basic_map(result, part);
9663 if (!result)
9664 goto error;
9666 if (remove_duplicates)
9667 result = isl_map_remove_obvious_duplicates(result);
9668 isl_map_free(map1);
9669 isl_map_free(map2);
9670 return result;
9671 error:
9672 isl_map_free(map1);
9673 isl_map_free(map2);
9674 return NULL;
9677 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9679 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9680 __isl_take isl_map *map2)
9682 return map_product(map1, map2, &isl_space_product,
9683 &isl_basic_map_product, 0);
9686 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9687 __isl_take isl_map *map2)
9689 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9692 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9694 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9695 __isl_take isl_map *map2)
9697 isl_map *prod;
9699 prod = isl_map_product(map1, map2);
9700 prod = isl_map_flatten(prod);
9701 return prod;
9704 /* Given two set A and B, construct its Cartesian product A x B.
9706 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9708 return isl_map_range_product(set1, set2);
9711 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9712 __isl_take isl_set *set2)
9714 return isl_map_flat_range_product(set1, set2);
9717 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9719 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9720 __isl_take isl_map *map2)
9722 return map_product(map1, map2, &isl_space_domain_product,
9723 &isl_basic_map_domain_product, 1);
9726 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9728 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9729 __isl_take isl_map *map2)
9731 return map_product(map1, map2, &isl_space_range_product,
9732 &isl_basic_map_range_product, 1);
9735 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9736 __isl_take isl_map *map2)
9738 return isl_map_align_params_map_map_and(map1, map2,
9739 &map_domain_product_aligned);
9742 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9743 __isl_take isl_map *map2)
9745 return isl_map_align_params_map_map_and(map1, map2,
9746 &map_range_product_aligned);
9749 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9751 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9753 isl_space *space;
9754 int total1, keep1, total2, keep2;
9756 if (!map)
9757 return NULL;
9758 if (!isl_space_domain_is_wrapping(map->dim) ||
9759 !isl_space_range_is_wrapping(map->dim))
9760 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9761 "not a product", return isl_map_free(map));
9763 space = isl_map_get_space(map);
9764 total1 = isl_space_dim(space, isl_dim_in);
9765 total2 = isl_space_dim(space, isl_dim_out);
9766 space = isl_space_factor_domain(space);
9767 keep1 = isl_space_dim(space, isl_dim_in);
9768 keep2 = isl_space_dim(space, isl_dim_out);
9769 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9770 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9771 map = isl_map_reset_space(map, space);
9773 return map;
9776 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9778 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9780 isl_space *space;
9781 int total1, keep1, total2, keep2;
9783 if (!map)
9784 return NULL;
9785 if (!isl_space_domain_is_wrapping(map->dim) ||
9786 !isl_space_range_is_wrapping(map->dim))
9787 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9788 "not a product", return isl_map_free(map));
9790 space = isl_map_get_space(map);
9791 total1 = isl_space_dim(space, isl_dim_in);
9792 total2 = isl_space_dim(space, isl_dim_out);
9793 space = isl_space_factor_range(space);
9794 keep1 = isl_space_dim(space, isl_dim_in);
9795 keep2 = isl_space_dim(space, isl_dim_out);
9796 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9797 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9798 map = isl_map_reset_space(map, space);
9800 return map;
9803 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9805 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9807 isl_space *space;
9808 int total, keep;
9810 if (!map)
9811 return NULL;
9812 if (!isl_space_domain_is_wrapping(map->dim))
9813 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9814 "domain is not a product", return isl_map_free(map));
9816 space = isl_map_get_space(map);
9817 total = isl_space_dim(space, isl_dim_in);
9818 space = isl_space_domain_factor_domain(space);
9819 keep = isl_space_dim(space, isl_dim_in);
9820 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9821 map = isl_map_reset_space(map, space);
9823 return map;
9826 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9828 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9830 isl_space *space;
9831 int total, keep;
9833 if (!map)
9834 return NULL;
9835 if (!isl_space_domain_is_wrapping(map->dim))
9836 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9837 "domain is not a product", return isl_map_free(map));
9839 space = isl_map_get_space(map);
9840 total = isl_space_dim(space, isl_dim_in);
9841 space = isl_space_domain_factor_range(space);
9842 keep = isl_space_dim(space, isl_dim_in);
9843 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9844 map = isl_map_reset_space(map, space);
9846 return map;
9849 /* Given a map A -> [B -> C], extract the map A -> B.
9851 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9853 isl_space *space;
9854 int total, keep;
9856 if (!map)
9857 return NULL;
9858 if (!isl_space_range_is_wrapping(map->dim))
9859 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9860 "range is not a product", return isl_map_free(map));
9862 space = isl_map_get_space(map);
9863 total = isl_space_dim(space, isl_dim_out);
9864 space = isl_space_range_factor_domain(space);
9865 keep = isl_space_dim(space, isl_dim_out);
9866 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9867 map = isl_map_reset_space(map, space);
9869 return map;
9872 /* Given a map A -> [B -> C], extract the map A -> C.
9874 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9876 isl_space *space;
9877 int total, keep;
9879 if (!map)
9880 return NULL;
9881 if (!isl_space_range_is_wrapping(map->dim))
9882 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9883 "range is not a product", return isl_map_free(map));
9885 space = isl_map_get_space(map);
9886 total = isl_space_dim(space, isl_dim_out);
9887 space = isl_space_range_factor_range(space);
9888 keep = isl_space_dim(space, isl_dim_out);
9889 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9890 map = isl_map_reset_space(map, space);
9892 return map;
9895 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9897 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9898 __isl_take isl_map *map2)
9900 isl_map *prod;
9902 prod = isl_map_domain_product(map1, map2);
9903 prod = isl_map_flatten_domain(prod);
9904 return prod;
9907 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9909 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9910 __isl_take isl_map *map2)
9912 isl_map *prod;
9914 prod = isl_map_range_product(map1, map2);
9915 prod = isl_map_flatten_range(prod);
9916 return prod;
9919 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9921 int i;
9922 uint32_t hash = isl_hash_init();
9923 unsigned total;
9925 if (!bmap)
9926 return 0;
9927 bmap = isl_basic_map_copy(bmap);
9928 bmap = isl_basic_map_normalize(bmap);
9929 if (!bmap)
9930 return 0;
9931 total = isl_basic_map_total_dim(bmap);
9932 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9933 for (i = 0; i < bmap->n_eq; ++i) {
9934 uint32_t c_hash;
9935 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9936 isl_hash_hash(hash, c_hash);
9938 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9939 for (i = 0; i < bmap->n_ineq; ++i) {
9940 uint32_t c_hash;
9941 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9942 isl_hash_hash(hash, c_hash);
9944 isl_hash_byte(hash, bmap->n_div & 0xFF);
9945 for (i = 0; i < bmap->n_div; ++i) {
9946 uint32_t c_hash;
9947 if (isl_int_is_zero(bmap->div[i][0]))
9948 continue;
9949 isl_hash_byte(hash, i & 0xFF);
9950 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9951 isl_hash_hash(hash, c_hash);
9953 isl_basic_map_free(bmap);
9954 return hash;
9957 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9959 return isl_basic_map_get_hash((isl_basic_map *)bset);
9962 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9964 int i;
9965 uint32_t hash;
9967 if (!map)
9968 return 0;
9969 map = isl_map_copy(map);
9970 map = isl_map_normalize(map);
9971 if (!map)
9972 return 0;
9974 hash = isl_hash_init();
9975 for (i = 0; i < map->n; ++i) {
9976 uint32_t bmap_hash;
9977 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9978 isl_hash_hash(hash, bmap_hash);
9981 isl_map_free(map);
9983 return hash;
9986 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9988 return isl_map_get_hash((isl_map *)set);
9991 /* Check if the value for dimension dim is completely determined
9992 * by the values of the other parameters and variables.
9993 * That is, check if dimension dim is involved in an equality.
9995 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9997 int i;
9998 unsigned nparam;
10000 if (!bset)
10001 return -1;
10002 nparam = isl_basic_set_n_param(bset);
10003 for (i = 0; i < bset->n_eq; ++i)
10004 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10005 return 1;
10006 return 0;
10009 /* Check if the value for dimension dim is completely determined
10010 * by the values of the other parameters and variables.
10011 * That is, check if dimension dim is involved in an equality
10012 * for each of the subsets.
10014 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10016 int i;
10018 if (!set)
10019 return -1;
10020 for (i = 0; i < set->n; ++i) {
10021 int unique;
10022 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10023 if (unique != 1)
10024 return unique;
10026 return 1;
10029 /* Return the number of basic maps in the (current) representation of "map".
10031 int isl_map_n_basic_map(__isl_keep isl_map *map)
10033 return map ? map->n : 0;
10036 int isl_set_n_basic_set(__isl_keep isl_set *set)
10038 return set ? set->n : 0;
10041 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
10042 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10044 int i;
10046 if (!map)
10047 return -1;
10049 for (i = 0; i < map->n; ++i)
10050 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10051 return -1;
10053 return 0;
10056 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
10057 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10059 int i;
10061 if (!set)
10062 return -1;
10064 for (i = 0; i < set->n; ++i)
10065 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10066 return -1;
10068 return 0;
10071 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10073 isl_space *dim;
10075 if (!bset)
10076 return NULL;
10078 bset = isl_basic_set_cow(bset);
10079 if (!bset)
10080 return NULL;
10082 dim = isl_basic_set_get_space(bset);
10083 dim = isl_space_lift(dim, bset->n_div);
10084 if (!dim)
10085 goto error;
10086 isl_space_free(bset->dim);
10087 bset->dim = dim;
10088 bset->extra -= bset->n_div;
10089 bset->n_div = 0;
10091 bset = isl_basic_set_finalize(bset);
10093 return bset;
10094 error:
10095 isl_basic_set_free(bset);
10096 return NULL;
10099 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10101 int i;
10102 isl_space *dim;
10103 unsigned n_div;
10105 set = isl_set_align_divs(set);
10107 if (!set)
10108 return NULL;
10110 set = isl_set_cow(set);
10111 if (!set)
10112 return NULL;
10114 n_div = set->p[0]->n_div;
10115 dim = isl_set_get_space(set);
10116 dim = isl_space_lift(dim, n_div);
10117 if (!dim)
10118 goto error;
10119 isl_space_free(set->dim);
10120 set->dim = dim;
10122 for (i = 0; i < set->n; ++i) {
10123 set->p[i] = isl_basic_set_lift(set->p[i]);
10124 if (!set->p[i])
10125 goto error;
10128 return set;
10129 error:
10130 isl_set_free(set);
10131 return NULL;
10134 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10136 isl_space *dim;
10137 struct isl_basic_map *bmap;
10138 unsigned n_set;
10139 unsigned n_div;
10140 unsigned n_param;
10141 unsigned total;
10142 int i, k, l;
10144 set = isl_set_align_divs(set);
10146 if (!set)
10147 return NULL;
10149 dim = isl_set_get_space(set);
10150 if (set->n == 0 || set->p[0]->n_div == 0) {
10151 isl_set_free(set);
10152 return isl_map_identity(isl_space_map_from_set(dim));
10155 n_div = set->p[0]->n_div;
10156 dim = isl_space_map_from_set(dim);
10157 n_param = isl_space_dim(dim, isl_dim_param);
10158 n_set = isl_space_dim(dim, isl_dim_in);
10159 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10160 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10161 for (i = 0; i < n_set; ++i)
10162 bmap = var_equal(bmap, i);
10164 total = n_param + n_set + n_set + n_div;
10165 for (i = 0; i < n_div; ++i) {
10166 k = isl_basic_map_alloc_inequality(bmap);
10167 if (k < 0)
10168 goto error;
10169 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10170 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10171 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10172 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10173 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10174 set->p[0]->div[i][0]);
10176 l = isl_basic_map_alloc_inequality(bmap);
10177 if (l < 0)
10178 goto error;
10179 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10180 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10181 set->p[0]->div[i][0]);
10182 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10185 isl_set_free(set);
10186 bmap = isl_basic_map_simplify(bmap);
10187 bmap = isl_basic_map_finalize(bmap);
10188 return isl_map_from_basic_map(bmap);
10189 error:
10190 isl_set_free(set);
10191 isl_basic_map_free(bmap);
10192 return NULL;
10195 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10197 unsigned dim;
10198 int size = 0;
10200 if (!bset)
10201 return -1;
10203 dim = isl_basic_set_total_dim(bset);
10204 size += bset->n_eq * (1 + dim);
10205 size += bset->n_ineq * (1 + dim);
10206 size += bset->n_div * (2 + dim);
10208 return size;
10211 int isl_set_size(__isl_keep isl_set *set)
10213 int i;
10214 int size = 0;
10216 if (!set)
10217 return -1;
10219 for (i = 0; i < set->n; ++i)
10220 size += isl_basic_set_size(set->p[i]);
10222 return size;
10225 /* Check if there is any lower bound (if lower == 0) and/or upper
10226 * bound (if upper == 0) on the specified dim.
10228 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10229 enum isl_dim_type type, unsigned pos, int lower, int upper)
10231 int i;
10233 if (!bmap)
10234 return -1;
10236 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
10238 pos += isl_basic_map_offset(bmap, type);
10240 for (i = 0; i < bmap->n_div; ++i) {
10241 if (isl_int_is_zero(bmap->div[i][0]))
10242 continue;
10243 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10244 return 1;
10247 for (i = 0; i < bmap->n_eq; ++i)
10248 if (!isl_int_is_zero(bmap->eq[i][pos]))
10249 return 1;
10251 for (i = 0; i < bmap->n_ineq; ++i) {
10252 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10253 if (sgn > 0)
10254 lower = 1;
10255 if (sgn < 0)
10256 upper = 1;
10259 return lower && upper;
10262 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10263 enum isl_dim_type type, unsigned pos)
10265 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10268 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10269 enum isl_dim_type type, unsigned pos)
10271 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10274 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10275 enum isl_dim_type type, unsigned pos)
10277 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10280 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10281 enum isl_dim_type type, unsigned pos)
10283 int i;
10285 if (!map)
10286 return -1;
10288 for (i = 0; i < map->n; ++i) {
10289 int bounded;
10290 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10291 if (bounded < 0 || !bounded)
10292 return bounded;
10295 return 1;
10298 /* Return 1 if the specified dim is involved in both an upper bound
10299 * and a lower bound.
10301 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10302 enum isl_dim_type type, unsigned pos)
10304 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10307 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10309 static int has_any_bound(__isl_keep isl_map *map,
10310 enum isl_dim_type type, unsigned pos,
10311 int (*fn)(__isl_keep isl_basic_map *bmap,
10312 enum isl_dim_type type, unsigned pos))
10314 int i;
10316 if (!map)
10317 return -1;
10319 for (i = 0; i < map->n; ++i) {
10320 int bounded;
10321 bounded = fn(map->p[i], type, pos);
10322 if (bounded < 0 || bounded)
10323 return bounded;
10326 return 0;
10329 /* Return 1 if the specified dim is involved in any lower bound.
10331 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10332 enum isl_dim_type type, unsigned pos)
10334 return has_any_bound(set, type, pos,
10335 &isl_basic_map_dim_has_lower_bound);
10338 /* Return 1 if the specified dim is involved in any upper bound.
10340 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10341 enum isl_dim_type type, unsigned pos)
10343 return has_any_bound(set, type, pos,
10344 &isl_basic_map_dim_has_upper_bound);
10347 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10349 static int has_bound(__isl_keep isl_map *map,
10350 enum isl_dim_type type, unsigned pos,
10351 int (*fn)(__isl_keep isl_basic_map *bmap,
10352 enum isl_dim_type type, unsigned pos))
10354 int i;
10356 if (!map)
10357 return -1;
10359 for (i = 0; i < map->n; ++i) {
10360 int bounded;
10361 bounded = fn(map->p[i], type, pos);
10362 if (bounded < 0 || !bounded)
10363 return bounded;
10366 return 1;
10369 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10371 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10372 enum isl_dim_type type, unsigned pos)
10374 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10377 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10379 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10380 enum isl_dim_type type, unsigned pos)
10382 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10385 /* For each of the "n" variables starting at "first", determine
10386 * the sign of the variable and put the results in the first "n"
10387 * elements of the array "signs".
10388 * Sign
10389 * 1 means that the variable is non-negative
10390 * -1 means that the variable is non-positive
10391 * 0 means the variable attains both positive and negative values.
10393 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10394 unsigned first, unsigned n, int *signs)
10396 isl_vec *bound = NULL;
10397 struct isl_tab *tab = NULL;
10398 struct isl_tab_undo *snap;
10399 int i;
10401 if (!bset || !signs)
10402 return -1;
10404 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10405 tab = isl_tab_from_basic_set(bset, 0);
10406 if (!bound || !tab)
10407 goto error;
10409 isl_seq_clr(bound->el, bound->size);
10410 isl_int_set_si(bound->el[0], -1);
10412 snap = isl_tab_snap(tab);
10413 for (i = 0; i < n; ++i) {
10414 int empty;
10416 isl_int_set_si(bound->el[1 + first + i], -1);
10417 if (isl_tab_add_ineq(tab, bound->el) < 0)
10418 goto error;
10419 empty = tab->empty;
10420 isl_int_set_si(bound->el[1 + first + i], 0);
10421 if (isl_tab_rollback(tab, snap) < 0)
10422 goto error;
10424 if (empty) {
10425 signs[i] = 1;
10426 continue;
10429 isl_int_set_si(bound->el[1 + first + i], 1);
10430 if (isl_tab_add_ineq(tab, bound->el) < 0)
10431 goto error;
10432 empty = tab->empty;
10433 isl_int_set_si(bound->el[1 + first + i], 0);
10434 if (isl_tab_rollback(tab, snap) < 0)
10435 goto error;
10437 signs[i] = empty ? -1 : 0;
10440 isl_tab_free(tab);
10441 isl_vec_free(bound);
10442 return 0;
10443 error:
10444 isl_tab_free(tab);
10445 isl_vec_free(bound);
10446 return -1;
10449 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10450 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10452 if (!bset || !signs)
10453 return -1;
10454 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10455 return -1);
10457 first += pos(bset->dim, type) - 1;
10458 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10461 /* Is it possible for the integer division "div" to depend (possibly
10462 * indirectly) on any output dimensions?
10464 * If the div is undefined, then we conservatively assume that it
10465 * may depend on them.
10466 * Otherwise, we check if it actually depends on them or on any integer
10467 * divisions that may depend on them.
10469 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10471 int i;
10472 unsigned n_out, o_out;
10473 unsigned n_div, o_div;
10475 if (isl_int_is_zero(bmap->div[div][0]))
10476 return 1;
10478 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10479 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10481 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10482 return 1;
10484 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10485 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10487 for (i = 0; i < n_div; ++i) {
10488 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10489 continue;
10490 if (div_may_involve_output(bmap, i))
10491 return 1;
10494 return 0;
10497 /* Return the index of the equality of "bmap" that defines
10498 * the output dimension "pos" in terms of earlier dimensions.
10499 * The equality may also involve integer divisions, as long
10500 * as those integer divisions are defined in terms of
10501 * parameters or input dimensions.
10502 * Return bmap->n_eq if there is no such equality.
10503 * Return -1 on error.
10505 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10506 int pos)
10508 int j, k;
10509 unsigned n_out, o_out;
10510 unsigned n_div, o_div;
10512 if (!bmap)
10513 return -1;
10515 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10516 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10517 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10518 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10520 for (j = 0; j < bmap->n_eq; ++j) {
10521 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10522 continue;
10523 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10524 n_out - (pos + 1)) != -1)
10525 continue;
10526 for (k = 0; k < n_div; ++k) {
10527 if (isl_int_is_zero(bmap->eq[j][o_div + k]))
10528 continue;
10529 if (div_may_involve_output(bmap, k))
10530 break;
10532 if (k >= n_div)
10533 return j;
10536 return bmap->n_eq;
10539 /* Check if the given basic map is obviously single-valued.
10540 * In particular, for each output dimension, check that there is
10541 * an equality that defines the output dimension in terms of
10542 * earlier dimensions.
10544 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10546 int i;
10547 unsigned n_out;
10549 if (!bmap)
10550 return -1;
10552 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10554 for (i = 0; i < n_out; ++i) {
10555 int eq;
10557 eq = isl_basic_map_output_defining_equality(bmap, i);
10558 if (eq < 0)
10559 return -1;
10560 if (eq >= bmap->n_eq)
10561 return 0;
10564 return 1;
10567 /* Check if the given basic map is single-valued.
10568 * We simply compute
10570 * M \circ M^-1
10572 * and check if the result is a subset of the identity mapping.
10574 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10576 isl_space *space;
10577 isl_basic_map *test;
10578 isl_basic_map *id;
10579 int sv;
10581 sv = isl_basic_map_plain_is_single_valued(bmap);
10582 if (sv < 0 || sv)
10583 return sv;
10585 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10586 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10588 space = isl_basic_map_get_space(bmap);
10589 space = isl_space_map_from_set(isl_space_range(space));
10590 id = isl_basic_map_identity(space);
10592 sv = isl_basic_map_is_subset(test, id);
10594 isl_basic_map_free(test);
10595 isl_basic_map_free(id);
10597 return sv;
10600 /* Check if the given map is obviously single-valued.
10602 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10604 if (!map)
10605 return -1;
10606 if (map->n == 0)
10607 return 1;
10608 if (map->n >= 2)
10609 return 0;
10611 return isl_basic_map_plain_is_single_valued(map->p[0]);
10614 /* Check if the given map is single-valued.
10615 * We simply compute
10617 * M \circ M^-1
10619 * and check if the result is a subset of the identity mapping.
10621 int isl_map_is_single_valued(__isl_keep isl_map *map)
10623 isl_space *dim;
10624 isl_map *test;
10625 isl_map *id;
10626 int sv;
10628 sv = isl_map_plain_is_single_valued(map);
10629 if (sv < 0 || sv)
10630 return sv;
10632 test = isl_map_reverse(isl_map_copy(map));
10633 test = isl_map_apply_range(test, isl_map_copy(map));
10635 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10636 id = isl_map_identity(dim);
10638 sv = isl_map_is_subset(test, id);
10640 isl_map_free(test);
10641 isl_map_free(id);
10643 return sv;
10646 int isl_map_is_injective(__isl_keep isl_map *map)
10648 int in;
10650 map = isl_map_copy(map);
10651 map = isl_map_reverse(map);
10652 in = isl_map_is_single_valued(map);
10653 isl_map_free(map);
10655 return in;
10658 /* Check if the given map is obviously injective.
10660 int isl_map_plain_is_injective(__isl_keep isl_map *map)
10662 int in;
10664 map = isl_map_copy(map);
10665 map = isl_map_reverse(map);
10666 in = isl_map_plain_is_single_valued(map);
10667 isl_map_free(map);
10669 return in;
10672 int isl_map_is_bijective(__isl_keep isl_map *map)
10674 int sv;
10676 sv = isl_map_is_single_valued(map);
10677 if (sv < 0 || !sv)
10678 return sv;
10680 return isl_map_is_injective(map);
10683 int isl_set_is_singleton(__isl_keep isl_set *set)
10685 return isl_map_is_single_valued((isl_map *)set);
10688 int isl_map_is_translation(__isl_keep isl_map *map)
10690 int ok;
10691 isl_set *delta;
10693 delta = isl_map_deltas(isl_map_copy(map));
10694 ok = isl_set_is_singleton(delta);
10695 isl_set_free(delta);
10697 return ok;
10700 static int unique(isl_int *p, unsigned pos, unsigned len)
10702 if (isl_seq_first_non_zero(p, pos) != -1)
10703 return 0;
10704 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10705 return 0;
10706 return 1;
10709 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10711 int i, j;
10712 unsigned nvar;
10713 unsigned ovar;
10715 if (!bset)
10716 return -1;
10718 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10719 return 0;
10721 nvar = isl_basic_set_dim(bset, isl_dim_set);
10722 ovar = isl_space_offset(bset->dim, isl_dim_set);
10723 for (j = 0; j < nvar; ++j) {
10724 int lower = 0, upper = 0;
10725 for (i = 0; i < bset->n_eq; ++i) {
10726 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10727 continue;
10728 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10729 return 0;
10730 break;
10732 if (i < bset->n_eq)
10733 continue;
10734 for (i = 0; i < bset->n_ineq; ++i) {
10735 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10736 continue;
10737 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10738 return 0;
10739 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10740 lower = 1;
10741 else
10742 upper = 1;
10744 if (!lower || !upper)
10745 return 0;
10748 return 1;
10751 int isl_set_is_box(__isl_keep isl_set *set)
10753 if (!set)
10754 return -1;
10755 if (set->n != 1)
10756 return 0;
10758 return isl_basic_set_is_box(set->p[0]);
10761 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10763 if (!bset)
10764 return -1;
10766 return isl_space_is_wrapping(bset->dim);
10769 int isl_set_is_wrapping(__isl_keep isl_set *set)
10771 if (!set)
10772 return -1;
10774 return isl_space_is_wrapping(set->dim);
10777 /* Is the domain of "map" a wrapped relation?
10779 int isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10781 if (!map)
10782 return -1;
10784 return isl_space_domain_is_wrapping(map->dim);
10787 /* Is the range of "map" a wrapped relation?
10789 int isl_map_range_is_wrapping(__isl_keep isl_map *map)
10791 if (!map)
10792 return -1;
10794 return isl_space_range_is_wrapping(map->dim);
10797 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10799 bmap = isl_basic_map_cow(bmap);
10800 if (!bmap)
10801 return NULL;
10803 bmap->dim = isl_space_wrap(bmap->dim);
10804 if (!bmap->dim)
10805 goto error;
10807 bmap = isl_basic_map_finalize(bmap);
10809 return (isl_basic_set *)bmap;
10810 error:
10811 isl_basic_map_free(bmap);
10812 return NULL;
10815 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10817 int i;
10819 map = isl_map_cow(map);
10820 if (!map)
10821 return NULL;
10823 for (i = 0; i < map->n; ++i) {
10824 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10825 if (!map->p[i])
10826 goto error;
10828 map->dim = isl_space_wrap(map->dim);
10829 if (!map->dim)
10830 goto error;
10832 return (isl_set *)map;
10833 error:
10834 isl_map_free(map);
10835 return NULL;
10838 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10840 bset = isl_basic_set_cow(bset);
10841 if (!bset)
10842 return NULL;
10844 bset->dim = isl_space_unwrap(bset->dim);
10845 if (!bset->dim)
10846 goto error;
10848 bset = isl_basic_set_finalize(bset);
10850 return (isl_basic_map *)bset;
10851 error:
10852 isl_basic_set_free(bset);
10853 return NULL;
10856 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10858 int i;
10860 if (!set)
10861 return NULL;
10863 if (!isl_set_is_wrapping(set))
10864 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10865 goto error);
10867 set = isl_set_cow(set);
10868 if (!set)
10869 return NULL;
10871 for (i = 0; i < set->n; ++i) {
10872 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10873 if (!set->p[i])
10874 goto error;
10877 set->dim = isl_space_unwrap(set->dim);
10878 if (!set->dim)
10879 goto error;
10881 return (isl_map *)set;
10882 error:
10883 isl_set_free(set);
10884 return NULL;
10887 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10888 enum isl_dim_type type)
10890 if (!bmap)
10891 return NULL;
10893 if (!isl_space_is_named_or_nested(bmap->dim, type))
10894 return bmap;
10896 bmap = isl_basic_map_cow(bmap);
10897 if (!bmap)
10898 return NULL;
10900 bmap->dim = isl_space_reset(bmap->dim, type);
10901 if (!bmap->dim)
10902 goto error;
10904 bmap = isl_basic_map_finalize(bmap);
10906 return bmap;
10907 error:
10908 isl_basic_map_free(bmap);
10909 return NULL;
10912 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10913 enum isl_dim_type type)
10915 int i;
10917 if (!map)
10918 return NULL;
10920 if (!isl_space_is_named_or_nested(map->dim, type))
10921 return map;
10923 map = isl_map_cow(map);
10924 if (!map)
10925 return NULL;
10927 for (i = 0; i < map->n; ++i) {
10928 map->p[i] = isl_basic_map_reset(map->p[i], type);
10929 if (!map->p[i])
10930 goto error;
10932 map->dim = isl_space_reset(map->dim, type);
10933 if (!map->dim)
10934 goto error;
10936 return map;
10937 error:
10938 isl_map_free(map);
10939 return NULL;
10942 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10944 if (!bmap)
10945 return NULL;
10947 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10948 return bmap;
10950 bmap = isl_basic_map_cow(bmap);
10951 if (!bmap)
10952 return NULL;
10954 bmap->dim = isl_space_flatten(bmap->dim);
10955 if (!bmap->dim)
10956 goto error;
10958 bmap = isl_basic_map_finalize(bmap);
10960 return bmap;
10961 error:
10962 isl_basic_map_free(bmap);
10963 return NULL;
10966 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10968 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10971 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10972 __isl_take isl_basic_map *bmap)
10974 if (!bmap)
10975 return NULL;
10977 if (!bmap->dim->nested[0])
10978 return bmap;
10980 bmap = isl_basic_map_cow(bmap);
10981 if (!bmap)
10982 return NULL;
10984 bmap->dim = isl_space_flatten_domain(bmap->dim);
10985 if (!bmap->dim)
10986 goto error;
10988 bmap = isl_basic_map_finalize(bmap);
10990 return bmap;
10991 error:
10992 isl_basic_map_free(bmap);
10993 return NULL;
10996 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10997 __isl_take isl_basic_map *bmap)
10999 if (!bmap)
11000 return NULL;
11002 if (!bmap->dim->nested[1])
11003 return bmap;
11005 bmap = isl_basic_map_cow(bmap);
11006 if (!bmap)
11007 return NULL;
11009 bmap->dim = isl_space_flatten_range(bmap->dim);
11010 if (!bmap->dim)
11011 goto error;
11013 bmap = isl_basic_map_finalize(bmap);
11015 return bmap;
11016 error:
11017 isl_basic_map_free(bmap);
11018 return NULL;
11021 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11023 int i;
11025 if (!map)
11026 return NULL;
11028 if (!map->dim->nested[0] && !map->dim->nested[1])
11029 return map;
11031 map = isl_map_cow(map);
11032 if (!map)
11033 return NULL;
11035 for (i = 0; i < map->n; ++i) {
11036 map->p[i] = isl_basic_map_flatten(map->p[i]);
11037 if (!map->p[i])
11038 goto error;
11040 map->dim = isl_space_flatten(map->dim);
11041 if (!map->dim)
11042 goto error;
11044 return map;
11045 error:
11046 isl_map_free(map);
11047 return NULL;
11050 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11052 return (isl_set *)isl_map_flatten((isl_map *)set);
11055 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11057 isl_space *dim, *flat_dim;
11058 isl_map *map;
11060 dim = isl_set_get_space(set);
11061 flat_dim = isl_space_flatten(isl_space_copy(dim));
11062 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11063 map = isl_map_intersect_domain(map, set);
11065 return map;
11068 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11070 int i;
11072 if (!map)
11073 return NULL;
11075 if (!map->dim->nested[0])
11076 return map;
11078 map = isl_map_cow(map);
11079 if (!map)
11080 return NULL;
11082 for (i = 0; i < map->n; ++i) {
11083 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
11084 if (!map->p[i])
11085 goto error;
11087 map->dim = isl_space_flatten_domain(map->dim);
11088 if (!map->dim)
11089 goto error;
11091 return map;
11092 error:
11093 isl_map_free(map);
11094 return NULL;
11097 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11099 int i;
11101 if (!map)
11102 return NULL;
11104 if (!map->dim->nested[1])
11105 return map;
11107 map = isl_map_cow(map);
11108 if (!map)
11109 return NULL;
11111 for (i = 0; i < map->n; ++i) {
11112 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
11113 if (!map->p[i])
11114 goto error;
11116 map->dim = isl_space_flatten_range(map->dim);
11117 if (!map->dim)
11118 goto error;
11120 return map;
11121 error:
11122 isl_map_free(map);
11123 return NULL;
11126 /* Reorder the dimensions of "bmap" according to the given dim_map
11127 * and set the dimension specification to "dim".
11129 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11130 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11132 isl_basic_map *res;
11133 unsigned flags;
11135 bmap = isl_basic_map_cow(bmap);
11136 if (!bmap || !dim || !dim_map)
11137 goto error;
11139 flags = bmap->flags;
11140 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11141 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11142 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11143 res = isl_basic_map_alloc_space(dim,
11144 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11145 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11146 if (res)
11147 res->flags = flags;
11148 res = isl_basic_map_finalize(res);
11149 return res;
11150 error:
11151 free(dim_map);
11152 isl_basic_map_free(bmap);
11153 isl_space_free(dim);
11154 return NULL;
11157 /* Reorder the dimensions of "map" according to given reordering.
11159 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11160 __isl_take isl_reordering *r)
11162 int i;
11163 struct isl_dim_map *dim_map;
11165 map = isl_map_cow(map);
11166 dim_map = isl_dim_map_from_reordering(r);
11167 if (!map || !r || !dim_map)
11168 goto error;
11170 for (i = 0; i < map->n; ++i) {
11171 struct isl_dim_map *dim_map_i;
11173 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11175 map->p[i] = isl_basic_map_realign(map->p[i],
11176 isl_space_copy(r->dim), dim_map_i);
11178 if (!map->p[i])
11179 goto error;
11182 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11184 isl_reordering_free(r);
11185 free(dim_map);
11186 return map;
11187 error:
11188 free(dim_map);
11189 isl_map_free(map);
11190 isl_reordering_free(r);
11191 return NULL;
11194 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11195 __isl_take isl_reordering *r)
11197 return (isl_set *)isl_map_realign((isl_map *)set, r);
11200 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11201 __isl_take isl_space *model)
11203 isl_ctx *ctx;
11205 if (!map || !model)
11206 goto error;
11208 ctx = isl_space_get_ctx(model);
11209 if (!isl_space_has_named_params(model))
11210 isl_die(ctx, isl_error_invalid,
11211 "model has unnamed parameters", goto error);
11212 if (!isl_space_has_named_params(map->dim))
11213 isl_die(ctx, isl_error_invalid,
11214 "relation has unnamed parameters", goto error);
11215 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11216 isl_reordering *exp;
11218 model = isl_space_drop_dims(model, isl_dim_in,
11219 0, isl_space_dim(model, isl_dim_in));
11220 model = isl_space_drop_dims(model, isl_dim_out,
11221 0, isl_space_dim(model, isl_dim_out));
11222 exp = isl_parameter_alignment_reordering(map->dim, model);
11223 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11224 map = isl_map_realign(map, exp);
11227 isl_space_free(model);
11228 return map;
11229 error:
11230 isl_space_free(model);
11231 isl_map_free(map);
11232 return NULL;
11235 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11236 __isl_take isl_space *model)
11238 return isl_map_align_params(set, model);
11241 /* Align the parameters of "bmap" to those of "model", introducing
11242 * additional parameters if needed.
11244 __isl_give isl_basic_map *isl_basic_map_align_params(
11245 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11247 isl_ctx *ctx;
11249 if (!bmap || !model)
11250 goto error;
11252 ctx = isl_space_get_ctx(model);
11253 if (!isl_space_has_named_params(model))
11254 isl_die(ctx, isl_error_invalid,
11255 "model has unnamed parameters", goto error);
11256 if (!isl_space_has_named_params(bmap->dim))
11257 isl_die(ctx, isl_error_invalid,
11258 "relation has unnamed parameters", goto error);
11259 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11260 isl_reordering *exp;
11261 struct isl_dim_map *dim_map;
11263 model = isl_space_drop_dims(model, isl_dim_in,
11264 0, isl_space_dim(model, isl_dim_in));
11265 model = isl_space_drop_dims(model, isl_dim_out,
11266 0, isl_space_dim(model, isl_dim_out));
11267 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11268 exp = isl_reordering_extend_space(exp,
11269 isl_basic_map_get_space(bmap));
11270 dim_map = isl_dim_map_from_reordering(exp);
11271 bmap = isl_basic_map_realign(bmap,
11272 exp ? isl_space_copy(exp->dim) : NULL,
11273 isl_dim_map_extend(dim_map, bmap));
11274 isl_reordering_free(exp);
11275 free(dim_map);
11278 isl_space_free(model);
11279 return bmap;
11280 error:
11281 isl_space_free(model);
11282 isl_basic_map_free(bmap);
11283 return NULL;
11286 /* Align the parameters of "bset" to those of "model", introducing
11287 * additional parameters if needed.
11289 __isl_give isl_basic_set *isl_basic_set_align_params(
11290 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11292 return isl_basic_map_align_params(bset, model);
11295 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11296 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11297 enum isl_dim_type c2, enum isl_dim_type c3,
11298 enum isl_dim_type c4, enum isl_dim_type c5)
11300 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11301 struct isl_mat *mat;
11302 int i, j, k;
11303 int pos;
11305 if (!bmap)
11306 return NULL;
11307 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11308 isl_basic_map_total_dim(bmap) + 1);
11309 if (!mat)
11310 return NULL;
11311 for (i = 0; i < bmap->n_eq; ++i)
11312 for (j = 0, pos = 0; j < 5; ++j) {
11313 int off = isl_basic_map_offset(bmap, c[j]);
11314 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11315 isl_int_set(mat->row[i][pos],
11316 bmap->eq[i][off + k]);
11317 ++pos;
11321 return mat;
11324 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11325 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11326 enum isl_dim_type c2, enum isl_dim_type c3,
11327 enum isl_dim_type c4, enum isl_dim_type c5)
11329 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11330 struct isl_mat *mat;
11331 int i, j, k;
11332 int pos;
11334 if (!bmap)
11335 return NULL;
11336 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11337 isl_basic_map_total_dim(bmap) + 1);
11338 if (!mat)
11339 return NULL;
11340 for (i = 0; i < bmap->n_ineq; ++i)
11341 for (j = 0, pos = 0; j < 5; ++j) {
11342 int off = isl_basic_map_offset(bmap, c[j]);
11343 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11344 isl_int_set(mat->row[i][pos],
11345 bmap->ineq[i][off + k]);
11346 ++pos;
11350 return mat;
11353 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11354 __isl_take isl_space *dim,
11355 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11356 enum isl_dim_type c2, enum isl_dim_type c3,
11357 enum isl_dim_type c4, enum isl_dim_type c5)
11359 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11360 isl_basic_map *bmap;
11361 unsigned total;
11362 unsigned extra;
11363 int i, j, k, l;
11364 int pos;
11366 if (!dim || !eq || !ineq)
11367 goto error;
11369 if (eq->n_col != ineq->n_col)
11370 isl_die(dim->ctx, isl_error_invalid,
11371 "equalities and inequalities matrices should have "
11372 "same number of columns", goto error);
11374 total = 1 + isl_space_dim(dim, isl_dim_all);
11376 if (eq->n_col < total)
11377 isl_die(dim->ctx, isl_error_invalid,
11378 "number of columns too small", goto error);
11380 extra = eq->n_col - total;
11382 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11383 eq->n_row, ineq->n_row);
11384 if (!bmap)
11385 goto error;
11386 for (i = 0; i < extra; ++i) {
11387 k = isl_basic_map_alloc_div(bmap);
11388 if (k < 0)
11389 goto error;
11390 isl_int_set_si(bmap->div[k][0], 0);
11392 for (i = 0; i < eq->n_row; ++i) {
11393 l = isl_basic_map_alloc_equality(bmap);
11394 if (l < 0)
11395 goto error;
11396 for (j = 0, pos = 0; j < 5; ++j) {
11397 int off = isl_basic_map_offset(bmap, c[j]);
11398 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11399 isl_int_set(bmap->eq[l][off + k],
11400 eq->row[i][pos]);
11401 ++pos;
11405 for (i = 0; i < ineq->n_row; ++i) {
11406 l = isl_basic_map_alloc_inequality(bmap);
11407 if (l < 0)
11408 goto error;
11409 for (j = 0, pos = 0; j < 5; ++j) {
11410 int off = isl_basic_map_offset(bmap, c[j]);
11411 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11412 isl_int_set(bmap->ineq[l][off + k],
11413 ineq->row[i][pos]);
11414 ++pos;
11419 isl_space_free(dim);
11420 isl_mat_free(eq);
11421 isl_mat_free(ineq);
11423 bmap = isl_basic_map_simplify(bmap);
11424 return isl_basic_map_finalize(bmap);
11425 error:
11426 isl_space_free(dim);
11427 isl_mat_free(eq);
11428 isl_mat_free(ineq);
11429 return NULL;
11432 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11433 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11434 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11436 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11437 c1, c2, c3, c4, isl_dim_in);
11440 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11441 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11442 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11444 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11445 c1, c2, c3, c4, isl_dim_in);
11448 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11449 __isl_take isl_space *dim,
11450 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11451 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11453 return (isl_basic_set*)
11454 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11455 c1, c2, c3, c4, isl_dim_in);
11458 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11460 if (!bmap)
11461 return -1;
11463 return isl_space_can_zip(bmap->dim);
11466 int isl_map_can_zip(__isl_keep isl_map *map)
11468 if (!map)
11469 return -1;
11471 return isl_space_can_zip(map->dim);
11474 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11475 * (A -> C) -> (B -> D).
11477 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11479 unsigned pos;
11480 unsigned n1;
11481 unsigned n2;
11483 if (!bmap)
11484 return NULL;
11486 if (!isl_basic_map_can_zip(bmap))
11487 isl_die(bmap->ctx, isl_error_invalid,
11488 "basic map cannot be zipped", goto error);
11489 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11490 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11491 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11492 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11493 bmap = isl_basic_map_cow(bmap);
11494 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11495 if (!bmap)
11496 return NULL;
11497 bmap->dim = isl_space_zip(bmap->dim);
11498 if (!bmap->dim)
11499 goto error;
11500 return bmap;
11501 error:
11502 isl_basic_map_free(bmap);
11503 return NULL;
11506 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11507 * (A -> C) -> (B -> D).
11509 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11511 int i;
11513 if (!map)
11514 return NULL;
11516 if (!isl_map_can_zip(map))
11517 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11518 goto error);
11520 map = isl_map_cow(map);
11521 if (!map)
11522 return NULL;
11524 for (i = 0; i < map->n; ++i) {
11525 map->p[i] = isl_basic_map_zip(map->p[i]);
11526 if (!map->p[i])
11527 goto error;
11530 map->dim = isl_space_zip(map->dim);
11531 if (!map->dim)
11532 goto error;
11534 return map;
11535 error:
11536 isl_map_free(map);
11537 return NULL;
11540 /* Can we apply isl_basic_map_curry to "bmap"?
11541 * That is, does it have a nested relation in its domain?
11543 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11545 if (!bmap)
11546 return -1;
11548 return isl_space_can_curry(bmap->dim);
11551 /* Can we apply isl_map_curry to "map"?
11552 * That is, does it have a nested relation in its domain?
11554 int isl_map_can_curry(__isl_keep isl_map *map)
11556 if (!map)
11557 return -1;
11559 return isl_space_can_curry(map->dim);
11562 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11563 * A -> (B -> C).
11565 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11568 if (!bmap)
11569 return NULL;
11571 if (!isl_basic_map_can_curry(bmap))
11572 isl_die(bmap->ctx, isl_error_invalid,
11573 "basic map cannot be curried", goto error);
11574 bmap = isl_basic_map_cow(bmap);
11575 if (!bmap)
11576 return NULL;
11577 bmap->dim = isl_space_curry(bmap->dim);
11578 if (!bmap->dim)
11579 goto error;
11580 return bmap;
11581 error:
11582 isl_basic_map_free(bmap);
11583 return NULL;
11586 /* Given a map (A -> B) -> C, return the corresponding map
11587 * A -> (B -> C).
11589 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11591 int i;
11593 if (!map)
11594 return NULL;
11596 if (!isl_map_can_curry(map))
11597 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11598 goto error);
11600 map = isl_map_cow(map);
11601 if (!map)
11602 return NULL;
11604 for (i = 0; i < map->n; ++i) {
11605 map->p[i] = isl_basic_map_curry(map->p[i]);
11606 if (!map->p[i])
11607 goto error;
11610 map->dim = isl_space_curry(map->dim);
11611 if (!map->dim)
11612 goto error;
11614 return map;
11615 error:
11616 isl_map_free(map);
11617 return NULL;
11620 /* Can we apply isl_basic_map_uncurry to "bmap"?
11621 * That is, does it have a nested relation in its domain?
11623 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11625 if (!bmap)
11626 return -1;
11628 return isl_space_can_uncurry(bmap->dim);
11631 /* Can we apply isl_map_uncurry to "map"?
11632 * That is, does it have a nested relation in its domain?
11634 int isl_map_can_uncurry(__isl_keep isl_map *map)
11636 if (!map)
11637 return -1;
11639 return isl_space_can_uncurry(map->dim);
11642 /* Given a basic map A -> (B -> C), return the corresponding basic map
11643 * (A -> B) -> C.
11645 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11648 if (!bmap)
11649 return NULL;
11651 if (!isl_basic_map_can_uncurry(bmap))
11652 isl_die(bmap->ctx, isl_error_invalid,
11653 "basic map cannot be uncurried",
11654 return isl_basic_map_free(bmap));
11655 bmap = isl_basic_map_cow(bmap);
11656 if (!bmap)
11657 return NULL;
11658 bmap->dim = isl_space_uncurry(bmap->dim);
11659 if (!bmap->dim)
11660 return isl_basic_map_free(bmap);
11661 return bmap;
11664 /* Given a map A -> (B -> C), return the corresponding map
11665 * (A -> B) -> C.
11667 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11669 int i;
11671 if (!map)
11672 return NULL;
11674 if (!isl_map_can_uncurry(map))
11675 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11676 return isl_map_free(map));
11678 map = isl_map_cow(map);
11679 if (!map)
11680 return NULL;
11682 for (i = 0; i < map->n; ++i) {
11683 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11684 if (!map->p[i])
11685 return isl_map_free(map);
11688 map->dim = isl_space_uncurry(map->dim);
11689 if (!map->dim)
11690 return isl_map_free(map);
11692 return map;
11695 /* Construct a basic map mapping the domain of the affine expression
11696 * to a one-dimensional range prescribed by the affine expression.
11698 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11700 int k;
11701 int pos;
11702 isl_local_space *ls;
11703 isl_basic_map *bmap;
11705 if (!aff)
11706 return NULL;
11708 ls = isl_aff_get_local_space(aff);
11709 bmap = isl_basic_map_from_local_space(ls);
11710 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11711 k = isl_basic_map_alloc_equality(bmap);
11712 if (k < 0)
11713 goto error;
11715 pos = isl_basic_map_offset(bmap, isl_dim_out);
11716 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11717 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11718 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11719 aff->v->size - (pos + 1));
11721 isl_aff_free(aff);
11722 bmap = isl_basic_map_finalize(bmap);
11723 return bmap;
11724 error:
11725 isl_aff_free(aff);
11726 isl_basic_map_free(bmap);
11727 return NULL;
11730 /* Construct a map mapping the domain of the affine expression
11731 * to a one-dimensional range prescribed by the affine expression.
11733 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11735 isl_basic_map *bmap;
11737 bmap = isl_basic_map_from_aff(aff);
11738 return isl_map_from_basic_map(bmap);
11741 /* Construct a basic map mapping the domain the multi-affine expression
11742 * to its range, with each dimension in the range equated to the
11743 * corresponding affine expression.
11745 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11746 __isl_take isl_multi_aff *maff)
11748 int i;
11749 isl_space *space;
11750 isl_basic_map *bmap;
11752 if (!maff)
11753 return NULL;
11755 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11756 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11757 "invalid space", goto error);
11759 space = isl_space_domain(isl_multi_aff_get_space(maff));
11760 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11762 for (i = 0; i < maff->n; ++i) {
11763 isl_aff *aff;
11764 isl_basic_map *bmap_i;
11766 aff = isl_aff_copy(maff->p[i]);
11767 bmap_i = isl_basic_map_from_aff(aff);
11769 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11772 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11774 isl_multi_aff_free(maff);
11775 return bmap;
11776 error:
11777 isl_multi_aff_free(maff);
11778 return NULL;
11781 /* Construct a map mapping the domain the multi-affine expression
11782 * to its range, with each dimension in the range equated to the
11783 * corresponding affine expression.
11785 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11787 isl_basic_map *bmap;
11789 bmap = isl_basic_map_from_multi_aff(maff);
11790 return isl_map_from_basic_map(bmap);
11793 /* Construct a basic map mapping a domain in the given space to
11794 * to an n-dimensional range, with n the number of elements in the list,
11795 * where each coordinate in the range is prescribed by the
11796 * corresponding affine expression.
11797 * The domains of all affine expressions in the list are assumed to match
11798 * domain_dim.
11800 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11801 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11803 int i;
11804 isl_space *dim;
11805 isl_basic_map *bmap;
11807 if (!list)
11808 return NULL;
11810 dim = isl_space_from_domain(domain_dim);
11811 bmap = isl_basic_map_universe(dim);
11813 for (i = 0; i < list->n; ++i) {
11814 isl_aff *aff;
11815 isl_basic_map *bmap_i;
11817 aff = isl_aff_copy(list->p[i]);
11818 bmap_i = isl_basic_map_from_aff(aff);
11820 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11823 isl_aff_list_free(list);
11824 return bmap;
11827 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11828 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11830 return isl_map_equate(set, type1, pos1, type2, pos2);
11833 /* Construct a basic map where the given dimensions are equal to each other.
11835 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11836 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11838 isl_basic_map *bmap = NULL;
11839 int i;
11841 if (!space)
11842 return NULL;
11844 if (pos1 >= isl_space_dim(space, type1))
11845 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11846 "index out of bounds", goto error);
11847 if (pos2 >= isl_space_dim(space, type2))
11848 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11849 "index out of bounds", goto error);
11851 if (type1 == type2 && pos1 == pos2)
11852 return isl_basic_map_universe(space);
11854 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11855 i = isl_basic_map_alloc_equality(bmap);
11856 if (i < 0)
11857 goto error;
11858 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11859 pos1 += isl_basic_map_offset(bmap, type1);
11860 pos2 += isl_basic_map_offset(bmap, type2);
11861 isl_int_set_si(bmap->eq[i][pos1], -1);
11862 isl_int_set_si(bmap->eq[i][pos2], 1);
11863 bmap = isl_basic_map_finalize(bmap);
11864 isl_space_free(space);
11865 return bmap;
11866 error:
11867 isl_space_free(space);
11868 isl_basic_map_free(bmap);
11869 return NULL;
11872 /* Add a constraint imposing that the given two dimensions are equal.
11874 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11875 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11877 isl_basic_map *eq;
11879 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11881 bmap = isl_basic_map_intersect(bmap, eq);
11883 return bmap;
11886 /* Add a constraint imposing that the given two dimensions are equal.
11888 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11889 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11891 isl_basic_map *bmap;
11893 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11895 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11897 return map;
11900 /* Add a constraint imposing that the given two dimensions have opposite values.
11902 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11903 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11905 isl_basic_map *bmap = NULL;
11906 int i;
11908 if (!map)
11909 return NULL;
11911 if (pos1 >= isl_map_dim(map, type1))
11912 isl_die(map->ctx, isl_error_invalid,
11913 "index out of bounds", goto error);
11914 if (pos2 >= isl_map_dim(map, type2))
11915 isl_die(map->ctx, isl_error_invalid,
11916 "index out of bounds", goto error);
11918 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11919 i = isl_basic_map_alloc_equality(bmap);
11920 if (i < 0)
11921 goto error;
11922 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11923 pos1 += isl_basic_map_offset(bmap, type1);
11924 pos2 += isl_basic_map_offset(bmap, type2);
11925 isl_int_set_si(bmap->eq[i][pos1], 1);
11926 isl_int_set_si(bmap->eq[i][pos2], 1);
11927 bmap = isl_basic_map_finalize(bmap);
11929 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11931 return map;
11932 error:
11933 isl_basic_map_free(bmap);
11934 isl_map_free(map);
11935 return NULL;
11938 /* Construct a constraint imposing that the value of the first dimension is
11939 * greater than or equal to that of the second.
11941 static __isl_give isl_constraint *constraint_order_ge(
11942 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11943 enum isl_dim_type type2, int pos2)
11945 isl_constraint *c;
11947 if (!space)
11948 return NULL;
11950 c = isl_inequality_alloc(isl_local_space_from_space(space));
11952 if (pos1 >= isl_constraint_dim(c, type1))
11953 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11954 "index out of bounds", return isl_constraint_free(c));
11955 if (pos2 >= isl_constraint_dim(c, type2))
11956 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11957 "index out of bounds", return isl_constraint_free(c));
11959 if (type1 == type2 && pos1 == pos2)
11960 return c;
11962 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11963 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11965 return c;
11968 /* Add a constraint imposing that the value of the first dimension is
11969 * greater than or equal to that of the second.
11971 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11972 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11974 isl_constraint *c;
11975 isl_space *space;
11977 if (type1 == type2 && pos1 == pos2)
11978 return bmap;
11979 space = isl_basic_map_get_space(bmap);
11980 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11981 bmap = isl_basic_map_add_constraint(bmap, c);
11983 return bmap;
11986 /* Add a constraint imposing that the value of the first dimension is
11987 * greater than or equal to that of the second.
11989 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11990 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11992 isl_constraint *c;
11993 isl_space *space;
11995 if (type1 == type2 && pos1 == pos2)
11996 return map;
11997 space = isl_map_get_space(map);
11998 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11999 map = isl_map_add_constraint(map, c);
12001 return map;
12004 /* Add a constraint imposing that the value of the first dimension is
12005 * less than or equal to that of the second.
12007 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12008 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12010 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12013 /* Construct a basic map where the value of the first dimension is
12014 * greater than that of the second.
12016 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12017 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12019 isl_basic_map *bmap = NULL;
12020 int i;
12022 if (!space)
12023 return NULL;
12025 if (pos1 >= isl_space_dim(space, type1))
12026 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12027 "index out of bounds", goto error);
12028 if (pos2 >= isl_space_dim(space, type2))
12029 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12030 "index out of bounds", goto error);
12032 if (type1 == type2 && pos1 == pos2)
12033 return isl_basic_map_empty(space);
12035 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12036 i = isl_basic_map_alloc_inequality(bmap);
12037 if (i < 0)
12038 return isl_basic_map_free(bmap);
12039 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12040 pos1 += isl_basic_map_offset(bmap, type1);
12041 pos2 += isl_basic_map_offset(bmap, type2);
12042 isl_int_set_si(bmap->ineq[i][pos1], 1);
12043 isl_int_set_si(bmap->ineq[i][pos2], -1);
12044 isl_int_set_si(bmap->ineq[i][0], -1);
12045 bmap = isl_basic_map_finalize(bmap);
12047 return bmap;
12048 error:
12049 isl_space_free(space);
12050 isl_basic_map_free(bmap);
12051 return NULL;
12054 /* Add a constraint imposing that the value of the first dimension is
12055 * greater than that of the second.
12057 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12058 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12060 isl_basic_map *gt;
12062 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12064 bmap = isl_basic_map_intersect(bmap, gt);
12066 return bmap;
12069 /* Add a constraint imposing that the value of the first dimension is
12070 * greater than that of the second.
12072 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12073 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12075 isl_basic_map *bmap;
12077 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12079 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12081 return map;
12084 /* Add a constraint imposing that the value of the first dimension is
12085 * smaller than that of the second.
12087 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12088 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12090 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12093 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12094 int pos)
12096 isl_aff *div;
12097 isl_local_space *ls;
12099 if (!bmap)
12100 return NULL;
12102 if (!isl_basic_map_divs_known(bmap))
12103 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12104 "some divs are unknown", return NULL);
12106 ls = isl_basic_map_get_local_space(bmap);
12107 div = isl_local_space_get_div(ls, pos);
12108 isl_local_space_free(ls);
12110 return div;
12113 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12114 int pos)
12116 return isl_basic_map_get_div(bset, pos);
12119 /* Plug in "subs" for dimension "type", "pos" of "bset".
12121 * Let i be the dimension to replace and let "subs" be of the form
12123 * f/d
12125 * Any integer division with a non-zero coefficient for i,
12127 * floor((a i + g)/m)
12129 * is replaced by
12131 * floor((a f + d g)/(m d))
12133 * Constraints of the form
12135 * a i + g
12137 * are replaced by
12139 * a f + d g
12141 * We currently require that "subs" is an integral expression.
12142 * Handling rational expressions may require us to add stride constraints
12143 * as we do in isl_basic_set_preimage_multi_aff.
12145 __isl_give isl_basic_set *isl_basic_set_substitute(
12146 __isl_take isl_basic_set *bset,
12147 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12149 int i;
12150 isl_int v;
12151 isl_ctx *ctx;
12153 if (bset && isl_basic_set_plain_is_empty(bset))
12154 return bset;
12156 bset = isl_basic_set_cow(bset);
12157 if (!bset || !subs)
12158 goto error;
12160 ctx = isl_basic_set_get_ctx(bset);
12161 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12162 isl_die(ctx, isl_error_invalid,
12163 "spaces don't match", goto error);
12164 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12165 isl_die(ctx, isl_error_unsupported,
12166 "cannot handle divs yet", goto error);
12167 if (!isl_int_is_one(subs->v->el[0]))
12168 isl_die(ctx, isl_error_invalid,
12169 "can only substitute integer expressions", goto error);
12171 pos += isl_basic_set_offset(bset, type);
12173 isl_int_init(v);
12175 for (i = 0; i < bset->n_eq; ++i) {
12176 if (isl_int_is_zero(bset->eq[i][pos]))
12177 continue;
12178 isl_int_set(v, bset->eq[i][pos]);
12179 isl_int_set_si(bset->eq[i][pos], 0);
12180 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12181 v, subs->v->el + 1, subs->v->size - 1);
12184 for (i = 0; i < bset->n_ineq; ++i) {
12185 if (isl_int_is_zero(bset->ineq[i][pos]))
12186 continue;
12187 isl_int_set(v, bset->ineq[i][pos]);
12188 isl_int_set_si(bset->ineq[i][pos], 0);
12189 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12190 v, subs->v->el + 1, subs->v->size - 1);
12193 for (i = 0; i < bset->n_div; ++i) {
12194 if (isl_int_is_zero(bset->div[i][1 + pos]))
12195 continue;
12196 isl_int_set(v, bset->div[i][1 + pos]);
12197 isl_int_set_si(bset->div[i][1 + pos], 0);
12198 isl_seq_combine(bset->div[i] + 1,
12199 subs->v->el[0], bset->div[i] + 1,
12200 v, subs->v->el + 1, subs->v->size - 1);
12201 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12204 isl_int_clear(v);
12206 bset = isl_basic_set_simplify(bset);
12207 return isl_basic_set_finalize(bset);
12208 error:
12209 isl_basic_set_free(bset);
12210 return NULL;
12213 /* Plug in "subs" for dimension "type", "pos" of "set".
12215 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12216 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12218 int i;
12220 if (set && isl_set_plain_is_empty(set))
12221 return set;
12223 set = isl_set_cow(set);
12224 if (!set || !subs)
12225 goto error;
12227 for (i = set->n - 1; i >= 0; --i) {
12228 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12229 if (remove_if_empty(set, i) < 0)
12230 goto error;
12233 return set;
12234 error:
12235 isl_set_free(set);
12236 return NULL;
12239 /* Check if the range of "ma" is compatible with the domain or range
12240 * (depending on "type") of "bmap".
12241 * Return -1 if anything is wrong.
12243 static int check_basic_map_compatible_range_multi_aff(
12244 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12245 __isl_keep isl_multi_aff *ma)
12247 int m;
12248 isl_space *ma_space;
12250 ma_space = isl_multi_aff_get_space(ma);
12252 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12253 if (m < 0)
12254 goto error;
12255 if (!m)
12256 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12257 "parameters don't match", goto error);
12258 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12259 if (m < 0)
12260 goto error;
12261 if (!m)
12262 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12263 "spaces don't match", goto error);
12265 isl_space_free(ma_space);
12266 return m;
12267 error:
12268 isl_space_free(ma_space);
12269 return -1;
12272 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12273 * coefficients before the transformed range of dimensions,
12274 * the "n_after" coefficients after the transformed range of dimensions
12275 * and the coefficients of the other divs in "bmap".
12277 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12278 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12280 int i;
12281 int n_param;
12282 int n_set;
12283 isl_local_space *ls;
12285 if (n_div == 0)
12286 return 0;
12288 ls = isl_aff_get_domain_local_space(ma->p[0]);
12289 if (!ls)
12290 return -1;
12292 n_param = isl_local_space_dim(ls, isl_dim_param);
12293 n_set = isl_local_space_dim(ls, isl_dim_set);
12294 for (i = 0; i < n_div; ++i) {
12295 int o_bmap = 0, o_ls = 0;
12297 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12298 o_bmap += 1 + 1 + n_param;
12299 o_ls += 1 + 1 + n_param;
12300 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12301 o_bmap += n_before;
12302 isl_seq_cpy(bmap->div[i] + o_bmap,
12303 ls->div->row[i] + o_ls, n_set);
12304 o_bmap += n_set;
12305 o_ls += n_set;
12306 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12307 o_bmap += n_after;
12308 isl_seq_cpy(bmap->div[i] + o_bmap,
12309 ls->div->row[i] + o_ls, n_div);
12310 o_bmap += n_div;
12311 o_ls += n_div;
12312 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12313 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12314 goto error;
12317 isl_local_space_free(ls);
12318 return 0;
12319 error:
12320 isl_local_space_free(ls);
12321 return -1;
12324 /* How many stride constraints does "ma" enforce?
12325 * That is, how many of the affine expressions have a denominator
12326 * different from one?
12328 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12330 int i;
12331 int strides = 0;
12333 for (i = 0; i < ma->n; ++i)
12334 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12335 strides++;
12337 return strides;
12340 /* For each affine expression in ma of the form
12342 * x_i = (f_i y + h_i)/m_i
12344 * with m_i different from one, add a constraint to "bmap"
12345 * of the form
12347 * f_i y + h_i = m_i alpha_i
12349 * with alpha_i an additional existentially quantified variable.
12351 static __isl_give isl_basic_map *add_ma_strides(
12352 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12353 int n_before, int n_after)
12355 int i, k;
12356 int div;
12357 int total;
12358 int n_param;
12359 int n_in;
12360 int n_div;
12362 total = isl_basic_map_total_dim(bmap);
12363 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12364 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12365 n_div = isl_multi_aff_dim(ma, isl_dim_div);
12366 for (i = 0; i < ma->n; ++i) {
12367 int o_bmap = 0, o_ma = 1;
12369 if (isl_int_is_one(ma->p[i]->v->el[0]))
12370 continue;
12371 div = isl_basic_map_alloc_div(bmap);
12372 k = isl_basic_map_alloc_equality(bmap);
12373 if (div < 0 || k < 0)
12374 goto error;
12375 isl_int_set_si(bmap->div[div][0], 0);
12376 isl_seq_cpy(bmap->eq[k] + o_bmap,
12377 ma->p[i]->v->el + o_ma, 1 + n_param);
12378 o_bmap += 1 + n_param;
12379 o_ma += 1 + n_param;
12380 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12381 o_bmap += n_before;
12382 isl_seq_cpy(bmap->eq[k] + o_bmap,
12383 ma->p[i]->v->el + o_ma, n_in);
12384 o_bmap += n_in;
12385 o_ma += n_in;
12386 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12387 o_bmap += n_after;
12388 isl_seq_cpy(bmap->eq[k] + o_bmap,
12389 ma->p[i]->v->el + o_ma, n_div);
12390 o_bmap += n_div;
12391 o_ma += n_div;
12392 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12393 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12394 total++;
12397 return bmap;
12398 error:
12399 isl_basic_map_free(bmap);
12400 return NULL;
12403 /* Replace the domain or range space (depending on "type) of "space" by "set".
12405 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12406 enum isl_dim_type type, __isl_take isl_space *set)
12408 if (type == isl_dim_in) {
12409 space = isl_space_range(space);
12410 space = isl_space_map_from_domain_and_range(set, space);
12411 } else {
12412 space = isl_space_domain(space);
12413 space = isl_space_map_from_domain_and_range(space, set);
12416 return space;
12419 /* Compute the preimage of the domain or range (depending on "type")
12420 * of "bmap" under the function represented by "ma".
12421 * In other words, plug in "ma" in the domain or range of "bmap".
12422 * The result is a basic map that lives in the same space as "bmap"
12423 * except that the domain or range has been replaced by
12424 * the domain space of "ma".
12426 * If bmap is represented by
12428 * A(p) + S u + B x + T v + C(divs) >= 0,
12430 * where u and x are input and output dimensions if type == isl_dim_out
12431 * while x and v are input and output dimensions if type == isl_dim_in,
12432 * and ma is represented by
12434 * x = D(p) + F(y) + G(divs')
12436 * then the result is
12438 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12440 * The divs in the input set are similarly adjusted.
12441 * In particular
12443 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12445 * becomes
12447 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12448 * B_i G(divs') + c_i(divs))/n_i)
12450 * If bmap is not a rational map and if F(y) involves any denominators
12452 * x_i = (f_i y + h_i)/m_i
12454 * then additional constraints are added to ensure that we only
12455 * map back integer points. That is we enforce
12457 * f_i y + h_i = m_i alpha_i
12459 * with alpha_i an additional existentially quantified variable.
12461 * We first copy over the divs from "ma".
12462 * Then we add the modified constraints and divs from "bmap".
12463 * Finally, we add the stride constraints, if needed.
12465 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12466 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12467 __isl_take isl_multi_aff *ma)
12469 int i, k;
12470 isl_space *space;
12471 isl_basic_map *res = NULL;
12472 int n_before, n_after, n_div_bmap, n_div_ma;
12473 isl_int f, c1, c2, g;
12474 int rational, strides;
12476 isl_int_init(f);
12477 isl_int_init(c1);
12478 isl_int_init(c2);
12479 isl_int_init(g);
12481 ma = isl_multi_aff_align_divs(ma);
12482 if (!bmap || !ma)
12483 goto error;
12484 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12485 goto error;
12487 if (type == isl_dim_in) {
12488 n_before = 0;
12489 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12490 } else {
12491 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12492 n_after = 0;
12494 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12495 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12497 space = isl_multi_aff_get_domain_space(ma);
12498 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12499 rational = isl_basic_map_is_rational(bmap);
12500 strides = rational ? 0 : multi_aff_strides(ma);
12501 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12502 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12503 if (rational)
12504 res = isl_basic_map_set_rational(res);
12506 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12507 if (isl_basic_map_alloc_div(res) < 0)
12508 goto error;
12510 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12511 goto error;
12513 for (i = 0; i < bmap->n_eq; ++i) {
12514 k = isl_basic_map_alloc_equality(res);
12515 if (k < 0)
12516 goto error;
12517 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12518 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12521 for (i = 0; i < bmap->n_ineq; ++i) {
12522 k = isl_basic_map_alloc_inequality(res);
12523 if (k < 0)
12524 goto error;
12525 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12526 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12529 for (i = 0; i < bmap->n_div; ++i) {
12530 if (isl_int_is_zero(bmap->div[i][0])) {
12531 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12532 continue;
12534 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12535 n_before, n_after, n_div_ma, n_div_bmap,
12536 f, c1, c2, g, 1);
12539 if (strides)
12540 res = add_ma_strides(res, ma, n_before, n_after);
12542 isl_int_clear(f);
12543 isl_int_clear(c1);
12544 isl_int_clear(c2);
12545 isl_int_clear(g);
12546 isl_basic_map_free(bmap);
12547 isl_multi_aff_free(ma);
12548 res = isl_basic_set_simplify(res);
12549 return isl_basic_map_finalize(res);
12550 error:
12551 isl_int_clear(f);
12552 isl_int_clear(c1);
12553 isl_int_clear(c2);
12554 isl_int_clear(g);
12555 isl_basic_map_free(bmap);
12556 isl_multi_aff_free(ma);
12557 isl_basic_map_free(res);
12558 return NULL;
12561 /* Compute the preimage of "bset" under the function represented by "ma".
12562 * In other words, plug in "ma" in "bset". The result is a basic set
12563 * that lives in the domain space of "ma".
12565 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12566 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12568 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12571 /* Compute the preimage of the domain of "bmap" under the function
12572 * represented by "ma".
12573 * In other words, plug in "ma" in the domain of "bmap".
12574 * The result is a basic map that lives in the same space as "bmap"
12575 * except that the domain has been replaced by the domain space of "ma".
12577 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12578 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12580 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12583 /* Compute the preimage of the range of "bmap" under the function
12584 * represented by "ma".
12585 * In other words, plug in "ma" in the range of "bmap".
12586 * The result is a basic map that lives in the same space as "bmap"
12587 * except that the range has been replaced by the domain space of "ma".
12589 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12590 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12592 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12595 /* Check if the range of "ma" is compatible with the domain or range
12596 * (depending on "type") of "map".
12597 * Return -1 if anything is wrong.
12599 static int check_map_compatible_range_multi_aff(
12600 __isl_keep isl_map *map, enum isl_dim_type type,
12601 __isl_keep isl_multi_aff *ma)
12603 int m;
12604 isl_space *ma_space;
12606 ma_space = isl_multi_aff_get_space(ma);
12607 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12608 isl_space_free(ma_space);
12609 if (m >= 0 && !m)
12610 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12611 "spaces don't match", return -1);
12612 return m;
12615 /* Compute the preimage of the domain or range (depending on "type")
12616 * of "map" under the function represented by "ma".
12617 * In other words, plug in "ma" in the domain or range of "map".
12618 * The result is a map that lives in the same space as "map"
12619 * except that the domain or range has been replaced by
12620 * the domain space of "ma".
12622 * The parameters are assumed to have been aligned.
12624 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12625 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12627 int i;
12628 isl_space *space;
12630 map = isl_map_cow(map);
12631 ma = isl_multi_aff_align_divs(ma);
12632 if (!map || !ma)
12633 goto error;
12634 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12635 goto error;
12637 for (i = 0; i < map->n; ++i) {
12638 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12639 isl_multi_aff_copy(ma));
12640 if (!map->p[i])
12641 goto error;
12644 space = isl_multi_aff_get_domain_space(ma);
12645 space = isl_space_set(isl_map_get_space(map), type, space);
12647 isl_space_free(map->dim);
12648 map->dim = space;
12649 if (!map->dim)
12650 goto error;
12652 isl_multi_aff_free(ma);
12653 if (map->n > 1)
12654 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12655 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12656 return map;
12657 error:
12658 isl_multi_aff_free(ma);
12659 isl_map_free(map);
12660 return NULL;
12663 /* Compute the preimage of the domain or range (depending on "type")
12664 * of "map" under the function represented by "ma".
12665 * In other words, plug in "ma" in the domain or range of "map".
12666 * The result is a map that lives in the same space as "map"
12667 * except that the domain or range has been replaced by
12668 * the domain space of "ma".
12670 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12671 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12673 if (!map || !ma)
12674 goto error;
12676 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12677 return map_preimage_multi_aff(map, type, ma);
12679 if (!isl_space_has_named_params(map->dim) ||
12680 !isl_space_has_named_params(ma->space))
12681 isl_die(map->ctx, isl_error_invalid,
12682 "unaligned unnamed parameters", goto error);
12683 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12684 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12686 return map_preimage_multi_aff(map, type, ma);
12687 error:
12688 isl_multi_aff_free(ma);
12689 return isl_map_free(map);
12692 /* Compute the preimage of "set" under the function represented by "ma".
12693 * In other words, plug in "ma" in "set". The result is a set
12694 * that lives in the domain space of "ma".
12696 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12697 __isl_take isl_multi_aff *ma)
12699 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12702 /* Compute the preimage of the domain of "map" under the function
12703 * represented by "ma".
12704 * In other words, plug in "ma" in the domain of "map".
12705 * The result is a map that lives in the same space as "map"
12706 * except that the domain has been replaced by the domain space of "ma".
12708 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12709 __isl_take isl_multi_aff *ma)
12711 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12714 /* Compute the preimage of the range of "map" under the function
12715 * represented by "ma".
12716 * In other words, plug in "ma" in the range of "map".
12717 * The result is a map that lives in the same space as "map"
12718 * except that the range has been replaced by the domain space of "ma".
12720 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12721 __isl_take isl_multi_aff *ma)
12723 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12726 /* Compute the preimage of "map" under the function represented by "pma".
12727 * In other words, plug in "pma" in the domain or range of "map".
12728 * The result is a map that lives in the same space as "map",
12729 * except that the space of type "type" has been replaced by
12730 * the domain space of "pma".
12732 * The parameters of "map" and "pma" are assumed to have been aligned.
12734 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12735 __isl_take isl_map *map, enum isl_dim_type type,
12736 __isl_take isl_pw_multi_aff *pma)
12738 int i;
12739 isl_map *res;
12741 if (!pma)
12742 goto error;
12744 if (pma->n == 0) {
12745 isl_pw_multi_aff_free(pma);
12746 res = isl_map_empty(isl_map_get_space(map));
12747 isl_map_free(map);
12748 return res;
12751 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12752 isl_multi_aff_copy(pma->p[0].maff));
12753 if (type == isl_dim_in)
12754 res = isl_map_intersect_domain(res,
12755 isl_map_copy(pma->p[0].set));
12756 else
12757 res = isl_map_intersect_range(res,
12758 isl_map_copy(pma->p[0].set));
12760 for (i = 1; i < pma->n; ++i) {
12761 isl_map *res_i;
12763 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12764 isl_multi_aff_copy(pma->p[i].maff));
12765 if (type == isl_dim_in)
12766 res_i = isl_map_intersect_domain(res_i,
12767 isl_map_copy(pma->p[i].set));
12768 else
12769 res_i = isl_map_intersect_range(res_i,
12770 isl_map_copy(pma->p[i].set));
12771 res = isl_map_union(res, res_i);
12774 isl_pw_multi_aff_free(pma);
12775 isl_map_free(map);
12776 return res;
12777 error:
12778 isl_pw_multi_aff_free(pma);
12779 isl_map_free(map);
12780 return NULL;
12783 /* Compute the preimage of "map" under the function represented by "pma".
12784 * In other words, plug in "pma" in the domain or range of "map".
12785 * The result is a map that lives in the same space as "map",
12786 * except that the space of type "type" has been replaced by
12787 * the domain space of "pma".
12789 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12790 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12792 if (!map || !pma)
12793 goto error;
12795 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12796 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12798 if (!isl_space_has_named_params(map->dim) ||
12799 !isl_space_has_named_params(pma->dim))
12800 isl_die(map->ctx, isl_error_invalid,
12801 "unaligned unnamed parameters", goto error);
12802 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12803 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12805 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12806 error:
12807 isl_pw_multi_aff_free(pma);
12808 return isl_map_free(map);
12811 /* Compute the preimage of "set" under the function represented by "pma".
12812 * In other words, plug in "pma" in "set". The result is a set
12813 * that lives in the domain space of "pma".
12815 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12816 __isl_take isl_pw_multi_aff *pma)
12818 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12821 /* Compute the preimage of the domain of "map" under the function
12822 * represented by "pma".
12823 * In other words, plug in "pma" in the domain of "map".
12824 * The result is a map that lives in the same space as "map",
12825 * except that domain space has been replaced by the domain space of "pma".
12827 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12828 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12830 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12833 /* Compute the preimage of the range of "map" under the function
12834 * represented by "pma".
12835 * In other words, plug in "pma" in the range of "map".
12836 * The result is a map that lives in the same space as "map",
12837 * except that range space has been replaced by the domain space of "pma".
12839 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12840 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12842 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12845 /* Compute the preimage of "map" under the function represented by "mpa".
12846 * In other words, plug in "mpa" in the domain or range of "map".
12847 * The result is a map that lives in the same space as "map",
12848 * except that the space of type "type" has been replaced by
12849 * the domain space of "mpa".
12851 * If the map does not involve any constraints that refer to the
12852 * dimensions of the substituted space, then the only possible
12853 * effect of "mpa" on the map is to map the space to a different space.
12854 * We create a separate isl_multi_aff to effectuate this change
12855 * in order to avoid spurious splitting of the map along the pieces
12856 * of "mpa".
12858 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12859 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12861 int n;
12862 isl_pw_multi_aff *pma;
12864 if (!map || !mpa)
12865 goto error;
12867 n = isl_map_dim(map, type);
12868 if (!isl_map_involves_dims(map, type, 0, n)) {
12869 isl_space *space;
12870 isl_multi_aff *ma;
12872 space = isl_multi_pw_aff_get_space(mpa);
12873 isl_multi_pw_aff_free(mpa);
12874 ma = isl_multi_aff_zero(space);
12875 return isl_map_preimage_multi_aff(map, type, ma);
12878 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12879 return isl_map_preimage_pw_multi_aff(map, type, pma);
12880 error:
12881 isl_map_free(map);
12882 isl_multi_pw_aff_free(mpa);
12883 return NULL;
12886 /* Compute the preimage of "map" under the function represented by "mpa".
12887 * In other words, plug in "mpa" in the domain "map".
12888 * The result is a map that lives in the same space as "map",
12889 * except that domain space has been replaced by the domain space of "mpa".
12891 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12892 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12894 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12897 /* Compute the preimage of "set" by the function represented by "mpa".
12898 * In other words, plug in "mpa" in "set".
12900 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12901 __isl_take isl_multi_pw_aff *mpa)
12903 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);