add isl_union_set_sample_point
[isl.git] / isl_map.c
blobd6b4a2821d82c2bd14ba2b8042e7464cb86af59a
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 /* For each known div d = floor(f/m), add the constraints
327 * f - m d >= 0
328 * -(f-(n-1)) + m d >= 0
330 * Do not finalize the result.
332 static __isl_give isl_basic_map *add_known_div_constraints(
333 __isl_take isl_basic_map *bmap)
335 int i;
336 unsigned n_div;
338 if (!bmap)
339 return NULL;
340 n_div = isl_basic_map_dim(bmap, isl_dim_div);
341 if (n_div == 0)
342 return bmap;
343 bmap = isl_basic_map_cow(bmap);
344 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
345 if (!bmap)
346 return NULL;
347 for (i = 0; i < n_div; ++i) {
348 if (isl_int_is_zero(bmap->div[i][0]))
349 continue;
350 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
351 return isl_basic_map_free(bmap);
354 return bmap;
357 __isl_give isl_basic_map *isl_basic_map_from_local_space(
358 __isl_take isl_local_space *ls)
360 int i;
361 int n_div;
362 isl_basic_map *bmap;
364 if (!ls)
365 return NULL;
367 n_div = isl_local_space_dim(ls, isl_dim_div);
368 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
369 n_div, 0, 2 * n_div);
371 for (i = 0; i < n_div; ++i)
372 if (isl_basic_map_alloc_div(bmap) < 0)
373 goto error;
375 for (i = 0; i < n_div; ++i)
376 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
377 bmap = add_known_div_constraints(bmap);
379 isl_local_space_free(ls);
380 return bmap;
381 error:
382 isl_local_space_free(ls);
383 isl_basic_map_free(bmap);
384 return NULL;
387 __isl_give isl_basic_set *isl_basic_set_from_local_space(
388 __isl_take isl_local_space *ls)
390 return isl_basic_map_from_local_space(ls);
393 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
395 if (!map)
396 return NULL;
397 return isl_space_copy(map->dim);
400 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
402 if (!set)
403 return NULL;
404 return isl_space_copy(set->dim);
407 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
408 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
410 bmap = isl_basic_map_cow(bmap);
411 if (!bmap)
412 return NULL;
413 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
414 if (!bmap->dim)
415 goto error;
416 bmap = isl_basic_map_finalize(bmap);
417 return bmap;
418 error:
419 isl_basic_map_free(bmap);
420 return NULL;
423 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
424 __isl_take isl_basic_set *bset, const char *s)
426 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
429 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
430 enum isl_dim_type type)
432 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
435 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
436 enum isl_dim_type type, const char *s)
438 int i;
440 map = isl_map_cow(map);
441 if (!map)
442 return NULL;
444 map->dim = isl_space_set_tuple_name(map->dim, type, s);
445 if (!map->dim)
446 goto error;
448 for (i = 0; i < map->n; ++i) {
449 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
450 if (!map->p[i])
451 goto error;
454 return map;
455 error:
456 isl_map_free(map);
457 return NULL;
460 /* Replace the identifier of the tuple of type "type" by "id".
462 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
463 __isl_take isl_basic_map *bmap,
464 enum isl_dim_type type, __isl_take isl_id *id)
466 bmap = isl_basic_map_cow(bmap);
467 if (!bmap)
468 goto error;
469 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
470 if (!bmap->dim)
471 return isl_basic_map_free(bmap);
472 bmap = isl_basic_map_finalize(bmap);
473 return bmap;
474 error:
475 isl_id_free(id);
476 return NULL;
479 /* Replace the identifier of the tuple by "id".
481 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
482 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
484 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
487 /* Does the input or output tuple have a name?
489 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
491 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
494 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
495 enum isl_dim_type type)
497 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
500 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
501 const char *s)
503 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
506 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
507 enum isl_dim_type type, __isl_take isl_id *id)
509 map = isl_map_cow(map);
510 if (!map)
511 goto error;
513 map->dim = isl_space_set_tuple_id(map->dim, type, id);
515 return isl_map_reset_space(map, isl_space_copy(map->dim));
516 error:
517 isl_id_free(id);
518 return NULL;
521 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
522 __isl_take isl_id *id)
524 return isl_map_set_tuple_id(set, isl_dim_set, id);
527 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
528 enum isl_dim_type type)
530 map = isl_map_cow(map);
531 if (!map)
532 return NULL;
534 map->dim = isl_space_reset_tuple_id(map->dim, type);
536 return isl_map_reset_space(map, isl_space_copy(map->dim));
539 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
541 return isl_map_reset_tuple_id(set, isl_dim_set);
544 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
546 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
549 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
550 enum isl_dim_type type)
552 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
555 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
557 return isl_map_has_tuple_id(set, isl_dim_set);
560 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
562 return isl_map_get_tuple_id(set, isl_dim_set);
565 /* Does the set tuple have a name?
567 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
569 if (!set)
570 return isl_bool_error;
571 return isl_space_has_tuple_name(set->dim, isl_dim_set);
575 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
577 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
580 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
582 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
585 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
586 enum isl_dim_type type, unsigned pos)
588 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
591 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
592 enum isl_dim_type type, unsigned pos)
594 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
597 /* Does the given dimension have a name?
599 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
600 enum isl_dim_type type, unsigned pos)
602 if (!map)
603 return isl_bool_error;
604 return isl_space_has_dim_name(map->dim, type, pos);
607 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
608 enum isl_dim_type type, unsigned pos)
610 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
613 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
614 enum isl_dim_type type, unsigned pos)
616 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
619 /* Does the given dimension have a name?
621 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
622 enum isl_dim_type type, unsigned pos)
624 if (!set)
625 return isl_bool_error;
626 return isl_space_has_dim_name(set->dim, type, pos);
629 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
630 __isl_take isl_basic_map *bmap,
631 enum isl_dim_type type, unsigned pos, const char *s)
633 bmap = isl_basic_map_cow(bmap);
634 if (!bmap)
635 return NULL;
636 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
637 if (!bmap->dim)
638 goto error;
639 return isl_basic_map_finalize(bmap);
640 error:
641 isl_basic_map_free(bmap);
642 return NULL;
645 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
646 enum isl_dim_type type, unsigned pos, const char *s)
648 int i;
650 map = isl_map_cow(map);
651 if (!map)
652 return NULL;
654 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
655 if (!map->dim)
656 goto error;
658 for (i = 0; i < map->n; ++i) {
659 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
660 if (!map->p[i])
661 goto error;
664 return map;
665 error:
666 isl_map_free(map);
667 return NULL;
670 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
671 __isl_take isl_basic_set *bset,
672 enum isl_dim_type type, unsigned pos, const char *s)
674 return (isl_basic_set *)isl_basic_map_set_dim_name(
675 (isl_basic_map *)bset, type, pos, s);
678 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
679 enum isl_dim_type type, unsigned pos, const char *s)
681 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
684 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
685 enum isl_dim_type type, unsigned pos)
687 if (!bmap)
688 return isl_bool_error;
689 return isl_space_has_dim_id(bmap->dim, type, pos);
692 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
693 enum isl_dim_type type, unsigned pos)
695 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
698 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
699 enum isl_dim_type type, unsigned pos)
701 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
704 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
705 enum isl_dim_type type, unsigned pos)
707 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
710 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
711 enum isl_dim_type type, unsigned pos)
713 return isl_map_has_dim_id(set, type, pos);
716 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
717 enum isl_dim_type type, unsigned pos)
719 return isl_map_get_dim_id(set, type, pos);
722 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
723 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
725 map = isl_map_cow(map);
726 if (!map)
727 goto error;
729 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
731 return isl_map_reset_space(map, isl_space_copy(map->dim));
732 error:
733 isl_id_free(id);
734 return NULL;
737 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
738 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
740 return isl_map_set_dim_id(set, type, pos, id);
743 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
744 __isl_keep isl_id *id)
746 if (!map)
747 return -1;
748 return isl_space_find_dim_by_id(map->dim, type, id);
751 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
752 __isl_keep isl_id *id)
754 return isl_map_find_dim_by_id(set, type, id);
757 /* Return the position of the dimension of the given type and name
758 * in "bmap".
759 * Return -1 if no such dimension can be found.
761 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
762 enum isl_dim_type type, const char *name)
764 if (!bmap)
765 return -1;
766 return isl_space_find_dim_by_name(bmap->dim, type, name);
769 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
770 const char *name)
772 if (!map)
773 return -1;
774 return isl_space_find_dim_by_name(map->dim, type, name);
777 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
778 const char *name)
780 return isl_map_find_dim_by_name(set, type, name);
783 /* Reset the user pointer on all identifiers of parameters and tuples
784 * of the space of "map".
786 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
788 isl_space *space;
790 space = isl_map_get_space(map);
791 space = isl_space_reset_user(space);
792 map = isl_map_reset_space(map, space);
794 return map;
797 /* Reset the user pointer on all identifiers of parameters and tuples
798 * of the space of "set".
800 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
802 return isl_map_reset_user(set);
805 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
807 if (!bmap)
808 return -1;
809 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
812 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
814 return isl_basic_map_is_rational(bset);
817 /* Does "bmap" contain any rational points?
819 * If "bmap" has an equality for each dimension, equating the dimension
820 * to an integer constant, then it has no rational points, even if it
821 * is marked as rational.
823 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
825 int has_rational = 1;
826 unsigned total;
828 if (!bmap)
829 return -1;
830 if (isl_basic_map_plain_is_empty(bmap))
831 return 0;
832 if (!isl_basic_map_is_rational(bmap))
833 return 0;
834 bmap = isl_basic_map_copy(bmap);
835 bmap = isl_basic_map_implicit_equalities(bmap);
836 if (!bmap)
837 return -1;
838 total = isl_basic_map_total_dim(bmap);
839 if (bmap->n_eq == total) {
840 int i, j;
841 for (i = 0; i < bmap->n_eq; ++i) {
842 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
843 if (j < 0)
844 break;
845 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
846 !isl_int_is_negone(bmap->eq[i][1 + j]))
847 break;
848 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
849 total - j - 1);
850 if (j >= 0)
851 break;
853 if (i == bmap->n_eq)
854 has_rational = 0;
856 isl_basic_map_free(bmap);
858 return has_rational;
861 /* Does "map" contain any rational points?
863 int isl_map_has_rational(__isl_keep isl_map *map)
865 int i;
866 int has_rational;
868 if (!map)
869 return -1;
870 for (i = 0; i < map->n; ++i) {
871 has_rational = isl_basic_map_has_rational(map->p[i]);
872 if (has_rational < 0)
873 return -1;
874 if (has_rational)
875 return 1;
877 return 0;
880 /* Does "set" contain any rational points?
882 int isl_set_has_rational(__isl_keep isl_set *set)
884 return isl_map_has_rational(set);
887 /* Is this basic set a parameter domain?
889 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
891 if (!bset)
892 return -1;
893 return isl_space_is_params(bset->dim);
896 /* Is this set a parameter domain?
898 isl_bool isl_set_is_params(__isl_keep isl_set *set)
900 if (!set)
901 return isl_bool_error;
902 return isl_space_is_params(set->dim);
905 /* Is this map actually a parameter domain?
906 * Users should never call this function. Outside of isl,
907 * a map can never be a parameter domain.
909 int isl_map_is_params(__isl_keep isl_map *map)
911 if (!map)
912 return -1;
913 return isl_space_is_params(map->dim);
916 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
917 struct isl_basic_map *bmap, unsigned extra,
918 unsigned n_eq, unsigned n_ineq)
920 int i;
921 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
923 bmap->ctx = ctx;
924 isl_ctx_ref(ctx);
926 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
927 if (isl_blk_is_error(bmap->block))
928 goto error;
930 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
931 if ((n_ineq + n_eq) && !bmap->ineq)
932 goto error;
934 if (extra == 0) {
935 bmap->block2 = isl_blk_empty();
936 bmap->div = NULL;
937 } else {
938 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
939 if (isl_blk_is_error(bmap->block2))
940 goto error;
942 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
943 if (!bmap->div)
944 goto error;
947 for (i = 0; i < n_ineq + n_eq; ++i)
948 bmap->ineq[i] = bmap->block.data + i * row_size;
950 for (i = 0; i < extra; ++i)
951 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
953 bmap->ref = 1;
954 bmap->flags = 0;
955 bmap->c_size = n_eq + n_ineq;
956 bmap->eq = bmap->ineq + n_ineq;
957 bmap->extra = extra;
958 bmap->n_eq = 0;
959 bmap->n_ineq = 0;
960 bmap->n_div = 0;
961 bmap->sample = NULL;
963 return bmap;
964 error:
965 isl_basic_map_free(bmap);
966 return NULL;
969 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
970 unsigned nparam, unsigned dim, unsigned extra,
971 unsigned n_eq, unsigned n_ineq)
973 struct isl_basic_map *bmap;
974 isl_space *space;
976 space = isl_space_set_alloc(ctx, nparam, dim);
977 if (!space)
978 return NULL;
980 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
981 return (struct isl_basic_set *)bmap;
984 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
985 unsigned extra, unsigned n_eq, unsigned n_ineq)
987 struct isl_basic_map *bmap;
988 if (!dim)
989 return NULL;
990 isl_assert(dim->ctx, dim->n_in == 0, goto error);
991 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
992 return (struct isl_basic_set *)bmap;
993 error:
994 isl_space_free(dim);
995 return NULL;
998 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
999 unsigned extra, unsigned n_eq, unsigned n_ineq)
1001 struct isl_basic_map *bmap;
1003 if (!dim)
1004 return NULL;
1005 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1006 if (!bmap)
1007 goto error;
1008 bmap->dim = dim;
1010 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1011 error:
1012 isl_space_free(dim);
1013 return NULL;
1016 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1017 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1018 unsigned n_eq, unsigned n_ineq)
1020 struct isl_basic_map *bmap;
1021 isl_space *dim;
1023 dim = isl_space_alloc(ctx, nparam, in, out);
1024 if (!dim)
1025 return NULL;
1027 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1028 return bmap;
1031 static void dup_constraints(
1032 struct isl_basic_map *dst, struct isl_basic_map *src)
1034 int i;
1035 unsigned total = isl_basic_map_total_dim(src);
1037 for (i = 0; i < src->n_eq; ++i) {
1038 int j = isl_basic_map_alloc_equality(dst);
1039 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1042 for (i = 0; i < src->n_ineq; ++i) {
1043 int j = isl_basic_map_alloc_inequality(dst);
1044 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1047 for (i = 0; i < src->n_div; ++i) {
1048 int j = isl_basic_map_alloc_div(dst);
1049 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1051 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1054 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1056 struct isl_basic_map *dup;
1058 if (!bmap)
1059 return NULL;
1060 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1061 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1062 if (!dup)
1063 return NULL;
1064 dup_constraints(dup, bmap);
1065 dup->flags = bmap->flags;
1066 dup->sample = isl_vec_copy(bmap->sample);
1067 return dup;
1070 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1072 struct isl_basic_map *dup;
1074 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1075 return (struct isl_basic_set *)dup;
1078 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1080 if (!bset)
1081 return NULL;
1083 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1084 bset->ref++;
1085 return bset;
1087 return isl_basic_set_dup(bset);
1090 struct isl_set *isl_set_copy(struct isl_set *set)
1092 if (!set)
1093 return NULL;
1095 set->ref++;
1096 return set;
1099 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1101 if (!bmap)
1102 return NULL;
1104 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1105 bmap->ref++;
1106 return bmap;
1108 bmap = isl_basic_map_dup(bmap);
1109 if (bmap)
1110 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1111 return bmap;
1114 struct isl_map *isl_map_copy(struct isl_map *map)
1116 if (!map)
1117 return NULL;
1119 map->ref++;
1120 return map;
1123 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1125 if (!bmap)
1126 return NULL;
1128 if (--bmap->ref > 0)
1129 return NULL;
1131 isl_ctx_deref(bmap->ctx);
1132 free(bmap->div);
1133 isl_blk_free(bmap->ctx, bmap->block2);
1134 free(bmap->ineq);
1135 isl_blk_free(bmap->ctx, bmap->block);
1136 isl_vec_free(bmap->sample);
1137 isl_space_free(bmap->dim);
1138 free(bmap);
1140 return NULL;
1143 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1145 return isl_basic_map_free((struct isl_basic_map *)bset);
1148 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1150 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1153 __isl_give isl_map *isl_map_align_params_map_map_and(
1154 __isl_take isl_map *map1, __isl_take isl_map *map2,
1155 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1156 __isl_take isl_map *map2))
1158 if (!map1 || !map2)
1159 goto error;
1160 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1161 return fn(map1, map2);
1162 if (!isl_space_has_named_params(map1->dim) ||
1163 !isl_space_has_named_params(map2->dim))
1164 isl_die(map1->ctx, isl_error_invalid,
1165 "unaligned unnamed parameters", goto error);
1166 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1167 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1168 return fn(map1, map2);
1169 error:
1170 isl_map_free(map1);
1171 isl_map_free(map2);
1172 return NULL;
1175 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1176 __isl_keep isl_map *map2,
1177 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1179 isl_bool r;
1181 if (!map1 || !map2)
1182 return isl_bool_error;
1183 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1184 return fn(map1, map2);
1185 if (!isl_space_has_named_params(map1->dim) ||
1186 !isl_space_has_named_params(map2->dim))
1187 isl_die(map1->ctx, isl_error_invalid,
1188 "unaligned unnamed parameters", return isl_bool_error);
1189 map1 = isl_map_copy(map1);
1190 map2 = isl_map_copy(map2);
1191 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1192 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1193 r = fn(map1, map2);
1194 isl_map_free(map1);
1195 isl_map_free(map2);
1196 return r;
1199 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1201 struct isl_ctx *ctx;
1202 if (!bmap)
1203 return -1;
1204 ctx = bmap->ctx;
1205 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1206 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1207 return -1);
1208 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1209 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1210 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1211 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1212 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1213 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1214 isl_int *t;
1215 int j = isl_basic_map_alloc_inequality(bmap);
1216 if (j < 0)
1217 return -1;
1218 t = bmap->ineq[j];
1219 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1220 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1221 bmap->eq[-1] = t;
1222 bmap->n_eq++;
1223 bmap->n_ineq--;
1224 bmap->eq--;
1225 return 0;
1227 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1228 bmap->extra - bmap->n_div);
1229 return bmap->n_eq++;
1232 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1234 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1237 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1239 if (!bmap)
1240 return -1;
1241 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1242 bmap->n_eq -= n;
1243 return 0;
1246 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1248 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1251 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1253 isl_int *t;
1254 if (!bmap)
1255 return -1;
1256 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1258 if (pos != bmap->n_eq - 1) {
1259 t = bmap->eq[pos];
1260 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1261 bmap->eq[bmap->n_eq - 1] = t;
1263 bmap->n_eq--;
1264 return 0;
1267 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1269 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1272 /* Turn inequality "pos" of "bmap" into an equality.
1274 * In particular, we move the inequality in front of the equalities
1275 * and move the last inequality in the position of the moved inequality.
1276 * Note that isl_tab_make_equalities_explicit depends on this particular
1277 * change in the ordering of the constraints.
1279 void isl_basic_map_inequality_to_equality(
1280 struct isl_basic_map *bmap, unsigned pos)
1282 isl_int *t;
1284 t = bmap->ineq[pos];
1285 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1286 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1287 bmap->eq[-1] = t;
1288 bmap->n_eq++;
1289 bmap->n_ineq--;
1290 bmap->eq--;
1291 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1292 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1293 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1294 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1297 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1299 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1302 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1304 struct isl_ctx *ctx;
1305 if (!bmap)
1306 return -1;
1307 ctx = bmap->ctx;
1308 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1309 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1310 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1311 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1312 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1313 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1314 1 + isl_basic_map_total_dim(bmap),
1315 bmap->extra - bmap->n_div);
1316 return bmap->n_ineq++;
1319 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1321 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1324 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1326 if (!bmap)
1327 return -1;
1328 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1329 bmap->n_ineq -= n;
1330 return 0;
1333 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1335 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1338 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1340 isl_int *t;
1341 if (!bmap)
1342 return -1;
1343 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1345 if (pos != bmap->n_ineq - 1) {
1346 t = bmap->ineq[pos];
1347 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1348 bmap->ineq[bmap->n_ineq - 1] = t;
1349 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1351 bmap->n_ineq--;
1352 return 0;
1355 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1357 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1360 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1361 isl_int *eq)
1363 int k;
1365 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1366 if (!bmap)
1367 return NULL;
1368 k = isl_basic_map_alloc_equality(bmap);
1369 if (k < 0)
1370 goto error;
1371 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1372 return bmap;
1373 error:
1374 isl_basic_map_free(bmap);
1375 return NULL;
1378 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1379 isl_int *eq)
1381 return (isl_basic_set *)
1382 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1385 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1386 isl_int *ineq)
1388 int k;
1390 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1391 if (!bmap)
1392 return NULL;
1393 k = isl_basic_map_alloc_inequality(bmap);
1394 if (k < 0)
1395 goto error;
1396 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1397 return bmap;
1398 error:
1399 isl_basic_map_free(bmap);
1400 return NULL;
1403 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1404 isl_int *ineq)
1406 return (isl_basic_set *)
1407 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1410 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1412 if (!bmap)
1413 return -1;
1414 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1415 isl_seq_clr(bmap->div[bmap->n_div] +
1416 1 + 1 + isl_basic_map_total_dim(bmap),
1417 bmap->extra - bmap->n_div);
1418 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1419 return bmap->n_div++;
1422 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1424 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1427 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1429 if (!bmap)
1430 return -1;
1431 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1432 bmap->n_div -= n;
1433 return 0;
1436 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1438 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1441 /* Copy constraint from src to dst, putting the vars of src at offset
1442 * dim_off in dst and the divs of src at offset div_off in dst.
1443 * If both sets are actually map, then dim_off applies to the input
1444 * variables.
1446 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1447 struct isl_basic_map *src_map, isl_int *src,
1448 unsigned in_off, unsigned out_off, unsigned div_off)
1450 unsigned src_nparam = isl_basic_map_n_param(src_map);
1451 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1452 unsigned src_in = isl_basic_map_n_in(src_map);
1453 unsigned dst_in = isl_basic_map_n_in(dst_map);
1454 unsigned src_out = isl_basic_map_n_out(src_map);
1455 unsigned dst_out = isl_basic_map_n_out(dst_map);
1456 isl_int_set(dst[0], src[0]);
1457 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1458 if (dst_nparam > src_nparam)
1459 isl_seq_clr(dst+1+src_nparam,
1460 dst_nparam - src_nparam);
1461 isl_seq_clr(dst+1+dst_nparam, in_off);
1462 isl_seq_cpy(dst+1+dst_nparam+in_off,
1463 src+1+src_nparam,
1464 isl_min(dst_in-in_off, src_in));
1465 if (dst_in-in_off > src_in)
1466 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1467 dst_in - in_off - src_in);
1468 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1469 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1470 src+1+src_nparam+src_in,
1471 isl_min(dst_out-out_off, src_out));
1472 if (dst_out-out_off > src_out)
1473 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1474 dst_out - out_off - src_out);
1475 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1476 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1477 src+1+src_nparam+src_in+src_out,
1478 isl_min(dst_map->extra-div_off, src_map->n_div));
1479 if (dst_map->n_div-div_off > src_map->n_div)
1480 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1481 div_off+src_map->n_div,
1482 dst_map->n_div - div_off - src_map->n_div);
1485 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1486 struct isl_basic_map *src_map, isl_int *src,
1487 unsigned in_off, unsigned out_off, unsigned div_off)
1489 isl_int_set(dst[0], src[0]);
1490 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1493 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1494 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1496 int i;
1497 unsigned div_off;
1499 if (!bmap1 || !bmap2)
1500 goto error;
1502 div_off = bmap1->n_div;
1504 for (i = 0; i < bmap2->n_eq; ++i) {
1505 int i1 = isl_basic_map_alloc_equality(bmap1);
1506 if (i1 < 0)
1507 goto error;
1508 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1509 i_pos, o_pos, div_off);
1512 for (i = 0; i < bmap2->n_ineq; ++i) {
1513 int i1 = isl_basic_map_alloc_inequality(bmap1);
1514 if (i1 < 0)
1515 goto error;
1516 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1517 i_pos, o_pos, div_off);
1520 for (i = 0; i < bmap2->n_div; ++i) {
1521 int i1 = isl_basic_map_alloc_div(bmap1);
1522 if (i1 < 0)
1523 goto error;
1524 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1525 i_pos, o_pos, div_off);
1528 isl_basic_map_free(bmap2);
1530 return bmap1;
1532 error:
1533 isl_basic_map_free(bmap1);
1534 isl_basic_map_free(bmap2);
1535 return NULL;
1538 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1539 struct isl_basic_set *bset2, unsigned pos)
1541 return (struct isl_basic_set *)
1542 add_constraints((struct isl_basic_map *)bset1,
1543 (struct isl_basic_map *)bset2, 0, pos);
1546 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1547 __isl_take isl_space *dim, unsigned extra,
1548 unsigned n_eq, unsigned n_ineq)
1550 struct isl_basic_map *ext;
1551 unsigned flags;
1552 int dims_ok;
1554 if (!dim)
1555 goto error;
1557 if (!base)
1558 goto error;
1560 dims_ok = isl_space_is_equal(base->dim, dim) &&
1561 base->extra >= base->n_div + extra;
1563 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1564 room_for_ineq(base, n_ineq)) {
1565 isl_space_free(dim);
1566 return base;
1569 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1570 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1571 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1572 extra += base->extra;
1573 n_eq += base->n_eq;
1574 n_ineq += base->n_ineq;
1576 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1577 dim = NULL;
1578 if (!ext)
1579 goto error;
1581 if (dims_ok)
1582 ext->sample = isl_vec_copy(base->sample);
1583 flags = base->flags;
1584 ext = add_constraints(ext, base, 0, 0);
1585 if (ext) {
1586 ext->flags = flags;
1587 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1590 return ext;
1592 error:
1593 isl_space_free(dim);
1594 isl_basic_map_free(base);
1595 return NULL;
1598 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1599 __isl_take isl_space *dim, unsigned extra,
1600 unsigned n_eq, unsigned n_ineq)
1602 return (struct isl_basic_set *)
1603 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1604 extra, n_eq, n_ineq);
1607 struct isl_basic_map *isl_basic_map_extend_constraints(
1608 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1610 if (!base)
1611 return NULL;
1612 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1613 0, n_eq, n_ineq);
1616 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1617 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1618 unsigned n_eq, unsigned n_ineq)
1620 struct isl_basic_map *bmap;
1621 isl_space *dim;
1623 if (!base)
1624 return NULL;
1625 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1626 if (!dim)
1627 goto error;
1629 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1630 return bmap;
1631 error:
1632 isl_basic_map_free(base);
1633 return NULL;
1636 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1637 unsigned nparam, unsigned dim, unsigned extra,
1638 unsigned n_eq, unsigned n_ineq)
1640 return (struct isl_basic_set *)
1641 isl_basic_map_extend((struct isl_basic_map *)base,
1642 nparam, 0, dim, extra, n_eq, n_ineq);
1645 struct isl_basic_set *isl_basic_set_extend_constraints(
1646 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1648 return (struct isl_basic_set *)
1649 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1650 n_eq, n_ineq);
1653 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1655 return (struct isl_basic_set *)
1656 isl_basic_map_cow((struct isl_basic_map *)bset);
1659 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1661 if (!bmap)
1662 return NULL;
1664 if (bmap->ref > 1) {
1665 bmap->ref--;
1666 bmap = isl_basic_map_dup(bmap);
1668 if (bmap) {
1669 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1670 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1672 return bmap;
1675 struct isl_set *isl_set_cow(struct isl_set *set)
1677 if (!set)
1678 return NULL;
1680 if (set->ref == 1)
1681 return set;
1682 set->ref--;
1683 return isl_set_dup(set);
1686 struct isl_map *isl_map_cow(struct isl_map *map)
1688 if (!map)
1689 return NULL;
1691 if (map->ref == 1)
1692 return map;
1693 map->ref--;
1694 return isl_map_dup(map);
1697 static void swap_vars(struct isl_blk blk, isl_int *a,
1698 unsigned a_len, unsigned b_len)
1700 isl_seq_cpy(blk.data, a+a_len, b_len);
1701 isl_seq_cpy(blk.data+b_len, a, a_len);
1702 isl_seq_cpy(a, blk.data, b_len+a_len);
1705 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1706 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1708 int i;
1709 struct isl_blk blk;
1711 if (!bmap)
1712 goto error;
1714 isl_assert(bmap->ctx,
1715 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1717 if (n1 == 0 || n2 == 0)
1718 return bmap;
1720 bmap = isl_basic_map_cow(bmap);
1721 if (!bmap)
1722 return NULL;
1724 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1725 if (isl_blk_is_error(blk))
1726 goto error;
1728 for (i = 0; i < bmap->n_eq; ++i)
1729 swap_vars(blk,
1730 bmap->eq[i] + pos, n1, n2);
1732 for (i = 0; i < bmap->n_ineq; ++i)
1733 swap_vars(blk,
1734 bmap->ineq[i] + pos, n1, n2);
1736 for (i = 0; i < bmap->n_div; ++i)
1737 swap_vars(blk,
1738 bmap->div[i]+1 + pos, n1, n2);
1740 isl_blk_free(bmap->ctx, blk);
1742 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1743 bmap = isl_basic_map_gauss(bmap, NULL);
1744 return isl_basic_map_finalize(bmap);
1745 error:
1746 isl_basic_map_free(bmap);
1747 return NULL;
1750 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1752 int i = 0;
1753 unsigned total;
1754 if (!bmap)
1755 goto error;
1756 total = isl_basic_map_total_dim(bmap);
1757 isl_basic_map_free_div(bmap, bmap->n_div);
1758 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1759 if (bmap->n_eq > 0)
1760 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1761 else {
1762 i = isl_basic_map_alloc_equality(bmap);
1763 if (i < 0)
1764 goto error;
1766 isl_int_set_si(bmap->eq[i][0], 1);
1767 isl_seq_clr(bmap->eq[i]+1, total);
1768 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1769 isl_vec_free(bmap->sample);
1770 bmap->sample = NULL;
1771 return isl_basic_map_finalize(bmap);
1772 error:
1773 isl_basic_map_free(bmap);
1774 return NULL;
1777 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1779 return (struct isl_basic_set *)
1780 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1783 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1784 * of "bmap").
1786 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1788 isl_int *t = bmap->div[a];
1789 bmap->div[a] = bmap->div[b];
1790 bmap->div[b] = t;
1793 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1794 * div definitions accordingly.
1796 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1798 int i;
1799 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1801 swap_div(bmap, a, b);
1803 for (i = 0; i < bmap->n_eq; ++i)
1804 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1806 for (i = 0; i < bmap->n_ineq; ++i)
1807 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1809 for (i = 0; i < bmap->n_div; ++i)
1810 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1811 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1814 /* Eliminate the specified n dimensions starting at first from the
1815 * constraints, without removing the dimensions from the space.
1816 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1818 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1819 enum isl_dim_type type, unsigned first, unsigned n)
1821 int i;
1823 if (!map)
1824 return NULL;
1825 if (n == 0)
1826 return map;
1828 if (first + n > isl_map_dim(map, type) || first + n < first)
1829 isl_die(map->ctx, isl_error_invalid,
1830 "index out of bounds", goto error);
1832 map = isl_map_cow(map);
1833 if (!map)
1834 return NULL;
1836 for (i = 0; i < map->n; ++i) {
1837 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1838 if (!map->p[i])
1839 goto error;
1841 return map;
1842 error:
1843 isl_map_free(map);
1844 return NULL;
1847 /* Eliminate the specified n dimensions starting at first from the
1848 * constraints, without removing the dimensions from the space.
1849 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1851 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1852 enum isl_dim_type type, unsigned first, unsigned n)
1854 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1857 /* Eliminate the specified n dimensions starting at first from the
1858 * constraints, without removing the dimensions from the space.
1859 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1861 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1862 unsigned first, unsigned n)
1864 return isl_set_eliminate(set, isl_dim_set, first, n);
1867 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1868 __isl_take isl_basic_map *bmap)
1870 if (!bmap)
1871 return NULL;
1872 bmap = isl_basic_map_eliminate_vars(bmap,
1873 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1874 if (!bmap)
1875 return NULL;
1876 bmap->n_div = 0;
1877 return isl_basic_map_finalize(bmap);
1880 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1881 __isl_take isl_basic_set *bset)
1883 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1884 (struct isl_basic_map *)bset);
1887 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1889 int i;
1891 if (!map)
1892 return NULL;
1893 if (map->n == 0)
1894 return map;
1896 map = isl_map_cow(map);
1897 if (!map)
1898 return NULL;
1900 for (i = 0; i < map->n; ++i) {
1901 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1902 if (!map->p[i])
1903 goto error;
1905 return map;
1906 error:
1907 isl_map_free(map);
1908 return NULL;
1911 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1913 return isl_map_remove_divs(set);
1916 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1917 enum isl_dim_type type, unsigned first, unsigned n)
1919 if (!bmap)
1920 return NULL;
1921 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1922 goto error);
1923 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1924 return bmap;
1925 bmap = isl_basic_map_eliminate_vars(bmap,
1926 isl_basic_map_offset(bmap, type) - 1 + first, n);
1927 if (!bmap)
1928 return bmap;
1929 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1930 return bmap;
1931 bmap = isl_basic_map_drop(bmap, type, first, n);
1932 return bmap;
1933 error:
1934 isl_basic_map_free(bmap);
1935 return NULL;
1938 /* Return true if the definition of the given div (recursively) involves
1939 * any of the given variables.
1941 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1942 unsigned first, unsigned n)
1944 int i;
1945 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1947 if (isl_int_is_zero(bmap->div[div][0]))
1948 return 0;
1949 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1950 return 1;
1952 for (i = bmap->n_div - 1; i >= 0; --i) {
1953 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1954 continue;
1955 if (div_involves_vars(bmap, i, first, n))
1956 return 1;
1959 return 0;
1962 /* Try and add a lower and/or upper bound on "div" to "bmap"
1963 * based on inequality "i".
1964 * "total" is the total number of variables (excluding the divs).
1965 * "v" is a temporary object that can be used during the calculations.
1966 * If "lb" is set, then a lower bound should be constructed.
1967 * If "ub" is set, then an upper bound should be constructed.
1969 * The calling function has already checked that the inequality does not
1970 * reference "div", but we still need to check that the inequality is
1971 * of the right form. We'll consider the case where we want to construct
1972 * a lower bound. The construction of upper bounds is similar.
1974 * Let "div" be of the form
1976 * q = floor((a + f(x))/d)
1978 * We essentially check if constraint "i" is of the form
1980 * b + f(x) >= 0
1982 * so that we can use it to derive a lower bound on "div".
1983 * However, we allow a slightly more general form
1985 * b + g(x) >= 0
1987 * with the condition that the coefficients of g(x) - f(x) are all
1988 * divisible by d.
1989 * Rewriting this constraint as
1991 * 0 >= -b - g(x)
1993 * adding a + f(x) to both sides and dividing by d, we obtain
1995 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1997 * Taking the floor on both sides, we obtain
1999 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2001 * or
2003 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2005 * In the case of an upper bound, we construct the constraint
2007 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2010 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2011 __isl_take isl_basic_map *bmap, int div, int i,
2012 unsigned total, isl_int v, int lb, int ub)
2014 int j;
2016 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2017 if (lb) {
2018 isl_int_sub(v, bmap->ineq[i][1 + j],
2019 bmap->div[div][1 + 1 + j]);
2020 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2022 if (ub) {
2023 isl_int_add(v, bmap->ineq[i][1 + j],
2024 bmap->div[div][1 + 1 + j]);
2025 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2028 if (!lb && !ub)
2029 return bmap;
2031 bmap = isl_basic_map_cow(bmap);
2032 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2033 if (lb) {
2034 int k = isl_basic_map_alloc_inequality(bmap);
2035 if (k < 0)
2036 goto error;
2037 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2038 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2039 bmap->div[div][1 + j]);
2040 isl_int_cdiv_q(bmap->ineq[k][j],
2041 bmap->ineq[k][j], bmap->div[div][0]);
2043 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2045 if (ub) {
2046 int k = isl_basic_map_alloc_inequality(bmap);
2047 if (k < 0)
2048 goto error;
2049 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2050 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2051 bmap->div[div][1 + j]);
2052 isl_int_fdiv_q(bmap->ineq[k][j],
2053 bmap->ineq[k][j], bmap->div[div][0]);
2055 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2058 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2059 return bmap;
2060 error:
2061 isl_basic_map_free(bmap);
2062 return NULL;
2065 /* This function is called right before "div" is eliminated from "bmap"
2066 * using Fourier-Motzkin.
2067 * Look through the constraints of "bmap" for constraints on the argument
2068 * of the integer division and use them to construct constraints on the
2069 * integer division itself. These constraints can then be combined
2070 * during the Fourier-Motzkin elimination.
2071 * Note that it is only useful to introduce lower bounds on "div"
2072 * if "bmap" already contains upper bounds on "div" as the newly
2073 * introduce lower bounds can then be combined with the pre-existing
2074 * upper bounds. Similarly for upper bounds.
2075 * We therefore first check if "bmap" contains any lower and/or upper bounds
2076 * on "div".
2078 * It is interesting to note that the introduction of these constraints
2079 * can indeed lead to more accurate results, even when compared to
2080 * deriving constraints on the argument of "div" from constraints on "div".
2081 * Consider, for example, the set
2083 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2085 * The second constraint can be rewritten as
2087 * 2 * [(-i-2j+3)/4] + k >= 0
2089 * from which we can derive
2091 * -i - 2j + 3 >= -2k
2093 * or
2095 * i + 2j <= 3 + 2k
2097 * Combined with the first constraint, we obtain
2099 * -3 <= 3 + 2k or k >= -3
2101 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2102 * the first constraint, we obtain
2104 * [(i + 2j)/4] >= [-3/4] = -1
2106 * Combining this constraint with the second constraint, we obtain
2108 * k >= -2
2110 static __isl_give isl_basic_map *insert_bounds_on_div(
2111 __isl_take isl_basic_map *bmap, int div)
2113 int i;
2114 int check_lb, check_ub;
2115 isl_int v;
2116 unsigned total;
2118 if (!bmap)
2119 return NULL;
2121 if (isl_int_is_zero(bmap->div[div][0]))
2122 return bmap;
2124 total = isl_space_dim(bmap->dim, isl_dim_all);
2126 check_lb = 0;
2127 check_ub = 0;
2128 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2129 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2130 if (s > 0)
2131 check_ub = 1;
2132 if (s < 0)
2133 check_lb = 1;
2136 if (!check_lb && !check_ub)
2137 return bmap;
2139 isl_int_init(v);
2141 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2142 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2143 continue;
2145 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2146 check_lb, check_ub);
2149 isl_int_clear(v);
2151 return bmap;
2154 /* Remove all divs (recursively) involving any of the given dimensions
2155 * in their definitions.
2157 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2158 __isl_take isl_basic_map *bmap,
2159 enum isl_dim_type type, unsigned first, unsigned n)
2161 int i;
2163 if (!bmap)
2164 return NULL;
2165 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2166 goto error);
2167 first += isl_basic_map_offset(bmap, type);
2169 for (i = bmap->n_div - 1; i >= 0; --i) {
2170 if (!div_involves_vars(bmap, i, first, n))
2171 continue;
2172 bmap = insert_bounds_on_div(bmap, i);
2173 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2174 if (!bmap)
2175 return NULL;
2176 i = bmap->n_div;
2179 return bmap;
2180 error:
2181 isl_basic_map_free(bmap);
2182 return NULL;
2185 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2186 __isl_take isl_basic_set *bset,
2187 enum isl_dim_type type, unsigned first, unsigned n)
2189 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2192 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2193 enum isl_dim_type type, unsigned first, unsigned n)
2195 int i;
2197 if (!map)
2198 return NULL;
2199 if (map->n == 0)
2200 return map;
2202 map = isl_map_cow(map);
2203 if (!map)
2204 return NULL;
2206 for (i = 0; i < map->n; ++i) {
2207 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2208 type, first, n);
2209 if (!map->p[i])
2210 goto error;
2212 return map;
2213 error:
2214 isl_map_free(map);
2215 return NULL;
2218 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2219 enum isl_dim_type type, unsigned first, unsigned n)
2221 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2222 type, first, n);
2225 /* Does the desciption of "bmap" depend on the specified dimensions?
2226 * We also check whether the dimensions appear in any of the div definitions.
2227 * In principle there is no need for this check. If the dimensions appear
2228 * in a div definition, they also appear in the defining constraints of that
2229 * div.
2231 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2232 enum isl_dim_type type, unsigned first, unsigned n)
2234 int i;
2236 if (!bmap)
2237 return isl_bool_error;
2239 if (first + n > isl_basic_map_dim(bmap, type))
2240 isl_die(bmap->ctx, isl_error_invalid,
2241 "index out of bounds", return isl_bool_error);
2243 first += isl_basic_map_offset(bmap, type);
2244 for (i = 0; i < bmap->n_eq; ++i)
2245 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2246 return isl_bool_true;
2247 for (i = 0; i < bmap->n_ineq; ++i)
2248 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2249 return isl_bool_true;
2250 for (i = 0; i < bmap->n_div; ++i) {
2251 if (isl_int_is_zero(bmap->div[i][0]))
2252 continue;
2253 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2254 return isl_bool_true;
2257 return isl_bool_false;
2260 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2261 enum isl_dim_type type, unsigned first, unsigned n)
2263 int i;
2265 if (!map)
2266 return isl_bool_error;
2268 if (first + n > isl_map_dim(map, type))
2269 isl_die(map->ctx, isl_error_invalid,
2270 "index out of bounds", return isl_bool_error);
2272 for (i = 0; i < map->n; ++i) {
2273 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2274 type, first, n);
2275 if (involves < 0 || involves)
2276 return involves;
2279 return isl_bool_false;
2282 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2283 enum isl_dim_type type, unsigned first, unsigned n)
2285 return isl_basic_map_involves_dims(bset, type, first, n);
2288 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2289 enum isl_dim_type type, unsigned first, unsigned n)
2291 return isl_map_involves_dims(set, type, first, n);
2294 /* Return true if the definition of the given div is unknown or depends
2295 * on unknown divs.
2297 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2299 int i;
2300 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2302 if (isl_int_is_zero(bmap->div[div][0]))
2303 return 1;
2305 for (i = bmap->n_div - 1; i >= 0; --i) {
2306 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2307 continue;
2308 if (div_is_unknown(bmap, i))
2309 return 1;
2312 return 0;
2315 /* Remove all divs that are unknown or defined in terms of unknown divs.
2317 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2318 __isl_take isl_basic_map *bmap)
2320 int i;
2322 if (!bmap)
2323 return NULL;
2325 for (i = bmap->n_div - 1; i >= 0; --i) {
2326 if (!div_is_unknown(bmap, i))
2327 continue;
2328 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2329 if (!bmap)
2330 return NULL;
2331 i = bmap->n_div;
2334 return bmap;
2337 /* Remove all divs that are unknown or defined in terms of unknown divs.
2339 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2340 __isl_take isl_basic_set *bset)
2342 return isl_basic_map_remove_unknown_divs(bset);
2345 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2347 int i;
2349 if (!map)
2350 return NULL;
2351 if (map->n == 0)
2352 return map;
2354 map = isl_map_cow(map);
2355 if (!map)
2356 return NULL;
2358 for (i = 0; i < map->n; ++i) {
2359 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2360 if (!map->p[i])
2361 goto error;
2363 return map;
2364 error:
2365 isl_map_free(map);
2366 return NULL;
2369 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2371 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2374 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2375 __isl_take isl_basic_set *bset,
2376 enum isl_dim_type type, unsigned first, unsigned n)
2378 return (isl_basic_set *)
2379 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2382 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2383 enum isl_dim_type type, unsigned first, unsigned n)
2385 int i;
2387 if (n == 0)
2388 return map;
2390 map = isl_map_cow(map);
2391 if (!map)
2392 return NULL;
2393 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2395 for (i = 0; i < map->n; ++i) {
2396 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2397 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2398 if (!map->p[i])
2399 goto error;
2401 map = isl_map_drop(map, type, first, n);
2402 return map;
2403 error:
2404 isl_map_free(map);
2405 return NULL;
2408 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2409 enum isl_dim_type type, unsigned first, unsigned n)
2411 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2414 /* Project out n inputs starting at first using Fourier-Motzkin */
2415 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2416 unsigned first, unsigned n)
2418 return isl_map_remove_dims(map, isl_dim_in, first, n);
2421 static void dump_term(struct isl_basic_map *bmap,
2422 isl_int c, int pos, FILE *out)
2424 const char *name;
2425 unsigned in = isl_basic_map_n_in(bmap);
2426 unsigned dim = in + isl_basic_map_n_out(bmap);
2427 unsigned nparam = isl_basic_map_n_param(bmap);
2428 if (!pos)
2429 isl_int_print(out, c, 0);
2430 else {
2431 if (!isl_int_is_one(c))
2432 isl_int_print(out, c, 0);
2433 if (pos < 1 + nparam) {
2434 name = isl_space_get_dim_name(bmap->dim,
2435 isl_dim_param, pos - 1);
2436 if (name)
2437 fprintf(out, "%s", name);
2438 else
2439 fprintf(out, "p%d", pos - 1);
2440 } else if (pos < 1 + nparam + in)
2441 fprintf(out, "i%d", pos - 1 - nparam);
2442 else if (pos < 1 + nparam + dim)
2443 fprintf(out, "o%d", pos - 1 - nparam - in);
2444 else
2445 fprintf(out, "e%d", pos - 1 - nparam - dim);
2449 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2450 int sign, FILE *out)
2452 int i;
2453 int first;
2454 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2455 isl_int v;
2457 isl_int_init(v);
2458 for (i = 0, first = 1; i < len; ++i) {
2459 if (isl_int_sgn(c[i]) * sign <= 0)
2460 continue;
2461 if (!first)
2462 fprintf(out, " + ");
2463 first = 0;
2464 isl_int_abs(v, c[i]);
2465 dump_term(bmap, v, i, out);
2467 isl_int_clear(v);
2468 if (first)
2469 fprintf(out, "0");
2472 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2473 const char *op, FILE *out, int indent)
2475 int i;
2477 fprintf(out, "%*s", indent, "");
2479 dump_constraint_sign(bmap, c, 1, out);
2480 fprintf(out, " %s ", op);
2481 dump_constraint_sign(bmap, c, -1, out);
2483 fprintf(out, "\n");
2485 for (i = bmap->n_div; i < bmap->extra; ++i) {
2486 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2487 continue;
2488 fprintf(out, "%*s", indent, "");
2489 fprintf(out, "ERROR: unused div coefficient not zero\n");
2490 abort();
2494 static void dump_constraints(struct isl_basic_map *bmap,
2495 isl_int **c, unsigned n,
2496 const char *op, FILE *out, int indent)
2498 int i;
2500 for (i = 0; i < n; ++i)
2501 dump_constraint(bmap, c[i], op, out, indent);
2504 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2506 int j;
2507 int first = 1;
2508 unsigned total = isl_basic_map_total_dim(bmap);
2510 for (j = 0; j < 1 + total; ++j) {
2511 if (isl_int_is_zero(exp[j]))
2512 continue;
2513 if (!first && isl_int_is_pos(exp[j]))
2514 fprintf(out, "+");
2515 dump_term(bmap, exp[j], j, out);
2516 first = 0;
2520 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2522 int i;
2524 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2525 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2527 for (i = 0; i < bmap->n_div; ++i) {
2528 fprintf(out, "%*s", indent, "");
2529 fprintf(out, "e%d = [(", i);
2530 dump_affine(bmap, bmap->div[i]+1, out);
2531 fprintf(out, ")/");
2532 isl_int_print(out, bmap->div[i][0], 0);
2533 fprintf(out, "]\n");
2537 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2538 FILE *out, int indent)
2540 if (!bset) {
2541 fprintf(out, "null basic set\n");
2542 return;
2545 fprintf(out, "%*s", indent, "");
2546 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2547 bset->ref, bset->dim->nparam, bset->dim->n_out,
2548 bset->extra, bset->flags);
2549 dump((struct isl_basic_map *)bset, out, indent);
2552 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2553 FILE *out, int indent)
2555 if (!bmap) {
2556 fprintf(out, "null basic map\n");
2557 return;
2560 fprintf(out, "%*s", indent, "");
2561 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2562 "flags: %x, n_name: %d\n",
2563 bmap->ref,
2564 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2565 bmap->extra, bmap->flags, bmap->dim->n_id);
2566 dump(bmap, out, indent);
2569 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2571 unsigned total;
2572 if (!bmap)
2573 return -1;
2574 total = isl_basic_map_total_dim(bmap);
2575 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2576 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2577 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2578 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2579 return 0;
2582 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2583 unsigned flags)
2585 struct isl_set *set;
2587 if (!dim)
2588 return NULL;
2589 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2590 isl_assert(dim->ctx, n >= 0, goto error);
2591 set = isl_alloc(dim->ctx, struct isl_set,
2592 sizeof(struct isl_set) +
2593 (n - 1) * sizeof(struct isl_basic_set *));
2594 if (!set)
2595 goto error;
2597 set->ctx = dim->ctx;
2598 isl_ctx_ref(set->ctx);
2599 set->ref = 1;
2600 set->size = n;
2601 set->n = 0;
2602 set->dim = dim;
2603 set->flags = flags;
2604 return set;
2605 error:
2606 isl_space_free(dim);
2607 return NULL;
2610 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2611 unsigned nparam, unsigned dim, int n, unsigned flags)
2613 struct isl_set *set;
2614 isl_space *dims;
2616 dims = isl_space_alloc(ctx, nparam, 0, dim);
2617 if (!dims)
2618 return NULL;
2620 set = isl_set_alloc_space(dims, n, flags);
2621 return set;
2624 /* Make sure "map" has room for at least "n" more basic maps.
2626 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2628 int i;
2629 struct isl_map *grown = NULL;
2631 if (!map)
2632 return NULL;
2633 isl_assert(map->ctx, n >= 0, goto error);
2634 if (map->n + n <= map->size)
2635 return map;
2636 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2637 if (!grown)
2638 goto error;
2639 for (i = 0; i < map->n; ++i) {
2640 grown->p[i] = isl_basic_map_copy(map->p[i]);
2641 if (!grown->p[i])
2642 goto error;
2643 grown->n++;
2645 isl_map_free(map);
2646 return grown;
2647 error:
2648 isl_map_free(grown);
2649 isl_map_free(map);
2650 return NULL;
2653 /* Make sure "set" has room for at least "n" more basic sets.
2655 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2657 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2660 struct isl_set *isl_set_dup(struct isl_set *set)
2662 int i;
2663 struct isl_set *dup;
2665 if (!set)
2666 return NULL;
2668 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2669 if (!dup)
2670 return NULL;
2671 for (i = 0; i < set->n; ++i)
2672 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2673 return dup;
2676 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2678 return isl_map_from_basic_map(bset);
2681 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2683 struct isl_map *map;
2685 if (!bmap)
2686 return NULL;
2688 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2689 return isl_map_add_basic_map(map, bmap);
2692 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2693 __isl_take isl_basic_set *bset)
2695 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2696 (struct isl_basic_map *)bset);
2699 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2701 int i;
2703 if (!set)
2704 return NULL;
2706 if (--set->ref > 0)
2707 return NULL;
2709 isl_ctx_deref(set->ctx);
2710 for (i = 0; i < set->n; ++i)
2711 isl_basic_set_free(set->p[i]);
2712 isl_space_free(set->dim);
2713 free(set);
2715 return NULL;
2718 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2720 int i;
2722 if (!set) {
2723 fprintf(out, "null set\n");
2724 return;
2727 fprintf(out, "%*s", indent, "");
2728 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2729 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2730 set->flags);
2731 for (i = 0; i < set->n; ++i) {
2732 fprintf(out, "%*s", indent, "");
2733 fprintf(out, "basic set %d:\n", i);
2734 isl_basic_set_print_internal(set->p[i], out, indent+4);
2738 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2740 int i;
2742 if (!map) {
2743 fprintf(out, "null map\n");
2744 return;
2747 fprintf(out, "%*s", indent, "");
2748 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2749 "flags: %x, n_name: %d\n",
2750 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2751 map->dim->n_out, map->flags, map->dim->n_id);
2752 for (i = 0; i < map->n; ++i) {
2753 fprintf(out, "%*s", indent, "");
2754 fprintf(out, "basic map %d:\n", i);
2755 isl_basic_map_print_internal(map->p[i], out, indent+4);
2759 struct isl_basic_map *isl_basic_map_intersect_domain(
2760 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2762 struct isl_basic_map *bmap_domain;
2764 if (!bmap || !bset)
2765 goto error;
2767 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2768 bset->dim, isl_dim_param), goto error);
2770 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2771 isl_assert(bset->ctx,
2772 isl_basic_map_compatible_domain(bmap, bset), goto error);
2774 bmap = isl_basic_map_cow(bmap);
2775 if (!bmap)
2776 goto error;
2777 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2778 bset->n_div, bset->n_eq, bset->n_ineq);
2779 bmap_domain = isl_basic_map_from_domain(bset);
2780 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2782 bmap = isl_basic_map_simplify(bmap);
2783 return isl_basic_map_finalize(bmap);
2784 error:
2785 isl_basic_map_free(bmap);
2786 isl_basic_set_free(bset);
2787 return NULL;
2790 struct isl_basic_map *isl_basic_map_intersect_range(
2791 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2793 struct isl_basic_map *bmap_range;
2795 if (!bmap || !bset)
2796 goto error;
2798 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2799 bset->dim, isl_dim_param), goto error);
2801 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2802 isl_assert(bset->ctx,
2803 isl_basic_map_compatible_range(bmap, bset), goto error);
2805 if (isl_basic_set_is_universe(bset)) {
2806 isl_basic_set_free(bset);
2807 return bmap;
2810 bmap = isl_basic_map_cow(bmap);
2811 if (!bmap)
2812 goto error;
2813 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2814 bset->n_div, bset->n_eq, bset->n_ineq);
2815 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2816 bmap = add_constraints(bmap, bmap_range, 0, 0);
2818 bmap = isl_basic_map_simplify(bmap);
2819 return isl_basic_map_finalize(bmap);
2820 error:
2821 isl_basic_map_free(bmap);
2822 isl_basic_set_free(bset);
2823 return NULL;
2826 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2827 __isl_keep isl_vec *vec)
2829 int i;
2830 unsigned total;
2831 isl_int s;
2833 if (!bmap || !vec)
2834 return isl_bool_error;
2836 total = 1 + isl_basic_map_total_dim(bmap);
2837 if (total != vec->size)
2838 return isl_bool_error;
2840 isl_int_init(s);
2842 for (i = 0; i < bmap->n_eq; ++i) {
2843 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2844 if (!isl_int_is_zero(s)) {
2845 isl_int_clear(s);
2846 return isl_bool_false;
2850 for (i = 0; i < bmap->n_ineq; ++i) {
2851 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2852 if (isl_int_is_neg(s)) {
2853 isl_int_clear(s);
2854 return isl_bool_false;
2858 isl_int_clear(s);
2860 return isl_bool_true;
2863 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2864 __isl_keep isl_vec *vec)
2866 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2869 struct isl_basic_map *isl_basic_map_intersect(
2870 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2872 struct isl_vec *sample = NULL;
2874 if (!bmap1 || !bmap2)
2875 goto error;
2877 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2878 bmap2->dim, isl_dim_param), goto error);
2879 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2880 isl_space_dim(bmap1->dim, isl_dim_param) &&
2881 isl_space_dim(bmap2->dim, isl_dim_all) !=
2882 isl_space_dim(bmap2->dim, isl_dim_param))
2883 return isl_basic_map_intersect(bmap2, bmap1);
2885 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2886 isl_space_dim(bmap2->dim, isl_dim_param))
2887 isl_assert(bmap1->ctx,
2888 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2890 if (isl_basic_map_plain_is_empty(bmap1)) {
2891 isl_basic_map_free(bmap2);
2892 return bmap1;
2894 if (isl_basic_map_plain_is_empty(bmap2)) {
2895 isl_basic_map_free(bmap1);
2896 return bmap2;
2899 if (bmap1->sample &&
2900 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2901 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2902 sample = isl_vec_copy(bmap1->sample);
2903 else if (bmap2->sample &&
2904 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2905 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2906 sample = isl_vec_copy(bmap2->sample);
2908 bmap1 = isl_basic_map_cow(bmap1);
2909 if (!bmap1)
2910 goto error;
2911 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2912 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2913 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2915 if (!bmap1)
2916 isl_vec_free(sample);
2917 else if (sample) {
2918 isl_vec_free(bmap1->sample);
2919 bmap1->sample = sample;
2922 bmap1 = isl_basic_map_simplify(bmap1);
2923 return isl_basic_map_finalize(bmap1);
2924 error:
2925 if (sample)
2926 isl_vec_free(sample);
2927 isl_basic_map_free(bmap1);
2928 isl_basic_map_free(bmap2);
2929 return NULL;
2932 struct isl_basic_set *isl_basic_set_intersect(
2933 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2935 return (struct isl_basic_set *)
2936 isl_basic_map_intersect(
2937 (struct isl_basic_map *)bset1,
2938 (struct isl_basic_map *)bset2);
2941 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2942 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2944 return isl_basic_set_intersect(bset1, bset2);
2947 /* Special case of isl_map_intersect, where both map1 and map2
2948 * are convex, without any divs and such that either map1 or map2
2949 * contains a single constraint. This constraint is then simply
2950 * added to the other map.
2952 static __isl_give isl_map *map_intersect_add_constraint(
2953 __isl_take isl_map *map1, __isl_take isl_map *map2)
2955 isl_assert(map1->ctx, map1->n == 1, goto error);
2956 isl_assert(map2->ctx, map1->n == 1, goto error);
2957 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2958 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2960 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2961 return isl_map_intersect(map2, map1);
2963 isl_assert(map2->ctx,
2964 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2966 map1 = isl_map_cow(map1);
2967 if (!map1)
2968 goto error;
2969 if (isl_map_plain_is_empty(map1)) {
2970 isl_map_free(map2);
2971 return map1;
2973 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2974 if (map2->p[0]->n_eq == 1)
2975 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2976 else
2977 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2978 map2->p[0]->ineq[0]);
2980 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2981 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2982 if (!map1->p[0])
2983 goto error;
2985 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2986 isl_basic_map_free(map1->p[0]);
2987 map1->n = 0;
2990 isl_map_free(map2);
2992 return map1;
2993 error:
2994 isl_map_free(map1);
2995 isl_map_free(map2);
2996 return NULL;
2999 /* map2 may be either a parameter domain or a map living in the same
3000 * space as map1.
3002 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3003 __isl_take isl_map *map2)
3005 unsigned flags = 0;
3006 isl_map *result;
3007 int i, j;
3009 if (!map1 || !map2)
3010 goto error;
3012 if ((isl_map_plain_is_empty(map1) ||
3013 isl_map_plain_is_universe(map2)) &&
3014 isl_space_is_equal(map1->dim, map2->dim)) {
3015 isl_map_free(map2);
3016 return map1;
3018 if ((isl_map_plain_is_empty(map2) ||
3019 isl_map_plain_is_universe(map1)) &&
3020 isl_space_is_equal(map1->dim, map2->dim)) {
3021 isl_map_free(map1);
3022 return map2;
3025 if (map1->n == 1 && map2->n == 1 &&
3026 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3027 isl_space_is_equal(map1->dim, map2->dim) &&
3028 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3029 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3030 return map_intersect_add_constraint(map1, map2);
3032 if (isl_space_dim(map2->dim, isl_dim_all) !=
3033 isl_space_dim(map2->dim, isl_dim_param))
3034 isl_assert(map1->ctx,
3035 isl_space_is_equal(map1->dim, map2->dim), goto error);
3037 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3038 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3039 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3041 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3042 map1->n * map2->n, flags);
3043 if (!result)
3044 goto error;
3045 for (i = 0; i < map1->n; ++i)
3046 for (j = 0; j < map2->n; ++j) {
3047 struct isl_basic_map *part;
3048 part = isl_basic_map_intersect(
3049 isl_basic_map_copy(map1->p[i]),
3050 isl_basic_map_copy(map2->p[j]));
3051 if (isl_basic_map_is_empty(part) < 0)
3052 part = isl_basic_map_free(part);
3053 result = isl_map_add_basic_map(result, part);
3054 if (!result)
3055 goto error;
3057 isl_map_free(map1);
3058 isl_map_free(map2);
3059 return result;
3060 error:
3061 isl_map_free(map1);
3062 isl_map_free(map2);
3063 return NULL;
3066 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3067 __isl_take isl_map *map2)
3069 if (!map1 || !map2)
3070 goto error;
3071 if (!isl_space_is_equal(map1->dim, map2->dim))
3072 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3073 "spaces don't match", goto error);
3074 return map_intersect_internal(map1, map2);
3075 error:
3076 isl_map_free(map1);
3077 isl_map_free(map2);
3078 return NULL;
3081 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3082 __isl_take isl_map *map2)
3084 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3087 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3089 return (struct isl_set *)
3090 isl_map_intersect((struct isl_map *)set1,
3091 (struct isl_map *)set2);
3094 /* map_intersect_internal accepts intersections
3095 * with parameter domains, so we can just call that function.
3097 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3098 __isl_take isl_set *params)
3100 return map_intersect_internal(map, params);
3103 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3104 __isl_take isl_map *map2)
3106 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3109 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3110 __isl_take isl_set *params)
3112 return isl_map_intersect_params(set, params);
3115 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3117 isl_space *space;
3118 unsigned pos, n1, n2;
3120 if (!bmap)
3121 return NULL;
3122 bmap = isl_basic_map_cow(bmap);
3123 if (!bmap)
3124 return NULL;
3125 space = isl_space_reverse(isl_space_copy(bmap->dim));
3126 pos = isl_basic_map_offset(bmap, isl_dim_in);
3127 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3128 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3129 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3130 return isl_basic_map_reset_space(bmap, space);
3133 static __isl_give isl_basic_map *basic_map_space_reset(
3134 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3136 isl_space *space;
3138 if (!bmap)
3139 return NULL;
3140 if (!isl_space_is_named_or_nested(bmap->dim, type))
3141 return bmap;
3143 space = isl_basic_map_get_space(bmap);
3144 space = isl_space_reset(space, type);
3145 bmap = isl_basic_map_reset_space(bmap, space);
3146 return bmap;
3149 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3150 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3151 unsigned pos, unsigned n)
3153 isl_space *res_dim;
3154 struct isl_basic_map *res;
3155 struct isl_dim_map *dim_map;
3156 unsigned total, off;
3157 enum isl_dim_type t;
3159 if (n == 0)
3160 return basic_map_space_reset(bmap, type);
3162 if (!bmap)
3163 return NULL;
3165 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3167 total = isl_basic_map_total_dim(bmap) + n;
3168 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3169 off = 0;
3170 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3171 if (t != type) {
3172 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3173 } else {
3174 unsigned size = isl_basic_map_dim(bmap, t);
3175 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3176 0, pos, off);
3177 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3178 pos, size - pos, off + pos + n);
3180 off += isl_space_dim(res_dim, t);
3182 isl_dim_map_div(dim_map, bmap, off);
3184 res = isl_basic_map_alloc_space(res_dim,
3185 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3186 if (isl_basic_map_is_rational(bmap))
3187 res = isl_basic_map_set_rational(res);
3188 if (isl_basic_map_plain_is_empty(bmap)) {
3189 isl_basic_map_free(bmap);
3190 free(dim_map);
3191 return isl_basic_map_set_to_empty(res);
3193 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3194 return isl_basic_map_finalize(res);
3197 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3198 __isl_take isl_basic_set *bset,
3199 enum isl_dim_type type, unsigned pos, unsigned n)
3201 return isl_basic_map_insert_dims(bset, type, pos, n);
3204 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3205 enum isl_dim_type type, unsigned n)
3207 if (!bmap)
3208 return NULL;
3209 return isl_basic_map_insert_dims(bmap, type,
3210 isl_basic_map_dim(bmap, type), n);
3213 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3214 enum isl_dim_type type, unsigned n)
3216 if (!bset)
3217 return NULL;
3218 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3219 return isl_basic_map_add_dims(bset, type, n);
3220 error:
3221 isl_basic_set_free(bset);
3222 return NULL;
3225 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3226 enum isl_dim_type type)
3228 isl_space *space;
3230 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3231 return map;
3233 space = isl_map_get_space(map);
3234 space = isl_space_reset(space, type);
3235 map = isl_map_reset_space(map, space);
3236 return map;
3239 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3240 enum isl_dim_type type, unsigned pos, unsigned n)
3242 int i;
3244 if (n == 0)
3245 return map_space_reset(map, type);
3247 map = isl_map_cow(map);
3248 if (!map)
3249 return NULL;
3251 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3252 if (!map->dim)
3253 goto error;
3255 for (i = 0; i < map->n; ++i) {
3256 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3257 if (!map->p[i])
3258 goto error;
3261 return map;
3262 error:
3263 isl_map_free(map);
3264 return NULL;
3267 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3268 enum isl_dim_type type, unsigned pos, unsigned n)
3270 return isl_map_insert_dims(set, type, pos, n);
3273 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3274 enum isl_dim_type type, unsigned n)
3276 if (!map)
3277 return NULL;
3278 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3281 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3282 enum isl_dim_type type, unsigned n)
3284 if (!set)
3285 return NULL;
3286 isl_assert(set->ctx, type != isl_dim_in, goto error);
3287 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3288 error:
3289 isl_set_free(set);
3290 return NULL;
3293 __isl_give isl_basic_map *isl_basic_map_move_dims(
3294 __isl_take isl_basic_map *bmap,
3295 enum isl_dim_type dst_type, unsigned dst_pos,
3296 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3298 struct isl_dim_map *dim_map;
3299 struct isl_basic_map *res;
3300 enum isl_dim_type t;
3301 unsigned total, off;
3303 if (!bmap)
3304 return NULL;
3305 if (n == 0)
3306 return bmap;
3308 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3309 goto error);
3311 if (dst_type == src_type && dst_pos == src_pos)
3312 return bmap;
3314 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3316 if (pos(bmap->dim, dst_type) + dst_pos ==
3317 pos(bmap->dim, src_type) + src_pos +
3318 ((src_type < dst_type) ? n : 0)) {
3319 bmap = isl_basic_map_cow(bmap);
3320 if (!bmap)
3321 return NULL;
3323 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3324 src_type, src_pos, n);
3325 if (!bmap->dim)
3326 goto error;
3328 bmap = isl_basic_map_finalize(bmap);
3330 return bmap;
3333 total = isl_basic_map_total_dim(bmap);
3334 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3336 off = 0;
3337 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3338 unsigned size = isl_space_dim(bmap->dim, t);
3339 if (t == dst_type) {
3340 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3341 0, dst_pos, off);
3342 off += dst_pos;
3343 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3344 src_pos, n, off);
3345 off += n;
3346 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3347 dst_pos, size - dst_pos, off);
3348 off += size - dst_pos;
3349 } else if (t == src_type) {
3350 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3351 0, src_pos, off);
3352 off += src_pos;
3353 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3354 src_pos + n, size - src_pos - n, off);
3355 off += size - src_pos - n;
3356 } else {
3357 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3358 off += size;
3361 isl_dim_map_div(dim_map, bmap, off);
3363 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3364 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3365 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3366 if (!bmap)
3367 goto error;
3369 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3370 src_type, src_pos, n);
3371 if (!bmap->dim)
3372 goto error;
3374 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3375 bmap = isl_basic_map_gauss(bmap, NULL);
3376 bmap = isl_basic_map_finalize(bmap);
3378 return bmap;
3379 error:
3380 isl_basic_map_free(bmap);
3381 return NULL;
3384 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3385 enum isl_dim_type dst_type, unsigned dst_pos,
3386 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3388 return (isl_basic_set *)isl_basic_map_move_dims(
3389 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3392 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3393 enum isl_dim_type dst_type, unsigned dst_pos,
3394 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3396 if (!set)
3397 return NULL;
3398 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3399 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3400 src_type, src_pos, n);
3401 error:
3402 isl_set_free(set);
3403 return NULL;
3406 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3407 enum isl_dim_type dst_type, unsigned dst_pos,
3408 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3410 int i;
3412 if (!map)
3413 return NULL;
3414 if (n == 0)
3415 return map;
3417 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3418 goto error);
3420 if (dst_type == src_type && dst_pos == src_pos)
3421 return map;
3423 isl_assert(map->ctx, dst_type != src_type, goto error);
3425 map = isl_map_cow(map);
3426 if (!map)
3427 return NULL;
3429 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3430 if (!map->dim)
3431 goto error;
3433 for (i = 0; i < map->n; ++i) {
3434 map->p[i] = isl_basic_map_move_dims(map->p[i],
3435 dst_type, dst_pos,
3436 src_type, src_pos, n);
3437 if (!map->p[i])
3438 goto error;
3441 return map;
3442 error:
3443 isl_map_free(map);
3444 return NULL;
3447 /* Move the specified dimensions to the last columns right before
3448 * the divs. Don't change the dimension specification of bmap.
3449 * That's the responsibility of the caller.
3451 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3452 enum isl_dim_type type, unsigned first, unsigned n)
3454 struct isl_dim_map *dim_map;
3455 struct isl_basic_map *res;
3456 enum isl_dim_type t;
3457 unsigned total, off;
3459 if (!bmap)
3460 return NULL;
3461 if (pos(bmap->dim, type) + first + n ==
3462 1 + isl_space_dim(bmap->dim, isl_dim_all))
3463 return bmap;
3465 total = isl_basic_map_total_dim(bmap);
3466 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3468 off = 0;
3469 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3470 unsigned size = isl_space_dim(bmap->dim, t);
3471 if (t == type) {
3472 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3473 0, first, off);
3474 off += first;
3475 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3476 first, n, total - bmap->n_div - n);
3477 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3478 first + n, size - (first + n), off);
3479 off += size - (first + n);
3480 } else {
3481 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3482 off += size;
3485 isl_dim_map_div(dim_map, bmap, off + n);
3487 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3488 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3489 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3490 return res;
3493 /* Insert "n" rows in the divs of "bmap".
3495 * The number of columns is not changed, which means that the last
3496 * dimensions of "bmap" are being reintepreted as the new divs.
3497 * The space of "bmap" is not adjusted, however, which means
3498 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3499 * from the space of "bmap" is the responsibility of the caller.
3501 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3502 int n)
3504 int i;
3505 size_t row_size;
3506 isl_int **new_div;
3507 isl_int *old;
3509 bmap = isl_basic_map_cow(bmap);
3510 if (!bmap)
3511 return NULL;
3513 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3514 old = bmap->block2.data;
3515 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3516 (bmap->extra + n) * (1 + row_size));
3517 if (!bmap->block2.data)
3518 return isl_basic_map_free(bmap);
3519 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3520 if (!new_div)
3521 return isl_basic_map_free(bmap);
3522 for (i = 0; i < n; ++i) {
3523 new_div[i] = bmap->block2.data +
3524 (bmap->extra + i) * (1 + row_size);
3525 isl_seq_clr(new_div[i], 1 + row_size);
3527 for (i = 0; i < bmap->extra; ++i)
3528 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3529 free(bmap->div);
3530 bmap->div = new_div;
3531 bmap->n_div += n;
3532 bmap->extra += n;
3534 return bmap;
3537 /* Turn the n dimensions of type type, starting at first
3538 * into existentially quantified variables.
3540 __isl_give isl_basic_map *isl_basic_map_project_out(
3541 __isl_take isl_basic_map *bmap,
3542 enum isl_dim_type type, unsigned first, unsigned n)
3544 if (n == 0)
3545 return basic_map_space_reset(bmap, type);
3547 if (!bmap)
3548 return NULL;
3550 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3551 return isl_basic_map_remove_dims(bmap, type, first, n);
3553 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3554 goto error);
3556 bmap = move_last(bmap, type, first, n);
3557 bmap = isl_basic_map_cow(bmap);
3558 bmap = insert_div_rows(bmap, n);
3559 if (!bmap)
3560 return NULL;
3562 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3563 if (!bmap->dim)
3564 goto error;
3565 bmap = isl_basic_map_simplify(bmap);
3566 bmap = isl_basic_map_drop_redundant_divs(bmap);
3567 return isl_basic_map_finalize(bmap);
3568 error:
3569 isl_basic_map_free(bmap);
3570 return NULL;
3573 /* Turn the n dimensions of type type, starting at first
3574 * into existentially quantified variables.
3576 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3577 enum isl_dim_type type, unsigned first, unsigned n)
3579 return (isl_basic_set *)isl_basic_map_project_out(
3580 (isl_basic_map *)bset, type, first, n);
3583 /* Turn the n dimensions of type type, starting at first
3584 * into existentially quantified variables.
3586 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3587 enum isl_dim_type type, unsigned first, unsigned n)
3589 int i;
3591 if (!map)
3592 return NULL;
3594 if (n == 0)
3595 return map_space_reset(map, type);
3597 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3599 map = isl_map_cow(map);
3600 if (!map)
3601 return NULL;
3603 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3604 if (!map->dim)
3605 goto error;
3607 for (i = 0; i < map->n; ++i) {
3608 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3609 if (!map->p[i])
3610 goto error;
3613 return map;
3614 error:
3615 isl_map_free(map);
3616 return NULL;
3619 /* Turn the n dimensions of type type, starting at first
3620 * into existentially quantified variables.
3622 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3623 enum isl_dim_type type, unsigned first, unsigned n)
3625 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3628 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3630 int i, j;
3632 for (i = 0; i < n; ++i) {
3633 j = isl_basic_map_alloc_div(bmap);
3634 if (j < 0)
3635 goto error;
3636 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3638 return bmap;
3639 error:
3640 isl_basic_map_free(bmap);
3641 return NULL;
3644 struct isl_basic_map *isl_basic_map_apply_range(
3645 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3647 isl_space *dim_result = NULL;
3648 struct isl_basic_map *bmap;
3649 unsigned n_in, n_out, n, nparam, total, pos;
3650 struct isl_dim_map *dim_map1, *dim_map2;
3652 if (!bmap1 || !bmap2)
3653 goto error;
3654 if (!isl_space_match(bmap1->dim, isl_dim_param,
3655 bmap2->dim, isl_dim_param))
3656 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3657 "parameters don't match", goto error);
3658 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3659 bmap2->dim, isl_dim_in))
3660 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3661 "spaces don't match", goto error);
3663 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3664 isl_space_copy(bmap2->dim));
3666 n_in = isl_basic_map_n_in(bmap1);
3667 n_out = isl_basic_map_n_out(bmap2);
3668 n = isl_basic_map_n_out(bmap1);
3669 nparam = isl_basic_map_n_param(bmap1);
3671 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3672 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3673 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3674 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3675 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3676 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3677 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3678 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3679 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3680 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3681 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3683 bmap = isl_basic_map_alloc_space(dim_result,
3684 bmap1->n_div + bmap2->n_div + n,
3685 bmap1->n_eq + bmap2->n_eq,
3686 bmap1->n_ineq + bmap2->n_ineq);
3687 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3688 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3689 bmap = add_divs(bmap, n);
3690 bmap = isl_basic_map_simplify(bmap);
3691 bmap = isl_basic_map_drop_redundant_divs(bmap);
3692 return isl_basic_map_finalize(bmap);
3693 error:
3694 isl_basic_map_free(bmap1);
3695 isl_basic_map_free(bmap2);
3696 return NULL;
3699 struct isl_basic_set *isl_basic_set_apply(
3700 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3702 if (!bset || !bmap)
3703 goto error;
3705 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3706 goto error);
3708 return (struct isl_basic_set *)
3709 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3710 error:
3711 isl_basic_set_free(bset);
3712 isl_basic_map_free(bmap);
3713 return NULL;
3716 struct isl_basic_map *isl_basic_map_apply_domain(
3717 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3719 if (!bmap1 || !bmap2)
3720 goto error;
3722 isl_assert(bmap1->ctx,
3723 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3724 isl_assert(bmap1->ctx,
3725 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3726 goto error);
3728 bmap1 = isl_basic_map_reverse(bmap1);
3729 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3730 return isl_basic_map_reverse(bmap1);
3731 error:
3732 isl_basic_map_free(bmap1);
3733 isl_basic_map_free(bmap2);
3734 return NULL;
3737 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3738 * A \cap B -> f(A) + f(B)
3740 struct isl_basic_map *isl_basic_map_sum(
3741 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3743 unsigned n_in, n_out, nparam, total, pos;
3744 struct isl_basic_map *bmap = NULL;
3745 struct isl_dim_map *dim_map1, *dim_map2;
3746 int i;
3748 if (!bmap1 || !bmap2)
3749 goto error;
3751 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3752 goto error);
3754 nparam = isl_basic_map_n_param(bmap1);
3755 n_in = isl_basic_map_n_in(bmap1);
3756 n_out = isl_basic_map_n_out(bmap1);
3758 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3759 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3760 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3761 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3762 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3763 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3764 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3765 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3766 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3767 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3768 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3770 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3771 bmap1->n_div + bmap2->n_div + 2 * n_out,
3772 bmap1->n_eq + bmap2->n_eq + n_out,
3773 bmap1->n_ineq + bmap2->n_ineq);
3774 for (i = 0; i < n_out; ++i) {
3775 int j = isl_basic_map_alloc_equality(bmap);
3776 if (j < 0)
3777 goto error;
3778 isl_seq_clr(bmap->eq[j], 1+total);
3779 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3780 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3781 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3783 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3784 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3785 bmap = add_divs(bmap, 2 * n_out);
3787 bmap = isl_basic_map_simplify(bmap);
3788 return isl_basic_map_finalize(bmap);
3789 error:
3790 isl_basic_map_free(bmap);
3791 isl_basic_map_free(bmap1);
3792 isl_basic_map_free(bmap2);
3793 return NULL;
3796 /* Given two maps A -> f(A) and B -> g(B), construct a map
3797 * A \cap B -> f(A) + f(B)
3799 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3801 struct isl_map *result;
3802 int i, j;
3804 if (!map1 || !map2)
3805 goto error;
3807 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3809 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3810 map1->n * map2->n, 0);
3811 if (!result)
3812 goto error;
3813 for (i = 0; i < map1->n; ++i)
3814 for (j = 0; j < map2->n; ++j) {
3815 struct isl_basic_map *part;
3816 part = isl_basic_map_sum(
3817 isl_basic_map_copy(map1->p[i]),
3818 isl_basic_map_copy(map2->p[j]));
3819 if (isl_basic_map_is_empty(part))
3820 isl_basic_map_free(part);
3821 else
3822 result = isl_map_add_basic_map(result, part);
3823 if (!result)
3824 goto error;
3826 isl_map_free(map1);
3827 isl_map_free(map2);
3828 return result;
3829 error:
3830 isl_map_free(map1);
3831 isl_map_free(map2);
3832 return NULL;
3835 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3836 __isl_take isl_set *set2)
3838 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3841 /* Given a basic map A -> f(A), construct A -> -f(A).
3843 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3845 int i, j;
3846 unsigned off, n;
3848 bmap = isl_basic_map_cow(bmap);
3849 if (!bmap)
3850 return NULL;
3852 n = isl_basic_map_dim(bmap, isl_dim_out);
3853 off = isl_basic_map_offset(bmap, isl_dim_out);
3854 for (i = 0; i < bmap->n_eq; ++i)
3855 for (j = 0; j < n; ++j)
3856 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3857 for (i = 0; i < bmap->n_ineq; ++i)
3858 for (j = 0; j < n; ++j)
3859 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3860 for (i = 0; i < bmap->n_div; ++i)
3861 for (j = 0; j < n; ++j)
3862 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3863 bmap = isl_basic_map_gauss(bmap, NULL);
3864 return isl_basic_map_finalize(bmap);
3867 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3869 return isl_basic_map_neg(bset);
3872 /* Given a map A -> f(A), construct A -> -f(A).
3874 struct isl_map *isl_map_neg(struct isl_map *map)
3876 int i;
3878 map = isl_map_cow(map);
3879 if (!map)
3880 return NULL;
3882 for (i = 0; i < map->n; ++i) {
3883 map->p[i] = isl_basic_map_neg(map->p[i]);
3884 if (!map->p[i])
3885 goto error;
3888 return map;
3889 error:
3890 isl_map_free(map);
3891 return NULL;
3894 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3896 return (isl_set *)isl_map_neg((isl_map *)set);
3899 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3900 * A -> floor(f(A)/d).
3902 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3903 isl_int d)
3905 unsigned n_in, n_out, nparam, total, pos;
3906 struct isl_basic_map *result = NULL;
3907 struct isl_dim_map *dim_map;
3908 int i;
3910 if (!bmap)
3911 return NULL;
3913 nparam = isl_basic_map_n_param(bmap);
3914 n_in = isl_basic_map_n_in(bmap);
3915 n_out = isl_basic_map_n_out(bmap);
3917 total = nparam + n_in + n_out + bmap->n_div + n_out;
3918 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3919 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3920 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3921 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3922 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3924 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3925 bmap->n_div + n_out,
3926 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3927 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3928 result = add_divs(result, n_out);
3929 for (i = 0; i < n_out; ++i) {
3930 int j;
3931 j = isl_basic_map_alloc_inequality(result);
3932 if (j < 0)
3933 goto error;
3934 isl_seq_clr(result->ineq[j], 1+total);
3935 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3936 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3937 j = isl_basic_map_alloc_inequality(result);
3938 if (j < 0)
3939 goto error;
3940 isl_seq_clr(result->ineq[j], 1+total);
3941 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3942 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3943 isl_int_sub_ui(result->ineq[j][0], d, 1);
3946 result = isl_basic_map_simplify(result);
3947 return isl_basic_map_finalize(result);
3948 error:
3949 isl_basic_map_free(result);
3950 return NULL;
3953 /* Given a map A -> f(A) and an integer d, construct a map
3954 * A -> floor(f(A)/d).
3956 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3958 int i;
3960 map = isl_map_cow(map);
3961 if (!map)
3962 return NULL;
3964 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3965 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3966 for (i = 0; i < map->n; ++i) {
3967 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3968 if (!map->p[i])
3969 goto error;
3972 return map;
3973 error:
3974 isl_map_free(map);
3975 return NULL;
3978 /* Given a map A -> f(A) and an integer d, construct a map
3979 * A -> floor(f(A)/d).
3981 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3982 __isl_take isl_val *d)
3984 if (!map || !d)
3985 goto error;
3986 if (!isl_val_is_int(d))
3987 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3988 "expecting integer denominator", goto error);
3989 map = isl_map_floordiv(map, d->n);
3990 isl_val_free(d);
3991 return map;
3992 error:
3993 isl_map_free(map);
3994 isl_val_free(d);
3995 return NULL;
3998 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4000 int i;
4001 unsigned nparam;
4002 unsigned n_in;
4004 i = isl_basic_map_alloc_equality(bmap);
4005 if (i < 0)
4006 goto error;
4007 nparam = isl_basic_map_n_param(bmap);
4008 n_in = isl_basic_map_n_in(bmap);
4009 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4010 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4011 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4012 return isl_basic_map_finalize(bmap);
4013 error:
4014 isl_basic_map_free(bmap);
4015 return NULL;
4018 /* Add a constraints to "bmap" expressing i_pos < o_pos
4020 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4022 int i;
4023 unsigned nparam;
4024 unsigned n_in;
4026 i = isl_basic_map_alloc_inequality(bmap);
4027 if (i < 0)
4028 goto error;
4029 nparam = isl_basic_map_n_param(bmap);
4030 n_in = isl_basic_map_n_in(bmap);
4031 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4032 isl_int_set_si(bmap->ineq[i][0], -1);
4033 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4034 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4035 return isl_basic_map_finalize(bmap);
4036 error:
4037 isl_basic_map_free(bmap);
4038 return NULL;
4041 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4043 static __isl_give isl_basic_map *var_less_or_equal(
4044 __isl_take isl_basic_map *bmap, unsigned pos)
4046 int i;
4047 unsigned nparam;
4048 unsigned n_in;
4050 i = isl_basic_map_alloc_inequality(bmap);
4051 if (i < 0)
4052 goto error;
4053 nparam = isl_basic_map_n_param(bmap);
4054 n_in = isl_basic_map_n_in(bmap);
4055 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4056 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4057 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4058 return isl_basic_map_finalize(bmap);
4059 error:
4060 isl_basic_map_free(bmap);
4061 return NULL;
4064 /* Add a constraints to "bmap" expressing i_pos > o_pos
4066 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4068 int i;
4069 unsigned nparam;
4070 unsigned n_in;
4072 i = isl_basic_map_alloc_inequality(bmap);
4073 if (i < 0)
4074 goto error;
4075 nparam = isl_basic_map_n_param(bmap);
4076 n_in = isl_basic_map_n_in(bmap);
4077 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4078 isl_int_set_si(bmap->ineq[i][0], -1);
4079 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4080 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4081 return isl_basic_map_finalize(bmap);
4082 error:
4083 isl_basic_map_free(bmap);
4084 return NULL;
4087 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4089 static __isl_give isl_basic_map *var_more_or_equal(
4090 __isl_take isl_basic_map *bmap, unsigned pos)
4092 int i;
4093 unsigned nparam;
4094 unsigned n_in;
4096 i = isl_basic_map_alloc_inequality(bmap);
4097 if (i < 0)
4098 goto error;
4099 nparam = isl_basic_map_n_param(bmap);
4100 n_in = isl_basic_map_n_in(bmap);
4101 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4102 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4103 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4104 return isl_basic_map_finalize(bmap);
4105 error:
4106 isl_basic_map_free(bmap);
4107 return NULL;
4110 __isl_give isl_basic_map *isl_basic_map_equal(
4111 __isl_take isl_space *dim, unsigned n_equal)
4113 int i;
4114 struct isl_basic_map *bmap;
4115 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4116 if (!bmap)
4117 return NULL;
4118 for (i = 0; i < n_equal && bmap; ++i)
4119 bmap = var_equal(bmap, i);
4120 return isl_basic_map_finalize(bmap);
4123 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4125 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4126 unsigned pos)
4128 int i;
4129 struct isl_basic_map *bmap;
4130 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4131 if (!bmap)
4132 return NULL;
4133 for (i = 0; i < pos && bmap; ++i)
4134 bmap = var_equal(bmap, i);
4135 if (bmap)
4136 bmap = var_less(bmap, pos);
4137 return isl_basic_map_finalize(bmap);
4140 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4142 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4143 __isl_take isl_space *dim, unsigned pos)
4145 int i;
4146 isl_basic_map *bmap;
4148 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4149 for (i = 0; i < pos; ++i)
4150 bmap = var_equal(bmap, i);
4151 bmap = var_less_or_equal(bmap, pos);
4152 return isl_basic_map_finalize(bmap);
4155 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4157 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4158 unsigned pos)
4160 int i;
4161 struct isl_basic_map *bmap;
4162 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4163 if (!bmap)
4164 return NULL;
4165 for (i = 0; i < pos && bmap; ++i)
4166 bmap = var_equal(bmap, i);
4167 if (bmap)
4168 bmap = var_more(bmap, pos);
4169 return isl_basic_map_finalize(bmap);
4172 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4174 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4175 __isl_take isl_space *dim, unsigned pos)
4177 int i;
4178 isl_basic_map *bmap;
4180 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4181 for (i = 0; i < pos; ++i)
4182 bmap = var_equal(bmap, i);
4183 bmap = var_more_or_equal(bmap, pos);
4184 return isl_basic_map_finalize(bmap);
4187 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4188 unsigned n, int equal)
4190 struct isl_map *map;
4191 int i;
4193 if (n == 0 && equal)
4194 return isl_map_universe(dims);
4196 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4198 for (i = 0; i + 1 < n; ++i)
4199 map = isl_map_add_basic_map(map,
4200 isl_basic_map_less_at(isl_space_copy(dims), i));
4201 if (n > 0) {
4202 if (equal)
4203 map = isl_map_add_basic_map(map,
4204 isl_basic_map_less_or_equal_at(dims, n - 1));
4205 else
4206 map = isl_map_add_basic_map(map,
4207 isl_basic_map_less_at(dims, n - 1));
4208 } else
4209 isl_space_free(dims);
4211 return map;
4214 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4216 if (!dims)
4217 return NULL;
4218 return map_lex_lte_first(dims, dims->n_out, equal);
4221 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4223 return map_lex_lte_first(dim, n, 0);
4226 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4228 return map_lex_lte_first(dim, n, 1);
4231 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4233 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4236 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4238 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4241 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4242 unsigned n, int equal)
4244 struct isl_map *map;
4245 int i;
4247 if (n == 0 && equal)
4248 return isl_map_universe(dims);
4250 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4252 for (i = 0; i + 1 < n; ++i)
4253 map = isl_map_add_basic_map(map,
4254 isl_basic_map_more_at(isl_space_copy(dims), i));
4255 if (n > 0) {
4256 if (equal)
4257 map = isl_map_add_basic_map(map,
4258 isl_basic_map_more_or_equal_at(dims, n - 1));
4259 else
4260 map = isl_map_add_basic_map(map,
4261 isl_basic_map_more_at(dims, n - 1));
4262 } else
4263 isl_space_free(dims);
4265 return map;
4268 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4270 if (!dims)
4271 return NULL;
4272 return map_lex_gte_first(dims, dims->n_out, equal);
4275 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4277 return map_lex_gte_first(dim, n, 0);
4280 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4282 return map_lex_gte_first(dim, n, 1);
4285 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4287 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4290 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4292 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4295 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4296 __isl_take isl_set *set2)
4298 isl_map *map;
4299 map = isl_map_lex_le(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_set_lex_lt_set(__isl_take isl_set *set1,
4306 __isl_take isl_set *set2)
4308 isl_map *map;
4309 map = isl_map_lex_lt(isl_set_get_space(set1));
4310 map = isl_map_intersect_domain(map, set1);
4311 map = isl_map_intersect_range(map, set2);
4312 return map;
4315 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4316 __isl_take isl_set *set2)
4318 isl_map *map;
4319 map = isl_map_lex_ge(isl_set_get_space(set1));
4320 map = isl_map_intersect_domain(map, set1);
4321 map = isl_map_intersect_range(map, set2);
4322 return map;
4325 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4326 __isl_take isl_set *set2)
4328 isl_map *map;
4329 map = isl_map_lex_gt(isl_set_get_space(set1));
4330 map = isl_map_intersect_domain(map, set1);
4331 map = isl_map_intersect_range(map, set2);
4332 return map;
4335 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4336 __isl_take isl_map *map2)
4338 isl_map *map;
4339 map = isl_map_lex_le(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_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4346 __isl_take isl_map *map2)
4348 isl_map *map;
4349 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4350 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4351 map = isl_map_apply_range(map, isl_map_reverse(map2));
4352 return map;
4355 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4356 __isl_take isl_map *map2)
4358 isl_map *map;
4359 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4360 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4361 map = isl_map_apply_range(map, isl_map_reverse(map2));
4362 return map;
4365 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4366 __isl_take isl_map *map2)
4368 isl_map *map;
4369 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4370 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4371 map = isl_map_apply_range(map, isl_map_reverse(map2));
4372 return map;
4375 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4376 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4378 struct isl_basic_map *bmap;
4380 bset = isl_basic_set_cow(bset);
4381 if (!bset || !dim)
4382 goto error;
4384 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4385 isl_space_free(bset->dim);
4386 bmap = (struct isl_basic_map *) bset;
4387 bmap->dim = dim;
4388 return isl_basic_map_finalize(bmap);
4389 error:
4390 isl_basic_set_free(bset);
4391 isl_space_free(dim);
4392 return NULL;
4395 /* For a div d = floor(f/m), add the constraint
4397 * f - m d >= 0
4399 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4400 unsigned pos, isl_int *div)
4402 int i;
4403 unsigned total = isl_basic_map_total_dim(bmap);
4405 i = isl_basic_map_alloc_inequality(bmap);
4406 if (i < 0)
4407 return -1;
4408 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4409 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4411 return 0;
4414 /* For a div d = floor(f/m), add the constraint
4416 * -(f-(n-1)) + m d >= 0
4418 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4419 unsigned pos, isl_int *div)
4421 int i;
4422 unsigned total = isl_basic_map_total_dim(bmap);
4424 i = isl_basic_map_alloc_inequality(bmap);
4425 if (i < 0)
4426 return -1;
4427 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4428 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4429 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4430 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4432 return 0;
4435 /* For a div d = floor(f/m), add the constraints
4437 * f - m d >= 0
4438 * -(f-(n-1)) + m d >= 0
4440 * Note that the second constraint is the negation of
4442 * f - m d >= n
4444 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4445 unsigned pos, isl_int *div)
4447 if (add_upper_div_constraint(bmap, pos, div) < 0)
4448 return -1;
4449 if (add_lower_div_constraint(bmap, pos, div) < 0)
4450 return -1;
4451 return 0;
4454 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4455 unsigned pos, isl_int *div)
4457 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4458 pos, div);
4461 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4463 unsigned total = isl_basic_map_total_dim(bmap);
4464 unsigned div_pos = total - bmap->n_div + div;
4466 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4467 bmap->div[div]);
4470 /* For each known div d = floor(f/m), add the constraints
4472 * f - m d >= 0
4473 * -(f-(n-1)) + m d >= 0
4475 * Remove duplicate constraints in case of some these div constraints
4476 * already appear in "bmap".
4478 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4479 __isl_take isl_basic_map *bmap)
4481 unsigned n_div;
4483 if (!bmap)
4484 return NULL;
4485 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4486 if (n_div == 0)
4487 return bmap;
4489 bmap = add_known_div_constraints(bmap);
4490 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4491 bmap = isl_basic_map_finalize(bmap);
4492 return bmap;
4495 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4497 * In particular, if this div is of the form d = floor(f/m),
4498 * then add the constraint
4500 * f - m d >= 0
4502 * if sign < 0 or the constraint
4504 * -(f-(n-1)) + m d >= 0
4506 * if sign > 0.
4508 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4509 unsigned div, int sign)
4511 unsigned total;
4512 unsigned div_pos;
4514 if (!bmap)
4515 return -1;
4517 total = isl_basic_map_total_dim(bmap);
4518 div_pos = total - bmap->n_div + div;
4520 if (sign < 0)
4521 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4522 else
4523 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4526 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4528 return isl_basic_map_add_div_constraints(bset, div);
4531 struct isl_basic_set *isl_basic_map_underlying_set(
4532 struct isl_basic_map *bmap)
4534 if (!bmap)
4535 goto error;
4536 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4537 bmap->n_div == 0 &&
4538 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4539 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4540 return (struct isl_basic_set *)bmap;
4541 bmap = isl_basic_map_cow(bmap);
4542 if (!bmap)
4543 goto error;
4544 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4545 if (!bmap->dim)
4546 goto error;
4547 bmap->extra -= bmap->n_div;
4548 bmap->n_div = 0;
4549 bmap = isl_basic_map_finalize(bmap);
4550 return (struct isl_basic_set *)bmap;
4551 error:
4552 isl_basic_map_free(bmap);
4553 return NULL;
4556 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4557 __isl_take isl_basic_set *bset)
4559 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4562 /* Replace each element in "list" by the result of applying
4563 * isl_basic_map_underlying_set to the element.
4565 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4566 __isl_take isl_basic_map_list *list)
4568 int i, n;
4570 if (!list)
4571 return NULL;
4573 n = isl_basic_map_list_n_basic_map(list);
4574 for (i = 0; i < n; ++i) {
4575 isl_basic_map *bmap;
4576 isl_basic_set *bset;
4578 bmap = isl_basic_map_list_get_basic_map(list, i);
4579 bset = isl_basic_set_underlying_set(bmap);
4580 list = isl_basic_set_list_set_basic_set(list, i, bset);
4583 return list;
4586 struct isl_basic_map *isl_basic_map_overlying_set(
4587 struct isl_basic_set *bset, struct isl_basic_map *like)
4589 struct isl_basic_map *bmap;
4590 struct isl_ctx *ctx;
4591 unsigned total;
4592 int i;
4594 if (!bset || !like)
4595 goto error;
4596 ctx = bset->ctx;
4597 isl_assert(ctx, bset->n_div == 0, goto error);
4598 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4599 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4600 goto error);
4601 if (like->n_div == 0) {
4602 isl_space *space = isl_basic_map_get_space(like);
4603 isl_basic_map_free(like);
4604 return isl_basic_map_reset_space(bset, space);
4606 bset = isl_basic_set_cow(bset);
4607 if (!bset)
4608 goto error;
4609 total = bset->dim->n_out + bset->extra;
4610 bmap = (struct isl_basic_map *)bset;
4611 isl_space_free(bmap->dim);
4612 bmap->dim = isl_space_copy(like->dim);
4613 if (!bmap->dim)
4614 goto error;
4615 bmap->n_div = like->n_div;
4616 bmap->extra += like->n_div;
4617 if (bmap->extra) {
4618 unsigned ltotal;
4619 isl_int **div;
4620 ltotal = total - bmap->extra + like->extra;
4621 if (ltotal > total)
4622 ltotal = total;
4623 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4624 bmap->extra * (1 + 1 + total));
4625 if (isl_blk_is_error(bmap->block2))
4626 goto error;
4627 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4628 if (!div)
4629 goto error;
4630 bmap->div = div;
4631 for (i = 0; i < bmap->extra; ++i)
4632 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4633 for (i = 0; i < like->n_div; ++i) {
4634 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4635 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4637 bmap = isl_basic_map_add_known_div_constraints(bmap);
4639 isl_basic_map_free(like);
4640 bmap = isl_basic_map_simplify(bmap);
4641 bmap = isl_basic_map_finalize(bmap);
4642 return bmap;
4643 error:
4644 isl_basic_map_free(like);
4645 isl_basic_set_free(bset);
4646 return NULL;
4649 struct isl_basic_set *isl_basic_set_from_underlying_set(
4650 struct isl_basic_set *bset, struct isl_basic_set *like)
4652 return (struct isl_basic_set *)
4653 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4656 struct isl_set *isl_set_from_underlying_set(
4657 struct isl_set *set, struct isl_basic_set *like)
4659 int i;
4661 if (!set || !like)
4662 goto error;
4663 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4664 goto error);
4665 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4666 isl_basic_set_free(like);
4667 return set;
4669 set = isl_set_cow(set);
4670 if (!set)
4671 goto error;
4672 for (i = 0; i < set->n; ++i) {
4673 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4674 isl_basic_set_copy(like));
4675 if (!set->p[i])
4676 goto error;
4678 isl_space_free(set->dim);
4679 set->dim = isl_space_copy(like->dim);
4680 if (!set->dim)
4681 goto error;
4682 isl_basic_set_free(like);
4683 return set;
4684 error:
4685 isl_basic_set_free(like);
4686 isl_set_free(set);
4687 return NULL;
4690 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4692 int i;
4694 map = isl_map_cow(map);
4695 if (!map)
4696 return NULL;
4697 map->dim = isl_space_cow(map->dim);
4698 if (!map->dim)
4699 goto error;
4701 for (i = 1; i < map->n; ++i)
4702 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4703 goto error);
4704 for (i = 0; i < map->n; ++i) {
4705 map->p[i] = (struct isl_basic_map *)
4706 isl_basic_map_underlying_set(map->p[i]);
4707 if (!map->p[i])
4708 goto error;
4710 if (map->n == 0)
4711 map->dim = isl_space_underlying(map->dim, 0);
4712 else {
4713 isl_space_free(map->dim);
4714 map->dim = isl_space_copy(map->p[0]->dim);
4716 if (!map->dim)
4717 goto error;
4718 return (struct isl_set *)map;
4719 error:
4720 isl_map_free(map);
4721 return NULL;
4724 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4726 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4729 __isl_give isl_basic_map *isl_basic_map_reset_space(
4730 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4732 isl_bool equal;
4734 if (!bmap)
4735 goto error;
4736 equal = isl_space_is_equal(bmap->dim, space);
4737 if (equal < 0)
4738 goto error;
4739 if (equal) {
4740 isl_space_free(space);
4741 return bmap;
4743 bmap = isl_basic_map_cow(bmap);
4744 if (!bmap || !space)
4745 goto error;
4747 isl_space_free(bmap->dim);
4748 bmap->dim = space;
4750 bmap = isl_basic_map_finalize(bmap);
4752 return bmap;
4753 error:
4754 isl_basic_map_free(bmap);
4755 isl_space_free(space);
4756 return NULL;
4759 __isl_give isl_basic_set *isl_basic_set_reset_space(
4760 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4762 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4763 dim);
4766 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4767 __isl_take isl_space *dim)
4769 int i;
4771 map = isl_map_cow(map);
4772 if (!map || !dim)
4773 goto error;
4775 for (i = 0; i < map->n; ++i) {
4776 map->p[i] = isl_basic_map_reset_space(map->p[i],
4777 isl_space_copy(dim));
4778 if (!map->p[i])
4779 goto error;
4781 isl_space_free(map->dim);
4782 map->dim = dim;
4784 return map;
4785 error:
4786 isl_map_free(map);
4787 isl_space_free(dim);
4788 return NULL;
4791 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4792 __isl_take isl_space *dim)
4794 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4797 /* Compute the parameter domain of the given basic set.
4799 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4801 isl_space *space;
4802 unsigned n;
4804 if (isl_basic_set_is_params(bset))
4805 return bset;
4807 n = isl_basic_set_dim(bset, isl_dim_set);
4808 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4809 space = isl_basic_set_get_space(bset);
4810 space = isl_space_params(space);
4811 bset = isl_basic_set_reset_space(bset, space);
4812 return bset;
4815 /* Construct a zero-dimensional basic set with the given parameter domain.
4817 __isl_give isl_basic_set *isl_basic_set_from_params(
4818 __isl_take isl_basic_set *bset)
4820 isl_space *space;
4821 space = isl_basic_set_get_space(bset);
4822 space = isl_space_set_from_params(space);
4823 bset = isl_basic_set_reset_space(bset, space);
4824 return bset;
4827 /* Compute the parameter domain of the given set.
4829 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4831 isl_space *space;
4832 unsigned n;
4834 if (isl_set_is_params(set))
4835 return set;
4837 n = isl_set_dim(set, isl_dim_set);
4838 set = isl_set_project_out(set, isl_dim_set, 0, n);
4839 space = isl_set_get_space(set);
4840 space = isl_space_params(space);
4841 set = isl_set_reset_space(set, space);
4842 return set;
4845 /* Construct a zero-dimensional set with the given parameter domain.
4847 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4849 isl_space *space;
4850 space = isl_set_get_space(set);
4851 space = isl_space_set_from_params(space);
4852 set = isl_set_reset_space(set, space);
4853 return set;
4856 /* Compute the parameter domain of the given map.
4858 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4860 isl_space *space;
4861 unsigned n;
4863 n = isl_map_dim(map, isl_dim_in);
4864 map = isl_map_project_out(map, isl_dim_in, 0, n);
4865 n = isl_map_dim(map, isl_dim_out);
4866 map = isl_map_project_out(map, isl_dim_out, 0, n);
4867 space = isl_map_get_space(map);
4868 space = isl_space_params(space);
4869 map = isl_map_reset_space(map, space);
4870 return map;
4873 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4875 isl_space *space;
4876 unsigned n_out;
4878 if (!bmap)
4879 return NULL;
4880 space = isl_space_domain(isl_basic_map_get_space(bmap));
4882 n_out = isl_basic_map_n_out(bmap);
4883 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
4885 return isl_basic_map_reset_space(bmap, space);
4888 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4890 if (!bmap)
4891 return -1;
4892 return isl_space_may_be_set(bmap->dim);
4895 /* Is this basic map actually a set?
4896 * Users should never call this function. Outside of isl,
4897 * the type should indicate whether something is a set or a map.
4899 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4901 if (!bmap)
4902 return -1;
4903 return isl_space_is_set(bmap->dim);
4906 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4908 if (!bmap)
4909 return NULL;
4910 if (isl_basic_map_is_set(bmap))
4911 return bmap;
4912 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4915 __isl_give isl_basic_map *isl_basic_map_domain_map(
4916 __isl_take isl_basic_map *bmap)
4918 int i, k;
4919 isl_space *dim;
4920 isl_basic_map *domain;
4921 int nparam, n_in, n_out;
4922 unsigned total;
4924 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4925 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4926 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4928 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4929 domain = isl_basic_map_universe(dim);
4931 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4932 bmap = isl_basic_map_apply_range(bmap, domain);
4933 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4935 total = isl_basic_map_total_dim(bmap);
4937 for (i = 0; i < n_in; ++i) {
4938 k = isl_basic_map_alloc_equality(bmap);
4939 if (k < 0)
4940 goto error;
4941 isl_seq_clr(bmap->eq[k], 1 + total);
4942 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4943 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4946 bmap = isl_basic_map_gauss(bmap, NULL);
4947 return isl_basic_map_finalize(bmap);
4948 error:
4949 isl_basic_map_free(bmap);
4950 return NULL;
4953 __isl_give isl_basic_map *isl_basic_map_range_map(
4954 __isl_take isl_basic_map *bmap)
4956 int i, k;
4957 isl_space *dim;
4958 isl_basic_map *range;
4959 int nparam, n_in, n_out;
4960 unsigned total;
4962 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4963 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4964 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4966 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4967 range = isl_basic_map_universe(dim);
4969 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4970 bmap = isl_basic_map_apply_range(bmap, range);
4971 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4973 total = isl_basic_map_total_dim(bmap);
4975 for (i = 0; i < n_out; ++i) {
4976 k = isl_basic_map_alloc_equality(bmap);
4977 if (k < 0)
4978 goto error;
4979 isl_seq_clr(bmap->eq[k], 1 + total);
4980 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4981 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4984 bmap = isl_basic_map_gauss(bmap, NULL);
4985 return isl_basic_map_finalize(bmap);
4986 error:
4987 isl_basic_map_free(bmap);
4988 return NULL;
4991 int isl_map_may_be_set(__isl_keep isl_map *map)
4993 if (!map)
4994 return -1;
4995 return isl_space_may_be_set(map->dim);
4998 /* Is this map actually a set?
4999 * Users should never call this function. Outside of isl,
5000 * the type should indicate whether something is a set or a map.
5002 int isl_map_is_set(__isl_keep isl_map *map)
5004 if (!map)
5005 return -1;
5006 return isl_space_is_set(map->dim);
5009 struct isl_set *isl_map_range(struct isl_map *map)
5011 int i;
5012 struct isl_set *set;
5014 if (!map)
5015 goto error;
5016 if (isl_map_is_set(map))
5017 return (isl_set *)map;
5019 map = isl_map_cow(map);
5020 if (!map)
5021 goto error;
5023 set = (struct isl_set *) map;
5024 set->dim = isl_space_range(set->dim);
5025 if (!set->dim)
5026 goto error;
5027 for (i = 0; i < map->n; ++i) {
5028 set->p[i] = isl_basic_map_range(map->p[i]);
5029 if (!set->p[i])
5030 goto error;
5032 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5033 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5034 return set;
5035 error:
5036 isl_map_free(map);
5037 return NULL;
5040 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5042 int i;
5044 map = isl_map_cow(map);
5045 if (!map)
5046 return NULL;
5048 map->dim = isl_space_domain_map(map->dim);
5049 if (!map->dim)
5050 goto error;
5051 for (i = 0; i < map->n; ++i) {
5052 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5053 if (!map->p[i])
5054 goto error;
5056 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5057 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5058 return map;
5059 error:
5060 isl_map_free(map);
5061 return NULL;
5064 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5066 int i;
5067 isl_space *range_dim;
5069 map = isl_map_cow(map);
5070 if (!map)
5071 return NULL;
5073 range_dim = isl_space_range(isl_map_get_space(map));
5074 range_dim = isl_space_from_range(range_dim);
5075 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5076 map->dim = isl_space_join(map->dim, range_dim);
5077 if (!map->dim)
5078 goto error;
5079 for (i = 0; i < map->n; ++i) {
5080 map->p[i] = isl_basic_map_range_map(map->p[i]);
5081 if (!map->p[i])
5082 goto error;
5084 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5085 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5086 return map;
5087 error:
5088 isl_map_free(map);
5089 return NULL;
5092 /* Given a wrapped map of the form A[B -> C],
5093 * return the map A[B -> C] -> B.
5095 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5097 isl_id *id;
5098 isl_map *map;
5100 if (!set)
5101 return NULL;
5102 if (!isl_set_has_tuple_id(set))
5103 return isl_map_domain_map(isl_set_unwrap(set));
5105 id = isl_set_get_tuple_id(set);
5106 map = isl_map_domain_map(isl_set_unwrap(set));
5107 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5109 return map;
5112 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5113 __isl_take isl_space *dim)
5115 int i;
5116 struct isl_map *map = NULL;
5118 set = isl_set_cow(set);
5119 if (!set || !dim)
5120 goto error;
5121 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5122 map = (struct isl_map *)set;
5123 for (i = 0; i < set->n; ++i) {
5124 map->p[i] = isl_basic_map_from_basic_set(
5125 set->p[i], isl_space_copy(dim));
5126 if (!map->p[i])
5127 goto error;
5129 isl_space_free(map->dim);
5130 map->dim = dim;
5131 return map;
5132 error:
5133 isl_space_free(dim);
5134 isl_set_free(set);
5135 return NULL;
5138 __isl_give isl_basic_map *isl_basic_map_from_domain(
5139 __isl_take isl_basic_set *bset)
5141 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5144 __isl_give isl_basic_map *isl_basic_map_from_range(
5145 __isl_take isl_basic_set *bset)
5147 isl_space *space;
5148 space = isl_basic_set_get_space(bset);
5149 space = isl_space_from_range(space);
5150 bset = isl_basic_set_reset_space(bset, space);
5151 return (isl_basic_map *)bset;
5154 /* Create a relation with the given set as range.
5155 * The domain of the created relation is a zero-dimensional
5156 * flat anonymous space.
5158 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5160 isl_space *space;
5161 space = isl_set_get_space(set);
5162 space = isl_space_from_range(space);
5163 set = isl_set_reset_space(set, space);
5164 return (struct isl_map *)set;
5167 /* Create a relation with the given set as domain.
5168 * The range of the created relation is a zero-dimensional
5169 * flat anonymous space.
5171 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5173 return isl_map_reverse(isl_map_from_range(set));
5176 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5177 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5179 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5182 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5183 __isl_take isl_set *range)
5185 return isl_map_apply_range(isl_map_reverse(domain), range);
5188 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5189 unsigned flags)
5191 struct isl_map *map;
5193 if (!dim)
5194 return NULL;
5195 if (n < 0)
5196 isl_die(dim->ctx, isl_error_internal,
5197 "negative number of basic maps", goto error);
5198 map = isl_alloc(dim->ctx, struct isl_map,
5199 sizeof(struct isl_map) +
5200 (n - 1) * sizeof(struct isl_basic_map *));
5201 if (!map)
5202 goto error;
5204 map->ctx = dim->ctx;
5205 isl_ctx_ref(map->ctx);
5206 map->ref = 1;
5207 map->size = n;
5208 map->n = 0;
5209 map->dim = dim;
5210 map->flags = flags;
5211 return map;
5212 error:
5213 isl_space_free(dim);
5214 return NULL;
5217 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5218 unsigned nparam, unsigned in, unsigned out, int n,
5219 unsigned flags)
5221 struct isl_map *map;
5222 isl_space *dims;
5224 dims = isl_space_alloc(ctx, nparam, in, out);
5225 if (!dims)
5226 return NULL;
5228 map = isl_map_alloc_space(dims, n, flags);
5229 return map;
5232 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5234 struct isl_basic_map *bmap;
5235 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5236 bmap = isl_basic_map_set_to_empty(bmap);
5237 return bmap;
5240 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5242 struct isl_basic_set *bset;
5243 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5244 bset = isl_basic_set_set_to_empty(bset);
5245 return bset;
5248 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5250 struct isl_basic_map *bmap;
5251 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5252 bmap = isl_basic_map_finalize(bmap);
5253 return bmap;
5256 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5258 struct isl_basic_set *bset;
5259 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5260 bset = isl_basic_set_finalize(bset);
5261 return bset;
5264 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5266 int i;
5267 unsigned total = isl_space_dim(dim, isl_dim_all);
5268 isl_basic_map *bmap;
5270 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5271 for (i = 0; i < total; ++i) {
5272 int k = isl_basic_map_alloc_inequality(bmap);
5273 if (k < 0)
5274 goto error;
5275 isl_seq_clr(bmap->ineq[k], 1 + total);
5276 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5278 return bmap;
5279 error:
5280 isl_basic_map_free(bmap);
5281 return NULL;
5284 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5286 return isl_basic_map_nat_universe(dim);
5289 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5291 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5294 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5296 return isl_map_nat_universe(dim);
5299 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5301 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5304 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5306 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5309 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5311 struct isl_map *map;
5312 if (!dim)
5313 return NULL;
5314 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5315 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5316 return map;
5319 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5321 struct isl_set *set;
5322 if (!dim)
5323 return NULL;
5324 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5325 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5326 return set;
5329 struct isl_map *isl_map_dup(struct isl_map *map)
5331 int i;
5332 struct isl_map *dup;
5334 if (!map)
5335 return NULL;
5336 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5337 for (i = 0; i < map->n; ++i)
5338 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5339 return dup;
5342 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5343 __isl_take isl_basic_map *bmap)
5345 if (!bmap || !map)
5346 goto error;
5347 if (isl_basic_map_plain_is_empty(bmap)) {
5348 isl_basic_map_free(bmap);
5349 return map;
5351 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5352 isl_assert(map->ctx, map->n < map->size, goto error);
5353 map->p[map->n] = bmap;
5354 map->n++;
5355 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5356 return map;
5357 error:
5358 if (map)
5359 isl_map_free(map);
5360 if (bmap)
5361 isl_basic_map_free(bmap);
5362 return NULL;
5365 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5367 int i;
5369 if (!map)
5370 return NULL;
5372 if (--map->ref > 0)
5373 return NULL;
5375 isl_ctx_deref(map->ctx);
5376 for (i = 0; i < map->n; ++i)
5377 isl_basic_map_free(map->p[i]);
5378 isl_space_free(map->dim);
5379 free(map);
5381 return NULL;
5384 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5385 struct isl_basic_map *bmap, unsigned pos, int value)
5387 int j;
5389 bmap = isl_basic_map_cow(bmap);
5390 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5391 j = isl_basic_map_alloc_equality(bmap);
5392 if (j < 0)
5393 goto error;
5394 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5395 isl_int_set_si(bmap->eq[j][pos], -1);
5396 isl_int_set_si(bmap->eq[j][0], value);
5397 bmap = isl_basic_map_simplify(bmap);
5398 return isl_basic_map_finalize(bmap);
5399 error:
5400 isl_basic_map_free(bmap);
5401 return NULL;
5404 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5405 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5407 int j;
5409 bmap = isl_basic_map_cow(bmap);
5410 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5411 j = isl_basic_map_alloc_equality(bmap);
5412 if (j < 0)
5413 goto error;
5414 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5415 isl_int_set_si(bmap->eq[j][pos], -1);
5416 isl_int_set(bmap->eq[j][0], value);
5417 bmap = isl_basic_map_simplify(bmap);
5418 return isl_basic_map_finalize(bmap);
5419 error:
5420 isl_basic_map_free(bmap);
5421 return NULL;
5424 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5425 enum isl_dim_type type, unsigned pos, int value)
5427 if (!bmap)
5428 return NULL;
5429 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5430 return isl_basic_map_fix_pos_si(bmap,
5431 isl_basic_map_offset(bmap, type) + pos, value);
5432 error:
5433 isl_basic_map_free(bmap);
5434 return NULL;
5437 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5438 enum isl_dim_type type, unsigned pos, isl_int value)
5440 if (!bmap)
5441 return NULL;
5442 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5443 return isl_basic_map_fix_pos(bmap,
5444 isl_basic_map_offset(bmap, type) + pos, value);
5445 error:
5446 isl_basic_map_free(bmap);
5447 return NULL;
5450 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5451 * to be equal to "v".
5453 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5454 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5456 if (!bmap || !v)
5457 goto error;
5458 if (!isl_val_is_int(v))
5459 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5460 "expecting integer value", goto error);
5461 if (pos >= isl_basic_map_dim(bmap, type))
5462 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5463 "index out of bounds", goto error);
5464 pos += isl_basic_map_offset(bmap, type);
5465 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5466 isl_val_free(v);
5467 return bmap;
5468 error:
5469 isl_basic_map_free(bmap);
5470 isl_val_free(v);
5471 return NULL;
5474 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5475 * to be equal to "v".
5477 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5478 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5480 return isl_basic_map_fix_val(bset, type, pos, v);
5483 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5484 enum isl_dim_type type, unsigned pos, int value)
5486 return (struct isl_basic_set *)
5487 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5488 type, pos, value);
5491 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5492 enum isl_dim_type type, unsigned pos, isl_int value)
5494 return (struct isl_basic_set *)
5495 isl_basic_map_fix((struct isl_basic_map *)bset,
5496 type, pos, value);
5499 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5500 unsigned input, int value)
5502 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5505 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5506 unsigned dim, int value)
5508 return (struct isl_basic_set *)
5509 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5510 isl_dim_set, dim, value);
5513 static int remove_if_empty(__isl_keep isl_map *map, int i)
5515 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5517 if (empty < 0)
5518 return -1;
5519 if (!empty)
5520 return 0;
5522 isl_basic_map_free(map->p[i]);
5523 if (i != map->n - 1) {
5524 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5525 map->p[i] = map->p[map->n - 1];
5527 map->n--;
5529 return 0;
5532 /* Perform "fn" on each basic map of "map", where we may not be holding
5533 * the only reference to "map".
5534 * In particular, "fn" should be a semantics preserving operation
5535 * that we want to apply to all copies of "map". We therefore need
5536 * to be careful not to modify "map" in a way that breaks "map"
5537 * in case anything goes wrong.
5539 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5540 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5542 struct isl_basic_map *bmap;
5543 int i;
5545 if (!map)
5546 return NULL;
5548 for (i = map->n - 1; i >= 0; --i) {
5549 bmap = isl_basic_map_copy(map->p[i]);
5550 bmap = fn(bmap);
5551 if (!bmap)
5552 goto error;
5553 isl_basic_map_free(map->p[i]);
5554 map->p[i] = bmap;
5555 if (remove_if_empty(map, i) < 0)
5556 goto error;
5559 return map;
5560 error:
5561 isl_map_free(map);
5562 return NULL;
5565 struct isl_map *isl_map_fix_si(struct isl_map *map,
5566 enum isl_dim_type type, unsigned pos, int value)
5568 int i;
5570 map = isl_map_cow(map);
5571 if (!map)
5572 return NULL;
5574 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5575 for (i = map->n - 1; i >= 0; --i) {
5576 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5577 if (remove_if_empty(map, i) < 0)
5578 goto error;
5580 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5581 return map;
5582 error:
5583 isl_map_free(map);
5584 return NULL;
5587 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5588 enum isl_dim_type type, unsigned pos, int value)
5590 return (struct isl_set *)
5591 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5594 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5595 enum isl_dim_type type, unsigned pos, isl_int value)
5597 int i;
5599 map = isl_map_cow(map);
5600 if (!map)
5601 return NULL;
5603 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5604 for (i = 0; i < map->n; ++i) {
5605 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5606 if (!map->p[i])
5607 goto error;
5609 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5610 return map;
5611 error:
5612 isl_map_free(map);
5613 return NULL;
5616 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5617 enum isl_dim_type type, unsigned pos, isl_int value)
5619 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5622 /* Fix the value of the variable at position "pos" of type "type" of "map"
5623 * to be equal to "v".
5625 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5626 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5628 int i;
5630 map = isl_map_cow(map);
5631 if (!map || !v)
5632 goto error;
5634 if (!isl_val_is_int(v))
5635 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5636 "expecting integer value", goto error);
5637 if (pos >= isl_map_dim(map, type))
5638 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5639 "index out of bounds", goto error);
5640 for (i = map->n - 1; i >= 0; --i) {
5641 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5642 isl_val_copy(v));
5643 if (remove_if_empty(map, i) < 0)
5644 goto error;
5646 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5647 isl_val_free(v);
5648 return map;
5649 error:
5650 isl_map_free(map);
5651 isl_val_free(v);
5652 return NULL;
5655 /* Fix the value of the variable at position "pos" of type "type" of "set"
5656 * to be equal to "v".
5658 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5659 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5661 return isl_map_fix_val(set, type, pos, v);
5664 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5665 unsigned input, int value)
5667 return isl_map_fix_si(map, isl_dim_in, input, value);
5670 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5672 return (struct isl_set *)
5673 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5676 static __isl_give isl_basic_map *basic_map_bound_si(
5677 __isl_take isl_basic_map *bmap,
5678 enum isl_dim_type type, unsigned pos, int value, int upper)
5680 int j;
5682 if (!bmap)
5683 return NULL;
5684 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5685 pos += isl_basic_map_offset(bmap, type);
5686 bmap = isl_basic_map_cow(bmap);
5687 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5688 j = isl_basic_map_alloc_inequality(bmap);
5689 if (j < 0)
5690 goto error;
5691 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5692 if (upper) {
5693 isl_int_set_si(bmap->ineq[j][pos], -1);
5694 isl_int_set_si(bmap->ineq[j][0], value);
5695 } else {
5696 isl_int_set_si(bmap->ineq[j][pos], 1);
5697 isl_int_set_si(bmap->ineq[j][0], -value);
5699 bmap = isl_basic_map_simplify(bmap);
5700 return isl_basic_map_finalize(bmap);
5701 error:
5702 isl_basic_map_free(bmap);
5703 return NULL;
5706 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5707 __isl_take isl_basic_map *bmap,
5708 enum isl_dim_type type, unsigned pos, int value)
5710 return basic_map_bound_si(bmap, type, pos, value, 0);
5713 /* Constrain the values of the given dimension to be no greater than "value".
5715 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5716 __isl_take isl_basic_map *bmap,
5717 enum isl_dim_type type, unsigned pos, int value)
5719 return basic_map_bound_si(bmap, type, pos, value, 1);
5722 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5723 unsigned dim, isl_int value)
5725 int j;
5727 bset = isl_basic_set_cow(bset);
5728 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5729 j = isl_basic_set_alloc_inequality(bset);
5730 if (j < 0)
5731 goto error;
5732 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5733 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5734 isl_int_neg(bset->ineq[j][0], value);
5735 bset = isl_basic_set_simplify(bset);
5736 return isl_basic_set_finalize(bset);
5737 error:
5738 isl_basic_set_free(bset);
5739 return NULL;
5742 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5743 enum isl_dim_type type, unsigned pos, int value, int upper)
5745 int i;
5747 map = isl_map_cow(map);
5748 if (!map)
5749 return NULL;
5751 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5752 for (i = 0; i < map->n; ++i) {
5753 map->p[i] = basic_map_bound_si(map->p[i],
5754 type, pos, value, upper);
5755 if (!map->p[i])
5756 goto error;
5758 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5759 return map;
5760 error:
5761 isl_map_free(map);
5762 return NULL;
5765 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5766 enum isl_dim_type type, unsigned pos, int value)
5768 return map_bound_si(map, type, pos, value, 0);
5771 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5772 enum isl_dim_type type, unsigned pos, int value)
5774 return map_bound_si(map, type, pos, value, 1);
5777 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5778 enum isl_dim_type type, unsigned pos, int value)
5780 return (struct isl_set *)
5781 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5784 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5785 enum isl_dim_type type, unsigned pos, int value)
5787 return isl_map_upper_bound_si(set, type, pos, value);
5790 /* Bound the given variable of "bmap" from below (or above is "upper"
5791 * is set) to "value".
5793 static __isl_give isl_basic_map *basic_map_bound(
5794 __isl_take isl_basic_map *bmap,
5795 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5797 int j;
5799 if (!bmap)
5800 return NULL;
5801 if (pos >= isl_basic_map_dim(bmap, type))
5802 isl_die(bmap->ctx, isl_error_invalid,
5803 "index out of bounds", goto error);
5804 pos += isl_basic_map_offset(bmap, type);
5805 bmap = isl_basic_map_cow(bmap);
5806 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5807 j = isl_basic_map_alloc_inequality(bmap);
5808 if (j < 0)
5809 goto error;
5810 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5811 if (upper) {
5812 isl_int_set_si(bmap->ineq[j][pos], -1);
5813 isl_int_set(bmap->ineq[j][0], value);
5814 } else {
5815 isl_int_set_si(bmap->ineq[j][pos], 1);
5816 isl_int_neg(bmap->ineq[j][0], value);
5818 bmap = isl_basic_map_simplify(bmap);
5819 return isl_basic_map_finalize(bmap);
5820 error:
5821 isl_basic_map_free(bmap);
5822 return NULL;
5825 /* Bound the given variable of "map" from below (or above is "upper"
5826 * is set) to "value".
5828 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5829 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5831 int i;
5833 map = isl_map_cow(map);
5834 if (!map)
5835 return NULL;
5837 if (pos >= isl_map_dim(map, type))
5838 isl_die(map->ctx, isl_error_invalid,
5839 "index out of bounds", goto error);
5840 for (i = map->n - 1; i >= 0; --i) {
5841 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5842 if (remove_if_empty(map, i) < 0)
5843 goto error;
5845 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5846 return map;
5847 error:
5848 isl_map_free(map);
5849 return NULL;
5852 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5853 enum isl_dim_type type, unsigned pos, isl_int value)
5855 return map_bound(map, type, pos, value, 0);
5858 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5859 enum isl_dim_type type, unsigned pos, isl_int value)
5861 return map_bound(map, type, pos, value, 1);
5864 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5865 enum isl_dim_type type, unsigned pos, isl_int value)
5867 return isl_map_lower_bound(set, type, pos, value);
5870 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5871 enum isl_dim_type type, unsigned pos, isl_int value)
5873 return isl_map_upper_bound(set, type, pos, value);
5876 /* Force the values of the variable at position "pos" of type "type" of "set"
5877 * to be no smaller than "value".
5879 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5880 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5882 if (!value)
5883 goto error;
5884 if (!isl_val_is_int(value))
5885 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5886 "expecting integer value", goto error);
5887 set = isl_set_lower_bound(set, type, pos, value->n);
5888 isl_val_free(value);
5889 return set;
5890 error:
5891 isl_val_free(value);
5892 isl_set_free(set);
5893 return NULL;
5896 /* Force the values of the variable at position "pos" of type "type" of "set"
5897 * to be no greater than "value".
5899 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5900 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5902 if (!value)
5903 goto error;
5904 if (!isl_val_is_int(value))
5905 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5906 "expecting integer value", goto error);
5907 set = isl_set_upper_bound(set, type, pos, value->n);
5908 isl_val_free(value);
5909 return set;
5910 error:
5911 isl_val_free(value);
5912 isl_set_free(set);
5913 return NULL;
5916 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5917 isl_int value)
5919 int i;
5921 set = isl_set_cow(set);
5922 if (!set)
5923 return NULL;
5925 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5926 for (i = 0; i < set->n; ++i) {
5927 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5928 if (!set->p[i])
5929 goto error;
5931 return set;
5932 error:
5933 isl_set_free(set);
5934 return NULL;
5937 struct isl_map *isl_map_reverse(struct isl_map *map)
5939 int i;
5941 map = isl_map_cow(map);
5942 if (!map)
5943 return NULL;
5945 map->dim = isl_space_reverse(map->dim);
5946 if (!map->dim)
5947 goto error;
5948 for (i = 0; i < map->n; ++i) {
5949 map->p[i] = isl_basic_map_reverse(map->p[i]);
5950 if (!map->p[i])
5951 goto error;
5953 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5954 return map;
5955 error:
5956 isl_map_free(map);
5957 return NULL;
5960 static struct isl_map *isl_basic_map_partial_lexopt(
5961 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5962 struct isl_set **empty, int max)
5964 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5967 struct isl_map *isl_basic_map_partial_lexmax(
5968 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5969 struct isl_set **empty)
5971 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5974 struct isl_map *isl_basic_map_partial_lexmin(
5975 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5976 struct isl_set **empty)
5978 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5981 struct isl_set *isl_basic_set_partial_lexmin(
5982 struct isl_basic_set *bset, struct isl_basic_set *dom,
5983 struct isl_set **empty)
5985 return (struct isl_set *)
5986 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5987 dom, empty);
5990 struct isl_set *isl_basic_set_partial_lexmax(
5991 struct isl_basic_set *bset, struct isl_basic_set *dom,
5992 struct isl_set **empty)
5994 return (struct isl_set *)
5995 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5996 dom, empty);
5999 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6000 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6001 __isl_give isl_set **empty)
6003 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6006 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6007 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6008 __isl_give isl_set **empty)
6010 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6013 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6014 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6015 __isl_give isl_set **empty)
6017 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6020 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6021 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6022 __isl_give isl_set **empty)
6024 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6027 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6028 __isl_take isl_basic_map *bmap, int max)
6030 isl_basic_set *dom = NULL;
6031 isl_space *dom_space;
6033 if (!bmap)
6034 goto error;
6035 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6036 dom = isl_basic_set_universe(dom_space);
6037 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6038 error:
6039 isl_basic_map_free(bmap);
6040 return NULL;
6043 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6044 __isl_take isl_basic_map *bmap)
6046 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6049 #undef TYPE
6050 #define TYPE isl_pw_multi_aff
6051 #undef SUFFIX
6052 #define SUFFIX _pw_multi_aff
6053 #undef EMPTY
6054 #define EMPTY isl_pw_multi_aff_empty
6055 #undef ADD
6056 #define ADD isl_pw_multi_aff_union_add
6057 #include "isl_map_lexopt_templ.c"
6059 /* Given a map "map", compute the lexicographically minimal
6060 * (or maximal) image element for each domain element in dom,
6061 * in the form of an isl_pw_multi_aff.
6062 * Set *empty to those elements in dom that do not have an image element.
6064 * We first compute the lexicographically minimal or maximal element
6065 * in the first basic map. This results in a partial solution "res"
6066 * and a subset "todo" of dom that still need to be handled.
6067 * We then consider each of the remaining maps in "map" and successively
6068 * update both "res" and "todo".
6070 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6071 __isl_take isl_map *map, __isl_take isl_set *dom,
6072 __isl_give isl_set **empty, int max)
6074 int i;
6075 isl_pw_multi_aff *res;
6076 isl_set *todo;
6078 if (!map || !dom)
6079 goto error;
6081 if (isl_map_plain_is_empty(map)) {
6082 if (empty)
6083 *empty = dom;
6084 else
6085 isl_set_free(dom);
6086 return isl_pw_multi_aff_from_map(map);
6089 res = basic_map_partial_lexopt_pw_multi_aff(
6090 isl_basic_map_copy(map->p[0]),
6091 isl_set_copy(dom), &todo, max);
6093 for (i = 1; i < map->n; ++i) {
6094 isl_pw_multi_aff *res_i;
6095 isl_set *todo_i;
6097 res_i = basic_map_partial_lexopt_pw_multi_aff(
6098 isl_basic_map_copy(map->p[i]),
6099 isl_set_copy(dom), &todo_i, max);
6101 if (max)
6102 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6103 else
6104 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6106 todo = isl_set_intersect(todo, todo_i);
6109 isl_set_free(dom);
6110 isl_map_free(map);
6112 if (empty)
6113 *empty = todo;
6114 else
6115 isl_set_free(todo);
6117 return res;
6118 error:
6119 if (empty)
6120 *empty = NULL;
6121 isl_set_free(dom);
6122 isl_map_free(map);
6123 return NULL;
6126 #undef TYPE
6127 #define TYPE isl_map
6128 #undef SUFFIX
6129 #define SUFFIX
6130 #undef EMPTY
6131 #define EMPTY isl_map_empty
6132 #undef ADD
6133 #define ADD isl_map_union_disjoint
6134 #include "isl_map_lexopt_templ.c"
6136 /* Given a map "map", compute the lexicographically minimal
6137 * (or maximal) image element for each domain element in dom.
6138 * Set *empty to those elements in dom that do not have an image element.
6140 * We first compute the lexicographically minimal or maximal element
6141 * in the first basic map. This results in a partial solution "res"
6142 * and a subset "todo" of dom that still need to be handled.
6143 * We then consider each of the remaining maps in "map" and successively
6144 * update both "res" and "todo".
6146 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6147 * Assume we are computing the lexicographical maximum.
6148 * We first compute the lexicographically maximal element in basic map i.
6149 * This results in a partial solution res_i and a subset todo_i.
6150 * Then we combine these results with those obtain for the first k basic maps
6151 * to obtain a result that is valid for the first k+1 basic maps.
6152 * In particular, the set where there is no solution is the set where
6153 * there is no solution for the first k basic maps and also no solution
6154 * for the ith basic map, i.e.,
6156 * todo^i = todo^k * todo_i
6158 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6159 * solutions, arbitrarily breaking ties in favor of res^k.
6160 * That is, when res^k(a) >= res_i(a), we pick res^k and
6161 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6162 * the lexicographic order.)
6163 * In practice, we compute
6165 * res^k * (res_i . "<=")
6167 * and
6169 * res_i * (res^k . "<")
6171 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6172 * where only one of res^k and res_i provides a solution and we simply pick
6173 * that one, i.e.,
6175 * res^k * todo_i
6176 * and
6177 * res_i * todo^k
6179 * Note that we only compute these intersections when dom(res^k) intersects
6180 * dom(res_i). Otherwise, the only effect of these intersections is to
6181 * potentially break up res^k and res_i into smaller pieces.
6182 * We want to avoid such splintering as much as possible.
6183 * In fact, an earlier implementation of this function would look for
6184 * better results in the domain of res^k and for extra results in todo^k,
6185 * but this would always result in a splintering according to todo^k,
6186 * even when the domain of basic map i is disjoint from the domains of
6187 * the previous basic maps.
6189 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6190 __isl_take isl_map *map, __isl_take isl_set *dom,
6191 __isl_give isl_set **empty, int max)
6193 int i;
6194 struct isl_map *res;
6195 struct isl_set *todo;
6197 if (!map || !dom)
6198 goto error;
6200 if (isl_map_plain_is_empty(map)) {
6201 if (empty)
6202 *empty = dom;
6203 else
6204 isl_set_free(dom);
6205 return map;
6208 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6209 isl_set_copy(dom), &todo, max);
6211 for (i = 1; i < map->n; ++i) {
6212 isl_map *lt, *le;
6213 isl_map *res_i;
6214 isl_set *todo_i;
6215 isl_space *dim = isl_space_range(isl_map_get_space(res));
6217 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6218 isl_set_copy(dom), &todo_i, max);
6220 if (max) {
6221 lt = isl_map_lex_lt(isl_space_copy(dim));
6222 le = isl_map_lex_le(dim);
6223 } else {
6224 lt = isl_map_lex_gt(isl_space_copy(dim));
6225 le = isl_map_lex_ge(dim);
6227 lt = isl_map_apply_range(isl_map_copy(res), lt);
6228 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6229 le = isl_map_apply_range(isl_map_copy(res_i), le);
6230 le = isl_map_intersect(le, isl_map_copy(res));
6232 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6233 res = isl_map_intersect_domain(res,
6234 isl_set_copy(todo_i));
6235 res_i = isl_map_intersect_domain(res_i,
6236 isl_set_copy(todo));
6239 res = isl_map_union_disjoint(res, res_i);
6240 res = isl_map_union_disjoint(res, lt);
6241 res = isl_map_union_disjoint(res, le);
6243 todo = isl_set_intersect(todo, todo_i);
6246 isl_set_free(dom);
6247 isl_map_free(map);
6249 if (empty)
6250 *empty = todo;
6251 else
6252 isl_set_free(todo);
6254 return res;
6255 error:
6256 if (empty)
6257 *empty = NULL;
6258 isl_set_free(dom);
6259 isl_map_free(map);
6260 return NULL;
6263 __isl_give isl_map *isl_map_partial_lexmax(
6264 __isl_take isl_map *map, __isl_take isl_set *dom,
6265 __isl_give isl_set **empty)
6267 return isl_map_partial_lexopt(map, dom, empty, 1);
6270 __isl_give isl_map *isl_map_partial_lexmin(
6271 __isl_take isl_map *map, __isl_take isl_set *dom,
6272 __isl_give isl_set **empty)
6274 return isl_map_partial_lexopt(map, dom, empty, 0);
6277 __isl_give isl_set *isl_set_partial_lexmin(
6278 __isl_take isl_set *set, __isl_take isl_set *dom,
6279 __isl_give isl_set **empty)
6281 return (struct isl_set *)
6282 isl_map_partial_lexmin((struct isl_map *)set,
6283 dom, empty);
6286 __isl_give isl_set *isl_set_partial_lexmax(
6287 __isl_take isl_set *set, __isl_take isl_set *dom,
6288 __isl_give isl_set **empty)
6290 return (struct isl_set *)
6291 isl_map_partial_lexmax((struct isl_map *)set,
6292 dom, empty);
6295 /* Compute the lexicographic minimum (or maximum if "max" is set)
6296 * of "bmap" over its domain.
6298 * Since we are not interested in the part of the domain space where
6299 * there is no solution, we initialize the domain to those constraints
6300 * of "bmap" that only involve the parameters and the input dimensions.
6301 * This relieves the parametric programming engine from detecting those
6302 * inequalities and transferring them to the context. More importantly,
6303 * it ensures that those inequalities are transferred first and not
6304 * intermixed with inequalities that actually split the domain.
6306 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6308 int n_div;
6309 int n_out;
6310 isl_basic_map *copy;
6311 isl_basic_set *dom;
6313 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6314 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6315 copy = isl_basic_map_copy(bmap);
6316 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6317 isl_dim_div, 0, n_div);
6318 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6319 isl_dim_out, 0, n_out);
6320 dom = isl_basic_map_domain(copy);
6321 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6324 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6326 return isl_basic_map_lexopt(bmap, 0);
6329 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6331 return isl_basic_map_lexopt(bmap, 1);
6334 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6336 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6339 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6341 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6344 /* Extract the first and only affine expression from list
6345 * and then add it to *pwaff with the given dom.
6346 * This domain is known to be disjoint from other domains
6347 * because of the way isl_basic_map_foreach_lexmax works.
6349 static int update_dim_opt(__isl_take isl_basic_set *dom,
6350 __isl_take isl_aff_list *list, void *user)
6352 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6353 isl_aff *aff;
6354 isl_pw_aff **pwaff = user;
6355 isl_pw_aff *pwaff_i;
6357 if (!list)
6358 goto error;
6359 if (isl_aff_list_n_aff(list) != 1)
6360 isl_die(ctx, isl_error_internal,
6361 "expecting single element list", goto error);
6363 aff = isl_aff_list_get_aff(list, 0);
6364 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6366 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6368 isl_aff_list_free(list);
6370 return 0;
6371 error:
6372 isl_basic_set_free(dom);
6373 isl_aff_list_free(list);
6374 return -1;
6377 /* Given a basic map with one output dimension, compute the minimum or
6378 * maximum of that dimension as an isl_pw_aff.
6380 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6381 * call update_dim_opt on each leaf of the result.
6383 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6384 int max)
6386 isl_space *dim = isl_basic_map_get_space(bmap);
6387 isl_pw_aff *pwaff;
6388 int r;
6390 dim = isl_space_from_domain(isl_space_domain(dim));
6391 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6392 pwaff = isl_pw_aff_empty(dim);
6394 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6395 if (r < 0)
6396 return isl_pw_aff_free(pwaff);
6398 return pwaff;
6401 /* Compute the minimum or maximum of the given output dimension
6402 * as a function of the parameters and the input dimensions,
6403 * but independently of the other output dimensions.
6405 * We first project out the other output dimension and then compute
6406 * the "lexicographic" maximum in each basic map, combining the results
6407 * using isl_pw_aff_union_max.
6409 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6410 int max)
6412 int i;
6413 isl_pw_aff *pwaff;
6414 unsigned n_out;
6416 n_out = isl_map_dim(map, isl_dim_out);
6417 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6418 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6419 if (!map)
6420 return NULL;
6422 if (map->n == 0) {
6423 isl_space *dim = isl_map_get_space(map);
6424 isl_map_free(map);
6425 return isl_pw_aff_empty(dim);
6428 pwaff = basic_map_dim_opt(map->p[0], max);
6429 for (i = 1; i < map->n; ++i) {
6430 isl_pw_aff *pwaff_i;
6432 pwaff_i = basic_map_dim_opt(map->p[i], max);
6433 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6436 isl_map_free(map);
6438 return pwaff;
6441 /* Compute the maximum of the given output dimension as a function of the
6442 * parameters and input dimensions, but independently of
6443 * the other output dimensions.
6445 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6447 return map_dim_opt(map, pos, 1);
6450 /* Compute the minimum or maximum of the given set dimension
6451 * as a function of the parameters,
6452 * but independently of the other set dimensions.
6454 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6455 int max)
6457 return map_dim_opt(set, pos, max);
6460 /* Compute the maximum of the given set dimension as a function of the
6461 * parameters, but independently of the other set dimensions.
6463 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6465 return set_dim_opt(set, pos, 1);
6468 /* Compute the minimum of the given set dimension as a function of the
6469 * parameters, but independently of the other set dimensions.
6471 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6473 return set_dim_opt(set, pos, 0);
6476 /* Apply a preimage specified by "mat" on the parameters of "bset".
6477 * bset is assumed to have only parameters and divs.
6479 static struct isl_basic_set *basic_set_parameter_preimage(
6480 struct isl_basic_set *bset, struct isl_mat *mat)
6482 unsigned nparam;
6484 if (!bset || !mat)
6485 goto error;
6487 bset->dim = isl_space_cow(bset->dim);
6488 if (!bset->dim)
6489 goto error;
6491 nparam = isl_basic_set_dim(bset, isl_dim_param);
6493 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6495 bset->dim->nparam = 0;
6496 bset->dim->n_out = nparam;
6497 bset = isl_basic_set_preimage(bset, mat);
6498 if (bset) {
6499 bset->dim->nparam = bset->dim->n_out;
6500 bset->dim->n_out = 0;
6502 return bset;
6503 error:
6504 isl_mat_free(mat);
6505 isl_basic_set_free(bset);
6506 return NULL;
6509 /* Apply a preimage specified by "mat" on the parameters of "set".
6510 * set is assumed to have only parameters and divs.
6512 static struct isl_set *set_parameter_preimage(
6513 struct isl_set *set, struct isl_mat *mat)
6515 isl_space *dim = NULL;
6516 unsigned nparam;
6518 if (!set || !mat)
6519 goto error;
6521 dim = isl_space_copy(set->dim);
6522 dim = isl_space_cow(dim);
6523 if (!dim)
6524 goto error;
6526 nparam = isl_set_dim(set, isl_dim_param);
6528 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6530 dim->nparam = 0;
6531 dim->n_out = nparam;
6532 isl_set_reset_space(set, dim);
6533 set = isl_set_preimage(set, mat);
6534 if (!set)
6535 goto error2;
6536 dim = isl_space_copy(set->dim);
6537 dim = isl_space_cow(dim);
6538 if (!dim)
6539 goto error2;
6540 dim->nparam = dim->n_out;
6541 dim->n_out = 0;
6542 isl_set_reset_space(set, dim);
6543 return set;
6544 error:
6545 isl_space_free(dim);
6546 isl_mat_free(mat);
6547 error2:
6548 isl_set_free(set);
6549 return NULL;
6552 /* Intersect the basic set "bset" with the affine space specified by the
6553 * equalities in "eq".
6555 static struct isl_basic_set *basic_set_append_equalities(
6556 struct isl_basic_set *bset, struct isl_mat *eq)
6558 int i, k;
6559 unsigned len;
6561 if (!bset || !eq)
6562 goto error;
6564 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6565 eq->n_row, 0);
6566 if (!bset)
6567 goto error;
6569 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6570 for (i = 0; i < eq->n_row; ++i) {
6571 k = isl_basic_set_alloc_equality(bset);
6572 if (k < 0)
6573 goto error;
6574 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6575 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6577 isl_mat_free(eq);
6579 bset = isl_basic_set_gauss(bset, NULL);
6580 bset = isl_basic_set_finalize(bset);
6582 return bset;
6583 error:
6584 isl_mat_free(eq);
6585 isl_basic_set_free(bset);
6586 return NULL;
6589 /* Intersect the set "set" with the affine space specified by the
6590 * equalities in "eq".
6592 static struct isl_set *set_append_equalities(struct isl_set *set,
6593 struct isl_mat *eq)
6595 int i;
6597 if (!set || !eq)
6598 goto error;
6600 for (i = 0; i < set->n; ++i) {
6601 set->p[i] = basic_set_append_equalities(set->p[i],
6602 isl_mat_copy(eq));
6603 if (!set->p[i])
6604 goto error;
6606 isl_mat_free(eq);
6607 return set;
6608 error:
6609 isl_mat_free(eq);
6610 isl_set_free(set);
6611 return NULL;
6614 /* Given a basic set "bset" that only involves parameters and existentially
6615 * quantified variables, return the index of the first equality
6616 * that only involves parameters. If there is no such equality then
6617 * return bset->n_eq.
6619 * This function assumes that isl_basic_set_gauss has been called on "bset".
6621 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6623 int i, j;
6624 unsigned nparam, n_div;
6626 if (!bset)
6627 return -1;
6629 nparam = isl_basic_set_dim(bset, isl_dim_param);
6630 n_div = isl_basic_set_dim(bset, isl_dim_div);
6632 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6633 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6634 ++i;
6637 return i;
6640 /* Compute an explicit representation for the existentially quantified
6641 * variables in "bset" by computing the "minimal value" of the set
6642 * variables. Since there are no set variables, the computation of
6643 * the minimal value essentially computes an explicit representation
6644 * of the non-empty part(s) of "bset".
6646 * The input only involves parameters and existentially quantified variables.
6647 * All equalities among parameters have been removed.
6649 * Since the existentially quantified variables in the result are in general
6650 * going to be different from those in the input, we first replace
6651 * them by the minimal number of variables based on their equalities.
6652 * This should simplify the parametric integer programming.
6654 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6656 isl_morph *morph1, *morph2;
6657 isl_set *set;
6658 unsigned n;
6660 if (!bset)
6661 return NULL;
6662 if (bset->n_eq == 0)
6663 return isl_basic_set_lexmin(bset);
6665 morph1 = isl_basic_set_parameter_compression(bset);
6666 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6667 bset = isl_basic_set_lift(bset);
6668 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6669 bset = isl_morph_basic_set(morph2, bset);
6670 n = isl_basic_set_dim(bset, isl_dim_set);
6671 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6673 set = isl_basic_set_lexmin(bset);
6675 set = isl_morph_set(isl_morph_inverse(morph1), set);
6677 return set;
6680 /* Project the given basic set onto its parameter domain, possibly introducing
6681 * new, explicit, existential variables in the constraints.
6682 * The input has parameters and (possibly implicit) existential variables.
6683 * The output has the same parameters, but only
6684 * explicit existentially quantified variables.
6686 * The actual projection is performed by pip, but pip doesn't seem
6687 * to like equalities very much, so we first remove the equalities
6688 * among the parameters by performing a variable compression on
6689 * the parameters. Afterward, an inverse transformation is performed
6690 * and the equalities among the parameters are inserted back in.
6692 * The variable compression on the parameters may uncover additional
6693 * equalities that were only implicit before. We therefore check
6694 * if there are any new parameter equalities in the result and
6695 * if so recurse. The removal of parameter equalities is required
6696 * for the parameter compression performed by base_compute_divs.
6698 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6700 int i;
6701 struct isl_mat *eq;
6702 struct isl_mat *T, *T2;
6703 struct isl_set *set;
6704 unsigned nparam;
6706 bset = isl_basic_set_cow(bset);
6707 if (!bset)
6708 return NULL;
6710 if (bset->n_eq == 0)
6711 return base_compute_divs(bset);
6713 bset = isl_basic_set_gauss(bset, NULL);
6714 if (!bset)
6715 return NULL;
6716 if (isl_basic_set_plain_is_empty(bset))
6717 return isl_set_from_basic_set(bset);
6719 i = first_parameter_equality(bset);
6720 if (i == bset->n_eq)
6721 return base_compute_divs(bset);
6723 nparam = isl_basic_set_dim(bset, isl_dim_param);
6724 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6725 0, 1 + nparam);
6726 eq = isl_mat_cow(eq);
6727 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6728 if (T && T->n_col == 0) {
6729 isl_mat_free(T);
6730 isl_mat_free(T2);
6731 isl_mat_free(eq);
6732 bset = isl_basic_set_set_to_empty(bset);
6733 return isl_set_from_basic_set(bset);
6735 bset = basic_set_parameter_preimage(bset, T);
6737 i = first_parameter_equality(bset);
6738 if (!bset)
6739 set = NULL;
6740 else if (i == bset->n_eq)
6741 set = base_compute_divs(bset);
6742 else
6743 set = parameter_compute_divs(bset);
6744 set = set_parameter_preimage(set, T2);
6745 set = set_append_equalities(set, eq);
6746 return set;
6749 /* Insert the divs from "ls" before those of "bmap".
6751 * The number of columns is not changed, which means that the last
6752 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6753 * The caller is responsible for removing the same number of dimensions
6754 * from the space of "bmap".
6756 static __isl_give isl_basic_map *insert_divs_from_local_space(
6757 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6759 int i;
6760 int n_div;
6761 int old_n_div;
6763 n_div = isl_local_space_dim(ls, isl_dim_div);
6764 if (n_div == 0)
6765 return bmap;
6767 old_n_div = bmap->n_div;
6768 bmap = insert_div_rows(bmap, n_div);
6769 if (!bmap)
6770 return NULL;
6772 for (i = 0; i < n_div; ++i) {
6773 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6774 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6777 return bmap;
6780 /* Replace the space of "bmap" by the space and divs of "ls".
6782 * If "ls" has any divs, then we simplify the result since we may
6783 * have discovered some additional equalities that could simplify
6784 * the div expressions.
6786 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6787 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6789 int n_div;
6791 bmap = isl_basic_map_cow(bmap);
6792 if (!bmap || !ls)
6793 goto error;
6795 n_div = isl_local_space_dim(ls, isl_dim_div);
6796 bmap = insert_divs_from_local_space(bmap, ls);
6797 if (!bmap)
6798 goto error;
6800 isl_space_free(bmap->dim);
6801 bmap->dim = isl_local_space_get_space(ls);
6802 if (!bmap->dim)
6803 goto error;
6805 isl_local_space_free(ls);
6806 if (n_div > 0)
6807 bmap = isl_basic_map_simplify(bmap);
6808 bmap = isl_basic_map_finalize(bmap);
6809 return bmap;
6810 error:
6811 isl_basic_map_free(bmap);
6812 isl_local_space_free(ls);
6813 return NULL;
6816 /* Replace the space of "map" by the space and divs of "ls".
6818 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6819 __isl_take isl_local_space *ls)
6821 int i;
6823 map = isl_map_cow(map);
6824 if (!map || !ls)
6825 goto error;
6827 for (i = 0; i < map->n; ++i) {
6828 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6829 isl_local_space_copy(ls));
6830 if (!map->p[i])
6831 goto error;
6833 isl_space_free(map->dim);
6834 map->dim = isl_local_space_get_space(ls);
6835 if (!map->dim)
6836 goto error;
6838 isl_local_space_free(ls);
6839 return map;
6840 error:
6841 isl_local_space_free(ls);
6842 isl_map_free(map);
6843 return NULL;
6846 /* Compute an explicit representation for the existentially
6847 * quantified variables for which do not know any explicit representation yet.
6849 * We first sort the existentially quantified variables so that the
6850 * existentially quantified variables for which we already have an explicit
6851 * representation are placed before those for which we do not.
6852 * The input dimensions, the output dimensions and the existentially
6853 * quantified variables for which we already have an explicit
6854 * representation are then turned into parameters.
6855 * compute_divs returns a map with the same parameters and
6856 * no input or output dimensions and the dimension specification
6857 * is reset to that of the input, including the existentially quantified
6858 * variables for which we already had an explicit representation.
6860 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6862 struct isl_basic_set *bset;
6863 struct isl_set *set;
6864 struct isl_map *map;
6865 isl_space *dim;
6866 isl_local_space *ls;
6867 unsigned nparam;
6868 unsigned n_in;
6869 unsigned n_out;
6870 unsigned n_known;
6871 int i;
6873 bmap = isl_basic_map_sort_divs(bmap);
6874 bmap = isl_basic_map_cow(bmap);
6875 if (!bmap)
6876 return NULL;
6878 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6879 if (isl_int_is_zero(bmap->div[n_known][0]))
6880 break;
6882 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6883 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6884 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6885 dim = isl_space_set_alloc(bmap->ctx,
6886 nparam + n_in + n_out + n_known, 0);
6887 if (!dim)
6888 goto error;
6890 ls = isl_basic_map_get_local_space(bmap);
6891 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6892 n_known, bmap->n_div - n_known);
6893 if (n_known > 0) {
6894 for (i = n_known; i < bmap->n_div; ++i)
6895 swap_div(bmap, i - n_known, i);
6896 bmap->n_div -= n_known;
6897 bmap->extra -= n_known;
6899 bmap = isl_basic_map_reset_space(bmap, dim);
6900 bset = (struct isl_basic_set *)bmap;
6902 set = parameter_compute_divs(bset);
6903 map = (struct isl_map *)set;
6904 map = replace_space_by_local_space(map, ls);
6906 return map;
6907 error:
6908 isl_basic_map_free(bmap);
6909 return NULL;
6912 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6914 int i;
6915 unsigned off;
6917 if (!bmap)
6918 return -1;
6920 off = isl_space_dim(bmap->dim, isl_dim_all);
6921 for (i = 0; i < bmap->n_div; ++i) {
6922 if (isl_int_is_zero(bmap->div[i][0]))
6923 return 0;
6924 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6925 return -1);
6927 return 1;
6930 static int map_divs_known(__isl_keep isl_map *map)
6932 int i;
6934 if (!map)
6935 return -1;
6937 for (i = 0; i < map->n; ++i) {
6938 int known = isl_basic_map_divs_known(map->p[i]);
6939 if (known <= 0)
6940 return known;
6943 return 1;
6946 /* If bmap contains any unknown divs, then compute explicit
6947 * expressions for them. However, this computation may be
6948 * quite expensive, so first try to remove divs that aren't
6949 * strictly needed.
6951 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6953 int known;
6954 struct isl_map *map;
6956 known = isl_basic_map_divs_known(bmap);
6957 if (known < 0)
6958 goto error;
6959 if (known)
6960 return isl_map_from_basic_map(bmap);
6962 bmap = isl_basic_map_drop_redundant_divs(bmap);
6964 known = isl_basic_map_divs_known(bmap);
6965 if (known < 0)
6966 goto error;
6967 if (known)
6968 return isl_map_from_basic_map(bmap);
6970 map = compute_divs(bmap);
6971 return map;
6972 error:
6973 isl_basic_map_free(bmap);
6974 return NULL;
6977 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6979 int i;
6980 int known;
6981 struct isl_map *res;
6983 if (!map)
6984 return NULL;
6985 if (map->n == 0)
6986 return map;
6988 known = map_divs_known(map);
6989 if (known < 0) {
6990 isl_map_free(map);
6991 return NULL;
6993 if (known)
6994 return map;
6996 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6997 for (i = 1 ; i < map->n; ++i) {
6998 struct isl_map *r2;
6999 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7000 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7001 res = isl_map_union_disjoint(res, r2);
7002 else
7003 res = isl_map_union(res, r2);
7005 isl_map_free(map);
7007 return res;
7010 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7012 return (struct isl_set *)
7013 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7016 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7018 return (struct isl_set *)
7019 isl_map_compute_divs((struct isl_map *)set);
7022 struct isl_set *isl_map_domain(struct isl_map *map)
7024 int i;
7025 struct isl_set *set;
7027 if (!map)
7028 goto error;
7030 map = isl_map_cow(map);
7031 if (!map)
7032 return NULL;
7034 set = (struct isl_set *)map;
7035 set->dim = isl_space_domain(set->dim);
7036 if (!set->dim)
7037 goto error;
7038 for (i = 0; i < map->n; ++i) {
7039 set->p[i] = isl_basic_map_domain(map->p[i]);
7040 if (!set->p[i])
7041 goto error;
7043 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7044 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7045 return set;
7046 error:
7047 isl_map_free(map);
7048 return NULL;
7051 /* Return the union of "map1" and "map2", where we assume for now that
7052 * "map1" and "map2" are disjoint. Note that the basic maps inside
7053 * "map1" or "map2" may not be disjoint from each other.
7054 * Also note that this function is also called from isl_map_union,
7055 * which takes care of handling the situation where "map1" and "map2"
7056 * may not be disjoint.
7058 * If one of the inputs is empty, we can simply return the other input.
7059 * Similarly, if one of the inputs is universal, then it is equal to the union.
7061 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7062 __isl_take isl_map *map2)
7064 int i;
7065 unsigned flags = 0;
7066 struct isl_map *map = NULL;
7067 int is_universe;
7069 if (!map1 || !map2)
7070 goto error;
7072 if (!isl_space_is_equal(map1->dim, map2->dim))
7073 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7074 "spaces don't match", goto error);
7076 if (map1->n == 0) {
7077 isl_map_free(map1);
7078 return map2;
7080 if (map2->n == 0) {
7081 isl_map_free(map2);
7082 return map1;
7085 is_universe = isl_map_plain_is_universe(map1);
7086 if (is_universe < 0)
7087 goto error;
7088 if (is_universe) {
7089 isl_map_free(map2);
7090 return map1;
7093 is_universe = isl_map_plain_is_universe(map2);
7094 if (is_universe < 0)
7095 goto error;
7096 if (is_universe) {
7097 isl_map_free(map1);
7098 return map2;
7101 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7102 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7103 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7105 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7106 map1->n + map2->n, flags);
7107 if (!map)
7108 goto error;
7109 for (i = 0; i < map1->n; ++i) {
7110 map = isl_map_add_basic_map(map,
7111 isl_basic_map_copy(map1->p[i]));
7112 if (!map)
7113 goto error;
7115 for (i = 0; i < map2->n; ++i) {
7116 map = isl_map_add_basic_map(map,
7117 isl_basic_map_copy(map2->p[i]));
7118 if (!map)
7119 goto error;
7121 isl_map_free(map1);
7122 isl_map_free(map2);
7123 return map;
7124 error:
7125 isl_map_free(map);
7126 isl_map_free(map1);
7127 isl_map_free(map2);
7128 return NULL;
7131 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7132 * guaranteed to be disjoint by the caller.
7134 * Note that this functions is called from within isl_map_make_disjoint,
7135 * so we have to be careful not to touch the constraints of the inputs
7136 * in any way.
7138 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7139 __isl_take isl_map *map2)
7141 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7144 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7145 * not be disjoint. The parameters are assumed to have been aligned.
7147 * We currently simply call map_union_disjoint, the internal operation
7148 * of which does not really depend on the inputs being disjoint.
7149 * If the result contains more than one basic map, then we clear
7150 * the disjoint flag since the result may contain basic maps from
7151 * both inputs and these are not guaranteed to be disjoint.
7153 * As a special case, if "map1" and "map2" are obviously equal,
7154 * then we simply return "map1".
7156 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7157 __isl_take isl_map *map2)
7159 int equal;
7161 if (!map1 || !map2)
7162 goto error;
7164 equal = isl_map_plain_is_equal(map1, map2);
7165 if (equal < 0)
7166 goto error;
7167 if (equal) {
7168 isl_map_free(map2);
7169 return map1;
7172 map1 = map_union_disjoint(map1, map2);
7173 if (!map1)
7174 return NULL;
7175 if (map1->n > 1)
7176 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7177 return map1;
7178 error:
7179 isl_map_free(map1);
7180 isl_map_free(map2);
7181 return NULL;
7184 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7185 * not be disjoint.
7187 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7188 __isl_take isl_map *map2)
7190 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7193 struct isl_set *isl_set_union_disjoint(
7194 struct isl_set *set1, struct isl_set *set2)
7196 return (struct isl_set *)
7197 isl_map_union_disjoint(
7198 (struct isl_map *)set1, (struct isl_map *)set2);
7201 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7203 return (struct isl_set *)
7204 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7207 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7208 * the results.
7210 * "map" and "set" are assumed to be compatible and non-NULL.
7212 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7213 __isl_take isl_set *set,
7214 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7215 __isl_take isl_basic_set *bset))
7217 unsigned flags = 0;
7218 struct isl_map *result;
7219 int i, j;
7221 if (isl_set_plain_is_universe(set)) {
7222 isl_set_free(set);
7223 return map;
7226 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7227 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7228 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7230 result = isl_map_alloc_space(isl_space_copy(map->dim),
7231 map->n * set->n, flags);
7232 for (i = 0; result && i < map->n; ++i)
7233 for (j = 0; j < set->n; ++j) {
7234 result = isl_map_add_basic_map(result,
7235 fn(isl_basic_map_copy(map->p[i]),
7236 isl_basic_set_copy(set->p[j])));
7237 if (!result)
7238 break;
7241 isl_map_free(map);
7242 isl_set_free(set);
7243 return result;
7246 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7247 __isl_take isl_set *set)
7249 if (!map || !set)
7250 goto error;
7252 if (!isl_map_compatible_range(map, set))
7253 isl_die(set->ctx, isl_error_invalid,
7254 "incompatible spaces", goto error);
7256 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7257 error:
7258 isl_map_free(map);
7259 isl_set_free(set);
7260 return NULL;
7263 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7264 __isl_take isl_set *set)
7266 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7269 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7270 __isl_take isl_set *set)
7272 if (!map || !set)
7273 goto error;
7275 if (!isl_map_compatible_domain(map, set))
7276 isl_die(set->ctx, isl_error_invalid,
7277 "incompatible spaces", goto error);
7279 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7280 error:
7281 isl_map_free(map);
7282 isl_set_free(set);
7283 return NULL;
7286 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7287 __isl_take isl_set *set)
7289 return isl_map_align_params_map_map_and(map, set,
7290 &map_intersect_domain);
7293 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7294 __isl_take isl_map *map2)
7296 if (!map1 || !map2)
7297 goto error;
7298 map1 = isl_map_reverse(map1);
7299 map1 = isl_map_apply_range(map1, map2);
7300 return isl_map_reverse(map1);
7301 error:
7302 isl_map_free(map1);
7303 isl_map_free(map2);
7304 return NULL;
7307 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7308 __isl_take isl_map *map2)
7310 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7313 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7314 __isl_take isl_map *map2)
7316 isl_space *dim_result;
7317 struct isl_map *result;
7318 int i, j;
7320 if (!map1 || !map2)
7321 goto error;
7323 dim_result = isl_space_join(isl_space_copy(map1->dim),
7324 isl_space_copy(map2->dim));
7326 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7327 if (!result)
7328 goto error;
7329 for (i = 0; i < map1->n; ++i)
7330 for (j = 0; j < map2->n; ++j) {
7331 result = isl_map_add_basic_map(result,
7332 isl_basic_map_apply_range(
7333 isl_basic_map_copy(map1->p[i]),
7334 isl_basic_map_copy(map2->p[j])));
7335 if (!result)
7336 goto error;
7338 isl_map_free(map1);
7339 isl_map_free(map2);
7340 if (result && result->n <= 1)
7341 ISL_F_SET(result, ISL_MAP_DISJOINT);
7342 return result;
7343 error:
7344 isl_map_free(map1);
7345 isl_map_free(map2);
7346 return NULL;
7349 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7350 __isl_take isl_map *map2)
7352 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7356 * returns range - domain
7358 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7360 isl_space *target_space;
7361 struct isl_basic_set *bset;
7362 unsigned dim;
7363 unsigned nparam;
7364 int i;
7366 if (!bmap)
7367 goto error;
7368 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7369 bmap->dim, isl_dim_out),
7370 goto error);
7371 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7372 dim = isl_basic_map_n_in(bmap);
7373 nparam = isl_basic_map_n_param(bmap);
7374 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7375 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7376 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7377 for (i = 0; i < dim; ++i) {
7378 int j = isl_basic_map_alloc_equality(bmap);
7379 if (j < 0) {
7380 bmap = isl_basic_map_free(bmap);
7381 break;
7383 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7384 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7385 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7386 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7388 bset = isl_basic_map_domain(bmap);
7389 bset = isl_basic_set_reset_space(bset, target_space);
7390 return bset;
7391 error:
7392 isl_basic_map_free(bmap);
7393 return NULL;
7397 * returns range - domain
7399 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7401 int i;
7402 isl_space *dim;
7403 struct isl_set *result;
7405 if (!map)
7406 return NULL;
7408 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7409 map->dim, isl_dim_out),
7410 goto error);
7411 dim = isl_map_get_space(map);
7412 dim = isl_space_domain(dim);
7413 result = isl_set_alloc_space(dim, map->n, 0);
7414 if (!result)
7415 goto error;
7416 for (i = 0; i < map->n; ++i)
7417 result = isl_set_add_basic_set(result,
7418 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7419 isl_map_free(map);
7420 return result;
7421 error:
7422 isl_map_free(map);
7423 return NULL;
7427 * returns [domain -> range] -> range - domain
7429 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7430 __isl_take isl_basic_map *bmap)
7432 int i, k;
7433 isl_space *dim;
7434 isl_basic_map *domain;
7435 int nparam, n;
7436 unsigned total;
7438 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7439 bmap->dim, isl_dim_out))
7440 isl_die(bmap->ctx, isl_error_invalid,
7441 "domain and range don't match", goto error);
7443 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7444 n = isl_basic_map_dim(bmap, isl_dim_in);
7446 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7447 domain = isl_basic_map_universe(dim);
7449 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7450 bmap = isl_basic_map_apply_range(bmap, domain);
7451 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7453 total = isl_basic_map_total_dim(bmap);
7455 for (i = 0; i < n; ++i) {
7456 k = isl_basic_map_alloc_equality(bmap);
7457 if (k < 0)
7458 goto error;
7459 isl_seq_clr(bmap->eq[k], 1 + total);
7460 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7461 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7462 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7465 bmap = isl_basic_map_gauss(bmap, NULL);
7466 return isl_basic_map_finalize(bmap);
7467 error:
7468 isl_basic_map_free(bmap);
7469 return NULL;
7473 * returns [domain -> range] -> range - domain
7475 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7477 int i;
7478 isl_space *domain_dim;
7480 if (!map)
7481 return NULL;
7483 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7484 map->dim, isl_dim_out))
7485 isl_die(map->ctx, isl_error_invalid,
7486 "domain and range don't match", goto error);
7488 map = isl_map_cow(map);
7489 if (!map)
7490 return NULL;
7492 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7493 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7494 map->dim = isl_space_join(map->dim, domain_dim);
7495 if (!map->dim)
7496 goto error;
7497 for (i = 0; i < map->n; ++i) {
7498 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7499 if (!map->p[i])
7500 goto error;
7502 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7503 return map;
7504 error:
7505 isl_map_free(map);
7506 return NULL;
7509 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7511 struct isl_basic_map *bmap;
7512 unsigned nparam;
7513 unsigned dim;
7514 int i;
7516 if (!dims)
7517 return NULL;
7519 nparam = dims->nparam;
7520 dim = dims->n_out;
7521 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7522 if (!bmap)
7523 goto error;
7525 for (i = 0; i < dim; ++i) {
7526 int j = isl_basic_map_alloc_equality(bmap);
7527 if (j < 0)
7528 goto error;
7529 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7530 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7531 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7533 return isl_basic_map_finalize(bmap);
7534 error:
7535 isl_basic_map_free(bmap);
7536 return NULL;
7539 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7541 if (!dim)
7542 return NULL;
7543 if (dim->n_in != dim->n_out)
7544 isl_die(dim->ctx, isl_error_invalid,
7545 "number of input and output dimensions needs to be "
7546 "the same", goto error);
7547 return basic_map_identity(dim);
7548 error:
7549 isl_space_free(dim);
7550 return NULL;
7553 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7555 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7558 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7560 isl_space *dim = isl_set_get_space(set);
7561 isl_map *id;
7562 id = isl_map_identity(isl_space_map_from_set(dim));
7563 return isl_map_intersect_range(id, set);
7566 /* Construct a basic set with all set dimensions having only non-negative
7567 * values.
7569 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7570 __isl_take isl_space *space)
7572 int i;
7573 unsigned nparam;
7574 unsigned dim;
7575 struct isl_basic_set *bset;
7577 if (!space)
7578 return NULL;
7579 nparam = space->nparam;
7580 dim = space->n_out;
7581 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7582 if (!bset)
7583 return NULL;
7584 for (i = 0; i < dim; ++i) {
7585 int k = isl_basic_set_alloc_inequality(bset);
7586 if (k < 0)
7587 goto error;
7588 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7589 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7591 return bset;
7592 error:
7593 isl_basic_set_free(bset);
7594 return NULL;
7597 /* Construct the half-space x_pos >= 0.
7599 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7600 int pos)
7602 int k;
7603 isl_basic_set *nonneg;
7605 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7606 k = isl_basic_set_alloc_inequality(nonneg);
7607 if (k < 0)
7608 goto error;
7609 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7610 isl_int_set_si(nonneg->ineq[k][pos], 1);
7612 return isl_basic_set_finalize(nonneg);
7613 error:
7614 isl_basic_set_free(nonneg);
7615 return NULL;
7618 /* Construct the half-space x_pos <= -1.
7620 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7622 int k;
7623 isl_basic_set *neg;
7625 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7626 k = isl_basic_set_alloc_inequality(neg);
7627 if (k < 0)
7628 goto error;
7629 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7630 isl_int_set_si(neg->ineq[k][0], -1);
7631 isl_int_set_si(neg->ineq[k][pos], -1);
7633 return isl_basic_set_finalize(neg);
7634 error:
7635 isl_basic_set_free(neg);
7636 return NULL;
7639 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7640 enum isl_dim_type type, unsigned first, unsigned n)
7642 int i;
7643 unsigned offset;
7644 isl_basic_set *nonneg;
7645 isl_basic_set *neg;
7647 if (!set)
7648 return NULL;
7649 if (n == 0)
7650 return set;
7652 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7654 offset = pos(set->dim, type);
7655 for (i = 0; i < n; ++i) {
7656 nonneg = nonneg_halfspace(isl_set_get_space(set),
7657 offset + first + i);
7658 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7660 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7663 return set;
7664 error:
7665 isl_set_free(set);
7666 return NULL;
7669 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7670 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7671 void *user)
7673 isl_set *half;
7675 if (!set)
7676 return -1;
7677 if (isl_set_plain_is_empty(set)) {
7678 isl_set_free(set);
7679 return 0;
7681 if (first == len)
7682 return fn(set, signs, user);
7684 signs[first] = 1;
7685 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7686 1 + first));
7687 half = isl_set_intersect(half, isl_set_copy(set));
7688 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7689 goto error;
7691 signs[first] = -1;
7692 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7693 1 + first));
7694 half = isl_set_intersect(half, set);
7695 return foreach_orthant(half, signs, first + 1, len, fn, user);
7696 error:
7697 isl_set_free(set);
7698 return -1;
7701 /* Call "fn" on the intersections of "set" with each of the orthants
7702 * (except for obviously empty intersections). The orthant is identified
7703 * by the signs array, with each entry having value 1 or -1 according
7704 * to the sign of the corresponding variable.
7706 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7707 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7708 void *user)
7710 unsigned nparam;
7711 unsigned nvar;
7712 int *signs;
7713 int r;
7715 if (!set)
7716 return -1;
7717 if (isl_set_plain_is_empty(set))
7718 return 0;
7720 nparam = isl_set_dim(set, isl_dim_param);
7721 nvar = isl_set_dim(set, isl_dim_set);
7723 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7725 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7726 fn, user);
7728 free(signs);
7730 return r;
7733 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7735 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7738 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7739 __isl_keep isl_basic_map *bmap2)
7741 int is_subset;
7742 struct isl_map *map1;
7743 struct isl_map *map2;
7745 if (!bmap1 || !bmap2)
7746 return isl_bool_error;
7748 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7749 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7751 is_subset = isl_map_is_subset(map1, map2);
7753 isl_map_free(map1);
7754 isl_map_free(map2);
7756 return is_subset;
7759 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7760 __isl_keep isl_basic_set *bset2)
7762 return isl_basic_map_is_subset(bset1, bset2);
7765 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7766 __isl_keep isl_basic_map *bmap2)
7768 isl_bool is_subset;
7770 if (!bmap1 || !bmap2)
7771 return isl_bool_error;
7772 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7773 if (is_subset != isl_bool_true)
7774 return is_subset;
7775 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7776 return is_subset;
7779 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7780 __isl_keep isl_basic_set *bset2)
7782 return isl_basic_map_is_equal(
7783 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7786 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7788 int i;
7789 int is_empty;
7791 if (!map)
7792 return isl_bool_error;
7793 for (i = 0; i < map->n; ++i) {
7794 is_empty = isl_basic_map_is_empty(map->p[i]);
7795 if (is_empty < 0)
7796 return isl_bool_error;
7797 if (!is_empty)
7798 return isl_bool_false;
7800 return isl_bool_true;
7803 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7805 return map ? map->n == 0 : isl_bool_error;
7808 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7810 return set ? set->n == 0 : isl_bool_error;
7813 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7815 return isl_map_is_empty((struct isl_map *)set);
7818 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7820 if (!map1 || !map2)
7821 return -1;
7823 return isl_space_is_equal(map1->dim, map2->dim);
7826 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7828 if (!set1 || !set2)
7829 return -1;
7831 return isl_space_is_equal(set1->dim, set2->dim);
7834 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7836 isl_bool is_subset;
7838 if (!map1 || !map2)
7839 return isl_bool_error;
7840 is_subset = isl_map_is_subset(map1, map2);
7841 if (is_subset != isl_bool_true)
7842 return is_subset;
7843 is_subset = isl_map_is_subset(map2, map1);
7844 return is_subset;
7847 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7849 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7852 isl_bool isl_basic_map_is_strict_subset(
7853 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7855 isl_bool is_subset;
7857 if (!bmap1 || !bmap2)
7858 return isl_bool_error;
7859 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7860 if (is_subset != isl_bool_true)
7861 return is_subset;
7862 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7863 if (is_subset == isl_bool_error)
7864 return is_subset;
7865 return !is_subset;
7868 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7869 __isl_keep isl_map *map2)
7871 isl_bool is_subset;
7873 if (!map1 || !map2)
7874 return isl_bool_error;
7875 is_subset = isl_map_is_subset(map1, map2);
7876 if (is_subset != isl_bool_true)
7877 return is_subset;
7878 is_subset = isl_map_is_subset(map2, map1);
7879 if (is_subset == isl_bool_error)
7880 return is_subset;
7881 return !is_subset;
7884 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
7885 __isl_keep isl_set *set2)
7887 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7890 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
7892 if (!bmap)
7893 return isl_bool_error;
7894 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7897 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
7899 if (!bset)
7900 return isl_bool_error;
7901 return bset->n_eq == 0 && bset->n_ineq == 0;
7904 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
7906 int i;
7908 if (!map)
7909 return isl_bool_error;
7911 for (i = 0; i < map->n; ++i) {
7912 isl_bool r = isl_basic_map_is_universe(map->p[i]);
7913 if (r < 0 || r)
7914 return r;
7917 return isl_bool_false;
7920 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
7922 return isl_map_plain_is_universe((isl_map *) set);
7925 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
7927 struct isl_basic_set *bset = NULL;
7928 struct isl_vec *sample = NULL;
7929 isl_bool empty;
7930 unsigned total;
7932 if (!bmap)
7933 return isl_bool_error;
7935 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7936 return isl_bool_true;
7938 if (isl_basic_map_is_universe(bmap))
7939 return isl_bool_false;
7941 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7942 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7943 copy = isl_basic_map_remove_redundancies(copy);
7944 empty = isl_basic_map_plain_is_empty(copy);
7945 isl_basic_map_free(copy);
7946 return empty;
7949 total = 1 + isl_basic_map_total_dim(bmap);
7950 if (bmap->sample && bmap->sample->size == total) {
7951 int contains = isl_basic_map_contains(bmap, bmap->sample);
7952 if (contains < 0)
7953 return isl_bool_error;
7954 if (contains)
7955 return isl_bool_false;
7957 isl_vec_free(bmap->sample);
7958 bmap->sample = NULL;
7959 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7960 if (!bset)
7961 return isl_bool_error;
7962 sample = isl_basic_set_sample_vec(bset);
7963 if (!sample)
7964 return isl_bool_error;
7965 empty = sample->size == 0;
7966 isl_vec_free(bmap->sample);
7967 bmap->sample = sample;
7968 if (empty)
7969 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7971 return empty;
7974 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7976 if (!bmap)
7977 return isl_bool_error;
7978 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7981 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7983 if (!bset)
7984 return isl_bool_error;
7985 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7988 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
7990 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7993 struct isl_map *isl_basic_map_union(
7994 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7996 struct isl_map *map;
7997 if (!bmap1 || !bmap2)
7998 goto error;
8000 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8002 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8003 if (!map)
8004 goto error;
8005 map = isl_map_add_basic_map(map, bmap1);
8006 map = isl_map_add_basic_map(map, bmap2);
8007 return map;
8008 error:
8009 isl_basic_map_free(bmap1);
8010 isl_basic_map_free(bmap2);
8011 return NULL;
8014 struct isl_set *isl_basic_set_union(
8015 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8017 return (struct isl_set *)isl_basic_map_union(
8018 (struct isl_basic_map *)bset1,
8019 (struct isl_basic_map *)bset2);
8022 /* Order divs such that any div only depends on previous divs */
8023 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8025 int i;
8026 unsigned off;
8028 if (!bmap)
8029 return NULL;
8031 off = isl_space_dim(bmap->dim, isl_dim_all);
8033 for (i = 0; i < bmap->n_div; ++i) {
8034 int pos;
8035 if (isl_int_is_zero(bmap->div[i][0]))
8036 continue;
8037 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8038 bmap->n_div-i);
8039 if (pos == -1)
8040 continue;
8041 isl_basic_map_swap_div(bmap, i, i + pos);
8042 --i;
8044 return bmap;
8047 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8049 return (struct isl_basic_set *)
8050 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8053 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8055 int i;
8057 if (!map)
8058 return 0;
8060 for (i = 0; i < map->n; ++i) {
8061 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8062 if (!map->p[i])
8063 goto error;
8066 return map;
8067 error:
8068 isl_map_free(map);
8069 return NULL;
8072 /* Apply the expansion computed by isl_merge_divs.
8073 * The expansion itself is given by "exp" while the resulting
8074 * list of divs is given by "div".
8076 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8077 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8079 int i, j;
8080 int n_div;
8082 bset = isl_basic_set_cow(bset);
8083 if (!bset || !div)
8084 goto error;
8086 if (div->n_row < bset->n_div)
8087 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8088 "not an expansion", goto error);
8090 n_div = bset->n_div;
8091 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8092 div->n_row - n_div, 0,
8093 2 * (div->n_row - n_div));
8095 for (i = n_div; i < div->n_row; ++i)
8096 if (isl_basic_set_alloc_div(bset) < 0)
8097 goto error;
8099 j = n_div - 1;
8100 for (i = div->n_row - 1; i >= 0; --i) {
8101 if (j >= 0 && exp[j] == i) {
8102 if (i != j)
8103 isl_basic_map_swap_div(bset, i, j);
8104 j--;
8105 } else {
8106 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8107 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8108 goto error;
8112 isl_mat_free(div);
8113 return bset;
8114 error:
8115 isl_basic_set_free(bset);
8116 isl_mat_free(div);
8117 return NULL;
8120 /* Look for a div in dst that corresponds to the div "div" in src.
8121 * The divs before "div" in src and dst are assumed to be the same.
8123 * Returns -1 if no corresponding div was found and the position
8124 * of the corresponding div in dst otherwise.
8126 static int find_div(struct isl_basic_map *dst,
8127 struct isl_basic_map *src, unsigned div)
8129 int i;
8131 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8133 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8134 for (i = div; i < dst->n_div; ++i)
8135 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8136 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8137 dst->n_div - div) == -1)
8138 return i;
8139 return -1;
8142 /* Align the divs of "dst" to those of "src", adding divs from "src"
8143 * if needed. That is, make sure that the first src->n_div divs
8144 * of the result are equal to those of src.
8146 * The result is not finalized as by design it will have redundant
8147 * divs if any divs from "src" were copied.
8149 __isl_give isl_basic_map *isl_basic_map_align_divs(
8150 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8152 int i;
8153 int known, extended;
8154 unsigned total;
8156 if (!dst || !src)
8157 return isl_basic_map_free(dst);
8159 if (src->n_div == 0)
8160 return dst;
8162 known = isl_basic_map_divs_known(src);
8163 if (known < 0)
8164 return isl_basic_map_free(dst);
8165 if (!known)
8166 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8167 "some src divs are unknown",
8168 return isl_basic_map_free(dst));
8170 src = isl_basic_map_order_divs(src);
8172 extended = 0;
8173 total = isl_space_dim(src->dim, isl_dim_all);
8174 for (i = 0; i < src->n_div; ++i) {
8175 int j = find_div(dst, src, i);
8176 if (j < 0) {
8177 if (!extended) {
8178 int extra = src->n_div - i;
8179 dst = isl_basic_map_cow(dst);
8180 if (!dst)
8181 return NULL;
8182 dst = isl_basic_map_extend_space(dst,
8183 isl_space_copy(dst->dim),
8184 extra, 0, 2 * extra);
8185 extended = 1;
8187 j = isl_basic_map_alloc_div(dst);
8188 if (j < 0)
8189 return isl_basic_map_free(dst);
8190 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8191 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8192 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8193 return isl_basic_map_free(dst);
8195 if (j != i)
8196 isl_basic_map_swap_div(dst, i, j);
8198 return dst;
8201 struct isl_basic_set *isl_basic_set_align_divs(
8202 struct isl_basic_set *dst, struct isl_basic_set *src)
8204 return (struct isl_basic_set *)isl_basic_map_align_divs(
8205 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8208 struct isl_map *isl_map_align_divs(struct isl_map *map)
8210 int i;
8212 if (!map)
8213 return NULL;
8214 if (map->n == 0)
8215 return map;
8216 map = isl_map_compute_divs(map);
8217 map = isl_map_cow(map);
8218 if (!map)
8219 return NULL;
8221 for (i = 1; i < map->n; ++i)
8222 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8223 for (i = 1; i < map->n; ++i) {
8224 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8225 if (!map->p[i])
8226 return isl_map_free(map);
8229 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8230 return map;
8233 struct isl_set *isl_set_align_divs(struct isl_set *set)
8235 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8238 /* Align the divs of the basic maps in "map" to those
8239 * of the basic maps in "list", as well as to the other basic maps in "map".
8240 * The elements in "list" are assumed to have known divs.
8242 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8243 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8245 int i, n;
8247 map = isl_map_compute_divs(map);
8248 map = isl_map_cow(map);
8249 if (!map || !list)
8250 return isl_map_free(map);
8251 if (map->n == 0)
8252 return map;
8254 n = isl_basic_map_list_n_basic_map(list);
8255 for (i = 0; i < n; ++i) {
8256 isl_basic_map *bmap;
8258 bmap = isl_basic_map_list_get_basic_map(list, i);
8259 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8260 isl_basic_map_free(bmap);
8262 if (!map->p[0])
8263 return isl_map_free(map);
8265 return isl_map_align_divs(map);
8268 /* Align the divs of each element of "list" to those of "bmap".
8269 * Both "bmap" and the elements of "list" are assumed to have known divs.
8271 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8272 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8274 int i, n;
8276 if (!list || !bmap)
8277 return isl_basic_map_list_free(list);
8279 n = isl_basic_map_list_n_basic_map(list);
8280 for (i = 0; i < n; ++i) {
8281 isl_basic_map *bmap_i;
8283 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8284 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8285 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8288 return list;
8291 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8292 __isl_take isl_map *map)
8294 if (!set || !map)
8295 goto error;
8296 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8297 map = isl_map_intersect_domain(map, set);
8298 set = isl_map_range(map);
8299 return set;
8300 error:
8301 isl_set_free(set);
8302 isl_map_free(map);
8303 return NULL;
8306 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8307 __isl_take isl_map *map)
8309 return isl_map_align_params_map_map_and(set, map, &set_apply);
8312 /* There is no need to cow as removing empty parts doesn't change
8313 * the meaning of the set.
8315 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8317 int i;
8319 if (!map)
8320 return NULL;
8322 for (i = map->n - 1; i >= 0; --i)
8323 remove_if_empty(map, i);
8325 return map;
8328 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8330 return (struct isl_set *)
8331 isl_map_remove_empty_parts((struct isl_map *)set);
8334 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8336 struct isl_basic_map *bmap;
8337 if (!map || map->n == 0)
8338 return NULL;
8339 bmap = map->p[map->n-1];
8340 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8341 return isl_basic_map_copy(bmap);
8344 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8346 return (struct isl_basic_set *)
8347 isl_map_copy_basic_map((struct isl_map *)set);
8350 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8351 __isl_keep isl_basic_map *bmap)
8353 int i;
8355 if (!map || !bmap)
8356 goto error;
8357 for (i = map->n-1; i >= 0; --i) {
8358 if (map->p[i] != bmap)
8359 continue;
8360 map = isl_map_cow(map);
8361 if (!map)
8362 goto error;
8363 isl_basic_map_free(map->p[i]);
8364 if (i != map->n-1) {
8365 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8366 map->p[i] = map->p[map->n-1];
8368 map->n--;
8369 return map;
8371 return map;
8372 error:
8373 isl_map_free(map);
8374 return NULL;
8377 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8378 struct isl_basic_set *bset)
8380 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8381 (struct isl_basic_map *)bset);
8384 /* Given two basic sets bset1 and bset2, compute the maximal difference
8385 * between the values of dimension pos in bset1 and those in bset2
8386 * for any common value of the parameters and dimensions preceding pos.
8388 static enum isl_lp_result basic_set_maximal_difference_at(
8389 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8390 int pos, isl_int *opt)
8392 isl_space *dims;
8393 struct isl_basic_map *bmap1 = NULL;
8394 struct isl_basic_map *bmap2 = NULL;
8395 struct isl_ctx *ctx;
8396 struct isl_vec *obj;
8397 unsigned total;
8398 unsigned nparam;
8399 unsigned dim1, dim2;
8400 enum isl_lp_result res;
8402 if (!bset1 || !bset2)
8403 return isl_lp_error;
8405 nparam = isl_basic_set_n_param(bset1);
8406 dim1 = isl_basic_set_n_dim(bset1);
8407 dim2 = isl_basic_set_n_dim(bset2);
8408 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8409 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8410 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8411 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8412 if (!bmap1 || !bmap2)
8413 goto error;
8414 bmap1 = isl_basic_map_cow(bmap1);
8415 bmap1 = isl_basic_map_extend(bmap1, nparam,
8416 pos, (dim1 - pos) + (dim2 - pos),
8417 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8418 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8419 if (!bmap1)
8420 goto error2;
8421 total = isl_basic_map_total_dim(bmap1);
8422 ctx = bmap1->ctx;
8423 obj = isl_vec_alloc(ctx, 1 + total);
8424 if (!obj)
8425 goto error2;
8426 isl_seq_clr(obj->block.data, 1 + total);
8427 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8428 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8429 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8430 opt, NULL, NULL);
8431 isl_basic_map_free(bmap1);
8432 isl_vec_free(obj);
8433 return res;
8434 error:
8435 isl_basic_map_free(bmap2);
8436 error2:
8437 isl_basic_map_free(bmap1);
8438 return isl_lp_error;
8441 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8442 * for any common value of the parameters and dimensions preceding pos
8443 * in both basic sets, the values of dimension pos in bset1 are
8444 * smaller or larger than those in bset2.
8446 * Returns
8447 * 1 if bset1 follows bset2
8448 * -1 if bset1 precedes bset2
8449 * 0 if bset1 and bset2 are incomparable
8450 * -2 if some error occurred.
8452 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8453 struct isl_basic_set *bset2, int pos)
8455 isl_int opt;
8456 enum isl_lp_result res;
8457 int cmp;
8459 isl_int_init(opt);
8461 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8463 if (res == isl_lp_empty)
8464 cmp = 0;
8465 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8466 res == isl_lp_unbounded)
8467 cmp = 1;
8468 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8469 cmp = -1;
8470 else
8471 cmp = -2;
8473 isl_int_clear(opt);
8474 return cmp;
8477 /* Given two basic sets bset1 and bset2, check whether
8478 * for any common value of the parameters and dimensions preceding pos
8479 * there is a value of dimension pos in bset1 that is larger
8480 * than a value of the same dimension in bset2.
8482 * Return
8483 * 1 if there exists such a pair
8484 * 0 if there is no such pair, but there is a pair of equal values
8485 * -1 otherwise
8486 * -2 if some error occurred.
8488 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8489 __isl_keep isl_basic_set *bset2, int pos)
8491 isl_int opt;
8492 enum isl_lp_result res;
8493 int cmp;
8495 isl_int_init(opt);
8497 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8499 if (res == isl_lp_empty)
8500 cmp = -1;
8501 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8502 res == isl_lp_unbounded)
8503 cmp = 1;
8504 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8505 cmp = -1;
8506 else if (res == isl_lp_ok)
8507 cmp = 0;
8508 else
8509 cmp = -2;
8511 isl_int_clear(opt);
8512 return cmp;
8515 /* Given two sets set1 and set2, check whether
8516 * for any common value of the parameters and dimensions preceding pos
8517 * there is a value of dimension pos in set1 that is larger
8518 * than a value of the same dimension in set2.
8520 * Return
8521 * 1 if there exists such a pair
8522 * 0 if there is no such pair, but there is a pair of equal values
8523 * -1 otherwise
8524 * -2 if some error occurred.
8526 int isl_set_follows_at(__isl_keep isl_set *set1,
8527 __isl_keep isl_set *set2, int pos)
8529 int i, j;
8530 int follows = -1;
8532 if (!set1 || !set2)
8533 return -2;
8535 for (i = 0; i < set1->n; ++i)
8536 for (j = 0; j < set2->n; ++j) {
8537 int f;
8538 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8539 if (f == 1 || f == -2)
8540 return f;
8541 if (f > follows)
8542 follows = f;
8545 return follows;
8548 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8549 unsigned pos, isl_int *val)
8551 int i;
8552 int d;
8553 unsigned total;
8555 if (!bmap)
8556 return -1;
8557 total = isl_basic_map_total_dim(bmap);
8558 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8559 for (; d+1 > pos; --d)
8560 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8561 break;
8562 if (d != pos)
8563 continue;
8564 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8565 return 0;
8566 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8567 return 0;
8568 if (!isl_int_is_one(bmap->eq[i][1+d]))
8569 return 0;
8570 if (val)
8571 isl_int_neg(*val, bmap->eq[i][0]);
8572 return 1;
8574 return 0;
8577 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8578 unsigned pos, isl_int *val)
8580 int i;
8581 isl_int v;
8582 isl_int tmp;
8583 int fixed;
8585 if (!map)
8586 return -1;
8587 if (map->n == 0)
8588 return 0;
8589 if (map->n == 1)
8590 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8591 isl_int_init(v);
8592 isl_int_init(tmp);
8593 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8594 for (i = 1; fixed == 1 && i < map->n; ++i) {
8595 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8596 if (fixed == 1 && isl_int_ne(tmp, v))
8597 fixed = 0;
8599 if (val)
8600 isl_int_set(*val, v);
8601 isl_int_clear(tmp);
8602 isl_int_clear(v);
8603 return fixed;
8606 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8607 unsigned pos, isl_int *val)
8609 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8610 pos, val);
8613 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8614 isl_int *val)
8616 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8619 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8620 enum isl_dim_type type, unsigned pos, isl_int *val)
8622 if (pos >= isl_basic_map_dim(bmap, type))
8623 return -1;
8624 return isl_basic_map_plain_has_fixed_var(bmap,
8625 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8628 /* If "bmap" obviously lies on a hyperplane where the given dimension
8629 * has a fixed value, then return that value.
8630 * Otherwise return NaN.
8632 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8633 __isl_keep isl_basic_map *bmap,
8634 enum isl_dim_type type, unsigned pos)
8636 isl_ctx *ctx;
8637 isl_val *v;
8638 int fixed;
8640 if (!bmap)
8641 return NULL;
8642 ctx = isl_basic_map_get_ctx(bmap);
8643 v = isl_val_alloc(ctx);
8644 if (!v)
8645 return NULL;
8646 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8647 if (fixed < 0)
8648 return isl_val_free(v);
8649 if (fixed) {
8650 isl_int_set_si(v->d, 1);
8651 return v;
8653 isl_val_free(v);
8654 return isl_val_nan(ctx);
8657 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8658 enum isl_dim_type type, unsigned pos, isl_int *val)
8660 if (pos >= isl_map_dim(map, type))
8661 return -1;
8662 return isl_map_plain_has_fixed_var(map,
8663 map_offset(map, type) - 1 + pos, val);
8666 /* If "map" obviously lies on a hyperplane where the given dimension
8667 * has a fixed value, then return that value.
8668 * Otherwise return NaN.
8670 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8671 enum isl_dim_type type, unsigned pos)
8673 isl_ctx *ctx;
8674 isl_val *v;
8675 int fixed;
8677 if (!map)
8678 return NULL;
8679 ctx = isl_map_get_ctx(map);
8680 v = isl_val_alloc(ctx);
8681 if (!v)
8682 return NULL;
8683 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8684 if (fixed < 0)
8685 return isl_val_free(v);
8686 if (fixed) {
8687 isl_int_set_si(v->d, 1);
8688 return v;
8690 isl_val_free(v);
8691 return isl_val_nan(ctx);
8694 /* If "set" obviously lies on a hyperplane where the given dimension
8695 * has a fixed value, then return that value.
8696 * Otherwise return NaN.
8698 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8699 enum isl_dim_type type, unsigned pos)
8701 return isl_map_plain_get_val_if_fixed(set, type, pos);
8704 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8705 enum isl_dim_type type, unsigned pos, isl_int *val)
8707 return isl_map_plain_is_fixed(set, type, pos, val);
8710 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8711 * then return this fixed value in *val.
8713 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8714 unsigned dim, isl_int *val)
8716 return isl_basic_set_plain_has_fixed_var(bset,
8717 isl_basic_set_n_param(bset) + dim, val);
8720 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8721 * then return this fixed value in *val.
8723 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8724 unsigned dim, isl_int *val)
8726 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8729 /* Check if input variable in has fixed value and if so and if val is not NULL,
8730 * then return this fixed value in *val.
8732 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8733 unsigned in, isl_int *val)
8735 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8738 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8739 * and if val is not NULL, then return this lower bound in *val.
8741 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8742 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8744 int i, i_eq = -1, i_ineq = -1;
8745 isl_int *c;
8746 unsigned total;
8747 unsigned nparam;
8749 if (!bset)
8750 return -1;
8751 total = isl_basic_set_total_dim(bset);
8752 nparam = isl_basic_set_n_param(bset);
8753 for (i = 0; i < bset->n_eq; ++i) {
8754 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8755 continue;
8756 if (i_eq != -1)
8757 return 0;
8758 i_eq = i;
8760 for (i = 0; i < bset->n_ineq; ++i) {
8761 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8762 continue;
8763 if (i_eq != -1 || i_ineq != -1)
8764 return 0;
8765 i_ineq = i;
8767 if (i_eq == -1 && i_ineq == -1)
8768 return 0;
8769 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8770 /* The coefficient should always be one due to normalization. */
8771 if (!isl_int_is_one(c[1+nparam+dim]))
8772 return 0;
8773 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8774 return 0;
8775 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8776 total - nparam - dim - 1) != -1)
8777 return 0;
8778 if (val)
8779 isl_int_neg(*val, c[0]);
8780 return 1;
8783 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8784 unsigned dim, isl_int *val)
8786 int i;
8787 isl_int v;
8788 isl_int tmp;
8789 int fixed;
8791 if (!set)
8792 return -1;
8793 if (set->n == 0)
8794 return 0;
8795 if (set->n == 1)
8796 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8797 dim, val);
8798 isl_int_init(v);
8799 isl_int_init(tmp);
8800 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8801 dim, &v);
8802 for (i = 1; fixed == 1 && i < set->n; ++i) {
8803 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8804 dim, &tmp);
8805 if (fixed == 1 && isl_int_ne(tmp, v))
8806 fixed = 0;
8808 if (val)
8809 isl_int_set(*val, v);
8810 isl_int_clear(tmp);
8811 isl_int_clear(v);
8812 return fixed;
8815 /* uset_gist depends on constraints without existentially quantified
8816 * variables sorting first.
8818 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8820 isl_int **c1 = (isl_int **) p1;
8821 isl_int **c2 = (isl_int **) p2;
8822 int l1, l2;
8823 unsigned size = *(unsigned *) arg;
8825 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8826 l2 = isl_seq_last_non_zero(*c2 + 1, size);
8828 if (l1 != l2)
8829 return l1 - l2;
8831 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
8834 static struct isl_basic_map *isl_basic_map_sort_constraints(
8835 struct isl_basic_map *bmap)
8837 unsigned total;
8839 if (!bmap)
8840 return NULL;
8841 if (bmap->n_ineq == 0)
8842 return bmap;
8843 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8844 return bmap;
8845 total = isl_basic_map_total_dim(bmap);
8846 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
8847 &sort_constraint_cmp, &total) < 0)
8848 return isl_basic_map_free(bmap);
8849 return bmap;
8852 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8853 __isl_take isl_basic_set *bset)
8855 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8856 (struct isl_basic_map *)bset);
8859 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8861 if (!bmap)
8862 return NULL;
8863 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8864 return bmap;
8865 bmap = isl_basic_map_remove_redundancies(bmap);
8866 bmap = isl_basic_map_sort_constraints(bmap);
8867 if (bmap)
8868 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8869 return bmap;
8872 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8874 return (struct isl_basic_set *)isl_basic_map_normalize(
8875 (struct isl_basic_map *)bset);
8878 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8879 const __isl_keep isl_basic_map *bmap2)
8881 int i, cmp;
8882 unsigned total;
8884 if (!bmap1 || !bmap2)
8885 return -1;
8887 if (bmap1 == bmap2)
8888 return 0;
8889 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8890 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8891 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8892 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8893 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8894 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8895 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8896 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8897 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8898 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8899 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8900 return 0;
8901 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8902 return 1;
8903 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8904 return -1;
8905 if (bmap1->n_eq != bmap2->n_eq)
8906 return bmap1->n_eq - bmap2->n_eq;
8907 if (bmap1->n_ineq != bmap2->n_ineq)
8908 return bmap1->n_ineq - bmap2->n_ineq;
8909 if (bmap1->n_div != bmap2->n_div)
8910 return bmap1->n_div - bmap2->n_div;
8911 total = isl_basic_map_total_dim(bmap1);
8912 for (i = 0; i < bmap1->n_eq; ++i) {
8913 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8914 if (cmp)
8915 return cmp;
8917 for (i = 0; i < bmap1->n_ineq; ++i) {
8918 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8919 if (cmp)
8920 return cmp;
8922 for (i = 0; i < bmap1->n_div; ++i) {
8923 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8924 if (cmp)
8925 return cmp;
8927 return 0;
8930 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8931 const __isl_keep isl_basic_set *bset2)
8933 return isl_basic_map_plain_cmp(bset1, bset2);
8936 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8938 int i, cmp;
8940 if (set1 == set2)
8941 return 0;
8942 if (set1->n != set2->n)
8943 return set1->n - set2->n;
8945 for (i = 0; i < set1->n; ++i) {
8946 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8947 if (cmp)
8948 return cmp;
8951 return 0;
8954 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8955 __isl_keep isl_basic_map *bmap2)
8957 if (!bmap1 || !bmap2)
8958 return isl_bool_error;
8959 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8962 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8963 __isl_keep isl_basic_set *bset2)
8965 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8966 (isl_basic_map *)bset2);
8969 static int qsort_bmap_cmp(const void *p1, const void *p2)
8971 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8972 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8974 return isl_basic_map_plain_cmp(bmap1, bmap2);
8977 /* Sort the basic maps of "map" and remove duplicate basic maps.
8979 * While removing basic maps, we make sure that the basic maps remain
8980 * sorted because isl_map_normalize expects the basic maps of the result
8981 * to be sorted.
8983 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
8985 int i, j;
8987 map = isl_map_remove_empty_parts(map);
8988 if (!map)
8989 return NULL;
8990 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8991 for (i = map->n - 1; i >= 1; --i) {
8992 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
8993 continue;
8994 isl_basic_map_free(map->p[i-1]);
8995 for (j = i; j < map->n; ++j)
8996 map->p[j - 1] = map->p[j];
8997 map->n--;
9000 return map;
9003 /* Remove obvious duplicates among the basic maps of "map".
9005 * Unlike isl_map_normalize, this function does not remove redundant
9006 * constraints and only removes duplicates that have exactly the same
9007 * constraints in the input. It does sort the constraints and
9008 * the basic maps to ease the detection of duplicates.
9010 * If "map" has already been normalized or if the basic maps are
9011 * disjoint, then there can be no duplicates.
9013 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9015 int i;
9016 isl_basic_map *bmap;
9018 if (!map)
9019 return NULL;
9020 if (map->n <= 1)
9021 return map;
9022 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9023 return map;
9024 for (i = 0; i < map->n; ++i) {
9025 bmap = isl_basic_map_copy(map->p[i]);
9026 bmap = isl_basic_map_sort_constraints(bmap);
9027 if (!bmap)
9028 return isl_map_free(map);
9029 isl_basic_map_free(map->p[i]);
9030 map->p[i] = bmap;
9033 map = sort_and_remove_duplicates(map);
9034 return map;
9037 /* We normalize in place, but if anything goes wrong we need
9038 * to return NULL, so we need to make sure we don't change the
9039 * meaning of any possible other copies of map.
9041 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9043 int i;
9044 struct isl_basic_map *bmap;
9046 if (!map)
9047 return NULL;
9048 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9049 return map;
9050 for (i = 0; i < map->n; ++i) {
9051 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9052 if (!bmap)
9053 goto error;
9054 isl_basic_map_free(map->p[i]);
9055 map->p[i] = bmap;
9058 map = sort_and_remove_duplicates(map);
9059 if (map)
9060 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9061 return map;
9062 error:
9063 isl_map_free(map);
9064 return NULL;
9067 struct isl_set *isl_set_normalize(struct isl_set *set)
9069 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9072 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9073 __isl_keep isl_map *map2)
9075 int i;
9076 isl_bool equal;
9078 if (!map1 || !map2)
9079 return isl_bool_error;
9081 if (map1 == map2)
9082 return isl_bool_true;
9083 if (!isl_space_is_equal(map1->dim, map2->dim))
9084 return isl_bool_false;
9086 map1 = isl_map_copy(map1);
9087 map2 = isl_map_copy(map2);
9088 map1 = isl_map_normalize(map1);
9089 map2 = isl_map_normalize(map2);
9090 if (!map1 || !map2)
9091 goto error;
9092 equal = map1->n == map2->n;
9093 for (i = 0; equal && i < map1->n; ++i) {
9094 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9095 if (equal < 0)
9096 goto error;
9098 isl_map_free(map1);
9099 isl_map_free(map2);
9100 return equal;
9101 error:
9102 isl_map_free(map1);
9103 isl_map_free(map2);
9104 return isl_bool_error;
9107 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9108 __isl_keep isl_set *set2)
9110 return isl_map_plain_is_equal((struct isl_map *)set1,
9111 (struct isl_map *)set2);
9114 /* Return an interval that ranges from min to max (inclusive)
9116 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9117 isl_int min, isl_int max)
9119 int k;
9120 struct isl_basic_set *bset = NULL;
9122 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9123 if (!bset)
9124 goto error;
9126 k = isl_basic_set_alloc_inequality(bset);
9127 if (k < 0)
9128 goto error;
9129 isl_int_set_si(bset->ineq[k][1], 1);
9130 isl_int_neg(bset->ineq[k][0], min);
9132 k = isl_basic_set_alloc_inequality(bset);
9133 if (k < 0)
9134 goto error;
9135 isl_int_set_si(bset->ineq[k][1], -1);
9136 isl_int_set(bset->ineq[k][0], max);
9138 return bset;
9139 error:
9140 isl_basic_set_free(bset);
9141 return NULL;
9144 /* Return the basic maps in "map" as a list.
9146 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9147 __isl_keep isl_map *map)
9149 int i;
9150 isl_ctx *ctx;
9151 isl_basic_map_list *list;
9153 if (!map)
9154 return NULL;
9155 ctx = isl_map_get_ctx(map);
9156 list = isl_basic_map_list_alloc(ctx, map->n);
9158 for (i = 0; i < map->n; ++i) {
9159 isl_basic_map *bmap;
9161 bmap = isl_basic_map_copy(map->p[i]);
9162 list = isl_basic_map_list_add(list, bmap);
9165 return list;
9168 /* Return the intersection of the elements in the non-empty list "list".
9169 * All elements are assumed to live in the same space.
9171 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9172 __isl_take isl_basic_map_list *list)
9174 int i, n;
9175 isl_basic_map *bmap;
9177 if (!list)
9178 return NULL;
9179 n = isl_basic_map_list_n_basic_map(list);
9180 if (n < 1)
9181 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9182 "expecting non-empty list", goto error);
9184 bmap = isl_basic_map_list_get_basic_map(list, 0);
9185 for (i = 1; i < n; ++i) {
9186 isl_basic_map *bmap_i;
9188 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9189 bmap = isl_basic_map_intersect(bmap, bmap_i);
9192 isl_basic_map_list_free(list);
9193 return bmap;
9194 error:
9195 isl_basic_map_list_free(list);
9196 return NULL;
9199 /* Return the intersection of the elements in the non-empty list "list".
9200 * All elements are assumed to live in the same space.
9202 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9203 __isl_take isl_basic_set_list *list)
9205 return isl_basic_map_list_intersect(list);
9208 /* Return the Cartesian product of the basic sets in list (in the given order).
9210 __isl_give isl_basic_set *isl_basic_set_list_product(
9211 __isl_take struct isl_basic_set_list *list)
9213 int i;
9214 unsigned dim;
9215 unsigned nparam;
9216 unsigned extra;
9217 unsigned n_eq;
9218 unsigned n_ineq;
9219 struct isl_basic_set *product = NULL;
9221 if (!list)
9222 goto error;
9223 isl_assert(list->ctx, list->n > 0, goto error);
9224 isl_assert(list->ctx, list->p[0], goto error);
9225 nparam = isl_basic_set_n_param(list->p[0]);
9226 dim = isl_basic_set_n_dim(list->p[0]);
9227 extra = list->p[0]->n_div;
9228 n_eq = list->p[0]->n_eq;
9229 n_ineq = list->p[0]->n_ineq;
9230 for (i = 1; i < list->n; ++i) {
9231 isl_assert(list->ctx, list->p[i], goto error);
9232 isl_assert(list->ctx,
9233 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9234 dim += isl_basic_set_n_dim(list->p[i]);
9235 extra += list->p[i]->n_div;
9236 n_eq += list->p[i]->n_eq;
9237 n_ineq += list->p[i]->n_ineq;
9239 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9240 n_eq, n_ineq);
9241 if (!product)
9242 goto error;
9243 dim = 0;
9244 for (i = 0; i < list->n; ++i) {
9245 isl_basic_set_add_constraints(product,
9246 isl_basic_set_copy(list->p[i]), dim);
9247 dim += isl_basic_set_n_dim(list->p[i]);
9249 isl_basic_set_list_free(list);
9250 return product;
9251 error:
9252 isl_basic_set_free(product);
9253 isl_basic_set_list_free(list);
9254 return NULL;
9257 struct isl_basic_map *isl_basic_map_product(
9258 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9260 isl_space *dim_result = NULL;
9261 struct isl_basic_map *bmap;
9262 unsigned in1, in2, out1, out2, nparam, total, pos;
9263 struct isl_dim_map *dim_map1, *dim_map2;
9265 if (!bmap1 || !bmap2)
9266 goto error;
9268 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9269 bmap2->dim, isl_dim_param), goto error);
9270 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9271 isl_space_copy(bmap2->dim));
9273 in1 = isl_basic_map_n_in(bmap1);
9274 in2 = isl_basic_map_n_in(bmap2);
9275 out1 = isl_basic_map_n_out(bmap1);
9276 out2 = isl_basic_map_n_out(bmap2);
9277 nparam = isl_basic_map_n_param(bmap1);
9279 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9280 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9281 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9282 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9283 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9284 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9285 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9286 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9287 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9288 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9289 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9291 bmap = isl_basic_map_alloc_space(dim_result,
9292 bmap1->n_div + bmap2->n_div,
9293 bmap1->n_eq + bmap2->n_eq,
9294 bmap1->n_ineq + bmap2->n_ineq);
9295 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9296 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9297 bmap = isl_basic_map_simplify(bmap);
9298 return isl_basic_map_finalize(bmap);
9299 error:
9300 isl_basic_map_free(bmap1);
9301 isl_basic_map_free(bmap2);
9302 return NULL;
9305 __isl_give isl_basic_map *isl_basic_map_flat_product(
9306 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9308 isl_basic_map *prod;
9310 prod = isl_basic_map_product(bmap1, bmap2);
9311 prod = isl_basic_map_flatten(prod);
9312 return prod;
9315 __isl_give isl_basic_set *isl_basic_set_flat_product(
9316 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9318 return isl_basic_map_flat_range_product(bset1, bset2);
9321 __isl_give isl_basic_map *isl_basic_map_domain_product(
9322 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9324 isl_space *space_result = NULL;
9325 isl_basic_map *bmap;
9326 unsigned in1, in2, out, nparam, total, pos;
9327 struct isl_dim_map *dim_map1, *dim_map2;
9329 if (!bmap1 || !bmap2)
9330 goto error;
9332 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9333 isl_space_copy(bmap2->dim));
9335 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9336 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9337 out = isl_basic_map_dim(bmap1, isl_dim_out);
9338 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9340 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9341 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9342 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9343 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9344 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9345 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9346 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9347 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9348 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9349 isl_dim_map_div(dim_map1, bmap1, pos += out);
9350 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9352 bmap = isl_basic_map_alloc_space(space_result,
9353 bmap1->n_div + bmap2->n_div,
9354 bmap1->n_eq + bmap2->n_eq,
9355 bmap1->n_ineq + bmap2->n_ineq);
9356 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9357 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9358 bmap = isl_basic_map_simplify(bmap);
9359 return isl_basic_map_finalize(bmap);
9360 error:
9361 isl_basic_map_free(bmap1);
9362 isl_basic_map_free(bmap2);
9363 return NULL;
9366 __isl_give isl_basic_map *isl_basic_map_range_product(
9367 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9369 isl_space *dim_result = NULL;
9370 isl_basic_map *bmap;
9371 unsigned in, out1, out2, nparam, total, pos;
9372 struct isl_dim_map *dim_map1, *dim_map2;
9374 if (!bmap1 || !bmap2)
9375 goto error;
9377 if (!isl_space_match(bmap1->dim, isl_dim_param,
9378 bmap2->dim, isl_dim_param))
9379 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9380 "parameters don't match", goto error);
9382 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9383 isl_space_copy(bmap2->dim));
9385 in = isl_basic_map_dim(bmap1, isl_dim_in);
9386 out1 = isl_basic_map_n_out(bmap1);
9387 out2 = isl_basic_map_n_out(bmap2);
9388 nparam = isl_basic_map_n_param(bmap1);
9390 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9391 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9392 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9393 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9394 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9395 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9396 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9397 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9398 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9399 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9400 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9402 bmap = isl_basic_map_alloc_space(dim_result,
9403 bmap1->n_div + bmap2->n_div,
9404 bmap1->n_eq + bmap2->n_eq,
9405 bmap1->n_ineq + bmap2->n_ineq);
9406 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9407 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9408 bmap = isl_basic_map_simplify(bmap);
9409 return isl_basic_map_finalize(bmap);
9410 error:
9411 isl_basic_map_free(bmap1);
9412 isl_basic_map_free(bmap2);
9413 return NULL;
9416 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9417 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9419 isl_basic_map *prod;
9421 prod = isl_basic_map_range_product(bmap1, bmap2);
9422 prod = isl_basic_map_flatten_range(prod);
9423 return prod;
9426 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9427 * and collect the results.
9428 * The result live in the space obtained by calling "space_product"
9429 * on the spaces of "map1" and "map2".
9430 * If "remove_duplicates" is set then the result may contain duplicates
9431 * (even if the inputs do not) and so we try and remove the obvious
9432 * duplicates.
9434 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9435 __isl_take isl_map *map2,
9436 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9437 __isl_take isl_space *right),
9438 __isl_give isl_basic_map *(*basic_map_product)(
9439 __isl_take isl_basic_map *left,
9440 __isl_take isl_basic_map *right),
9441 int remove_duplicates)
9443 unsigned flags = 0;
9444 struct isl_map *result;
9445 int i, j;
9447 if (!map1 || !map2)
9448 goto error;
9450 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9451 map2->dim, isl_dim_param), goto error);
9453 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9454 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9455 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9457 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9458 isl_space_copy(map2->dim)),
9459 map1->n * map2->n, flags);
9460 if (!result)
9461 goto error;
9462 for (i = 0; i < map1->n; ++i)
9463 for (j = 0; j < map2->n; ++j) {
9464 struct isl_basic_map *part;
9465 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9466 isl_basic_map_copy(map2->p[j]));
9467 if (isl_basic_map_is_empty(part))
9468 isl_basic_map_free(part);
9469 else
9470 result = isl_map_add_basic_map(result, part);
9471 if (!result)
9472 goto error;
9474 if (remove_duplicates)
9475 result = isl_map_remove_obvious_duplicates(result);
9476 isl_map_free(map1);
9477 isl_map_free(map2);
9478 return result;
9479 error:
9480 isl_map_free(map1);
9481 isl_map_free(map2);
9482 return NULL;
9485 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9487 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9488 __isl_take isl_map *map2)
9490 return map_product(map1, map2, &isl_space_product,
9491 &isl_basic_map_product, 0);
9494 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9495 __isl_take isl_map *map2)
9497 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9500 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9502 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9503 __isl_take isl_map *map2)
9505 isl_map *prod;
9507 prod = isl_map_product(map1, map2);
9508 prod = isl_map_flatten(prod);
9509 return prod;
9512 /* Given two set A and B, construct its Cartesian product A x B.
9514 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9516 return isl_map_range_product(set1, set2);
9519 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9520 __isl_take isl_set *set2)
9522 return isl_map_flat_range_product(set1, set2);
9525 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9527 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9528 __isl_take isl_map *map2)
9530 return map_product(map1, map2, &isl_space_domain_product,
9531 &isl_basic_map_domain_product, 1);
9534 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9536 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9537 __isl_take isl_map *map2)
9539 return map_product(map1, map2, &isl_space_range_product,
9540 &isl_basic_map_range_product, 1);
9543 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9544 __isl_take isl_map *map2)
9546 return isl_map_align_params_map_map_and(map1, map2,
9547 &map_domain_product_aligned);
9550 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9551 __isl_take isl_map *map2)
9553 return isl_map_align_params_map_map_and(map1, map2,
9554 &map_range_product_aligned);
9557 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9559 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9561 isl_space *space;
9562 int total1, keep1, total2, keep2;
9564 if (!map)
9565 return NULL;
9566 if (!isl_space_domain_is_wrapping(map->dim) ||
9567 !isl_space_range_is_wrapping(map->dim))
9568 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9569 "not a product", return isl_map_free(map));
9571 space = isl_map_get_space(map);
9572 total1 = isl_space_dim(space, isl_dim_in);
9573 total2 = isl_space_dim(space, isl_dim_out);
9574 space = isl_space_factor_domain(space);
9575 keep1 = isl_space_dim(space, isl_dim_in);
9576 keep2 = isl_space_dim(space, isl_dim_out);
9577 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9578 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9579 map = isl_map_reset_space(map, space);
9581 return map;
9584 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9586 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9588 isl_space *space;
9589 int total1, keep1, total2, keep2;
9591 if (!map)
9592 return NULL;
9593 if (!isl_space_domain_is_wrapping(map->dim) ||
9594 !isl_space_range_is_wrapping(map->dim))
9595 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9596 "not a product", return isl_map_free(map));
9598 space = isl_map_get_space(map);
9599 total1 = isl_space_dim(space, isl_dim_in);
9600 total2 = isl_space_dim(space, isl_dim_out);
9601 space = isl_space_factor_range(space);
9602 keep1 = isl_space_dim(space, isl_dim_in);
9603 keep2 = isl_space_dim(space, isl_dim_out);
9604 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9605 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9606 map = isl_map_reset_space(map, space);
9608 return map;
9611 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9613 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9615 isl_space *space;
9616 int total, keep;
9618 if (!map)
9619 return NULL;
9620 if (!isl_space_domain_is_wrapping(map->dim))
9621 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9622 "domain is not a product", return isl_map_free(map));
9624 space = isl_map_get_space(map);
9625 total = isl_space_dim(space, isl_dim_in);
9626 space = isl_space_domain_factor_domain(space);
9627 keep = isl_space_dim(space, isl_dim_in);
9628 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9629 map = isl_map_reset_space(map, space);
9631 return map;
9634 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9636 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9638 isl_space *space;
9639 int total, keep;
9641 if (!map)
9642 return NULL;
9643 if (!isl_space_domain_is_wrapping(map->dim))
9644 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9645 "domain is not a product", return isl_map_free(map));
9647 space = isl_map_get_space(map);
9648 total = isl_space_dim(space, isl_dim_in);
9649 space = isl_space_domain_factor_range(space);
9650 keep = isl_space_dim(space, isl_dim_in);
9651 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9652 map = isl_map_reset_space(map, space);
9654 return map;
9657 /* Given a map A -> [B -> C], extract the map A -> B.
9659 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9661 isl_space *space;
9662 int total, keep;
9664 if (!map)
9665 return NULL;
9666 if (!isl_space_range_is_wrapping(map->dim))
9667 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9668 "range is not a product", return isl_map_free(map));
9670 space = isl_map_get_space(map);
9671 total = isl_space_dim(space, isl_dim_out);
9672 space = isl_space_range_factor_domain(space);
9673 keep = isl_space_dim(space, isl_dim_out);
9674 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9675 map = isl_map_reset_space(map, space);
9677 return map;
9680 /* Given a map A -> [B -> C], extract the map A -> C.
9682 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9684 isl_space *space;
9685 int total, keep;
9687 if (!map)
9688 return NULL;
9689 if (!isl_space_range_is_wrapping(map->dim))
9690 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9691 "range is not a product", return isl_map_free(map));
9693 space = isl_map_get_space(map);
9694 total = isl_space_dim(space, isl_dim_out);
9695 space = isl_space_range_factor_range(space);
9696 keep = isl_space_dim(space, isl_dim_out);
9697 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9698 map = isl_map_reset_space(map, space);
9700 return map;
9703 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9705 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9706 __isl_take isl_map *map2)
9708 isl_map *prod;
9710 prod = isl_map_domain_product(map1, map2);
9711 prod = isl_map_flatten_domain(prod);
9712 return prod;
9715 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9717 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9718 __isl_take isl_map *map2)
9720 isl_map *prod;
9722 prod = isl_map_range_product(map1, map2);
9723 prod = isl_map_flatten_range(prod);
9724 return prod;
9727 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9729 int i;
9730 uint32_t hash = isl_hash_init();
9731 unsigned total;
9733 if (!bmap)
9734 return 0;
9735 bmap = isl_basic_map_copy(bmap);
9736 bmap = isl_basic_map_normalize(bmap);
9737 if (!bmap)
9738 return 0;
9739 total = isl_basic_map_total_dim(bmap);
9740 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9741 for (i = 0; i < bmap->n_eq; ++i) {
9742 uint32_t c_hash;
9743 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9744 isl_hash_hash(hash, c_hash);
9746 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9747 for (i = 0; i < bmap->n_ineq; ++i) {
9748 uint32_t c_hash;
9749 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9750 isl_hash_hash(hash, c_hash);
9752 isl_hash_byte(hash, bmap->n_div & 0xFF);
9753 for (i = 0; i < bmap->n_div; ++i) {
9754 uint32_t c_hash;
9755 if (isl_int_is_zero(bmap->div[i][0]))
9756 continue;
9757 isl_hash_byte(hash, i & 0xFF);
9758 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9759 isl_hash_hash(hash, c_hash);
9761 isl_basic_map_free(bmap);
9762 return hash;
9765 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9767 return isl_basic_map_get_hash((isl_basic_map *)bset);
9770 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9772 int i;
9773 uint32_t hash;
9775 if (!map)
9776 return 0;
9777 map = isl_map_copy(map);
9778 map = isl_map_normalize(map);
9779 if (!map)
9780 return 0;
9782 hash = isl_hash_init();
9783 for (i = 0; i < map->n; ++i) {
9784 uint32_t bmap_hash;
9785 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9786 isl_hash_hash(hash, bmap_hash);
9789 isl_map_free(map);
9791 return hash;
9794 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9796 return isl_map_get_hash((isl_map *)set);
9799 /* Check if the value for dimension dim is completely determined
9800 * by the values of the other parameters and variables.
9801 * That is, check if dimension dim is involved in an equality.
9803 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9805 int i;
9806 unsigned nparam;
9808 if (!bset)
9809 return -1;
9810 nparam = isl_basic_set_n_param(bset);
9811 for (i = 0; i < bset->n_eq; ++i)
9812 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9813 return 1;
9814 return 0;
9817 /* Check if the value for dimension dim is completely determined
9818 * by the values of the other parameters and variables.
9819 * That is, check if dimension dim is involved in an equality
9820 * for each of the subsets.
9822 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9824 int i;
9826 if (!set)
9827 return -1;
9828 for (i = 0; i < set->n; ++i) {
9829 int unique;
9830 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9831 if (unique != 1)
9832 return unique;
9834 return 1;
9837 /* Return the number of basic maps in the (current) representation of "map".
9839 int isl_map_n_basic_map(__isl_keep isl_map *map)
9841 return map ? map->n : 0;
9844 int isl_set_n_basic_set(__isl_keep isl_set *set)
9846 return set ? set->n : 0;
9849 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
9850 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9852 int i;
9854 if (!map)
9855 return isl_stat_error;
9857 for (i = 0; i < map->n; ++i)
9858 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9859 return isl_stat_error;
9861 return isl_stat_ok;
9864 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
9865 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9867 int i;
9869 if (!set)
9870 return isl_stat_error;
9872 for (i = 0; i < set->n; ++i)
9873 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9874 return isl_stat_error;
9876 return isl_stat_ok;
9879 /* Return a list of basic sets, the union of which is equal to "set".
9881 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
9882 __isl_keep isl_set *set)
9884 int i;
9885 isl_basic_set_list *list;
9887 if (!set)
9888 return NULL;
9890 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
9891 for (i = 0; i < set->n; ++i) {
9892 isl_basic_set *bset;
9894 bset = isl_basic_set_copy(set->p[i]);
9895 list = isl_basic_set_list_add(list, bset);
9898 return list;
9901 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9903 isl_space *dim;
9905 if (!bset)
9906 return NULL;
9908 bset = isl_basic_set_cow(bset);
9909 if (!bset)
9910 return NULL;
9912 dim = isl_basic_set_get_space(bset);
9913 dim = isl_space_lift(dim, bset->n_div);
9914 if (!dim)
9915 goto error;
9916 isl_space_free(bset->dim);
9917 bset->dim = dim;
9918 bset->extra -= bset->n_div;
9919 bset->n_div = 0;
9921 bset = isl_basic_set_finalize(bset);
9923 return bset;
9924 error:
9925 isl_basic_set_free(bset);
9926 return NULL;
9929 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9931 int i;
9932 isl_space *dim;
9933 unsigned n_div;
9935 set = isl_set_align_divs(set);
9937 if (!set)
9938 return NULL;
9940 set = isl_set_cow(set);
9941 if (!set)
9942 return NULL;
9944 n_div = set->p[0]->n_div;
9945 dim = isl_set_get_space(set);
9946 dim = isl_space_lift(dim, n_div);
9947 if (!dim)
9948 goto error;
9949 isl_space_free(set->dim);
9950 set->dim = dim;
9952 for (i = 0; i < set->n; ++i) {
9953 set->p[i] = isl_basic_set_lift(set->p[i]);
9954 if (!set->p[i])
9955 goto error;
9958 return set;
9959 error:
9960 isl_set_free(set);
9961 return NULL;
9964 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9966 isl_space *dim;
9967 struct isl_basic_map *bmap;
9968 unsigned n_set;
9969 unsigned n_div;
9970 unsigned n_param;
9971 unsigned total;
9972 int i, k, l;
9974 set = isl_set_align_divs(set);
9976 if (!set)
9977 return NULL;
9979 dim = isl_set_get_space(set);
9980 if (set->n == 0 || set->p[0]->n_div == 0) {
9981 isl_set_free(set);
9982 return isl_map_identity(isl_space_map_from_set(dim));
9985 n_div = set->p[0]->n_div;
9986 dim = isl_space_map_from_set(dim);
9987 n_param = isl_space_dim(dim, isl_dim_param);
9988 n_set = isl_space_dim(dim, isl_dim_in);
9989 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9990 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9991 for (i = 0; i < n_set; ++i)
9992 bmap = var_equal(bmap, i);
9994 total = n_param + n_set + n_set + n_div;
9995 for (i = 0; i < n_div; ++i) {
9996 k = isl_basic_map_alloc_inequality(bmap);
9997 if (k < 0)
9998 goto error;
9999 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10000 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10001 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10002 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10003 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10004 set->p[0]->div[i][0]);
10006 l = isl_basic_map_alloc_inequality(bmap);
10007 if (l < 0)
10008 goto error;
10009 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10010 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10011 set->p[0]->div[i][0]);
10012 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10015 isl_set_free(set);
10016 bmap = isl_basic_map_simplify(bmap);
10017 bmap = isl_basic_map_finalize(bmap);
10018 return isl_map_from_basic_map(bmap);
10019 error:
10020 isl_set_free(set);
10021 isl_basic_map_free(bmap);
10022 return NULL;
10025 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10027 unsigned dim;
10028 int size = 0;
10030 if (!bset)
10031 return -1;
10033 dim = isl_basic_set_total_dim(bset);
10034 size += bset->n_eq * (1 + dim);
10035 size += bset->n_ineq * (1 + dim);
10036 size += bset->n_div * (2 + dim);
10038 return size;
10041 int isl_set_size(__isl_keep isl_set *set)
10043 int i;
10044 int size = 0;
10046 if (!set)
10047 return -1;
10049 for (i = 0; i < set->n; ++i)
10050 size += isl_basic_set_size(set->p[i]);
10052 return size;
10055 /* Check if there is any lower bound (if lower == 0) and/or upper
10056 * bound (if upper == 0) on the specified dim.
10058 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10059 enum isl_dim_type type, unsigned pos, int lower, int upper)
10061 int i;
10063 if (!bmap)
10064 return isl_bool_error;
10066 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10067 return isl_bool_error);
10069 pos += isl_basic_map_offset(bmap, type);
10071 for (i = 0; i < bmap->n_div; ++i) {
10072 if (isl_int_is_zero(bmap->div[i][0]))
10073 continue;
10074 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10075 return isl_bool_true;
10078 for (i = 0; i < bmap->n_eq; ++i)
10079 if (!isl_int_is_zero(bmap->eq[i][pos]))
10080 return isl_bool_true;
10082 for (i = 0; i < bmap->n_ineq; ++i) {
10083 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10084 if (sgn > 0)
10085 lower = 1;
10086 if (sgn < 0)
10087 upper = 1;
10090 return lower && upper;
10093 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10094 enum isl_dim_type type, unsigned pos)
10096 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10099 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10100 enum isl_dim_type type, unsigned pos)
10102 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10105 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10106 enum isl_dim_type type, unsigned pos)
10108 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10111 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10112 enum isl_dim_type type, unsigned pos)
10114 int i;
10116 if (!map)
10117 return -1;
10119 for (i = 0; i < map->n; ++i) {
10120 int bounded;
10121 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10122 if (bounded < 0 || !bounded)
10123 return bounded;
10126 return 1;
10129 /* Return 1 if the specified dim is involved in both an upper bound
10130 * and a lower bound.
10132 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10133 enum isl_dim_type type, unsigned pos)
10135 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10138 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10140 static isl_bool has_any_bound(__isl_keep isl_map *map,
10141 enum isl_dim_type type, unsigned pos,
10142 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10143 enum isl_dim_type type, unsigned pos))
10145 int i;
10147 if (!map)
10148 return isl_bool_error;
10150 for (i = 0; i < map->n; ++i) {
10151 isl_bool bounded;
10152 bounded = fn(map->p[i], type, pos);
10153 if (bounded < 0 || bounded)
10154 return bounded;
10157 return isl_bool_false;
10160 /* Return 1 if the specified dim is involved in any lower bound.
10162 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10163 enum isl_dim_type type, unsigned pos)
10165 return has_any_bound(set, type, pos,
10166 &isl_basic_map_dim_has_lower_bound);
10169 /* Return 1 if the specified dim is involved in any upper bound.
10171 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10172 enum isl_dim_type type, unsigned pos)
10174 return has_any_bound(set, type, pos,
10175 &isl_basic_map_dim_has_upper_bound);
10178 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10180 static isl_bool has_bound(__isl_keep isl_map *map,
10181 enum isl_dim_type type, unsigned pos,
10182 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10183 enum isl_dim_type type, unsigned pos))
10185 int i;
10187 if (!map)
10188 return isl_bool_error;
10190 for (i = 0; i < map->n; ++i) {
10191 isl_bool bounded;
10192 bounded = fn(map->p[i], type, pos);
10193 if (bounded < 0 || !bounded)
10194 return bounded;
10197 return isl_bool_true;
10200 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10202 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10203 enum isl_dim_type type, unsigned pos)
10205 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10208 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10210 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10211 enum isl_dim_type type, unsigned pos)
10213 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10216 /* For each of the "n" variables starting at "first", determine
10217 * the sign of the variable and put the results in the first "n"
10218 * elements of the array "signs".
10219 * Sign
10220 * 1 means that the variable is non-negative
10221 * -1 means that the variable is non-positive
10222 * 0 means the variable attains both positive and negative values.
10224 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10225 unsigned first, unsigned n, int *signs)
10227 isl_vec *bound = NULL;
10228 struct isl_tab *tab = NULL;
10229 struct isl_tab_undo *snap;
10230 int i;
10232 if (!bset || !signs)
10233 return -1;
10235 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10236 tab = isl_tab_from_basic_set(bset, 0);
10237 if (!bound || !tab)
10238 goto error;
10240 isl_seq_clr(bound->el, bound->size);
10241 isl_int_set_si(bound->el[0], -1);
10243 snap = isl_tab_snap(tab);
10244 for (i = 0; i < n; ++i) {
10245 int empty;
10247 isl_int_set_si(bound->el[1 + first + i], -1);
10248 if (isl_tab_add_ineq(tab, bound->el) < 0)
10249 goto error;
10250 empty = tab->empty;
10251 isl_int_set_si(bound->el[1 + first + i], 0);
10252 if (isl_tab_rollback(tab, snap) < 0)
10253 goto error;
10255 if (empty) {
10256 signs[i] = 1;
10257 continue;
10260 isl_int_set_si(bound->el[1 + first + i], 1);
10261 if (isl_tab_add_ineq(tab, bound->el) < 0)
10262 goto error;
10263 empty = tab->empty;
10264 isl_int_set_si(bound->el[1 + first + i], 0);
10265 if (isl_tab_rollback(tab, snap) < 0)
10266 goto error;
10268 signs[i] = empty ? -1 : 0;
10271 isl_tab_free(tab);
10272 isl_vec_free(bound);
10273 return 0;
10274 error:
10275 isl_tab_free(tab);
10276 isl_vec_free(bound);
10277 return -1;
10280 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10281 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10283 if (!bset || !signs)
10284 return -1;
10285 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10286 return -1);
10288 first += pos(bset->dim, type) - 1;
10289 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10292 /* Is it possible for the integer division "div" to depend (possibly
10293 * indirectly) on any output dimensions?
10295 * If the div is undefined, then we conservatively assume that it
10296 * may depend on them.
10297 * Otherwise, we check if it actually depends on them or on any integer
10298 * divisions that may depend on them.
10300 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10302 int i;
10303 unsigned n_out, o_out;
10304 unsigned n_div, o_div;
10306 if (isl_int_is_zero(bmap->div[div][0]))
10307 return 1;
10309 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10310 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10312 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10313 return 1;
10315 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10316 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10318 for (i = 0; i < n_div; ++i) {
10319 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10320 continue;
10321 if (div_may_involve_output(bmap, i))
10322 return 1;
10325 return 0;
10328 /* Return the index of the equality of "bmap" that defines
10329 * the output dimension "pos" in terms of earlier dimensions.
10330 * The equality may also involve integer divisions, as long
10331 * as those integer divisions are defined in terms of
10332 * parameters or input dimensions.
10333 * Return bmap->n_eq if there is no such equality.
10334 * Return -1 on error.
10336 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10337 int pos)
10339 int j, k;
10340 unsigned n_out, o_out;
10341 unsigned n_div, o_div;
10343 if (!bmap)
10344 return -1;
10346 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10347 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10348 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10349 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10351 for (j = 0; j < bmap->n_eq; ++j) {
10352 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10353 continue;
10354 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10355 n_out - (pos + 1)) != -1)
10356 continue;
10357 for (k = 0; k < n_div; ++k) {
10358 if (isl_int_is_zero(bmap->eq[j][o_div + k]))
10359 continue;
10360 if (div_may_involve_output(bmap, k))
10361 break;
10363 if (k >= n_div)
10364 return j;
10367 return bmap->n_eq;
10370 /* Check if the given basic map is obviously single-valued.
10371 * In particular, for each output dimension, check that there is
10372 * an equality that defines the output dimension in terms of
10373 * earlier dimensions.
10375 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10377 int i;
10378 unsigned n_out;
10380 if (!bmap)
10381 return isl_bool_error;
10383 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10385 for (i = 0; i < n_out; ++i) {
10386 int eq;
10388 eq = isl_basic_map_output_defining_equality(bmap, i);
10389 if (eq < 0)
10390 return isl_bool_error;
10391 if (eq >= bmap->n_eq)
10392 return isl_bool_false;
10395 return isl_bool_true;
10398 /* Check if the given basic map is single-valued.
10399 * We simply compute
10401 * M \circ M^-1
10403 * and check if the result is a subset of the identity mapping.
10405 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10407 isl_space *space;
10408 isl_basic_map *test;
10409 isl_basic_map *id;
10410 isl_bool sv;
10412 sv = isl_basic_map_plain_is_single_valued(bmap);
10413 if (sv < 0 || sv)
10414 return sv;
10416 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10417 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10419 space = isl_basic_map_get_space(bmap);
10420 space = isl_space_map_from_set(isl_space_range(space));
10421 id = isl_basic_map_identity(space);
10423 sv = isl_basic_map_is_subset(test, id);
10425 isl_basic_map_free(test);
10426 isl_basic_map_free(id);
10428 return sv;
10431 /* Check if the given map is obviously single-valued.
10433 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10435 if (!map)
10436 return isl_bool_error;
10437 if (map->n == 0)
10438 return isl_bool_true;
10439 if (map->n >= 2)
10440 return isl_bool_false;
10442 return isl_basic_map_plain_is_single_valued(map->p[0]);
10445 /* Check if the given map is single-valued.
10446 * We simply compute
10448 * M \circ M^-1
10450 * and check if the result is a subset of the identity mapping.
10452 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10454 isl_space *dim;
10455 isl_map *test;
10456 isl_map *id;
10457 isl_bool sv;
10459 sv = isl_map_plain_is_single_valued(map);
10460 if (sv < 0 || sv)
10461 return sv;
10463 test = isl_map_reverse(isl_map_copy(map));
10464 test = isl_map_apply_range(test, isl_map_copy(map));
10466 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10467 id = isl_map_identity(dim);
10469 sv = isl_map_is_subset(test, id);
10471 isl_map_free(test);
10472 isl_map_free(id);
10474 return sv;
10477 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10479 isl_bool in;
10481 map = isl_map_copy(map);
10482 map = isl_map_reverse(map);
10483 in = isl_map_is_single_valued(map);
10484 isl_map_free(map);
10486 return in;
10489 /* Check if the given map is obviously injective.
10491 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10493 isl_bool in;
10495 map = isl_map_copy(map);
10496 map = isl_map_reverse(map);
10497 in = isl_map_plain_is_single_valued(map);
10498 isl_map_free(map);
10500 return in;
10503 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10505 isl_bool sv;
10507 sv = isl_map_is_single_valued(map);
10508 if (sv < 0 || !sv)
10509 return sv;
10511 return isl_map_is_injective(map);
10514 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10516 return isl_map_is_single_valued((isl_map *)set);
10519 int isl_map_is_translation(__isl_keep isl_map *map)
10521 int ok;
10522 isl_set *delta;
10524 delta = isl_map_deltas(isl_map_copy(map));
10525 ok = isl_set_is_singleton(delta);
10526 isl_set_free(delta);
10528 return ok;
10531 static int unique(isl_int *p, unsigned pos, unsigned len)
10533 if (isl_seq_first_non_zero(p, pos) != -1)
10534 return 0;
10535 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10536 return 0;
10537 return 1;
10540 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10542 int i, j;
10543 unsigned nvar;
10544 unsigned ovar;
10546 if (!bset)
10547 return -1;
10549 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10550 return 0;
10552 nvar = isl_basic_set_dim(bset, isl_dim_set);
10553 ovar = isl_space_offset(bset->dim, isl_dim_set);
10554 for (j = 0; j < nvar; ++j) {
10555 int lower = 0, upper = 0;
10556 for (i = 0; i < bset->n_eq; ++i) {
10557 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10558 continue;
10559 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10560 return 0;
10561 break;
10563 if (i < bset->n_eq)
10564 continue;
10565 for (i = 0; i < bset->n_ineq; ++i) {
10566 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10567 continue;
10568 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10569 return 0;
10570 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10571 lower = 1;
10572 else
10573 upper = 1;
10575 if (!lower || !upper)
10576 return 0;
10579 return 1;
10582 int isl_set_is_box(__isl_keep isl_set *set)
10584 if (!set)
10585 return -1;
10586 if (set->n != 1)
10587 return 0;
10589 return isl_basic_set_is_box(set->p[0]);
10592 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10594 if (!bset)
10595 return isl_bool_error;
10597 return isl_space_is_wrapping(bset->dim);
10600 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
10602 if (!set)
10603 return isl_bool_error;
10605 return isl_space_is_wrapping(set->dim);
10608 /* Modify the space of "map" through a call to "change".
10609 * If "can_change" is set (not NULL), then first call it to check
10610 * if the modification is allowed, printing the error message "cannot_change"
10611 * if it is not.
10613 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
10614 isl_bool (*can_change)(__isl_keep isl_map *map),
10615 const char *cannot_change,
10616 __isl_give isl_space *(*change)(__isl_take isl_space *space))
10618 isl_bool ok;
10619 isl_space *space;
10621 if (!map)
10622 return NULL;
10624 ok = can_change ? can_change(map) : isl_bool_true;
10625 if (ok < 0)
10626 return isl_map_free(map);
10627 if (!ok)
10628 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
10629 return isl_map_free(map));
10631 space = change(isl_map_get_space(map));
10632 map = isl_map_reset_space(map, space);
10634 return map;
10637 /* Is the domain of "map" a wrapped relation?
10639 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10641 if (!map)
10642 return isl_bool_error;
10644 return isl_space_domain_is_wrapping(map->dim);
10647 /* Is the range of "map" a wrapped relation?
10649 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
10651 if (!map)
10652 return isl_bool_error;
10654 return isl_space_range_is_wrapping(map->dim);
10657 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10659 bmap = isl_basic_map_cow(bmap);
10660 if (!bmap)
10661 return NULL;
10663 bmap->dim = isl_space_wrap(bmap->dim);
10664 if (!bmap->dim)
10665 goto error;
10667 bmap = isl_basic_map_finalize(bmap);
10669 return (isl_basic_set *)bmap;
10670 error:
10671 isl_basic_map_free(bmap);
10672 return NULL;
10675 /* Given a map A -> B, return the set (A -> B).
10677 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10679 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
10682 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10684 bset = isl_basic_set_cow(bset);
10685 if (!bset)
10686 return NULL;
10688 bset->dim = isl_space_unwrap(bset->dim);
10689 if (!bset->dim)
10690 goto error;
10692 bset = isl_basic_set_finalize(bset);
10694 return (isl_basic_map *)bset;
10695 error:
10696 isl_basic_set_free(bset);
10697 return NULL;
10700 /* Given a set (A -> B), return the map A -> B.
10701 * Error out if "set" is not of the form (A -> B).
10703 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10705 return isl_map_change_space(set, &isl_set_is_wrapping,
10706 "not a wrapping set", &isl_space_unwrap);
10709 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10710 enum isl_dim_type type)
10712 if (!bmap)
10713 return NULL;
10715 if (!isl_space_is_named_or_nested(bmap->dim, type))
10716 return bmap;
10718 bmap = isl_basic_map_cow(bmap);
10719 if (!bmap)
10720 return NULL;
10722 bmap->dim = isl_space_reset(bmap->dim, type);
10723 if (!bmap->dim)
10724 goto error;
10726 bmap = isl_basic_map_finalize(bmap);
10728 return bmap;
10729 error:
10730 isl_basic_map_free(bmap);
10731 return NULL;
10734 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10735 enum isl_dim_type type)
10737 int i;
10739 if (!map)
10740 return NULL;
10742 if (!isl_space_is_named_or_nested(map->dim, type))
10743 return map;
10745 map = isl_map_cow(map);
10746 if (!map)
10747 return NULL;
10749 for (i = 0; i < map->n; ++i) {
10750 map->p[i] = isl_basic_map_reset(map->p[i], type);
10751 if (!map->p[i])
10752 goto error;
10754 map->dim = isl_space_reset(map->dim, type);
10755 if (!map->dim)
10756 goto error;
10758 return map;
10759 error:
10760 isl_map_free(map);
10761 return NULL;
10764 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10766 if (!bmap)
10767 return NULL;
10769 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10770 return bmap;
10772 bmap = isl_basic_map_cow(bmap);
10773 if (!bmap)
10774 return NULL;
10776 bmap->dim = isl_space_flatten(bmap->dim);
10777 if (!bmap->dim)
10778 goto error;
10780 bmap = isl_basic_map_finalize(bmap);
10782 return bmap;
10783 error:
10784 isl_basic_map_free(bmap);
10785 return NULL;
10788 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10790 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10793 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10794 __isl_take isl_basic_map *bmap)
10796 if (!bmap)
10797 return NULL;
10799 if (!bmap->dim->nested[0])
10800 return bmap;
10802 bmap = isl_basic_map_cow(bmap);
10803 if (!bmap)
10804 return NULL;
10806 bmap->dim = isl_space_flatten_domain(bmap->dim);
10807 if (!bmap->dim)
10808 goto error;
10810 bmap = isl_basic_map_finalize(bmap);
10812 return bmap;
10813 error:
10814 isl_basic_map_free(bmap);
10815 return NULL;
10818 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10819 __isl_take isl_basic_map *bmap)
10821 if (!bmap)
10822 return NULL;
10824 if (!bmap->dim->nested[1])
10825 return bmap;
10827 bmap = isl_basic_map_cow(bmap);
10828 if (!bmap)
10829 return NULL;
10831 bmap->dim = isl_space_flatten_range(bmap->dim);
10832 if (!bmap->dim)
10833 goto error;
10835 bmap = isl_basic_map_finalize(bmap);
10837 return bmap;
10838 error:
10839 isl_basic_map_free(bmap);
10840 return NULL;
10843 /* Remove any internal structure from the spaces of domain and range of "map".
10845 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10847 if (!map)
10848 return NULL;
10850 if (!map->dim->nested[0] && !map->dim->nested[1])
10851 return map;
10853 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
10856 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10858 return (isl_set *)isl_map_flatten((isl_map *)set);
10861 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10863 isl_space *dim, *flat_dim;
10864 isl_map *map;
10866 dim = isl_set_get_space(set);
10867 flat_dim = isl_space_flatten(isl_space_copy(dim));
10868 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10869 map = isl_map_intersect_domain(map, set);
10871 return map;
10874 /* Remove any internal structure from the space of the domain of "map".
10876 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10878 if (!map)
10879 return NULL;
10881 if (!map->dim->nested[0])
10882 return map;
10884 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
10887 /* Remove any internal structure from the space of the range of "map".
10889 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10891 if (!map)
10892 return NULL;
10894 if (!map->dim->nested[1])
10895 return map;
10897 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
10900 /* Reorder the dimensions of "bmap" according to the given dim_map
10901 * and set the dimension specification to "dim".
10903 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10904 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10906 isl_basic_map *res;
10907 unsigned flags;
10909 bmap = isl_basic_map_cow(bmap);
10910 if (!bmap || !dim || !dim_map)
10911 goto error;
10913 flags = bmap->flags;
10914 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10915 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10916 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10917 res = isl_basic_map_alloc_space(dim,
10918 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10919 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10920 if (res)
10921 res->flags = flags;
10922 res = isl_basic_map_finalize(res);
10923 return res;
10924 error:
10925 free(dim_map);
10926 isl_basic_map_free(bmap);
10927 isl_space_free(dim);
10928 return NULL;
10931 /* Reorder the dimensions of "map" according to given reordering.
10933 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10934 __isl_take isl_reordering *r)
10936 int i;
10937 struct isl_dim_map *dim_map;
10939 map = isl_map_cow(map);
10940 dim_map = isl_dim_map_from_reordering(r);
10941 if (!map || !r || !dim_map)
10942 goto error;
10944 for (i = 0; i < map->n; ++i) {
10945 struct isl_dim_map *dim_map_i;
10947 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10949 map->p[i] = isl_basic_map_realign(map->p[i],
10950 isl_space_copy(r->dim), dim_map_i);
10952 if (!map->p[i])
10953 goto error;
10956 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10958 isl_reordering_free(r);
10959 free(dim_map);
10960 return map;
10961 error:
10962 free(dim_map);
10963 isl_map_free(map);
10964 isl_reordering_free(r);
10965 return NULL;
10968 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10969 __isl_take isl_reordering *r)
10971 return (isl_set *)isl_map_realign((isl_map *)set, r);
10974 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10975 __isl_take isl_space *model)
10977 isl_ctx *ctx;
10979 if (!map || !model)
10980 goto error;
10982 ctx = isl_space_get_ctx(model);
10983 if (!isl_space_has_named_params(model))
10984 isl_die(ctx, isl_error_invalid,
10985 "model has unnamed parameters", goto error);
10986 if (!isl_space_has_named_params(map->dim))
10987 isl_die(ctx, isl_error_invalid,
10988 "relation has unnamed parameters", goto error);
10989 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10990 isl_reordering *exp;
10992 model = isl_space_drop_dims(model, isl_dim_in,
10993 0, isl_space_dim(model, isl_dim_in));
10994 model = isl_space_drop_dims(model, isl_dim_out,
10995 0, isl_space_dim(model, isl_dim_out));
10996 exp = isl_parameter_alignment_reordering(map->dim, model);
10997 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10998 map = isl_map_realign(map, exp);
11001 isl_space_free(model);
11002 return map;
11003 error:
11004 isl_space_free(model);
11005 isl_map_free(map);
11006 return NULL;
11009 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11010 __isl_take isl_space *model)
11012 return isl_map_align_params(set, model);
11015 /* Align the parameters of "bmap" to those of "model", introducing
11016 * additional parameters if needed.
11018 __isl_give isl_basic_map *isl_basic_map_align_params(
11019 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11021 isl_ctx *ctx;
11023 if (!bmap || !model)
11024 goto error;
11026 ctx = isl_space_get_ctx(model);
11027 if (!isl_space_has_named_params(model))
11028 isl_die(ctx, isl_error_invalid,
11029 "model has unnamed parameters", goto error);
11030 if (!isl_space_has_named_params(bmap->dim))
11031 isl_die(ctx, isl_error_invalid,
11032 "relation has unnamed parameters", goto error);
11033 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11034 isl_reordering *exp;
11035 struct isl_dim_map *dim_map;
11037 model = isl_space_drop_dims(model, isl_dim_in,
11038 0, isl_space_dim(model, isl_dim_in));
11039 model = isl_space_drop_dims(model, isl_dim_out,
11040 0, isl_space_dim(model, isl_dim_out));
11041 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11042 exp = isl_reordering_extend_space(exp,
11043 isl_basic_map_get_space(bmap));
11044 dim_map = isl_dim_map_from_reordering(exp);
11045 bmap = isl_basic_map_realign(bmap,
11046 exp ? isl_space_copy(exp->dim) : NULL,
11047 isl_dim_map_extend(dim_map, bmap));
11048 isl_reordering_free(exp);
11049 free(dim_map);
11052 isl_space_free(model);
11053 return bmap;
11054 error:
11055 isl_space_free(model);
11056 isl_basic_map_free(bmap);
11057 return NULL;
11060 /* Align the parameters of "bset" to those of "model", introducing
11061 * additional parameters if needed.
11063 __isl_give isl_basic_set *isl_basic_set_align_params(
11064 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11066 return isl_basic_map_align_params(bset, model);
11069 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11070 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11071 enum isl_dim_type c2, enum isl_dim_type c3,
11072 enum isl_dim_type c4, enum isl_dim_type c5)
11074 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11075 struct isl_mat *mat;
11076 int i, j, k;
11077 int pos;
11079 if (!bmap)
11080 return NULL;
11081 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11082 isl_basic_map_total_dim(bmap) + 1);
11083 if (!mat)
11084 return NULL;
11085 for (i = 0; i < bmap->n_eq; ++i)
11086 for (j = 0, pos = 0; j < 5; ++j) {
11087 int off = isl_basic_map_offset(bmap, c[j]);
11088 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11089 isl_int_set(mat->row[i][pos],
11090 bmap->eq[i][off + k]);
11091 ++pos;
11095 return mat;
11098 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11099 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11100 enum isl_dim_type c2, enum isl_dim_type c3,
11101 enum isl_dim_type c4, enum isl_dim_type c5)
11103 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11104 struct isl_mat *mat;
11105 int i, j, k;
11106 int pos;
11108 if (!bmap)
11109 return NULL;
11110 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11111 isl_basic_map_total_dim(bmap) + 1);
11112 if (!mat)
11113 return NULL;
11114 for (i = 0; i < bmap->n_ineq; ++i)
11115 for (j = 0, pos = 0; j < 5; ++j) {
11116 int off = isl_basic_map_offset(bmap, c[j]);
11117 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11118 isl_int_set(mat->row[i][pos],
11119 bmap->ineq[i][off + k]);
11120 ++pos;
11124 return mat;
11127 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11128 __isl_take isl_space *dim,
11129 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11130 enum isl_dim_type c2, enum isl_dim_type c3,
11131 enum isl_dim_type c4, enum isl_dim_type c5)
11133 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11134 isl_basic_map *bmap;
11135 unsigned total;
11136 unsigned extra;
11137 int i, j, k, l;
11138 int pos;
11140 if (!dim || !eq || !ineq)
11141 goto error;
11143 if (eq->n_col != ineq->n_col)
11144 isl_die(dim->ctx, isl_error_invalid,
11145 "equalities and inequalities matrices should have "
11146 "same number of columns", goto error);
11148 total = 1 + isl_space_dim(dim, isl_dim_all);
11150 if (eq->n_col < total)
11151 isl_die(dim->ctx, isl_error_invalid,
11152 "number of columns too small", goto error);
11154 extra = eq->n_col - total;
11156 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11157 eq->n_row, ineq->n_row);
11158 if (!bmap)
11159 goto error;
11160 for (i = 0; i < extra; ++i) {
11161 k = isl_basic_map_alloc_div(bmap);
11162 if (k < 0)
11163 goto error;
11164 isl_int_set_si(bmap->div[k][0], 0);
11166 for (i = 0; i < eq->n_row; ++i) {
11167 l = isl_basic_map_alloc_equality(bmap);
11168 if (l < 0)
11169 goto error;
11170 for (j = 0, pos = 0; j < 5; ++j) {
11171 int off = isl_basic_map_offset(bmap, c[j]);
11172 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11173 isl_int_set(bmap->eq[l][off + k],
11174 eq->row[i][pos]);
11175 ++pos;
11179 for (i = 0; i < ineq->n_row; ++i) {
11180 l = isl_basic_map_alloc_inequality(bmap);
11181 if (l < 0)
11182 goto error;
11183 for (j = 0, pos = 0; j < 5; ++j) {
11184 int off = isl_basic_map_offset(bmap, c[j]);
11185 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11186 isl_int_set(bmap->ineq[l][off + k],
11187 ineq->row[i][pos]);
11188 ++pos;
11193 isl_space_free(dim);
11194 isl_mat_free(eq);
11195 isl_mat_free(ineq);
11197 bmap = isl_basic_map_simplify(bmap);
11198 return isl_basic_map_finalize(bmap);
11199 error:
11200 isl_space_free(dim);
11201 isl_mat_free(eq);
11202 isl_mat_free(ineq);
11203 return NULL;
11206 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11207 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11208 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11210 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11211 c1, c2, c3, c4, isl_dim_in);
11214 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11215 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11216 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11218 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11219 c1, c2, c3, c4, isl_dim_in);
11222 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11223 __isl_take isl_space *dim,
11224 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11225 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11227 return (isl_basic_set*)
11228 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11229 c1, c2, c3, c4, isl_dim_in);
11232 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11234 if (!bmap)
11235 return isl_bool_error;
11237 return isl_space_can_zip(bmap->dim);
11240 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11242 if (!map)
11243 return isl_bool_error;
11245 return isl_space_can_zip(map->dim);
11248 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11249 * (A -> C) -> (B -> D).
11251 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11253 unsigned pos;
11254 unsigned n1;
11255 unsigned n2;
11257 if (!bmap)
11258 return NULL;
11260 if (!isl_basic_map_can_zip(bmap))
11261 isl_die(bmap->ctx, isl_error_invalid,
11262 "basic map cannot be zipped", goto error);
11263 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11264 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11265 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11266 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11267 bmap = isl_basic_map_cow(bmap);
11268 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11269 if (!bmap)
11270 return NULL;
11271 bmap->dim = isl_space_zip(bmap->dim);
11272 if (!bmap->dim)
11273 goto error;
11274 bmap = isl_basic_map_mark_final(bmap);
11275 return bmap;
11276 error:
11277 isl_basic_map_free(bmap);
11278 return NULL;
11281 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11282 * (A -> C) -> (B -> D).
11284 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11286 int i;
11288 if (!map)
11289 return NULL;
11291 if (!isl_map_can_zip(map))
11292 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11293 goto error);
11295 map = isl_map_cow(map);
11296 if (!map)
11297 return NULL;
11299 for (i = 0; i < map->n; ++i) {
11300 map->p[i] = isl_basic_map_zip(map->p[i]);
11301 if (!map->p[i])
11302 goto error;
11305 map->dim = isl_space_zip(map->dim);
11306 if (!map->dim)
11307 goto error;
11309 return map;
11310 error:
11311 isl_map_free(map);
11312 return NULL;
11315 /* Can we apply isl_basic_map_curry to "bmap"?
11316 * That is, does it have a nested relation in its domain?
11318 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11320 if (!bmap)
11321 return isl_bool_error;
11323 return isl_space_can_curry(bmap->dim);
11326 /* Can we apply isl_map_curry to "map"?
11327 * That is, does it have a nested relation in its domain?
11329 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11331 if (!map)
11332 return isl_bool_error;
11334 return isl_space_can_curry(map->dim);
11337 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11338 * A -> (B -> C).
11340 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11343 if (!bmap)
11344 return NULL;
11346 if (!isl_basic_map_can_curry(bmap))
11347 isl_die(bmap->ctx, isl_error_invalid,
11348 "basic map cannot be curried", goto error);
11349 bmap = isl_basic_map_cow(bmap);
11350 if (!bmap)
11351 return NULL;
11352 bmap->dim = isl_space_curry(bmap->dim);
11353 if (!bmap->dim)
11354 goto error;
11355 bmap = isl_basic_map_mark_final(bmap);
11356 return bmap;
11357 error:
11358 isl_basic_map_free(bmap);
11359 return NULL;
11362 /* Given a map (A -> B) -> C, return the corresponding map
11363 * A -> (B -> C).
11365 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11367 return isl_map_change_space(map, &isl_map_can_curry,
11368 "map cannot be curried", &isl_space_curry);
11371 /* Can isl_map_range_curry be applied to "map"?
11372 * That is, does it have a nested relation in its range,
11373 * the domain of which is itself a nested relation?
11375 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11377 if (!map)
11378 return isl_bool_error;
11380 return isl_space_can_range_curry(map->dim);
11383 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11384 * A -> (B -> (C -> D)).
11386 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11388 return isl_map_change_space(map, &isl_map_can_range_curry,
11389 "map range cannot be curried",
11390 &isl_space_range_curry);
11393 /* Can we apply isl_basic_map_uncurry to "bmap"?
11394 * That is, does it have a nested relation in its domain?
11396 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11398 if (!bmap)
11399 return isl_bool_error;
11401 return isl_space_can_uncurry(bmap->dim);
11404 /* Can we apply isl_map_uncurry to "map"?
11405 * That is, does it have a nested relation in its domain?
11407 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11409 if (!map)
11410 return isl_bool_error;
11412 return isl_space_can_uncurry(map->dim);
11415 /* Given a basic map A -> (B -> C), return the corresponding basic map
11416 * (A -> B) -> C.
11418 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11421 if (!bmap)
11422 return NULL;
11424 if (!isl_basic_map_can_uncurry(bmap))
11425 isl_die(bmap->ctx, isl_error_invalid,
11426 "basic map cannot be uncurried",
11427 return isl_basic_map_free(bmap));
11428 bmap = isl_basic_map_cow(bmap);
11429 if (!bmap)
11430 return NULL;
11431 bmap->dim = isl_space_uncurry(bmap->dim);
11432 if (!bmap->dim)
11433 return isl_basic_map_free(bmap);
11434 bmap = isl_basic_map_mark_final(bmap);
11435 return bmap;
11438 /* Given a map A -> (B -> C), return the corresponding map
11439 * (A -> B) -> C.
11441 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11443 return isl_map_change_space(map, &isl_map_can_uncurry,
11444 "map cannot be uncurried", &isl_space_uncurry);
11447 /* Construct a basic map mapping the domain of the affine expression
11448 * to a one-dimensional range prescribed by the affine expression.
11450 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11452 int k;
11453 int pos;
11454 isl_local_space *ls;
11455 isl_basic_map *bmap;
11457 if (!aff)
11458 return NULL;
11460 ls = isl_aff_get_local_space(aff);
11461 bmap = isl_basic_map_from_local_space(ls);
11462 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11463 k = isl_basic_map_alloc_equality(bmap);
11464 if (k < 0)
11465 goto error;
11467 pos = isl_basic_map_offset(bmap, isl_dim_out);
11468 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11469 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11470 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11471 aff->v->size - (pos + 1));
11473 isl_aff_free(aff);
11474 bmap = isl_basic_map_finalize(bmap);
11475 return bmap;
11476 error:
11477 isl_aff_free(aff);
11478 isl_basic_map_free(bmap);
11479 return NULL;
11482 /* Construct a map mapping the domain of the affine expression
11483 * to a one-dimensional range prescribed by the affine expression.
11485 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11487 isl_basic_map *bmap;
11489 bmap = isl_basic_map_from_aff(aff);
11490 return isl_map_from_basic_map(bmap);
11493 /* Construct a basic map mapping the domain the multi-affine expression
11494 * to its range, with each dimension in the range equated to the
11495 * corresponding affine expression.
11497 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11498 __isl_take isl_multi_aff *maff)
11500 int i;
11501 isl_space *space;
11502 isl_basic_map *bmap;
11504 if (!maff)
11505 return NULL;
11507 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11508 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11509 "invalid space", goto error);
11511 space = isl_space_domain(isl_multi_aff_get_space(maff));
11512 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11514 for (i = 0; i < maff->n; ++i) {
11515 isl_aff *aff;
11516 isl_basic_map *bmap_i;
11518 aff = isl_aff_copy(maff->p[i]);
11519 bmap_i = isl_basic_map_from_aff(aff);
11521 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11524 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11526 isl_multi_aff_free(maff);
11527 return bmap;
11528 error:
11529 isl_multi_aff_free(maff);
11530 return NULL;
11533 /* Construct a map mapping the domain the multi-affine expression
11534 * to its range, with each dimension in the range equated to the
11535 * corresponding affine expression.
11537 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11539 isl_basic_map *bmap;
11541 bmap = isl_basic_map_from_multi_aff(maff);
11542 return isl_map_from_basic_map(bmap);
11545 /* Construct a basic map mapping a domain in the given space to
11546 * to an n-dimensional range, with n the number of elements in the list,
11547 * where each coordinate in the range is prescribed by the
11548 * corresponding affine expression.
11549 * The domains of all affine expressions in the list are assumed to match
11550 * domain_dim.
11552 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11553 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11555 int i;
11556 isl_space *dim;
11557 isl_basic_map *bmap;
11559 if (!list)
11560 return NULL;
11562 dim = isl_space_from_domain(domain_dim);
11563 bmap = isl_basic_map_universe(dim);
11565 for (i = 0; i < list->n; ++i) {
11566 isl_aff *aff;
11567 isl_basic_map *bmap_i;
11569 aff = isl_aff_copy(list->p[i]);
11570 bmap_i = isl_basic_map_from_aff(aff);
11572 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11575 isl_aff_list_free(list);
11576 return bmap;
11579 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11580 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11582 return isl_map_equate(set, type1, pos1, type2, pos2);
11585 /* Construct a basic map where the given dimensions are equal to each other.
11587 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11588 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11590 isl_basic_map *bmap = NULL;
11591 int i;
11593 if (!space)
11594 return NULL;
11596 if (pos1 >= isl_space_dim(space, type1))
11597 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11598 "index out of bounds", goto error);
11599 if (pos2 >= isl_space_dim(space, type2))
11600 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11601 "index out of bounds", goto error);
11603 if (type1 == type2 && pos1 == pos2)
11604 return isl_basic_map_universe(space);
11606 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11607 i = isl_basic_map_alloc_equality(bmap);
11608 if (i < 0)
11609 goto error;
11610 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11611 pos1 += isl_basic_map_offset(bmap, type1);
11612 pos2 += isl_basic_map_offset(bmap, type2);
11613 isl_int_set_si(bmap->eq[i][pos1], -1);
11614 isl_int_set_si(bmap->eq[i][pos2], 1);
11615 bmap = isl_basic_map_finalize(bmap);
11616 isl_space_free(space);
11617 return bmap;
11618 error:
11619 isl_space_free(space);
11620 isl_basic_map_free(bmap);
11621 return NULL;
11624 /* Add a constraint imposing that the given two dimensions are equal.
11626 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11627 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11629 isl_basic_map *eq;
11631 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11633 bmap = isl_basic_map_intersect(bmap, eq);
11635 return bmap;
11638 /* Add a constraint imposing that the given two dimensions are equal.
11640 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11641 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11643 isl_basic_map *bmap;
11645 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11647 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11649 return map;
11652 /* Add a constraint imposing that the given two dimensions have opposite values.
11654 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11655 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11657 isl_basic_map *bmap = NULL;
11658 int i;
11660 if (!map)
11661 return NULL;
11663 if (pos1 >= isl_map_dim(map, type1))
11664 isl_die(map->ctx, isl_error_invalid,
11665 "index out of bounds", goto error);
11666 if (pos2 >= isl_map_dim(map, type2))
11667 isl_die(map->ctx, isl_error_invalid,
11668 "index out of bounds", goto error);
11670 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11671 i = isl_basic_map_alloc_equality(bmap);
11672 if (i < 0)
11673 goto error;
11674 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11675 pos1 += isl_basic_map_offset(bmap, type1);
11676 pos2 += isl_basic_map_offset(bmap, type2);
11677 isl_int_set_si(bmap->eq[i][pos1], 1);
11678 isl_int_set_si(bmap->eq[i][pos2], 1);
11679 bmap = isl_basic_map_finalize(bmap);
11681 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11683 return map;
11684 error:
11685 isl_basic_map_free(bmap);
11686 isl_map_free(map);
11687 return NULL;
11690 /* Construct a constraint imposing that the value of the first dimension is
11691 * greater than or equal to that of the second.
11693 static __isl_give isl_constraint *constraint_order_ge(
11694 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11695 enum isl_dim_type type2, int pos2)
11697 isl_constraint *c;
11699 if (!space)
11700 return NULL;
11702 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
11704 if (pos1 >= isl_constraint_dim(c, type1))
11705 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11706 "index out of bounds", return isl_constraint_free(c));
11707 if (pos2 >= isl_constraint_dim(c, type2))
11708 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11709 "index out of bounds", return isl_constraint_free(c));
11711 if (type1 == type2 && pos1 == pos2)
11712 return c;
11714 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11715 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11717 return c;
11720 /* Add a constraint imposing that the value of the first dimension is
11721 * greater than or equal to that of the second.
11723 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11724 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11726 isl_constraint *c;
11727 isl_space *space;
11729 if (type1 == type2 && pos1 == pos2)
11730 return bmap;
11731 space = isl_basic_map_get_space(bmap);
11732 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11733 bmap = isl_basic_map_add_constraint(bmap, c);
11735 return bmap;
11738 /* Add a constraint imposing that the value of the first dimension is
11739 * greater than or equal to that of the second.
11741 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11742 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11744 isl_constraint *c;
11745 isl_space *space;
11747 if (type1 == type2 && pos1 == pos2)
11748 return map;
11749 space = isl_map_get_space(map);
11750 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11751 map = isl_map_add_constraint(map, c);
11753 return map;
11756 /* Add a constraint imposing that the value of the first dimension is
11757 * less than or equal to that of the second.
11759 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11760 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11762 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11765 /* Construct a basic map where the value of the first dimension is
11766 * greater than that of the second.
11768 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11769 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11771 isl_basic_map *bmap = NULL;
11772 int i;
11774 if (!space)
11775 return NULL;
11777 if (pos1 >= isl_space_dim(space, type1))
11778 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11779 "index out of bounds", goto error);
11780 if (pos2 >= isl_space_dim(space, type2))
11781 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11782 "index out of bounds", goto error);
11784 if (type1 == type2 && pos1 == pos2)
11785 return isl_basic_map_empty(space);
11787 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11788 i = isl_basic_map_alloc_inequality(bmap);
11789 if (i < 0)
11790 return isl_basic_map_free(bmap);
11791 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11792 pos1 += isl_basic_map_offset(bmap, type1);
11793 pos2 += isl_basic_map_offset(bmap, type2);
11794 isl_int_set_si(bmap->ineq[i][pos1], 1);
11795 isl_int_set_si(bmap->ineq[i][pos2], -1);
11796 isl_int_set_si(bmap->ineq[i][0], -1);
11797 bmap = isl_basic_map_finalize(bmap);
11799 return bmap;
11800 error:
11801 isl_space_free(space);
11802 isl_basic_map_free(bmap);
11803 return NULL;
11806 /* Add a constraint imposing that the value of the first dimension is
11807 * greater than that of the second.
11809 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11810 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11812 isl_basic_map *gt;
11814 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11816 bmap = isl_basic_map_intersect(bmap, gt);
11818 return bmap;
11821 /* Add a constraint imposing that the value of the first dimension is
11822 * greater than that of the second.
11824 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11825 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11827 isl_basic_map *bmap;
11829 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11831 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11833 return map;
11836 /* Add a constraint imposing that the value of the first dimension is
11837 * smaller than that of the second.
11839 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11840 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11842 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11845 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11846 int pos)
11848 isl_aff *div;
11849 isl_local_space *ls;
11851 if (!bmap)
11852 return NULL;
11854 if (!isl_basic_map_divs_known(bmap))
11855 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11856 "some divs are unknown", return NULL);
11858 ls = isl_basic_map_get_local_space(bmap);
11859 div = isl_local_space_get_div(ls, pos);
11860 isl_local_space_free(ls);
11862 return div;
11865 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11866 int pos)
11868 return isl_basic_map_get_div(bset, pos);
11871 /* Plug in "subs" for dimension "type", "pos" of "bset".
11873 * Let i be the dimension to replace and let "subs" be of the form
11875 * f/d
11877 * Any integer division with a non-zero coefficient for i,
11879 * floor((a i + g)/m)
11881 * is replaced by
11883 * floor((a f + d g)/(m d))
11885 * Constraints of the form
11887 * a i + g
11889 * are replaced by
11891 * a f + d g
11893 * We currently require that "subs" is an integral expression.
11894 * Handling rational expressions may require us to add stride constraints
11895 * as we do in isl_basic_set_preimage_multi_aff.
11897 __isl_give isl_basic_set *isl_basic_set_substitute(
11898 __isl_take isl_basic_set *bset,
11899 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11901 int i;
11902 isl_int v;
11903 isl_ctx *ctx;
11905 if (bset && isl_basic_set_plain_is_empty(bset))
11906 return bset;
11908 bset = isl_basic_set_cow(bset);
11909 if (!bset || !subs)
11910 goto error;
11912 ctx = isl_basic_set_get_ctx(bset);
11913 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11914 isl_die(ctx, isl_error_invalid,
11915 "spaces don't match", goto error);
11916 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11917 isl_die(ctx, isl_error_unsupported,
11918 "cannot handle divs yet", goto error);
11919 if (!isl_int_is_one(subs->v->el[0]))
11920 isl_die(ctx, isl_error_invalid,
11921 "can only substitute integer expressions", goto error);
11923 pos += isl_basic_set_offset(bset, type);
11925 isl_int_init(v);
11927 for (i = 0; i < bset->n_eq; ++i) {
11928 if (isl_int_is_zero(bset->eq[i][pos]))
11929 continue;
11930 isl_int_set(v, bset->eq[i][pos]);
11931 isl_int_set_si(bset->eq[i][pos], 0);
11932 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11933 v, subs->v->el + 1, subs->v->size - 1);
11936 for (i = 0; i < bset->n_ineq; ++i) {
11937 if (isl_int_is_zero(bset->ineq[i][pos]))
11938 continue;
11939 isl_int_set(v, bset->ineq[i][pos]);
11940 isl_int_set_si(bset->ineq[i][pos], 0);
11941 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11942 v, subs->v->el + 1, subs->v->size - 1);
11945 for (i = 0; i < bset->n_div; ++i) {
11946 if (isl_int_is_zero(bset->div[i][1 + pos]))
11947 continue;
11948 isl_int_set(v, bset->div[i][1 + pos]);
11949 isl_int_set_si(bset->div[i][1 + pos], 0);
11950 isl_seq_combine(bset->div[i] + 1,
11951 subs->v->el[0], bset->div[i] + 1,
11952 v, subs->v->el + 1, subs->v->size - 1);
11953 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11956 isl_int_clear(v);
11958 bset = isl_basic_set_simplify(bset);
11959 return isl_basic_set_finalize(bset);
11960 error:
11961 isl_basic_set_free(bset);
11962 return NULL;
11965 /* Plug in "subs" for dimension "type", "pos" of "set".
11967 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11968 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11970 int i;
11972 if (set && isl_set_plain_is_empty(set))
11973 return set;
11975 set = isl_set_cow(set);
11976 if (!set || !subs)
11977 goto error;
11979 for (i = set->n - 1; i >= 0; --i) {
11980 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11981 if (remove_if_empty(set, i) < 0)
11982 goto error;
11985 return set;
11986 error:
11987 isl_set_free(set);
11988 return NULL;
11991 /* Check if the range of "ma" is compatible with the domain or range
11992 * (depending on "type") of "bmap".
11993 * Return -1 if anything is wrong.
11995 static int check_basic_map_compatible_range_multi_aff(
11996 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11997 __isl_keep isl_multi_aff *ma)
11999 int m;
12000 isl_space *ma_space;
12002 ma_space = isl_multi_aff_get_space(ma);
12004 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12005 if (m < 0)
12006 goto error;
12007 if (!m)
12008 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12009 "parameters don't match", goto error);
12010 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12011 if (m < 0)
12012 goto error;
12013 if (!m)
12014 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12015 "spaces don't match", goto error);
12017 isl_space_free(ma_space);
12018 return m;
12019 error:
12020 isl_space_free(ma_space);
12021 return -1;
12024 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12025 * coefficients before the transformed range of dimensions,
12026 * the "n_after" coefficients after the transformed range of dimensions
12027 * and the coefficients of the other divs in "bmap".
12029 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12030 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12032 int i;
12033 int n_param;
12034 int n_set;
12035 isl_local_space *ls;
12037 if (n_div == 0)
12038 return 0;
12040 ls = isl_aff_get_domain_local_space(ma->p[0]);
12041 if (!ls)
12042 return -1;
12044 n_param = isl_local_space_dim(ls, isl_dim_param);
12045 n_set = isl_local_space_dim(ls, isl_dim_set);
12046 for (i = 0; i < n_div; ++i) {
12047 int o_bmap = 0, o_ls = 0;
12049 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12050 o_bmap += 1 + 1 + n_param;
12051 o_ls += 1 + 1 + n_param;
12052 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12053 o_bmap += n_before;
12054 isl_seq_cpy(bmap->div[i] + o_bmap,
12055 ls->div->row[i] + o_ls, n_set);
12056 o_bmap += n_set;
12057 o_ls += n_set;
12058 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12059 o_bmap += n_after;
12060 isl_seq_cpy(bmap->div[i] + o_bmap,
12061 ls->div->row[i] + o_ls, n_div);
12062 o_bmap += n_div;
12063 o_ls += n_div;
12064 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12065 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12066 goto error;
12069 isl_local_space_free(ls);
12070 return 0;
12071 error:
12072 isl_local_space_free(ls);
12073 return -1;
12076 /* How many stride constraints does "ma" enforce?
12077 * That is, how many of the affine expressions have a denominator
12078 * different from one?
12080 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12082 int i;
12083 int strides = 0;
12085 for (i = 0; i < ma->n; ++i)
12086 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12087 strides++;
12089 return strides;
12092 /* For each affine expression in ma of the form
12094 * x_i = (f_i y + h_i)/m_i
12096 * with m_i different from one, add a constraint to "bmap"
12097 * of the form
12099 * f_i y + h_i = m_i alpha_i
12101 * with alpha_i an additional existentially quantified variable.
12103 static __isl_give isl_basic_map *add_ma_strides(
12104 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12105 int n_before, int n_after)
12107 int i, k;
12108 int div;
12109 int total;
12110 int n_param;
12111 int n_in;
12112 int n_div;
12114 total = isl_basic_map_total_dim(bmap);
12115 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12116 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12117 n_div = isl_multi_aff_dim(ma, isl_dim_div);
12118 for (i = 0; i < ma->n; ++i) {
12119 int o_bmap = 0, o_ma = 1;
12121 if (isl_int_is_one(ma->p[i]->v->el[0]))
12122 continue;
12123 div = isl_basic_map_alloc_div(bmap);
12124 k = isl_basic_map_alloc_equality(bmap);
12125 if (div < 0 || k < 0)
12126 goto error;
12127 isl_int_set_si(bmap->div[div][0], 0);
12128 isl_seq_cpy(bmap->eq[k] + o_bmap,
12129 ma->p[i]->v->el + o_ma, 1 + n_param);
12130 o_bmap += 1 + n_param;
12131 o_ma += 1 + n_param;
12132 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12133 o_bmap += n_before;
12134 isl_seq_cpy(bmap->eq[k] + o_bmap,
12135 ma->p[i]->v->el + o_ma, n_in);
12136 o_bmap += n_in;
12137 o_ma += n_in;
12138 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12139 o_bmap += n_after;
12140 isl_seq_cpy(bmap->eq[k] + o_bmap,
12141 ma->p[i]->v->el + o_ma, n_div);
12142 o_bmap += n_div;
12143 o_ma += n_div;
12144 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12145 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12146 total++;
12149 return bmap;
12150 error:
12151 isl_basic_map_free(bmap);
12152 return NULL;
12155 /* Replace the domain or range space (depending on "type) of "space" by "set".
12157 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12158 enum isl_dim_type type, __isl_take isl_space *set)
12160 if (type == isl_dim_in) {
12161 space = isl_space_range(space);
12162 space = isl_space_map_from_domain_and_range(set, space);
12163 } else {
12164 space = isl_space_domain(space);
12165 space = isl_space_map_from_domain_and_range(space, set);
12168 return space;
12171 /* Compute the preimage of the domain or range (depending on "type")
12172 * of "bmap" under the function represented by "ma".
12173 * In other words, plug in "ma" in the domain or range of "bmap".
12174 * The result is a basic map that lives in the same space as "bmap"
12175 * except that the domain or range has been replaced by
12176 * the domain space of "ma".
12178 * If bmap is represented by
12180 * A(p) + S u + B x + T v + C(divs) >= 0,
12182 * where u and x are input and output dimensions if type == isl_dim_out
12183 * while x and v are input and output dimensions if type == isl_dim_in,
12184 * and ma is represented by
12186 * x = D(p) + F(y) + G(divs')
12188 * then the result is
12190 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12192 * The divs in the input set are similarly adjusted.
12193 * In particular
12195 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12197 * becomes
12199 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12200 * B_i G(divs') + c_i(divs))/n_i)
12202 * If bmap is not a rational map and if F(y) involves any denominators
12204 * x_i = (f_i y + h_i)/m_i
12206 * then additional constraints are added to ensure that we only
12207 * map back integer points. That is we enforce
12209 * f_i y + h_i = m_i alpha_i
12211 * with alpha_i an additional existentially quantified variable.
12213 * We first copy over the divs from "ma".
12214 * Then we add the modified constraints and divs from "bmap".
12215 * Finally, we add the stride constraints, if needed.
12217 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12218 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12219 __isl_take isl_multi_aff *ma)
12221 int i, k;
12222 isl_space *space;
12223 isl_basic_map *res = NULL;
12224 int n_before, n_after, n_div_bmap, n_div_ma;
12225 isl_int f, c1, c2, g;
12226 int rational, strides;
12228 isl_int_init(f);
12229 isl_int_init(c1);
12230 isl_int_init(c2);
12231 isl_int_init(g);
12233 ma = isl_multi_aff_align_divs(ma);
12234 if (!bmap || !ma)
12235 goto error;
12236 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12237 goto error;
12239 if (type == isl_dim_in) {
12240 n_before = 0;
12241 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12242 } else {
12243 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12244 n_after = 0;
12246 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12247 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12249 space = isl_multi_aff_get_domain_space(ma);
12250 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12251 rational = isl_basic_map_is_rational(bmap);
12252 strides = rational ? 0 : multi_aff_strides(ma);
12253 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12254 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12255 if (rational)
12256 res = isl_basic_map_set_rational(res);
12258 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12259 if (isl_basic_map_alloc_div(res) < 0)
12260 goto error;
12262 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12263 goto error;
12265 for (i = 0; i < bmap->n_eq; ++i) {
12266 k = isl_basic_map_alloc_equality(res);
12267 if (k < 0)
12268 goto error;
12269 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12270 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12273 for (i = 0; i < bmap->n_ineq; ++i) {
12274 k = isl_basic_map_alloc_inequality(res);
12275 if (k < 0)
12276 goto error;
12277 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12278 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12281 for (i = 0; i < bmap->n_div; ++i) {
12282 if (isl_int_is_zero(bmap->div[i][0])) {
12283 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12284 continue;
12286 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12287 n_before, n_after, n_div_ma, n_div_bmap,
12288 f, c1, c2, g, 1);
12291 if (strides)
12292 res = add_ma_strides(res, ma, n_before, n_after);
12294 isl_int_clear(f);
12295 isl_int_clear(c1);
12296 isl_int_clear(c2);
12297 isl_int_clear(g);
12298 isl_basic_map_free(bmap);
12299 isl_multi_aff_free(ma);
12300 res = isl_basic_set_simplify(res);
12301 return isl_basic_map_finalize(res);
12302 error:
12303 isl_int_clear(f);
12304 isl_int_clear(c1);
12305 isl_int_clear(c2);
12306 isl_int_clear(g);
12307 isl_basic_map_free(bmap);
12308 isl_multi_aff_free(ma);
12309 isl_basic_map_free(res);
12310 return NULL;
12313 /* Compute the preimage of "bset" under the function represented by "ma".
12314 * In other words, plug in "ma" in "bset". The result is a basic set
12315 * that lives in the domain space of "ma".
12317 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12318 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12320 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12323 /* Compute the preimage of the domain of "bmap" under the function
12324 * represented by "ma".
12325 * In other words, plug in "ma" in the domain of "bmap".
12326 * The result is a basic map that lives in the same space as "bmap"
12327 * except that the domain has been replaced by the domain space of "ma".
12329 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12330 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12332 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12335 /* Compute the preimage of the range of "bmap" under the function
12336 * represented by "ma".
12337 * In other words, plug in "ma" in the range of "bmap".
12338 * The result is a basic map that lives in the same space as "bmap"
12339 * except that the range has been replaced by the domain space of "ma".
12341 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12342 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12344 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12347 /* Check if the range of "ma" is compatible with the domain or range
12348 * (depending on "type") of "map".
12349 * Return -1 if anything is wrong.
12351 static int check_map_compatible_range_multi_aff(
12352 __isl_keep isl_map *map, enum isl_dim_type type,
12353 __isl_keep isl_multi_aff *ma)
12355 int m;
12356 isl_space *ma_space;
12358 ma_space = isl_multi_aff_get_space(ma);
12359 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12360 isl_space_free(ma_space);
12361 if (m >= 0 && !m)
12362 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12363 "spaces don't match", return -1);
12364 return m;
12367 /* Compute the preimage of the domain or range (depending on "type")
12368 * of "map" under the function represented by "ma".
12369 * In other words, plug in "ma" in the domain or range of "map".
12370 * The result is a map that lives in the same space as "map"
12371 * except that the domain or range has been replaced by
12372 * the domain space of "ma".
12374 * The parameters are assumed to have been aligned.
12376 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12377 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12379 int i;
12380 isl_space *space;
12382 map = isl_map_cow(map);
12383 ma = isl_multi_aff_align_divs(ma);
12384 if (!map || !ma)
12385 goto error;
12386 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12387 goto error;
12389 for (i = 0; i < map->n; ++i) {
12390 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12391 isl_multi_aff_copy(ma));
12392 if (!map->p[i])
12393 goto error;
12396 space = isl_multi_aff_get_domain_space(ma);
12397 space = isl_space_set(isl_map_get_space(map), type, space);
12399 isl_space_free(map->dim);
12400 map->dim = space;
12401 if (!map->dim)
12402 goto error;
12404 isl_multi_aff_free(ma);
12405 if (map->n > 1)
12406 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12407 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12408 return map;
12409 error:
12410 isl_multi_aff_free(ma);
12411 isl_map_free(map);
12412 return NULL;
12415 /* Compute the preimage of the domain or range (depending on "type")
12416 * of "map" under the function represented by "ma".
12417 * In other words, plug in "ma" in the domain or range of "map".
12418 * The result is a map that lives in the same space as "map"
12419 * except that the domain or range has been replaced by
12420 * the domain space of "ma".
12422 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12423 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12425 if (!map || !ma)
12426 goto error;
12428 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12429 return map_preimage_multi_aff(map, type, ma);
12431 if (!isl_space_has_named_params(map->dim) ||
12432 !isl_space_has_named_params(ma->space))
12433 isl_die(map->ctx, isl_error_invalid,
12434 "unaligned unnamed parameters", goto error);
12435 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12436 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12438 return map_preimage_multi_aff(map, type, ma);
12439 error:
12440 isl_multi_aff_free(ma);
12441 return isl_map_free(map);
12444 /* Compute the preimage of "set" under the function represented by "ma".
12445 * In other words, plug in "ma" in "set". The result is a set
12446 * that lives in the domain space of "ma".
12448 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12449 __isl_take isl_multi_aff *ma)
12451 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12454 /* Compute the preimage of the domain of "map" under the function
12455 * represented by "ma".
12456 * In other words, plug in "ma" in the domain of "map".
12457 * The result is a map that lives in the same space as "map"
12458 * except that the domain has been replaced by the domain space of "ma".
12460 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12461 __isl_take isl_multi_aff *ma)
12463 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12466 /* Compute the preimage of the range of "map" under the function
12467 * represented by "ma".
12468 * In other words, plug in "ma" in the range of "map".
12469 * The result is a map that lives in the same space as "map"
12470 * except that the range has been replaced by the domain space of "ma".
12472 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12473 __isl_take isl_multi_aff *ma)
12475 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12478 /* Compute the preimage of "map" under the function represented by "pma".
12479 * In other words, plug in "pma" in the domain or range of "map".
12480 * The result is a map that lives in the same space as "map",
12481 * except that the space of type "type" has been replaced by
12482 * the domain space of "pma".
12484 * The parameters of "map" and "pma" are assumed to have been aligned.
12486 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12487 __isl_take isl_map *map, enum isl_dim_type type,
12488 __isl_take isl_pw_multi_aff *pma)
12490 int i;
12491 isl_map *res;
12493 if (!pma)
12494 goto error;
12496 if (pma->n == 0) {
12497 isl_pw_multi_aff_free(pma);
12498 res = isl_map_empty(isl_map_get_space(map));
12499 isl_map_free(map);
12500 return res;
12503 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12504 isl_multi_aff_copy(pma->p[0].maff));
12505 if (type == isl_dim_in)
12506 res = isl_map_intersect_domain(res,
12507 isl_map_copy(pma->p[0].set));
12508 else
12509 res = isl_map_intersect_range(res,
12510 isl_map_copy(pma->p[0].set));
12512 for (i = 1; i < pma->n; ++i) {
12513 isl_map *res_i;
12515 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12516 isl_multi_aff_copy(pma->p[i].maff));
12517 if (type == isl_dim_in)
12518 res_i = isl_map_intersect_domain(res_i,
12519 isl_map_copy(pma->p[i].set));
12520 else
12521 res_i = isl_map_intersect_range(res_i,
12522 isl_map_copy(pma->p[i].set));
12523 res = isl_map_union(res, res_i);
12526 isl_pw_multi_aff_free(pma);
12527 isl_map_free(map);
12528 return res;
12529 error:
12530 isl_pw_multi_aff_free(pma);
12531 isl_map_free(map);
12532 return NULL;
12535 /* Compute the preimage of "map" under the function represented by "pma".
12536 * In other words, plug in "pma" in the domain or range of "map".
12537 * The result is a map that lives in the same space as "map",
12538 * except that the space of type "type" has been replaced by
12539 * the domain space of "pma".
12541 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12542 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12544 if (!map || !pma)
12545 goto error;
12547 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12548 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12550 if (!isl_space_has_named_params(map->dim) ||
12551 !isl_space_has_named_params(pma->dim))
12552 isl_die(map->ctx, isl_error_invalid,
12553 "unaligned unnamed parameters", goto error);
12554 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12555 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12557 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12558 error:
12559 isl_pw_multi_aff_free(pma);
12560 return isl_map_free(map);
12563 /* Compute the preimage of "set" under the function represented by "pma".
12564 * In other words, plug in "pma" in "set". The result is a set
12565 * that lives in the domain space of "pma".
12567 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12568 __isl_take isl_pw_multi_aff *pma)
12570 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12573 /* Compute the preimage of the domain of "map" under the function
12574 * represented by "pma".
12575 * In other words, plug in "pma" in the domain of "map".
12576 * The result is a map that lives in the same space as "map",
12577 * except that domain space has been replaced by the domain space of "pma".
12579 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12580 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12582 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12585 /* Compute the preimage of the range of "map" under the function
12586 * represented by "pma".
12587 * In other words, plug in "pma" in the range of "map".
12588 * The result is a map that lives in the same space as "map",
12589 * except that range space has been replaced by the domain space of "pma".
12591 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12592 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12594 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12597 /* Compute the preimage of "map" under the function represented by "mpa".
12598 * In other words, plug in "mpa" in the domain or range of "map".
12599 * The result is a map that lives in the same space as "map",
12600 * except that the space of type "type" has been replaced by
12601 * the domain space of "mpa".
12603 * If the map does not involve any constraints that refer to the
12604 * dimensions of the substituted space, then the only possible
12605 * effect of "mpa" on the map is to map the space to a different space.
12606 * We create a separate isl_multi_aff to effectuate this change
12607 * in order to avoid spurious splitting of the map along the pieces
12608 * of "mpa".
12610 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12611 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12613 int n;
12614 isl_pw_multi_aff *pma;
12616 if (!map || !mpa)
12617 goto error;
12619 n = isl_map_dim(map, type);
12620 if (!isl_map_involves_dims(map, type, 0, n)) {
12621 isl_space *space;
12622 isl_multi_aff *ma;
12624 space = isl_multi_pw_aff_get_space(mpa);
12625 isl_multi_pw_aff_free(mpa);
12626 ma = isl_multi_aff_zero(space);
12627 return isl_map_preimage_multi_aff(map, type, ma);
12630 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12631 return isl_map_preimage_pw_multi_aff(map, type, pma);
12632 error:
12633 isl_map_free(map);
12634 isl_multi_pw_aff_free(mpa);
12635 return NULL;
12638 /* Compute the preimage of "map" under the function represented by "mpa".
12639 * In other words, plug in "mpa" in the domain "map".
12640 * The result is a map that lives in the same space as "map",
12641 * except that domain space has been replaced by the domain space of "mpa".
12643 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12644 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12646 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12649 /* Compute the preimage of "set" by the function represented by "mpa".
12650 * In other words, plug in "mpa" in "set".
12652 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12653 __isl_take isl_multi_pw_aff *mpa)
12655 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);